-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreadcmd.c
More file actions
298 lines (266 loc) · 4.92 KB
/
readcmd.c
File metadata and controls
298 lines (266 loc) · 4.92 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
/*
* Copyright (C) 2002, Simon Nieuviarts
*/
/*
* Backgrounding added. [PM] Ajout d'une rustine nécessaire : lignes 153 et 293 commentées
*/
#include <stdio.h>
#include <stddef.h>
#include <stdlib.h>
#include <errno.h>
#include <limits.h>
#include <string.h>
#include "readcmd.h"
static void memory_error(void)
{
errno = ENOMEM;
perror(0);
exit(1);
}
static void *xmalloc(size_t size)
{
void *p = malloc(size);
if (!p) memory_error();
return p;
}
static void *xrealloc(void *ptr, size_t size)
{
void *p = realloc(ptr, size);
if (!p) memory_error();
return p;
}
/* Read a line from standard input and put it in a char[] */
static char *readline(void)
{
size_t buf_len = 16;
char *buf = xmalloc(buf_len * sizeof(char));
if (fgets(buf, buf_len, stdin) == NULL) {
free(buf);
return NULL;
}
do {
size_t l = strlen(buf);
if ((l > 0) && (buf[l-1] == '\n')) {
l--;
buf[l] = 0;
return buf;
}
if (buf_len >= (INT_MAX / 2)) memory_error();
buf_len *= 2;
buf = xrealloc(buf, buf_len * sizeof(char));
if (fgets(buf + l, buf_len - l, stdin) == NULL) return buf;
} while (1);
}
/* Split the string in words, according to the simple shell grammar. */
static char **split_in_words(char *line)
{
char *cur = line;
char **tab = 0;
size_t l = 0;
char c;
while ((c = *cur) != 0) {
char *w = 0;
char *start;
switch (c) {
case ' ':
case '\t':
/* Ignore any whitespace */
cur++;
break;
case '<':
w = "<";
cur++;
break;
case '>':
w = ">";
cur++;
break;
case '|':
w = "|";
cur++;
break;
case '&':
w = "&";
cur++;
break;
default:
/* Another word */
start = cur;
while (c) {
c = *++cur;
switch (c) {
case 0:
case ' ':
case '\t':
case '<':
case '>':
case '|':
case '&':
c = 0;
break;
default: ;
}
}
w = xmalloc((cur - start + 1) * sizeof(char));
strncpy(w, start, cur - start);
w[cur - start] = 0;
}
if (w) {
tab = xrealloc(tab, (l + 1) * sizeof(char *));
tab[l++] = w;
}
}
tab = xrealloc(tab, (l + 1) * sizeof(char *));
tab[l++] = 0;
return tab;
}
static void freeseq(char ***seq)
{
int i, j;
for (i=0; seq[i]!=0; i++) {
char **cmd = seq[i];
for (j=0; cmd[j]!=0; j++) free(cmd[j]);
free(cmd);
}
free(seq);
}
/* Free the fields of the structure but not the structure itself */
static void freecmd(struct cmdline *s)
{
if (s->in) free(s->in);
if (s->out) free(s->out);
// if (s->backgrounded) free(s->backgrounded);
if (s->seq) freeseq(s->seq);
}
struct cmdline *readcmd(void)
{
static struct cmdline *static_cmdline = 0;
struct cmdline *s = static_cmdline;
char *line;
char **words;
int i;
char *w;
char **cmd;
char ***seq;
size_t cmd_len, seq_len;
line = readline();
if (line == NULL) {
if (s) {
freecmd(s);
free(s);
}
return static_cmdline = 0;
}
cmd = xmalloc(sizeof(char *));
cmd[0] = 0;
cmd_len = 0;
seq = xmalloc(sizeof(char **));
seq[0] = 0;
seq_len = 0;
words = split_in_words(line);
free(line);
if (!s)
static_cmdline = s = xmalloc(sizeof(struct cmdline));
else
freecmd(s);
s->err = 0;
s->in = 0;
s->out = 0;
s->backgrounded = 0;
s->seq = 0;
i = 0;
while ((w = words[i++]) != 0) {
switch (w[0]) {
case '&':
if(s->backgrounded){
s->err = "error on &";
goto error;
}
s->backgrounded = &w[0];
break;
case '<':
/* Tricky : the word can only be "<" */
if (s->in) {
s->err = "only one input file supported";
goto error;
}
if (words[i] == 0) {
s->err = "filename missing for input redirection";
goto error;
}
s->in = words[i++];
break;
case '>':
/* Tricky : the word can only be ">" */
if (s->out) {
s->err = "only one output file supported";
goto error;
}
if (words[i] == 0) {
s->err = "filename missing for output redirection";
goto error;
}
s->out = words[i++];
break;
case '|':
/* Tricky : the word can only be "|" */
if (cmd_len == 0) {
s->err = "misplaced pipe";
goto error;
}
seq = xrealloc(seq, (seq_len + 2) * sizeof(char **));
seq[seq_len++] = cmd;
seq[seq_len] = 0;
cmd = xmalloc(sizeof(char *));
cmd[0] = 0;
cmd_len = 0;
break;
default:
cmd = xrealloc(cmd, (cmd_len + 2) * sizeof(char *));
cmd[cmd_len++] = w;
cmd[cmd_len] = 0;
}
}
if (cmd_len != 0) {
seq = xrealloc(seq, (seq_len + 2) * sizeof(char **));
seq[seq_len++] = cmd;
seq[seq_len] = 0;
} else if (seq_len != 0) {
s->err = "misplaced pipe";
i--;
goto error;
} else
free(cmd);
free(words);
s->seq = seq;
return s;
error:
while ((w = words[i++]) != 0) {
switch (w[0]) {
case '<':
case '>':
case '|':
case '&':
break;
default:
free(w);
}
}
free(words);
freeseq(seq);
for (i=0; cmd[i]!=0; i++) free(cmd[i]);
free(cmd);
if (s->in) {
free(s->in);
s->in = 0;
}
if (s->out) {
free(s->out);
s->out = 0;
}
if (s->backgrounded) {
// free(s->backgrounded);
s->out = 0;
}
return s;
}