-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcl.cpp
555 lines (458 loc) · 12.6 KB
/
cl.cpp
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
/**
** cl.cpp
** Monday, July 8, 2002
**
** Describe a cursor stepping thru the chars of the args of the argv
** of the main routine of a C/C++ program.
**
** Grep views include:
**
** grep "^[A-Za-z_]" // subroutines
**
** Read -xyz as -x yz, or as -x -y z, or as -x -y -z.
**
** Read ints printed as decimal or hexadecimal.
**
** Read // commentary.
**
** Read the blank-delimited words of a string (for example, an
** environment variable) as a list of args.
**
** Allocate string space as needed from the malloc heap. Leave the
** work of garbage collection to the caller, or to stdlib.exit.
**
** Wish the Posix standard and Gnu extensions of:
** http://www.gnu.org/software/gengetopt/man_getopt.html
** did more of this via getopt argv, getopt argc, optind, & nextchar.
**
** See also "IEEE Std 1003.2-1992 Interpretation #150"
** http://standards.ieee.org/reading/ieee/interp/1003-2-92_int/pasc-1003.2-150.html
**/
/**
** Link with standard C libraries.
**/
#include <limits.h> /* INT_MAX ... */
#include <stdio.h> /* FILE ... */
#include <stdlib.h> /* calloc ... */
#include <string.h> /* memset ... */
/**
** Link with the *.c* of ../plscsi/.
**/
#include "plscsi.h"
/**
** Describe the main command line of a C/C++ program.
**/
struct CommandLine
{
char const * const * theArgs;
int theArgsLength;
int theArgIndex;
int theArgOffset;
};
/**
** List the decimal digits in order, so that the offset of a digit
** is its value.
**/
#define DECIMAL_DIGIT_SET "0123456789"
/**
** List the decimal subset of the hex digits twice, so that the
** offset of an upper or lower case hex digit, modulo x10, is its
** value.
**/
#define HEX_DIGIT_SET "0123456789ABCDEF0123456789abcdef"
/**
** Construct a new CommandLine.
**/
CommandLine * newCommandLine(void)
{
CommandLine * cl = (CommandLine *) calloc(1, sizeof (CommandLine));
if (cl != NULL)
{
cl->theArgs = NULL;
cl->theArgsLength = 0;
cl->theArgIndex = 0;
cl->theArgOffset = 0;
return cl;
}
return NULL;
}
/**
** Re/start the parse of the main args.
**/
void clSet(CommandLine * cl, char const * const * argv, int argc)
{
cl->theArgs = argv;
cl->theArgsLength = argc;
cl->theArgIndex = 0;
cl->theArgOffset = 0;
}
/**
** Convert to a mutable array of mutable strings from a string.
**
** Treat the chars of the blankSet, except not the trailing NUL,
** as blanks that separate strings. See strings separated by
** blanks. Strip leading and trailing blanks.
**
** Also add a trailing null string past the last string.
**
** Example usage:
**
** char * * argv = toArgs(getenv("SOME_VAR_NAME"), " \t\n\r");
** int argc = toArgsLength(argv);
**
** Compare java.util.StringTokenizer
** http://java.sun.com/j2se/1.4/docs/api/java/util/StringTokenizer.html
**
** See toArgsLength(char const * const *).
**/
static char * * toArgs(char const * st, char const * set)
{
/* Fail if no chars. */
if (st == NULL)
{
return NULL; /* fail */
}
/* Construct a large enough array. */
/* int maxArgsLength = (((strlen(st) + 1) / 2) + 1); // perhaps */
int maxArgsLength = strlen(st);
char * * args = (char * *) calloc(1, maxArgsLength * sizeof (char const *));
if (args == NULL)
{
return NULL; /* fail */
}
/* Fill the array with words. */
int argsIndex = 0;
for (;;)
{
/* Skip over white space & break past the last word. */
st += strspn(st, set);
if (*st == '\0')
{
break;
}
/* Measure and copy a word. */
int argLength = strcspn(st, set);
char * arg = (char *) calloc(1, argLength + 1); /* add 1 to add sizeof trailing Nul */
if (arg == NULL)
{
return NULL; /* fail */
}
(void) memmove(arg, st, argLength); /* could memcpy */
arg[argLength] = '\0'; /* calloc did this already */
st += argLength;
/* Remember the copied word. */
args[argsIndex++] = arg;
}
/* Add a trailing null string past the last string. */
args[argsIndex++] = NULL;
return args;
}
/**
** Count the not-null strings of an array of strings.
**/
static int toArgsLength(char const * const * args)
{
int argsLength = 0;
if (args != NULL)
{
while (*args++ != NULL)
{
++argsLength;
}
}
return argsLength;
}
/**
** Re/start the parse of args from a string.
**/
void clSetFromString(CommandLine * cl, char const * st)
{
char * * argv = toArgs(st, " \t\n\r"); /* leave out "\a\b\f" */
cl->theArgs = (char const * const *) argv; /* tcpp101 requires this cast */
cl->theArgsLength = toArgsLength(cl->theArgs);
cl->theArgIndex = 0;
cl->theArgOffset = 0;
}
/**
** Return the next arg that will be read, else null.
**/
char const * clPeek(CommandLine * cl)
{
/* Fetch the CommandLine. */
char const * const * args = cl->theArgs;
int argsIndex = cl->theArgIndex;
int argsLength = cl->theArgsLength;
int argOffset = cl->theArgOffset;
/* Find the arg. */
if (args != NULL)
if (argsIndex < argsLength)
{
char const * arg = args[argsIndex];
/* Split the arg. */
if (arg != NULL)
{
if (0 <= argOffset)
{
arg = &arg[argOffset];
}
}
return arg;
}
return NULL;
}
/**
** Consume the selected arg and return it, else return null.
**/
char const * clRead(CommandLine * cl)
{
char const * arg = clPeek(cl);
if (arg != NULL)
{
++cl->theArgIndex;
cl->theArgOffset = 0;
}
return arg;
}
/**
** Consume a comment and return zero, else return negative.
**
** Read an arg of -- as an isolated comment.
**
** Read all the args following the arg // as part of one comment.
**/
int clReadComment(CommandLine * cl)
{
char const * arg = clPeek(cl);
if (arg != NULL)
{
if ((arg[0] == '-') && (arg[1] == '-') && (arg[2] == '\0'))
{
(void) clRead(cl);
return 0; /* succeed */
}
if ((arg[0] == '/') && (arg[1] == '/') && (arg[2] == '\0'))
{
cl->theArgIndex = cl->theArgsLength;
cl->theArgOffset = 0;
return 0; /* succeed */
}
}
return -1; /* fail */
}
/**
** Consume another concise -xyz switch and return it, else
** return -1.
**/
int clReadSwitch(CommandLine * cl)
{
/* Find the arg. */
char const * arg = clPeek(cl);
if (arg != NULL)
{
/* Read "-" from "-xyz", but leave "-" or "--" alone. */
int argOffset = cl->theArgOffset;
if (argOffset == 0)
{
char ch0 = arg[0];
if (ch0 == '-')
{
char ch1 = arg[1];
if ((ch1 != '\0') && (ch1 != '-'))
{
cl->theArgOffset = argOffset = 1;
++arg; /* in effect, again: arg = peekArg(cl); */
}
}
}
/* Read the first else the next of the switches of a list. */
if (0 < argOffset)
{
char ch0 = arg[0]; /* theArgs[theArgIndex][theArgOffset] */
int switchInt = (ch0 & 0xFF);
char ch1 = arg[1];
/* Read the switch, don't just peek at it. */
cl->theArgOffset = (argOffset + 1);
/* After consuming the last switch, consume the arg. */
if (ch1 == '\0')
{
(void) clRead(cl);
}
/* Return the switch read. */
return switchInt;
}
/* Else say no switch read. */
}
return -1;
}
/**
** Return the leftmost offset into a string at which a char is
** found, else return negative. Differ from c.lang.string.strchr
** by never finding '\0' in a string.
**/
static int indexOf(char const * st, char ch)
{
int index = -1;
if (st != NULL)
{
char const * which = strchr(st, ch);
if ((which != NULL) && (*which != '\0'))
{
index = ((int) (which - st));
}
}
return index;
}
/**
** Convert to int from nothing but digits. Return an equal
** nonnegative int value, else return negative.
**
** Skip over leading zeroes without changing the radix or digitSet.
**
** Reject anything not in the digitSet, even a leading plus or
** minus sign.
**
** Differ from c.lang.stdio.sscanf by ensuring c.lang.stdio.printf
** is equal. For example, in places where INT_MAX is x7FFF, do
** not accept x12345 as meaning x2345.
**
** Differ from c.lang.stdio.sscanf %d by ensuring that all of the
** string was read. Note c.lang.stdio.sscanf %d%c should return 1
** rather than 2 if indeed all of a string was read.
**/
static INT fromIntDigits(char const * st, char const * set, int radix)
{
/* Constrain args. */
if ((st == NULL) || (set == NULL) && (radix < 2))
{
return -1; /* fail */
}
/* Sum the digits. */
INT sum = 0;
while (*st != '\0')
{
char ch = *st++;
int delta = indexOf(set, ch);
if (delta < 0)
{
return -1; /* fail */
}
int digit = (delta % radix);
INT maxInt = INT_MOST_POS;
if (((maxInt - digit) / radix) < sum)
{
return -1; /* fail */
}
sum *= radix; sum += digit;
}
return sum; /* succeed */
}
/**
** Consume an int and return an equal nonnegative int value, else
** return negative.
**
** Differ from c.lang.stdio.sscanf by ensuring c.lang.stdio.printf
** is equal. For example, in places where INT_MAX is x7FFF, do
** not accept x12345 as meaning x2345.
**
** Differ from C by accepting any of the prefixes "0x x X" to mean
** hexadecimal, not just the prefix "0x".
**
** Differ from c.lang.stdio.sscanf %d by refusing to let a decimal
** int other than "0" to begin with zeroes.
**
** Differ from C by accepting only decimal or hexadecimal ints,
** not also octal ints composed of decimal digits that happen to
** begin with the zero.
**/
INT clReadInt(CommandLine * cl)
{
INT sum = -1;
/* Parse the arg. */
char const * arg = clPeek(cl);
if (arg != NULL)
{
/* Choose radix. */
int radix = 10;
char const * set = DECIMAL_DIGIT_SET;
if ((arg[0] == '0') && (arg[1] != 'x') && (arg[1] != '\0'))
{
return -1; /* reject octal */
}
if ((arg[0] == '0') && (arg[1] == 'x'))
{
++arg;
}
if ((arg[0] == 'x') || (arg[0] == 'X'))
{
++arg;
radix = 0x10;
set = HEX_DIGIT_SET;
}
/* Accept decimal. */
sum = fromIntDigits(arg, set, radix);
/* Consume only an acceptable arg. */
if (0 <= sum)
{
(void) clRead(cl);
}
}
/* Proceed. */
return sum;
}
/**
** Consume a hex byte and return an equal nonnegative int value,
** else return negative.
**
** As a hex byte, accept:
**
** ( *|:)?[0-9A-Fa-f][0-9A-Fa-f]?
**
** That is, one or two hexadecimal digits in upper or lower or mixed
** case, preceded by run of blanks or by a single colon.
**/
int clReadXX(CommandLine * cl)
{
/* Read an arg. */
char const * arg = clPeek(cl);
if (arg == NULL)
{
return -1;
}
/* Disregard one colon or a number of blanks (or neither). */
int index = 0;
if (arg[index] == ':')
{
++index;
}
else
{
while (arg[index] == ' ')
{
++index;
}
}
/* Accept one or two hexadecimal digits. */
int digit = indexOf(HEX_DIGIT_SET, arg[index]);
if (0 <= digit)
{
++index;
int sum = (digit & 0xF);
digit = indexOf(HEX_DIGIT_SET, arg[index]);
if (0 <= digit)
{
++index;
sum <<= 4; sum += (digit & 0xF);
}
/* Consume everything accepted. */
cl->theArgOffset += index;
if (arg[index] == '\0')
{
(void) clRead(cl);
}
/* Succeed. */
return sum;
}
/* Otherwise fail. */
return -1; /* fail */
}
/* end of file */