-
Notifications
You must be signed in to change notification settings - Fork 0
/
mshell.c
258 lines (216 loc) · 7.05 KB
/
mshell.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
/* mshell - a job manager */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <signal.h>
#include <sys/types.h>
#include <unistd.h>
#include "cmd.h"
#include "sighandlers.h"
#include "jobs.h"
#include "pipe.h"
#include "common.h"
static char prompt[] = "mshell> "; /* command line prompt */
/*
* usage - print a help message
*/
void usage(void) {
printf("Usage: mshell [-hv]\n");
printf(" -h print this message\n");
printf(" -v print additional diagnostic information\n");
exit(EXIT_FAILURE);
}
/*
* parseline - Parse the command line and build the token array, which
* contains argv arrays.
*
* Return true if the user has requested a BG job, false if
* the user has requested a FG job.
*/
int parseline(char *cmdline, char *token[MAXCMDS][MAXARGS], int *nbcmd) {
int i, j, k;
strtok(cmdline, "|");
token[0][0] = cmdline;
i = 1;
while ((token[i][0] = (char *) strtok(NULL, "|")) != NULL) {
while (*token[i][0] == ' ')
token[i][0]++;
i++;
}
for (j = 0; j < i; j++) {
strtok(token[j][0], " ");
k = 1;
while ((token[j][k] = (char *) strtok(NULL, " ")) != NULL) {
while (*token[j][k] == ' ')
token[j][k]++;
k++;
}
token[j][k] = NULL;
}
*nbcmd = i;
/* case: cmd & */
if ((*token[j - 1][k - 1] == '&') != 0) {
token[j - 1][k - 1] = NULL;
return 1;
}
/* case: cmd& */
if ((token[j - 1][k - 1][strlen(token[j - 1][k - 1]) - 1] == '&') != 0) {
token[j - 1][k - 1][strlen(token[j - 1][k - 1]) - 1] = '\0';
return 1;
}
return 0; /* foreground job */
}
/*
* builtin_cmd - If the user has typed a built-in command then execute
* it immediately.
*/
int builtin_cmd(char **argv) {
char *cmd = argv[0];
if (!strcmp(cmd, "help")) {
do_help();
return 1;
}
if (!strcmp(cmd, "stop")) {
do_stop(argv);
return 1;
}
if (!strcmp(cmd, "exit")) {
do_exit();
return 1;
}
if (!strcmp(cmd, "jobs")) {
do_jobs();
return 1;
}
if (!strcmp(cmd, "bg")) {
do_bg(argv);
return 1;
}
if (!strcmp(cmd, "fg")) {
do_fg(argv);
return 1;
}
if (!strcmp(cmd, "kill")) {
do_kill(argv);
return 1;
}
return 0; /* not a builtin command */
}
void eval(char *cmdline) {
char *token[MAXCMDS][MAXARGS]; /* token[i] is a command typed in the command line */
int nbcmd; /* number of commands typed in the command line */
int bg; /* should the job run in bg or fg? */
pid_t pid; /* process id */
sigset_t mask; /* signal mask */
char **argv;
/* Parse command line */
bg = parseline(cmdline, token, &nbcmd);
if (nbcmd > 1) /* a command pipeline has been typed in */
do_pipe(token, nbcmd, bg);
else { /* no pipeline, only one command */
argv = token[0];
if (!builtin_cmd(argv)) {
/*
* This is a little tricky. Block SIGCHLD, SIGINT, and SIGTSTP
* signals until we can add the job to the job list. This
* eliminates some nasty races between adding a job to the job
* list and the arrival of SIGCHLD, SIGINT, and SIGTSTP signals.
*/
if (sigemptyset(&mask) < 0)
unix_error("sigemptyset error");
if (sigaddset(&mask, SIGCHLD))
unix_error("sigaddset error");
if (sigaddset(&mask, SIGINT))
unix_error("sigaddset error");
if (sigaddset(&mask, SIGTSTP))
unix_error("sigaddset error");
if (sigprocmask(SIG_BLOCK, &mask, NULL) < 0)
unix_error("sigprocmask error");
/* Create a child process */
if ((pid = fork()) < 0)
unix_error("fork error");
/*
* Child process
*/
if (pid == 0) {
/* Child unblocks signals */
sigprocmask(SIG_UNBLOCK, &mask, NULL);
/* Each new job must get a new process group ID
so that the kernel doesn't send ctrl-c and ctrl-z
signals to all of the shell's jobs */
if (setpgid(0, 0) < 0)
unix_error("setpgid error");
/* Now load and run the program in the new job */
if (execvp(argv[0], argv) < 0) {
printf("%s: Command not found\n", argv[0]);
exit(EXIT_FAILURE);
}
}
/*
* Parent process
*/
/* Parent adds the job, and then unblocks signals so that
the signals handlers can run again */
jobs_addjob(pid, (bg == 1 ? BG : FG), cmdline);
sigprocmask(SIG_UNBLOCK, &mask, NULL);
if (!bg)
waitfg(pid);
else
printf("[%d] (%d) %s\n", jobs_pid2jid(pid), (int) pid, cmdline);
}
}
return;
}
/*
* main - The shell's main routine
*/
int main(int argc, char **argv) {
char c;
char cmdline[MAXLINE];
verbose = 0;
/* 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, "hv")) != EOF) {
switch (c) {
case 'h': /* print help message */
usage();
break;
case 'v': /* emit additional diagnostic info */
verbose = 1;
break;
default:
usage();
}
}
/* Install the signal handlers */
/* These are the ones you will need to implement */
sigaction_wrapper(SIGINT, sigint_handler); /* ctrl-c */
sigaction_wrapper(SIGTSTP, sigtstp_handler); /* ctrl-z */
sigaction_wrapper(SIGCHLD, sigchld_handler); /* Terminated or stopped child */
/* Initialize the job list */
jobs_initjobs();
printf("\nType help to find out all the available commands in mshell\n\n");
/* Execute the shell's read/eval loop */
while (1) {
/* Read command line */
printf("%s", prompt);
if ((fgets(cmdline, MAXLINE, stdin) == NULL) && ferror(stdin)) {
fprintf(stdout, "fgets error\n");
exit(EXIT_FAILURE);
}
if (feof(stdin)) { /* End of file (ctrl-d) */
fflush(stdout);
exit(EXIT_SUCCESS);
}
/* Evaluate the command line */
if (cmdline[0] != '\n') { /* evaluate the command line only when not empty */
cmdline[strlen(cmdline) - 1] = '\0'; /* remove the '\n' at the end */
eval(cmdline);
fflush(stdout);
fflush(stdout);
}
}
exit(EXIT_SUCCESS); /* control never reaches here */
}