Dillo v3.1.1-46-g8a360e32
Loading...
Searching...
No Matches
prefsparser.cc
Go to the documentation of this file.
1/*
2 * Preferences parser
3 *
4 * Copyright (C) 2006-2009 Jorge Arellano Cid <jcid@dillo.org>
5 * Copyright (C) 2024 Rodrigo Arias Mallo <rodarima@gmail.com>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 3 of the License, or
10 * (at your option) any later version.
11 */
12
18#include <sys/types.h>
19#include <stdlib.h>
20#include <locale.h> /* for setlocale */
21#include <math.h> /* for isinf */
22#include <limits.h>
23
24#include "prefs.h"
25#include "misc.h"
26#include "msg.h"
27#include "colors.h"
28
29#include "prefsparser.hh"
30
43
44typedef struct {
45 const char *name;
46 void *pref;
47 PrefType_t type;
48 int count;
49} SymNode_t;
50
54static int parseOption(char *name, char *value,
55 SymNode_t *symbols, int n_symbols)
56{
57 SymNode_t *node;
58 int i;
59 int st;
60
61 node = NULL;
62 for (i = 0; i < n_symbols; i++) {
63 if (!strcmp(symbols[i].name, name)) {
64 node = & (symbols[i]);
65 break;
66 }
67 }
68
69 if (!node) {
70 MSG("prefs: {%s} is not a recognized token.\n", name);
71 return -1;
72 }
73
74 switch (node->type) {
75 case PREFS_BOOL:
76 *(bool_t *)node->pref = (!dStrAsciiCasecmp(value, "yes") ||
77 !dStrAsciiCasecmp(value, "true"));
78 break;
79 case PREFS_COLOR:
80 *(int32_t *)node->pref = a_Color_parse(value, *(int32_t*)node->pref,&st);
81 if (st == 1)
82 MSG("prefs: Color '%s' not recognized.\n", value);
83 break;
84 case PREFS_STRING:
85 dFree(*(char **)node->pref);
86 *(char **)node->pref = dStrdup(value);
87 break;
88 case PREFS_STRINGS:
89 {
90 Dlist *lp = *(Dlist **)node->pref;
91 if (node->count == 0) {
92 /* override the default */
93 for (i = 0; i < dList_length(lp); i++) {
94 void *data = dList_nth_data(lp, i);
95 dList_remove(lp, data);
96 dFree(data);
97 }
98 }
99 dList_append(lp, dStrdup(value));
100 break;
101 }
102 case PREFS_URL:
103 a_Url_free(*(DilloUrl **)node->pref);
104 *(DilloUrl **)node->pref = a_Url_new(value, NULL);
105 break;
106 case PREFS_INT32:
107 *(int32_t *)node->pref = strtol(value, NULL, 10);
108 break;
109 case PREFS_DOUBLE:
110 *(double *)node->pref = strtod(value, NULL);
111 break;
113 {
114 double d = strtod (value, NULL);
115 if (isinf(d)) {
116 if (d > 0)
117 *(int*)node->pref = INT_MAX;
118 else
119 *(int*)node->pref = INT_MIN;
120 } else
121 *(int*)node->pref = 100 * d;
122 }
123 break;
124 case PREFS_GEOMETRY:
127 break;
128 case PREFS_PANEL_SIZE:
129 if (!dStrAsciiCasecmp(value, "tiny"))
131 else if (!dStrAsciiCasecmp(value, "small"))
133 else /* default to "medium" */
135 break;
136 default:
137 MSG_WARN("prefs: {%s} IS recognized but not handled!\n", name);
138 break; /* Not reached */
139 }
140 node->count++;
141
142 return 0;
143}
144
148void PrefsParser::parse(FILE *fp)
149{
150 char *line, *name, *value, *oldLocale;
151 int st, line_number = 1;
152
153 /* Symbol array, sorted alphabetically */
154 static SymNode_t symbols[] = {
155 { "allow_white_bg", &prefs.allow_white_bg, PREFS_BOOL, 0 },
156 { "white_bg_replacement", &prefs.white_bg_replacement, PREFS_COLOR, 0 },
157 { "bg_color", &prefs.bg_color, PREFS_COLOR, 0 },
158 { "buffered_drawing", &prefs.buffered_drawing, PREFS_INT32, 0 },
159 { "contrast_visited_color", &prefs.contrast_visited_color, PREFS_BOOL, 0 },
160 { "enterpress_forces_submit", &prefs.enterpress_forces_submit,
161 PREFS_BOOL, 0 },
162 { "focus_new_tab", &prefs.focus_new_tab, PREFS_BOOL, 0 },
163 { "font_cursive", &prefs.font_cursive, PREFS_STRING, 0 },
164 { "font_factor", &prefs.font_factor, PREFS_DOUBLE, 0 },
165 { "font_fantasy", &prefs.font_fantasy, PREFS_STRING, 0 },
166 { "font_max_size", &prefs.font_max_size, PREFS_INT32, 0 },
167 { "font_min_size", &prefs.font_min_size, PREFS_INT32, 0 },
168 { "font_monospace", &prefs.font_monospace, PREFS_STRING, 0 },
169 { "font_sans_serif", &prefs.font_sans_serif, PREFS_STRING, 0 },
170 { "font_serif", &prefs.font_serif, PREFS_STRING, 0 },
171 { "fullwindow_start", &prefs.fullwindow_start, PREFS_BOOL, 0 },
172 { "geometry", NULL, PREFS_GEOMETRY, 0 },
173 { "home", &prefs.home, PREFS_URL, 0 },
174 { "new_tab_page", &prefs.new_tab_page, PREFS_URL, 0 },
175 { "http_language", &prefs.http_language, PREFS_STRING, 0 },
176 { "http_max_conns", &prefs.http_max_conns, PREFS_INT32, 0 },
177 { "http_persistent_conns", &prefs.http_persistent_conns, PREFS_BOOL, 0 },
178 { "http_proxy", &prefs.http_proxy, PREFS_URL, 0 },
179 { "http_proxyuser", &prefs.http_proxyuser, PREFS_STRING, 0 },
180 { "http_referer", &prefs.http_referer, PREFS_STRING, 0 },
181 { "http_strict_transport_security",&prefs.http_strict_transport_security,
182 PREFS_BOOL, 0 },
183 { "http_force_https", &prefs.http_force_https, PREFS_BOOL, 0 },
184 { "http_user_agent", &prefs.http_user_agent, PREFS_STRING, 0 },
185 { "limit_text_width", &prefs.limit_text_width, PREFS_BOOL, 0 },
186 { "adjust_min_width", &prefs.adjust_min_width, PREFS_BOOL, 0 },
187 { "adjust_table_min_width", &prefs.adjust_table_min_width, PREFS_BOOL, 0 },
188 { "load_images", &prefs.load_images, PREFS_BOOL, 0 },
189 { "load_background_images", &prefs.load_background_images, PREFS_BOOL, 0 },
190 { "load_stylesheets", &prefs.load_stylesheets, PREFS_BOOL, 0 },
191 { "middle_click_drags_page", &prefs.middle_click_drags_page,
192 PREFS_BOOL, 0 },
193 { "middle_click_opens_new_tab", &prefs.middle_click_opens_new_tab,
194 PREFS_BOOL, 0 },
195 { "right_click_closes_tab", &prefs.right_click_closes_tab, PREFS_BOOL, 0 },
196 { "scroll_switches_tabs", &prefs.scroll_switches_tabs, PREFS_BOOL, 0 },
197 { "scroll_switches_tabs_reverse", &prefs.scroll_switches_tabs_reverse, PREFS_BOOL, 0 },
198 { "no_proxy", &prefs.no_proxy, PREFS_STRING, 0 },
199 { "panel_size", &prefs.panel_size, PREFS_PANEL_SIZE, 0 },
200 { "parse_embedded_css", &prefs.parse_embedded_css, PREFS_BOOL, 0 },
201 { "save_dir", &prefs.save_dir, PREFS_STRING, 0 },
202 { "scroll_step", &prefs.scroll_step, PREFS_INT32, 0 },
203 { "search_url", &prefs.search_urls, PREFS_STRINGS, 0 },
204 { "show_back", &prefs.show_back, PREFS_BOOL, 0 },
205 { "show_bookmarks", &prefs.show_bookmarks, PREFS_BOOL, 0 },
206 { "show_clear_url", &prefs.show_clear_url, PREFS_BOOL, 0 },
207 { "show_extra_warnings", &prefs.show_extra_warnings, PREFS_BOOL, 0 },
208 { "show_filemenu", &prefs.show_filemenu, PREFS_BOOL, 0 },
209 { "show_forw", &prefs.show_forw, PREFS_BOOL, 0 },
210 { "show_help", &prefs.show_help, PREFS_BOOL, 0 },
211 { "show_home", &prefs.show_home, PREFS_BOOL, 0 },
212 { "show_msg", &prefs.show_msg, PREFS_BOOL, 0 },
213 { "show_progress_box", &prefs.show_progress_box, PREFS_BOOL, 0 },
214 { "show_quit_dialog", &prefs.show_quit_dialog, PREFS_BOOL, 0 },
215 { "show_reload", &prefs.show_reload, PREFS_BOOL, 0 },
216 { "show_save", &prefs.show_save, PREFS_BOOL, 0 },
217 { "show_url", &prefs.show_url, PREFS_BOOL, 0 },
218 { "show_search", &prefs.show_search, PREFS_BOOL, 0 },
219 { "show_stop", &prefs.show_stop, PREFS_BOOL, 0 },
220 { "show_tools", &prefs.show_tools, PREFS_BOOL, 0 },
221 { "show_tooltip", &prefs.show_tooltip, PREFS_BOOL, 0 },
222 { "show_ui_tooltip", &prefs.show_ui_tooltip, PREFS_BOOL, 0 },
223 { "small_icons", &prefs.small_icons, PREFS_BOOL, 0 },
224 { "start_page", &prefs.start_page, PREFS_URL, 0 },
225 { "theme", &prefs.theme, PREFS_STRING, 0 },
226 { "ui_button_highlight_color", &prefs.ui_button_highlight_color,
227 PREFS_COLOR, 0 },
228 { "ui_fg_color", &prefs.ui_fg_color, PREFS_COLOR, 0 },
229 { "ui_main_bg_color", &prefs.ui_main_bg_color, PREFS_COLOR, 0 },
230 { "ui_selection_color", &prefs.ui_selection_color, PREFS_COLOR, 0 },
231 { "ui_tab_active_bg_color", &prefs.ui_tab_active_bg_color, PREFS_COLOR, 0 },
232 { "ui_tab_bg_color", &prefs.ui_tab_bg_color, PREFS_COLOR, 0 },
233 { "ui_tab_active_fg_color", &prefs.ui_tab_active_fg_color, PREFS_COLOR, 0 },
234 { "ui_tab_fg_color", &prefs.ui_tab_fg_color, PREFS_COLOR, 0 },
235 { "ui_tab_height", &prefs.ui_tab_height, PREFS_INT32, 0 },
236 { "ui_text_bg_color", &prefs.ui_text_bg_color, PREFS_COLOR, 0 },
237 { "penalty_hyphen", &prefs.penalty_hyphen, PREFS_FRACTION_100, 0 },
238 { "penalty_hyphen_2", &prefs.penalty_hyphen_2, PREFS_FRACTION_100, 0 },
239 { "penalty_em_dash_left", &prefs.penalty_em_dash_left,
241 { "penalty_em_dash_right", &prefs.penalty_em_dash_right,
243 { "penalty_em_dash_right_2", &prefs.penalty_em_dash_right_2,
245 { "stretchability_factor", &prefs.stretchability_factor,
247 { "zoom_factor", &prefs.zoom_factor, PREFS_DOUBLE, 0 }
248 };
249 // changing the LC_NUMERIC locale (temporarily) to C
250 // avoids parsing problems with float numbers
251 oldLocale = dStrdup(setlocale(LC_NUMERIC, NULL));
252 setlocale(LC_NUMERIC, "C");
253
254 // scan the file line by line
255 while ((line = dGetline(fp)) != NULL) {
256 st = dParser_parse_rc_line(&line, &name, &value);
257
258 if (st == 0) {
259 _MSG("prefsparser: name=%s, value=%s\n", name, value);
260 parseOption(name, value, symbols, sizeof(symbols) / sizeof(symbols[0]));
261 } else if (st < 0) {
262 MSG_ERR("prefsparser: Syntax error in dillorc line %d:"
263 " name=\"%s\" value=\"%s\"\n", line_number, name, value);
264 }
265 dFree(line);
266 line_number++;
267 }
268 fclose(fp);
269
270 // restore the old numeric locale
271 setlocale(LC_NUMERIC, oldLocale);
272 dFree(oldLocale);
273}
#define _MSG(...)
Definition bookmarks.c:45
#define MSG(...)
Definition bookmarks.c:46
int32_t a_Color_parse(const char *str, int32_t default_color, int *err)
Parse a color string.
Definition colors.c:258
unsigned char bool_t
Definition d_size.h:21
char * dGetline(FILE *stream)
Get a line from a FILE stream.
Definition dlib.c:928
void dFree(void *mem)
Definition dlib.c:68
int dStrAsciiCasecmp(const char *s1, const char *s2)
Definition dlib.c:203
char * dStrdup(const char *s)
Definition dlib.c:77
int dList_length(Dlist *lp)
For completing the ADT.
Definition dlib.c:613
void * dList_nth_data(Dlist *lp, int n0)
Return the nth data item, NULL when not found or 'n0' is out of range.
Definition dlib.c:662
void dList_append(Dlist *lp, void *data)
Append a data item to the list.
Definition dlib.c:597
int dParser_parse_rc_line(char **line, char **name, char **value)
Take a dillo rc line and return 'name' and 'value' pointers to it.
Definition dlib.c:834
void dList_remove(Dlist *lp, const void *data)
Definition dlib.c:641
#define MSG_ERR(...)
Definition dpid_common.h:23
#define MSG_WARN(...)
Definition msg.h:26
int a_Misc_parse_geometry(char *str, int *x, int *y, int *w, int *h)
Parse a geometry string.
Definition misc.c:360
DilloPrefs prefs
Global Data.
Definition prefs.c:33
@ P_small
Definition prefs.h:35
@ P_tiny
Definition prefs.h:35
@ P_medium
Definition prefs.h:35
PrefType_t
@ PREFS_URL
@ PREFS_PANEL_SIZE
@ PREFS_STRING
@ PREFS_GEOMETRY
@ PREFS_STRINGS
@ PREFS_DOUBLE
@ PREFS_INT32
@ PREFS_COLOR
@ PREFS_FRACTION_100
@ PREFS_BOOL
static int parseOption(char *name, char *value, SymNode_t *symbols, int n_symbols)
Parse a name/value pair and set preferences accordingly.
bool_t http_persistent_conns
Definition prefs.h:100
bool_t enterpress_forces_submit
Definition prefs.h:109
bool_t show_progress_box
Definition prefs.h:93
int32_t ui_tab_fg_color
Definition prefs.h:62
char * font_sans_serif
Definition prefs.h:105
char * font_monospace
Definition prefs.h:108
bool_t parse_embedded_css
Definition prefs.h:99
bool_t show_reload
Definition prefs.h:83
bool_t show_filemenu
Definition prefs.h:88
bool_t small_icons
Definition prefs.h:70
int32_t ui_button_highlight_color
Definition prefs.h:55
char * font_fantasy
Definition prefs.h:107
bool_t load_images
Definition prefs.h:96
bool_t show_tooltip
Definition prefs.h:66
char * http_user_agent
Definition prefs.h:47
int penalty_hyphen_2
Definition prefs.h:120
int32_t http_max_conns
Definition prefs.h:43
char * no_proxy
Definition prefs.h:48
int xpos
Definition prefs.h:40
int32_t ui_tab_active_bg_color
Definition prefs.h:59
bool_t fullwindow_start
Definition prefs.h:95
bool_t show_help
Definition prefs.h:92
int width
Definition prefs.h:38
char * http_proxyuser
Definition prefs.h:45
DilloUrl * http_proxy
Definition prefs.h:44
int ypos
Definition prefs.h:41
int32_t font_min_size
Definition prefs.h:78
bool_t limit_text_width
Definition prefs.h:71
char * font_serif
Definition prefs.h:104
bool_t focus_new_tab
Definition prefs.h:74
int32_t ui_selection_color
Definition prefs.h:58
int32_t bg_color
Definition prefs.h:54
int32_t ui_fg_color
Definition prefs.h:56
int32_t white_bg_replacement
Definition prefs.h:53
bool_t show_extra_warnings
Definition prefs.h:118
bool_t load_background_images
Definition prefs.h:97
bool_t load_stylesheets
Definition prefs.h:98
bool_t show_tools
Definition prefs.h:87
int penalty_em_dash_right
Definition prefs.h:121
int32_t ui_tab_active_fg_color
Definition prefs.h:60
int32_t buffered_drawing
Definition prefs.h:103
bool_t show_search
Definition prefs.h:91
bool_t allow_white_bg
Definition prefs.h:52
bool_t show_stop
Definition prefs.h:85
int32_t ui_tab_bg_color
Definition prefs.h:61
int panel_size
Definition prefs.h:69
int32_t ui_text_bg_color
Definition prefs.h:64
bool_t middle_click_drags_page
Definition prefs.h:119
bool_t right_click_closes_tab
Definition prefs.h:111
int32_t ui_main_bg_color
Definition prefs.h:57
bool_t adjust_table_min_width
Definition prefs.h:73
bool_t show_quit_dialog
Definition prefs.h:94
bool_t http_strict_transport_security
Definition prefs.h:101
bool_t show_home
Definition prefs.h:82
DilloUrl * start_page
Definition prefs.h:49
bool_t http_force_https
Definition prefs.h:102
int32_t scroll_step
Definition prefs.h:79
bool_t show_ui_tooltip
Definition prefs.h:67
DilloUrl * new_tab_page
Definition prefs.h:51
bool_t adjust_min_width
Definition prefs.h:72
bool_t show_save
Definition prefs.h:84
bool_t contrast_visited_color
Definition prefs.h:65
bool_t scroll_switches_tabs
Definition prefs.h:112
int penalty_em_dash_right_2
Definition prefs.h:121
bool_t show_clear_url
Definition prefs.h:89
bool_t show_msg
Definition prefs.h:117
int stretchability_factor
Definition prefs.h:122
bool_t show_back
Definition prefs.h:80
bool_t scroll_switches_tabs_reverse
Definition prefs.h:113
bool_t show_bookmarks
Definition prefs.h:86
double zoom_factor
Definition prefs.h:76
Dlist * search_urls
Definition prefs.h:115
double font_factor
Definition prefs.h:75
char * http_referer
Definition prefs.h:46
char * theme
Definition prefs.h:68
char * font_cursive
Definition prefs.h:106
char * save_dir
Definition prefs.h:116
int32_t font_max_size
Definition prefs.h:77
bool_t middle_click_opens_new_tab
Definition prefs.h:110
bool_t show_url
Definition prefs.h:90
int32_t ui_tab_height
Definition prefs.h:63
int height
Definition prefs.h:39
int penalty_hyphen
Definition prefs.h:120
char * http_language
Definition prefs.h:42
DilloUrl * home
Definition prefs.h:50
bool_t show_forw
Definition prefs.h:81
int penalty_em_dash_left
Definition prefs.h:121
Definition url.h:88
Definition dlib.h:131
void a_Url_free(DilloUrl *url)
Free a DilloUrl.
Definition url.c:208
DilloUrl * a_Url_new(const char *url_str, const char *base_url)
Transform (and resolve) an URL string into the respective DilloURL.
Definition url.c:371