-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparser.c
More file actions
252 lines (240 loc) · 7.13 KB
/
parser.c
File metadata and controls
252 lines (240 loc) · 7.13 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
#include "shell.h"
int initTokensTable(char *line);
int tokensIsEmpty();
token *tokensGetNextElement();
token *tokensCheckNextElement();
void tokensSkipElement();
static job *parseJob();
static process *parseProcess(char **infile, char **outfile, char **appfile);
int isControlSymbol(char c)
{
return (c == '|' || c == ';' || c == '&' || c == '>' || c == '<');
}
/**
* ÏÖÉÄÁÅÍÙÊ ××ÏÄ: job1 [&;] job2 [&;] ...
* ÈÜÎÄÌÉÔ '&', ';', Ô.Ë. ÜÔÏ ÏÓÎÏ×ÎÏÊ ÜÌÅÍÅÎÔ ÄÌÑ ÒÁÚÄÅÌÅÎÉÑ ÚÁÄÁÎÉÊ
* ÄÏÇÏ×ÏÒÅÎÎÏÓÔØ: ËÁÖÄÁÑ ÆÕÎËÃÉÑ ÄÏÌÖÎÁ "ÐÒÏÞÉÔÙ×ÁÔØ" ÔÏÌØËÏ ÔÅ ÔÏËÅÎÙ, ËÏÔÏÒÙÅ ÏÎÁ ÈÜÎÄÌÉÔ
*/
int parseline(char *line)
{
int tokens = initTokensTable(line);
if (tokens == -1)
return 0;
job *currentJob;
for (currentJob = first_job; currentJob && currentJob->next; currentJob = currentJob->next)
;
/* ÕÓÌÏ×ÉÅ ÏËÏÎÞÁÎÉÑ ÓÔÒÏËÉ - ÏÔÓÕÔÓÔ×ÉÅ ÔÏËÅÎÏ× */
while (!tokensIsEmpty())
{
token *t = tokensCheckNextElement();
switch (t->type)
{
case AMPERSAND:
if (currentJob)
{
currentJob->foreground = 0;
tokensSkipElement();
}
else
{
/* ÐÙÔÁÅÍÓÑ ÓÄÅÌÁÔØ ÆÏÎÏ×ÙÍ ÚÁÄÁÎÉÅ, ËÏÔÏÒÏÅ ÐÏ ÆÁËÔÕ ÎÅ ÓÕÝÅÓÔ×ÕÅÔ */
fprintf(stderr, "syntax error\n");
return 0;
}
break;
case SEMICOLON:
tokensSkipElement();
break;
default:
if (!currentJob)
{
if((first_job = currentJob = parseJob()) == 0){
return 0;
}
}
else
{
if((currentJob->next = parseJob()) == 0){
return 0;
}
currentJob = currentJob->next;
}
break;
}
}
return tokens;
}
/**
* ÏÖÉÄÁÅÍÙÊ ×ÈÏÄ: "proc1 | proc2 | ... | procn"
* ÈÜÎÄÌÉÔ '|', Ô.Ë. ÜÔÏ ÏÓÎÏ×ÎÏÊ ÓÔÒÏÉÔÅÌØÎÙÊ ÜÌ-Ô ÄÌÑ ÚÁÄÁÎÉÊ
* ÏÂÒÁÂÁÔÙ×ÁÅÔ <> ÄÌÑ ÐÅÒ×ÏÇÏ É ÐÏÓÌÅÄÎÅÇÏ ÐÒÏÃÅÓÓÁ × ÔÒÕÂÅ??
*/
static job *parseJob()
{
char *infile = 0, *outfile = 0, *appfile = 0;
job *j = (job *)malloc(sizeof(job));
j->next = 0;
j->stdinno = STDIN_FILENO;
j->stdoutno = STDOUT_FILENO;
j->foreground = 1;
j->pgid = 0;
j->launched = 0;
j->notified = 0;
j->command = 0;
j->builtin = 0;
process *currentProcess = j->first_process = 0;
token *t = tokensCheckNextElement();
/* ÕÓÌÏ×ÉÑ ÏËÏÎÞÁÎÉÑ ÚÁÄÁÎÉÑ - ÔÏËÅÎÙ ;& ÉÌÉ ÏÔÓÕÔÓÔ×ÉÅ ÔÏËÅÎÏ× */
while (!tokensIsEmpty() && t->type != AMPERSAND && t->type != SEMICOLON)
{
switch (t->type)
{
case PIPE:
tokensSkipElement();
if (currentProcess && !j->builtin)
{
/* ÐÅÒÅÈÏÄÉÍ ÐÏ ÔÒÕÂÅ, ÐÒÏÃÅÓÓ ÓÌÅ×Á ÕÖÅ ÂÙÌ */
/* ÓÐÒÁ×Á ÏÔ ÐÁÊÐÁ ÏÂÑÚÁÔÅÌØÎÏ ÄÏÌÖÅÎ ÂÙÔØ ÄÒÕÇÏÊ ÐÒÏÃÅÓÓ */
if (!(!tokensIsEmpty() && t->type != AMPERSAND && t->type != SEMICOLON))
{
fprintf(stderr, "syntax error\n");
return 0;
}
if((currentProcess->next = parseProcess(&infile, &outfile, &appfile)) == 0) {
return 0;
}
currentProcess = currentProcess->next;
}
else
{
fprintf(stderr, "syntax error\n");
return 0;
}
break;
default:
/* ÜÔÏ ÐÅÒ×ÙÊ ÐÒÏÃÅÓÓ */
if((currentProcess = j->first_process = parseProcess(&infile, &outfile, &appfile)) == 0) {
return 0;
}
if (strcmp(j->first_process->argv[0], "jobs") == 0 ||
strcmp(j->first_process->argv[0], "fg") == 0 ||
strcmp(j->first_process->argv[0], "bg") == 0 ||
strcmp(j->first_process->argv[0], "cd") == 0)
{
j->builtin = 1;
}
break;
}
t = tokensCheckNextElement();
}
/* ÚÄÅÓØ ÍÙ ×ÎÅÚÁÐÎÏ ÏÔËÒÙ×ÁÅÍ ÆÁÊÌÙ. ÌÏÇÉËÁ ËÏÎÅÞÎÏ ÎÅ ÁÈÔÉ, ÎÏ ÌÕÞÛÅÇÏ ÍÅÓÔÁ ÐÏËÁ ÎÅ ÎÁÛÌÏÓØ.
ÐÏ ËÏÎÔÒÁËÔÕ ÍÙ ÄÏÇÏ×ÏÒÉÌÉÓØ ÈÒÁÎÉÔØ × ÚÁÄÁÎÉÉ ÒÕÞËÉ ÆÁÊÌÏ×. */
if (infile)
{
if ((j->stdinno = open(infile, O_RDONLY | O_CLOEXEC)) == -1)
{
perror(infile);
exit(1);
}
}
if (outfile)
{
if ((j->stdoutno = open(outfile, O_CREAT | O_TRUNC | O_WRONLY | O_CLOEXEC, 0644)) == -1)
{
perror(outfile);
exit(1);
}
}
else if (appfile)
{
if ((j->stdoutno = open(appfile, O_CREAT | O_APPEND | O_WRONLY | O_CLOEXEC, 0644)) == -1)
{
perror(appfile);
exit(1);
}
}
return j;
}
/**
* ÏÖÉÄÁÅÍÙÊ ×ÈÏÄ: "arg0 arg1 ... argn [<,>,>>]"
* ÅÓÌÉ infile != 0, ÔÕÄÁ ÎÕÖÎÏ ÐÏÌÏÖÉÔØ <
* ÁÎÁÌÏÇÉÞÎÏ ÄÌÑ outfile, appfile, errfile.
* ÅÓÌÉ ÁÒÇÕÍÅÎÔ 0, ÎÏ ×ÓÔÒÅÞÁÅÔÓÑ ÐÅÒÅÎÁÐÒÁ×ÌÅÎÉÅ ÅÇÏ ÔÉÐÁ - syntax error
*/
static process *parseProcess(char **infile, char **outfile, char **appfile)
{
process *p = (process *)malloc(sizeof(process));
p->nargs = 0;
p->next = 0;
p->argv[0] = (char *)NULL;
p->completed = 0;
p->stopped = 0;
p->stderrno = STDERR_FILENO;
char * errfile = 0;
token *t = tokensCheckNextElement();
while (!tokensIsEmpty() && t->type != SEMICOLON && t->type != AMPERSAND && t->type != PIPE)
{
switch (t->type)
{
case REALLYRIGHTARROW:
/* appfile */
tokensSkipElement();
t = tokensGetNextElement();
if (!t || t->type != WORD)
{
fprintf(stderr, "after >> should be filename\n");
return 0;
}
*appfile = t->place;
break;
case ERRORRIGHTARROW:
/* errfile */
tokensSkipElement();
t = tokensGetNextElement();
if (!t || t->type != WORD)
{
fprintf(stderr, "after 2> should be filename\n");
return 0;
}
errfile = t->place;
break;
case RIGHTARROW:
/* outfile */
tokensSkipElement();
t = tokensGetNextElement();
if (!t || t->type != WORD)
{
fprintf(stderr, "after > should be filename\n");
return 0;
}
*outfile = t->place;
break;
case LEFTARROW:
/* infile */
tokensSkipElement();
t = tokensGetNextElement();
if (!t || t->type != WORD)
{
fprintf(stderr, "after < should be filename\n");
return 0;
}
*infile = t->place;
break;
default:
p->argv[p->nargs++] = t->place;
p->argv[p->nargs] = (char *)NULL;
tokensSkipElement();
break;
}
t = tokensCheckNextElement();
}
if(errfile)
{
if ((p->stderrno = open(errfile, O_CREAT | O_TRUNC | O_WRONLY | O_CLOEXEC, 0644)) == -1)
{
perror(errfile);
exit(1);
}
}
return p;
}