Make PHP 5 happy.
[elinks.git] / src / config / options.c
blob4996a4ffefeb8f1744556f4d8f98b9106c879ea4
1 /* Options variables manipulation core */
3 #ifdef HAVE_CONFIG_H
4 #include "config.h"
5 #endif
7 #include <ctype.h>
8 #include <string.h>
10 #include "elinks.h"
12 #include "bfu/dialog.h"
13 #include "cache/cache.h"
14 #include "config/conf.h"
15 #include "config/dialogs.h"
16 #include "config/options.h"
17 #include "config/opttypes.h"
18 #include "dialogs/status.h"
19 #include "document/document.h"
20 #include "globhist/globhist.h"
21 #include "intl/charsets.h"
22 #include "intl/gettext/libintl.h"
23 #include "main/main.h" /* shrink_memory() */
24 #include "main/select.h"
25 #include "network/connection.h"
26 #include "session/session.h"
27 #include "terminal/color.h"
28 #include "terminal/screen.h"
29 #include "terminal/terminal.h"
30 #include "util/color.h"
31 #include "util/error.h"
32 #include "util/memory.h"
33 #include "util/string.h"
34 #include "viewer/text/draw.h"
37 /* TODO? In the past, covered by shadow and legends, remembered only by the
38 * ELinks Elders now, options were in hashes (it was not for a long time, after
39 * we started to use dynamic options lists and before we really started to use
40 * hierarchic options). Hashes might be swift and deft, but they had a flaw and
41 * the flaw showed up as the fatal flaw. They were unsorted, and it was
42 * unfriendly to mere mortal users, without pasky's options handlers in their
43 * brain, but their own poor-written software. And thus pasky went and rewrote
44 * options so that they were in lists from then to now and for all the ages of
45 * men, to the glory of mankind. However, one true hero may arise in future
46 * fabulous and implement possibility to have both lists and hashes for trees,
47 * as it may be useful for some supernatural entities. And when that age will
48 * come... */
51 /* TODO: We should remove special case for root options and use some auxiliary
52 * (struct option *) instead. This applies to bookmarks, global history and
53 * listbox items as well, though. --pasky */
55 static INIT_LIST_HEAD(options_root_tree);
57 static struct option options_root = INIT_OPTION(
58 /* name: */ "",
59 /* flags: */ 0,
60 /* type: */ OPT_TREE,
61 /* min, max: */ 0, 0,
62 /* value: */ &options_root_tree,
63 /* desc: */ "",
64 /* capt: */ NULL
67 struct option *config_options;
68 struct option *cmdline_options;
70 static void add_opt_rec(struct option *, unsigned char *, struct option *);
71 static void free_options_tree(struct list_head *, int recursive);
73 #ifdef CONFIG_DEBUG
74 /* Detect ending '.' (and some others) in options captions.
75 * It will emit a message in debug mode only. --Zas */
77 #define bad_punct(c) (c != ')' && c != '>' && !isquote(c) && ispunct(c))
79 static void
80 check_caption(unsigned char *caption)
82 int len;
83 unsigned char c;
85 if (!caption) return;
87 len = strlen(caption);
88 if (!len) return;
90 c = caption[len - 1];
91 if (isspace(c) || bad_punct(c))
92 DBG("bad char at end of caption [%s]", caption);
94 #ifdef CONFIG_NLS
95 caption = gettext(caption);
96 len = strlen(caption);
97 if (!len) return;
99 c = caption[len - 1];
100 if (isspace(c) || bad_punct(c))
101 DBG("bad char at end of i18n caption [%s]", caption);
102 #endif
105 #undef bad_punct
107 static void
108 check_description(unsigned char *desc)
110 int len;
111 unsigned char c;
113 if (!desc) return;
115 len = strlen(desc);
116 if (!len) return;
118 c = desc[len - 1];
119 if (isspace(c))
120 DBG("bad char at end of description [%s]", desc);
122 #ifdef CONFIG_NLS
123 desc = gettext(desc);
124 len = strlen(desc);
125 if (!len) return;
127 if (ispunct(c) != ispunct(desc[len - 1]))
128 DBG("punctuation char possibly missing at end of i18n description [%s]", desc);
130 c = desc[len - 1];
131 if (isspace(c))
132 DBG("bad char at end of i18n description [%s]", desc);
133 #endif
136 static void
137 debug_check_option_syntax(struct option *option)
139 if (!option) return;
140 check_caption(option->capt);
141 check_description(option->desc);
144 #else
145 #define debug_check_option_syntax(option)
146 #endif
149 /**********************************************************************
150 Options interface
151 **********************************************************************/
153 /* If option name contains dots, they are created as "categories" - first,
154 * first category is retrieved from list, taken as a list, second category
155 * is retrieved etc. */
157 /* Ugly kludge */
158 static int no_autocreate = 0;
160 /* Get record of option of given name, or NULL if there's no such option. */
161 struct option *
162 get_opt_rec(struct option *tree, unsigned char *name_)
164 struct option *option;
165 unsigned char *aname = stracpy(name_);
166 unsigned char *name = aname;
167 unsigned char *sep;
169 if (!aname) return NULL;
171 /* We iteratively call get_opt_rec() each for path_elements-1, getting
172 * appropriate tree for it and then resolving [path_elements]. */
173 if ((sep = strrchr(name, '.'))) {
174 *sep = '\0';
176 tree = get_opt_rec(tree, name);
177 if (!tree || tree->type != OPT_TREE || tree->flags & OPT_HIDDEN) {
178 #if 0
179 DBG("ERROR in get_opt_rec() crawl: %s (%d) -> %s",
180 name, tree ? tree->type : -1, sep + 1);
181 #endif
182 mem_free(aname);
183 return NULL;
186 *sep = '.';
187 name = sep + 1;
190 foreach (option, *tree->value.tree) {
191 if (option->name && !strcmp(option->name, name)) {
192 mem_free(aname);
193 return option;
197 if (tree && tree->flags & OPT_AUTOCREATE && !no_autocreate) {
198 struct option *template = get_opt_rec(tree, "_template_");
200 assertm(template, "Requested %s should be autocreated but "
201 "%.*s._template_ is missing!", name_, sep - name_,
202 name_);
203 if_assert_failed {
204 mem_free(aname);
205 return NULL;
208 /* We will just create the option and return pointer to it
209 * automagically. And, we will create it by cloning _template_
210 * option. By having _template_ OPT_AUTOCREATE and _template_
211 * inside, you can have even multi-level autocreating. */
213 option = copy_option(template);
214 if (!option) {
215 mem_free(aname);
216 return NULL;
218 mem_free_set(&option->name, stracpy(name));
220 add_opt_rec(tree, "", option);
222 mem_free(aname);
223 return option;
226 mem_free(aname);
227 return NULL;
230 /* Get record of option of given name, or NULL if there's no such option. But
231 * do not create the option if it doesn't exist and there's autocreation
232 * enabled. */
233 struct option *
234 get_opt_rec_real(struct option *tree, unsigned char *name)
236 struct option *opt;
238 no_autocreate = 1;
239 opt = get_opt_rec(tree, name);
240 no_autocreate = 0;
241 return opt;
244 /* Fetch pointer to value of certain option. It is guaranteed to never return
245 * NULL. Note that you are supposed to use wrapper get_opt(). */
246 union option_value *
247 get_opt_(
248 #ifdef CONFIG_DEBUG
249 unsigned char *file, int line, enum option_type option_type,
250 #endif
251 struct option *tree, unsigned char *name)
253 struct option *opt = get_opt_rec(tree, name);
255 #ifdef CONFIG_DEBUG
256 errfile = file;
257 errline = line;
258 if (!opt) elinks_internal("Attempted to fetch nonexisting option %s!", name);
260 /* Various sanity checks. */
261 if (option_type != opt->type)
262 DBG("get_opt_*(\"%s\") @ %s:%d: call with wrapper for %s for option of type %s",
263 name, file, line,
264 get_option_type_name(option_type),
265 get_option_type_name(opt->type));
267 switch (opt->type) {
268 case OPT_TREE:
269 if (!opt->value.tree)
270 elinks_internal("Option %s has no value!", name);
271 break;
272 case OPT_ALIAS:
273 elinks_internal("Invalid use of alias %s for option %s!",
274 name, opt->value.string);
275 break;
276 case OPT_STRING:
277 if (!opt->value.string)
278 elinks_internal("Option %s has no value!", name);
279 break;
280 case OPT_BOOL:
281 case OPT_INT:
282 case OPT_LONG:
283 if (opt->value.number < opt->min
284 || opt->value.number > opt->max)
285 elinks_internal("Option %s has invalid value %d!", name, opt->value.number);
286 break;
287 case OPT_COMMAND:
288 if (!opt->value.command)
289 elinks_internal("Option %s has no value!", name);
290 break;
291 case OPT_CODEPAGE: /* TODO: check these too. */
292 case OPT_LANGUAGE:
293 case OPT_COLOR:
294 break;
296 #endif
298 return &opt->value;
301 static void
302 add_opt_sort(struct option *tree, struct option *option, int abi)
304 struct list_head *cat = tree->value.tree;
305 struct list_head *bcat = &tree->box_item->child;
306 struct option *pos;
308 /* The list is empty, just add it there. */
309 if (list_empty(*cat)) {
310 add_to_list(*cat, option);
311 if (abi) add_to_list(*bcat, option->box_item);
313 /* This fits as the last list entry, add it there. This
314 * optimizes the most expensive BUT most common case ;-). */
315 } else if ((option->type != OPT_TREE
316 || ((struct option *) cat->prev)->type == OPT_TREE)
317 && strcmp(((struct option *) cat->prev)->name,
318 option->name) <= 0) {
319 append:
320 add_to_list_end(*cat, option);
321 if (abi) add_to_list_end(*bcat, option->box_item);
323 /* At the end of the list is tree and we are ordinary. That's
324 * clear case then. */
325 } else if (option->type != OPT_TREE
326 && ((struct option *) cat->prev)->type == OPT_TREE) {
327 goto append;
329 /* Scan the list linearly. This could be probably optimized ie.
330 * to choose direction based on the first letter or so. */
331 } else {
332 struct listbox_item *bpos = (struct listbox_item *) bcat;
334 foreach (pos, *cat) {
335 /* First move the box item to the current position but
336 * only if the position has not been marked as deleted
337 * and actually has a box_item -- else we will end up
338 * 'overflowing' and causing assertion failure. */
339 if (!(pos->flags & OPT_DELETED) && pos->box_item) {
340 bpos = bpos->next;
341 assert(bpos != (struct listbox_item *) bcat);
344 if ((option->type != OPT_TREE
345 || pos->type == OPT_TREE)
346 && strcmp(pos->name, option->name) <= 0)
347 continue;
349 /* Ordinary options always sort behind trees. */
350 if (option->type != OPT_TREE
351 && pos->type == OPT_TREE)
352 continue;
354 /* The (struct option) add_at_pos() can mess up the
355 * order so that we add the box_item to itself, so
356 * better do it first. */
358 /* Always ensure that them _template_ options are
359 * before anything else so a lonely autocreated option
360 * (with _template_ options set to invisible) will be
361 * connected with an upper corner (ascii: `-) instead
362 * of a rotated T (ascii: +-) when displaying it. */
363 if (option->type == pos->type
364 && *option->name <= '_'
365 && !strcmp(pos->name, "_template_")) {
366 if (abi) add_at_pos(bpos, option->box_item);
367 add_at_pos(pos, option);
368 break;
371 if (abi) add_at_pos(bpos->prev, option->box_item);
372 add_at_pos(pos->prev, option);
373 break;
376 assert(pos != (struct option *) cat);
377 assert(bpos != (struct listbox_item *) bcat);
381 /* Add option to tree. */
382 static void
383 add_opt_rec(struct option *tree, unsigned char *path, struct option *option)
385 int abi = 0;
387 assert(path && option && tree);
388 if (*path) tree = get_opt_rec(tree, path);
390 assertm(tree, "Missing option tree for '%s'", path);
391 if (!tree->value.tree) return;
393 object_nolock(option, "option");
395 if (option->box_item && option->name && !strcmp(option->name, "_template_"))
396 option->box_item->visible = get_opt_bool("config.show_template");
398 if (tree->flags & OPT_AUTOCREATE && !option->desc) {
399 struct option *template = get_opt_rec(tree, "_template_");
401 assert(template);
402 option->desc = template->desc;
405 option->root = tree;
407 abi = (tree->box_item && option->box_item);
409 if (abi) {
410 /* The config_root tree is a just a placeholder for the
411 * box_items, it actually isn't a real box_item by itself;
412 * these ghosts are indicated by the fact that they have
413 * NULL @next. */
414 if (tree->box_item->next) {
415 option->box_item->depth = tree->box_item->depth + 1;
419 if (tree->flags & OPT_SORT) {
420 add_opt_sort(tree, option, abi);
422 } else {
423 add_to_list_end(*tree->value.tree, option);
424 if (abi) add_to_list_end(tree->box_item->child, option->box_item);
427 update_hierbox_browser(&option_browser);
430 static inline struct listbox_item *
431 init_option_listbox_item(struct option *option)
433 struct listbox_item *item = mem_calloc(1, sizeof(*item));
435 if (!item) return NULL;
437 init_list(item->child);
438 item->visible = 1;
439 item->udata = option;
440 item->type = (option->type == OPT_TREE) ? BI_FOLDER : BI_LEAF;
442 return item;
445 struct option *
446 add_opt(struct option *tree, unsigned char *path, unsigned char *capt,
447 unsigned char *name, enum option_flags flags, enum option_type type,
448 int min, int max, void *value, unsigned char *desc)
450 struct option *option = mem_calloc(1, sizeof(*option));
452 if (!option) return NULL;
454 option->name = stracpy(name);
455 if (!option->name) {
456 mem_free(option);
457 return NULL;
459 option->flags = (flags | OPT_ALLOC);
460 option->type = type;
461 option->min = min;
462 option->max = max;
463 option->capt = capt;
464 option->desc = desc;
466 debug_check_option_syntax(option);
468 if (option->type != OPT_ALIAS
469 && ((tree->flags & OPT_LISTBOX) || (option->flags & OPT_LISTBOX))) {
470 option->box_item = init_option_listbox_item(option);
471 if (!option->box_item) {
472 delete_option(option);
473 return NULL;
477 /* XXX: For allocated values we allocate in the add_opt_<type>() macro.
478 * This involves OPT_TREE and OPT_STRING. */
479 switch (type) {
480 case OPT_TREE:
481 if (!value) {
482 delete_option(option);
483 return NULL;
485 option->value.tree = value;
486 break;
487 case OPT_STRING:
488 if (!value) {
489 delete_option(option);
490 return NULL;
492 option->value.string = value;
493 break;
494 case OPT_ALIAS:
495 option->value.string = value;
496 break;
497 case OPT_BOOL:
498 case OPT_INT:
499 case OPT_CODEPAGE:
500 case OPT_LONG:
501 option->value.number = (long) value;
502 break;
503 case OPT_COLOR:
504 decode_color(value, strlen((unsigned char *) value),
505 &option->value.color);
506 break;
507 case OPT_COMMAND:
508 option->value.command = value;
509 break;
510 case OPT_LANGUAGE:
511 break;
514 add_opt_rec(tree, path, option);
515 return option;
518 static void
519 done_option(struct option *option)
521 switch (option->type) {
522 case OPT_STRING:
523 mem_free_if(option->value.string);
524 break;
525 case OPT_TREE:
526 mem_free_if(option->value.tree);
527 break;
528 default:
529 break;
532 if (option->box_item)
533 done_listbox_item(&option_browser, option->box_item);
535 if (option->flags & OPT_ALLOC) {
536 mem_free_if(option->name);
537 mem_free(option);
538 } else if (!option->capt) {
539 /* We are probably dealing with a built-in autocreated option
540 * that will be attempted to be deleted when shutting down. */
541 /* Clear it so nothing will be done later. */
542 memset(option, 0, sizeof(*option));
546 /* The namespace may start to seem a bit chaotic here; it indeed is, maybe the
547 * function names above should be renamed and only macros should keep their old
548 * short names. */
549 /* The simple rule I took as an apologize is that functions which take already
550 * completely filled (struct option *) have long name and functions which take
551 * only option specs have short name. */
553 static void
554 delete_option_do(struct option *option, int recursive)
556 if (option->next) {
557 del_from_list(option);
558 option->prev = option->next = NULL;
561 if (recursive == -1) {
562 ERROR("Orphaned option %s", option->name);
565 if (option->type == OPT_TREE && option->value.tree
566 && !list_empty(*option->value.tree)) {
567 if (!recursive) {
568 if (option->flags & OPT_AUTOCREATE) {
569 recursive = 1;
570 } else {
571 ERROR("Orphaned unregistered "
572 "option in subtree %s!",
573 option->name);
574 recursive = -1;
577 free_options_tree(option->value.tree, recursive);
580 done_option(option);
583 void
584 mark_option_as_deleted(struct option *option)
586 if (option->type == OPT_TREE) {
587 struct option *unmarked;
589 assert(option->value.tree);
591 foreach (unmarked, *option->value.tree)
592 mark_option_as_deleted(unmarked);
595 option->box_item->visible = 0;
597 option->flags |= (OPT_TOUCHED | OPT_DELETED);
600 void
601 delete_option(struct option *option)
603 delete_option_do(option, 1);
606 struct option *
607 copy_option(struct option *template)
609 struct option *option = mem_calloc(1, sizeof(*option));
611 if (!option) return NULL;
613 option->name = null_or_stracpy(template->name);
614 option->flags = (template->flags | OPT_ALLOC);
615 option->type = template->type;
616 option->min = template->min;
617 option->max = template->max;
618 option->capt = template->capt;
619 option->desc = template->desc;
620 option->change_hook = template->change_hook;
622 option->box_item = init_option_listbox_item(option);
623 if (option->box_item) {
624 if (template->box_item) {
625 option->box_item->type = template->box_item->type;
626 option->box_item->depth = template->box_item->depth;
630 if (option_types[template->type].dup) {
631 option_types[template->type].dup(option, template);
632 } else {
633 option->value = template->value;
636 return option;
639 struct list_head *
640 init_options_tree(void)
642 struct list_head *ptr = mem_alloc(sizeof(*ptr));
644 if (ptr) init_list(*ptr);
645 return ptr;
648 /* Some default pre-autocreated options. Doh. */
649 static inline void
650 register_autocreated_options(void)
652 /* TODO: Use table-driven initialization. --jonas */
653 get_opt_int("terminal.linux.type") = 2;
654 get_opt_int("terminal.linux.colors") = 1;
655 get_opt_bool("terminal.linux.m11_hack") = 1;
656 get_opt_int("terminal.vt100.type") = 1;
657 get_opt_int("terminal.vt110.type") = 1;
658 get_opt_int("terminal.xterm.type") = 1;
659 get_opt_bool("terminal.xterm.underline") = 1;
660 get_opt_int("terminal.xterm-color.type") = 1;
661 get_opt_int("terminal.xterm-color.colors") = COLOR_MODE_16;
662 get_opt_bool("terminal.xterm-color.underline") = 1;
663 #ifdef CONFIG_88_COLORS
664 get_opt_int("terminal.xterm-88color.type") = 1;
665 get_opt_int("terminal.xterm-88color.colors") = COLOR_MODE_88;
666 get_opt_bool("terminal.xterm-88color.underline") = 1;
667 #endif
668 #ifdef CONFIG_256_COLORS
669 get_opt_int("terminal.xterm-256color.type") = 1;
670 get_opt_int("terminal.xterm-256color.colors") = COLOR_MODE_256;
671 get_opt_bool("terminal.xterm-256color.underline") = 1;
672 #endif
675 static struct option_info config_options_info[];
676 extern struct option_info cmdline_options_info[];
677 static struct change_hook_info change_hooks[];
679 void
680 init_options(void)
682 cmdline_options = add_opt_tree_tree(&options_root, "", "",
683 "cmdline", 0, "");
684 register_options(cmdline_options_info, cmdline_options);
686 config_options = add_opt_tree_tree(&options_root, "", "",
687 "config", OPT_SORT, "");
688 config_options->flags |= OPT_LISTBOX;
689 config_options->box_item = &option_browser.root;
690 register_options(config_options_info, config_options);
692 register_autocreated_options();
693 register_change_hooks(change_hooks);
696 static void
697 free_options_tree(struct list_head *tree, int recursive)
699 while (!list_empty(*tree))
700 delete_option_do(tree->next, recursive);
703 void
704 done_options(void)
706 unregister_options(config_options_info, config_options);
707 unregister_options(cmdline_options_info, cmdline_options);
708 config_options->box_item = NULL;
709 free_options_tree(&options_root_tree, 0);
712 void
713 register_change_hooks(struct change_hook_info *change_hooks)
715 int i;
717 for (i = 0; change_hooks[i].name; i++) {
718 struct option *option = get_opt_rec(config_options,
719 change_hooks[i].name);
721 assert(option);
722 option->change_hook = change_hooks[i].change_hook;
726 void
727 unmark_options_tree(struct list_head *tree)
729 struct option *option;
731 foreach (option, *tree) {
732 option->flags &= ~OPT_WATERMARK;
733 if (option->type == OPT_TREE)
734 unmark_options_tree(option->value.tree);
738 void
739 watermark_deleted_options(struct list_head *tree)
741 struct option *option;
743 foreach (option, *tree) {
744 if (option->flags & OPT_DELETED)
745 option->flags |= OPT_WATERMARK;
746 else if (option->type == OPT_TREE)
747 watermark_deleted_options(option->value.tree);
751 static int
752 check_nonempty_tree(struct list_head *options)
754 struct option *opt;
756 foreach (opt, *options) {
757 if (opt->type == OPT_TREE) {
758 if (check_nonempty_tree(opt->value.tree))
759 return 1;
760 } else if (!(opt->flags & OPT_WATERMARK)) {
761 return 1;
765 return 0;
768 void
769 smart_config_string(struct string *str, int print_comment, int i18n,
770 struct list_head *options, unsigned char *path, int depth,
771 void (*fn)(struct string *, struct option *,
772 unsigned char *, int, int, int, int))
774 struct option *option;
776 foreach (option, *options) {
777 int do_print_comment = 1;
779 if (option->flags & OPT_HIDDEN ||
780 option->flags & OPT_WATERMARK ||
781 option->type == OPT_ALIAS ||
782 !strcmp(option->name, "_template_"))
783 continue;
785 /* Is there anything to be printed anyway? */
786 if (option->type == OPT_TREE
787 && !check_nonempty_tree(option->value.tree))
788 continue;
790 /* We won't pop out the description when we're in autocreate
791 * category and not template. It'd be boring flood of
792 * repetitive comments otherwise ;). */
794 /* This print_comment parameter is weird. If it is negative, it
795 * means that we shouldn't print comments at all. If it is 1,
796 * we shouldn't print comment UNLESS the option is _template_
797 * or not-an-autocreating-tree (it is set for the first-level
798 * autocreation tree). When it is 2, we can print out comments
799 * normally. */
800 /* It is still broken somehow, as it didn't work for terminal.*
801 * (the first autocreated level) by the time I wrote this. Good
802 * summer job for bored mad hackers with spare boolean mental
803 * power. I have better things to think about, personally.
804 * Maybe we should just mark autocreated options somehow ;). */
805 if (!print_comment || (print_comment == 1
806 && (strcmp(option->name, "_template_")
807 && (option->flags & OPT_AUTOCREATE
808 && option->type == OPT_TREE))))
809 do_print_comment = 0;
811 /* Pop out the comment */
813 /* For config file, we ignore do_print_comment everywhere
814 * except 1, but sometimes we want to skip the option totally.
816 fn(str, option, path, depth,
817 option->type == OPT_TREE ? print_comment
818 : do_print_comment,
819 0, i18n);
821 fn(str, option, path, depth, do_print_comment, 1, i18n);
823 /* And the option itself */
825 if (option_types[option->type].write) {
826 fn(str, option, path, depth,
827 do_print_comment, 2, i18n);
829 } else if (option->type == OPT_TREE) {
830 struct string newpath;
831 int pc = print_comment;
833 if (!init_string(&newpath)) continue; /* OK? */
835 if (pc == 2 && option->flags & OPT_AUTOCREATE)
836 pc = 1;
837 else if (pc == 1 && strcmp(option->name, "_template_"))
838 pc = 0;
840 fn(str, option, path, depth, /*pc*/1, 3, i18n);
842 if (path) {
843 add_to_string(&newpath, path);
844 add_char_to_string(&newpath, '.');
846 add_to_string(&newpath, option->name);
847 smart_config_string(str, pc, i18n, option->value.tree,
848 newpath.source, depth + 1, fn);
849 done_string(&newpath);
851 fn(str, option, path, depth, /*pc*/1, 3, i18n);
854 /* TODO: We should maybe clear the touched flag only when really
855 * saving the stuff...? --pasky */
856 option->flags &= ~OPT_TOUCHED;
861 static int
862 change_hook_cache(struct session *ses, struct option *current, struct option *changed)
864 shrink_memory(0);
865 return 0;
868 static int
869 change_hook_connection(struct session *ses, struct option *current, struct option *changed)
871 register_check_queue();
872 return 0;
875 static int
876 change_hook_html(struct session *ses, struct option *current, struct option *changed)
878 foreach (ses, sessions) ses->tab->resize = 1;
880 return 0;
883 static int
884 change_hook_insert_mode(struct session *ses, struct option *current, struct option *changed)
886 update_status();
887 return 0;
890 static int
891 change_hook_active_link(struct session *ses, struct option *current, struct option *changed)
893 update_cached_document_options();
894 return 0;
897 static int
898 change_hook_terminal(struct session *ses, struct option *current, struct option *changed)
900 cls_redraw_all_terminals();
901 return 0;
904 static int
905 change_hook_ui(struct session *ses, struct option *current, struct option *changed)
907 update_status();
908 return 0;
911 /* Bit 2 of show means we should always set visibility, otherwise we set it
912 * only on templates. */
913 static void
914 update_visibility(struct list_head *tree, int show)
916 struct option *opt;
918 foreach (opt, *tree) {
919 if (opt->flags & OPT_DELETED) continue;
921 if (!strcmp(opt->name, "_template_")) {
922 if (opt->box_item)
923 opt->box_item->visible = (show & 1);
925 if (opt->type == OPT_TREE)
926 update_visibility(opt->value.tree, show | 2);
927 } else {
928 if (opt->box_item && (show & 2))
929 opt->box_item->visible = (show & 1);
931 if (opt->type == OPT_TREE)
932 update_visibility(opt->value.tree, show);
937 void
938 update_options_visibility(void)
940 update_visibility(config_options->value.tree,
941 get_opt_bool("config.show_template"));
944 void
945 toggle_option(struct session *ses, struct option *option)
947 long number = option->value.number + 1;
949 assert(option->type == OPT_BOOL || option->type == OPT_INT);
950 assert(option->max);
952 /* TODO: call change hooks. --jonas */
953 option->value.number = (number <= option->max) ? number : option->min;
954 option_changed(ses, option, option);
957 static int
958 change_hook_stemplate(struct session *ses, struct option *current, struct option *changed)
960 update_visibility(config_options->value.tree, changed->value.number);
961 return 0;
964 static int
965 change_hook_language(struct session *ses, struct option *current, struct option *changed)
967 #ifdef CONFIG_NLS
968 set_language(changed->value.number);
969 #endif
970 return 0;
973 static struct change_hook_info change_hooks[] = {
974 { "config.show_template", change_hook_stemplate },
975 { "connection", change_hook_connection },
976 { "document.browse", change_hook_html },
977 { "document.browse.forms.insert_mode",
978 change_hook_insert_mode },
979 { "document.browse.links.active_link",
980 change_hook_active_link },
981 { "document.cache", change_hook_cache },
982 { "document.codepage", change_hook_html },
983 { "document.colors", change_hook_html },
984 { "document.html", change_hook_html },
985 { "document.plain", change_hook_html },
986 { "terminal", change_hook_terminal },
987 { "ui.language", change_hook_language },
988 { "ui", change_hook_ui },
989 { NULL, NULL },
992 void
993 call_change_hooks(struct session *ses, struct option *current, struct option *option)
995 /* This boolean thing can look a little weird - it
996 * basically says that we should proceed when there's
997 * no change_hook or there's one and its return value
998 * was zero. */
999 while (current && (!current->change_hook ||
1000 !current->change_hook(ses, current, option))) {
1001 if (!current->root)
1002 break;
1004 current = current->root;
1008 void
1009 option_changed(struct session *ses, struct option *current, struct option *option)
1011 option->flags |= OPT_TOUCHED;
1012 /* Notify everyone out there! */
1013 call_change_hooks(ses, current, option);
1017 commit_option_values(struct option_resolver *resolvers,
1018 struct option *root, union option_value *values, int size)
1020 int touched = 0;
1021 int i;
1023 assert(resolvers && root && values && size);
1025 for (i = 0; i < size; i++) {
1026 unsigned char *name = resolvers[i].name;
1027 struct option *option = get_opt_rec(root, name);
1028 int id = resolvers[i].id;
1030 if (memcmp(&option->value, &values[id], sizeof(union option_value))) {
1031 option->value = values[id];
1032 option->flags |= OPT_TOUCHED;
1033 /* Speed hack: Directly call the change-hook for each
1034 * option in resolvers and later call call_change_hooks
1035 * on the root; if we were to call call_change_hooks
1036 * on each option in resolvers, we would end up calling
1037 * the change-hooks of root, its parents, its
1038 * grandparents, and so on for each option in resolvers
1039 * because call_change_hooks is recursive. -- Miciah */
1040 if (option->change_hook)
1041 option->change_hook(NULL, option, NULL);
1042 touched++;
1046 /* See above 'Speed hack' comment. */
1047 call_change_hooks(NULL, root, NULL);
1049 return touched;
1052 void
1053 checkout_option_values(struct option_resolver *resolvers,
1054 struct option *root,
1055 union option_value *values, int size)
1057 int i;
1059 for (i = 0; i < size; i++) {
1060 unsigned char *name = resolvers[i].name;
1061 struct option *option = get_opt_rec(root, name);
1062 int id = resolvers[i].id;
1064 values[id] = option->value;
1068 /**********************************************************************
1069 Options values
1070 **********************************************************************/
1072 #include "config/options.inc"
1074 void
1075 register_options(struct option_info info[], struct option *tree)
1077 int i;
1079 for (i = 0; info[i].path; i++) {
1080 struct option *option = &info[i].option;
1081 unsigned char *string;
1083 debug_check_option_syntax(option);
1085 if (option->type != OPT_ALIAS
1086 && ((tree->flags & OPT_LISTBOX)
1087 || (option->flags & OPT_LISTBOX))) {
1088 option->box_item = init_option_listbox_item(option);
1089 if (!option->box_item) {
1090 delete_option(option);
1091 continue;
1095 switch (option->type) {
1096 case OPT_TREE:
1097 option->value.tree = init_options_tree();
1098 if (!option->value.tree) {
1099 delete_option(option);
1100 continue;
1102 break;
1103 case OPT_STRING:
1104 string = mem_alloc(MAX_STR_LEN);
1105 if (!string) {
1106 delete_option(option);
1107 continue;
1109 safe_strncpy(string, option->value.string, MAX_STR_LEN);
1110 option->value.string = string;
1111 break;
1112 case OPT_COLOR:
1113 string = option->value.string;
1114 assert(string);
1115 decode_color(string, strlen(string),
1116 &option->value.color);
1117 break;
1118 case OPT_CODEPAGE:
1119 string = option->value.string;
1120 assert(string);
1121 option->value.number = get_cp_index(string);
1122 break;
1123 case OPT_BOOL:
1124 case OPT_INT:
1125 case OPT_LONG:
1126 case OPT_LANGUAGE:
1127 case OPT_COMMAND:
1128 case OPT_ALIAS:
1129 break;
1132 add_opt_rec(tree, info[i].path, option);
1136 void
1137 unregister_options(struct option_info info[], struct option *tree)
1139 int i = 0;
1141 /* We need to remove the options in inverse order to the order how we
1142 * added them. */
1144 while (info[i].path) i++;
1146 for (i--; i >= 0; i--)
1147 delete_option_do(&info[i].option, 0);