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 #include <signal.h>
16 #include <stdio.h>
17 #include <stdlib.h>
18 
19 #include "begetctl.h"
20 #include "shell.h"
21 #include "shell_utils.h"
22 
23 static BShellHandle g_handle = NULL;
GetShellHandle(void)24 BShellHandle GetShellHandle(void)
25 {
26     if (g_handle == NULL) {
27         BShellInfo info = {PARAM_SHELL_DEFAULT_PROMPT, NULL};
28         BShellEnvInit(&g_handle, &info);
29     }
30     return g_handle;
31 }
32 
signalHandler(int signal)33 static void signalHandler(int signal)
34 {
35     demoExit();
36     exit(0);
37 }
38 
main(int argc,char * argv[])39 int main(int argc, char *argv[])
40 {
41     (void)signal(SIGINT, signalHandler);
42     const char *last = strrchr(argv[0], '/');
43     // Get the first ending command name
44     if (last != NULL) {
45         last = last + 1;
46     } else {
47         last = argv[0];
48     }
49 
50     // If it is begetctl with subcommand name, try to do subcommand first
51     int number = argc;
52     char **args = argv;
53     if ((argc > 1) && (strcmp(last, "begetctl") == 0)) {
54         number = argc - 1;
55         args = argv + 1;
56     }
57     if (number >= 1 && strcmp(args[0], "devctl") == 0) {
58         if (memcpy_s(args[0], strlen(args[0]), "reboot", strlen("reboot")) != 0) {
59             printf("Failed to copy\n");
60         }
61     }
62     BShellHandle handle = GetShellHandle();
63     if (handle == NULL) {
64         printf("Failed to get shell handle \n");
65         return 0;
66     }
67 
68     BShellParamCmdRegister(handle, 0);
69 #ifdef INIT_TEST
70     BShellCmdRegister(handle, 0);
71 #endif
72     BShellEnvDirectExecute(handle, number, args);
73     demoExit();
74     return 0;
75 }
76