2 * Routines for handling preferences
4 * $Id: prefs.c,v 1.31 2000/07/05 09:40:41 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>
57 #include "prefs-int.h"
59 /* Internal functions */
60 static int set_pref(gchar*, gchar*);
61 static GList *get_string_list(gchar *);
62 static void clear_string_list(GList *);
64 #define PF_NAME "preferences"
66 #define GPF_PATH DATAFILE_DIR "/ethereal.conf"
68 static gboolean init_prefs = TRUE;
69 static gchar *pf_path = NULL;
73 gchar *gui_ptree_line_style_text[] =
74 { "NONE", "SOLID", "DOTTED", "TABBED", NULL };
76 gchar *gui_ptree_expander_style_text[] =
77 { "NONE", "SQUARE", "TRIANGLE", "CIRCULAR", NULL };
81 * List of modules with preference settings.
83 static GList *modules;
86 * Register a module that will have preferences.
87 * Specify the name used for the module in the preferences file, the
88 * title used in the tab for it in a preferences dialog box, and a
89 * routine to call back when we apply the preferences.
92 prefs_register_module(const char *name, const char *title,
93 void (*apply_cb)(void))
97 module = g_malloc(sizeof (module_t));
99 module->title = title;
100 module->apply_cb = apply_cb;
101 module->prefs = NULL; /* no preferences, to start */
102 module->numprefs = 0;
104 modules = g_list_append(modules, module);
110 * Find a module, given its name.
113 module_match(gconstpointer a, gconstpointer b)
115 const module_t *module = a;
116 const char *name = b;
118 return strcmp(name, module->name);
122 find_module(char *name)
126 list_entry = g_list_find_custom(modules, name, module_match);
127 if (list_entry == NULL)
128 return NULL; /* no such module */
129 return (module_t *) list_entry->data;
138 do_module_callback(gpointer data, gpointer user_data)
140 module_t *module = data;
141 module_cb_arg_t *arg = user_data;
143 (*arg->callback)(module, arg->user_data);
147 * Call a callback function, with a specified argument, for each module.
150 prefs_module_foreach(module_cb callback, gpointer user_data)
154 arg.callback = callback;
155 arg.user_data = user_data;
156 g_list_foreach(modules, do_module_callback, &arg);
160 call_apply_cb(gpointer data, gpointer user_data)
162 module_t *module = data;
164 (*module->apply_cb)();
168 * Call the "apply" callback function for each module.
171 prefs_apply_all(void)
173 g_list_foreach(modules, call_apply_cb, NULL);
177 * Register a preference in a module's list of preferences.
180 register_preference(module_t *module, const char *name, const char *title,
181 const char *description)
185 preference = g_malloc(sizeof (pref_t));
186 preference->name = name;
187 preference->title = title;
188 preference->description = description;
189 preference->ordinal = module->numprefs;
191 module->prefs = g_list_append(module->prefs, preference);
198 * Find a preference in a module's list of preferences, given the module
199 * and the preference's name.
202 preference_match(gconstpointer a, gconstpointer b)
204 const pref_t *pref = a;
205 const char *name = b;
207 return strcmp(name, pref->name);
210 static struct preference *
211 find_preference(module_t *module, char *name)
215 list_entry = g_list_find_custom(module->prefs, name, preference_match);
216 if (list_entry == NULL)
217 return NULL; /* no such preference */
218 return (struct preference *) list_entry->data;
222 * Register a preference with an unsigned integral value.
225 prefs_register_uint_preference(module_t *module, const char *name,
226 const char *title, const char *description, guint base, guint *var)
230 preference = register_preference(module, name, title, description);
231 preference->type = PREF_UINT;
232 preference->varp.uint = var;
233 preference->info.base = base;
237 * Register a preference with an Boolean value.
240 prefs_register_bool_preference(module_t *module, const char *name,
241 const char *title, const char *description, gboolean *var)
245 preference = register_preference(module, name, title, description);
246 preference->type = PREF_BOOL;
247 preference->varp.bool = var;
251 * Register a preference with an enumerated value.
254 prefs_register_enum_preference(module_t *module, const char *name,
255 const char *title, const char *description, gint *var,
256 const enum_val *enumvals, gboolean radio_buttons)
260 preference = register_preference(module, name, title, description);
261 preference->type = PREF_ENUM;
262 preference->varp.enump = var;
263 preference->info.enum_info.enumvals = enumvals;
264 preference->info.enum_info.radio_buttons = radio_buttons;
268 * Register a preference with a character-string value.
271 prefs_register_string_preference(module_t *module, const char *name,
272 const char *title, const char *description, char **var)
276 preference = register_preference(module, name, title, description);
277 preference->type = PREF_STRING;
278 preference->varp.string = var;
279 preference->saved_val.string = NULL;
288 do_pref_callback(gpointer data, gpointer user_data)
291 pref_cb_arg_t *arg = user_data;
293 (*arg->callback)(pref, arg->user_data);
297 * Call a callback function, with a specified argument, for each preference
301 prefs_pref_foreach(module_t *module, pref_cb callback, gpointer user_data)
305 arg.callback = callback;
306 arg.user_data = user_data;
307 g_list_foreach(module->prefs, do_pref_callback, &arg);
311 * Register all non-dissector modules' preferences.
314 prefs_register_modules(void)
318 /* Parse through a list of comma-separated, quoted strings. Return a
319 list of the string data */
321 get_string_list(gchar *str) {
322 enum { PRE_QUOT, IN_QUOT, POST_QUOT };
324 gint state = PRE_QUOT, i = 0, j = 0;
325 gboolean backslash = FALSE;
326 gchar cur_c, *slstr = NULL;
329 while ((cur_c = str[i]) != '\0') {
330 if (cur_c == '"' && ! backslash) {
334 slstr = (gchar *) g_malloc(sizeof(gchar) * COL_MAX_LEN);
340 sl = g_list_append(sl, slstr);
343 clear_string_list(sl);
349 } else if (cur_c == '\\' && ! backslash) {
351 } else if (cur_c == ',' && state == POST_QUOT) {
353 } else if (state == IN_QUOT && j < COL_MAX_LEN) {
359 if (state != POST_QUOT) {
360 clear_string_list(sl);
366 clear_string_list(GList *sl) {
371 l = g_list_remove_link(l, l);
376 * Takes a string, a pointer to an array of "enum_val"s, and a default gint
378 * The array must be terminated by an entry with a null "name" string.
379 * If the string matches a "name" strings in an entry, the value from that
380 * entry is returned. Otherwise, the default value that was passed as the
381 * third argument is returned.
384 find_val_for_string(const char *needle, const enum_val *haystack,
389 while (haystack[i].name != NULL) {
390 if (strcasecmp(needle, haystack[i].name) == 0) {
391 return haystack[i].value;
395 return default_value;
398 /* Takes an string and a pointer to an array of strings, and a default int value.
399 * The array must be terminated by a NULL string. If the string is found in the array
400 * of strings, the index of that string in the array is returned. Otherwise, the
401 * default value that was passed as the third argument is returned.
404 find_index_from_string_array(char *needle, char **haystack, int default_value)
408 while (haystack[i] != NULL) {
409 if (strcmp(needle, haystack[i]) == 0) {
414 return default_value;
417 /* Preferences file format:
418 * - Configuration directives start at the beginning of the line, and
419 * are terminated with a colon.
420 * - Directives can be continued on the next line by preceding them with
425 # This is a comment line
427 print.file: /a/very/long/path/
432 #define MAX_VAR_LEN 48
433 #define MAX_VAL_LEN 1024
435 #define DEF_NUM_COLS 6
437 static void read_prefs_file(const char *pf_path, FILE *pf);
440 read_prefs(int *gpf_errno_return, char **gpf_path_return,
441 int *pf_errno_return, char **pf_path_return)
446 gchar *col_fmt[] = {"No.", "%m", "Time", "%t",
447 "Source", "%s", "Destination", "%d",
448 "Protocol", "%p", "Info", "%i"};
452 /* Initialize preferences to wired-in default values.
453 They may be overridded by the global preferences file or the
454 user's preferences file. */
456 prefs.pr_format = PR_FMT_TEXT;
457 prefs.pr_dest = PR_DEST_CMD;
458 prefs.pr_file = g_strdup("ethereal.out");
459 prefs.pr_cmd = g_strdup("lpr");
460 prefs.col_list = NULL;
461 for (i = 0; i < DEF_NUM_COLS; i++) {
462 cfmt = (fmt_data *) g_malloc(sizeof(fmt_data));
463 cfmt->title = g_strdup(col_fmt[i * 2]);
464 cfmt->fmt = g_strdup(col_fmt[(i * 2) + 1]);
465 prefs.col_list = g_list_append(prefs.col_list, cfmt);
467 prefs.num_cols = DEF_NUM_COLS;
468 prefs.st_client_fg.pixel = 0;
469 prefs.st_client_fg.red = 32767;
470 prefs.st_client_fg.green = 0;
471 prefs.st_client_fg.blue = 0;
472 prefs.st_client_bg.pixel = 65535;
473 prefs.st_client_bg.red = 65535;
474 prefs.st_client_bg.green = 65535;
475 prefs.st_client_bg.blue = 65535;
476 prefs.st_server_fg.pixel = 0;
477 prefs.st_server_fg.red = 0;
478 prefs.st_server_fg.green = 0;
479 prefs.st_server_fg.blue = 32767;
480 prefs.st_server_bg.pixel = 65535;
481 prefs.st_server_bg.red = 65535;
482 prefs.st_server_bg.green = 65535;
483 prefs.st_server_bg.blue = 65535;
484 prefs.gui_scrollbar_on_right = TRUE;
485 prefs.gui_plist_sel_browse = FALSE;
486 prefs.gui_ptree_sel_browse = FALSE;
487 prefs.gui_ptree_line_style = 0;
488 prefs.gui_ptree_expander_style = 1;
491 /* Read the global preferences file, if it exists. */
492 *gpf_path_return = NULL;
493 if ((pf = fopen(GPF_PATH, "r")) != NULL) {
494 /* We succeeded in opening it; read it. */
495 read_prefs_file(GPF_PATH, pf);
498 /* We failed to open it. If we failed for some reason other than
499 "it doesn't exist", return the errno and the pathname, so our
500 caller can report the error. */
501 if (errno != ENOENT) {
502 *gpf_errno_return = errno;
503 *gpf_path_return = GPF_PATH;
507 /* Construct the pathname of the user's preferences file. */
509 pf_path = (gchar *) g_malloc(strlen(get_home_dir()) + strlen(PF_DIR) +
510 strlen(PF_NAME) + 4);
511 sprintf(pf_path, "%s/%s/%s", get_home_dir(), PF_DIR, PF_NAME);
514 /* Read the user's preferences file, if it exists. */
515 *pf_path_return = NULL;
516 if ((pf = fopen(pf_path, "r")) != NULL) {
517 /* We succeeded in opening it; read it. */
518 read_prefs_file(pf_path, pf);
521 /* We failed to open it. If we failed for some reason other than
522 "it doesn't exist", return the errno and the pathname, so our
523 caller can report the error. */
524 if (errno != ENOENT) {
525 *pf_errno_return = errno;
526 *pf_path_return = pf_path;
534 read_prefs_file(const char *pf_path, FILE *pf)
536 enum { START, IN_VAR, PRE_VAL, IN_VAL, IN_SKIP };
537 gchar cur_var[MAX_VAR_LEN], cur_val[MAX_VAL_LEN];
538 int got_c, state = START;
539 gboolean got_val = FALSE;
540 gint var_len = 0, val_len = 0, fline = 1, pline = 1;
542 while ((got_c = getc(pf)) != EOF) {
548 if (var_len >= MAX_VAR_LEN) {
549 g_warning ("%s line %d: Variable too long", pf_path, fline);
554 if (val_len >= MAX_VAL_LEN) {
555 g_warning ("%s line %d: Value too long", pf_path, fline);
563 if (isalnum(got_c)) {
566 cur_var[var_len] = '\0';
567 cur_val[val_len] = '\0';
568 switch (set_pref(cur_var, cur_val)) {
570 case PREFS_SET_SYNTAX_ERR:
571 g_warning ("%s line %d: Syntax error", pf_path, pline);
574 case PREFS_SET_NO_SUCH_PREF:
575 g_warning ("%s line %d: No such preference \"%s\"", pf_path,
580 g_warning ("%s line %d: Incomplete preference", pf_path, pline);
588 } else if (isspace(got_c) && var_len > 0 && got_val) {
590 } else if (got_c == '#') {
593 g_warning ("%s line %d: Malformed line", pf_path, fline);
598 cur_var[var_len] = got_c;
607 if (!isspace(got_c)) {
609 cur_val[val_len] = got_c;
615 cur_val[val_len] = got_c;
618 while (isspace(cur_val[val_len]) && val_len > 0)
627 cur_var[var_len] = '\0';
628 cur_val[val_len] = '\0';
629 switch (set_pref(cur_var, cur_val)) {
631 case PREFS_SET_SYNTAX_ERR:
632 g_warning ("%s line %d: Syntax error", pf_path, pline);
635 case PREFS_SET_NO_SUCH_PREF:
636 g_warning ("%s line %d: No such preference \"%s\"", pf_path,
641 g_warning ("%s line %d: Incomplete preference", pf_path, pline);
647 * Given a string of the form "<pref name>:<pref value>", as might appear
648 * as an argument to a "-o" option, parse it and set the preference in
649 * question. Return an indication of whether it succeeded or failed
653 prefs_set_pref(char *prefarg)
658 colonp = strchr(prefarg, ':');
660 return PREFS_SET_SYNTAX_ERR;
666 * Skip over any white space (there probably won't be any, but
667 * as we allow it in the preferences file, we might as well
674 * Put the colon back, so if our caller uses, in an
675 * error message, the string they passed us, the message
679 return PREFS_SET_SYNTAX_ERR;
682 ret = set_pref(prefarg, p);
683 *colonp = ':'; /* put the colon back */
687 #define PRS_PRINT_FMT "print.format"
688 #define PRS_PRINT_DEST "print.destination"
689 #define PRS_PRINT_FILE "print.file"
690 #define PRS_PRINT_CMD "print.command"
691 #define PRS_COL_FMT "column.format"
692 #define PRS_STREAM_CL_FG "stream.client.fg"
693 #define PRS_STREAM_CL_BG "stream.client.bg"
694 #define PRS_STREAM_SR_FG "stream.server.fg"
695 #define PRS_STREAM_SR_BG "stream.server.bg"
696 #define PRS_GUI_SCROLLBAR_ON_RIGHT "gui.scrollbar_on_right"
697 #define PRS_GUI_PLIST_SEL_BROWSE "gui.packet_list_sel_browse"
698 #define PRS_GUI_PTREE_SEL_BROWSE "gui.protocol_tree_sel_browse"
699 #define PRS_GUI_PTREE_LINE_STYLE "gui.protocol_tree_line_style"
700 #define PRS_GUI_PTREE_EXPANDER_STYLE "gui.protocol_tree_expander_style"
702 #define RED_COMPONENT(x) ((((x) >> 16) & 0xff) * 65535 / 255)
703 #define GREEN_COMPONENT(x) ((((x) >> 8) & 0xff) * 65535 / 255)
704 #define BLUE_COMPONENT(x) (((x) & 0xff) * 65535 / 255)
706 static gchar *pr_formats[] = { "text", "postscript" };
707 static gchar *pr_dests[] = { "command", "file" };
710 set_pref(gchar *pref_name, gchar *value)
715 unsigned long int cval;
722 if (strcmp(pref_name, PRS_PRINT_FMT) == 0) {
723 if (strcmp(value, pr_formats[PR_FMT_TEXT]) == 0) {
724 prefs.pr_format = PR_FMT_TEXT;
725 } else if (strcmp(value, pr_formats[PR_FMT_PS]) == 0) {
726 prefs.pr_format = PR_FMT_PS;
728 return PREFS_SET_SYNTAX_ERR;
730 } else if (strcmp(pref_name, PRS_PRINT_DEST) == 0) {
731 if (strcmp(value, pr_dests[PR_DEST_CMD]) == 0) {
732 prefs.pr_dest = PR_DEST_CMD;
733 } else if (strcmp(value, pr_dests[PR_DEST_FILE]) == 0) {
734 prefs.pr_dest = PR_DEST_FILE;
736 return PREFS_SET_SYNTAX_ERR;
738 } else if (strcmp(pref_name, PRS_PRINT_FILE) == 0) {
739 if (prefs.pr_file) g_free(prefs.pr_file);
740 prefs.pr_file = g_strdup(value);
741 } else if (strcmp(pref_name, PRS_PRINT_CMD) == 0) {
742 if (prefs.pr_cmd) g_free(prefs.pr_cmd);
743 prefs.pr_cmd = g_strdup(value);
744 } else if (strcmp(pref_name, PRS_COL_FMT) == 0) {
745 if ((col_l = get_string_list(value)) && (g_list_length(col_l) % 2) == 0) {
746 while (prefs.col_list) {
747 cfmt = prefs.col_list->data;
751 prefs.col_list = g_list_remove_link(prefs.col_list, prefs.col_list);
753 llen = g_list_length(col_l);
754 prefs.num_cols = llen / 2;
755 col_l = g_list_first(col_l);
757 cfmt = (fmt_data *) g_malloc(sizeof(fmt_data));
758 cfmt->title = g_strdup(col_l->data);
760 cfmt->fmt = g_strdup(col_l->data);
762 prefs.col_list = g_list_append(prefs.col_list, cfmt);
764 /* To do: else print some sort of error? */
766 clear_string_list(col_l);
767 } else if (strcmp(pref_name, PRS_STREAM_CL_FG) == 0) {
768 cval = strtoul(value, NULL, 16);
769 prefs.st_client_fg.pixel = 0;
770 prefs.st_client_fg.red = RED_COMPONENT(cval);
771 prefs.st_client_fg.green = GREEN_COMPONENT(cval);
772 prefs.st_client_fg.blue = BLUE_COMPONENT(cval);
773 } else if (strcmp(pref_name, PRS_STREAM_CL_BG) == 0) {
774 cval = strtoul(value, NULL, 16);
775 prefs.st_client_bg.pixel = 0;
776 prefs.st_client_bg.red = RED_COMPONENT(cval);
777 prefs.st_client_bg.green = GREEN_COMPONENT(cval);
778 prefs.st_client_bg.blue = BLUE_COMPONENT(cval);
779 } else if (strcmp(pref_name, PRS_STREAM_SR_FG) == 0) {
780 cval = strtoul(value, NULL, 16);
781 prefs.st_server_fg.pixel = 0;
782 prefs.st_server_fg.red = RED_COMPONENT(cval);
783 prefs.st_server_fg.green = GREEN_COMPONENT(cval);
784 prefs.st_server_fg.blue = BLUE_COMPONENT(cval);
785 } else if (strcmp(pref_name, PRS_STREAM_SR_BG) == 0) {
786 cval = strtoul(value, NULL, 16);
787 prefs.st_server_bg.pixel = 0;
788 prefs.st_server_bg.red = RED_COMPONENT(cval);
789 prefs.st_server_bg.green = GREEN_COMPONENT(cval);
790 prefs.st_server_bg.blue = BLUE_COMPONENT(cval);
791 } else if (strcmp(pref_name, PRS_GUI_SCROLLBAR_ON_RIGHT) == 0) {
792 if (strcmp(value, "TRUE") == 0) {
793 prefs.gui_scrollbar_on_right = TRUE;
796 prefs.gui_scrollbar_on_right = FALSE;
798 } else if (strcmp(pref_name, PRS_GUI_PLIST_SEL_BROWSE) == 0) {
799 if (strcmp(value, "TRUE") == 0) {
800 prefs.gui_plist_sel_browse = TRUE;
803 prefs.gui_plist_sel_browse = FALSE;
805 } else if (strcmp(pref_name, PRS_GUI_PTREE_SEL_BROWSE) == 0) {
806 if (strcmp(value, "TRUE") == 0) {
807 prefs.gui_ptree_sel_browse = TRUE;
810 prefs.gui_ptree_sel_browse = FALSE;
812 } else if (strcmp(pref_name, PRS_GUI_PTREE_LINE_STYLE) == 0) {
813 prefs.gui_ptree_line_style =
814 find_index_from_string_array(value, gui_ptree_line_style_text, 0);
815 } else if (strcmp(pref_name, PRS_GUI_PTREE_EXPANDER_STYLE) == 0) {
816 prefs.gui_ptree_expander_style =
817 find_index_from_string_array(value, gui_ptree_expander_style_text, 1);
819 /* To which module does this preference belong? */
820 dotp = strchr(pref_name, '.');
822 return PREFS_SET_SYNTAX_ERR; /* no ".", so no module/name separator */
823 *dotp = '\0'; /* separate module and preference name */
824 module = find_module(pref_name);
825 *dotp = '.'; /* put the preference string back */
827 return PREFS_SET_NO_SUCH_PREF; /* no such module */
828 dotp++; /* skip past separator to preference name */
829 pref = find_preference(module, dotp);
831 return PREFS_SET_NO_SUCH_PREF; /* no such preference */
833 switch (pref->type) {
836 uval = strtoul(value, &p, pref->info.base);
837 if (p == value || *p != '\0')
838 return PREFS_SET_SYNTAX_ERR; /* number was bad */
839 *pref->varp.uint = uval;
843 /* XXX - give an error if it's neither "true" nor "false"? */
844 if (strcasecmp(value, "true") == 0)
845 *pref->varp.bool = TRUE;
847 *pref->varp.bool = FALSE;
851 /* XXX - give an error if it doesn't match? */
852 *pref->varp.enump = find_val_for_string(value,
853 pref->info.enum_info.enumvals, 1);
857 if (*pref->varp.string != NULL)
858 g_free(*pref->varp.string);
859 *pref->varp.string = g_strdup(value);
873 * Write out a single preference.
876 write_pref(gpointer data, gpointer user_data)
879 write_pref_arg_t *arg = user_data;
880 const enum_val *enum_valp;
881 const char *val_string;
883 fprintf(arg->pf, "\n# %s\n", pref->description);
885 switch (pref->type) {
888 switch (pref->info.base) {
891 fprintf(arg->pf, "# A decimal number.\n");
892 fprintf(arg->pf, "%s.%s: %u\n", arg->module->name,
893 pref->name, *pref->varp.uint);
897 fprintf(arg->pf, "# An octal number.\n");
898 fprintf(arg->pf, "%s.%s: %#o\n", arg->module->name,
899 pref->name, *pref->varp.uint);
903 fprintf(arg->pf, "# A hexadecimal number.\n");
904 fprintf(arg->pf, "%s.%s: %#x\n", arg->module->name,
905 pref->name, *pref->varp.uint);
911 fprintf(arg->pf, "# TRUE or FALSE (case-insensitive).\n");
912 fprintf(arg->pf, "%s.%s: %s\n", arg->module->name, pref->name,
913 *pref->varp.bool ? "TRUE" : "FALSE");
917 fprintf(arg->pf, "# One of: ");
918 enum_valp = pref->info.enum_info.enumvals;
920 while (enum_valp->name != NULL) {
921 if (enum_valp->value == *pref->varp.enump)
922 val_string = enum_valp->name;
923 fprintf(arg->pf, "%s", enum_valp->name);
925 if (enum_valp->name == NULL)
926 fprintf(arg->pf, "\n");
928 fprintf(arg->pf, ", ");
930 fprintf(arg->pf, "# (case-insensitive).\n");
931 fprintf(arg->pf, "%s.%s: %s\n", arg->module->name, pref->name,
936 fprintf(arg->pf, "# A string.\n");
937 fprintf(arg->pf, "%s.%s: %s\n", arg->module->name, pref->name,
944 write_module_prefs(gpointer data, gpointer user_data)
946 write_pref_arg_t arg;
950 g_list_foreach(arg.module->prefs, write_pref, &arg);
954 write_prefs(char **pf_path_return)
960 * - Split output lines longer than MAX_VAL_LEN
961 * - Create a function for the preference directory check/creation
962 * so that duplication can be avoided with filter.c
966 pf_path = (gchar *) g_malloc(strlen(get_home_dir()) + strlen(PF_DIR) +
967 strlen(PF_NAME) + 4);
970 sprintf(pf_path, "%s/%s", get_home_dir(), PF_DIR);
971 if (stat(pf_path, &s_buf) != 0)
975 mkdir(pf_path, 0755);
978 sprintf(pf_path, "%s/%s/%s", get_home_dir(), PF_DIR, PF_NAME);
979 if ((pf = fopen(pf_path, "w")) == NULL) {
980 *pf_path_return = pf_path;
984 fputs("# Configuration file for Ethereal " VERSION ".\n"
986 "# This file is regenerated each time preferences are saved within\n"
987 "# Ethereal. Making manual changes should be safe, however.\n"
989 "######## Printing ########\n"
992 fprintf (pf, "# Can be one of \"text\" or \"postscript\".\n"
993 "print.format: %s\n\n", pr_formats[prefs.pr_format]);
995 fprintf (pf, "# Can be one of \"command\" or \"file\".\n"
996 "print.destination: %s\n\n", pr_dests[prefs.pr_dest]);
998 fprintf (pf, "# This is the file that gets written to when the "
999 "destination is set to \"file\"\n"
1000 "%s: %s\n\n", PRS_PRINT_FILE, prefs.pr_file);
1002 fprintf (pf, "# Output gets piped to this command when the destination "
1003 "is set to \"command\"\n"
1004 "%s: %s\n\n", PRS_PRINT_CMD, prefs.pr_cmd);
1006 fprintf (pf, "# Packet list column format. Each pair of strings consists "
1007 "of a column title \n# and its format.\n"
1008 "%s: %s\n\n", PRS_COL_FMT, col_format_to_pref_str());
1010 fprintf (pf, "# TCP stream window color preferences. Each value is a six "
1011 "digit hexadecimal value in the form rrggbb.\n");
1012 fprintf (pf, "%s: %02x%02x%02x\n", PRS_STREAM_CL_FG,
1013 (prefs.st_client_fg.red * 255 / 65535),
1014 (prefs.st_client_fg.green * 255 / 65535),
1015 (prefs.st_client_fg.blue * 255 / 65535));
1016 fprintf (pf, "%s: %02x%02x%02x\n", PRS_STREAM_CL_BG,
1017 (prefs.st_client_bg.red * 255 / 65535),
1018 (prefs.st_client_bg.green * 255 / 65535),
1019 (prefs.st_client_bg.blue * 255 / 65535));
1020 fprintf (pf, "%s: %02x%02x%02x\n", PRS_STREAM_SR_FG,
1021 (prefs.st_server_fg.red * 255 / 65535),
1022 (prefs.st_server_fg.green * 255 / 65535),
1023 (prefs.st_server_fg.blue * 255 / 65535));
1024 fprintf (pf, "%s: %02x%02x%02x\n", PRS_STREAM_SR_BG,
1025 (prefs.st_server_bg.red * 255 / 65535),
1026 (prefs.st_server_bg.green * 255 / 65535),
1027 (prefs.st_server_bg.blue * 255 / 65535));
1029 fprintf(pf, "\n# Vertical scrollbars should be on right side? TRUE/FALSE\n");
1030 fprintf(pf, PRS_GUI_SCROLLBAR_ON_RIGHT ": %s\n",
1031 prefs.gui_scrollbar_on_right == TRUE ? "TRUE" : "FALSE");
1033 fprintf(pf, "\n# Packet-list selection bar can be used to browse w/o selecting? TRUE/FALSE\n");
1034 fprintf(pf, PRS_GUI_PLIST_SEL_BROWSE ": %s\n",
1035 prefs.gui_plist_sel_browse == TRUE ? "TRUE" : "FALSE");
1037 fprintf(pf, "\n# Protocol-tree selection bar can be used to browse w/o selecting? TRUE/FALSE\n");
1038 fprintf(pf, PRS_GUI_PTREE_SEL_BROWSE ": %s\n",
1039 prefs.gui_ptree_sel_browse == TRUE ? "TRUE" : "FALSE");
1041 fprintf(pf, "\n# Protocol-tree line style. One of: NONE, SOLID, DOTTED, TABBED\n");
1042 fprintf(pf, PRS_GUI_PTREE_LINE_STYLE ": %s\n",
1043 gui_ptree_line_style_text[prefs.gui_ptree_line_style]);
1045 fprintf(pf, "\n# Protocol-tree expander style. One of: NONE, SQUARE, TRIANGLE, CIRCULAR\n");
1046 fprintf(pf, PRS_GUI_PTREE_EXPANDER_STYLE ": %s\n",
1047 gui_ptree_expander_style_text[prefs.gui_ptree_expander_style]);
1049 g_list_foreach(modules, write_module_prefs, pf);
1053 /* XXX - catch I/O errors (e.g. "ran out of disk space") and return
1054 an error indication, or maybe write to a new preferences file and
1055 rename that file on top of the old one only if there are not I/O