-
Notifications
You must be signed in to change notification settings - Fork 0
/
tokenlist.c
164 lines (145 loc) · 3.63 KB
/
tokenlist.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
#include "tokenlist.h"
char std_peek() {
char c = getchar();
if (c != EOF)
ungetc(c, stdin);
return c;
}
char *ch2str(char c) {
char *str = malloc(2);
if (str == NULL)
fatal_error(HEAP_OVERFLOW);
sprintf(str, "%c", c);
return str;
}
int iscmd(char c) {
if (c == '&' || c == '|' || c == '>' || c == '<' || c == ';' || c == '(' || c == ')')
return 1;
else
return 0;
}
token_list *new_token(char *token) {
token_list *t;
if ((t = malloc(sizeof(token_list)))) {
t->token = token;
t->next = NULL;
return t;
} else {
fatal_error(HEAP_OVERFLOW);
return NULL;
}
}
void push_token(token_list **t, char *token) {
token_list *p = new_token(token);
if (*t) {
token_list *q = *t;
while(q->next)
q = q->next;
q->next = p;
} else
*t = p;
}
void print_token_list(token_list *t) {
printf("\nToken list:\n");
while (t) {
printf(t->next ? "├ " : "└ ");
printf("`%s`\n", t->token);
t = t->next;
}
}
void clear_token_list(token_list *t) {
if (t == NULL)
return;
if (t->next)
clear_token_list(t->next);
free(t->token);
free(t);
}
token_list *get_token_list() {
/*
* Possible operations in the order of their priority:
* (expression)
* | > >> <
* && ||
* ; &
*/
token_list *t = NULL;
char c;
while ((c = getchar()) != EOF && c != '\n') {
if (c == ' ')
continue;
if (!iscmd(c)) {
ungetc(c, stdin);
char *tmp = get_word_std();
if (tmp == NULL) {
/* Error: unclosed quote */
clear_token_list(t);
return NULL;
}
push_token(&t, tmp);
continue;
}
if (c == '(' || c == ')' || c == '<' || c == ';') {
push_token(&t, ch2str(c));
continue;
}
if (c == '|' || c == '&' || c == '>') {
if (c == std_peek()) {
getchar();
char *tmp = malloc(3);
if (tmp == NULL)
fatal_error(HEAP_OVERFLOW);
sprintf(tmp, "%c%c", c, c);
push_token(&t, tmp);
} else {
push_token(&t, ch2str(c));
}
continue;
}
/* if we got here, some invalid character is used */
printf("Error: unexpected symbol %c is used\n", c);
clear_token_list(t);
return NULL;
}
if (c == EOF && t == NULL) {
printf("\nCommand file finished\n");
exit(0);
}
return t;
}
char *get_word_std() {
int size = 4, cur = 0;
int q_flag = 0; /* quote flag */
char c, *str;
char *tmp = malloc(size * sizeof(char));
if (tmp == NULL) {
printf("Error: string is too long\n");
return NULL;
} else
str = tmp;
while (((!iscmd(c = std_peek()) && c != ' ') || q_flag == 1) && c != '\n' && c != EOF) {
c = getchar();
if (c == '"') {
q_flag ^= 1;
continue;
}
if (cur == size - 2) {
tmp = realloc(str, size *= 2);
if (tmp == NULL) {
printf("Error: string is too long\n");
free(str);
return NULL;
} else
str = tmp;
}
str[cur++] = c;
}
if (q_flag != 0) {
printf("Error: unclosed quote\n");
return NULL;
}
if (cur == 0 && c == EOF)
return NULL;
str[cur] = 0;
return str;
}