1/* 2 * Copyright (c) 2022 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15#ifndef BSHELL_BAS_H 16#define BSHELL_BAS_H 17#include <stdint.h> 18#include <stdlib.h> 19#include "shell.h" 20 21#define BSH_VERSION "0.0.1" 22#define BSH_KEY_LF 0x0A 23#define BSH_KEY_CR 0x0D 24#define BSH_KEY_TAB 0x09 25#define BSH_KEY_BACKSPACE 0x08 26#define BSH_KEY_DELETE 0x7F 27#define BSH_KEY_CTRLC 0x03 // ctr + c 28#define BSH_KEY_ESC 0x1B // ecs 29 30#define BSH_COMMAND_MAX_LENGTH (5 * 1024) 31#define BSH_PARAMETER_MAX_NUMBER 10 32#define BSH_CMD_NAME_END 48 33#define BSH_CMD_MAX_KEY 5 34 35#ifdef __cplusplus 36#if __cplusplus 37extern "C" { 38#endif 39#endif 40 41#ifdef STARTUP_INIT_TEST 42#define SHELLSTATIC 43#else 44#define SHELLSTATIC static 45#endif 46 47typedef enum { 48 BSH_IN_NORMAL = 0, 49 BSH_ANSI_ESC, 50 BSH_ANSI_CSI, 51} BShellState; 52 53typedef enum { 54 BSH_EXEC_TASK = 0, 55 BSH_EXEC_INDEPENDENT, 56} BShellExecMode; 57 58typedef struct BShellCommand_ { 59 char *desc; 60 char *help; 61 BShellCmdExecuter_ executer; 62 struct BShellCommand_ *next; 63 char *multikey; 64 char *multikeys[BSH_CMD_MAX_KEY]; 65 uint8_t argStart; 66 char name[0]; 67} BShellCommand; 68 69typedef struct { 70 uint8_t shellState : 2; 71 uint8_t tabFlag : 1; 72} BShellStatus; 73 74typedef struct BShellKey_ { 75 uint8_t keyCode; 76 BShellkeyHandle keyHandle; 77 struct BShellKey_ *next; 78} BShellKey; 79 80typedef struct BShellEnv_ { 81 char *prompt; 82 char buffer[BSH_COMMAND_MAX_LENGTH]; 83 uint16_t length; 84 uint16_t cursor; 85 char *args[BSH_PARAMETER_MAX_NUMBER]; 86 uint8_t argc; 87 uint8_t shellState : 3; 88 uint8_t execMode : 2; 89 BShellCommand *command; 90 BShellParam *param; 91 BShellKey *keyHandle; 92 BShellStatus status; 93 BShellInput_ input; 94 char data[BSH_COMMAND_MAX_LENGTH]; 95} BShellEnv; 96 97BShellKey *BShellEnvGetKey(BShellHandle handle, uint8_t code); 98BShellCommand *BShellEnvGetCmd(BShellHandle handle, int32_t argc, char *argv[]); 99int32_t BShellEnvOutputString(BShellHandle handle, const char *string); 100void BShellEnvOutputByte(BShellHandle handle, char data); 101void BShellEnvOutputResult(BShellHandle handle, int32_t result); 102char *BShellEnvErrString(BShellHandle handle, int32_t err); 103const char *BShellEnvGetStringParam(BShellHandle handle, const char *name); 104 105#ifdef STARTUP_INIT_TEST 106void BShellEnvProcessInput(BShellHandle handle, char data); 107BShellKey *BShellEnvGetDefaultKey(uint8_t code); 108#endif 109 110#ifdef __cplusplus 111#if __cplusplus 112} 113#endif 114#endif 115#endif