-
Notifications
You must be signed in to change notification settings - Fork 0
/
parser.y
280 lines (228 loc) · 6.23 KB
/
parser.y
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
//
// parser.y
// yxsh
//
// Created by Kirill on 14.03.2018.
// Copyright © 2018 Kirill. All rights reserved.
//
%{
#define YYSTYPE char*
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <pthread.h>
#include "parseline.h"
static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
static char* current_buff;
static commandline_t* current_cmds;
static int commands_count;
static int args_count;
static command_t current_cmd;
/**
* Commands for bison.
*/
static int yylex(void);
static void yyerror(const char*);
/**
* Prepares current_cmd structure for next command.
*/
static void prepare_temp_command();
/**
* Stores arg with current command.
*
* @param word Arg word.
*
* @return Non-zero if to many args.
*/
static int store_arg(char* word);
/**
* Stores current command.
*
* @param name Command name.
*
* @return Non-zero if to many commands.
*/
static int store_command(char* name);
/**
* Flags previous commands with pipe.
*/
static void pipe_commands();
/**
* Flags previous command with background.
*/
static void background_command();
%}
%token WORD
%token QUOTES_WORD
%token NEWLINE
%token PIPE
%token BACKGROUND
%token SEPARATOR
%token IN_FILE
%token OUT_FILE
%token ADD_FILE
%token MERGE_OUT
%token ERROR
%start complete_command
%%
complete_command : list separator { YYACCEPT; }
| list { YYACCEPT; }
| NEWLINE { YYACCEPT; }
;
list : list separator_op pipeline
| pipeline
;
pipeline : command
| pipeline PIPE linebreak command { pipe_commands(); }
;
command : WORD cmd_suffix { store_command($1); }
| WORD { store_command($1); }
;
cmd_suffix : io_file
| cmd_suffix io_file
| WORD { store_arg($1); }
| cmd_suffix WORD { store_arg($2); }
| QUOTES_WORD { store_arg($1); }
| cmd_suffix QUOTES_WORD { store_arg($2); }
| ERROR { yyerror("unexpected quotes"); YYABORT; }
| cmd_suffix ERROR { yyerror("unexpected qoutes"); YYABORT; }
;
io_file : IN_FILE WORD { current_cmd.infile = $2; }
| OUT_FILE WORD { current_cmd.outfile = $2; }
| ADD_FILE WORD { current_cmd.outfile = $2; current_cmd.flags |= FLAG_APPLY_FILE; }
| MERGE_OUT WORD { current_cmd.outfile = $2; current_cmd.flags |= FLAG_MERGE_OUT; }
;
linebreak : NEWLINE
| /* empty */
;
separator_op : BACKGROUND { background_command(); }
| SEPARATOR
;
separator : separator_op linebreak
| NEWLINE
;
%%
static void yyerror(const char* s) {
printf("yxsh: %s\n", s);
}
static void free_unfinished() {
if (!current_cmd.infile)
free(current_cmd.infile);
if (!current_cmd.outfile)
free(current_cmd.outfile);
if (!current_cmd.cmdargs[0])
free(current_cmd.cmdargs[0]);
for (size_t i = 1; i < args_count; i++)
free(current_cmd.cmdargs[i]);
}
void free_cmds_strings(commandline_t* cmdline, size_t ncmds) {
for (size_t i = 0; i < ncmds; i++) {
if (cmdline->cmds[i].infile)
free(cmdline->cmds[i].infile);
if (cmdline->cmds[i].outfile)
free(cmdline->cmds[i].outfile);
size_t j = 0;
while (cmdline->cmds[i].cmdargs[j])
free(cmdline->cmds[i].cmdargs[j++]);
}
}
int parseline(char* line, commandline_t* cmds) {
size_t count;
if (pthread_mutex_lock(&mutex))
return -1;
current_buff = line;
current_cmds = cmds;
prepare_temp_command();
commands_count = 0;
if (yyparse()) {
free_cmds_strings(cmds, commands_count);
free_unfinished();
return -1;
}
count = cmds->ncmds = commands_count;
if (pthread_mutex_unlock(&mutex))
return -1;
return count;
}
static void pipe_commands() {
current_cmds->cmds[commands_count - 1].flags |= FLAG_IN_PIPE;
current_cmds->cmds[commands_count - 2].flags |= FLAG_OUT_PIPE;
}
static void background_command() {
current_cmds->cmds[commands_count - 1].flags |= FLAG_BACKGROUND;
}
static int store_arg(char* word) {
if (args_count >= MAXARGS)
return -1;
current_cmd.cmdargs[args_count++] = word;
return 0;
}
static int store_command(char* name) {
if (commands_count >= MAXCMDS)
return -1;
current_cmd.cmdargs[0] = name;
current_cmd.cmdargs[args_count] = NULL;
memcpy(&(current_cmds->cmds[commands_count++]), ¤t_cmd,
sizeof(command_t));
prepare_temp_command();
return 0;
}
static void prepare_temp_command() {
current_cmd.flags = 0;
args_count = 1;
current_cmd.pipes[0] = -1;
current_cmd.pipes[1] = -1;
current_cmd.cmdargs[0] = NULL;
current_cmd.infile = NULL;
current_cmd.outfile = NULL;
}
static int yylex() {
int c = *current_buff;
int word_len = 0;
bool quotes = false;
while (c == ' ' || c == '\t')
c = *(++current_buff);
current_buff++;
switch (c) {
case '\0':
return NEWLINE;
case '&':
return BACKGROUND;
case ';':
return SEPARATOR;
case '\n':
return NEWLINE;
case '|':
return PIPE;
case '<':
return IN_FILE;
case '>':
if ((c = *current_buff) == '>') {
current_buff++;
return ADD_FILE;
} else if ((c = *current_buff) == '&') {
current_buff++;
return MERGE_OUT;
}
return OUT_FILE;
}
current_buff--;
if (c == '"') {
quotes = true;
c = *(++current_buff);
}
while ((quotes && c != '"' && c != '\0') ||
(!quotes && c != '\0' && !isspace(c))) {
word_len++;
c = *(current_buff + word_len);
}
if ((quotes && c != '"') ||
(!quotes && *(current_buff + word_len - 1) == '"'))
return ERROR;
yylval = (char*)malloc((word_len + 1) * sizeof(char));
memcpy(yylval, current_buff, word_len * sizeof(char));
current_buff += word_len + (quotes ? 1 : 0);
yylval[word_len] = '\0';
return quotes ? QUOTES_WORD : WORD;
}