-
Notifications
You must be signed in to change notification settings - Fork 0
/
expansion.c
511 lines (476 loc) · 14.1 KB
/
expansion.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
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
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
#include "expansion.h"
int is_escaped(char *ch, char *head)
{
char *p;
int backslash_count = 0;
if(!ch || !head || ch <= head) return false;
for(p = ch - 1; p >= head; p--)
{
if(*p == '\\') backslash_count++;
else break;
}
if(backslash_count%2) return true;
else return false;
}
char *tilde_expansion(char *str)
{
int i = 0;
// It is ensured that the tilde expansion is done only once
int done = 0;
char buffer[MAX_COMMAND_LEN] = {};
char *p;
char *result;
for(p = str; *p; p++)
{
if(*p == '~' && !is_escaped(p, str) &&!done)
{
if(p == str && p[1] == 0)
{
// [^]~[\0]
strcat(buffer, "${HOME}");
i += strlen("${HOME}");
done = 1;
continue;
}
else if(p[1] == '/')
{
// ~/
strcat(buffer, "${HOME}");
i += strlen("${HOME}");
done = 1;
continue;
}
else if(p == str && p[1] == '+' && p[2] == 0)
{
// [^]~+[\0]
strcat(buffer, "${PWD}");
i += strlen("${PWD}");
p++;
done = 1;
if(!*p) break;
continue;
}
else if(p[1] == '+' && p[2] == '/')
{
// ~+/
strcat(buffer, "${PWD}");
i += strlen("${PWD}");
p++;
done = 1;
if(!*p) break;
continue;
}
}
buffer[i++] = *p;
}
result = (char*)malloc((strlen(buffer) + 1) * sizeof(char));
strcpy(result, buffer);
return result;
}
// $#
int is_myshell_argc(char *str)
{
if(!str || !*str) return 0;
if(str[0] == '$' && str[1] == '#') return 1;
return 0;
}
// $[@|*]
int is_myshell_argv(char *str)
{
if(!str || !*str) return 0;
if(str[0] == '$' && (str[1] == '@' || str[1] == '*'))
return 1;
return 0;
}
int is_latest_status(char *str)
{
if(!str || !*str) return 0;
if(str[0] == '$' && str[1] == '?')
return 1;
return 0;
}
// $[digit]
int is_myshell_arg(char *str)
{
if(!str || !*str) return 0;
if(str[0] == '$' && isdigit(str[1])) return 1;
return 0;
}
// $[identifier]
int is_myshell_unbraced_var(char *str)
{
if(!str || !*str) return 0;
if(str[0] == '$' && (isalpha(str[1]) || str[1] == '_'))
return 1;
return 0;
}
// ${[string]}
int is_myshell_var(char *str)
{
char *p = str;
if(!str || !*str) return 0;
if(str[0] != '$' || !str[1]) return 0;
str++;
if(*str != '{') return 0;
while(*p)
{
if(*p == '}' && !is_escaped(p, str)) return 1;
p++;
}
return 0;
}
// The return value is the expanded string with allocated space
// If an error occur, return NULL
// The first slot, dollar_pos is the pointer to '$' in the string
// After this procedure is done,
// The last character of the expanded sequence is passed in end_pos
char *expand_myshell_argc(char *dollar_pos, char **end_pos)
{
char buffer[MAX_HOSTNAME_LEN] = {};
int arg_count;
char *result;
if(!dollar_pos || !end_pos) return NULL;
for(arg_count = 0; ; arg_count++)
{
// This is the string in which stores the number
char query_num_str[MAX_HOSTNAME_LEN];
char *varv;
sprintf(query_num_str, "%d", arg_count);
varv = get_variable(query_num_str, 0);
if(!varv || strlen(varv) == 0)
{
if(varv) free(varv);
break;
}
free(varv);
}
sprintf(buffer, "%d", arg_count);
result = (char*)malloc((strlen(buffer) + 1) * sizeof(char));
if(!result) exit(MEM_ALLOC_ERR_);
strcpy(result, buffer);
*end_pos = dollar_pos + 1;
return result;
}
// Still the return value is the expanded string with allocated space
// See definition of expand_myshell_argc
char *expand_myshell_argv(char *dollar_pos, char **end_pos)
{
char buffer[MAX_COMMAND_LEN] = {};
int arg_count;
char *result;
if(!dollar_pos || !end_pos) return NULL;
for(arg_count = 0; ; arg_count++)
{
// This is the string in which stores the number
char query_num_str[MAX_HOSTNAME_LEN];
char *varv;
sprintf(query_num_str, "%d", arg_count);
varv = get_variable(query_num_str, 0);
if(!varv || strlen(varv) == 0)
{
if(varv) free(varv);
break;
}
// append
strcat(buffer, varv);
strcat(buffer, " ");
free(varv);
}
// Remove the last space
if(*buffer) buffer[strlen(buffer) - 1] = 0;
result = (char*)malloc((strlen(buffer) + 1) * sizeof(char));
if(!result) exit(MEM_ALLOC_ERR_);
strcpy(result, buffer);
*end_pos = dollar_pos + 1;
return result;
}
char *expand_latest_status(char *dollar_pos, char **end_pos)
{
char buffer[MAX_HOSTNAME_LEN] = {};
char *result;
if(!dollar_pos || !end_pos) return NULL;
sprintf(buffer, "%d", LATEST_STATUS);
result = (char*)malloc((strlen(buffer) + 1) * sizeof(char));
if(!result) exit(MEM_ALLOC_ERR_);
strcpy(result, buffer);
*end_pos = dollar_pos + 1;
return result;
}
char *expand_myshell_arg(char *dollar_pos, char **end_pos)
{
char digit[2] = {};
if(!dollar_pos || !end_pos) return NULL;
digit[0] = dollar_pos[1];
*end_pos = dollar_pos + 1;
return get_variable(digit, 0);
}
char *expand_myshell_unbraced_var(char *dollar_pos, char **end_pos)
{
char key_buffer[MAX_HOSTNAME_LEN] = {};
int i = 0;
char *p = dollar_pos;
if(!dollar_pos || !end_pos) return NULL;
// The first element of an identifier
p++;
key_buffer[i++] = *p;
p++;
// Read in the key, greedily
while(*p)
{
if(isalpha(*p) || isdigit(*p) || *p == '_')
{
key_buffer[i++] = *p;
p++;
}
else break;
}
*end_pos = p - 1;
return get_variable(key_buffer, 0);
}
// This is the major function to handle things like ${}
// Damn it!
char *expand_myshell_var(char *dollar_pos, char **end_pos)
{
char *p = dollar_pos;
char braced_buffer[MAX_HOSTNAME_LEN] = {};
char bracket_buffer[MAX_HOSTNAME_LEN] = {};
int i = 0;
int sharp = 0;
int all_elements = 0;
int index_of_element = 0;
char *left_bracket_pos;
char *right_bracket_pos;
char left_bracket[2] = {'['};
char right_bracket[2] = {']'};
if(!dollar_pos || !end_pos) return NULL;
// Read in the braced part
p += 2;
while(*p != '}' && *p && !is_escaped(p, dollar_pos))
{
braced_buffer[i++] = *p;
p++;
}
*end_pos = p;
// Judge if the first character is a sharp
if(braced_buffer[0] == '#')
{
sharp = 1;
strcpy(braced_buffer, braced_buffer + 1);
}
// Find the brackets
left_bracket_pos = strstr(braced_buffer, left_bracket);
right_bracket_pos = strstr(braced_buffer, right_bracket);
// Left and right do not match
if((left_bracket_pos || right_bracket_pos)
&& !(left_bracket_pos && right_bracket_pos))
return NULL;
// Get the string in the brackets
if(left_bracket_pos && right_bracket_pos)
{
memcpy(bracket_buffer, left_bracket_pos + 1,
right_bracket_pos - left_bracket_pos - 1);
bracket_buffer[right_bracket_pos - left_bracket_pos - 1] = 0;
}
// If the string in the brackets indicates an all_element
if(bracket_buffer[0] == '*' || bracket_buffer[0] == '@')
all_elements = 1;
// Else if it indicates an index
else if(isdigit(bracket_buffer[0]))
sscanf(bracket_buffer, "%d", &index_of_element);
// Else it is illegal
else if(bracket_buffer[0]) return NULL;
// Make the braced buffer stores the identifier only
if(left_bracket_pos) *left_bracket_pos = 0;
// Clean up the braced buffer, remove the blank characters
{
char *blank_removed = remove_extra_blank(braced_buffer);
if(blank_removed)
{
blank_removed[strlen(blank_removed) - 1] = 0;
strcpy(braced_buffer, blank_removed);
}
else return NULL;
free(blank_removed);
blank_removed = NULL;
if(!is_identifier(braced_buffer) && !isdigit(braced_buffer[0]))
return NULL;
}
// If it is to find out the number of elements in a list
if(all_elements && sharp)
{
return get_variable(braced_buffer, ElementCount);
}
else if(sharp)
{
char *result = get_variable(braced_buffer, index_of_element);
char result_buffer[MAX_HOSTNAME_LEN];
if(!result) return NULL;
sprintf(result_buffer, "%d", (int)strlen(result));
free(result);
result = (char*)malloc((strlen(result_buffer) + 1) * sizeof(char));
if(!result) exit(MEM_ALLOC_ERR_);
strcpy(result, result_buffer);
return result;
}
else
{
if(all_elements) index_of_element = ListAll;
return get_variable(braced_buffer, index_of_element);
}
}
char *var_expansion(char *str)
{
int i = 0;
char buffer[MAX_COMMAND_LEN] = {};
char *p;
char *result;
for(p = str; *p; p++)
{
// Start of the expansion sequence
if(*p == '$' && !is_escaped(p, str))
{
if(is_myshell_argc(p))
{
char *next_p;
char *expanded = expand_myshell_argc(p, &next_p);
if(!expanded)
{
char err_info[MAX_HOSTNAME_LEN] = {};
strcpy(err_info, "Expansion error (Unknown argc expr): ");
strncat(err_info, p, 4);
strcat(err_info, "...");
print_myshell_err(err_info);
return NULL;
}
strcat(buffer, expanded);
i = strlen(buffer);
p = next_p;
free(expanded);
continue;
}
else if(is_myshell_argv(p))
{
char *next_p;
char *expanded = expand_myshell_argv(p, &next_p);
if(!expanded)
{
char err_info[MAX_HOSTNAME_LEN] = {};
strcpy(err_info, "Expansion error (Unknown argv expr): ");
strncat(err_info, p, 4);
strcat(err_info, "... ");
print_myshell_err(err_info);
return NULL;
}
strcat(buffer, expanded);
i = strlen(buffer);
p = next_p;
free(expanded);
continue;
}
else if(is_latest_status(p))
{
char *next_p;
char *expanded = expand_latest_status(p, &next_p);
if(!expanded)
{
char err_info[MAX_HOSTNAME_LEN] = {};
strcpy(err_info, "Expansion error (Unknown latest status): ");
strncat(err_info, p, 4);
strcat(err_info, "... ");
print_myshell_err(err_info);
return NULL;
}
strcat(buffer, expanded);
i = strlen(buffer);
p = next_p;
free(expanded);
continue;
}
else if(is_myshell_arg(p))
{
char *next_p;
char *expanded = expand_myshell_arg(p, &next_p);
if(!expanded)
{
char err_info[MAX_HOSTNAME_LEN] = {};
strcpy(err_info, "Expansion error (Unknown arg expr): ");
strncat(err_info, p, 4);
strcat(err_info, "... ");
print_myshell_err(err_info);
return NULL;
}
strcat(buffer, expanded);
i = strlen(buffer);
p = next_p;
free(expanded);
continue;
}
else if(is_myshell_unbraced_var(p))
{
char *next_p;
char *expanded = expand_myshell_unbraced_var(p, &next_p);
if(!expanded)
{
char err_info[MAX_HOSTNAME_LEN] = {};
strcpy(err_info, "Expansion error (Unknown unbraced var): ");
strncat(err_info, p, 4);
strcat(err_info, "... ");
print_myshell_err(err_info);
return NULL;
}
strcat(buffer, expanded);
i = strlen(buffer);
p = next_p;
free(expanded);
continue;
}
else if(is_myshell_var(p))
{
char *next_p;
char *expanded = expand_myshell_var(p, &next_p);
if(!expanded)
{
char err_info[MAX_HOSTNAME_LEN] = {};
strcpy(err_info, "Expansion error (Unknown var expr): ");
strncat(err_info, p, 4);
strcat(err_info, "... ");
print_myshell_err(err_info);
return NULL;
}
strcat(buffer, expanded);
i = strlen(buffer);
p = next_p;
free(expanded);
continue;
}
// Else simply output the string
}
buffer[i++] = *p;
}
result = (char*)malloc((strlen(buffer) + 1) * sizeof(char));
strcpy(result, buffer);
return result;
}
char *escape_expansion(char *str)
{
char buffer[MAX_COMMAND_LEN] = {};
int i = 0;
char *p = str;
char *result;
if(!str) return NULL;
for(p = str; *p; p++)
{
if(*p == '\\')
{
p++;
if(!*p) break;
buffer[i++] = *p;
continue;
}
buffer[i++] = *p;
}
result = (char*)malloc((strlen(buffer) + 1) * sizeof(char));
if(!result) exit(MEM_ALLOC_ERR_);
strcpy(result, buffer);
return result;
}