]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdb/mi/mi-getopt.c
Copyright updates for 2007.
[thirdparty/binutils-gdb.git] / gdb / mi / mi-getopt.c
CommitLineData
fb40c209 1/* MI Command Set - MI Option Parser.
6aba47ca 2 Copyright (C) 2000, 2001, 2007 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
9 the Free Software Foundation; either version 2 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, write to the Free Software
1caae165
EZ
19 Foundation, Inc., 51 Franklin Street, Fifth Floor,
20 Boston, MA 02110-1301, USA. */
fb40c209
AC
21
22#include "defs.h"
23#include "mi-getopt.h"
27b82ed2 24#include "gdb_string.h"
fb40c209
AC
25
26int
27mi_getopt (const char *prefix,
28 int argc, char **argv,
29 struct mi_opt *opts,
30 int *optind, char **optarg)
31{
32 char *arg;
33 struct mi_opt *opt;
34 /* We assume that argv/argc are ok. */
35 if (*optind > argc || *optind < 0)
8e65ff28 36 internal_error (__FILE__, __LINE__,
e2e0b3e5 37 _("mi_getopt_long: optind out of bounds"));
fb40c209
AC
38 if (*optind == argc)
39 return -1;
40 arg = argv[*optind];
41 /* ``--''? */
42 if (strcmp (arg, "--") == 0)
43 {
44 *optind += 1;
45 *optarg = NULL;
46 return -1;
47 }
48 /* End of option list. */
49 if (arg[0] != '-')
50 {
51 *optarg = 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 optarg option. */
62 if (argc < *optind + 2)
8a3fe4f8 63 error (_("%s: Option %s requires an argument"), prefix, arg);
fb40c209
AC
64 *optarg = argv[(*optind) + 1];
65 *optind = (*optind) + 2;
66 return opt->index;
67 }
68 else
69 {
70 *optarg = NULL;
71 *optind = (*optind) + 1;
72 return opt->index;
73 }
74 }
8a3fe4f8 75 error (_("%s: Unknown option ``%s''"), prefix, arg + 1);
fb40c209 76}
1abaf70c
BR
77
78int
79mi_valid_noargs (const char *prefix, int argc, char **argv)
80{
81 int optind = 0;
82 char *optarg;
83 static struct mi_opt opts[] =
84 {
d5d6fca5 85 { 0, 0, 0 }
1abaf70c
BR
86 };
87
88 if (mi_getopt (prefix, argc, argv, opts, &optind, &optarg) == -1)
89 return 1;
90 else
91 return 0;
92}