-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLineParser.c
206 lines (160 loc) · 4.06 KB
/
LineParser.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
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#include "LineParser.h"
#ifndef NULL
#define NULL 0
#endif
#define FREE(X) if(X) free((void*)X)
static char *cloneFirstWord(char *str)
{
char *start = NULL;
char *end = NULL;
char *word;
while (!end) {
switch (*str) {
case '>':
case '<':
case 0:
end = str - 1;
break;
case ' ':
if (start)
end = str - 1;
break;
default:
if (!start)
start = str;
break;
}
str++;
}
if (start == NULL)
return NULL;
word = (char*) malloc(end-start+2);
strncpy(word, start, ((int)(end-start)+1)) ;
word[ (int)((end-start)+1)] = 0;
return word;
}
static void extractRedirections(char *strLine, cmdLine *pCmdLine)
{
char *s = strLine;
while ( (s = strpbrk(s,"<>")) ) {
if (*s == '<') {
FREE(pCmdLine->inputRedirect);
pCmdLine->inputRedirect = cloneFirstWord(s+1);
}
else {
FREE(pCmdLine->outputRedirect);
pCmdLine->outputRedirect = cloneFirstWord(s+1);
}
*s++ = 0;
}
}
static char *strClone(const char *source)
{
char* clone = (char*)malloc(strlen(source) + 1);
strcpy(clone, source);
return clone;
}
static int isEmpty(const char *str)
{
if (!str)
return 1;
while (*str)
if (!isspace(*(str++)))
return 0;
return 1;
}
static cmdLine *parseSingleCmdLine(const char *strLine)
{
char *delimiter = " ";
char *line, *result;
if (isEmpty(strLine))
return NULL;
cmdLine* pCmdLine = (cmdLine*)malloc( sizeof(cmdLine) ) ;
memset(pCmdLine, 0, sizeof(cmdLine));
line = strClone(strLine);
extractRedirections(line, pCmdLine);
result = strtok( line, delimiter);
while( result && pCmdLine->argCount < MAX_ARGUMENTS-1) {
((char**)pCmdLine->arguments)[pCmdLine->argCount++] = strClone(result);
result = strtok ( NULL, delimiter);
}
FREE(line);
return pCmdLine;
}
static cmdLine* _parseCmdLines(char *line)
{
char *nextStrCmd;
cmdLine *pCmdLine;
char pipeDelimiter = '|';
if (isEmpty(line))
return NULL;
nextStrCmd = strchr(line , pipeDelimiter);
if (nextStrCmd)
*nextStrCmd = 0;
pCmdLine = parseSingleCmdLine(line);
if (!pCmdLine)
return NULL;
if (nextStrCmd)
pCmdLine->next = _parseCmdLines(nextStrCmd+1);
return pCmdLine;
}
cmdLine *parseCmdLines(const char *strLine)
{
char* line, *ampersand;
cmdLine *head, *last;
int idx = 0;
if (isEmpty(strLine))
return NULL;
line = strClone(strLine);
if (line[strlen(line)-1] == '\n')
line[strlen(line)-1] = 0;
ampersand = strchr( line, '&');
if (ampersand)
*(ampersand) = 0;
if ( (last = head = _parseCmdLines(line)) )
{
while (last->next)
last = last->next;
last->blocking = ampersand? 0:1;
}
for (last = head; last; last = last->next)
last->idx = idx++;
FREE(line);
return head;
}
void freeCmdLines(cmdLine *pCmdLine)
{
int i;
if (!pCmdLine)
return;
FREE(pCmdLine->inputRedirect);
FREE(pCmdLine->outputRedirect);
for (i=0; i<pCmdLine->argCount; ++i)
FREE(pCmdLine->arguments[i]);
if (pCmdLine->next)
freeCmdLines(pCmdLine->next);
FREE(pCmdLine);
}
int replaceCmdArg(cmdLine *pCmdLine, int num, const char *newString)
{
if (num >= pCmdLine->argCount)
return 0;
FREE(pCmdLine->arguments[num]);
((char**)pCmdLine->arguments)[num] = strClone(newString);
return 1;
}
void freeCmdLinesNoNext(cmdLine *pCmdLine)
{
int i;
if (!pCmdLine)
return;
FREE(pCmdLine->inputRedirect);
FREE(pCmdLine->outputRedirect);
for (i=0; i<pCmdLine->argCount; ++i)
FREE(pCmdLine->arguments[i]);
FREE(pCmdLine);
}