add sane defaults for refered based on browser_mode
[xombrero.git] / settings.c
blobf987c260e9808340244453c2be35ef16f029996d
1 /*
2 * Copyright (c) 2010, 2011 Marco Peereboom <marco@peereboom.us>
3 * Copyright (c) 2011 Stevan Andjelkovic <stevan@student.chalmers.se>
4 * Copyright (c) 2010, 2011 Edd Barrett <vext01@gmail.com>
5 * Copyright (c) 2011 Todd T. Fries <todd@fries.net>
6 * Copyright (c) 2011 Raphael Graf <r@undefined.ch>
7 * Copyright (c) 2011 Michal Mazurek <akfaew@jasminek.net>
9 * Permission to use, copy, modify, and distribute this software for any
10 * purpose with or without fee is hereby granted, provided that the above
11 * copyright notice and this permission notice appear in all copies.
13 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
14 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
15 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
16 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
17 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
18 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
19 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
22 #include "xxxterm.h"
24 /* globals */
25 SoupURI *proxy_uri = NULL;
26 PangoFontDescription *cmd_font;
27 PangoFontDescription *oops_font;
28 PangoFontDescription *statusbar_font;
29 PangoFontDescription *tabbar_font;
31 /* settings that require restart */
32 int tabless = 0; /* allow only 1 tab */
33 int enable_socket = 0;
34 int single_instance = 0; /* only allow one xxxterm to run */
35 int fancy_bar = 1; /* fancy toolbar */
36 int browser_mode = XT_BM_NORMAL;
37 int gui_mode = XT_GM_CLASSIC;
38 int enable_localstorage = 1;
39 char *statusbar_elems = NULL;
41 /* runtime settings */
42 int show_tabs = 1; /* show tabs on notebook */
43 int tab_style = XT_TABS_NORMAL; /* tab bar style */
44 int show_url = 1; /* show url toolbar on notebook */
45 int show_statusbar = 0; /* vimperator style status bar */
46 int ctrl_click_focus = 0; /* ctrl click gets focus */
47 int cookies_enabled = 1; /* enable cookies */
48 int read_only_cookies = 0; /* enable to not write cookies */
49 int enable_scripts = 1;
50 int enable_plugins = 1;
51 gfloat default_zoom_level = 1.0;
52 char default_script[PATH_MAX];
53 int window_height = 768;
54 int window_width = 1024;
55 int window_maximize = 0;
56 int icon_size = 2; /* 1 = smallest, 2+ = bigger */
57 int refresh_interval = 10; /* download refresh interval */
58 int enable_plugin_whitelist = 0;
59 int enable_cookie_whitelist = 0;
60 int enable_js_whitelist = 0;
61 int session_timeout = 3600; /* cookie session timeout */
62 int cookie_policy = SOUP_COOKIE_JAR_ACCEPT_ALWAYS;
63 char *ssl_ca_file = NULL;
64 char *resource_dir = NULL;
65 gboolean ssl_strict_certs = FALSE;
66 int append_next = 1; /* append tab after current tab */
67 char *home = NULL;
68 char *search_string = NULL;
69 char *http_proxy = NULL;
70 char download_dir[PATH_MAX];
71 int download_mode = XT_DM_START;
72 char runtime_settings[PATH_MAX]; /* override of settings */
73 int allow_volatile_cookies = 0;
74 int color_visited_uris = 1;
75 int save_global_history = 0; /* save global history to disk */
76 struct user_agent *user_agent = NULL;
77 int save_rejected_cookies = 0;
78 int session_autosave = 0;
79 int guess_search = 0;
80 int dns_prefetch = FALSE;
81 gint max_connections = 25;
82 gint max_host_connections = 5;
83 gint enable_spell_checking = 0;
84 char *spell_check_languages = NULL;
85 int xterm_workaround = 0;
86 char *url_regex = NULL;
87 int history_autosave = 0;
88 char search_file[PATH_MAX];
89 char command_file[PATH_MAX];
90 char *encoding = NULL;
91 int autofocus_onload = 0;
92 int js_autorun_enabled = 1;
93 int edit_mode = XT_EM_HYBRID;
94 int userstyle_global = 0;
95 int auto_load_images = 1;
96 int enable_autoscroll = 0;
97 int enable_favicon_entry = 1;
98 int enable_favicon_tabs = 0;
99 char *external_editor = NULL;
100 int referer_mode = XT_REFERER_ALWAYS;
101 char *referer_custom = NULL;
103 char *cmd_font_name = NULL;
104 char *oops_font_name = NULL;
105 char *statusbar_font_name = NULL;
106 char *tabbar_font_name = NULL;
108 char *get_download_dir(struct settings *);
109 char *get_default_script(struct settings *);
110 char *get_runtime_dir(struct settings *);
111 char *get_tab_style(struct settings *);
112 char *get_edit_mode(struct settings *);
113 char *get_download_mode(struct settings *);
114 char *get_work_dir(struct settings *);
115 char *get_referer(struct settings *);
117 int add_cookie_wl(struct settings *, char *);
118 int add_js_wl(struct settings *, char *);
119 int add_pl_wl(struct settings *, char *);
120 int add_mime_type(struct settings *, char *);
121 int add_alias(struct settings *, char *);
122 int add_kb(struct settings *, char *);
123 int add_ua(struct settings *, char *);
125 int set_download_dir(struct settings *, char *);
126 int set_default_script(struct settings *, char *);
127 int set_runtime_dir(struct settings *, char *);
128 int set_tab_style(struct settings *, char *);
129 int set_edit_mode(struct settings *, char *);
130 int set_work_dir(struct settings *, char *);
131 int set_auto_load_images(char *value);
132 int set_enable_autoscroll(char *value);
133 int set_enable_favicon_entry(char *value);
134 int set_enable_favicon_tabs(char *value);
135 int set_download_mode(struct settings *, char *);
136 int set_download_mode_rt(char *);
137 int set_referer(struct settings *, char *);
138 int set_referer_rt(char *);
139 int set_external_editor(char *);
141 void walk_mime_type(struct settings *, void (*)(struct settings *,
142 char *, void *), void *);
143 void walk_alias(struct settings *, void (*)(struct settings *,
144 char *, void *), void *);
145 void walk_cookie_wl(struct settings *, void (*)(struct settings *,
146 char *, void *), void *);
147 void walk_js_wl(struct settings *, void (*)(struct settings *,
148 char *, void *), void *);
149 void walk_pl_wl(struct settings *, void (*)(struct settings *,
150 char *, void *), void *);
151 void walk_kb(struct settings *, void (*)(struct settings *, char *,
152 void *), void *);
153 void walk_ua(struct settings *, void (*)(struct settings *, char *,
154 void *), void *);
157 set_http_proxy(char *proxy)
159 SoupURI *uri;
161 if (proxy == NULL)
162 return (1);
164 /* see if we need to clear it instead */
165 if (strlen(proxy) == 0) {
166 setup_proxy(NULL);
167 return (0);
170 uri = soup_uri_new(proxy);
171 if (uri == NULL || !SOUP_URI_VALID_FOR_HTTP(uri))
172 return (1);
174 setup_proxy(proxy);
176 soup_uri_free(uri);
178 return (0);
181 struct special {
182 int (*set)(struct settings *, char *);
183 char *(*get)(struct settings *);
184 void (*walk)(struct settings *,
185 void (*cb)(struct settings *, char *, void *),
186 void *);
189 struct special s_browser_mode = {
190 set_browser_mode,
191 get_browser_mode,
192 NULL
195 struct special s_gui_mode = {
196 set_gui_mode,
197 get_gui_mode,
198 NULL
201 struct special s_cookie = {
202 set_cookie_policy,
203 get_cookie_policy,
204 NULL
207 struct special s_alias = {
208 add_alias,
209 NULL,
210 walk_alias
213 struct special s_mime = {
214 add_mime_type,
215 NULL,
216 walk_mime_type
219 struct special s_js = {
220 add_js_wl,
221 NULL,
222 walk_js_wl
225 struct special s_pl = {
226 add_pl_wl,
227 NULL,
228 walk_pl_wl
231 struct special s_kb = {
232 add_kb,
233 NULL,
234 walk_kb
237 struct special s_cookie_wl = {
238 add_cookie_wl,
239 NULL,
240 walk_cookie_wl
243 struct special s_default_script = {
244 set_default_script,
245 get_default_script,
246 NULL
249 struct special s_download_dir = {
250 set_download_dir,
251 get_download_dir,
252 NULL
255 struct special s_work_dir = {
256 set_work_dir,
257 get_work_dir,
258 NULL
261 struct special s_tab_style = {
262 set_tab_style,
263 get_tab_style,
264 NULL
267 struct special s_edit_mode = {
268 set_edit_mode,
269 get_edit_mode,
270 NULL
273 struct special s_download_mode = {
274 set_download_mode,
275 get_download_mode,
276 NULL
279 struct special s_ua = {
280 add_ua,
281 NULL,
282 walk_ua
285 struct special s_referer = {
286 set_referer,
287 get_referer,
288 NULL
291 struct settings rs[] = {
292 { "allow_volatile_cookies", XT_S_INT, 0, &allow_volatile_cookies, NULL, NULL },
293 { "append_next", XT_S_INT, 0, &append_next, NULL, NULL },
294 { "autofocus_onload", XT_S_INT, 0, &autofocus_onload, NULL, NULL },
295 { "browser_mode", XT_S_INT, 0, NULL, NULL,&s_browser_mode },
296 { "gui_mode", XT_S_INT, 0, NULL, NULL,&s_gui_mode },
297 { "color_visited_uris", XT_S_INT, 0, &color_visited_uris, NULL, NULL },
298 { "cookie_policy", XT_S_INT, 0, NULL, NULL,&s_cookie },
299 { "cookies_enabled", XT_S_INT, 0, &cookies_enabled, NULL, NULL },
300 { "ctrl_click_focus", XT_S_INT, 0, &ctrl_click_focus, NULL, NULL },
301 { "default_zoom_level", XT_S_FLOAT, 0, NULL, NULL, NULL, &default_zoom_level },
302 { "default_script", XT_S_STR, 0, NULL, NULL,&s_default_script },
303 { "download_dir", XT_S_STR, 0, NULL, NULL,&s_download_dir },
304 { "download_mode", XT_S_STR, 0, NULL, NULL,&s_download_mode, NULL, set_download_mode_rt },
305 { "edit_mode", XT_S_STR, 0, NULL, NULL,&s_edit_mode },
306 { "enable_cookie_whitelist", XT_S_INT, 0, &enable_cookie_whitelist, NULL, NULL },
307 { "enable_js_whitelist", XT_S_INT, 0, &enable_js_whitelist, NULL, NULL },
308 { "enable_plugin_whitelist", XT_S_INT, 0, &enable_plugin_whitelist, NULL, NULL },
309 { "enable_localstorage", XT_S_INT, 0, &enable_localstorage, NULL, NULL },
310 { "enable_plugins", XT_S_INT, 0, &enable_plugins, NULL, NULL },
311 { "enable_scripts", XT_S_INT, 0, &enable_scripts, NULL, NULL },
312 { "enable_socket", XT_S_INT, XT_SF_RESTART,&enable_socket, NULL, NULL },
313 { "enable_spell_checking", XT_S_INT, 0, &enable_spell_checking, NULL, NULL },
314 { "encoding", XT_S_STR, 0, NULL, &encoding, NULL },
315 { "external_editor", XT_S_STR,0, NULL, &external_editor, NULL, NULL, set_external_editor },
316 { "fancy_bar", XT_S_INT, XT_SF_RESTART,&fancy_bar, NULL, NULL },
317 { "guess_search", XT_S_INT, 0, &guess_search, NULL, NULL },
318 { "history_autosave", XT_S_INT, 0, &history_autosave, NULL, NULL },
319 { "home", XT_S_STR, 0, NULL, &home, NULL },
320 { "http_proxy", XT_S_STR, 0, NULL, &http_proxy, NULL, NULL, set_http_proxy },
321 { "icon_size", XT_S_INT, 0, &icon_size, NULL, NULL },
322 { "js_autorun_enabled", XT_S_INT, 0, &js_autorun_enabled, NULL, NULL },
323 { "max_connections", XT_S_INT, XT_SF_RESTART,&max_connections, NULL, NULL },
324 { "max_host_connections", XT_S_INT, XT_SF_RESTART,&max_host_connections, NULL, NULL },
325 { "read_only_cookies", XT_S_INT, 0, &read_only_cookies, NULL, NULL },
326 { "refresh_interval", XT_S_INT, 0, &refresh_interval, NULL, NULL },
327 { "resource_dir", XT_S_STR, 0, NULL, &resource_dir, NULL },
328 { "search_string", XT_S_STR, 0, NULL, &search_string, NULL },
329 { "save_global_history", XT_S_INT, XT_SF_RESTART,&save_global_history, NULL, NULL },
330 { "save_rejected_cookies", XT_S_INT, XT_SF_RESTART,&save_rejected_cookies, NULL, NULL },
331 { "session_timeout", XT_S_INT, 0, &session_timeout, NULL, NULL },
332 { "session_autosave", XT_S_INT, 0, &session_autosave, NULL, NULL },
333 { "single_instance", XT_S_INT, XT_SF_RESTART,&single_instance, NULL, NULL },
334 { "show_tabs", XT_S_INT, 0, &show_tabs, NULL, NULL },
335 { "show_url", XT_S_INT, 0, &show_url, NULL, NULL },
336 { "show_statusbar", XT_S_INT, 0, &show_statusbar, NULL, NULL },
337 { "spell_check_languages", XT_S_STR, 0, NULL, &spell_check_languages, NULL },
338 { "ssl_ca_file", XT_S_STR, 0, NULL, &ssl_ca_file, NULL },
339 { "ssl_strict_certs", XT_S_INT, 0, &ssl_strict_certs, NULL, NULL },
340 { "statusbar_elems", XT_S_STR, 0, NULL, &statusbar_elems, NULL },
341 { "tab_style", XT_S_STR, 0, NULL, NULL,&s_tab_style },
342 { "url_regex", XT_S_STR, 0, NULL, &url_regex, NULL },
343 { "window_height", XT_S_INT, 0, &window_height, NULL, NULL },
344 { "window_width", XT_S_INT, 0, &window_width, NULL, NULL },
345 { "window_maximize", XT_S_INT, 0, &window_maximize, NULL, NULL },
346 { "work_dir", XT_S_STR, 0, NULL, NULL,&s_work_dir },
347 { "xterm_workaround", XT_S_INT, 0, &xterm_workaround, NULL, NULL },
348 { "auto_load_images", XT_S_INT, 0, &auto_load_images, NULL, NULL, NULL, set_auto_load_images },
349 { "enable_autoscroll", XT_S_INT, 0, &enable_autoscroll, NULL, NULL, NULL, set_enable_autoscroll },
350 { "enable_favicon_entry", XT_S_INT, 0, &enable_favicon_entry, NULL, NULL, NULL, set_enable_favicon_entry },
351 { "enable_favicon_tabs", XT_S_INT, 0, &enable_favicon_tabs, NULL, NULL, NULL, set_enable_favicon_tabs },
352 { "referer", XT_S_STR, 0, NULL, NULL,&s_referer, NULL, set_referer_rt },
354 /* font settings */
355 { "cmd_font", XT_S_STR, 0, NULL, &cmd_font_name, NULL },
356 { "oops_font", XT_S_STR, 0, NULL, &oops_font_name, NULL },
357 { "statusbar_font", XT_S_STR, 0, NULL, &statusbar_font_name, NULL },
358 { "tabbar_font", XT_S_STR, 0, NULL, &tabbar_font_name, NULL },
360 /* runtime settings */
361 { "alias", XT_S_STR, XT_SF_RUNTIME, NULL, NULL, &s_alias },
362 { "cookie_wl", XT_S_STR, XT_SF_RUNTIME, NULL, NULL, &s_cookie_wl },
363 { "js_wl", XT_S_STR, XT_SF_RUNTIME, NULL, NULL, &s_js },
364 { "keybinding", XT_S_STR, XT_SF_RUNTIME, NULL, NULL, &s_kb },
365 { "mime_type", XT_S_STR, XT_SF_RUNTIME, NULL, NULL, &s_mime },
366 { "pl_wl", XT_S_STR, XT_SF_RUNTIME, NULL, NULL, &s_pl },
367 { "user_agent", XT_S_STR, XT_SF_RUNTIME, NULL, NULL, &s_ua },
370 size_t
371 get_settings_size(void)
373 return (LENGTH(rs));
376 char *
377 get_setting_name(int i)
379 if (i > LENGTH(rs))
380 return (NULL);
381 return (rs[i].name);
384 char *
385 get_as_string(struct settings *s)
387 char *r = NULL;
389 if (s == NULL)
390 return (NULL);
392 if (s->s) {
393 if (s->s->get)
394 r = s->s->get(s);
395 else
396 warnx("get_as_string skip %s\n", s->name);
397 } else if (s->type == XT_S_INT)
398 r = g_strdup_printf("%d", *s->ival);
399 else if (s->type == XT_S_STR)
400 r = g_strdup(*s->sval);
401 else if (s->type == XT_S_FLOAT)
402 r = g_strdup_printf("%f", *s->fval);
403 else
404 r = g_strdup_printf("INVALID TYPE");
406 return (r);
409 void
410 settings_walk(void (*cb)(struct settings *, char *, void *), void *cb_args)
412 int i;
413 char *s;
415 for (i = 0; i < LENGTH(rs); i++) {
416 if (rs[i].s && rs[i].s->walk)
417 rs[i].s->walk(&rs[i], cb, cb_args);
418 else {
419 s = get_as_string(&rs[i]);
420 cb(&rs[i], s, cb_args);
421 g_free(s);
427 set_browser_mode(struct settings *s, char *val)
429 if (!strcmp(val, "whitelist")) {
430 browser_mode = XT_BM_WHITELIST;
431 allow_volatile_cookies = 0;
432 cookie_policy = SOUP_COOKIE_JAR_ACCEPT_NO_THIRD_PARTY;
433 cookies_enabled = 1;
434 enable_cookie_whitelist = 1;
435 enable_plugin_whitelist = 1;
436 enable_plugins = 0;
437 read_only_cookies = 0;
438 save_rejected_cookies = 0;
439 session_timeout = 3600;
440 enable_scripts = 0;
441 enable_js_whitelist = 1;
442 enable_localstorage = 0;
443 referer_mode = XT_REFERER_SAME_DOMAIN;
444 } else if (!strcmp(val, "normal")) {
445 browser_mode = XT_BM_NORMAL;
446 allow_volatile_cookies = 0;
447 cookie_policy = SOUP_COOKIE_JAR_ACCEPT_ALWAYS;
448 cookies_enabled = 1;
449 enable_cookie_whitelist = 0;
450 enable_plugin_whitelist = 0;
451 enable_plugins = 1;
452 read_only_cookies = 0;
453 save_rejected_cookies = 0;
454 session_timeout = 3600;
455 enable_scripts = 1;
456 enable_js_whitelist = 0;
457 enable_localstorage = 1;
458 referer_mode = XT_REFERER_ALWAYS;
459 } else if (!strcmp(val, "kiosk")) {
460 browser_mode = XT_BM_KIOSK;
461 allow_volatile_cookies = 0;
462 cookie_policy = SOUP_COOKIE_JAR_ACCEPT_ALWAYS;
463 cookies_enabled = 1;
464 enable_cookie_whitelist = 0;
465 enable_plugin_whitelist = 0;
466 enable_plugins = 1;
467 read_only_cookies = 0;
468 save_rejected_cookies = 0;
469 session_timeout = 3600;
470 enable_scripts = 1;
471 enable_js_whitelist = 0;
472 enable_localstorage = 1;
473 referer_mode = XT_REFERER_ALWAYS;
474 show_tabs = 0;
475 tabless = 1;
476 } else
477 return (1);
479 return (0);
482 char *
483 get_browser_mode(struct settings *s)
485 char *r = NULL;
487 if (browser_mode == XT_BM_WHITELIST)
488 r = g_strdup("whitelist");
489 else if (browser_mode == XT_BM_NORMAL)
490 r = g_strdup("normal");
491 else if (browser_mode == XT_BM_KIOSK)
492 r = g_strdup("kiosk");
493 else
494 return (NULL);
496 return (r);
500 set_gui_mode(struct settings *s, char *val)
502 if (!strcmp(val, "classic")) {
503 fancy_bar = 1;
504 show_tabs = 1;
505 tab_style = XT_TABS_NORMAL;
506 show_url = 1;
507 show_statusbar = 0;
508 } else if (!strcmp(val, "minimal")) {
509 fancy_bar = 0;
510 show_tabs = 1;
511 tab_style = XT_TABS_COMPACT;
512 show_url = 0;
513 show_statusbar = 1;
514 } else
515 return (1);
517 return (0);
520 char *
521 get_gui_mode(struct settings *s)
523 char *r = NULL;
525 if (gui_mode == XT_GM_CLASSIC)
526 r = g_strdup("classic");
527 else if (browser_mode == XT_GM_MINIMAL)
528 r = g_strdup("minimal");
529 else
530 return (NULL);
532 return (r);
536 set_cookie_policy(struct settings *s, char *val)
538 if (!strcmp(val, "no3rdparty"))
539 cookie_policy = SOUP_COOKIE_JAR_ACCEPT_NO_THIRD_PARTY;
540 else if (!strcmp(val, "accept"))
541 cookie_policy = SOUP_COOKIE_JAR_ACCEPT_ALWAYS;
542 else if (!strcmp(val, "reject"))
543 cookie_policy = SOUP_COOKIE_JAR_ACCEPT_NEVER;
544 else
545 return (1);
547 return (0);
550 char *
551 get_cookie_policy(struct settings *s)
553 char *r = NULL;
555 if (cookie_policy == SOUP_COOKIE_JAR_ACCEPT_NO_THIRD_PARTY)
556 r = g_strdup("no3rdparty");
557 else if (cookie_policy == SOUP_COOKIE_JAR_ACCEPT_ALWAYS)
558 r = g_strdup("accept");
559 else if (cookie_policy == SOUP_COOKIE_JAR_ACCEPT_NEVER)
560 r = g_strdup("reject");
561 else
562 return (NULL);
564 return (r);
567 char *
568 get_default_script(struct settings *s)
570 if (default_script[0] == '\0')
571 return (0);
572 return (g_strdup(default_script));
576 set_default_script(struct settings *s, char *val)
578 if (val[0] == '~')
579 snprintf(default_script, sizeof default_script, "%s/%s",
580 pwd->pw_dir, &val[1]);
581 else
582 strlcpy(default_script, val, sizeof default_script);
584 return (0);
587 char *
588 get_download_dir(struct settings *s)
590 if (download_dir[0] == '\0')
591 return (0);
592 return (g_strdup(download_dir));
596 set_download_dir(struct settings *s, char *val)
598 if (val[0] == '~')
599 snprintf(download_dir, sizeof download_dir, "%s/%s",
600 pwd->pw_dir, &val[1]);
601 else
602 strlcpy(download_dir, val, sizeof download_dir);
604 return (0);
608 add_alias(struct settings *s, char *line)
610 char *l, *alias;
611 struct alias *a = NULL;
613 if (s == NULL || line == NULL) {
614 show_oops(NULL, "add_alias invalid parameters");
615 return (1);
618 l = line;
619 a = g_malloc(sizeof(*a));
621 if ((alias = strsep(&l, " \t,")) == NULL || l == NULL) {
622 show_oops(NULL, "add_alias: incomplete alias definition");
623 goto bad;
625 if (strlen(alias) == 0 || strlen(l) == 0) {
626 show_oops(NULL, "add_alias: invalid alias definition");
627 goto bad;
630 a->a_name = g_strdup(alias);
631 a->a_uri = g_strdup(l);
633 DNPRINTF(XT_D_CONFIG, "add_alias: %s for %s\n", a->a_name, a->a_uri);
635 TAILQ_INSERT_TAIL(&aliases, a, entry);
637 return (0);
638 bad:
639 if (a)
640 g_free(a);
641 return (1);
644 void
645 walk_alias(struct settings *s,
646 void (*cb)(struct settings *, char *, void *), void *cb_args)
648 struct alias *a;
649 char *str;
651 if (s == NULL || cb == NULL) {
652 show_oops(NULL, "walk_alias invalid parameters");
653 return;
656 TAILQ_FOREACH(a, &aliases, entry) {
657 str = g_strdup_printf("%s --> %s", a->a_name, a->a_uri);
658 cb(s, str, cb_args);
659 g_free(str);
664 add_mime_type(struct settings *s, char *line)
666 char *mime_type;
667 char *l;
668 struct mime_type *m = NULL;
669 int downloadfirst = 0;
671 /* XXX this could be smarter */
673 if (line == NULL || strlen(line) == 0) {
674 show_oops(NULL, "add_mime_type invalid parameters");
675 return (1);
678 l = line;
679 if (*l == '@') {
680 downloadfirst = 1;
681 l++;
683 m = g_malloc(sizeof(*m));
685 if ((mime_type = strsep(&l, " \t,")) == NULL || l == NULL) {
686 show_oops(NULL, "add_mime_type: invalid mime_type");
687 goto bad;
689 if (mime_type[strlen(mime_type) - 1] == '*') {
690 mime_type[strlen(mime_type) - 1] = '\0';
691 m->mt_default = 1;
692 } else
693 m->mt_default = 0;
695 if (strlen(mime_type) == 0 || strlen(l) == 0) {
696 show_oops(NULL, "add_mime_type: invalid mime_type");
697 goto bad;
700 m->mt_type = g_strdup(mime_type);
701 m->mt_action = g_strdup(l);
702 m->mt_download = downloadfirst;
704 DNPRINTF(XT_D_CONFIG, "add_mime_type: type %s action %s default %d\n",
705 m->mt_type, m->mt_action, m->mt_default);
707 TAILQ_INSERT_TAIL(&mtl, m, entry);
709 return (0);
710 bad:
711 if (m)
712 g_free(m);
713 return (1);
716 void
717 walk_mime_type(struct settings *s,
718 void (*cb)(struct settings *, char *, void *), void *cb_args)
720 struct mime_type *m;
721 char *str;
723 if (s == NULL || cb == NULL) {
724 show_oops(NULL, "walk_mime_type invalid parameters");
725 return;
728 TAILQ_FOREACH(m, &mtl, entry) {
729 str = g_strdup_printf("%s%s --> %s",
730 m->mt_type,
731 m->mt_default ? "*" : "",
732 m->mt_action);
733 cb(s, str, cb_args);
734 g_free(str);
738 /* inherent to GTK not all keys will be caught at all times */
739 /* XXX sort key bindings */
740 struct key_binding keys[] = {
741 { "command_mode", 0, 1, GDK_Escape },
742 { "insert_mode", 0, 0, GDK_i },
743 { "cookiejar", MOD1, 1, GDK_j },
744 { "downloadmgr", MOD1, 1, GDK_d },
745 { "history", MOD1, 1, GDK_h },
746 { "print", CTRL, 1, GDK_p },
747 { "search", 0, 0, GDK_slash },
748 { "searchb", 0, 0, GDK_question },
749 { "statustoggle", CTRL, 1, GDK_n },
750 { "command", 0, 0, GDK_colon },
751 { "qa", CTRL, 1, GDK_q },
752 { "restart", MOD1, 1, GDK_q },
753 { "js toggle", CTRL, 1, GDK_j },
754 { "plugin toggle", MOD1, 1, GDK_p },
755 { "cookie toggle", MOD1, 1, GDK_c },
756 { "togglesrc", CTRL, 1, GDK_s },
757 { "yankuri", 0, 0, GDK_y },
758 { "pasteuricur", 0, 0, GDK_p },
759 { "pasteurinew", 0, 0, GDK_P },
760 { "toplevel toggle", 0, 1, GDK_F4 },
761 { "help", 0, 1, GDK_F1 },
762 { "run_script", MOD1, 1, GDK_r },
763 { "proxy toggle", 0, 1, GDK_F2 },
764 { "editelement", CTRL, 1, GDK_i },
765 { "passthrough", CTRL, 1, GDK_z },
767 /* search */
768 { "searchnext", 0, 0, GDK_n },
769 { "searchprevious", 0, 0, GDK_N },
771 /* focus */
772 { "focusaddress", 0, 1, GDK_F6 },
773 { "focussearch", 0, 1, GDK_F7 },
775 /* hinting */
776 { "hinting", 0, 0, GDK_f },
777 { "hinting", 0, 0, GDK_period },
778 { "hinting_newtab", SHFT, 0, GDK_F },
779 { "hinting_newtab", 0, 0, GDK_comma },
781 /* custom stylesheet */
782 { "userstyle", 0, 0, GDK_s },
783 { "userstyle_global", SHFT, 0, GDK_S },
785 /* navigation */
786 { "goback", 0, 0, GDK_BackSpace },
787 { "goback", MOD1, 1, GDK_Left },
788 { "goforward", SHFT, 1, GDK_BackSpace },
789 { "goforward", MOD1, 1, GDK_Right },
790 { "reload", 0, 1, GDK_F5 },
791 { "reload", CTRL, 1, GDK_r },
792 { "reload", CTRL, 1, GDK_l },
793 { "favorites", MOD1, 1, GDK_f },
795 /* vertical movement */
796 { "scrolldown", 0, 0, GDK_j },
797 { "scrolldown", 0, 0, GDK_Down },
798 { "scrollup", 0, 0, GDK_Up },
799 { "scrollup", 0, 0, GDK_k },
800 { "scrollbottom", 0, 0, GDK_G },
801 { "scrollbottom", 0, 0, GDK_End },
802 { "scrolltop", 0, 0, GDK_Home },
803 { "scrollpagedown", 0, 0, GDK_space },
804 { "scrollpagedown", CTRL, 1, GDK_f },
805 { "scrollhalfdown", CTRL, 1, GDK_d },
806 { "scrollpagedown", 0, 0, GDK_Page_Down },
807 { "scrollpageup", 0, 0, GDK_Page_Up },
808 { "scrollpageup", CTRL, 1, GDK_b },
809 { "scrollhalfup", CTRL, 1, GDK_u },
810 /* horizontal movement */
811 { "scrollright", 0, 0, GDK_l },
812 { "scrollright", 0, 0, GDK_Right },
813 { "scrollleft", 0, 0, GDK_Left },
814 { "scrollleft", 0, 0, GDK_h },
815 { "scrollfarright", 0, 0, GDK_dollar },
816 { "scrollfarleft", 0, 0, GDK_0 },
818 /* tabs */
819 { "tabnew", CTRL, 1, GDK_t },
820 { "999tabnew", CTRL, 1, GDK_T },
821 { "tabclose", CTRL, 1, GDK_w },
822 { "tabundoclose", 0, 0, GDK_U },
823 { "tabnext 1", CTRL, 1, GDK_1 },
824 { "tabnext 2", CTRL, 1, GDK_2 },
825 { "tabnext 3", CTRL, 1, GDK_3 },
826 { "tabnext 4", CTRL, 1, GDK_4 },
827 { "tabnext 5", CTRL, 1, GDK_5 },
828 { "tabnext 6", CTRL, 1, GDK_6 },
829 { "tabnext 7", CTRL, 1, GDK_7 },
830 { "tabnext 8", CTRL, 1, GDK_8 },
831 { "tabnext 9", CTRL, 1, GDK_9 },
832 { "tabfirst", CTRL, 1, GDK_less },
833 { "tablast", CTRL, 1, GDK_greater },
834 { "tabprevious", CTRL, 1, GDK_Left },
835 { "tabnext", CTRL, 1, GDK_Right },
836 { "focusout", CTRL, 1, GDK_minus },
837 { "focusin", CTRL, 1, GDK_plus },
838 { "focusin", CTRL, 1, GDK_equal },
839 { "focusreset", CTRL, 1, GDK_0 },
841 /* command aliases (handy when -S flag is used) */
842 { "promptopen", 0, 1, GDK_F9 },
843 { "promptopencurrent", 0, 1, GDK_F10 },
844 { "prompttabnew", 0, 1, GDK_F11 },
845 { "prompttabnewcurrent",0, 1, GDK_F12 },
848 void
849 walk_kb(struct settings *s,
850 void (*cb)(struct settings *, char *, void *), void *cb_args)
852 struct key_binding *k;
853 char str[1024];
855 if (s == NULL || cb == NULL) {
856 show_oops(NULL, "walk_kb invalid parameters");
857 return;
860 TAILQ_FOREACH(k, &kbl, entry) {
861 if (k->cmd == NULL)
862 continue;
863 str[0] = '\0';
865 /* sanity */
866 if (gdk_keyval_name(k->key) == NULL)
867 continue;
869 strlcat(str, k->cmd, sizeof str);
870 strlcat(str, ",", sizeof str);
872 if (k->mask & GDK_SHIFT_MASK)
873 strlcat(str, "S-", sizeof str);
874 if (k->mask & GDK_CONTROL_MASK)
875 strlcat(str, "C-", sizeof str);
876 if (k->mask & GDK_MOD1_MASK)
877 strlcat(str, "M1-", sizeof str);
878 if (k->mask & GDK_MOD2_MASK)
879 strlcat(str, "M2-", sizeof str);
880 if (k->mask & GDK_MOD3_MASK)
881 strlcat(str, "M3-", sizeof str);
882 if (k->mask & GDK_MOD4_MASK)
883 strlcat(str, "M4-", sizeof str);
884 if (k->mask & GDK_MOD5_MASK)
885 strlcat(str, "M5-", sizeof str);
887 strlcat(str, gdk_keyval_name(k->key), sizeof str);
888 cb(s, str, cb_args);
892 void
893 init_keybindings(void)
895 int i;
896 struct key_binding *k;
898 for (i = 0; i < LENGTH(keys); i++) {
899 k = g_malloc0(sizeof *k);
900 k->cmd = keys[i].cmd;
901 k->mask = keys[i].mask;
902 k->use_in_entry = keys[i].use_in_entry;
903 k->key = keys[i].key;
904 TAILQ_INSERT_HEAD(&kbl, k, entry);
906 DNPRINTF(XT_D_KEYBINDING, "init_keybindings: added: %s\n",
907 k->cmd ? k->cmd : "unnamed key");
911 void
912 keybinding_clearall(void)
914 struct key_binding *k, *next;
916 for (k = TAILQ_FIRST(&kbl); k; k = next) {
917 next = TAILQ_NEXT(k, entry);
918 if (k->cmd == NULL)
919 continue;
921 DNPRINTF(XT_D_KEYBINDING, "keybinding_clearall: %s\n",
922 k->cmd ? k->cmd : "unnamed key");
923 TAILQ_REMOVE(&kbl, k, entry);
924 g_free(k);
929 keybinding_add(char *cmd, char *key, int use_in_entry)
931 struct key_binding *k;
932 guint keyval, mask = 0;
933 int i;
935 DNPRINTF(XT_D_KEYBINDING, "keybinding_add: %s %s\n", cmd, key);
937 /* Keys which are to be used in entry have been prefixed with an
938 * exclamation mark. */
939 if (use_in_entry)
940 key++;
942 /* find modifier keys */
943 if (strstr(key, "S-"))
944 mask |= GDK_SHIFT_MASK;
945 if (strstr(key, "C-"))
946 mask |= GDK_CONTROL_MASK;
947 if (strstr(key, "M1-"))
948 mask |= GDK_MOD1_MASK;
949 if (strstr(key, "M2-"))
950 mask |= GDK_MOD2_MASK;
951 if (strstr(key, "M3-"))
952 mask |= GDK_MOD3_MASK;
953 if (strstr(key, "M4-"))
954 mask |= GDK_MOD4_MASK;
955 if (strstr(key, "M5-"))
956 mask |= GDK_MOD5_MASK;
958 /* find keyname */
959 for (i = strlen(key) - 1; i > 0; i--)
960 if (key[i] == '-')
961 key = &key[i + 1];
963 /* validate keyname */
964 keyval = gdk_keyval_from_name(key);
965 if (keyval == GDK_VoidSymbol) {
966 warnx("invalid keybinding name %s", key);
967 return (1);
969 /* must run this test too, gtk+ doesn't handle 10 for example */
970 if (gdk_keyval_name(keyval) == NULL) {
971 warnx("invalid keybinding name %s", key);
972 return (1);
975 /* Remove eventual dupes. */
976 TAILQ_FOREACH(k, &kbl, entry)
977 if (k->key == keyval && k->mask == mask) {
978 TAILQ_REMOVE(&kbl, k, entry);
979 g_free(k);
980 break;
983 /* add keyname */
984 k = g_malloc0(sizeof *k);
985 k->cmd = g_strdup(cmd);
986 k->mask = mask;
987 k->use_in_entry = use_in_entry;
988 k->key = keyval;
990 DNPRINTF(XT_D_KEYBINDING, "keybinding_add: %s 0x%x %d 0x%x\n",
991 k->cmd,
992 k->mask,
993 k->use_in_entry,
994 k->key);
995 DNPRINTF(XT_D_KEYBINDING, "keybinding_add: adding: %s %s\n",
996 k->cmd, gdk_keyval_name(keyval));
998 TAILQ_INSERT_HEAD(&kbl, k, entry);
1000 return (0);
1004 add_kb(struct settings *s, char *entry)
1006 char *kb, *key;
1008 DNPRINTF(XT_D_KEYBINDING, "add_kb: %s\n", entry);
1010 /* clearall is special */
1011 if (!strcmp(entry, "clearall")) {
1012 keybinding_clearall();
1013 return (0);
1016 kb = strstr(entry, ",");
1017 if (kb == NULL)
1018 return (1);
1019 *kb = '\0';
1020 key = kb + 1;
1022 return (keybinding_add(entry, key, key[0] == '!'));
1026 add_ua(struct settings *s, char *value)
1028 struct user_agent *ua;
1030 ua = g_malloc0(sizeof *ua);
1031 ua->value = g_strdup(value);
1033 TAILQ_INSERT_HEAD(&ua_list, ua, entry);
1035 /* use the last added user agent */
1036 user_agent = TAILQ_FIRST(&ua_list);
1037 user_agent_count++;
1039 return (0);
1043 void
1044 walk_ua(struct settings *s,
1045 void (*cb)(struct settings *, char *, void *), void *cb_args)
1047 struct user_agent *ua;
1049 if (s == NULL || cb == NULL) {
1050 show_oops(NULL, "walk_ua invalid parameters");
1051 return;
1054 TAILQ_FOREACH(ua, &ua_list, entry) {
1055 cb(s, ua->value, cb_args);
1060 set_auto_load_images(char *value)
1062 struct tab *t;
1064 auto_load_images = atoi(value);
1065 TAILQ_FOREACH(t, &tabs, entry) {
1066 g_object_set(G_OBJECT(t->settings),
1067 "auto-load-images", auto_load_images, (char *)NULL);
1068 webkit_web_view_set_settings(t->wv, t->settings);
1070 return (0);
1074 set_enable_autoscroll(char *value)
1076 enable_autoscroll = atoi(value);
1077 return (0);
1081 set_enable_favicon_entry(char *value)
1083 enable_favicon_entry = atoi(value);
1084 return (0);
1088 set_enable_favicon_tabs(char *value)
1090 enable_favicon_tabs = atoi(value);
1091 return (0);
1094 char *
1095 get_referer(struct settings *s)
1097 if (referer_mode == XT_REFERER_ALWAYS)
1098 return (g_strdup("always"));
1099 if (referer_mode == XT_REFERER_NEVER)
1100 return (g_strdup("never"));
1101 if (referer_mode == XT_REFERER_SAME_DOMAIN)
1102 return (g_strdup("same-domain"));
1103 if (referer_mode == XT_REFERER_CUSTOM)
1104 return (g_strdup(referer_custom));
1105 return (NULL);
1109 set_referer(struct settings *s, char *value)
1111 if (referer_custom)
1112 g_free(referer_custom);
1114 if (!strcmp(value, "always"))
1115 referer_mode = XT_REFERER_ALWAYS;
1116 else if (!strcmp(value, "never"))
1117 referer_mode = XT_REFERER_NEVER;
1118 else if (!strcmp(value, "same-domain"))
1119 referer_mode = XT_REFERER_SAME_DOMAIN;
1120 else if (!valid_url_type(value)) {
1121 referer_mode = XT_REFERER_CUSTOM;
1122 referer_custom = g_strdup(value);
1123 } else {
1124 /* we've already free'd the custom referer */
1125 if (referer_mode == XT_REFERER_CUSTOM)
1126 referer_mode = XT_REFERER_NEVER;
1127 return (1);
1130 return (0);
1134 set_referer_rt(char *value)
1136 return set_referer(NULL, value);
1140 set_external_editor(char *editor)
1142 if (external_editor)
1143 g_free(external_editor);
1145 external_editor = g_strdup(editor);
1147 return (0);
1150 void
1151 setup_proxy(char *uri)
1153 if (proxy_uri) {
1154 g_object_set(session, "proxy_uri", NULL, (char *)NULL);
1155 soup_uri_free(proxy_uri);
1156 proxy_uri = NULL;
1158 if (http_proxy) {
1159 if (http_proxy != uri) {
1160 g_free(http_proxy);
1161 http_proxy = NULL;
1165 if (uri) {
1166 http_proxy = g_strdup(uri);
1167 DNPRINTF(XT_D_CONFIG, "setup_proxy: %s\n", uri);
1168 proxy_uri = soup_uri_new(http_proxy);
1169 if (!(proxy_uri == NULL || !SOUP_URI_VALID_FOR_HTTP(proxy_uri)))
1170 g_object_set(session, "proxy-uri", proxy_uri,
1171 (char *)NULL);
1175 char *
1176 get_tab_style(struct settings *s)
1178 if (tab_style == XT_TABS_NORMAL)
1179 return (g_strdup("normal"));
1180 else
1181 return (g_strdup("compact"));
1185 set_tab_style(struct settings *s, char *val)
1187 if (!strcmp(val, "normal"))
1188 tab_style = XT_TABS_NORMAL;
1189 else if (!strcmp(val, "compact"))
1190 tab_style = XT_TABS_COMPACT;
1191 else
1192 return (1);
1194 return (0);
1197 char *
1198 get_edit_mode(struct settings *s)
1200 if (edit_mode == XT_EM_HYBRID)
1201 return (g_strdup("hybrid"));
1202 else
1203 return (g_strdup("vi"));
1207 set_edit_mode(struct settings *s, char *val)
1209 if (!strcmp(val, "hybrid"))
1210 edit_mode = XT_EM_HYBRID;
1211 else if (!strcmp(val, "vi"))
1212 edit_mode = XT_EM_VI;
1213 else
1214 return (1);
1216 return (0);
1219 char *
1220 get_download_mode(struct settings *s)
1222 switch (download_mode) {
1223 case XT_DM_START:
1224 return (g_strdup("start"));
1225 break;
1226 case XT_DM_ASK:
1227 return (g_strdup("ask"));
1228 break;
1229 case XT_DM_ADD:
1230 return (g_strdup("add"));
1231 break;
1233 return (g_strdup("unknown"));
1237 set_download_mode(struct settings *s, char *val)
1239 if (!strcmp(val, "start"))
1240 download_mode = XT_DM_START;
1241 else if (!strcmp(val, "ask"))
1242 download_mode = XT_DM_ASK;
1243 else if (!strcmp(val, "add"))
1244 download_mode = XT_DM_ADD;
1245 else
1246 return (1);
1248 return (0);
1252 set_download_mode_rt(char *val)
1254 return set_download_mode(NULL, val);
1257 char *
1258 get_work_dir(struct settings *s)
1260 if (work_dir[0] == '\0')
1261 return (0);
1262 return (g_strdup(work_dir));
1266 set_work_dir(struct settings *s, char *val)
1268 if (val[0] == '~')
1269 snprintf(work_dir, sizeof work_dir, "%s/%s",
1270 pwd->pw_dir, &val[1]);
1271 else
1272 strlcpy(work_dir, val, sizeof work_dir);
1274 return (0);
1277 void
1278 walk_cookie_wl(struct settings *s,
1279 void (*cb)(struct settings *, char *, void *), void *cb_args)
1281 struct domain *d;
1283 if (s == NULL || cb == NULL) {
1284 show_oops(NULL, "walk_cookie_wl invalid parameters");
1285 return;
1288 RB_FOREACH_REVERSE(d, domain_list, &c_wl)
1289 cb(s, d->d, cb_args);
1292 void
1293 walk_js_wl(struct settings *s,
1294 void (*cb)(struct settings *, char *, void *), void *cb_args)
1296 struct domain *d;
1298 if (s == NULL || cb == NULL) {
1299 show_oops(NULL, "walk_js_wl invalid parameters");
1300 return;
1303 RB_FOREACH_REVERSE(d, domain_list, &js_wl)
1304 cb(s, d->d, cb_args);
1307 void
1308 walk_pl_wl(struct settings *s,
1309 void (*cb)(struct settings *, char *, void *), void *cb_args)
1311 struct domain *d;
1313 if (s == NULL || cb == NULL) {
1314 show_oops(NULL, "walk_pl_wl invalid parameters");
1315 return;
1318 RB_FOREACH_REVERSE(d, domain_list, &pl_wl)
1319 cb(s, d->d, cb_args);
1323 settings_add(char *var, char *val)
1325 int i, rv, *p;
1326 gfloat *f;
1327 char **s;
1329 /* get settings */
1330 for (i = 0, rv = 0; i < LENGTH(rs); i++) {
1331 if (strcmp(var, rs[i].name))
1332 continue;
1334 if (rs[i].s) {
1335 if (rs[i].s->set(&rs[i], val))
1336 errx(1, "invalid value for %s: %s", var, val);
1337 rv = 1;
1338 break;
1339 } else
1340 switch (rs[i].type) {
1341 case XT_S_INT:
1342 p = rs[i].ival;
1343 *p = atoi(val);
1344 rv = 1;
1345 break;
1346 case XT_S_STR:
1347 s = rs[i].sval;
1348 if (s == NULL)
1349 errx(1, "invalid sval for %s",
1350 rs[i].name);
1351 if (*s)
1352 g_free(*s);
1353 *s = g_strdup(val);
1354 rv = 1;
1355 break;
1356 case XT_S_FLOAT:
1357 f = rs[i].fval;
1358 *f = atof(val);
1359 rv = 1;
1360 break;
1361 case XT_S_INVALID:
1362 default:
1363 errx(1, "invalid type for %s", var);
1365 break;
1367 return (rv);
1370 #define WS "\n= \t"
1371 void
1372 config_parse(char *filename, int runtime)
1374 FILE *config, *f;
1375 char *line, *cp, *var, *val;
1376 size_t len, lineno = 0;
1377 int handled;
1378 char file[PATH_MAX];
1379 struct stat sb;
1381 DNPRINTF(XT_D_CONFIG, "config_parse: filename %s\n", filename);
1383 if (filename == NULL)
1384 return;
1386 if (runtime && runtime_settings[0] != '\0') {
1387 snprintf(file, sizeof file, "%s/%s",
1388 work_dir, runtime_settings);
1389 if (stat(file, &sb)) {
1390 warnx("runtime file doesn't exist, creating it");
1391 if ((f = fopen(file, "w")) == NULL)
1392 err(1, "runtime");
1393 fprintf(f, "# AUTO GENERATED, DO NOT EDIT\n");
1394 fclose(f);
1396 } else
1397 strlcpy(file, filename, sizeof file);
1399 if ((config = fopen(file, "r")) == NULL) {
1400 warn("config_parse: cannot open %s", filename);
1401 return;
1404 for (;;) {
1405 if ((line = fparseln(config, &len, &lineno, NULL, 0)) == NULL)
1406 if (feof(config) || ferror(config))
1407 break;
1409 cp = line;
1410 cp += (long)strspn(cp, WS);
1411 if (cp[0] == '\0') {
1412 /* empty line */
1413 free(line);
1414 continue;
1417 if ((var = strsep(&cp, WS)) == NULL || cp == NULL)
1418 startpage_add("invalid configuration file entry: %s",
1419 line);
1420 else {
1421 cp += (long)strspn(cp, WS);
1423 if ((val = strsep(&cp, "\0")) == NULL)
1424 break;
1426 DNPRINTF(XT_D_CONFIG, "config_parse: %s=%s\n",
1427 var, val);
1428 handled = settings_add(var, val);
1430 if (handled == 0)
1431 startpage_add("invalid configuration file entry"
1432 ": %s=%s", var, val);
1435 free(line);
1438 fclose(config);
1441 struct settings_args {
1442 char **body;
1443 int i;
1446 void
1447 print_setting(struct settings *s, char *val, void *cb_args)
1449 char *tmp, *color;
1450 struct settings_args *sa = cb_args;
1452 if (sa == NULL)
1453 return;
1455 if (s->flags & XT_SF_RUNTIME)
1456 color = "#22cc22";
1457 else
1458 color = "#cccccc";
1460 tmp = *sa->body;
1461 *sa->body = g_strdup_printf(
1462 "%s\n<tr>"
1463 "<td style='background-color: %s; width: 10%%;word-break:break-all'>%s</td>"
1464 "<td style='background-color: %s; width: 20%%;word-break:break-all'>%s</td>",
1465 *sa->body,
1466 color,
1467 s->name,
1468 color,
1471 g_free(tmp);
1472 sa->i++;
1476 set_show(struct tab *t, struct karg *args)
1478 char *body, *page, *tmp;
1479 int i = 1;
1480 struct settings_args sa;
1482 bzero(&sa, sizeof sa);
1483 sa.body = &body;
1485 /* body */
1486 body = g_strdup_printf("<div align='center'><table><tr>"
1487 "<th align='left'>Setting</th>"
1488 "<th align='left'>Value</th></tr>\n");
1490 settings_walk(print_setting, &sa);
1491 i = sa.i;
1493 /* small message if there are none */
1494 if (i == 1) {
1495 tmp = body;
1496 body = g_strdup_printf("%s\n<tr><td style='text-align:center'"
1497 "colspan='2'>No settings</td></tr>\n", body);
1498 g_free(tmp);
1501 tmp = body;
1502 body = g_strdup_printf("%s</table></div>", body);
1503 g_free(tmp);
1505 page = get_html_page("Settings", body, "", 0);
1507 g_free(body);
1509 load_webkit_string(t, page, XT_URI_ABOUT_SET);
1511 g_free(page);
1513 return (XT_CB_PASSTHROUGH);
1517 set(struct tab *t, struct karg *args)
1519 char *p, *val;
1520 int i;
1522 if (args == NULL || args->s == NULL)
1523 return (set_show(t, args));
1525 /* strip spaces */
1526 p = g_strstrip(args->s);
1528 if (strlen(p) == 0)
1529 return (set_show(t, args));
1531 /* we got some sort of string */
1532 val = g_strrstr(p, "=");
1533 if (val) {
1534 *val++ = '\0';
1535 val = g_strchomp(val);
1536 p = g_strchomp(p);
1538 for (i = 0; i < get_settings_size(); i++) {
1539 if (strcmp(rs[i].name, p))
1540 continue;
1542 if (rs[i].activate) {
1543 if (rs[i].activate(val))
1544 show_oops(t, "%s invalid value %s",
1545 p, val);
1546 else
1547 show_oops(t, ":set %s = %s", p, val);
1548 goto done;
1549 } else {
1550 show_oops(t, "not a runtime option: %s", p);
1551 goto done;
1554 show_oops(t, "unknown option: %s", p);
1555 } else {
1556 p = g_strchomp(p);
1558 for (i = 0; i < get_settings_size(); i++) {
1559 if (strcmp(rs[i].name, p))
1560 continue;
1562 /* XXX this could use some cleanup */
1563 switch (rs[i].type) {
1564 case XT_S_INT:
1565 if (rs[i].ival)
1566 show_oops(t, "%s = %d",
1567 rs[i].name, *rs[i].ival);
1568 else if (rs[i].s && rs[i].s->get)
1569 show_oops(t, "%s = %s",
1570 rs[i].name,
1571 rs[i].s->get(&rs[i]));
1572 else if (rs[i].s && rs[i].s->get == NULL)
1573 show_oops(t, "%s = ...", rs[i].name);
1574 else
1575 show_oops(t, "%s = ", rs[i].name);
1576 break;
1577 case XT_S_FLOAT:
1578 if (rs[i].fval)
1579 show_oops(t, "%s = %f",
1580 rs[i].name, *rs[i].fval);
1581 else if (rs[i].s && rs[i].s->get)
1582 show_oops(t, "%s = %s",
1583 rs[i].name,
1584 rs[i].s->get(&rs[i]));
1585 else if (rs[i].s && rs[i].s->get == NULL)
1586 show_oops(t, "%s = ...", rs[i].name);
1587 else
1588 show_oops(t, "%s = ", rs[i].name);
1589 break;
1590 case XT_S_STR:
1591 if (rs[i].sval && *rs[i].sval)
1592 show_oops(t, "%s = %s",
1593 rs[i].name, *rs[i].sval);
1594 else if (rs[i].s && rs[i].s->get)
1595 show_oops(t, "%s = %s",
1596 rs[i].name,
1597 rs[i].s->get(&rs[i]));
1598 else if (rs[i].s && rs[i].s->get == NULL)
1599 show_oops(t, "%s = ...", rs[i].name);
1600 else
1601 show_oops(t, "%s = ", rs[i].name);
1602 break;
1603 default:
1604 show_oops(t, "unknown type for %s", rs[i].name);
1605 goto done;
1608 goto done;
1610 show_oops(t, "unknown option: %s", p);
1612 done:
1613 return (XT_CB_PASSTHROUGH);