Encoding tests rewritten. They do not create files in /tmp.
[elinks.git] / src / config / options.h
blob2baea8cd19554056a371f07a2d749a99a52442ed
2 #ifndef EL__CONFIG_OPTIONS_H
3 #define EL__CONFIG_OPTIONS_H
5 #include "main/object.h"
6 #include "util/color.h"
7 #include "util/lists.h"
8 #include "util/memory.h"
9 #include "util/string.h"
11 /* TODO: We should provide some generic mechanism for options caching. */
13 /* Fix namespace clash on Cygwin. */
14 #define option option_elinks
17 enum option_flags {
18 /* bitmask */
19 /* The option is hidden - it serves for internal purposes, never is
20 * read, never is written, never is displayed, never is crawled through
21 * etc. */
22 OPT_HIDDEN = 1,
23 /* For OPT_TREE, automatically create missing hiearchy piece just under
24 * this category when adding an option. The 'template' for the added
25 * hiearchy piece (category) is stored as "_template_" category. */
26 OPT_AUTOCREATE = 2,
27 /* The option has been modified in some way and must be saved
28 * to elinks.conf. ELinks uses this flag only while it is
29 * saving the options. When the config.saving_style option
30 * has value 3, saving works like this:
31 * - First, ELinks sets OPT_MUST_SAVE in the options that have
32 * OPT_TOUCHED or OPT_DELETED, and clears it in the rest.
33 * - ELinks then parses the old configuration file and any
34 * files named in "include" commands.
35 * - If the old configuration file contains a "set" or "unset"
36 * command for this option, ELinks rewrites the command and
37 * clears OPT_MUST_SAVE.
38 * - If an included file contains a "set" or "unset" command
39 * for this option, ELinks compares the value of the option
40 * to the value given in the command. ELinks clears
41 * OPT_MUST_SAVE if the values match, or sets it if they
42 * differ.
43 * - After ELinks has rewritten the configuration file and
44 * parsed the included files, it appends the options that
45 * still have the OPT_MUST_SAVE flag.
46 * Other saving styles are variants of this:
47 * - 0: ELinks does not append any options to the
48 * configuration file. So OPT_MUST_SAVE has no effect.
49 * - 1: ELinks initially sets OPT_MUST_SAVE in all options,
50 * regardless of OPT_TOUCHED and OPT_DELETED.
51 * - 2: ELinks initially sets OPT_MUST_SAVE in all options,
52 * and does not read any configuration files. */
53 OPT_MUST_SAVE = 4,
54 /* This is used to mark options modified after the last save. That's
55 * being useful if you want to save only the options whose value
56 * changed. */
57 OPT_TOUCHED = 8,
58 /* If set on the tree argument to add_opt (not necessarily the direct
59 * parent) or on the option itself, it will create the listbox (options
60 * manager) item for the option. */
61 OPT_LISTBOX = 16,
62 /* This is used to mark that the option _and_ the option name is
63 * allocated and should be freed when the option is released. */
64 OPT_ALLOC = 32,
65 /* For OPT_TREE, automatically sort the content of the tree
66 * alphabetically (but all subtrees in front of ordinary options) when
67 * adding new options. Note that this applies only to the one level
68 * below - it will not apply to the sub-trees in this tree. Also, this
69 * can be quite expensive for busy-adding big trees, so think twice
70 * before doing it - in fact, it is supposed to be used only where you
71 * add stuff from more modules, not all at once; typically the
72 * config_options root tree. Especially NOT RECOMMENDED to be used on
73 * the template trees. */
74 OPT_SORT = 64,
75 /* This is used to mark option as deleted */
76 OPT_DELETED = 128,
77 /* Specifies that values of boolean aliases should be inverted. */
78 OPT_ALIAS_NEGATE = 256
81 enum option_type {
82 OPT_BOOL = 0,
83 OPT_INT,
84 OPT_LONG,
85 OPT_STRING,
87 OPT_CODEPAGE,
88 OPT_LANGUAGE,
89 OPT_COLOR,
91 OPT_COMMAND,
93 OPT_ALIAS,
95 OPT_TREE,
98 struct listbox_item; /* bfu/listbox.h */
99 struct option; /* defined later in this file */
100 struct session; /* session/session.h */
102 union option_value {
103 /* XXX: Keep first to make @options_root initialization possible. */
104 /* The OPT_TREE list_head is allocated. */
105 LIST_OF(struct option) *tree;
107 /* Used by OPT_BOOL, OPT_INT, OPT_CODEPAGE and OPT_LANGUAGE */
108 int number;
110 /* Used by OPT_LONG */
111 long big_number;
113 /* The OPT_COLOR value */
114 color_T color;
116 /* The OPT_COMMAND value */
117 unsigned char *(*command)(struct option *, unsigned char ***, int *);
119 /* The OPT_STRING string is allocated and has length MAX_STR_LEN.
120 * The OPT_ALIAS string is NOT allocated, has variable length
121 * (opt->max) and should remain untouched! It contains the full path to
122 * the "real" / aliased option. */
123 unsigned char *string;
127 /* @session is the session via which the user changed the options,
128 * or NULL if not known. Because the options are currently not
129 * session-specific, it is best to ignore this parameter. In a future
130 * version of ELinks, this parameter might mean the session to which
131 * the changed options apply.
133 * @current is the option whose change hook is being called. It is
134 * never NULL.
136 * @changed is the option that was changed, or NULL if multiple
137 * descendants of @current may have been changed. */
138 typedef int (*change_hook_T)(struct session *session, struct option *current,
139 struct option *changed);
141 struct option {
142 OBJECT_HEAD(struct option);
144 unsigned char *name;
145 enum option_flags flags;
146 enum option_type type;
147 long min, max;
148 union option_value value;
149 unsigned char *desc;
150 unsigned char *capt;
152 struct option *root;
154 /* To be called when the option (or sub-option if it's a tree) is
155 * changed. If it returns zero, we will continue descending the options
156 * tree checking for change handlers. */
157 change_hook_T change_hook;
159 struct listbox_item *box_item;
162 #define INIT_OPTION(name, flags, type, min, max, value, desc, capt) \
163 { NULL_LIST_HEAD, INIT_OBJECT("option"), name, flags, type, min, max, { (LIST_OF(struct option) *) (value) }, desc, capt }
165 extern struct option *config_options;
166 extern struct option *cmdline_options;
169 extern void init_options(void);
170 extern void done_options(void);
173 struct change_hook_info {
174 unsigned char *name;
175 change_hook_T change_hook;
178 extern void register_change_hooks(const struct change_hook_info *change_hooks);
181 extern LIST_OF(struct option) *init_options_tree(void);
182 extern void prepare_mustsave_flags(LIST_OF(struct option) *, int set_all);
183 extern void untouch_options(LIST_OF(struct option) *);
185 extern void smart_config_string(struct string *, int, int,
186 LIST_OF(struct option) *, unsigned char *, int,
187 void (*)(struct string *, struct option *,
188 unsigned char *, int, int, int, int));
190 extern struct option *copy_option(struct option *);
191 extern void delete_option(struct option *);
192 void mark_option_as_deleted(struct option *);
194 /* Some minimal option cache */
196 struct option_resolver {
197 int id;
198 unsigned char *name;
201 /* Update the visibility of the box item of each option
202 * in config_options to honour the value of config.show_template. */
203 void update_options_visibility(void);
205 /* Toggle the value of the given option numeric, respecting option->min
206 * and option->max. */
207 void toggle_option(struct session *ses, struct option *option);
209 /* Call the change-hooks for the given option and recur on its parent. */
210 void call_change_hooks(struct session *ses, struct option *current,
211 struct option *option);
213 /* Do proper bookkeeping after an option has changed - call this every time
214 * you change an option value. */
215 void option_changed(struct session *ses, struct option *option);
217 extern int commit_option_values(struct option_resolver *resolvers,
218 struct option *root,
219 union option_value *values, int size);
220 extern void checkout_option_values(struct option_resolver *resolvers,
221 struct option *root,
222 union option_value *values, int size);
224 /* Shitload of various incredible macro combinations and other unusable garbage
225 * follows. Have fun. */
227 /* Basically, for main hiearchy addressed from root (almost always) you want to
228 * use get_opt_type() and add_opt_type(). For command line options, you want to
229 * use get_opt_type_tree(cmdline_options, "option"). */
231 extern struct option *get_opt_rec(struct option *, const unsigned char *);
232 extern struct option *get_opt_rec_real(struct option *, const unsigned char *);
233 struct option *indirect_option(struct option *);
234 #ifdef CONFIG_DEBUG
235 extern union option_value *get_opt_(unsigned char *, int, enum option_type, struct option *, unsigned char *);
236 #define get_opt(tree, name, type) get_opt_(__FILE__, __LINE__, type, tree, name)
237 #else
238 extern union option_value *get_opt_(struct option *, unsigned char *);
239 #define get_opt(tree, name, type) get_opt_(tree, name)
240 #endif
242 #define get_opt_bool_tree(tree, name) get_opt(tree, name, OPT_BOOL)->number
243 #define get_opt_int_tree(tree, name) get_opt(tree, name, OPT_INT)->number
244 #define get_opt_long_tree(tree, name) get_opt(tree, name, OPT_LONG)->big_number
245 #define get_opt_str_tree(tree, name) get_opt(tree, name, OPT_STRING)->string
246 #define get_opt_codepage_tree(tree, name) get_opt(tree, name, OPT_CODEPAGE)->number
247 #define get_opt_color_tree(tree, name) get_opt(tree, name, OPT_COLOR)->color
248 #define get_opt_tree_tree(tree_, name) get_opt(tree_, name, OPT_TREE)->tree
250 #define get_opt_bool(name) get_opt_bool_tree(config_options, name)
251 #define get_opt_int(name) get_opt_int_tree(config_options, name)
252 #define get_opt_long(name) get_opt_long_tree(config_options, name)
253 #define get_opt_str(name) get_opt_str_tree(config_options, name)
254 #define get_opt_codepage(name) get_opt_codepage_tree(config_options, name)
255 #define get_opt_color(name) get_opt_color_tree(config_options, name)
256 #define get_opt_tree(name) get_opt_tree_tree(config_options, name)
258 #define get_cmd_opt_bool(name) get_opt_bool_tree(cmdline_options, name)
259 #define get_cmd_opt_int(name) get_opt_int_tree(cmdline_options, name)
260 #define get_cmd_opt_long(name) get_opt_long_tree(cmdline_options, name)
261 #define get_cmd_opt_str(name) get_opt_str_tree(cmdline_options, name)
262 #define get_cmd_opt_codepage(name) get_opt_codepage_tree(cmdline_options, name)
263 #define get_cmd_opt_color(name) get_opt_color_tree(cmdline_options, name)
264 #define get_cmd_opt_tree(name) get_opt_tree_tree(cmdline_options, name)
266 extern struct option *add_opt(struct option *, unsigned char *, unsigned char *,
267 unsigned char *, enum option_flags, enum option_type,
268 long, long, longptr_T, unsigned char *);
270 /* Hack which permit to disable option descriptions, to reduce elinks binary size.
271 * It may of some use for people wanting a very small static non-i18n elinks binary,
272 * at time of writing gain is over 25Kbytes. --Zas */
273 #ifndef CONFIG_SMALL
274 #define DESC(x) (x)
275 #else
276 #define DESC(x) ((unsigned char *) "")
277 #endif
280 #define add_opt_bool_tree(tree, path, capt, name, flags, def, desc) \
281 add_opt(tree, path, capt, name, flags, OPT_BOOL, 0, 1, (longptr_T) def, DESC(desc))
283 #define add_opt_int_tree(tree, path, capt, name, flags, min, max, def, desc) \
284 add_opt(tree, path, capt, name, flags, OPT_INT, min, max, (longptr_T) def, DESC(desc))
286 #define add_opt_long_tree(tree, path, capt, name, flags, min, max, def, desc) \
287 add_opt(tree, path, capt, name, flags, OPT_LONG, min, max, (longptr_T) def, DESC(desc))
289 #define add_opt_str_tree(tree, path, capt, name, flags, def, desc) \
290 do { \
291 unsigned char *ptr = mem_alloc(MAX_STR_LEN); \
292 safe_strncpy(ptr, def, MAX_STR_LEN); \
293 add_opt(tree, path, capt, name, flags, OPT_STRING, 0, MAX_STR_LEN, (longptr_T) ptr, DESC(desc)); \
294 } while (0)
296 #define add_opt_codepage_tree(tree, path, capt, name, flags, def, desc) \
297 add_opt(tree, path, capt, name, flags, OPT_CODEPAGE, 0, 0, (longptr_T) get_cp_index(def), DESC(desc))
299 #define add_opt_lang_tree(tree, path, capt, name, flags, desc) \
300 add_opt(tree, path, capt, name, flags, OPT_LANGUAGE, 0, 0, (longptr_T) 0, DESC(desc))
302 #define add_opt_color_tree(tree, path, capt, name, flags, def, desc) \
303 add_opt(tree, path, capt, name, flags, OPT_COLOR, 0, 0, (longptr_T) def, DESC(desc))
305 #define add_opt_command_tree(tree, path, capt, name, flags, cmd, desc) \
306 add_opt(tree, path, capt, name, flags, OPT_COMMAND, 0, 0, (longptr_T) cmd, DESC(desc));
308 #define add_opt_alias_tree(tree, path, capt, name, flags, def, desc) \
309 add_opt(tree, path, capt, name, flags, OPT_ALIAS, 0, strlen(def), (longptr_T) def, DESC(desc))
311 #define add_opt_tree_tree(tree, path, capt, name, flags, desc) \
312 add_opt(tree, path, capt, name, flags, OPT_TREE, 0, 0, (longptr_T) init_options_tree(), DESC(desc));
315 /* Builtin options */
317 struct option_info {
318 struct option option;
319 unsigned char *path;
322 extern void register_options(struct option_info info[], struct option *tree);
323 extern void unregister_options(struct option_info info[], struct option *tree);
325 #define NULL_OPTION_INFO \
326 { INIT_OPTION(NULL, 0, 0, 0, 0, NULL, NULL, NULL), NULL }
328 #define INIT_OPT_BOOL(path, capt, name, flags, def, desc) \
329 { INIT_OPTION(name, flags, OPT_BOOL, 0, 1, def, DESC(desc), capt), path }
331 #define INIT_OPT_INT(path, capt, name, flags, min, max, def, desc) \
332 { INIT_OPTION(name, flags, OPT_INT, min, max, def, DESC(desc), capt), path }
334 #define INIT_OPT_LONG(path, capt, name, flags, min, max, def, desc) \
335 { INIT_OPTION(name, flags, OPT_LONG, min, max, def, DESC(desc), capt), path }
337 #define INIT_OPT_STRING(path, capt, name, flags, def, desc) \
338 { INIT_OPTION(name, flags, OPT_STRING, 0, MAX_STR_LEN, def, DESC(desc), capt), path }
340 #define INIT_OPT_CODEPAGE(path, capt, name, flags, def, desc) \
341 { INIT_OPTION(name, flags, OPT_CODEPAGE, 0, 0, def, DESC(desc), capt), path }
343 #define INIT_OPT_COLOR(path, capt, name, flags, def, desc) \
344 { INIT_OPTION(name, flags, OPT_COLOR, 0, 0, def, DESC(desc), capt), path }
346 #define INIT_OPT_LANGUAGE(path, capt, name, flags, desc) \
347 { INIT_OPTION(name, flags, OPT_LANGUAGE, 0, 0, 0, DESC(desc), capt), path }
349 #define INIT_OPT_COMMAND(path, capt, name, flags, cmd, desc) \
350 { INIT_OPTION(name, flags, OPT_COMMAND, 0, 0, cmd, DESC(desc), capt), path }
352 #define INIT_OPT_CMDALIAS(path, capt, name, flags, def, desc) \
353 { INIT_OPTION(name, flags, OPT_ALIAS, 0, sizeof(def) - 1, def, DESC(desc), capt), path }
355 #define INIT_OPT_ALIAS(path, name, flags, def) \
356 { INIT_OPTION(name, flags, OPT_ALIAS, 0, sizeof(def) - 1, def, NULL, NULL), path }
358 #define INIT_OPT_TREE(path, capt, name, flags, desc) \
359 { INIT_OPTION(name, flags, OPT_TREE, 0, 0, NULL, DESC(desc), capt), path }
362 /* TODO: We need to do *something* with this ;). */
364 enum referer {
365 REFERER_NONE,
366 REFERER_SAME_URL,
367 REFERER_FAKE,
368 REFERER_TRUE,
371 enum verbose_level {
372 VERBOSE_QUIET,
373 VERBOSE_WARNINGS,
374 VERBOSE_ALL,
376 VERBOSE_LEVELS,
379 #endif