* mi/mi-getopt.c (mi_getopt): Rename `optind' and `optarg' to
[external/binutils.git] / gdb / mi / mi-getopt.c
1 /* MI Command Set - MI Option Parser.
2    Copyright (C) 2000, 2001, 2007, 2008, 2009, 2010, 2011
3    Free Software Foundation, Inc.
4    Contributed by Cygnus Solutions (a Red Hat company).
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
10    the Free Software Foundation; either version 3 of the License, or
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
19    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
20
21 #include "defs.h"
22 #include "mi-getopt.h"
23 #include "gdb_string.h"
24
25 int
26 mi_getopt (const char *prefix,
27            int argc, char **argv,
28            const struct mi_opt *opts,
29            int *oind, char **oarg)
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   error (_("%s: Unknown option ``%s''"), prefix, arg + 1);
76 }
77
78 int 
79 mi_valid_noargs (const char *prefix, int argc, char **argv) 
80 {
81   int oind = 0;
82   char *oarg;
83   static const struct mi_opt opts[] =
84   {
85     { 0, 0, 0 }
86   };
87
88   if (mi_getopt (prefix, argc, argv, opts, &oind, &oarg) == -1)
89     return 1;
90   else
91     return 0;
92 }