From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 2126) id 0176E385840C; Tue, 23 May 2023 19:47:57 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 0176E385840C DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1684871278; bh=kMrrhs8lo+eRYdYUo7KZxKjmImRybRdI9XT7h2+MNpQ=; h=From:To:Subject:Date:From; b=qsIO8briINAmcaNOw3OrqnsOUggUXCABTpT1v9uxX5dITX2rVDBe8ZJUScRmFCAwT FFA4O4l/+pDHSJjXgOQBLq01MR9Vh9XGtbc7nLTAXTBxviT7BXCq0yr5JdDS0nRqYD CEK44JhMFOEXmh+Ta2jpNT58WheFNwCE6v8QWyOM= Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable From: Tom Tromey To: gdb-cvs@sourceware.org Subject: [binutils-gdb] Add second mi_parse constructor X-Act-Checkin: binutils-gdb X-Git-Author: Tom Tromey X-Git-Refname: refs/heads/master X-Git-Oldrev: 6b2cb925fe4a65ae96c7603b5d002712b66c7122 X-Git-Newrev: e7a2797eb00dbbceb6796f1baa120055d174a229 Message-Id: <20230523194758.0176E385840C@sourceware.org> Date: Tue, 23 May 2023 19:47:57 +0000 (GMT) List-Id: https://sourceware.org/git/gitweb.cgi?p=3Dbinutils-gdb.git;h=3De7a2797eb00d= bbceb6796f1baa120055d174a229 commit e7a2797eb00dbbceb6796f1baa120055d174a229 Author: Tom Tromey Date: Mon Mar 20 11:38:00 2023 -0600 Add second mi_parse constructor =20 This adds a second mi_parse constructor. This constructor takes a command name and vector of arguments, and does not do any escape processing. This also changes mi_parse::args to handle parse objects created this new way. Diff: --- gdb/mi/mi-parse.c | 97 +++++++++++++++++++++++++++++++++++++++++++++++++++= ++++ gdb/mi/mi-parse.h | 12 +++++-- 2 files changed, 107 insertions(+), 2 deletions(-) diff --git a/gdb/mi/mi-parse.c b/gdb/mi/mi-parse.c index 09207164291..a113d4d48da 100644 --- a/gdb/mi/mi-parse.c +++ b/gdb/mi/mi-parse.c @@ -109,6 +109,11 @@ mi_parse_escape (const char **string_ptr) void mi_parse::parse_argv () { + /* If arguments were already computed (or were supplied at + construction), then there's no need to re-compute them. */ + if (argv !=3D nullptr) + return; + const char *chp =3D m_args.c_str (); int argc =3D 0; char **argv =3D XNEWVEC (char *, argc + 1); @@ -217,6 +222,27 @@ mi_parse::~mi_parse () =20 /* See mi-parse.h. */ =20 +const char * +mi_parse::args () +{ + /* If args were already computed, or if there is no pre-computed + argv, just return the args. */ + if (!m_args.empty () || argv =3D=3D nullptr) + return m_args.c_str (); + + /* Compute args from argv. */ + for (int i =3D 0; i < argc; ++i) + { + if (!m_args.empty ()) + m_args +=3D " "; + m_args +=3D argv[i]; + } + + return m_args.c_str (); +} + +/* See mi-parse.h. */ + void mi_parse::set_thread_group (const char *arg, char **endp) { @@ -387,6 +413,77 @@ mi_parse::make (const char *cmd, char **token) return parse; } =20 +/* See mi-parse.h. */ + +std::unique_ptr +mi_parse::make (gdb::unique_xmalloc_ptr command, + std::vector> args) +{ + std::unique_ptr parse (new struct mi_parse); + + parse->command =3D command.release (); + parse->token =3D xstrdup (""); + + if (parse->command[0] !=3D '-') + throw_error (UNDEFINED_COMMAND_ERROR, + _("MI command '%s' does not start with '-'"), + parse->command); + + /* Find the command in the MI table. */ + parse->cmd =3D mi_cmd_lookup (parse->command + 1); + if (parse->cmd =3D=3D NULL) + throw_error (UNDEFINED_COMMAND_ERROR, + _("Undefined MI command: %s"), parse->command); + + /* This over-allocates slightly, but it seems unimportant. */ + parse->argv =3D XCNEWVEC (char *, args.size () + 1); + + for (size_t i =3D 0; i < args.size (); ++i) + { + const char *chp =3D args[i].get (); + + /* See if --all is the last token in the input. */ + if (strcmp (chp, "--all") =3D=3D 0) + { + parse->all =3D 1; + } + else if (strcmp (chp, "--thread-group") =3D=3D 0) + { + ++i; + if (i =3D=3D args.size ()) + error ("No argument to '--thread-group'"); + parse->set_thread_group (args[i].get (), nullptr); + } + else if (strcmp (chp, "--thread") =3D=3D 0) + { + ++i; + if (i =3D=3D args.size ()) + error ("No argument to '--thread'"); + parse->set_thread (args[i].get (), nullptr); + } + else if (strcmp (chp, "--frame") =3D=3D 0) + { + ++i; + if (i =3D=3D args.size ()) + error ("No argument to '--frame'"); + parse->set_frame (args[i].get (), nullptr); + } + else if (strcmp (chp, "--language") =3D=3D 0) + { + ++i; + if (i =3D=3D args.size ()) + error ("No argument to '--language'"); + parse->set_language (args[i].get (), nullptr); + } + else + parse->argv[parse->argc++] =3D args[i].release (); + } + + /* Fully parsed, flag as an MI command. */ + parse->op =3D MI_COMMAND; + return parse; +} + enum print_values mi_parse_print_values (const char *name) { diff --git a/gdb/mi/mi-parse.h b/gdb/mi/mi-parse.h index 19c41f23ed6..6373543529b 100644 --- a/gdb/mi/mi-parse.h +++ b/gdb/mi/mi-parse.h @@ -52,6 +52,15 @@ struct mi_parse static std::unique_ptr make (const char *cmd, char **token); =20 + /* Create an mi_parse object given the command name and a vector + of arguments. Unlike with the other constructor, here the + arguments are treated "as is" -- no escape processing is + done. */ + + static std::unique_ptr make + (gdb::unique_xmalloc_ptr command, + std::vector> args); + ~mi_parse (); =20 DISABLE_COPY_AND_ASSIGN (mi_parse); @@ -61,8 +70,7 @@ struct mi_parse =20 /* Return the full argument string, as used by commands which are implemented as CLI commands. */ - const char *args () const - { return m_args.c_str (); } + const char *args (); =20 enum mi_command_type op =3D MI_COMMAND; char *command =3D nullptr;