-
Notifications
You must be signed in to change notification settings - Fork 3
/
shell.c
87 lines (72 loc) · 1.74 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
#include <string.h>
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <comp421/yalnix.h>
#include <comp421/hardware.h>
main(int argc, char **argv)
{
char buf[TERMINAL_MAX_LINE];
char *cmd_argv[TERMINAL_MAX_LINE]; /* An overkill for expediency */
char *prompt = "yalnix> ";
char separators[4];
int termno;
separators[0] = ' ';
separators[1] = '\t';
separators[2] = '\n';
separators[3] = '\0';
if (argc < 2) {
TtyPrintf(TTY_CONSOLE, "usage: shell termno\n");
Exit(1);
}
termno = atoi(argv[1]);
if ((termno < 0) || (termno >= NUM_TERMINALS)) {
TtyPrintf(TTY_CONSOLE, "shell: invalid terminal number %d\n", termno);
Exit(1);
}
TtyPrintf(termno, "Starting shell....\n");
while (1)
{
int n, pid, pid2;
int status;
int j;
TtyPrintf(termno, prompt);
n = TtyRead(termno, buf, sizeof(buf));
if (n <= 0 || n >= sizeof(buf)) /* line too big */
continue;
buf[n] = '\0';
if (!(cmd_argv[0] = strtok(buf, separators)))
continue;
if (strcmp(cmd_argv[0], "exit") == 0) {
TtyPrintf(termno, "Exitting shell....\n");
Exit(0);
}
j = 1;
while ((cmd_argv[j++] = strtok(NULL, separators)) != NULL)
;
pid = Fork();
if (pid < 0) {
TtyPrintf(termno, "Cannot Fork process\n");
continue;
}
if (pid == 0) {
Exec(cmd_argv[0], cmd_argv);
TtyPrintf(termno, "Could not Exec `%s'\n", cmd_argv[0]);
Exit(1);
}
pid2 = Wait(&status);
if (pid2 < 0) {
TtyPrintf(termno, "Wait returned error!\n");
continue;
}
if (pid2 == pid) {
TtyPrintf(termno, "Pid %d exited with status %d\n",
pid2, status);
}
else {
TtyPrintf(termno, "Mystery pid %d exited with status %d\n",
pid2, status);
}
}
}