-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshell.c
178 lines (153 loc) · 4.33 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
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <signal.h>
#include <string.h>
#include <assert.h>
#include <errno.h>
#include "proc-common.h"
#include "request.h"
#define SHELL_CMDLINE_SZ 100
void issue_request(int wfd, int rfd, struct request_struct *rq)
{
int ret;
/* Issue the request */
fprintf(stderr, "Shell: issuing request...\n");
if (write(wfd, rq, sizeof(*rq)) != sizeof(*rq)) {
perror("Shell: write request struct");
exit(1);
}
/* Block until a reply has been received */
fprintf(stderr, "Shell: receiving request return value...\n");
if (read(rfd, &ret, sizeof(ret)) != sizeof(ret)) {
perror("Shell: read request return value");
exit(1);
}
if(ret < 0){
fprintf(stderr, "Shell: request return value ret = %d\n", ret);
fprintf(stderr, " %s\n", strerror(-ret));
}
}
/*
* Read a command line from the stream pointed to by fp
* [a standard C library stream, *not* a file descriptor],
* strip any trailing newlines.
*/
void get_cmdline(FILE *fp, char *buf, int bufsz)
{
if (fgets(buf, bufsz, fp) == NULL) {
fprintf(stderr, "Shell: could not read command line, exiting.\n");
exit(1);
}
if (buf[strlen(buf) - 1] == '\n')
buf[strlen(buf) - 1] = '\0';
}
/* print help */
void help(void)
{
printf(" ? : print help\n"
" q : quit\n"
" p : print tasks\n"
" k <id> : kill task identified by id\n"
" e <program>: execute program\n"
" h <id> : set task identified by id to high priority\n"
" l <id> : set task identified by id to low priority\n");
}
/*
* Parse a command line, construct and
* issue the relevant request to the scheduler.
*
* Parsing is very simple, a better way would be to
* break up the command line in tokens.
*/
void process_cmdline(char *cmdline, int wfd, int rfd)
{
struct request_struct rq;
if (strlen(cmdline) == 0 || strcmp(cmdline, "?") == 0){
help();
return;
}
/* Quit */
if (strcmp(cmdline, "q") == 0 || strcmp(cmdline, "Q") == 0) {
fprintf(stderr, "Shell: Exiting. Goodbye.\n");
rq.request_no = REQ_KILL_SHELL;
issue_request(wfd, rfd, &rq);
return;
}
/* Print Tasks */
if (strcmp(cmdline, "p") == 0 || strcmp(cmdline, "P") == 0) {
rq.request_no = REQ_PRINT_TASKS;
issue_request(wfd, rfd, &rq);
return;
}
/* Kill Task */
if ((cmdline[0] == 'k' || cmdline[0] == 'K') &&
cmdline[1] == ' ') {
rq.request_no = REQ_KILL_TASK;
rq.task_arg = atoi(&cmdline[2]);
issue_request(wfd, rfd, &rq);
return;
}
/* Exec Task */
if ((cmdline[0] == 'e' || cmdline[0] == 'E') && cmdline[1] == ' ') {
rq.request_no = REQ_EXEC_TASK;
strncpy(rq.exec_task_arg, &cmdline[2], EXEC_TASK_NAME_SZ);
rq.exec_task_arg[EXEC_TASK_NAME_SZ - 1] = '\0';
issue_request(wfd, rfd, &rq);
return;
}
/* High-prioritize task */
if ((cmdline[0] == 'h' || cmdline[0] == 'h') && cmdline[1] == ' ') {
rq.request_no = REQ_HIGH_TASK;
rq.task_arg = atoi(&cmdline[2]);
issue_request(wfd, rfd, &rq);
return;
}
/* Low-prioritize task */
if ((cmdline[0] == 'l' || cmdline[0] == 'L') && cmdline[1] == ' ') {
rq.request_no = REQ_LOW_TASK;
rq.task_arg = atoi(&cmdline[2]);
issue_request(wfd, rfd, &rq);
return;
}
/* Parse error, malformed command, whatever... */
printf("command `%s': Bad Command.\n", cmdline);
}
int main(int argc, char *argv[])
{
int rfd, wfd;
char cmdline[SHELL_CMDLINE_SZ];
/*
* Communication with the scheduler happens over two UNIX pipes.
*
* The scheduler first creates the pipes, then execve()s the shell
* program. It passes two file descriptors as command-line arguments:
*
* argument 1: wfd: the file descriptor to write request structures into.
* argument 2: rfd: the file descriptor to read request return values from.
*/
if (argc != 3) {
fprintf(stderr, "Shell: must be called with exactly two arguments.\n");
exit(1);
}
wfd = atoi(argv[1]);
rfd = atoi(argv[2]);
if (!wfd || !rfd) {
fprintf(stderr, "Shell: descriptors must be non-zero: wfd = %d, rfd = %d\n",
wfd, rfd);
exit(1);
}
/*
* Forever: show the prompt, read a command line, then process it.
*/
printf("\nThis is the Shell. Welcome.\n\n");
for (;;) {
printf("Shell> ");
fflush(stdout);
get_cmdline(stdin, cmdline, SHELL_CMDLINE_SZ);
process_cmdline(cmdline, wfd, rfd);
}
/* Unreachable */
fprintf(stderr, "Shell: reached unreachable point.\n");
return 1;
}