]> mj.ucw.cz Git - libucw.git/blob - ucw/getopt.h
Conf: Removed declarations of cf_def_file and cf_env_file
[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_UCW_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 int cf_reload(const char *file);        /** Reload configuration from @file, replace the old one. **/
31 int cf_load(const char *file);          /** Load configuration from @file. If @file is NULL, reload all loaded configuration files. **/
32 /**
33  * Parse some part of configuration passed in @string.
34  * The syntax is the same as in the <<config:,configuration file>>.
35  **/
36 int cf_set(const char *string);
37
38 /***
39  * [[conf_direct]]
40  * Direct access
41  * ~~~~~~~~~~~~~
42  *
43  * Direct access to configuration items.
44  * You probably should not need this.
45  ***/
46
47 /**
48  * List of operations used on items.
49  * This macro is used to generate internal source code,
50  * but you may be interested in the list of operations it creates.
51  *
52  * Each operation corresponds to the same-named operation
53  * described in <<config:operations,configuration syntax>>.
54  **/
55 #define CF_OPERATIONS T(CLOSE) T(SET) T(CLEAR) T(ALL) \
56   T(APPEND) T(PREPEND) T(REMOVE) T(EDIT) T(AFTER) T(BEFORE) T(COPY) T(RESET)
57   /* Closing brace finishes previous block.
58    * Basic attributes (static, dynamic, parsed) can be used with SET.
59    * Dynamic arrays can be used with SET, APPEND, PREPEND.
60    * Sections can be used with SET.
61    * Lists can be used with everything. */
62 #define T(x) OP_##x,
63 enum cf_operation { CF_OPERATIONS };    /** Allowed operations on items. See <<def_CF_OPERATIONS,`CF_OPERATIONS`>> for list (they have an `OP_` prefix -- it means you use `OP_SET` instead of just `SET`). **/
64 #undef T
65
66 struct cf_item;
67 /**
68  * Searches for a configuration item called @name.
69  * If it is found, it is copied into @item and NULL is returned.
70  * Otherwise, an error is returned and @item is zeroed.
71  **/
72 char *cf_find_item(const char *name, struct cf_item *item);
73 /**
74  * Performs a single operation on a given item.
75  **/
76 char *cf_modify_item(struct cf_item *item, enum cf_operation op, int number, char **pars);
77
78 /***
79  * [[conf_dump]]
80  * Debug dumping
81  * ~~~~~~~~~~~~~
82  ***/
83
84 struct fastbuf;
85 /**
86  * Take everything and write it into @fb.
87  **/
88 void cf_dump_sections(struct fastbuf *fb);
89
90 /***
91  * [[conf_journal]]
92  * Journaling control
93  * ~~~~~~~~~~~~~~~~~~
94  *
95  * The configuration system uses journaling to safely reload
96  * configuration. It begins a transaction and tries to load the
97  * configuration. If it fails, it restores the original state.
98  *
99  * The behaviour of journal is described in <<reload,reloading configuration>>.
100  ***/
101
102 struct cf_journal_item;         /** Opaque identifier of the journal state. **/
103 /**
104  * Starts a new transaction. It returns the current state so you can
105  * get back to it. The @new_pool parameter tells if a new memory pool
106  * should be created and used from now.
107  **/
108 struct cf_journal_item *cf_journal_new_transaction(uns new_pool);
109 /**
110  * Marks current state as a complete transaction. The @new_pool
111  * parameter tells if the transaction was created with new memory pool
112  * (the parameter must be the same as the one with
113  * @cf_journal_new_transaction() was called with). The @oldj parameter
114  * is the journal state returned from last
115  * @cf_journal_new_transaction() call.
116  **/
117 void cf_journal_commit_transaction(uns new_pool, struct cf_journal_item *oldj);
118 /**
119  * Returns to an old journal state, reverting anything the current
120  * transaction did. The @new_pool parameter must be the same as the
121  * one you used when you created the transaction. The @oldj parameter
122  * is the journal state you got from @cf_journal_new_transaction() --
123  * it is the state to return to.
124  **/
125 void cf_journal_rollback_transaction(uns new_pool, struct cf_journal_item *oldj);
126
127 /***
128  * [[conf_getopt]]
129  * Loading by @cf_getopt()
130  * ~~~~~~~~~~~~~~~~~~~~~~~
131  ***/
132
133 /**
134  * Short options for loading configuration by @cf_getopt().
135  * Prepend to your own options.
136  **/
137 #define CF_SHORT_OPTS   "C:S:"
138 /**
139  * Long options for loading configuration by @cf_getopt().
140  * Prepend to your own options.
141  **/
142 #define CF_LONG_OPTS    {"config",      1, 0, 'C'}, {"set",             1, 0, 'S'}, CF_LONG_OPTS_DEBUG
143 /**
144  * Use this constant as @long_opts parameter of @cf_getopt() if you do
145  * not have any long options in your program.
146  **/
147 #define CF_NO_LONG_OPTS (const struct option []) { CF_LONG_OPTS { NULL, 0, 0, 0 } }
148 #ifndef CF_USAGE_TAB
149 #define CF_USAGE_TAB ""
150 #endif
151 /**
152  * This macro provides text describing usage of the configuration
153  * loading options. Concatenate with description of your options and
154  * write to the user, if he/she provides invalid options.
155  **/
156 #define CF_USAGE        \
157 "-C, --config filename\t" CF_USAGE_TAB "Override the default configuration file\n\
158 -S, --set sec.item=val\t" CF_USAGE_TAB "Manual setting of a configuration item\n" CF_USAGE_DEBUG
159
160 #ifdef CONFIG_UCW_DEBUG
161 #define CF_LONG_OPTS_DEBUG { "dumpconfig", 0, 0, 0x64436667 } ,
162 #define CF_USAGE_DEBUG "    --dumpconfig\t" CF_USAGE_TAB "Dump program configuration\n"
163 #else
164 #define CF_LONG_OPTS_DEBUG
165 #define CF_USAGE_DEBUG
166 #endif
167
168 /**
169  * Takes care of parsing the command-line arguments, loading the
170  * default configuration file (<<var_cf_def_file,`cf_def_file`>>) and processing
171  * configuration options. The calling convention is the same as with GNU getopt_long(),
172  * but you must prefix your own short/long options by the
173  * <<def_CF_LONG_OPTS,`CF_LONG_OPTS`>> or <<def_CF_SHORT_OPTS,`CF_SHORT_OPTS`>> or
174  * pass <<def_CF_NO_LONG_OPTS,`CF_NO_LONG_OPTS`>> if there are no long options.
175  *
176  * The default configuration file can be overwritten by the --config options,
177  * which must come first. During parsing of all other options, the configuration
178  * is already available.
179  **/
180 int cf_getopt(int argc, char * const argv[], const char *short_opts, const struct option *long_opts, int *long_index);
181
182 #endif