All of lore.kernel.org
 help / color / mirror / Atom feed
From: Fouad Hilly <fouad.hilly@cloud.com>
To: Xen-devel <xen-devel@lists.xenproject.org>
Cc: Fouad Hilly <fouad.hilly@cloud.com>,
	Anthony PERARD <anthony.perard@citrix.com>
Subject: [PATCH v2 4/5] x86: Use getopt to handle command line args
Date: Tue, 16 Apr 2024 10:15:45 +0100	[thread overview]
Message-ID: <20240416091546.11622-5-fouad.hilly@cloud.com> (raw)
In-Reply-To: <20240416091546.11622-1-fouad.hilly@cloud.com>

Use getopt_long() to handle command line arguments.
Introduce ext_err for common exit with errors.

[v2]
1- Removed unnecessary NULL initialization.
2- Used static at the beginning of type struct declaration.
3- Corrected switch\case indentations.
4- Removed redundant checks.
5- Corrected label indentation.

Signed-off-by: Fouad Hilly <fouad.hilly@cloud.com>
---
 tools/misc/xen-ucode.c | 43 ++++++++++++++++++++++++++++++------------
 1 file changed, 31 insertions(+), 12 deletions(-)

diff --git a/tools/misc/xen-ucode.c b/tools/misc/xen-ucode.c
index 0c0b2337b4ea..e3c1943e3633 100644
--- a/tools/misc/xen-ucode.c
+++ b/tools/misc/xen-ucode.c
@@ -11,6 +11,7 @@
 #include <sys/stat.h>
 #include <fcntl.h>
 #include <xenctrl.h>
+#include <getopt.h>
 
 static xc_interface *xch;
 
@@ -22,7 +23,8 @@ static void usage(const char *name)
     printf("%s: Xen microcode updating tool\n"
            "Usage: %s [microcode file] [options]\n"
            "Options:\n"
-           "show-cou-info   show CPU information and exit\n",
+           "  -h, --help            display this help and exit\n"
+           "  -s, --show-cpu-info   show CPU information and exit\n",
            name, name);
 }
 
@@ -86,6 +88,13 @@ int main(int argc, char *argv[])
     char *filename, *buf;
     size_t len;
     struct stat st;
+    int opt;
+
+    static const struct option options[] = {
+        {"help", no_argument, NULL, 'h'},
+        {"show-cpu-info", no_argument, NULL, 's'},
+        {NULL, no_argument, NULL, 0}
+    };
 
     xch = xc_interface_open(NULL, NULL, 0);
     if ( xch == NULL )
@@ -95,19 +104,22 @@ int main(int argc, char *argv[])
         exit(1);
     }
 
-    if ( argc < 2 )
-    {
-        fprintf(stderr,
-                "xen-ucode: Xen microcode updating tool\n"
-                "Usage: %s [<microcode file> | show-cpu-info]\n", argv[0]);
-        show_curr_cpu(stderr);
-        exit(2);
-    }
+    if ( argc != 2 )
+        goto ext_err;
 
-    if ( !strcmp(argv[1], "show-cpu-info") )
+    while ( (opt = getopt_long(argc, argv, "hs", options, NULL)) != -1 )
     {
-        show_curr_cpu(stdout);
-        return 0;
+        switch (opt)
+        {
+        case 'h':
+            usage(argv[0]);
+            exit(EXIT_SUCCESS);
+        case 's':
+            show_curr_cpu(stdout);
+            exit(EXIT_SUCCESS);
+        default:
+            goto ext_err;
+        }
     }
 
     filename = argv[1];
@@ -152,4 +164,11 @@ int main(int argc, char *argv[])
     close(fd);
 
     return 0;
+
+ ext_err:
+    fprintf(stderr,
+            "xen-ucode: Xen microcode updating tool\n"
+            "Usage: %s [microcode file] [options]\n", argv[0]);
+    show_curr_cpu(stderr);
+    exit(STDERR_FILENO);
 }
-- 
2.42.0



  parent reply	other threads:[~2024-04-16  9:16 UTC|newest]

Thread overview: 20+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-04-16  9:15 [PATCH v2 0/5] x86/xen-ucode: Introduce --force option Fouad Hilly
2024-04-16  9:15 ` [PATCH v2 1/5] x86: Update x86 low level version check of microcode Fouad Hilly
2024-04-18 10:05   ` Andrew Cooper
2024-04-23 15:01     ` Fouad Hilly
2024-04-22 14:09   ` Jan Beulich
2024-04-23 15:00     ` Fouad Hilly
2024-04-16  9:15 ` [PATCH v2 2/5] x86: Refactor microcode_update() hypercall with flags Fouad Hilly
2024-04-22 14:18   ` Jan Beulich
2024-04-23 14:43     ` Fouad Hilly
2024-04-16  9:15 ` [PATCH v2 3/5] x86: Add usage() to print out usage message Fouad Hilly
2024-04-18  6:18   ` Jan Beulich
2024-04-22 17:30   ` Anthony PERARD
2024-04-16  9:15 ` Fouad Hilly [this message]
2024-04-22 17:48   ` [PATCH v2 4/5] x86: Use getopt to handle command line args Anthony PERARD
2024-04-23 15:16     ` Fouad Hilly
2024-04-23 16:50       ` Anthony PERARD
2024-04-16  9:15 ` [PATCH v2 5/5] x86: Add --force option to xen-ucode to override microcode version check Fouad Hilly
2024-04-22 17:57   ` Anthony PERARD
2024-04-23 15:26     ` Fouad Hilly
2024-04-23 16:58       ` Anthony PERARD

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=20240416091546.11622-5-fouad.hilly@cloud.com \
    --to=fouad.hilly@cloud.com \
    --cc=anthony.perard@citrix.com \
    --cc=xen-devel@lists.xenproject.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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.