2 * Routines for handling preferences
4 * $Id: prefs.c,v 1.29 2000/01/03 06:29:32 guy Exp $
6 * Ethereal - Network traffic analyzer
7 * By Gerald Combs <gerald@zing.org>
8 * Copyright 1998 Gerald Combs
11 * This program is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU General Public License
13 * as published by the Free Software Foundation; either version 2
14 * of the License, or (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
30 #ifdef HAVE_SYS_TYPES_H
31 #include <sys/types.h>
55 /* Internal functions */
56 static int set_pref(gchar*, gchar*);
57 static GList *get_string_list(gchar *);
58 static void clear_string_list(GList *);
60 #define PF_NAME "preferences"
62 static int init_prefs = 1;
63 static gchar *pf_path = NULL;
67 gchar *gui_ptree_line_style_text[] =
68 { "NONE", "SOLID", "DOTTED", "TABBED", NULL };
70 gchar *gui_ptree_expander_style_text[] =
71 { "NONE", "SQUARE", "TRIANGLE", "CIRCULAR", NULL };
74 /* Parse through a list of comma-separated, quoted strings. Return a
75 list of the string data */
77 get_string_list(gchar *str) {
78 enum { PRE_QUOT, IN_QUOT, POST_QUOT };
80 gint state = PRE_QUOT, i = 0, j = 0;
81 gboolean backslash = FALSE;
82 gchar cur_c, *slstr = NULL;
85 while ((cur_c = str[i]) != '\0') {
86 if (cur_c == '"' && ! backslash) {
90 slstr = (gchar *) g_malloc(sizeof(gchar) * COL_MAX_LEN);
96 sl = g_list_append(sl, slstr);
99 clear_string_list(sl);
105 } else if (cur_c == '\\' && ! backslash) {
107 } else if (cur_c == ',' && state == POST_QUOT) {
109 } else if (state == IN_QUOT && j < COL_MAX_LEN) {
115 if (state != POST_QUOT) {
116 clear_string_list(sl);
122 clear_string_list(GList *sl) {
127 l = g_list_remove_link(l, l);
131 /* Takes an string and a pointer to an array of strings, and a default int value.
132 * The array must be terminated by a NULL string. If the string is found in the array
133 * of strings, the index of that string in the array is returned. Otherwise, the
134 * default value that was passed as the third argument is returned.
137 find_index_from_string_array(char *needle, char **haystack, int default_value)
141 while (haystack[i] != NULL) {
142 if (strcmp(needle, haystack[i]) == 0) {
147 return default_value;
150 /* Preferences file format:
151 * - Configuration directives start at the beginning of the line, and
152 * are terminated with a colon.
153 * - Directives can be continued on the next line by preceding them with
158 # This is a comment line
160 print.file: /a/very/long/path/
165 #define MAX_VAR_LEN 48
166 #define MAX_VAL_LEN 1024
168 #define DEF_NUM_COLS 6
170 read_prefs(char **pf_path_return) {
171 enum { START, IN_VAR, PRE_VAL, IN_VAL, IN_SKIP };
173 gchar cur_var[MAX_VAR_LEN], cur_val[MAX_VAL_LEN];
174 int got_c, state = START, i;
175 gint var_len = 0, val_len = 0, fline = 1, pline = 1;
176 gboolean got_val = FALSE;
178 gchar *col_fmt[] = {"No.", "%m", "Time", "%t",
179 "Source", "%s", "Destination", "%d",
180 "Protocol", "%p", "Info", "%i"};
183 /* Initialize preferences. With any luck, these values will be
184 overwritten below. */
187 prefs.pr_format = PR_FMT_TEXT;
188 prefs.pr_dest = PR_DEST_CMD;
189 prefs.pr_file = g_strdup("ethereal.out");
190 prefs.pr_cmd = g_strdup("lpr");
191 prefs.col_list = NULL;
192 for (i = 0; i < DEF_NUM_COLS; i++) {
193 cfmt = (fmt_data *) g_malloc(sizeof(fmt_data));
194 cfmt->title = g_strdup(col_fmt[i * 2]);
195 cfmt->fmt = g_strdup(col_fmt[(i * 2) + 1]);
196 prefs.col_list = g_list_append(prefs.col_list, cfmt);
198 prefs.num_cols = DEF_NUM_COLS;
199 prefs.st_client_fg.pixel = 0;
200 prefs.st_client_fg.red = 32767;
201 prefs.st_client_fg.green = 0;
202 prefs.st_client_fg.blue = 0;
203 prefs.st_client_bg.pixel = 65535;
204 prefs.st_client_bg.red = 65535;
205 prefs.st_client_bg.green = 65535;
206 prefs.st_client_bg.blue = 65535;
207 prefs.st_server_fg.pixel = 0;
208 prefs.st_server_fg.red = 0;
209 prefs.st_server_fg.green = 0;
210 prefs.st_server_fg.blue = 32767;
211 prefs.st_server_bg.pixel = 65535;
212 prefs.st_server_bg.red = 65535;
213 prefs.st_server_bg.green = 65535;
214 prefs.st_server_bg.blue = 65535;
215 prefs.gui_scrollbar_on_right = TRUE;
216 prefs.gui_plist_sel_browse = FALSE;
217 prefs.gui_ptree_sel_browse = FALSE;
218 prefs.gui_ptree_line_style = 0;
219 prefs.gui_ptree_expander_style = 1;
223 pf_path = (gchar *) g_malloc(strlen(getenv("HOME")) + strlen(PF_DIR) +
224 strlen(PF_NAME) + 4);
225 sprintf(pf_path, "%s/%s/%s", getenv("HOME"), PF_DIR, PF_NAME);
228 *pf_path_return = NULL;
229 if ((pf = fopen(pf_path, "r")) == NULL) {
231 *pf_path_return = pf_path;
235 while ((got_c = getc(pf)) != EOF) {
241 if (var_len >= MAX_VAR_LEN) {
242 g_warning ("%s line %d: Variable too long", pf_path, fline);
247 if (val_len >= MAX_VAL_LEN) {
248 g_warning ("%s line %d: Value too long", pf_path, fline);
256 if (isalnum(got_c)) {
259 cur_var[var_len] = '\0';
260 cur_val[val_len] = '\0';
261 if (! set_pref(cur_var, cur_val))
262 g_warning ("%s line %d: Bogus preference", pf_path, pline);
264 g_warning ("%s line %d: Incomplete preference", pf_path, pline);
272 } else if (isspace(got_c) && var_len > 0 && got_val) {
274 } else if (got_c == '#') {
277 g_warning ("%s line %d: Malformed line", pf_path, fline);
282 cur_var[var_len] = got_c;
291 if (!isspace(got_c)) {
293 cur_val[val_len] = got_c;
299 cur_val[val_len] = got_c;
302 while (isspace(cur_val[val_len]) && val_len > 0)
311 cur_var[var_len] = '\0';
312 cur_val[val_len] = '\0';
313 if (! set_pref(cur_var, cur_val))
314 g_warning ("%s line %d: Bogus preference", pf_path, pline);
316 g_warning ("%s line %d: Incomplete preference", pf_path, pline);
324 #define PRS_PRINT_FMT "print.format"
325 #define PRS_PRINT_DEST "print.destination"
326 #define PRS_PRINT_FILE "print.file"
327 #define PRS_PRINT_CMD "print.command"
328 #define PRS_COL_FMT "column.format"
329 #define PRS_STREAM_CL_FG "stream.client.fg"
330 #define PRS_STREAM_CL_BG "stream.client.bg"
331 #define PRS_STREAM_SR_FG "stream.server.fg"
332 #define PRS_STREAM_SR_BG "stream.server.bg"
333 #define PRS_GUI_SCROLLBAR_ON_RIGHT "gui.scrollbar_on_right"
334 #define PRS_GUI_PLIST_SEL_BROWSE "gui.packet_list_sel_browse"
335 #define PRS_GUI_PTREE_SEL_BROWSE "gui.protocol_tree_sel_browse"
336 #define PRS_GUI_PTREE_LINE_STYLE "gui.protocol_tree_line_style"
337 #define PRS_GUI_PTREE_EXPANDER_STYLE "gui.protocol_tree_expander_style"
339 #define RED_COMPONENT(x) ((((x) >> 16) & 0xff) * 65535 / 255)
340 #define GREEN_COMPONENT(x) ((((x) >> 8) & 0xff) * 65535 / 255)
341 #define BLUE_COMPONENT(x) (((x) & 0xff) * 65535 / 255)
343 static gchar *pr_formats[] = { "text", "postscript" };
344 static gchar *pr_dests[] = { "command", "file" };
347 set_pref(gchar *pref, gchar *value) {
351 unsigned long int cval;
353 if (strcmp(pref, PRS_PRINT_FMT) == 0) {
354 if (strcmp(value, pr_formats[PR_FMT_TEXT]) == 0) {
355 prefs.pr_format = PR_FMT_TEXT;
356 } else if (strcmp(value, pr_formats[PR_FMT_PS]) == 0) {
357 prefs.pr_format = PR_FMT_PS;
361 } else if (strcmp(pref, PRS_PRINT_DEST) == 0) {
362 if (strcmp(value, pr_dests[PR_DEST_CMD]) == 0) {
363 prefs.pr_dest = PR_DEST_CMD;
364 } else if (strcmp(value, pr_dests[PR_DEST_FILE]) == 0) {
365 prefs.pr_dest = PR_DEST_FILE;
369 } else if (strcmp(pref, PRS_PRINT_FILE) == 0) {
370 if (prefs.pr_file) g_free(prefs.pr_file);
371 prefs.pr_file = g_strdup(value);
372 } else if (strcmp(pref, PRS_PRINT_CMD) == 0) {
373 if (prefs.pr_cmd) g_free(prefs.pr_cmd);
374 prefs.pr_cmd = g_strdup(value);
375 } else if (strcmp(pref, PRS_COL_FMT) == 0) {
376 if ((col_l = get_string_list(value)) && (g_list_length(col_l) % 2) == 0) {
377 while (prefs.col_list) {
378 cfmt = prefs.col_list->data;
382 prefs.col_list = g_list_remove_link(prefs.col_list, prefs.col_list);
384 llen = g_list_length(col_l);
385 prefs.num_cols = llen / 2;
386 col_l = g_list_first(col_l);
388 cfmt = (fmt_data *) g_malloc(sizeof(fmt_data));
389 cfmt->title = g_strdup(col_l->data);
391 cfmt->fmt = g_strdup(col_l->data);
393 prefs.col_list = g_list_append(prefs.col_list, cfmt);
395 /* To do: else print some sort of error? */
397 clear_string_list(col_l);
398 } else if (strcmp(pref, PRS_STREAM_CL_FG) == 0) {
399 cval = strtoul(value, NULL, 16);
400 prefs.st_client_fg.pixel = 0;
401 prefs.st_client_fg.red = RED_COMPONENT(cval);
402 prefs.st_client_fg.green = GREEN_COMPONENT(cval);
403 prefs.st_client_fg.blue = BLUE_COMPONENT(cval);
404 } else if (strcmp(pref, PRS_STREAM_CL_BG) == 0) {
405 cval = strtoul(value, NULL, 16);
406 prefs.st_client_bg.pixel = 0;
407 prefs.st_client_bg.red = RED_COMPONENT(cval);
408 prefs.st_client_bg.green = GREEN_COMPONENT(cval);
409 prefs.st_client_bg.blue = BLUE_COMPONENT(cval);
410 } else if (strcmp(pref, PRS_STREAM_SR_FG) == 0) {
411 cval = strtoul(value, NULL, 16);
412 prefs.st_server_fg.pixel = 0;
413 prefs.st_server_fg.red = RED_COMPONENT(cval);
414 prefs.st_server_fg.green = GREEN_COMPONENT(cval);
415 prefs.st_server_fg.blue = BLUE_COMPONENT(cval);
416 } else if (strcmp(pref, PRS_STREAM_SR_BG) == 0) {
417 cval = strtoul(value, NULL, 16);
418 prefs.st_server_bg.pixel = 0;
419 prefs.st_server_bg.red = RED_COMPONENT(cval);
420 prefs.st_server_bg.green = GREEN_COMPONENT(cval);
421 prefs.st_server_bg.blue = BLUE_COMPONENT(cval);
422 } else if (strcmp(pref, PRS_GUI_SCROLLBAR_ON_RIGHT) == 0) {
423 if (strcmp(value, "TRUE") == 0) {
424 prefs.gui_scrollbar_on_right = TRUE;
427 prefs.gui_scrollbar_on_right = FALSE;
429 } else if (strcmp(pref, PRS_GUI_PLIST_SEL_BROWSE) == 0) {
430 if (strcmp(value, "TRUE") == 0) {
431 prefs.gui_plist_sel_browse = TRUE;
434 prefs.gui_plist_sel_browse = FALSE;
436 } else if (strcmp(pref, PRS_GUI_PTREE_SEL_BROWSE) == 0) {
437 if (strcmp(value, "TRUE") == 0) {
438 prefs.gui_ptree_sel_browse = TRUE;
441 prefs.gui_ptree_sel_browse = FALSE;
443 } else if (strcmp(pref, PRS_GUI_PTREE_LINE_STYLE) == 0) {
444 prefs.gui_ptree_line_style =
445 find_index_from_string_array(value, gui_ptree_line_style_text, 0);
446 } else if (strcmp(pref, PRS_GUI_PTREE_EXPANDER_STYLE) == 0) {
447 prefs.gui_ptree_expander_style =
448 find_index_from_string_array(value, gui_ptree_expander_style_text, 1);
457 write_prefs(char **pf_path_return) {
462 * - Split output lines longer than MAX_VAL_LEN
463 * - Create a function for the preference directory check/creation
464 * so that duplication can be avoided with filter.c
468 pf_path = (gchar *) g_malloc(strlen(getenv("HOME")) + strlen(PF_DIR) +
469 strlen(PF_NAME) + 4);
472 sprintf(pf_path, "%s/%s", getenv("HOME"), PF_DIR);
473 if (stat(pf_path, &s_buf) != 0)
477 mkdir(pf_path, 0755);
480 sprintf(pf_path, "%s/%s/%s", getenv("HOME"), PF_DIR, PF_NAME);
481 if ((pf = fopen(pf_path, "w")) == NULL) {
482 *pf_path_return = pf_path;
486 fputs("# Configuration file for Ethereal " VERSION ".\n"
488 "# This file is regenerated each time preferences are saved within\n"
489 "# Ethereal. Making manual changes should be safe, however.\n"
491 "######## Printing ########\n"
494 fprintf (pf, "# Can be one of \"text\" or \"postscript\".\n"
495 "print.format: %s\n\n", pr_formats[prefs.pr_format]);
497 fprintf (pf, "# Can be one of \"command\" or \"file\".\n"
498 "print.destination: %s\n\n", pr_dests[prefs.pr_dest]);
500 fprintf (pf, "# This is the file that gets written to when the "
501 "destination is set to \"file\"\n"
502 "%s: %s\n\n", PRS_PRINT_FILE, prefs.pr_file);
504 fprintf (pf, "# Output gets piped to this command when the destination "
505 "is set to \"command\"\n"
506 "%s: %s\n\n", PRS_PRINT_CMD, prefs.pr_cmd);
508 fprintf (pf, "# Packet list column format. Each pair of strings consists "
509 "of a column title \n# and its format.\n"
510 "%s: %s\n\n", PRS_COL_FMT, col_format_to_pref_str());
512 fprintf (pf, "# TCP stream window color preferences. Each value is a six "
513 "digit hexadecimal value in the form rrggbb.\n");
514 fprintf (pf, "%s: %02x%02x%02x\n", PRS_STREAM_CL_FG,
515 (prefs.st_client_fg.red * 255 / 65535),
516 (prefs.st_client_fg.green * 255 / 65535),
517 (prefs.st_client_fg.blue * 255 / 65535));
518 fprintf (pf, "%s: %02x%02x%02x\n", PRS_STREAM_CL_BG,
519 (prefs.st_client_bg.red * 255 / 65535),
520 (prefs.st_client_bg.green * 255 / 65535),
521 (prefs.st_client_bg.blue * 255 / 65535));
522 fprintf (pf, "%s: %02x%02x%02x\n", PRS_STREAM_SR_FG,
523 (prefs.st_server_fg.red * 255 / 65535),
524 (prefs.st_server_fg.green * 255 / 65535),
525 (prefs.st_server_fg.blue * 255 / 65535));
526 fprintf (pf, "%s: %02x%02x%02x\n", PRS_STREAM_SR_BG,
527 (prefs.st_server_bg.red * 255 / 65535),
528 (prefs.st_server_bg.green * 255 / 65535),
529 (prefs.st_server_bg.blue * 255 / 65535));
531 fprintf(pf, "\n# Vertical scrollbars should be on right side? TRUE/FALSE\n");
532 fprintf(pf, PRS_GUI_SCROLLBAR_ON_RIGHT ": %s\n",
533 prefs.gui_scrollbar_on_right == TRUE ? "TRUE" : "FALSE");
535 fprintf(pf, "\n# Packet-list selection bar can be used to browse w/o selecting? TRUE/FALSE\n");
536 fprintf(pf, PRS_GUI_PLIST_SEL_BROWSE ": %s\n",
537 prefs.gui_plist_sel_browse == TRUE ? "TRUE" : "FALSE");
539 fprintf(pf, "\n# Protocol-tree selection bar can be used to browse w/o selecting? TRUE/FALSE\n");
540 fprintf(pf, PRS_GUI_PTREE_SEL_BROWSE ": %s\n",
541 prefs.gui_ptree_sel_browse == TRUE ? "TRUE" : "FALSE");
543 fprintf(pf, "\n# Protocol-tree line style. One of: NONE, SOLID, DOTTED, TABBED\n");
544 fprintf(pf, PRS_GUI_PTREE_LINE_STYLE ": %s\n",
545 gui_ptree_line_style_text[prefs.gui_ptree_line_style]);
547 fprintf(pf, "\n# Protocol-tree expander style. One of: NONE, SQUARE, TRIANGLE, CIRCULAR\n");
548 fprintf(pf, PRS_GUI_PTREE_EXPANDER_STYLE ": %s\n",
549 gui_ptree_expander_style_text[prefs.gui_ptree_expander_style]);
553 /* XXX - catch I/O errors (e.g. "ran out of disk space") and return
554 an error indication, or maybe write to a new preferences file and
555 rename that file on top of the old one only if there are not I/O