GDB (xrefs)

mi-getopt.c
Go to the documentation of this file.
1 /* MI Command Set - MI Option Parser.
2 Copyright (C) 2000-2018 Free Software Foundation, Inc.
3 Contributed by Cygnus Solutions (a Red Hat company).
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
9 the Free Software Foundation; either version 3 of the License, or
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
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
19
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. */
25
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)
30 {
31 char *arg;
32 const struct mi_opt *opt;
33
34 /* We assume that argv/argc are ok. */
35 if (*oind > argc || *oind < 0)
36 internal_error (__FILE__, __LINE__,
37 _("mi_getopt_long: oind out of bounds"));
38 if (*oind == argc)
39 return -1;
40 arg = argv[*oind];
41 /* ``--''? */
42 if (strcmp (arg, "--") == 0)
43 {
44 *oind += 1;
45 *oarg = NULL;
46 return -1;
47 }
48 /* End of option list. */
49 if (arg[0] != '-')
50 {
51 *oarg = NULL;
52 return -1;
53 }
54 /* Look the option up. */
55 for (opt = opts; opt->name != NULL; opt++)
56 {
57 if (strcmp (opt->name, arg + 1) != 0)
58 continue;
59 if (opt->arg_p)
60 {
61 /* A non-simple oarg option. */
62 if (argc < *oind + 2)
63 error (_("%s: Option %s requires an argument"), prefix, arg);
64 *oarg = argv[(*oind) + 1];
65 *oind = (*oind) + 2;
66 return opt->index;
67 }
68 else
69 {
70 *oarg = NULL;
71 *oind = (*oind) + 1;
72 return opt->index;
73 }
74 }
75
76 if (error_on_unknown)
77 error (_("%s: Unknown option ``%s''"), prefix, arg + 1);
78 else
79 return -1;
80 }
81
82 int
83 mi_getopt (const char *prefix,
84 int argc, char **argv,
85 const struct mi_opt *opts,
86 int *oind, char **oarg)
87 {
88 return mi_getopt_1 (prefix, argc, argv, opts, oind, oarg, 1);
89 }
90
91 int
92 mi_getopt_allow_unknown (const char *prefix, int argc, char **argv,
93 const struct mi_opt *opts, int *oind, char **oarg)
94 {
95 return mi_getopt_1 (prefix, argc, argv, opts, oind, oarg, 0);
96 }
97
98 int
99 mi_valid_noargs (const char *prefix, int argc, char **argv)
100 {
101 int oind = 0;
102 char *oarg;
103 static const struct mi_opt opts[] =
104 {
105 { 0, 0, 0 }
106 };
107
108 if (mi_getopt (prefix, argc, argv, opts, &oind, &oarg) == -1)
109 return 1;
110 else
111 return 0;
112 }
mi_opt::name
const char * name
Definition: mi-getopt.h:28
internal_error
void internal_error(const char *file, int line, const char *fmt,...)
Definition: errors.c:50
_
#define _(String)
Definition: gdb_locale.h:35
defs.h
mi_getopt
int mi_getopt(const char *prefix, int argc, char **argv, const struct mi_opt *opts, int *oind, char **oarg)
Definition: mi-getopt.c:83
mi_opt::arg_p
int arg_p
Definition: mi-getopt.h:30
mi_getopt_1
static int mi_getopt_1(const char *prefix, int argc, char **argv, const struct mi_opt *opts, int *oind, char **oarg, int error_on_unknown)
Definition: mi-getopt.c:27
mi_valid_noargs
int mi_valid_noargs(const char *prefix, int argc, char **argv)
Definition: mi-getopt.c:99
mi_getopt_allow_unknown
int mi_getopt_allow_unknown(const char *prefix, int argc, char **argv, const struct mi_opt *opts, int *oind, char **oarg)
Definition: mi-getopt.c:92
mi_opt::index
int index
Definition: mi-getopt.h:29
gdb.argv
argv
Definition: __init__.py:63
mi-getopt.h
error
void error(const char *fmt,...)
Definition: errors.c:38
mi_opt
Definition: mi-getopt.h:26