1 /* MI Command Set - MI parser.
2 Copyright (C) 2000, Free Software Foundation, Inc.
3 Contributed by Cygnus Solutions (a Red Hat company).
5 This file is part of GDB.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
30 #define XMALLOC(TYPE) ((TYPE*) xmalloc (sizeof (TYPE)))
33 mi_parse_argv (char *args
, struct mi_parse
*parse
)
37 char **argv
= xmalloc ((argc
+ 1) * sizeof (char *));
42 /* skip leading white space */
43 while (isspace (*chp
))
45 /* Three possibilities: EOF, quoted string, or other text. */
54 /* A quoted string. */
56 char *start
= chp
+ 1;
57 /* Determine the buffer size. */
60 while (*chp
!= '\0' && *chp
!= '"')
65 if (parse_escape (&chp
) <= 0)
67 /* Do not allow split lines or "\000" */
76 /* Insist on a closing quote. */
82 /* Insist on trailing white space. */
83 if (chp
[1] != '\0' && !isspace (chp
[1]))
88 /* create the buffer. */
89 arg
= xmalloc ((len
+ 1) * sizeof (char));
90 /* And copy the characters in. */
93 while (*chp
!= '\0' && *chp
!= '"')
98 arg
[len
] = parse_escape (&chp
);
105 chp
++; /* that closing quote. */
110 /* An unquoted string. Accumulate all non blank
111 characters into a buffer. */
114 while (*chp
!= '\0' && !isspace (*chp
))
119 arg
= xmalloc ((len
+ 1) * sizeof (char));
120 strncpy (arg
, start
, len
);
125 /* Append arg to argv. */
126 argv
= xrealloc (argv
, (argc
+ 2) * sizeof (char *));
134 mi_parse_free (struct mi_parse
*parse
)
138 if (parse
->command
!= NULL
)
139 free (parse
->command
);
140 if (parse
->token
!= NULL
)
142 if (parse
->args
!= NULL
)
144 if (parse
->argv
!= NULL
)
145 freeargv (parse
->argv
);
154 struct mi_parse
*parse
= XMALLOC (struct mi_parse
);
155 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 /* For new argv commands, attempt to return the parsed argument
207 if (parse
->cmd
->argv_func
!= NULL
)
209 mi_parse_argv (chp
, parse
);
210 if (parse
->argv
== NULL
)
212 /* FIXME: This should be a function call. */
215 "%s^error,msg=\"Problem parsing arguments: %s %s\"\n",
216 parse
->token
, parse
->command
, chp
);
217 mi_parse_free (parse
);
222 /* FIXME: DELETE THIS */
223 /* For CLI and old ARGS commands, also return the remainder of the
224 command line as a single string. */
225 if (parse
->cmd
->args_func
!= NULL
226 || parse
->cmd
->cli
!= NULL
)
228 parse
->args
= xstrdup (chp
);
232 parse
->op
= MI_COMMAND
;
237 _initialize_mi_parse (void)
This page took 0.052057 seconds and 4 git commands to generate.