-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtree.c
More file actions
277 lines (222 loc) · 7.01 KB
/
tree.c
File metadata and controls
277 lines (222 loc) · 7.01 KB
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
#include "tree.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
/* ===== Глобальные для парсера ===== */
static char **token_array = NULL; // Массив токенов
static int pos = 0; // Текущая позиция
static int parse_error = 0; // Флаг ошибки
/* ===== Вспомогательные функции ===== */
/* Текущая лексема */
static char* current(void) {
return token_array ? token_array[pos] : NULL;
}
/* Продвинуться вперед */
static void advance(void) {
if (token_array && token_array[pos]) pos++;
}
/* Проверка типа лексемы */
static int is_special(const char *tok) {
if (!tok) return 0;
return strchr("|&;<>()", tok[0]) != NULL;
}
static int is_redirection(const char *tok) {
return tok && (strcmp(tok, "<") == 0 ||
strcmp(tok, ">") == 0 ||
strcmp(tok, ">>") == 0);
}
/* Функция обработки ошибок */
static void error(const char *format, ...) {
parse_error = 1;
fprintf(stderr, "Syntax error: %s\n", format);
}
/* Создать простую команду (статическая, только для tree.c) */
static Command* create_simple_command(char **argv) {
Command *cmd = malloc(sizeof(Command));
if (!cmd) return NULL;
cmd->type = CMD_SIMPLE;
cmd->argv = argv;
cmd->infile = NULL;
cmd->outfile = NULL;
cmd->append = 0;
cmd->left = cmd->right = cmd->pipe = cmd->next = NULL;
cmd->background = 0;
return cmd;
}
/* ===== Рекурсивный спуск ===== */
static Command* parse_command_list(void);
static Command* parse_pipeline(void);
static Command* parse_simple_command(void);
static void parse_redirection(Command *cmd);
Command* parse_tokens(TokenList tok) {
token_array = tok.tokens;
pos = 0;
parse_error = 0;
if (!token_array || !token_array[0]) return NULL;
Command *cmd = parse_command_list();
if (!parse_error && current()) {
error("unexpected token '%s'", current());
free_command(cmd);
return NULL;
}
if (parse_error) {
free_command(cmd);
return NULL;
}
return cmd;
}
static Command* parse_command_list(void) {
Command *cmd = parse_pipeline();
if (!cmd || parse_error) return NULL;
while (current()) {
char *tok = current();
if (strcmp(tok, ";") == 0) {
advance();
Command *next = parse_pipeline();
if (!next) break;
Command *seq = malloc(sizeof(Command));
seq->type = CMD_SEQ;
seq->left = cmd;
seq->right = next;
seq->next = NULL;
cmd = seq;
}
else if (strcmp(tok, "&") == 0) {
advance();
cmd->background = 1;
break;
}
else {
break;
}
}
return cmd;
}
static Command* parse_pipeline(void) {
Command *first = parse_simple_command();
if (!first || parse_error) return NULL;
Command *current_cmd = first;
while (current() && strcmp(current(), "|") == 0) {
advance();
Command *next = parse_simple_command();
if (!next) {
parse_error = 1;
free_command(first);
return NULL;
}
Command *pipe_cmd = malloc(sizeof(Command));
pipe_cmd->type = CMD_PIPE;
pipe_cmd->left = current_cmd;
pipe_cmd->right = next;
current_cmd = pipe_cmd;
}
return first;
}
static Command* parse_simple_command(void) {
char *args[256];
int arg_count = 0;
while (current() && !is_special(current())) {
args[arg_count++] = strdup(current());
advance();
if (arg_count >= 255) break;
}
if (arg_count == 0) {
error("expected command");
return NULL;
}
args[arg_count] = NULL;
char **argv = malloc((arg_count + 1) * sizeof(char*));
for (int i = 0; i <= arg_count; i++) {
argv[i] = args[i];
}
Command *cmd = create_simple_command(argv);
while (is_redirection(current())) {
parse_redirection(cmd);
if (parse_error) {
free_command(cmd);
return NULL;
}
}
return cmd;
}
static void parse_redirection(Command *cmd) {
char *redir = current();
advance();
char *filename = current();
if (!filename || is_special(filename)) {
error("expected filename after '%s'", redir);
return;
}
advance();
if (strcmp(redir, "<") == 0) {
if (cmd->infile) free(cmd->infile);
cmd->infile = strdup(filename);
}
else if (strcmp(redir, ">") == 0) {
if (cmd->outfile) free(cmd->outfile);
cmd->outfile = strdup(filename);
cmd->append = 0;
}
else if (strcmp(redir, ">>") == 0) {
if (cmd->outfile) free(cmd->outfile);
cmd->outfile = strdup(filename);
cmd->append = 1;
}
}
/* ===== Освобождение памяти ===== */
void free_command(Command *cmd) {
if (!cmd) return;
switch (cmd->type) {
case CMD_SIMPLE:
if (cmd->argv) {
for (int i = 0; cmd->argv[i]; i++) free(cmd->argv[i]);
free(cmd->argv);
}
if (cmd->infile) free(cmd->infile);
if (cmd->outfile) free(cmd->outfile);
break;
case CMD_PIPE:
case CMD_SEQ:
free_command(cmd->left);
free_command(cmd->right);
break;
case CMD_BACKGROUND:
free_command(cmd->left);
break;
}
free(cmd);
}
/* ===== Отладочная печать ===== */
void print_command_tree(Command *cmd, int depth) {
if (!cmd) return;
for (int i = 0; i < depth; i++) printf(" ");
switch (cmd->type) {
case CMD_SIMPLE:
printf("SIMPLE:");
for (int i = 0; cmd->argv && cmd->argv[i]; i++)
printf(" %s", cmd->argv[i]);
if (cmd->infile) printf(" < %s", cmd->infile);
if (cmd->outfile) printf(" %s %s", cmd->append ? ">>" : ">", cmd->outfile);
if (cmd->background) printf(" &");
printf("\n");
break;
case CMD_PIPE:
printf("PIPE:\n");
print_command_tree(cmd->left, depth + 1);
printf(" |\n");
print_command_tree(cmd->right, depth + 1);
break;
case CMD_SEQ:
printf("SEQ:\n");
print_command_tree(cmd->left, depth + 1);
printf(" ;\n");
print_command_tree(cmd->right, depth + 1);
break;
case CMD_BACKGROUND:
printf("BACKGROUND:\n");
print_command_tree(cmd->left, depth + 1);
printf(" &\n");
break;
}
}