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