win_posix.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. /*
  2. * Copyright (c) 2017 - 2020, Arm Limited and Contributors. All rights reserved.
  3. *
  4. * SPDX-License-Identifier: BSD-3-Clause
  5. */
  6. #include <assert.h>
  7. #include "win_posix.h"
  8. /*
  9. * This variable is set by getopt to the index of the next element of the
  10. * argv array to be processed. Once getopt has found all of the option
  11. * arguments, you can use this variable to determine where the remaining
  12. * non-option arguments begin. The initial value of this variable is 1.
  13. */
  14. int optind = 1;
  15. /*
  16. * If the value of this variable is nonzero, then getopt prints an error
  17. * message to the standard error stream if it encounters an unknown option
  18. * default character or an option with a missing required argument.
  19. * If you set this variable to zero, getopt does not print any messages,
  20. * but it still returns the character ? to indicate an error.
  21. */
  22. const int opterr; /* = 0; */
  23. /* const because we do not implement error printing.*/
  24. /* Not initialised to conform with the coding standard. */
  25. /*
  26. * When getopt encounters an unknown option character or an option with a
  27. * missing required argument, it stores that option character in this
  28. * variable.
  29. */
  30. int optopt; /* = 0; */
  31. /*
  32. * This variable is set by getopt to point at the value of the option
  33. * argument, for those options that accept arguments.
  34. */
  35. char *optarg; /* = 0; */
  36. enum return_flags {
  37. RET_ERROR = -1,
  38. RET_END_OPT_LIST = -1,
  39. RET_NO_PARAM = '?',
  40. RET_NO_PARAM2 = ':',
  41. RET_UNKNOWN_OPT = '?'
  42. };
  43. /*
  44. * Common initialisation on entry.
  45. */
  46. static
  47. void getopt_init(void)
  48. {
  49. optarg = (char *)0;
  50. optopt = 0;
  51. /* optind may be zero with some POSIX uses.
  52. * For our purposes we just change it to 1.
  53. */
  54. if (optind == 0)
  55. optind = 1;
  56. }
  57. /*
  58. * Common handling for a single letter option.
  59. */
  60. static
  61. int getopt_1char(int argc,
  62. char *const argv[],
  63. const char *const opstring,
  64. const int optchar)
  65. {
  66. size_t nlen = (opstring == 0) ? 0 : strlen(opstring);
  67. size_t loptn;
  68. for (loptn = 0; loptn < nlen; loptn++) {
  69. if (optchar == opstring[loptn]) {
  70. if (opstring[loptn + 1] == ':') {
  71. /* Option has argument */
  72. if (optind < argc) {
  73. /* Found argument. */
  74. assert(argv != 0);
  75. optind++;
  76. optarg = argv[optind++];
  77. return optchar;
  78. }
  79. /* Missing argument. */
  80. if (opstring[loptn + 2] == ':') {
  81. /* OK if optional "x::". */
  82. optind++;
  83. return optchar;
  84. }
  85. /* Actual missing value. */
  86. optopt = optchar;
  87. return ((opstring[0] == ':')
  88. ? RET_NO_PARAM2
  89. : RET_NO_PARAM);
  90. }
  91. /* No argument, just return option char */
  92. optind++;
  93. return optchar;
  94. }
  95. }
  96. /*
  97. * If getopt finds an option character in argv that was not included in
  98. * options, ... it returns '?' and sets the external variable optopt to
  99. * the actual option character.
  100. */
  101. optopt = optchar;
  102. return RET_UNKNOWN_OPT;
  103. }
  104. int getopt(int argc,
  105. char *argv[],
  106. char *opstring)
  107. {
  108. int result = RET_END_OPT_LIST;
  109. size_t argn = 0;
  110. size_t nlen = strlen(opstring);
  111. getopt_init();
  112. /* If we have an argument left to play with */
  113. if ((argc > optind) && (argv != 0)) {
  114. const char *arg = (const char *)argv[optind];
  115. if ((arg != 0) && (arg[0] == '-'))
  116. result = getopt_1char(argc, argv, opstring, arg[1]);
  117. }
  118. return result;
  119. }
  120. /*
  121. * Match an argument value against an option name.
  122. * Note that we only match over the shorter length of the pair, to allow
  123. * for abbreviation or say --match=value
  124. * Long option names may be abbreviated if the abbreviation is unique or an
  125. * exact match for some defined option. This function does not check that the
  126. * abbreviations are unique and should be handled by the caller.
  127. * A long option may take a parameter, of the form --opt=param or --opt param.
  128. */
  129. static
  130. int optmatch(const char *argval, const char *optname)
  131. {
  132. int result = 0;
  133. while ((result == 0) && (*optname != 0) && (*argval != 0))
  134. result = (*argval++) - (*optname++);
  135. return result;
  136. }
  137. /* Handling for a single long option. */
  138. static
  139. int getopt_1long(const int argc,
  140. char *const argv[],
  141. const struct option *const longopts,
  142. const char *const optname,
  143. int *const indexptr)
  144. {
  145. int result = RET_UNKNOWN_OPT;
  146. size_t loptn = 0;
  147. bool match_found = false;
  148. /*
  149. * Long option names may be abbreviated if the abbreviation
  150. * is unique or an exact match for some defined option.
  151. * To handle this:
  152. * - First search for an exact match.
  153. * - If exact match was not found search for a abbreviated match.
  154. * By doing this an incorrect option selection can be avoided.
  155. */
  156. /* 1. Search for an exact match. */
  157. while (longopts[loptn].name != NULL) {
  158. if (strcmp(optname, longopts[loptn].name) == 0) {
  159. match_found = true;
  160. break;
  161. }
  162. ++loptn;
  163. }
  164. /* 2. If exact match was not found search for a abbreviated match. */
  165. if (!match_found) {
  166. loptn = 0;
  167. while (longopts[loptn].name != NULL) {
  168. if (optmatch(optname, longopts[loptn].name) == 0) {
  169. match_found = true;
  170. break;
  171. }
  172. ++loptn;
  173. }
  174. }
  175. if (match_found) {
  176. /* We found a match. */
  177. result = longopts[loptn].val;
  178. if (indexptr != 0) {
  179. *indexptr = loptn;
  180. }
  181. switch (longopts[loptn].has_arg) {
  182. case required_argument:
  183. if ((optind + 1) >= argc) {
  184. /* Missing argument. */
  185. optopt = result;
  186. return RET_NO_PARAM;
  187. }
  188. /* Fallthrough to get option value. */
  189. case optional_argument:
  190. if ((argc - optind) > 0) {
  191. /* Found argument. */
  192. optarg = argv[++optind];
  193. }
  194. /* Fallthrough to handle flag. */
  195. case no_argument:
  196. optind++;
  197. if (longopts[loptn].flag != 0) {
  198. *longopts[loptn].flag = result;
  199. result = 0;
  200. }
  201. break;
  202. }
  203. return result;
  204. }
  205. /*
  206. * If getopt finds an option character in argv that was not included
  207. * in options, ... it returns '?' and sets the external variable
  208. * optopt to the actual option character.
  209. */
  210. return RET_UNKNOWN_OPT;
  211. }
  212. /*
  213. * getopt_long gets the next option argument from the argument list
  214. * specified by the argv and argc arguments. Options may be either short
  215. * (single letter) as for getopt, or longer names (preceded by --).
  216. */
  217. int getopt_long(int argc,
  218. char *argv[],
  219. const char *shortopts,
  220. const struct option *longopts,
  221. int *indexptr)
  222. {
  223. int result = RET_END_OPT_LIST;
  224. getopt_init();
  225. /* If we have an argument left to play with */
  226. if ((argc > optind) && (argv != 0)) {
  227. const char *arg = argv[optind];
  228. if ((arg != 0) && (arg[0] == '-')) {
  229. if (arg[1] == '-') {
  230. /* Looks like a long option. */
  231. result = getopt_1long(argc,
  232. argv,
  233. longopts,
  234. &arg[2],
  235. indexptr);
  236. } else {
  237. result = getopt_1char(argc,
  238. argv,
  239. shortopts,
  240. arg[1]);
  241. }
  242. }
  243. }
  244. return result;
  245. }
  246. /*
  247. * getopt_long_only gets the next option argument from the argument list
  248. * specified by the argv and argc arguments. Options may be either short
  249. * or long as for getopt_long, but the long names may have a single '-'
  250. * prefix too.
  251. */
  252. int getopt_long_only(int argc,
  253. char *argv[],
  254. const char *shortopts,
  255. const struct option *longopts,
  256. int *indexptr)
  257. {
  258. int result = RET_END_OPT_LIST;
  259. getopt_init();
  260. /* If we have an argument left to play with */
  261. if ((argc > optind) && (argv != 0)) {
  262. const char *arg = argv[optind];
  263. if ((arg != 0) && (arg[0] == '-')) {
  264. if (arg[1] == '-') {
  265. /* Looks like a long option. */
  266. result = getopt_1long(argc,
  267. argv,
  268. longopts,
  269. &arg[2],
  270. indexptr);
  271. } else {
  272. result = getopt_1long(argc,
  273. argv,
  274. longopts,
  275. &arg[1],
  276. indexptr);
  277. if (result == RET_UNKNOWN_OPT) {
  278. result = getopt_1char(argc,
  279. argv,
  280. shortopts,
  281. arg[1]);
  282. }
  283. }
  284. }
  285. }
  286. return result;
  287. }