]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdb/mi/mi-getopt.c
2010-05-17 Michael Snyder <msnyder@vmware.com>
[thirdparty/binutils-gdb.git] / gdb / mi / mi-getopt.c
CommitLineData
fb40c209 1/* MI Command Set - MI Option Parser.
4c38e0a4
JB
2 Copyright (C) 2000, 2001, 2007, 2008, 2009, 2010
3 Free Software Foundation, Inc.
ab91fdd5 4 Contributed by Cygnus Solutions (a Red Hat company).
fb40c209
AC
5
6 This file is part of GDB.
7
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
a9762ec7 10 the Free Software Foundation; either version 3 of the License, or
fb40c209
AC
11 (at your option) any later version.
12
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
a9762ec7 19 along with this program. If not, see <http://www.gnu.org/licenses/>. */
fb40c209
AC
20
21#include "defs.h"
22#include "mi-getopt.h"
27b82ed2 23#include "gdb_string.h"
fb40c209
AC
24
25int
26mi_getopt (const char *prefix,
27 int argc, char **argv,
28 struct mi_opt *opts,
29 int *optind, char **optarg)
30{
31 char *arg;
32 struct mi_opt *opt;
33 /* We assume that argv/argc are ok. */
34 if (*optind > argc || *optind < 0)
8e65ff28 35 internal_error (__FILE__, __LINE__,
e2e0b3e5 36 _("mi_getopt_long: optind out of bounds"));
fb40c209
AC
37 if (*optind == argc)
38 return -1;
39 arg = argv[*optind];
40 /* ``--''? */
41 if (strcmp (arg, "--") == 0)
42 {
43 *optind += 1;
44 *optarg = NULL;
45 return -1;
46 }
47 /* End of option list. */
48 if (arg[0] != '-')
49 {
50 *optarg = NULL;
51 return -1;
52 }
53 /* Look the option up. */
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 {
60 /* A non-simple optarg option. */
61 if (argc < *optind + 2)
8a3fe4f8 62 error (_("%s: Option %s requires an argument"), prefix, arg);
fb40c209
AC
63 *optarg = argv[(*optind) + 1];
64 *optind = (*optind) + 2;
65 return opt->index;
66 }
67 else
68 {
69 *optarg = NULL;
70 *optind = (*optind) + 1;
71 return opt->index;
72 }
73 }
8a3fe4f8 74 error (_("%s: Unknown option ``%s''"), prefix, arg + 1);
fb40c209 75}
1abaf70c
BR
76
77int
78mi_valid_noargs (const char *prefix, int argc, char **argv)
79{
80 int optind = 0;
81 char *optarg;
82 static struct mi_opt opts[] =
83 {
d5d6fca5 84 { 0, 0, 0 }
1abaf70c
BR
85 };
86
87 if (mi_getopt (prefix, argc, argv, opts, &optind, &optarg) == -1)
88 return 1;
89 else
90 return 0;
91}