getopt_ext.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. /** BEGIN COPYRIGHT BLOCK
  2. * This Program is free software; you can redistribute it and/or modify it under
  3. * the terms of the GNU General Public License as published by the Free Software
  4. * Foundation; version 2 of the License.
  5. *
  6. * This Program is distributed in the hope that it will be useful, but WITHOUT
  7. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  8. * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  9. *
  10. * You should have received a copy of the GNU General Public License along with
  11. * this Program; if not, write to the Free Software Foundation, Inc., 59 Temple
  12. * Place, Suite 330, Boston, MA 02111-1307 USA.
  13. *
  14. * In addition, as a special exception, Red Hat, Inc. gives You the additional
  15. * right to link the code of this Program with code not covered under the GNU
  16. * General Public License ("Non-GPL Code") and to distribute linked combinations
  17. * including the two, subject to the limitations in this paragraph. Non-GPL Code
  18. * permitted under this exception must only link to the code of this Program
  19. * through those well defined interfaces identified in the file named EXCEPTION
  20. * found in the source code files (the "Approved Interfaces"). The files of
  21. * Non-GPL Code may instantiate templates or use macros or inline functions from
  22. * the Approved Interfaces without causing the resulting work to be covered by
  23. * the GNU General Public License. Only Red Hat, Inc. may make changes or
  24. * additions to the list of Approved Interfaces. You must obey the GNU General
  25. * Public License in all respects for all of the Program code and other code used
  26. * in conjunction with the Program except the Non-GPL Code covered by this
  27. * exception. If you modify this file, you may extend this exception to your
  28. * version of the file, but you are not obligated to do so. If you do not wish to
  29. * provide this exception without modification, you must delete this exception
  30. * statement from your version and license this file solely under the GPL without
  31. * exception.
  32. *
  33. *
  34. * Copyright (C) 2001 Sun Microsystems, Inc. Used by permission.
  35. * Copyright (C) 2005 Red Hat, Inc.
  36. * All rights reserved.
  37. * END COPYRIGHT BLOCK **/
  38. #include "getopt_ext.h"
  39. char *optarg_ext;
  40. int optind_ext=1;
  41. int optopt_ext;
  42. int opterr_ext;
  43. int optind_last;
  44. static int _getopt_ext_inited = 0;
  45. static int _optind_firstHandled = 0;
  46. static int _optind_firstRecognized = 0;
  47. static int _getopt_ext_done_long = 0;
  48. static
  49. int _getopt_ext_init(int argc)
  50. {
  51. _getopt_ext_inited = 1;
  52. /* optind_ext = 1;*/
  53. optind_last = argc;
  54. _optind_firstHandled = argc;
  55. _optind_firstRecognized = argc;
  56. /* optind = 1;*/
  57. _getopt_ext_done_long = 0;
  58. return(0);
  59. }
  60. #if 0
  61. static
  62. int _getopt_ext_done()
  63. {
  64. _getopt_ext_done_long = 1;
  65. return(0);
  66. }
  67. #endif
  68. static
  69. int _getopt_ext_find(int argc,
  70. char **argv,
  71. const struct opt_ext *longOpts)
  72. {
  73. int i=0;
  74. struct opt_ext *lopt;
  75. i=0;
  76. lopt = (struct opt_ext *)longOpts;
  77. while(lopt->o_string) {
  78. if(0 != strcmp(argv[optind_ext]+2,lopt->o_string)) {
  79. i++;
  80. lopt++;
  81. continue;
  82. }
  83. /*
  84. * found it
  85. */
  86. return(i);
  87. }
  88. /* should never come here */
  89. return(-1);
  90. }
  91. static
  92. int _getopt_ext_tailit(int argc,
  93. char **argv,
  94. int hasArg,
  95. int recognized)
  96. {
  97. char *_optPtr=NULL;
  98. char *_optArgPtr=NULL;
  99. int _endIndex=optind_last;
  100. int _nextDest=optind_ext;
  101. int _nextSource=optind_ext;
  102. _optPtr = argv[optind_ext];
  103. if(hasArg) {
  104. _nextSource = optind_ext + 2;
  105. _endIndex = ((recognized)?_optind_firstRecognized:_optind_firstHandled);
  106. _optArgPtr = argv[optind_ext + 1];
  107. } else {
  108. _nextSource = optind_ext + 1;
  109. _endIndex = ((recognized)?_optind_firstRecognized:_optind_firstHandled);
  110. _optArgPtr = NULL;
  111. }
  112. while(_nextSource < _endIndex) {
  113. argv[_nextDest] = argv[_nextSource];
  114. _nextSource++;
  115. _nextDest++;
  116. }
  117. argv[_nextDest] = _optPtr;
  118. /* argv[_nextDest] = NULL; */
  119. if(hasArg) {
  120. argv[_nextDest + 1] = _optArgPtr;
  121. if(recognized) {
  122. _optind_firstRecognized -=2;
  123. }
  124. _optind_firstHandled -=2;
  125. } else {
  126. if(recognized) {
  127. _optind_firstRecognized -=1;
  128. }
  129. _optind_firstHandled -=1;
  130. }
  131. optind_last = _optind_firstRecognized;
  132. return(0);
  133. }
  134. /*
  135. * First process all the long options (using exact string matches)
  136. * Then, follow up with regular option processing using good ol' getopt
  137. *
  138. * return the same return codes as getopt
  139. *
  140. */
  141. int getopt_ext(int argc,
  142. char **argv,
  143. const char *optstring,
  144. const struct opt_ext *longOpts,
  145. int *longOptIndex)
  146. {
  147. int retVal;
  148. if(!_getopt_ext_inited) {
  149. _getopt_ext_init(argc);
  150. }
  151. if(_optind_firstHandled < optind_ext) {
  152. /* we are not processing extended options anymore...
  153. let getopt handle it.
  154. */
  155. goto _doneWithExtendedOptions;
  156. }
  157. /*
  158. * if we are here, we are not done with extended options...
  159. *
  160. */
  161. while(_optind_firstHandled > optind_ext) {
  162. if(0 != strncmp(argv[optind_ext], "--", 2)) {
  163. optind_ext++;
  164. continue;
  165. }
  166. /*
  167. * possibly an extended option
  168. */
  169. retVal = _getopt_ext_find(argc,argv,longOpts);
  170. if(-1 == retVal) {
  171. /*
  172. * unrecognized long option...
  173. * we will let getopt handle it later...
  174. *
  175. */
  176. optind_ext++;
  177. continue;
  178. }
  179. /*
  180. * we found an extended option
  181. * now find its arg...
  182. */
  183. switch((longOpts+retVal)->o_type) {
  184. case ArgNone:
  185. default:
  186. {
  187. /* send this option to the end of the arglist */
  188. _getopt_ext_tailit(argc,argv,0,1);
  189. optarg_ext = NULL;
  190. *longOptIndex = retVal;
  191. return((longOpts+retVal)->o_return);
  192. }
  193. case ArgRequired:
  194. {
  195. /*
  196. * make sure the next arg is a "valid" argument
  197. */
  198. if(((optind_ext + 1) == _optind_firstHandled)) {
  199. /*
  200. * did not find the argument
  201. * let getopt handle it
  202. * we will just push it to the end and continue
  203. * looking for more extended options
  204. *
  205. * _getopt_ext_tailit(argc,argv,0,0);
  206. */
  207. optind_ext++;
  208. fprintf(stderr, "%s: option requires an argument -- %s\n",
  209. argv[0],(longOpts+retVal)->o_string);
  210. exit(0);
  211. continue;
  212. }
  213. /* like getopt, we don't care what the arg looks like! */
  214. /* send this option to the end of the arglist */
  215. _getopt_ext_tailit(argc,argv,1,1);
  216. optarg_ext = argv[_optind_firstRecognized + 1];
  217. *longOptIndex = retVal;
  218. return((longOpts+retVal)->o_return);
  219. }
  220. case ArgOptional:
  221. {
  222. if(((optind_ext + 1) == optind_last) ||
  223. ('-' == argv[optind_ext + 1][0])){
  224. /*
  225. * did not find the argument
  226. *
  227. */
  228. _getopt_ext_tailit(argc,argv,0,1);
  229. optarg_ext = NULL;
  230. *longOptIndex = retVal;
  231. return((longOpts+retVal)->o_return);
  232. }
  233. /* send this option to the end of the arglist */
  234. _getopt_ext_tailit(argc,argv,1,1);
  235. optarg_ext = argv[optind_last + 1];
  236. *longOptIndex = retVal;
  237. return((longOpts+retVal)->o_return);
  238. }
  239. }
  240. /* optind_ext++;*/
  241. }
  242. _doneWithExtendedOptions:
  243. retVal = getopt(optind_last,argv,optstring);
  244. optarg_ext = optarg;
  245. /* optopt_ext = optopt; */
  246. optind_last = _optind_firstHandled;
  247. return(retVal);
  248. }