public inbox for gdb-patches@sourceware.org
 help / color / mirror / Atom feed
* [PATCH v2 1/2] gdbreplay: Use getopt_long to parse command line arguments
@ 2024-10-17 15:14 Alexandra Hájková
  2024-10-17 20:09 ` Tom Tromey
  0 siblings, 1 reply; 2+ messages in thread
From: Alexandra Hájková @ 2024-10-17 15:14 UTC (permalink / raw)
  To: gdb-patches; +Cc: ahajkova

---
v2: - drop next_arg and use argv[optind] instead
    - use nullptr instead of NULL
    - fix the segfault
 gdbserver/gdbreplay.cc | 38 +++++++++++++++++++++++++++-----------
 1 file changed, 27 insertions(+), 11 deletions(-)

diff --git a/gdbserver/gdbreplay.cc b/gdbserver/gdbreplay.cc
index c2359e4ab43..0d77bce6596 100644
--- a/gdbserver/gdbreplay.cc
+++ b/gdbserver/gdbreplay.cc
@@ -57,6 +57,8 @@
 #include "gdbsupport/netstuff.h"
 #include "gdbsupport/rsp-low.h"
 
+#include "getopt.h"
+
 #ifndef HAVE_SOCKLEN_T
 typedef int socklen_t;
 #endif
@@ -425,30 +427,44 @@ gdbreplay_usage (FILE *stream)
 captured_main (int argc, char *argv[])
 {
   FILE *fp;
-  int ch;
+  int ch, optc;
+  enum opts { OPT_VERSION = 1, OPT_HELP };
+  static struct option longopts[] =
+    {
+	{"version", no_argument, nullptr, OPT_VERSION},
+	{"help", no_argument, nullptr, OPT_HELP},
+	{nullptr, no_argument, nullptr, 0}
+    };
 
-  if (argc >= 2 && strcmp (argv[1], "--version") == 0)
+  while ((optc = getopt_long (argc, argv, "", longopts, nullptr)) != -1)
     {
-      gdbreplay_version ();
-      exit (0);
+      switch (optc)
+	{
+	case OPT_VERSION:
+	  gdbreplay_version ();
+	  exit (0);
+	case OPT_HELP:
+	  gdbreplay_usage (stdout);
+	  exit (0);
+	}
     }
-  if (argc >= 2 && strcmp (argv[1], "--help") == 0)
+
+  if (optind + 2 != argc)
     {
-      gdbreplay_usage (stdout);
-      exit (0);
+      gdbreplay_usage (stderr);
+      exit (1);
     }
-
   if (argc < 3)
     {
       gdbreplay_usage (stderr);
       exit (1);
     }
-  fp = fopen (argv[1], "r");
+  fp = fopen (argv[optind], "r");
   if (fp == NULL)
     {
-      perror_with_name (argv[1]);
+      perror_with_name (argv[optind]);
     }
-  remote_open (argv[2]);
+  remote_open (argv[optind + 1]);
   while ((ch = logchar (fp)) != EOF)
     {
       switch (ch)
-- 
2.46.2


^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: [PATCH v2 1/2] gdbreplay: Use getopt_long to parse command line arguments
  2024-10-17 15:14 [PATCH v2 1/2] gdbreplay: Use getopt_long to parse command line arguments Alexandra Hájková
@ 2024-10-17 20:09 ` Tom Tromey
  0 siblings, 0 replies; 2+ messages in thread
From: Tom Tromey @ 2024-10-17 20:09 UTC (permalink / raw)
  To: Alexandra Hájková; +Cc: gdb-patches

>>>>> "Alexandra" == Alexandra Hájková <ahajkova@redhat.com> writes:

Alexandra> +  if (optind + 2 != argc)
Alexandra>      {
Alexandra> -      gdbreplay_usage (stdout);
Alexandra> -      exit (0);
Alexandra> +      gdbreplay_usage (stderr);
Alexandra> +      exit (1);
Alexandra>      }
Alexandra> -
Alexandra>    if (argc < 3)
Alexandra>      {
Alexandra>        gdbreplay_usage (stderr);
Alexandra>        exit (1);
Alexandra>      }

It seems to me that this argc check and the 'optind + 2' check are
basically doing the same thing, and so probably the latter should be
removed.

Tom

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2024-10-17 20:09 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-10-17 15:14 [PATCH v2 1/2] gdbreplay: Use getopt_long to parse command line arguments Alexandra Hájková
2024-10-17 20:09 ` Tom Tromey

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).