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)++;
55 int i = host_hex_value (c);
60 if (isdigit (c) && c != '8' && c != '9')
64 i += host_hex_value (c);
104 mi_parse_argv (char *args, struct mi_parse *parse)
108 char **argv = xmalloc ((argc + 1) * sizeof (char *));
113 /* skip leading white space */
114 while (isspace (*chp))
116 /* Three possibilities: EOF, quoted string, or other text. */
125 /* A quoted string. */
127 char *start = chp + 1;
128 /* Determine the buffer size. */
131 while (*chp != '\0' && *chp != '"')
136 if (mi_parse_escape (&chp) <= 0)
138 /* Do not allow split lines or "\000" */
147 /* Insist on a closing quote. */
153 /* Insist on trailing white space. */
154 if (chp[1] != '\0' && !isspace (chp[1]))
159 /* create the buffer. */
160 arg = xmalloc ((len + 1) * sizeof (char));
161 /* And copy the characters in. */
164 while (*chp != '\0' && *chp != '"')
169 arg[len] = mi_parse_escape (&chp);
176 chp++; /* that closing quote. */
181 /* An unquoted string. Accumulate all non blank
182 characters into a buffer. */
185 while (*chp != '\0' && !isspace (*chp))
190 arg = xmalloc ((len + 1) * sizeof (char));
191 strncpy (arg, start, len);
196 /* Append arg to argv. */
197 argv = xrealloc (argv, (argc + 2) * sizeof (char *));
205 mi_parse_free (struct mi_parse *parse)
209 if (parse->command != NULL)
210 xfree (parse->command);
211 if (parse->token != NULL)
212 xfree (parse->token);
213 if (parse->args != NULL)
215 if (parse->argv != NULL)
216 freeargv (parse->argv);
225 struct mi_parse *parse = XMALLOC (struct mi_parse);
226 memset (parse, 0, sizeof (*parse));
228 parse->thread_group = -1;
232 /* Before starting, skip leading white space. */
233 while (isspace (*cmd))
236 /* Find/skip any token and then extract it. */
237 for (chp = cmd; *chp >= '0' && *chp <= '9'; chp++)
239 parse->token = xmalloc ((chp - cmd + 1) * sizeof (char *));
240 memcpy (parse->token, cmd, (chp - cmd));
241 parse->token[chp - cmd] = '\0';
243 /* This wasn't a real MI command. Return it as a CLI_COMMAND. */
246 while (isspace (*chp))
248 parse->command = xstrdup (chp);
249 parse->op = CLI_COMMAND;
253 /* Extract the command. */
255 char *tmp = chp + 1; /* discard ``-'' */
256 for (; *chp && !isspace (*chp); chp++)
258 parse->command = xmalloc ((chp - tmp + 1) * sizeof (char *));
259 memcpy (parse->command, tmp, chp - tmp);
260 parse->command[chp - tmp] = '\0';
263 /* Find the command in the MI table. */
264 parse->cmd = mi_lookup (parse->command);
265 if (parse->cmd == NULL)
267 /* FIXME: This should be a function call. */
270 "%s^error,msg=\"Undefined MI command: %s\"\n",
271 parse->token, parse->command);
272 mi_parse_free (parse);
276 /* Skip white space following the command. */
277 while (isspace (*chp))
280 /* Parse the --thread and --frame options, if present. At present,
281 some important commands, like '-break-*' are implemented by forwarding
282 to the CLI layer directly. We want to parse --thread and --frame
283 here, so as not to leave those option in the string that will be passed
288 size_t as = sizeof ("--all ") - 1;
289 size_t tgs = sizeof ("--thread-group ") - 1;
290 size_t ts = sizeof ("--thread ") - 1;
291 size_t fs = sizeof ("--frame ") - 1;
292 if (strncmp (chp, "--all ", as) == 0)
297 /* See if --all is the last token in the input. */
298 if (strcmp (chp, "--all") == 0)
303 if (strncmp (chp, "--thread-group ", tgs) == 0)
305 if (parse->thread_group != -1)
306 error (_("Duplicate '--thread-group' option"));
309 error (_("Invalid thread group id"));
311 parse->thread_group = strtol (chp, &chp, 10);
313 if (strncmp (chp, "--thread ", ts) == 0)
315 if (parse->thread != -1)
316 error (_("Duplicate '--thread' option"));
318 parse->thread = strtol (chp, &chp, 10);
320 else if (strncmp (chp, "--frame ", fs) == 0)
322 if (parse->frame != -1)
323 error (_("Duplicate '--frame' option"));
325 parse->frame = strtol (chp, &chp, 10);
330 if (*chp != '\0' && !isspace (*chp))
331 error (_("Invalid value for the '%s' option"),
332 start[2] == 't' ? "--thread" : "--frame");
333 while (isspace (*chp))
337 /* For new argv commands, attempt to return the parsed argument
339 if (parse->cmd->argv_func != NULL)
341 mi_parse_argv (chp, parse);
342 if (parse->argv == NULL)
344 /* FIXME: This should be a function call. */
347 "%s^error,msg=\"Problem parsing arguments: %s %s\"\n",
348 parse->token, parse->command, chp);
349 mi_parse_free (parse);
354 /* FIXME: DELETE THIS */
355 /* For CLI commands, also return the remainder of the
356 command line as a single string. */
357 if (parse->cmd->cli.cmd != NULL)
358 parse->args = xstrdup (chp);
361 parse->op = MI_COMMAND;