Fix Gerald's address.
[obnox/wireshark/wip.git] / prefs.c
1 /* prefs.c
2  * Routines for handling preferences
3  *
4  * $Id: prefs.c,v 1.50 2001/04/15 03:37:13 guy Exp $
5  *
6  * Ethereal - Network traffic analyzer
7  * By Gerald Combs <gerald@zing.org>
8  * Copyright 1998 Gerald Combs
9  *
10  * 
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.
15  * 
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.
20  * 
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.
24  */
25
26 #ifdef HAVE_CONFIG_H
27 # include "config.h"
28 #endif
29
30 #ifdef HAVE_SYS_TYPES_H
31 #include <sys/types.h>
32 #endif
33
34 #ifdef HAVE_DIRECT_H
35 #include <direct.h>
36 #endif
37
38 #include <stdlib.h>
39 #include <string.h>
40 #include <ctype.h>
41 #include <errno.h>
42
43 #ifdef HAVE_UNISTD_H
44 #include <unistd.h>
45 #endif
46
47 #ifdef HAVE_SYS_STAT_H
48 #include <sys/stat.h>
49 #endif
50
51 #include <epan.h>
52 #include <filesystem.h>
53 #include "globals.h"
54 #include "packet.h"
55 #include "file.h"
56 #include "prefs.h"
57 #include "proto.h"
58 #include "column.h"
59 #include "print.h"
60
61 #include "prefs-int.h"
62
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 *);
68
69 #define PF_NAME "preferences"
70
71 #define GPF_PATH        DATAFILE_DIR "/ethereal.conf"
72
73 static gboolean init_prefs = TRUE;
74 static gchar *pf_path = NULL;
75
76 /*
77  * XXX - variables to allow us to attempt to interpret the first
78  * "mgcp.{tcp,udp}.port" in a preferences file as
79  * "mgcp.{tcp,udp}.gateway_port" and the second as
80  * "mgcp.{tcp,udp}.callagent_port".
81  */
82 static int mgcp_tcp_port_count;
83 static int mgcp_udp_port_count;
84
85 e_prefs prefs;
86
87 gchar   *gui_ptree_line_style_text[] =
88         { "NONE", "SOLID", "DOTTED", "TABBED", NULL };
89
90 gchar   *gui_ptree_expander_style_text[] =
91         { "NONE", "SQUARE", "TRIANGLE", "CIRCULAR", NULL };
92
93 gchar   *gui_hex_dump_highlight_style_text[] =
94         { "BOLD", "INVERSE", NULL };
95
96 /*
97  * List of modules with preference settings.
98  */
99 static GList *modules;
100
101 /*
102  * Register a module that will have preferences.
103  * Specify the name used for the module in the preferences file, the
104  * title used in the tab for it in a preferences dialog box, and a
105  * routine to call back when we apply the preferences.
106  */
107 module_t *
108 prefs_register_module(const char *name, const char *title,
109     void (*apply_cb)(void))
110 {
111         module_t *module;
112
113         module = g_malloc(sizeof (module_t));
114         module->name = name;
115         module->title = title;
116         module->apply_cb = apply_cb;
117         module->prefs = NULL;   /* no preferences, to start */
118         module->numprefs = 0;
119         module->prefs_changed = FALSE;
120
121         modules = g_list_append(modules, module);
122
123         return module;
124 }
125
126 /*
127  * Register that a protocol has preferences.
128  */
129 module_t *
130 prefs_register_protocol(int id, void (*apply_cb)(void))
131 {
132         return prefs_register_module(proto_get_protocol_filter_name(id),
133                                      proto_get_protocol_short_name(id),
134                                      apply_cb);
135 }
136
137 /*
138  * Find a module, given its name.
139  */
140 static gint
141 module_match(gconstpointer a, gconstpointer b)
142 {
143         const module_t *module = a;
144         const char *name = b;
145
146         return strcmp(name, module->name);
147 }
148
149 static module_t *
150 find_module(char *name)
151 {
152         GList *list_entry;
153
154         list_entry = g_list_find_custom(modules, name, module_match);
155         if (list_entry == NULL)
156                 return NULL;    /* no such module */
157         return (module_t *) list_entry->data;
158 }
159
160 typedef struct {
161         module_cb callback;
162         gpointer user_data;
163 } module_cb_arg_t;
164
165 static void
166 do_module_callback(gpointer data, gpointer user_data)
167 {
168         module_t *module = data;
169         module_cb_arg_t *arg = user_data;
170
171         (*arg->callback)(module, arg->user_data);
172 }
173
174 /*
175  * Call a callback function, with a specified argument, for each module.
176  */
177 void
178 prefs_module_foreach(module_cb callback, gpointer user_data)
179 {
180         module_cb_arg_t arg;
181
182         arg.callback = callback;
183         arg.user_data = user_data;
184         g_list_foreach(modules, do_module_callback, &arg);
185 }
186
187 static void
188 call_apply_cb(gpointer data, gpointer user_data)
189 {
190         module_t *module = data;
191
192         if (module->prefs_changed) {
193                 if (module->apply_cb != NULL)
194                         (*module->apply_cb)();
195                 module->prefs_changed = FALSE;
196         }
197 }
198
199 /*
200  * Call the "apply" callback function for each module if any of its
201  * preferences have changed, and then clear the flag saying its
202  * preferences have changed, as the module has been notified of that
203  * fact.
204  */
205 void
206 prefs_apply_all(void)
207 {
208         g_list_foreach(modules, call_apply_cb, NULL);
209 }
210
211 /*
212  * Register a preference in a module's list of preferences.
213  */
214 static pref_t *
215 register_preference(module_t *module, const char *name, const char *title,
216     const char *description)
217 {
218         pref_t *preference;
219
220         preference = g_malloc(sizeof (pref_t));
221         preference->name = name;
222         preference->title = title;
223         preference->description = description;
224         preference->ordinal = module->numprefs;
225
226         module->prefs = g_list_append(module->prefs, preference);
227         module->numprefs++;
228
229         return preference;
230 }
231
232 /*
233  * Find a preference in a module's list of preferences, given the module
234  * and the preference's name.
235  */
236 static gint
237 preference_match(gconstpointer a, gconstpointer b)
238 {
239         const pref_t *pref = a;
240         const char *name = b;
241
242         return strcmp(name, pref->name);
243 }
244
245 static struct preference *
246 find_preference(module_t *module, char *name)
247 {
248         GList *list_entry;
249
250         list_entry = g_list_find_custom(module->prefs, name, preference_match);
251         if (list_entry == NULL)
252                 return NULL;    /* no such preference */
253         return (struct preference *) list_entry->data;
254 }
255
256 /*
257  * Returns TRUE if the given protocol has registered preferences
258  */
259 gboolean
260 prefs_is_registered_protocol(char *name)
261 {
262         return (find_module(name) != NULL);
263 }
264
265 /*
266  * Returns the module title of a registered protocol
267  */
268 const char *
269 prefs_get_title_by_name(char *name)
270 {
271         module_t *m = find_module(name);
272         return  (m) ? m->title : NULL;
273 }
274
275 /*
276  * Register a preference with an unsigned integral value.
277  */
278 void
279 prefs_register_uint_preference(module_t *module, const char *name,
280     const char *title, const char *description, guint base, guint *var)
281 {
282         pref_t *preference;
283
284         preference = register_preference(module, name, title, description);
285         preference->type = PREF_UINT;
286         preference->varp.uint = var;
287         preference->info.base = base;
288 }
289
290 /*
291  * Register a preference with an Boolean value.
292  */
293 void
294 prefs_register_bool_preference(module_t *module, const char *name,
295     const char *title, const char *description, gboolean *var)
296 {
297         pref_t *preference;
298
299         preference = register_preference(module, name, title, description);
300         preference->type = PREF_BOOL;
301         preference->varp.bool = var;
302 }
303
304 /*
305  * Register a preference with an enumerated value.
306  */
307 void
308 prefs_register_enum_preference(module_t *module, const char *name,
309     const char *title, const char *description, gint *var,
310     const enum_val_t *enumvals, gboolean radio_buttons)
311 {
312         pref_t *preference;
313
314         preference = register_preference(module, name, title, description);
315         preference->type = PREF_ENUM;
316         preference->varp.enump = var;
317         preference->info.enum_info.enumvals = enumvals;
318         preference->info.enum_info.radio_buttons = radio_buttons;
319 }
320
321 /*
322  * Register a preference with a character-string value.
323  */
324 void
325 prefs_register_string_preference(module_t *module, const char *name,
326     const char *title, const char *description, char **var)
327 {
328         pref_t *preference;
329
330         preference = register_preference(module, name, title, description);
331         preference->type = PREF_STRING;
332         preference->varp.string = var;
333         preference->saved_val.string = NULL;
334 }
335
336 typedef struct {
337         pref_cb callback;
338         gpointer user_data;
339 } pref_cb_arg_t;
340
341 static void
342 do_pref_callback(gpointer data, gpointer user_data)
343 {
344         pref_t *pref = data;
345         pref_cb_arg_t *arg = user_data;
346
347         (*arg->callback)(pref, arg->user_data);
348 }
349
350 /*
351  * Call a callback function, with a specified argument, for each preference
352  * in a given module.
353  */
354 void
355 prefs_pref_foreach(module_t *module, pref_cb callback, gpointer user_data)
356 {
357         pref_cb_arg_t arg;
358
359         arg.callback = callback;
360         arg.user_data = user_data;
361         g_list_foreach(module->prefs, do_pref_callback, &arg);
362 }
363
364 /*
365  * Register all non-dissector modules' preferences.
366  */
367 void
368 prefs_register_modules(void)
369 {
370 }
371
372 /* Parse through a list of comma-separated, quoted strings.  Return a
373    list of the string data */
374 static GList *
375 get_string_list(gchar *str) {
376   enum { PRE_QUOT, IN_QUOT, POST_QUOT };
377
378   gint      state = PRE_QUOT, i = 0, j = 0;
379   gboolean  backslash = FALSE;
380   gchar     cur_c, *slstr = NULL;
381   GList    *sl = NULL;
382   
383   while ((cur_c = str[i]) != '\0') {
384     if (cur_c == '"' && ! backslash) {
385       switch (state) {
386         case PRE_QUOT:
387           state = IN_QUOT;
388           slstr = (gchar *) g_malloc(sizeof(gchar) * COL_MAX_LEN);
389           j = 0;
390           break;
391         case IN_QUOT:
392           state  = POST_QUOT;
393           slstr[j] = '\0';
394           sl = g_list_append(sl, slstr);
395           break;
396         case POST_QUOT:
397           clear_string_list(sl);
398           return NULL;
399           break;
400         default:
401           break;
402       }
403     } else if (cur_c == '\\' && ! backslash) {
404       backslash = TRUE;
405     } else if (cur_c == ',' && state == POST_QUOT) {
406       state = PRE_QUOT;
407     } else if (state == IN_QUOT && j < COL_MAX_LEN) {
408       slstr[j] = str[i];
409       j++;
410     }
411     i++;
412   }
413   if (state != POST_QUOT) {
414     clear_string_list(sl);
415   }
416   return(sl);
417 }
418
419 void
420 clear_string_list(GList *sl) {
421   GList *l = sl;
422   
423   while (l) {
424     g_free(l->data);
425     l = g_list_remove_link(l, l);
426   }
427 }
428
429 /*
430  * Takes a string, a pointer to an array of "enum_val_t"s, and a default gint
431  * value.
432  * The array must be terminated by an entry with a null "name" string.
433  * If the string matches a "name" strings in an entry, the value from that
434  * entry is returned. Otherwise, the default value that was passed as the
435  * third argument is returned.
436  */
437 gint
438 find_val_for_string(const char *needle, const enum_val_t *haystack,
439     gint default_value)
440 {
441         int i = 0;
442
443         while (haystack[i].name != NULL) {
444                 if (strcasecmp(needle, haystack[i].name) == 0) {
445                         return haystack[i].value;
446                 }
447                 i++;    
448         }
449         return default_value;
450 }
451
452 /* Takes an string and a pointer to an array of strings, and a default int value.
453  * The array must be terminated by a NULL string. If the string is found in the array
454  * of strings, the index of that string in the array is returned. Otherwise, the
455  * default value that was passed as the third argument is returned.
456  */
457 static int
458 find_index_from_string_array(char *needle, char **haystack, int default_value)
459 {
460         int i = 0;
461
462         while (haystack[i] != NULL) {
463                 if (strcmp(needle, haystack[i]) == 0) {
464                         return i;
465                 }
466                 i++;    
467         }
468         return default_value;
469 }
470
471 /* Preferences file format:
472  * - Configuration directives start at the beginning of the line, and 
473  *   are terminated with a colon.
474  * - Directives can be continued on the next line by preceding them with
475  *   whitespace.
476  *
477  * Example:
478
479 # This is a comment line
480 print.command: lpr
481 print.file: /a/very/long/path/
482         to/ethereal-out.ps
483  *
484  */
485
486 #define MAX_VAR_LEN    48
487 #define MAX_VAL_LEN  1024
488
489 #define DEF_NUM_COLS    6
490
491 static void read_prefs_file(const char *pf_path, FILE *pf);
492
493 /* Read the preferences file, fill in "prefs", and return a pointer to it.
494
495    If we got an error (other than "it doesn't exist") trying to read
496    the global preferences file, stuff the errno into "*gpf_errno_return"
497    and a pointer to the path of the file into "*gpf_path_return", and
498    return NULL.
499
500    If we got an error (other than "it doesn't exist") trying to read
501    the user's preferences file, stuff the errno into "*pf_errno_return"
502    and a pointer to the path of the file into "*pf_path_return", and
503    return NULL. */
504 e_prefs *
505 read_prefs(int *gpf_errno_return, char **gpf_path_return,
506            int *pf_errno_return, char **pf_path_return)
507 {
508   int       i;
509   FILE     *pf;
510   fmt_data *cfmt;
511   gchar    *col_fmt[] = {"No.",      "%m", "Time",        "%t",
512                          "Source",   "%s", "Destination", "%d",
513                          "Protocol", "%p", "Info",        "%i"};
514
515   
516   if (init_prefs) {
517     /* Initialize preferences to wired-in default values.
518        They may be overridded by the global preferences file or the
519        user's preferences file. */
520     init_prefs       = FALSE;
521     prefs.pr_format  = PR_FMT_TEXT;
522     prefs.pr_dest    = PR_DEST_CMD;
523     prefs.pr_file    = g_strdup("ethereal.out");
524     prefs.pr_cmd     = g_strdup("lpr");
525     prefs.col_list = NULL;
526     for (i = 0; i < DEF_NUM_COLS; i++) {
527       cfmt = (fmt_data *) g_malloc(sizeof(fmt_data));
528       cfmt->title = g_strdup(col_fmt[i * 2]);
529       cfmt->fmt   = g_strdup(col_fmt[(i * 2) + 1]);
530       prefs.col_list = g_list_append(prefs.col_list, cfmt);
531     }
532     prefs.num_cols  = DEF_NUM_COLS;
533     prefs.st_client_fg.pixel =     0;
534     prefs.st_client_fg.red   = 32767;
535     prefs.st_client_fg.green =     0;
536     prefs.st_client_fg.blue  =     0;
537     prefs.st_client_bg.pixel = 65535;
538     prefs.st_client_bg.red   = 65535;
539     prefs.st_client_bg.green = 65535;
540     prefs.st_client_bg.blue  = 65535;
541     prefs.st_server_fg.pixel =     0;
542     prefs.st_server_fg.red   =     0;
543     prefs.st_server_fg.green =     0;
544     prefs.st_server_fg.blue  = 32767;
545     prefs.st_server_bg.pixel = 65535;
546     prefs.st_server_bg.red   = 65535;
547     prefs.st_server_bg.green = 65535;
548     prefs.st_server_bg.blue  = 65535;
549     prefs.gui_scrollbar_on_right = TRUE;
550     prefs.gui_plist_sel_browse = FALSE;
551     prefs.gui_ptree_sel_browse = FALSE;
552     prefs.gui_ptree_line_style = 0;
553     prefs.gui_ptree_expander_style = 1;
554     prefs.gui_hex_dump_highlight_style = 1;
555 #ifdef WIN32
556     prefs.gui_font_name = g_strdup("-*-lucida console-medium-r-*-*-*-100-*-*-*-*-*-*");
557 #else
558     /*
559      * XXX - for now, we make the initial font name a pattern that matches
560      * only ISO 8859/1 fonts, so that we don't match 2-byte fonts such
561      * as ISO 10646 fonts.
562      *
563      * Users in locales using other one-byte fonts will have to choose
564      * a different font from the preferences dialog - or put the font
565      * selection in the global preferences file to make that font the
566      * default for all users who don't explicitly specify a different
567      * font.
568      *
569      * Making this a font set rather than a font has two problems:
570      *
571      *  1) as far as I know, you can't select font sets with the
572      *     font selection dialog;
573      *
574      *  2) if you use a font set, the text to be drawn must be a
575      *     multi-byte string in the appropriate locale, but
576      *     Ethereal does *NOT* guarantee that's the case - in
577      *     the hex-dump window, each character in the text portion
578      *     of the display must be a *single* byte, and in the
579      *     packet-list and protocol-tree windows, text extracted
580      *     from the packet is not necessarily in the right format.
581      *
582      * "Doing this right" may, for the packet-list and protocol-tree
583      * windows, require that dissectors know what the locale is
584      * *AND* know what locale and text representation is used in
585      * the packets they're dissecting, and may be impossible in
586      * the hex-dump window (except by punting and displaying only
587      * ASCII characters).
588      *
589      * GTK+ 2.0 may simplify part of the problem, as it will, as I
590      * understand it, use UTF-8-encoded Unicode as its internal
591      * character set; however, we'd still have to know whatever
592      * character set and encoding is used in the packet (which
593      * may differ for different protocols, e.g. SMB might use
594      * PC code pages for some strings and Unicode for others, whilst
595      * NFS might use some UNIX character set encoding, e.g. ISO 8859/x,
596      * or one of the EUC character sets for Asian languages, or one
597      * of the other multi-byte character sets, or UTF-8, or...).
598      *
599      * I.e., as far as I can tell, "internationalizing" the packet-list,
600      * protocol-tree, and hex-dump windows involves a lot more than, say,
601      * just using font sets rather than fonts.
602      */
603     prefs.gui_font_name = g_strdup("-*-fixed-medium-r-semicondensed-*-*-120-*-*-*-*-iso8859-1");
604 #endif
605     prefs.gui_marked_fg.pixel = 65535;
606     prefs.gui_marked_fg.red   = 65535;
607     prefs.gui_marked_fg.green = 65535;
608     prefs.gui_marked_fg.blue  = 65535;
609     prefs.gui_marked_bg.pixel =     0;
610     prefs.gui_marked_bg.red   =     0;
611     prefs.gui_marked_bg.green =     0;
612     prefs.gui_marked_bg.blue  =     0;
613
614 /* set the default values for the capture dialog box */
615     prefs.capture_prom_mode   =     0;
616     prefs.capture_real_time   =     0;
617     prefs.capture_auto_scroll =     0;
618     prefs.name_resolve=     1;
619
620   }
621
622   /* Read the global preferences file, if it exists. */
623   *gpf_path_return = NULL;
624   if ((pf = fopen(GPF_PATH, "r")) != NULL) {
625     /* We succeeded in opening it; read it. */
626     read_prefs_file(GPF_PATH, pf);
627     fclose(pf);
628   } else {
629     /* We failed to open it.  If we failed for some reason other than
630        "it doesn't exist", return the errno and the pathname, so our
631        caller can report the error. */
632     if (errno != ENOENT) {
633       *gpf_errno_return = errno;
634       *gpf_path_return = GPF_PATH;
635     }
636   }
637
638   /* Construct the pathname of the user's preferences file. */
639   if (! pf_path) {
640     pf_path = (gchar *) g_malloc(strlen(get_home_dir()) + strlen(PF_DIR) +
641       strlen(PF_NAME) + 4);
642     sprintf(pf_path, "%s/%s/%s", get_home_dir(), PF_DIR, PF_NAME);
643   }
644     
645   /* Read the user's preferences file, if it exists. */
646   *pf_path_return = NULL;
647   if ((pf = fopen(pf_path, "r")) != NULL) {
648     /* We succeeded in opening it; read it. */
649     read_prefs_file(pf_path, pf);
650     fclose(pf);
651   } else {
652     /* We failed to open it.  If we failed for some reason other than
653        "it doesn't exist", return the errno and the pathname, so our
654        caller can report the error. */
655     if (errno != ENOENT) {
656       *pf_errno_return = errno;
657       *pf_path_return = pf_path;
658     }
659   }
660   
661   return &prefs;
662 }
663
664 static void
665 read_prefs_file(const char *pf_path, FILE *pf)
666 {
667   enum { START, IN_VAR, PRE_VAL, IN_VAL, IN_SKIP };
668   gchar     cur_var[MAX_VAR_LEN], cur_val[MAX_VAL_LEN];
669   int       got_c, state = START;
670   gboolean  got_val = FALSE;
671   gint      var_len = 0, val_len = 0, fline = 1, pline = 1;
672
673   /*
674    * Start out the counters of "mgcp.{tcp,udp}.port" entries we've
675    * seen.
676    */
677   mgcp_tcp_port_count = 0;
678   mgcp_udp_port_count = 0;
679
680   while ((got_c = getc(pf)) != EOF) {
681     if (got_c == '\n') {
682       state = START;
683       fline++;
684       continue;
685     }
686     if (var_len >= MAX_VAR_LEN) {
687       g_warning ("%s line %d: Variable too long", pf_path, fline);
688       state = IN_SKIP;
689       var_len = 0;
690       continue;
691     }
692     if (val_len >= MAX_VAL_LEN) {
693       g_warning ("%s line %d: Value too long", pf_path, fline);
694       state = IN_SKIP;
695       var_len = 0;
696       continue;
697     }
698     
699     switch (state) {
700       case START:
701         if (isalnum(got_c)) {
702           if (var_len > 0) {
703             if (got_val) {
704               cur_var[var_len] = '\0';
705               cur_val[val_len] = '\0';
706               switch (set_pref(cur_var, cur_val)) {
707
708               case PREFS_SET_SYNTAX_ERR:
709                 g_warning ("%s line %d: Syntax error", pf_path, pline);
710                 break;
711
712               case PREFS_SET_NO_SUCH_PREF:
713                 g_warning ("%s line %d: No such preference \"%s\"", pf_path,
714                                 pline, cur_var);
715                 break;
716               }
717             } else {
718               g_warning ("%s line %d: Incomplete preference", pf_path, pline);
719             }
720           }
721           state      = IN_VAR;
722           got_val    = FALSE;
723           cur_var[0] = got_c;
724           var_len    = 1;
725           pline = fline;
726         } else if (isspace(got_c) && var_len > 0 && got_val) {
727           state = PRE_VAL;
728         } else if (got_c == '#') {
729           state = IN_SKIP;
730         } else {
731           g_warning ("%s line %d: Malformed line", pf_path, fline);
732         }
733         break;
734       case IN_VAR:
735         if (got_c != ':') {
736           cur_var[var_len] = got_c;
737           var_len++;
738         } else {
739           state   = PRE_VAL;
740           val_len = 0;
741           got_val = TRUE;
742         }
743         break;
744       case PRE_VAL:
745         if (!isspace(got_c)) {
746           state = IN_VAL;
747           cur_val[val_len] = got_c;
748           val_len++;
749         }
750         break;
751       case IN_VAL:
752         if (got_c != '#')  {
753           cur_val[val_len] = got_c;
754           val_len++;
755         } else {
756           while (isspace((guchar)cur_val[val_len]) && val_len > 0)
757             val_len--;
758           state = IN_SKIP;
759         }
760         break;
761     }
762   }
763   if (var_len > 0) {
764     if (got_val) {
765       cur_var[var_len] = '\0';
766       cur_val[val_len] = '\0';
767       switch (set_pref(cur_var, cur_val)) {
768
769       case PREFS_SET_SYNTAX_ERR:
770         g_warning ("%s line %d: Syntax error", pf_path, pline);
771         break;
772
773       case PREFS_SET_NO_SUCH_PREF:
774         g_warning ("%s line %d: No such preference \"%s\"", pf_path,
775                         pline, cur_var);
776         break;
777       }
778     } else {
779       g_warning ("%s line %d: Incomplete preference", pf_path, pline);
780     }
781   }
782 }
783
784 /*
785  * Given a string of the form "<pref name>:<pref value>", as might appear
786  * as an argument to a "-o" option, parse it and set the preference in
787  * question.  Return an indication of whether it succeeded or failed
788  * in some fashion.
789  */
790 int
791 prefs_set_pref(char *prefarg)
792 {
793         u_char *p, *colonp;
794         int ret;
795
796         /*
797          * Set the counters of "mgcp.{tcp,udp}.port" entries we've
798          * seen to values that keep us from trying to interpret tham
799          * as "mgcp.{tcp,udp}.gateway_port" or "mgcp.{tcp,udp}.callagent_port",
800          * as, from the command line, we have no way of guessing which
801          * the user had in mind.
802          */
803         mgcp_tcp_port_count = -1;
804         mgcp_udp_port_count = -1;
805
806         colonp = strchr(prefarg, ':');
807         if (colonp == NULL)
808                 return PREFS_SET_SYNTAX_ERR;
809
810         p = colonp;
811         *p++ = '\0';
812
813         /*
814          * Skip over any white space (there probably won't be any, but
815          * as we allow it in the preferences file, we might as well
816          * allow it here).
817          */
818         while (isspace(*p))
819                 p++;
820         if (*p == '\0') {
821                 /*
822                  * Put the colon back, so if our caller uses, in an
823                  * error message, the string they passed us, the message
824                  * looks correct.
825                  */
826                 *colonp = ':';
827                 return PREFS_SET_SYNTAX_ERR;
828         }
829
830         ret = set_pref(prefarg, p);
831         *colonp = ':';  /* put the colon back */
832         return ret;
833 }
834
835 #define PRS_PRINT_FMT    "print.format"
836 #define PRS_PRINT_DEST   "print.destination"
837 #define PRS_PRINT_FILE   "print.file"
838 #define PRS_PRINT_CMD    "print.command"
839 #define PRS_COL_FMT      "column.format"
840 #define PRS_STREAM_CL_FG "stream.client.fg"
841 #define PRS_STREAM_CL_BG "stream.client.bg"
842 #define PRS_STREAM_SR_FG "stream.server.fg"
843 #define PRS_STREAM_SR_BG "stream.server.bg"
844 #define PRS_GUI_SCROLLBAR_ON_RIGHT "gui.scrollbar_on_right"
845 #define PRS_GUI_PLIST_SEL_BROWSE "gui.packet_list_sel_browse"
846 #define PRS_GUI_PTREE_SEL_BROWSE "gui.protocol_tree_sel_browse"
847 #define PRS_GUI_PTREE_LINE_STYLE "gui.protocol_tree_line_style"
848 #define PRS_GUI_PTREE_EXPANDER_STYLE "gui.protocol_tree_expander_style"
849 #define PRS_GUI_HEX_DUMP_HIGHLIGHT_STYLE "gui.hex_dump_highlight_style"
850 #define PRS_GUI_FONT_NAME "gui.font_name"
851 #define PRS_GUI_MARKED_FG "gui.marked_frame.fg"
852 #define PRS_GUI_MARKED_BG "gui.marked_frame.bg"
853
854 /*
855  * This applies to more than just captures, so it's not "capture.name_resolve";
856  * "capture.name_resolve" is supported on input for backwards compatibility.
857  *
858  * It's not a preference for a particular part of Ethereal, it's used all
859  * over the place, so its name doesn't have two components.
860  */
861 #define PRS_NAME_RESOLVE "name_resolve"
862 #define PRS_CAP_NAME_RESOLVE "capture.name_resolve"
863
864 /*  values for the capture dialog box */
865 #define PRS_CAP_REAL_TIME "capture.real_time_update"
866 #define PRS_CAP_PROM_MODE "capture.prom_mode"
867 #define PRS_CAP_AUTO_SCROLL "capture.auto_scroll"
868
869 #define RED_COMPONENT(x)   ((((x) >> 16) & 0xff) * 65535 / 255)
870 #define GREEN_COMPONENT(x) ((((x) >>  8) & 0xff) * 65535 / 255)
871 #define BLUE_COMPONENT(x)   (((x)        & 0xff) * 65535 / 255)
872
873 static gchar *pr_formats[] = { "text", "postscript" };
874 static gchar *pr_dests[]   = { "command", "file" };
875
876 static int
877 set_pref(gchar *pref_name, gchar *value)
878 {
879   GList    *col_l;
880   gint      llen;
881   fmt_data *cfmt;
882   unsigned long int cval;
883   guint    uval;
884   gboolean bval;
885   gint     enum_val;
886   char     *p;
887   gchar    *dotp;
888   module_t *module;
889   pref_t   *pref;
890
891   if (strcmp(pref_name, PRS_PRINT_FMT) == 0) {
892     if (strcmp(value, pr_formats[PR_FMT_TEXT]) == 0) {
893       prefs.pr_format = PR_FMT_TEXT;
894     } else if (strcmp(value, pr_formats[PR_FMT_PS]) == 0) {
895       prefs.pr_format = PR_FMT_PS;
896     } else {
897       return PREFS_SET_SYNTAX_ERR;
898     }
899   } else if (strcmp(pref_name, PRS_PRINT_DEST) == 0) {
900     if (strcmp(value, pr_dests[PR_DEST_CMD]) == 0) {
901       prefs.pr_dest = PR_DEST_CMD;
902     } else if (strcmp(value, pr_dests[PR_DEST_FILE]) == 0) {
903       prefs.pr_dest = PR_DEST_FILE;
904     } else {
905       return PREFS_SET_SYNTAX_ERR;
906     }
907   } else if (strcmp(pref_name, PRS_PRINT_FILE) == 0) {
908     if (prefs.pr_file) g_free(prefs.pr_file);
909     prefs.pr_file = g_strdup(value);
910   } else if (strcmp(pref_name, PRS_PRINT_CMD) == 0) {
911     if (prefs.pr_cmd) g_free(prefs.pr_cmd);
912     prefs.pr_cmd = g_strdup(value);
913   } else if (strcmp(pref_name, PRS_COL_FMT) == 0) {
914     if ((col_l = get_string_list(value)) && (g_list_length(col_l) % 2) == 0) {
915       free_col_info(&prefs);
916       prefs.col_list = NULL;
917       llen             = g_list_length(col_l);
918       prefs.num_cols   = llen / 2;
919       col_l = g_list_first(col_l);
920       while(col_l) {
921         cfmt = (fmt_data *) g_malloc(sizeof(fmt_data));
922         cfmt->title    = g_strdup(col_l->data);
923         col_l          = col_l->next;
924         cfmt->fmt      = g_strdup(col_l->data);
925         col_l          = col_l->next;
926         prefs.col_list = g_list_append(prefs.col_list, cfmt);
927       }
928       /* To do: else print some sort of error? */
929     }
930     clear_string_list(col_l);
931   } else if (strcmp(pref_name, PRS_STREAM_CL_FG) == 0) {
932     cval = strtoul(value, NULL, 16);
933     prefs.st_client_fg.pixel = 0;
934     prefs.st_client_fg.red   = RED_COMPONENT(cval);
935     prefs.st_client_fg.green = GREEN_COMPONENT(cval);
936     prefs.st_client_fg.blue  = BLUE_COMPONENT(cval);
937   } else if (strcmp(pref_name, PRS_STREAM_CL_BG) == 0) {
938     cval = strtoul(value, NULL, 16);
939     prefs.st_client_bg.pixel = 0;
940     prefs.st_client_bg.red   = RED_COMPONENT(cval);
941     prefs.st_client_bg.green = GREEN_COMPONENT(cval);
942     prefs.st_client_bg.blue  = BLUE_COMPONENT(cval);
943   } else if (strcmp(pref_name, PRS_STREAM_SR_FG) == 0) {
944     cval = strtoul(value, NULL, 16);
945     prefs.st_server_fg.pixel = 0;
946     prefs.st_server_fg.red   = RED_COMPONENT(cval);
947     prefs.st_server_fg.green = GREEN_COMPONENT(cval);
948     prefs.st_server_fg.blue  = BLUE_COMPONENT(cval);
949   } else if (strcmp(pref_name, PRS_STREAM_SR_BG) == 0) {
950     cval = strtoul(value, NULL, 16);
951     prefs.st_server_bg.pixel = 0;
952     prefs.st_server_bg.red   = RED_COMPONENT(cval);
953     prefs.st_server_bg.green = GREEN_COMPONENT(cval);
954     prefs.st_server_bg.blue  = BLUE_COMPONENT(cval);
955   } else if (strcmp(pref_name, PRS_GUI_SCROLLBAR_ON_RIGHT) == 0) {
956     if (strcmp(value, "TRUE") == 0) {
957             prefs.gui_scrollbar_on_right = TRUE;
958     }
959     else {
960             prefs.gui_scrollbar_on_right = FALSE;
961     }
962   } else if (strcmp(pref_name, PRS_GUI_PLIST_SEL_BROWSE) == 0) {
963     if (strcmp(value, "TRUE") == 0) {
964             prefs.gui_plist_sel_browse = TRUE;
965     }
966     else {
967             prefs.gui_plist_sel_browse = FALSE;
968     }
969   } else if (strcmp(pref_name, PRS_GUI_PTREE_SEL_BROWSE) == 0) {
970     if (strcmp(value, "TRUE") == 0) {
971             prefs.gui_ptree_sel_browse = TRUE;
972     }
973     else {
974             prefs.gui_ptree_sel_browse = FALSE;
975     }
976   } else if (strcmp(pref_name, PRS_GUI_PTREE_LINE_STYLE) == 0) {
977           prefs.gui_ptree_line_style =
978                   find_index_from_string_array(value, gui_ptree_line_style_text, 0);
979   } else if (strcmp(pref_name, PRS_GUI_PTREE_EXPANDER_STYLE) == 0) {
980           prefs.gui_ptree_expander_style =
981                   find_index_from_string_array(value, gui_ptree_expander_style_text, 1);
982   } else if (strcmp(pref_name, PRS_GUI_HEX_DUMP_HIGHLIGHT_STYLE) == 0) {
983           prefs.gui_hex_dump_highlight_style =
984                   find_index_from_string_array(value, gui_hex_dump_highlight_style_text, 1);
985   } else if (strcmp(pref_name, PRS_GUI_FONT_NAME) == 0) {
986           if (prefs.gui_font_name != NULL)
987                 g_free(prefs.gui_font_name);
988           prefs.gui_font_name = g_strdup(value);
989   } else if (strcmp(pref_name, PRS_GUI_MARKED_FG) == 0) {
990     cval = strtoul(value, NULL, 16);
991     prefs.gui_marked_fg.pixel = 0;
992     prefs.gui_marked_fg.red   = RED_COMPONENT(cval);
993     prefs.gui_marked_fg.green = GREEN_COMPONENT(cval);
994     prefs.gui_marked_fg.blue  = BLUE_COMPONENT(cval);
995   } else if (strcmp(pref_name, PRS_GUI_MARKED_BG) == 0) {
996     cval = strtoul(value, NULL, 16);
997     prefs.gui_marked_bg.pixel = 0;
998     prefs.gui_marked_bg.red   = RED_COMPONENT(cval);
999     prefs.gui_marked_bg.green = GREEN_COMPONENT(cval);
1000     prefs.gui_marked_bg.blue  = BLUE_COMPONENT(cval);
1001
1002 /* handle the capture options */ 
1003   } else if (strcmp(pref_name, PRS_CAP_PROM_MODE) == 0) {
1004     prefs.capture_prom_mode = ((strcmp(value, "TRUE") == 0)?TRUE:FALSE); 
1005  
1006   } else if (strcmp(pref_name, PRS_CAP_REAL_TIME) == 0) {
1007     prefs.capture_real_time = ((strcmp(value, "TRUE") == 0)?TRUE:FALSE); 
1008
1009   } else if (strcmp(pref_name, PRS_CAP_AUTO_SCROLL) == 0) {
1010     prefs.capture_auto_scroll = ((strcmp(value, "TRUE") == 0)?TRUE:FALSE); 
1011  
1012   } else if (strcmp(pref_name, PRS_NAME_RESOLVE) == 0 ||
1013              strcmp(pref_name, PRS_CAP_NAME_RESOLVE) == 0) {
1014     prefs.name_resolve = ((strcmp(value, "TRUE") == 0)?TRUE:FALSE); 
1015
1016   } else {
1017     /* To which module does this preference belong? */
1018     dotp = strchr(pref_name, '.');
1019     if (dotp == NULL)
1020       return PREFS_SET_SYNTAX_ERR;      /* no ".", so no module/name separator */
1021     *dotp = '\0';               /* separate module and preference name */
1022     module = find_module(pref_name);
1023
1024     /*
1025      * XXX - "Diameter" rather than "diameter" was used in earlier
1026      * versions of Ethereal; if we didn't find the module, and its name
1027      * was "Diameter", look for "diameter" instead.
1028      */
1029     if (module == NULL && strcmp(pref_name, "Diameter") == 0)
1030       module = find_module("diameter");
1031     *dotp = '.';                /* put the preference string back */
1032     if (module == NULL)
1033       return PREFS_SET_NO_SUCH_PREF;    /* no such module */
1034     dotp++;                     /* skip past separator to preference name */
1035     pref = find_preference(module, dotp);
1036
1037     /*
1038      * XXX - "mgcp.display raw text toggle" and "mgcp.display dissect tree"
1039      * rather than "mgcp.display_raw_text" and "mgcp.display_dissect_tree"
1040      * were used in earlier versions of Ethereal; if we didn't find the
1041      * preference, it was an MGCP preference, and its name was
1042      * "display raw text toggle" or "display dissect tree", look for
1043      * "display_raw_text" or "display_dissect_tree" instead.
1044      *
1045      * "mgcp.tcp.port" and "mgcp.udp.port" are harder to handle, as both
1046      * the gateway and callagent ports were given those names; we interpret
1047      * the first as "mgcp.{tcp,udp}.gateway_port" and the second as
1048      * "mgcp.{tcp,udp}.callagent_port", as that's the order in which
1049      * they were registered by the MCCP dissector and thus that's the
1050      * order in which they were written to the preferences file.  (If
1051      * we're not reading the preferences file, but are handling stuff
1052      * from a "-o" command-line option, we have no clue which the user
1053      * had in mind - they should have used "mgcp.{tcp,udp}.gateway_port"
1054      * or "mgcp.{tcp,udp}.callagent_port" instead.)
1055      */
1056     if (pref == NULL && strncmp(pref_name, "mgcp.", 5) == 0) {
1057       if (strcmp(dotp, "display raw text toggle") == 0)
1058         pref = find_preference(module, "display_raw_text");
1059       else if (strcmp(dotp, "display dissect tree") == 0)
1060         pref = find_preference(module, "display_dissect_tree");
1061       else if (strcmp(dotp, "tcp.port") == 0) {
1062         mgcp_tcp_port_count++;
1063         if (mgcp_tcp_port_count == 1) {
1064           /* It's the first one */
1065           pref = find_preference(module, "tcp.gateway_port");
1066         } else if (mgcp_tcp_port_count == 2) {
1067           /* It's the second one */
1068           pref = find_preference(module, "tcp.callagent_port");
1069         }
1070         /* Otherwise it's from the command line, and we don't bother
1071            mapping it. */
1072       } else if (strcmp(dotp, "udp.port") == 0) {
1073         mgcp_udp_port_count++;
1074         if (mgcp_udp_port_count == 1) {
1075           /* It's the first one */
1076           pref = find_preference(module, "udp.gateway_port");
1077         } else if (mgcp_udp_port_count == 2) {
1078           /* It's the second one */
1079           pref = find_preference(module, "udp.callagent_port");
1080         }
1081         /* Otherwise it's from the command line, and we don't bother
1082            mapping it. */
1083       }
1084     }
1085     if (pref == NULL)
1086       return PREFS_SET_NO_SUCH_PREF;    /* no such preference */
1087
1088     switch (pref->type) {
1089
1090     case PREF_UINT:
1091       uval = strtoul(value, &p, pref->info.base);
1092       if (p == value || *p != '\0')
1093         return PREFS_SET_SYNTAX_ERR;    /* number was bad */
1094       if (*pref->varp.uint != uval) {
1095         module->prefs_changed = TRUE;
1096         *pref->varp.uint = uval;
1097       }
1098       break;
1099
1100     case PREF_BOOL:
1101       /* XXX - give an error if it's neither "true" nor "false"? */
1102       if (strcasecmp(value, "true") == 0)
1103         bval = TRUE;
1104       else
1105         bval = FALSE;
1106       if (*pref->varp.bool != bval) {
1107         module->prefs_changed = TRUE;
1108         *pref->varp.bool = bval;
1109       }
1110       break;
1111
1112     case PREF_ENUM:
1113       /* XXX - give an error if it doesn't match? */
1114       enum_val = find_val_for_string(value,
1115                                         pref->info.enum_info.enumvals, 1);
1116       if (*pref->varp.enump != enum_val) {
1117         module->prefs_changed = TRUE;
1118         *pref->varp.enump = enum_val;
1119       }
1120       break;
1121
1122     case PREF_STRING:
1123       if (*pref->varp.string == NULL || strcmp(*pref->varp.string, value) != 0) {
1124         module->prefs_changed = TRUE;
1125         if (*pref->varp.string != NULL)
1126           g_free(*pref->varp.string);
1127         *pref->varp.string = g_strdup(value);
1128       }
1129       break;
1130     }
1131   }
1132   
1133   return PREFS_SET_OK;
1134 }
1135
1136 typedef struct {
1137         module_t *module;
1138         FILE    *pf;
1139 } write_pref_arg_t;
1140
1141 /*
1142  * Write out a single preference.
1143  */
1144 static void
1145 write_pref(gpointer data, gpointer user_data)
1146 {
1147         pref_t *pref = data;
1148         write_pref_arg_t *arg = user_data;
1149         const enum_val_t *enum_valp;
1150         const char *val_string;
1151
1152         fprintf(arg->pf, "\n# %s\n", pref->description);
1153
1154         switch (pref->type) {
1155
1156         case PREF_UINT:
1157                 switch (pref->info.base) {
1158
1159                 case 10:
1160                         fprintf(arg->pf, "# A decimal number.\n");
1161                         fprintf(arg->pf, "%s.%s: %u\n", arg->module->name,
1162                             pref->name, *pref->varp.uint);
1163                         break;
1164
1165                 case 8:
1166                         fprintf(arg->pf, "# An octal number.\n");
1167                         fprintf(arg->pf, "%s.%s: %#o\n", arg->module->name,
1168                             pref->name, *pref->varp.uint);
1169                         break;
1170
1171                 case 16:
1172                         fprintf(arg->pf, "# A hexadecimal number.\n");
1173                         fprintf(arg->pf, "%s.%s: %#x\n", arg->module->name,
1174                             pref->name, *pref->varp.uint);
1175                         break;
1176                 }
1177                 break;
1178
1179         case PREF_BOOL:
1180                 fprintf(arg->pf, "# TRUE or FALSE (case-insensitive).\n");
1181                 fprintf(arg->pf, "%s.%s: %s\n", arg->module->name, pref->name,
1182                     *pref->varp.bool ? "TRUE" : "FALSE");
1183                 break;
1184
1185         case PREF_ENUM:
1186                 fprintf(arg->pf, "# One of: ");
1187                 enum_valp = pref->info.enum_info.enumvals;
1188                 val_string = NULL;
1189                 while (enum_valp->name != NULL) {
1190                         if (enum_valp->value == *pref->varp.enump)
1191                                 val_string = enum_valp->name;
1192                         fprintf(arg->pf, "%s", enum_valp->name);
1193                         enum_valp++;
1194                         if (enum_valp->name == NULL)
1195                                 fprintf(arg->pf, "\n");
1196                         else
1197                                 fprintf(arg->pf, ", ");
1198                 }
1199                 fprintf(arg->pf, "# (case-insensitive).\n");
1200                 fprintf(arg->pf, "%s.%s: %s\n", arg->module->name, pref->name,
1201                     val_string);
1202                 break;
1203
1204         case PREF_STRING:
1205                 fprintf(arg->pf, "# A string.\n");
1206                 fprintf(arg->pf, "%s.%s: %s\n", arg->module->name, pref->name,
1207                     *pref->varp.string);
1208                 break;
1209         }
1210 }
1211
1212 static void
1213 write_module_prefs(gpointer data, gpointer user_data)
1214 {
1215         write_pref_arg_t arg;
1216
1217         arg.module = data;
1218         arg.pf = user_data;
1219         g_list_foreach(arg.module->prefs, write_pref, &arg);
1220 }
1221
1222 /* Write out "prefs" to the user's preferences file, and return 0.
1223
1224    If we got an error, stuff a pointer to the path of the preferences file
1225    into "*pf_path_return", and return the errno. */
1226 int
1227 write_prefs(char **pf_path_return)
1228 {
1229   FILE        *pf;
1230   struct stat  s_buf;
1231   
1232   /* To do:
1233    * - Split output lines longer than MAX_VAL_LEN
1234    * - Create a function for the preference directory check/creation
1235    *   so that duplication can be avoided with filter.c
1236    */
1237
1238   if (! pf_path) {
1239     pf_path = (gchar *) g_malloc(strlen(get_home_dir()) + strlen(PF_DIR) +
1240       strlen(PF_NAME) + 4);
1241   }
1242
1243   sprintf(pf_path, "%s/%s", get_home_dir(), PF_DIR);
1244   if (stat(pf_path, &s_buf) != 0)
1245 #ifdef WIN32
1246     mkdir(pf_path);
1247 #else
1248     mkdir(pf_path, 0755);
1249 #endif
1250
1251   sprintf(pf_path, "%s/%s/%s", get_home_dir(), PF_DIR, PF_NAME);
1252   if ((pf = fopen(pf_path, "w")) == NULL) {
1253     *pf_path_return = pf_path;
1254     return errno;
1255   }
1256     
1257   fputs("# Configuration file for Ethereal " VERSION ".\n"
1258     "#\n"
1259     "# This file is regenerated each time preferences are saved within\n"
1260     "# Ethereal.  Making manual changes should be safe, however.\n"
1261     "\n"
1262     "######## Printing ########\n"
1263     "\n", pf);
1264
1265   fprintf (pf, "# Can be one of \"text\" or \"postscript\".\n"
1266     "print.format: %s\n\n", pr_formats[prefs.pr_format]);
1267
1268   fprintf (pf, "# Can be one of \"command\" or \"file\".\n"
1269     "print.destination: %s\n\n", pr_dests[prefs.pr_dest]);
1270
1271   fprintf (pf, "# This is the file that gets written to when the "
1272     "destination is set to \"file\"\n"
1273     "%s: %s\n\n", PRS_PRINT_FILE, prefs.pr_file);
1274
1275   fprintf (pf, "# Output gets piped to this command when the destination "
1276     "is set to \"command\"\n"
1277     "%s: %s\n\n", PRS_PRINT_CMD, prefs.pr_cmd);
1278
1279   fprintf (pf, "# Packet list column format.  Each pair of strings consists "
1280     "of a column title \n# and its format.\n"
1281     "%s: %s\n\n", PRS_COL_FMT, col_format_to_pref_str());
1282
1283   fprintf (pf, "# TCP stream window color preferences.  Each value is a six "
1284     "digit hexadecimal value in the form rrggbb.\n");
1285   fprintf (pf, "%s: %02x%02x%02x\n", PRS_STREAM_CL_FG,
1286     (prefs.st_client_fg.red * 255 / 65535),
1287     (prefs.st_client_fg.green * 255 / 65535),
1288     (prefs.st_client_fg.blue * 255 / 65535));
1289   fprintf (pf, "%s: %02x%02x%02x\n", PRS_STREAM_CL_BG,
1290     (prefs.st_client_bg.red * 255 / 65535),
1291     (prefs.st_client_bg.green * 255 / 65535),
1292     (prefs.st_client_bg.blue * 255 / 65535));
1293   fprintf (pf, "%s: %02x%02x%02x\n", PRS_STREAM_SR_FG,
1294     (prefs.st_server_fg.red * 255 / 65535),
1295     (prefs.st_server_fg.green * 255 / 65535),
1296     (prefs.st_server_fg.blue * 255 / 65535));
1297   fprintf (pf, "%s: %02x%02x%02x\n", PRS_STREAM_SR_BG,
1298     (prefs.st_server_bg.red * 255 / 65535),
1299     (prefs.st_server_bg.green * 255 / 65535),
1300     (prefs.st_server_bg.blue * 255 / 65535));
1301
1302   fprintf(pf, "\n# Vertical scrollbars should be on right side? TRUE/FALSE\n");
1303   fprintf(pf, PRS_GUI_SCROLLBAR_ON_RIGHT ": %s\n",
1304                   prefs.gui_scrollbar_on_right == TRUE ? "TRUE" : "FALSE");
1305
1306   fprintf(pf, "\n# Packet-list selection bar can be used to browse w/o selecting? TRUE/FALSE\n");
1307   fprintf(pf, PRS_GUI_PLIST_SEL_BROWSE ": %s\n",
1308                   prefs.gui_plist_sel_browse == TRUE ? "TRUE" : "FALSE");
1309
1310   fprintf(pf, "\n# Protocol-tree selection bar can be used to browse w/o selecting? TRUE/FALSE\n");
1311   fprintf(pf, PRS_GUI_PTREE_SEL_BROWSE ": %s\n",
1312                   prefs.gui_ptree_sel_browse == TRUE ? "TRUE" : "FALSE");
1313
1314   fprintf(pf, "\n# Protocol-tree line style. One of: NONE, SOLID, DOTTED, TABBED\n");
1315   fprintf(pf, PRS_GUI_PTREE_LINE_STYLE ": %s\n",
1316                   gui_ptree_line_style_text[prefs.gui_ptree_line_style]);
1317
1318   fprintf(pf, "\n# Protocol-tree expander style. One of: NONE, SQUARE, TRIANGLE, CIRCULAR\n");
1319   fprintf(pf, PRS_GUI_PTREE_EXPANDER_STYLE ": %s\n",
1320                   gui_ptree_expander_style_text[prefs.gui_ptree_expander_style]);
1321
1322   fprintf(pf, "\n# Hex dump highlight style. One of: BOLD, INVERSE\n");
1323   fprintf(pf, PRS_GUI_HEX_DUMP_HIGHLIGHT_STYLE ": %s\n",
1324                   gui_hex_dump_highlight_style_text[prefs.gui_hex_dump_highlight_style]);
1325
1326   fprintf(pf, "\n# Font name for packet list, protocol tree, and hex dump panes.\n");
1327   fprintf(pf, PRS_GUI_FONT_NAME ": %s\n", prefs.gui_font_name);
1328
1329   fprintf (pf, "\n# Color preferences for a marked frame.  Each value is a six "
1330     "digit hexadecimal value in the form rrggbb.\n");
1331   fprintf (pf, "%s: %02x%02x%02x\n", PRS_GUI_MARKED_FG,
1332     (prefs.gui_marked_fg.red * 255 / 65535),
1333     (prefs.gui_marked_fg.green * 255 / 65535),
1334     (prefs.gui_marked_fg.blue * 255 / 65535));
1335   fprintf (pf, "%s: %02x%02x%02x\n", PRS_GUI_MARKED_BG,
1336     (prefs.gui_marked_bg.red * 255 / 65535),
1337     (prefs.gui_marked_bg.green * 255 / 65535),
1338     (prefs.gui_marked_bg.blue * 255 / 65535));
1339
1340   fprintf(pf, "\n# Resolve addresses to names? TRUE/FALSE\n");
1341   fprintf(pf, PRS_NAME_RESOLVE ": %s\n",
1342                   prefs.name_resolve == TRUE ? "TRUE" : "FALSE");
1343
1344 /* write the capture options */
1345   fprintf(pf, "\n# Capture in promiscuous mode? TRUE/FALSE\n");
1346   fprintf(pf, PRS_CAP_PROM_MODE ": %s\n",
1347                   prefs.capture_prom_mode == TRUE ? "TRUE" : "FALSE");
1348
1349   fprintf(pf, "\n# Update packet list in real time during capture? TRUE/FALSE\n");
1350   fprintf(pf, PRS_CAP_REAL_TIME ": %s\n",
1351                   prefs.capture_real_time == TRUE ? "TRUE" : "FALSE");
1352
1353   fprintf(pf, "\n# scroll packet list during capture? TRUE/FALSE\n");
1354   fprintf(pf, PRS_CAP_AUTO_SCROLL ": %s\n",
1355                   prefs.capture_auto_scroll == TRUE ? "TRUE" : "FALSE");
1356
1357   g_list_foreach(modules, write_module_prefs, pf);
1358
1359   fclose(pf);
1360
1361   /* XXX - catch I/O errors (e.g. "ran out of disk space") and return
1362      an error indication, or maybe write to a new preferences file and
1363      rename that file on top of the old one only if there are not I/O
1364      errors. */
1365   return 0;
1366 }
1367
1368 /* Copy a set of preferences. */
1369 void
1370 copy_prefs(e_prefs *dest, e_prefs *src)
1371 {
1372   fmt_data *src_cfmt, *dest_cfmt;
1373   GList *entry;
1374
1375   dest->pr_format = src->pr_format;
1376   dest->pr_dest = src->pr_dest;
1377   dest->pr_file = g_strdup(src->pr_file);
1378   dest->pr_cmd = g_strdup(src->pr_cmd);
1379   dest->col_list = NULL;
1380   for (entry = src->col_list; entry != NULL; entry = g_list_next(entry)) {
1381     src_cfmt = entry->data;
1382     dest_cfmt = (fmt_data *) g_malloc(sizeof(fmt_data));
1383     dest_cfmt->title = g_strdup(src_cfmt->title);
1384     dest_cfmt->fmt = g_strdup(src_cfmt->fmt);
1385     dest->col_list = g_list_append(dest->col_list, dest_cfmt);
1386   }
1387   dest->num_cols = src->num_cols;
1388   dest->st_client_fg = src->st_client_fg;
1389   dest->st_client_bg = src->st_client_bg;
1390   dest->st_server_fg = src->st_server_fg;
1391   dest->st_server_bg = src->st_server_bg;
1392   dest->gui_scrollbar_on_right = src->gui_scrollbar_on_right;
1393   dest->gui_plist_sel_browse = src->gui_plist_sel_browse;
1394   dest->gui_ptree_sel_browse = src->gui_ptree_sel_browse;
1395   dest->gui_ptree_line_style = src->gui_ptree_line_style;
1396   dest->gui_ptree_expander_style = src->gui_ptree_expander_style;
1397   dest->gui_hex_dump_highlight_style = src->gui_hex_dump_highlight_style;
1398   dest->gui_font_name = g_strdup(src->gui_font_name);
1399   dest->gui_marked_fg = src->gui_marked_fg;
1400   dest->gui_marked_bg = src->gui_marked_bg;
1401 /*  values for the capture dialog box */
1402   dest->capture_prom_mode = src->capture_prom_mode;
1403   dest->capture_real_time = src->capture_real_time;
1404   dest->capture_auto_scroll = src->capture_auto_scroll;
1405   dest->name_resolve = src->name_resolve;
1406
1407 }
1408
1409 /* Free a set of preferences. */
1410 void
1411 free_prefs(e_prefs *pr)
1412 {
1413   if (pr->pr_file != NULL) {
1414     g_free(pr->pr_file);
1415     pr->pr_file = NULL;
1416   }
1417   if (pr->pr_cmd != NULL) {
1418     g_free(pr->pr_cmd);
1419     pr->pr_cmd = NULL;
1420   }
1421   free_col_info(pr);
1422   if (pr->gui_font_name != NULL) {
1423     g_free(pr->gui_font_name);
1424     pr->gui_font_name = NULL;
1425   }
1426 }
1427
1428 static void
1429 free_col_info(e_prefs *pr)
1430 {
1431   fmt_data *cfmt;
1432
1433   while (pr->col_list != NULL) {
1434     cfmt = pr->col_list->data;
1435     g_free(cfmt->title);
1436     g_free(cfmt->fmt);
1437     g_free(cfmt);
1438     pr->col_list = g_list_remove_link(pr->col_list, pr->col_list);
1439   }
1440   g_list_free(pr->col_list);
1441   pr->col_list = NULL;
1442 }