-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommand_utility.c
More file actions
279 lines (254 loc) · 6.54 KB
/
command_utility.c
File metadata and controls
279 lines (254 loc) · 6.54 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
#include <error.h>
#include <errno.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <fcntl.h>
#include <stdlib.h>
#include <stdio.h>
#include "command_utility.h"
#define io_num(io, d) (io->io_num != NULL ? (int) strtol (io->io_num, NULL, 10) : (d))
static void
command_indented_print (int indent, command_t c)
{
switch (c->type)
{
case CMD_AND:
case CMD_SEQUENCE:
case CMD_OR:
case CMD_PIPE:
{
command_indented_print (indent + 2 * (c->u.command[0]->type != c->type), c->u.command[0]);
static char const command_label[][3] = { "&&", ";", "||", "|" };
printf (" \\\n%*s%s\n", indent, "", command_label[c->type]);
command_indented_print (indent + 2 * (c->u.command[1]->type != c->type), c->u.command[1]);
break;
}
case CMD_SIMPLE:
{
char **w = c->u.word;
printf ("%*s%s", indent, "", *w);
while (*++w)
printf (" %s", *w);
break;
}
case CMD_SUBSHELL:
{
printf ("%*s(\n", indent, "");
command_indented_print (indent + 1, c->u.subshell_command);
printf ("\n%*s)", indent, "");
break;
}
default:
abort ();
}
io_node_t io = c->io_head;
while (io != NULL)
{
printf (" ");
if (io->io_num)
printf ("%s", io->io_num);
switch (io->op)
{
case IO_INPUT: printf ("<%s", io->word); break;
case IO_DUPIN: printf ("<&%s", io->word); break;
case IO_IODUAL: printf ("<>%s", io->word); break;
case IO_OUTPUT: printf (">%s", io->word); break;
case IO_DUPOUT: printf (">&%s", io->word); break;
case IO_APPEND: printf (">>%s", io->word); break;
case IO_CLOBBER: printf (">|%s", io->word); break;
}
io = io->next;
}
}
void
print_command (command_t c)
{
command_indented_print (2, c);
putchar ('\n');
}
int
command_status (command_t c)
{
return c->status;
}
void
execute_sequence_command (command_t cmd, int no_clobber)
{
execute_command (cmd->u.command[0], no_clobber);
execute_command (cmd->u.command[1], no_clobber);
cmd->status = cmd->u.command[1]->status;
}
void
execute_and_command (command_t cmd, int no_clobber)
{
int i;
command_t c = NULL;
for (i = 0; i < 2; i++)
{
c = cmd->u.command[i];
execute_command (c, no_clobber);
if (c->status != 0)
break;
}
cmd->status = c->status;
}
void
execute_or_command (command_t cmd, int no_clobber)
{
int i;
command_t c = NULL;
for (i = 0; i < 2; i++)
{
c = cmd->u.command[i];
execute_command (c, no_clobber);
if (c->status == 0)
break;
}
cmd->status = c->status;
}
void
execute_pipe_command (command_t cmd, int no_clobber)
{
pid_t l_pid;
pid_t r_pid;
int pfd[2];
int status;
pipe (pfd);
l_pid = fork ();
if (l_pid == 0)
{
close (pfd[0]);
dup2 (pfd[1], STDOUT_FILENO);
close (pfd[1]);
execute_command (cmd->u.command[0], no_clobber);
exit (cmd->u.command[0]->status);
}
else if (l_pid > 0)
{
r_pid = fork ();
if (r_pid == 0)
{
close (pfd[1]);
dup2 (pfd[0], STDIN_FILENO);
close (pfd[0]);
execute_command (cmd->u.command[1], no_clobber);
exit (cmd->u.command[1]->status);
}
else if (r_pid > 0)
{
close (pfd[0]);
close (pfd[1]);
waitpid (l_pid, &status, 0);
waitpid (r_pid, &status, 0); // overwrite status
cmd->status = status;
}
}
}
void open_io (command_t cmd, int no_clobber)
{
int fdionum;
int fdword;
int closefdword = 1;
int write_flags = O_WRONLY | O_CREAT | O_TRUNC;
int append_flags = O_WRONLY | O_CREAT | O_APPEND;
int rw_flags = O_RDWR | O_CREAT;
int perm_flags = S_IRUSR | S_IWUSR;
io_node_t io = cmd->io_head;
io = cmd->io_head;
while (io != NULL)
{
if (io->op == IO_INPUT)
{
fdionum = io_num (io, STDIN_FILENO);
fdword = open (io->word, O_RDONLY);
}
else if (io->op == IO_DUPIN)
{
fdionum = io_num (io, STDIN_FILENO);
fdword = (int) strtol (io->word, NULL, 10);
closefdword = 0;
}
else if (io->op == IO_IODUAL)
{
fdionum = io_num (io, STDIN_FILENO);
fdword = open (io->word, rw_flags, perm_flags);
}
else if (io->op == IO_OUTPUT)
{
if (no_clobber && access (io->word, F_OK) != -1)
error (errno, errno, "overwriting existing file");
fdionum = io_num (io, STDOUT_FILENO);
fdword = open (io->word, write_flags, perm_flags);
}
else if (io->op == IO_DUPOUT)
{
fdionum = io_num (io, STDOUT_FILENO);
fdword = (int) strtol (io->word, NULL, 10);
closefdword = 0;
}
else if (io->op == IO_APPEND)
{
fdionum = io_num (io, STDOUT_FILENO);
fdword = open (io->word, append_flags, perm_flags);
}
else if (io->op == IO_CLOBBER)
{
fdionum = io_num (io, STDOUT_FILENO);
fdword = open (io->word, write_flags, perm_flags);
}
if (fdword == -1 || dup2 (fdword, fdionum) == -1)
error (errno, errno, NULL);
if (closefdword)
close (fdword);
io = io->next;
}
}
void
execute_simple_command (command_t cmd, int no_clobber)
{
pid_t child_pid;
int status;
child_pid = fork ();
if (child_pid == 0)
{
open_io (cmd, no_clobber);
execvp (cmd->u.word[0], cmd->u.word);
}
else if (child_pid > 0)
{
waitpid (child_pid, &status, 0);
cmd->status = WEXITSTATUS (status);
}
}
void
execute_subshell_command (command_t cmd, int no_clobber)
{
pid_t child_pid;
int status;
child_pid = fork ();
if (child_pid == 0)
{
open_io (cmd, no_clobber);
execute_command (cmd->u.subshell_command, no_clobber);
exit (cmd->u.subshell_command->status);
}
else if (child_pid > 0)
{
waitpid (child_pid, &status, 0);
cmd->status = status;
}
}
void
execute_command (command_t c, int no_clobber)
{
switch (c->type)
{
case CMD_SEQUENCE: execute_sequence_command (c, no_clobber); break;
case CMD_AND: execute_and_command (c, no_clobber); break;
case CMD_OR: execute_or_command (c, no_clobber); break;
case CMD_PIPE: execute_pipe_command (c, no_clobber); break;
case CMD_SIMPLE: execute_simple_command (c, no_clobber); break;
case CMD_SUBSHELL: execute_subshell_command (c, no_clobber); break;
}
}