xref: /dflybsd-src/contrib/gdb-7/gdb/mi/mi-parse.c (revision de8e141f24382815c10a4012d209bbbf7abf1112)
15796c8dcSSimon Schubert /* MI Command Set - MI parser.
25796c8dcSSimon Schubert 
3*ef5ccd6cSJohn Marino    Copyright (C) 2000-2013 Free Software Foundation, Inc.
45796c8dcSSimon Schubert 
55796c8dcSSimon Schubert    Contributed by Cygnus Solutions (a Red Hat company).
65796c8dcSSimon Schubert 
75796c8dcSSimon Schubert    This file is part of GDB.
85796c8dcSSimon Schubert 
95796c8dcSSimon Schubert    This program is free software; you can redistribute it and/or modify
105796c8dcSSimon Schubert    it under the terms of the GNU General Public License as published by
115796c8dcSSimon Schubert    the Free Software Foundation; either version 3 of the License, or
125796c8dcSSimon Schubert    (at your option) any later version.
135796c8dcSSimon Schubert 
145796c8dcSSimon Schubert    This program is distributed in the hope that it will be useful,
155796c8dcSSimon Schubert    but WITHOUT ANY WARRANTY; without even the implied warranty of
165796c8dcSSimon Schubert    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
175796c8dcSSimon Schubert    GNU General Public License for more details.
185796c8dcSSimon Schubert 
195796c8dcSSimon Schubert    You should have received a copy of the GNU General Public License
205796c8dcSSimon Schubert    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
215796c8dcSSimon Schubert 
225796c8dcSSimon Schubert #include "defs.h"
235796c8dcSSimon Schubert #include "mi-cmds.h"
245796c8dcSSimon Schubert #include "mi-parse.h"
25cf7f2e2dSJohn Marino #include "charset.h"
265796c8dcSSimon Schubert 
275796c8dcSSimon Schubert #include <ctype.h>
285796c8dcSSimon Schubert #include "gdb_string.h"
29*ef5ccd6cSJohn Marino #include "cli/cli-utils.h"
305796c8dcSSimon Schubert 
31cf7f2e2dSJohn Marino /* Like parse_escape, but leave the results as a host char, not a
32cf7f2e2dSJohn Marino    target char.  */
33cf7f2e2dSJohn Marino 
34cf7f2e2dSJohn Marino static int
mi_parse_escape(const char ** string_ptr)35*ef5ccd6cSJohn Marino mi_parse_escape (const char **string_ptr)
36cf7f2e2dSJohn Marino {
37cf7f2e2dSJohn Marino   int c = *(*string_ptr)++;
38cf7f2e2dSJohn Marino 
39cf7f2e2dSJohn Marino   switch (c)
40cf7f2e2dSJohn Marino     {
41cf7f2e2dSJohn Marino       case '\n':
42cf7f2e2dSJohn Marino 	return -2;
43cf7f2e2dSJohn Marino       case 0:
44cf7f2e2dSJohn Marino 	(*string_ptr)--;
45cf7f2e2dSJohn Marino 	return 0;
46cf7f2e2dSJohn Marino 
47cf7f2e2dSJohn Marino       case '0':
48cf7f2e2dSJohn Marino       case '1':
49cf7f2e2dSJohn Marino       case '2':
50cf7f2e2dSJohn Marino       case '3':
51cf7f2e2dSJohn Marino       case '4':
52cf7f2e2dSJohn Marino       case '5':
53cf7f2e2dSJohn Marino       case '6':
54cf7f2e2dSJohn Marino       case '7':
55cf7f2e2dSJohn Marino 	{
56cf7f2e2dSJohn Marino 	  int i = host_hex_value (c);
57cf7f2e2dSJohn Marino 	  int count = 0;
58cf7f2e2dSJohn Marino 
59cf7f2e2dSJohn Marino 	  while (++count < 3)
60cf7f2e2dSJohn Marino 	    {
61cf7f2e2dSJohn Marino 	      c = (**string_ptr);
62cf7f2e2dSJohn Marino 	      if (isdigit (c) && c != '8' && c != '9')
63cf7f2e2dSJohn Marino 		{
64cf7f2e2dSJohn Marino 		  (*string_ptr)++;
65cf7f2e2dSJohn Marino 		  i *= 8;
66cf7f2e2dSJohn Marino 		  i += host_hex_value (c);
67cf7f2e2dSJohn Marino 		}
68cf7f2e2dSJohn Marino 	      else
69cf7f2e2dSJohn Marino 		{
70cf7f2e2dSJohn Marino 		  break;
71cf7f2e2dSJohn Marino 		}
72cf7f2e2dSJohn Marino 	    }
73cf7f2e2dSJohn Marino 	  return i;
74cf7f2e2dSJohn Marino 	}
75cf7f2e2dSJohn Marino 
76cf7f2e2dSJohn Marino     case 'a':
77cf7f2e2dSJohn Marino       c = '\a';
78cf7f2e2dSJohn Marino       break;
79cf7f2e2dSJohn Marino     case 'b':
80cf7f2e2dSJohn Marino       c = '\b';
81cf7f2e2dSJohn Marino       break;
82cf7f2e2dSJohn Marino     case 'f':
83cf7f2e2dSJohn Marino       c = '\f';
84cf7f2e2dSJohn Marino       break;
85cf7f2e2dSJohn Marino     case 'n':
86cf7f2e2dSJohn Marino       c = '\n';
87cf7f2e2dSJohn Marino       break;
88cf7f2e2dSJohn Marino     case 'r':
89cf7f2e2dSJohn Marino       c = '\r';
90cf7f2e2dSJohn Marino       break;
91cf7f2e2dSJohn Marino     case 't':
92cf7f2e2dSJohn Marino       c = '\t';
93cf7f2e2dSJohn Marino       break;
94cf7f2e2dSJohn Marino     case 'v':
95cf7f2e2dSJohn Marino       c = '\v';
96cf7f2e2dSJohn Marino       break;
97cf7f2e2dSJohn Marino 
98cf7f2e2dSJohn Marino     default:
99cf7f2e2dSJohn Marino       break;
100cf7f2e2dSJohn Marino     }
101cf7f2e2dSJohn Marino 
102cf7f2e2dSJohn Marino   return c;
103cf7f2e2dSJohn Marino }
104cf7f2e2dSJohn Marino 
1055796c8dcSSimon Schubert static void
mi_parse_argv(const char * args,struct mi_parse * parse)106*ef5ccd6cSJohn Marino mi_parse_argv (const char *args, struct mi_parse *parse)
1075796c8dcSSimon Schubert {
108*ef5ccd6cSJohn Marino   const char *chp = args;
1095796c8dcSSimon Schubert   int argc = 0;
1105796c8dcSSimon Schubert   char **argv = xmalloc ((argc + 1) * sizeof (char *));
111cf7f2e2dSJohn Marino 
1125796c8dcSSimon Schubert   argv[argc] = NULL;
1135796c8dcSSimon Schubert   while (1)
1145796c8dcSSimon Schubert     {
1155796c8dcSSimon Schubert       char *arg;
116cf7f2e2dSJohn Marino 
117*ef5ccd6cSJohn Marino       /* Skip leading white space.  */
118*ef5ccd6cSJohn Marino       chp = skip_spaces_const (chp);
1195796c8dcSSimon Schubert       /* Three possibilities: EOF, quoted string, or other text. */
1205796c8dcSSimon Schubert       switch (*chp)
1215796c8dcSSimon Schubert 	{
1225796c8dcSSimon Schubert 	case '\0':
1235796c8dcSSimon Schubert 	  parse->argv = argv;
1245796c8dcSSimon Schubert 	  parse->argc = argc;
1255796c8dcSSimon Schubert 	  return;
1265796c8dcSSimon Schubert 	case '"':
1275796c8dcSSimon Schubert 	  {
1285796c8dcSSimon Schubert 	    /* A quoted string.  */
1295796c8dcSSimon Schubert 	    int len;
130*ef5ccd6cSJohn Marino 	    const char *start = chp + 1;
131cf7f2e2dSJohn Marino 
1325796c8dcSSimon Schubert 	    /* Determine the buffer size.  */
1335796c8dcSSimon Schubert 	    chp = start;
1345796c8dcSSimon Schubert 	    len = 0;
1355796c8dcSSimon Schubert 	    while (*chp != '\0' && *chp != '"')
1365796c8dcSSimon Schubert 	      {
1375796c8dcSSimon Schubert 		if (*chp == '\\')
1385796c8dcSSimon Schubert 		  {
1395796c8dcSSimon Schubert 		    chp++;
140cf7f2e2dSJohn Marino 		    if (mi_parse_escape (&chp) <= 0)
1415796c8dcSSimon Schubert 		      {
142*ef5ccd6cSJohn Marino 			/* Do not allow split lines or "\000".  */
1435796c8dcSSimon Schubert 			freeargv (argv);
1445796c8dcSSimon Schubert 			return;
1455796c8dcSSimon Schubert 		      }
1465796c8dcSSimon Schubert 		  }
1475796c8dcSSimon Schubert 		else
1485796c8dcSSimon Schubert 		  chp++;
1495796c8dcSSimon Schubert 		len++;
1505796c8dcSSimon Schubert 	      }
1515796c8dcSSimon Schubert 	    /* Insist on a closing quote.  */
1525796c8dcSSimon Schubert 	    if (*chp != '"')
1535796c8dcSSimon Schubert 	      {
1545796c8dcSSimon Schubert 		freeargv (argv);
1555796c8dcSSimon Schubert 		return;
1565796c8dcSSimon Schubert 	      }
1575796c8dcSSimon Schubert 	    /* Insist on trailing white space.  */
1585796c8dcSSimon Schubert 	    if (chp[1] != '\0' && !isspace (chp[1]))
1595796c8dcSSimon Schubert 	      {
1605796c8dcSSimon Schubert 		freeargv (argv);
1615796c8dcSSimon Schubert 		return;
1625796c8dcSSimon Schubert 	      }
163*ef5ccd6cSJohn Marino 	    /* Create the buffer and copy characters in.  */
1645796c8dcSSimon Schubert 	    arg = xmalloc ((len + 1) * sizeof (char));
1655796c8dcSSimon Schubert 	    chp = start;
1665796c8dcSSimon Schubert 	    len = 0;
1675796c8dcSSimon Schubert 	    while (*chp != '\0' && *chp != '"')
1685796c8dcSSimon Schubert 	      {
1695796c8dcSSimon Schubert 		if (*chp == '\\')
1705796c8dcSSimon Schubert 		  {
1715796c8dcSSimon Schubert 		    chp++;
172cf7f2e2dSJohn Marino 		    arg[len] = mi_parse_escape (&chp);
1735796c8dcSSimon Schubert 		  }
1745796c8dcSSimon Schubert 		else
1755796c8dcSSimon Schubert 		  arg[len] = *chp++;
1765796c8dcSSimon Schubert 		len++;
1775796c8dcSSimon Schubert 	      }
1785796c8dcSSimon Schubert 	    arg[len] = '\0';
179*ef5ccd6cSJohn Marino 	    chp++;		/* That closing quote.  */
1805796c8dcSSimon Schubert 	    break;
1815796c8dcSSimon Schubert 	  }
1825796c8dcSSimon Schubert 	default:
1835796c8dcSSimon Schubert 	  {
184*ef5ccd6cSJohn Marino 	    /* An unquoted string.  Accumulate all non-blank
1855796c8dcSSimon Schubert 	       characters into a buffer.  */
1865796c8dcSSimon Schubert 	    int len;
187*ef5ccd6cSJohn Marino 	    const char *start = chp;
188cf7f2e2dSJohn Marino 
1895796c8dcSSimon Schubert 	    while (*chp != '\0' && !isspace (*chp))
1905796c8dcSSimon Schubert 	      {
1915796c8dcSSimon Schubert 		chp++;
1925796c8dcSSimon Schubert 	      }
1935796c8dcSSimon Schubert 	    len = chp - start;
1945796c8dcSSimon Schubert 	    arg = xmalloc ((len + 1) * sizeof (char));
1955796c8dcSSimon Schubert 	    strncpy (arg, start, len);
1965796c8dcSSimon Schubert 	    arg[len] = '\0';
1975796c8dcSSimon Schubert 	    break;
1985796c8dcSSimon Schubert 	  }
1995796c8dcSSimon Schubert 	}
2005796c8dcSSimon Schubert       /* Append arg to argv.  */
2015796c8dcSSimon Schubert       argv = xrealloc (argv, (argc + 2) * sizeof (char *));
2025796c8dcSSimon Schubert       argv[argc++] = arg;
2035796c8dcSSimon Schubert       argv[argc] = NULL;
2045796c8dcSSimon Schubert     }
2055796c8dcSSimon Schubert }
2065796c8dcSSimon Schubert 
2075796c8dcSSimon Schubert void
mi_parse_free(struct mi_parse * parse)2085796c8dcSSimon Schubert mi_parse_free (struct mi_parse *parse)
2095796c8dcSSimon Schubert {
2105796c8dcSSimon Schubert   if (parse == NULL)
2115796c8dcSSimon Schubert     return;
2125796c8dcSSimon Schubert   if (parse->command != NULL)
2135796c8dcSSimon Schubert     xfree (parse->command);
2145796c8dcSSimon Schubert   if (parse->token != NULL)
2155796c8dcSSimon Schubert     xfree (parse->token);
2165796c8dcSSimon Schubert   if (parse->args != NULL)
2175796c8dcSSimon Schubert     xfree (parse->args);
2185796c8dcSSimon Schubert   if (parse->argv != NULL)
2195796c8dcSSimon Schubert     freeargv (parse->argv);
2205796c8dcSSimon Schubert   xfree (parse);
2215796c8dcSSimon Schubert }
2225796c8dcSSimon Schubert 
223c50c785cSJohn Marino /* A cleanup that calls mi_parse_free.  */
224c50c785cSJohn Marino 
225c50c785cSJohn Marino static void
mi_parse_cleanup(void * arg)226c50c785cSJohn Marino mi_parse_cleanup (void *arg)
227c50c785cSJohn Marino {
228c50c785cSJohn Marino   mi_parse_free (arg);
229c50c785cSJohn Marino }
2305796c8dcSSimon Schubert 
2315796c8dcSSimon Schubert struct mi_parse *
mi_parse(const char * cmd,char ** token)232*ef5ccd6cSJohn Marino mi_parse (const char *cmd, char **token)
2335796c8dcSSimon Schubert {
234*ef5ccd6cSJohn Marino   const char *chp;
2355796c8dcSSimon Schubert   struct mi_parse *parse = XMALLOC (struct mi_parse);
236c50c785cSJohn Marino   struct cleanup *cleanup;
237cf7f2e2dSJohn Marino 
2385796c8dcSSimon Schubert   memset (parse, 0, sizeof (*parse));
239cf7f2e2dSJohn Marino   parse->all = 0;
240cf7f2e2dSJohn Marino   parse->thread_group = -1;
2415796c8dcSSimon Schubert   parse->thread = -1;
2425796c8dcSSimon Schubert   parse->frame = -1;
2435796c8dcSSimon Schubert 
244c50c785cSJohn Marino   cleanup = make_cleanup (mi_parse_cleanup, parse);
245c50c785cSJohn Marino 
2465796c8dcSSimon Schubert   /* Before starting, skip leading white space.  */
247*ef5ccd6cSJohn Marino   cmd = skip_spaces_const (cmd);
2485796c8dcSSimon Schubert 
2495796c8dcSSimon Schubert   /* Find/skip any token and then extract it.  */
2505796c8dcSSimon Schubert   for (chp = cmd; *chp >= '0' && *chp <= '9'; chp++)
2515796c8dcSSimon Schubert     ;
252a45ae5f8SJohn Marino   *token = xmalloc (chp - cmd + 1);
253c50c785cSJohn Marino   memcpy (*token, cmd, (chp - cmd));
254c50c785cSJohn Marino   (*token)[chp - cmd] = '\0';
2555796c8dcSSimon Schubert 
2565796c8dcSSimon Schubert   /* This wasn't a real MI command.  Return it as a CLI_COMMAND.  */
2575796c8dcSSimon Schubert   if (*chp != '-')
2585796c8dcSSimon Schubert     {
259*ef5ccd6cSJohn Marino       chp = skip_spaces_const (chp);
2605796c8dcSSimon Schubert       parse->command = xstrdup (chp);
2615796c8dcSSimon Schubert       parse->op = CLI_COMMAND;
262c50c785cSJohn Marino 
263c50c785cSJohn Marino       discard_cleanups (cleanup);
264c50c785cSJohn Marino 
2655796c8dcSSimon Schubert       return parse;
2665796c8dcSSimon Schubert     }
2675796c8dcSSimon Schubert 
2685796c8dcSSimon Schubert   /* Extract the command.  */
2695796c8dcSSimon Schubert   {
270*ef5ccd6cSJohn Marino     const char *tmp = chp + 1;	/* discard ``-'' */
271cf7f2e2dSJohn Marino 
2725796c8dcSSimon Schubert     for (; *chp && !isspace (*chp); chp++)
2735796c8dcSSimon Schubert       ;
274a45ae5f8SJohn Marino     parse->command = xmalloc (chp - tmp + 1);
2755796c8dcSSimon Schubert     memcpy (parse->command, tmp, chp - tmp);
2765796c8dcSSimon Schubert     parse->command[chp - tmp] = '\0';
2775796c8dcSSimon Schubert   }
2785796c8dcSSimon Schubert 
2795796c8dcSSimon Schubert   /* Find the command in the MI table.  */
2805796c8dcSSimon Schubert   parse->cmd = mi_lookup (parse->command);
2815796c8dcSSimon Schubert   if (parse->cmd == NULL)
282c50c785cSJohn Marino     error (_("Undefined MI command: %s"), parse->command);
2835796c8dcSSimon Schubert 
2845796c8dcSSimon Schubert   /* Skip white space following the command.  */
285*ef5ccd6cSJohn Marino   chp = skip_spaces_const (chp);
2865796c8dcSSimon Schubert 
2875796c8dcSSimon Schubert   /* Parse the --thread and --frame options, if present.  At present,
288*ef5ccd6cSJohn Marino      some important commands, like '-break-*' are implemented by
289*ef5ccd6cSJohn Marino      forwarding to the CLI layer directly.  We want to parse --thread
290*ef5ccd6cSJohn Marino      and --frame here, so as not to leave those option in the string
291*ef5ccd6cSJohn Marino      that will be passed to CLI.  */
2925796c8dcSSimon Schubert   for (;;)
2935796c8dcSSimon Schubert     {
294c50c785cSJohn Marino       const char *option;
295cf7f2e2dSJohn Marino       size_t as = sizeof ("--all ") - 1;
296cf7f2e2dSJohn Marino       size_t tgs = sizeof ("--thread-group ") - 1;
2975796c8dcSSimon Schubert       size_t ts = sizeof ("--thread ") - 1;
2985796c8dcSSimon Schubert       size_t fs = sizeof ("--frame ") - 1;
299cf7f2e2dSJohn Marino 
300cf7f2e2dSJohn Marino       if (strncmp (chp, "--all ", as) == 0)
301cf7f2e2dSJohn Marino 	{
302cf7f2e2dSJohn Marino 	  parse->all = 1;
303cf7f2e2dSJohn Marino 	  chp += as;
304cf7f2e2dSJohn Marino 	}
305cf7f2e2dSJohn Marino       /* See if --all is the last token in the input.  */
306cf7f2e2dSJohn Marino       if (strcmp (chp, "--all") == 0)
307cf7f2e2dSJohn Marino 	{
308cf7f2e2dSJohn Marino           parse->all = 1;
309cf7f2e2dSJohn Marino           chp += strlen (chp);
310cf7f2e2dSJohn Marino         }
311cf7f2e2dSJohn Marino       if (strncmp (chp, "--thread-group ", tgs) == 0)
312cf7f2e2dSJohn Marino 	{
313*ef5ccd6cSJohn Marino 	  char *endp;
314*ef5ccd6cSJohn Marino 
315c50c785cSJohn Marino 	  option = "--thread-group";
316cf7f2e2dSJohn Marino 	  if (parse->thread_group != -1)
317cf7f2e2dSJohn Marino 	    error (_("Duplicate '--thread-group' option"));
318cf7f2e2dSJohn Marino 	  chp += tgs;
319cf7f2e2dSJohn Marino 	  if (*chp != 'i')
320cf7f2e2dSJohn Marino 	    error (_("Invalid thread group id"));
321cf7f2e2dSJohn Marino 	  chp += 1;
322*ef5ccd6cSJohn Marino 	  parse->thread_group = strtol (chp, &endp, 10);
323*ef5ccd6cSJohn Marino 	  chp = endp;
324cf7f2e2dSJohn Marino 	}
325c50c785cSJohn Marino       else if (strncmp (chp, "--thread ", ts) == 0)
3265796c8dcSSimon Schubert 	{
327*ef5ccd6cSJohn Marino 	  char *endp;
328*ef5ccd6cSJohn Marino 
329c50c785cSJohn Marino 	  option = "--thread";
3305796c8dcSSimon Schubert 	  if (parse->thread != -1)
331cf7f2e2dSJohn Marino 	    error (_("Duplicate '--thread' option"));
3325796c8dcSSimon Schubert 	  chp += ts;
333*ef5ccd6cSJohn Marino 	  parse->thread = strtol (chp, &endp, 10);
334*ef5ccd6cSJohn Marino 	  chp = endp;
3355796c8dcSSimon Schubert 	}
3365796c8dcSSimon Schubert       else if (strncmp (chp, "--frame ", fs) == 0)
3375796c8dcSSimon Schubert 	{
338*ef5ccd6cSJohn Marino 	  char *endp;
339*ef5ccd6cSJohn Marino 
340c50c785cSJohn Marino 	  option = "--frame";
3415796c8dcSSimon Schubert 	  if (parse->frame != -1)
342cf7f2e2dSJohn Marino 	    error (_("Duplicate '--frame' option"));
3435796c8dcSSimon Schubert 	  chp += fs;
344*ef5ccd6cSJohn Marino 	  parse->frame = strtol (chp, &endp, 10);
345*ef5ccd6cSJohn Marino 	  chp = endp;
3465796c8dcSSimon Schubert 	}
3475796c8dcSSimon Schubert       else
3485796c8dcSSimon Schubert 	break;
3495796c8dcSSimon Schubert 
3505796c8dcSSimon Schubert       if (*chp != '\0' && !isspace (*chp))
351c50c785cSJohn Marino 	error (_("Invalid value for the '%s' option"), option);
352*ef5ccd6cSJohn Marino       chp = skip_spaces_const (chp);
3535796c8dcSSimon Schubert     }
3545796c8dcSSimon Schubert 
3555796c8dcSSimon Schubert   /* For new argv commands, attempt to return the parsed argument
3565796c8dcSSimon Schubert      list.  */
3575796c8dcSSimon Schubert   if (parse->cmd->argv_func != NULL)
3585796c8dcSSimon Schubert     {
3595796c8dcSSimon Schubert       mi_parse_argv (chp, parse);
3605796c8dcSSimon Schubert       if (parse->argv == NULL)
361c50c785cSJohn Marino 	error (_("Problem parsing arguments: %s %s"), parse->command, chp);
3625796c8dcSSimon Schubert     }
3635796c8dcSSimon Schubert 
3645796c8dcSSimon Schubert   /* FIXME: DELETE THIS */
3655796c8dcSSimon Schubert   /* For CLI commands, also return the remainder of the
3665796c8dcSSimon Schubert      command line as a single string. */
3675796c8dcSSimon Schubert   if (parse->cmd->cli.cmd != NULL)
3685796c8dcSSimon Schubert     parse->args = xstrdup (chp);
3695796c8dcSSimon Schubert 
370c50c785cSJohn Marino   discard_cleanups (cleanup);
371c50c785cSJohn Marino 
372*ef5ccd6cSJohn Marino   /* Fully parsed, flag as an MI command.  */
3735796c8dcSSimon Schubert   parse->op = MI_COMMAND;
3745796c8dcSSimon Schubert   return parse;
3755796c8dcSSimon Schubert }
376