This repository has been archived by the owner on Feb 16, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathutility.c
430 lines (377 loc) · 9.88 KB
/
utility.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
/* Copyright (c) 2009 Sebastian Nowicki <[email protected]>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include "utility.h"
/* Function: _strcpy_partial
Copy a substring, from start to end.
Parameters:
string - The string to be copied.
start - A marker indicating the beginning of the substring.
end - A marker indicating the end of a substring.
Returns:
A copy of the string between start and end. The returned string should be
deallocated by the caller.
*/
static char *_strcpy_partial(char *string, char *start, char *end);
/* Function: _find_next_substitution
Locate the next word substitution within a string.
Example:
(start code)
char *text = "foo$bar baz";
char *start = NULL;
char *end = NULL;
int status = 0;
status = _find_next_substitution(text, &start, &end);
// start -> '$'
// end -> 'r'
// status -> 1
(end)
Parameters:
string - The string to be searched.
start - The address where the location of the next substitution should be
stored. The location will always point to the sigil ('$').
end - The address where the location of the end of the word should be stored.
Returns:
True (1) on success, otherwise false (0).
*/
static int _find_next_substitution(char *string, char **start, char **end);
/* Function: _substitute_words
Substitute variables with their values
Parameters:
table - A symbol table containing the values of variables
string - The string to be parsed.
Returns:
A string with substituted words, or NULL on error. The returned string
should be deallocated by the caller.
See Also:
<sh_parse_word()>
*/
static char *_substitute_words(table_t *table, char *string);
/* Function: _array_cat
Concatenate an array of strings to a single string.
Parameters:
array - An array of strings.
Returns:
A string consisting of all elements in array, delimited by a space, or NULL
on error, or if array is empty. The returned string should be deallocated by
the caller.
*/
static char *_array_cat(char **array);
static char *_strcpy_partial(char *string, char *start, char *end)
{
char *result;
size_t len = end - start + 1;
result = malloc((len + 1) * sizeof(*result));
result = strncpy(result, string, len);
result[len] = '\0';
return result;
}
static char *_array_cat(char **array)
{
char *result = NULL;
size_t size = 0;
int i;
if(array == NULL || array[0] == NULL) {
return result;
}
for(i = 0; array[i] != NULL; i++) {
size += strlen(array[i]);
}
size += i - 1; /* spaces between elements */
result = malloc(sizeof(*result) * (size + 1));
result[size] = '\0';
for(i = 0; array[i] != NULL; i++) {
result = strncat(result, array[i], size);
if(array[i + 1] != NULL) {
result = strncat(result, " ", size);
}
}
return result;
}
static size_t _array_size(const char *array)
{
const char *str_ptr = array;
char quote_char;
int in_quote = 0;
size_t count = 0;
int end_of_array;
/* Skip left parenthesis, otherwise attempt to count elements anyway */
if(*str_ptr == '(') {
str_ptr++;
}
end_of_array = *array == ')' || *array == '\0';
while(!end_of_array) {
switch(*str_ptr) {
/* Entering or exitting quote */
case '\'':
case '"':
if(*(str_ptr - 1) != '\\') {
in_quote = !(in_quote && *str_ptr == quote_char);
quote_char = *str_ptr;
}
break;
/* Element separator, if not in quote */
case ' ':
case '\t':
case '\n':
if(!in_quote && *(str_ptr - 1) != '\\') {
count++;
}
break;
case ')':
if(!in_quote && *(str_ptr - 1) != '\\') {
count++;
end_of_array = 1;
}
break;
case '\0':
end_of_array = 1;
break;
default:
break;
}
str_ptr++;
}
return count;
}
char **sh_split_array(char *string)
{
size_t count_elem;
int in_quote = 0;
unsigned int elem;
char quote_char = '\0';
char *str_cpy;
char *str_ptr;
char *start_ptr;
char **array;
int end_of_array;
/* Copy string as we will be mutating it */
str_cpy = strdup(string);
count_elem = _array_size(str_cpy);
array = malloc((count_elem + 1) * sizeof(*array));
array[count_elem] = NULL;
elem = 0;
end_of_array = count_elem == 0;
/* Skip the left parenthesis */
str_ptr = str_cpy + 1;
start_ptr = str_ptr;
/* Loop until end of string, and extract the elements */
while(!end_of_array) {
switch(*str_ptr) {
/* Entering or exitting quote */
case '\'':
case '"':
if(*(str_ptr - 1) != '\\') {
in_quote = !(in_quote && *str_ptr == quote_char);
quote_char = *str_ptr;
}
break;
/* Element separator, if not in quote */
case ' ':
case '\t':
case '\n':
if(!in_quote && *(str_ptr - 1) != '\\') {
*str_ptr = '\0';
/* Skip multiple spaces */
if(strlen(start_ptr) != 1 && !isspace(*start_ptr)) {
array[elem] = strdup(start_ptr);
elem++;
}
start_ptr = str_ptr + 1;
}
break;
case ')':
if(!in_quote && *(str_ptr - 1) != '\\') {
*str_ptr = '\0';
array[elem] = strdup(start_ptr);
end_of_array = 1;
}
break;
case '\0':
end_of_array = 1;
break;
default:
break;
}
str_ptr++;
}
free(str_cpy);
return array;
}
/* TODO: Unquote strings in the middle of a string, e.g.
* "foo'bar baz'zer" */
char *sh_unquote(char *string)
{
size_t len = 0;
char *str_ptr = string;
char *return_string = NULL;
if(string == NULL) {
return NULL;
}
if(*str_ptr == '\'' || *str_ptr == '"') {
str_ptr++;
len = strlen(str_ptr);
return_string = malloc(len * sizeof(*string));
return_string = strncpy(return_string, str_ptr, len - 1);
return_string[len - 1] = '\0';
} else {
/* Since we allocate when the string is quoted, we should do so
* when it's not for consistency. */
return_string = strdup(string);
}
return return_string;
}
static int _find_next_substitution(char *string, char **start, char **end)
{
char *str_ptr;
int escaped = 0;
int in_literal_quote = 0;
int in_brace = 0;
int variable = 0;
int found = 0;
int end_of_word;
*start = NULL;
*end = NULL;
for(str_ptr = string; *str_ptr != '\0' && !found; str_ptr++) {
switch(*str_ptr) {
case '\'':
in_literal_quote = !escaped && !in_literal_quote;
escaped = 0;
break;
case '\\':
escaped = !escaped;
break;
case '$':
if(!escaped && !in_literal_quote) {
variable = 1;
*start = str_ptr;
}
escaped = 0;
break;
case '{':
in_brace = variable && !escaped;
break;
case '}':
if(in_brace) {
*end = str_ptr;
found = 1;
}
break;
default:
end_of_word = !isalnum(*(str_ptr + 1))
&& *(str_ptr + 1) != '_'
|| *(str_ptr + 1) == '\0';
if(variable && !in_brace && end_of_word) {
*end = str_ptr;
found = 1;
}
escaped = 0;
break;
}
}
return found;
}
static char *_substitute_words(table_t *table, char *string)
{
size_t len = 0;
size_t result_len;
char *str_ptr = string;
char *start = NULL;
char *end = NULL;
char *word = NULL;
char *result = NULL;
char *value = NULL;
int free_value = 0;
symbol_t *symbol = NULL;
if(!_find_next_substitution(str_ptr, &start, &end)) {
return strdup(string);
} else {
/* The string has to be NULL terminated for strncpy() to work */
result = malloc(sizeof(*result));
result[0] = '\0';
result_len = 1;
}
while(_find_next_substitution(str_ptr, &start, &end)) {
/* Skip word identifier marks ("${...}") */
if(*(start + 1) == '{' && *end == '}') {
word = _strcpy_partial(start + 2, start + 2, end - 1);
} else {
word = _strcpy_partial(start + 1, start + 1, end);
}
symbol = table_lookupr(table, word);
free(word);
if(symbol != NULL) {
if(symbol_type(symbol) == kSymbolTypeArray) {
value = _array_cat(symbol_array(symbol));
free_value = 1;
} else {
value = symbol_string(symbol);
}
len = strlen(value);
result_len += start - str_ptr + len;
result = realloc(result, result_len * sizeof(*result));
/* Concatenate the string preceeding substitution */
result = strncat(result, str_ptr, (start - str_ptr) * sizeof(*result));
result = strncat(result, value, len);
if(free_value) {
free(value);
free_value = 0;
value = NULL;
}
}
str_ptr = end + 1;
}
/* Append the remainder of the string */
if(strlen(str_ptr) > 0) {
len = strlen(str_ptr);
result_len += len;
result = realloc(result, result_len * sizeof(*result));
result = strncat(result, str_ptr, len);
}
return result;
}
char **sh_parse_array(table_t *table, char *string)
{
char **result;
char **array_ptr;
char *parsed;
result = sh_split_array(string);
if(result != NULL && table != NULL) {
for(array_ptr = result; *array_ptr != NULL; array_ptr++) {
parsed = sh_parse_word(table, *array_ptr);
free(*array_ptr);
*array_ptr = parsed;
parsed = NULL;
}
}
return result;
}
char *sh_parse_word(table_t *table, char *string)
{
char *substituted;
char *parsed;
substituted = _substitute_words(table, string);
parsed = sh_unquote(substituted);
free(substituted);
return parsed;
}