-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshell.c
246 lines (193 loc) · 5.4 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
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
#include "shell.h"
#define SIZE 256
// heap variables
char *pwd; // current directory
char *line; // user input, malloc by readline
char **cmds; // array of commands
char **args; // array of args per command
wordexp_t p;
// stack variables
int nextfd[2]; // next pipe fildes
int prevfd[2]; // previous pipe fildes
pid_t pid;
char time_str[9];
int exit_status;
int main(int argc, char *argv[])
{
int ret = 0;
// initialize heap variables
pwd = (char *)calloc(sizeof(char), MAXPATHLEN);
cmds = (char **)calloc(sizeof(char *), SIZE);
args = (char **)calloc(sizeof(char *), SIZE);
getcwd(pwd, MAXPATHLEN);
if (argc > 1 && strcmp(argv[1], "-c") == 0) {
// run command directly
if (argv[2] == NULL) {
puts("Usage: pzash -c [\033[4mcommand\033[0m]");
return -1;
}
parse_run(argv[2]);
} else {
// start interactive shell
while (ret < 1) { ret = prompt(); }
free(line);
}
// free memory for graceful exit
free(args);
free(cmds);
free(pwd);
wordfree(&p);
printf("\n");
exit(0);
}
int prompt()
{
// main prompt loop
// ensure fresh memory for new input
for (int i = 0; i < SIZE; ++i) {
args[i] = NULL;
cmds[i] = NULL;
}
update_time();
// build prompt and get input
char *prompt_str = build_prompt();
line = readline(prompt_str);
free(prompt_str);
// handle Ctrl-D
if (!line)
return 1;
// handle empty input
if (*line == '\0')
return 0;
// add line to readline history
add_history(line);
// parse and execute
parse_run(line);
return 0;
}
void parse_run(char *line)
{
int cmd_count = tokenize(cmds, line, "|");
int first_cmd;
int last_cmd;
for (int i = 0; i < cmd_count; ++i) {
// ensure fresh args for each cmd
for (int i = 0; i < SIZE; ++i) { args[i] = NULL; }
if (expand(args, *(cmds + i)) < 0)
return;
first_cmd = (i == 0);
last_cmd = (i == cmd_count - 1);
if (execute(args, first_cmd, last_cmd) < 0)
return;
}
}
int tokenize(char **dest, char *source, char *delim)
{
// splits source by delimiter into destination array
// skips leading delims and empties
// skip leading delims
while (*source == *delim) { source++; }
// iterate until source is NULL
int len = 0;
while ((dest[len] = strsep(&source, delim))) {
if (*dest[len] != '\0') len++; // only iterate on non-empty
}
return len;
}
int expand(char **dest, char *source)
{
// splits by whitespace and performs sh-like word expansion
// i.e. replaces special chars like ~, $, *
int len = p.we_wordc;
int err = wordexp(source, &p, 0);
switch (err) {
case WRDE_BADCHAR:
puts("illegal character");
break;
case WRDE_BADVAL:
puts("undefined variable");
break;
case WRDE_NOSPACE:
puts("out of memory");
break;
case WRDE_SYNTAX:
puts("syntax error");
break;
}
if (err)
return -1;
for (int i = 0; i < p.we_wordc; ++i) {
dest[i] = p.we_wordv[i];
}
return len;
}
int execute(char **args, int first_cmd, int last_cmd)
{
char *command = *args;
if (command == NULL) {
fprintf(stderr, "How did this happen? We're smarter than this.\n");
exit(-1);
}
// check for built-in
for (int i = 0; i < builtin_count; ++i) {
if (strcmp(command, builtin_names[i]) == 0) {
if (builtins[i](args) < 0) {
perror(command);
return -1;
}
if (!last_cmd) {
puts("ooohh we don't support piping builtins yet...");
return -1;
}
exit_status = 0;
return 0;
}
}
// pipe handling
if (!last_cmd) {
if (pipe(nextfd) < 0) {
perror("PIPE ERROR");
return -1;
}
}
// must be external program, need to fork
pid = fork();
switch (pid) {
case -1:
// ERROR
fprintf(stderr, "fork failed\n");
break;
case 0:
// CHILD
if (!first_cmd) {
dup2(prevfd[0], 0); // set input to previous pipe
close(prevfd[0]); // close previous pipe input
close(prevfd[1]); // close previous pipe output
}
if (!last_cmd) {
close(nextfd[0]); // close next pipe input
dup2(nextfd[1], 1); // set output to next pipe
close(nextfd[1]); // close next pipe output
}
if (execvp(command, args) < 0) { perror("pzash"); }
exit(-1); // only runs if execvp fails anyway
default:
// PARENT
if (!first_cmd) {
// close previous pipe
close(prevfd[0]);
close(prevfd[1]);
}
if (!last_cmd) {
// recycle previous pipe
prevfd[0] = nextfd[0];
prevfd[1] = nextfd[1];
}
int status;
waitpid(pid, &status, 0);
if (WIFEXITED(status))
exit_status = WEXITSTATUS(status);
break;
}
return 0;
}