1/*
2 * Copyright (c) 2021 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_H_
16#define _BSHELL_H_
17#include <stdint.h>
18#include <stdio.h>
19#include <stdlib.h>
20
21#ifdef __cplusplus
22#if __cplusplus
23extern "C" {
24#endif
25#endif
26
27#define PARAM_REVERESD_NAME_CURR_PARAMETER "_current_param_"
28#define PARAM_SHELL_DEFAULT_PROMPT "param#"
29
30typedef struct BShellEnv_ *BShellHandle;
31typedef int32_t (*BShellCmdExecuter_)(BShellHandle handle, int32_t argc, char *argv[]);
32typedef int32_t (*BShellInput_)(char *, int32_t);
33typedef int32_t (*BShellOutput_)(const char *, int32_t);
34typedef int32_t (*BShellkeyHandle)(BShellHandle, uint8_t code);
35
36typedef enum {
37    BSH_ERRNO_BASE = 1000,
38    BSH_SHELL_INFO,
39    BSH_INVALID_PARAM,
40    BSH_CMD_TOO_LONG,
41    BSH_SHOW_CMD_LIST,
42    BSH_CMD_NOT_EXIST,
43    BSH_CMD_PARAM_INVALID,
44    BSH_SYSTEM_ERR,
45} BShellErrNo;
46
47typedef struct BShellErrInfo_ {
48    BShellErrNo err;
49    char *desc;
50} BShellErrInfo;
51
52typedef struct CmdInfo_ {
53    char *name;
54    BShellCmdExecuter_ executer;
55    char *desc;
56    char *help;
57    char *multikey;
58} CmdInfo;
59
60typedef enum {
61    PARAM_INT8 = 0,
62    PARAM_INT16,
63    PARAM_INT32,
64    PARAM_STRING,
65} BShellParamType;
66
67typedef struct ParamInfo_ {
68    char *name;
69    char *desc;
70    BShellParamType type;
71} ParamInfo;
72
73typedef struct BShellParam_ {
74    char *desc;
75    BShellParamType type;
76    union {
77        char data;
78        uint8_t data8;
79        uint16_t data16;
80        uint32_t data32;
81        char *string;
82    } value;
83    struct BShellParam_ *next;
84    char name[0];
85} BShellParam;
86
87typedef struct BShellInfo_ {
88    char *prompt;
89    BShellInput_ input;
90} BShellInfo;
91
92int BShellEnvInit(BShellHandle *handle, const BShellInfo *info);
93int BShellEnvStart(BShellHandle handle);
94void BShellEnvDestory(BShellHandle handle);
95
96int BShellEnvRegisterCmd(BShellHandle handle, const CmdInfo *cmdInfo);
97int BShellEnvSetParam(BShellHandle handle, const char *name, const char *desc, BShellParamType type, void *value);
98const BShellParam *BShellEnvGetParam(BShellHandle handle, const char *name);
99int BShellEnvRegisterKeyHandle(BShellHandle handle, uint8_t code, BShellkeyHandle keyHandle);
100void BShellEnvLoop(BShellHandle handle);
101const ParamInfo *BShellEnvGetReservedParam(BShellHandle handle, const char *name);
102
103int32_t BShellEnvOutput(BShellHandle handle, char *fmt, ...);
104int32_t BShellEnvOutputString(BShellHandle handle, const char *string);
105int32_t BShellEnvOutputPrompt(BShellHandle handle, const char *prompt);
106
107int32_t BShellCmdHelp(BShellHandle handle, int32_t argc, char *argv[]);
108int32_t BShellEnvDirectExecute(BShellHandle handle, int argc, char *args[]);
109#ifdef __cplusplus
110#if __cplusplus
111}
112#endif
113#endif
114#endif