File: command-t.c

package info (click to toggle)
openafs 1.8.14-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 42,972 kB
  • sloc: ansic: 455,934; xml: 66,858; perl: 11,967; makefile: 10,038; sh: 7,955; objc: 6,354; java: 5,638; cpp: 2,268; asm: 1,214; yacc: 441; tcl: 249; lex: 201; csh: 85
file content (396 lines) | stat: -rw-r--r-- 14,867 bytes parent folder | download | duplicates (3)
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
/*
 * Copyright (c) 2010 Your File System Inc. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR `AS IS'' AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

/*
 * Test the command line parsing library
 */

#include <afsconfig.h>
#include <afs/param.h>

#include <roken.h>

#include <afs/cmd.h>

#include <tests/tap/basic.h>

enum cmdOptions {
   copt_flag = 0,
   copt_first,
   copt_second,
   copt_sugar,
   copt_fourth,
   copt_fifth,
   copt_perhaps,
   copt_sanity
};

static int
testproc(struct cmd_syndesc *as, void *rock)
{
    is_string("foo", as->parms[copt_first].items->data,
	      "first option matches");
    is_string("bar", as->parms[copt_second].items->data,
	      "second option matches");
    ok(as->parms[copt_flag].items != NULL,
       "flag is set");

    return 0;
}

static void
checkList(struct cmd_item *list, ...)
{
    va_list ap;
    char *el;

    va_start(ap, list);
    el = va_arg(ap, char *);
    while (el != NULL && list != NULL) {
       is_string(el, list->data, "list element matches");
       list = list->next;
       el = va_arg(ap, char *);
    }

    if (el == NULL && list == NULL) {
	ok(1, "List has correct number of elements");
    } else if (el == NULL) {
	ok(0, "List has too many elements");
    } else if (list == NULL) {
	ok(0, "List has too few elements");
    }
}

int
main(int argc, char **argv)
{
    char *tv[100];
    struct cmd_syndesc *opts;
    struct cmd_syndesc *retopts;
    struct cmd_item *list;
    int code;
    int tc;
    int retval;
    char *path;
    char *retstring = NULL;

    plan(109);

    initialize_CMD_error_table();

    opts = cmd_CreateSyntax(NULL, testproc, NULL, 0, NULL);
    cmd_AddParm(opts, "-flag", CMD_FLAG, CMD_OPTIONAL, "a flag");
    cmd_AddParm(opts, "-first", CMD_SINGLE, CMD_REQUIRED, "first option");
    cmd_AddParm(opts, "-second", CMD_LIST, CMD_OPTIONAL, "second option");

    /* A simple command line */
    code = cmd_ParseLine("-first foo -second bar -flag", tv, &tc, 100);
    is_int(0, code, "cmd_ParseLine succeeds");
    code = cmd_Dispatch(tc, tv);
    is_int(0, code, "dispatching simple comamnd line succeeds");
    code = cmd_Parse(tc, tv, &retopts);
    is_int(0, code, "parsing simple command line succeeds");
    is_string("foo", retopts->parms[copt_first].items->data,
	      " ... 1st option matches");
    is_string("bar", retopts->parms[copt_second].items->data,
	      " ... 2nd option matches");
    ok(retopts->parms[copt_flag].items != NULL, " ... 3rd option matches");
    cmd_FreeOptions(&retopts);
    cmd_FreeArgv(tv);

    /* unknown switch */
    code = cmd_ParseLine("-first foo -second bar -third -flag", tv, &tc, 100);
    is_int(0, code, "cmd_ParseLine succeeds");
    code = cmd_Dispatch(tc, tv);
    is_int(CMD_UNKNOWNSWITCH, code, "invalid options fail as expected");
    code = cmd_Parse(tc, tv, &retopts);
    is_int(CMD_UNKNOWNSWITCH, code, "and still fail with cmd_Parse");
    cmd_FreeArgv(tv);

    /* missing parameter */
    code = cmd_ParseLine("-first foo -second -flag", tv, &tc, 100);
    is_int(0, code, "cmd_ParseLine succeeds");
    code = cmd_Dispatch(tc, tv);
    is_int(CMD_TOOFEW, code, "missing parameters fail as expected");
    code = cmd_Parse(tc, tv, &retopts);
    is_int(CMD_TOOFEW, code, "and still fail with cmd_Parse");
    cmd_FreeArgv(tv);

    /* missing option */
    code = cmd_ParseLine("-second bar -third -flag", tv, &tc, 100);
    is_int(0, code, "cmd_ParseLine succeeds");
    code = cmd_Dispatch(tc, tv);
    is_int(CMD_UNKNOWNSWITCH, code, "missing options fail as expected");
    code = cmd_Parse(tc, tv, &retopts);
    is_int(CMD_UNKNOWNSWITCH, code, "and still fail with cmd_Parse");
    cmd_FreeArgv(tv);

    code = cmd_ParseLine("-first foo baz -second bar -third -flag", tv, &tc, 100);
    is_int(0, code, "cmd_ParseLine succeeds");
    code = cmd_Dispatch(tc, tv);
    is_int(CMD_NOTLIST, code, "too many parameters fails as expected");
    code = cmd_Parse(tc, tv, &retopts);
    is_int(CMD_NOTLIST, code, "and still fail with cmd_Parse");
    cmd_FreeArgv(tv);

    /* Positional parameters */
    code = cmd_ParseLine("foo bar -flag", tv, &tc, 100);
    is_int(0, code, "cmd_ParseLine succeeds");
    code = cmd_Dispatch(tc, tv);
    is_int(0, code, "dispatching positional parameters succeeds");
    code = cmd_Parse(tc, tv, &retopts);
    is_int(0, code, "and works with cmd_Parse");
    cmd_FreeOptions(&retopts);
    cmd_FreeArgv(tv);

    /* Abbreviations */
    code = cmd_ParseLine("-fi foo -s bar -flag", tv, &tc, 100);
    is_int(0, code, "cmd_ParseLine succeeds");
    code = cmd_Dispatch(tc, tv);
    is_int(0, code, "dispatching abbreviations succeeds");
    code = cmd_Parse(tc, tv, &retopts);
    is_int(0, code, "and works with cmd_Parse");
    cmd_FreeOptions(&retopts);

    cmd_FreeArgv(tv);

    /* Ambiguous */
    code = cmd_ParseLine("-f foo -s bar -flag", tv, &tc, 100);
    is_int(0, code, "cmd_ParseLine succeeds");
    code = cmd_Dispatch(tc, tv);
    is_int(CMD_UNKNOWNSWITCH, code, "ambiguous abbreviations correctly fail");
    code = cmd_Parse(tc, tv, &retopts);
    is_int(CMD_UNKNOWNSWITCH, code, "and fail with cmd_Parse too");
    cmd_FreeArgv(tv);

    /* Check that paramaters with abbreviation disabled don't make things
     * ambiguous */
    cmd_AddParmAtOffset(opts, copt_sugar, "-sugar", CMD_SINGLE,
		        CMD_OPTIONAL | CMD_NOABBRV, "sugar with that");
    code = cmd_ParseLine("-fi foo -s bar -flag", tv, &tc, 100);
    is_int(0, code, "cmd_ParseLine succeeds");
    code = cmd_Dispatch(tc, tv);
    is_int(0, code, "disabling specific abbreviations succeeds");
    code = cmd_Parse(tc, tv, &retopts);
    is_int(0, code, "and works with cmd_Parse into the bargain");
    cmd_FreeOptions(&retopts);
    cmd_FreeArgv(tv);

    /* Disable positional commands */
    cmd_DisablePositionalCommands();
    code = cmd_ParseLine("foo bar -flag", tv, &tc, 100);
    is_int(0, code, "cmd_ParseLine succeeds");
    code = cmd_Dispatch(tc, tv);
    is_int(CMD_NOTLIST, code, "positional parameters can be disabled");
    code = cmd_Parse(tc, tv, &retopts);
    is_int(CMD_NOTLIST, code, "and fail with cmd_Parse too");
    cmd_FreeArgv(tv);

    /* Disable abbreviations */
    cmd_DisableAbbreviations();
    code = cmd_ParseLine("-fi foo -s bar -flag", tv, &tc, 100);
    is_int(0, code, "cmd_ParseLine succeeds");
    code = cmd_Dispatch(tc, tv);
    is_int(CMD_UNKNOWNSWITCH, code, "dispatching abbreviations succeeds");
    code = cmd_Parse(tc, tv, &retopts);
    is_int(CMD_UNKNOWNSWITCH, code, "and fail with cmd_Parse too");

    cmd_FreeArgv(tv);

    /* Try the new cmd_Parse function with something different*/
    code = cmd_ParseLine("-first one -second two -flag", tv, &tc, 100);
    is_int(0, code, "cmd_ParseLine succeeds");
    code = cmd_Parse(tc, tv, &retopts);
    is_int(0, code, "Parsing with cmd_Parse works");
    is_string("one", retopts->parms[copt_first].items->data,
	      " ... 1st option matches");
    is_string("two", retopts->parms[copt_second].items->data,
	      " ... 2nd option matches");
    ok(retopts->parms[copt_flag].items != NULL,
	      " ... 3rd option matches");

    cmd_FreeOptions(&retopts);
    cmd_FreeArgv(tv);
    /* Try adding a couple of parameters at specific positions */
    cmd_AddParmAtOffset(opts, copt_fifth, "-fifth", CMD_SINGLE, CMD_OPTIONAL,
		       "fifth option");
    cmd_AddParmAtOffset(opts, copt_fourth, "-fourth", CMD_SINGLE, CMD_OPTIONAL,
		       "fourth option" );
    code = cmd_ParseLine("-first a -fourth b -fifth c", tv, &tc, 100);
    is_int(0, code, "cmd_ParseLine succeeds");
    code = cmd_Parse(tc, tv, &retopts);
    is_int(0, code, "parsing our new options succeeds");
    is_string("b", retopts->parms[copt_fourth].items->data,
	      " Fourth option in right place");
    is_string("c", retopts->parms[copt_fifth].items->data,
	      " Fifth option in right place");
    cmd_FreeOptions(&retopts);
    cmd_FreeArgv(tv);

    /* Check Accessors */
    code = cmd_ParseLine("-first 1 -second second -flag", tv, &tc, 100);
    is_int(0, code, "cmd_ParseLine succeeds");
    code = cmd_Parse(tc, tv, &retopts);

    code = cmd_OptionAsInt(retopts, copt_first, &retval);
    is_int(0, code, "cmd_OptionsAsInt succeeds");
    is_int(1, retval, " ... and returns correct value");
    ok(cmd_OptionPresent(retopts, copt_first),
       " ... and is marked as present");

    code = cmd_OptionAsString(retopts, copt_second, &retstring);
    is_int(0, code, "cmd_OptionsAsString succeeds");
    is_string("second", retstring, " ... and returns correct value");
    free(retstring);
    retstring = NULL;
    ok(cmd_OptionPresent(retopts, copt_second),
       " ... and is marked as present");

    code = cmd_OptionAsFlag(retopts, copt_flag, &retval);
    is_int(0, code, "cmd_OptionsAsFlag succeeds");
    ok(retval, " ... and flag is correct");
    ok(cmd_OptionPresent(retopts, copt_flag),
       " ... and is marked as present");

    ok(!cmd_OptionPresent(retopts, copt_sugar),
       "Missing option is marked as such");

    cmd_FreeOptions(&retopts);
    cmd_FreeArgv(tv);

    /* Add an alias */
    code = cmd_AddParmAlias(opts, copt_second, "-twa");
    is_int(0, code, "cmd_AddParmAlias succeeds");

    code = cmd_ParseLine("-first 1 -twa tup", tv, &tc, 100);
    is_int(0, code, "cmd_ParseLine succeeds");
    code = cmd_Parse(tc, tv, &retopts);
    is_int(0, code, "cmd_Parse succeeds for alias");
    cmd_OptionAsString(retopts, copt_second, &retstring);
    is_string("tup", retstring, " ... and we have the correct value");
    free(retstring);
    retstring = NULL;

    cmd_FreeOptions(&retopts);
    cmd_FreeArgv(tv);

    /* Add something that can be a flag or a value, and put something after
     * it so we can check for parse problems*/
    cmd_AddParm(opts, "-perhaps", CMD_SINGLE_OR_FLAG, CMD_OPTIONAL,
	        "what am I");
    cmd_AddParm(opts, "-sanity", CMD_SINGLE, CMD_OPTIONAL, "sanity check");

    /* Try using as an option */

    code = cmd_ParseLine("-first 1 -perhaps 2 -sanity 3", tv, &tc, 100);
    is_int(0, code, "cmd_ParseLine succeeds");
    code = cmd_Parse(tc, tv, &retopts);
    is_int(0, code, "cmd_Parse succeeds for option-as-flag as opt");
    code = cmd_OptionAsInt(retopts, copt_perhaps, &retval);
    is_int(0, code, "cmd_OptionAsInt succeeds");
    is_int(2, retval, " ... and we have the correct value");
    cmd_FreeOptions(&retopts);
    cmd_FreeArgv(tv);

    /* And now, as a flag */

    code = cmd_ParseLine("-first 1 -perhaps -sanity 3", tv, &tc, 100);
    is_int(0, code, "cmd_ParseLine succeeds");
    code = cmd_Parse(tc, tv, &retopts);
    is_int(0, code, "cmd_Parse succeeds for option-as-flag as flag");
    code = cmd_OptionAsInt(retopts, copt_perhaps, &retval);
    is_int(CMD_MISSING, code, " ... pulling out a value fails as expected");
    cmd_OptionAsFlag(retopts, copt_perhaps, &retval);
    ok(retval, " ... but parsing as a flag works");
    cmd_FreeOptions(&retopts);
    cmd_FreeArgv(tv);

    /* Check that we can produce help output */
    code = cmd_ParseLine("-help", tv, &tc, 100);
    is_int(0, code, "cmd_ParseLine succeeds");
    code = cmd_Parse(tc, tv, &retopts);
    is_int(CMD_HELP, code, "cmd_Parse returns help indicator with help output");
    ok(retopts == NULL, " ... and options is empty");

    /* Check splitting with '=' */

    code = cmd_ParseLine("-first 1 -perhaps=6 -sanity=3", tv, &tc, 100);
    is_int(0, code, "cmd_ParseLine succeeds");
    code = cmd_Parse(tc, tv, &retopts);
    is_int(0, code, "cmd_Parse succeeds for items split with '='");
    code = cmd_OptionAsInt(retopts, copt_perhaps, &retval);
    is_int(0, code, "cmd_OptionAsInt succeeds");
    is_int(6, retval, " ... and we have the correct value once");
    code = cmd_OptionAsInt(retopts, copt_sanity, &retval);
    is_int(0, code, "cmd_OptionAsInt succeeds");
    is_int(3, retval, " ... and we have the correct value twice");
    cmd_FreeOptions(&retopts);
    cmd_FreeArgv(tv);

    /* Check list behaviour */
    code = cmd_ParseLine("-first 1 -second one two three", tv, &tc, 100);
    is_int(0, code, "cmd_ParseLine succeeds");
    code = cmd_Parse(tc, tv, &retopts);
    is_int(0, code, "cmd_Parse succeeds for a list");
    code = cmd_OptionAsList(retopts, copt_second, &list);
    is_int(0, code, "cmd_OptionAsList succeeds");
    checkList(list, "one", "two", "three", NULL);
    cmd_FreeOptions(&retopts);
    cmd_FreeArgv(tv);

    /* Now, try adding a configuration file into the mix */
    if (getenv("C_TAP_SOURCE") == NULL)
	path = strdup("test1.conf");
    else {
	if (asprintf(&path, "%s/cmd/test1.conf", getenv("C_TAP_SOURCE")) < 0)
	    path = NULL;
    }
    if (path != NULL) {
	cmd_SetCommandName("test");
	code = cmd_OpenConfigFile(path);
	is_int(0, code, "cmd_OpenConfigFile succeeds");
    } else {
	skip("no memory to build config file path");
    }

    code = cmd_ParseLine("-first 1", tv, &tc, 100);
    is_int(0, code, "cmd_ParseLine succeeds");
    code = cmd_Parse(tc, tv, &retopts);
    is_int(0, code, "cmd_Parse succeeds when we have a config file");
    code = cmd_OptionAsInt(retopts, copt_perhaps, &retval);
    is_int(0, code, "cmd_OptionsAsInt succeeds");
    is_int(10, retval, " ... and we have the correct value for perhaps");
    code = cmd_OptionAsString(retopts, copt_sanity, &retstring);
    is_int(0, code, "cmd_OptionAsString succeeds");
    is_string("testing", retstring,
	      " ... and we have the correct value for sanity");

    /* Check breaking up a list of options */
    code = cmd_OptionAsList(retopts, copt_second, &list);
    is_int(0, code, "cmd_OptionAsList succeeds");
    checkList(list, "one", "two", "three", "four", NULL);

    return 0;
}