-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathCmdParams.c
More file actions
413 lines (353 loc) · 14.1 KB
/
CmdParams.c
File metadata and controls
413 lines (353 loc) · 14.1 KB
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
/* CmdParams.c */
#include "CmdParams.h"
CmdParams* CmdParams_ctor(int printVals)
/*------------------------------------------------------------*//*!
Allocate space for a CmdParams object.
\author Michelle Strout 9/28/02
*//*--------------------------------------------------------------*/
{
CmdParams* thee = (CmdParams*)malloc(sizeof(CmdParams));
thee->numParams = 0;
thee->printVals = printVals;
return thee;
}
void CmdParams_dtor(CmdParams **cmdparamPtr)
/*------------------------------------------------------------*//*!
Deallocate space for a CmdParams object.
\author Michelle Strout 9/28/02
*//*--------------------------------------------------------------*/
{
if (*cmdparamPtr != NULL) {
free(*cmdparamPtr);
*cmdparamPtr = NULL;
}
}
void CmdParams_describeEnumParam(CmdParams *thee, const char *paramName,
char flag, int optional,
const char *helpstr,
EnumStringPair possibleStrings[], int numPairs,
int defaultVal)
/*------------------------------------------------------------*//*!
Describe a number command line parameter. Will be of the
form "-f string".
Enum parameters have an enumerated type which is associated
with strings.
\param paramName name of parameter if want to print it out
\param flag The flag character
\param optional Is this command line parameter optional
\param helpstr String describing the command line parameter
\param possibleStrings Array of {enumval,string} pairs.
\param numPairs number of pairs in possibleStrings array
\param defaultVal Value of enumerated type which is default
\author Michelle Strout 9/28/02
*//*--------------------------------------------------------------*/
{
int i;
struct cmd_param * cps;
assert(thee->numParams < MAXCMDPARAMS);
/* get array of command parameter descriptions */
cps = thee->cmd_params;
/* Put this command line param next in the array */
strcpy(cps[thee->numParams].paramName, paramName);
cps[thee->numParams].paramType = paramType_ENUM;
cps[thee->numParams].flagChar = flag;
cps[thee->numParams].optional = optional;
cps[thee->numParams].actualVal = defaultVal;
cps[thee->numParams].numPairs = numPairs;
strcpy(cps[thee->numParams].helpstr, helpstr);
for (i=0; i<numPairs; i++) {
strcpy(cps[thee->numParams].possibleStrings[i].string,
possibleStrings[i].string);
cps[thee->numParams].possibleStrings[i].enumVal
= possibleStrings[i].enumVal;
}
thee->numParams++;
}
void CmdParams_describeStringParam(CmdParams *thee, const char *paramName,
char flag, int optional,
const char *helpstr, const char *defaultString)
/*------------------------------------------------------------*//*!
Describe a number command line parameter. Will be of the
form "-f string".
\param paramName name of parameter if want to print it out
\param flag The flag character
\param optional Is this command line parameter optional
\param helpstr String describing the command line parameter
\param defaultString The default string for the parameter
\author Michelle Strout 9/28/02
*//*--------------------------------------------------------------*/
{
struct cmd_param * cps;
assert(thee->numParams < MAXCMDPARAMS);
/* get array of command parameter descriptions */
cps = thee->cmd_params;
/* Put this command line param next in the array */
strcpy(cps[thee->numParams].paramName, paramName);
cps[thee->numParams].paramType = paramType_STRING;
cps[thee->numParams].flagChar = flag;
cps[thee->numParams].optional = optional;
strcpy(cps[thee->numParams].helpstr, helpstr);
strcpy(cps[thee->numParams].stringVal, defaultString);
thee->numParams++;
}
void CmdParams_describeNumParam(CmdParams *thee, const char *paramName,
char flag, int optional,
const char *helpstr,
int startRange, int endRange, int defaultVal)
/*------------------------------------------------------------*//*!
Describe a number command line parameter. Will be of the
form "-f num".
\param paramName name of parameter if want to print it out
\param flag The flag character
\param optional Is this command line parameter optional
\param helpstr String describing the command line parameter
\param startRange The value of the param can be in the range
[startRange to endRange]
\param endRange
\param defaultVal The default value for the parameter
\author Michelle Strout 9/28/02
*//*--------------------------------------------------------------*/
{
struct cmd_param * cps;
assert(thee->numParams < MAXCMDPARAMS);
/* get array of command parameter descriptions */
cps = thee->cmd_params;
/* Put this command line param next in the array */
strcpy(cps[thee->numParams].paramName, paramName);
cps[thee->numParams].paramType = paramType_INT;
cps[thee->numParams].flagChar = flag;
cps[thee->numParams].optional = optional;
strcpy(cps[thee->numParams].helpstr, helpstr);
cps[thee->numParams].startRange = startRange;
cps[thee->numParams].endRange = endRange;
cps[thee->numParams].actualVal = defaultVal;
thee->numParams++;
}
void CmdParams_setValue(CmdParams* thee, char whichFlag, int val)
/*------------------------------------------------------------*//*!
Set the value for the given flag.
\author Michelle Strout 10/1/02
*//*--------------------------------------------------------------*/
{
int i, j;
struct cmd_param *cps;
/* get pointer to the array of cmd_params */
cps = thee->cmd_params;
/* create a string with all of the command line flags */
for (i=0; i<thee->numParams; i++) {
if (cps[i].flagChar == whichFlag) {
cps[i].actualVal = val;
break;
}
}
}
int CmdParams_getValue(CmdParams* thee, char whichFlag)
/*------------------------------------------------------------*//*!
Return the value for the given flag.
\author Michelle Strout 9/28/02
*//*--------------------------------------------------------------*/
{
int i, j, retval=0;
struct cmd_param *cps;
/* get pointer to the array of cmd_params */
cps = thee->cmd_params;
/* create a string with all of the command line flags */
for (i=0; i<thee->numParams; i++) {
if (cps[i].flagChar == whichFlag) {
retval = cps[i].actualVal;
break;
}
}
return retval;
}
char* CmdParams_getString(CmdParams* thee, char whichFlag)
/*------------------------------------------------------------*//*!
Return the string value for the given flag.
\author Michelle Strout 9/28/02
*//*--------------------------------------------------------------*/
{
int i, j;
char * retval = NULL;
struct cmd_param *cps;
/* get pointer to the array of cmd_params */
cps = thee->cmd_params;
/* create a string with all of the command line flags */
for (i=0; i<thee->numParams; i++) {
if (cps[i].flagChar == whichFlag) {
switch (cps[i].paramType) {
case paramType_STRING:
retval = cps[i].stringVal;
break;
case paramType_ENUM:
for (j=0; j<cps[i].numPairs; j++) {
if (cps[i].possibleStrings[j].enumVal
== cps[i].actualVal)
{
retval = cps[i].possibleStrings[j].string;
}
}
break;
case paramType_INT:
assert(0);
break;
}
break;
}
}
assert(retval!=NULL);
return retval;
}
void CmdParams_printHelp(CmdParams *thee)
/*------------------------------------------------------------*//*!
Print a help message for all command line parameters.
\author Michelle Strout 9/28/02
*//*--------------------------------------------------------------*/
{
int i, j, defaultEnumIndex;
struct cmd_param *cps;
/* get pointer to the array of cmd_params */
cps = thee->cmd_params;
printf("\nCommand line parameter help\n");
/* create a string with all of the command line flags */
for (i=0; i<thee->numParams; i++) {
printf("\n\t-%c\t%s\n",
cps[i].flagChar, cps[i].helpstr );
switch(cps[i].paramType) {
case paramType_INT:
printf("\t\tDefault: %d, Range: [%d to %d]\n",
cps[i].actualVal, cps[i].startRange, cps[i].endRange);
break;
case paramType_STRING:
printf("\t\tDefault: %s\n", cps[i].stringVal);
break;
case paramType_ENUM:
defaultEnumIndex = -1;
/* find string for enumval and print out possibles */
printf("\t\tPossible values: ");
for (j=0; j<cps[i].numPairs; j++) {
if (cps[i].possibleStrings[j].enumVal
== cps[i].actualVal)
{
defaultEnumIndex = j;
}
printf("%s ", cps[i].possibleStrings[j].string);
}
assert(defaultEnumIndex >= 0);
printf("\n\t\tDefault: %s\n",
cps[i].possibleStrings[defaultEnumIndex].string);
break;
} /* end switch */
}
}
void CmdParams_parseParams(CmdParams *thee, int argc, char **argv)
/*------------------------------------------------------------*//*!
Parse argc and argv for command line parameters.
Expects -f val for each possible command line parameter.
The one exception is -h or --help, if either of those are at
the beginning then help will be printed and the program
will be exited.
For other parameters, if thee->printVals is true then
the value of the parameter will be printed to stdout as it is parsed.
\param argc number of command line params
\param argv array of string tokens from command line
\author Michelle Strout 9/28/02
*//*--------------------------------------------------------------*/
{
int i,c,j;
char flagstr[MAXCMDPARAMS*2+1];
char tempstr[2+1];
struct cmd_param *cps;
int flagFound, foundEnum;
/* first check if this is a request for help */
if (argc > 1) {
if (strcmp(argv[1],"--help")==0 ||
strcmp(argv[1],"-h")==0 )
{
CmdParams_printHelp(thee);
exit(1);
}
}
/* empty this string */
flagstr[0] = '\0';
/* get pointer to the array of cmd_params */
cps = thee->cmd_params;
/*
* optind is an external variable that getopt uses to remember
* it's place from previous call. resetting to 1 allows a new
* pp_argc and pp_argv to be parsed of each call to this routine
*/
optind=1;
/*oldoptind = 0;*/ /* needed for blue */
/* create a string with all of the command line flags */
for (i=0; i<thee->numParams; i++) {
sprintf(tempstr, "%c:", cps[i].flagChar );
strcat(flagstr, tempstr);
}
/* parse command line parameters, returns EOF=-1 when done */
while ( (c=getopt(argc,argv, flagstr)) != -1)
{
#if (defined(MYDEBUG) || defined(MYDEBUG_VERBOSE) )
printf("CmdParams_parseParams: c=%c, optarg=%s\n", c, optarg);
printf(" and: pp_argc= %d\n", argc);
printf(" and: optind=%d\n",optind);
#endif
/*if (optind == oldoptind) { break; } */ /* needed for blue */
/*oldoptind = optind; */ /* needed for blue */
flagFound = 0;
for (i=0; i<thee->numParams; i++) {
if (c == cps[i].flagChar ) {
flagFound = 1;
/* process the param based on its type */
switch(cps[i].paramType) {
case paramType_STRING:
strcpy(cps[i].stringVal, optarg);
if (thee->printVals) {
printf("CmdParams_parseParams: %s = %s\n",
cps[i].paramName, cps[i].stringVal);
}
break;
case paramType_INT:
cps[i].actualVal = atoi(optarg);
if (thee->printVals) {
printf("CmdParams_parseParams: %s = %d\n",
cps[i].paramName, cps[i].actualVal);
}
if ((cps[i].actualVal < cps[i].startRange) ||
(cps[i].actualVal > cps[i].endRange) )
{
fprintf(stderr,"Error: -%c %d is out of range\n",
c, cps[i].actualVal );
exit(1);
}
break;
case paramType_ENUM:
foundEnum = 0;
for (j = 0; j<cps[i].numPairs; j++) {
if (strcmp(optarg,
cps[i].possibleStrings[j].string)==0)
{
foundEnum = 1;
cps[i].actualVal
= cps[i].possibleStrings[j].enumVal;
}
}
if (thee->printVals) {
printf("CmdParams_parseParams: %s = %s\n",
cps[i].paramName, optarg);
}
if (!foundEnum) {
fprintf(stderr, "Error: "
"-%c %s is not in list of possible strings\n",
c, optarg );
exit(1);
}
break;
} /* switch */
} /* if found flag */
} /* loop over cmd_params to find flag */
if (!flagFound) {
fprintf(stderr, "Error: -%c is not a known flag\n", c);
exit(1);
}
}
}