1 /* MI Command Set - MI parser.
3 Copyright (C) 2000, 2001, 2002, 2007, 2008, 2009
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/>. */
28 #include "gdb_string.h"
31 mi_parse_argv (char *args
, struct mi_parse
*parse
)
35 char **argv
= xmalloc ((argc
+ 1) * sizeof (char *));
40 /* skip leading white space */
41 while (isspace (*chp
))
43 /* Three possibilities: EOF, quoted string, or other text. */
52 /* A quoted string. */
54 char *start
= chp
+ 1;
55 /* Determine the buffer size. */
58 while (*chp
!= '\0' && *chp
!= '"')
63 if (parse_escape (&chp
) <= 0)
65 /* Do not allow split lines or "\000" */
74 /* Insist on a closing quote. */
80 /* Insist on trailing white space. */
81 if (chp
[1] != '\0' && !isspace (chp
[1]))
86 /* create the buffer. */
87 arg
= xmalloc ((len
+ 1) * sizeof (char));
88 /* And copy the characters in. */
91 while (*chp
!= '\0' && *chp
!= '"')
96 arg
[len
] = parse_escape (&chp
);
103 chp
++; /* that closing quote. */
108 /* An unquoted string. Accumulate all non blank
109 characters into a buffer. */
112 while (*chp
!= '\0' && !isspace (*chp
))
117 arg
= xmalloc ((len
+ 1) * sizeof (char));
118 strncpy (arg
, start
, len
);
123 /* Append arg to argv. */
124 argv
= xrealloc (argv
, (argc
+ 2) * sizeof (char *));
132 mi_parse_free (struct mi_parse
*parse
)
136 if (parse
->command
!= NULL
)
137 xfree (parse
->command
);
138 if (parse
->token
!= NULL
)
139 xfree (parse
->token
);
140 if (parse
->args
!= NULL
)
142 if (parse
->argv
!= NULL
)
143 freeargv (parse
->argv
);
152 struct mi_parse
*parse
= XMALLOC (struct mi_parse
);
153 memset (parse
, 0, sizeof (*parse
));
157 /* Before starting, skip leading white space. */
158 while (isspace (*cmd
))
161 /* Find/skip any token and then extract it. */
162 for (chp
= cmd
; *chp
>= '0' && *chp
<= '9'; chp
++)
164 parse
->token
= xmalloc ((chp
- cmd
+ 1) * sizeof (char *));
165 memcpy (parse
->token
, cmd
, (chp
- cmd
));
166 parse
->token
[chp
- cmd
] = '\0';
168 /* This wasn't a real MI command. Return it as a CLI_COMMAND. */
171 while (isspace (*chp
))
173 parse
->command
= xstrdup (chp
);
174 parse
->op
= CLI_COMMAND
;
178 /* Extract the command. */
180 char *tmp
= chp
+ 1; /* discard ``-'' */
181 for (; *chp
&& !isspace (*chp
); chp
++)
183 parse
->command
= xmalloc ((chp
- tmp
+ 1) * sizeof (char *));
184 memcpy (parse
->command
, tmp
, chp
- tmp
);
185 parse
->command
[chp
- tmp
] = '\0';
188 /* Find the command in the MI table. */
189 parse
->cmd
= mi_lookup (parse
->command
);
190 if (parse
->cmd
== NULL
)
192 /* FIXME: This should be a function call. */
195 "%s^error,msg=\"Undefined MI command: %s\"\n",
196 parse
->token
, parse
->command
);
197 mi_parse_free (parse
);
201 /* Skip white space following the command. */
202 while (isspace (*chp
))
205 /* Parse the --thread and --frame options, if present. At present,
206 some important commands, like '-break-*' are implemented by forwarding
207 to the CLI layer directly. We want to parse --thread and --frame
208 here, so as not to leave those option in the string that will be passed
213 size_t ts
= sizeof ("--thread ") - 1;
214 size_t fs
= sizeof ("--frame ") - 1;
215 if (strncmp (chp
, "--thread ", ts
) == 0)
217 if (parse
->thread
!= -1)
218 error ("Duplicate '--thread' option");
220 parse
->thread
= strtol (chp
, &chp
, 10);
222 else if (strncmp (chp
, "--frame ", fs
) == 0)
224 if (parse
->frame
!= -1)
225 error ("Duplicate '--frame' option");
227 parse
->frame
= strtol (chp
, &chp
, 10);
232 if (*chp
!= '\0' && !isspace (*chp
))
233 error ("Invalid value for the '%s' option",
234 start
[2] == 't' ? "--thread" : "--frame");
235 while (isspace (*chp
))
239 /* For new argv commands, attempt to return the parsed argument
241 if (parse
->cmd
->argv_func
!= NULL
)
243 mi_parse_argv (chp
, parse
);
244 if (parse
->argv
== NULL
)
246 /* FIXME: This should be a function call. */
249 "%s^error,msg=\"Problem parsing arguments: %s %s\"\n",
250 parse
->token
, parse
->command
, chp
);
251 mi_parse_free (parse
);
256 /* FIXME: DELETE THIS */
257 /* For CLI commands, also return the remainder of the
258 command line as a single string. */
259 if (parse
->cmd
->cli
.cmd
!= NULL
)
260 parse
->args
= xstrdup (chp
);
263 parse
->op
= MI_COMMAND
;
This page took 0.034782 seconds and 4 git commands to generate.