Debian Codegraph @

lib/util/shhopt.h

raw
1 #if 0
2 =============================================================================
3 HERE IS AN EXAMPLE OF THE USE OF SHHOPT:
4
5
6 #include <shhopt.h>
7 int
8 main ( int argc, char **argv ) {
9
10 /* initial values here are just to demonstrate what gets set and
11 what doesn't by the code below.
12 */
13 unsigned int heightSpec =7;
14 unsigned int nameSpec= 7;
15 char *name= "initial";
16 int height=7;
17 int verboseFlag=7;
18 int debugFlag=7;
19 char ** methodlist;
20 struct optNameValue * optlist;
21
22 optStruct3 opt;
23 unsigned int option_def_index = 0;
24 optEntry * option_def;
25 MALLOCARRAY(option_def, 100);
26
27 OPTENT3(0, "height", OPT_INT, &height, &heightSpec, 0);
28 OPTENT3('n', "name", OPT_STRING, &name, &nameSpec, 0);
29 OPTENT3('v', "verbose", OPT_FLAG, &verboseFlag, NULL, 0);
30 OPTENT3('g', "debug", OPT_FLAG, &debugFlag, NULL, 0);
31 OPTENT3(0, "methods", OPT_STRINGLIST, &methodlist, &methodSpec, 0);
32 OPTENT3(0, "options", OPT_NAMELIST, &optlist, &optSpec, 0);
33
34 opt.opt_table = option_def;
35 opt.short_allowed = 1;
36 opt.allowNegNum = 1;
37
38
39 pm_optParseOptions3(&argc, argv, opt, sizeof(opt), 0);
40
41
42 printf("argc=%d\n", argc);
43 printf("height=%d\n", height);
44 printf("height_spec=%d\n", heightSpec);
45 printf("name='%s'\n", name);
46 printf("name_spec=%d\n", nameSpec);
47 printf("verbose_flag=%d\n", verboseFlag);
48 printf("debug_flag=%d\n", verboseFlag);
49
50 if (methodSpec) {
51 unsigned int i;
52 printf("methods: ");
53 while (methodlist[i]) {
54 printf("'%s', ", methodlist[i]);
55 ++i;
56 }
57 free(methodlist);
58 } else
59 printf("No -options\n");
60
61 if (optSpec) {
62 unsigned int i;
63 while (optlist[i].name) {
64 printf("option '%s' = '%s'\n", optlist[i].name, optlist[i].value);
65 ++i;
66 }
67 pm_optDestroyNameValueList(optlist);
68 } else
69 printf("No -options\n");
70 }
71
72 Now run this program with something like
73
74 myprog -vg --name=Bryan --hei 4 "My first argument" --verbose
75
76 or do it with opt.short_allowed=0 and
77
78 myprog -v /etc/passwd -name=Bryan --hei=4
79
80
81 If your program takes no options (so you have no OPTENT3 macro invocations),
82 you need an OPTENTINIT call to establish the empty option table:
83
84 optEntry * option_def;
85 unsigned int option_def_index;
86 MALLOCARRAY(option_def, 1);
87 OPTENTINIT;
88
89 ========================================================================
90 #endif /* 0 */
91
92 #ifndef SHHOPT_H
93 #define SHHOPT_H
94
95 #ifdef __cplusplus
96 extern "C" {
97 #endif
98 #if 0
99 } /* to fake out automatic code indenters */
100 #endif
101
102 /* constants for recognized option types. */
103 typedef enum {
104 OPT_END, /* nothing. used as ending element. */
105 OPT_FLAG, /* no argument following. sets variable to 1. */
106 OPT_STRING, /* string argument. */
107 OPT_INT, /* signed integer argument. */
108 OPT_UINT, /* unsigned integer argument. */
109 OPT_LONG, /* signed long integer argument. */
110 OPT_ULONG, /* unsigned long integer argument. */
111 OPT_FLOAT, /* floating point argument. */
112 OPT_STRINGLIST, /* list like "arg1,arg2.arg3" */
113 OPT_NAMELIST /* list like "key1=val1,key2=val2" */
114 } optArgType;
115
116
117 typedef struct {
118 /* This structure describes a single program option in a form for
119 use by the pm_optParseOptions() or pm_optParseOptions2() function.
120 */
121 char shortName; /* short option name. */
122 const char *longName; /* long option name, not including '--'. */
123 optArgType type; /* option type. */
124 void *arg; /* pointer to variable to fill with argument,
125 * or pointer to function if type == OPT_FUNC. */
126 int flags; /* modifier flags. */
127 } optStruct;
128
129 typedef struct {
130 /* This structure describes a single program option in a form for
131 use by the pm_optParseOptions3() function.
132 */
133 char shortName; /* short option name. */
134 const char *longName; /* long option name, not including '--' or '-' */
135 optArgType type; /* option type. */
136 void *arg;
137 /* pointer to variable in which to return option's argument (or TRUE
138 if it's a flag option), or pointer to function if
139 type == OPT_FUNC. If the option is specified multiple times, only
140 the rightmost one affects this return value.
141 */
142 unsigned int *specified;
143 /* pointer to variable in which to return 1 if the option was
144 specified and 0 if it was not. If NULL, don't return anything.
145 */
146 int flags; /* modifier flags. */
147 } optEntry;
148
149
150 typedef struct {
151 /* This structure describes the options of a program in a form for
152 use with the pm_optParseOptions2() function.
153 */
154 unsigned char short_allowed; /* boolean */
155 /* The syntax may include short (i.e. one-character) options.
156 These options may be stacked within a single token (e.g.
157 -abc = -a -b -c). If this value is not true, the short option
158 member of the option table entry is meaningless and long
159 options may have either one or two dashes.
160 */
161 unsigned char allowNegNum; /* boolean */
162 /* Anything that starts with - and then a digit is a numeric
163 parameter, not an option
164 */
165 optStruct *opt_table;
166 } optStruct2;
167
168 typedef struct {
169 /* Same as optStruct2, but for pm_optParseOptions3() */
170 unsigned char short_allowed;
171 unsigned char allowNegNum;
172 optEntry *opt_table;
173 } optStruct3;
174
175
176 /* You can use OPTENTRY to assign a value to a dynamically or automatically
177 allocated optStruct structure with minimal typing and easy readability.
178
179 Here is an example:
180
181 unsigned int option_def_index = 0;
182 optStruct *option_def = malloc(100*sizeof(optStruct));
183 OPTENTRY('h', "verbose", OPT_FLAG, &verbose_flag, 0);
184 OPTENTRY(0, "alphaout", OPT_STRING, &alpha_filename, 0);
185 */
186
187 /* If you name your variables option_def and option_def_index like in the
188 example above, everything's easy. If you want to use OPTENTRY with other
189 variables, define macros OPTION_DEF and OPTION_DEF_INDEX before calling
190 OPTENTRY.
191 */
192
193 #ifndef OPTION_DEF
194 #define OPTION_DEF option_def
195 #endif
196 #ifndef OPTION_DEF_INDEX
197 #define OPTION_DEF_INDEX option_def_index
198 #endif
199
200 #define OPTENTRY(shortvalue,longvalue,typevalue,outputvalue,flagvalue) {\
201 OPTION_DEF[OPTION_DEF_INDEX].shortName = (shortvalue); \
202 OPTION_DEF[OPTION_DEF_INDEX].longName = (longvalue); \
203 OPTION_DEF[OPTION_DEF_INDEX].type = (typevalue); \
204 OPTION_DEF[OPTION_DEF_INDEX].arg = (outputvalue); \
205 OPTION_DEF[OPTION_DEF_INDEX].flags = (flagvalue); \
206 OPTION_DEF[++OPTION_DEF_INDEX].type = OPT_END; \
207 }
208
209 /* OPTENT3 is the same as OPTENTRY except that it also sets the "specified"
210 element of the table entry (so it assumes OPTION_DEF is a table of optEntry
211 instead of optStruct). This is a pointer to a variable that the parser
212 will set to an indication of whether the option appears in the command
213 line. 1 for yes; 0 for no.
214
215 HISTORICAL NOTE: Until 2019, this was the number of times the option was
216 specified, but much Netpbm code assumed it was never more than 1, and no
217 Netpbm code has ever given semantics to specifying the same option class
218 multiple times.
219
220 Here is an example:
221
222 unsigned int option_def_index = 0;
223 unsigned int verbose_flag;
224 const char * alpha_filename
225 unsigned int alpha_spec;
226 MALLOCARRAY_NOFAIL(option_def, 100);
227 OPTENT3('h', "verbose", OPT_FLAG, &verbose_flag, NULL, 0);
228 OPTENT3(0, "alphaout", OPT_STRING, &alpha_filename, &alpha_spec, 0);
229 */
230
231 #define OPTENT3(shortvalue,longvalue,typevalue,outputvalue,specifiedvalue, \
232 flagvalue) {\
233 OPTION_DEF[OPTION_DEF_INDEX].specified = (specifiedvalue); \
234 OPTENTRY(shortvalue, longvalue, typevalue, outputvalue, flagvalue) \
235 }
236
237 #define OPTENTINIT \
238 do {OPTION_DEF_INDEX=0; \
239 OPTION_DEF[OPTION_DEF_INDEX].type = OPT_END; \
240 } while (0)
241
242
243 struct optNameValue {
244 const char * name;
245 const char * value;
246 };
247
248
249
250 void
251 pm_optSetFatalFunc(void (*f)(const char *, ...));
252
253 void
254 pm_optParseOptions(int *argc, char *argv[],
255 optStruct opt[], int allowNegNum);
256 void
257 pm_optParseOptions2(int * const argc_p, char *argv[], const optStruct2 opt,
258 const unsigned long flags);
259 void
260 pm_optParseOptions3(int * const argc_p, char *argv[], const optStruct3 opt,
261 const unsigned int optStructSize, const unsigned long flags);
262
263 void
264 pm_optParseOptions4(int * const argcP,
265 const char ** const argv,
266 optStruct3 const opt,
267 unsigned int const optStructSize,
268 unsigned long const flags);
269
270 void
271 pm_optDestroyNameValueList(struct optNameValue * const list);
272
273 #ifdef __cplusplus
274 }
275 #endif
276
277 #endif