aboutsummaryrefslogtreecommitdiff
path: root/kernel/test/kshell/command.c
blob: 836b7434ef9267f210630de2e3aae36fe292f6c1 (plain)
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include "command.h"

#include "mm/kmalloc.h"

#include "util/debug.h"
#include "util/string.h"

kshell_command_t *kshell_command_create(const char *name,
                                        kshell_cmd_func_t cmd_func,
                                        const char *desc)
{
    kshell_command_t *cmd;
    size_t len;

    KASSERT(NULL != name);
    KASSERT(NULL != cmd_func);

    cmd = (kshell_command_t *)kmalloc(sizeof(kshell_command_t));
    if (NULL == cmd)
    {
        return NULL;
    }

    len = strnlen(name, KSH_CMD_NAME_LEN);
    strncpy(cmd->kc_name, name, len);
    cmd->kc_name[len] = '\0';

    cmd->kc_cmd_func = cmd_func;

    if (NULL != desc)
    {
        len = strnlen(desc, KSH_DESC_LEN);
        strncpy(cmd->kc_desc, desc, len);
        cmd->kc_desc[len] = '\0';
    }
    else
    {
        cmd->kc_desc[0] = '\0';
    }

    list_link_init(&cmd->kc_commands_link);

    return cmd;
}

void kshell_command_destroy(kshell_command_t *cmd) { kfree(cmd); }