-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmyshell.c
More file actions
406 lines (340 loc) · 8.89 KB
/
Copy pathmyshell.c
File metadata and controls
406 lines (340 loc) · 8.89 KB
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
396
397
398
399
400
401
402
403
404
405
406
#include <errno.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <setjmp.h>
#include <fcntl.h>
#include <sys/wait.h>
#include <sys/types.h>
#define MAXARGS 128
#define MAXLINE 8192
void eval(char* cmdline);
int builtin_command(char* first_cmd);
char* skip_blank(char* buf);
char* find_delimiter(char* buf);
char* find_special_delimiter(char* buf);
int check_syntax_error(char* cmdline);
void print_syntax_error(char c);
void cmd_to_argv(char* cmd, char** argv);
void redirection(char* cmd);
char* pipelining(char* cmdline, pid_t* job_pgid);
void signal_handler(int sig);
void set_terminal_pgid(pid_t pgid);
jmp_buf jbuf;
pid_t shell_pgid;
int main() {
char cmdline[MAXLINE];
signal(SIGTTOU, SIG_IGN);
signal(SIGTTIN, SIG_IGN);
signal(SIGTSTP, SIG_IGN);
// Set terminal pgid to shell pgid.
shell_pgid = getpid();
if (setpgid(0, 0) < 0 && errno != EACCES && errno != EPERM) {
perror("setpgid");
exit(1);
}
set_terminal_pgid(shell_pgid);
sigsetjmp(jbuf, 1);
while (1) {
if (signal(SIGINT, signal_handler) == SIG_ERR) {
perror("signal");
exit(1);
}
printf("[JS]# ");
fgets(cmdline, MAXLINE, stdin);
if (feof(stdin))
exit(0);
cmdline[strlen(cmdline) - 1] = ' '; // Replace '\n' to ' '.
// Check syntax error.
if (check_syntax_error(cmdline))
continue;
// Evalutate.
eval(cmdline);
}
}
/* Evaluate a command line */
void eval(char* cmdline) {
char* argv[MAXARGS]; // Argument list execvp().
char* cmd; // Holds modified command line separated by pipeline in each child process.
// Ignore SIGINT
if (signal(SIGINT, SIG_IGN) == SIG_ERR) {
perror("signal");
exit(1);
}
// Check built in command.
char* first_cmd = skip_blank(cmdline);
if (builtin_command(first_cmd)) {
return;
}
// Pipelining ( separate the process and command according to the pipeline. )
pid_t job_pgid = 0;
cmd = pipelining(cmdline, &job_pgid);
if (cmd != NULL) { // If not shell process. Shell process: cmd == NULL.
redirection(cmd); // Redirection for each separated command.
cmd_to_argv(cmd, argv); // Change command to argv.
// SIG allowed.
signal(SIGINT, SIG_DFL);
signal(SIGTSTP, SIG_DFL);
signal(SIGTTIN, SIG_DFL);
signal(SIGTTOU, SIG_DFL);
// If command is blank
if (argv[0] == NULL)
exit(0);
if (execvp(argv[0], argv) < 0) {
fprintf(stderr, "JS: %s: command not found...\n", argv[0]);
exit(0);
}
}
// After this line excuted only by shell process.
// Set terminal pgid to child pgid.
set_terminal_pgid(job_pgid);
// Wait child(exec) process to be terminated.
int status;
int sigint_received = 0;
while (waitpid(-job_pgid, &status, WUNTRACED) > 0) {
if (WIFSIGNALED(status) && WTERMSIG(status) == SIGINT) {
sigint_received = 1;
}
}
// Set terminal pgid to shell pgid.
set_terminal_pgid(shell_pgid);
// If child process receiept SIGINT.
if (sigint_received)
printf("\n");
return;
}
/* Built in command to terminate shell or do something to shell */
int builtin_command(char* first_cmd) {
if (strncmp(first_cmd, "quit", 4) == 0 &&
(first_cmd[4] == ' ' || first_cmd[4] == '\0' || first_cmd[4] == '\n')) {
exit(0);
}
return 0;
}
/* Skip blank from buffer start position address. */
char* skip_blank(char* buf) {
while (*buf && (*buf == ' ')) {
buf++;
}
return buf;
}
/* Find delimiter position address in buffer. */
char* find_delimiter(char* buf) {
while (*buf) {
if (*buf == '>' || *buf == '<' || *buf == '|' || *buf == ' ') {
return buf;
}
buf++;
}
return buf;
}
/* Find special delimiter position address in buffer. */
char* find_special_delimiter(char* buf) {
while (*buf) {
if (*buf == '>' || *buf == '<' || *buf == '|') {
return buf;
}
buf++;
}
return buf;
}
/* Check syntax error in command line. */
int check_syntax_error(char* cmdline) {
char* delim; // Delimiter position address.
cmdline = skip_blank(cmdline);
while ((delim = find_special_delimiter(cmdline)) && *delim) {
if (*delim == '|' && (cmdline == delim || skip_blank(delim + 1) == strchr(delim + 1, '|') || *(skip_blank(delim + 1)) == '\0')) {
print_syntax_error('|');
return 1;
}
if (*delim == '<') {
if (skip_blank(delim + 1) == find_special_delimiter(delim + 1)) {
print_syntax_error(*find_special_delimiter(delim + 1));
return 1;
}
}
if (*delim == '>') {
if (*(delim + 1) == '>')
delim += 1;
if (skip_blank(delim + 1) == find_special_delimiter(delim + 1)) {
print_syntax_error(*find_special_delimiter(delim + 1));
return 1;
}
}
cmdline = delim + 1;
cmdline = skip_blank(cmdline);
}
return 0;
}
/* Print syntax error message. */
void print_syntax_error(char c) {
if (c == '\0')
fprintf(stderr, "JS: syntax error near unexpected token 'newline'\n");
else
fprintf(stderr, "JS: syntax error near unexpected token '%c'\n", c);
return;
}
/* Separate command to argv. */
void cmd_to_argv(char* cmd, char** argv) {
char* delim;
int argc = 0;
cmd = skip_blank(cmd);
while ((delim = find_delimiter(cmd)) && *delim) {
*delim = '\0';
argv[argc++] = cmd;
cmd = delim + 1;
cmd = skip_blank(cmd);
}
argv[argc] = NULL;
return;
}
/* Do I/O rediection. */
void redirection(char* cmd) {
char* delim;
char delimTemp;
int fd;
int redirectionFlag;
while ((delim = find_special_delimiter(cmd)) && *delim) {
if (*delim == '<' || *delim == '>') {
if (*delim == '<')
redirectionFlag = 1;
if (*delim == '>')
redirectionFlag = 2;
*delim = ' ';
if (*(delim + 1) == '>') {
delim++;
*delim = ' ';
redirectionFlag = 3;
}
// Parsing file name.
cmd = skip_blank(delim + 1);
delim = find_delimiter(cmd);
delimTemp = *delim;
*delim = '\0';
switch (redirectionFlag) {
case 1:
if ((fd = open(cmd, O_RDONLY)) == -1) {
fprintf(stderr, "JS: %s: %s\n", cmd, strerror(errno));
exit(1);
}
if ((dup2(fd, STDIN_FILENO)) == -1) {
fprintf(stderr, "JS: %s: %s\n", cmd, strerror(errno));
exit(1);
}
close(fd);
break;
case 2:
if ((fd = open(cmd, O_WRONLY | O_TRUNC | O_CREAT, 0644)) == -1) {
fprintf(stderr, "JS: %s: %s\n", cmd, strerror(errno));
exit(1);
}
if ((dup2(fd, STDOUT_FILENO)) == -1) {
fprintf(stderr, "JS: %s: %s\n", cmd, strerror(errno));
exit(1);
}
close(fd);
break;
case 3:
if ((fd = open(cmd, O_WRONLY | O_APPEND | O_CREAT, 0644)) == -1) {
fprintf(stderr, "JS: %s: %s\n", cmd, strerror(errno));
exit(1);
}
if ((dup2(fd, STDOUT_FILENO)) == -1) {
fprintf(stderr, "JS: %s: %s\n", cmd, strerror(errno));
exit(1);
}
close(fd);
break;
}
// Change the buffer address used for redirection to blank.
while (cmd != delim) {
*cmd = ' ';
cmd++;
}
// Change '\0' used in file name parsing to original delimiter
if (delimTemp)
*delim = delimTemp;
continue;
}
cmd = delim + 1;
}
return;
}
/* Separate the process and command according to the pipeline. */
char* pipelining(char* cmdline, pid_t* job_pgid) {
pid_t pid;
char* pipeDelim; // Delimiter where pipeline is in command line.
int curPipefd[2];
int prevPipefd;
int pipeFlag = 0; // 1 if current command come after '|' symbol. 0 if not.
while ((pipeDelim = strchr(cmdline, '|')) || *cmdline) {
if (pipeFlag) {
prevPipefd = curPipefd[0]; // This line used to connect pipe of previous process to current process.
}
if (pipeDelim) {
*pipeDelim = '\0';
if (pipe(curPipefd) == -1) {
perror("pipe");
return NULL;
}
}
if ((pid = fork()) == 0) {
if (*job_pgid == 0)
*job_pgid = getpid();
setpgid(0, *job_pgid);
if (pipeFlag) {
if ((dup2(prevPipefd, STDIN_FILENO)) == -1) {
perror("dup2");
exit(1);
}
close(prevPipefd);
}
if (pipeDelim) {
close(curPipefd[0]);
if ((dup2(curPipefd[1], STDOUT_FILENO)) == -1) {
perror("dup2");
exit(1);
}
close(curPipefd[1]);
}
return cmdline;
}
else if (pid == -1) {
perror("fork");
exit(1);
}
// After this line only excuted by shell process. Close all pipefd linked to shell process.
// Set group id to pid of first child process.
if (*job_pgid == 0)
*job_pgid = pid;
setpgid(pid, *job_pgid);
// Close all connections between shell process and all pipe.
if (pipeFlag) {
close(prevPipefd);
pipeFlag = 0;
}
if (pipeDelim) {
close(curPipefd[1]);
pipeFlag = 1; // Next command come after pipeline. Which mean current command is pipelined to next command.
cmdline = pipeDelim + 1;
}
else { // If there is no left pipeline, then stop pipelining.
break;
}
}
return NULL;
}
/* SIGINT handler. */
void signal_handler(int sig) {
printf("\nYour shell cannot be terminated using an interrupt signal!\n");
siglongjmp(jbuf, 1);
}
/* Set terminal pgid to input pgid. */
void set_terminal_pgid(pid_t pgid) {
if (tcsetpgrp(STDIN_FILENO, pgid) < 0) {
perror("tcsetpgrp");
exit(1);
}
}