-
Notifications
You must be signed in to change notification settings - Fork 0
/
parseline.c
executable file
·174 lines (154 loc) · 4.13 KB
/
parseline.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
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include "shell.h"
static char *blankskip(register char *);
static char *quotesHandling(char *s);
static char *internalquotesHandling(char *s);
static void leftMove(char *s);
#define PRINT(s) { \
for (int i = 0; i < strlen(s); i++) \
printf("%d ", s[i]); \
printf("\n"); \
} \
int parseline(char *line, struct parserData *data) {
int nargs, ncmds;
register char *s;
char aflg = 0;
/*number of commands*/
int rval;
register int i;
static char delim[] = " \t|&<>;\n";
bkgrnd = nargs = ncmds = rval = 0;
s = line;
infile = outfile = appfile = (char *) NULL;
/*Global array cmds creates in file "shell.c".
His length is MAXCMDS*/
cmds[0].cmdargs[0] = (char *) NULL;
for (i = 0; i < MAXCMDS; i++)
cmds[i].cmdflag = 0;
s = quotesHandling(s);
if (!s) {
fprintf(stderr, "syntax error\n");
return -1;
}
while (*s) { /* until line has been parsed */
s = blankskip(s); /* skip white space */
if (!*s) break; /* done with line */
/* handle <, >, |, &, and ; */
switch(*s) {
case '&':
++bkgrnd;
*s++ = '\0';
break;
case '>':
if (*(s+1) == '>') {
++aflg;
*s++ = '\0';
}
*s++ = '\0';
s = blankskip(s);
if (!*s) {
fprintf(stderr, "syntax error\n");
return -1;
}
if (aflg)
appfile = s;
else
outfile = s;
/*Search next special character*/
s = strpbrk(s, delim);
if (isspace(*s))
*s++ = '\0';
break;
case '<':
*s++ = '\0';
s = blankskip(s);
if (!*s) {
fprintf(stderr, "syntax error\n");
return -1;
}
infile = s;
s = strpbrk(s, delim);
if (isspace(*s))
*s++ = '\0';
break;
case '|':
if (nargs == 0) {
fprintf(stderr, "syntax error\n");
return -1;
}
cmds[ncmds++].cmdflag |= OUTPIP;
cmds[ncmds].cmdflag |= INPIP;
*s++ = '\0';
nargs = 0;
break;
case ';':
*s++ = '\0';
++ncmds;
nargs = 0;
break;
default:
/* a command argument */
if (nargs == 0) /* next command */
rval = ncmds+1;
cmds[ncmds].cmdargs[nargs++] = s;
cmds[ncmds].cmdargs[nargs] = (char *) NULL;
s = strpbrk(s, delim);
if (isspace(*s))
*s++ = '\0';
break;
} /* close switch */
} /* close while */
/* error check */
/*
* The only errors that will be checked for are
* no command on the right side of a pipe
* no command to the left of a pipe is checked above
*/
if (cmds[ncmds-1].cmdflag & OUTPIP) {
if (nargs == 0) {
fprintf(stderr, "syntax error\n");
return -1;
}
}
data->nargs = nargs;
return rval;
}
static char *blankskip(register char *s) {
while (isspace(*s) && *s) ++s;
return(s);
}
static void leftMove(char *s) {
char *tempstr = s;
for (int j = 0; *(tempstr + 1); j++) {
*tempstr = *(tempstr + 1);
tempstr++;
}
*tempstr = '\0';
}
static char *quotesHandling(char *s) {
char *strbackup = s;
char doublequotes = 0;
for (; *s; s++) {
switch(*s) {
case '\"': {
doublequotes = (doublequotes) ? (0) : (1);
leftMove(s);
s--;
break;
}
case '\\': {
if (*(s + 1) == '\"')
leftMove(s);
break;
}
case '\'': {
//
}
}
}
if (doublequotes)
return NULL;
return strbackup;
}