eeeb916ee71edefb52c6e11cbfbf941f5f951e0f
[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@*/ poptContext con,
10                 /*@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 int aFlag = 0;
37 /*@unchecked@*/
38 static int bFlag = 0;
39
40 /*@unchecked@*/
41 static int aInt = 271828;
42 /*@unchecked@*/
43 static int bInt = 271828;
44 /*@unchecked@*/
45 static long aLong = 738905609L;
46 /*@unchecked@*/
47 static long bLong = 738905609L;
48 /*@unchecked@*/
49 static float aFloat = 3.1415926535;
50 /*@unchecked@*/
51 static float bFloat = 3.1415926535;
52 /*@unchecked@*/
53 static double aDouble = 9.86960440108935861883;
54 /*@unchecked@*/
55 static double bDouble = 9.86960440108935861883;
56 /*@unchecked@*/ /*@null@*/
57 static char * oStr = (char *) -1;
58 /*@unchecked@*/
59 static int singleDash = 0;
60
61 /*@unchecked@*/ /*@observer@*/
62 static char * lStr =
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; 
70
71 /*@unchecked@*/
72 static struct poptOption moreCallbackArgs[] = {
73   { NULL, '\0', POPT_ARG_CALLBACK|POPT_CBFLAG_INC_DATA,
74         (void *)option_callback, 0,
75         NULL, NULL },
76   { "cb2", 'c', POPT_ARG_STRING, NULL, 'c',
77         "Test argument callbacks", NULL },
78   POPT_TABLEEND
79 };
80
81 /*@unchecked@*/
82 static struct poptOption callbackArgs[] = {
83   { NULL, '\0', POPT_ARG_CALLBACK, (void *)option_callback, 0,
84         "sampledata", NULL },
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 },
89   POPT_TABLEEND
90 };
91
92 /*@unchecked@*/
93 static struct poptOption moreArgs[] = {
94   { "inc", 'I', 0, &inc, 0, "An included argument", NULL },
95   POPT_TABLEEND
96 };
97
98 /*@unchecked@*/
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)",
113         NULL },
114   { "optional", '\0', POPT_ARG_STRING | POPT_ARGFLAG_OPTIONAL, &oStr, 0,
115         "POPT_ARGFLAG_OPTIONAL: Takes an optional string argument", NULL },
116
117   { "val", '\0', POPT_ARG_VAL | POPT_ARGFLAG_SHOW_DEFAULT, &aVal, 125992,
118         "POPT_ARG_VAL: 125992 141421", 0},
119
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 },
128
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},
133
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},
138
139   { NULL, '-', POPT_ARG_NONE | POPT_ARGFLAG_DOC_HIDDEN, &singleDash, 0,
140         NULL, NULL },
141   { NULL, '\0', POPT_ARG_INCLUDE_TABLE, &moreArgs, 0,
142         NULL, NULL },
143   { NULL, '\0', POPT_ARG_INCLUDE_TABLE, &callbackArgs, 0,
144         "Callback arguments", NULL },
145   POPT_AUTOHELP
146   POPT_TABLEEND
147 };
148
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 @*/
156 {
157     arg1 = 0;
158     arg2 = "(none)";
159     arg3 = 0;
160     inc = 0;
161     shortopt = 0;
162
163     aVal = bVal;
164     aFlag = bFlag;
165
166     aInt = bInt;
167     aLong = bLong;
168     aFloat = bFloat;
169     aDouble = bDouble;
170
171     oStr = (char *) -1;
172
173     singleDash = 0;
174     pass2 = 0;
175 }
176
177 int main(int argc, const char ** argv)
178         /*@globals pass2, fileSystem, internalState @*/
179         /*@modifies pass2, fileSystem, internalState @*/
180 {
181     int rc;
182     int ec = 0;
183     poptContext optCon;
184     const char ** rest;
185     int help = 0;
186     int usage = 0;
187
188 #if HAVE_MCHECK_H && HAVE_MTRACE
189     /*@-moduncon -noeffectuncon@*/
190     mtrace();   /* Trace malloc only if MALLOC_TRACE=mtrace-output-file. */
191     /*@=moduncon =noeffectuncon@*/
192 #endif
193
194 /*@-modobserver@*/
195     resetVars();
196 /*@=modobserver@*/
197 /*@-temptrans@*/
198     optCon = poptGetContext("test1", argc, argv, options, 0);
199 /*@=temptrans@*/
200     (void) poptReadConfigFile(optCon, "./test-poptrc");
201
202 #if 1
203     while ((rc = poptGetNextOpt(optCon)) > 0)   /* Read all the options ... */
204         {};
205
206     poptResetContext(optCon);                   /* ... and then start over. */
207 /*@-modobserver@*/
208     resetVars();
209 /*@=modobserver@*/
210 #endif
211
212     pass2 = 1;
213     if ((rc = poptGetNextOpt(optCon)) < -1) {
214         fprintf(stderr, "test1: bad argument %s: %s\n",
215                 poptBadOption(optCon, POPT_BADOPTION_NOALIAS),
216                 poptStrerror(rc));
217         ec = 2;
218         goto exit;
219     }
220
221     if (help) {
222         poptPrintHelp(optCon, stdout, 0);
223         goto exit;
224     }
225     if (usage) {
226         poptPrintUsage(optCon, stdout, 0);
227         goto exit;
228     }
229
230     fprintf(stdout, "arg1: %d arg2: %s", arg1, arg2);
231
232     if (arg3)
233         fprintf(stdout, " arg3: %d", arg3);
234     if (inc)
235         fprintf(stdout, " inc: %d", inc);
236     if (shortopt)
237         fprintf(stdout, " short: %d", shortopt);
238     if (aVal != bVal)
239         fprintf(stdout, " aVal: %d", aVal);
240     if (aFlag != bFlag)
241         fprintf(stdout, " aFlag: %d", aFlag);
242     if (aInt != bInt)
243         fprintf(stdout, " aInt: %d", aInt);
244     if (aLong != bLong)
245         fprintf(stdout, " aLong: %ld", aLong);
246 /*@-realcompare@*/
247     if (aFloat != bFloat)
248         fprintf(stdout, " aFloat: %g", aFloat);
249     if (aDouble != bDouble)
250         fprintf(stdout, " aDouble: %g", aDouble);
251 /*@=realcompare@*/
252     if (oStr != (char *)-1)
253         fprintf(stdout, " oStr: %s", (oStr ? oStr : "(none)"));
254     if (singleDash)
255         fprintf(stdout, " -");
256
257 /*@-boundsread@*/
258     rest = poptGetArgs(optCon);
259     if (rest) {
260         fprintf(stdout, " rest:");
261         while (*rest) {
262             fprintf(stdout, " %s", *rest);
263             rest++;
264         }
265     }
266 /*@=boundsread@*/
267
268     fprintf(stdout, "\n");
269
270 exit:
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@*/
276 #endif
277     return ec;
278 }