1d9f0492fSopenharmony_ci/*
2d9f0492fSopenharmony_ci * Copyright (c) 2021 Huawei Device Co., Ltd.
3d9f0492fSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4d9f0492fSopenharmony_ci * you may not use this file except in compliance with the License.
5d9f0492fSopenharmony_ci * You may obtain a copy of the License at
6d9f0492fSopenharmony_ci *
7d9f0492fSopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8d9f0492fSopenharmony_ci *
9d9f0492fSopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10d9f0492fSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11d9f0492fSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12d9f0492fSopenharmony_ci * See the License for the specific language governing permissions and
13d9f0492fSopenharmony_ci * limitations under the License.
14d9f0492fSopenharmony_ci */
15d9f0492fSopenharmony_ci#include <signal.h>
16d9f0492fSopenharmony_ci#include <stdio.h>
17d9f0492fSopenharmony_ci#include <stdlib.h>
18d9f0492fSopenharmony_ci
19d9f0492fSopenharmony_ci#include "begetctl.h"
20d9f0492fSopenharmony_ci#include "shell.h"
21d9f0492fSopenharmony_ci#include "shell_utils.h"
22d9f0492fSopenharmony_ci
23d9f0492fSopenharmony_cistatic BShellHandle g_handle = NULL;
24d9f0492fSopenharmony_ciBShellHandle GetShellHandle(void)
25d9f0492fSopenharmony_ci{
26d9f0492fSopenharmony_ci    if (g_handle == NULL) {
27d9f0492fSopenharmony_ci        BShellInfo info = {PARAM_SHELL_DEFAULT_PROMPT, NULL};
28d9f0492fSopenharmony_ci        BShellEnvInit(&g_handle, &info);
29d9f0492fSopenharmony_ci    }
30d9f0492fSopenharmony_ci    return g_handle;
31d9f0492fSopenharmony_ci}
32d9f0492fSopenharmony_ci
33d9f0492fSopenharmony_cistatic void signalHandler(int signal)
34d9f0492fSopenharmony_ci{
35d9f0492fSopenharmony_ci    demoExit();
36d9f0492fSopenharmony_ci    exit(0);
37d9f0492fSopenharmony_ci}
38d9f0492fSopenharmony_ci
39d9f0492fSopenharmony_ciint main(int argc, char *argv[])
40d9f0492fSopenharmony_ci{
41d9f0492fSopenharmony_ci    (void)signal(SIGINT, signalHandler);
42d9f0492fSopenharmony_ci    const char *last = strrchr(argv[0], '/');
43d9f0492fSopenharmony_ci    // Get the first ending command name
44d9f0492fSopenharmony_ci    if (last != NULL) {
45d9f0492fSopenharmony_ci        last = last + 1;
46d9f0492fSopenharmony_ci    } else {
47d9f0492fSopenharmony_ci        last = argv[0];
48d9f0492fSopenharmony_ci    }
49d9f0492fSopenharmony_ci
50d9f0492fSopenharmony_ci    // If it is begetctl with subcommand name, try to do subcommand first
51d9f0492fSopenharmony_ci    int number = argc;
52d9f0492fSopenharmony_ci    char **args = argv;
53d9f0492fSopenharmony_ci    if ((argc > 1) && (strcmp(last, "begetctl") == 0)) {
54d9f0492fSopenharmony_ci        number = argc - 1;
55d9f0492fSopenharmony_ci        args = argv + 1;
56d9f0492fSopenharmony_ci    }
57d9f0492fSopenharmony_ci    if (number >= 1 && strcmp(args[0], "devctl") == 0) {
58d9f0492fSopenharmony_ci        if (memcpy_s(args[0], strlen(args[0]), "reboot", strlen("reboot")) != 0) {
59d9f0492fSopenharmony_ci            printf("Failed to copy\n");
60d9f0492fSopenharmony_ci        }
61d9f0492fSopenharmony_ci    }
62d9f0492fSopenharmony_ci    BShellHandle handle = GetShellHandle();
63d9f0492fSopenharmony_ci    if (handle == NULL) {
64d9f0492fSopenharmony_ci        printf("Failed to get shell handle \n");
65d9f0492fSopenharmony_ci        return 0;
66d9f0492fSopenharmony_ci    }
67d9f0492fSopenharmony_ci
68d9f0492fSopenharmony_ci    BShellParamCmdRegister(handle, 0);
69d9f0492fSopenharmony_ci#ifdef INIT_TEST
70d9f0492fSopenharmony_ci    BShellCmdRegister(handle, 0);
71d9f0492fSopenharmony_ci#endif
72d9f0492fSopenharmony_ci    BShellEnvDirectExecute(handle, number, args);
73d9f0492fSopenharmony_ci    demoExit();
74d9f0492fSopenharmony_ci    return 0;
75d9f0492fSopenharmony_ci}
76