]> mj.ucw.cz Git - libucw.git/blob - lib/getopt.h
b4ff823ffb4f43bb9087535d79a5f658dc330a53
[libucw.git] / lib / getopt.h
1 /*
2  *      UCW Library -- Parsing of configuration and command-line options
3  *
4  *      (c) 2001--2006 Robert Spalek <robert@ucw.cz>
5  *      (c) 2003--2006 Martin Mares <mj@ucw.cz>
6  *
7  *      This software may be freely distributed and used according to the terms
8  *      of the GNU Lesser General Public License.
9  */
10
11 #ifndef _UCW_GETOPT_H
12 #define _UCW_GETOPT_H
13
14 #ifdef CONFIG_OWN_GETOPT
15 #include "lib/getopt/getopt-sh.h"
16 #else
17 #include <getopt.h>
18 #endif
19
20 void reset_getopt(void);
21
22 /* Safe loading and reloading of configuration files: conf-input.c */
23
24 extern char *cf_def_file;               /* DEFAULT_CONFIG; NULL if already loaded */
25 extern char *cf_env_file;               /* ENV_VAR_CONFIG */
26 int cf_reload(const char *file);
27 int cf_load(const char *file);
28 int cf_set(const char *string);
29
30 /* Direct access to configuration items: conf-intr.c */
31
32 #define CF_OPERATIONS T(CLOSE) T(SET) T(CLEAR) T(ALL) \
33   T(APPEND) T(PREPEND) T(REMOVE) T(EDIT) T(AFTER) T(BEFORE) T(COPY)
34   /* Closing brace finishes previous block.
35    * Basic attributes (static, dynamic, parsed) can be used with SET.
36    * Dynamic arrays can be used with SET, APPEND, PREPEND.
37    * Sections can be used with SET.
38    * Lists can be used with everything. */
39 #define T(x) OP_##x,
40 enum cf_operation { CF_OPERATIONS };
41 #undef T
42
43 struct cf_item;
44 char *cf_find_item(const char *name, struct cf_item *item);
45 char *cf_write_item(struct cf_item *item, enum cf_operation op, int number, char **pars);
46
47 /* Debug dumping: conf-dump.c */
48
49 struct fastbuf;
50 void cf_dump_sections(struct fastbuf *fb);
51
52 /* Journaling control: conf-journal.c */
53
54 struct cf_journal_item;
55 struct cf_journal_item *cf_journal_new_transaction(uns new_pool);
56 void cf_journal_commit_transaction(uns new_pool, struct cf_journal_item *oldj);
57 void cf_journal_rollback_transaction(uns new_pool, struct cf_journal_item *oldj);
58
59 /*
60  * cf_getopt() takes care of parsing the command-line arguments, loading the
61  * default configuration file (cf_def_file) and processing configuration options.
62  * The calling convention is the same as with GNU getopt_long(), but you must prefix
63  * your own short/long options by the CF_(SHORT|LONG)_OPTS or pass CF_NO_LONG_OPTS
64  * of there are no long options.
65  *
66  * The default configuration file can be overriden by the --config options,
67  * which must come first. During parsing of all other options, the configuration
68  * is already available.
69  */
70
71 #define CF_SHORT_OPTS   "C:S:"
72 #define CF_LONG_OPTS    {"config",      1, 0, 'C'}, {"set",             1, 0, 'S'}, CF_LONG_OPTS_DEBUG
73 #define CF_NO_LONG_OPTS (const struct option []) { CF_LONG_OPTS { NULL, 0, 0, 0 } }
74 #ifndef CF_USAGE_TAB
75 #define CF_USAGE_TAB ""
76 #endif
77 #define CF_USAGE        \
78 "-C, --config filename\t" CF_USAGE_TAB "Override the default configuration file\n\
79 -S, --set sec.item=val\t" CF_USAGE_TAB "Manual setting of a configuration item\n" CF_USAGE_DEBUG
80
81 #ifdef CONFIG_DEBUG
82 #define CF_LONG_OPTS_DEBUG { "dumpconfig", 0, 0, 0x64436667 } ,
83 #define CF_USAGE_DEBUG "    --dumpconfig\t" CF_USAGE_TAB "Dump program configuration\n"
84 #else
85 #define CF_LONG_OPTS_DEBUG
86 #define CF_USAGE_DEBUG
87 #endif
88
89 // conf-input.c
90 int cf_getopt(int argc, char * const argv[], const char *short_opts, const struct option *long_opts, int *long_index);
91
92 #endif