• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include <signal.h>
16 #include <stdio.h>
17 #include <stdlib.h>
18 #include <termios.h>
19 
20 #include "begetctl.h"
21 #include "shell.h"
22 #include "shell_utils.h"
23 
24 static BShellHandle g_handle = NULL;
25 struct termios terminalState;
signalHandler(int signal)26 static void signalHandler(int signal)
27 {
28     tcsetattr(0, TCSAFLUSH, &terminalState);
29     demoExit();
30     exit(0);
31 }
32 
ShellInput(char * data,int32_t len)33 static int32_t ShellInput(char *data, int32_t len)
34 {
35     for (int32_t i = 0; i < len; i++) {
36         data[i] = getchar();
37     }
38     return len;
39 }
40 
GetShellHandle(void)41 BShellHandle GetShellHandle(void)
42 {
43     if (g_handle == NULL) {
44         BShellInfo info = {PARAM_SHELL_DEFAULT_PROMPT, ShellInput};
45         BShellEnvInit(&g_handle, &info);
46     }
47     return g_handle;
48 }
49 
50 #ifndef STARTUP_INIT_TEST
main(int argc,char * args[])51 int main(int argc, char *args[])
52 {
53     (void)signal(SIGINT, signalHandler);
54     (void)signal(SIGKILL, signalHandler);
55     if (tcgetattr(0, &terminalState)) {
56         return -1;
57     }
58     struct termios tio;
59     if (tcgetattr(0, &tio)) {
60         return -1;
61     }
62     tio.c_lflag &= ~(ECHO | ICANON | ISIG);
63     tio.c_cc[VTIME] = 0;
64     tio.c_cc[VMIN] = 1;
65     tcsetattr(0, TCSAFLUSH, &tio);
66 
67     BSH_LOGV("BShellEnvStart %d", argc);
68     do {
69         BShellHandle handle = GetShellHandle();
70         if (handle == NULL) {
71             printf("Failed to get shell handle \n");
72             return 0;
73         }
74         const ParamInfo *param = BShellEnvGetReservedParam(handle, PARAM_REVERESD_NAME_CURR_PARAMETER);
75         BSH_CHECK(param != NULL && param->type == PARAM_STRING, break, "Failed to get reversed param");
76         BShellEnvSetParam(handle, param->name, param->desc, param->type, (void *)"");
77         if (argc > 1) {
78             int ret = SetParamShellPrompt(handle, args[1]);
79             if (ret != 0) {
80                 break;
81             }
82         }
83         BShellParamCmdRegister(handle, 1);
84 #ifdef INIT_TEST
85         BShellCmdRegister(handle, 1);
86 #endif
87         BShellEnvStart(handle);
88         BShellEnvLoop(handle);
89     } while (0);
90     demoExit();
91     tcsetattr(0, TCSAFLUSH, &terminalState);
92     return 0;
93 }
94 #endif
95