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