-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathspawnshell.c
More file actions
266 lines (225 loc) · 6.5 KB
/
spawnshell.c
File metadata and controls
266 lines (225 loc) · 6.5 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
/* $begin shellmain */
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <spawn.h>
#include <stdbool.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <signal.h>
#define MAXARGS 128
#define MAXLINE 8192 /* Max text line length */
extern char **environ; /* Defined by libc */
int pipe_idx = -1;
int wait_idx = -1;
/* Function prototypes */
void eval(char *cmdline);
int parseline(char *buf, char **argv, posix_spawn_file_actions_t* actions, posix_spawn_file_actions_t* actions_2, int* pipe_fds);
int builtin_command(char **argv);
void unix_error(char *msg) /* Unix-style error */
{
fprintf(stderr, "%s: %s\n", msg, strerror(errno));
exit(EXIT_FAILURE);
}
// -- my added signal handler for ctrl+c and ctrl+z
static void signal_handler(int a_signal)
{
if(a_signal == SIGINT)
{
write(STDOUT_FILENO, "\ncaught sigint\nCS361 >", 22);
}
else if(a_signal == SIGTSTP)
{
write(STDOUT_FILENO, "\ncaught sigstop\nCS361 >", 23);
}
}
// ----- main function -----
int main() {
char cmdline[MAXLINE]; /* Command line */
signal(SIGINT,signal_handler);
signal(SIGTSTP, signal_handler);
while (1) {
char *result;
/* Read */
printf("CS361 > ");
result = fgets(cmdline, MAXLINE, stdin);
if (result == NULL && ferror(stdin)) {
fprintf(stderr, "fatal fgets error\n");
exit(EXIT_FAILURE);
}
if (feof(stdin)) exit(0);
/* Evaluate */
eval(cmdline);
}
}
// ---- end main function ----
// ---- eval ----
/* eval - Evaluate a command line */
void eval(char *cmdline) {
char *argv[MAXARGS]; /* Argument list execve() */
char buf[MAXLINE]; /* Holds modified command line */
int bg; /* Should the job run in bg or fg? */
pid_t pid; // Process id / result
pid_t pid2; // process id / result 2
// --- fab added variables ---
pipe_idx = -1;
wait_idx = -1;
posix_spawn_file_actions_t actions;
posix_spawn_file_actions_init(&actions);
posix_spawn_file_actions_t actions_2;
posix_spawn_file_actions_init(&actions_2);
int pipe_fds[2]; // pipe[0] = read end
// pipe[1] = write end
pipe(pipe_fds);
strcpy(buf, cmdline);
bg = parseline(buf, argv, &actions, &actions_2, &pipe_fds);
if (argv[0] == NULL)
return; /* Ignore empty lines */
if (!builtin_command(argv)) {
// run the command 'argv' using posix_spawnp.
if (0 != posix_spawnp(&pid, argv[0], &actions, NULL, argv, environ)) {
perror("spawn failed");
exit(1);
}
// for piping case to do the 2nd command.
if(pipe_idx != -1)
{
if (0 != posix_spawnp(&pid2, argv[pipe_idx], &actions_2, NULL, argv+pipe_idx, environ)) {
perror("spawn failed");
exit(1);
}
}
// Close the read and write ends in the parent process.
if(pipe_idx != -1)
{
close(pipe_fds[0]);
close(pipe_fds[1]);
}
/* Parent waits for foreground job to terminate */
if (!bg) {
int status;
int status_two;
if (waitpid(pid, &status, 0) < 0)
unix_error("waitfg: waitpid error");
// WAIT case
if(wait_idx > 0)
{
if (0 != posix_spawnp(&pid2, argv[wait_idx], &actions_2, NULL, argv+wait_idx, environ)) {
perror("spawn failed");
exit(1);
}
if (waitpid(pid2, &status_two, 0) < 0)
unix_error("waitfg: waitpid error");
}
}
// bg != 0
else
{
//int status_three;
printf("%d %s", pid, cmdline);
/*
int olderrno = errno;
while (waitpid(-1, NULL, 0) > 0)
{
Sio_puts("Handler reaped child\n");
}
if (errno != ECHILD)
Sio_error("waitpid error");
Sleep(1);
errno = olderrno;
*/
}
}
return;
}
/* If first arg is a builtin command, run it and return true */
int builtin_command(char **argv) {
if (!strcmp(argv[0], "exit")) /* exit command */
exit(0);
if (!strcmp(argv[0], "&")) /* Ignore singleton & */
return 1;
// TODO: implement "?" commands!
return 0; /* Not a builtin command */
}
// ---- end eval ----
/* $begin parseline */
/* parseline - Parse the command line and build the argv array */
int parseline(char *buf, char **argv, posix_spawn_file_actions_t* actions, posix_spawn_file_actions_t* actions_2, int* pipe_fds) {
char *delim; /* Points to first space delimiter */
int argc; /* Number of args */
int bg; /* Background job? */
buf[strlen(buf) - 1] = ' '; /* Replace trailing '\n' with space */
while (*buf && (*buf == ' ')) /* Ignore leading spaces */
buf++;
argc = 0;
while ((delim = strchr(buf, ' '))) {
argv[argc++] = buf;
*delim = '\0';
buf = delim + 1;
while (*buf && (*buf == ' ')) /* Ignore spaces */
buf++;
}
argv[argc] = NULL;
if (argc == 0) /* Ignore blank line */
return 1;
/* Should the job run in the background? */
if ((bg = (*argv[argc - 1] == '&')) != 0)
argv[--argc] = NULL;
//printf("bg = %d\n\n", bg);
//printf("argc: %d\n", argc);
// -- fab added code to print and set flags. --
// <
// >
// < >
// !
// |
// 0 = normal/unchanged
// 1 = output redirection
// 2 = input redirection
// 3 = both redirection
// 4 = pipe
// 5 = wait
int i = 0;
while(argv[i] != NULL)
{
//printf("argv[%d]: %s\n", i, argv[i]);
if(strcmp(argv[i], ">") == 0)
{
argv[i] = NULL;
//printf("> detected...\n");
posix_spawn_file_actions_addopen(actions, STDOUT_FILENO, argv[i+1],
O_WRONLY | O_CREAT, S_IRUSR | S_IWUSR);
//printf("2 > detected...\n");
}
else if(strcmp(argv[i], "<") == 0)
{
argv[i] = NULL;
posix_spawn_file_actions_addopen(actions, STDIN_FILENO, argv[i+1],
O_RDONLY, S_IRUSR | S_IRGRP);
//printf("< detected...\n");
}
else if(strcmp(argv[i], "|") == 0)
{
argv[i] = NULL;
pipe_idx = i+1;
posix_spawn_file_actions_adddup2(actions, pipe_fds[1], STDOUT_FILENO);
posix_spawn_file_actions_addclose(actions, pipe_fds[0]);
posix_spawn_file_actions_adddup2(actions_2, pipe_fds[0], STDIN_FILENO);
posix_spawn_file_actions_addclose(actions_2, pipe_fds[1]);
//printf("| detected...\n");
}
else if(strcmp(argv[i], ";") == 0)
{
argv[i] = NULL;
wait_idx = i+1;
//printf("; detected...\n");
}
i++;
}
return bg;
}
/* $end parseline */