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
|
#include <stddef.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include <stdio.h>
#define DECLARE_VEC(T, Name) \
typedef struct { \
T* items; \
size_t count; \
size_t cap; \
size_t incr_step; \
} Name; \
static inline Name make_##Name(size_t step) { \
return (Name) { \
.items = malloc(step* sizeof(T)), \
.count = 0, \
.cap = step, \
.incr_step = step, \
}; \
} \
static inline void push_to_##Name(Name* vec, T item) { \
if (vec->count == vec->cap) { \
vec->cap += vec->incr_step; \
vec->items = realloc( \
vec->items, \
vec->cap * sizeof(T) \
); \
} \
vec->items[vec->count++] = item; \
} \
DECLARE_VEC(char, String);
void append_str_to_String(String* target, char* src_str) {
size_t required_size =
((target->count + strlen(src_str)) / target->incr_step + 1) * target->incr_step;
if (required_size > target->cap) {
target->cap = required_size;
target->items = realloc(target->items, required_size * sizeof(char));
}
memcpy(target->items + target->count, src_str, strlen(src_str));
target->count += strlen(src_str);
}
#define DECLARE_MAYBE(T) \
typedef struct { \
bool some; \
T value; \
} Maybe##T; \
typedef enum {
UN_Q,
SINGLE_Q,
DOUBLE_Q,
} QuotationMode;
typedef struct Region Region;
typedef enum {
LITERAL,
CMD_SUB,
ARITHM,
PARAM,
} ChunkKind;
typedef struct {
ChunkKind kind;
QuotationMode mode;
union {
char* lit_str;
Region* sub_cmd_region;
Region* arithm_expr_region;
Region* param_region;
} as;
} Chunk;
DECLARE_VEC(Chunk, ChunkVec)
typedef enum {
TOK_WORD,
TOK_SPECIAL,
} TokenKind;
typedef enum {
PIPE,
WRITE_REDIR_TRUNC,
WRITE_REDIR_APPEND,
READ_REDIR,
EOL,
} SpecialToken;
typedef struct {
TokenKind kind;
union {
ChunkVec word;
SpecialToken spec;
} as;
} Token;
DECLARE_VEC(Token, TokenVec)
DECLARE_MAYBE(SpecialToken)
struct Region {
QuotationMode mode;
TokenVec tokens;
};
DECLARE_MAYBE(Chunk);
MaybeChunk unquoted_next_chunk(char* input, size_t* pos) {
String chunk;
char curr_c;
for (;(curr_c = input[*pos], curr_c != '$' && curr_c != '\'' && curr_c != '\"' && curr_c != '\0'); pos++) {
exit(1);
}
exit(1);
}
TokenVec lex_region(char* input, size_t* pos, char until) {
TokenVec tokens = make_TokenVec(256);
while (input[*pos] != until) {
while (input[*pos] == ' ') (*pos)++;
}
return tokens;
}
TokenVec lex(char* input) {
size_t pos = 0;
return lex_region(input, &pos, '\0');
}
|