Tizen 2.1 base
[external/popt.git] / test1.c
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. */
4
5 #include "system.h"
6
7 /*@unchecked@*/
8 static int pass2 = 0;
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 @*/
15 {
16     if (pass2)
17         fprintf(stdout, "callback: %c %s %s ", opt->val, (char *) data, arg);
18 }
19
20 /*@unchecked@*/
21 static int arg1 = 0;
22 /*@unchecked@*/ /*@observer@*/
23 static char * arg2 = "(none)";
24 /*@unchecked@*/
25 static int arg3 = 0;
26 /*@unchecked@*/
27 static int inc = 0;
28 /*@unchecked@*/
29 static int shortopt = 0;
30
31 /*@unchecked@*/
32 static int aVal = 141421;
33 /*@unchecked@*/
34 static int bVal = 141421;
35 /*@unchecked@*/
36 static unsigned int aFlag = 0x8aceU;
37 /*@unchecked@*/
38 static unsigned int bFlag = 0x8aceU;
39
40 /*@unchecked@*/
41 static short aShort = (short)4523;
42 /*@unchecked@*/
43 static short bShort = (short)4523;
44 /*@unchecked@*/
45 static int aInt = 271828;
46 /*@unchecked@*/
47 static int bInt = 271828;
48 /*@unchecked@*/
49 static long aLong = 738905609L;
50 /*@unchecked@*/
51 static long bLong = 738905609L;
52 /*@unchecked@*/
53 static long long aLongLong = 738905609LL;
54 /*@unchecked@*/
55 static long long bLongLong = 738905609LL;
56 /*@unchecked@*/
57 static float aFloat = 3.1415926535;
58 /*@unchecked@*/
59 static float bFloat = 3.1415926535;
60 /*@unchecked@*/
61 static double aDouble = 9.86960440108935861883;
62 /*@unchecked@*/
63 static double bDouble = 9.86960440108935861883;
64
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"
72 };
73 /*@unchecked@*/
74 static size_t nattributes = (sizeof(attributes) / sizeof(attributes[0]));
75
76 /*@unchecked@*/ /*@null@*/
77 static char * oStr = (char *) -1;
78 /*@unchecked@*/
79 static int singleDash = 0;
80
81 /*@unchecked@*/ /*@observer@*/
82 static char * lStr =
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; 
90
91 /*@unchecked@*/
92 static struct poptOption moreCallbackArgs[] = {
93   { NULL, '\0', POPT_ARG_CALLBACK|POPT_CBFLAG_INC_DATA,
94         (void *)option_callback, 0,
95         NULL, NULL },
96   { "cb2", 'c', POPT_ARG_STRING, NULL, (int)'c',
97         "Test argument callbacks", NULL },
98   POPT_TABLEEND
99 };
100
101 /*@unchecked@*/
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 },
109   POPT_TABLEEND
110 };
111
112 /*@unchecked@*/
113 static struct poptOption moreArgs[] = {
114   { "inc", 'I', 0, &inc, 0, "An included argument", NULL },
115   POPT_TABLEEND
116 };
117
118 /*@unchecked@*/
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)",
133         NULL },
134   { "optional", '\0', POPT_ARG_STRING | POPT_ARGFLAG_OPTIONAL, &oStr, 0,
135         "POPT_ARGFLAG_OPTIONAL: Takes an optional string argument", NULL },
136
137   { "val", '\0', POPT_ARG_VAL | POPT_ARGFLAG_SHOW_DEFAULT, &aVal, 125992,
138         "POPT_ARG_VAL: 125992 141421", 0},
139
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 },
152
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 },
161
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"},
166
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},
173
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},
178
179   { NULL, '-', POPT_ARG_NONE | POPT_ARGFLAG_DOC_HIDDEN, &singleDash, 0,
180         NULL, NULL },
181   { NULL, '\0', POPT_ARG_INCLUDE_TABLE, &moreArgs, 0,
182         NULL, NULL },
183   { NULL, '\0', POPT_ARG_INCLUDE_TABLE, &callbackArgs, 0,
184         "Callback arguments", NULL },
185   POPT_AUTOALIAS
186   POPT_AUTOHELP
187   POPT_TABLEEND
188 };
189
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 @*/
197 {
198     arg1 = 0;
199     arg2 = "(none)";
200     arg3 = 0;
201     inc = 0;
202     shortopt = 0;
203
204     aVal = bVal;
205     aFlag = bFlag;
206
207     aShort = bShort;
208     aInt = bInt;
209     aLong = bLong;
210     aLongLong = bLongLong;
211     aFloat = bFloat;
212     aDouble = bDouble;
213
214     if (aArgv) {
215         int i;
216         for (i = 0; aArgv[i] != NULL; i++) {
217 /*@-unqualifiedtrans@*/
218             free((void *)aArgv[i]);
219 /*@=unqualifiedtrans@*/
220             aArgv[i] = NULL;
221         }
222         free(aArgv);
223         aArgv = NULL;
224     }
225     if (aBits)
226         (void) poptBitsClr(aBits);
227
228     oStr = (char *) -1;
229
230     singleDash = 0;
231     pass2 = 0;
232 }
233
234 int main(int argc, const char ** argv)
235         /*@globals pass2, fileSystem, internalState @*/
236         /*@modifies pass2, fileSystem, internalState @*/
237 {
238     int rc;
239     int ec = 0;
240     poptContext optCon;
241     const char ** rest;
242     int help = 0;
243     int usage = 0;
244
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@*/
249 #endif
250
251 /*@-modobserver@*/
252     resetVars();
253 /*@=modobserver@*/
254 /*@-temptrans@*/
255     optCon = poptGetContext("test1", argc, argv, options, 0);
256 /*@=temptrans@*/
257     (void) poptReadConfigFile(optCon, "./test-poptrc");
258     (void) poptReadDefaultConfig(optCon, 1);
259
260     poptSetExecPath(optCon, ".", 1);
261
262 #if 1
263     while ((rc = poptGetNextOpt(optCon)) > 0)   /* Read all the options ... */
264         {};
265
266     poptResetContext(optCon);                   /* ... and then start over. */
267 /*@-modobserver@*/
268     resetVars();
269 /*@=modobserver@*/
270 #endif
271
272     pass2 = 1;
273     if ((rc = poptGetNextOpt(optCon)) < -1) {
274         fprintf(stderr, "test1: bad argument %s: %s\n",
275                 poptBadOption(optCon, POPT_BADOPTION_NOALIAS),
276                 poptStrerror(rc));
277         ec = 2;
278         goto exit;
279     }
280
281     if (help) {
282         poptPrintHelp(optCon, stdout, 0);
283         goto exit;
284     }
285     if (usage) {
286         poptPrintUsage(optCon, stdout, 0);
287         goto exit;
288     }
289
290     fprintf(stdout, "arg1: %d arg2: %s", arg1, arg2);
291
292     if (arg3)
293         fprintf(stdout, " arg3: %d", arg3);
294     if (inc)
295         fprintf(stdout, " inc: %d", inc);
296     if (shortopt)
297         fprintf(stdout, " short: %d", shortopt);
298     if (aVal != bVal)
299         fprintf(stdout, " aVal: %d", aVal);
300     if (aFlag != bFlag)
301         fprintf(stdout, " aFlag: 0x%x", aFlag);
302     if (aShort != bShort)
303         fprintf(stdout, " aShort: %hd", aShort);
304     if (aInt != bInt)
305         fprintf(stdout, " aInt: %d", aInt);
306     if (aLong != bLong)
307         fprintf(stdout, " aLong: %ld", aLong);
308     if (aLongLong != bLongLong)
309         fprintf(stdout, " aLongLong: %lld", aLongLong);
310 /*@-realcompare@*/
311     if (aFloat != bFloat)
312         fprintf(stdout, " aFloat: %g", (double)aFloat);
313     if (aDouble != bDouble)
314         fprintf(stdout, " aDouble: %g", aDouble);
315 /*@=realcompare@*/
316     if (aArgv != NULL) {
317         const char **av = aArgv;
318         const char * arg;
319         fprintf(stdout, " aArgv:");
320         while ((arg = *av++) != NULL)
321             fprintf(stdout, " %s", arg);
322     }
323     if (aBits) {
324         const char * separator = " ";
325         size_t i;
326         fprintf(stdout, " aBits:");
327         for (i = 0; i < nattributes; i++) {
328             if (!poptBitsChk(aBits, attributes[i]))
329                 continue;
330             fprintf(stdout, "%s%s", separator, attributes[i]);
331             separator = ",";
332         }
333     }
334 /*@-nullpass@*/
335     if (oStr != (char *)-1)
336         fprintf(stdout, " oStr: %s", (oStr ? oStr : "(none)"));
337 /*@=nullpass@*/
338     if (singleDash)
339         fprintf(stdout, " -");
340
341     if (poptPeekArg(optCon) != NULL) {
342         rest = poptGetArgs(optCon);
343         if (rest) {
344             fprintf(stdout, " rest:");
345             while (*rest) {
346                 fprintf(stdout, " %s", *rest);
347                 rest++;
348             }
349         }
350     }
351
352     fprintf(stdout, "\n");
353
354 exit:
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@*/
360 #endif
361     return ec;
362 }