Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/sbin/gpt Refactor the command dispatching and help printing ...



details:   https://anonhg.NetBSD.org/src/rev/800f78d1b530
branches:  trunk
changeset: 341979:800f78d1b530
user:      christos <christos%NetBSD.org@localhost>
date:      Tue Dec 01 16:32:19 2015 +0000

description:
Refactor the command dispatching and help printing logic.

diffstat:

 sbin/gpt/add.c        |   65 ++++++++++++-----------
 sbin/gpt/backup.c     |   26 +++++----
 sbin/gpt/biosboot.c   |   54 ++++++++++++-------
 sbin/gpt/create.c     |   28 +++++----
 sbin/gpt/destroy.c    |   28 +++++----
 sbin/gpt/gpt.c        |   24 ++++++++-
 sbin/gpt/gpt.h        |   27 +++------
 sbin/gpt/header.c     |   26 +++++----
 sbin/gpt/label.c      |   64 +++++++++++-----------
 sbin/gpt/main.c       |  136 +++++++++++++++++++------------------------------
 sbin/gpt/migrate.c    |   28 +++++----
 sbin/gpt/recover.c    |   28 +++++----
 sbin/gpt/remove.c     |   57 ++++++++++----------
 sbin/gpt/resize.c     |   50 +++++++++--------
 sbin/gpt/resizedisk.c |   38 +++++++------
 sbin/gpt/restore.c    |   28 +++++----
 sbin/gpt/set.c        |   36 +++++++-----
 sbin/gpt/show.c       |   32 ++++++-----
 sbin/gpt/type.c       |   65 +++++++++++------------
 sbin/gpt/unset.c      |   36 +++++++-----
 20 files changed, 461 insertions(+), 415 deletions(-)

diffs (truncated from 1843 to 300 lines):

diff -r 6d15ac679f5a -r 800f78d1b530 sbin/gpt/add.c
--- a/sbin/gpt/add.c    Tue Dec 01 12:07:41 2015 +0000
+++ b/sbin/gpt/add.c    Tue Dec 01 16:32:19 2015 +0000
@@ -33,7 +33,7 @@
 __FBSDID("$FreeBSD: src/sbin/gpt/add.c,v 1.14 2006/06/22 22:05:28 marcel Exp $");
 #endif
 #ifdef __RCSID
-__RCSID("$NetBSD: add.c,v 1.31 2015/12/01 09:05:33 christos Exp $");
+__RCSID("$NetBSD: add.c,v 1.32 2015/12/01 16:32:19 christos Exp $");
 #endif
 
 #include <sys/types.h>
@@ -55,20 +55,21 @@
 static off_t alignment, block, sectors, size;
 static unsigned int entry;
 static uint8_t *name;
+static int cmd_add(gpt_t, int, char *[]);
 
-const char addmsg1[] = "add [-a alignment] [-b blocknr] [-i index] [-l label]";
-const char addmsg2[] = "    [-s size] [-t type]";
+static const char *addhelp[] = {
+    "[-a alignment] [-b blocknr] [-i index] [-l label]",
+    "[-s size] [-t type]",
+};
 
-static int
-usage_add(void)
-{
+struct gpt_cmd c_add = {
+       "add",
+       cmd_add,
+       addhelp, __arraycount(addhelp),
+       0,
+};
 
-       fprintf(stderr,
-           "usage: %s %s\n"
-           "       %*s %s\n", getprogname(), addmsg1,
-           (int)strlen(getprogname()), "", addmsg2);
-       return -1;
-}
+#define usage() gpt_usage(NULL, &c_add)
 
 static int
 add(gpt_t gpt)
@@ -145,7 +146,7 @@
        return 0;
 }
 
-int
+static int
 cmd_add(gpt_t gpt, int argc, char *argv[])
 {
        char *p;
@@ -156,47 +157,47 @@
                switch(ch) {
                case 'a':
                        if (alignment > 0)
-                               usage_add();
+                               return usage();
                        if (dehumanize_number(optarg, &human_num) < 0)
-                               usage_add();
+                               return usage();
                        alignment = human_num;
                        if (alignment < 1)
-                               usage_add();
+                               return usage();
                        break;
                case 'b':
                        if (block > 0)
-                               usage_add();
+                               return usage();
                        if (dehumanize_number(optarg, &human_num) < 0)
-                               usage_add();
+                               return usage();
                        block = human_num;
                        if (block < 1)
-                               usage_add();
+                               return usage();
                        break;
                case 'i':
                        if (entry > 0)
-                               usage_add();
+                               usage();
                        entry = strtoul(optarg, &p, 10);
                        if (*p != 0 || entry < 1)
-                               usage_add();
+                               return usage();
                        break;
                case 'l':
                        if (name != NULL)
-                               usage_add();
+                               return usage();
                        name = (uint8_t *)strdup(optarg);
                        break;
                case 's':
                        if (sectors > 0 || size > 0)
-                               usage_add();
+                               return usage();
                        sectors = strtoll(optarg, &p, 10);
                        if (sectors < 1)
-                               usage_add();
+                               return usage();
                        if (*p == '\0')
                                break;
                        if (*p == 's' || *p == 'S') {
                                if (*(p + 1) == '\0')
                                        break;
                                else
-                                       usage_add();
+                                       return usage();
                        }
                        if (*p == 'b' || *p == 'B') {
                                if (*(p + 1) == '\0') {
@@ -204,26 +205,26 @@
                                        sectors = 0;
                                        break;
                                } else
-                                       usage_add();
+                                       return usage();
                        }
                        if (dehumanize_number(optarg, &human_num) < 0)
-                               usage_add();
+                               return usage();
                        size = human_num;
                        sectors = 0;
                        break;
                case 't':
                        if (!gpt_uuid_is_nil(type))
-                               usage_add();
+                               return usage();
                        if (gpt_uuid_parse(optarg, type) != 0)
-                               usage_add();
+                               return usage();
                        break;
                default:
-                       return usage_add();
+                       return usage();
                }
        }
 
        if (argc == optind)
-               return usage_add();
+               return usage();
 
        /* Create NetBSD FFS partitions by default. */
        if (gpt_uuid_is_nil(type)) {
@@ -231,7 +232,7 @@
        }
 
        if (optind != argc)
-               return usage_add();
+               return usage();
 
        if ((sectors = gpt_check(gpt, alignment, size)) == -1)
                return -1;
diff -r 6d15ac679f5a -r 800f78d1b530 sbin/gpt/backup.c
--- a/sbin/gpt/backup.c Tue Dec 01 12:07:41 2015 +0000
+++ b/sbin/gpt/backup.c Tue Dec 01 16:32:19 2015 +0000
@@ -33,7 +33,7 @@
 __FBSDID("$FreeBSD: src/sbin/gpt/show.c,v 1.14 2006/06/22 22:22:32 marcel Exp $");
 #endif
 #ifdef __RCSID
-__RCSID("$NetBSD: backup.c,v 1.10 2015/12/01 09:05:33 christos Exp $");
+__RCSID("$NetBSD: backup.c,v 1.11 2015/12/01 16:32:19 christos Exp $");
 #endif
 
 #include <sys/bootblock.h>
@@ -52,16 +52,20 @@
 #include "gpt_private.h"
 
 
-const char backupmsg[] = "backup";
+static const char *backuphelp[] = {
+    "",
+};
+
+static int cmd_backup(gpt_t, int, char *[]);
 
-static int
-usage_backup(void)
-{
+struct gpt_cmd c_backup = {
+       "backup",
+       cmd_backup,
+       backuphelp, __arraycount(backuphelp),
+       GPT_READONLY,
+};
 
-       fprintf(stderr,
-           "usage: %s %s\n", getprogname(), backupmsg);
-       return -1;
-}
+#define usage() gpt_usage(NULL, &c_backup)
 
 #define PROP_ERR(x)    if (!(x)) {                     \
                gpt_warnx(gpt, "proplib failure");      \
@@ -289,11 +293,11 @@
        return 0;
 }
 
-int
+static int
 cmd_backup(gpt_t gpt, int argc, char *argv[])
 {
        if (argc != optind)
-               usage_backup();
+               return usage();
 
        return backup(gpt);
 }
diff -r 6d15ac679f5a -r 800f78d1b530 sbin/gpt/biosboot.c
--- a/sbin/gpt/biosboot.c       Tue Dec 01 12:07:41 2015 +0000
+++ b/sbin/gpt/biosboot.c       Tue Dec 01 16:32:19 2015 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: biosboot.c,v 1.16 2015/12/01 09:05:33 christos Exp $ */
+/*     $NetBSD: biosboot.c,v 1.17 2015/12/01 16:32:19 christos Exp $ */
 
 /*
  * Copyright (c) 2009 The NetBSD Foundation, Inc.
@@ -37,7 +37,7 @@
 
 #include <sys/cdefs.h>
 #ifdef __RCSID
-__RCSID("$NetBSD: biosboot.c,v 1.16 2015/12/01 09:05:33 christos Exp $");
+__RCSID("$NetBSD: biosboot.c,v 1.17 2015/12/01 16:32:19 christos Exp $");
 #endif
 
 #include <sys/stat.h>
@@ -72,15 +72,22 @@
 static unsigned int entry;
 static uint8_t *label;
 
-const char biosbootmsg[] = "biosboot [-c bootcode] [-i index] "
-       "[-L label]";
+static int cmd_biosboot(gpt_t, int, char *[]);
+
+static const char *biosboothelp[] = {
+    "[-c bootcode] [-i index] [-L label]",
+    "[-a alignment] [-b blocknr] [-i index] [-l label]",
+    "[-s size] [-t type]",
+};
 
-static int
-usage_biosboot(void)
-{
-       fprintf(stderr, "usage: %s %s\n", getprogname(), biosbootmsg);
-       return -1;
-}
+struct gpt_cmd c_biosboot = {
+       "biosboot",
+       cmd_biosboot,
+       biosboothelp, __arraycount(biosboothelp),
+       0,
+};
+
+#define usage() gpt_usage(NULL, &c_biosboot)
 
 static struct mbr*
 read_boot(gpt_t gpt)
@@ -239,7 +246,7 @@
        return 0;
 }
 
-int
+static int
 cmd_biosboot(gpt_t gpt, int argc, char *argv[])
 {
 #ifdef DIOCGWEDGEINFO
@@ -253,29 +260,34 @@
                switch(ch) {
                case 'c':
                        if (bootpath != NULL)
-                               usage_biosboot();
-                       if ((bootpath = strdup(optarg)) == NULL)
-                               err(1, "Malloc failed");
+                               usage();
+                       if ((bootpath = strdup(optarg)) == NULL) {
+                               gpt_warn(gpt, "strdup failed");
+                               return -1;
+                       }
                        break;
                case 'i':
                        if (entry > 0)
-                               usage_biosboot();
+                               usage();
                        entry = strtoul(optarg, &p, 10);
                        if (*p != 0 || entry < 1)
-                               usage_biosboot();
+                               return usage();
                        break;
                case 'L':
                        if (label != NULL)
-                               usage_biosboot();
-                       label = (uint8_t *)strdup(optarg);
+                               return usage();
+                       if ((label = (uint8_t *)strdup(optarg)) == NULL) {
+                               gpt_warn(gpt, "strdup failed");
+                               return -1;
+                       }



Home | Main Index | Thread Index | Old Index