1 /* MI Command Set - MI parser.
3 Copyright (C) 2000-2013 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 "gdb_string.h"
29 #include "cli/cli-utils.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 chp
= skip_spaces (chp
);
119 /* Three possibilities: EOF, quoted string, or other text. */
128 /* A quoted string. */
130 char *start
= chp
+ 1;
132 /* Determine the buffer size. */
135 while (*chp
!= '\0' && *chp
!= '"')
140 if (mi_parse_escape (&chp
) <= 0)
142 /* Do not allow split lines or "\000". */
151 /* Insist on a closing quote. */
157 /* Insist on trailing white space. */
158 if (chp
[1] != '\0' && !isspace (chp
[1]))
163 /* Create the buffer and copy characters in. */
164 arg
= xmalloc ((len
+ 1) * sizeof (char));
167 while (*chp
!= '\0' && *chp
!= '"')
172 arg
[len
] = mi_parse_escape (&chp
);
179 chp
++; /* That closing quote. */
184 /* An unquoted string. Accumulate all non-blank
185 characters into a buffer. */
189 while (*chp
!= '\0' && !isspace (*chp
))
194 arg
= xmalloc ((len
+ 1) * sizeof (char));
195 strncpy (arg
, start
, len
);
200 /* Append arg to argv. */
201 argv
= xrealloc (argv
, (argc
+ 2) * sizeof (char *));
208 mi_parse_free (struct mi_parse
*parse
)
212 if (parse
->command
!= NULL
)
213 xfree (parse
->command
);
214 if (parse
->token
!= NULL
)
215 xfree (parse
->token
);
216 if (parse
->args
!= NULL
)
218 if (parse
->argv
!= NULL
)
219 freeargv (parse
->argv
);
223 /* A cleanup that calls mi_parse_free. */
226 mi_parse_cleanup (void *arg
)
232 mi_parse (char *cmd
, char **token
)
235 struct mi_parse
*parse
= XMALLOC (struct mi_parse
);
236 struct cleanup
*cleanup
;
238 memset (parse
, 0, sizeof (*parse
));
240 parse
->thread_group
= -1;
244 cleanup
= make_cleanup (mi_parse_cleanup
, parse
);
246 /* Before starting, skip leading white space. */
247 cmd
= skip_spaces (cmd
);
249 /* Find/skip any token and then extract it. */
250 for (chp
= cmd
; *chp
>= '0' && *chp
<= '9'; chp
++)
252 *token
= xmalloc (chp
- cmd
+ 1);
253 memcpy (*token
, cmd
, (chp
- cmd
));
254 (*token
)[chp
- cmd
] = '\0';
256 /* This wasn't a real MI command. Return it as a CLI_COMMAND. */
259 chp
= skip_spaces (chp
);
260 parse
->command
= xstrdup (chp
);
261 parse
->op
= CLI_COMMAND
;
263 discard_cleanups (cleanup
);
268 /* Extract the command. */
270 char *tmp
= chp
+ 1; /* discard ``-'' */
272 for (; *chp
&& !isspace (*chp
); chp
++)
274 parse
->command
= xmalloc (chp
- tmp
+ 1);
275 memcpy (parse
->command
, tmp
, chp
- tmp
);
276 parse
->command
[chp
- tmp
] = '\0';
279 /* Find the command in the MI table. */
280 parse
->cmd
= mi_lookup (parse
->command
);
281 if (parse
->cmd
== NULL
)
282 error (_("Undefined MI command: %s"), parse
->command
);
284 /* Skip white space following the command. */
285 chp
= skip_spaces (chp
);
287 /* Parse the --thread and --frame options, if present. At present,
288 some important commands, like '-break-*' are implemented by
289 forwarding to the CLI layer directly. We want to parse --thread
290 and --frame here, so as not to leave those option in the string
291 that will be passed to CLI. */
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;
300 if (strncmp (chp
, "--all ", as
) == 0)
305 /* See if --all is the last token in the input. */
306 if (strcmp (chp
, "--all") == 0)
311 if (strncmp (chp
, "--thread-group ", tgs
) == 0)
313 option
= "--thread-group";
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)
325 if (parse
->thread
!= -1)
326 error (_("Duplicate '--thread' option"));
328 parse
->thread
= strtol (chp
, &chp
, 10);
330 else if (strncmp (chp
, "--frame ", fs
) == 0)
333 if (parse
->frame
!= -1)
334 error (_("Duplicate '--frame' option"));
336 parse
->frame
= strtol (chp
, &chp
, 10);
341 if (*chp
!= '\0' && !isspace (*chp
))
342 error (_("Invalid value for the '%s' option"), option
);
343 chp
= skip_spaces (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
)
352 error (_("Problem parsing arguments: %s %s"), parse
->command
, chp
);
355 /* FIXME: DELETE THIS */
356 /* For CLI commands, also return the remainder of the
357 command line as a single string. */
358 if (parse
->cmd
->cli
.cmd
!= NULL
)
359 parse
->args
= xstrdup (chp
);
361 discard_cleanups (cleanup
);
363 /* Fully parsed, flag as an MI command. */
364 parse
->op
= MI_COMMAND
;
This page took 0.037769 seconds and 4 git commands to generate.