Source-Changes-HG archive

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

[src/trunk]: src/usr.sbin/makefs remove duplicated code, and try to cleanup p...



details:   https://anonhg.NetBSD.org/src/rev/c3ca0072ea12
branches:  trunk
changeset: 784277:c3ca0072ea12
user:      christos <christos%NetBSD.org@localhost>
date:      Wed Jan 23 21:32:32 2013 +0000

description:
remove duplicated code, and try to cleanup parsing by using the shared code.
cd9660 needs a lot of work.

diffstat:

 usr.sbin/makefs/cd9660.c |   6 +++---
 usr.sbin/makefs/chfs.c   |  20 +-------------------
 usr.sbin/makefs/ffs.c    |  48 ++++++++++++++++++++----------------------------
 usr.sbin/makefs/makefs.c |  30 +++++++++++++++++++++++++++---
 usr.sbin/makefs/makefs.h |   5 +++--
 usr.sbin/makefs/msdos.c  |   8 +++-----
 usr.sbin/makefs/v7fs.c   |   6 +++---
 7 files changed, 60 insertions(+), 63 deletions(-)

diffs (280 lines):

diff -r 91d1b61a0a89 -r c3ca0072ea12 usr.sbin/makefs/cd9660.c
--- a/usr.sbin/makefs/cd9660.c  Wed Jan 23 21:03:25 2013 +0000
+++ b/usr.sbin/makefs/cd9660.c  Wed Jan 23 21:32:32 2013 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: cd9660.c,v 1.36 2013/01/23 20:46:39 christos Exp $     */
+/*     $NetBSD: cd9660.c,v 1.37 2013/01/23 21:32:32 christos Exp $     */
 
 /*
  * Copyright (c) 2005 Daniel Watt, Walter Deignan, Ryan Gabrys, Alan
@@ -103,7 +103,7 @@
 
 #include <sys/cdefs.h>
 #if defined(__RCSID) && !defined(__lint)
-__RCSID("$NetBSD: cd9660.c,v 1.36 2013/01/23 20:46:39 christos Exp $");
+__RCSID("$NetBSD: cd9660.c,v 1.37 2013/01/23 21:32:32 christos Exp $");
 #endif  /* !__lint */
 
 #include <string.h>
@@ -424,7 +424,7 @@
                        warnx("Option `%s' doesn't contain a value", var);
                        rv = 0;
                } else
-                       rv = set_option(cd9660_options, var, val);
+                       rv = set_option_var(cd9660_options, var, val);
        }
 
        if (var)
diff -r 91d1b61a0a89 -r c3ca0072ea12 usr.sbin/makefs/chfs.c
--- a/usr.sbin/makefs/chfs.c    Wed Jan 23 21:03:25 2013 +0000
+++ b/usr.sbin/makefs/chfs.c    Wed Jan 23 21:32:32 2013 +0000
@@ -83,28 +83,10 @@
                { .name = NULL }
        };
 
-       char *var, *val;
-       int retval;
-
        assert(option != NULL);
        assert(fsopts != NULL);
 
-       if ((var = strdup(option)) == NULL) {
-               err(EXIT_FAILURE, "Allocating memory for copy of option string");
-       }
-       retval = 0;
-
-       if ((val = strchr(var, '=')) == NULL) {
-               warnx("Option `%s' doesn't contain a value", var);
-               goto leave_chfs_parse_opts;
-       }
-       *val++ = '\0';
-
-       retval = set_option(chfs_options, var, val);
-       
-leave_chfs_parse_opts:
-       free(var);
-       return retval;
+       return set_option(chfs_options, option);
 }
 
 void
diff -r 91d1b61a0a89 -r c3ca0072ea12 usr.sbin/makefs/ffs.c
--- a/usr.sbin/makefs/ffs.c     Wed Jan 23 21:03:25 2013 +0000
+++ b/usr.sbin/makefs/ffs.c     Wed Jan 23 21:32:32 2013 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: ffs.c,v 1.50 2013/01/23 20:46:39 christos Exp $        */
+/*     $NetBSD: ffs.c,v 1.51 2013/01/23 21:32:32 christos Exp $        */
 
 /*
  * Copyright (c) 2001 Wasabi Systems, Inc.
@@ -71,7 +71,7 @@
 
 #include <sys/cdefs.h>
 #if defined(__RCSID) && !defined(__lint)
-__RCSID("$NetBSD: ffs.c,v 1.50 2013/01/23 20:46:39 christos Exp $");
+__RCSID("$NetBSD: ffs.c,v 1.51 2013/01/23 21:32:32 christos Exp $");
 #endif /* !__lint */
 
 #include <sys/param.h>
@@ -186,6 +186,7 @@
 ffs_parse_opts(const char *option, fsinfo_t *fsopts)
 {
        ffs_opt_t       *ffs_opts = fsopts->fs_specific;
+       char optimization[24];
 
        option_t ffs_options[] = {
            { '\0', "bsize", &ffs_opts->bsize, OPT_INT32,
@@ -208,11 +209,14 @@
              1, INT_MAX, "max # of blocks per group" },
            { '\0', "version", &ffs_opts->version, OPT_INT32,
              1, 2, "UFS version" },
+           { '\0', "optimization", optimization, OPT_STRARRAY,
+             1, sizeof(optimization), "Optimization (time|space)" },
+           { '\0', "label", ffs_opts->label, OPT_STRARRAY,
+             1, sizeof(ffs_opts->label), "UFS label" },
            { .name = NULL }
        };
 
-       char    *var, *val;
-       int     rv;
+       int     rv, i;
 
        assert(option != NULL);
        assert(fsopts != NULL);
@@ -221,36 +225,24 @@
        if (debug & DEBUG_FS_PARSE_OPTS)
                printf("ffs_parse_opts: got `%s'\n", option);
 
-       if ((var = strdup(option)) == NULL)
-               err(1, "Allocating memory for copy of option string");
-       rv = 0;
+       rv = set_option(ffs_options, option);
+       if (rv == 0)
+               return 0;
 
-       if ((val = strchr(var, '=')) == NULL) {
-               warnx("Option `%s' doesn't contain a value", var);
-               goto leave_ffs_parse_opts;
-       }
-       *val++ = '\0';
+       for (i = 0; ffs_options[i].name && (1 << i) != rv; i++)
+               continue;
 
-       if (strcmp(var, "optimization") == 0) {
-               if (strcmp(val, "time") == 0) {
+       if (strcmp(ffs_options[i].name, "optimization") == 0) {
+               if (strcmp(optimization, "time") == 0) {
                        ffs_opts->optimization = FS_OPTTIME;
-               } else if (strcmp(val, "space") == 0) {
+               } else if (strcmp(optimization, "space") == 0) {
                        ffs_opts->optimization = FS_OPTSPACE;
                } else {
-                       warnx("Invalid optimization `%s'", val);
-                       goto leave_ffs_parse_opts;
+                       warnx("Invalid optimization `%s'", optimization);
+                       return 0;
                }
-               rv = 1;
-       } else if (strcmp(var, "label") == 0) {
-               strlcpy(ffs_opts->label, val, sizeof(ffs_opts->label));
-               rv = 1;
-       } else
-               rv = set_option(ffs_options, var, val);
-
- leave_ffs_parse_opts:
-       if (var)
-               free(var);
-       return (rv);
+       }
+       return rv;
 }
 
 
diff -r 91d1b61a0a89 -r c3ca0072ea12 usr.sbin/makefs/makefs.c
--- a/usr.sbin/makefs/makefs.c  Wed Jan 23 21:03:25 2013 +0000
+++ b/usr.sbin/makefs/makefs.c  Wed Jan 23 21:32:32 2013 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: makefs.c,v 1.36 2013/01/23 20:46:39 christos Exp $     */
+/*     $NetBSD: makefs.c,v 1.37 2013/01/23 21:32:32 christos Exp $     */
 
 /*
  * Copyright (c) 2001-2003 Wasabi Systems, Inc.
@@ -41,7 +41,7 @@
 
 #include <sys/cdefs.h>
 #if defined(__RCSID) && !defined(__lint)
-__RCSID("$NetBSD: makefs.c,v 1.36 2013/01/23 20:46:39 christos Exp $");
+__RCSID("$NetBSD: makefs.c,v 1.37 2013/01/23 21:32:32 christos Exp $");
 #endif /* !__lint */
 
 #include <assert.h>
@@ -300,9 +300,33 @@
        /* NOTREACHED */
 }
 
+int
+set_option(const option_t *options, const char *option)
+{
+       char *var, *val;
+       int retval;
+
+       assert(option != NULL);
+
+       if ((var = strdup(option)) == NULL) {
+               err(EXIT_FAILURE, "Allocating memory for copy of option string");
+       }
+       retval = 0;
+       if ((val = strchr(var, '=')) == NULL) {
+               warnx("Option `%s' doesn't contain a value", var);
+               goto out;
+       }
+       *val++ = '\0';
+
+       retval = set_option_var(options, var, val);
+       
+out:
+       free(var);
+       return retval;
+}
 
 int
-set_option(const option_t *options, const char *var, const char *val)
+set_option_var(const option_t *options, const char *var, const char *val)
 {
        char *s;
        size_t i;
diff -r 91d1b61a0a89 -r c3ca0072ea12 usr.sbin/makefs/makefs.h
--- a/usr.sbin/makefs/makefs.h  Wed Jan 23 21:03:25 2013 +0000
+++ b/usr.sbin/makefs/makefs.h  Wed Jan 23 21:32:32 2013 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: makefs.h,v 1.28 2013/01/23 20:46:39 christos Exp $     */
+/*     $NetBSD: makefs.h,v 1.29 2013/01/23 21:32:32 christos Exp $     */
 
 /*
  * Copyright (c) 2001 Wasabi Systems, Inc.
@@ -171,7 +171,8 @@
 void           apply_specfile(const char *, const char *, fsnode *, int);
 void           dump_fsnodes(fsnode *);
 const char *   inode_type(mode_t);
-int            set_option(const option_t *, const char *, const char *);
+int            set_option(const option_t *, const char *);
+int            set_option_var(const option_t *, const char *, const char *);
 fsnode *       walk_dir(const char *, const char *, fsnode *, fsnode *);
 void           free_fsnodes(fsnode *);
 
diff -r 91d1b61a0a89 -r c3ca0072ea12 usr.sbin/makefs/msdos.c
--- a/usr.sbin/makefs/msdos.c   Wed Jan 23 21:03:25 2013 +0000
+++ b/usr.sbin/makefs/msdos.c   Wed Jan 23 21:32:32 2013 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: msdos.c,v 1.1 2013/01/23 20:46:39 christos Exp $       */
+/*     $NetBSD: msdos.c,v 1.2 2013/01/23 21:32:32 christos Exp $       */
 
 /*-
  * Copyright (c) 2013 The NetBSD Foundation, Inc.
@@ -37,7 +37,7 @@
 
 #include <sys/cdefs.h>
 #if defined(__RCSID) && !defined(__lint)
-__RCSID("$NetBSD: msdos.c,v 1.1 2013/01/23 20:46:39 christos Exp $");
+__RCSID("$NetBSD: msdos.c,v 1.2 2013/01/23 21:32:32 christos Exp $");
 #endif /* !__lint */
 
 #include <sys/param.h>
@@ -109,9 +109,7 @@
        if (debug & DEBUG_FS_PARSE_OPTS)
                printf("msdos_parse_opts: got `%s'\n", option);
 
-       set_option(msdos_options, option, "1");
-
-       return 1;
+       return set_option(msdos_options, option);
 }
 
 
diff -r 91d1b61a0a89 -r c3ca0072ea12 usr.sbin/makefs/v7fs.c
--- a/usr.sbin/makefs/v7fs.c    Wed Jan 23 21:03:25 2013 +0000
+++ b/usr.sbin/makefs/v7fs.c    Wed Jan 23 21:32:32 2013 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: v7fs.c,v 1.4 2013/01/23 20:46:39 christos Exp $        */
+/*     $NetBSD: v7fs.c,v 1.5 2013/01/23 21:32:32 christos Exp $        */
 
 /*-
  * Copyright (c) 2011 The NetBSD Foundation, Inc.
@@ -35,7 +35,7 @@
 
 #include <sys/cdefs.h>
 #if defined(__RCSID) && !defined(__lint)
-__RCSID("$NetBSD: v7fs.c,v 1.4 2013/01/23 20:46:39 christos Exp $");
+__RCSID("$NetBSD: v7fs.c,v 1.5 2013/01/23 21:32:32 christos Exp $");
 #endif /* !__lint */
 
 #include <stdio.h>
@@ -82,7 +82,7 @@
                { .name = NULL }
        };
 
-       set_option(v7fs_options, option, "1");
+       set_option_var(v7fs_options, option, "1");
 
        return 1;
 }



Home | Main Index | Thread Index | Old Index