getopt_ext.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  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. #ifdef HAVE_CONFIG_H
  39. # include <config.h>
  40. #endif
  41. #include "getopt_ext.h"
  42. char *optarg_ext;
  43. int optind_ext=1;
  44. int optopt_ext;
  45. int opterr_ext;
  46. int optind_last;
  47. static int _getopt_ext_inited = 0;
  48. static int _optind_firstHandled = 0;
  49. static int _optind_firstRecognized = 0;
  50. static
  51. int _getopt_ext_init(int argc)
  52. {
  53. _getopt_ext_inited = 1;
  54. /* optind_ext = 1;*/
  55. optind_last = argc;
  56. _optind_firstHandled = argc;
  57. _optind_firstRecognized = argc;
  58. /* optind = 1;*/
  59. return(0);
  60. }
  61. #if 0
  62. static
  63. int _getopt_ext_done()
  64. {
  65. _getopt_ext_done_long = 1;
  66. return(0);
  67. }
  68. #endif
  69. static
  70. int _getopt_ext_find(int argc,
  71. char **argv,
  72. const struct opt_ext *longOpts)
  73. {
  74. int i=0;
  75. struct opt_ext *lopt;
  76. i=0;
  77. lopt = (struct opt_ext *)longOpts;
  78. while(lopt->o_string) {
  79. if(0 != strcmp(argv[optind_ext]+2,lopt->o_string)) {
  80. i++;
  81. lopt++;
  82. continue;
  83. }
  84. /*
  85. * found it
  86. */
  87. return(i);
  88. }
  89. /* should never come here */
  90. return(-1);
  91. }
  92. static
  93. int _getopt_ext_tailit(int argc,
  94. char **argv,
  95. int hasArg,
  96. int recognized)
  97. {
  98. char *_optPtr=NULL;
  99. char *_optArgPtr=NULL;
  100. int _endIndex=optind_last;
  101. int _nextDest=optind_ext;
  102. int _nextSource=optind_ext;
  103. _optPtr = argv[optind_ext];
  104. if(hasArg) {
  105. _nextSource = optind_ext + 2;
  106. _endIndex = ((recognized)?_optind_firstRecognized:_optind_firstHandled);
  107. _optArgPtr = argv[optind_ext + 1];
  108. } else {
  109. _nextSource = optind_ext + 1;
  110. _endIndex = ((recognized)?_optind_firstRecognized:_optind_firstHandled);
  111. _optArgPtr = NULL;
  112. }
  113. while(_nextSource < _endIndex) {
  114. argv[_nextDest] = argv[_nextSource];
  115. _nextSource++;
  116. _nextDest++;
  117. }
  118. argv[_nextDest] = _optPtr;
  119. /* argv[_nextDest] = NULL; */
  120. if(hasArg) {
  121. argv[_nextDest + 1] = _optArgPtr;
  122. if(recognized) {
  123. _optind_firstRecognized -=2;
  124. }
  125. _optind_firstHandled -=2;
  126. } else {
  127. if(recognized) {
  128. _optind_firstRecognized -=1;
  129. }
  130. _optind_firstHandled -=1;
  131. }
  132. optind_last = _optind_firstRecognized;
  133. return(0);
  134. }
  135. /*
  136. * First process all the long options (using exact string matches)
  137. * Then, follow up with regular option processing using good ol' getopt
  138. *
  139. * return the same return codes as getopt
  140. *
  141. */
  142. int getopt_ext(int argc,
  143. char **argv,
  144. const char *optstring,
  145. const struct opt_ext *longOpts,
  146. int *longOptIndex)
  147. {
  148. int retVal;
  149. if(!_getopt_ext_inited) {
  150. _getopt_ext_init(argc);
  151. }
  152. if(_optind_firstHandled < optind_ext) {
  153. /* we are not processing extended options anymore...
  154. let getopt handle it.
  155. */
  156. goto _doneWithExtendedOptions;
  157. }
  158. /*
  159. * if we are here, we are not done with extended options...
  160. *
  161. */
  162. while(_optind_firstHandled > optind_ext) {
  163. if(0 != strncmp(argv[optind_ext], "--", 2)) {
  164. optind_ext++;
  165. continue;
  166. }
  167. /*
  168. * possibly an extended option
  169. */
  170. retVal = _getopt_ext_find(argc,argv,longOpts);
  171. if(-1 == retVal) {
  172. /*
  173. * unrecognized long option...
  174. * we will let getopt handle it later...
  175. *
  176. */
  177. optind_ext++;
  178. continue;
  179. }
  180. /*
  181. * we found an extended option
  182. * now find its arg...
  183. */
  184. switch((longOpts+retVal)->o_type) {
  185. case ArgNone:
  186. default:
  187. {
  188. /* send this option to the end of the arglist */
  189. _getopt_ext_tailit(argc,argv,0,1);
  190. optarg_ext = NULL;
  191. *longOptIndex = retVal;
  192. return((longOpts+retVal)->o_return);
  193. }
  194. case ArgRequired:
  195. {
  196. /*
  197. * make sure the next arg is a "valid" argument
  198. */
  199. if(((optind_ext + 1) == _optind_firstHandled)) {
  200. /*
  201. * did not find the argument
  202. * let getopt handle it
  203. * we will just push it to the end and continue
  204. * looking for more extended options
  205. *
  206. * _getopt_ext_tailit(argc,argv,0,0);
  207. */
  208. optind_ext++;
  209. fprintf(stderr, "%s: option requires an argument -- %s\n",
  210. argv[0],(longOpts+retVal)->o_string);
  211. exit(0);
  212. continue;
  213. }
  214. /* like getopt, we don't care what the arg looks like! */
  215. /* send this option to the end of the arglist */
  216. _getopt_ext_tailit(argc,argv,1,1);
  217. optarg_ext = argv[_optind_firstRecognized + 1];
  218. *longOptIndex = retVal;
  219. return((longOpts+retVal)->o_return);
  220. }
  221. case ArgOptional:
  222. {
  223. if(((optind_ext + 1) == optind_last) ||
  224. ('-' == argv[optind_ext + 1][0])){
  225. /*
  226. * did not find the argument
  227. *
  228. */
  229. _getopt_ext_tailit(argc,argv,0,1);
  230. optarg_ext = NULL;
  231. *longOptIndex = retVal;
  232. return((longOpts+retVal)->o_return);
  233. }
  234. /* send this option to the end of the arglist */
  235. _getopt_ext_tailit(argc,argv,1,1);
  236. optarg_ext = argv[optind_last + 1];
  237. *longOptIndex = retVal;
  238. return((longOpts+retVal)->o_return);
  239. }
  240. }
  241. /* optind_ext++;*/
  242. }
  243. _doneWithExtendedOptions:
  244. retVal = getopt(optind_last,argv,optstring);
  245. optarg_ext = optarg;
  246. /* optopt_ext = optopt; */
  247. optind_last = _optind_firstHandled;
  248. return(retVal);
  249. }