From 2602861f231b85634d99e9c7409ed09a9c7eed54 Mon Sep 17 00:00:00 2001 From: David van Moolenbroek Date: Mon, 7 Nov 2011 16:16:08 +0100 Subject: [PATCH] Move optset.c into libsys; remove redundant copies --- common/include/Makefile.inc | 2 +- .../filter => common/include/minix}/optset.h | 10 +- drivers/filter/Makefile | 2 +- drivers/filter/inc.h | 1 + drivers/filter/main.c | 1 - drivers/filter/optset.c | 128 ------------------ lib/libsys/Makefile | 1 + {test/blocktest => lib/libsys}/optset.c | 3 +- servers/ext2/Makefile | 2 +- servers/ext2/main.c | 3 +- servers/ext2/optset.c | 128 ------------------ servers/ext2/optset.h | 30 ---- servers/hgfs/Makefile | 4 +- servers/hgfs/inc.h | 1 + servers/hgfs/main.c | 2 - servers/hgfs/optset.c | 128 ------------------ servers/hgfs/optset.h | 30 ---- test/blocktest/Makefile | 2 +- test/blocktest/blocktest.c | 3 +- test/blocktest/optset.h | 30 ---- 20 files changed, 17 insertions(+), 494 deletions(-) rename {drivers/filter => common/include/minix}/optset.h (89%) delete mode 100644 drivers/filter/optset.c rename {test/blocktest => lib/libsys}/optset.c (99%) delete mode 100644 servers/ext2/optset.c delete mode 100644 servers/ext2/optset.h delete mode 100644 servers/hgfs/optset.c delete mode 100644 servers/hgfs/optset.h delete mode 100644 test/blocktest/optset.h diff --git a/common/include/Makefile.inc b/common/include/Makefile.inc index f4f274a83..24a39355d 100644 --- a/common/include/Makefile.inc +++ b/common/include/Makefile.inc @@ -12,7 +12,7 @@ INCS+= minix/acpi.h minix/ansi.h minix/audio_fw.h minix/bitmap.h \ minix/endpoint.h minix/fslib.h minix/gcov.h minix/hash.h \ minix/ioctl.h minix/input.h minix/ipc.h minix/ipcconst.h \ minix/keymap.h minix/limits.h minix/mthread.h minix/minlib.h \ - minix/netdriver.h minix/partition.h minix/portio.h \ + minix/netdriver.h minix/optset.h minix/partition.h minix/portio.h \ minix/priv.h minix/procfs.h minix/profile.h minix/queryparam.h \ minix/rs.h minix/safecopies.h minix/sched.h minix/sef.h \ minix/sound.h minix/spin.h minix/sys_config.h minix/sysinfo.h \ diff --git a/drivers/filter/optset.h b/common/include/minix/optset.h similarity index 89% rename from drivers/filter/optset.h rename to common/include/minix/optset.h index 411d80452..8990ce6bd 100644 --- a/drivers/filter/optset.h +++ b/common/include/minix/optset.h @@ -1,11 +1,11 @@ -#ifndef _OPTSET_H -#define _OPTSET_H +#ifndef _MINIX_OPTSET_H +#define _MINIX_OPTSET_H typedef enum { OPT_BOOL, OPT_STRING, OPT_INT -} opt_type; +} optset_type; /* An entry for the parser of an options set. The 'os_name' field must point * to a string, which is treated case-insensitively; the last entry of a table @@ -20,11 +20,11 @@ typedef enum { */ struct optset { char *os_name; - opt_type os_type; + optset_type os_type; void *os_ptr; int os_val; }; _PROTOTYPE( void optset_parse, (struct optset *table, char *string) ); -#endif /* _OPTSET_H */ +#endif /* _MINIX_OPTSET_H */ diff --git a/drivers/filter/Makefile b/drivers/filter/Makefile index 119372f57..7d91a0f74 100644 --- a/drivers/filter/Makefile +++ b/drivers/filter/Makefile @@ -1,6 +1,6 @@ # Makefile for filter driver PROG= filter -SRCS= main.c sum.c driver.c util.c optset.c crc.c md5.c +SRCS= main.c sum.c driver.c util.c crc.c md5.c DPADD+= ${LIBDRIVER} ${LIBSYS} LDADD+= -ldriver -lsys diff --git a/drivers/filter/inc.h b/drivers/filter/inc.h index 693b8d5ba..ccd5f9e34 100644 --- a/drivers/filter/inc.h +++ b/drivers/filter/inc.h @@ -13,6 +13,7 @@ #include #include #include +#include #include #include #include diff --git a/drivers/filter/main.c b/drivers/filter/main.c index 6a77bbddc..2b36e7c6a 100644 --- a/drivers/filter/main.c +++ b/drivers/filter/main.c @@ -8,7 +8,6 @@ */ #include "inc.h" -#include "optset.h" #define _POSIX_SOURCE 1 #include diff --git a/drivers/filter/optset.c b/drivers/filter/optset.c deleted file mode 100644 index a338a4717..000000000 --- a/drivers/filter/optset.c +++ /dev/null @@ -1,128 +0,0 @@ -/* This file provides functionality to parse strings of comma-separated - * options, each being either a single key name or a key=value pair, where the - * value may be enclosed in quotes. A table of optset entries is provided to - * determine which options are recognized, how to parse their values, and where - * to store those. Unrecognized options are silently ignored; improperly - * formatted options are silently set to reasonably acceptable values. - * - * The entry points into this file are: - * optset_parse parse the given options string using the given table - * - * Created: - * May 2009 (D.C. van Moolenbroek) - */ - -#define _MINIX 1 -#include -#include -#include -#include - -#include "optset.h" - -FORWARD _PROTOTYPE( void optset_parse_entry, (struct optset *entry, - char *ptr, int len) ); - -/*===========================================================================* - * optset_parse_entry * - *===========================================================================*/ -PRIVATE void optset_parse_entry(entry, ptr, len) -struct optset *entry; -char *ptr; -int len; -{ -/* Parse and store the value of a single option. - */ - char *dst; - int val; - - switch (entry->os_type) { - case OPT_BOOL: - *((int *) entry->os_ptr) = entry->os_val; - - break; - - case OPT_STRING: - if (len >= entry->os_val) - len = entry->os_val - 1; - - dst = (char *) entry->os_ptr; - - if (len > 0) - memcpy(dst, ptr, len); - dst[len] = 0; - - break; - - case OPT_INT: - if (len > 0) - val = strtol(ptr, NULL, entry->os_val); - else - val = 0; - - *((int *) entry->os_ptr) = val; - - break; - } -} - -/*===========================================================================* - * optset_parse * - *===========================================================================*/ -PUBLIC void optset_parse(table, string) -struct optset *table; -char *string; -{ -/* Parse a string of options, using the provided table of optset entries. - */ - char *p, *kptr, *vptr; - int i, klen, vlen; - - for (p = string; *p; ) { - /* Get the key name for the field. */ - for (kptr = p, klen = 0; *p && *p != '=' && *p != ','; p++, klen++); - - if (*p == '=') { - /* The field has an associated value. */ - vptr = ++p; - - /* If the first character after the '=' is a quote character, - * find a matching quote character followed by either a comma - * or the terminating null character, and use the string in - * between. Otherwise, use the string up to the next comma or - * the terminating null character. - */ - if (*p == '\'' || *p == '"') { - p++; - - for (vlen = 0; *p && (*p != *vptr || - (p[1] && p[1] != ',')); p++, vlen++); - - if (*p) p++; - vptr++; - } - else - for (vlen = 0; *p && *p != ','; p++, vlen++); - } - else { - vptr = NULL; - vlen = 0; - } - - if (*p == ',') p++; - - /* Find a matching entry for this key in the given table. If found, - * call optset_parse_entry() on it. Silently ignore the option - * otherwise. - */ - for (i = 0; table[i].os_name != NULL; i++) { - if (strlen(table[i].os_name) == klen && - !strncasecmp(table[i].os_name, kptr, klen)) { - - optset_parse_entry(&table[i], vptr, vlen); - - break; - } - } - } -} diff --git a/lib/libsys/Makefile b/lib/libsys/Makefile index bbb0bc325..183359f7e 100644 --- a/lib/libsys/Makefile +++ b/lib/libsys/Makefile @@ -23,6 +23,7 @@ SRCS= \ kprintf.c \ kputc.c \ kputs.c \ + optset.c \ panic.c \ pci_attr_r16.c \ pci_attr_r32.c \ diff --git a/test/blocktest/optset.c b/lib/libsys/optset.c similarity index 99% rename from test/blocktest/optset.c rename to lib/libsys/optset.c index c53dd61ff..962814dfd 100644 --- a/test/blocktest/optset.c +++ b/lib/libsys/optset.c @@ -17,8 +17,7 @@ #include #include #include - -#include "optset.h" +#include FORWARD _PROTOTYPE( void optset_parse_entry, (struct optset *entry, char *ptr, int len) ); diff --git a/servers/ext2/Makefile b/servers/ext2/Makefile index 35bd6892e..599355c96 100644 --- a/servers/ext2/Makefile +++ b/servers/ext2/Makefile @@ -4,7 +4,7 @@ SRCS= balloc.c cache.c device.c link.c \ mount.c misc.c open.c protect.c read.c \ stadir.c table.c time.c utility.c \ write.c ialloc.c inode.c main.c path.c \ - super.c optset.c + super.c DPADD+= ${LIBSYS} LDADD+= -lminixfs -lsys diff --git a/servers/ext2/main.c b/servers/ext2/main.c index 07caa5a3f..0561877ec 100644 --- a/servers/ext2/main.c +++ b/servers/ext2/main.c @@ -8,11 +8,10 @@ #include #include #include +#include #include "buf.h" #include "inode.h" #include "drivers.h" -#include "optset.h" - /* Declare some local functions. */ FORWARD _PROTOTYPE(void get_work, (message *m_in) ); diff --git a/servers/ext2/optset.c b/servers/ext2/optset.c deleted file mode 100644 index a338a4717..000000000 --- a/servers/ext2/optset.c +++ /dev/null @@ -1,128 +0,0 @@ -/* This file provides functionality to parse strings of comma-separated - * options, each being either a single key name or a key=value pair, where the - * value may be enclosed in quotes. A table of optset entries is provided to - * determine which options are recognized, how to parse their values, and where - * to store those. Unrecognized options are silently ignored; improperly - * formatted options are silently set to reasonably acceptable values. - * - * The entry points into this file are: - * optset_parse parse the given options string using the given table - * - * Created: - * May 2009 (D.C. van Moolenbroek) - */ - -#define _MINIX 1 -#include -#include -#include -#include - -#include "optset.h" - -FORWARD _PROTOTYPE( void optset_parse_entry, (struct optset *entry, - char *ptr, int len) ); - -/*===========================================================================* - * optset_parse_entry * - *===========================================================================*/ -PRIVATE void optset_parse_entry(entry, ptr, len) -struct optset *entry; -char *ptr; -int len; -{ -/* Parse and store the value of a single option. - */ - char *dst; - int val; - - switch (entry->os_type) { - case OPT_BOOL: - *((int *) entry->os_ptr) = entry->os_val; - - break; - - case OPT_STRING: - if (len >= entry->os_val) - len = entry->os_val - 1; - - dst = (char *) entry->os_ptr; - - if (len > 0) - memcpy(dst, ptr, len); - dst[len] = 0; - - break; - - case OPT_INT: - if (len > 0) - val = strtol(ptr, NULL, entry->os_val); - else - val = 0; - - *((int *) entry->os_ptr) = val; - - break; - } -} - -/*===========================================================================* - * optset_parse * - *===========================================================================*/ -PUBLIC void optset_parse(table, string) -struct optset *table; -char *string; -{ -/* Parse a string of options, using the provided table of optset entries. - */ - char *p, *kptr, *vptr; - int i, klen, vlen; - - for (p = string; *p; ) { - /* Get the key name for the field. */ - for (kptr = p, klen = 0; *p && *p != '=' && *p != ','; p++, klen++); - - if (*p == '=') { - /* The field has an associated value. */ - vptr = ++p; - - /* If the first character after the '=' is a quote character, - * find a matching quote character followed by either a comma - * or the terminating null character, and use the string in - * between. Otherwise, use the string up to the next comma or - * the terminating null character. - */ - if (*p == '\'' || *p == '"') { - p++; - - for (vlen = 0; *p && (*p != *vptr || - (p[1] && p[1] != ',')); p++, vlen++); - - if (*p) p++; - vptr++; - } - else - for (vlen = 0; *p && *p != ','; p++, vlen++); - } - else { - vptr = NULL; - vlen = 0; - } - - if (*p == ',') p++; - - /* Find a matching entry for this key in the given table. If found, - * call optset_parse_entry() on it. Silently ignore the option - * otherwise. - */ - for (i = 0; table[i].os_name != NULL; i++) { - if (strlen(table[i].os_name) == klen && - !strncasecmp(table[i].os_name, kptr, klen)) { - - optset_parse_entry(&table[i], vptr, vlen); - - break; - } - } - } -} diff --git a/servers/ext2/optset.h b/servers/ext2/optset.h deleted file mode 100644 index 87ea4ce9f..000000000 --- a/servers/ext2/optset.h +++ /dev/null @@ -1,30 +0,0 @@ -#ifndef _OPTSET_H -#define _OPTSET_H - -enum { - OPT_BOOL, - OPT_STRING, - OPT_INT -}; - -/* An entry for the parser of an options set. The 'os_name' field must point - * to a string, which is treated case-insensitively; the last entry of a table - * must have NULL name. The 'os_type' field must be set to one of the OPT_ - * values defined above. The 'os_ptr' field must point to the field that is to - * receive the value of a recognized option. For OPT_STRING, it must point to a - * string of a size set in 'os_val'; the resulting string may be truncated, but - * will always be null-terminated. For OPT_BOOL, it must point to an int which - * will be set to the value in 'os_val' if the option is present. For OPT_INT, - * it must point to an int which will be set to the provided option value; - * 'os_val' is then a base passed to strtol(). - */ -struct optset { - char *os_name; - int os_type; - void *os_ptr; - int os_val; -}; - -_PROTOTYPE( void optset_parse, (struct optset *table, char *string) ); - -#endif /* _OPTSET_H */ diff --git a/servers/hgfs/Makefile b/servers/hgfs/Makefile index eadf787c9..cb8d9636c 100644 --- a/servers/hgfs/Makefile +++ b/servers/hgfs/Makefile @@ -1,8 +1,8 @@ # Makefile for VMware Host/Guest File System (HGFS) server PROG= hgfs SRCS= dentry.c handle.c inode.c link.c lookup.c main.c \ - misc.c mount.c name.c optset.c path.c read.c \ - stat.c table.c util.c verify.c write.c + misc.c mount.c name.c path.c read.c stat.c table.c \ + util.c verify.c write.c DPADD+= ${LIBHGFS} ${LIBSYS} LDADD+= -lhgfs -lsys diff --git a/servers/hgfs/inc.h b/servers/hgfs/inc.h index 7e2c6ec31..b83f73081 100644 --- a/servers/hgfs/inc.h +++ b/servers/hgfs/inc.h @@ -13,6 +13,7 @@ #include #include #include +#include #include #if DEBUG diff --git a/servers/hgfs/main.c b/servers/hgfs/main.c index a68496202..61ac1c64c 100644 --- a/servers/hgfs/main.c +++ b/servers/hgfs/main.c @@ -9,8 +9,6 @@ #include "inc.h" -#include "optset.h" - #include #include #include diff --git a/servers/hgfs/optset.c b/servers/hgfs/optset.c deleted file mode 100644 index a338a4717..000000000 --- a/servers/hgfs/optset.c +++ /dev/null @@ -1,128 +0,0 @@ -/* This file provides functionality to parse strings of comma-separated - * options, each being either a single key name or a key=value pair, where the - * value may be enclosed in quotes. A table of optset entries is provided to - * determine which options are recognized, how to parse their values, and where - * to store those. Unrecognized options are silently ignored; improperly - * formatted options are silently set to reasonably acceptable values. - * - * The entry points into this file are: - * optset_parse parse the given options string using the given table - * - * Created: - * May 2009 (D.C. van Moolenbroek) - */ - -#define _MINIX 1 -#include -#include -#include -#include - -#include "optset.h" - -FORWARD _PROTOTYPE( void optset_parse_entry, (struct optset *entry, - char *ptr, int len) ); - -/*===========================================================================* - * optset_parse_entry * - *===========================================================================*/ -PRIVATE void optset_parse_entry(entry, ptr, len) -struct optset *entry; -char *ptr; -int len; -{ -/* Parse and store the value of a single option. - */ - char *dst; - int val; - - switch (entry->os_type) { - case OPT_BOOL: - *((int *) entry->os_ptr) = entry->os_val; - - break; - - case OPT_STRING: - if (len >= entry->os_val) - len = entry->os_val - 1; - - dst = (char *) entry->os_ptr; - - if (len > 0) - memcpy(dst, ptr, len); - dst[len] = 0; - - break; - - case OPT_INT: - if (len > 0) - val = strtol(ptr, NULL, entry->os_val); - else - val = 0; - - *((int *) entry->os_ptr) = val; - - break; - } -} - -/*===========================================================================* - * optset_parse * - *===========================================================================*/ -PUBLIC void optset_parse(table, string) -struct optset *table; -char *string; -{ -/* Parse a string of options, using the provided table of optset entries. - */ - char *p, *kptr, *vptr; - int i, klen, vlen; - - for (p = string; *p; ) { - /* Get the key name for the field. */ - for (kptr = p, klen = 0; *p && *p != '=' && *p != ','; p++, klen++); - - if (*p == '=') { - /* The field has an associated value. */ - vptr = ++p; - - /* If the first character after the '=' is a quote character, - * find a matching quote character followed by either a comma - * or the terminating null character, and use the string in - * between. Otherwise, use the string up to the next comma or - * the terminating null character. - */ - if (*p == '\'' || *p == '"') { - p++; - - for (vlen = 0; *p && (*p != *vptr || - (p[1] && p[1] != ',')); p++, vlen++); - - if (*p) p++; - vptr++; - } - else - for (vlen = 0; *p && *p != ','; p++, vlen++); - } - else { - vptr = NULL; - vlen = 0; - } - - if (*p == ',') p++; - - /* Find a matching entry for this key in the given table. If found, - * call optset_parse_entry() on it. Silently ignore the option - * otherwise. - */ - for (i = 0; table[i].os_name != NULL; i++) { - if (strlen(table[i].os_name) == klen && - !strncasecmp(table[i].os_name, kptr, klen)) { - - optset_parse_entry(&table[i], vptr, vlen); - - break; - } - } - } -} diff --git a/servers/hgfs/optset.h b/servers/hgfs/optset.h deleted file mode 100644 index 87ea4ce9f..000000000 --- a/servers/hgfs/optset.h +++ /dev/null @@ -1,30 +0,0 @@ -#ifndef _OPTSET_H -#define _OPTSET_H - -enum { - OPT_BOOL, - OPT_STRING, - OPT_INT -}; - -/* An entry for the parser of an options set. The 'os_name' field must point - * to a string, which is treated case-insensitively; the last entry of a table - * must have NULL name. The 'os_type' field must be set to one of the OPT_ - * values defined above. The 'os_ptr' field must point to the field that is to - * receive the value of a recognized option. For OPT_STRING, it must point to a - * string of a size set in 'os_val'; the resulting string may be truncated, but - * will always be null-terminated. For OPT_BOOL, it must point to an int which - * will be set to the value in 'os_val' if the option is present. For OPT_INT, - * it must point to an int which will be set to the provided option value; - * 'os_val' is then a base passed to strtol(). - */ -struct optset { - char *os_name; - int os_type; - void *os_ptr; - int os_val; -}; - -_PROTOTYPE( void optset_parse, (struct optset *table, char *string) ); - -#endif /* _OPTSET_H */ diff --git a/test/blocktest/Makefile b/test/blocktest/Makefile index bc17de6be..80f415bb7 100644 --- a/test/blocktest/Makefile +++ b/test/blocktest/Makefile @@ -1,6 +1,6 @@ # Makefile for the Block Device Driver Test driver (blocktest) PROG= blocktest -SRCS= blocktest.c optset.c +SRCS= blocktest.c DPADD+= ${LIBSYS} LDADD+= -lsys diff --git a/test/blocktest/blocktest.c b/test/blocktest/blocktest.c index 05614976a..698e59bfe 100644 --- a/test/blocktest/blocktest.c +++ b/test/blocktest/blocktest.c @@ -6,6 +6,7 @@ #include #include #include +#include #include #include @@ -13,8 +14,6 @@ #include "servers/avfs/const.h" #include "servers/avfs/dmap.h" -#include "optset.h" - enum { RESULT_OK, /* exactly as expected */ RESULT_COMMFAIL, /* communication failed */ diff --git a/test/blocktest/optset.h b/test/blocktest/optset.h deleted file mode 100644 index 411d80452..000000000 --- a/test/blocktest/optset.h +++ /dev/null @@ -1,30 +0,0 @@ -#ifndef _OPTSET_H -#define _OPTSET_H - -typedef enum { - OPT_BOOL, - OPT_STRING, - OPT_INT -} opt_type; - -/* An entry for the parser of an options set. The 'os_name' field must point - * to a string, which is treated case-insensitively; the last entry of a table - * must have NULL name. The 'os_type' field must be set to one of the OPT_ - * values defined above. The 'os_ptr' field must point to the field that is to - * receive the value of a recognized option. For OPT_STRING, it must point to a - * string of a size set in 'os_val'; the resulting string may be truncated, but - * will always be null-terminated. For OPT_BOOL, it must point to an int which - * will be set to the value in 'os_val' if the option is present. For OPT_INT, - * it must point to an int which will be set to the provided option value; - * 'os_val' is then a base passed to strtol(). - */ -struct optset { - char *os_name; - opt_type os_type; - void *os_ptr; - int os_val; -}; - -_PROTOTYPE( void optset_parse, (struct optset *table, char *string) ); - -#endif /* _OPTSET_H */