]> mj.ucw.cz Git - libucw.git/blob - ucw/getopt.h
18788b41d822ba77db519cb88dc788b01c080433
[libucw.git] / ucw / 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 "ucw/getopt/getopt-sh.h"
16 #else
17 #include <getopt.h>
18 #endif
19
20 void reset_getopt(void);        /** If you want to start parsing of the arguments from the first one again. **/
21
22 /***
23  * [[conf_load]]
24  * Safe configuration loading
25  * ~~~~~~~~~~~~~~~~~~~~~~~~~~
26  *
27  * These functions can be used to to safely load or reload configuration.
28  */
29
30 /**
31  * The default config (DEFAULT_CONFIG config option) or NULL if already loaded.
32  * You can set it to something else manually.
33  */
34 extern char *cf_def_file;               /* DEFAULT_CONFIG; NULL if already loaded */
35 extern char *cf_env_file;               /* ENV_VAR_CONFIG */
36 int cf_reload(const char *file);        /** Reload configuration from @file, replace the old one. **/
37 int cf_load(const char *file);          /** Load configuration from @file. **/
38 /**
39  * Parse some part of configuration passed in @string.
40  * The syntax is the same as in the <<config:,configuration file>>.
41  **/
42 int cf_set(const char *string);
43
44 /***
45  * [[conf_direct]]
46  * Direct access
47  * ~~~~~~~~~~~~~
48  *
49  * Direct access to configuration items.
50  * You probably should not need this.
51  ***/
52
53 #define CF_OPERATIONS T(CLOSE) T(SET) T(CLEAR) T(ALL) \
54   T(APPEND) T(PREPEND) T(REMOVE) T(EDIT) T(AFTER) T(BEFORE) T(COPY)
55   /* Closing brace finishes previous block.
56    * Basic attributes (static, dynamic, parsed) can be used with SET.
57    * Dynamic arrays can be used with SET, APPEND, PREPEND.
58    * Sections can be used with SET.
59    * Lists can be used with everything. */
60 #define T(x) OP_##x,
61 enum cf_operation { CF_OPERATIONS };
62 #undef T
63
64 struct cf_item;
65 /**
66  * Searches for a configuration item called @name.
67  * If it is found, it is copied into @item and NULL is returned.
68  * Otherwise, an error is returned and @item is zeroed.
69  **/
70 char *cf_find_item(const char *name, struct cf_item *item);
71 // TODO What does this do?
72 char *cf_modify_item(struct cf_item *item, enum cf_operation op, int number, char **pars);
73
74 /***
75  * [[conf_dump]]
76  * Debug dumping
77  * ~~~~~~~~~~~~~
78  ***/
79
80 struct fastbuf;
81 /**
82  * Take everything and write it into @fb.
83  **/
84 void cf_dump_sections(struct fastbuf *fb);
85
86 /***
87  * [[conf_journal]]
88  * Journaling control
89  * ~~~~~~~~~~~~~~~~~~
90  ***/
91
92 struct cf_journal_item;
93 // TODO
94 struct cf_journal_item *cf_journal_new_transaction(uns new_pool);
95 void cf_journal_commit_transaction(uns new_pool, struct cf_journal_item *oldj);
96 void cf_journal_rollback_transaction(uns new_pool, struct cf_journal_item *oldj);
97
98 /***
99  * [[conf_getopt]]
100  * Loading by @cf_getopt()
101  * ~~~~~~~~~~~~~~~~~~~~~~~
102  ***/
103
104 /**
105  * Short options for loading configuration by @cf_getopt().
106  * Prepend to your own options.
107  **/
108 #define CF_SHORT_OPTS   "C:S:"
109 /**
110  * Long options for loading configuration by @cf_getopt().
111  * Prepend to your own options.
112  **/
113 #define CF_LONG_OPTS    {"config",      1, 0, 'C'}, {"set",             1, 0, 'S'}, CF_LONG_OPTS_DEBUG
114 /**
115  * Use this constant as @long_opts parameter of @cf_getopt() if you do
116  * not have any long options in your program.
117  **/
118 #define CF_NO_LONG_OPTS (const struct option []) { CF_LONG_OPTS { NULL, 0, 0, 0 } }
119 #ifndef CF_USAGE_TAB
120 #define CF_USAGE_TAB ""
121 #endif
122 /**
123  * This macro provides text describing usage of the configuration
124  * loading options. Concatenate with description of your options and
125  * write to the user, if he/she provides invalid options.
126  **/
127 #define CF_USAGE        \
128 "-C, --config filename\t" CF_USAGE_TAB "Override the default configuration file\n\
129 -S, --set sec.item=val\t" CF_USAGE_TAB "Manual setting of a configuration item\n" CF_USAGE_DEBUG
130
131 #ifdef CONFIG_DEBUG
132 #define CF_LONG_OPTS_DEBUG { "dumpconfig", 0, 0, 0x64436667 } ,
133 #define CF_USAGE_DEBUG "    --dumpconfig\t" CF_USAGE_TAB "Dump program configuration\n"
134 #else
135 #define CF_LONG_OPTS_DEBUG
136 #define CF_USAGE_DEBUG
137 #endif
138
139 /***
140  * Takes care of parsing the command-line arguments, loading the
141  * default configuration file (<<var_cf_def_file,`cf_def_file`>>) and processing
142  * configuration options. The calling convention is the same as with GNU getopt_long(),
143  * but you must prefix your own short/long options by the
144  * <<def_CF_LONG_OPTS,`CF_LONG_OPTS`>> or <<def_CF_SHORT_OPTS,`CF_SHORT_OPTS`>>or
145  * pass <<def_CF_NO_LONG_OPTS,`CF_NO_LONG_OPTS`>> if there are no long options.
146  *
147  * The default configuration file can be overwriten by the --config options,
148  * which must come first. During parsing of all other options, the configuration
149  * is already available.
150  ***/
151 int cf_getopt(int argc, char * const argv[], const char *short_opts, const struct option *long_opts, int *long_index);
152
153 #endif