1 /* (C) 1998-2002 Red Hat, Inc. -- Licensing details are in the COPYING
2 file accompanying popt source distributions, available from
3 ftp://ftp.rpm.org/pub/rpm/dist. */
9 static void option_callback(/*@unused@*/ UNUSED(poptContext con),
10 /*@unused@*/ UNUSED(enum poptCallbackReason reason),
11 const struct poptOption * opt,
12 char * arg, void * data)
13 /*@globals fileSystem @*/
14 /*@modifies fileSystem @*/
17 fprintf(stdout, "callback: %c %s %s ", opt->val, (char *) data, arg);
22 /*@unchecked@*/ /*@observer@*/
23 static char * arg2 = "(none)";
29 static int shortopt = 0;
32 static int aVal = 141421;
34 static int bVal = 141421;
36 static unsigned int aFlag = 0x8aceU;
38 static unsigned int bFlag = 0x8aceU;
41 static short aShort = (short)4523;
43 static short bShort = (short)4523;
45 static int aInt = 271828;
47 static int bInt = 271828;
49 static long aLong = 738905609L;
51 static long bLong = 738905609L;
53 static long long aLongLong = 738905609LL;
55 static long long bLongLong = 738905609LL;
57 static float aFloat = 3.1415926535;
59 static float bFloat = 3.1415926535;
61 static double aDouble = 9.86960440108935861883;
63 static double bDouble = 9.86960440108935861883;
65 /*@unchecked@*/ /*@only@*/ /*@null@*/
66 static const char ** aArgv = NULL;
67 /*@unchecked@*/ /*@only@*/ /*@null@*/
68 static void * aBits = NULL;
69 /*@unchecked@*/ /*@observer@*/
70 static const char *attributes[] = {
71 "foo", "bar", "baz", "bing", "bang", "boom"
74 static size_t nattributes = (sizeof(attributes) / sizeof(attributes[0]));
76 /*@unchecked@*/ /*@null@*/
77 static char * oStr = (char *) -1;
79 static int singleDash = 0;
81 /*@unchecked@*/ /*@observer@*/
83 "This tests default strings and exceeds the ... limit. "
84 "123456789+123456789+123456789+123456789+123456789+ "
85 "123456789+123456789+123456789+123456789+123456789+ "
86 "123456789+123456789+123456789+123456789+123456789+ "
87 "123456789+123456789+123456789+123456789+123456789+ ";
88 /*@unchecked@*/ /*@null@*/
89 static char * nStr = NULL;
92 static struct poptOption moreCallbackArgs[] = {
93 { NULL, '\0', POPT_ARG_CALLBACK|POPT_CBFLAG_INC_DATA,
94 (void *)option_callback, 0,
96 { "cb2", 'c', POPT_ARG_STRING, NULL, (int)'c',
97 "Test argument callbacks", NULL },
102 static struct poptOption callbackArgs[] = {
103 { NULL, '\0', POPT_ARG_CALLBACK, (void *)option_callback, 0,
104 "sampledata", NULL },
105 { "cb", 'c', POPT_ARG_STRING, NULL, (int)'c',
106 "Test argument callbacks", NULL },
107 { "longopt", '\0', 0, NULL, (int)'l',
108 "Unused option for help testing", NULL },
113 static struct poptOption moreArgs[] = {
114 { "inc", 'I', 0, &inc, 0, "An included argument", NULL },
119 static struct poptOption options[] = {
120 { NULL, '\0', POPT_ARG_INCLUDE_TABLE, &moreCallbackArgs, 0,
121 "arg for cb2", NULL },
122 { "arg1", '\0', 0, &arg1, 0, "First argument with a really long"
123 " description. After all, we have to test argument help"
124 " wrapping somehow, right?", NULL },
125 { "arg2", '2', POPT_ARG_STRING | POPT_ARGFLAG_SHOW_DEFAULT, &arg2, 0,
126 "Another argument", "ARG" },
127 { "arg3", '3', POPT_ARG_INT, &arg3, 0,
128 "A third argument", "ANARG" },
129 { "onedash", '\0', POPT_ARGFLAG_ONEDASH, &shortopt, 0,
130 "POPT_ARGFLAG_ONEDASH: Option takes a single -", NULL },
131 { "hidden", '\0', POPT_ARG_STRING | POPT_ARGFLAG_DOC_HIDDEN, NULL, 0,
132 "POPT_ARGFLAG_HIDDEN: A hidden option (--help shouldn't display)",
134 { "optional", '\0', POPT_ARG_STRING | POPT_ARGFLAG_OPTIONAL, &oStr, 0,
135 "POPT_ARGFLAG_OPTIONAL: Takes an optional string argument", NULL },
137 { "val", '\0', POPT_ARG_VAL | POPT_ARGFLAG_SHOW_DEFAULT, &aVal, 125992,
138 "POPT_ARG_VAL: 125992 141421", 0},
140 { "int", 'i', POPT_ARG_INT | POPT_ARGFLAG_SHOW_DEFAULT, &aInt, 0,
141 "POPT_ARG_INT: 271828", NULL },
142 { "short", 's', POPT_ARG_SHORT | POPT_ARGFLAG_SHOW_DEFAULT, &aShort, 0,
143 "POPT_ARG_SHORT: 4523", NULL },
144 { "long", 'l', POPT_ARG_LONG | POPT_ARGFLAG_SHOW_DEFAULT, &aLong, 0,
145 "POPT_ARG_LONG: 738905609", NULL },
146 { "longlong", 'L', POPT_ARG_LONGLONG | POPT_ARGFLAG_SHOW_DEFAULT, &aLongLong, 0,
147 "POPT_ARG_LONGLONG: 738905609", NULL },
148 { "float", 'f', POPT_ARG_FLOAT | POPT_ARGFLAG_SHOW_DEFAULT, &aFloat, 0,
149 "POPT_ARG_FLOAT: 3.14159", NULL },
150 { "double", 'd', POPT_ARG_DOUBLE | POPT_ARGFLAG_SHOW_DEFAULT, &aDouble, 0,
151 "POPT_ARG_DOUBLE: 9.8696", NULL },
153 { "randint", '\0', POPT_ARG_INT|POPT_ARGFLAG_RANDOM, &aInt, 0,
154 "POPT_ARGFLAG_RANDOM: experimental", NULL },
155 { "randshort", '\0', POPT_ARG_SHORT|POPT_ARGFLAG_RANDOM, &aShort, 0,
156 "POPT_ARGFLAG_RANDOM: experimental", NULL },
157 { "randlong", '\0', POPT_ARG_LONG|POPT_ARGFLAG_RANDOM, &aLong, 0,
158 "POPT_ARGFLAG_RANDOM: experimental", NULL },
159 { "randlonglong", '\0', POPT_ARG_LONGLONG|POPT_ARGFLAG_RANDOM, &aLongLong, 0,
160 "POPT_ARGFLAG_RANDOM: experimental", NULL },
162 { "argv", '\0', POPT_ARG_ARGV, &aArgv, 0,
163 "POPT_ARG_ARGV: append string to argv array (can be used multiple times)","STRING"},
164 { "bits", '\0', POPT_ARG_BITSET|POPT_ARGFLAG_DOC_HIDDEN, &aBits, 0,
165 "POPT_ARG_BITSET: add string to bit set (can be used multiple times)","STRING"},
167 { "bitset", '\0', POPT_BIT_SET | POPT_ARGFLAG_TOGGLE | POPT_ARGFLAG_SHOW_DEFAULT, &aFlag, 0x7777,
168 "POPT_BIT_SET: |= 0x7777", 0},
169 { "bitclr", '\0', POPT_BIT_CLR | POPT_ARGFLAG_TOGGLE | POPT_ARGFLAG_SHOW_DEFAULT, &aFlag, 0xf842,
170 "POPT_BIT_CLR: &= ~0xf842", 0},
171 { "bitxor", '\0', POPT_ARG_VAL | POPT_ARGFLAG_XOR | POPT_ARGFLAG_SHOW_DEFAULT, &aFlag, (0x8ace^0xfeed),
172 "POPT_ARGFLAG_XOR: ^= (0x8ace^0xfeed)", 0},
174 { "nstr", '\0', POPT_ARG_STRING | POPT_ARGFLAG_SHOW_DEFAULT, &nStr, 0,
175 "POPT_ARG_STRING: (null)", NULL},
176 { "lstr", '\0', POPT_ARG_STRING | POPT_ARGFLAG_SHOW_DEFAULT, &lStr, 0,
177 "POPT_ARG_STRING: \"123456789...\"", NULL},
179 { NULL, '-', POPT_ARG_NONE | POPT_ARGFLAG_DOC_HIDDEN, &singleDash, 0,
181 { NULL, '\0', POPT_ARG_INCLUDE_TABLE, &moreArgs, 0,
183 { NULL, '\0', POPT_ARG_INCLUDE_TABLE, &callbackArgs, 0,
184 "Callback arguments", NULL },
190 static void resetVars(void)
191 /*@globals arg1, arg2, arg3, inc, shortopt,
192 aVal, aFlag, aShort, aInt, aLong, aLongLong, aFloat, aDouble,
193 aArgv, aBits, oStr, singleDash, pass2 @*/
194 /*@modifies arg1, arg2, arg3, inc, shortopt,
195 aVal, aFlag, aShort, aInt, aLong, aLongLong, aFloat, aDouble,
196 aArgv, aBits, oStr, singleDash, pass2 @*/
210 aLongLong = bLongLong;
216 for (i = 0; aArgv[i] != NULL; i++) {
217 /*@-unqualifiedtrans@*/
218 free((void *)aArgv[i]);
219 /*@=unqualifiedtrans@*/
226 (void) poptBitsClr(aBits);
234 int main(int argc, const char ** argv)
235 /*@globals pass2, fileSystem, internalState @*/
236 /*@modifies pass2, fileSystem, internalState @*/
245 #if defined(HAVE_MCHECK_H) && defined(HAVE_MTRACE)
246 /*@-moduncon -noeffectuncon@*/
247 mtrace(); /* Trace malloc only if MALLOC_TRACE=mtrace-output-file. */
248 /*@=moduncon =noeffectuncon@*/
255 optCon = poptGetContext("test1", argc, argv, options, 0);
257 (void) poptReadConfigFile(optCon, "./test-poptrc");
258 (void) poptReadDefaultConfig(optCon, 1);
260 poptSetExecPath(optCon, ".", 1);
263 while ((rc = poptGetNextOpt(optCon)) > 0) /* Read all the options ... */
266 poptResetContext(optCon); /* ... and then start over. */
273 if ((rc = poptGetNextOpt(optCon)) < -1) {
274 fprintf(stderr, "test1: bad argument %s: %s\n",
275 poptBadOption(optCon, POPT_BADOPTION_NOALIAS),
282 poptPrintHelp(optCon, stdout, 0);
286 poptPrintUsage(optCon, stdout, 0);
290 fprintf(stdout, "arg1: %d arg2: %s", arg1, arg2);
293 fprintf(stdout, " arg3: %d", arg3);
295 fprintf(stdout, " inc: %d", inc);
297 fprintf(stdout, " short: %d", shortopt);
299 fprintf(stdout, " aVal: %d", aVal);
301 fprintf(stdout, " aFlag: 0x%x", aFlag);
302 if (aShort != bShort)
303 fprintf(stdout, " aShort: %hd", aShort);
305 fprintf(stdout, " aInt: %d", aInt);
307 fprintf(stdout, " aLong: %ld", aLong);
308 if (aLongLong != bLongLong)
309 fprintf(stdout, " aLongLong: %lld", aLongLong);
311 if (aFloat != bFloat)
312 fprintf(stdout, " aFloat: %g", (double)aFloat);
313 if (aDouble != bDouble)
314 fprintf(stdout, " aDouble: %g", aDouble);
317 const char **av = aArgv;
319 fprintf(stdout, " aArgv:");
320 while ((arg = *av++) != NULL)
321 fprintf(stdout, " %s", arg);
324 const char * separator = " ";
326 fprintf(stdout, " aBits:");
327 for (i = 0; i < nattributes; i++) {
328 if (!poptBitsChk(aBits, attributes[i]))
330 fprintf(stdout, "%s%s", separator, attributes[i]);
335 if (oStr != (char *)-1)
336 fprintf(stdout, " oStr: %s", (oStr ? oStr : "(none)"));
339 fprintf(stdout, " -");
341 if (poptPeekArg(optCon) != NULL) {
342 rest = poptGetArgs(optCon);
344 fprintf(stdout, " rest:");
346 fprintf(stdout, " %s", *rest);
352 fprintf(stdout, "\n");
355 optCon = poptFreeContext(optCon);
356 #if defined(HAVE_MCHECK_H) && defined(HAVE_MTRACE)
357 /*@-moduncon -noeffectuncon@*/
358 muntrace(); /* Trace malloc only if MALLOC_TRACE=mtrace-output-file. */
359 /*@=moduncon =noeffectuncon@*/