From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 2126) id DFE6B3858291; Tue, 23 May 2023 19:47:52 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org DFE6B3858291 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1684871272; bh=wkf/9zANPLXXvhsblmh5ByUj21NuTqk9DxIFWs3Va9o=; h=From:To:Subject:Date:From; b=QiNvHqfZ8yB6avWpckpoVlCn7c8HuKYtGa73UVdiSlyIEfjZQIPcs1x1w2CpZI82E 8E9sP5JDc8qrCEcrT3UVMnf4CHj8pwrMWzFIBdsTAMC6h35nIIIl7sjTHumBkmGyAD FGcS5KpIqRJMM0qhg6R2J11/Pdau0TNwJkey8uXs= 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] Introduce mi_parse helper methods X-Act-Checkin: binutils-gdb X-Git-Author: Tom Tromey X-Git-Refname: refs/heads/master X-Git-Oldrev: fde3f93adb50c9937cd2e1c93561aea2fd167156 X-Git-Newrev: 6b2cb925fe4a65ae96c7603b5d002712b66c7122 Message-Id: <20230523194752.DFE6B3858291@sourceware.org> Date: Tue, 23 May 2023 19:47:52 +0000 (GMT) List-Id: https://sourceware.org/git/gitweb.cgi?p=3Dbinutils-gdb.git;h=3D6b2cb925fe4a= 65ae96c7603b5d002712b66c7122 commit 6b2cb925fe4a65ae96c7603b5d002712b66c7122 Author: Tom Tromey Date: Mon Mar 20 11:25:12 2023 -0600 Introduce mi_parse helper methods =20 This introduces some helper methods for mi_parse that handle some of the details of parsing. This approach lets us reuse them later. Diff: --- gdb/mi/mi-parse.c | 69 +++++++++++++++++++++++++++++++++++++++++----------= ---- gdb/mi/mi-parse.h | 9 ++++++++ 2 files changed, 61 insertions(+), 17 deletions(-) diff --git a/gdb/mi/mi-parse.c b/gdb/mi/mi-parse.c index b7c5a8cecdf..09207164291 100644 --- a/gdb/mi/mi-parse.c +++ b/gdb/mi/mi-parse.c @@ -215,6 +215,54 @@ mi_parse::~mi_parse () freeargv (argv); } =20 +/* See mi-parse.h. */ + +void +mi_parse::set_thread_group (const char *arg, char **endp) +{ + if (thread_group !=3D -1) + error (_("Duplicate '--thread-group' option")); + if (*arg !=3D 'i') + error (_("Invalid thread group id")); + arg +=3D 1; + thread_group =3D strtol (arg, endp, 10); +} + +/* See mi-parse.h. */ + +void +mi_parse::set_thread (const char *arg, char **endp) +{ + if (thread !=3D -1) + error (_("Duplicate '--thread' option")); + thread =3D strtol (arg, endp, 10); +} + +/* See mi-parse.h. */ + +void +mi_parse::set_frame (const char *arg, char **endp) +{ + if (frame !=3D -1) + error (_("Duplicate '--frame' option")); + frame =3D strtol (arg, endp, 10); +} + +/* See mi-parse.h. */ + +void +mi_parse::set_language (const char *arg, const char **endp) +{ + std::string lang_name =3D extract_arg (&arg); + + language =3D language_enum (lang_name.c_str ()); + if (language =3D=3D language_unknown) + error (_("Invalid --language argument: %s"), lang_name.c_str ()); + + if (endp !=3D nullptr) + *endp =3D arg; +} + std::unique_ptr mi_parse::make (const char *cmd, char **token) { @@ -295,13 +343,8 @@ mi_parse::make (const char *cmd, char **token) char *endp; =20 option =3D "--thread-group"; - if (parse->thread_group !=3D -1) - error (_("Duplicate '--thread-group' option")); chp +=3D tgs; - if (*chp !=3D 'i') - error (_("Invalid thread group id")); - chp +=3D 1; - parse->thread_group =3D strtol (chp, &endp, 10); + parse->set_thread_group (chp, &endp); chp =3D endp; } else if (strncmp (chp, "--thread ", ts) =3D=3D 0) @@ -309,10 +352,8 @@ mi_parse::make (const char *cmd, char **token) char *endp; =20 option =3D "--thread"; - if (parse->thread !=3D -1) - error (_("Duplicate '--thread' option")); chp +=3D ts; - parse->thread =3D strtol (chp, &endp, 10); + parse->set_thread (chp, &endp); chp =3D endp; } else if (strncmp (chp, "--frame ", fs) =3D=3D 0) @@ -320,21 +361,15 @@ mi_parse::make (const char *cmd, char **token) char *endp; =20 option =3D "--frame"; - if (parse->frame !=3D -1) - error (_("Duplicate '--frame' option")); chp +=3D fs; - parse->frame =3D strtol (chp, &endp, 10); + parse->set_frame (chp, &endp); chp =3D endp; } else if (strncmp (chp, "--language ", ls) =3D=3D 0) { option =3D "--language"; chp +=3D ls; - std::string lang_name =3D extract_arg (&chp); - - parse->language =3D language_enum (lang_name.c_str ()); - if (parse->language =3D=3D language_unknown) - error (_("Invalid --language argument: %s"), lang_name.c_str ()); + parse->set_language (chp, &chp); } else break; diff --git a/gdb/mi/mi-parse.h b/gdb/mi/mi-parse.h index 6f1da6e6eb5..19c41f23ed6 100644 --- a/gdb/mi/mi-parse.h +++ b/gdb/mi/mi-parse.h @@ -84,6 +84,15 @@ struct mi_parse =20 mi_parse () =3D default; =20 + /* Helper methods for parsing arguments. Each takes the argument + to be parsed. It will either set a member of this object, or + throw an exception on error. In each case, *ENDP, if non-NULL, + will be updated to just after the argument text. */ + void set_thread_group (const char *arg, char **endp); + void set_thread (const char *arg, char **endp); + void set_frame (const char *arg, char **endp); + void set_language (const char *arg, const char **endp); + std::string m_args; };