-
Notifications
You must be signed in to change notification settings - Fork 9
/
cmdparse.c
375 lines (345 loc) · 7.06 KB
/
cmdparse.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
/* Parse command line, set up command arguments Unix-style, and call function.
* Note: argument is modified (delimiters are overwritten with nulls)
*
* Copyright 1991 Phil Karn, KA9Q
*
* Improved error handling by Brian Boesch of Stanford University
* Feb '91 - Bill Simpson
* bit16cmd for PPP
* Mar '91 - Glenn McGregor
* handle string escaped sequences
*/
#include <stdio.h>
#include "global.h"
#include "proc.h"
#include "cmdparse.h"
struct boolcmd {
char *str; /* Token */
int val; /* Value */
};
static struct boolcmd Boolcmds[] = {
"y", 1, /* Synonyms for "true" */
"yes", 1,
"true", 1,
"on", 1,
"1", 1,
"set", 1,
"enable", 1,
"n", 0, /* Synonyms for "false" */
"no", 0,
"false", 0,
"off", 0,
"0", 0,
"clear", 0,
"disable", 0,
NULL
};
static char *stringparse(char *line);
static char *
stringparse(line)
char *line;
{
register char *cp = line;
unsigned long num;
while ( *line != '\0' && *line != '\"' ) {
if ( *line == '\\' ) {
line++;
switch ( *line++ ) {
case 'n':
*cp++ = '\n';
break;
case 't':
*cp++ = '\t';
break;
case 'v':
*cp++ = '\v';
break;
case 'b':
*cp++ = '\b';
break;
case 'r':
*cp++ = '\r';
break;
case 'f':
*cp++ = '\f';
break;
case 'a':
*cp++ = '\a';
break;
case '\\':
*cp++ = '\\';
break;
case '\?':
*cp++ = '\?';
break;
case '\'':
*cp++ = '\'';
break;
case '\"':
*cp++ = '\"';
break;
case 'x':
num = strtoul( --line, &line, 16 );
*cp++ = (char) num;
break;
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
num = strtoul( --line, &line, 8 );
*cp++ = (char) num;
break;
case '\0':
return NULL;
default:
*cp++ = *(line - 1);
break;
};
} else {
*cp++ = *line++;
}
}
if ( *line == '\"' )
line++; /* skip final quote */
*cp = '\0'; /* terminate string */
return line;
}
int
cmdparse(cmds,line,p)
struct cmds cmds[];
register char *line;
void *p;
{
struct cmds *cmdp;
char *argv[NARG],*cp;
char **pargv;
int argc,i;
/* Remove cr/lf */
rip(line);
for(argc = 0;argc < NARG;argc++)
argv[argc] = NULL;
for(argc = 0;argc < NARG;){
register int qflag = FALSE;
/* Skip leading white space */
while(*line == ' ' || *line == '\t')
line++;
if(*line == '\0')
break;
/* return if comment character first non-white */
if ( argc == 0 && *line == '#' )
return 0;
/* Check for quoted token */
if(*line == '"'){
line++; /* Suppress quote */
qflag = TRUE;
}
argv[argc++] = line; /* Beginning of token */
if(qflag){
/* Find terminating delimiter */
if((line = stringparse(line)) == NULL){
return -1;
}
} else {
/* Find space or tab. If not present,
* then we've already found the last
* token.
*/
if((cp = strchr(line,' ')) == NULL
&& (cp = strchr(line,'\t')) == NULL){
break;
}
*cp++ = '\0';
line = cp;
}
}
if (argc < 1) { /* empty command line */
argc = 1;
argv[0] = "";
}
/* Look up command in table; prefix matches are OK */
for(cmdp = cmds;cmdp->name != NULL;cmdp++){
if(strncmp(argv[0],cmdp->name,strlen(argv[0])) == 0)
break;
}
if(cmdp->name == NULL) {
if(cmdp->argc_errmsg != NULL)
printf("%s\n",cmdp->argc_errmsg);
return -1;
}
if(argc < cmdp->argcmin) {
/* Insufficient arguments */
printf("Usage: %s\n",cmdp->argc_errmsg);
return -1;
}
if(cmdp->func == NULL)
return 0;
if(cmdp->stksize == 0){
return (*cmdp->func)(argc,argv,p);
} else {
/* Make private copy of argv and args,
* spawn off subprocess and return.
*/
pargv = (char **)callocw(argc,sizeof(char *));
for(i=0;i<argc;i++)
pargv[i] = strdup(argv[i]);
newproc(cmdp->name,cmdp->stksize,
(void (*)())cmdp->func,argc,pargv,p,1);
return 0;
}
}
/* Call a subcommand based on the first token in an already-parsed line */
int
subcmd(tab,argc,argv,p)
struct cmds tab[];
int argc;
char *argv[];
void *p;
{
register struct cmds *cmdp;
char **pargv;
int found = 0;
int i;
/* Strip off first token and pass rest of line to subcommand */
if (argc < 2) {
if (argc < 1)
printf("SUBCMD - Don't know what to do?\n");
else
printf("\"%s\" - takes at least one argument\n",argv[0]);
return -1;
}
argc--;
argv++;
for(cmdp = tab;cmdp->name != NULL;cmdp++){
if(strncmp(argv[0],cmdp->name,strlen(argv[0])) == 0){
found = 1;
break;
}
}
if(!found){
printf("valid subcommands:");
for(cmdp = tab;cmdp->name != NULL;cmdp++)
printf(" %s",cmdp->name);
printf("\n");
return -1;
}
if(argc < cmdp->argcmin){
if(cmdp->argc_errmsg != NULL)
printf("Usage: %s\n",cmdp->argc_errmsg);
return -1;
}
if(cmdp->stksize == 0){
return (*cmdp->func)(argc,argv,p);
} else {
/* Make private copy of argv and args */
pargv = (char **)callocw(argc,sizeof(char *));
for(i=0;i<argc;i++)
pargv[i] = strdup(argv[i]);
newproc(cmdp->name,cmdp->stksize,
(void (*)())cmdp->func,argc,pargv,p,1);
return(0);
}
}
/* Subroutine for setting and displaying boolean flags */
int
setbool(var,label,argc,argv)
int *var;
char *label;
int argc;
char *argv[];
{
struct boolcmd *bc;
if(argc < 2){
printf("%s: %s\n",label,*var ? "on":"off");
return 0;
}
for(bc = Boolcmds;bc->str != NULL;bc++){
if(strcmpi(argv[1],bc->str) == 0){
*var = bc->val;
return 0;
}
}
printf("Valid options:");
for(bc = Boolcmds;bc->str != NULL;bc++)
printf(" %s",bc->str);
printf("\n");
return 1;
}
/* Subroutine for setting and displaying bit values */
int
bit16cmd(bits,mask,label,argc,argv)
uint16 *bits;
uint16 mask;
char *label;
int argc;
char *argv[];
{
int doing = (*bits & mask);
int result = setbool( &doing, label, argc, argv );
if ( !result ) {
if ( doing )
*bits |= mask;
else
*bits &= ~mask;
}
return result;
}
/* Subroutine for setting and displaying long variables */
int
setlong(var,label,argc,argv)
long *var;
char *label;
int argc;
char *argv[];
{
if(argc < 2)
printf("%s: %ld\n",label,*var);
else
*var = atol(argv[1]);
return 0;
}
/* Subroutine for setting and displaying short variables */
int
setshort(var,label,argc,argv)
unsigned short *var;
char *label;
int argc;
char *argv[];
{
if(argc < 2)
printf("%s: %u\n",label,*var);
else
*var = atoi(argv[1]);
return 0;
}
/* Subroutine for setting and displaying integer variables */
int
setint(var,label,argc,argv)
int *var;
char *label;
int argc;
char *argv[];
{
if(argc < 2)
printf("%s: %i\n",label,*var);
else
*var = atoi(argv[1]);
return 0;
}
/* Subroutine for setting and displaying unsigned integer variables */
int
setuns(var,label,argc,argv)
unsigned *var;
char *label;
int argc;
char *argv[];
{
if(argc < 2)
printf("%s: %u\n",label,*var);
else
*var = atoi(argv[1]);
return 0;
}