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);
226 /* A cleanup that calls mi_parse_free. */
229 mi_parse_cleanup (void *arg)
235 mi_parse (char *cmd, char **token)
238 struct mi_parse *parse = XMALLOC (struct mi_parse);
239 struct cleanup *cleanup;
241 memset (parse, 0, sizeof (*parse));
243 parse->thread_group = -1;
247 cleanup = make_cleanup (mi_parse_cleanup, parse);
249 /* Before starting, skip leading white space. */
250 while (isspace (*cmd))
253 /* Find/skip any token and then extract it. */
254 for (chp = cmd; *chp >= '0' && *chp <= '9'; chp++)
256 *token = xmalloc ((chp - cmd + 1) * sizeof (char *));
257 memcpy (*token, cmd, (chp - cmd));
258 (*token)[chp - cmd] = '\0';
260 /* This wasn't a real MI command. Return it as a CLI_COMMAND. */
263 while (isspace (*chp))
265 parse->command = xstrdup (chp);
266 parse->op = CLI_COMMAND;
268 discard_cleanups (cleanup);
273 /* Extract the command. */
275 char *tmp = chp + 1; /* discard ``-'' */
277 for (; *chp && !isspace (*chp); chp++)
279 parse->command = xmalloc ((chp - tmp + 1) * sizeof (char *));
280 memcpy (parse->command, tmp, chp - tmp);
281 parse->command[chp - tmp] = '\0';
284 /* Find the command in the MI table. */
285 parse->cmd = mi_lookup (parse->command);
286 if (parse->cmd == NULL)
287 error (_("Undefined MI command: %s"), parse->command);
289 /* Skip white space following the command. */
290 while (isspace (*chp))
293 /* Parse the --thread and --frame options, if present. At present,
294 some important commands, like '-break-*' are implemented by forwarding
295 to the CLI layer directly. We want to parse --thread and --frame
296 here, so as not to leave those option in the string that will be passed
301 size_t as = sizeof ("--all ") - 1;
302 size_t tgs = sizeof ("--thread-group ") - 1;
303 size_t ts = sizeof ("--thread ") - 1;
304 size_t fs = sizeof ("--frame ") - 1;
306 if (strncmp (chp, "--all ", as) == 0)
311 /* See if --all is the last token in the input. */
312 if (strcmp (chp, "--all") == 0)
317 if (strncmp (chp, "--thread-group ", tgs) == 0)
319 option = "--thread-group";
320 if (parse->thread_group != -1)
321 error (_("Duplicate '--thread-group' option"));
324 error (_("Invalid thread group id"));
326 parse->thread_group = strtol (chp, &chp, 10);
328 else if (strncmp (chp, "--thread ", ts) == 0)
331 if (parse->thread != -1)
332 error (_("Duplicate '--thread' option"));
334 parse->thread = strtol (chp, &chp, 10);
336 else if (strncmp (chp, "--frame ", fs) == 0)
339 if (parse->frame != -1)
340 error (_("Duplicate '--frame' option"));
342 parse->frame = strtol (chp, &chp, 10);
347 if (*chp != '\0' && !isspace (*chp))
348 error (_("Invalid value for the '%s' option"), option);
349 while (isspace (*chp))
353 /* For new argv commands, attempt to return the parsed argument
355 if (parse->cmd->argv_func != NULL)
357 mi_parse_argv (chp, parse);
358 if (parse->argv == NULL)
359 error (_("Problem parsing arguments: %s %s"), parse->command, chp);
362 /* FIXME: DELETE THIS */
363 /* For CLI commands, also return the remainder of the
364 command line as a single string. */
365 if (parse->cmd->cli.cmd != NULL)
366 parse->args = xstrdup (chp);
368 discard_cleanups (cleanup);
371 parse->op = MI_COMMAND;