summaryrefslogtreecommitdiff
path: root/42sh/src/lexer/expansion.c
blob: d648009209ab265c7ecefecdcc48ef0914056fc4 (plain)
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
#include <ctype.h>
#include <lexer/expansion.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>

#include "utils/env.h"

#define BUFFER_SIZE 51

#define ERROR_CHECK(MSG)                                                       \
    if (str[*i] == '\0')                                                       \
    {                                                                          \
        string_free(input);                                                    \
        return clean_exit(MSG, error);                                         \
    }

#define DQUOTEESCAPED "$\\\n`\""
// Identifies double-quote escaped characters
#define ISDQUOTEESCAPED(C) strchr(DQUOTEESCAPED, (int)C)

#define SPECIAL "@*?$#"
// Identifies special variable names
#define ISSPECIAL(C) strchr(SPECIAL, (int)C)

// error = 1 indicates a missing }
// error = 0 and NULL returned indicates an unrecognized var name
// error = 0 and anything else than NULL returned is the var name
static struct string *get_var_name(char *str, int *error)
{
    struct string *res = string_create(NULL);
    int i = 0;
    if (str[i] == '{')
    {
        while (str[i] && str[i] != '}')
        {
            if (str[i] == '\\' && str[i + 1] == '}')
                i++;
            string_pushc(res, str[i]);
            i++;
        }
        if (!str[i])
        {
            string_free(res);
            *error = 1;
            return NULL;
        }
        *error = 0;
        return res;
    }
    else if (ISSPECIAL(str[i]) || isdigit(str[i]))
    {
        string_pushc(res, str[i]);
        *error = 0;
        return res;
    }
    else if (!isalpha(str[i]))
    {
        *error = 0;
        string_free(res);
        return NULL;
    }
    else
    {
        while (isalnum(str[i]) || str[i] == '_')
            string_pushc(res, str[i++]);
        return res;
    }
}

// Useful to automate the same exit process accross the few functions
// that often do this
static struct string *clean_exit(char *txt, int *error)
{
    fprintf(stderr, "%s", txt);
    *error = 1;
    return NULL;
}

// Creates the fork() in order to make a subshell for the command expansion
// (cf section 2.6.3 of the SCL)
// Only called in expand_substitution()
static struct string *fork_subshell(struct string *input, int j, char *str,
                                    int *error)
{
    int fds[2];
    if (pipe(fds) == -1)
    {
        return clean_exit("pipe() faild to create 2 fds\n", error);
    }

    struct string *output;
    pid_t child = fork();

    if (child == -1)
    {
        // Fork not working
        return clean_exit("fork() faild to produce a children\n", error);
    }
    else if (child)
    {
        // Parent process
        close(fds[1]);

        output = string_create(NULL);

        char buff[BUFFER_SIZE];

        buff[BUFFER_SIZE - 1] = 0;

        int r;

        int status;
        waitpid(child, &status, 0);

        // Check if the child terminated normally
        if (!WIFEXITED(status))
        {
            close(fds[0]);
            string_free(output);
            return clean_exit("Child process failed miserably\n", error);
        }

        while ((r = read(fds[0], buff, 50)))
        {
            buff[r] = 0;
            if (!string_pushstr(output, buff))
            {
                string_free(output);
                return clean_exit("Failed to transfer from pipe\n", error);
            }
        }

        close(fds[0]);
        return output;
    }
    else
    {
        // Child process
        str += j;
        close(fds[0]);

        if (dup2(fds[1], STDOUT_FILENO) == -1)
        {
            // We are forced to return NULL
            // how are we going to know if something wnet wrong ?
            exit(-1);
        }

        _process_input(input);

        close(fds[1]);
        exit(1);
    }
}

static int look_for_next(char *in, int i, char c)
{
    int escaped = 0;
    while (in[i] && (in[i] != c || (escaped && in[i] == c)))
    {
        if (in[i] == '\\')
            escaped ^= 1;
        else
            escaped = 0;
        i++;
    }
    return i;
}

// Removes all the <newline> characters at the end of the string obtained by
// the command substitution (Also section 2.6.3 of the SCL)
static void trimming_newline(struct string *txt)
{
    if (!txt->length)
    {
        return;
    }
    char *str = txt->data;
    size_t len = txt->length;

    size_t i = len - 1;
    while (str[i] == '\n')
    {
        str[i] = 0;
        len--;
    }

    // I am scared and I know this isn't useful but just in case
    txt->data = str;
    txt->length = len;
}

// Performs the substitution (forks and get back the stdout)
struct string *expand_substitution(char *str, int *i, int *error, char delim)
{
    int j = *i;
    struct string *input = string_create(NULL);
    if (input == NULL)
    {
        return clean_exit("Could not create string for input\n", error);
    }

    if (delim == '`')
    {
        *i = look_for_next(str, j, delim);

        ERROR_CHECK("Could not match `\n")

        str[*i] = '\0';
    }
    // Sadly, there is no other way around this
    else
    {
        int escaped = 0;
        int par_count = 1;

        while (str[*i] != 0)
        {
            if (str[*i] == '\\')
            {
                escaped ^= 1;
            }
            else if (str[*i] == '\'')
            {
                (*i) += 1;
                while (str[*i] != '\0' && str[*i] != '\'')
                {
                    (*i) += 1;
                }
                ERROR_CHECK("Missing matching '\n")
            }
            else if ((str[*i] == '\"' || str[*i] == '`') && !escaped)
            {
                (*i) += 1;
                *i = look_for_next(str, *i, str[(*i) - 1]);

                ERROR_CHECK("Missing matching `\n")
            }
            else if (str[*i] == '(' && !escaped)
            {
                par_count++;
            }
            else if (str[*i] == delim && !escaped)
            {
                par_count--;
                if (!par_count)
                {
                    str[*i] = 0;
                    break;
                }
            }
            else
            {
                escaped = 0;
            }

            (*i)++;
        }
    }

    string_pushstr(input, str + j);
    struct string *output = fork_subshell(input, j, str, error);
    string_free(input);

    trimming_newline(output);
    str[*i] = delim;
    return output;
}

static int expand_var(struct string *res, char *input, int i)
{
    // Will only be called after a '$' was read

    int e = 0;
    struct string *name = get_var_name(input + i + 1, &e);

    if (e)
    {
        string_free(name);
        fprintf(stderr, "Missing } in variable expansion\n");
        return -1;
    }
    else if (name == NULL)
    {
        string_pushc(res, input[i]);
        i++;
    }
    else
    {
        // Get the value associated to the name
        char *value = env_get(name->data);
        // Concatenate the strings if the variable has a value
        if (value)
            string_pushstr(res, value);
        if (input[++i] == '{')
            i += 2;
        i += name->length;
        string_free(name);
    }
    return i;
}

static int expand_dquotes(char *input, int i, struct string *res)
{
    while (input[i] != '"')
    {
        if (input[i] == '$')
        {
            if ((i = i + expand_var(res, input, i)) == -1)
            {
                string_free(res);
                return -1;
            }
            continue;
        }
        if ((input[i] == '`' || (input[i] == '$' && input[i + 1] == '(')))
        {
            int e = 0;
            i += (input[i] == '$' ? 2 : 1);
            struct string *output =
                expand_substitution(input, &i, &e, input[i]);
            if (!e)
            {
                string_free(res);
                return -1;
            }

            // +1 for the last parenthesis/backquote
            i++;
            string_catenate(res, output);
            continue;
        }
        if (input[i] == '\\' && ISDQUOTEESCAPED(input[i + 1]))
            i++;
        string_pushc(res, input[i]);
        i++;
    }
    return i;
}

struct string *expand_word(struct string *word)
{
    char *input = word->data;
    int escape = 0;
    struct string *res = string_create(NULL);
    for (int i = 0; input[i]; i++)
    {
        if (!escape && input[i] == '\'')
        {
            while (input[++i] != '\'')
                string_pushc(res, input[i]);
        }
        else if (!escape && input[i] == '"')
        {
            i++;

            if ((i = expand_dquotes(input, i, res)) == -1)
                return NULL;
        }
        else if (!escape && input[i] == '\\')
            escape ^= 1;
        else
        {
            // We don't care if we are after a backslash, we just include this
            // char
            if (input[i] == '$' && !escape)
            {
                if ((i = i + expand_var(res, input, i)) == -1)
                {
                    string_free(res);
                    return NULL;
                }
                continue;
            }
            string_pushc(res, input[i]);
            escape = 0;
        }
    }

    // string_free(word);
    return res;
}