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