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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
|
/*
* tsh - A tiny shell program with job control
*
* <Put your name and login ID here>
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <ctype.h>
#include <signal.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <errno.h>
#include <iostream>
#include <vector>
#include <string>
#include "util.h"
#include "job.h"
#include "parse.h"
#include "handler.h"
using namespace std;
using namespace ov4;
/* Job states */
#define UNDEF 0 /* undefined */
#define FG 1 /* running in foreground */
#define BG 2 /* running in background */
#define ST 3 /* stopped */
/*
* Jobs states: FG (foreground), BG (background), ST (stopped)
* Job state transitions and enabling actions:
* FG -> ST : ctrl-z
* ST -> FG : fg command
* ST -> BG : bg command
* BG -> FG : fg command
* At most 1 job can be in the FG state.
*/
/* Global variables */
char prompt[] = "$ "; /* command line prompt (DO NOT CHANGE) */
char sbuf[MAXLINE]; /* for composing sprintf messages */
vector<string> builtin_cmd_list = {"quit", "fg", "bg", "jobs"};
/* End global variables */
/* Function prototypes */
/* Here are the functions that you will implement */
void eval(char *cmdline);
int builtin_cmd(char *c_str);
void do_bgfg(char **argv);
void waitfg(pid_t pid);
void usage(void);
void unix_error(char *msg);
void app_error(char *msg);
typedef void handler_t(int);
handler_t *Signal(int signum, handler_t *handler);
// built-in commands
void builtin_command_quit();
/*
* main - The shell's main routine
*/
int main(int argc, char **argv)
{
init();
char c;
char cmdline[MAXLINE];
int emit_prompt = 1; /* emit prompt (default) */
/* Redirect stderr to stdout (so that driver will get all output
* on the pipe connected to stdout) */
dup2(1, 2);
/* Parse the command line */
while ((c = getopt(argc, argv, "hvp")) != EOF) {
switch (c) {
case 'h': /* print help message */
usage();
break;
case 'v': /* emit additional diagnostic info */
verbose = true;
break;
case 'p': /* don't print a prompt */
emit_prompt = 0; /* handy for automatic testing */
break;
default:
usage();
}
}
/* Install the signal handlers */
/* These are the ones you will need to implement */
Signal(SIGINT, sigint_handler); /* ctrl-c */
Signal(SIGTSTP, sigtstp_handler); /* ctrl-z */
Signal(SIGCHLD, sigchld_handler); /* Terminated or stopped child */
/* This one provides a clean way to kill the shell */
Signal(SIGQUIT, sigquit_handler);
/* Initialize the job list */
initjobs(jobs);
/* Execute the shell's read/eval loop */
while (1) {
/* Read command line */
if (emit_prompt) {
printf("%s", prompt);
fflush(stdout);
}
if ((fgets(cmdline, MAXLINE, stdin) == NULL) && ferror(stdin))
app_error("fgets error");
if (feof(stdin)) { /* End of file (ctrl-d) */
fflush(stdout);
exit(0);
}
/* Evaluate the command line */
eval(cmdline);
fflush(stdout);
fflush(stdout);
}
exit(0); /* control never reaches here */
}
/*
* eval - Evaluate the command line that the user has just typed in
*
* If the user has requested a built-in command (quit, jobs, bg or fg)
* then execute it immediately. Otherwise, fork a child process and
* run the job in the context of the child. If the job is running in
* the foreground, wait for it to terminate and then return. Note:
* each child process must have a unique process group ID so that our
* background children don't receive SIGINT (SIGTSTP) from the kernel
* when we type ctrl-c (ctrl-z) at the keyboard.
*/
void eval(char *cmdline)
{
char arg[MAXARGS][MAXLINE];
char *argv[MAXARGS];
for (int i = 0 ; i < MAXARGS; i++)
{
argv[i] = &arg[i][0];
}
int ground = parseline(cmdline, argv);
if (argv[0] == nullptr) return;
if (builtin_cmd(argv[0]))
{
LOG << "built-in" << endl;
if (strcmp(argv[0], "quit") == 0) builtin_command_quit();
else if (strcmp(argv[0], "jobs") == 0) listjobs(jobs);
else if (strcmp(argv[0], "fg") == 0) do_bgfg(argv);
else if (strcmp(argv[0], "bg") == 0) do_bgfg(argv);
else cerr << "built-in but not supported" << endl;
return;
}
sigset_t prev;
block_all(&prev);
pid_t pid = fork();
if (pid == 0) // child
{
sigprocmask(SIG_SETMASK, &prev, nullptr);
setpgid(0, 0);
execve(argv[0], argv, environ);
atomic_print(cmdline, true);
atomic_print(": Command not found\n");
exit(-1); // if no command is found
}
// parent
if (!ground) // foreground
{
addjob(jobs, pid, FG, cmdline);
sigset_t prev_with_sigchld_blocked = prev;
sigaddset(&prev_with_sigchld_blocked, SIGCHLD);
sigprocmask(SIG_SETMASK, &prev_with_sigchld_blocked, NULL);
waitfg(pid);
sigprocmask(SIG_SETMASK, &prev, NULL);
}
else // background
{
addjob(jobs, pid, BG, cmdline);
sigprocmask(SIG_SETMASK, &prev, NULL);
cout << "[" << pid2jid(pid) << "] (" << pid << ") " << cmdline;
}
return;
}
/*
* parseline - Parse the command line and build the argv array.
*
* Characters enclosed in single quotes are treated as a single
* argument. Return true if the user has requested a BG job, false if
* the user has requested a FG job.
*/
/*
* builtin_cmd - If the user has typed a built-in command then execute
* it immediately.
*/
int builtin_cmd(char *c_str)
{
string s(c_str);
for (auto c : builtin_cmd_list)
if (c == s)
return 1;
return 0; /* not a builtin command */
}
/*
* do_bgfg - Execute the builtin bg and fg commands
*/
void do_bgfg(char **argv)
{
if ((strcmp(argv[0], "fg") == 0) || (strcmp(argv[0], "bg") == 0))
{
int x;
job_t *j = nullptr;
sigset_t prev;
if (argv[1] == nullptr)
{
cerr << ((strcmp(argv[0], "fg") == 0) ? "fg" : "bg") << " command requires PID or %jobid argument" << endl;
return;
}
if (argv[1][0] == '%') // jid provided
{
if (!isnum(&argv[1][1]))
{
cerr << ((strcmp(argv[0], "fg") == 0) ? "fg" : "bg") << ": argument must be a PID or %jobid" << endl;
return;
}
x = atoi(&argv[1][1]); // eat %
j = getjobjid(jobs, x);
if (j == nullptr)
{
cerr << argv[1] << ": No such job" << endl;
return;
}
} else { // pid
if (!isnum(&argv[1][0]))
{
cerr << ((strcmp(argv[0], "fg") == 0) ? "fg" : "bg") << ": argument must be a PID or %jobid" << endl;
return;
}
x = atoi(&argv[1][0]);
j = getjobpid(jobs, x);
if (j == nullptr)
{
cerr << "(" << argv[1] << "): No such process" << endl;
return;
}
}
block_all(&prev);
if (strcmp(argv[0], "fg") == 0) // fg
{
LOG << "fg" << endl;
j->state = FG;
sigset_t prev_with_sigchld_blocked = prev;
sigaddset(&prev_with_sigchld_blocked, SIGCHLD);
sigprocmask(SIG_SETMASK, &prev_with_sigchld_blocked, NULL);
if (kill(-(j->pid), SIGCONT) < 0)
{
cerr << "Cannot continue process " << j->pid;
sigprocmask(SIG_SETMASK, &prev, NULL);
return;
}
waitfg(j->pid);
sigprocmask(SIG_SETMASK, &prev, NULL);
} else // bg
{
LOG << "bg" << endl;
j->state = BG;
kill(-(j->pid), SIGCONT);
sigprocmask(SIG_SETMASK, &prev, NULL);
// cmdline has nerd '\n' at the end
// so just flush
cout << "[" << j->jid << "] (" << j->pid << ") " << j->cmdline << flush;
}
}
else
cerr << "not fg/bg" << endl;
}
/*
* waitfg - Block until process pid is no longer the foreground process
*/
void waitfg(pid_t pid)
{
sigset_t prev_with_sigchld_unblocked;
// get current blocked signals
sigprocmask(0, nullptr, &prev_with_sigchld_unblocked);
sigdelset(&prev_with_sigchld_unblocked, SIGCHLD);
LOG << "waiting for pid "<< fgpid(jobs) << endl;
while (fgpid(jobs) != 0)
sigsuspend(&prev_with_sigchld_unblocked);
LOG << "finished fg " << endl;
return;
}
/***********************
* Other helper routines
***********************/
/*
* usage - print a help message
*/
void usage(void)
{
printf("Usage: shell [-hvp]\n");
printf(" -h print this message\n");
printf(" -v print additional diagnostic information\n");
printf(" -p do not emit a command prompt\n");
exit(1);
}
/*
* unix_error - unix-style error routine
*/
void unix_error(char *msg)
{
fprintf(stdout, "%s: %s\n", msg, strerror(errno));
exit(1);
}
/*
* app_error - application-style error routine
*/
void app_error(char *msg)
{
fprintf(stdout, "%s\n", msg);
exit(1);
}
/*
* Signal - wrapper for the sigaction function
*/
handler_t *Signal(int signum, handler_t *handler)
{
struct sigaction action, old_action;
action.sa_handler = handler;
sigemptyset(&action.sa_mask); /* block sigs of type being handled */
action.sa_flags = SA_RESTART; /* restart syscalls if possible */
if (sigaction(signum, &action, &old_action) < 0)
unix_error("Signal error");
return (old_action.sa_handler);
}
void builtin_command_quit()
{
exit(0);
}
|