sort.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  1. /*
  2. * sort.c $Id: sort.c,v 4.7 2006/03/25 19:24:56 bkorb Exp $
  3. * Time-stamp: "2005-02-20 17:18:41 bkorb"
  4. *
  5. * This module implements argument sorting.
  6. */
  7. /*
  8. * Automated Options copyright 1992-2006 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, *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
  193. */
  194. if (getenv( "POSIXLY_CORRECT" ) != NULL) {
  195. errno = 0;
  196. return;
  197. }
  198. errno = ENOENT;
  199. /*
  200. * If all arguments are named, we can't sort 'em. There are no operands.
  201. */
  202. if (NAMED_OPTS(pOpts))
  203. return;
  204. /*
  205. * Make sure we can allocate two full-sized arg vectors.
  206. */
  207. ppzOpts = malloc( pOpts->origArgCt * sizeof( char* ));
  208. if (ppzOpts == NULL)
  209. goto exit_no_mem;
  210. ppzOpds = malloc( pOpts->origArgCt * sizeof( char* ));
  211. if (ppzOpds == NULL) {
  212. free( ppzOpts );
  213. goto exit_no_mem;
  214. }
  215. pOpts->curOptIdx = 1;
  216. pOpts->pzCurOpt = NULL;
  217. /*
  218. * Now, process all the options from our current position onward.
  219. * (This allows interspersed options and arguments for the few
  220. * non-standard programs that require it.)
  221. */
  222. for (;;) {
  223. char* pzArg;
  224. tSuccess res;
  225. /*
  226. * If we're out of arguments, we're done. Join the option and
  227. * operand lists into the original argument vector.
  228. */
  229. if (pOpts->curOptIdx >= pOpts->origArgCt) {
  230. errno = 0;
  231. goto joinLists;
  232. }
  233. pzArg = pOpts->origArgVect[ pOpts->curOptIdx ];
  234. if (*pzArg != '-') {
  235. ppzOpds[ opdsIdx++ ] = pOpts->origArgVect[ (pOpts->curOptIdx)++ ];
  236. continue;
  237. }
  238. switch (pzArg[1]) {
  239. case NUL:
  240. /*
  241. * A regular option. Put it on the operand list.
  242. */
  243. ppzOpds[ opdsIdx++ ] = pOpts->origArgVect[ (pOpts->curOptIdx)++ ];
  244. continue;
  245. case '-':
  246. /*
  247. * Two consecutive hypens. Put them on the options list and then
  248. * _always_ force the remainder of the arguments to be operands.
  249. */
  250. if (pzArg[2] == NUL) {
  251. ppzOpts[ optsIdx++ ] =
  252. pOpts->origArgVect[ (pOpts->curOptIdx)++ ];
  253. goto restOperands;
  254. }
  255. res = longOptionFind( pOpts, pzArg+2, &os );
  256. break;
  257. default:
  258. /*
  259. * If short options are not allowed, then do long
  260. * option processing. Otherwise the character must be a
  261. * short (i.e. single character) option.
  262. */
  263. if ((pOpts->fOptSet & OPTPROC_SHORTOPT) == 0) {
  264. res = longOptionFind( pOpts, pzArg+1, &os );
  265. } else {
  266. res = shortOptionFind( pOpts, pzArg[1], &os );
  267. }
  268. break;
  269. }
  270. if (FAILED( res )) {
  271. errno = EIO;
  272. goto freeTemps;
  273. }
  274. /*
  275. * We've found an option. Add the argument to the option list.
  276. * Next, we have to see if we need to pull another argument to be
  277. * used as the option argument.
  278. */
  279. ppzOpts[ optsIdx++ ] = pOpts->origArgVect[ (pOpts->curOptIdx)++ ];
  280. if (OPTST_GET_ARGTYPE(os.pOD->fOptState) == OPARG_TYPE_NONE) {
  281. /*
  282. * No option argument. If we have a short option here,
  283. * then scan for short options until we get to the end
  284. * of the argument string.
  285. */
  286. if ( (os.optType == TOPT_SHORT)
  287. && FAILED( checkShortOpts( pOpts, pzArg+2, &os,
  288. ppzOpts, &optsIdx )) ) {
  289. errno = EIO;
  290. goto freeTemps;
  291. }
  292. } else if (os.pOD->fOptState & OPTST_ARG_OPTIONAL) {
  293. switch (mayHandleArg( pOpts, pzArg+2, &os, ppzOpts, &optsIdx )) {
  294. case FAILURE: errno = EIO; goto freeTemps;
  295. case PROBLEM: errno = 0; goto joinLists;
  296. }
  297. } else {
  298. switch (mustHandleArg( pOpts, pzArg+2, &os, ppzOpts, &optsIdx )) {
  299. case PROBLEM:
  300. case FAILURE: errno = EIO; goto freeTemps;
  301. }
  302. }
  303. } /* for (;;) */
  304. restOperands:
  305. while (pOpts->curOptIdx < pOpts->origArgCt)
  306. ppzOpds[ opdsIdx++ ] = pOpts->origArgVect[ (pOpts->curOptIdx)++ ];
  307. joinLists:
  308. if (optsIdx > 0)
  309. memcpy( pOpts->origArgVect + 1, ppzOpts, optsIdx * sizeof( char* ));
  310. if (opdsIdx > 0)
  311. memcpy( pOpts->origArgVect + 1 + optsIdx,
  312. ppzOpds, opdsIdx * sizeof( char* ));
  313. freeTemps:
  314. free( ppzOpts );
  315. free( ppzOpds );
  316. return;
  317. exit_no_mem:
  318. errno = ENOMEM;
  319. return;
  320. }
  321. /*
  322. * Local Variables:
  323. * mode: C
  324. * c-file-style: "stroustrup"
  325. * tab-width: 4
  326. * indent-tabs-mode: nil
  327. * End:
  328. * end of autoopts/sort.c */