summaryrefslogtreecommitdiff
path: root/exec.c
blob: 7c2b8b5caf816c459ed59e84b0e51d280f2b09cf (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
#include "lex.h"
#include "parse.h"
#include "typeutils.h"
#include <stdlib.h>
#include <string.h>

typedef struct {
    char* key;
    char* val;
} StrPair;

DECLARE_VEC(StrPair, KeyValMap)

DECLARE_MAYBE(char*, MaybeStr)

void set_map_pair(KeyValMap* map, char* key, char* val) {
for (int i = 0; i < map->count; i++) {
        char* key_i = get_from_KeyValMap(map, i)->key;
        if (key_i == NULL) continue;
        if (strcmp(key_i, key) == 0) {
            // TODO: maybe copy instead
            strcpy(
                map->blocks[i / map->block_size][i % map->block_size].val,
                val
            );
            break;
        }
    }
}

MaybeStr get_map_value(KeyValMap* map, char* key) {
    for (int i = 0; i < map->count; i++) {
        char* key_i = get_from_KeyValMap(map, i)->key;
        if (key_i == NULL) continue;
        if (strcmp(key_i, key) == 0) {
            return (MaybeStr){
                .some = true,
                .value = get_from_KeyValMap(map, i)->val
            };
        }
    }
    return (MaybeStr){.some = false};
} 

void unset_map_key(KeyValMap* map, char* key) {
    for (int i = 0; i < map->count; i++) {
        char* key_i = get_from_KeyValMap(map, i)->key;
        if (key_i == NULL) continue;
        if (strcmp(key_i, key) == 0) {
            map->blocks[i / map->block_size][i % map->block_size].key = NULL;
            break;
        }
    }
}


typedef struct {
    KeyValMap aliases;
    KeyValMap shell_vars;
} ShellState;


char* get_var(char* varname, ShellState* shstate) {
    MaybeStr maybe_varval = get_map_value(&shstate->shell_vars, varname); 
    if (maybe_varval.some == true) return maybe_varval.value;
    return getenv(varname);
}

typedef struct {
    QuotationMode qmode;  
    char* content;
} ExpandedChunk;


ExpandedChunk expand_chunk(Chunk chunk, ShellState* shstate) {
    char* content;
    if (chunk.qmode == SINGLE_Q || chunk.kind == LITERAL) {
        if (chunk.qmode == SINGLE_Q && chunk.kind != LITERAL) exit(69);
        content = chunk.as.literal_str;
    } else if (chunk.kind == VAR_EXP) {
        content = get_var(chunk.as.varname, shstate);
    } else if (chunk.kind == REGION_EXP) {
        exit(7); // Not supported yet; arithm
    }
    return (ExpandedChunk){.qmode = chunk.qmode, .content = content};
}

DECLARE_VEC(char*, StrVec)


typedef struct {
    StrVec complete_strs;
    String active_str;
} WordSplittingGerm;

void add_expchunk(char* next_str, WordSplittingGerm* curr_res) {
     for (int pos = 0; pos < strlen(next_str); pos++) {
        if (next_str[pos] == ' ') {
            push_to_StrVec(&curr_res->complete_strs, inner(&curr_res->active_str));
            curr_res->active_str = make_String(256);
        } else {
            push_to_String(&curr_res->active_str, next_str[pos]);
        }
    }
}

StrVec expand_word(Word word, ShellState* state) {
    WordSplittingGerm curr_res = {.complete_strs = make_StrVec(256), .active_str = make_String(25)};
    for (int i = 0; i < word.count; i++) {
        Chunk curr_cunk = *get_from_Word(&word, i);
        ExpandedChunk curr_exp = expand_chunk(curr_cunk, state);
        switch (curr_exp.qmode) {
            case UN_Q:
                add_expchunk(curr_exp.content, &curr_res);
                break;
            case SINGLE_Q:
                append_str_to_String(&curr_res.active_str, curr_exp.content);
                break;
            case DOUBLE_Q:
                append_str_to_String(&curr_res.active_str, curr_exp.content);
        }
    }

    if (curr_res.active_str.count > 0) {
        push_to_StrVec(&curr_res.complete_strs, inner(&curr_res.active_str));
    }

    return curr_res.complete_strs;
   
    /*
    char** res_ptr = malloc(sizeof(char*) * curr_res.complete_strs.count);
    for (int j = 0; j < curr_res.complete_strs.count; j++) {
        res_ptr[j] = curr_res.complete_strs.blocks[j / curr_res.complete_strs.block_size][j % curr_res.complete_strs.block_size];
    }
    return res_ptr;
    */
}

char** expand_token(Token token, ShellState* shstate) {
    // TODO: alias expansion
    


    // TODO: make some kind of extractor for the Vec-s that returns a regular T*-type NULL-term array
    // TODO: we need to split strs on spaces
    exit(1);
}

void command_thing(Command command, ShellState* shtate) {
    char**
}

void run_pipeline(Pipeline pl, ShellState* shstate) {

}