1 /* MI Command Set - MI parser.
3 Copyright (C) 2000, 2001, 2002, 2007, 2008, 2009, 2010
4 Free Software Foundation, Inc.
6 Contributed by Cygnus Solutions (a Red Hat company).
8 This file is part of GDB.
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 3 of the License, or
13 (at your option) any later version.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program. If not, see <http://www.gnu.org/licenses/>. */
29 #include "gdb_string.h"
31 /* Like parse_escape, but leave the results as a host char, not a
35 mi_parse_escape (char **string_ptr)
37 int c = *(*string_ptr)++;
56 int i = host_hex_value (c);
62 if (isdigit (c) && c != '8' && c != '9')
66 i += host_hex_value (c);
106 mi_parse_argv (char *args, struct mi_parse *parse)
110 char **argv = xmalloc ((argc + 1) * sizeof (char *));
117 /* skip leading white space */
118 while (isspace (*chp))
120 /* Three possibilities: EOF, quoted string, or other text. */
129 /* A quoted string. */
131 char *start = chp + 1;
133 /* Determine the buffer size. */
136 while (*chp != '\0' && *chp != '"')
141 if (mi_parse_escape (&chp) <= 0)
143 /* Do not allow split lines or "\000" */
152 /* Insist on a closing quote. */
158 /* Insist on trailing white space. */
159 if (chp[1] != '\0' && !isspace (chp[1]))
164 /* create the buffer. */
165 arg = xmalloc ((len + 1) * sizeof (char));
166 /* And copy the characters in. */
169 while (*chp != '\0' && *chp != '"')
174 arg[len] = mi_parse_escape (&chp);
181 chp++; /* that closing quote. */
186 /* An unquoted string. Accumulate all non blank
187 characters into a buffer. */
191 while (*chp != '\0' && !isspace (*chp))
196 arg = xmalloc ((len + 1) * sizeof (char));
197 strncpy (arg, start, len);
202 /* Append arg to argv. */
203 argv = xrealloc (argv, (argc + 2) * sizeof (char *));
211 mi_parse_free (struct mi_parse *parse)
215 if (parse->command != NULL)
216 xfree (parse->command);
217 if (parse->token != NULL)
218 xfree (parse->token);
219 if (parse->args != NULL)
221 if (parse->argv != NULL)
222 freeargv (parse->argv);
231 struct mi_parse *parse = XMALLOC (struct mi_parse);
233 memset (parse, 0, sizeof (*parse));
235 parse->thread_group = -1;
239 /* Before starting, skip leading white space. */
240 while (isspace (*cmd))
243 /* Find/skip any token and then extract it. */
244 for (chp = cmd; *chp >= '0' && *chp <= '9'; chp++)
246 parse->token = xmalloc ((chp - cmd + 1) * sizeof (char *));
247 memcpy (parse->token, cmd, (chp - cmd));
248 parse->token[chp - cmd] = '\0';
250 /* This wasn't a real MI command. Return it as a CLI_COMMAND. */
253 while (isspace (*chp))
255 parse->command = xstrdup (chp);
256 parse->op = CLI_COMMAND;
260 /* Extract the command. */
262 char *tmp = chp + 1; /* discard ``-'' */
264 for (; *chp && !isspace (*chp); chp++)
266 parse->command = xmalloc ((chp - tmp + 1) * sizeof (char *));
267 memcpy (parse->command, tmp, chp - tmp);
268 parse->command[chp - tmp] = '\0';
271 /* Find the command in the MI table. */
272 parse->cmd = mi_lookup (parse->command);
273 if (parse->cmd == NULL)
275 /* FIXME: This should be a function call. */
278 "%s^error,msg=\"Undefined MI command: %s\"\n",
279 parse->token, parse->command);
280 mi_parse_free (parse);
284 /* Skip white space following the command. */
285 while (isspace (*chp))
288 /* Parse the --thread and --frame options, if present. At present,
289 some important commands, like '-break-*' are implemented by forwarding
290 to the CLI layer directly. We want to parse --thread and --frame
291 here, so as not to leave those option in the string that will be passed
296 size_t as = sizeof ("--all ") - 1;
297 size_t tgs = sizeof ("--thread-group ") - 1;
298 size_t ts = sizeof ("--thread ") - 1;
299 size_t fs = sizeof ("--frame ") - 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)
314 if (parse->thread_group != -1)
315 error (_("Duplicate '--thread-group' option"));
318 error (_("Invalid thread group id"));
320 parse->thread_group = strtol (chp, &chp, 10);
322 else if (strncmp (chp, "--thread ", ts) == 0)
324 if (parse->thread != -1)
325 error (_("Duplicate '--thread' option"));
327 parse->thread = strtol (chp, &chp, 10);
329 else if (strncmp (chp, "--frame ", fs) == 0)
331 if (parse->frame != -1)
332 error (_("Duplicate '--frame' option"));
334 parse->frame = strtol (chp, &chp, 10);
339 if (*chp != '\0' && !isspace (*chp))
340 error (_("Invalid value for the '%s' option"),
341 start[2] == 't' ? "--thread" : "--frame");
342 while (isspace (*chp))
346 /* For new argv commands, attempt to return the parsed argument
348 if (parse->cmd->argv_func != NULL)
350 mi_parse_argv (chp, parse);
351 if (parse->argv == NULL)
353 /* FIXME: This should be a function call. */
356 "%s^error,msg=\"Problem parsing arguments: %s %s\"\n",
357 parse->token, parse->command, chp);
358 mi_parse_free (parse);
363 /* FIXME: DELETE THIS */
364 /* For CLI commands, also return the remainder of the
365 command line as a single string. */
366 if (parse->cmd->cli.cmd != NULL)
367 parse->args = xstrdup (chp);
370 parse->op = MI_COMMAND;