sort.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  1. /*
  2. * sort.c $Id: sort.c,v 4.10 2007/04/28 22:19:23 bkorb Exp $
  3. * Time-stamp: "2006-10-18 11:29:04 bkorb"
  4. *
  5. * This module implements argument sorting.
  6. */
  7. /*
  8. * Automated Options copyright 1992-2007 Bruce Korb
  9. *
  10. * Automated Options is free software.
  11. * You may redistribute it and/or modify it under the terms of the
  12. * GNU General Public License, as published by the Free Software
  13. * Foundation; either version 2, or (at your option) any later version.
  14. *
  15. * Automated Options is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with Automated Options. See the file "COPYING". If not,
  22. * write to: The Free Software Foundation, Inc.,
  23. * 51 Franklin Street, Fifth Floor,
  24. * Boston, MA 02110-1301, USA.
  25. *
  26. * As a special exception, Bruce Korb gives permission for additional
  27. * uses of the text contained in his release of AutoOpts.
  28. *
  29. * The exception is that, if you link the AutoOpts library with other
  30. * files to produce an executable, this does not by itself cause the
  31. * resulting executable to be covered by the GNU General Public License.
  32. * Your use of that executable is in no way restricted on account of
  33. * linking the AutoOpts library code into it.
  34. *
  35. * This exception does not however invalidate any other reasons why
  36. * the executable file might be covered by the GNU General Public License.
  37. *
  38. * This exception applies only to the code released by Bruce Korb under
  39. * the name AutoOpts. If you copy code from other sources under the
  40. * General Public License into a copy of AutoOpts, as the General Public
  41. * License permits, the exception does not apply to the code that you add
  42. * in this way. To avoid misleading anyone as to the status of such
  43. * modified files, you must delete this exception notice from them.
  44. *
  45. * If you write modifications of your own for AutoOpts, it is your choice
  46. * whether to permit this exception to apply to your modifications.
  47. * If you do not wish that, delete this exception notice.
  48. */
  49. /* = = = START-STATIC-FORWARD = = = */
  50. /* static forward declarations maintained by :mkfwd */
  51. static tSuccess
  52. mustHandleArg( tOptions* pOpts, char* pzArg, tOptState* pOS,
  53. char** ppzOpts, int* pOptsIdx );
  54. static tSuccess
  55. mayHandleArg( tOptions* pOpts, char* pzArg, tOptState* pOS,
  56. char** ppzOpts, int* pOptsIdx );
  57. static tSuccess
  58. checkShortOpts( tOptions* pOpts, char* pzArg, tOptState* pOS,
  59. char** ppzOpts, int* pOptsIdx );
  60. /* = = = END-STATIC-FORWARD = = = */
  61. /*
  62. * "mustHandleArg" and "mayHandleArg" are really similar. The biggest
  63. * difference is that "may" will consume the next argument only if it
  64. * does not start with a hyphen and "must" will consume it, hyphen or not.
  65. */
  66. static tSuccess
  67. mustHandleArg( tOptions* pOpts, char* pzArg, tOptState* pOS,
  68. char** ppzOpts, int* pOptsIdx )
  69. {
  70. /*
  71. * An option argument is required. Long options can either have
  72. * a separate command line argument, or an argument attached by
  73. * the '=' character. Figure out which.
  74. */
  75. switch (pOS->optType) {
  76. case TOPT_SHORT:
  77. /*
  78. * See if an arg string follows the flag character. If not,
  79. * the next arg must be the option argument.
  80. */
  81. if (*pzArg != NUL)
  82. return SUCCESS;
  83. break;
  84. case TOPT_LONG:
  85. /*
  86. * See if an arg string has already been assigned (glued on
  87. * with an `=' character). If not, the next is the opt arg.
  88. */
  89. if (pOS->pzOptArg != NULL)
  90. return SUCCESS;
  91. break;
  92. default:
  93. return FAILURE;
  94. }
  95. if (pOpts->curOptIdx >= pOpts->origArgCt)
  96. return FAILURE;
  97. ppzOpts[ (*pOptsIdx)++ ] = pOpts->origArgVect[ (pOpts->curOptIdx)++ ];
  98. return SUCCESS;
  99. }
  100. static tSuccess
  101. mayHandleArg( tOptions* pOpts, char* pzArg, tOptState* pOS,
  102. char** ppzOpts, int* pOptsIdx )
  103. {
  104. /*
  105. * An option argument is optional.
  106. */
  107. switch (pOS->optType) {
  108. case TOPT_SHORT:
  109. /*
  110. * IF nothing is glued on after the current flag character,
  111. * THEN see if there is another argument. If so and if it
  112. * does *NOT* start with a hyphen, then it is the option arg.
  113. */
  114. if (*pzArg != NUL)
  115. return SUCCESS;
  116. break;
  117. case TOPT_LONG:
  118. /*
  119. * Look for an argument if we don't already have one (glued on
  120. * with a `=' character)
  121. */
  122. if (pOS->pzOptArg != NULL)
  123. return SUCCESS;
  124. break;
  125. default:
  126. return FAILURE;
  127. }
  128. if (pOpts->curOptIdx >= pOpts->origArgCt)
  129. return PROBLEM;
  130. pzArg = pOpts->origArgVect[ pOpts->curOptIdx ];
  131. if (*pzArg != '-')
  132. ppzOpts[ (*pOptsIdx)++ ] = pOpts->origArgVect[ (pOpts->curOptIdx)++ ];
  133. return SUCCESS;
  134. }
  135. /*
  136. * Process a string of short options glued together. If the last one
  137. * does or may take an argument, the do the argument processing and leave.
  138. */
  139. static tSuccess
  140. checkShortOpts( tOptions* pOpts, char* pzArg, tOptState* pOS,
  141. char** ppzOpts, int* pOptsIdx )
  142. {
  143. while (*pzArg != NUL) {
  144. if (FAILED( shortOptionFind( pOpts, (tAoUC)*pzArg, pOS )))
  145. return FAILURE;
  146. /*
  147. * See if we can have an arg.
  148. */
  149. if (OPTST_GET_ARGTYPE(pOS->pOD->fOptState) == OPARG_TYPE_NONE) {
  150. pzArg++;
  151. } else if (pOS->pOD->fOptState & OPTST_ARG_OPTIONAL) {
  152. /*
  153. * Take an argument if it is not attached and it does not
  154. * start with a hyphen.
  155. */
  156. if (pzArg[1] != NUL)
  157. return SUCCESS;
  158. pzArg = pOpts->origArgVect[ pOpts->curOptIdx ];
  159. if (*pzArg != '-')
  160. ppzOpts[ (*pOptsIdx)++ ] =
  161. pOpts->origArgVect[ (pOpts->curOptIdx)++ ];
  162. return SUCCESS;
  163. } else {
  164. /*
  165. * IF we need another argument, be sure it is there and
  166. * take it.
  167. */
  168. if (pzArg[1] == NUL) {
  169. if (pOpts->curOptIdx >= pOpts->origArgCt)
  170. return FAILURE;
  171. ppzOpts[ (*pOptsIdx)++ ] =
  172. pOpts->origArgVect[ (pOpts->curOptIdx)++ ];
  173. }
  174. return SUCCESS;
  175. }
  176. }
  177. return SUCCESS;
  178. }
  179. /*
  180. * If the program wants sorted options (separated operands and options),
  181. * then this routine will to the trick.
  182. */
  183. LOCAL void
  184. optionSort( tOptions* pOpts )
  185. {
  186. char** ppzOpts;
  187. char** ppzOpds;
  188. int optsIdx = 0;
  189. int opdsIdx = 0;
  190. tOptState os = OPTSTATE_INITIALIZER(DEFINED);
  191. /*
  192. * Disable for POSIX conformance, or if there are no operands.
  193. */
  194. if ( (getenv( "POSIXLY_CORRECT" ) != NULL)
  195. || NAMED_OPTS(pOpts))
  196. return;
  197. /*
  198. * Make sure we can allocate two full-sized arg vectors.
  199. */
  200. ppzOpts = malloc( pOpts->origArgCt * sizeof( char* ));
  201. if (ppzOpts == NULL)
  202. goto exit_no_mem;
  203. ppzOpds = malloc( pOpts->origArgCt * sizeof( char* ));
  204. if (ppzOpds == NULL) {
  205. free( ppzOpts );
  206. goto exit_no_mem;
  207. }
  208. pOpts->curOptIdx = 1;
  209. pOpts->pzCurOpt = NULL;
  210. /*
  211. * Now, process all the options from our current position onward.
  212. * (This allows interspersed options and arguments for the few
  213. * non-standard programs that require it.)
  214. */
  215. for (;;) {
  216. char* pzArg;
  217. tSuccess res;
  218. /*
  219. * If we're out of arguments, we're done. Join the option and
  220. * operand lists into the original argument vector.
  221. */
  222. if (pOpts->curOptIdx >= pOpts->origArgCt) {
  223. errno = 0;
  224. goto joinLists;
  225. }
  226. pzArg = pOpts->origArgVect[ pOpts->curOptIdx ];
  227. if (*pzArg != '-') {
  228. ppzOpds[ opdsIdx++ ] = pOpts->origArgVect[ (pOpts->curOptIdx)++ ];
  229. continue;
  230. }
  231. switch (pzArg[1]) {
  232. case NUL:
  233. /*
  234. * A single hyphen is an operand.
  235. */
  236. ppzOpds[ opdsIdx++ ] = pOpts->origArgVect[ (pOpts->curOptIdx)++ ];
  237. continue;
  238. case '-':
  239. /*
  240. * Two consecutive hypens. Put them on the options list and then
  241. * _always_ force the remainder of the arguments to be operands.
  242. */
  243. if (pzArg[2] == NUL) {
  244. ppzOpts[ optsIdx++ ] =
  245. pOpts->origArgVect[ (pOpts->curOptIdx)++ ];
  246. goto restOperands;
  247. }
  248. res = longOptionFind( pOpts, pzArg+2, &os );
  249. break;
  250. default:
  251. /*
  252. * If short options are not allowed, then do long
  253. * option processing. Otherwise the character must be a
  254. * short (i.e. single character) option.
  255. */
  256. if ((pOpts->fOptSet & OPTPROC_SHORTOPT) == 0) {
  257. res = longOptionFind( pOpts, pzArg+1, &os );
  258. } else {
  259. res = shortOptionFind( pOpts, (tAoUC)pzArg[1], &os );
  260. }
  261. break;
  262. }
  263. if (FAILED( res )) {
  264. errno = EINVAL;
  265. goto freeTemps;
  266. }
  267. /*
  268. * We've found an option. Add the argument to the option list.
  269. * Next, we have to see if we need to pull another argument to be
  270. * used as the option argument.
  271. */
  272. ppzOpts[ optsIdx++ ] = pOpts->origArgVect[ (pOpts->curOptIdx)++ ];
  273. if (OPTST_GET_ARGTYPE(os.pOD->fOptState) == OPARG_TYPE_NONE) {
  274. /*
  275. * No option argument. If we have a short option here,
  276. * then scan for short options until we get to the end
  277. * of the argument string.
  278. */
  279. if ( (os.optType == TOPT_SHORT)
  280. && FAILED( checkShortOpts( pOpts, pzArg+2, &os,
  281. ppzOpts, &optsIdx )) ) {
  282. errno = EINVAL;
  283. goto freeTemps;
  284. }
  285. } else if (os.pOD->fOptState & OPTST_ARG_OPTIONAL) {
  286. switch (mayHandleArg( pOpts, pzArg+2, &os, ppzOpts, &optsIdx )) {
  287. case FAILURE: errno = EIO; goto freeTemps;
  288. case PROBLEM: errno = 0; goto joinLists;
  289. }
  290. } else {
  291. switch (mustHandleArg( pOpts, pzArg+2, &os, ppzOpts, &optsIdx )) {
  292. case PROBLEM:
  293. case FAILURE: errno = EIO; goto freeTemps;
  294. }
  295. }
  296. } /* for (;;) */
  297. restOperands:
  298. while (pOpts->curOptIdx < pOpts->origArgCt)
  299. ppzOpds[ opdsIdx++ ] = pOpts->origArgVect[ (pOpts->curOptIdx)++ ];
  300. joinLists:
  301. if (optsIdx > 0)
  302. memcpy( pOpts->origArgVect + 1, ppzOpts, optsIdx * sizeof( char* ));
  303. if (opdsIdx > 0)
  304. memcpy( pOpts->origArgVect + 1 + optsIdx,
  305. ppzOpds, opdsIdx * sizeof( char* ));
  306. freeTemps:
  307. free( ppzOpts );
  308. free( ppzOpds );
  309. return;
  310. exit_no_mem:
  311. errno = ENOMEM;
  312. return;
  313. }
  314. /*
  315. * Local Variables:
  316. * mode: C
  317. * c-file-style: "stroustrup"
  318. * indent-tabs-mode: nil
  319. * End:
  320. * end of autoopts/sort.c */