GDB (API)
|
00001 /* MI Command Set - MI Option Parser. 00002 Copyright (C) 2000-2013 Free Software Foundation, Inc. 00003 Contributed by Cygnus Solutions (a Red Hat company). 00004 00005 This file is part of GDB. 00006 00007 This program is free software; you can redistribute it and/or modify 00008 it under the terms of the GNU General Public License as published by 00009 the Free Software Foundation; either version 3 of the License, or 00010 (at your option) any later version. 00011 00012 This program is distributed in the hope that it will be useful, 00013 but WITHOUT ANY WARRANTY; without even the implied warranty of 00014 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00015 GNU General Public License for more details. 00016 00017 You should have received a copy of the GNU General Public License 00018 along with this program. If not, see <http://www.gnu.org/licenses/>. */ 00019 00020 #include "defs.h" 00021 #include "mi-getopt.h" 00022 #include "gdb_string.h" 00023 00024 /* See comments about mi_getopt and mi_getopt_silent in mi-getopt.h. 00025 When there is an unknown option, if ERROR_ON_UNKNOWN is true, 00026 throw an error, otherwise return -1. */ 00027 00028 static int 00029 mi_getopt_1 (const char *prefix, int argc, char **argv, 00030 const struct mi_opt *opts, int *oind, char **oarg, 00031 int error_on_unknown) 00032 { 00033 char *arg; 00034 const struct mi_opt *opt; 00035 00036 /* We assume that argv/argc are ok. */ 00037 if (*oind > argc || *oind < 0) 00038 internal_error (__FILE__, __LINE__, 00039 _("mi_getopt_long: oind out of bounds")); 00040 if (*oind == argc) 00041 return -1; 00042 arg = argv[*oind]; 00043 /* ``--''? */ 00044 if (strcmp (arg, "--") == 0) 00045 { 00046 *oind += 1; 00047 *oarg = NULL; 00048 return -1; 00049 } 00050 /* End of option list. */ 00051 if (arg[0] != '-') 00052 { 00053 *oarg = NULL; 00054 return -1; 00055 } 00056 /* Look the option up. */ 00057 for (opt = opts; opt->name != NULL; opt++) 00058 { 00059 if (strcmp (opt->name, arg + 1) != 0) 00060 continue; 00061 if (opt->arg_p) 00062 { 00063 /* A non-simple oarg option. */ 00064 if (argc < *oind + 2) 00065 error (_("%s: Option %s requires an argument"), prefix, arg); 00066 *oarg = argv[(*oind) + 1]; 00067 *oind = (*oind) + 2; 00068 return opt->index; 00069 } 00070 else 00071 { 00072 *oarg = NULL; 00073 *oind = (*oind) + 1; 00074 return opt->index; 00075 } 00076 } 00077 00078 if (error_on_unknown) 00079 error (_("%s: Unknown option ``%s''"), prefix, arg + 1); 00080 else 00081 return -1; 00082 } 00083 00084 int 00085 mi_getopt (const char *prefix, 00086 int argc, char **argv, 00087 const struct mi_opt *opts, 00088 int *oind, char **oarg) 00089 { 00090 return mi_getopt_1 (prefix, argc, argv, opts, oind, oarg, 1); 00091 } 00092 00093 int 00094 mi_getopt_allow_unknown (const char *prefix, int argc, char **argv, 00095 const struct mi_opt *opts, int *oind, char **oarg) 00096 { 00097 return mi_getopt_1 (prefix, argc, argv, opts, oind, oarg, 0); 00098 } 00099 00100 int 00101 mi_valid_noargs (const char *prefix, int argc, char **argv) 00102 { 00103 int oind = 0; 00104 char *oarg; 00105 static const struct mi_opt opts[] = 00106 { 00107 { 0, 0, 0 } 00108 }; 00109 00110 if (mi_getopt (prefix, argc, argv, opts, &oind, &oarg) == -1) 00111 return 1; 00112 else 00113 return 0; 00114 }