mi-getopt.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. /* MI Command Set - MI Option Parser.
  2. Copyright (C) 2000-2022 Free Software Foundation, Inc.
  3. Contributed by Cygnus Solutions (a Red Hat company).
  4. This file is part of GDB.
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 3 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  15. #include "defs.h"
  16. #include "mi-getopt.h"
  17. /* See comments about mi_getopt and mi_getopt_silent in mi-getopt.h.
  18. When there is an unknown option, if ERROR_ON_UNKNOWN is true,
  19. throw an error, otherwise return -1. */
  20. static int
  21. mi_getopt_1 (const char *prefix, int argc, char **argv,
  22. const struct mi_opt *opts, int *oind, char **oarg,
  23. int error_on_unknown)
  24. {
  25. char *arg;
  26. const struct mi_opt *opt;
  27. /* We assume that argv/argc are ok. */
  28. if (*oind > argc || *oind < 0)
  29. internal_error (__FILE__, __LINE__,
  30. _("mi_getopt_long: oind out of bounds"));
  31. if (*oind == argc)
  32. return -1;
  33. arg = argv[*oind];
  34. /* ``--''? */
  35. if (strcmp (arg, "--") == 0)
  36. {
  37. *oind += 1;
  38. *oarg = NULL;
  39. return -1;
  40. }
  41. /* End of option list. */
  42. if (arg[0] != '-')
  43. {
  44. *oarg = NULL;
  45. return -1;
  46. }
  47. /* Look the option up. */
  48. for (opt = opts; opt->name != NULL; opt++)
  49. {
  50. if (strcmp (opt->name, arg + 1) != 0)
  51. continue;
  52. if (opt->arg_p)
  53. {
  54. /* A non-simple oarg option. */
  55. if (argc < *oind + 2)
  56. error (_("%s: Option %s requires an argument"), prefix, arg);
  57. *oarg = argv[(*oind) + 1];
  58. *oind = (*oind) + 2;
  59. return opt->index;
  60. }
  61. else
  62. {
  63. *oarg = NULL;
  64. *oind = (*oind) + 1;
  65. return opt->index;
  66. }
  67. }
  68. if (error_on_unknown)
  69. error (_("%s: Unknown option ``%s''"), prefix, arg + 1);
  70. else
  71. return -1;
  72. }
  73. int
  74. mi_getopt (const char *prefix,
  75. int argc, char **argv,
  76. const struct mi_opt *opts,
  77. int *oind, char **oarg)
  78. {
  79. return mi_getopt_1 (prefix, argc, argv, opts, oind, oarg, 1);
  80. }
  81. int
  82. mi_getopt_allow_unknown (const char *prefix, int argc, char **argv,
  83. const struct mi_opt *opts, int *oind, char **oarg)
  84. {
  85. return mi_getopt_1 (prefix, argc, argv, opts, oind, oarg, 0);
  86. }
  87. int
  88. mi_valid_noargs (const char *prefix, int argc, char **argv)
  89. {
  90. int oind = 0;
  91. char *oarg;
  92. static const struct mi_opt opts[] =
  93. {
  94. { 0, 0, 0 }
  95. };
  96. if (mi_getopt (prefix, argc, argv, opts, &oind, &oarg) == -1)
  97. return 1;
  98. else
  99. return 0;
  100. }