15796c8dcSSimon Schubert /* MI Command Set - MI Option Parser.
2*ef5ccd6cSJohn Marino Copyright (C) 2000-2013 Free Software Foundation, Inc.
35796c8dcSSimon Schubert Contributed by Cygnus Solutions (a Red Hat company).
45796c8dcSSimon Schubert
55796c8dcSSimon Schubert This file is part of GDB.
65796c8dcSSimon Schubert
75796c8dcSSimon Schubert This program is free software; you can redistribute it and/or modify
85796c8dcSSimon Schubert it under the terms of the GNU General Public License as published by
95796c8dcSSimon Schubert the Free Software Foundation; either version 3 of the License, or
105796c8dcSSimon Schubert (at your option) any later version.
115796c8dcSSimon Schubert
125796c8dcSSimon Schubert This program is distributed in the hope that it will be useful,
135796c8dcSSimon Schubert but WITHOUT ANY WARRANTY; without even the implied warranty of
145796c8dcSSimon Schubert MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
155796c8dcSSimon Schubert GNU General Public License for more details.
165796c8dcSSimon Schubert
175796c8dcSSimon Schubert You should have received a copy of the GNU General Public License
185796c8dcSSimon Schubert along with this program. If not, see <http://www.gnu.org/licenses/>. */
195796c8dcSSimon Schubert
205796c8dcSSimon Schubert #include "defs.h"
215796c8dcSSimon Schubert #include "mi-getopt.h"
225796c8dcSSimon Schubert #include "gdb_string.h"
235796c8dcSSimon Schubert
245796c8dcSSimon Schubert int
mi_getopt(const char * prefix,int argc,char ** argv,const struct mi_opt * opts,int * oind,char ** oarg)255796c8dcSSimon Schubert mi_getopt (const char *prefix,
265796c8dcSSimon Schubert int argc, char **argv,
27a45ae5f8SJohn Marino const struct mi_opt *opts,
28*ef5ccd6cSJohn Marino int *oind, char **oarg)
295796c8dcSSimon Schubert {
305796c8dcSSimon Schubert char *arg;
31a45ae5f8SJohn Marino const struct mi_opt *opt;
32cf7f2e2dSJohn Marino
335796c8dcSSimon Schubert /* We assume that argv/argc are ok. */
34*ef5ccd6cSJohn Marino if (*oind > argc || *oind < 0)
355796c8dcSSimon Schubert internal_error (__FILE__, __LINE__,
36*ef5ccd6cSJohn Marino _("mi_getopt_long: oind out of bounds"));
37*ef5ccd6cSJohn Marino if (*oind == argc)
385796c8dcSSimon Schubert return -1;
39*ef5ccd6cSJohn Marino arg = argv[*oind];
405796c8dcSSimon Schubert /* ``--''? */
415796c8dcSSimon Schubert if (strcmp (arg, "--") == 0)
425796c8dcSSimon Schubert {
43*ef5ccd6cSJohn Marino *oind += 1;
44*ef5ccd6cSJohn Marino *oarg = NULL;
455796c8dcSSimon Schubert return -1;
465796c8dcSSimon Schubert }
475796c8dcSSimon Schubert /* End of option list. */
485796c8dcSSimon Schubert if (arg[0] != '-')
495796c8dcSSimon Schubert {
50*ef5ccd6cSJohn Marino *oarg = NULL;
515796c8dcSSimon Schubert return -1;
525796c8dcSSimon Schubert }
535796c8dcSSimon Schubert /* Look the option up. */
545796c8dcSSimon Schubert for (opt = opts; opt->name != NULL; opt++)
555796c8dcSSimon Schubert {
565796c8dcSSimon Schubert if (strcmp (opt->name, arg + 1) != 0)
575796c8dcSSimon Schubert continue;
585796c8dcSSimon Schubert if (opt->arg_p)
595796c8dcSSimon Schubert {
60*ef5ccd6cSJohn Marino /* A non-simple oarg option. */
61*ef5ccd6cSJohn Marino if (argc < *oind + 2)
625796c8dcSSimon Schubert error (_("%s: Option %s requires an argument"), prefix, arg);
63*ef5ccd6cSJohn Marino *oarg = argv[(*oind) + 1];
64*ef5ccd6cSJohn Marino *oind = (*oind) + 2;
655796c8dcSSimon Schubert return opt->index;
665796c8dcSSimon Schubert }
675796c8dcSSimon Schubert else
685796c8dcSSimon Schubert {
69*ef5ccd6cSJohn Marino *oarg = NULL;
70*ef5ccd6cSJohn Marino *oind = (*oind) + 1;
715796c8dcSSimon Schubert return opt->index;
725796c8dcSSimon Schubert }
735796c8dcSSimon Schubert }
745796c8dcSSimon Schubert error (_("%s: Unknown option ``%s''"), prefix, arg + 1);
755796c8dcSSimon Schubert }
765796c8dcSSimon Schubert
775796c8dcSSimon Schubert int
mi_valid_noargs(const char * prefix,int argc,char ** argv)785796c8dcSSimon Schubert mi_valid_noargs (const char *prefix, int argc, char **argv)
795796c8dcSSimon Schubert {
80*ef5ccd6cSJohn Marino int oind = 0;
81*ef5ccd6cSJohn Marino char *oarg;
82a45ae5f8SJohn Marino static const struct mi_opt opts[] =
835796c8dcSSimon Schubert {
845796c8dcSSimon Schubert { 0, 0, 0 }
855796c8dcSSimon Schubert };
865796c8dcSSimon Schubert
87*ef5ccd6cSJohn Marino if (mi_getopt (prefix, argc, argv, opts, &oind, &oarg) == -1)
885796c8dcSSimon Schubert return 1;
895796c8dcSSimon Schubert else
905796c8dcSSimon Schubert return 0;
915796c8dcSSimon Schubert }
92