constify ui_out_impl
[deliverable/binutils-gdb.git] / gdb / mi / mi-getopt.c
1 /* MI Command Set - MI Option Parser.
2 Copyright (C) 2000-2014 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 #include <string.h>
23
24 /* See comments about mi_getopt and mi_getopt_silent in mi-getopt.h.
25 When there is an unknown option, if ERROR_ON_UNKNOWN is true,
26 throw an error, otherwise return -1. */
27
28 static int
29 mi_getopt_1 (const char *prefix, int argc, char **argv,
30 const struct mi_opt *opts, int *oind, char **oarg,
31 int error_on_unknown)
32 {
33 char *arg;
34 const struct mi_opt *opt;
35
36 /* We assume that argv/argc are ok. */
37 if (*oind > argc || *oind < 0)
38 internal_error (__FILE__, __LINE__,
39 _("mi_getopt_long: oind out of bounds"));
40 if (*oind == argc)
41 return -1;
42 arg = argv[*oind];
43 /* ``--''? */
44 if (strcmp (arg, "--") == 0)
45 {
46 *oind += 1;
47 *oarg = NULL;
48 return -1;
49 }
50 /* End of option list. */
51 if (arg[0] != '-')
52 {
53 *oarg = NULL;
54 return -1;
55 }
56 /* Look the option up. */
57 for (opt = opts; opt->name != NULL; opt++)
58 {
59 if (strcmp (opt->name, arg + 1) != 0)
60 continue;
61 if (opt->arg_p)
62 {
63 /* A non-simple oarg option. */
64 if (argc < *oind + 2)
65 error (_("%s: Option %s requires an argument"), prefix, arg);
66 *oarg = argv[(*oind) + 1];
67 *oind = (*oind) + 2;
68 return opt->index;
69 }
70 else
71 {
72 *oarg = NULL;
73 *oind = (*oind) + 1;
74 return opt->index;
75 }
76 }
77
78 if (error_on_unknown)
79 error (_("%s: Unknown option ``%s''"), prefix, arg + 1);
80 else
81 return -1;
82 }
83
84 int
85 mi_getopt (const char *prefix,
86 int argc, char **argv,
87 const struct mi_opt *opts,
88 int *oind, char **oarg)
89 {
90 return mi_getopt_1 (prefix, argc, argv, opts, oind, oarg, 1);
91 }
92
93 int
94 mi_getopt_allow_unknown (const char *prefix, int argc, char **argv,
95 const struct mi_opt *opts, int *oind, char **oarg)
96 {
97 return mi_getopt_1 (prefix, argc, argv, opts, oind, oarg, 0);
98 }
99
100 int
101 mi_valid_noargs (const char *prefix, int argc, char **argv)
102 {
103 int oind = 0;
104 char *oarg;
105 static const struct mi_opt opts[] =
106 {
107 { 0, 0, 0 }
108 };
109
110 if (mi_getopt (prefix, argc, argv, opts, &oind, &oarg) == -1)
111 return 1;
112 else
113 return 0;
114 }
This page took 0.038277 seconds and 4 git commands to generate.