1 /* MI Command Set - MI parser.
3 Copyright (C) 2000-2014 Free Software Foundation, Inc.
5 Contributed by Cygnus Solutions (a Red Hat company).
7 This file is part of GDB.
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program. If not, see <http://www.gnu.org/licenses/>. */
29 #include "cli/cli-utils.h"
32 static const char mi_no_values[] = "--no-values";
33 static const char mi_simple_values[] = "--simple-values";
34 static const char mi_all_values[] = "--all-values";
36 /* Like parse_escape, but leave the results as a host char, not a
40 mi_parse_escape (const char **string_ptr)
42 int c = *(*string_ptr)++;
61 int i = host_hex_value (c);
67 if (isdigit (c) && c != '8' && c != '9')
71 i += host_hex_value (c);
111 mi_parse_argv (const char *args, struct mi_parse *parse)
113 const char *chp = args;
115 char **argv = xmalloc ((argc + 1) * sizeof (char *));
122 /* Skip leading white space. */
123 chp = skip_spaces_const (chp);
124 /* Three possibilities: EOF, quoted string, or other text. */
133 /* A quoted string. */
135 const char *start = chp + 1;
137 /* Determine the buffer size. */
140 while (*chp != '\0' && *chp != '"')
145 if (mi_parse_escape (&chp) <= 0)
147 /* Do not allow split lines or "\000". */
156 /* Insist on a closing quote. */
162 /* Insist on trailing white space. */
163 if (chp[1] != '\0' && !isspace (chp[1]))
168 /* Create the buffer and copy characters in. */
169 arg = xmalloc ((len + 1) * sizeof (char));
172 while (*chp != '\0' && *chp != '"')
177 arg[len] = mi_parse_escape (&chp);
184 chp++; /* That closing quote. */
189 /* An unquoted string. Accumulate all non-blank
190 characters into a buffer. */
192 const char *start = chp;
194 while (*chp != '\0' && !isspace (*chp))
199 arg = xmalloc ((len + 1) * sizeof (char));
200 strncpy (arg, start, len);
205 /* Append arg to argv. */
206 argv = xrealloc (argv, (argc + 2) * sizeof (char *));
213 mi_parse_free (struct mi_parse *parse)
217 if (parse->command != NULL)
218 xfree (parse->command);
219 if (parse->token != NULL)
220 xfree (parse->token);
221 if (parse->args != NULL)
223 if (parse->argv != NULL)
224 freeargv (parse->argv);
228 /* A cleanup that calls mi_parse_free. */
231 mi_parse_cleanup (void *arg)
237 mi_parse (const char *cmd, char **token)
240 struct mi_parse *parse = XNEW (struct mi_parse);
241 struct cleanup *cleanup;
243 memset (parse, 0, sizeof (*parse));
245 parse->thread_group = -1;
248 parse->language = language_unknown;
250 cleanup = make_cleanup (mi_parse_cleanup, parse);
252 /* Before starting, skip leading white space. */
253 cmd = skip_spaces_const (cmd);
255 /* Find/skip any token and then extract it. */
256 for (chp = cmd; *chp >= '0' && *chp <= '9'; chp++)
258 *token = xmalloc (chp - cmd + 1);
259 memcpy (*token, cmd, (chp - cmd));
260 (*token)[chp - cmd] = '\0';
262 /* This wasn't a real MI command. Return it as a CLI_COMMAND. */
265 chp = skip_spaces_const (chp);
266 parse->command = xstrdup (chp);
267 parse->op = CLI_COMMAND;
269 discard_cleanups (cleanup);
274 /* Extract the command. */
276 const char *tmp = chp + 1; /* discard ``-'' */
278 for (; *chp && !isspace (*chp); chp++)
280 parse->command = xmalloc (chp - tmp + 1);
281 memcpy (parse->command, tmp, chp - tmp);
282 parse->command[chp - tmp] = '\0';
285 /* Find the command in the MI table. */
286 parse->cmd = mi_lookup (parse->command);
287 if (parse->cmd == NULL)
288 throw_error (UNDEFINED_COMMAND_ERROR,
289 _("Undefined MI command: %s"), parse->command);
291 /* Skip white space following the command. */
292 chp = skip_spaces_const (chp);
294 /* Parse the --thread and --frame options, if present. At present,
295 some important commands, like '-break-*' are implemented by
296 forwarding to the CLI layer directly. We want to parse --thread
297 and --frame here, so as not to leave those option in the string
298 that will be passed to CLI.
300 Same for the --language option. */
305 size_t as = sizeof ("--all ") - 1;
306 size_t tgs = sizeof ("--thread-group ") - 1;
307 size_t ts = sizeof ("--thread ") - 1;
308 size_t fs = sizeof ("--frame ") - 1;
309 size_t ls = sizeof ("--language ") - 1;
311 if (strncmp (chp, "--all ", as) == 0)
316 /* See if --all is the last token in the input. */
317 if (strcmp (chp, "--all") == 0)
322 if (strncmp (chp, "--thread-group ", tgs) == 0)
326 option = "--thread-group";
327 if (parse->thread_group != -1)
328 error (_("Duplicate '--thread-group' option"));
331 error (_("Invalid thread group id"));
333 parse->thread_group = strtol (chp, &endp, 10);
336 else if (strncmp (chp, "--thread ", ts) == 0)
341 if (parse->thread != -1)
342 error (_("Duplicate '--thread' option"));
344 parse->thread = strtol (chp, &endp, 10);
347 else if (strncmp (chp, "--frame ", fs) == 0)
352 if (parse->frame != -1)
353 error (_("Duplicate '--frame' option"));
355 parse->frame = strtol (chp, &endp, 10);
358 else if (strncmp (chp, "--language ", ls) == 0)
361 struct cleanup *old_chain;
363 option = "--language";
365 lang_name = extract_arg_const (&chp);
366 old_chain = make_cleanup (xfree, lang_name);
368 parse->language = language_enum (lang_name);
369 if (parse->language == language_unknown
370 || parse->language == language_auto)
371 error (_("Invalid --language argument: %s"), lang_name);
373 do_cleanups (old_chain);
378 if (*chp != '\0' && !isspace (*chp))
379 error (_("Invalid value for the '%s' option"), option);
380 chp = skip_spaces_const (chp);
383 /* For new argv commands, attempt to return the parsed argument
385 if (parse->cmd->argv_func != NULL)
387 mi_parse_argv (chp, parse);
388 if (parse->argv == NULL)
389 error (_("Problem parsing arguments: %s %s"), parse->command, chp);
392 /* FIXME: DELETE THIS */
393 /* For CLI commands, also return the remainder of the
394 command line as a single string. */
395 if (parse->cmd->cli.cmd != NULL)
396 parse->args = xstrdup (chp);
398 discard_cleanups (cleanup);
400 /* Fully parsed, flag as an MI command. */
401 parse->op = MI_COMMAND;
406 mi_parse_print_values (const char *name)
408 if (strcmp (name, "0") == 0
409 || strcmp (name, mi_no_values) == 0)
410 return PRINT_NO_VALUES;
411 else if (strcmp (name, "1") == 0
412 || strcmp (name, mi_all_values) == 0)
413 return PRINT_ALL_VALUES;
414 else if (strcmp (name, "2") == 0
415 || strcmp (name, mi_simple_values) == 0)
416 return PRINT_SIMPLE_VALUES;
418 error (_("Unknown value for PRINT_VALUES: must be: \
419 0 or \"%s\", 1 or \"%s\", 2 or \"%s\""),
420 mi_no_values, mi_all_values, mi_simple_values);