2 * Routines for handling preferences
4 * $Id: prefs.c,v 1.46 2001/01/03 07:53:44 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>
47 #ifdef HAVE_SYS_STAT_H
61 #include "prefs-int.h"
63 /* Internal functions */
64 static int set_pref(gchar*, gchar*);
65 static GList *get_string_list(gchar *);
66 static void clear_string_list(GList *);
67 static void free_col_info(e_prefs *);
69 #define PF_NAME "preferences"
71 #define GPF_PATH DATAFILE_DIR "/ethereal.conf"
73 static gboolean init_prefs = TRUE;
74 static gchar *pf_path = NULL;
78 gchar *gui_ptree_line_style_text[] =
79 { "NONE", "SOLID", "DOTTED", "TABBED", NULL };
81 gchar *gui_ptree_expander_style_text[] =
82 { "NONE", "SQUARE", "TRIANGLE", "CIRCULAR", NULL };
84 gchar *gui_hex_dump_highlight_style_text[] =
85 { "BOLD", "INVERSE", NULL };
88 * List of modules with preference settings.
90 static GList *modules;
93 * Register a module that will have preferences.
94 * Specify the name used for the module in the preferences file, the
95 * title used in the tab for it in a preferences dialog box, and a
96 * routine to call back when we apply the preferences.
99 prefs_register_module(const char *name, const char *title,
100 void (*apply_cb)(void))
104 module = g_malloc(sizeof (module_t));
106 module->title = title;
107 module->apply_cb = apply_cb;
108 module->prefs = NULL; /* no preferences, to start */
109 module->numprefs = 0;
110 module->prefs_changed = FALSE;
112 modules = g_list_append(modules, module);
118 * Register that a protocol has preferences.
121 prefs_register_protocol(int id, void (*apply_cb)(void))
123 return prefs_register_module(proto_get_protocol_filter_name(id),
124 proto_get_protocol_short_name(id),
129 * Find a module, given its name.
132 module_match(gconstpointer a, gconstpointer b)
134 const module_t *module = a;
135 const char *name = b;
137 return strcmp(name, module->name);
141 find_module(char *name)
145 list_entry = g_list_find_custom(modules, name, module_match);
146 if (list_entry == NULL)
147 return NULL; /* no such module */
148 return (module_t *) list_entry->data;
157 do_module_callback(gpointer data, gpointer user_data)
159 module_t *module = data;
160 module_cb_arg_t *arg = user_data;
162 (*arg->callback)(module, arg->user_data);
166 * Call a callback function, with a specified argument, for each module.
169 prefs_module_foreach(module_cb callback, gpointer user_data)
173 arg.callback = callback;
174 arg.user_data = user_data;
175 g_list_foreach(modules, do_module_callback, &arg);
179 call_apply_cb(gpointer data, gpointer user_data)
181 module_t *module = data;
183 if (module->prefs_changed) {
184 if (module->apply_cb != NULL)
185 (*module->apply_cb)();
186 module->prefs_changed = FALSE;
191 * Call the "apply" callback function for each module if any of its
192 * preferences have changed, and then clear the flag saying its
193 * preferences have changed, as the module has been notified of that
197 prefs_apply_all(void)
199 g_list_foreach(modules, call_apply_cb, NULL);
203 * Register a preference in a module's list of preferences.
206 register_preference(module_t *module, const char *name, const char *title,
207 const char *description)
211 preference = g_malloc(sizeof (pref_t));
212 preference->name = name;
213 preference->title = title;
214 preference->description = description;
215 preference->ordinal = module->numprefs;
217 module->prefs = g_list_append(module->prefs, preference);
224 * Find a preference in a module's list of preferences, given the module
225 * and the preference's name.
228 preference_match(gconstpointer a, gconstpointer b)
230 const pref_t *pref = a;
231 const char *name = b;
233 return strcmp(name, pref->name);
236 static struct preference *
237 find_preference(module_t *module, char *name)
241 list_entry = g_list_find_custom(module->prefs, name, preference_match);
242 if (list_entry == NULL)
243 return NULL; /* no such preference */
244 return (struct preference *) list_entry->data;
248 * Returns TRUE if the given protocol has registered preferences
251 prefs_is_registered_protocol(char *name)
253 return (find_module(name) != NULL);
257 * Returns the module title of a registered protocol
260 prefs_get_title_by_name(char *name)
262 module_t *m = find_module(name);
263 return (m) ? m->title : NULL;
267 * Register a preference with an unsigned integral value.
270 prefs_register_uint_preference(module_t *module, const char *name,
271 const char *title, const char *description, guint base, guint *var)
275 preference = register_preference(module, name, title, description);
276 preference->type = PREF_UINT;
277 preference->varp.uint = var;
278 preference->info.base = base;
282 * Register a preference with an Boolean value.
285 prefs_register_bool_preference(module_t *module, const char *name,
286 const char *title, const char *description, gboolean *var)
290 preference = register_preference(module, name, title, description);
291 preference->type = PREF_BOOL;
292 preference->varp.bool = var;
296 * Register a preference with an enumerated value.
299 prefs_register_enum_preference(module_t *module, const char *name,
300 const char *title, const char *description, gint *var,
301 const enum_val_t *enumvals, gboolean radio_buttons)
305 preference = register_preference(module, name, title, description);
306 preference->type = PREF_ENUM;
307 preference->varp.enump = var;
308 preference->info.enum_info.enumvals = enumvals;
309 preference->info.enum_info.radio_buttons = radio_buttons;
313 * Register a preference with a character-string value.
316 prefs_register_string_preference(module_t *module, const char *name,
317 const char *title, const char *description, char **var)
321 preference = register_preference(module, name, title, description);
322 preference->type = PREF_STRING;
323 preference->varp.string = var;
324 preference->saved_val.string = NULL;
333 do_pref_callback(gpointer data, gpointer user_data)
336 pref_cb_arg_t *arg = user_data;
338 (*arg->callback)(pref, arg->user_data);
342 * Call a callback function, with a specified argument, for each preference
346 prefs_pref_foreach(module_t *module, pref_cb callback, gpointer user_data)
350 arg.callback = callback;
351 arg.user_data = user_data;
352 g_list_foreach(module->prefs, do_pref_callback, &arg);
356 * Register all non-dissector modules' preferences.
359 prefs_register_modules(void)
363 /* Parse through a list of comma-separated, quoted strings. Return a
364 list of the string data */
366 get_string_list(gchar *str) {
367 enum { PRE_QUOT, IN_QUOT, POST_QUOT };
369 gint state = PRE_QUOT, i = 0, j = 0;
370 gboolean backslash = FALSE;
371 gchar cur_c, *slstr = NULL;
374 while ((cur_c = str[i]) != '\0') {
375 if (cur_c == '"' && ! backslash) {
379 slstr = (gchar *) g_malloc(sizeof(gchar) * COL_MAX_LEN);
385 sl = g_list_append(sl, slstr);
388 clear_string_list(sl);
394 } else if (cur_c == '\\' && ! backslash) {
396 } else if (cur_c == ',' && state == POST_QUOT) {
398 } else if (state == IN_QUOT && j < COL_MAX_LEN) {
404 if (state != POST_QUOT) {
405 clear_string_list(sl);
411 clear_string_list(GList *sl) {
416 l = g_list_remove_link(l, l);
421 * Takes a string, a pointer to an array of "enum_val_t"s, and a default gint
423 * The array must be terminated by an entry with a null "name" string.
424 * If the string matches a "name" strings in an entry, the value from that
425 * entry is returned. Otherwise, the default value that was passed as the
426 * third argument is returned.
429 find_val_for_string(const char *needle, const enum_val_t *haystack,
434 while (haystack[i].name != NULL) {
435 if (strcasecmp(needle, haystack[i].name) == 0) {
436 return haystack[i].value;
440 return default_value;
443 /* Takes an string and a pointer to an array of strings, and a default int value.
444 * The array must be terminated by a NULL string. If the string is found in the array
445 * of strings, the index of that string in the array is returned. Otherwise, the
446 * default value that was passed as the third argument is returned.
449 find_index_from_string_array(char *needle, char **haystack, int default_value)
453 while (haystack[i] != NULL) {
454 if (strcmp(needle, haystack[i]) == 0) {
459 return default_value;
462 /* Preferences file format:
463 * - Configuration directives start at the beginning of the line, and
464 * are terminated with a colon.
465 * - Directives can be continued on the next line by preceding them with
470 # This is a comment line
472 print.file: /a/very/long/path/
477 #define MAX_VAR_LEN 48
478 #define MAX_VAL_LEN 1024
480 #define DEF_NUM_COLS 6
482 static void read_prefs_file(const char *pf_path, FILE *pf);
484 /* Read the preferences file, fill in "prefs", and return a pointer to it.
486 If we got an error (other than "it doesn't exist") trying to read
487 the global preferences file, stuff the errno into "*gpf_errno_return"
488 and a pointer to the path of the file into "*gpf_path_return", and
491 If we got an error (other than "it doesn't exist") trying to read
492 the user's preferences file, stuff the errno into "*pf_errno_return"
493 and a pointer to the path of the file into "*pf_path_return", and
496 read_prefs(int *gpf_errno_return, char **gpf_path_return,
497 int *pf_errno_return, char **pf_path_return)
502 gchar *col_fmt[] = {"No.", "%m", "Time", "%t",
503 "Source", "%s", "Destination", "%d",
504 "Protocol", "%p", "Info", "%i"};
508 /* Initialize preferences to wired-in default values.
509 They may be overridded by the global preferences file or the
510 user's preferences file. */
512 prefs.pr_format = PR_FMT_TEXT;
513 prefs.pr_dest = PR_DEST_CMD;
514 prefs.pr_file = g_strdup("ethereal.out");
515 prefs.pr_cmd = g_strdup("lpr");
516 prefs.col_list = NULL;
517 for (i = 0; i < DEF_NUM_COLS; i++) {
518 cfmt = (fmt_data *) g_malloc(sizeof(fmt_data));
519 cfmt->title = g_strdup(col_fmt[i * 2]);
520 cfmt->fmt = g_strdup(col_fmt[(i * 2) + 1]);
521 prefs.col_list = g_list_append(prefs.col_list, cfmt);
523 prefs.num_cols = DEF_NUM_COLS;
524 prefs.st_client_fg.pixel = 0;
525 prefs.st_client_fg.red = 32767;
526 prefs.st_client_fg.green = 0;
527 prefs.st_client_fg.blue = 0;
528 prefs.st_client_bg.pixel = 65535;
529 prefs.st_client_bg.red = 65535;
530 prefs.st_client_bg.green = 65535;
531 prefs.st_client_bg.blue = 65535;
532 prefs.st_server_fg.pixel = 0;
533 prefs.st_server_fg.red = 0;
534 prefs.st_server_fg.green = 0;
535 prefs.st_server_fg.blue = 32767;
536 prefs.st_server_bg.pixel = 65535;
537 prefs.st_server_bg.red = 65535;
538 prefs.st_server_bg.green = 65535;
539 prefs.st_server_bg.blue = 65535;
540 prefs.gui_scrollbar_on_right = TRUE;
541 prefs.gui_plist_sel_browse = FALSE;
542 prefs.gui_ptree_sel_browse = FALSE;
543 prefs.gui_ptree_line_style = 0;
544 prefs.gui_ptree_expander_style = 1;
545 prefs.gui_hex_dump_highlight_style = 1;
547 prefs.gui_font_name = g_strdup("-*-lucida console-medium-r-*-*-*-100-*-*-*-*-*-*");
550 * XXX - for now, we make the initial font name a pattern that matches
551 * only ISO 8859/1 fonts, so that we don't match 2-byte fonts such
552 * as ISO 10646 fonts.
554 * Users in locales using other one-byte fonts will have to choose
555 * a different font from the preferences dialog - or put the font
556 * selection in the global preferences file to make that font the
557 * default for all users who don't explicitly specify a different
560 * Making this a font set rather than a font has two problems:
562 * 1) as far as I know, you can't select font sets with the
563 * font selection dialog;
565 * 2) if you use a font set, the text to be drawn must be a
566 * multi-byte string in the appropriate locale, but
567 * Ethereal does *NOT* guarantee that's the case - in
568 * the hex-dump window, each character in the text portion
569 * of the display must be a *single* byte, and in the
570 * packet-list and protocol-tree windows, text extracted
571 * from the packet is not necessarily in the right format.
573 * "Doing this right" may, for the packet-list and protocol-tree
574 * windows, require that dissectors know what the locale is
575 * *AND* know what locale and text representation is used in
576 * the packets they're dissecting, and may be impossible in
577 * the hex-dump window (except by punting and displaying only
580 * GTK+ 2.0 may simplify part of the problem, as it will, as I
581 * understand it, use UTF-8-encoded Unicode as its internal
582 * character set; however, we'd still have to know whatever
583 * character set and encoding is used in the packet (which
584 * may differ for different protocols, e.g. SMB might use
585 * PC code pages for some strings and Unicode for others, whilst
586 * NFS might use some UNIX character set encoding, e.g. ISO 8859/x,
587 * or one of the EUC character sets for Asian languages, or one
588 * of the other multi-byte character sets, or UTF-8, or...).
590 * I.e., as far as I can tell, "internationalizing" the packet-list,
591 * protocol-tree, and hex-dump windows involves a lot more than, say,
592 * just using font sets rather than fonts.
594 prefs.gui_font_name = g_strdup("-*-fixed-medium-r-semicondensed-*-*-120-*-*-*-*-iso8859-1");
596 prefs.gui_marked_fg.pixel = 65535;
597 prefs.gui_marked_fg.red = 65535;
598 prefs.gui_marked_fg.green = 65535;
599 prefs.gui_marked_fg.blue = 65535;
600 prefs.gui_marked_bg.pixel = 0;
601 prefs.gui_marked_bg.red = 0;
602 prefs.gui_marked_bg.green = 0;
603 prefs.gui_marked_bg.blue = 0;
607 /* Read the global preferences file, if it exists. */
608 *gpf_path_return = NULL;
609 if ((pf = fopen(GPF_PATH, "r")) != NULL) {
610 /* We succeeded in opening it; read it. */
611 read_prefs_file(GPF_PATH, pf);
614 /* We failed to open it. If we failed for some reason other than
615 "it doesn't exist", return the errno and the pathname, so our
616 caller can report the error. */
617 if (errno != ENOENT) {
618 *gpf_errno_return = errno;
619 *gpf_path_return = GPF_PATH;
623 /* Construct the pathname of the user's preferences file. */
625 pf_path = (gchar *) g_malloc(strlen(get_home_dir()) + strlen(PF_DIR) +
626 strlen(PF_NAME) + 4);
627 sprintf(pf_path, "%s/%s/%s", get_home_dir(), PF_DIR, PF_NAME);
630 /* Read the user's preferences file, if it exists. */
631 *pf_path_return = NULL;
632 if ((pf = fopen(pf_path, "r")) != NULL) {
633 /* We succeeded in opening it; read it. */
634 read_prefs_file(pf_path, pf);
637 /* We failed to open it. If we failed for some reason other than
638 "it doesn't exist", return the errno and the pathname, so our
639 caller can report the error. */
640 if (errno != ENOENT) {
641 *pf_errno_return = errno;
642 *pf_path_return = pf_path;
650 read_prefs_file(const char *pf_path, FILE *pf)
652 enum { START, IN_VAR, PRE_VAL, IN_VAL, IN_SKIP };
653 gchar cur_var[MAX_VAR_LEN], cur_val[MAX_VAL_LEN];
654 int got_c, state = START;
655 gboolean got_val = FALSE;
656 gint var_len = 0, val_len = 0, fline = 1, pline = 1;
658 while ((got_c = getc(pf)) != EOF) {
664 if (var_len >= MAX_VAR_LEN) {
665 g_warning ("%s line %d: Variable too long", pf_path, fline);
670 if (val_len >= MAX_VAL_LEN) {
671 g_warning ("%s line %d: Value too long", pf_path, fline);
679 if (isalnum(got_c)) {
682 cur_var[var_len] = '\0';
683 cur_val[val_len] = '\0';
684 switch (set_pref(cur_var, cur_val)) {
686 case PREFS_SET_SYNTAX_ERR:
687 g_warning ("%s line %d: Syntax error", pf_path, pline);
690 case PREFS_SET_NO_SUCH_PREF:
691 g_warning ("%s line %d: No such preference \"%s\"", pf_path,
696 g_warning ("%s line %d: Incomplete preference", pf_path, pline);
704 } else if (isspace(got_c) && var_len > 0 && got_val) {
706 } else if (got_c == '#') {
709 g_warning ("%s line %d: Malformed line", pf_path, fline);
714 cur_var[var_len] = got_c;
723 if (!isspace(got_c)) {
725 cur_val[val_len] = got_c;
731 cur_val[val_len] = got_c;
734 while (isspace((guchar)cur_val[val_len]) && val_len > 0)
743 cur_var[var_len] = '\0';
744 cur_val[val_len] = '\0';
745 switch (set_pref(cur_var, cur_val)) {
747 case PREFS_SET_SYNTAX_ERR:
748 g_warning ("%s line %d: Syntax error", pf_path, pline);
751 case PREFS_SET_NO_SUCH_PREF:
752 g_warning ("%s line %d: No such preference \"%s\"", pf_path,
757 g_warning ("%s line %d: Incomplete preference", pf_path, pline);
763 * Given a string of the form "<pref name>:<pref value>", as might appear
764 * as an argument to a "-o" option, parse it and set the preference in
765 * question. Return an indication of whether it succeeded or failed
769 prefs_set_pref(char *prefarg)
774 colonp = strchr(prefarg, ':');
776 return PREFS_SET_SYNTAX_ERR;
782 * Skip over any white space (there probably won't be any, but
783 * as we allow it in the preferences file, we might as well
790 * Put the colon back, so if our caller uses, in an
791 * error message, the string they passed us, the message
795 return PREFS_SET_SYNTAX_ERR;
798 ret = set_pref(prefarg, p);
799 *colonp = ':'; /* put the colon back */
803 #define PRS_PRINT_FMT "print.format"
804 #define PRS_PRINT_DEST "print.destination"
805 #define PRS_PRINT_FILE "print.file"
806 #define PRS_PRINT_CMD "print.command"
807 #define PRS_COL_FMT "column.format"
808 #define PRS_STREAM_CL_FG "stream.client.fg"
809 #define PRS_STREAM_CL_BG "stream.client.bg"
810 #define PRS_STREAM_SR_FG "stream.server.fg"
811 #define PRS_STREAM_SR_BG "stream.server.bg"
812 #define PRS_GUI_SCROLLBAR_ON_RIGHT "gui.scrollbar_on_right"
813 #define PRS_GUI_PLIST_SEL_BROWSE "gui.packet_list_sel_browse"
814 #define PRS_GUI_PTREE_SEL_BROWSE "gui.protocol_tree_sel_browse"
815 #define PRS_GUI_PTREE_LINE_STYLE "gui.protocol_tree_line_style"
816 #define PRS_GUI_PTREE_EXPANDER_STYLE "gui.protocol_tree_expander_style"
817 #define PRS_GUI_HEX_DUMP_HIGHLIGHT_STYLE "gui.hex_dump_highlight_style"
818 #define PRS_GUI_FONT_NAME "gui.font_name"
819 #define PRS_GUI_MARKED_FG "gui.marked_frame.fg"
820 #define PRS_GUI_MARKED_BG "gui.marked_frame.bg"
822 #define RED_COMPONENT(x) ((((x) >> 16) & 0xff) * 65535 / 255)
823 #define GREEN_COMPONENT(x) ((((x) >> 8) & 0xff) * 65535 / 255)
824 #define BLUE_COMPONENT(x) (((x) & 0xff) * 65535 / 255)
826 static gchar *pr_formats[] = { "text", "postscript" };
827 static gchar *pr_dests[] = { "command", "file" };
830 set_pref(gchar *pref_name, gchar *value)
835 unsigned long int cval;
844 if (strcmp(pref_name, PRS_PRINT_FMT) == 0) {
845 if (strcmp(value, pr_formats[PR_FMT_TEXT]) == 0) {
846 prefs.pr_format = PR_FMT_TEXT;
847 } else if (strcmp(value, pr_formats[PR_FMT_PS]) == 0) {
848 prefs.pr_format = PR_FMT_PS;
850 return PREFS_SET_SYNTAX_ERR;
852 } else if (strcmp(pref_name, PRS_PRINT_DEST) == 0) {
853 if (strcmp(value, pr_dests[PR_DEST_CMD]) == 0) {
854 prefs.pr_dest = PR_DEST_CMD;
855 } else if (strcmp(value, pr_dests[PR_DEST_FILE]) == 0) {
856 prefs.pr_dest = PR_DEST_FILE;
858 return PREFS_SET_SYNTAX_ERR;
860 } else if (strcmp(pref_name, PRS_PRINT_FILE) == 0) {
861 if (prefs.pr_file) g_free(prefs.pr_file);
862 prefs.pr_file = g_strdup(value);
863 } else if (strcmp(pref_name, PRS_PRINT_CMD) == 0) {
864 if (prefs.pr_cmd) g_free(prefs.pr_cmd);
865 prefs.pr_cmd = g_strdup(value);
866 } else if (strcmp(pref_name, PRS_COL_FMT) == 0) {
867 if ((col_l = get_string_list(value)) && (g_list_length(col_l) % 2) == 0) {
868 free_col_info(&prefs);
869 prefs.col_list = NULL;
870 llen = g_list_length(col_l);
871 prefs.num_cols = llen / 2;
872 col_l = g_list_first(col_l);
874 cfmt = (fmt_data *) g_malloc(sizeof(fmt_data));
875 cfmt->title = g_strdup(col_l->data);
877 cfmt->fmt = g_strdup(col_l->data);
879 prefs.col_list = g_list_append(prefs.col_list, cfmt);
881 /* To do: else print some sort of error? */
883 clear_string_list(col_l);
884 } else if (strcmp(pref_name, PRS_STREAM_CL_FG) == 0) {
885 cval = strtoul(value, NULL, 16);
886 prefs.st_client_fg.pixel = 0;
887 prefs.st_client_fg.red = RED_COMPONENT(cval);
888 prefs.st_client_fg.green = GREEN_COMPONENT(cval);
889 prefs.st_client_fg.blue = BLUE_COMPONENT(cval);
890 } else if (strcmp(pref_name, PRS_STREAM_CL_BG) == 0) {
891 cval = strtoul(value, NULL, 16);
892 prefs.st_client_bg.pixel = 0;
893 prefs.st_client_bg.red = RED_COMPONENT(cval);
894 prefs.st_client_bg.green = GREEN_COMPONENT(cval);
895 prefs.st_client_bg.blue = BLUE_COMPONENT(cval);
896 } else if (strcmp(pref_name, PRS_STREAM_SR_FG) == 0) {
897 cval = strtoul(value, NULL, 16);
898 prefs.st_server_fg.pixel = 0;
899 prefs.st_server_fg.red = RED_COMPONENT(cval);
900 prefs.st_server_fg.green = GREEN_COMPONENT(cval);
901 prefs.st_server_fg.blue = BLUE_COMPONENT(cval);
902 } else if (strcmp(pref_name, PRS_STREAM_SR_BG) == 0) {
903 cval = strtoul(value, NULL, 16);
904 prefs.st_server_bg.pixel = 0;
905 prefs.st_server_bg.red = RED_COMPONENT(cval);
906 prefs.st_server_bg.green = GREEN_COMPONENT(cval);
907 prefs.st_server_bg.blue = BLUE_COMPONENT(cval);
908 } else if (strcmp(pref_name, PRS_GUI_SCROLLBAR_ON_RIGHT) == 0) {
909 if (strcmp(value, "TRUE") == 0) {
910 prefs.gui_scrollbar_on_right = TRUE;
913 prefs.gui_scrollbar_on_right = FALSE;
915 } else if (strcmp(pref_name, PRS_GUI_PLIST_SEL_BROWSE) == 0) {
916 if (strcmp(value, "TRUE") == 0) {
917 prefs.gui_plist_sel_browse = TRUE;
920 prefs.gui_plist_sel_browse = FALSE;
922 } else if (strcmp(pref_name, PRS_GUI_PTREE_SEL_BROWSE) == 0) {
923 if (strcmp(value, "TRUE") == 0) {
924 prefs.gui_ptree_sel_browse = TRUE;
927 prefs.gui_ptree_sel_browse = FALSE;
929 } else if (strcmp(pref_name, PRS_GUI_PTREE_LINE_STYLE) == 0) {
930 prefs.gui_ptree_line_style =
931 find_index_from_string_array(value, gui_ptree_line_style_text, 0);
932 } else if (strcmp(pref_name, PRS_GUI_PTREE_EXPANDER_STYLE) == 0) {
933 prefs.gui_ptree_expander_style =
934 find_index_from_string_array(value, gui_ptree_expander_style_text, 1);
935 } else if (strcmp(pref_name, PRS_GUI_HEX_DUMP_HIGHLIGHT_STYLE) == 0) {
936 prefs.gui_hex_dump_highlight_style =
937 find_index_from_string_array(value, gui_hex_dump_highlight_style_text, 1);
938 } else if (strcmp(pref_name, PRS_GUI_FONT_NAME) == 0) {
939 if (prefs.gui_font_name != NULL)
940 g_free(prefs.gui_font_name);
941 prefs.gui_font_name = g_strdup(value);
942 } else if (strcmp(pref_name, PRS_GUI_MARKED_FG) == 0) {
943 cval = strtoul(value, NULL, 16);
944 prefs.gui_marked_fg.pixel = 0;
945 prefs.gui_marked_fg.red = RED_COMPONENT(cval);
946 prefs.gui_marked_fg.green = GREEN_COMPONENT(cval);
947 prefs.gui_marked_fg.blue = BLUE_COMPONENT(cval);
948 } else if (strcmp(pref_name, PRS_GUI_MARKED_BG) == 0) {
949 cval = strtoul(value, NULL, 16);
950 prefs.gui_marked_bg.pixel = 0;
951 prefs.gui_marked_bg.red = RED_COMPONENT(cval);
952 prefs.gui_marked_bg.green = GREEN_COMPONENT(cval);
953 prefs.gui_marked_bg.blue = BLUE_COMPONENT(cval);
955 /* To which module does this preference belong? */
956 dotp = strchr(pref_name, '.');
958 return PREFS_SET_SYNTAX_ERR; /* no ".", so no module/name separator */
959 *dotp = '\0'; /* separate module and preference name */
960 module = find_module(pref_name);
963 * XXX - "Diameter" rather than "diameter" was used in earlier
964 * versions of Ethereal; if we didn't find the module, and its name
965 * was "Diameter", look for "diameter" instead.
967 if (module == NULL && strcmp(pref_name, "Diameter") == 0)
968 module = find_module("diameter");
969 *dotp = '.'; /* put the preference string back */
971 return PREFS_SET_NO_SUCH_PREF; /* no such module */
972 dotp++; /* skip past separator to preference name */
973 pref = find_preference(module, dotp);
975 return PREFS_SET_NO_SUCH_PREF; /* no such preference */
977 switch (pref->type) {
980 uval = strtoul(value, &p, pref->info.base);
981 if (p == value || *p != '\0')
982 return PREFS_SET_SYNTAX_ERR; /* number was bad */
983 if (*pref->varp.uint != uval) {
984 module->prefs_changed = TRUE;
985 *pref->varp.uint = uval;
990 /* XXX - give an error if it's neither "true" nor "false"? */
991 if (strcasecmp(value, "true") == 0)
995 if (*pref->varp.bool != bval) {
996 module->prefs_changed = TRUE;
997 *pref->varp.bool = bval;
1002 /* XXX - give an error if it doesn't match? */
1003 enum_val = find_val_for_string(value,
1004 pref->info.enum_info.enumvals, 1);
1005 if (*pref->varp.enump != enum_val) {
1006 module->prefs_changed = TRUE;
1007 *pref->varp.enump = enum_val;
1012 if (*pref->varp.string == NULL || strcmp(*pref->varp.string, value) != 0) {
1013 module->prefs_changed = TRUE;
1014 if (*pref->varp.string != NULL)
1015 g_free(*pref->varp.string);
1016 *pref->varp.string = g_strdup(value);
1022 return PREFS_SET_OK;
1031 * Write out a single preference.
1034 write_pref(gpointer data, gpointer user_data)
1036 pref_t *pref = data;
1037 write_pref_arg_t *arg = user_data;
1038 const enum_val_t *enum_valp;
1039 const char *val_string;
1041 fprintf(arg->pf, "\n# %s\n", pref->description);
1043 switch (pref->type) {
1046 switch (pref->info.base) {
1049 fprintf(arg->pf, "# A decimal number.\n");
1050 fprintf(arg->pf, "%s.%s: %u\n", arg->module->name,
1051 pref->name, *pref->varp.uint);
1055 fprintf(arg->pf, "# An octal number.\n");
1056 fprintf(arg->pf, "%s.%s: %#o\n", arg->module->name,
1057 pref->name, *pref->varp.uint);
1061 fprintf(arg->pf, "# A hexadecimal number.\n");
1062 fprintf(arg->pf, "%s.%s: %#x\n", arg->module->name,
1063 pref->name, *pref->varp.uint);
1069 fprintf(arg->pf, "# TRUE or FALSE (case-insensitive).\n");
1070 fprintf(arg->pf, "%s.%s: %s\n", arg->module->name, pref->name,
1071 *pref->varp.bool ? "TRUE" : "FALSE");
1075 fprintf(arg->pf, "# One of: ");
1076 enum_valp = pref->info.enum_info.enumvals;
1078 while (enum_valp->name != NULL) {
1079 if (enum_valp->value == *pref->varp.enump)
1080 val_string = enum_valp->name;
1081 fprintf(arg->pf, "%s", enum_valp->name);
1083 if (enum_valp->name == NULL)
1084 fprintf(arg->pf, "\n");
1086 fprintf(arg->pf, ", ");
1088 fprintf(arg->pf, "# (case-insensitive).\n");
1089 fprintf(arg->pf, "%s.%s: %s\n", arg->module->name, pref->name,
1094 fprintf(arg->pf, "# A string.\n");
1095 fprintf(arg->pf, "%s.%s: %s\n", arg->module->name, pref->name,
1096 *pref->varp.string);
1102 write_module_prefs(gpointer data, gpointer user_data)
1104 write_pref_arg_t arg;
1108 g_list_foreach(arg.module->prefs, write_pref, &arg);
1111 /* Write out "prefs" to the user's preferences file, and return 0.
1113 If we got an error, stuff a pointer to the path of the preferences file
1114 into "*pf_path_return", and return the errno. */
1116 write_prefs(char **pf_path_return)
1122 * - Split output lines longer than MAX_VAL_LEN
1123 * - Create a function for the preference directory check/creation
1124 * so that duplication can be avoided with filter.c
1128 pf_path = (gchar *) g_malloc(strlen(get_home_dir()) + strlen(PF_DIR) +
1129 strlen(PF_NAME) + 4);
1132 sprintf(pf_path, "%s/%s", get_home_dir(), PF_DIR);
1133 if (stat(pf_path, &s_buf) != 0)
1137 mkdir(pf_path, 0755);
1140 sprintf(pf_path, "%s/%s/%s", get_home_dir(), PF_DIR, PF_NAME);
1141 if ((pf = fopen(pf_path, "w")) == NULL) {
1142 *pf_path_return = pf_path;
1146 fputs("# Configuration file for Ethereal " VERSION ".\n"
1148 "# This file is regenerated each time preferences are saved within\n"
1149 "# Ethereal. Making manual changes should be safe, however.\n"
1151 "######## Printing ########\n"
1154 fprintf (pf, "# Can be one of \"text\" or \"postscript\".\n"
1155 "print.format: %s\n\n", pr_formats[prefs.pr_format]);
1157 fprintf (pf, "# Can be one of \"command\" or \"file\".\n"
1158 "print.destination: %s\n\n", pr_dests[prefs.pr_dest]);
1160 fprintf (pf, "# This is the file that gets written to when the "
1161 "destination is set to \"file\"\n"
1162 "%s: %s\n\n", PRS_PRINT_FILE, prefs.pr_file);
1164 fprintf (pf, "# Output gets piped to this command when the destination "
1165 "is set to \"command\"\n"
1166 "%s: %s\n\n", PRS_PRINT_CMD, prefs.pr_cmd);
1168 fprintf (pf, "# Packet list column format. Each pair of strings consists "
1169 "of a column title \n# and its format.\n"
1170 "%s: %s\n\n", PRS_COL_FMT, col_format_to_pref_str());
1172 fprintf (pf, "# TCP stream window color preferences. Each value is a six "
1173 "digit hexadecimal value in the form rrggbb.\n");
1174 fprintf (pf, "%s: %02x%02x%02x\n", PRS_STREAM_CL_FG,
1175 (prefs.st_client_fg.red * 255 / 65535),
1176 (prefs.st_client_fg.green * 255 / 65535),
1177 (prefs.st_client_fg.blue * 255 / 65535));
1178 fprintf (pf, "%s: %02x%02x%02x\n", PRS_STREAM_CL_BG,
1179 (prefs.st_client_bg.red * 255 / 65535),
1180 (prefs.st_client_bg.green * 255 / 65535),
1181 (prefs.st_client_bg.blue * 255 / 65535));
1182 fprintf (pf, "%s: %02x%02x%02x\n", PRS_STREAM_SR_FG,
1183 (prefs.st_server_fg.red * 255 / 65535),
1184 (prefs.st_server_fg.green * 255 / 65535),
1185 (prefs.st_server_fg.blue * 255 / 65535));
1186 fprintf (pf, "%s: %02x%02x%02x\n", PRS_STREAM_SR_BG,
1187 (prefs.st_server_bg.red * 255 / 65535),
1188 (prefs.st_server_bg.green * 255 / 65535),
1189 (prefs.st_server_bg.blue * 255 / 65535));
1191 fprintf(pf, "\n# Vertical scrollbars should be on right side? TRUE/FALSE\n");
1192 fprintf(pf, PRS_GUI_SCROLLBAR_ON_RIGHT ": %s\n",
1193 prefs.gui_scrollbar_on_right == TRUE ? "TRUE" : "FALSE");
1195 fprintf(pf, "\n# Packet-list selection bar can be used to browse w/o selecting? TRUE/FALSE\n");
1196 fprintf(pf, PRS_GUI_PLIST_SEL_BROWSE ": %s\n",
1197 prefs.gui_plist_sel_browse == TRUE ? "TRUE" : "FALSE");
1199 fprintf(pf, "\n# Protocol-tree selection bar can be used to browse w/o selecting? TRUE/FALSE\n");
1200 fprintf(pf, PRS_GUI_PTREE_SEL_BROWSE ": %s\n",
1201 prefs.gui_ptree_sel_browse == TRUE ? "TRUE" : "FALSE");
1203 fprintf(pf, "\n# Protocol-tree line style. One of: NONE, SOLID, DOTTED, TABBED\n");
1204 fprintf(pf, PRS_GUI_PTREE_LINE_STYLE ": %s\n",
1205 gui_ptree_line_style_text[prefs.gui_ptree_line_style]);
1207 fprintf(pf, "\n# Protocol-tree expander style. One of: NONE, SQUARE, TRIANGLE, CIRCULAR\n");
1208 fprintf(pf, PRS_GUI_PTREE_EXPANDER_STYLE ": %s\n",
1209 gui_ptree_expander_style_text[prefs.gui_ptree_expander_style]);
1211 fprintf(pf, "\n# Hex dump highlight style. One of: BOLD, INVERSE\n");
1212 fprintf(pf, PRS_GUI_HEX_DUMP_HIGHLIGHT_STYLE ": %s\n",
1213 gui_hex_dump_highlight_style_text[prefs.gui_hex_dump_highlight_style]);
1215 fprintf(pf, "\n# Font name for packet list, protocol tree, and hex dump panes.\n");
1216 fprintf(pf, PRS_GUI_FONT_NAME ": %s\n", prefs.gui_font_name);
1218 fprintf (pf, "\n# Color preferences for a marked frame. Each value is a six "
1219 "digit hexadecimal value in the form rrggbb.\n");
1220 fprintf (pf, "%s: %02x%02x%02x\n", PRS_GUI_MARKED_FG,
1221 (prefs.gui_marked_fg.red * 255 / 65535),
1222 (prefs.gui_marked_fg.green * 255 / 65535),
1223 (prefs.gui_marked_fg.blue * 255 / 65535));
1224 fprintf (pf, "%s: %02x%02x%02x\n", PRS_GUI_MARKED_BG,
1225 (prefs.gui_marked_bg.red * 255 / 65535),
1226 (prefs.gui_marked_bg.green * 255 / 65535),
1227 (prefs.gui_marked_bg.blue * 255 / 65535));
1229 g_list_foreach(modules, write_module_prefs, pf);
1233 /* XXX - catch I/O errors (e.g. "ran out of disk space") and return
1234 an error indication, or maybe write to a new preferences file and
1235 rename that file on top of the old one only if there are not I/O
1240 /* Copy a set of preferences. */
1242 copy_prefs(e_prefs *dest, e_prefs *src)
1244 fmt_data *src_cfmt, *dest_cfmt;
1247 dest->pr_format = src->pr_format;
1248 dest->pr_dest = src->pr_dest;
1249 dest->pr_file = g_strdup(src->pr_file);
1250 dest->pr_cmd = g_strdup(src->pr_cmd);
1251 dest->col_list = NULL;
1252 for (entry = src->col_list; entry != NULL; entry = g_list_next(entry)) {
1253 src_cfmt = entry->data;
1254 dest_cfmt = (fmt_data *) g_malloc(sizeof(fmt_data));
1255 dest_cfmt->title = g_strdup(src_cfmt->title);
1256 dest_cfmt->fmt = g_strdup(src_cfmt->fmt);
1257 dest->col_list = g_list_append(dest->col_list, dest_cfmt);
1259 dest->num_cols = src->num_cols;
1260 dest->st_client_fg = src->st_client_fg;
1261 dest->st_client_bg = src->st_client_bg;
1262 dest->st_server_fg = src->st_server_fg;
1263 dest->st_server_bg = src->st_server_bg;
1264 dest->gui_scrollbar_on_right = src->gui_scrollbar_on_right;
1265 dest->gui_plist_sel_browse = src->gui_plist_sel_browse;
1266 dest->gui_ptree_sel_browse = src->gui_ptree_sel_browse;
1267 dest->gui_ptree_line_style = src->gui_ptree_line_style;
1268 dest->gui_ptree_expander_style = src->gui_ptree_expander_style;
1269 dest->gui_hex_dump_highlight_style = src->gui_hex_dump_highlight_style;
1270 dest->gui_font_name = g_strdup(src->gui_font_name);
1271 dest->gui_marked_fg = src->gui_marked_fg;
1272 dest->gui_marked_bg = src->gui_marked_bg;
1275 /* Free a set of preferences. */
1277 free_prefs(e_prefs *pr)
1279 if (pr->pr_file != NULL) {
1280 g_free(pr->pr_file);
1283 if (pr->pr_cmd != NULL) {
1288 if (pr->gui_font_name != NULL) {
1289 g_free(pr->gui_font_name);
1290 pr->gui_font_name = NULL;
1295 free_col_info(e_prefs *pr)
1299 while (pr->col_list != NULL) {
1300 cfmt = pr->col_list->data;
1301 g_free(cfmt->title);
1304 pr->col_list = g_list_remove_link(pr->col_list, pr->col_list);
1306 g_list_free(pr->col_list);
1307 pr->col_list = NULL;