| 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 |
static int pass2 = 0; |
| 8 |
static void option_callback(UNUSED(poptContext con), |
| 9 |
UNUSED(enum poptCallbackReason reason), |
| 10 |
const struct poptOption * opt, |
| 11 |
char * arg, void * data) |
| 12 |
{ |
| 13 |
if (pass2) |
| 14 |
fprintf(stdout, "callback: %c %s %s ", opt->val, (char *) data, arg); |
| 15 |
} |
| 16 |
|
| 17 |
static int arg1 = 0; |
| 18 |
static const char * arg2 = "(none)"; |
| 19 |
static int arg3 = 0; |
| 20 |
static int inc = 0; |
| 21 |
static int shortopt = 0; |
| 22 |
|
| 23 |
static int aVal = 141421; |
| 24 |
static int bVal = 141421; |
| 25 |
static unsigned int aFlag = 0x8aceU; |
| 26 |
static unsigned int bFlag = 0x8aceU; |
| 27 |
|
| 28 |
static short aShort = (short)4523; |
| 29 |
static short bShort = (short)4523; |
| 30 |
static int aInt = 271828; |
| 31 |
static int bInt = 271828; |
| 32 |
static long aLong = 738905609L; |
| 33 |
static long bLong = 738905609L; |
| 34 |
static long long aLongLong = 738905609LL; |
| 35 |
static long long bLongLong = 738905609LL; |
| 36 |
static float aFloat = 3.1415926535f; |
| 37 |
static float bFloat = 3.1415926535f; |
| 38 |
static double aDouble = 9.86960440108935861883; |
| 39 |
static double bDouble = 9.86960440108935861883; |
| 40 |
|
| 41 |
static const char ** aArgv = NULL; |
| 42 |
static void * aBits = NULL; |
| 43 |
static const char *attributes[] = { |
| 44 |
"foo", "bar", "baz", "bing", "bang", "boom" |
| 45 |
}; |
| 46 |
static size_t nattributes = (sizeof(attributes) / sizeof(attributes[0])); |
| 47 |
|
| 48 |
static char * oStr = (char *) -1; |
| 49 |
static int singleDash = 0; |
| 50 |
|
| 51 |
static const char * lStr = |
| 52 |
"This tests default strings and exceeds the ... limit. " |
| 53 |
"123456789+123456789+123456789+123456789+123456789+ " |
| 54 |
"123456789+123456789+123456789+123456789+123456789+ " |
| 55 |
"123456789+123456789+123456789+123456789+123456789+ " |
| 56 |
"123456789+123456789+123456789+123456789+123456789+ "; |
| 57 |
static char * nStr = NULL; |
| 58 |
|
| 59 |
static struct poptOption moreCallbackArgs[] = { |
| 60 |
{ NULL, '\0', POPT_ARG_CALLBACK|POPT_CBFLAG_INC_DATA, |
| 61 |
(void *)option_callback, 0, |
| 62 |
NULL, NULL }, |
| 63 |
{ "cb2", 'c', POPT_ARG_STRING, NULL, (int)'c', |
| 64 |
"Test argument callbacks", NULL }, |
| 65 |
POPT_TABLEEND |
| 66 |
}; |
| 67 |
|
| 68 |
static struct poptOption callbackArgs[] = { |
| 69 |
{ NULL, '\0', POPT_ARG_CALLBACK, (void *)option_callback, 0, |
| 70 |
"sampledata", NULL }, |
| 71 |
{ "cb", 'c', POPT_ARG_STRING, NULL, (int)'c', |
| 72 |
"Test argument callbacks", NULL }, |
| 73 |
{ "longopt", '\0', 0, NULL, (int)'l', |
| 74 |
"Unused option for help testing", NULL }, |
| 75 |
POPT_TABLEEND |
| 76 |
}; |
| 77 |
|
| 78 |
static struct poptOption moreArgs[] = { |
| 79 |
{ "inc", 'I', 0, &inc, 0, "An included argument", NULL }, |
| 80 |
POPT_TABLEEND |
| 81 |
}; |
| 82 |
|
| 83 |
static struct poptOption options[] = { |
| 84 |
{ NULL, '\0', POPT_ARG_INCLUDE_TABLE, &moreCallbackArgs, 0, |
| 85 |
"arg for cb2", NULL }, |
| 86 |
{ "arg1", '\0', 0, &arg1, 0, "First argument with a really long" |
| 87 |
" description. After all, we have to test argument help" |
| 88 |
" wrapping somehow, right?", NULL }, |
| 89 |
{ "arg2", '2', POPT_ARG_STRING | POPT_ARGFLAG_SHOW_DEFAULT, &arg2, 0, |
| 90 |
"Another argument", "ARG" }, |
| 91 |
{ "arg3", '3', POPT_ARG_INT, &arg3, 0, |
| 92 |
"A third argument", "ANARG" }, |
| 93 |
{ "onedash", '\0', POPT_ARGFLAG_ONEDASH, &shortopt, 0, |
| 94 |
"POPT_ARGFLAG_ONEDASH: Option takes a single -", NULL }, |
| 95 |
{ "hidden", '\0', POPT_ARG_STRING | POPT_ARGFLAG_DOC_HIDDEN, NULL, 0, |
| 96 |
"POPT_ARGFLAG_HIDDEN: A hidden option (--help shouldn't display)", |
| 97 |
NULL }, |
| 98 |
{ "optional", '\0', POPT_ARG_STRING | POPT_ARGFLAG_OPTIONAL, &oStr, 0, |
| 99 |
"POPT_ARGFLAG_OPTIONAL: Takes an optional string argument", NULL }, |
| 100 |
|
| 101 |
{ "val", '\0', POPT_ARG_VAL | POPT_ARGFLAG_SHOW_DEFAULT, &aVal, 125992, |
| 102 |
"POPT_ARG_VAL: 125992 141421", 0}, |
| 103 |
|
| 104 |
{ "int", 'i', POPT_ARG_INT | POPT_ARGFLAG_SHOW_DEFAULT, &aInt, 0, |
| 105 |
"POPT_ARG_INT: 271828", NULL }, |
| 106 |
{ "short", 's', POPT_ARG_SHORT | POPT_ARGFLAG_SHOW_DEFAULT, &aShort, 0, |
| 107 |
"POPT_ARG_SHORT: 4523", NULL }, |
| 108 |
{ "long", 'l', POPT_ARG_LONG | POPT_ARGFLAG_SHOW_DEFAULT, &aLong, 0, |
| 109 |
"POPT_ARG_LONG: 738905609", NULL }, |
| 110 |
{ "longlong", 'L', POPT_ARG_LONGLONG | POPT_ARGFLAG_SHOW_DEFAULT, &aLongLong, 0, |
| 111 |
"POPT_ARG_LONGLONG: 738905609", NULL }, |
| 112 |
{ "float", 'f', POPT_ARG_FLOAT | POPT_ARGFLAG_SHOW_DEFAULT, &aFloat, 0, |
| 113 |
"POPT_ARG_FLOAT: 3.14159", NULL }, |
| 114 |
{ "double", 'd', POPT_ARG_DOUBLE | POPT_ARGFLAG_SHOW_DEFAULT, &aDouble, 0, |
| 115 |
"POPT_ARG_DOUBLE: 9.8696", NULL }, |
| 116 |
|
| 117 |
{ "randint", '\0', POPT_ARG_INT|POPT_ARGFLAG_RANDOM, &aInt, 0, |
| 118 |
"POPT_ARGFLAG_RANDOM: experimental", NULL }, |
| 119 |
{ "randshort", '\0', POPT_ARG_SHORT|POPT_ARGFLAG_RANDOM, &aShort, 0, |
| 120 |
"POPT_ARGFLAG_RANDOM: experimental", NULL }, |
| 121 |
{ "randlong", '\0', POPT_ARG_LONG|POPT_ARGFLAG_RANDOM, &aLong, 0, |
| 122 |
"POPT_ARGFLAG_RANDOM: experimental", NULL }, |
| 123 |
{ "randlonglong", '\0', POPT_ARG_LONGLONG|POPT_ARGFLAG_RANDOM, &aLongLong, 0, |
| 124 |
"POPT_ARGFLAG_RANDOM: experimental", NULL }, |
| 125 |
|
| 126 |
{ "argv", '\0', POPT_ARG_ARGV, &aArgv, 0, |
| 127 |
"POPT_ARG_ARGV: append string to argv array (can be used multiple times)","STRING"}, |
| 128 |
{ "bits", '\0', POPT_ARG_BITSET|POPT_ARGFLAG_DOC_HIDDEN, &aBits, 0, |
| 129 |
"POPT_ARG_BITSET: add string to bit set (can be used multiple times)","STRING"}, |
| 130 |
|
| 131 |
{ "bitset", '\0', POPT_BIT_SET | POPT_ARGFLAG_TOGGLE | POPT_ARGFLAG_SHOW_DEFAULT, &aFlag, 0x7777, |
| 132 |
"POPT_BIT_SET: |= 0x7777", 0}, |
| 133 |
{ "bitclr", '\0', POPT_BIT_CLR | POPT_ARGFLAG_TOGGLE | POPT_ARGFLAG_SHOW_DEFAULT, &aFlag, 0xf842, |
| 134 |
"POPT_BIT_CLR: &= ~0xf842", 0}, |
| 135 |
{ "bitxor", 'x', POPT_ARG_VAL | POPT_ARGFLAG_XOR | POPT_ARGFLAG_SHOW_DEFAULT, &aFlag, (0x8ace^0xfeed), |
| 136 |
"POPT_ARGFLAG_XOR: ^= (0x8ace^0xfeed)", 0}, |
| 137 |
|
| 138 |
{ "nstr", '\0', POPT_ARG_STRING | POPT_ARGFLAG_SHOW_DEFAULT, &nStr, 0, |
| 139 |
"POPT_ARG_STRING: (null)", NULL}, |
| 140 |
{ "lstr", '\0', POPT_ARG_STRING | POPT_ARGFLAG_SHOW_DEFAULT, &lStr, 0, |
| 141 |
"POPT_ARG_STRING: \"123456789...\"", NULL}, |
| 142 |
|
| 143 |
{ NULL, '-', POPT_ARG_NONE | POPT_ARGFLAG_DOC_HIDDEN, &singleDash, 0, |
| 144 |
NULL, NULL }, |
| 145 |
{ NULL, '\0', POPT_ARG_INCLUDE_TABLE, &moreArgs, 0, |
| 146 |
NULL, NULL }, |
| 147 |
{ NULL, '\0', POPT_ARG_INCLUDE_TABLE, &callbackArgs, 0, |
| 148 |
"Callback arguments", NULL }, |
| 149 |
POPT_AUTOALIAS |
| 150 |
POPT_AUTOHELP |
| 151 |
POPT_TABLEEND |
| 152 |
}; |
| 153 |
|
| 154 |
static void resetVars(void) |
| 155 |
{ |
| 156 |
arg1 = 0; |
| 157 |
arg2 = "(none)"; |
| 158 |
arg3 = 0; |
| 159 |
inc = 0; |
| 160 |
shortopt = 0; |
| 161 |
|
| 162 |
aVal = bVal; |
| 163 |
aFlag = bFlag; |
| 164 |
|
| 165 |
aShort = bShort; |
| 166 |
aInt = bInt; |
| 167 |
aLong = bLong; |
| 168 |
aLongLong = bLongLong; |
| 169 |
aFloat = bFloat; |
| 170 |
aDouble = bDouble; |
| 171 |
|
| 172 |
if (aArgv) { |
| 173 |
int i; |
| 174 |
for (i = 0; aArgv[i] != NULL; i++) { |
| 175 |
free((void *)aArgv[i]); |
| 176 |
aArgv[i] = NULL; |
| 177 |
} |
| 178 |
free(aArgv); |
| 179 |
aArgv = NULL; |
| 180 |
} |
| 181 |
if (aBits) |
| 182 |
(void) poptBitsClr(aBits); |
| 183 |
|
| 184 |
oStr = (char *) -1; |
| 185 |
|
| 186 |
singleDash = 0; |
| 187 |
pass2 = 0; |
| 188 |
} |
| 189 |
|
| 190 |
int main(int argc, const char ** argv) |
| 191 |
{ |
| 192 |
int rc; |
| 193 |
int ec = 0; |
| 194 |
poptContext optCon; |
| 195 |
const char ** rest; |
| 196 |
int help = 0; |
| 197 |
int usage = 0; |
| 198 |
|
| 199 |
#if defined(HAVE_MCHECK_H) && defined(HAVE_MTRACE) |
| 200 |
mtrace(); /* Trace malloc only if MALLOC_TRACE=mtrace-output-file. */ |
| 201 |
#endif |
| 202 |
|
| 203 |
resetVars(); |
| 204 |
optCon = poptGetContext("test1", argc, argv, options, 0); |
| 205 |
(void) poptReadConfigFile(optCon, "./test-poptrc"); |
| 206 |
(void) poptReadDefaultConfig(optCon, 1); |
| 207 |
|
| 208 |
poptSetExecPath(optCon, ".", 1); |
| 209 |
|
| 210 |
#if 1 |
| 211 |
while ((rc = poptGetNextOpt(optCon)) > 0) /* Read all the options ... */ |
| 212 |
{} |
| 213 |
|
| 214 |
poptResetContext(optCon); /* ... and then start over. */ |
| 215 |
resetVars(); |
| 216 |
#endif |
| 217 |
|
| 218 |
pass2 = 1; |
| 219 |
if ((rc = poptGetNextOpt(optCon)) < -1) { |
| 220 |
fprintf(stderr, "test1: bad argument %s: %s\n", |
| 221 |
poptBadOption(optCon, POPT_BADOPTION_NOALIAS), |
| 222 |
poptStrerror(rc)); |
| 223 |
ec = 2; |
| 224 |
goto exit; |
| 225 |
} |
| 226 |
|
| 227 |
if (help) { |
| 228 |
poptPrintHelp(optCon, stdout, 0); |
| 229 |
goto exit; |
| 230 |
} |
| 231 |
if (usage) { |
| 232 |
poptPrintUsage(optCon, stdout, 0); |
| 233 |
goto exit; |
| 234 |
} |
| 235 |
|
| 236 |
fprintf(stdout, "arg1: %d arg2: %s", arg1, arg2); |
| 237 |
|
| 238 |
if (arg3) |
| 239 |
fprintf(stdout, " arg3: %d", arg3); |
| 240 |
if (inc) |
| 241 |
fprintf(stdout, " inc: %d", inc); |
| 242 |
if (shortopt) |
| 243 |
fprintf(stdout, " short: %d", shortopt); |
| 244 |
if (aVal != bVal) |
| 245 |
fprintf(stdout, " aVal: %d", aVal); |
| 246 |
if (aFlag != bFlag) |
| 247 |
fprintf(stdout, " aFlag: 0x%x", aFlag); |
| 248 |
if (aShort != bShort) |
| 249 |
fprintf(stdout, " aShort: %hd", aShort); |
| 250 |
if (aInt != bInt) |
| 251 |
fprintf(stdout, " aInt: %d", aInt); |
| 252 |
if (aLong != bLong) |
| 253 |
fprintf(stdout, " aLong: %ld", aLong); |
| 254 |
if (aLongLong != bLongLong) |
| 255 |
fprintf(stdout, " aLongLong: %lld", aLongLong); |
| 256 |
if (aFloat != bFloat) |
| 257 |
fprintf(stdout, " aFloat: %g", (double)aFloat); |
| 258 |
if (aDouble != bDouble) |
| 259 |
fprintf(stdout, " aDouble: %g", aDouble); |
| 260 |
if (aArgv != NULL) { |
| 261 |
const char **av = aArgv; |
| 262 |
const char * arg; |
| 263 |
fprintf(stdout, " aArgv:"); |
| 264 |
while ((arg = *av++) != NULL) |
| 265 |
fprintf(stdout, " %s", arg); |
| 266 |
} |
| 267 |
if (aBits) { |
| 268 |
const char * separator = " "; |
| 269 |
size_t i; |
| 270 |
fprintf(stdout, " aBits:"); |
| 271 |
for (i = 0; i < nattributes; i++) { |
| 272 |
if (!poptBitsChk(aBits, attributes[i])) |
| 273 |
continue; |
| 274 |
fprintf(stdout, "%s%s", separator, attributes[i]); |
| 275 |
separator = ","; |
| 276 |
} |
| 277 |
} |
| 278 |
if (oStr != (char *)-1) |
| 279 |
fprintf(stdout, " oStr: %s", (oStr ? oStr : "(none)")); |
| 280 |
if (singleDash) |
| 281 |
fprintf(stdout, " -"); |
| 282 |
|
| 283 |
if (poptPeekArg(optCon) != NULL) { |
| 284 |
rest = poptGetArgs(optCon); |
| 285 |
if (rest) { |
| 286 |
fprintf(stdout, " rest:"); |
| 287 |
while (*rest) { |
| 288 |
fprintf(stdout, " %s", *rest); |
| 289 |
rest++; |
| 290 |
} |
| 291 |
} |
| 292 |
} |
| 293 |
|
| 294 |
fprintf(stdout, "\n"); |
| 295 |
|
| 296 |
exit: |
| 297 |
optCon = poptFreeContext(optCon); |
| 298 |
#if defined(HAVE_MCHECK_H) && defined(HAVE_MTRACE) |
| 299 |
muntrace(); /* Trace malloc only if MALLOC_TRACE=mtrace-output-file. */ |
| 300 |
#endif |
| 301 |
return ec; |
| 302 |
} |