Update copyright year range in header of all files managed by GDB
[binutils-gdb.git] / gdb / mi / mi-getopt.c
blob2fc26485650df1c44a517db2bd2be55a6a1f4cb9
1 /* MI Command Set - MI Option Parser.
2 Copyright (C) 2000-2023 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 "defs.h"
21 #include "mi-getopt.h"
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. */
26 static int
27 mi_getopt_1 (const char *prefix, int argc, char **argv,
28 const struct mi_opt *opts, int *oind, char **oarg,
29 int error_on_unknown)
31 char *arg;
32 const struct mi_opt *opt;
34 /* We assume that argv/argc are ok. */
35 if (*oind > argc || *oind < 0)
36 internal_error (_("mi_getopt_long: oind out of bounds"));
37 if (*oind == argc)
38 return -1;
39 arg = argv[*oind];
40 /* ``--''? */
41 if (strcmp (arg, "--") == 0)
43 *oind += 1;
44 *oarg = NULL;
45 return -1;
47 /* End of option list. */
48 if (arg[0] != '-')
50 *oarg = NULL;
51 return -1;
53 /* Look the option up. */
54 for (opt = opts; opt->name != NULL; opt++)
56 if (strcmp (opt->name, arg + 1) != 0)
57 continue;
58 if (opt->arg_p)
60 /* A non-simple oarg option. */
61 if (argc < *oind + 2)
62 error (_("%s: Option %s requires an argument"), prefix, arg);
63 *oarg = argv[(*oind) + 1];
64 *oind = (*oind) + 2;
65 return opt->index;
67 else
69 *oarg = NULL;
70 *oind = (*oind) + 1;
71 return opt->index;
75 if (error_on_unknown)
76 error (_("%s: Unknown option ``%s''"), prefix, arg + 1);
77 else
78 return -1;
81 int
82 mi_getopt (const char *prefix,
83 int argc, char **argv,
84 const struct mi_opt *opts,
85 int *oind, char **oarg)
87 return mi_getopt_1 (prefix, argc, argv, opts, oind, oarg, 1);
90 int
91 mi_getopt_allow_unknown (const char *prefix, int argc, char **argv,
92 const struct mi_opt *opts, int *oind, char **oarg)
94 return mi_getopt_1 (prefix, argc, argv, opts, oind, oarg, 0);
97 int
98 mi_valid_noargs (const char *prefix, int argc, char **argv)
100 int oind = 0;
101 char *oarg;
102 static const struct mi_opt opts[] =
104 { 0, 0, 0 }
107 if (mi_getopt (prefix, argc, argv, opts, &oind, &oarg) == -1)
108 return 1;
109 else
110 return 0;