test1.c 10 KB

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