This repository has been archived by the owner on Jul 3, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
parse_line.c
293 lines (232 loc) · 8.87 KB
/
parse_line.c
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
/*
* Copyright © 2018 Dimonchik0036. All rights reserved.
*/
#include "parse_line.h"
#define PRINT_SYNTAX_ERROR(token) fprintf(stderr, "shell: syntax error near unexpected token '%s'\n", token)
#define PRINT_SYNTAX_PIPELINE_ERROR(token) fprintf(stderr, "shell: syntax error in pipeline near token '%s'\n", token)
static char *blank_skip(char *data);
static int check_pipeline(const Command *command);
static int parse_redirect_output(char **data, Command *command);
static int parse_redirect_input(char **data, Command *command);
static int parse_background(char **data,
CommandLine *command_line,
size_t *current_index_of_command,
size_t *current_index_of_arguments);
static int parse_pipeline(char **data,
CommandLine *command_line,
size_t *current_index_of_command,
size_t *current_index_of_arguments);
static int parse_separator(char **data,
size_t *current_index_of_command,
size_t *current_index_of_arguments);
static int parse_add_command(char **data,
Command *current_command,
const size_t *current_index_of_command,
size_t *current_index_of_arguments,
size_t *number_of_command);
static int parse_tokens(char **data,
CommandLine *command_line,
size_t *current_index_of_command,
size_t *current_index_of_arguments,
size_t *number_of_command);
static void go_to_next_delimiter(char **data);
static void set_end(char **data);
static void reset_command_line(CommandLine *command_line);
static int check_command_line(CommandLine *command_line, size_t command_amount);
static int is_end(char const *data) {
return *data == END;
}
static char delimiters[] = " \t&<>;\n";
ssize_t parse_input_line(char *input_data, CommandLine *command_line) {
reset_command_line(command_line);
size_t index_of_arguments = 0;
size_t index_of_command = 0;
size_t result_number_of_command = 0;
while (!is_end(input_data)) {
if (index_of_command >= MAX_COMMANDS) {
fprintf(stderr, "shell: number of commands (%d) exceeded\n",
MAX_COMMANDS);
return BAD_SYNTAX;
}
input_data = blank_skip(input_data);
if (is_end(input_data)) {
break;
}
int exit_code = parse_tokens(&input_data, command_line,
&index_of_command,
&index_of_arguments,
&result_number_of_command);
if (exit_code != SUCCESS) {
return exit_code;
}
}
int answer = check_command_line(command_line, result_number_of_command);
if (answer != SUCCESS) {
return BAD_SYNTAX;
}
return result_number_of_command;
}
static int check_command_line(CommandLine *command_line,
size_t command_amount) {
if (command_amount == 0) {
return SUCCESS;
}
size_t index;
for (index = 0; index < command_amount; ++index) {
int answer = check_pipeline(&command_line->commands[index]);
if (answer != SUCCESS) {
return BAD_SYNTAX;
}
}
if (command_line->commands[command_amount - 1].flag & OUT_PIPE) {
PRINT_SYNTAX_ERROR(TOKEN_PIPELINE_STR);
return BAD_SYNTAX;
}
return SUCCESS;
}
static int parse_tokens(char **data,
CommandLine *command_line,
size_t *current_index_of_command,
size_t *current_index_of_arguments,
size_t *number_of_command) {
Command *current_command = &command_line->commands[*current_index_of_command];
char token = **data;
switch (token) {
case TOKEN_BACKGROUND:
return parse_background(data, command_line,
current_index_of_command,
current_index_of_arguments);
case TOKEN_OUTFILE:
return parse_redirect_output(data, current_command);
case TOKEN_INFILE:
return parse_redirect_input(data, current_command);
case TOKEN_SEPARATOR:
return parse_separator(data, current_index_of_command,
current_index_of_arguments);
case TOKEN_PIPELINE:
return parse_pipeline(data, command_line, current_index_of_command,
current_index_of_arguments);
default:
return parse_add_command(data, current_command,
current_index_of_command,
current_index_of_arguments,
number_of_command);
}
}
static char *blank_skip(char *data) {
while (isspace(*data) && !is_end(data)) {
++data;
}
return data;
}
static int check_pipeline(const Command *command) {
if (command->flag & IN_FILE && command->flag & IN_PIPE) {
PRINT_SYNTAX_PIPELINE_ERROR(TOKEN_INFILE_STR);
return BAD_SYNTAX;
}
if (command->flag & OUT_FILE && command->flag & OUT_PIPE) {
PRINT_SYNTAX_PIPELINE_ERROR(TOKEN_OUTFILE_STR);
return BAD_SYNTAX;
}
return SUCCESS;
}
static int parse_pipeline(char **data,
CommandLine *command_line,
size_t *current_index_of_command,
size_t *current_index_of_arguments) {
if (*current_index_of_arguments == 0
|| *current_index_of_command + 1 == MAX_COMMANDS) {
PRINT_SYNTAX_ERROR(TOKEN_PIPELINE_STR);
return BAD_SYNTAX;
}
command_line->commands[(*current_index_of_command)++].flag |= OUT_PIPE;
command_line->commands[*current_index_of_command].flag |= IN_PIPE;
set_end(data);
*current_index_of_arguments = 0;
return SUCCESS;
}
static int parse_add_command(char **data,
Command *current_command,
const size_t *current_index_of_command,
size_t *current_index_of_arguments,
size_t *number_of_command) {
if (*current_index_of_arguments == 0) {
*number_of_command = *current_index_of_command + 1;
}
if (*current_index_of_arguments + 1 == MAX_ARGS) {
fprintf(stderr, "shell: number of arguments (%d) exceeded\n", MAX_ARGS);
return BAD_SYNTAX;
}
current_command->arguments[(*current_index_of_arguments)++] = *data;
current_command->arguments[(*current_index_of_arguments)] = (char *) NULL;
go_to_next_delimiter(data);
return SUCCESS;
}
static int parse_separator(char **data,
size_t *current_index_of_command,
size_t *current_index_of_arguments) {
if (*current_index_of_arguments == 0) {
PRINT_SYNTAX_ERROR(TOKEN_SEPARATOR_STR);
return BAD_SYNTAX;
}
set_end(data);
++(*current_index_of_command);
*current_index_of_arguments = 0;
return SUCCESS;
}
static int parse_background(char **data,
CommandLine *command_line,
size_t *current_index_of_command,
size_t *current_index_of_arguments) {
if (*current_index_of_arguments == 0) {
PRINT_SYNTAX_ERROR(TOKEN_BACKGROUND_STR);
return BAD_SYNTAX;
}
command_line->commands[(*current_index_of_command)++].flag |= BACKGROUND;
*current_index_of_arguments = 0;
set_end(data);
return SUCCESS;
}
static int parse_redirect_output(char **data, Command *command) {
if ((*data)[1] == TOKEN_OUTFILE) {
command->appfile = TRUE;
set_end(data);
} else {
command->appfile = FALSE;
}
set_end(data);
*data = blank_skip(*data);
if (is_end(*data)) {
PRINT_SYNTAX_ERROR(TOKEN_OUTFILE_STR);
return BAD_SYNTAX;
}
command->outfile = *data;
command->flag |= OUT_FILE;
go_to_next_delimiter(data);
return SUCCESS;
}
static int parse_redirect_input(char **data, Command *command) {
set_end(data);
*data = blank_skip(*data);
if (is_end(*data)) {
PRINT_SYNTAX_ERROR(TOKEN_INFILE_STR);
return BAD_SYNTAX;
}
command->infile = *data;
command->flag |= IN_FILE;
go_to_next_delimiter(data);
return SUCCESS;
}
static void go_to_next_delimiter(char **data) {
*data = strpbrk(*data, delimiters);
if (isspace(**data) && !is_end(*data)) {
set_end(data);
}
}
static void set_end(char **data) {
**data = END;
++*data;
}
static void reset_command_line(CommandLine *command_line) {
memset(command_line->commands, 0, sizeof(Command) * MAX_COMMANDS);
}