sort.c 9.4 KB

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