-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcmd.c
executable file
·53 lines (42 loc) · 985 Bytes
/
cmd.c
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
47
48
49
50
51
52
53
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include "cmd.h"
struct list_head cmd_list;
void cmd_init(void)
{
INIT_LIST_HEAD(&cmd_list);
}
void cmd_add(const char *cmd, command_func func, void *data)
{
struct command *tmp;
tmp = malloc(sizeof(struct command));
tmp->cmd = cmd;
tmp->data = data;
tmp->func = func;
list_add_tail(&(tmp->list), &cmd_list);
}
void cmd_del(char *cmd)
{
struct list_head *tmp;
struct command *comm;
list_for_each(tmp, &cmd_list) {
if(NULL == tmp)
return;
comm = (struct command *)list_entry(tmp, struct command, list);
if (NULL != comm && !strcmp(comm->cmd, cmd)) {
list_del(&(comm->list));
free(comm);
}
}
}
void cmd_dispatch(char *cmd)
{
struct list_head *tmp;
struct command *comm;
list_for_each(tmp, &cmd_list) {
comm = (struct command *)list_entry(tmp, struct command, list);
if (!strcmp(comm->cmd, cmd))
comm->func(comm->data);
}
}