2 * Routines for handling preferences
4 * $Id: prefs.c,v 1.81 2002/03/31 21:05:11 guy Exp $
6 * Ethereal - Network traffic analyzer
7 * By Gerald Combs <gerald@ethereal.com>
8 * Copyright 1998 Gerald Combs
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License
12 * as published by the Free Software Foundation; either version 2
13 * of the License, or (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
29 #ifdef HAVE_SYS_TYPES_H
30 #include <sys/types.h>
44 #include <epan/filesystem.h>
46 #include <epan/resolv.h>
47 #include <epan/packet.h>
50 #include <epan/proto.h>
54 #include "prefs-int.h"
56 /* Internal functions */
57 static module_t *find_module(const char *name);
58 static struct preference *find_preference(module_t *, const char *);
59 static int set_pref(gchar*, gchar*);
60 static GList *get_string_list(gchar *);
61 static gchar *put_string_list(GList *);
62 static void clear_string_list(GList *);
63 static void free_col_info(e_prefs *);
65 #define GPF_NAME "ethereal.conf"
66 #define PF_NAME "preferences"
68 static gboolean init_prefs = TRUE;
69 static gchar *gpf_path = NULL;
72 * XXX - variables to allow us to attempt to interpret the first
73 * "mgcp.{tcp,udp}.port" in a preferences file as
74 * "mgcp.{tcp,udp}.gateway_port" and the second as
75 * "mgcp.{tcp,udp}.callagent_port".
77 static int mgcp_tcp_port_count;
78 static int mgcp_udp_port_count;
82 gchar *gui_ptree_line_style_text[] =
83 { "NONE", "SOLID", "DOTTED", "TABBED", NULL };
85 gchar *gui_ptree_expander_style_text[] =
86 { "NONE", "SQUARE", "TRIANGLE", "CIRCULAR", NULL };
88 gchar *gui_hex_dump_highlight_style_text[] =
89 { "BOLD", "INVERSE", NULL };
92 * List of modules with preference settings.
94 static GList *modules;
97 module_compare_name(gconstpointer p1_arg, gconstpointer p2_arg)
99 const module_t *p1 = p1_arg;
100 const module_t *p2 = p2_arg;
102 return g_strcasecmp(p1->name, p2->name);
106 * Register a module that will have preferences.
107 * Specify the name used for the module in the preferences file, the
108 * title used in the tab for it in a preferences dialog box, and a
109 * routine to call back when we apply the preferences.
112 prefs_register_module(const char *name, const char *title,
113 void (*apply_cb)(void))
118 module = g_malloc(sizeof (module_t));
120 module->title = title;
121 module->apply_cb = apply_cb;
122 module->prefs = NULL; /* no preferences, to start */
123 module->numprefs = 0;
124 module->prefs_changed = FALSE;
127 * Make sure that only lower-case ASCII letters, numbers,
128 * underscores, and dots appear in the module name.
130 * Crash if there is, as that's an error in the code;
131 * you can make the title a nice string with capitalization,
132 * white space, punctuation, etc., but the name can be used
133 * on the command line, and shouldn't require quoting,
136 for (p = name; *p != '\0'; p++)
137 g_assert(isascii(*p) &&
138 (islower(*p) || isdigit(*p) || *p == '_' || *p == '.'));
141 * Make sure there's not already a module with that
142 * name. Crash if there is, as that's an error in the
143 * code, and the code has to be fixed not to register
144 * more than one module with the same name.
146 g_assert(find_module(name) == NULL);
148 modules = g_list_insert_sorted(modules, module, module_compare_name);
154 * Register that a protocol has preferences.
157 prefs_register_protocol(int id, void (*apply_cb)(void))
159 return prefs_register_module(proto_get_protocol_filter_name(id),
160 proto_get_protocol_short_name(id),
165 * Find a module, given its name.
168 module_match(gconstpointer a, gconstpointer b)
170 const module_t *module = a;
171 const char *name = b;
173 return strcmp(name, module->name);
177 find_module(const char *name)
181 list_entry = g_list_find_custom(modules, (gpointer)name, module_match);
182 if (list_entry == NULL)
183 return NULL; /* no such module */
184 return (module_t *) list_entry->data;
193 do_module_callback(gpointer data, gpointer user_data)
195 module_t *module = data;
196 module_cb_arg_t *arg = user_data;
198 (*arg->callback)(module, arg->user_data);
202 * Call a callback function, with a specified argument, for each module.
205 prefs_module_foreach(module_cb callback, gpointer user_data)
209 arg.callback = callback;
210 arg.user_data = user_data;
211 g_list_foreach(modules, do_module_callback, &arg);
215 call_apply_cb(gpointer data, gpointer user_data _U_)
217 module_t *module = data;
219 if (module->prefs_changed) {
220 if (module->apply_cb != NULL)
221 (*module->apply_cb)();
222 module->prefs_changed = FALSE;
227 * Call the "apply" callback function for each module if any of its
228 * preferences have changed, and then clear the flag saying its
229 * preferences have changed, as the module has been notified of that
233 prefs_apply_all(void)
235 g_list_foreach(modules, call_apply_cb, NULL);
239 * Register a preference in a module's list of preferences.
242 register_preference(module_t *module, const char *name, const char *title,
243 const char *description)
248 preference = g_malloc(sizeof (pref_t));
249 preference->name = name;
250 preference->title = title;
251 preference->description = description;
252 preference->ordinal = module->numprefs;
255 * Make sure that only lower-case ASCII letters, numbers,
256 * underscores, and dots appear in the preference name.
258 * Crash if there is, as that's an error in the code;
259 * you can make the title and description nice strings
260 * with capitalization, white space, punctuation, etc.,
261 * but the name can be used on the command line,
262 * and shouldn't require quoting, shifting, etc.
264 for (p = name; *p != '\0'; p++)
265 g_assert(isascii(*p) &&
266 (islower(*p) || isdigit(*p) || *p == '_' || *p == '.'));
269 * Make sure there's not already a preference with that
270 * name. Crash if there is, as that's an error in the
271 * code, and the code has to be fixed not to register
272 * more than one preference with the same name.
274 g_assert(find_preference(module, name) == NULL);
277 * There isn't already one with that name, so add the
280 module->prefs = g_list_append(module->prefs, preference);
287 * Find a preference in a module's list of preferences, given the module
288 * and the preference's name.
291 preference_match(gconstpointer a, gconstpointer b)
293 const pref_t *pref = a;
294 const char *name = b;
296 return strcmp(name, pref->name);
299 static struct preference *
300 find_preference(module_t *module, const char *name)
304 list_entry = g_list_find_custom(module->prefs, (gpointer)name,
306 if (list_entry == NULL)
307 return NULL; /* no such preference */
308 return (struct preference *) list_entry->data;
312 * Returns TRUE if the given protocol has registered preferences
315 prefs_is_registered_protocol(char *name)
317 return (find_module(name) != NULL);
321 * Returns the module title of a registered protocol
324 prefs_get_title_by_name(char *name)
326 module_t *m = find_module(name);
327 return (m) ? m->title : NULL;
331 * Register a preference with an unsigned integral value.
334 prefs_register_uint_preference(module_t *module, const char *name,
335 const char *title, const char *description, guint base, guint *var)
339 preference = register_preference(module, name, title, description);
340 preference->type = PREF_UINT;
341 preference->varp.uint = var;
342 preference->info.base = base;
346 * Register a preference with an Boolean value.
349 prefs_register_bool_preference(module_t *module, const char *name,
350 const char *title, const char *description, gboolean *var)
354 preference = register_preference(module, name, title, description);
355 preference->type = PREF_BOOL;
356 preference->varp.bool = var;
360 * Register a preference with an enumerated value.
363 prefs_register_enum_preference(module_t *module, const char *name,
364 const char *title, const char *description, gint *var,
365 const enum_val_t *enumvals, gboolean radio_buttons)
369 preference = register_preference(module, name, title, description);
370 preference->type = PREF_ENUM;
371 preference->varp.enump = var;
372 preference->info.enum_info.enumvals = enumvals;
373 preference->info.enum_info.radio_buttons = radio_buttons;
377 * Register a preference with a character-string value.
380 prefs_register_string_preference(module_t *module, const char *name,
381 const char *title, const char *description, char **var)
385 preference = register_preference(module, name, title, description);
386 preference->type = PREF_STRING;
387 preference->varp.string = var;
388 preference->saved_val.string = NULL;
392 * Register a preference that used to be supported but no longer is.
395 prefs_register_obsolete_preference(module_t *module, const char *name)
399 preference = register_preference(module, name, NULL, NULL);
400 preference->type = PREF_OBSOLETE;
409 do_pref_callback(gpointer data, gpointer user_data)
412 pref_cb_arg_t *arg = user_data;
414 if (pref->type == PREF_OBSOLETE) {
416 * This preference is no longer supported; it's not a
417 * real preference, so we don't call the callback for
418 * it (i.e., we treat it as if it weren't found in the
419 * list of preferences, and we weren't called in the
425 (*arg->callback)(pref, arg->user_data);
429 * Call a callback function, with a specified argument, for each preference
433 prefs_pref_foreach(module_t *module, pref_cb callback, gpointer user_data)
437 arg.callback = callback;
438 arg.user_data = user_data;
439 g_list_foreach(module->prefs, do_pref_callback, &arg);
443 * Register all non-dissector modules' preferences.
446 prefs_register_modules(void)
450 /* Parse through a list of comma-separated, possibly quoted strings.
451 Return a list of the string data. */
453 get_string_list(gchar *str)
455 enum { PRE_STRING, IN_QUOT, NOT_IN_QUOT };
457 gint state = PRE_STRING, i = 0, j = 0;
458 gboolean backslash = FALSE;
463 /* Allocate a buffer for the first string. */
464 slstr = (gchar *) g_malloc(sizeof(gchar) * COL_MAX_LEN);
470 /* It's the end of the input, so it's the end of the string we
471 were working on, and there's no more input. */
472 if (state == IN_QUOT || backslash) {
473 /* We were in the middle of a quoted string or backslash escape,
474 and ran out of characters; that's an error. */
476 clear_string_list(sl);
480 sl = g_list_append(sl, slstr);
483 if (cur_c == '"' && ! backslash) {
486 /* We hadn't yet started processing a string; this starts the
487 string, and we're now quoting. */
491 /* We're in the middle of a quoted string, and we saw a quotation
492 mark; we're no longer quoting. */
496 /* We're working on a string, but haven't seen a quote; we're
503 } else if (cur_c == '\\' && ! backslash) {
504 /* We saw a backslash, and the previous character wasn't a
505 backslash; escape the next character.
507 This also means we've started a new string. */
509 if (state == PRE_STRING)
511 } else if (cur_c == ',' && state != IN_QUOT && ! backslash) {
512 /* We saw a comma, and we're not in the middle of a quoted string
513 and it wasn't preceded by a backslash; it's the end of
514 the string we were working on... */
516 sl = g_list_append(sl, slstr);
518 /* ...and the beginning of a new string. */
520 slstr = (gchar *) g_malloc(sizeof(gchar) * COL_MAX_LEN);
522 } else if (!isspace(cur_c) || state != PRE_STRING) {
523 /* Either this isn't a white-space character, or we've started a
524 string (i.e., already seen a non-white-space character for that
525 string and put it into the string).
527 The character is to be put into the string; do so if there's
529 if (j < COL_MAX_LEN) {
534 /* If it was backslash-escaped, we're done with the backslash escape. */
542 #define MAX_FMT_PREF_LEN 1024
543 #define MAX_FMT_PREF_LINE_LEN 60
545 put_string_list(GList *sl)
547 static gchar pref_str[MAX_FMT_PREF_LEN] = "";
548 GList *clp = g_list_first(sl);
550 int cur_pos = 0, cur_len = 0;
553 gchar *strp, *quoted_strp, c;
559 /* Allocate a buffer big enough to hold the entire string, with each
560 character quoted (that's the worst case). */
561 str_len = strlen(str);
562 quoted_str = g_malloc(str_len*2 + 1);
564 /* Now quote any " or \ characters in it. */
566 quoted_strp = quoted_str;
567 while ((c = *strp++) != '\0') {
568 if (c == '"' || c == '\\') {
569 /* It has to be backslash-quoted. */
570 *quoted_strp++ = '\\';
576 fmt_len = strlen(quoted_str) + 4;
577 if ((fmt_len + cur_len) < (MAX_FMT_PREF_LEN - 1)) {
578 if ((fmt_len + cur_pos) > MAX_FMT_PREF_LINE_LEN) {
582 pref_str[cur_len] = '\n'; cur_len++;
583 pref_str[cur_len] = '\t'; cur_len++;
585 sprintf(&pref_str[cur_len], "\"%s\", ", quoted_str);
593 /* If the string is at least two characters long, the last two characters
594 are ", ", and should be discarded, as there are no more items in the
597 pref_str[cur_len - 2] = '\0';
603 clear_string_list(GList *sl)
609 l = g_list_remove_link(l, l);
614 * Takes a string, a pointer to an array of "enum_val_t"s, and a default gint
616 * The array must be terminated by an entry with a null "name" string.
617 * If the string matches a "name" strings in an entry, the value from that
618 * entry is returned. Otherwise, the default value that was passed as the
619 * third argument is returned.
622 find_val_for_string(const char *needle, const enum_val_t *haystack,
627 while (haystack[i].name != NULL) {
628 if (strcasecmp(needle, haystack[i].name) == 0) {
629 return haystack[i].value;
633 return default_value;
636 /* Takes an string and a pointer to an array of strings, and a default int value.
637 * The array must be terminated by a NULL string. If the string is found in the array
638 * of strings, the index of that string in the array is returned. Otherwise, the
639 * default value that was passed as the third argument is returned.
642 find_index_from_string_array(char *needle, char **haystack, int default_value)
646 while (haystack[i] != NULL) {
647 if (strcmp(needle, haystack[i]) == 0) {
652 return default_value;
655 /* Preferences file format:
656 * - Configuration directives start at the beginning of the line, and
657 * are terminated with a colon.
658 * - Directives can be continued on the next line by preceding them with
663 # This is a comment line
665 print.file: /a/very/long/path/
670 #define MAX_VAR_LEN 48
671 #define MAX_VAL_LEN 1024
673 #define DEF_NUM_COLS 6
675 static void read_prefs_file(const char *pf_path, FILE *pf);
677 /* Read the preferences file, fill in "prefs", and return a pointer to it.
679 If we got an error (other than "it doesn't exist") trying to read
680 the global preferences file, stuff the errno into "*gpf_errno_return"
681 and a pointer to the path of the file into "*gpf_path_return", and
684 If we got an error (other than "it doesn't exist") trying to read
685 the user's preferences file, stuff the errno into "*pf_errno_return"
686 and a pointer to the path of the file into "*pf_path_return", and
689 read_prefs(int *gpf_errno_return, char **gpf_path_return,
690 int *pf_errno_return, const char **pf_path_return)
696 gchar *col_fmt[] = {"No.", "%m", "Time", "%t",
697 "Source", "%s", "Destination", "%d",
698 "Protocol", "%p", "Info", "%i"};
701 /* Initialize preferences to wired-in default values.
702 They may be overridded by the global preferences file or the
703 user's preferences file. */
705 prefs.pr_format = PR_FMT_TEXT;
706 prefs.pr_dest = PR_DEST_CMD;
707 prefs.pr_file = g_strdup("ethereal.out");
708 prefs.pr_cmd = g_strdup("lpr");
709 prefs.col_list = NULL;
710 for (i = 0; i < DEF_NUM_COLS; i++) {
711 cfmt = (fmt_data *) g_malloc(sizeof(fmt_data));
712 cfmt->title = g_strdup(col_fmt[i * 2]);
713 cfmt->fmt = g_strdup(col_fmt[(i * 2) + 1]);
714 prefs.col_list = g_list_append(prefs.col_list, cfmt);
716 prefs.num_cols = DEF_NUM_COLS;
717 prefs.st_client_fg.pixel = 0;
718 prefs.st_client_fg.red = 32767;
719 prefs.st_client_fg.green = 0;
720 prefs.st_client_fg.blue = 0;
721 prefs.st_client_bg.pixel = 65535;
722 prefs.st_client_bg.red = 65535;
723 prefs.st_client_bg.green = 65535;
724 prefs.st_client_bg.blue = 65535;
725 prefs.st_server_fg.pixel = 0;
726 prefs.st_server_fg.red = 0;
727 prefs.st_server_fg.green = 0;
728 prefs.st_server_fg.blue = 32767;
729 prefs.st_server_bg.pixel = 65535;
730 prefs.st_server_bg.red = 65535;
731 prefs.st_server_bg.green = 65535;
732 prefs.st_server_bg.blue = 65535;
733 prefs.gui_scrollbar_on_right = TRUE;
734 prefs.gui_plist_sel_browse = FALSE;
735 prefs.gui_ptree_sel_browse = FALSE;
736 prefs.gui_ptree_line_style = 0;
737 prefs.gui_ptree_expander_style = 1;
738 prefs.gui_hex_dump_highlight_style = 1;
740 prefs.gui_font_name = g_strdup("-*-lucida console-medium-r-*-*-*-100-*-*-*-*-*-*");
743 * XXX - for now, we make the initial font name a pattern that matches
744 * only ISO 8859/1 fonts, so that we don't match 2-byte fonts such
745 * as ISO 10646 fonts.
747 * Users in locales using other one-byte fonts will have to choose
748 * a different font from the preferences dialog - or put the font
749 * selection in the global preferences file to make that font the
750 * default for all users who don't explicitly specify a different
753 * Making this a font set rather than a font has two problems:
755 * 1) as far as I know, you can't select font sets with the
756 * font selection dialog;
758 * 2) if you use a font set, the text to be drawn must be a
759 * multi-byte string in the appropriate locale, but
760 * Ethereal does *NOT* guarantee that's the case - in
761 * the hex-dump window, each character in the text portion
762 * of the display must be a *single* byte, and in the
763 * packet-list and protocol-tree windows, text extracted
764 * from the packet is not necessarily in the right format.
766 * "Doing this right" may, for the packet-list and protocol-tree
767 * windows, require that dissectors know what the locale is
768 * *AND* know what locale and text representation is used in
769 * the packets they're dissecting, and may be impossible in
770 * the hex-dump window (except by punting and displaying only
773 * GTK+ 2.0 may simplify part of the problem, as it will, as I
774 * understand it, use UTF-8-encoded Unicode as its internal
775 * character set; however, we'd still have to know whatever
776 * character set and encoding is used in the packet (which
777 * may differ for different protocols, e.g. SMB might use
778 * PC code pages for some strings and Unicode for others, whilst
779 * NFS might use some UNIX character set encoding, e.g. ISO 8859/x,
780 * or one of the EUC character sets for Asian languages, or one
781 * of the other multi-byte character sets, or UTF-8, or...).
783 * I.e., as far as I can tell, "internationalizing" the packet-list,
784 * protocol-tree, and hex-dump windows involves a lot more than, say,
785 * just using font sets rather than fonts.
787 prefs.gui_font_name = g_strdup("-*-fixed-medium-r-semicondensed-*-*-120-*-*-*-*-iso8859-1");
789 prefs.gui_marked_fg.pixel = 65535;
790 prefs.gui_marked_fg.red = 65535;
791 prefs.gui_marked_fg.green = 65535;
792 prefs.gui_marked_fg.blue = 65535;
793 prefs.gui_marked_bg.pixel = 0;
794 prefs.gui_marked_bg.red = 0;
795 prefs.gui_marked_bg.green = 0;
796 prefs.gui_marked_bg.blue = 0;
797 prefs.gui_geometry_save_position = 0;
798 prefs.gui_geometry_save_size = 1;
799 prefs.gui_geometry_main_x = 20;
800 prefs.gui_geometry_main_y = 20;
801 prefs.gui_geometry_main_width = DEF_WIDTH;
802 prefs.gui_geometry_main_height = -1;
804 /* set the default values for the capture dialog box */
805 prefs.capture_device = NULL;
806 prefs.capture_prom_mode = TRUE;
807 prefs.capture_real_time = FALSE;
808 prefs.capture_auto_scroll = FALSE;
809 prefs.name_resolve = RESOLV_ALL;
812 /* Construct the pathname of the global preferences file. */
814 gpf_path = (gchar *) g_malloc(strlen(get_datafile_dir()) +
815 strlen(GPF_NAME) + 2);
816 sprintf(gpf_path, "%s" G_DIR_SEPARATOR_S "%s",
817 get_datafile_dir(), GPF_NAME);
820 /* Read the global preferences file, if it exists. */
821 *gpf_path_return = NULL;
822 if ((pf = fopen(gpf_path, "r")) != NULL) {
823 /* We succeeded in opening it; read it. */
824 read_prefs_file(gpf_path, pf);
827 /* We failed to open it. If we failed for some reason other than
828 "it doesn't exist", return the errno and the pathname, so our
829 caller can report the error. */
830 if (errno != ENOENT) {
831 *gpf_errno_return = errno;
832 *gpf_path_return = gpf_path;
836 /* Construct the pathname of the user's preferences file. */
837 pf_path = get_persconffile_path(PF_NAME, FALSE);
839 /* Read the user's preferences file, if it exists. */
840 *pf_path_return = NULL;
841 if ((pf = fopen(pf_path, "r")) != NULL) {
842 /* We succeeded in opening it; read it. */
843 read_prefs_file(pf_path, pf);
846 /* We failed to open it. If we failed for some reason other than
847 "it doesn't exist", return the errno and the pathname, so our
848 caller can report the error. */
849 if (errno != ENOENT) {
850 *pf_errno_return = errno;
851 *pf_path_return = pf_path;
859 read_prefs_file(const char *pf_path, FILE *pf)
861 enum { START, IN_VAR, PRE_VAL, IN_VAL, IN_SKIP };
862 gchar cur_var[MAX_VAR_LEN], cur_val[MAX_VAL_LEN];
863 int got_c, state = START;
864 gboolean got_val = FALSE;
865 gint var_len = 0, val_len = 0, fline = 1, pline = 1;
868 * Start out the counters of "mgcp.{tcp,udp}.port" entries we've
871 mgcp_tcp_port_count = 0;
872 mgcp_udp_port_count = 0;
874 while ((got_c = getc(pf)) != EOF) {
880 if (var_len >= MAX_VAR_LEN) {
881 g_warning ("%s line %d: Variable too long", pf_path, fline);
886 if (val_len >= MAX_VAL_LEN) {
887 g_warning ("%s line %d: Value too long", pf_path, fline);
895 if (isalnum(got_c)) {
898 cur_var[var_len] = '\0';
899 cur_val[val_len] = '\0';
900 switch (set_pref(cur_var, cur_val)) {
902 case PREFS_SET_SYNTAX_ERR:
903 g_warning ("%s line %d: Syntax error", pf_path, pline);
906 case PREFS_SET_NO_SUCH_PREF:
907 g_warning ("%s line %d: No such preference \"%s\"", pf_path,
911 case PREFS_SET_OBSOLETE:
912 /* We silently ignore attempts to set these; it's
913 probably not the user's fault that it's in there -
914 they may have saved preferences with a release that
919 g_warning ("%s line %d: Incomplete preference", pf_path, pline);
927 } else if (isspace(got_c) && var_len > 0 && got_val) {
929 } else if (got_c == '#') {
932 g_warning ("%s line %d: Malformed line", pf_path, fline);
937 cur_var[var_len] = got_c;
946 if (!isspace(got_c)) {
948 cur_val[val_len] = got_c;
954 cur_val[val_len] = got_c;
957 while (isspace((guchar)cur_val[val_len]) && val_len > 0)
966 cur_var[var_len] = '\0';
967 cur_val[val_len] = '\0';
968 switch (set_pref(cur_var, cur_val)) {
970 case PREFS_SET_SYNTAX_ERR:
971 g_warning ("%s line %d: Syntax error", pf_path, pline);
974 case PREFS_SET_NO_SUCH_PREF:
975 g_warning ("%s line %d: No such preference \"%s\"", pf_path,
979 case PREFS_SET_OBSOLETE:
980 /* We silently ignore attempts to set these; it's probably not
981 the user's fault that it's in there - they may have saved
982 preferences with a release that supported it. */
986 g_warning ("%s line %d: Incomplete preference", pf_path, pline);
992 * Given a string of the form "<pref name>:<pref value>", as might appear
993 * as an argument to a "-o" option, parse it and set the preference in
994 * question. Return an indication of whether it succeeded or failed
998 prefs_set_pref(char *prefarg)
1004 * Set the counters of "mgcp.{tcp,udp}.port" entries we've
1005 * seen to values that keep us from trying to interpret tham
1006 * as "mgcp.{tcp,udp}.gateway_port" or "mgcp.{tcp,udp}.callagent_port",
1007 * as, from the command line, we have no way of guessing which
1008 * the user had in mind.
1010 mgcp_tcp_port_count = -1;
1011 mgcp_udp_port_count = -1;
1013 colonp = strchr(prefarg, ':');
1015 return PREFS_SET_SYNTAX_ERR;
1021 * Skip over any white space (there probably won't be any, but
1022 * as we allow it in the preferences file, we might as well
1029 * Put the colon back, so if our caller uses, in an
1030 * error message, the string they passed us, the message
1034 return PREFS_SET_SYNTAX_ERR;
1037 ret = set_pref(prefarg, p);
1038 *colonp = ':'; /* put the colon back */
1042 #define PRS_PRINT_FMT "print.format"
1043 #define PRS_PRINT_DEST "print.destination"
1044 #define PRS_PRINT_FILE "print.file"
1045 #define PRS_PRINT_CMD "print.command"
1046 #define PRS_COL_FMT "column.format"
1047 #define PRS_STREAM_CL_FG "stream.client.fg"
1048 #define PRS_STREAM_CL_BG "stream.client.bg"
1049 #define PRS_STREAM_SR_FG "stream.server.fg"
1050 #define PRS_STREAM_SR_BG "stream.server.bg"
1051 #define PRS_GUI_SCROLLBAR_ON_RIGHT "gui.scrollbar_on_right"
1052 #define PRS_GUI_PLIST_SEL_BROWSE "gui.packet_list_sel_browse"
1053 #define PRS_GUI_PTREE_SEL_BROWSE "gui.protocol_tree_sel_browse"
1054 #define PRS_GUI_PTREE_LINE_STYLE "gui.protocol_tree_line_style"
1055 #define PRS_GUI_PTREE_EXPANDER_STYLE "gui.protocol_tree_expander_style"
1056 #define PRS_GUI_HEX_DUMP_HIGHLIGHT_STYLE "gui.hex_dump_highlight_style"
1057 #define PRS_GUI_FONT_NAME "gui.font_name"
1058 #define PRS_GUI_MARKED_FG "gui.marked_frame.fg"
1059 #define PRS_GUI_MARKED_BG "gui.marked_frame.bg"
1060 #define PRS_GUI_GEOMETRY_SAVE_POSITION "gui.geometry.save.position"
1061 #define PRS_GUI_GEOMETRY_SAVE_SIZE "gui.geometry.save.size"
1062 #define PRS_GUI_GEOMETRY_MAIN_X "gui.geometry.main.x"
1063 #define PRS_GUI_GEOMETRY_MAIN_Y "gui.geometry.main.y"
1064 #define PRS_GUI_GEOMETRY_MAIN_WIDTH "gui.geometry.main.width"
1065 #define PRS_GUI_GEOMETRY_MAIN_HEIGHT "gui.geometry.main.height"
1068 * This applies to more than just captures, so it's not "capture.name_resolve";
1069 * "capture.name_resolve" is supported on input for backwards compatibility.
1071 * It's not a preference for a particular part of Ethereal, it's used all
1072 * over the place, so its name doesn't have two components.
1074 #define PRS_NAME_RESOLVE "name_resolve"
1075 #define PRS_CAP_NAME_RESOLVE "capture.name_resolve"
1077 /* values for the capture dialog box */
1078 #define PRS_CAP_DEVICE "capture.device"
1079 #define PRS_CAP_PROM_MODE "capture.prom_mode"
1080 #define PRS_CAP_REAL_TIME "capture.real_time_update"
1081 #define PRS_CAP_AUTO_SCROLL "capture.auto_scroll"
1083 #define RED_COMPONENT(x) ((((x) >> 16) & 0xff) * 65535 / 255)
1084 #define GREEN_COMPONENT(x) ((((x) >> 8) & 0xff) * 65535 / 255)
1085 #define BLUE_COMPONENT(x) (((x) & 0xff) * 65535 / 255)
1087 static gchar *pr_formats[] = { "text", "postscript" };
1088 static gchar *pr_dests[] = { "command", "file" };
1093 } name_resolve_opt_t;
1095 static name_resolve_opt_t name_resolve_opt[] = {
1096 { 'm', RESOLV_MAC },
1097 { 'n', RESOLV_NETWORK },
1098 { 't', RESOLV_TRANSPORT },
1101 #define N_NAME_RESOLVE_OPT (sizeof name_resolve_opt / sizeof name_resolve_opt[0])
1104 name_resolve_to_string(guint32 name_resolve)
1106 static char string[N_NAME_RESOLVE_OPT+1];
1109 gboolean all_opts_set = TRUE;
1111 if (name_resolve == RESOLV_NONE)
1114 for (i = 0; i < N_NAME_RESOLVE_OPT; i++) {
1115 if (name_resolve & name_resolve_opt[i].value)
1116 *p++ = name_resolve_opt[i].letter;
1118 all_opts_set = FALSE;
1127 string_to_name_resolve(char *string, guint32 *name_resolve)
1133 while ((c = *string++) != '\0') {
1134 for (i = 0; i < N_NAME_RESOLVE_OPT; i++) {
1135 if (c == name_resolve_opt[i].letter) {
1136 *name_resolve |= name_resolve_opt[i].value;
1140 if (i == N_NAME_RESOLVE_OPT) {
1142 * Unrecognized letter.
1151 set_pref(gchar *pref_name, gchar *value)
1153 GList *col_l, *col_l_elt;
1156 unsigned long int cval;
1161 gchar *dotp, *last_dotp;
1166 if (strcmp(pref_name, PRS_PRINT_FMT) == 0) {
1167 if (strcmp(value, pr_formats[PR_FMT_TEXT]) == 0) {
1168 prefs.pr_format = PR_FMT_TEXT;
1169 } else if (strcmp(value, pr_formats[PR_FMT_PS]) == 0) {
1170 prefs.pr_format = PR_FMT_PS;
1172 return PREFS_SET_SYNTAX_ERR;
1174 } else if (strcmp(pref_name, PRS_PRINT_DEST) == 0) {
1175 if (strcmp(value, pr_dests[PR_DEST_CMD]) == 0) {
1176 prefs.pr_dest = PR_DEST_CMD;
1177 } else if (strcmp(value, pr_dests[PR_DEST_FILE]) == 0) {
1178 prefs.pr_dest = PR_DEST_FILE;
1180 return PREFS_SET_SYNTAX_ERR;
1182 } else if (strcmp(pref_name, PRS_PRINT_FILE) == 0) {
1183 if (prefs.pr_file) g_free(prefs.pr_file);
1184 prefs.pr_file = g_strdup(value);
1185 } else if (strcmp(pref_name, PRS_PRINT_CMD) == 0) {
1186 if (prefs.pr_cmd) g_free(prefs.pr_cmd);
1187 prefs.pr_cmd = g_strdup(value);
1188 } else if (strcmp(pref_name, PRS_COL_FMT) == 0) {
1189 col_l = get_string_list(value);
1191 return PREFS_SET_SYNTAX_ERR;
1192 if ((g_list_length(col_l) % 2) != 0) {
1193 /* A title didn't have a matching format. */
1194 clear_string_list(col_l);
1195 return PREFS_SET_SYNTAX_ERR;
1197 /* Check to make sure all column formats are valid. */
1198 col_l_elt = g_list_first(col_l);
1200 /* Make sure the title isn't empty. */
1201 if (strcmp(col_l_elt->data, "") == 0) {
1203 clear_string_list(col_l);
1204 return PREFS_SET_SYNTAX_ERR;
1207 /* Go past the title. */
1208 col_l_elt = col_l_elt->next;
1210 /* Check the format. */
1211 if (get_column_format_from_str(col_l_elt->data) == -1) {
1212 /* It's not a valid column format. */
1213 clear_string_list(col_l);
1214 return PREFS_SET_SYNTAX_ERR;
1217 /* Go past the format. */
1218 col_l_elt = col_l_elt->next;
1220 free_col_info(&prefs);
1221 prefs.col_list = NULL;
1222 llen = g_list_length(col_l);
1223 prefs.num_cols = llen / 2;
1224 col_l_elt = g_list_first(col_l);
1226 cfmt = (fmt_data *) g_malloc(sizeof(fmt_data));
1227 cfmt->title = g_strdup(col_l_elt->data);
1228 col_l_elt = col_l_elt->next;
1229 cfmt->fmt = g_strdup(col_l_elt->data);
1230 col_l_elt = col_l_elt->next;
1231 prefs.col_list = g_list_append(prefs.col_list, cfmt);
1233 clear_string_list(col_l);
1234 } else if (strcmp(pref_name, PRS_STREAM_CL_FG) == 0) {
1235 cval = strtoul(value, NULL, 16);
1236 prefs.st_client_fg.pixel = 0;
1237 prefs.st_client_fg.red = RED_COMPONENT(cval);
1238 prefs.st_client_fg.green = GREEN_COMPONENT(cval);
1239 prefs.st_client_fg.blue = BLUE_COMPONENT(cval);
1240 } else if (strcmp(pref_name, PRS_STREAM_CL_BG) == 0) {
1241 cval = strtoul(value, NULL, 16);
1242 prefs.st_client_bg.pixel = 0;
1243 prefs.st_client_bg.red = RED_COMPONENT(cval);
1244 prefs.st_client_bg.green = GREEN_COMPONENT(cval);
1245 prefs.st_client_bg.blue = BLUE_COMPONENT(cval);
1246 } else if (strcmp(pref_name, PRS_STREAM_SR_FG) == 0) {
1247 cval = strtoul(value, NULL, 16);
1248 prefs.st_server_fg.pixel = 0;
1249 prefs.st_server_fg.red = RED_COMPONENT(cval);
1250 prefs.st_server_fg.green = GREEN_COMPONENT(cval);
1251 prefs.st_server_fg.blue = BLUE_COMPONENT(cval);
1252 } else if (strcmp(pref_name, PRS_STREAM_SR_BG) == 0) {
1253 cval = strtoul(value, NULL, 16);
1254 prefs.st_server_bg.pixel = 0;
1255 prefs.st_server_bg.red = RED_COMPONENT(cval);
1256 prefs.st_server_bg.green = GREEN_COMPONENT(cval);
1257 prefs.st_server_bg.blue = BLUE_COMPONENT(cval);
1258 } else if (strcmp(pref_name, PRS_GUI_SCROLLBAR_ON_RIGHT) == 0) {
1259 if (strcasecmp(value, "true") == 0) {
1260 prefs.gui_scrollbar_on_right = TRUE;
1263 prefs.gui_scrollbar_on_right = FALSE;
1265 } else if (strcmp(pref_name, PRS_GUI_PLIST_SEL_BROWSE) == 0) {
1266 if (strcasecmp(value, "true") == 0) {
1267 prefs.gui_plist_sel_browse = TRUE;
1270 prefs.gui_plist_sel_browse = FALSE;
1272 } else if (strcmp(pref_name, PRS_GUI_PTREE_SEL_BROWSE) == 0) {
1273 if (strcasecmp(value, "true") == 0) {
1274 prefs.gui_ptree_sel_browse = TRUE;
1277 prefs.gui_ptree_sel_browse = FALSE;
1279 } else if (strcmp(pref_name, PRS_GUI_PTREE_LINE_STYLE) == 0) {
1280 prefs.gui_ptree_line_style =
1281 find_index_from_string_array(value, gui_ptree_line_style_text, 0);
1282 } else if (strcmp(pref_name, PRS_GUI_PTREE_EXPANDER_STYLE) == 0) {
1283 prefs.gui_ptree_expander_style =
1284 find_index_from_string_array(value, gui_ptree_expander_style_text, 1);
1285 } else if (strcmp(pref_name, PRS_GUI_HEX_DUMP_HIGHLIGHT_STYLE) == 0) {
1286 prefs.gui_hex_dump_highlight_style =
1287 find_index_from_string_array(value, gui_hex_dump_highlight_style_text, 1);
1288 } else if (strcmp(pref_name, PRS_GUI_FONT_NAME) == 0) {
1289 if (prefs.gui_font_name != NULL)
1290 g_free(prefs.gui_font_name);
1291 prefs.gui_font_name = g_strdup(value);
1292 } else if (strcmp(pref_name, PRS_GUI_MARKED_FG) == 0) {
1293 cval = strtoul(value, NULL, 16);
1294 prefs.gui_marked_fg.pixel = 0;
1295 prefs.gui_marked_fg.red = RED_COMPONENT(cval);
1296 prefs.gui_marked_fg.green = GREEN_COMPONENT(cval);
1297 prefs.gui_marked_fg.blue = BLUE_COMPONENT(cval);
1298 } else if (strcmp(pref_name, PRS_GUI_MARKED_BG) == 0) {
1299 cval = strtoul(value, NULL, 16);
1300 prefs.gui_marked_bg.pixel = 0;
1301 prefs.gui_marked_bg.red = RED_COMPONENT(cval);
1302 prefs.gui_marked_bg.green = GREEN_COMPONENT(cval);
1303 prefs.gui_marked_bg.blue = BLUE_COMPONENT(cval);
1304 } else if (strcmp(pref_name, PRS_GUI_GEOMETRY_SAVE_POSITION) == 0) {
1305 if (strcasecmp(value, "true") == 0) {
1306 prefs.gui_geometry_save_position = TRUE;
1309 prefs.gui_geometry_save_position = FALSE;
1311 } else if (strcmp(pref_name, PRS_GUI_GEOMETRY_SAVE_SIZE) == 0) {
1312 if (strcasecmp(value, "true") == 0) {
1313 prefs.gui_geometry_save_size = TRUE;
1316 prefs.gui_geometry_save_size = FALSE;
1318 } else if (strcmp(pref_name, PRS_GUI_GEOMETRY_MAIN_X) == 0) {
1319 prefs.gui_geometry_main_x = strtol(value, NULL, 10);
1320 } else if (strcmp(pref_name, PRS_GUI_GEOMETRY_MAIN_Y) == 0) {
1321 prefs.gui_geometry_main_y = strtol(value, NULL, 10);
1322 } else if (strcmp(pref_name, PRS_GUI_GEOMETRY_MAIN_WIDTH) == 0) {
1323 prefs.gui_geometry_main_width = strtol(value, NULL, 10);
1324 } else if (strcmp(pref_name, PRS_GUI_GEOMETRY_MAIN_HEIGHT) == 0) {
1325 prefs.gui_geometry_main_height = strtol(value, NULL, 10);
1327 /* handle the capture options */
1328 } else if (strcmp(pref_name, PRS_CAP_DEVICE) == 0) {
1329 if (prefs.capture_device != NULL)
1330 g_free(prefs.capture_device);
1331 prefs.capture_device = g_strdup(value);
1332 } else if (strcmp(pref_name, PRS_CAP_PROM_MODE) == 0) {
1333 prefs.capture_prom_mode = ((strcasecmp(value, "true") == 0)?TRUE:FALSE);
1334 } else if (strcmp(pref_name, PRS_CAP_REAL_TIME) == 0) {
1335 prefs.capture_real_time = ((strcasecmp(value, "true") == 0)?TRUE:FALSE);
1336 } else if (strcmp(pref_name, PRS_CAP_AUTO_SCROLL) == 0) {
1337 prefs.capture_auto_scroll = ((strcasecmp(value, "true") == 0)?TRUE:FALSE);
1339 /* handle the global options */
1340 } else if (strcmp(pref_name, PRS_NAME_RESOLVE) == 0 ||
1341 strcmp(pref_name, PRS_CAP_NAME_RESOLVE) == 0) {
1343 * "TRUE" and "FALSE", for backwards compatibility, are synonyms for
1344 * RESOLV_ALL and RESOLV_NONE.
1346 * Otherwise, we treat it as a list of name types we want to resolve.
1348 if (strcasecmp(value, "true") == 0)
1349 prefs.name_resolve = RESOLV_ALL;
1350 else if (strcasecmp(value, "false") == 0)
1351 prefs.name_resolve = RESOLV_NONE;
1353 prefs.name_resolve = RESOLV_NONE; /* start out with none set */
1354 if (string_to_name_resolve(value, &prefs.name_resolve) != '\0')
1355 return PREFS_SET_SYNTAX_ERR;
1358 /* To which module does this preference belong? */
1360 last_dotp = pref_name;
1363 dotp = strchr(last_dotp, '.');
1366 /* no such module */
1367 return PREFS_SET_NO_SUCH_PREF;
1370 /* no ".", so no module/name separator */
1371 return PREFS_SET_SYNTAX_ERR;
1377 *dotp = '\0'; /* separate module and preference name */
1378 module = find_module(pref_name);
1381 * XXX - "Diameter" rather than "diameter" was used in earlier
1382 * versions of Ethereal; if we didn't find the module, and its name
1383 * was "Diameter", look for "diameter" instead.
1385 * In addition, the BEEP protocol used to be the BXXP protocol,
1386 * so if we didn't find the module, and its name was "bxxp",
1387 * look for "beep" instead.
1389 if (module == NULL) {
1390 if (strcmp(pref_name, "Diameter") == 0)
1391 module = find_module("diameter");
1392 else if (strcmp(pref_name, "bxxp") == 0)
1393 module = find_module("beep");
1395 *dotp = '.'; /* put the preference string back */
1396 dotp++; /* skip past separator to preference name */
1400 pref = find_preference(module, dotp);
1403 if (strncmp(pref_name, "mgcp.", 5) == 0) {
1405 * XXX - "mgcp.display raw text toggle" and "mgcp.display dissect tree"
1406 * rather than "mgcp.display_raw_text" and "mgcp.display_dissect_tree"
1407 * were used in earlier versions of Ethereal; if we didn't find the
1408 * preference, it was an MGCP preference, and its name was
1409 * "display raw text toggle" or "display dissect tree", look for
1410 * "display_raw_text" or "display_dissect_tree" instead.
1412 * "mgcp.tcp.port" and "mgcp.udp.port" are harder to handle, as both
1413 * the gateway and callagent ports were given those names; we interpret
1414 * the first as "mgcp.{tcp,udp}.gateway_port" and the second as
1415 * "mgcp.{tcp,udp}.callagent_port", as that's the order in which
1416 * they were registered by the MCCP dissector and thus that's the
1417 * order in which they were written to the preferences file. (If
1418 * we're not reading the preferences file, but are handling stuff
1419 * from a "-o" command-line option, we have no clue which the user
1420 * had in mind - they should have used "mgcp.{tcp,udp}.gateway_port"
1421 * or "mgcp.{tcp,udp}.callagent_port" instead.)
1423 if (strcmp(dotp, "display raw text toggle") == 0)
1424 pref = find_preference(module, "display_raw_text");
1425 else if (strcmp(dotp, "display dissect tree") == 0)
1426 pref = find_preference(module, "display_dissect_tree");
1427 else if (strcmp(dotp, "tcp.port") == 0) {
1428 mgcp_tcp_port_count++;
1429 if (mgcp_tcp_port_count == 1) {
1430 /* It's the first one */
1431 pref = find_preference(module, "tcp.gateway_port");
1432 } else if (mgcp_tcp_port_count == 2) {
1433 /* It's the second one */
1434 pref = find_preference(module, "tcp.callagent_port");
1436 /* Otherwise it's from the command line, and we don't bother
1438 } else if (strcmp(dotp, "udp.port") == 0) {
1439 mgcp_udp_port_count++;
1440 if (mgcp_udp_port_count == 1) {
1441 /* It's the first one */
1442 pref = find_preference(module, "udp.gateway_port");
1443 } else if (mgcp_udp_port_count == 2) {
1444 /* It's the second one */
1445 pref = find_preference(module, "udp.callagent_port");
1447 /* Otherwise it's from the command line, and we don't bother
1450 } else if (strncmp(pref_name, "smb.", 4) == 0) {
1451 /* Handle old names for SMB preferences. */
1452 if (strcmp(dotp, "smb.trans.reassembly") == 0)
1453 pref = find_preference(module, "trans_reassembly");
1454 else if (strcmp(dotp, "smb.dcerpc.reassembly") == 0)
1455 pref = find_preference(module, "dcerpc_reassembly");
1456 } else if (strncmp(pref_name, "ndmp.", 5) == 0) {
1457 /* Handle old names for NDMP preferences. */
1458 if (strcmp(dotp, "ndmp.desegment") == 0)
1459 pref = find_preference(module, "desegment");
1460 } else if (strncmp(pref_name, "diameter.", 9) == 0) {
1461 /* Handle old names for Diameter preferences. */
1462 if (strcmp(dotp, "diameter.desegment") == 0)
1463 pref = find_preference(module, "desegment");
1467 return PREFS_SET_NO_SUCH_PREF; /* no such preference */
1469 switch (pref->type) {
1472 uval = strtoul(value, &p, pref->info.base);
1473 if (p == value || *p != '\0')
1474 return PREFS_SET_SYNTAX_ERR; /* number was bad */
1475 if (*pref->varp.uint != uval) {
1476 module->prefs_changed = TRUE;
1477 *pref->varp.uint = uval;
1482 /* XXX - give an error if it's neither "true" nor "false"? */
1483 if (strcasecmp(value, "true") == 0)
1487 if (*pref->varp.bool != bval) {
1488 module->prefs_changed = TRUE;
1489 *pref->varp.bool = bval;
1494 /* XXX - give an error if it doesn't match? */
1495 enum_val = find_val_for_string(value,
1496 pref->info.enum_info.enumvals, 1);
1497 if (*pref->varp.enump != enum_val) {
1498 module->prefs_changed = TRUE;
1499 *pref->varp.enump = enum_val;
1504 if (*pref->varp.string == NULL || strcmp(*pref->varp.string, value) != 0) {
1505 module->prefs_changed = TRUE;
1506 if (*pref->varp.string != NULL)
1507 g_free(*pref->varp.string);
1508 *pref->varp.string = g_strdup(value);
1513 return PREFS_SET_OBSOLETE; /* no such preference any more */
1517 return PREFS_SET_OK;
1526 * Write out a single preference.
1529 write_pref(gpointer data, gpointer user_data)
1531 pref_t *pref = data;
1532 write_pref_arg_t *arg = user_data;
1533 const enum_val_t *enum_valp;
1534 const char *val_string;
1536 if (pref->type == PREF_OBSOLETE) {
1538 * This preference is no longer supported; it's not a
1539 * real preference, so we don't write it out (i.e., we
1540 * treat it as if it weren't found in the list of
1541 * preferences, and we weren't called in the first place).
1546 fprintf(arg->pf, "\n# %s\n", pref->description);
1548 switch (pref->type) {
1551 switch (pref->info.base) {
1554 fprintf(arg->pf, "# A decimal number.\n");
1555 fprintf(arg->pf, "%s.%s: %u\n", arg->module->name,
1556 pref->name, *pref->varp.uint);
1560 fprintf(arg->pf, "# An octal number.\n");
1561 fprintf(arg->pf, "%s.%s: %#o\n", arg->module->name,
1562 pref->name, *pref->varp.uint);
1566 fprintf(arg->pf, "# A hexadecimal number.\n");
1567 fprintf(arg->pf, "%s.%s: %#x\n", arg->module->name,
1568 pref->name, *pref->varp.uint);
1574 fprintf(arg->pf, "# TRUE or FALSE (case-insensitive).\n");
1575 fprintf(arg->pf, "%s.%s: %s\n", arg->module->name, pref->name,
1576 *pref->varp.bool ? "TRUE" : "FALSE");
1580 fprintf(arg->pf, "# One of: ");
1581 enum_valp = pref->info.enum_info.enumvals;
1583 while (enum_valp->name != NULL) {
1584 if (enum_valp->value == *pref->varp.enump)
1585 val_string = enum_valp->name;
1586 fprintf(arg->pf, "%s", enum_valp->name);
1588 if (enum_valp->name == NULL)
1589 fprintf(arg->pf, "\n");
1591 fprintf(arg->pf, ", ");
1593 fprintf(arg->pf, "# (case-insensitive).\n");
1594 fprintf(arg->pf, "%s.%s: %s\n", arg->module->name, pref->name,
1599 fprintf(arg->pf, "# A string.\n");
1600 fprintf(arg->pf, "%s.%s: %s\n", arg->module->name, pref->name,
1601 *pref->varp.string);
1605 g_assert_not_reached();
1611 write_module_prefs(gpointer data, gpointer user_data)
1613 write_pref_arg_t arg;
1617 g_list_foreach(arg.module->prefs, write_pref, &arg);
1620 /* Write out "prefs" to the user's preferences file, and return 0.
1622 If we got an error, stuff a pointer to the path of the preferences file
1623 into "*pf_path_return", and return the errno. */
1625 write_prefs(const char **pf_path_return)
1627 const char *pf_path;
1633 * - Split output lines longer than MAX_VAL_LEN
1634 * - Create a function for the preference directory check/creation
1635 * so that duplication can be avoided with filter.c
1638 pf_path = get_persconffile_path(PF_NAME, TRUE);
1639 if ((pf = fopen(pf_path, "w")) == NULL) {
1640 *pf_path_return = pf_path;
1644 fputs("# Configuration file for Ethereal " VERSION ".\n"
1646 "# This file is regenerated each time preferences are saved within\n"
1647 "# Ethereal. Making manual changes should be safe, however.\n"
1649 "######## Printing ########\n"
1652 fprintf (pf, "# Can be one of \"text\" or \"postscript\".\n"
1653 "print.format: %s\n\n", pr_formats[prefs.pr_format]);
1655 fprintf (pf, "# Can be one of \"command\" or \"file\".\n"
1656 "print.destination: %s\n\n", pr_dests[prefs.pr_dest]);
1658 fprintf (pf, "# This is the file that gets written to when the "
1659 "destination is set to \"file\"\n"
1660 "%s: %s\n\n", PRS_PRINT_FILE, prefs.pr_file);
1662 fprintf (pf, "# Output gets piped to this command when the destination "
1663 "is set to \"command\"\n"
1664 "%s: %s\n\n", PRS_PRINT_CMD, prefs.pr_cmd);
1666 clp = prefs.col_list;
1669 cfmt = (fmt_data *) clp->data;
1670 col_l = g_list_append(col_l, cfmt->title);
1671 col_l = g_list_append(col_l, cfmt->fmt);
1674 fprintf (pf, "# Packet list column format. Each pair of strings consists "
1675 "of a column title \n# and its format.\n"
1676 "%s: %s\n\n", PRS_COL_FMT, put_string_list(col_l));
1677 /* This frees the list of strings, but not the strings to which it
1678 refers; that's what we want, as we haven't copied those strings,
1679 we just referred to them. */
1682 fprintf (pf, "# TCP stream window color preferences. Each value is a six "
1683 "digit hexadecimal value in the form rrggbb.\n");
1684 fprintf (pf, "%s: %02x%02x%02x\n", PRS_STREAM_CL_FG,
1685 (prefs.st_client_fg.red * 255 / 65535),
1686 (prefs.st_client_fg.green * 255 / 65535),
1687 (prefs.st_client_fg.blue * 255 / 65535));
1688 fprintf (pf, "%s: %02x%02x%02x\n", PRS_STREAM_CL_BG,
1689 (prefs.st_client_bg.red * 255 / 65535),
1690 (prefs.st_client_bg.green * 255 / 65535),
1691 (prefs.st_client_bg.blue * 255 / 65535));
1692 fprintf (pf, "%s: %02x%02x%02x\n", PRS_STREAM_SR_FG,
1693 (prefs.st_server_fg.red * 255 / 65535),
1694 (prefs.st_server_fg.green * 255 / 65535),
1695 (prefs.st_server_fg.blue * 255 / 65535));
1696 fprintf (pf, "%s: %02x%02x%02x\n", PRS_STREAM_SR_BG,
1697 (prefs.st_server_bg.red * 255 / 65535),
1698 (prefs.st_server_bg.green * 255 / 65535),
1699 (prefs.st_server_bg.blue * 255 / 65535));
1701 fprintf(pf, "\n# Vertical scrollbars should be on right side? TRUE/FALSE\n");
1702 fprintf(pf, PRS_GUI_SCROLLBAR_ON_RIGHT ": %s\n",
1703 prefs.gui_scrollbar_on_right == TRUE ? "TRUE" : "FALSE");
1705 fprintf(pf, "\n# Packet-list selection bar can be used to browse w/o selecting? TRUE/FALSE\n");
1706 fprintf(pf, PRS_GUI_PLIST_SEL_BROWSE ": %s\n",
1707 prefs.gui_plist_sel_browse == TRUE ? "TRUE" : "FALSE");
1709 fprintf(pf, "\n# Protocol-tree selection bar can be used to browse w/o selecting? TRUE/FALSE\n");
1710 fprintf(pf, PRS_GUI_PTREE_SEL_BROWSE ": %s\n",
1711 prefs.gui_ptree_sel_browse == TRUE ? "TRUE" : "FALSE");
1713 fprintf(pf, "\n# Protocol-tree line style. One of: NONE, SOLID, DOTTED, TABBED\n");
1714 fprintf(pf, PRS_GUI_PTREE_LINE_STYLE ": %s\n",
1715 gui_ptree_line_style_text[prefs.gui_ptree_line_style]);
1717 fprintf(pf, "\n# Protocol-tree expander style. One of: NONE, SQUARE, TRIANGLE, CIRCULAR\n");
1718 fprintf(pf, PRS_GUI_PTREE_EXPANDER_STYLE ": %s\n",
1719 gui_ptree_expander_style_text[prefs.gui_ptree_expander_style]);
1721 fprintf(pf, "\n# Hex dump highlight style. One of: BOLD, INVERSE\n");
1722 fprintf(pf, PRS_GUI_HEX_DUMP_HIGHLIGHT_STYLE ": %s\n",
1723 gui_hex_dump_highlight_style_text[prefs.gui_hex_dump_highlight_style]);
1725 fprintf(pf, "\n# Font name for packet list, protocol tree, and hex dump panes.\n");
1726 fprintf(pf, PRS_GUI_FONT_NAME ": %s\n", prefs.gui_font_name);
1728 fprintf (pf, "\n# Color preferences for a marked frame. Each value is a six "
1729 "digit hexadecimal value in the form rrggbb.\n");
1730 fprintf (pf, "%s: %02x%02x%02x\n", PRS_GUI_MARKED_FG,
1731 (prefs.gui_marked_fg.red * 255 / 65535),
1732 (prefs.gui_marked_fg.green * 255 / 65535),
1733 (prefs.gui_marked_fg.blue * 255 / 65535));
1734 fprintf (pf, "%s: %02x%02x%02x\n", PRS_GUI_MARKED_BG,
1735 (prefs.gui_marked_bg.red * 255 / 65535),
1736 (prefs.gui_marked_bg.green * 255 / 65535),
1737 (prefs.gui_marked_bg.blue * 255 / 65535));
1739 fprintf(pf, "\n# Save window position at exit? TRUE/FALSE\n");
1740 fprintf(pf, PRS_GUI_GEOMETRY_SAVE_POSITION ": %s\n",
1741 prefs.gui_geometry_save_position == TRUE ? "TRUE" : "FALSE");
1743 fprintf(pf, "\n# Save window size at exit? TRUE/FALSE\n");
1744 fprintf(pf, PRS_GUI_GEOMETRY_SAVE_SIZE ": %s\n",
1745 prefs.gui_geometry_save_size == TRUE ? "TRUE" : "FALSE");
1747 fprintf(pf, "\n# Main window geometry. Decimal integers.\n");
1748 fprintf(pf, PRS_GUI_GEOMETRY_MAIN_X ": %d\n", prefs.gui_geometry_main_x);
1749 fprintf(pf, PRS_GUI_GEOMETRY_MAIN_Y ": %d\n", prefs.gui_geometry_main_y);
1750 fprintf(pf, PRS_GUI_GEOMETRY_MAIN_WIDTH ": %d\n",
1751 prefs.gui_geometry_main_width);
1752 fprintf(pf, PRS_GUI_GEOMETRY_MAIN_HEIGHT ": %d\n",
1753 prefs.gui_geometry_main_height);
1755 fprintf(pf, "\n# Resolve addresses to names? TRUE/FALSE/{list of address types to resolve}\n");
1756 fprintf(pf, PRS_NAME_RESOLVE ": %s\n",
1757 name_resolve_to_string(prefs.name_resolve));
1759 /* write the capture options */
1760 if (prefs.capture_device != NULL) {
1761 fprintf(pf, "\n# Default capture device\n");
1762 fprintf(pf, PRS_CAP_DEVICE ": %s\n", prefs.capture_device);
1765 fprintf(pf, "\n# Capture in promiscuous mode? TRUE/FALSE\n");
1766 fprintf(pf, PRS_CAP_PROM_MODE ": %s\n",
1767 prefs.capture_prom_mode == TRUE ? "TRUE" : "FALSE");
1769 fprintf(pf, "\n# Update packet list in real time during capture? TRUE/FALSE\n");
1770 fprintf(pf, PRS_CAP_REAL_TIME ": %s\n",
1771 prefs.capture_real_time == TRUE ? "TRUE" : "FALSE");
1773 fprintf(pf, "\n# scroll packet list during capture? TRUE/FALSE\n");
1774 fprintf(pf, PRS_CAP_AUTO_SCROLL ": %s\n",
1775 prefs.capture_auto_scroll == TRUE ? "TRUE" : "FALSE");
1777 g_list_foreach(modules, write_module_prefs, pf);
1781 /* XXX - catch I/O errors (e.g. "ran out of disk space") and return
1782 an error indication, or maybe write to a new preferences file and
1783 rename that file on top of the old one only if there are not I/O
1788 /* Copy a set of preferences. */
1790 copy_prefs(e_prefs *dest, e_prefs *src)
1792 fmt_data *src_cfmt, *dest_cfmt;
1795 dest->pr_format = src->pr_format;
1796 dest->pr_dest = src->pr_dest;
1797 dest->pr_file = g_strdup(src->pr_file);
1798 dest->pr_cmd = g_strdup(src->pr_cmd);
1799 dest->col_list = NULL;
1800 for (entry = src->col_list; entry != NULL; entry = g_list_next(entry)) {
1801 src_cfmt = entry->data;
1802 dest_cfmt = (fmt_data *) g_malloc(sizeof(fmt_data));
1803 dest_cfmt->title = g_strdup(src_cfmt->title);
1804 dest_cfmt->fmt = g_strdup(src_cfmt->fmt);
1805 dest->col_list = g_list_append(dest->col_list, dest_cfmt);
1807 dest->num_cols = src->num_cols;
1808 dest->st_client_fg = src->st_client_fg;
1809 dest->st_client_bg = src->st_client_bg;
1810 dest->st_server_fg = src->st_server_fg;
1811 dest->st_server_bg = src->st_server_bg;
1812 dest->gui_scrollbar_on_right = src->gui_scrollbar_on_right;
1813 dest->gui_plist_sel_browse = src->gui_plist_sel_browse;
1814 dest->gui_ptree_sel_browse = src->gui_ptree_sel_browse;
1815 dest->gui_ptree_line_style = src->gui_ptree_line_style;
1816 dest->gui_ptree_expander_style = src->gui_ptree_expander_style;
1817 dest->gui_hex_dump_highlight_style = src->gui_hex_dump_highlight_style;
1818 dest->gui_font_name = g_strdup(src->gui_font_name);
1819 dest->gui_marked_fg = src->gui_marked_fg;
1820 dest->gui_marked_bg = src->gui_marked_bg;
1821 dest->gui_geometry_save_position = src->gui_geometry_save_position;
1822 dest->gui_geometry_save_size = src->gui_geometry_save_size;
1823 dest->gui_geometry_main_x = src->gui_geometry_main_x;
1824 dest->gui_geometry_main_y = src->gui_geometry_main_y;
1825 dest->gui_geometry_main_width = src->gui_geometry_main_width;
1826 dest->gui_geometry_main_height = src->gui_geometry_main_height;
1827 /* values for the capture dialog box */
1828 dest->capture_device = g_strdup(src->capture_device);
1829 dest->capture_prom_mode = src->capture_prom_mode;
1830 dest->capture_real_time = src->capture_real_time;
1831 dest->capture_auto_scroll = src->capture_auto_scroll;
1832 dest->name_resolve = src->name_resolve;
1836 /* Free a set of preferences. */
1838 free_prefs(e_prefs *pr)
1840 if (pr->pr_file != NULL) {
1841 g_free(pr->pr_file);
1844 if (pr->pr_cmd != NULL) {
1849 if (pr->gui_font_name != NULL) {
1850 g_free(pr->gui_font_name);
1851 pr->gui_font_name = NULL;
1853 if (pr->capture_device != NULL) {
1854 g_free(pr->capture_device);
1855 pr->capture_device = NULL;
1860 free_col_info(e_prefs *pr)
1864 while (pr->col_list != NULL) {
1865 cfmt = pr->col_list->data;
1866 g_free(cfmt->title);
1869 pr->col_list = g_list_remove_link(pr->col_list, pr->col_list);
1871 g_list_free(pr->col_list);
1872 pr->col_list = NULL;