]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdb/mi/mi-getopt.c
Update copyright year range in header of all files managed by GDB
[thirdparty/binutils-gdb.git] / gdb / mi / mi-getopt.c
CommitLineData
fb40c209 1/* MI Command Set - MI Option Parser.
1d506c26 2 Copyright (C) 2000-2024 Free Software Foundation, Inc.
ab91fdd5 3 Contributed by Cygnus Solutions (a Red Hat company).
fb40c209
AC
4
5 This file is part of GDB.
6
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
a9762ec7 9 the Free Software Foundation; either version 3 of the License, or
fb40c209
AC
10 (at your option) any later version.
11
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.
16
17 You should have received a copy of the GNU General Public License
a9762ec7 18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
fb40c209
AC
19
20#include "defs.h"
21#include "mi-getopt.h"
242f1fd7
YQ
22/* See comments about mi_getopt and mi_getopt_silent in mi-getopt.h.
23 When there is an unknown option, if ERROR_ON_UNKNOWN is true,
24 throw an error, otherwise return -1. */
25
26static int
9158e49a
TT
27mi_getopt_1 (const char *prefix, int argc, const char *const *argv,
28 const struct mi_opt *opts, int *oind, const char **oarg,
242f1fd7 29 int error_on_unknown)
fb40c209 30{
9158e49a 31 const char *arg;
91174723 32 const struct mi_opt *opt;
102040f0 33
2b03b41d 34 /* We assume that argv/argc are ok. */
324478ca 35 if (*oind > argc || *oind < 0)
f34652de 36 internal_error (_("mi_getopt_long: oind out of bounds"));
324478ca 37 if (*oind == argc)
fb40c209 38 return -1;
324478ca 39 arg = argv[*oind];
fb40c209
AC
40 /* ``--''? */
41 if (strcmp (arg, "--") == 0)
42 {
324478ca
AS
43 *oind += 1;
44 *oarg = NULL;
fb40c209
AC
45 return -1;
46 }
2b03b41d 47 /* End of option list. */
fb40c209
AC
48 if (arg[0] != '-')
49 {
324478ca 50 *oarg = NULL;
fb40c209
AC
51 return -1;
52 }
2b03b41d 53 /* Look the option up. */
fb40c209
AC
54 for (opt = opts; opt->name != NULL; opt++)
55 {
56 if (strcmp (opt->name, arg + 1) != 0)
57 continue;
58 if (opt->arg_p)
59 {
2b03b41d 60 /* A non-simple oarg option. */
324478ca 61 if (argc < *oind + 2)
8a3fe4f8 62 error (_("%s: Option %s requires an argument"), prefix, arg);
324478ca
AS
63 *oarg = argv[(*oind) + 1];
64 *oind = (*oind) + 2;
fb40c209
AC
65 return opt->index;
66 }
67 else
68 {
324478ca
AS
69 *oarg = NULL;
70 *oind = (*oind) + 1;
fb40c209
AC
71 return opt->index;
72 }
73 }
242f1fd7
YQ
74
75 if (error_on_unknown)
76 error (_("%s: Unknown option ``%s''"), prefix, arg + 1);
77 else
78 return -1;
79}
80
81int
82mi_getopt (const char *prefix,
9158e49a 83 int argc, const char *const *argv,
242f1fd7 84 const struct mi_opt *opts,
9158e49a 85 int *oind, const char **oarg)
242f1fd7
YQ
86{
87 return mi_getopt_1 (prefix, argc, argv, opts, oind, oarg, 1);
88}
89
90int
9158e49a
TT
91mi_getopt_allow_unknown (const char *prefix, int argc,
92 const char *const *argv,
93 const struct mi_opt *opts, int *oind,
94 const char **oarg)
242f1fd7
YQ
95{
96 return mi_getopt_1 (prefix, argc, argv, opts, oind, oarg, 0);
fb40c209 97}
1abaf70c
BR
98
99int
9158e49a 100mi_valid_noargs (const char *prefix, int argc, const char *const *argv)
1abaf70c 101{
324478ca 102 int oind = 0;
9158e49a 103 const char *oarg;
91174723 104 static const struct mi_opt opts[] =
2b03b41d
SS
105 {
106 { 0, 0, 0 }
107 };
1abaf70c 108
324478ca 109 if (mi_getopt (prefix, argc, argv, opts, &oind, &oarg) == -1)
1abaf70c
BR
110 return 1;
111 else
112 return 0;
113}