main.c 2.5 KB
Newer Older
H
handyohos 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
/*
 * 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.
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "begetctl.h"

struct CMD_LIST_ST {
    struct CMD_LIST_ST *next;
    const char *name;
    BegetCtlCmdPtr cmd;
};

static struct CMD_LIST_ST *m_cmdList = NULL;

H
handyohos 已提交
29 30
int BegetCtlCmdAdd(const char *name, BegetCtlCmdPtr cmd)
{
H
handyohos 已提交
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
    struct CMD_LIST_ST *item;

    if (name == NULL) {
        return -1;
    }

    item = (struct CMD_LIST_ST *)malloc(sizeof(struct CMD_LIST_ST));
    if (item == NULL) {
        return -1;
    }

    item->name = strdup(name);
    if (item->name == NULL) {
        free((void *)item);
        return -1;
    }
    item->cmd = cmd;

    item->next = m_cmdList;
    m_cmdList = item;

    return 0;
}

H
handyohos 已提交
55 56
static const struct CMD_LIST_ST *BegetCtlCmdFind(const char *name)
{
H
handyohos 已提交
57 58 59 60 61 62 63 64 65 66 67
    const struct CMD_LIST_ST *item = m_cmdList;

    while (item != NULL) {
        if (strcmp(item->name, name) == 0) {
            return item;
        }
        item = item->next;
    }
    return NULL;
}

H
handyohos 已提交
68 69
static void BegetCtlUsage(const char *command)
{
H
handyohos 已提交
70 71 72 73 74 75 76 77 78 79 80 81 82 83
    const struct CMD_LIST_ST *item = m_cmdList;
    int notFirst = 0;

    while (item != NULL) {
        if (notFirst) {
            printf(" ");
        }
        notFirst = 1;
        printf("%s", item->name);
        item = item->next;
    }
    printf("\n");
}

H
handyohos 已提交
84 85
int main(int argc, char **argv)
{
H
handyohos 已提交
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
    const struct CMD_LIST_ST *cmd;
    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
    if ((argc > 1) && (strcmp(last, "begetctl") == 0)) {
        argc = argc - 1;
        argv = argv + 1;
        last = argv[0];
    }

    // Match the command
    cmd = BegetCtlCmdFind(last);
    if (cmd == NULL) {
        BegetCtlUsage(last);
        return 0;
    }

    // Do command
    return cmd->cmd(argc, argv);
}