-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
380 lines (358 loc) · 9.72 KB
/
main.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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
#include <limits.h>
#include <setjmp.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "list.h"
#include "tree.h"
#include "exec.h"
// если поставить в дебаг 1, то шелл будет считывать команды из фаила, указанного в мейн'е
#define DEBUG 0
#define BUFFER_SIZE 1
#define f_ORDINARY_WORK 0
#define f_END_OF_FILE 1
#define f_END_OF_LINE 2
#define f_INCORRECT_INPUT 3
#define MAX_PATH 32
#if DEBUG == 1
FILE *f;
#define PATH f
#else
#define PATH stdin
#endif
char special_symbols[] = {'|', '&', ';', '>', '<', '(', ')'};
int in(char symbol, char *mas) // проверяет вхождение символа в массив
{
for (int i = 0; i < strlen(mas); i++)
if (symbol == mas[i])
return 1;
return 0;
}
void add_char_to_end(char symbol, char *word) // добавление символа в строку
{
size_t length = strlen(word);
word[length] = symbol;
word[length + 1] = '\0';
}
char *get_string(short *const flag)
{
static int i = BUFFER_SIZE, end_of_sequence = BUFFER_SIZE;
static char *buf, buf_initialized = 0, flag_of_EOF = 0, *tmp0;
if (flag_of_EOF == 1)
{
flag_of_EOF = 0;
free(buf);
buf_initialized = 0;
i = BUFFER_SIZE;
end_of_sequence = BUFFER_SIZE;
*flag = f_END_OF_FILE;
return NULL;
}
if (!buf_initialized)
{
tmp0 = malloc(BUFFER_SIZE * sizeof(char) + 1);
if (tmp0 != NULL)
buf = tmp0;
else
{
fprintf(stderr, "Seg. fault");
/*delete_list(tree);
delete_tree(unit);*/
return 0;
}
//buf = malloc(BUFFER_SIZE * sizeof(char) + 1); //for '\0'
buf_initialized = 1;
}
size_t word_size = BUFFER_SIZE * sizeof(char);
char *word = malloc(word_size + 1), *tmp; //for '\0'
word[0] = '\0';
int quote_flag = 0;
int slash_flag = 0;
for (;; i++)
{
if (i >= BUFFER_SIZE)
{
buf[0] = '\0';
end_of_sequence = (int) fread(buf, sizeof(char), BUFFER_SIZE, PATH);
if (end_of_sequence < BUFFER_SIZE)
{
buf[end_of_sequence] = '\0';
add_char_to_end('\n', buf);
*flag = f_END_OF_FILE;
}
i = 0;
}
size_t w_length = strlen(word);
if (w_length == word_size)
{
tmp = realloc(word, word_size + BUFFER_SIZE * sizeof(char) + 1);
if (tmp != NULL)
word = tmp;
else
{
fprintf(stderr, "Seg. fault");
/*delete_list(tree);
delete_tree(unit);*/
return 0;
}
//word = realloc(word, word_size + BUFFER_SIZE * sizeof(char) + 1);//for '\0'
word_size += BUFFER_SIZE * sizeof(char);
}
if (slash_flag == 1)
{
add_char_to_end(buf[i], word);
slash_flag = 0;
continue;
}
switch (buf[i])
{
case '"':
if (quote_flag == 1)
{
i++;
return word;
}
quote_flag = 1;
continue;
case '\\':
slash_flag = 1;
continue;
case '|':
case '&':
case '>':
if ((w_length == 0) || ((word[w_length - 1] == buf[i]) && (w_length == 1)))
add_char_to_end(buf[i], word);
else
{
if (quote_flag == 1)
continue;
*flag = f_ORDINARY_WORK;
return word;
}
break;
case ';':
case '<':
case '(':
case ')':
if (w_length == 0)
add_char_to_end(buf[i], word);
else
{
if (quote_flag == 1)
continue;
*flag = f_ORDINARY_WORK;
return word;
}
break;
case 'a'...'z':
case 'A'...'Z':
case '0'...'9':
case ':':
case '_':
case '$':
case '/':
case '.':
case '-':
if ((w_length == 0) || (in(word[w_length - 1], special_symbols) == 0))
add_char_to_end(buf[i], word);
else
{
if (quote_flag == 1)
continue;
*flag = f_ORDINARY_WORK;
return word;
}
break;
case ' ':
case '\t':
if (w_length == 0)
continue;
else
{
if (quote_flag == 1)
{
add_char_to_end(buf[i], word);
continue;
}
*flag = f_ORDINARY_WORK;
return word;
}
case '\n':
if (quote_flag != 1)
{
if (*flag == f_END_OF_FILE)
{
free(buf);
buf_initialized = 0;
i = BUFFER_SIZE;
end_of_sequence = BUFFER_SIZE;
}
else
{
*flag = f_END_OF_LINE;
i++;
}
if (w_length == 0)
{
free(word);
return NULL;
}
return word;
}
case '#':
#if DEBUG == 0
if (quote_flag != 1)
{
while ((buf[i] != '\n') && (i != end_of_sequence))
i++;
if (i == end_of_sequence)
{
int c;
do
c = fgetc(stdin);
while ((c != '\n') && (c != EOF));
if (c == EOF)
flag_of_EOF = 1;
}
*flag = f_END_OF_LINE;
return word;
}
#else
printf("comment was met\n");
#endif
// error
default:
#if DEBUG == 0
while ((buf[i] != '\n') && (i != end_of_sequence))
i++;
if (i == end_of_sequence)
{
int c;
do
c = fgetc(stdin);
while ((c != '\n') && (c != EOF));
if (c == EOF)
flag_of_EOF = 1;
}
*flag = f_INCORRECT_INPUT;
#else
printf("error\n");
*flag = f_END_OF_FILE;
#endif
free(word);
return NULL;
}
}
}
char *dollar_sign_handler(char *string) // замена переменных окржения
{
char *tmp;
if (string[0] != '$')
return string;
tmp = realloc(string, MAX_PATH);
if (tmp == NULL)
{
fprintf(stderr, "Seg. fault");
/*delete_list(tree);
delete_tree(init);*/
return 0;
}
else
{
if (!strcmp(string, "$HOME"))
{
string = realloc(string, MAX_PATH);
strcpy(string, getenv("HOME"));
}
else if (!strcmp(string, "$SHELL"))
{
string = realloc(string, MAX_PATH);
getcwd(string, PATH_MAX + 1);
}
else if (!strcmp(string, "$USER"))
{
string = realloc(string, MAX_PATH);
strcpy(string, getenv("LOGNAME"));
}
else if (!strcmp(string, "$EUID"))
{
string = realloc(string, MAX_PATH);
sprintf(string, "%d", getuid());
}
}
return string;
}
void handler(string_list unit)
{
if (unit == 0)
return;
if (check_brace(unit))
{
printf("incorrect braces\n");
return;
}
struct cmd_inf *tree = list_to_tree(unit);
//print_list(unit);
//print_tree(tree, 0);
tree_handler(tree);
delete_tree(tree);
delete_list(unit);
}
// приглашение ко вводу
void invitation()
{
printf("\n");
printf("%s", "\x1b[32m"); /*здесь изменяется цвет на зеленый */
char s[100]; /* ограничение: имя хоста и текущей директории не должно быть слишком длинным! */
gethostname(s, 100);
printf("%s@%s", getenv("USER"), s);
printf("%s", "\033[01;33m"); /* здесь изменяется цвет на желтый */
getcwd(s, 100);
printf(":%s$ ", s);
printf("%s", "\033[01;37m"); // белый
}
// тут собираются команды одной строки и запускается обработчик списка команд
int main()
{
#if DEBUG == 1
f = fopen("/home/kirson/tasks/task_5/try_5", "r");
#endif
signal(SIGINT, SIG_IGN);
short flag = f_ORDINARY_WORK;
while (flag != f_END_OF_FILE)
{
invitation();
#if DEBUG == 1
printf("\n");
#endif
string_list array = 0;
char *string;
while (1)
{
string = get_string(&flag);
if (string != NULL)
add_to_string(&array, dollar_sign_handler(string));
if (flag == f_END_OF_LINE)
{
handler(array);
break;
}
else if (flag == f_END_OF_FILE)
{
handler(array);
break;
}
else if (flag == f_INCORRECT_INPUT)
{
printf("Incorrect input :( \nPlease check your sequence of symbols and try again\n");
delete_list(array);
break;
}
}
}
#if DEBUG == 1
fclose(f);
#endif
}