remove duplicated code, and try to cleanup parsing by using the shared code.

cd9660 needs a lot of work.
This commit is contained in:
christos 2013-01-23 21:32:32 +00:00
parent 42788e3ad4
commit 1c35cd3809
7 changed files with 60 additions and 63 deletions

View File

@ -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 @@ cd9660_parse_opts(const char *option, fsinfo_t *fsopts)
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)

View File

@ -83,28 +83,10 @@ chfs_parse_opts(const char *option, fsinfo_t *fsopts)
{ .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

View File

@ -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 @@ int
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 @@ ffs_parse_opts(const char *option, fsinfo_t *fsopts)
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 @@ ffs_parse_opts(const char *option, fsinfo_t *fsopts)
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;
}

View File

@ -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 @@ main(int argc, char *argv[])
/* 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;

View File

@ -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 @@ typedef struct {
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 *);

View File

@ -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 @@ ALLOPTS
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);
}

View File

@ -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 @@ v7fs_parse_opts(const char *option, fsinfo_t *fsopts)
{ .name = NULL }
};
set_option(v7fs_options, option, "1");
set_option_var(v7fs_options, option, "1");
return 1;
}