-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmenu_final.c
203 lines (178 loc) · 4.78 KB
/
menu_final.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
/**************************************************************************************************/
/* Copyright (C) software design, SSE@USTC, 2014-2015 */
/* */
/* FILE NAME : menu_final.c */
/* PRINCIPAL AUTHOR : zhugexubin */
/* SUBSYSTEM NAME : menu */
/* MODULE NAME : menu */
/* LANGUAGE : C */
/* TARGET ENVIRONMENT : ANY */
/* DATE OF FIRST RELEASE : 2014/09/09 */
/* DESCRIPTION : This is a menu program */
/**************************************************************************************************/
/*
* Revision log:
*
* Created by Zhugexubin, 2014/09/12
*
* Modularity
*/
/*************** Attention: This program should compile and run in Linux System *******************/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#define NUM 1024
#define LNUM 50
#define SIZE 50
#define DESC_LEN 50
#define CMD_NUM 10
#define CMD_MAX_LEN 50
#define CMD_PATH_ONE "/user/bin/"
#define CMD_PATH_TWO "/bin/"
/* Data Structure Definition */
typedef struct DataNode
{
char* cmd;
char* descpt;
int (*handler)(void* cmd);
struct DataNode* next;
} tDataNode;
/* Function Declaration */
int Help();
int Exit();
int mystrtok(char *argv[], char* string);
int Handler(void* cmd);
tDataNode* FindCmd(tDataNode* p, char* cmd);
int ShowAllCmd(tDataNode* p);
/* Init static CMD Data */
static tDataNode head[] =
{
{ "help", "This is help command!", Help, &head[1] },
{ "version", "version 1.1.2", NULL, &head[2] },
{ "ls", "This is ls cmd:", Handler, &head[3] },
{ "exit", "Exit the Command Line", Exit, &head[4] },
{ "ps", "This is ps cmd:", Handler, &head[5] },
{ "ps -e", "This is ps -e cmd:", Handler, NULL },
};
/* Execute Cmd Line */
void BeginCmdLine()
{
tDataNode *p = NULL;
while(1)
{
char cmd[CMD_MAX_LEN];
printf("Input a cmd >");
gets(cmd);
p = FindCmd(head, cmd);
if(p != NULL)
{
printf("%s\n", p->descpt);
if(p->handler != NULL)
{
p->handler(cmd);
}
}
if(p == NULL)
{
printf("This is a wrong command!\n");
}
}
}
int main()
{
/* Command Line begins */
BeginCmdLine();
return 0;
}
/* Find Command */
tDataNode* FindCmd(tDataNode* p, char* cmd)
{
while(p != NULL)
{
if(!strcmp(cmd, p->cmd))
{
return p;
}
p = p->next;
}
}
/* Show All Command */
int ShowAllCmd(tDataNode* p)
{
printf("Menu List:\n");
while(p != NULL)
{
printf("\t%s--%s\n", p->cmd, p->descpt);
p = p->next;
}
return 0;
}
/* Help Handler */
int Help()
{
ShowAllCmd(head);
return 0;
}
/* Exit Handler */
int Exit()
{
exit(0);
}
/* Split Command String */
int mystrtok(char *argv[], char* string)
{
int i=0;
char delim[]=" ";
char *p;
argv[0] = strtok(string,delim);
while(argv[i]!=NULL)
{
argv[++i] = strtok(NULL,delim);
}
return 0;
}
/* Universal Handler */
int Handler(void* cmd)
{
pid_t pid;
int n;
char* argv[NUM];
char realCmdOne[CMD_MAX_LEN];
char realCmdTwo[CMD_MAX_LEN];
char agmt1[LNUM];
char agmt2[LNUM];
strcpy(realCmdOne, CMD_PATH_ONE);
strcpy(realCmdTwo, CMD_PATH_TWO);
pid = fork();
mystrtok(argv, cmd);
n = strlen((const char *)argv)/4;
switch(n)
{
case 2: strcpy(agmt2, argv[1]);
case 1: strcpy(agmt1, argv[0]);break;
default: break;
}
strcat(realCmdOne, cmd);
strcat(realCmdTwo, cmd);
if(pid==-1)
{
printf("fork failure!\n");
}
else if(pid==0)
{
switch(n)
{
case 2: execl(realCmdOne, agmt1, agmt2, NULL, NULL);
execl(realCmdTwo, agmt1, agmt2, NULL, NULL);break;
case 1: execl(realCmdOne, agmt1, NULL, NULL, NULL);
execl(realCmdTwo, agmt1, NULL, NULL, NULL); break;
default: break;
}
}
else
{
wait();
}
}