getopt_ext.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. /** BEGIN COPYRIGHT BLOCK
  2. * Copyright (C) 2001 Sun Microsystems, Inc. Used by permission.
  3. * Copyright (C) 2005 Red Hat, Inc.
  4. * All rights reserved.
  5. *
  6. * License: GPL (version 3 or any later version).
  7. * See LICENSE for details.
  8. * END COPYRIGHT BLOCK **/
  9. #ifdef HAVE_CONFIG_H
  10. # include <config.h>
  11. #endif
  12. #include "getopt_ext.h"
  13. char *optarg_ext;
  14. int optind_ext=1;
  15. int optopt_ext;
  16. int opterr_ext;
  17. int optind_last;
  18. static int _getopt_ext_inited = 0;
  19. static int _optind_firstHandled = 0;
  20. static int _optind_firstRecognized = 0;
  21. static
  22. int _getopt_ext_init(int argc)
  23. {
  24. _getopt_ext_inited = 1;
  25. /* optind_ext = 1;*/
  26. optind_last = argc;
  27. _optind_firstHandled = argc;
  28. _optind_firstRecognized = argc;
  29. /* optind = 1;*/
  30. return(0);
  31. }
  32. #if 0
  33. static
  34. int _getopt_ext_done()
  35. {
  36. _getopt_ext_done_long = 1;
  37. return(0);
  38. }
  39. #endif
  40. static
  41. int _getopt_ext_find(int argc,
  42. char **argv,
  43. const struct opt_ext *longOpts)
  44. {
  45. int i=0;
  46. struct opt_ext *lopt;
  47. i=0;
  48. lopt = (struct opt_ext *)longOpts;
  49. while(lopt->o_string) {
  50. if(0 != strcmp(argv[optind_ext]+2,lopt->o_string)) {
  51. i++;
  52. lopt++;
  53. continue;
  54. }
  55. /*
  56. * found it
  57. */
  58. return(i);
  59. }
  60. /* should never come here */
  61. return(-1);
  62. }
  63. static
  64. int _getopt_ext_tailit(int argc,
  65. char **argv,
  66. int hasArg,
  67. int recognized)
  68. {
  69. char *_optPtr=NULL;
  70. char *_optArgPtr=NULL;
  71. int _endIndex=optind_last;
  72. int _nextDest=optind_ext;
  73. int _nextSource=optind_ext;
  74. _optPtr = argv[optind_ext];
  75. if(hasArg) {
  76. _nextSource = optind_ext + 2;
  77. _endIndex = ((recognized)?_optind_firstRecognized:_optind_firstHandled);
  78. _optArgPtr = argv[optind_ext + 1];
  79. } else {
  80. _nextSource = optind_ext + 1;
  81. _endIndex = ((recognized)?_optind_firstRecognized:_optind_firstHandled);
  82. _optArgPtr = NULL;
  83. }
  84. while(_nextSource < _endIndex) {
  85. argv[_nextDest] = argv[_nextSource];
  86. _nextSource++;
  87. _nextDest++;
  88. }
  89. argv[_nextDest] = _optPtr;
  90. /* argv[_nextDest] = NULL; */
  91. if(hasArg) {
  92. argv[_nextDest + 1] = _optArgPtr;
  93. if(recognized) {
  94. _optind_firstRecognized -=2;
  95. }
  96. _optind_firstHandled -=2;
  97. } else {
  98. if(recognized) {
  99. _optind_firstRecognized -=1;
  100. }
  101. _optind_firstHandled -=1;
  102. }
  103. optind_last = _optind_firstRecognized;
  104. return(0);
  105. }
  106. /*
  107. * First process all the long options (using exact string matches)
  108. * Then, follow up with regular option processing using good ol' getopt
  109. *
  110. * return the same return codes as getopt
  111. *
  112. */
  113. int getopt_ext(int argc,
  114. char **argv,
  115. const char *optstring,
  116. const struct opt_ext *longOpts,
  117. int *longOptIndex)
  118. {
  119. int retVal;
  120. if(!_getopt_ext_inited) {
  121. _getopt_ext_init(argc);
  122. }
  123. if(_optind_firstHandled < optind_ext) {
  124. /* we are not processing extended options anymore...
  125. let getopt handle it.
  126. */
  127. goto _doneWithExtendedOptions;
  128. }
  129. /*
  130. * if we are here, we are not done with extended options...
  131. *
  132. */
  133. while(_optind_firstHandled > optind_ext) {
  134. if(0 != strncmp(argv[optind_ext], "--", 2)) {
  135. optind_ext++;
  136. continue;
  137. }
  138. /*
  139. * possibly an extended option
  140. */
  141. retVal = _getopt_ext_find(argc,argv,longOpts);
  142. if(-1 == retVal) {
  143. /*
  144. * unrecognized long option...
  145. * we will let getopt handle it later...
  146. *
  147. */
  148. optind_ext++;
  149. continue;
  150. }
  151. /*
  152. * we found an extended option
  153. * now find its arg...
  154. */
  155. switch((longOpts+retVal)->o_type) {
  156. case ArgNone:
  157. default:
  158. {
  159. /* send this option to the end of the arglist */
  160. _getopt_ext_tailit(argc,argv,0,1);
  161. optarg_ext = NULL;
  162. *longOptIndex = retVal;
  163. return((longOpts+retVal)->o_return);
  164. }
  165. case ArgRequired:
  166. {
  167. /*
  168. * make sure the next arg is a "valid" argument
  169. */
  170. if(((optind_ext + 1) == _optind_firstHandled)) {
  171. /*
  172. * did not find the argument
  173. * let getopt handle it
  174. * we will just push it to the end and continue
  175. * looking for more extended options
  176. *
  177. * _getopt_ext_tailit(argc,argv,0,0);
  178. */
  179. optind_ext++;
  180. fprintf(stderr, "%s: option requires an argument -- %s\n",
  181. argv[0],(longOpts+retVal)->o_string);
  182. exit(0);
  183. continue;
  184. }
  185. /* like getopt, we don't care what the arg looks like! */
  186. /* send this option to the end of the arglist */
  187. _getopt_ext_tailit(argc,argv,1,1);
  188. optarg_ext = argv[_optind_firstRecognized + 1];
  189. *longOptIndex = retVal;
  190. return((longOpts+retVal)->o_return);
  191. }
  192. case ArgOptional:
  193. {
  194. if(((optind_ext + 1) == optind_last) ||
  195. ('-' == argv[optind_ext + 1][0])){
  196. /*
  197. * did not find the argument
  198. *
  199. */
  200. _getopt_ext_tailit(argc,argv,0,1);
  201. optarg_ext = NULL;
  202. *longOptIndex = retVal;
  203. return((longOpts+retVal)->o_return);
  204. }
  205. /* send this option to the end of the arglist */
  206. _getopt_ext_tailit(argc,argv,1,1);
  207. optarg_ext = argv[optind_last + 1];
  208. *longOptIndex = retVal;
  209. return((longOpts+retVal)->o_return);
  210. }
  211. }
  212. /* optind_ext++;*/
  213. }
  214. _doneWithExtendedOptions:
  215. retVal = getopt(optind_last,argv,optstring);
  216. optarg_ext = optarg;
  217. /* optopt_ext = optopt; */
  218. optind_last = _optind_firstHandled;
  219. return(retVal);
  220. }