-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathcmd.c
642 lines (583 loc) · 13.3 KB
/
cmd.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
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
// $Id$
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#include "cmd.h"
#ifdef WIN32
# define popen _popen
# define pclose _pclose
#endif
typedef struct {
enum CommandType Type;
const char *Name,
*ArgStr;
void *Val;
const void *p;
} Cmd_T;
static const Enum_T BoolEnum[] = {
{ "FALSE", 0 },
{ "TRUE", 1 },
{ 0, 0 }
};
#ifdef NEEDSTRDUP
char *strdup();
#endif
#define FALSE 0
#define TRUE 1
#define LINSIZ 10240
#define MAXPARAM 256
static Cmd_T cmds[MAXPARAM+1];
static const char *SepString = " \t\n";
/// Return cmd->p, as an int.
static int get_p_int(const Cmd_T *cmd)
{
return *(const int *)cmd->p;
}
/// Return cmd->p, as a pointer to a null-terminated array of Enum_T.
static const Enum_T *get_p_enums(const Cmd_T *cmd)
{
return (const Enum_T *)cmd->p;
}
/// Return cmd->p, as a pointer to a string.
static const char *get_p_char(const Cmd_T *cmd)
{
return (const char *)cmd->p;
}
/// Return cmd->p, as a pointer to an array of two ints.
static const int *get_p_range(const Cmd_T *cmd)
{
return (const int *)cmd->p;
}
/// Return cmd->Val, as a pointer to int.
static int *get_val_int_ptr(const Cmd_T *cmd)
{
return (int *)cmd->Val;
}
/// Return the int at which cmd->Val points.
static int get_val_int(const Cmd_T *cmd)
{
return *get_val_int_ptr(cmd);
}
/// Update the int at which cmd->Val points.
static void update_val_int(const Cmd_T *cmd, int value)
{
*get_val_int_ptr(cmd) = value;
}
/// Return cmd->Val, as a pointer to double.
static double *get_val_double_ptr(const Cmd_T *cmd)
{
return (double *)cmd->Val;
}
/// Return the double at which cmd->Val points.
static double get_val_double(const Cmd_T *cmd)
{
return *get_val_double_ptr(cmd);
}
/// Return cmd->Val as a pointer to a string pointer.
static const char **get_val_char_ptr(const Cmd_T *cmd)
{
return (const char **)cmd->Val;
}
/// Return the string pointer at which cmd->Val points.
static const char *get_val_char(const Cmd_T *cmd)
{
return *get_val_char_ptr(cmd);
}
/// Update the string pointer at which cmd->Val points.
static void update_val_char(const Cmd_T *cmd, const char *s)
{
*get_val_char_ptr(cmd) = s;
}
int DeclareParams(const char *ParName, ...)
{
va_list args;
static int ParamN = 0;
va_start(args, ParName);
for(; ParName;) {
int c,
j = 0;
if(ParamN==MAXPARAM) {
fprintf(stderr, "Too many parameters !!\n");
break;
}
for(c=1; j<ParamN&&(c=strcmp(cmds[j].Name,ParName))<0; j++)
;
if(!c) {
fprintf(stderr,
"Warning: parameter \"%s\" declared twice.\n",
ParName);
}
for(c=ParamN; c>j; c--) {
cmds[c] = cmds[c-1];
}
cmds[j].Name = ParName;
cmds[j].Type = va_arg(args, enum CommandType);
cmds[j].Val = va_arg(args, void *);
switch(cmds[j].Type) {
case CMDENUMTYPE: /* get the pointer to Enum_T struct */
cmds[j].p = va_arg(args, void *);
break;
case CMDSUBRANGETYPE: { /* get the two extremes */
int *subrange = calloc(2, sizeof(int));
cmds[j].p = subrange;
subrange[0] = va_arg(args, int);
subrange[1] = va_arg(args, int);
}
break;
case CMDGTETYPE: /* get lower or upper bound */
case CMDLTETYPE: {
int *value = calloc(1, sizeof(int));
cmds[j].p = value;
value[0] = va_arg(args, int);
}
break;
case CMDSTRARRAYTYPE: { /* get the separators string */
const char *s = va_arg(args, const char *);
cmds[j].p = (s ? strdup(s) : NULL);
}
break;
case CMDBOOLTYPE:
cmds[j].Type = CMDENUMTYPE;
cmds[j].p = BoolEnum;
break;
case CMDDOUBLETYPE: /* nothing else is needed */
case CMDINTTYPE:
case CMDSTRINGTYPE:
break;
default:
fprintf(stderr, "%s: %s %d %s \"%s\"\n",
"DeclareParam()", "Unknown Type",
cmds[j].Type, "for parameter", cmds[j].Name);
exit(1);
}
ParamN++;
ParName = va_arg(args, const char *);
}
cmds[ParamN].Name = NULL;
va_end(args);
return 0;
}
static char *GetLine(FILE *fp, int n, char *Line)
{
int offs=0;
for(;;) {
int j, l;
if(!fgets(Line+offs, n-offs, fp)) {
return NULL;
}
if(Line[offs]=='#') continue;
l = strlen(Line+offs)-1;
Line[offs+l] = 0;
for(j=offs; Line[j] && isspace(Line[j]); j++, l--)
;
if(l<1) continue;
if(j > offs) {
char *s = Line+offs,
*q = Line+j;
while((*s++=*q++))
;
}
if(Line[offs+l-1]=='\\') {
offs += l;
Line[offs-1] = ' ';
} else {
break;
}
}
return Line;
}
static void EnumError(const Cmd_T *cmd, const char *s)
{
const Enum_T *en;
fprintf(stderr,
"Invalid value \"%s\" for parameter \"%s\"\n", s, cmd->Name);
fprintf(stderr, "Valid values are:\n");
for(en=get_p_enums(cmd); en->Name; en++) {
if(*en->Name) {
fprintf(stderr, " %s\n", en->Name);
}
}
fprintf(stderr, "\n");
exit(1);
}
static void GteError(const Cmd_T *cmd, int n)
{
fprintf(stderr,
"Value %d out of range for parameter \"%s\"\n", n, cmd->Name);
fprintf(stderr, "Valid values must be greater than or equal to %d\n",
get_p_int(cmd));
exit(1);
}
static void LteError(const Cmd_T *cmd, int n)
{
fprintf(stderr,
"Value %d out of range for parameter \"%s\"\n", n, cmd->Name);
fprintf(stderr, "Valid values must be less than or equal to %d\n",
get_p_int(cmd));
exit(1);
}
static void SubrangeError(const Cmd_T *cmd, int n)
{
const int *subrange = get_p_range(cmd);
fprintf(stderr,
"Value %d out of range for parameter \"%s\"\n", n, cmd->Name);
fprintf(stderr, "Valid values range from %d to %d\n",
subrange[0], subrange[1]);
exit(1);
}
static void SetEnum(Cmd_T *cmd, const char *s)
{
const Enum_T *en;
for(en=get_p_enums(cmd); en->Name; en++) {
if(*en->Name && !strcmp(s, en->Name)) {
update_val_int(cmd, en->Idx);
return;
}
}
EnumError(cmd, s);
}
static void SetSubrange(Cmd_T *cmd, const char *s)
{
int n;
const int *subrange = get_p_range(cmd);
if(sscanf(s, "%d", &n)!=1) {
fprintf(stderr,
"Integer value required for parameter \"%s\"\n",
cmd->Name);
exit(1);
}
if(n < subrange[0] || n > subrange[1]) {
SubrangeError(cmd, n);
}
update_val_int(cmd, n);
}
static void SetGte(Cmd_T *cmd, const char *s)
{
int n;
if(sscanf(s, "%d", &n)!=1) {
fprintf(stderr,
"Integer value required for parameter \"%s\"\n",
cmd->Name);
exit(1);
}
if(n<get_p_int(cmd)) {
GteError(cmd, n);
}
update_val_int(cmd, n);
}
static char **str2array(const char *s, const char *sep)
{
const char *p;
char **a;
int n = 0,
l;
if(!sep) sep = SepString;
p = s += strspn(s, sep);
while(*p) {
p += strcspn(p, sep);
p += strspn(p, sep);
++n;
}
a = calloc(n+1, sizeof(char *));
p = s;
n = 0;
while(*p) {
l = strcspn(p, sep);
a[n] = malloc(l+1);
memcpy(a[n], p, l);
a[n][l] = 0;
++n;
p += l;
p += strspn(p, sep);
}
return a;
}
static void SetStrArray(Cmd_T *cmd, const char *s)
{
*(char***)cmd->Val = str2array(s, get_p_char(cmd));
}
static void SetLte(Cmd_T *cmd, const char *s)
{
int n;
if(sscanf(s, "%d", &n)!=1) {
fprintf(stderr,
"Integer value required for parameter \"%s\"\n",
cmd->Name);
exit(1);
}
if(n > get_p_int(cmd)) {
LteError(cmd, n);
}
update_val_int(cmd, n);
}
static void SetParam(Cmd_T *cmd, const char *s)
{
if(!*s && cmd->Type != CMDSTRINGTYPE) {
fprintf(stderr,
"WARNING: No value specified for parameter \"%s\"\n",
cmd->Name);
return;
}
switch(cmd->Type) {
case CMDDOUBLETYPE:
if(sscanf(s, "%lf", get_val_double_ptr(cmd))!=1) {
fprintf(stderr,
"Float value required for parameter \"%s\"\n",
cmd->Name);
exit(1);
}
break;
case CMDENUMTYPE:
SetEnum(cmd, s);
break;
case CMDINTTYPE:
if(sscanf(s, "%d", get_val_int_ptr(cmd))!=1) {
fprintf(stderr,
"Integer value required for parameter \"%s\"\n",
cmd->Name);
exit(1);
}
break;
case CMDSTRINGTYPE:
update_val_char(cmd,
(strcmp(s, "<NULL>") && strcmp(s, "NULL"))
? strdup(s)
: 0);
break;
case CMDSTRARRAYTYPE:
SetStrArray(cmd, s);
break;
case CMDGTETYPE:
SetGte(cmd, s);
break;
case CMDLTETYPE:
SetLte(cmd, s);
break;
case CMDSUBRANGETYPE:
SetSubrange(cmd, s);
break;
default:
fprintf(stderr, "%s: %s %d %s \"%s\"\n",
"SetParam",
"Unknown Type",
cmd->Type,
"for parameter",
cmd->Name);
exit(1);
}
cmd->ArgStr = strdup(s);
}
static int Scan(const char *ProgName, Cmd_T *cmds, char *Line)
{
char *q,
*p;
int i,
hl,
HasToMatch = FALSE,
c0,
c;
p = Line+strspn(Line, SepString);
hl = strcspn(p, SepString);
if(!hl) {
return 0;
}
q = strchr(p, '/');
if(q && q-p<hl) {
*q = 0;
if(strcmp(p, ProgName)) {
*q = '/';
return 0;
}
*q = '/';
HasToMatch=TRUE;
p = q+1;
}
hl = strcspn(p, SepString);
if(!hl) {
return 0;
}
c0 = p[hl];
p[hl] = 0;
for(i=0, c=1; cmds[i].Name&&(c=strcmp(cmds[i].Name, p))<0; i++)
;
p[hl] = c0;
if (c)
return HasToMatch && c;
SetParam(cmds+i, p+hl+strspn(p+hl, SepString));
return 0;
}
static void PrintEnum(const Cmd_T *cmd, int ValFlag, FILE *fp)
{
const Enum_T *en;
fprintf(fp, "%s", cmd->Name);
if(ValFlag) {
for(en=get_p_enums(cmd); en->Name; en++) {
if(*en->Name && en->Idx==get_val_int(cmd)) {
fprintf(fp, ": %s", en->Name);
}
}
}
fprintf(fp, "\n");
}
static void PrintStrArray(const Cmd_T *cmd, int ValFlag, FILE *fp)
{
char *indent,
**s = *(char***)cmd->Val;
int l = 4+strlen(cmd->Name);
fprintf(fp, "%s", cmd->Name);
indent = malloc(l+2);
memset(indent, ' ', l+1);
indent[l+1] = 0;
if(ValFlag) {
fprintf(fp, ": %s", s ? (*s ? *s++ : "NULL") : "");
if(s) while(*s) {
fprintf(fp, "\n%s %s", indent, *s++);
}
}
free(indent);
fprintf(fp, "\n");
}
static void PrintParam(const Cmd_T *cmd, int ValFlag, FILE *fp)
{
fprintf(fp, "%4s", "");
switch(cmd->Type) {
case CMDDOUBLETYPE:
fprintf(fp, "%s", cmd->Name);
if(ValFlag) fprintf(fp, ": %22.15e", get_val_double(cmd));
fprintf(fp, "\n");
break;
case CMDENUMTYPE:
PrintEnum(cmd, ValFlag, fp);
break;
case CMDINTTYPE:
case CMDSUBRANGETYPE:
case CMDGTETYPE:
case CMDLTETYPE:
fprintf(fp, "%s", cmd->Name);
if(ValFlag) fprintf(fp, ": %d", get_val_int(cmd));
fprintf(fp, "\n");
break;
case CMDSTRINGTYPE:
fprintf(fp, "%s", cmd->Name);
if(ValFlag) {
const char *value = get_val_char(cmd);
if(value) {
fprintf(fp, ": \"%s\"", value);
} else {
fprintf(fp, ": %s", "NULL");
}
}
fprintf(fp, "\n");
break;
case CMDSTRARRAYTYPE:
PrintStrArray(cmd, ValFlag, fp);
break;
default:
fprintf(stderr, "%s: %s %d %s \"%s\"\n",
"PrintParam",
"Unknown Type",
cmd->Type,
"for parameter",
cmd->Name);
exit(1);
}
}
static void PrintParams(int ValFlag, FILE *fp)
{
int i;
fflush(fp);
if(ValFlag) {
fprintf(fp, "Parameters Values:\n");
} else {
fprintf(fp, "Parameters:\n");
}
for(i=0; cmds[i].Name; i++) PrintParam(cmds+i, ValFlag, fp);
fprintf(fp, "\n");
fflush(fp);
}
static void CmdError(const char *opt)
{
fprintf(stderr, "Invalid option \"%s\"\n", opt);
fprintf(stderr, "This program expectes the following parameters:\n");
PrintParams(FALSE, stderr);
exit(0);
}
int GetParams(int *n, char ***a, const char *CmdFileName)
{
char *Line,
*ProgName;
int argc = *n;
char **argv = *a,
*s;
FILE *fp;
int IsPipe;
#ifdef MSDOS
#define PATHSEP '\\'
char *dot = NULL;
#else
#define PATHSEP '/'
#endif
if(!(Line=malloc(LINSIZ))) {
fprintf(stderr, "GetParams(): Unable to alloc %d bytes\n",
LINSIZ);
exit(1);
}
if((ProgName=strrchr(*argv, PATHSEP))) {
++ProgName;
} else {
ProgName = *argv;
}
#ifdef MSDOS
if(dot=strchr(ProgName, '.')) *dot = 0;
#endif
--argc;
++argv;
for(;;) {
if(argc && argv[0][0]=='-' && argv[0][1]=='=') {
CmdFileName = argv[0]+2;
++argv;
--argc;
}
if(!CmdFileName) {
break;
}
IsPipe = !strncmp(CmdFileName, "@@", 2);
fp = IsPipe
? popen(CmdFileName+2, "r")
: strcmp(CmdFileName, "-")
? fopen(CmdFileName, "r")
: stdin;
if(!fp) {
fprintf(stderr, "Unable to open command file %s\n",
CmdFileName);
exit(1);
}
while(GetLine(fp, LINSIZ, Line) && strcmp(Line, "\\End")) {
if(Scan(ProgName, cmds, Line)) {
CmdError(Line);
}
}
if(fp!=stdin) {
if(IsPipe) pclose(fp);
else fclose(fp);
}
CmdFileName = NULL;
}
while(argc && **argv=='-' && (s=strchr(*argv, '='))) {
*s = ' ';
sprintf(Line, "%s/%s", ProgName, *argv+1);
*s = '=';
if(Scan(ProgName, cmds, Line)) CmdError(*argv);
--argc;
++argv;
}
*n = argc;
*a = argv;
#ifdef MSDOS
if(dot) *dot = '.';
#endif
free(Line);
return 0;
}