-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcmdline.c
More file actions
375 lines (319 loc) · 8.86 KB
/
cmdline.c
File metadata and controls
375 lines (319 loc) · 8.86 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
#include "cmdline.h"
#include <assert.h>
#include <string.h>
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
void line_init(struct line *li) {
assert(li);
memset(li, 0, sizeof(struct line));
}
/**
* Test the validity of command arguments or file names used in redirections
*
* This function is static : it means that it is a local function, accessible only in this source file
*
* @param word pointer on the first char of string to test
*
* @return true if the word is valid, false otherwise
*/
static bool valid_cmdarg_filename(const char *word){
const char *forbidden = "<>&|";// forbidden characters in commands arguments and filenames
size_t lenf = strlen(forbidden);
for (size_t i = 0; i < lenf ; ++i){
char *ptr = strchr(word, forbidden[i]);
if (ptr) {
return false;
}
}
return true;
}
/**
* Print the string "Error while parsing: ", followed by the string "format" to stderr
*
* This function is static : it means that it is a local function, accessible only in this source file.
* The string "format" may contain format specifications that specify how subsequent arguments are
* converted for output. So, this function can be called with a variable number of parameters.
* NB : vfprintf() uses the variable-length argument facilities of stdarg(3)
*
* @param format string
*/
static void parse_error(const char *format, ...) {
va_list ap;
fprintf(stderr, "Error while parsing: ");
va_start(ap, format);
vfprintf(stderr, format, ap);
va_end(ap);
}
/**
* Search a new word in the string "str" from the "index" position
*
* This function is static : it means that it is a local function, accessible only in this source file.
* After the call, "index" contains the position of the last character used plus one.
* If a word is found, it is copied to a dynamically allocated memory space. "pword" is a pointer
* on a pointer which retrieves the address of this dynamically allocated memory space.
*
* @param str pointer on the first char of the line entered by the user
* @param index pointer on the index
* @param pword pointer on a pointer which retrieves the address of this dynamically allocated memory space
*
* @return 0 if a word is found or if the end of the line is reached
* -1 if a malformed line is detected
* -2 if a memory allocation failure occurs
*/
static int line_next_word(const char *str, size_t *index, char **pword) {
assert(str);
assert(index);
assert(pword);
size_t i = *index;
*pword = NULL;
/* eat space */
while (str[i] != '\0' && isspace(str[i])) {
++i;
}
/* check if it is the end of the line */
if (str[i] == '\0') {
*index = i;
return 0;
}
size_t start = i;
size_t end = i;
if (str[i] == '"') {
++start;
do {
++i;
} while (str[i] != '\0' && str[i] != '"');
if (str[i] == '\0') {
parse_error("Malformed line\n");
return -1;
}
assert(str[i] == '"');
end = i;
++i;
}
else {
while (str[i] != '\0' && !isspace(str[i])) {
++i;
}
end = i;
}
*index = i;
/* copy this word */
assert(end >= start);
*pword = calloc(end - start + 1, sizeof(char));
if (*pword == NULL){
fprintf(stderr, "Memory allocation failure\n");
return -2;
}
memcpy(*pword, str + start, end - start);
return 0;
}
int line_parse(struct line *li, const char *str) {
assert(li);
assert(str);
size_t len = strlen(str);
assert(len >= 1);
if (str[len -1] != '\n'){
fprintf(stderr, "The command line is too long\n");
char c = 0;
while (c != '\n'){
c = fgetc(stdin);
}
return -1;
}
size_t index = 0;
size_t curr_n_cmd = 0;
size_t curr_n_arg = 0;
int valret = 0;
for (;;) {
/* get the next word */
char *word;
int err = line_next_word(str, &index, &word);
if (err) {
valret = -1;
break;
}
if (!word) {
break;
}
#ifdef DEBUG
fprintf(stderr, "\tnew word: \"%s\"\n", word);
#endif
if (strcmp(word, "|") == 0) {
free(word);
if (li->background) {
parse_error("No pipe allowed after a '&'\n");
valret = -1;
break;
}
if (li->file_output) {
parse_error("No pipe allowed after an output redirection\n");
valret = -1;
break;
}
if (curr_n_arg == 0){
parse_error("An empty command before a pipe detected\n");
valret = -1;
break;
}
li->cmds[curr_n_cmd].n_args = curr_n_arg;
curr_n_arg = 0;
++curr_n_cmd;
}
else if (strcmp(word, ">") == 0 || strcmp(word, ">>") == 0) {
bool append = strcmp(word, ">>") == 0;
free(word);
if (li->file_output) {
parse_error("Output redirection already defined\n");
valret = -1;
break;
}
if (li->background) {
parse_error("No output redirection allowed after a '&'\n");
valret = -1;
break;
}
err = line_next_word(str, &index, &word);
if (err) {
valret = -1;
break;
}
if (!word) {
parse_error("Waiting for a filename after an output redirection\n");
valret = -1;
break;
}
if (!valid_cmdarg_filename(word)){
parse_error("Filename \"%s\" is not valid\n", word);
free(word);
valret = -1;
break;
}
li->file_output = word;
li->file_output_append = append;
}
else if (strcmp(word, "<") == 0) {
free(word);
if (li->file_input) {
parse_error("Input redirection already defined\n");
valret = -1;
break;
}
if (li->background) {
parse_error("No input redirection allowed after a '&'\n");
valret = -1;
break;
}
if (curr_n_cmd > 0){
parse_error("Input redirection is only allowed for the first command\n");
valret = -1;
break;
}
err = line_next_word(str, &index, &word);
if (err) {
valret = -1;
break;
}
if (!word) {
parse_error("Waiting for a filename after an input redirection\n");
valret = -1;
break;
}
if (!valid_cmdarg_filename(word)){
parse_error("Filename \"%s\" is not valid\n", word);
free(word);
valret = -1;
break;
}
li->file_input = word;
}
else if (strcmp(word, "&") == 0) {
free(word);
if (li->background) {
parse_error("More than one '&' detected\n");
valret = -1;
break;
}
if (curr_n_arg == 0){
parse_error("An empty command before '&' detected\n");
valret = -1;
break;
}
li->background = true;
}
else {
if (li->background) {
free(word);
parse_error("No more commands allowed after a '&'\n");
valret = -1;
break;
}
if (curr_n_cmd == MAX_CMDS) {
free(word);
parse_error("Too much commands. Max: %i\n", MAX_CMDS);
valret = -1;
break;
}
if (curr_n_arg == MAX_ARGS) {
free(word);
parse_error("Too much arguments. Max: %i\n", MAX_ARGS);
valret = -1;
break;
}
if (!valid_cmdarg_filename(word)){
parse_error("Argument \"%s\" is not valid\n", word);
free(word);
valret = -1;
break;
}
li->cmds[curr_n_cmd].args[curr_n_arg] = word;
++curr_n_arg;
}
} //end of the loop for
if (!valret && curr_n_arg == 0) {
if (curr_n_cmd > 0){
parse_error("An empty command detected\n");
valret = -1;
}
// in a real shell, "< fic" is equivalent to "test -r fic"
else if (li->file_input){
parse_error("Missing first command\n");
valret = -1;
}
// in a real shell, "> fic" :
// - creates the regular file "fic" if it does not exist,
// - and truncates it if it already exists
// in a real shell, ">> fic" :
// - creates the regular file "fic" if it does not exist,
// - and doesn't truncate it if it already exists
else if (li->file_output){
parse_error("Missing last command\n");
valret = -1;
}
}
if (curr_n_arg != 0) {
li->cmds[curr_n_cmd].n_args = curr_n_arg;
++curr_n_cmd;
}
li->n_cmds = curr_n_cmd;
return valret;
}
void line_reset(struct line *li) {
assert(li);
for (size_t i = 0; i < li->n_cmds; ++i) {
for (size_t j = 0; j < li->cmds[i].n_args; ++j) {
free(li->cmds[i].args[j]);
li->cmds[i].args[j] = NULL; // useless here because of the call of memset()
}
}
if (li->file_input) {
free(li->file_input);
li->file_input = NULL; // useless here because of the call of memset()
}
if (li->file_output) {
free(li->file_output);
li->file_output = NULL; // useless here because of the call of memset()
}
memset(li, 0, sizeof(struct line));
}