main.c 2.1 KB
Newer Older
H
handyohos 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14
/*
 * Copyright (c) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
X
xionglei6 已提交
15
#include <signal.h>
H
handyohos 已提交
16 17 18
#include <stdio.h>
#include <stdlib.h>

X
xionglei6 已提交
19 20 21 22
#include "begetctl.h"
#include "shell.h"
#include "shell_utils.h"
#include "sys_param.h"
H
handyohos 已提交
23

X
xionglei6 已提交
24 25
static BShellHandle g_handle = NULL;
static int32_t ShellOuput(const char *data, int32_t len)
H
handyohos 已提交
26
{
X
xionglei6 已提交
27 28
    for (int32_t i = 0; i < len; i++) {
        putchar(*(data + i));
H
handyohos 已提交
29
    }
X
xionglei6 已提交
30 31
    (void)fflush(stdout);
    return len;
H
handyohos 已提交
32 33
}

X
xionglei6 已提交
34
BShellHandle GetShellHandle(void)
H
handyohos 已提交
35
{
X
xionglei6 已提交
36 37 38
    if (g_handle == NULL) {
        BShellInfo info = {PARAM_SHELL_DEFAULT_PROMPT, NULL, ShellOuput};
        BShellEnvInit(&g_handle, &info);
H
handyohos 已提交
39
    }
X
xionglei6 已提交
40
    return g_handle;
H
handyohos 已提交
41 42
}

X
xionglei6 已提交
43
static void signalHandler(int signal)
H
handyohos 已提交
44
{
X
xionglei6 已提交
45 46
    demoExit();
    exit(0);
H
handyohos 已提交
47 48
}

X
xionglei6 已提交
49
int main(int argc, char *argv[])
H
handyohos 已提交
50
{
X
xionglei6 已提交
51
    (void)signal(SIGINT, signalHandler);
H
handyohos 已提交
52 53 54 55 56 57 58 59 60
    const char *last = strrchr(argv[0], '/');
    // Get the first ending command name
    if (last != NULL) {
        last = last + 1;
    } else {
        last = argv[0];
    }

    // If it is begetctl with subcommand name, try to do subcommand first
X
xionglei6 已提交
61 62
    int number = argc;
    char **args = argv;
H
handyohos 已提交
63
    if ((argc > 1) && (strcmp(last, "begetctl") == 0)) {
X
xionglei6 已提交
64 65
        number = argc - 1;
        args = argv + 1;
H
handyohos 已提交
66
    }
X
xionglei6 已提交
67
    if (number >= 1 && strcmp(args[0], "devctl") == 0) {
X
xionglei6 已提交
68 69 70
        if (memcpy_s(args[0], strlen(args[0]), "reboot", strlen("reboot")) != 0) {
            printf("Failed to copy\n");
        }
X
xionglei6 已提交
71
    }
X
xionglei6 已提交
72 73
    SetInitLogLevel(0);
    BShellParamCmdRegister(g_handle, 0);
X
xionglei6 已提交
74 75 76
#ifdef INIT_TEST
    BShellCmdRegister(g_handle, 0);
#endif
X
xionglei6 已提交
77 78 79
    BShellEnvDirectExecute(g_handle, number, args);
    demoExit();
    return 0;
H
handyohos 已提交
80
}