From a86e328ab75068b1c27e17e3fb33a9b5845f63aa Mon Sep 17 00:00:00 2001 From: Robert Spalek Date: Fri, 28 Apr 2006 01:31:57 +0200 Subject: [PATCH] split conf2.h into conf.h and getopt.h --- lib/Makefile | 4 +- lib/{conf2-test.c => conf-test.c} | 6 +-- lib/{conf2.h => conf.h} | 61 +------------------------- lib/conf2.c | 8 ++-- lib/db-test.c | 2 +- lib/db-tool.c | 1 - lib/fb-mmap.c | 2 +- lib/fb-temp.c | 2 +- lib/getopt.h | 73 +++++++++++++++++++++++++++++++ lib/ipaccess.c | 5 ++- lib/lizard-test.c | 5 +-- lib/redblack-test.c | 5 +-- lib/shell/config.c | 5 ++- lib/sort-test.c | 5 +-- lib/sorter.c | 2 +- lib/url.c | 2 +- 16 files changed, 101 insertions(+), 87 deletions(-) rename lib/{conf2-test.c => conf-test.c} (97%) rename lib/{conf2.h => conf.h} (76%) create mode 100644 lib/getopt.h diff --git a/lib/Makefile b/lib/Makefile index 846d320b..f163c34d 100644 --- a/lib/Makefile +++ b/lib/Makefile @@ -39,7 +39,7 @@ LIBUCW_INCLUDES= \ hashfunc.h hashtable.h \ heap.h binheap.h binheap-node.h \ redblack.h \ - conf2.h ipaccess.h \ + conf.h getopt.h ipaccess.h \ profile.h \ fastbuf.h lfs.h ff-utf8.h \ chartype.h unicode.h stkstring.h \ @@ -67,7 +67,7 @@ $(o)/lib/lizard.o: CFLAGS += $(COPT2) -funroll-loops $(o)/lib/db-test: $(o)/lib/db-test.o $(LIBUCW) $(o)/lib/db-tool: $(o)/lib/db-tool.o $(LIBUCW) -$(o)/lib/conf2-test: $(o)/lib/conf2-test.o $(LIBUCW) +$(o)/lib/conf-test: $(o)/lib/conf-test.o $(LIBUCW) $(o)/lib/sort-test: $(o)/lib/sort-test.o $(LIBUCW) $(o)/lib/lfs-test: $(o)/lib/lfs-test.o $(LIBUCW) $(o)/lib/hash-test: $(o)/lib/hash-test.o $(LIBUCW) diff --git a/lib/conf2-test.c b/lib/conf-test.c similarity index 97% rename from lib/conf2-test.c rename to lib/conf-test.c index daf24e7b..458689de 100644 --- a/lib/conf2-test.c +++ b/lib/conf-test.c @@ -5,14 +5,14 @@ */ #include "lib/lib.h" -#include "lib/conf2.h" +#include "lib/conf.h" +#include "lib/getopt.h" #include "lib/clists.h" #include "lib/fastbuf.h" #include #include #include -#include static int verbose; @@ -190,7 +190,7 @@ main(int argc, char *argv[]) cf_def_file = "lib/conf2.t"; int opt; - while ((opt = cf_get_opt(argc, argv, short_opts, long_opts, NULL)) >= 0) + while ((opt = cf_getopt(argc, argv, short_opts, long_opts, NULL)) >= 0) switch (opt) { case 'v': verbose++; break; default: usage("unknown option %c\n", opt); diff --git a/lib/conf2.h b/lib/conf.h similarity index 76% rename from lib/conf2.h rename to lib/conf.h index 399ad005..033b7590 100644 --- a/lib/conf2.h +++ b/lib/conf.h @@ -8,8 +8,8 @@ * of the GNU Lesser General Public License. */ -#ifndef _UCW_CONF2_H -#define _UCW_CONF2_H +#ifndef _UCW_CONF_H +#define _UCW_CONF_H enum cf_class { CC_END, // end of list @@ -157,68 +157,11 @@ void cf_journal_rollback_transaction(uns new_pool, struct cf_journal_item *oldj) void cf_declare_section(byte *name, struct cf_section *sec, uns allow_unknown); void cf_init_section(byte *name, struct cf_section *sec, void *ptr, uns do_bzero); -/* Safe reloading and loading of configuration files */ -extern byte *cf_def_file; -int cf_reload(byte *file); -int cf_load(byte *file); -int cf_set(byte *string); - /* Parsers for basic types */ byte *cf_parse_int(byte *str, int *ptr); byte *cf_parse_u64(byte *str, u64 *ptr); byte *cf_parse_double(byte *str, double *ptr); byte *cf_parse_ip(byte *p, u32 *varp); -/* Direct access to configuration items */ - -#define CF_OPERATIONS T(CLOSE) T(SET) T(CLEAR) T(APPEND) T(PREPEND) \ - T(REMOVE) T(EDIT) T(AFTER) T(BEFORE) T(COPY) - /* Closing brace finishes previous block. - * Basic attributes (static, dynamic, parsed) can be used with SET. - * Dynamic arrays can be used with SET, APPEND, PREPEND. - * Sections can be used with SET. - * Lists can be used with everything. */ -#define T(x) OP_##x, -enum cf_operation { CF_OPERATIONS }; -#undef T - -byte *cf_find_item(byte *name, struct cf_item *item); -byte *cf_write_item(struct cf_item *item, enum cf_operation op, int number, byte **pars); -void cf_dump_sections(struct fastbuf *fb); - -/* - * When using cf_get_opt(), you must prefix your own short/long options by the - * CF_(SHORT|LONG)_OPTS. - * - * cf_def_file contains the name of a configuration file that will be - * automatically loaded before the first --set option is executed. If no --set - * option occurs, it will be loaded after getopt() returns -1 (i.e. at the end - * of the configuration options). cf_def_file will be ignored if another - * configuration file has already been loaded using the --config option. The - * initial value of cf_def_file is DEFAULT_CONFIG from config.h, but you can - * override it manually before calling cf_get_opt(). - */ - -#define CF_SHORT_OPTS "C:S:" -#define CF_LONG_OPTS {"config", 1, 0, 'C'}, {"set", 1, 0, 'S'}, CF_LONG_OPTS_DEBUG -#define CF_NO_LONG_OPTS (const struct option []) { CF_LONG_OPTS { NULL, 0, 0, 0 } } -#ifndef CF_USAGE_TAB -#define CF_USAGE_TAB "" -#endif -#define CF_USAGE \ -"-C, --config filename\t" CF_USAGE_TAB "Override the default configuration file\n\ --S, --set sec.item=val\t" CF_USAGE_TAB "Manual setting of a configuration item\n" CF_USAGE_DEBUG - -#ifdef CONFIG_DEBUG -#define CF_LONG_OPTS_DEBUG { "dumpconfig", 0, 0, 0x64436667 } , -#define CF_USAGE_DEBUG " --dumpconfig\t" CF_USAGE_TAB "Dump program configuration\n" -#else -#define CF_LONG_OPTS_DEBUG -#define CF_USAGE_DEBUG #endif -#include -struct option; -int cf_get_opt(int argc, char * const argv[], const char *short_opts, const struct option *long_opts, int *long_index); - -#endif diff --git a/lib/conf2.c b/lib/conf2.c index dad7399e..55118203 100644 --- a/lib/conf2.c +++ b/lib/conf2.c @@ -9,7 +9,8 @@ */ #include "lib/lib.h" -#include "lib/conf2.h" +#include "lib/conf.h" +#include "lib/getopt.h" #include "lib/mempool.h" #include "lib/clists.h" #include "lib/fastbuf.h" @@ -23,7 +24,6 @@ #include #include #include -#include #define TRY(f) do { byte *_msg = f; if (_msg) return _msg; } while (0) @@ -1085,7 +1085,7 @@ init_stack(void) }; } -static uns postpone_commit; // only for cf_get_opt() +static uns postpone_commit; // only for cf_getopt() static int done_stack(void) @@ -1403,7 +1403,7 @@ load_default(void) } int -cf_get_opt(int argc, char * const argv[], const char *short_opts, const struct option *long_opts, int *long_index) +cf_getopt(int argc, char * const argv[], const char *short_opts, const struct option *long_opts, int *long_index) { static int other_options = 0; while (1) { diff --git a/lib/db-test.c b/lib/db-test.c index 23d3f21c..d0f3a5eb 100644 --- a/lib/db-test.c +++ b/lib/db-test.c @@ -15,7 +15,7 @@ #define NAME "GDBM" #endif -#include +#include #include #include #include diff --git a/lib/db-tool.c b/lib/db-tool.c index 4453ab8d..8dca5204 100644 --- a/lib/db-tool.c +++ b/lib/db-tool.c @@ -18,7 +18,6 @@ #include #include #include -#include static int verbose=0; static int cache=1024; diff --git a/lib/fb-mmap.c b/lib/fb-mmap.c index ceb44c2a..d7eaa3d0 100644 --- a/lib/fb-mmap.c +++ b/lib/fb-mmap.c @@ -10,7 +10,7 @@ #include "lib/lib.h" #include "lib/fastbuf.h" #include "lib/lfs.h" -#include "lib/conf2.h" +#include "lib/conf.h" #include #include diff --git a/lib/fb-temp.c b/lib/fb-temp.c index 62c6e382..b8189ab9 100644 --- a/lib/fb-temp.c +++ b/lib/fb-temp.c @@ -8,7 +8,7 @@ */ #include "lib/lib.h" -#include "lib/conf2.h" +#include "lib/conf.h" #include "lib/fastbuf.h" #include diff --git a/lib/getopt.h b/lib/getopt.h new file mode 100644 index 00000000..ca3eaf00 --- /dev/null +++ b/lib/getopt.h @@ -0,0 +1,73 @@ +/* + * UCW Library -- Reading of configuration files + * + * (c) 2001--2006 Robert Spalek + * (c) 2003--2006 Martin Mares + * + * This software may be freely distributed and used according to the terms + * of the GNU Lesser General Public License. + */ + +#ifndef _UCW_GETOPT_H +#define _UCW_GETOPT_H + +/* Safe reloading and loading of configuration files */ +extern byte *cf_def_file; +int cf_reload(byte *file); +int cf_load(byte *file); +int cf_set(byte *string); + +/* Direct access to configuration items */ + +#define CF_OPERATIONS T(CLOSE) T(SET) T(CLEAR) T(APPEND) T(PREPEND) \ + T(REMOVE) T(EDIT) T(AFTER) T(BEFORE) T(COPY) + /* Closing brace finishes previous block. + * Basic attributes (static, dynamic, parsed) can be used with SET. + * Dynamic arrays can be used with SET, APPEND, PREPEND. + * Sections can be used with SET. + * Lists can be used with everything. */ +#define T(x) OP_##x, +enum cf_operation { CF_OPERATIONS }; +#undef T + +struct cf_item; +struct fastbuf; +byte *cf_find_item(byte *name, struct cf_item *item); +byte *cf_write_item(struct cf_item *item, enum cf_operation op, int number, byte **pars); +void cf_dump_sections(struct fastbuf *fb); + +/* + * When using cf_get_opt(), you must prefix your own short/long options by the + * CF_(SHORT|LONG)_OPTS. + * + * cf_def_file contains the name of a configuration file that will be + * automatically loaded before the first --set option is executed. If no --set + * option occurs, it will be loaded after getopt() returns -1 (i.e. at the end + * of the configuration options). cf_def_file will be ignored if another + * configuration file has already been loaded using the --config option. The + * initial value of cf_def_file is DEFAULT_CONFIG from config.h, but you can + * override it manually before calling cf_get_opt(). + */ + +#define CF_SHORT_OPTS "C:S:" +#define CF_LONG_OPTS {"config", 1, 0, 'C'}, {"set", 1, 0, 'S'}, CF_LONG_OPTS_DEBUG +#define CF_NO_LONG_OPTS (const struct option []) { CF_LONG_OPTS { NULL, 0, 0, 0 } } +#ifndef CF_USAGE_TAB +#define CF_USAGE_TAB "" +#endif +#define CF_USAGE \ +"-C, --config filename\t" CF_USAGE_TAB "Override the default configuration file\n\ +-S, --set sec.item=val\t" CF_USAGE_TAB "Manual setting of a configuration item\n" CF_USAGE_DEBUG + +#ifdef CONFIG_DEBUG +#define CF_LONG_OPTS_DEBUG { "dumpconfig", 0, 0, 0x64436667 } , +#define CF_USAGE_DEBUG " --dumpconfig\t" CF_USAGE_TAB "Dump program configuration\n" +#else +#define CF_LONG_OPTS_DEBUG +#define CF_USAGE_DEBUG +#endif + +#include +int cf_getopt(int argc, char * const argv[], const char *short_opts, const struct option *long_opts, int *long_index); + +#endif diff --git a/lib/ipaccess.c b/lib/ipaccess.c index 81b35ed0..79508ab0 100644 --- a/lib/ipaccess.c +++ b/lib/ipaccess.c @@ -9,7 +9,8 @@ #include "lib/lib.h" #include "lib/clists.h" -#include "lib/conf2.h" +#include "lib/conf.h" +#include "lib/getopt.h" #include "lib/fastbuf.h" #include "lib/ipaccess.h" @@ -103,7 +104,7 @@ static struct cf_section test_cf = { int main(int argc, char **argv) { cf_declare_section("T", &test_cf, 0); - if (cf_get_opt(argc, argv, CF_SHORT_OPTS, CF_NO_LONG_OPTS, NULL) != -1) + if (cf_getopt(argc, argv, CF_SHORT_OPTS, CF_NO_LONG_OPTS, NULL) != -1) die("Invalid arguments"); byte buf[256]; diff --git a/lib/lizard-test.c b/lib/lizard-test.c index 22fc96e4..3973cfcd 100644 --- a/lib/lizard-test.c +++ b/lib/lizard-test.c @@ -1,12 +1,11 @@ #include "lib/lib.h" -#include "lib/conf2.h" +#include "lib/getopt.h" #include "lib/fastbuf.h" #include "lib/lizard.h" #include #include #include #include -#include #include static char *options = CF_SHORT_OPTS "cdtx"; @@ -35,7 +34,7 @@ main(int argc, char **argv) uns action = 't'; uns crash = 0; log_init(argv[0]); - while ((opt = cf_get_opt(argc, argv, options, CF_NO_LONG_OPTS, NULL)) >= 0) + while ((opt = cf_getopt(argc, argv, options, CF_NO_LONG_OPTS, NULL)) >= 0) switch (opt) { case 'c': diff --git a/lib/redblack-test.c b/lib/redblack-test.c index 3c48f18f..80a7a000 100644 --- a/lib/redblack-test.c +++ b/lib/redblack-test.c @@ -5,11 +5,10 @@ */ #include "lib/lib.h" -#include "lib/conf2.h" +#include "lib/getopt.h" #include "lib/fastbuf.h" #include #include -#include struct my1_node { @@ -130,7 +129,7 @@ main(int argc, char **argv) int i; cf_def_file = NULL; log_init(argv[0]); - while ((opt = cf_get_opt(argc, argv, options, CF_NO_LONG_OPTS, NULL)) >= 0) + while ((opt = cf_getopt(argc, argv, options, CF_NO_LONG_OPTS, NULL)) >= 0) switch (opt) { case 'v': diff --git a/lib/shell/config.c b/lib/shell/config.c index f9fa9ae0..7fa16117 100644 --- a/lib/shell/config.c +++ b/lib/shell/config.c @@ -17,7 +17,8 @@ */ #include "lib/lib.h" -#include "lib/conf2.h" +#include "lib/conf.h" +#include "lib/getopt.h" #include #include @@ -171,7 +172,7 @@ int main(int argc, char **argv) } c->cls = CC_END; cf_declare_section(sec_name, sec, allow_unknown); - if (cf_get_opt(start, argv, CF_SHORT_OPTS, CF_NO_LONG_OPTS, NULL) != -1) + if (cf_getopt(start, argv, CF_SHORT_OPTS, CF_NO_LONG_OPTS, NULL) != -1) help(); return 0; } diff --git a/lib/sort-test.c b/lib/sort-test.c index 14f4a658..c3a48c8c 100644 --- a/lib/sort-test.c +++ b/lib/sort-test.c @@ -1,13 +1,12 @@ /* Test for sorting routines */ #include "lib/lib.h" -#include "lib/conf2.h" +#include "lib/getopt.h" #include "lib/fastbuf.h" #include #include #include -#include struct key { char line[4096]; @@ -79,7 +78,7 @@ int main(int argc, char **argv) { log_init(NULL); - if (cf_get_opt(argc, argv, CF_SHORT_OPTS, CF_NO_LONG_OPTS, NULL) >= 0 || + if (cf_getopt(argc, argv, CF_SHORT_OPTS, CF_NO_LONG_OPTS, NULL) >= 0 || optind != argc - 2) { fputs("This program supports only the following command-line arguments:\n" CF_USAGE, stderr); diff --git a/lib/sorter.c b/lib/sorter.c index 2272df72..2424bf46 100644 --- a/lib/sorter.c +++ b/lib/sorter.c @@ -8,7 +8,7 @@ */ #include "lib/lib.h" -#include "lib/conf2.h" +#include "lib/conf.h" #include "lib/fastbuf.h" #include diff --git a/lib/url.c b/lib/url.c index 2419f71f..c4662372 100644 --- a/lib/url.c +++ b/lib/url.c @@ -18,7 +18,7 @@ #include "lib/lib.h" #include "lib/url.h" #include "lib/chartype.h" -#include "lib/conf2.h" +#include "lib/conf.h" #include #include -- 2.39.2