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