1
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
53
54
55
56
57
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
84
85
86
87
88
89
90
91
92
93
94
95
100
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
122
123
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
208
209
210
216
217
218
219
220
221
222
223
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
276
277
278
279
280
281
282
283
284
285
286
287
288
292
297
312
319
326
333
340
347
348
349
350
351
352
353
354
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
415
416
417
418
424
427
433
436
437
438
439
440
441
442
443
444
451
457
464
465
466
467
474
475
476
477
478
479
480
481
482
483
484
485
486
487
493
494
506
512
513
514
515
516
517
518
519
520
521
522
523
524
525
529
530
531
532
533
534
535
536
539
540
546
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
569
570
571
572
573
574
575
/* ... */
/* ... */
#include "argtable3.h"
#ifndef ARG_AMALGAMATION
#include "argtable3_private.h"
#endif
#include <stdlib.h>
#include <string.h>
char* arg_strptime(const char* buf, const char* fmt, struct tm* tm);
static void arg_date_resetfn(struct arg_date* parent) {
ARG_TRACE(("%s:resetfn(%p)\n", __FILE__, parent));
parent->count = 0;
}{ ... }
static int arg_date_scanfn(struct arg_date* parent, const char* argval) {
int errorcode = 0;
if (parent->count == parent->hdr.maxcount) {
errorcode = ARG_ERR_MAXCOUNT;
}{...} else if (!argval) {
parent->count++;
}{...} else {
const char* pend;
struct tm tm = parent->tmval[parent->count];
pend = arg_strptime(argval, parent->format, &tm);
if (pend && pend[0] == '\0')
parent->tmval[parent->count++] = tm;
else
errorcode = ARG_ERR_BADDATE;
}{...}
ARG_TRACE(("%s:scanfn(%p) returns %d\n", __FILE__, parent, errorcode));
return errorcode;
}{ ... }
static int arg_date_checkfn(struct arg_date* parent) {
int errorcode = (parent->count < parent->hdr.mincount) ? ARG_ERR_MINCOUNT : 0;
ARG_TRACE(("%s:checkfn(%p) returns %d\n", __FILE__, parent, errorcode));
return errorcode;
}{ ... }
static void arg_date_errorfn(struct arg_date* parent, arg_dstr_t ds, int errorcode, const char* argval, const char* progname) {
const char* shortopts = parent->hdr.shortopts;
const char* longopts = parent->hdr.longopts;
const char* datatype = parent->hdr.datatype;
argval = argval ? argval : "";
arg_dstr_catf(ds, "%s: ", progname);
switch (errorcode) {
case ARG_ERR_MINCOUNT:
arg_dstr_cat(ds, "missing option ");
arg_print_option_ds(ds, shortopts, longopts, datatype, "\n");
break;
...
case ARG_ERR_MAXCOUNT:
arg_dstr_cat(ds, "excess option ");
arg_print_option_ds(ds, shortopts, longopts, argval, "\n");
break;
...
case ARG_ERR_BADDATE: {
struct tm tm;
char buff[200];
arg_dstr_catf(ds, "illegal timestamp format \"%s\"\n", argval);
memset(&tm, 0, sizeof(tm));
arg_strptime("1999-12-31 23:59:59", "%F %H:%M:%S", &tm);
strftime(buff, sizeof(buff), parent->format, &tm);
arg_dstr_catf(ds, "correct format is \"%s\"\n", buff);
break;
}{...}
... }{...}
}{ ... }
struct arg_date* arg_date0(const char* shortopts, const char* longopts, const char* format, const char* datatype, const char* glossary) {
return arg_daten(shortopts, longopts, format, datatype, 0, 1, glossary);
}{ ... }
struct arg_date* arg_date1(const char* shortopts, const char* longopts, const char* format, const char* datatype, const char* glossary) {
return arg_daten(shortopts, longopts, format, datatype, 1, 1, glossary);
}{ ... }
struct arg_date*
arg_daten(const char* shortopts, const char* longopts, const char* format, const char* datatype, int mincount, int maxcount, const char* glossary) {
size_t nbytes;
struct arg_date* result;
maxcount = (maxcount < mincount) ? mincount : maxcount;
if (!format)
format = "%x";
nbytes = sizeof(struct arg_date)
+ (size_t)maxcount * sizeof(struct tm);
result = (struct arg_date*)xcalloc(1, nbytes);
result->hdr.flag = ARG_HASVALUE;
result->hdr.shortopts = shortopts;
result->hdr.longopts = longopts;
result->hdr.datatype = datatype ? datatype : format;
result->hdr.glossary = glossary;
result->hdr.mincount = mincount;
result->hdr.maxcount = maxcount;
result->hdr.parent = result;
result->hdr.resetfn = (arg_resetfn*)arg_date_resetfn;
result->hdr.scanfn = (arg_scanfn*)arg_date_scanfn;
result->hdr.checkfn = (arg_checkfn*)arg_date_checkfn;
result->hdr.errorfn = (arg_errorfn*)arg_date_errorfn;
result->tmval = (struct tm*)(result + 1);
result->count = 0;
result->format = format;
ARG_TRACE(("arg_daten() returns %p\n", result));
return result;
}{ ... }
/* ... */
#include <ctype.h>
#include <string.h>
#include <time.h>
/* ... */
#define ALT_E 0x01
#define ALT_O 0x02
#define LEGAL_ALT(x) \
{ \
if (alt_format & ~(x)) \
return (0); \
}{...}
...#define TM_YEAR_BASE (1900)
static int conv_num(const char**, int*, int, int);
static const char* day[7] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
static const char* abday[7] = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};
static const char* mon[12] = {"January", "February", "March", "April", "May", "June",
"July", "August", "September", "October", "November", "December"}{...};
static const char* abmon[12] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
static const char* am_pm[2] = {"AM", "PM"};
static int arg_strcasecmp(const char* s1, const char* s2) {
const unsigned char* us1 = (const unsigned char*)s1;
const unsigned char* us2 = (const unsigned char*)s2;
while (tolower(*us1) == tolower(*us2++))
if (*us1++ == '\0')
return 0;
return tolower(*us1) - tolower(*--us2);
}{ ... }
static int arg_strncasecmp(const char* s1, const char* s2, size_t n) {
if (n != 0) {
const unsigned char* us1 = (const unsigned char*)s1;
const unsigned char* us2 = (const unsigned char*)s2;
do {
if (tolower(*us1) != tolower(*us2++))
return tolower(*us1) - tolower(*--us2);
if (*us1++ == '\0')
break;
}{...} while (--n != 0);
}{...}
return 0;
}{ ... }
char* arg_strptime(const char* buf, const char* fmt, struct tm* tm) {
char c;
const char* bp;
size_t len = 0;
int alt_format, i, split_year = 0;
bp = buf;
while ((c = *fmt) != '\0') {
alt_format = 0;
if (isspace(c)) {
while (isspace((int)(*bp)))
bp++;
fmt++;
continue;
}{...}
if ((c = *fmt++) != '%')
goto literal;
again:
switch (c = *fmt++) {
case '%':
literal:
if (c != *bp++)
return (0);
break;
/* ... */
... case 'E':
LEGAL_ALT(0);
alt_format |= ALT_E;
goto again;
...
case 'O':
LEGAL_ALT(0);
alt_format |= ALT_O;
goto again;
/* ... */
... case 'c':
LEGAL_ALT(ALT_E);
bp = arg_strptime(bp, "%x %X", tm);
if (!bp)
return (0);
break;
...
case 'D':
LEGAL_ALT(0);
bp = arg_strptime(bp, "%m/%d/%y", tm);
if (!bp)
return (0);
break;
...
case 'R':
LEGAL_ALT(0);
bp = arg_strptime(bp, "%H:%M", tm);
if (!bp)
return (0);
break;
...
case 'r':
LEGAL_ALT(0);
bp = arg_strptime(bp, "%I:%M:%S %p", tm);
if (!bp)
return (0);
break;
...
case 'T':
LEGAL_ALT(0);
bp = arg_strptime(bp, "%H:%M:%S", tm);
if (!bp)
return (0);
break;
...
case 'X':
LEGAL_ALT(ALT_E);
bp = arg_strptime(bp, "%H:%M:%S", tm);
if (!bp)
return (0);
break;
...
case 'x':
LEGAL_ALT(ALT_E);
bp = arg_strptime(bp, "%m/%d/%y", tm);
if (!bp)
return (0);
break;
/* ... */
... case 'A':
case 'a':
LEGAL_ALT(0);
for (i = 0; i < 7; i++) {
len = strlen(day[i]);
if (arg_strncasecmp(day[i], bp, len) == 0)
break;
len = strlen(abday[i]);
if (arg_strncasecmp(abday[i], bp, len) == 0)
break;
}{...}
if (i == 7)
return (0);
tm->tm_wday = i;
bp += len;
break;
...
case 'B':
case 'b':
case 'h':
LEGAL_ALT(0);
for (i = 0; i < 12; i++) {
len = strlen(mon[i]);
if (arg_strncasecmp(mon[i], bp, len) == 0)
break;
len = strlen(abmon[i]);
if (arg_strncasecmp(abmon[i], bp, len) == 0)
break;
}{...}
if (i == 12)
return (0);
tm->tm_mon = i;
bp += len;
break;
...
case 'C':
LEGAL_ALT(ALT_E);
if (!(conv_num(&bp, &i, 0, 99)))
return (0);
if (split_year) {
tm->tm_year = (tm->tm_year % 100) + (i * 100);
}{...} else {
tm->tm_year = i * 100;
split_year = 1;
}{...}
break;
...
case 'd':
case 'e':
LEGAL_ALT(ALT_O);
if (!(conv_num(&bp, &tm->tm_mday, 1, 31)))
return (0);
break;
...
case 'k':
LEGAL_ALT(0);
...
case 'H':
LEGAL_ALT(ALT_O);
if (!(conv_num(&bp, &tm->tm_hour, 0, 23)))
return (0);
break;
...
case 'l':
LEGAL_ALT(0);
...
case 'I':
LEGAL_ALT(ALT_O);
if (!(conv_num(&bp, &tm->tm_hour, 1, 12)))
return (0);
if (tm->tm_hour == 12)
tm->tm_hour = 0;
break;
...
case 'j':
LEGAL_ALT(0);
if (!(conv_num(&bp, &i, 1, 366)))
return (0);
tm->tm_yday = i - 1;
break;
...
case 'M':
LEGAL_ALT(ALT_O);
if (!(conv_num(&bp, &tm->tm_min, 0, 59)))
return (0);
break;
...
case 'm':
LEGAL_ALT(ALT_O);
if (!(conv_num(&bp, &i, 1, 12)))
return (0);
tm->tm_mon = i - 1;
break;
...
case 'p':
LEGAL_ALT(0);
if (arg_strcasecmp(am_pm[0], bp) == 0) {
if (tm->tm_hour > 11)
return (0);
bp += strlen(am_pm[0]);
break;
}{...}
else if (arg_strcasecmp(am_pm[1], bp) == 0) {
if (tm->tm_hour > 11)
return (0);
tm->tm_hour += 12;
bp += strlen(am_pm[1]);
break;
}{...}
return (0);
...
case 'S':
LEGAL_ALT(ALT_O);
if (!(conv_num(&bp, &tm->tm_sec, 0, 61)))
return (0);
break;
...
case 'U':
case 'W':
LEGAL_ALT(ALT_O);
/* ... */
if (!(conv_num(&bp, &i, 0, 53)))
return (0);
break;
...
case 'w':
LEGAL_ALT(ALT_O);
if (!(conv_num(&bp, &tm->tm_wday, 0, 6)))
return (0);
break;
...
case 'Y':
LEGAL_ALT(ALT_E);
if (!(conv_num(&bp, &i, 0, 9999)))
return (0);
tm->tm_year = i - TM_YEAR_BASE;
break;
...
case 'y':
LEGAL_ALT(ALT_E | ALT_O);
if (!(conv_num(&bp, &i, 0, 99)))
return (0);
if (split_year) {
tm->tm_year = ((tm->tm_year / 100) * 100) + i;
break;
}{...}
split_year = 1;
if (i <= 68)
tm->tm_year = i + 2000 - TM_YEAR_BASE;
else
tm->tm_year = i + 1900 - TM_YEAR_BASE;
break;
/* ... */
... case 'n':
case 't':
LEGAL_ALT(0);
while (isspace((int)(*bp)))
bp++;
break;
...
default:
return (0);...
}{...}
}{...}
return ((char*)bp);
}{ ... }
static int conv_num(const char** buf, int* dest, int llim, int ulim) {
int result = 0;
int rulim = ulim;
if (**buf < '0' || **buf > '9')
return (0);
do {
result *= 10;
result += *(*buf)++ - '0';
rulim /= 10;
}{...} while ((result * 10 <= ulim) && rulim && **buf >= '0' && **buf <= '9');
if (result < llim || result > ulim)
return (0);
*dest = result;
return (1);
}{ ... }