OpenCores
URL https://opencores.org/ocsvn/or1k/or1k/trunk

Subversion Repositories or1k

[/] [or1k/] [trunk/] [gdb-5.0/] [gdb/] [mi/] [mi-parse.c] - Diff between revs 107 and 1765

Go to most recent revision | Only display areas with differences | Details | Blame | View Log

Rev 107 Rev 1765
/* MI Command Set - MI parser.
/* MI Command Set - MI parser.
   Copyright (C) 2000, Free Software Foundation, Inc.
   Copyright (C) 2000, Free Software Foundation, Inc.
   Contributed by Cygnus Solutions (a Red Hat company).
   Contributed by Cygnus Solutions (a Red Hat company).
 
 
   This file is part of GDB.
   This file is part of GDB.
 
 
   This program is free software; you can redistribute it and/or modify
   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2 of the License, or
   the Free Software Foundation; either version 2 of the License, or
   (at your option) any later version.
   (at your option) any later version.
 
 
   This program is distributed in the hope that it will be useful,
   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.
   GNU General Public License for more details.
 
 
   You should have received a copy of the GNU General Public License
   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   along with this program; if not, write to the Free Software
   Foundation, Inc., 59 Temple Place - Suite 330,
   Foundation, Inc., 59 Temple Place - Suite 330,
   Boston, MA 02111-1307, USA.  */
   Boston, MA 02111-1307, USA.  */
 
 
#include "defs.h"
#include "defs.h"
#include "mi-cmds.h"
#include "mi-cmds.h"
#include "mi-parse.h"
#include "mi-parse.h"
 
 
#include <ctype.h>
#include <ctype.h>
#include <string.h>
#include <string.h>
 
 
#undef XMALLOC
#undef XMALLOC
#define XMALLOC(TYPE) ((TYPE*) xmalloc (sizeof (TYPE)))
#define XMALLOC(TYPE) ((TYPE*) xmalloc (sizeof (TYPE)))
 
 
static void
static void
mi_parse_argv (char *args, struct mi_parse *parse)
mi_parse_argv (char *args, struct mi_parse *parse)
{
{
  char *chp = args;
  char *chp = args;
  int argc = 0;
  int argc = 0;
  char **argv = xmalloc ((argc + 1) * sizeof (char *));
  char **argv = xmalloc ((argc + 1) * sizeof (char *));
  argv[argc] = NULL;
  argv[argc] = NULL;
  while (1)
  while (1)
    {
    {
      char *arg;
      char *arg;
      /* skip leading white space */
      /* skip leading white space */
      while (isspace (*chp))
      while (isspace (*chp))
        chp++;
        chp++;
      /* Three possibilities: EOF, quoted string, or other text. */
      /* Three possibilities: EOF, quoted string, or other text. */
      switch (*chp)
      switch (*chp)
        {
        {
        case '\0':
        case '\0':
          parse->argv = argv;
          parse->argv = argv;
          parse->argc = argc;
          parse->argc = argc;
          return;
          return;
        case '"':
        case '"':
          {
          {
            /* A quoted string. */
            /* A quoted string. */
            int len;
            int len;
            char *start = chp + 1;
            char *start = chp + 1;
            /* Determine the buffer size. */
            /* Determine the buffer size. */
            chp = start;
            chp = start;
            len = 0;
            len = 0;
            while (*chp != '\0' && *chp != '"')
            while (*chp != '\0' && *chp != '"')
              {
              {
                if (*chp == '\\')
                if (*chp == '\\')
                  {
                  {
                    chp++;
                    chp++;
                    if (parse_escape (&chp) <= 0)
                    if (parse_escape (&chp) <= 0)
                      {
                      {
                        /* Do not allow split lines or "\000" */
                        /* Do not allow split lines or "\000" */
                        freeargv (argv);
                        freeargv (argv);
                        return;
                        return;
                      }
                      }
                  }
                  }
                else
                else
                  chp++;
                  chp++;
                len++;
                len++;
              }
              }
            /* Insist on a closing quote. */
            /* Insist on a closing quote. */
            if (*chp != '"')
            if (*chp != '"')
              {
              {
                freeargv (argv);
                freeargv (argv);
                return;
                return;
              }
              }
            /* Insist on trailing white space. */
            /* Insist on trailing white space. */
            if (chp[1] != '\0' && !isspace (chp[1]))
            if (chp[1] != '\0' && !isspace (chp[1]))
              {
              {
                freeargv (argv);
                freeargv (argv);
                return;
                return;
              }
              }
            /* create the buffer. */
            /* create the buffer. */
            arg = xmalloc ((len + 1) * sizeof (char));
            arg = xmalloc ((len + 1) * sizeof (char));
            /* And copy the characters in. */
            /* And copy the characters in. */
            chp = start;
            chp = start;
            len = 0;
            len = 0;
            while (*chp != '\0' && *chp != '"')
            while (*chp != '\0' && *chp != '"')
              {
              {
                if (*chp == '\\')
                if (*chp == '\\')
                  {
                  {
                    chp++;
                    chp++;
                    arg[len] = parse_escape (&chp);
                    arg[len] = parse_escape (&chp);
                  }
                  }
                else
                else
                  arg[len] = *chp++;
                  arg[len] = *chp++;
                len++;
                len++;
              }
              }
            arg[len] = '\0';
            arg[len] = '\0';
            chp++;              /* that closing quote. */
            chp++;              /* that closing quote. */
            break;
            break;
          }
          }
        default:
        default:
          {
          {
            /* An unquoted string.  Accumulate all non blank
            /* An unquoted string.  Accumulate all non blank
               characters into a buffer. */
               characters into a buffer. */
            int len;
            int len;
            char *start = chp;
            char *start = chp;
            while (*chp != '\0' && !isspace (*chp))
            while (*chp != '\0' && !isspace (*chp))
              {
              {
                chp++;
                chp++;
              }
              }
            len = chp - start;
            len = chp - start;
            arg = xmalloc ((len + 1) * sizeof (char));
            arg = xmalloc ((len + 1) * sizeof (char));
            strncpy (arg, start, len);
            strncpy (arg, start, len);
            arg[len] = '\0';
            arg[len] = '\0';
            break;
            break;
          }
          }
        }
        }
      /* Append arg to argv. */
      /* Append arg to argv. */
      argv = xrealloc (argv, (argc + 2) * sizeof (char *));
      argv = xrealloc (argv, (argc + 2) * sizeof (char *));
      argv[argc++] = arg;
      argv[argc++] = arg;
      argv[argc] = NULL;
      argv[argc] = NULL;
    }
    }
}
}
 
 
 
 
void
void
mi_parse_free (struct mi_parse *parse)
mi_parse_free (struct mi_parse *parse)
{
{
  if (parse == NULL)
  if (parse == NULL)
    return;
    return;
  if (parse->command != NULL)
  if (parse->command != NULL)
    free (parse->command);
    free (parse->command);
  if (parse->token != NULL)
  if (parse->token != NULL)
    free (parse->token);
    free (parse->token);
  if (parse->args != NULL)
  if (parse->args != NULL)
    free (parse->args);
    free (parse->args);
  if (parse->argv != NULL)
  if (parse->argv != NULL)
    freeargv (parse->argv);
    freeargv (parse->argv);
  free (parse);
  free (parse);
}
}
 
 
 
 
struct mi_parse *
struct mi_parse *
mi_parse (char *cmd)
mi_parse (char *cmd)
{
{
  char *chp;
  char *chp;
  struct mi_parse *parse = XMALLOC (struct mi_parse);
  struct mi_parse *parse = XMALLOC (struct mi_parse);
  memset (parse, 0, sizeof (*parse));
  memset (parse, 0, sizeof (*parse));
 
 
  /* Before starting, skip leading white space. */
  /* Before starting, skip leading white space. */
  while (isspace (*cmd))
  while (isspace (*cmd))
    cmd++;
    cmd++;
 
 
  /* Find/skip any token and then extract it. */
  /* Find/skip any token and then extract it. */
  for (chp = cmd; *chp >= '0' && *chp <= '9'; chp++)
  for (chp = cmd; *chp >= '0' && *chp <= '9'; chp++)
    ;
    ;
  parse->token = xmalloc ((chp - cmd + 1) * sizeof (char *));
  parse->token = xmalloc ((chp - cmd + 1) * sizeof (char *));
  memcpy (parse->token, cmd, (chp - cmd));
  memcpy (parse->token, cmd, (chp - cmd));
  parse->token[chp - cmd] = '\0';
  parse->token[chp - cmd] = '\0';
 
 
  /* This wasn't a real MI command.  Return it as a CLI_COMMAND. */
  /* This wasn't a real MI command.  Return it as a CLI_COMMAND. */
  if (*chp != '-')
  if (*chp != '-')
    {
    {
      while (isspace (*chp))
      while (isspace (*chp))
        chp++;
        chp++;
      parse->command = xstrdup (chp);
      parse->command = xstrdup (chp);
      parse->op = CLI_COMMAND;
      parse->op = CLI_COMMAND;
      return parse;
      return parse;
    }
    }
 
 
  /* Extract the command. */
  /* Extract the command. */
  {
  {
    char *tmp = chp + 1;        /* discard ``-'' */
    char *tmp = chp + 1;        /* discard ``-'' */
    for (; *chp && !isspace (*chp); chp++)
    for (; *chp && !isspace (*chp); chp++)
      ;
      ;
    parse->command = xmalloc ((chp - tmp + 1) * sizeof (char *));
    parse->command = xmalloc ((chp - tmp + 1) * sizeof (char *));
    memcpy (parse->command, tmp, chp - tmp);
    memcpy (parse->command, tmp, chp - tmp);
    parse->command[chp - tmp] = '\0';
    parse->command[chp - tmp] = '\0';
  }
  }
 
 
  /* Find the command in the MI table. */
  /* Find the command in the MI table. */
  parse->cmd = mi_lookup (parse->command);
  parse->cmd = mi_lookup (parse->command);
  if (parse->cmd == NULL)
  if (parse->cmd == NULL)
    {
    {
      /* FIXME: This should be a function call. */
      /* FIXME: This should be a function call. */
      fprintf_unfiltered
      fprintf_unfiltered
        (raw_stdout,
        (raw_stdout,
         "%s^error,msg=\"Undefined MI command: %s\"\n",
         "%s^error,msg=\"Undefined MI command: %s\"\n",
         parse->token, parse->command);
         parse->token, parse->command);
      mi_parse_free (parse);
      mi_parse_free (parse);
      return NULL;
      return NULL;
    }
    }
 
 
  /* Skip white space following the command. */
  /* Skip white space following the command. */
  while (isspace (*chp))
  while (isspace (*chp))
    chp++;
    chp++;
 
 
  /* For new argv commands, attempt to return the parsed argument
  /* For new argv commands, attempt to return the parsed argument
     list. */
     list. */
  if (parse->cmd->argv_func != NULL)
  if (parse->cmd->argv_func != NULL)
    {
    {
      mi_parse_argv (chp, parse);
      mi_parse_argv (chp, parse);
      if (parse->argv == NULL)
      if (parse->argv == NULL)
        {
        {
          /* FIXME: This should be a function call. */
          /* FIXME: This should be a function call. */
          fprintf_unfiltered
          fprintf_unfiltered
            (raw_stdout,
            (raw_stdout,
             "%s^error,msg=\"Problem parsing arguments: %s %s\"\n",
             "%s^error,msg=\"Problem parsing arguments: %s %s\"\n",
             parse->token, parse->command, chp);
             parse->token, parse->command, chp);
          mi_parse_free (parse);
          mi_parse_free (parse);
          return NULL;
          return NULL;
        }
        }
    }
    }
 
 
  /* FIXME: DELETE THIS */
  /* FIXME: DELETE THIS */
  /* For CLI and old ARGS commands, also return the remainder of the
  /* For CLI and old ARGS commands, also return the remainder of the
     command line as a single string. */
     command line as a single string. */
  if (parse->cmd->args_func != NULL
  if (parse->cmd->args_func != NULL
      || parse->cmd->cli != NULL)
      || parse->cmd->cli != NULL)
    {
    {
      parse->args = xstrdup (chp);
      parse->args = xstrdup (chp);
    }
    }
 
 
  /* Fully parsed. */
  /* Fully parsed. */
  parse->op = MI_COMMAND;
  parse->op = MI_COMMAND;
  return parse;
  return parse;
}
}
 
 
void
void
_initialize_mi_parse ()
_initialize_mi_parse ()
{
{
}
}
 
 
/* Local variables: */
/* Local variables: */
/* change-log-default-name: "ChangeLog-mi" */
/* change-log-default-name: "ChangeLog-mi" */
/* End: */
/* End: */
 
 

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.