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@*/ poptContext con,
10 /*@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;
41 static int aInt = 271828;
43 static int bInt = 271828;
45 static long aLong = 738905609L;
47 static long bLong = 738905609L;
49 static float aFloat = 3.1415926535;
51 static float bFloat = 3.1415926535;
53 static double aDouble = 9.86960440108935861883;
55 static double bDouble = 9.86960440108935861883;
56 /*@unchecked@*/ /*@null@*/
57 static char * oStr = (char *) -1;
59 static int singleDash = 0;
61 /*@unchecked@*/ /*@observer@*/
63 "This tests default strings and exceeds the ... limit. "
64 "123456789+123456789+123456789+123456789+123456789+ "
65 "123456789+123456789+123456789+123456789+123456789+ "
66 "123456789+123456789+123456789+123456789+123456789+ "
67 "123456789+123456789+123456789+123456789+123456789+ ";
68 /*@unchecked@*/ /*@null@*/
69 static char * nStr = NULL;
72 static struct poptOption moreCallbackArgs[] = {
73 { NULL, '\0', POPT_ARG_CALLBACK|POPT_CBFLAG_INC_DATA,
74 (void *)option_callback, 0,
76 { "cb2", 'c', POPT_ARG_STRING, NULL, 'c',
77 "Test argument callbacks", NULL },
82 static struct poptOption callbackArgs[] = {
83 { NULL, '\0', POPT_ARG_CALLBACK, (void *)option_callback, 0,
85 { "cb", 'c', POPT_ARG_STRING, NULL, 'c',
86 "Test argument callbacks", NULL },
87 { "longopt", '\0', 0, NULL, 'l',
88 "Unused option for help testing", NULL },
93 static struct poptOption moreArgs[] = {
94 { "inc", 'I', 0, &inc, 0, "An included argument", NULL },
99 static struct poptOption options[] = {
100 { NULL, '\0', POPT_ARG_INCLUDE_TABLE, &moreCallbackArgs, 0,
101 "arg for cb2", NULL },
102 { "arg1", '\0', 0, &arg1, 0, "First argument with a really long"
103 " description. After all, we have to test argument help"
104 " wrapping somehow, right?", NULL },
105 { "arg2", '2', POPT_ARG_STRING | POPT_ARGFLAG_SHOW_DEFAULT, &arg2, 0,
106 "Another argument", "ARG" },
107 { "arg3", '3', POPT_ARG_INT, &arg3, 0,
108 "A third argument", "ANARG" },
109 { "onedash", '\0', POPT_ARGFLAG_ONEDASH, &shortopt, 0,
110 "POPT_ARGFLAG_ONEDASH: Option takes a single -", NULL },
111 { "hidden", '\0', POPT_ARG_STRING | POPT_ARGFLAG_DOC_HIDDEN, NULL, 0,
112 "POPT_ARGFLAG_HIDDEN: A hidden option (--help shouldn't display)",
114 { "optional", '\0', POPT_ARG_STRING | POPT_ARGFLAG_OPTIONAL, &oStr, 0,
115 "POPT_ARGFLAG_OPTIONAL: Takes an optional string argument", NULL },
117 { "val", '\0', POPT_ARG_VAL | POPT_ARGFLAG_SHOW_DEFAULT, &aVal, 125992,
118 "POPT_ARG_VAL: 125992 141421", 0},
120 { "int", 'i', POPT_ARG_INT | POPT_ARGFLAG_SHOW_DEFAULT, &aInt, 0,
121 "POPT_ARG_INT: 271828", NULL },
122 { "long", 'l', POPT_ARG_LONG | POPT_ARGFLAG_SHOW_DEFAULT, &aLong, 0,
123 "POPT_ARG_LONG: 738905609", NULL },
124 { "float", 'f', POPT_ARG_FLOAT | POPT_ARGFLAG_SHOW_DEFAULT, &aFloat, 0,
125 "POPT_ARG_FLOAT: 3.14159", NULL },
126 { "double", 'd', POPT_ARG_DOUBLE | POPT_ARGFLAG_SHOW_DEFAULT, &aDouble, 0,
127 "POPT_ARG_DOUBLE: 9.8696", NULL },
129 { "bitset", '\0', POPT_BIT_SET | POPT_ARGFLAG_SHOW_DEFAULT, &aFlag, 0x4321,
130 "POPT_BIT_SET: |= 0x4321", 0},
131 { "bitclr", '\0', POPT_BIT_CLR | POPT_ARGFLAG_SHOW_DEFAULT, &aFlag, 0x1234,
132 "POPT_BIT_CLR: &= ~0x1234", 0},
134 { "nstr", '\0', POPT_ARG_STRING | POPT_ARGFLAG_SHOW_DEFAULT, &nStr, 0,
135 "POPT_ARG_STRING: (null)", NULL},
136 { "lstr", '\0', POPT_ARG_STRING | POPT_ARGFLAG_SHOW_DEFAULT, &lStr, 0,
137 "POPT_ARG_STRING: \"123456789...\"", NULL},
139 { NULL, '-', POPT_ARG_NONE | POPT_ARGFLAG_DOC_HIDDEN, &singleDash, 0,
141 { NULL, '\0', POPT_ARG_INCLUDE_TABLE, &moreArgs, 0,
143 { NULL, '\0', POPT_ARG_INCLUDE_TABLE, &callbackArgs, 0,
144 "Callback arguments", NULL },
149 static void resetVars(void)
150 /*@globals arg1, arg2, arg3, inc, shortopt,
151 aVal, aFlag, aInt, aLong, aFloat, aDouble,
152 oStr, singleDash, pass2 @*/
153 /*@modifies arg1, arg2, arg3, inc, shortopt,
154 aVal, aFlag, aInt, aLong, aFloat, aDouble,
155 oStr, singleDash, pass2 @*/
177 int main(int argc, const char ** argv)
178 /*@globals pass2, fileSystem, internalState @*/
179 /*@modifies pass2, fileSystem, internalState @*/
188 #if HAVE_MCHECK_H && HAVE_MTRACE
189 /*@-moduncon -noeffectuncon@*/
190 mtrace(); /* Trace malloc only if MALLOC_TRACE=mtrace-output-file. */
191 /*@=moduncon =noeffectuncon@*/
198 optCon = poptGetContext("test1", argc, argv, options, 0);
200 (void) poptReadConfigFile(optCon, "./test-poptrc");
203 while ((rc = poptGetNextOpt(optCon)) > 0) /* Read all the options ... */
206 poptResetContext(optCon); /* ... and then start over. */
213 if ((rc = poptGetNextOpt(optCon)) < -1) {
214 fprintf(stderr, "test1: bad argument %s: %s\n",
215 poptBadOption(optCon, POPT_BADOPTION_NOALIAS),
222 poptPrintHelp(optCon, stdout, 0);
226 poptPrintUsage(optCon, stdout, 0);
230 fprintf(stdout, "arg1: %d arg2: %s", arg1, arg2);
233 fprintf(stdout, " arg3: %d", arg3);
235 fprintf(stdout, " inc: %d", inc);
237 fprintf(stdout, " short: %d", shortopt);
239 fprintf(stdout, " aVal: %d", aVal);
241 fprintf(stdout, " aFlag: %d", aFlag);
243 fprintf(stdout, " aInt: %d", aInt);
245 fprintf(stdout, " aLong: %ld", aLong);
247 if (aFloat != bFloat)
248 fprintf(stdout, " aFloat: %g", aFloat);
249 if (aDouble != bDouble)
250 fprintf(stdout, " aDouble: %g", aDouble);
252 if (oStr != (char *)-1)
253 fprintf(stdout, " oStr: %s", (oStr ? oStr : "(none)"));
255 fprintf(stdout, " -");
258 rest = poptGetArgs(optCon);
260 fprintf(stdout, " rest:");
262 fprintf(stdout, " %s", *rest);
268 fprintf(stdout, "\n");
271 optCon = poptFreeContext(optCon);
272 #if HAVE_MCHECK_H && HAVE_MTRACE
273 /*@-moduncon -noeffectuncon@*/
274 muntrace(); /* Trace malloc only if MALLOC_TRACE=mtrace-output-file. */
275 /*@=moduncon =noeffectuncon@*/