19f07849eSopenharmony_ci/* (C) 1998-2002 Red Hat, Inc. -- Licensing details are in the COPYING 29f07849eSopenharmony_ci file accompanying popt source distributions, available from 39f07849eSopenharmony_ci ftp://ftp.rpm.org/pub/rpm/dist. */ 49f07849eSopenharmony_ci 59f07849eSopenharmony_ci#include "system.h" 69f07849eSopenharmony_ci 79f07849eSopenharmony_cistatic int pass2 = 0; 89f07849eSopenharmony_cistatic void option_callback(UNUSED(poptContext con), 99f07849eSopenharmony_ci UNUSED(enum poptCallbackReason reason), 109f07849eSopenharmony_ci const struct poptOption * opt, 119f07849eSopenharmony_ci char * arg, void * data) 129f07849eSopenharmony_ci{ 139f07849eSopenharmony_ci if (pass2) 149f07849eSopenharmony_ci fprintf(stdout, "callback: %c %s %s ", opt->val, (char *) data, arg); 159f07849eSopenharmony_ci} 169f07849eSopenharmony_ci 179f07849eSopenharmony_cistatic int arg1 = 0; 189f07849eSopenharmony_cistatic const char * arg2 = "(none)"; 199f07849eSopenharmony_cistatic int arg3 = 0; 209f07849eSopenharmony_cistatic int inc = 0; 219f07849eSopenharmony_cistatic int shortopt = 0; 229f07849eSopenharmony_ci 239f07849eSopenharmony_cistatic int aVal = 141421; 249f07849eSopenharmony_cistatic int bVal = 141421; 259f07849eSopenharmony_cistatic unsigned int aFlag = 0x8aceU; 269f07849eSopenharmony_cistatic unsigned int bFlag = 0x8aceU; 279f07849eSopenharmony_ci 289f07849eSopenharmony_cistatic short aShort = (short)4523; 299f07849eSopenharmony_cistatic short bShort = (short)4523; 309f07849eSopenharmony_cistatic int aInt = 271828; 319f07849eSopenharmony_cistatic int bInt = 271828; 329f07849eSopenharmony_cistatic long aLong = 738905609L; 339f07849eSopenharmony_cistatic long bLong = 738905609L; 349f07849eSopenharmony_cistatic long long aLongLong = 738905609LL; 359f07849eSopenharmony_cistatic long long bLongLong = 738905609LL; 369f07849eSopenharmony_cistatic float aFloat = 3.1415926535f; 379f07849eSopenharmony_cistatic float bFloat = 3.1415926535f; 389f07849eSopenharmony_cistatic double aDouble = 9.86960440108935861883; 399f07849eSopenharmony_cistatic double bDouble = 9.86960440108935861883; 409f07849eSopenharmony_ci 419f07849eSopenharmony_cistatic const char ** aArgv = NULL; 429f07849eSopenharmony_cistatic void * aBits = NULL; 439f07849eSopenharmony_cistatic const char *attributes[] = { 449f07849eSopenharmony_ci "foo", "bar", "baz", "bing", "bang", "boom" 459f07849eSopenharmony_ci}; 469f07849eSopenharmony_cistatic size_t nattributes = (sizeof(attributes) / sizeof(attributes[0])); 479f07849eSopenharmony_ci 489f07849eSopenharmony_cistatic char * oStr = (char *) -1; 499f07849eSopenharmony_cistatic int singleDash = 0; 509f07849eSopenharmony_ci 519f07849eSopenharmony_cistatic const char * lStr = 529f07849eSopenharmony_ci"This tests default strings and exceeds the ... limit. " 539f07849eSopenharmony_ci"123456789+123456789+123456789+123456789+123456789+ " 549f07849eSopenharmony_ci"123456789+123456789+123456789+123456789+123456789+ " 559f07849eSopenharmony_ci"123456789+123456789+123456789+123456789+123456789+ " 569f07849eSopenharmony_ci"123456789+123456789+123456789+123456789+123456789+ "; 579f07849eSopenharmony_cistatic char * nStr = NULL; 589f07849eSopenharmony_ci 599f07849eSopenharmony_cistatic struct poptOption moreCallbackArgs[] = { 609f07849eSopenharmony_ci { NULL, '\0', POPT_ARG_CALLBACK|POPT_CBFLAG_INC_DATA, 619f07849eSopenharmony_ci (void *)option_callback, 0, 629f07849eSopenharmony_ci NULL, NULL }, 639f07849eSopenharmony_ci { "cb2", 'c', POPT_ARG_STRING, NULL, (int)'c', 649f07849eSopenharmony_ci "Test argument callbacks", NULL }, 659f07849eSopenharmony_ci POPT_TABLEEND 669f07849eSopenharmony_ci}; 679f07849eSopenharmony_ci 689f07849eSopenharmony_cistatic struct poptOption callbackArgs[] = { 699f07849eSopenharmony_ci { NULL, '\0', POPT_ARG_CALLBACK, (void *)option_callback, 0, 709f07849eSopenharmony_ci "sampledata", NULL }, 719f07849eSopenharmony_ci { "cb", 'c', POPT_ARG_STRING, NULL, (int)'c', 729f07849eSopenharmony_ci "Test argument callbacks", NULL }, 739f07849eSopenharmony_ci { "longopt", '\0', 0, NULL, (int)'l', 749f07849eSopenharmony_ci "Unused option for help testing", NULL }, 759f07849eSopenharmony_ci POPT_TABLEEND 769f07849eSopenharmony_ci}; 779f07849eSopenharmony_ci 789f07849eSopenharmony_cistatic struct poptOption moreArgs[] = { 799f07849eSopenharmony_ci { "inc", 'I', 0, &inc, 0, "An included argument", NULL }, 809f07849eSopenharmony_ci POPT_TABLEEND 819f07849eSopenharmony_ci}; 829f07849eSopenharmony_ci 839f07849eSopenharmony_cistatic struct poptOption options[] = { 849f07849eSopenharmony_ci { NULL, '\0', POPT_ARG_INCLUDE_TABLE, &moreCallbackArgs, 0, 859f07849eSopenharmony_ci "arg for cb2", NULL }, 869f07849eSopenharmony_ci { "arg1", '\0', 0, &arg1, 0, "First argument with a really long" 879f07849eSopenharmony_ci " description. After all, we have to test argument help" 889f07849eSopenharmony_ci " wrapping somehow, right?", NULL }, 899f07849eSopenharmony_ci { "arg2", '2', POPT_ARG_STRING | POPT_ARGFLAG_SHOW_DEFAULT, &arg2, 0, 909f07849eSopenharmony_ci "Another argument", "ARG" }, 919f07849eSopenharmony_ci { "arg3", '3', POPT_ARG_INT, &arg3, 0, 929f07849eSopenharmony_ci "A third argument", "ANARG" }, 939f07849eSopenharmony_ci { "onedash", '\0', POPT_ARGFLAG_ONEDASH, &shortopt, 0, 949f07849eSopenharmony_ci "POPT_ARGFLAG_ONEDASH: Option takes a single -", NULL }, 959f07849eSopenharmony_ci { "hidden", '\0', POPT_ARG_STRING | POPT_ARGFLAG_DOC_HIDDEN, NULL, 0, 969f07849eSopenharmony_ci "POPT_ARGFLAG_HIDDEN: A hidden option (--help shouldn't display)", 979f07849eSopenharmony_ci NULL }, 989f07849eSopenharmony_ci { "optional", '\0', POPT_ARG_STRING | POPT_ARGFLAG_OPTIONAL, &oStr, 0, 999f07849eSopenharmony_ci "POPT_ARGFLAG_OPTIONAL: Takes an optional string argument", NULL }, 1009f07849eSopenharmony_ci 1019f07849eSopenharmony_ci { "val", '\0', POPT_ARG_VAL | POPT_ARGFLAG_SHOW_DEFAULT, &aVal, 125992, 1029f07849eSopenharmony_ci "POPT_ARG_VAL: 125992 141421", 0}, 1039f07849eSopenharmony_ci 1049f07849eSopenharmony_ci { "int", 'i', POPT_ARG_INT | POPT_ARGFLAG_SHOW_DEFAULT, &aInt, 0, 1059f07849eSopenharmony_ci "POPT_ARG_INT: 271828", NULL }, 1069f07849eSopenharmony_ci { "short", 's', POPT_ARG_SHORT | POPT_ARGFLAG_SHOW_DEFAULT, &aShort, 0, 1079f07849eSopenharmony_ci "POPT_ARG_SHORT: 4523", NULL }, 1089f07849eSopenharmony_ci { "long", 'l', POPT_ARG_LONG | POPT_ARGFLAG_SHOW_DEFAULT, &aLong, 0, 1099f07849eSopenharmony_ci "POPT_ARG_LONG: 738905609", NULL }, 1109f07849eSopenharmony_ci { "longlong", 'L', POPT_ARG_LONGLONG | POPT_ARGFLAG_SHOW_DEFAULT, &aLongLong, 0, 1119f07849eSopenharmony_ci "POPT_ARG_LONGLONG: 738905609", NULL }, 1129f07849eSopenharmony_ci { "float", 'f', POPT_ARG_FLOAT | POPT_ARGFLAG_SHOW_DEFAULT, &aFloat, 0, 1139f07849eSopenharmony_ci "POPT_ARG_FLOAT: 3.14159", NULL }, 1149f07849eSopenharmony_ci { "double", 'd', POPT_ARG_DOUBLE | POPT_ARGFLAG_SHOW_DEFAULT, &aDouble, 0, 1159f07849eSopenharmony_ci "POPT_ARG_DOUBLE: 9.8696", NULL }, 1169f07849eSopenharmony_ci 1179f07849eSopenharmony_ci { "randint", '\0', POPT_ARG_INT|POPT_ARGFLAG_RANDOM, &aInt, 0, 1189f07849eSopenharmony_ci "POPT_ARGFLAG_RANDOM: experimental", NULL }, 1199f07849eSopenharmony_ci { "randshort", '\0', POPT_ARG_SHORT|POPT_ARGFLAG_RANDOM, &aShort, 0, 1209f07849eSopenharmony_ci "POPT_ARGFLAG_RANDOM: experimental", NULL }, 1219f07849eSopenharmony_ci { "randlong", '\0', POPT_ARG_LONG|POPT_ARGFLAG_RANDOM, &aLong, 0, 1229f07849eSopenharmony_ci "POPT_ARGFLAG_RANDOM: experimental", NULL }, 1239f07849eSopenharmony_ci { "randlonglong", '\0', POPT_ARG_LONGLONG|POPT_ARGFLAG_RANDOM, &aLongLong, 0, 1249f07849eSopenharmony_ci "POPT_ARGFLAG_RANDOM: experimental", NULL }, 1259f07849eSopenharmony_ci 1269f07849eSopenharmony_ci { "argv", '\0', POPT_ARG_ARGV, &aArgv, 0, 1279f07849eSopenharmony_ci "POPT_ARG_ARGV: append string to argv array (can be used multiple times)","STRING"}, 1289f07849eSopenharmony_ci { "bits", '\0', POPT_ARG_BITSET|POPT_ARGFLAG_DOC_HIDDEN, &aBits, 0, 1299f07849eSopenharmony_ci "POPT_ARG_BITSET: add string to bit set (can be used multiple times)","STRING"}, 1309f07849eSopenharmony_ci 1319f07849eSopenharmony_ci { "bitset", '\0', POPT_BIT_SET | POPT_ARGFLAG_TOGGLE | POPT_ARGFLAG_SHOW_DEFAULT, &aFlag, 0x7777, 1329f07849eSopenharmony_ci "POPT_BIT_SET: |= 0x7777", 0}, 1339f07849eSopenharmony_ci { "bitclr", '\0', POPT_BIT_CLR | POPT_ARGFLAG_TOGGLE | POPT_ARGFLAG_SHOW_DEFAULT, &aFlag, 0xf842, 1349f07849eSopenharmony_ci "POPT_BIT_CLR: &= ~0xf842", 0}, 1359f07849eSopenharmony_ci { "bitxor", 'x', POPT_ARG_VAL | POPT_ARGFLAG_XOR | POPT_ARGFLAG_SHOW_DEFAULT, &aFlag, (0x8ace^0xfeed), 1369f07849eSopenharmony_ci "POPT_ARGFLAG_XOR: ^= (0x8ace^0xfeed)", 0}, 1379f07849eSopenharmony_ci 1389f07849eSopenharmony_ci { "nstr", '\0', POPT_ARG_STRING | POPT_ARGFLAG_SHOW_DEFAULT, &nStr, 0, 1399f07849eSopenharmony_ci "POPT_ARG_STRING: (null)", NULL}, 1409f07849eSopenharmony_ci { "lstr", '\0', POPT_ARG_STRING | POPT_ARGFLAG_SHOW_DEFAULT, &lStr, 0, 1419f07849eSopenharmony_ci "POPT_ARG_STRING: \"123456789...\"", NULL}, 1429f07849eSopenharmony_ci 1439f07849eSopenharmony_ci { NULL, '-', POPT_ARG_NONE | POPT_ARGFLAG_DOC_HIDDEN, &singleDash, 0, 1449f07849eSopenharmony_ci NULL, NULL }, 1459f07849eSopenharmony_ci { NULL, '\0', POPT_ARG_INCLUDE_TABLE, &moreArgs, 0, 1469f07849eSopenharmony_ci NULL, NULL }, 1479f07849eSopenharmony_ci { NULL, '\0', POPT_ARG_INCLUDE_TABLE, &callbackArgs, 0, 1489f07849eSopenharmony_ci "Callback arguments", NULL }, 1499f07849eSopenharmony_ci POPT_AUTOALIAS 1509f07849eSopenharmony_ci POPT_AUTOHELP 1519f07849eSopenharmony_ci POPT_TABLEEND 1529f07849eSopenharmony_ci}; 1539f07849eSopenharmony_ci 1549f07849eSopenharmony_cistatic void resetVars(void) 1559f07849eSopenharmony_ci{ 1569f07849eSopenharmony_ci arg1 = 0; 1579f07849eSopenharmony_ci arg2 = "(none)"; 1589f07849eSopenharmony_ci arg3 = 0; 1599f07849eSopenharmony_ci inc = 0; 1609f07849eSopenharmony_ci shortopt = 0; 1619f07849eSopenharmony_ci 1629f07849eSopenharmony_ci aVal = bVal; 1639f07849eSopenharmony_ci aFlag = bFlag; 1649f07849eSopenharmony_ci 1659f07849eSopenharmony_ci aShort = bShort; 1669f07849eSopenharmony_ci aInt = bInt; 1679f07849eSopenharmony_ci aLong = bLong; 1689f07849eSopenharmony_ci aLongLong = bLongLong; 1699f07849eSopenharmony_ci aFloat = bFloat; 1709f07849eSopenharmony_ci aDouble = bDouble; 1719f07849eSopenharmony_ci 1729f07849eSopenharmony_ci if (aArgv) { 1739f07849eSopenharmony_ci int i; 1749f07849eSopenharmony_ci for (i = 0; aArgv[i] != NULL; i++) { 1759f07849eSopenharmony_ci free((void *)aArgv[i]); 1769f07849eSopenharmony_ci aArgv[i] = NULL; 1779f07849eSopenharmony_ci } 1789f07849eSopenharmony_ci free(aArgv); 1799f07849eSopenharmony_ci aArgv = NULL; 1809f07849eSopenharmony_ci } 1819f07849eSopenharmony_ci if (aBits) 1829f07849eSopenharmony_ci (void) poptBitsClr(aBits); 1839f07849eSopenharmony_ci 1849f07849eSopenharmony_ci oStr = (char *) -1; 1859f07849eSopenharmony_ci 1869f07849eSopenharmony_ci singleDash = 0; 1879f07849eSopenharmony_ci pass2 = 0; 1889f07849eSopenharmony_ci} 1899f07849eSopenharmony_ci 1909f07849eSopenharmony_ciint main(int argc, const char ** argv) 1919f07849eSopenharmony_ci{ 1929f07849eSopenharmony_ci int rc; 1939f07849eSopenharmony_ci int ec = 0; 1949f07849eSopenharmony_ci poptContext optCon; 1959f07849eSopenharmony_ci const char ** rest; 1969f07849eSopenharmony_ci int help = 0; 1979f07849eSopenharmony_ci int usage = 0; 1989f07849eSopenharmony_ci 1999f07849eSopenharmony_ci#if defined(HAVE_MCHECK_H) && defined(HAVE_MTRACE) 2009f07849eSopenharmony_ci mtrace(); /* Trace malloc only if MALLOC_TRACE=mtrace-output-file. */ 2019f07849eSopenharmony_ci#endif 2029f07849eSopenharmony_ci 2039f07849eSopenharmony_ci resetVars(); 2049f07849eSopenharmony_ci optCon = poptGetContext("test1", argc, argv, options, 0); 2059f07849eSopenharmony_ci (void) poptReadConfigFile(optCon, "./test-poptrc"); 2069f07849eSopenharmony_ci (void) poptReadDefaultConfig(optCon, 1); 2079f07849eSopenharmony_ci 2089f07849eSopenharmony_ci poptSetExecPath(optCon, ".", 1); 2099f07849eSopenharmony_ci 2109f07849eSopenharmony_ci#if 1 2119f07849eSopenharmony_ci while ((rc = poptGetNextOpt(optCon)) > 0) /* Read all the options ... */ 2129f07849eSopenharmony_ci {} 2139f07849eSopenharmony_ci 2149f07849eSopenharmony_ci poptResetContext(optCon); /* ... and then start over. */ 2159f07849eSopenharmony_ci resetVars(); 2169f07849eSopenharmony_ci#endif 2179f07849eSopenharmony_ci 2189f07849eSopenharmony_ci pass2 = 1; 2199f07849eSopenharmony_ci if ((rc = poptGetNextOpt(optCon)) < -1) { 2209f07849eSopenharmony_ci fprintf(stderr, "test1: bad argument %s: %s\n", 2219f07849eSopenharmony_ci poptBadOption(optCon, POPT_BADOPTION_NOALIAS), 2229f07849eSopenharmony_ci poptStrerror(rc)); 2239f07849eSopenharmony_ci ec = 2; 2249f07849eSopenharmony_ci goto exit; 2259f07849eSopenharmony_ci } 2269f07849eSopenharmony_ci 2279f07849eSopenharmony_ci if (help) { 2289f07849eSopenharmony_ci poptPrintHelp(optCon, stdout, 0); 2299f07849eSopenharmony_ci goto exit; 2309f07849eSopenharmony_ci } 2319f07849eSopenharmony_ci if (usage) { 2329f07849eSopenharmony_ci poptPrintUsage(optCon, stdout, 0); 2339f07849eSopenharmony_ci goto exit; 2349f07849eSopenharmony_ci } 2359f07849eSopenharmony_ci 2369f07849eSopenharmony_ci fprintf(stdout, "arg1: %d arg2: %s", arg1, arg2); 2379f07849eSopenharmony_ci 2389f07849eSopenharmony_ci if (arg3) 2399f07849eSopenharmony_ci fprintf(stdout, " arg3: %d", arg3); 2409f07849eSopenharmony_ci if (inc) 2419f07849eSopenharmony_ci fprintf(stdout, " inc: %d", inc); 2429f07849eSopenharmony_ci if (shortopt) 2439f07849eSopenharmony_ci fprintf(stdout, " short: %d", shortopt); 2449f07849eSopenharmony_ci if (aVal != bVal) 2459f07849eSopenharmony_ci fprintf(stdout, " aVal: %d", aVal); 2469f07849eSopenharmony_ci if (aFlag != bFlag) 2479f07849eSopenharmony_ci fprintf(stdout, " aFlag: 0x%x", aFlag); 2489f07849eSopenharmony_ci if (aShort != bShort) 2499f07849eSopenharmony_ci fprintf(stdout, " aShort: %hd", aShort); 2509f07849eSopenharmony_ci if (aInt != bInt) 2519f07849eSopenharmony_ci fprintf(stdout, " aInt: %d", aInt); 2529f07849eSopenharmony_ci if (aLong != bLong) 2539f07849eSopenharmony_ci fprintf(stdout, " aLong: %ld", aLong); 2549f07849eSopenharmony_ci if (aLongLong != bLongLong) 2559f07849eSopenharmony_ci fprintf(stdout, " aLongLong: %lld", aLongLong); 2569f07849eSopenharmony_ci if (aFloat != bFloat) 2579f07849eSopenharmony_ci fprintf(stdout, " aFloat: %g", (double)aFloat); 2589f07849eSopenharmony_ci if (aDouble != bDouble) 2599f07849eSopenharmony_ci fprintf(stdout, " aDouble: %g", aDouble); 2609f07849eSopenharmony_ci if (aArgv != NULL) { 2619f07849eSopenharmony_ci const char **av = aArgv; 2629f07849eSopenharmony_ci const char * arg; 2639f07849eSopenharmony_ci fprintf(stdout, " aArgv:"); 2649f07849eSopenharmony_ci while ((arg = *av++) != NULL) 2659f07849eSopenharmony_ci fprintf(stdout, " %s", arg); 2669f07849eSopenharmony_ci } 2679f07849eSopenharmony_ci if (aBits) { 2689f07849eSopenharmony_ci const char * separator = " "; 2699f07849eSopenharmony_ci size_t i; 2709f07849eSopenharmony_ci fprintf(stdout, " aBits:"); 2719f07849eSopenharmony_ci for (i = 0; i < nattributes; i++) { 2729f07849eSopenharmony_ci if (!poptBitsChk(aBits, attributes[i])) 2739f07849eSopenharmony_ci continue; 2749f07849eSopenharmony_ci fprintf(stdout, "%s%s", separator, attributes[i]); 2759f07849eSopenharmony_ci separator = ","; 2769f07849eSopenharmony_ci } 2779f07849eSopenharmony_ci } 2789f07849eSopenharmony_ci if (oStr != (char *)-1) 2799f07849eSopenharmony_ci fprintf(stdout, " oStr: %s", (oStr ? oStr : "(none)")); 2809f07849eSopenharmony_ci if (singleDash) 2819f07849eSopenharmony_ci fprintf(stdout, " -"); 2829f07849eSopenharmony_ci 2839f07849eSopenharmony_ci if (poptPeekArg(optCon) != NULL) { 2849f07849eSopenharmony_ci rest = poptGetArgs(optCon); 2859f07849eSopenharmony_ci if (rest) { 2869f07849eSopenharmony_ci fprintf(stdout, " rest:"); 2879f07849eSopenharmony_ci while (*rest) { 2889f07849eSopenharmony_ci fprintf(stdout, " %s", *rest); 2899f07849eSopenharmony_ci rest++; 2909f07849eSopenharmony_ci } 2919f07849eSopenharmony_ci } 2929f07849eSopenharmony_ci } 2939f07849eSopenharmony_ci 2949f07849eSopenharmony_ci fprintf(stdout, "\n"); 2959f07849eSopenharmony_ci 2969f07849eSopenharmony_ciexit: 2979f07849eSopenharmony_ci optCon = poptFreeContext(optCon); 2989f07849eSopenharmony_ci#if defined(HAVE_MCHECK_H) && defined(HAVE_MTRACE) 2999f07849eSopenharmony_ci muntrace(); /* Trace malloc only if MALLOC_TRACE=mtrace-output-file. */ 3009f07849eSopenharmony_ci#endif 3019f07849eSopenharmony_ci return ec; 3029f07849eSopenharmony_ci} 303