sort.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  1. /*
  2. * \file sort.c
  3. *
  4. * This module implements argument sorting.
  5. *
  6. * @addtogroup autoopts
  7. * @{
  8. */
  9. /*
  10. * This file is part of AutoOpts, a companion to AutoGen.
  11. * AutoOpts is free software.
  12. * AutoOpts is Copyright (C) 1992-2014 by Bruce Korb - all rights reserved
  13. *
  14. * AutoOpts is available under any one of two licenses. The license
  15. * in use must be one of these two and the choice is under the control
  16. * of the user of the license.
  17. *
  18. * The GNU Lesser General Public License, version 3 or later
  19. * See the files "COPYING.lgplv3" and "COPYING.gplv3"
  20. *
  21. * The Modified Berkeley Software Distribution License
  22. * See the file "COPYING.mbsd"
  23. *
  24. * These files have the following sha256 sums:
  25. *
  26. * 8584710e9b04216a394078dc156b781d0b47e1729104d666658aecef8ee32e95 COPYING.gplv3
  27. * 4379e7444a0e2ce2b12dd6f5a52a27a4d02d39d247901d3285c88cf0d37f477b COPYING.lgplv3
  28. * 13aa749a5b0a454917a944ed8fffc530b784f5ead522b1aacaf4ec8aa55a6239 COPYING.mbsd
  29. */
  30. /* = = = START-STATIC-FORWARD = = = */
  31. static tSuccess
  32. must_arg(tOptions * opts, char * arg_txt, tOptState * pOS,
  33. char ** opt_txt, uint32_t * opt_idx);
  34. static tSuccess
  35. maybe_arg(tOptions * opts, char * arg_txt, tOptState * pOS,
  36. char ** opt_txt, uint32_t * opt_idx);
  37. static tSuccess
  38. short_opt_ck(tOptions * opts, char * arg_txt, tOptState * pOS,
  39. char ** opt_txt, uint32_t * opt_idx);
  40. /* = = = END-STATIC-FORWARD = = = */
  41. /*
  42. * "must_arg" and "maybe_arg" are really similar. The biggest
  43. * difference is that "may" will consume the next argument only if it
  44. * does not start with a hyphen and "must" will consume it, hyphen or not.
  45. */
  46. static tSuccess
  47. must_arg(tOptions * opts, char * arg_txt, tOptState * pOS,
  48. char ** opt_txt, uint32_t * opt_idx)
  49. {
  50. /*
  51. * An option argument is required. Long options can either have
  52. * a separate command line argument, or an argument attached by
  53. * the '=' character. Figure out which.
  54. */
  55. switch (pOS->optType) {
  56. case TOPT_SHORT:
  57. /*
  58. * See if an arg string follows the flag character. If not,
  59. * the next arg must be the option argument.
  60. */
  61. if (*arg_txt != NUL)
  62. return SUCCESS;
  63. break;
  64. case TOPT_LONG:
  65. /*
  66. * See if an arg string has already been assigned (glued on
  67. * with an `=' character). If not, the next is the opt arg.
  68. */
  69. if (pOS->pzOptArg != NULL)
  70. return SUCCESS;
  71. break;
  72. default:
  73. return FAILURE;
  74. }
  75. if (opts->curOptIdx >= opts->origArgCt)
  76. return FAILURE;
  77. opt_txt[ (*opt_idx)++ ] = opts->origArgVect[ (opts->curOptIdx)++ ];
  78. return SUCCESS;
  79. }
  80. static tSuccess
  81. maybe_arg(tOptions * opts, char * arg_txt, tOptState * pOS,
  82. char ** opt_txt, uint32_t * opt_idx)
  83. {
  84. /*
  85. * An option argument is optional.
  86. */
  87. switch (pOS->optType) {
  88. case TOPT_SHORT:
  89. /*
  90. * IF nothing is glued on after the current flag character,
  91. * THEN see if there is another argument. If so and if it
  92. * does *NOT* start with a hyphen, then it is the option arg.
  93. */
  94. if (*arg_txt != NUL)
  95. return SUCCESS;
  96. break;
  97. case TOPT_LONG:
  98. /*
  99. * Look for an argument if we don't already have one (glued on
  100. * with a `=' character)
  101. */
  102. if (pOS->pzOptArg != NULL)
  103. return SUCCESS;
  104. break;
  105. default:
  106. return FAILURE;
  107. }
  108. if (opts->curOptIdx >= opts->origArgCt)
  109. return PROBLEM;
  110. arg_txt = opts->origArgVect[ opts->curOptIdx ];
  111. if (*arg_txt != '-')
  112. opt_txt[ (*opt_idx)++ ] = opts->origArgVect[ (opts->curOptIdx)++ ];
  113. return SUCCESS;
  114. }
  115. /*
  116. * Process a string of short options glued together. If the last one
  117. * does or may take an argument, the do the argument processing and leave.
  118. */
  119. static tSuccess
  120. short_opt_ck(tOptions * opts, char * arg_txt, tOptState * pOS,
  121. char ** opt_txt, uint32_t * opt_idx)
  122. {
  123. while (*arg_txt != NUL) {
  124. if (FAILED(opt_find_short(opts, (uint8_t)*arg_txt, pOS)))
  125. return FAILURE;
  126. /*
  127. * See if we can have an arg.
  128. */
  129. if (OPTST_GET_ARGTYPE(pOS->pOD->fOptState) == OPARG_TYPE_NONE) {
  130. arg_txt++;
  131. } else if (pOS->pOD->fOptState & OPTST_ARG_OPTIONAL) {
  132. /*
  133. * Take an argument if it is not attached and it does not
  134. * start with a hyphen.
  135. */
  136. if (arg_txt[1] != NUL)
  137. return SUCCESS;
  138. arg_txt = opts->origArgVect[ opts->curOptIdx ];
  139. if (*arg_txt != '-')
  140. opt_txt[ (*opt_idx)++ ] =
  141. opts->origArgVect[ (opts->curOptIdx)++ ];
  142. return SUCCESS;
  143. } else {
  144. /*
  145. * IF we need another argument, be sure it is there and
  146. * take it.
  147. */
  148. if (arg_txt[1] == NUL) {
  149. if (opts->curOptIdx >= opts->origArgCt)
  150. return FAILURE;
  151. opt_txt[ (*opt_idx)++ ] =
  152. opts->origArgVect[ (opts->curOptIdx)++ ];
  153. }
  154. return SUCCESS;
  155. }
  156. }
  157. return SUCCESS;
  158. }
  159. /*
  160. * If the program wants sorted options (separated operands and options),
  161. * then this routine will to the trick.
  162. */
  163. LOCAL void
  164. optionSort(tOptions * opts)
  165. {
  166. char ** opt_txt;
  167. char ** ppzOpds;
  168. uint32_t optsIdx = 0;
  169. uint32_t opdsIdx = 0;
  170. tOptState os = OPTSTATE_INITIALIZER(DEFINED);
  171. /*
  172. * Disable for POSIX conformance, or if there are no operands.
  173. */
  174. if ( (getenv("POSIXLY_CORRECT") != NULL)
  175. || NAMED_OPTS(opts))
  176. return;
  177. /*
  178. * Make sure we can allocate two full-sized arg vectors.
  179. */
  180. opt_txt = malloc(opts->origArgCt * sizeof(char*));
  181. if (opt_txt == NULL)
  182. goto exit_no_mem;
  183. ppzOpds = malloc(opts->origArgCt * sizeof(char*));
  184. if (ppzOpds == NULL) {
  185. free(opt_txt);
  186. goto exit_no_mem;
  187. }
  188. opts->curOptIdx = 1;
  189. opts->pzCurOpt = NULL;
  190. /*
  191. * Now, process all the options from our current position onward.
  192. * (This allows interspersed options and arguments for the few
  193. * non-standard programs that require it.)
  194. */
  195. for (;;) {
  196. char * arg_txt;
  197. tSuccess res;
  198. /*
  199. * If we're out of arguments, we're done. Join the option and
  200. * operand lists into the original argument vector.
  201. */
  202. if (opts->curOptIdx >= opts->origArgCt) {
  203. errno = 0;
  204. goto joinLists;
  205. }
  206. arg_txt = opts->origArgVect[ opts->curOptIdx ];
  207. if (*arg_txt != '-') {
  208. ppzOpds[ opdsIdx++ ] = opts->origArgVect[ (opts->curOptIdx)++ ];
  209. continue;
  210. }
  211. switch (arg_txt[1]) {
  212. case NUL:
  213. /*
  214. * A single hyphen is an operand.
  215. */
  216. ppzOpds[ opdsIdx++ ] = opts->origArgVect[ (opts->curOptIdx)++ ];
  217. continue;
  218. case '-':
  219. /*
  220. * Two consecutive hypens. Put them on the options list and then
  221. * _always_ force the remainder of the arguments to be operands.
  222. */
  223. if (arg_txt[2] == NUL) {
  224. opt_txt[ optsIdx++ ] =
  225. opts->origArgVect[ (opts->curOptIdx)++ ];
  226. goto restOperands;
  227. }
  228. res = opt_find_long(opts, arg_txt+2, &os);
  229. break;
  230. default:
  231. /*
  232. * If short options are not allowed, then do long
  233. * option processing. Otherwise the character must be a
  234. * short (i.e. single character) option.
  235. */
  236. if ((opts->fOptSet & OPTPROC_SHORTOPT) == 0) {
  237. res = opt_find_long(opts, arg_txt+1, &os);
  238. } else {
  239. res = opt_find_short(opts, (uint8_t)arg_txt[1], &os);
  240. }
  241. break;
  242. }
  243. if (FAILED(res)) {
  244. errno = EINVAL;
  245. goto freeTemps;
  246. }
  247. /*
  248. * We've found an option. Add the argument to the option list.
  249. * Next, we have to see if we need to pull another argument to be
  250. * used as the option argument.
  251. */
  252. opt_txt[ optsIdx++ ] = opts->origArgVect[ (opts->curOptIdx)++ ];
  253. if (OPTST_GET_ARGTYPE(os.pOD->fOptState) == OPARG_TYPE_NONE) {
  254. /*
  255. * No option argument. If we have a short option here,
  256. * then scan for short options until we get to the end
  257. * of the argument string.
  258. */
  259. if ( (os.optType == TOPT_SHORT)
  260. && FAILED(short_opt_ck(opts, arg_txt+2, &os, opt_txt,
  261. &optsIdx)) ) {
  262. errno = EINVAL;
  263. goto freeTemps;
  264. }
  265. } else if (os.pOD->fOptState & OPTST_ARG_OPTIONAL) {
  266. switch (maybe_arg(opts, arg_txt+2, &os, opt_txt, &optsIdx)) {
  267. case FAILURE: errno = EIO; goto freeTemps;
  268. case PROBLEM: errno = 0; goto joinLists;
  269. }
  270. } else {
  271. switch (must_arg(opts, arg_txt+2, &os, opt_txt, &optsIdx)) {
  272. case PROBLEM:
  273. case FAILURE: errno = EIO; goto freeTemps;
  274. }
  275. }
  276. } /* for (;;) */
  277. restOperands:
  278. while (opts->curOptIdx < opts->origArgCt)
  279. ppzOpds[ opdsIdx++ ] = opts->origArgVect[ (opts->curOptIdx)++ ];
  280. joinLists:
  281. if (optsIdx > 0)
  282. memcpy(opts->origArgVect + 1, opt_txt,
  283. (size_t)optsIdx * sizeof(char*));
  284. if (opdsIdx > 0)
  285. memcpy(opts->origArgVect + 1 + optsIdx, ppzOpds,
  286. (size_t)opdsIdx * sizeof(char*));
  287. freeTemps:
  288. free(opt_txt);
  289. free(ppzOpds);
  290. return;
  291. exit_no_mem:
  292. errno = ENOMEM;
  293. return;
  294. }
  295. /** @}
  296. *
  297. * Local Variables:
  298. * mode: C
  299. * c-file-style: "stroustrup"
  300. * indent-tabs-mode: nil
  301. * End:
  302. * end of autoopts/sort.c */