-
Notifications
You must be signed in to change notification settings - Fork 0
/
shell.c
315 lines (258 loc) · 7.64 KB
/
shell.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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
/* shell.c - shell */
#include <xinu.h>
#include <stdio.h>
#include "shprototypes.h"
/************************************************************************/
/* Table of Xinu shell commands and the function associated with each */
/************************************************************************/
const struct cmdent cmdtab[] = {
{"argecho", TRUE, xsh_argecho},
{"arp", FALSE, xsh_arp},
{"cat", FALSE, xsh_cat},
{"clear", TRUE, xsh_clear},
{"date", FALSE, xsh_date},
{"devdump", FALSE, xsh_devdump},
{"echo", FALSE, xsh_echo},
{"exit", TRUE, xsh_exit},
{"help", FALSE, xsh_help},
{"ipaddr", FALSE, xsh_ipaddr},
{"kill", TRUE, xsh_kill},
{"memdump", FALSE, xsh_memdump},
{"memstat", FALSE, xsh_memstat},
{"ping", FALSE, xsh_ping},
{"ps", FALSE, xsh_ps},
{"sleep", FALSE, xsh_sleep},
{"udp", FALSE, xsh_udpdump},
{"udpecho", FALSE, xsh_udpecho},
{"udpeserver", FALSE, xsh_udpeserver},
{"uptime", FALSE, xsh_uptime},
{"hello", FALSE, xsh_hello},
{"?", FALSE, xsh_help}
};
uint32 ncmd = sizeof(cmdtab) / sizeof(struct cmdent);
/************************************************************************/
/* shell - Provide an interactive user interface that executes */
/* commands. Each command begins with a command name, has */
/* a set of optional arguments, has optional input or */
/* output redirection, and an optional specification for */
/* background execution (ampersand). The syntax is: */
/* */
/* command_name [args*] [redirection] [&] */
/* */
/* Redirection is either or both of: */
/* */
/* < input_file */
/* or */
/* > output_file */
/* */
/************************************************************************/
process shell (
did32 dev /* ID of tty device from which */
) /* to accept commands */
{
char buf[SHELL_BUFLEN]; /* Input line (large enough for */
/* one line from a tty device */
int32 len; /* Length of line read */
char tokbuf[SHELL_BUFLEN + /* Buffer to hold a set of */
SHELL_MAXTOK]; /* Contiguous null-terminated */
/* Strings of tokens */
int32 tlen; /* Current length of all data */
/* in array tokbuf */
int32 tok[SHELL_MAXTOK]; /* Index of each token in */
/* array tokbuf */
int32 toktyp[SHELL_MAXTOK]; /* Type of each token in tokbuf */
int32 ntok; /* Number of tokens on line */
pid32 child; /* Process ID of spawned child */
bool8 backgnd; /* Run command in background? */
char *outname, *inname; /* Pointers to strings for file */
/* names that follow > and < */
did32 stdinput, stdoutput; /* Descriptors for redirected */
/* input and output */
int32 i; /* Index into array of tokens */
int32 j; /* Index into array of commands */
int32 msg; /* Message from receive() for */
/* child termination */
int32 tmparg; /* Address of this var is used */
/* when first creating child */
/* process, but is replaced */
char *src, *cmp; /* Pointers used during name */
/* comparison */
bool8 diff; /* Was difference found during */
/* comparison */
char *args[SHELL_MAXTOK]; /* Argument vector passed to */
/* builtin commands */
/* Print shell banner and startup message */
fprintf(dev, "\n\n%s%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n",
SHELL_BAN0,SHELL_BAN1,SHELL_BAN2,SHELL_BAN3,SHELL_BAN4,
SHELL_BAN5,SHELL_BAN6,SHELL_BAN7,SHELL_BAN8,SHELL_BAN9);
fprintf(dev, "%s\n\n", SHELL_STRTMSG);
/* Continually prompt the user, read input, and execute command */
while (TRUE) {
/* Display prompt */
fprintf(dev, SHELL_PROMPT);
/* Read a command */
len = read(dev, buf, sizeof(buf));
/* Exit gracefully on end-of-file */
if (len == EOF) {
break;
}
/* If line contains only NEWLINE, go to next line */
if (len <= 1) {
continue;
}
buf[len] = SH_NEWLINE; /* terminate line */
/* Parse input line and divide into tokens */
ntok = lexan(buf, len, tokbuf, &tlen, tok, toktyp);
/* Handle parsing error */
if (ntok == SYSERR) {
fprintf(dev,"%s\n", SHELL_SYNERRMSG);
continue;
}
/* If line is empty, go to next input line */
if (ntok == 0) {
fprintf(dev, "\n");
continue;
}
/* If last token is '&', set background */
if (toktyp[ntok-1] == SH_TOK_AMPER) {
ntok-- ;
tlen-= 2;
backgnd = TRUE;
} else {
backgnd = FALSE;
}
/* Check for input/output redirection (default is none) */
outname = inname = NULL;
if ( (ntok >=3) && ( (toktyp[ntok-2] == SH_TOK_LESS)
||(toktyp[ntok-2] == SH_TOK_GREATER))){
if (toktyp[ntok-1] != SH_TOK_OTHER) {
fprintf(dev,"%s\n", SHELL_SYNERRMSG);
continue;
}
if (toktyp[ntok-2] == SH_TOK_LESS) {
inname = &tokbuf[tok[ntok-1]];
} else {
outname = &tokbuf[tok[ntok-1]];
}
ntok -= 2;
tlen = tok[ntok];
}
if ( (ntok >=3) && ( (toktyp[ntok-2] == SH_TOK_LESS)
||(toktyp[ntok-2] == SH_TOK_GREATER))){
if (toktyp[ntok-1] != SH_TOK_OTHER) {
fprintf(dev,"%s\n", SHELL_SYNERRMSG);
continue;
}
if (toktyp[ntok-2] == SH_TOK_LESS) {
if (inname != NULL) {
fprintf(dev,"%s\n", SHELL_SYNERRMSG);
continue;
}
inname = &tokbuf[tok[ntok-1]];
} else {
if (outname != NULL) {
fprintf(dev,"%s\n", SHELL_SYNERRMSG);
continue;
}
outname = &tokbuf[tok[ntok-1]];
}
ntok -= 2;
tlen = tok[ntok];
}
/* Verify remaining tokens are type "other" */
for (i=0; i<ntok; i++) {
if (toktyp[i] != SH_TOK_OTHER) {
break;
}
}
if ((ntok == 0) || (i < ntok)) {
fprintf(dev, SHELL_SYNERRMSG);
continue;
}
stdinput = stdoutput = dev;
/* Lookup first token in the command table */
for (j = 0; j < ncmd; j++) {
src = cmdtab[j].cname;
cmp = tokbuf;
diff = FALSE;
while (*src != NULLCH) {
if (*cmp != *src) {
diff = TRUE;
break;
}
src++;
cmp++;
}
if (diff || (*cmp != NULLCH)) {
continue;
} else {
break;
}
}
/* Handle command not found */
if (j >= ncmd) {
fprintf(dev, "command %s not found\n", tokbuf);
continue;
}
/* Handle built-in command */
if (cmdtab[j].cbuiltin) { /* No background or redirect. */
if (inname != NULL || outname != NULL || backgnd){
fprintf(dev, SHELL_BGERRMSG);
continue;
} else {
/* Set up arg vector for call */
for (i=0; i<ntok; i++) {
args[i] = &tokbuf[tok[i]];
}
/* Call builtin shell function */
if ((*cmdtab[j].cfunc)(ntok, args)
== SHELL_EXIT) {
break;
}
}
continue;
}
/* Open files and redirect I/O if specified */
if (inname != NULL) {
stdinput = open(NAMESPACE,inname,"ro");
if (stdinput == SYSERR) {
fprintf(dev, SHELL_INERRMSG, inname);
continue;
}
}
if (outname != NULL) {
stdoutput = open(NAMESPACE,outname,"w");
if (stdoutput == SYSERR) {
fprintf(dev, SHELL_OUTERRMSG, outname);
continue;
} else {
control(stdoutput, F_CTL_TRUNC, 0, 0);
}
}
/* Spawn child thread for non-built-in commands */
child = create(cmdtab[j].cfunc,
SHELL_CMDSTK, SHELL_CMDPRIO,
cmdtab[j].cname, 2, ntok, &tmparg);
/* If creation or argument copy fails, report error */
if ((child == SYSERR) ||
(addargs(child, ntok, tok, tlen, tokbuf, &tmparg)
== SYSERR) ) {
fprintf(dev, SHELL_CREATMSG);
continue;
}
/* Set stdinput and stdoutput in child to redirect I/O */
proctab[child].prdesc[0] = stdinput;
proctab[child].prdesc[1] = stdoutput;
msg = recvclr();
resume(child);
if (! backgnd) {
msg = receive();
while (msg != child) {
msg = receive();
}
}
}
/* Terminate the shell process by returning from the top level */
fprintf(dev,SHELL_EXITMSG);
return OK;
}