1 /* MI Command Set - MI parser.
3 Copyright (C) 2000-2017 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/>. */
28 #include "cli/cli-utils.h"
31 static const char mi_no_values[] = "--no-values";
32 static const char mi_simple_values[] = "--simple-values";
33 static const char mi_all_values[] = "--all-values";
35 /* Like parse_escape, but leave the results as a host char, not a
39 mi_parse_escape (const char **string_ptr)
41 int c = *(*string_ptr)++;
60 int i = host_hex_value (c);
66 if (isdigit (c) && c != '8' && c != '9')
70 i += host_hex_value (c);
110 mi_parse_argv (const char *args, struct mi_parse *parse)
112 const char *chp = args;
114 char **argv = XNEWVEC (char *, argc + 1);
121 /* Skip leading white space. */
122 chp = skip_spaces (chp);
123 /* Three possibilities: EOF, quoted string, or other text. */
132 /* A quoted string. */
134 const char *start = chp + 1;
136 /* Determine the buffer size. */
139 while (*chp != '\0' && *chp != '"')
144 if (mi_parse_escape (&chp) <= 0)
146 /* Do not allow split lines or "\000". */
155 /* Insist on a closing quote. */
161 /* Insist on trailing white space. */
162 if (chp[1] != '\0' && !isspace (chp[1]))
167 /* Create the buffer and copy characters in. */
168 arg = XNEWVEC (char, len + 1);
171 while (*chp != '\0' && *chp != '"')
176 arg[len] = mi_parse_escape (&chp);
183 chp++; /* That closing quote. */
188 /* An unquoted string. Accumulate all non-blank
189 characters into a buffer. */
191 const char *start = chp;
193 while (*chp != '\0' && !isspace (*chp))
198 arg = XNEWVEC (char, len + 1);
199 strncpy (arg, start, len);
204 /* Append arg to argv. */
205 argv = XRESIZEVEC (char *, argv, argc + 2);
211 mi_parse::mi_parse ()
224 language (language_unknown)
228 mi_parse::~mi_parse ()
236 std::unique_ptr<struct mi_parse>
237 mi_parse (const char *cmd, char **token)
240 struct cleanup *cleanup;
242 std::unique_ptr<struct mi_parse> parse (new struct mi_parse);
244 /* Before starting, skip leading white space. */
245 cmd = skip_spaces (cmd);
247 /* Find/skip any token and then extract it. */
248 for (chp = cmd; *chp >= '0' && *chp <= '9'; chp++)
250 *token = (char *) xmalloc (chp - cmd + 1);
251 memcpy (*token, cmd, (chp - cmd));
252 (*token)[chp - cmd] = '\0';
254 /* This wasn't a real MI command. Return it as a CLI_COMMAND. */
257 chp = skip_spaces (chp);
258 parse->command = xstrdup (chp);
259 parse->op = CLI_COMMAND;
264 /* Extract the command. */
266 const char *tmp = chp + 1; /* discard ``-'' */
268 for (; *chp && !isspace (*chp); chp++)
270 parse->command = (char *) xmalloc (chp - tmp + 1);
271 memcpy (parse->command, tmp, chp - tmp);
272 parse->command[chp - tmp] = '\0';
275 /* Find the command in the MI table. */
276 parse->cmd = mi_lookup (parse->command);
277 if (parse->cmd == NULL)
278 throw_error (UNDEFINED_COMMAND_ERROR,
279 _("Undefined MI command: %s"), parse->command);
281 /* Skip white space following the command. */
282 chp = skip_spaces (chp);
284 /* Parse the --thread and --frame options, if present. At present,
285 some important commands, like '-break-*' are implemented by
286 forwarding to the CLI layer directly. We want to parse --thread
287 and --frame here, so as not to leave those option in the string
288 that will be passed to CLI.
290 Same for the --language option. */
295 size_t as = sizeof ("--all ") - 1;
296 size_t tgs = sizeof ("--thread-group ") - 1;
297 size_t ts = sizeof ("--thread ") - 1;
298 size_t fs = sizeof ("--frame ") - 1;
299 size_t ls = sizeof ("--language ") - 1;
301 if (strncmp (chp, "--all ", as) == 0)
306 /* See if --all is the last token in the input. */
307 if (strcmp (chp, "--all") == 0)
312 if (strncmp (chp, "--thread-group ", tgs) == 0)
316 option = "--thread-group";
317 if (parse->thread_group != -1)
318 error (_("Duplicate '--thread-group' option"));
321 error (_("Invalid thread group id"));
323 parse->thread_group = strtol (chp, &endp, 10);
326 else if (strncmp (chp, "--thread ", ts) == 0)
331 if (parse->thread != -1)
332 error (_("Duplicate '--thread' option"));
334 parse->thread = strtol (chp, &endp, 10);
337 else if (strncmp (chp, "--frame ", fs) == 0)
342 if (parse->frame != -1)
343 error (_("Duplicate '--frame' option"));
345 parse->frame = strtol (chp, &endp, 10);
348 else if (strncmp (chp, "--language ", ls) == 0)
350 option = "--language";
352 std::string lang_name = extract_arg (&chp);
354 parse->language = language_enum (lang_name.c_str ());
355 if (parse->language == language_unknown
356 || parse->language == language_auto)
357 error (_("Invalid --language argument: %s"), lang_name.c_str ());
362 if (*chp != '\0' && !isspace (*chp))
363 error (_("Invalid value for the '%s' option"), option);
364 chp = skip_spaces (chp);
367 /* For new argv commands, attempt to return the parsed argument
369 if (parse->cmd->argv_func != NULL)
371 mi_parse_argv (chp, parse.get ());
372 if (parse->argv == NULL)
373 error (_("Problem parsing arguments: %s %s"), parse->command, chp);
376 /* FIXME: DELETE THIS */
377 /* For CLI commands, also return the remainder of the
378 command line as a single string. */
379 if (parse->cmd->cli.cmd != NULL)
380 parse->args = xstrdup (chp);
382 /* Fully parsed, flag as an MI command. */
383 parse->op = MI_COMMAND;
388 mi_parse_print_values (const char *name)
390 if (strcmp (name, "0") == 0
391 || strcmp (name, mi_no_values) == 0)
392 return PRINT_NO_VALUES;
393 else if (strcmp (name, "1") == 0
394 || strcmp (name, mi_all_values) == 0)
395 return PRINT_ALL_VALUES;
396 else if (strcmp (name, "2") == 0
397 || strcmp (name, mi_simple_values) == 0)
398 return PRINT_SIMPLE_VALUES;
400 error (_("Unknown value for PRINT_VALUES: must be: \
401 0 or \"%s\", 1 or \"%s\", 2 or \"%s\""),
402 mi_no_values, mi_all_values, mi_simple_values);