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
55
56
74
75
81
82
83
84
85
86
87
88
89
90
91
102
103
104
107
108
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
/* ... */
/* ... */
#include "argtable3.h"
#ifndef ARG_AMALGAMATION
#include "argtable3_private.h"
#endif
#include <stdlib.h>
static void arg_str_resetfn(struct arg_str* parent) {
int i;
ARG_TRACE(("%s:resetfn(%p)\n", __FILE__, parent));
for (i = 0; i < parent->count; i++) {
parent->sval[i] = "";
}{...}
parent->count = 0;
}{ ... }
static int arg_str_scanfn(struct arg_str* parent, const char* argval) {
int errorcode = 0;
if (parent->count == parent->hdr.maxcount) {
errorcode = ARG_ERR_MAXCOUNT;
}{...} else if (!argval) {
parent->count++;
}{...} else {
parent->sval[parent->count++] = argval;
}{...}
ARG_TRACE(("%s:scanfn(%p) returns %d\n", __FILE__, parent, errorcode));
return errorcode;
}{ ... }
static int arg_str_checkfn(struct arg_str* 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_str_errorfn(struct arg_str* 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;...
}{...}
}{ ... }
struct arg_str* arg_str0(const char* shortopts, const char* longopts, const char* datatype, const char* glossary) {
return arg_strn(shortopts, longopts, datatype, 0, 1, glossary);
}{ ... }
struct arg_str* arg_str1(const char* shortopts, const char* longopts, const char* datatype, const char* glossary) {
return arg_strn(shortopts, longopts, datatype, 1, 1, glossary);
}{ ... }
struct arg_str* arg_strn(const char* shortopts, const char* longopts, const char* datatype, int mincount, int maxcount, const char* glossary) {
size_t nbytes;
struct arg_str* result;
int i;
maxcount = (maxcount < mincount) ? mincount : maxcount;
nbytes = sizeof(struct arg_str)
+ (size_t)maxcount * sizeof(char*);
result = (struct arg_str*)xmalloc(nbytes);
result->hdr.flag = ARG_HASVALUE;
result->hdr.shortopts = shortopts;
result->hdr.longopts = longopts;
result->hdr.datatype = datatype ? datatype : "<string>";
result->hdr.glossary = glossary;
result->hdr.mincount = mincount;
result->hdr.maxcount = maxcount;
result->hdr.parent = result;
result->hdr.resetfn = (arg_resetfn*)arg_str_resetfn;
result->hdr.scanfn = (arg_scanfn*)arg_str_scanfn;
result->hdr.checkfn = (arg_checkfn*)arg_str_checkfn;
result->hdr.errorfn = (arg_errorfn*)arg_str_errorfn;
result->sval = (const char**)(result + 1);
result->count = 0;
for (i = 0; i < maxcount; i++)
result->sval[i] = "";
ARG_TRACE(("arg_strn() returns %p\n", result));
return result;
}{ ... }