Commit | Line | Data |
---|---|---|
fb40c209 | 1 | /* MI Command Set - MI parser. |
349c5d5f | 2 | |
4c38e0a4 | 3 | Copyright (C) 2000, 2001, 2002, 2007, 2008, 2009, 2010 |
0fb0cc75 | 4 | Free Software Foundation, Inc. |
349c5d5f | 5 | |
ab91fdd5 | 6 | Contributed by Cygnus Solutions (a Red Hat company). |
fb40c209 AC |
7 | |
8 | This file is part of GDB. | |
9 | ||
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 | |
a9762ec7 | 12 | the Free Software Foundation; either version 3 of the License, or |
fb40c209 AC |
13 | (at your option) any later version. |
14 | ||
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. | |
19 | ||
20 | You should have received a copy of the GNU General Public License | |
a9762ec7 | 21 | along with this program. If not, see <http://www.gnu.org/licenses/>. */ |
fb40c209 AC |
22 | |
23 | #include "defs.h" | |
24 | #include "mi-cmds.h" | |
25 | #include "mi-parse.h" | |
26 | ||
27 | #include <ctype.h> | |
27b82ed2 | 28 | #include "gdb_string.h" |
fb40c209 | 29 | |
fb40c209 AC |
30 | static void |
31 | mi_parse_argv (char *args, struct mi_parse *parse) | |
32 | { | |
33 | char *chp = args; | |
34 | int argc = 0; | |
35 | char **argv = xmalloc ((argc + 1) * sizeof (char *)); | |
36 | argv[argc] = NULL; | |
37 | while (1) | |
38 | { | |
39 | char *arg; | |
40 | /* skip leading white space */ | |
41 | while (isspace (*chp)) | |
42 | chp++; | |
43 | /* Three possibilities: EOF, quoted string, or other text. */ | |
44 | switch (*chp) | |
45 | { | |
46 | case '\0': | |
47 | parse->argv = argv; | |
48 | parse->argc = argc; | |
49 | return; | |
50 | case '"': | |
51 | { | |
52 | /* A quoted string. */ | |
53 | int len; | |
54 | char *start = chp + 1; | |
55 | /* Determine the buffer size. */ | |
56 | chp = start; | |
57 | len = 0; | |
58 | while (*chp != '\0' && *chp != '"') | |
59 | { | |
60 | if (*chp == '\\') | |
61 | { | |
62 | chp++; | |
63 | if (parse_escape (&chp) <= 0) | |
64 | { | |
65 | /* Do not allow split lines or "\000" */ | |
66 | freeargv (argv); | |
67 | return; | |
68 | } | |
69 | } | |
70 | else | |
71 | chp++; | |
72 | len++; | |
73 | } | |
74 | /* Insist on a closing quote. */ | |
75 | if (*chp != '"') | |
76 | { | |
77 | freeargv (argv); | |
78 | return; | |
79 | } | |
80 | /* Insist on trailing white space. */ | |
81 | if (chp[1] != '\0' && !isspace (chp[1])) | |
82 | { | |
83 | freeargv (argv); | |
84 | return; | |
85 | } | |
86 | /* create the buffer. */ | |
87 | arg = xmalloc ((len + 1) * sizeof (char)); | |
88 | /* And copy the characters in. */ | |
89 | chp = start; | |
90 | len = 0; | |
91 | while (*chp != '\0' && *chp != '"') | |
92 | { | |
93 | if (*chp == '\\') | |
94 | { | |
95 | chp++; | |
96 | arg[len] = parse_escape (&chp); | |
97 | } | |
98 | else | |
99 | arg[len] = *chp++; | |
100 | len++; | |
101 | } | |
102 | arg[len] = '\0'; | |
103 | chp++; /* that closing quote. */ | |
104 | break; | |
105 | } | |
106 | default: | |
107 | { | |
108 | /* An unquoted string. Accumulate all non blank | |
109 | characters into a buffer. */ | |
110 | int len; | |
111 | char *start = chp; | |
112 | while (*chp != '\0' && !isspace (*chp)) | |
113 | { | |
114 | chp++; | |
115 | } | |
116 | len = chp - start; | |
117 | arg = xmalloc ((len + 1) * sizeof (char)); | |
118 | strncpy (arg, start, len); | |
119 | arg[len] = '\0'; | |
120 | break; | |
121 | } | |
122 | } | |
123 | /* Append arg to argv. */ | |
124 | argv = xrealloc (argv, (argc + 2) * sizeof (char *)); | |
125 | argv[argc++] = arg; | |
126 | argv[argc] = NULL; | |
127 | } | |
128 | } | |
129 | ||
130 | ||
131 | void | |
132 | mi_parse_free (struct mi_parse *parse) | |
133 | { | |
134 | if (parse == NULL) | |
135 | return; | |
136 | if (parse->command != NULL) | |
b8c9b27d | 137 | xfree (parse->command); |
fb40c209 | 138 | if (parse->token != NULL) |
b8c9b27d | 139 | xfree (parse->token); |
fb40c209 | 140 | if (parse->args != NULL) |
b8c9b27d | 141 | xfree (parse->args); |
fb40c209 AC |
142 | if (parse->argv != NULL) |
143 | freeargv (parse->argv); | |
b8c9b27d | 144 | xfree (parse); |
fb40c209 AC |
145 | } |
146 | ||
147 | ||
148 | struct mi_parse * | |
149 | mi_parse (char *cmd) | |
150 | { | |
151 | char *chp; | |
152 | struct mi_parse *parse = XMALLOC (struct mi_parse); | |
153 | memset (parse, 0, sizeof (*parse)); | |
a79b8f6e VP |
154 | parse->all = 0; |
155 | parse->thread_group = -1; | |
1e92afda VP |
156 | parse->thread = -1; |
157 | parse->frame = -1; | |
fb40c209 AC |
158 | |
159 | /* Before starting, skip leading white space. */ | |
160 | while (isspace (*cmd)) | |
161 | cmd++; | |
162 | ||
163 | /* Find/skip any token and then extract it. */ | |
164 | for (chp = cmd; *chp >= '0' && *chp <= '9'; chp++) | |
165 | ; | |
166 | parse->token = xmalloc ((chp - cmd + 1) * sizeof (char *)); | |
167 | memcpy (parse->token, cmd, (chp - cmd)); | |
168 | parse->token[chp - cmd] = '\0'; | |
169 | ||
170 | /* This wasn't a real MI command. Return it as a CLI_COMMAND. */ | |
171 | if (*chp != '-') | |
172 | { | |
173 | while (isspace (*chp)) | |
174 | chp++; | |
175 | parse->command = xstrdup (chp); | |
176 | parse->op = CLI_COMMAND; | |
177 | return parse; | |
178 | } | |
179 | ||
180 | /* Extract the command. */ | |
181 | { | |
182 | char *tmp = chp + 1; /* discard ``-'' */ | |
183 | for (; *chp && !isspace (*chp); chp++) | |
184 | ; | |
185 | parse->command = xmalloc ((chp - tmp + 1) * sizeof (char *)); | |
186 | memcpy (parse->command, tmp, chp - tmp); | |
187 | parse->command[chp - tmp] = '\0'; | |
188 | } | |
189 | ||
190 | /* Find the command in the MI table. */ | |
191 | parse->cmd = mi_lookup (parse->command); | |
192 | if (parse->cmd == NULL) | |
193 | { | |
194 | /* FIXME: This should be a function call. */ | |
195 | fprintf_unfiltered | |
196 | (raw_stdout, | |
197 | "%s^error,msg=\"Undefined MI command: %s\"\n", | |
198 | parse->token, parse->command); | |
199 | mi_parse_free (parse); | |
200 | return NULL; | |
201 | } | |
202 | ||
203 | /* Skip white space following the command. */ | |
204 | while (isspace (*chp)) | |
205 | chp++; | |
206 | ||
1e92afda VP |
207 | /* Parse the --thread and --frame options, if present. At present, |
208 | some important commands, like '-break-*' are implemented by forwarding | |
209 | to the CLI layer directly. We want to parse --thread and --frame | |
210 | here, so as not to leave those option in the string that will be passed | |
211 | to CLI. */ | |
212 | for (;;) | |
213 | { | |
214 | char *start = chp; | |
a79b8f6e VP |
215 | size_t as = sizeof ("--all ") - 1; |
216 | size_t tgs = sizeof ("--thread-group ") - 1; | |
1e92afda VP |
217 | size_t ts = sizeof ("--thread ") - 1; |
218 | size_t fs = sizeof ("--frame ") - 1; | |
a79b8f6e VP |
219 | if (strncmp (chp, "--all ", as) == 0) |
220 | { | |
221 | parse->all = 1; | |
222 | chp += as; | |
223 | } | |
224 | /* See if --all is the last token in the input. */ | |
225 | if (strcmp (chp, "--all") == 0) | |
226 | { | |
227 | parse->all = 1; | |
228 | chp += strlen (chp); | |
229 | } | |
230 | if (strncmp (chp, "--thread-group ", tgs) == 0) | |
231 | { | |
232 | if (parse->thread_group != -1) | |
233 | error (_("Duplicate '--thread-group' option")); | |
234 | chp += tgs; | |
235 | if (*chp != 'i') | |
236 | error (_("Invalid thread group id")); | |
237 | chp += 1; | |
238 | parse->thread_group = strtol (chp, &chp, 10); | |
239 | } | |
1e92afda VP |
240 | if (strncmp (chp, "--thread ", ts) == 0) |
241 | { | |
242 | if (parse->thread != -1) | |
a79b8f6e | 243 | error (_("Duplicate '--thread' option")); |
1e92afda VP |
244 | chp += ts; |
245 | parse->thread = strtol (chp, &chp, 10); | |
246 | } | |
247 | else if (strncmp (chp, "--frame ", fs) == 0) | |
248 | { | |
249 | if (parse->frame != -1) | |
a79b8f6e | 250 | error (_("Duplicate '--frame' option")); |
1e92afda VP |
251 | chp += fs; |
252 | parse->frame = strtol (chp, &chp, 10); | |
253 | } | |
254 | else | |
255 | break; | |
256 | ||
257 | if (*chp != '\0' && !isspace (*chp)) | |
a79b8f6e | 258 | error (_("Invalid value for the '%s' option"), |
1e92afda VP |
259 | start[2] == 't' ? "--thread" : "--frame"); |
260 | while (isspace (*chp)) | |
261 | chp++; | |
262 | } | |
263 | ||
fb40c209 AC |
264 | /* For new argv commands, attempt to return the parsed argument |
265 | list. */ | |
266 | if (parse->cmd->argv_func != NULL) | |
267 | { | |
268 | mi_parse_argv (chp, parse); | |
269 | if (parse->argv == NULL) | |
270 | { | |
271 | /* FIXME: This should be a function call. */ | |
272 | fprintf_unfiltered | |
273 | (raw_stdout, | |
274 | "%s^error,msg=\"Problem parsing arguments: %s %s\"\n", | |
275 | parse->token, parse->command, chp); | |
276 | mi_parse_free (parse); | |
277 | return NULL; | |
278 | } | |
279 | } | |
280 | ||
281 | /* FIXME: DELETE THIS */ | |
9e22b03a | 282 | /* For CLI commands, also return the remainder of the |
fb40c209 | 283 | command line as a single string. */ |
9e22b03a VP |
284 | if (parse->cmd->cli.cmd != NULL) |
285 | parse->args = xstrdup (chp); | |
fb40c209 AC |
286 | |
287 | /* Fully parsed. */ | |
288 | parse->op = MI_COMMAND; | |
289 | return parse; | |
290 | } |