summaryrefslogtreecommitdiff
path: root/test1.c
blob: e472cfeb3c3f6d66f1e96e923cd0a19fc0f12fcf (plain)
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
/* (C) 1998-2002 Red Hat, Inc. -- Licensing details are in the COPYING
   file accompanying popt source distributions, available from
   ftp://ftp.rpm.org/pub/rpm/dist. */

#include "system.h"

/*@unchecked@*/
static int pass2 = 0;
static void option_callback(/*@unused@*/ UNUSED(poptContext con),
		/*@unused@*/ UNUSED(enum poptCallbackReason reason),
		const struct poptOption * opt,
		char * arg, void * data)
	/*@globals fileSystem @*/
	/*@modifies fileSystem @*/
{
    if (pass2)
	fprintf(stdout, "callback: %c %s %s ", opt->val, (char *) data, arg);
}

/*@unchecked@*/
static int arg1 = 0;
/*@unchecked@*/ /*@observer@*/
static char * arg2 = "(none)";
/*@unchecked@*/
static int arg3 = 0;
/*@unchecked@*/
static int inc = 0;
/*@unchecked@*/
static int shortopt = 0;

/*@unchecked@*/
static int aVal = 141421;
/*@unchecked@*/
static int bVal = 141421;
/*@unchecked@*/
static unsigned int aFlag = 0x8aceU;
/*@unchecked@*/
static unsigned int bFlag = 0x8aceU;

/*@unchecked@*/
static short aShort = (short)4523;
/*@unchecked@*/
static short bShort = (short)4523;
/*@unchecked@*/
static int aInt = 271828;
/*@unchecked@*/
static int bInt = 271828;
/*@unchecked@*/
static long aLong = 738905609L;
/*@unchecked@*/
static long bLong = 738905609L;
/*@unchecked@*/
static long long aLongLong = 738905609LL;
/*@unchecked@*/
static long long bLongLong = 738905609LL;
/*@unchecked@*/
static float aFloat = 3.1415926535;
/*@unchecked@*/
static float bFloat = 3.1415926535;
/*@unchecked@*/
static double aDouble = 9.86960440108935861883;
/*@unchecked@*/
static double bDouble = 9.86960440108935861883;

/*@unchecked@*/
static int aCounter = 34543;
/*@unchecked@*/
static int bCounter = 34543;

/*@unchecked@*/ /*@only@*/ /*@null@*/
static const char ** aArgv = NULL;
/*@unchecked@*/ /*@only@*/ /*@null@*/
static void * aBits = NULL;
/*@unchecked@*/ /*@observer@*/
static const char *attributes[] = {
    "foo", "bar", "baz", "bing", "bang", "boom"
};
/*@unchecked@*/
static size_t nattributes = (sizeof(attributes) / sizeof(attributes[0]));

/*@unchecked@*/ /*@null@*/
static char * oStr = (char *) -1;
/*@unchecked@*/
static int singleDash = 0;

/*@unchecked@*/ /*@observer@*/
static char * lStr =
"This tests default strings and exceeds the ... limit. "
"123456789+123456789+123456789+123456789+123456789+ "
"123456789+123456789+123456789+123456789+123456789+ "
"123456789+123456789+123456789+123456789+123456789+ "
"123456789+123456789+123456789+123456789+123456789+ ";
/*@unchecked@*/ /*@null@*/
static char * nStr = NULL;

/*@unchecked@*/
static struct poptOption moreCallbackArgs[] = {
  { NULL, '\0', POPT_ARG_CALLBACK|POPT_CBFLAG_INC_DATA,
	(void *)option_callback, 0,
	NULL, NULL },
  { "cb2", 'c', POPT_ARG_STRING, NULL, (int)'c',
	"Test argument callbacks", NULL },
  POPT_TABLEEND
};

/*@unchecked@*/
static struct poptOption callbackArgs[] = {
  { NULL, '\0', POPT_ARG_CALLBACK, (void *)option_callback, 0,
	"sampledata", NULL },
  { "cb", 'c', POPT_ARG_STRING, NULL, (int)'c',
	"Test argument callbacks", NULL },
  { "longopt", '\0', 0, NULL, (int)'l',
	"Unused option for help testing", NULL },
  POPT_TABLEEND
};

/*@unchecked@*/
static struct poptOption moreArgs[] = {
  { "inc", 'I', 0, &inc, 0, "An included argument", NULL },
  POPT_TABLEEND
};

/*@unchecked@*/
static struct poptOption options[] = {
  { NULL, '\0', POPT_ARG_INCLUDE_TABLE, &moreCallbackArgs, 0,
	"arg for cb2", NULL },
  { "arg1", '\0', POPT_ARG_NONE, &arg1, 0, "First argument with a really long"
	    " description. After all, we have to test argument help"
	    " wrapping somehow, right?", NULL },
  { "arg2", '2', POPT_ARG_STRING | POPT_ARGFLAG_SHOW_DEFAULT, &arg2, 0,
	"Another argument", "ARG" },
  { "arg3", '3', POPT_ARG_INT, &arg3, 0,
	"A third argument", "ANARG" },
  { "onedash", '\0', POPT_ARGFLAG_ONEDASH, &shortopt, 0,
	"POPT_ARGFLAG_ONEDASH: Option takes a single -", NULL },
  { "hidden", '\0', POPT_ARG_STRING | POPT_ARGFLAG_DOC_HIDDEN, NULL, 0,
	"POPT_ARGFLAG_HIDDEN: A hidden option (--help shouldn't display)",
	NULL },
  { "optional", '\0', POPT_ARG_STRING | POPT_ARGFLAG_OPTIONAL, &oStr, 0,
	"POPT_ARGFLAG_OPTIONAL: Takes an optional string argument", NULL },

  { "val", '\0', POPT_ARG_VAL | POPT_ARGFLAG_SHOW_DEFAULT, &aVal, 125992,
	"POPT_ARG_VAL: 125992 141421", 0},

  { "int", 'i', POPT_ARG_INT | POPT_ARGFLAG_SHOW_DEFAULT, &aInt, 0,
	"POPT_ARG_INT: 271828", NULL },
  { "short", 's', POPT_ARG_SHORT | POPT_ARGFLAG_SHOW_DEFAULT, &aShort, 0,
	"POPT_ARG_SHORT: 4523", NULL },
  { "long", 'l', POPT_ARG_LONG | POPT_ARGFLAG_SHOW_DEFAULT, &aLong, 0,
	"POPT_ARG_LONG: 738905609", NULL },
  { "longlong", 'L', POPT_ARG_LONGLONG | POPT_ARGFLAG_SHOW_DEFAULT, &aLongLong, 0,
	"POPT_ARG_LONGLONG: 738905609", NULL },
  { "float", 'f', POPT_ARG_FLOAT | POPT_ARGFLAG_SHOW_DEFAULT, &aFloat, 0,
	"POPT_ARG_FLOAT: 3.14159", NULL },
  { "double", 'd', POPT_ARG_DOUBLE | POPT_ARGFLAG_SHOW_DEFAULT, &aDouble, 0,
	"POPT_ARG_DOUBLE: 9.8696", NULL },

   { "randint", '\0', POPT_ARG_INT|POPT_ARGFLAG_RANDOM, &aInt, 0,
	"POPT_ARGFLAG_RANDOM: experimental", NULL },
   { "randshort", '\0', POPT_ARG_SHORT|POPT_ARGFLAG_RANDOM, &aShort, 0,
	"POPT_ARGFLAG_RANDOM: experimental", NULL },
   { "randlong", '\0', POPT_ARG_LONG|POPT_ARGFLAG_RANDOM, &aLong, 0,
	"POPT_ARGFLAG_RANDOM: experimental", NULL },
   { "randlonglong", '\0', POPT_ARG_LONGLONG|POPT_ARGFLAG_RANDOM, &aLongLong, 0,
	"POPT_ARGFLAG_RANDOM: experimental", NULL },

   { "argv", '\0', POPT_ARG_ARGV, &aArgv, 0,
	"POPT_ARG_ARGV: append string to argv array (can be used multiple times)","STRING"},
   { "bits", '\0', POPT_ARG_BITSET|POPT_ARGFLAG_DOC_HIDDEN, &aBits, 0,
	"POPT_ARG_BITSET: add string to bit set (can be used multiple times)","STRING"},

   { "verbose", 'v', POPT_ARG_VAL|POPT_ARGFLAG_CALCULATOR|POPT_ARGFLAG_DOC_HIDDEN, &aCounter, 1,
	"POPT_ARGFLAG_CALCULATOR: increment a counter", "+" },
   { "quiet", 'q', POPT_ARG_VAL|POPT_ARGFLAG_NOT|POPT_ARGFLAG_CALCULATOR|POPT_ARGFLAG_DOC_HIDDEN, &aCounter, 1,
	"POPT_ARGFLAG_CALCULATOR: decrement a counter", "-" },
   { "add", '\0', POPT_ARG_VAL|POPT_ARGFLAG_CALCULATOR|POPT_ARGFLAG_DOC_HIDDEN, &aCounter, 24,
	"POPT_ARGFLAG_CALCULATOR: add a value to a counter", "P 24 +" },
   { "sub", '\0', POPT_ARG_VAL|POPT_ARGFLAG_NOT|POPT_ARGFLAG_CALCULATOR|POPT_ARGFLAG_DOC_HIDDEN, &aCounter, 24,
	"POPT_ARGFLAG_CALCULATOR: subtract a value from a counter", "P 24 -" },

  { "bitset", '\0', POPT_BIT_SET | POPT_ARGFLAG_TOGGLE | POPT_ARGFLAG_SHOW_DEFAULT, &aFlag, 0x7777,
	"POPT_BIT_SET: |= 0x7777", 0},
  { "bitclr", '\0', POPT_BIT_CLR | POPT_ARGFLAG_TOGGLE | POPT_ARGFLAG_SHOW_DEFAULT, &aFlag, 0xf842,
	"POPT_BIT_CLR: &= ~0xf842", 0},
  { "bitxor", '\0', POPT_ARG_VAL | POPT_ARGFLAG_XOR | POPT_ARGFLAG_SHOW_DEFAULT, &aFlag, (0x8ace^0xfeed),
	"POPT_ARGFLAG_XOR: ^= (0x8ace^0xfeed)", 0},

  { "nstr", '\0', POPT_ARG_STRING | POPT_ARGFLAG_SHOW_DEFAULT, &nStr, 0,
	"POPT_ARG_STRING: (null)", NULL},
  { "lstr", '\0', POPT_ARG_STRING | POPT_ARGFLAG_SHOW_DEFAULT, &lStr, 0,
	"POPT_ARG_STRING: \"123456789...\"", NULL},

  { NULL, '-', POPT_ARG_NONE | POPT_ARGFLAG_DOC_HIDDEN, &singleDash, 0,
	NULL, NULL },
  { NULL, '\0', POPT_ARG_INCLUDE_TABLE, &moreArgs, 0,
	NULL, NULL },
  { NULL, '\0', POPT_ARG_INCLUDE_TABLE, &callbackArgs, 0,
	"Callback arguments", NULL },
  POPT_AUTOALIAS
  POPT_AUTOHELP
  POPT_TABLEEND
};

static void resetVars(void)
	/*@globals arg1, arg2, arg3, inc, shortopt,
		aVal, aFlag, aShort, aInt, aLong, aLongLong, aFloat, aDouble,
		aArgv, aBits, oStr, singleDash, pass2 @*/
	/*@modifies arg1, arg2, arg3, inc, shortopt,
		aVal, aFlag, aShort, aInt, aLong, aLongLong, aFloat, aDouble,
		aArgv, aBits, oStr, singleDash, pass2 @*/
{
    arg1 = 0;
    arg2 = "(none)";
    arg3 = 0;
    inc = 0;
    shortopt = 0;

    aVal = bVal;
    aFlag = bFlag;

    aShort = bShort;
    aInt = bInt;
    aLong = bLong;
    aLongLong = bLongLong;
    aFloat = bFloat;
    aDouble = bDouble;
    aCounter = bCounter;

    if (aArgv) {
	int i;
	for (i = 0; aArgv[i] != NULL; i++) {
/*@-unqualifiedtrans@*/
	    free((void *)aArgv[i]);
/*@=unqualifiedtrans@*/
	    aArgv[i] = NULL;
	}
	free(aArgv);
	aArgv = NULL;
    }
    if (aBits)
	(void) poptBitsClr(aBits);

    oStr = (char *) -1;

    singleDash = 0;
    pass2 = 0;
}

int main(int argc, const char ** argv)
	/*@globals pass2, fileSystem, internalState @*/
	/*@modifies pass2, fileSystem, internalState @*/
{
    int rc;
    int ec = 0;
    poptContext optCon;
    const char ** rest;
    int help = 0;
    int usage = 0;
    char * testpoptrc;

#if defined(HAVE_MCHECK_H) && defined(HAVE_MTRACE)
    /*@-moduncon -noeffectuncon@*/
    mtrace();   /* Trace malloc only if MALLOC_TRACE=mtrace-output-file. */
    /*@=moduncon =noeffectuncon@*/
#endif

/*@-modobserver@*/
    resetVars();
/*@=modobserver@*/
/*@-temptrans@*/
    optCon = poptGetContext("test1", argc, argv, options, 0);
#ifdef HAVE_STDLIB_H
    testpoptrc = getenv ("testpoptrc");
    if (testpoptrc != NULL )
    (void) poptReadConfigFile(optCon, testpoptrc);
    else {
    (void) poptReadConfigFile(optCon, "./test-poptrc");
/* XXXX: make distcheck succed : test1 is in popt-<version>/_build */
    (void) poptReadConfigFile(optCon, "../../test-poptrc");
    }
#else
    (void) poptReadConfigFile(optCon, "./test-poptrc");
/* XXXX: make distcheck succed : test1 is in popt-<version>/_build */
    (void) poptReadConfigFile(optCon, "../../test-poptrc");
#endif
    (void) poptReadDefaultConfig(optCon, 1);

    poptSetExecPath(optCon, ".", 1);

#if 1
    while ((rc = poptGetNextOpt(optCon)) > 0)	/* Read all the options ... */
	{};

    poptResetContext(optCon);			/* ... and then start over. */
/*@-modobserver@*/
    resetVars();
/*@=modobserver@*/
#endif

    pass2 = 1;
    if ((rc = poptGetNextOpt(optCon)) < -1) {
	fprintf(stderr, "test1: bad argument %s: %s\n",
		poptBadOption(optCon, POPT_BADOPTION_NOALIAS),
		poptStrerror(rc));
	ec = 2;
	goto exit;
    }

    if (help) {
	poptPrintHelp(optCon, stdout, 0);
	goto exit;
    }
    if (usage) {
	poptPrintUsage(optCon, stdout, 0);
	goto exit;
    }

    fprintf(stdout, "arg1: %d arg2: %s", arg1, arg2);

    if (arg3)
	fprintf(stdout, " arg3: %d", arg3);
    if (inc)
	fprintf(stdout, " inc: %d", inc);
    if (shortopt)
	fprintf(stdout, " short: %d", shortopt);
    if (aVal != bVal)
	fprintf(stdout, " aVal: %d", aVal);
    if (aFlag != bFlag)
	fprintf(stdout, " aFlag: 0x%x", aFlag);
    if (aShort != bShort)
	fprintf(stdout, " aShort: %hd", aShort);
    if (aInt != bInt)
	fprintf(stdout, " aInt: %d", aInt);
    if (aLong != bLong)
	fprintf(stdout, " aLong: %ld", aLong);
    if (aLongLong != bLongLong)
#if defined(_MSC_VER) || defined(__MINGW32__)
	fprintf(stdout, " aLongLong: %I64d", aLongLong);
#else
        fprintf(stdout, " aLongLong: %lld", aLongLong);
#endif
/*@-realcompare@*/
    if (aFloat != bFloat)
	fprintf(stdout, " aFloat: %g", (double)aFloat);
    if (aDouble != bDouble)
	fprintf(stdout, " aDouble: %g", aDouble);
/*@=realcompare@*/

    if (aCounter != bCounter)
	fprintf(stdout, " aCounter: %d", aCounter);

    if (aArgv != NULL) {
	const char **av = aArgv;
	const char * arg;
	fprintf(stdout, " aArgv:");
	while ((arg = *av++) != NULL)
	    fprintf(stdout, " %s", arg);
    }
    if (aBits) {
	const char * separator = " ";
	size_t i;
	fprintf(stdout, " aBits:");
 	for (i = 0; i < nattributes; i++) {
	    if (!poptBitsChk(aBits, attributes[i]))
		continue;
	    fprintf(stdout, "%s%s", separator, attributes[i]);
	    separator = ",";
	}
    }
/*@-nullpass@*/
    if (oStr != (char *)-1)
	fprintf(stdout, " oStr: %s", (oStr ? oStr : "(none)"));
/*@=nullpass@*/
    if (singleDash)
	fprintf(stdout, " -");

    if (poptPeekArg(optCon) != NULL) {
	rest = poptGetArgs(optCon);
	if (rest) {
	    fprintf(stdout, " rest:");
	    while (*rest) {
		fprintf(stdout, " %s", *rest);
		rest++;
	    }
	}
    }

    fprintf(stdout, "\n");

exit:
    optCon = poptFreeContext(optCon);
#if defined(HAVE_MCHECK_H) && defined(HAVE_MTRACE)
    /*@-moduncon -noeffectuncon@*/
    muntrace();   /* Trace malloc only if MALLOC_TRACE=mtrace-output-file. */
    /*@=moduncon =noeffectuncon@*/
#endif
    return ec;
}