gdb/mi/mi-parse.c - gdb
 Global variables defined
 
 Functions defined
 
 Source code
  
 
 
 
 
 
 
 
- #include "defs.h"
 
- #include "mi-cmds.h"
 
- #include "mi-parse.h"
 
- #include "charset.h"
 
 
- #include <ctype.h>
 
- #include "cli/cli-utils.h"
 
- #include "language.h"
 
 
- static const char mi_no_values[] = "--no-values";
 
- static const char mi_simple_values[] = "--simple-values";
 
- static const char mi_all_values[] = "--all-values";
 
 
 
- static int
 
- mi_parse_escape (const char **string_ptr)
 
- {
 
-   int c = *(*string_ptr)++;
 
 
-   switch (c)
 
-     {
 
-       case '\n':
 
-         return -2;
 
-       case 0:
 
-         (*string_ptr)--;
 
-         return 0;
 
 
-       case '0':
 
-       case '1':
 
-       case '2':
 
-       case '3':
 
-       case '4':
 
-       case '5':
 
-       case '6':
 
-       case '7':
 
-         {
 
-           int i = host_hex_value (c);
 
-           int count = 0;
 
 
-           while (++count < 3)
 
-             {
 
-               c = (**string_ptr);
 
-               if (isdigit (c) && c != '8' && c != '9')
 
-                 {
 
-                   (*string_ptr)++;
 
-                   i *= 8;
 
-                   i += host_hex_value (c);
 
-                 }
 
-               else
 
-                 {
 
-                   break;
 
-                 }
 
-             }
 
-           return i;
 
-         }
 
 
-     case 'a':
 
-       c = '\a';
 
-       break;
 
-     case 'b':
 
-       c = '\b';
 
-       break;
 
-     case 'f':
 
-       c = '\f';
 
-       break;
 
-     case 'n':
 
-       c = '\n';
 
-       break;
 
-     case 'r':
 
-       c = '\r';
 
-       break;
 
-     case 't':
 
-       c = '\t';
 
-       break;
 
-     case 'v':
 
-       c = '\v';
 
-       break;
 
 
-     default:
 
-       break;
 
-     }
 
 
-   return c;
 
- }
 
 
- static void
 
- mi_parse_argv (const char *args, struct mi_parse *parse)
 
- {
 
-   const char *chp = args;
 
-   int argc = 0;
 
-   char **argv = xmalloc ((argc + 1) * sizeof (char *));
 
 
-   argv[argc] = NULL;
 
-   while (1)
 
-     {
 
-       char *arg;
 
 
-       
 
-       chp = skip_spaces_const (chp);
 
-       
 
-       switch (*chp)
 
-         {
 
-         case '\0':
 
-           parse->argv = argv;
 
-           parse->argc = argc;
 
-           return;
 
-         case '"':
 
-           {
 
-             
 
-             int len;
 
-             const char *start = chp + 1;
 
 
-             
 
-             chp = start;
 
-             len = 0;
 
-             while (*chp != '\0' && *chp != '"')
 
-               {
 
-                 if (*chp == '\\')
 
-                   {
 
-                     chp++;
 
-                     if (mi_parse_escape (&chp) <= 0)
 
-                       {
 
-                         
 
-                         freeargv (argv);
 
-                         return;
 
-                       }
 
-                   }
 
-                 else
 
-                   chp++;
 
-                 len++;
 
-               }
 
-             
 
-             if (*chp != '"')
 
-               {
 
-                 freeargv (argv);
 
-                 return;
 
-               }
 
-             
 
-             if (chp[1] != '\0' && !isspace (chp[1]))
 
-               {
 
-                 freeargv (argv);
 
-                 return;
 
-               }
 
-             
 
-             arg = xmalloc ((len + 1) * sizeof (char));
 
-             chp = start;
 
-             len = 0;
 
-             while (*chp != '\0' && *chp != '"')
 
-               {
 
-                 if (*chp == '\\')
 
-                   {
 
-                     chp++;
 
-                     arg[len] = mi_parse_escape (&chp);
 
-                   }
 
-                 else
 
-                   arg[len] = *chp++;
 
-                 len++;
 
-               }
 
-             arg[len] = '\0';
 
-             chp++;                
 
-             break;
 
-           }
 
-         default:
 
-           {
 
-             
 
-             int len;
 
-             const char *start = chp;
 
 
-             while (*chp != '\0' && !isspace (*chp))
 
-               {
 
-                 chp++;
 
-               }
 
-             len = chp - start;
 
-             arg = xmalloc ((len + 1) * sizeof (char));
 
-             strncpy (arg, start, len);
 
-             arg[len] = '\0';
 
-             break;
 
-           }
 
-         }
 
-       
 
-       argv = xrealloc (argv, (argc + 2) * sizeof (char *));
 
-       argv[argc++] = arg;
 
-       argv[argc] = NULL;
 
-     }
 
- }
 
 
- void
 
- mi_parse_free (struct mi_parse *parse)
 
- {
 
-   if (parse == NULL)
 
-     return;
 
-   if (parse->command != NULL)
 
-     xfree (parse->command);
 
-   if (parse->token != NULL)
 
-     xfree (parse->token);
 
-   if (parse->args != NULL)
 
-     xfree (parse->args);
 
-   if (parse->argv != NULL)
 
-     freeargv (parse->argv);
 
-   xfree (parse);
 
- }
 
 
 
- static void
 
- mi_parse_cleanup (void *arg)
 
- {
 
-   mi_parse_free (arg);
 
- }
 
 
- struct mi_parse *
 
- mi_parse (const char *cmd, char **token)
 
- {
 
-   const char *chp;
 
-   struct mi_parse *parse = XNEW (struct mi_parse);
 
-   struct cleanup *cleanup;
 
 
-   memset (parse, 0, sizeof (*parse));
 
-   parse->all = 0;
 
-   parse->thread_group = -1;
 
-   parse->thread = -1;
 
-   parse->frame = -1;
 
-   parse->language = language_unknown;
 
 
-   cleanup = make_cleanup (mi_parse_cleanup, parse);
 
 
-   
 
-   cmd = skip_spaces_const (cmd);
 
 
-   
 
-   for (chp = cmd; *chp >= '0' && *chp <= '9'; chp++)
 
-     ;
 
-   *token = xmalloc (chp - cmd + 1);
 
-   memcpy (*token, cmd, (chp - cmd));
 
-   (*token)[chp - cmd] = '\0';
 
 
-   
 
-   if (*chp != '-')
 
-     {
 
-       chp = skip_spaces_const (chp);
 
-       parse->command = xstrdup (chp);
 
-       parse->op = CLI_COMMAND;
 
 
-       discard_cleanups (cleanup);
 
 
-       return parse;
 
-     }
 
 
-   
 
-   {
 
-     const char *tmp = chp + 1;        
 
 
-     for (; *chp && !isspace (*chp); chp++)
 
-       ;
 
-     parse->command = xmalloc (chp - tmp + 1);
 
-     memcpy (parse->command, tmp, chp - tmp);
 
-     parse->command[chp - tmp] = '\0';
 
-   }
 
 
-   
 
-   parse->cmd = mi_lookup (parse->command);
 
-   if (parse->cmd == NULL)
 
-     throw_error (UNDEFINED_COMMAND_ERROR,
 
-                  _("Undefined MI command: %s"), parse->command);
 
 
-   
 
-   chp = skip_spaces_const (chp);
 
 
-   
 
 
 
-   for (;;)
 
-     {
 
-       const char *option;
 
-       size_t as = sizeof ("--all ") - 1;
 
-       size_t tgs = sizeof ("--thread-group ") - 1;
 
-       size_t ts = sizeof ("--thread ") - 1;
 
-       size_t fs = sizeof ("--frame ") - 1;
 
-       size_t ls = sizeof ("--language ") - 1;
 
 
-       if (strncmp (chp, "--all ", as) == 0)
 
-         {
 
-           parse->all = 1;
 
-           chp += as;
 
-         }
 
-       
 
-       if (strcmp (chp, "--all") == 0)
 
-         {
 
-           parse->all = 1;
 
-           chp += strlen (chp);
 
-         }
 
-       if (strncmp (chp, "--thread-group ", tgs) == 0)
 
-         {
 
-           char *endp;
 
 
-           option = "--thread-group";
 
-           if (parse->thread_group != -1)
 
-             error (_("Duplicate '--thread-group' option"));
 
-           chp += tgs;
 
-           if (*chp != 'i')
 
-             error (_("Invalid thread group id"));
 
-           chp += 1;
 
-           parse->thread_group = strtol (chp, &endp, 10);
 
-           chp = endp;
 
-         }
 
-       else if (strncmp (chp, "--thread ", ts) == 0)
 
-         {
 
-           char *endp;
 
 
-           option = "--thread";
 
-           if (parse->thread != -1)
 
-             error (_("Duplicate '--thread' option"));
 
-           chp += ts;
 
-           parse->thread = strtol (chp, &endp, 10);
 
-           chp = endp;
 
-         }
 
-       else if (strncmp (chp, "--frame ", fs) == 0)
 
-         {
 
-           char *endp;
 
 
-           option = "--frame";
 
-           if (parse->frame != -1)
 
-             error (_("Duplicate '--frame' option"));
 
-           chp += fs;
 
-           parse->frame = strtol (chp, &endp, 10);
 
-           chp = endp;
 
-         }
 
-       else if (strncmp (chp, "--language ", ls) == 0)
 
-         {
 
-           char *lang_name;
 
-           struct cleanup *old_chain;
 
 
-           option = "--language";
 
-           chp += ls;
 
-           lang_name = extract_arg_const (&chp);
 
-           old_chain = make_cleanup (xfree, lang_name);
 
 
-           parse->language = language_enum (lang_name);
 
-           if (parse->language == language_unknown
 
-               || parse->language == language_auto)
 
-             error (_("Invalid --language argument: %s"), lang_name);
 
 
-           do_cleanups (old_chain);
 
-         }
 
-       else
 
-         break;
 
 
-       if (*chp != '\0' && !isspace (*chp))
 
-         error (_("Invalid value for the '%s' option"), option);
 
-       chp = skip_spaces_const (chp);
 
-     }
 
 
-   
 
-   if (parse->cmd->argv_func != NULL)
 
-     {
 
-       mi_parse_argv (chp, parse);
 
-       if (parse->argv == NULL)
 
-         error (_("Problem parsing arguments: %s %s"), parse->command, chp);
 
-     }
 
 
-   FIXME
 
-   
 
-   if (parse->cmd->cli.cmd != NULL)
 
-     parse->args = xstrdup (chp);
 
 
-   discard_cleanups (cleanup);
 
 
-   
 
-   parse->op = MI_COMMAND;
 
-   return parse;
 
- }
 
 
- enum print_values
 
- mi_parse_print_values (const char *name)
 
- {
 
-    if (strcmp (name, "0") == 0
 
-        || strcmp (name, mi_no_values) == 0)
 
-      return PRINT_NO_VALUES;
 
-    else if (strcmp (name, "1") == 0
 
-             || strcmp (name, mi_all_values) == 0)
 
-      return PRINT_ALL_VALUES;
 
-    else if (strcmp (name, "2") == 0
 
-             || strcmp (name, mi_simple_values) == 0)
 
-      return PRINT_SIMPLE_VALUES;
 
-    else
 
-      error (_("Unknown value for PRINT_VALUES: must be: \
 
- 0 or \"%s\", 1 or \"%s\", 2 or \"%s\""),
 
-             mi_no_values, mi_all_values, mi_simple_values);
 
- }