1 /* MI Command Set - MI parser.
3 Copyright (C) 2000-2002, 2007-2012 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"
30 /* Like parse_escape, but leave the results as a host char, not a
34 mi_parse_escape (char **string_ptr
)
36 int c
= *(*string_ptr
)++;
55 int i
= host_hex_value (c
);
61 if (isdigit (c
) && c
!= '8' && c
!= '9')
65 i
+= host_hex_value (c
);
105 mi_parse_argv (char *args
, struct mi_parse
*parse
)
109 char **argv
= xmalloc ((argc
+ 1) * sizeof (char *));
116 /* skip leading white space */
117 while (isspace (*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. */
164 arg
= xmalloc ((len
+ 1) * sizeof (char));
165 /* And copy the characters in. */
168 while (*chp
!= '\0' && *chp
!= '"')
173 arg
[len
] = mi_parse_escape (&chp
);
180 chp
++; /* that closing quote. */
185 /* An unquoted string. Accumulate all non blank
186 characters into a buffer. */
190 while (*chp
!= '\0' && !isspace (*chp
))
195 arg
= xmalloc ((len
+ 1) * sizeof (char));
196 strncpy (arg
, start
, len
);
201 /* Append arg to argv. */
202 argv
= xrealloc (argv
, (argc
+ 2) * sizeof (char *));
210 mi_parse_free (struct mi_parse
*parse
)
214 if (parse
->command
!= NULL
)
215 xfree (parse
->command
);
216 if (parse
->token
!= NULL
)
217 xfree (parse
->token
);
218 if (parse
->args
!= NULL
)
220 if (parse
->argv
!= NULL
)
221 freeargv (parse
->argv
);
225 /* A cleanup that calls mi_parse_free. */
228 mi_parse_cleanup (void *arg
)
234 mi_parse (char *cmd
, char **token
)
237 struct mi_parse
*parse
= XMALLOC (struct mi_parse
);
238 struct cleanup
*cleanup
;
240 memset (parse
, 0, sizeof (*parse
));
242 parse
->thread_group
= -1;
246 cleanup
= make_cleanup (mi_parse_cleanup
, parse
);
248 /* Before starting, skip leading white space. */
249 while (isspace (*cmd
))
252 /* Find/skip any token and then extract it. */
253 for (chp
= cmd
; *chp
>= '0' && *chp
<= '9'; chp
++)
255 *token
= xmalloc (chp
- cmd
+ 1);
256 memcpy (*token
, cmd
, (chp
- cmd
));
257 (*token
)[chp
- cmd
] = '\0';
259 /* This wasn't a real MI command. Return it as a CLI_COMMAND. */
262 while (isspace (*chp
))
264 parse
->command
= xstrdup (chp
);
265 parse
->op
= CLI_COMMAND
;
267 discard_cleanups (cleanup
);
272 /* Extract the command. */
274 char *tmp
= chp
+ 1; /* discard ``-'' */
276 for (; *chp
&& !isspace (*chp
); chp
++)
278 parse
->command
= xmalloc (chp
- tmp
+ 1);
279 memcpy (parse
->command
, tmp
, chp
- tmp
);
280 parse
->command
[chp
- tmp
] = '\0';
283 /* Find the command in the MI table. */
284 parse
->cmd
= mi_lookup (parse
->command
);
285 if (parse
->cmd
== NULL
)
286 error (_("Undefined MI command: %s"), parse
->command
);
288 /* Skip white space following the command. */
289 while (isspace (*chp
))
292 /* Parse the --thread and --frame options, if present. At present,
293 some important commands, like '-break-*' are implemented by forwarding
294 to the CLI layer directly. We want to parse --thread and --frame
295 here, so as not to leave those option in the string that will be passed
300 size_t as
= sizeof ("--all ") - 1;
301 size_t tgs
= sizeof ("--thread-group ") - 1;
302 size_t ts
= sizeof ("--thread ") - 1;
303 size_t fs
= sizeof ("--frame ") - 1;
305 if (strncmp (chp
, "--all ", as
) == 0)
310 /* See if --all is the last token in the input. */
311 if (strcmp (chp
, "--all") == 0)
316 if (strncmp (chp
, "--thread-group ", tgs
) == 0)
318 option
= "--thread-group";
319 if (parse
->thread_group
!= -1)
320 error (_("Duplicate '--thread-group' option"));
323 error (_("Invalid thread group id"));
325 parse
->thread_group
= strtol (chp
, &chp
, 10);
327 else if (strncmp (chp
, "--thread ", ts
) == 0)
330 if (parse
->thread
!= -1)
331 error (_("Duplicate '--thread' option"));
333 parse
->thread
= strtol (chp
, &chp
, 10);
335 else if (strncmp (chp
, "--frame ", fs
) == 0)
338 if (parse
->frame
!= -1)
339 error (_("Duplicate '--frame' option"));
341 parse
->frame
= strtol (chp
, &chp
, 10);
346 if (*chp
!= '\0' && !isspace (*chp
))
347 error (_("Invalid value for the '%s' option"), option
);
348 while (isspace (*chp
))
352 /* For new argv commands, attempt to return the parsed argument
354 if (parse
->cmd
->argv_func
!= NULL
)
356 mi_parse_argv (chp
, parse
);
357 if (parse
->argv
== NULL
)
358 error (_("Problem parsing arguments: %s %s"), parse
->command
, chp
);
361 /* FIXME: DELETE THIS */
362 /* For CLI commands, also return the remainder of the
363 command line as a single string. */
364 if (parse
->cmd
->cli
.cmd
!= NULL
)
365 parse
->args
= xstrdup (chp
);
367 discard_cleanups (cleanup
);
370 parse
->op
= MI_COMMAND
;
This page took 0.140692 seconds and 5 git commands to generate.