-
Notifications
You must be signed in to change notification settings - Fork 0
/
menu_string.c
69 lines (60 loc) · 989 Bytes
/
menu_string.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#include<stdio.h>
#define DESC_LEN 50
#define CMD_NUM 10
#define CMD_MAX_LEN 50
int Help();
/* Data Structure Definition */
typedef struct DataNode
{
char* cmd;
char* descpt;
char (*handler)();
struct DataNode* next;
} tDataNode;
/* Init static Data */
static tDataNode head[] =
{
{ "help", "This is help command!", Help, &head[1] },
{ "version", "This is version command!", NULL, NULL }
};
int main()
{
tDataNode *p = NULL;
/* Command Line begins */
while(1)
{
char cmd[CMD_MAX_LEN];
printf("Input a cmd >");
scanf("%s", cmd);
p = head;
while(p != NULL)
{
if(!strcmp(cmd, p->cmd))
{
if(p->handler != NULL)
{
p->handler();
}
printf("%s\n", p->descpt);
break;
}
p = p->next;
}
if(p == NULL)
{
printf("This is a wrong command!\n");
}
}
return 0;
}
int Help()
{
printf("Menu List:\n");
tDataNode* p = head;
while(p != NULL)
{
printf("%s--%s\n", p->cmd, p->descpt);
p = p->next;
}
return 0;
}