back to scratko.xyz
summaryrefslogtreecommitdiff
path: root/shell.c
blob: c03b62b037b764072edf43effd2c1d5e5f0b89b9 (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
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
#include "queue.h"
#include "dynamic_array.h"
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/wait.h>

enum modes { word_separation, whole_word };
enum {
    new_line        = 10,
    whitespace      = ' ',
    tab             = 9,
    backslash       = '\\',
    double_quotes   = '"'
};

/* two-letter tokens */
enum {
    append          = '>' + 1,
    and             = '&' + 1,
    or              = '|' + 1
};

struct param_type {
    int is_word;
    int escape_sequences;
    unsigned int double_quotes_counter;
    char stored_symbol;
    int empty_word_flag;
    int tokens;
    int wrong_command;
};

static void show_invitation()
{
    printf("> ");
}

static void print_error()
{
    fprintf(stderr, "Error: unmatched quotes\n");
}

static void init_params(struct param_type *params, enum modes *current_mode)
{
    params->is_word = 0;
    params->escape_sequences = 0;
    params->double_quotes_counter = 0;
    params->stored_symbol = ' ';
    params->empty_word_flag = 0;
    params->tokens = 0;
    params->wrong_command = 0;
    *current_mode = word_separation;
}

static void reset_params(struct param_type *params,
                             enum modes *current_mode,
                             struct queue *word_chain,
                             struct dynamic_array *tmp_word)
{
    init_params(params, current_mode);
    queue_clear(word_chain);
    dynarr_drop_word(tmp_word);
}

static int is_double_quotes_pair(struct param_type params)
{
    return !(params.double_quotes_counter % 2);
}

static int check_separation(int ch, struct param_type params)
{
    return (ch == whitespace || ch == tab) && params.is_word &&
            !params.escape_sequences;
}

static int single_tokens(int ch)
{
    return ch == '<' || ch == ';' || ch == '(' || ch == ')';
}

static void clean_input_buffer()
{
    int ch;
    while((ch = getchar() != new_line))
    {}
}

static void double_tokens_and_fg(int ch, struct param_type *params)
{
    int next_ch;
    next_ch = getchar();
    ungetc(next_ch, stdin);
    if(ch == '>' && next_ch == '>')
        params->tokens = append;
    else if(ch == '|' && next_ch == '|')
        params->tokens = or;
    else if(ch == '&' && next_ch == '&')
        params->tokens = and;
    else if(ch == '&') {
        while((ch = getchar()) != new_line) {
            if(ch != whitespace && ch != tab) {
                fprintf(stderr, "incorrect command\n");
                params->wrong_command = 1;
                return;
            }
        }
        params->tokens = '&';
    }
}

static int check_tokens(int ch, struct param_type *params)
{
    if(single_tokens(ch))
        params->tokens = ch;
    else
        double_tokens_and_fg(ch, params);
    return params->tokens;
}

static int ignore_spaces(int ch, struct param_type params)
{
    return (ch == whitespace || ch == tab) && !params.escape_sequences;
}

static int change_mode(int ch, struct param_type params)
{
    return ch == '"' && !params.escape_sequences;
}

static int start_escape_sequence(int ch, struct param_type params)
{
    return ch == backslash && !params.escape_sequences;
}

static void add_word(struct queue *word_chain,
                     struct dynamic_array *tmp_word,
                     struct param_type *params)
{
    dynarr_push_back(tmp_word, 0);
    char *word = malloc(tmp_word->last_element_index+1);
    dynarr_copy_array(tmp_word, word);
    queue_push(word_chain, word);
    dynarr_drop_word(tmp_word);
    params->is_word = 0;
}

#if 0
static void print_word(char *word)
{
    putchar('[');
    while(*word) {
        putchar(*word);
        ++word;
    }
    printf("]\n");
}

static void print_line(const struct queue *word_chain)
{
    queue_processing(word_chain, print_word);
}
#endif

static char** create_cmdline(const struct queue *word_chain,
                                   int word_counter)
{
    char **cmdline = malloc((word_counter + 1) * sizeof(char*));
    queue_copy_words_to_args(word_chain, cmdline);
    return cmdline;
}

static int check_cd(const char *arg)
{
    return !strcmp(arg, "cd");
}

static void change_directory(char **cmdline)
{
    int result;
    char *first_arg = cmdline[1];
    char *path = NULL;
    /* change to user home directory */
    if(first_arg == NULL) {
        path = getenv("HOME");
        if(!path) {
            perror("I don't know where's your home...");
            return;
        }
    } else {
        if(cmdline[2]) {
            perror("cd: too many arguments");
            return;
        }
        path = first_arg;
    }
    result = chdir(path);
    if(result == -1)
        perror(path);
}

static void clean_up_memory(struct queue *word_chain, char **cmdline)
{
    queue_clear(word_chain);
    free(cmdline);
}

static void clean_up_zombie_process()
{
    int pid;
    do {
        pid = wait4(-1, NULL, WNOHANG, NULL);
    } while(pid > 0);
}

static void run_external_program(struct queue *word_chain,
                                 struct param_type *params)
{
    int pid, wait_pid, result;
    if(word_chain->first == NULL) {
        fprintf(stderr, "empty command\n");
        return;
    }
    char **cmdline = create_cmdline(word_chain,
                                    queue_get_word_count(word_chain));
    if(check_cd(cmdline[0])) {
        change_directory(cmdline);
        clean_up_memory(word_chain, cmdline);
    }
    else {
        pid = fork();
        if(pid == -1) {
            perror("fork error");
            exit(1);
        }
        /* child process  */
        if(pid == 0) {
            execvp(cmdline[0], cmdline);
            perror(cmdline[0]);
            exit(1);
        }
        /* parent process */

        clean_up_memory(word_chain, cmdline);

        /* waiting for forground process */
        if(params->tokens != '&') {
            do {
                wait_pid = wait(&result);
            } while(wait_pid != pid);
        } else
            clean_up_zombie_process();
    }
}

static void command_processing(struct param_type *params,
                                enum modes *current_mode,
                                struct queue *word_chain,
                                struct dynamic_array *tmp_word)
{
    /* and odd number of double quotes  */
    if(!is_double_quotes_pair(*params)) {
        print_error();
        reset_params(params, current_mode, word_chain, tmp_word);
        show_invitation();
        return;
    }
    if(params->empty_word_flag || params->is_word) {
        if(params->empty_word_flag)
            dynarr_push_back(tmp_word, '\0');
        add_word(word_chain, tmp_word, params);
    }
    run_external_program(word_chain, params);
    if(params->tokens == '&' || params->tokens == 0)
        show_invitation();
    reset_params(params, current_mode, word_chain, tmp_word);
#if 0
    print_line(word_chain);
#endif
}

static int is_empty_word(int ch, struct param_type params)
{
    return (ch == whitespace || ch == tab) && !params.is_word &&
            params.empty_word_flag;
}

static void word_separation_processing(int ch, struct param_type *params,
                                  enum modes *current_mode,
                                  struct queue *word_chain,
                                  struct dynamic_array *tmp_word)
{
    /* could be a marker for the beginning of a blank word */
    params->stored_symbol = ch;
    if(is_empty_word(ch, *params)) {
        dynarr_push_back(tmp_word, '\0');
        add_word(word_chain, tmp_word, params);
        params->empty_word_flag = 0;
        return;
    }
    params->empty_word_flag = 0;
    if(change_mode(ch, *params)) {
        ++params->double_quotes_counter;
        *current_mode = whole_word;
        return;
    }
    if(start_escape_sequence(ch, *params)) {
        params->escape_sequences = 1;
        return;
    }
    if(check_tokens(ch, params)) {
        command_processing(params, current_mode, word_chain, tmp_word);
        return;
    }
    if(params->wrong_command) {
        clean_input_buffer();
        reset_params(params, current_mode, word_chain, tmp_word);
        show_invitation();
        return;
    }
    if(check_separation(ch, *params)) {
        add_word(word_chain, tmp_word, params);
        params->is_word = 0;
        return;
    }
    if(ignore_spaces(ch, *params))
        return;

    dynarr_push_back(tmp_word, ch);
    params->is_word = 1;
    params->escape_sequences = 0;
}

static int escape_double_quotes_or_backslash(int ch, struct param_type params)
{
    return params.escape_sequences &&
        (ch == double_quotes || ch == backslash);
}

static int double_quotes_again(int ch, struct param_type params)
{
    return ch == double_quotes && !params.is_word &&
        params.stored_symbol == '"';
}

static void whole_word_processing(int ch, struct param_type *params,
                                  enum modes *current_mode,
                                  struct queue *word_chain,
                                  struct dynamic_array *tmp_word)
{
    if(double_quotes_again(ch, *params)) {
        params->stored_symbol = '\0';
        params->empty_word_flag = 1;
        ++params->double_quotes_counter;
        *current_mode = word_separation;
        return;
    }
    params->stored_symbol = '\0';
    if(change_mode(ch, *params)) {
        ++params->double_quotes_counter;
        *current_mode = word_separation;
        return;
    }
    if(escape_double_quotes_or_backslash(ch, *params)) {
        dynarr_push_back(tmp_word, ch);
        params->escape_sequences = 0;
        params->is_word = 1;
        return;
    }
    if(start_escape_sequence(ch, *params)) {
        params->escape_sequences = 1;
        return;
    }
    /* backslash recovery */
    if(params->escape_sequences)
        dynarr_push_back(tmp_word, backslash);
    dynarr_push_back(tmp_word, ch);
    params->is_word = 1;
    params->escape_sequences = 0;
}

int main()
{
    int ch;
    struct param_type params;
    struct queue word_chain;
    struct dynamic_array tmp_word;
    enum modes current_mode = word_separation;
    queue_init(&word_chain);
    dynarr_create_array(&tmp_word);
    init_params(&params, &current_mode);
    show_invitation();
    while((ch = getchar()) != EOF) {
        clean_up_zombie_process();
        if(ch == new_line)
            command_processing(&params, &current_mode, &word_chain,
                                &tmp_word);
        else if(current_mode == word_separation)
            word_separation_processing(ch, &params, &current_mode, &word_chain,
                                       &tmp_word);
        else if(current_mode == whole_word)
            whole_word_processing(ch, &params, &current_mode, &word_chain,
                                  &tmp_word);
    }
    putchar(new_line);
    dynarr_clear(&tmp_word);
    return 0;
}