You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

326 lines
9.0 KiB

7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /** @file getopt_long.c
  2. ** @brief getopt_long - Definition
  3. ** @author Andrea Vedaldi
  4. **/
  5. /*
  6. Copyright (C) 2007-12 Andrea Vedaldi and Brian Fulkerson.
  7. All rights reserved.
  8. This file is part of the VLFeat library and is made available under
  9. the terms of the BSD license (see the COPYING file).
  10. */
  11. /**
  12. @file getopt_long.h
  13. @brief getopt_long
  14. @author Andrea Vedaldi
  15. This is a drop-in replacament of GNU getopt_long meant to be used
  16. on platforms that do not support such functionality.
  17. **/
  18. #include <stdlib.h>
  19. #include <string.h>
  20. #include <stdio.h>
  21. #include "generic.h"
  22. #include "getopt_long.h"
  23. int opterr = 1 ;
  24. int optind = 1 ;
  25. int optopt ;
  26. char * optarg ;
  27. int optreset ;
  28. #define BADCH '?'
  29. #define BADARG ':'
  30. #define EEND -1
  31. #define EMSG ""
  32. /** @brief Parse long options (BSD style)
  33. ** @param argc number of arguments.
  34. ** @param argv pointer to the vector of arguments.
  35. ** @param optstring list of abbreviated options
  36. ** @param longopts list of long options.
  37. ** @param longindex index of current option in @a longopts.
  38. ** @return the code of the next option.
  39. **
  40. ** This function extract long and short options from the argument
  41. ** list @a argv of @a argc entries.
  42. **
  43. ** A short options sequence is introduced by a single dash character
  44. ** @c -. Each short option is described by a single character in the
  45. ** string @a optstring, possibly followed by a @c : character to
  46. ** denote a (mandatory) argument of the short option. A short option
  47. ** with an argument cannot appear in the middle of a short option
  48. ** sequence, but only at the end.
  49. **
  50. ** A long option is introduced by a double dash @c --. Each long
  51. ** option is described by an instance of the ::option structure in
  52. ** the @a longopts table (the last entry must be filled with zeroes
  53. ** to denote the end).
  54. **
  55. ** Illegal options and missing arguments cause the function to skip
  56. ** the option and return '?'. If ::opterr is @c true (default), the
  57. ** function prints an error message to @a stderr. Finally, if @a
  58. ** optstring has a leading @c :, then error messages are suppressed
  59. ** and a missing argument causes @a : to be returned.
  60. **
  61. ** @remark The function is currently <em>not</em> thread safe.
  62. **/
  63. VL_EXPORT int
  64. getopt_long(int argc, char *const argv[],
  65. const char *optstring,
  66. const struct option * longopts,
  67. int *longindex)
  68. {
  69. static char *place = EMSG; /* option letter processing */
  70. static int optbegin = 0 ;
  71. static int optend = 0 ;
  72. char *oli; /* option letter list index */
  73. int has_colon = 0 ;
  74. int ret_val = 0 ;
  75. /*
  76. A semicolon at the beginning of optstring has a special meaning.
  77. If we find one, we annote and remove it.
  78. */
  79. has_colon = optstring && optstring[0] == ':' ;
  80. if (has_colon) ++ optstring ;
  81. /*
  82. Here we are either processing a short option sequence or
  83. we start processing a new option. This is indicated by optreset.
  84. */
  85. if (optreset || *place == '\0') {
  86. /* ---------------------------------------------------------------
  87. * Look for next short/long option
  88. * ------------------------------------------------------------ */
  89. optreset = 0 ;
  90. /* no more arguments ? */
  91. if (optind >= argc) {
  92. place = EMSG ;
  93. return -1 ;
  94. }
  95. /* next argument that may hold an option */
  96. optbegin = optind ;
  97. /* ---------------------------------------------------------------
  98. * Look for an option to parse
  99. * ------------------------------------------------------------ */
  100. parse_option_at_optbegin :
  101. /* place points to the candidate option */
  102. place = argv [optbegin] ;
  103. /* an option is introduced by '-' */
  104. if (place [0] != '-') {
  105. /* this argument is not an option: try next argument */
  106. ++ optbegin ;
  107. if (optbegin >= argc) {
  108. /* no more arguments to look for options */
  109. place = EMSG ;
  110. return -1 ;
  111. }
  112. goto parse_option_at_optbegin ;
  113. }
  114. /* consume leading `-' */
  115. ++ place ;
  116. /* assume the option is composed of one argument only */
  117. optend = optbegin + 1 ;
  118. /* assume no argument */
  119. optarg = 0 ;
  120. /* ---------------------------------------------------------------
  121. * option `--'
  122. * ------------------------------------------------------------ */
  123. /* this special option (void long option) ends the option processing */
  124. if (place[0] &&
  125. place[0] == '-' &&
  126. place[1] == '\0') {
  127. optind = optend ;
  128. place = EMSG ;
  129. ret_val = -1 ;
  130. goto done_option ;
  131. }
  132. /* ---------------------------------------------------------------
  133. * long option
  134. * ------------------------------------------------------------ */
  135. if (place[0] &&
  136. place[0] == '-' &&
  137. place[1] ) {
  138. size_t namelen ;
  139. int i ;
  140. /* consume second `-' */
  141. ++ place ;
  142. /* count characters before `=' */
  143. namelen = strcspn(place, "=") ;
  144. /* scan longopts for this option */
  145. for (i = 0 ; longopts[i].name != NULL ; ++ i) {
  146. if (strlen ( longopts[i].name) == namelen &&
  147. strncmp (place, longopts[i].name, namelen) == 0 ) {
  148. /* save back long option index */
  149. if (longindex) *longindex = i ;
  150. /* process long option argument */
  151. if (longopts[i].has_arg == required_argument ||
  152. longopts[i].has_arg == optional_argument) {
  153. /* --option=value style */
  154. if (place[namelen] == '=') {
  155. optarg = place + namelen + 1 ;
  156. }
  157. /* --option value style (only required_argument) */
  158. else if (longopts[i].has_arg == required_argument) {
  159. /* missing argument ? */
  160. if (optbegin >= argc - 1) {
  161. if (! has_colon && opterr)
  162. fprintf(stderr,
  163. "%s: option requires an argument -- %s\n",
  164. argv[0], place);
  165. place = EMSG ;
  166. ret_val = has_colon ? BADARG : BADCH ;
  167. goto done_option ;
  168. }
  169. optarg = argv [optend] ;
  170. ++ optend ;
  171. }
  172. }
  173. /* determine return value */
  174. if (longopts[i].flag == NULL) {
  175. ret_val = longopts[i].val ;
  176. }
  177. else {
  178. *longopts[i].flag = longopts[i].val;
  179. ret_val = 0 ;
  180. }
  181. /* mark sequence closed */
  182. place = EMSG ;
  183. goto done_option ;
  184. } /* if match */
  185. } /* scan longoptions */
  186. /* no matching option found */
  187. if (! has_colon && opterr)
  188. fprintf(stderr,
  189. "%s: illegal option -- %s\n", argv[0], place) ;
  190. place = EMSG ;
  191. ret_val = BADCH ;
  192. goto done_option ;
  193. }
  194. } /* end new option */
  195. /* -----------------------------------------------------------------
  196. * Finish short option sequence
  197. * -------------------------------------------------------------- */
  198. optopt = (int) *place++ ;
  199. /* search charcater in option list */
  200. oli = (char*) strchr(optstring, optopt);
  201. /* short option not found */
  202. if (!oli) {
  203. if (! has_colon && opterr)
  204. fprintf(stderr,
  205. "%s: illegal option -- %c\n",
  206. argv[0], optopt);
  207. if (*place) {
  208. /* more short options in the list */
  209. return BADCH ;
  210. }
  211. else {
  212. /* error occured as last option in the list */
  213. place = EMSG ;
  214. ret_val = BADCH ;
  215. goto done_option ;
  216. }
  217. } /* end short option not found */
  218. if (oli[1] != ':') {
  219. /* short option with no argument */
  220. if (*place) {
  221. /* more short options in the list */
  222. return optopt ;
  223. }
  224. else {
  225. /* last option in the list */
  226. place = EMSG ;
  227. ret_val = optopt ;
  228. goto done_option ;
  229. }
  230. } else {
  231. /* short option with argument */
  232. /* -ovalue style */
  233. if (*place) {
  234. optarg = place ;
  235. place = EMSG ;
  236. ret_val = optopt ;
  237. goto done_option ;
  238. }
  239. /* -o value style: missing argument */
  240. else if (optbegin >= argc - 1) {
  241. if (! has_colon && opterr)
  242. fprintf(stderr,
  243. "%s: option requires an argument -- %c\n",
  244. argv[0], optopt);
  245. place = EMSG ;
  246. ret_val = has_colon ? BADARG : BADCH ;
  247. goto done_option ;
  248. }
  249. /* -o value style: process argument */
  250. optarg = argv [optend] ;
  251. ++ optend ;
  252. place = EMSG ;
  253. ret_val = optopt ;
  254. goto done_option ;
  255. } /* short with argument */
  256. done_option :
  257. {
  258. int pos = optend - optbegin ; /* n of circular shifts */
  259. int c = pos ;
  260. while (c --) {
  261. int i ;
  262. char *tmp = argv [optend - 1] ;
  263. for (i = optend - 1 ; i > optind ; -- i) {
  264. ((char**)argv) [i] = argv [i-1] ;
  265. }
  266. ((char**)argv) [optind] = tmp ;
  267. }
  268. optind += pos ;
  269. }
  270. return ret_val ;
  271. }