public inbox for gdb-patches@sourceware.org
 help / color / mirror / Atom feed
From: "Alexandra Hájková" <ahajkova@redhat.com>
To: gdb-patches@sourceware.org
Cc: ahajkova@redhat.com
Subject: [PATCH v2 1/2] gdbreplay: Use getopt_long to parse command line arguments
Date: Thu, 17 Oct 2024 17:14:15 +0200	[thread overview]
Message-ID: <20241017151559.1165089-1-ahajkova@redhat.com> (raw)

---
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


             reply	other threads:[~2024-10-17 15:16 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-10-17 15:14 Alexandra Hájková [this message]
2024-10-17 20:09 ` Tom Tromey

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20241017151559.1165089-1-ahajkova@redhat.com \
    --to=ahajkova@redhat.com \
    --cc=gdb-patches@sourceware.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).