Add the RFC 3203 FORCERENEW message type, as suggested by Suresh K.
[obnox/wireshark/wip.git] / prefs.c
1 /* prefs.c
2  * Routines for handling preferences
3  *
4  * $Id: prefs.c,v 1.95 2003/01/29 21:27:36 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 #include <stdlib.h>
30 #include <string.h>
31 #include <ctype.h>
32 #include <errno.h>
33
34 #ifdef HAVE_UNISTD_H
35 #include <unistd.h>
36 #endif
37
38 #include <glib.h>
39
40 #include <epan/filesystem.h>
41 #include "globals.h"
42 #include <epan/resolv.h>
43 #include <epan/packet.h>
44 #include "file.h"
45 #include "prefs.h"
46 #include <epan/proto.h>
47 #include "column.h"
48 #include "print.h"
49
50 #include "prefs-int.h"
51
52 /* Internal functions */
53 static module_t *find_module(const char *name);
54 static module_t *prefs_register_module_or_subtree(module_t *parent,
55     const char *name, const char *title, gboolean is_subtree,
56     void (*apply_cb)(void));
57 static struct preference *find_preference(module_t *, const char *);
58 static int    set_pref(gchar*, gchar*);
59 static GList *get_string_list(gchar *);
60 static gchar *put_string_list(GList *);
61 static void   clear_string_list(GList *);
62 static void   free_col_info(e_prefs *);
63
64 #define GPF_NAME        "ethereal.conf"
65 #define PF_NAME         "preferences"
66
67 static gboolean init_prefs = TRUE;
68 static gchar *gpf_path = NULL;
69
70 /*
71  * XXX - variables to allow us to attempt to interpret the first
72  * "mgcp.{tcp,udp}.port" in a preferences file as
73  * "mgcp.{tcp,udp}.gateway_port" and the second as
74  * "mgcp.{tcp,udp}.callagent_port".
75  */
76 static int mgcp_tcp_port_count;
77 static int mgcp_udp_port_count;
78
79 e_prefs prefs;
80
81 gchar   *gui_ptree_line_style_text[] =
82         { "NONE", "SOLID", "DOTTED", "TABBED", NULL };
83
84 gchar   *gui_ptree_expander_style_text[] =
85         { "NONE", "SQUARE", "TRIANGLE", "CIRCULAR", NULL };
86
87 gchar   *gui_hex_dump_highlight_style_text[] =
88         { "BOLD", "INVERSE", NULL };
89
90 /*
91  * List of all modules with preference settings.
92  */
93 static GList *modules;
94
95 /*
96  * List of all modules that should show up at the top level of the
97  * tree in the preference dialog box.
98  */
99 static GList *top_level_modules;
100
101 static gint
102 module_compare_name(gconstpointer p1_arg, gconstpointer p2_arg)
103 {
104         const module_t *p1 = p1_arg;
105         const module_t *p2 = p2_arg;
106
107         return g_strcasecmp(p1->name, p2->name);
108 }
109
110 static gint
111 module_compare_title(gconstpointer p1_arg, gconstpointer p2_arg)
112 {
113         const module_t *p1 = p1_arg;
114         const module_t *p2 = p2_arg;
115
116         return g_strcasecmp(p1->title, p2->title);
117 }
118
119 /*
120  * Register a module that will have preferences.
121  * Specify the module under which to register it or NULL to register it
122  * at the top level, the name used for the module in the preferences file,
123  * the title used in the tab for it in a preferences dialog box, and a
124  * routine to call back when we apply the preferences.
125  */
126 module_t *
127 prefs_register_module(module_t *parent, const char *name, const char *title,
128     void (*apply_cb)(void))
129 {
130         return prefs_register_module_or_subtree(parent, name, title, FALSE,
131             apply_cb);
132 }
133
134 /*
135  * Register a subtree that will have modules under it.
136  * Specify the module under which to register it or NULL to register it
137  * at the top level and the title used in the tab for it in a preferences
138  * dialog box.
139  */
140 module_t *
141 prefs_register_subtree(module_t *parent, const char *title)
142 {
143         return prefs_register_module_or_subtree(parent, NULL, title, TRUE,
144             NULL);
145 }
146
147 static module_t *
148 prefs_register_module_or_subtree(module_t *parent, const char *name,
149     const char *title, gboolean is_subtree, void (*apply_cb)(void))
150 {
151         module_t *module;
152         const guchar *p;
153
154         module = g_malloc(sizeof (module_t));
155         module->name = name;
156         module->title = title;
157         module->is_subtree = is_subtree;
158         module->apply_cb = apply_cb;
159         module->prefs = NULL;   /* no preferences, to start */
160         module->numprefs = 0;
161         module->prefs_changed = FALSE;
162         module->obsolete = FALSE;
163
164         /*
165          * Do we have a module name?
166          */
167         if (name != NULL) {
168                 /*
169                  * Yes.
170                  * Make sure that only lower-case ASCII letters, numbers,
171                  * underscores, and dots appear in the name.
172                  *
173                  * Crash if there is, as that's an error in the code;
174                  * you can make the title a nice string with capitalization,
175                  * white space, punctuation, etc., but the name can be used
176                  * on the command line, and shouldn't require quoting,
177                  * shifting, etc.
178                  */
179                 for (p = name; *p != '\0'; p++)
180                         g_assert(isascii(*p) &&
181                             (islower(*p) || isdigit(*p) || *p == '_' ||
182                              *p == '.'));
183
184                 /*
185                  * Make sure there's not already a module with that
186                  * name.  Crash if there is, as that's an error in the
187                  * code, and the code has to be fixed not to register
188                  * more than one module with the same name.
189                  *
190                  * We search the list of all modules; the subtree stuff
191                  * doesn't require preferences in subtrees to have names
192                  * that reflect the subtree they're in (that would require
193                  * protocol preferences to have a bogus "protocol.", or
194                  * something such as that, to be added to all their names).
195                  */
196                 g_assert(find_module(name) == NULL);
197
198                 /*
199                  * Insert this module in the list of all modules.
200                  */
201                 modules = g_list_insert_sorted(modules, module,
202                     module_compare_name);
203         } else {
204                 /*
205                  * This has no name, just a title; check to make sure it's a
206                  * subtree, and crash if it's not.
207                  */
208                 g_assert(is_subtree);
209         }
210
211         /*
212          * Insert this module into the appropriate place in the display
213          * tree.
214          */
215         if (parent == NULL) {
216                 /*
217                  * It goes at the top.
218                  */
219                 top_level_modules = g_list_insert_sorted(top_level_modules,
220                     module, module_compare_title);
221         } else {
222                 /*
223                  * It goes into the list for this module.
224                  */
225                 parent->prefs = g_list_insert_sorted(parent->prefs, module,
226                     module_compare_title);
227         }
228
229         return module;
230 }
231
232 /*
233  * Register that a protocol has preferences.
234  */
235 module_t *protocols_module;
236
237 module_t *
238 prefs_register_protocol(int id, void (*apply_cb)(void))
239 {
240         /*
241          * Have we yet created the "Protocols" subtree?
242          */
243         if (protocols_module == NULL) {
244                 /*
245                  * No.  Do so.
246                  */
247                 protocols_module = prefs_register_subtree(NULL, "Protocols");
248         }
249         return prefs_register_module(protocols_module,
250             proto_get_protocol_filter_name(id),
251             proto_get_protocol_short_name(id), apply_cb);
252 }
253
254 /*
255  * Register that a protocol used to have preferences but no longer does,
256  * by creating an "obsolete" module for it.
257  */
258 module_t *
259 prefs_register_protocol_obsolete(int id)
260 {
261         module_t *module;
262
263         /*
264          * Have we yet created the "Protocols" subtree?
265          */
266         if (protocols_module == NULL) {
267                 /*
268                  * No.  Do so.
269                  */
270                 protocols_module = prefs_register_subtree(NULL, "Protocols");
271         }
272         module = prefs_register_module(protocols_module,
273             proto_get_protocol_filter_name(id),
274             proto_get_protocol_short_name(id), NULL);
275         module->obsolete = TRUE;
276         return module;
277 }
278
279 /*
280  * Find a module, given its name.
281  */
282 static gint
283 module_match(gconstpointer a, gconstpointer b)
284 {
285         const module_t *module = a;
286         const char *name = b;
287
288         return strcmp(name, module->name);
289 }
290
291 static module_t *
292 find_module(const char *name)
293 {
294         GList *list_entry;
295
296         list_entry = g_list_find_custom(modules, (gpointer)name, module_match);
297         if (list_entry == NULL)
298                 return NULL;    /* no such module */
299         return (module_t *) list_entry->data;
300 }
301
302 typedef struct {
303         module_cb callback;
304         gpointer user_data;
305 } module_cb_arg_t;
306
307 static void
308 do_module_callback(gpointer data, gpointer user_data)
309 {
310         module_t *module = data;
311         module_cb_arg_t *arg = user_data;
312
313         if (!module->obsolete)
314                 (*arg->callback)(module, arg->user_data);
315 }
316
317 /*
318  * Call a callback function, with a specified argument, for each module
319  * in a list of modules.  If the list is NULL, searches the top-level
320  * list in the display tree of modules.
321  *
322  * Ignores "obsolete" modules; their sole purpose is to allow old
323  * preferences for dissectors that no longer have preferences to be
324  * silently ignored in preference files.  Does not ignore subtrees,
325  * as this can be used when walking the display tree of modules.
326  */
327 void
328 prefs_module_list_foreach(GList *module_list, module_cb callback,
329     gpointer user_data)
330 {
331         module_cb_arg_t arg;
332
333         if (module_list == NULL)
334                 module_list = top_level_modules;
335
336         arg.callback = callback;
337         arg.user_data = user_data;
338         g_list_foreach(module_list, do_module_callback, &arg);
339 }
340
341 /*
342  * Call a callback function, with a specified argument, for each module
343  * in the list of all modules.  (This list does not include subtrees.)
344  *
345  * Ignores "obsolete" modules; their sole purpose is to allow old
346  * preferences for dissectors that no longer have preferences to be
347  * silently ignored in preference files.
348  */
349 void
350 prefs_modules_foreach(module_cb callback, gpointer user_data)
351 {
352         prefs_module_list_foreach(modules, callback, user_data);
353 }
354
355 static void
356 call_apply_cb(gpointer data, gpointer user_data _U_)
357 {
358         module_t *module = data;
359
360         if (module->obsolete)
361                 return;
362         if (module->prefs_changed) {
363                 if (module->apply_cb != NULL)
364                         (*module->apply_cb)();
365                 module->prefs_changed = FALSE;
366         }
367 }
368
369 /*
370  * Call the "apply" callback function for each module if any of its
371  * preferences have changed, and then clear the flag saying its
372  * preferences have changed, as the module has been notified of that
373  * fact.
374  */
375 void
376 prefs_apply_all(void)
377 {
378         g_list_foreach(modules, call_apply_cb, NULL);
379 }
380
381 /*
382  * Register a preference in a module's list of preferences.
383  * If it has a title, give it an ordinal number; otherwise, it's a
384  * preference that won't show up in the UI, so it shouldn't get an
385  * ordinal number (the ordinal should be the ordinal in the set of
386  * *visible* preferences).
387  */
388 static pref_t *
389 register_preference(module_t *module, const char *name, const char *title,
390     const char *description, pref_type_t type)
391 {
392         pref_t *preference;
393         const guchar *p;
394
395         preference = g_malloc(sizeof (pref_t));
396         preference->name = name;
397         preference->title = title;
398         preference->description = description;
399         preference->type = type;
400         if (title != NULL)
401                 preference->ordinal = module->numprefs;
402         else
403                 preference->ordinal = -1;       /* no ordinal for you */
404
405         /*
406          * Make sure that only lower-case ASCII letters, numbers,
407          * underscores, and dots appear in the preference name.
408          *
409          * Crash if there is, as that's an error in the code;
410          * you can make the title and description nice strings
411          * with capitalization, white space, punctuation, etc.,
412          * but the name can be used on the command line,
413          * and shouldn't require quoting, shifting, etc.
414          */
415         for (p = name; *p != '\0'; p++)
416                 g_assert(isascii(*p) &&
417                     (islower(*p) || isdigit(*p) || *p == '_' || *p == '.'));
418
419         /*
420          * Make sure there's not already a preference with that
421          * name.  Crash if there is, as that's an error in the
422          * code, and the code has to be fixed not to register
423          * more than one preference with the same name.
424          */
425         g_assert(find_preference(module, name) == NULL);
426
427         if (type != PREF_OBSOLETE) {
428                 /*
429                  * Make sure the preference name doesn't begin with the
430                  * module name, as that's redundant and Just Silly.
431                  */
432                 g_assert(strncmp(name, module->name, strlen(module->name)) != 0);
433         }
434
435         /*
436          * There isn't already one with that name, so add the
437          * preference.
438          */
439         module->prefs = g_list_append(module->prefs, preference);
440         if (title != NULL)
441                 module->numprefs++;
442
443         return preference;
444 }
445
446 /*
447  * Find a preference in a module's list of preferences, given the module
448  * and the preference's name.
449  */
450 static gint
451 preference_match(gconstpointer a, gconstpointer b)
452 {
453         const pref_t *pref = a;
454         const char *name = b;
455
456         return strcmp(name, pref->name);
457 }
458
459 static struct preference *
460 find_preference(module_t *module, const char *name)
461 {
462         GList *list_entry;
463
464         list_entry = g_list_find_custom(module->prefs, (gpointer)name,
465             preference_match);
466         if (list_entry == NULL)
467                 return NULL;    /* no such preference */
468         return (struct preference *) list_entry->data;
469 }
470
471 /*
472  * Returns TRUE if the given protocol has registered preferences
473  */
474 gboolean
475 prefs_is_registered_protocol(char *name)
476 {
477         module_t *m = find_module(name);
478
479         return (m != NULL && !m->obsolete);
480 }
481
482 /*
483  * Returns the module title of a registered protocol
484  */
485 const char *
486 prefs_get_title_by_name(char *name)
487 {
488         module_t *m = find_module(name);
489
490         return (m != NULL && !m->obsolete) ? m->title : NULL;
491 }
492
493 /*
494  * Register a preference with an unsigned integral value.
495  */
496 void
497 prefs_register_uint_preference(module_t *module, const char *name,
498     const char *title, const char *description, guint base, guint *var)
499 {
500         pref_t *preference;
501
502         preference = register_preference(module, name, title, description,
503             PREF_UINT);
504         preference->varp.uint = var;
505         preference->info.base = base;
506 }
507
508 /*
509  * Register a preference with an Boolean value.
510  */
511 void
512 prefs_register_bool_preference(module_t *module, const char *name,
513     const char *title, const char *description, gboolean *var)
514 {
515         pref_t *preference;
516
517         preference = register_preference(module, name, title, description,
518             PREF_BOOL);
519         preference->varp.boolp = var;
520 }
521
522 /*
523  * Register a preference with an enumerated value.
524  */
525 void
526 prefs_register_enum_preference(module_t *module, const char *name,
527     const char *title, const char *description, gint *var,
528     const enum_val_t *enumvals, gboolean radio_buttons)
529 {
530         pref_t *preference;
531
532         preference = register_preference(module, name, title, description,
533             PREF_ENUM);
534         preference->varp.enump = var;
535         preference->info.enum_info.enumvals = enumvals;
536         preference->info.enum_info.radio_buttons = radio_buttons;
537 }
538
539 /*
540  * Register a preference with a character-string value.
541  */
542 void
543 prefs_register_string_preference(module_t *module, const char *name,
544     const char *title, const char *description, char **var)
545 {
546         pref_t *preference;
547
548         preference = register_preference(module, name, title, description,
549             PREF_STRING);
550         preference->varp.string = var;
551         preference->saved_val.string = NULL;
552 }
553
554 /*
555  * Register a preference that used to be supported but no longer is.
556  */
557 void
558 prefs_register_obsolete_preference(module_t *module, const char *name)
559 {
560         register_preference(module, name, NULL, NULL, PREF_OBSOLETE);
561 }
562
563 typedef struct {
564         pref_cb callback;
565         gpointer user_data;
566 } pref_cb_arg_t;
567
568 static void
569 do_pref_callback(gpointer data, gpointer user_data)
570 {
571         pref_t *pref = data;
572         pref_cb_arg_t *arg = user_data;
573
574         if (pref->type == PREF_OBSOLETE) {
575                 /*
576                  * This preference is no longer supported; it's not a
577                  * real preference, so we don't call the callback for
578                  * it (i.e., we treat it as if it weren't found in the
579                  * list of preferences, and we weren't called in the
580                  * first place).
581                  */
582                 return;
583         }
584
585         (*arg->callback)(pref, arg->user_data);
586 }
587
588 /*
589  * Call a callback function, with a specified argument, for each preference
590  * in a given module.
591  */
592 void
593 prefs_pref_foreach(module_t *module, pref_cb callback, gpointer user_data)
594 {
595         pref_cb_arg_t arg;
596
597         arg.callback = callback;
598         arg.user_data = user_data;
599         g_list_foreach(module->prefs, do_pref_callback, &arg);
600 }
601
602 /*
603  * Register all non-dissector modules' preferences.
604  */
605 void
606 prefs_register_modules(void)
607 {
608 }
609
610 /* Parse through a list of comma-separated, possibly quoted strings.
611    Return a list of the string data. */
612 static GList *
613 get_string_list(gchar *str)
614 {
615   enum { PRE_STRING, IN_QUOT, NOT_IN_QUOT };
616
617   gint      state = PRE_STRING, i = 0, j = 0;
618   gboolean  backslash = FALSE;
619   guchar    cur_c;
620   gchar    *slstr = NULL;
621   GList    *sl = NULL;
622
623   /* Allocate a buffer for the first string.   */
624   slstr = (gchar *) g_malloc(sizeof(gchar) * COL_MAX_LEN);
625   j = 0;
626
627   for (;;) {
628     cur_c = str[i];
629     if (cur_c == '\0') {
630       /* It's the end of the input, so it's the end of the string we
631          were working on, and there's no more input. */
632       if (state == IN_QUOT || backslash) {
633         /* We were in the middle of a quoted string or backslash escape,
634            and ran out of characters; that's an error.  */
635         g_free(slstr);
636         clear_string_list(sl);
637         return NULL;
638       }
639       slstr[j] = '\0';
640       sl = g_list_append(sl, slstr);
641       break;
642     }
643     if (cur_c == '"' && ! backslash) {
644       switch (state) {
645         case PRE_STRING:
646           /* We hadn't yet started processing a string; this starts the
647              string, and we're now quoting.  */
648           state = IN_QUOT;
649           break;
650         case IN_QUOT:
651           /* We're in the middle of a quoted string, and we saw a quotation
652              mark; we're no longer quoting.   */
653           state = NOT_IN_QUOT;
654           break;
655         case NOT_IN_QUOT:
656           /* We're working on a string, but haven't seen a quote; we're
657              now quoting.  */
658           state = IN_QUOT;
659           break;
660         default:
661           break;
662       }
663     } else if (cur_c == '\\' && ! backslash) {
664       /* We saw a backslash, and the previous character wasn't a
665          backslash; escape the next character.
666
667          This also means we've started a new string. */
668       backslash = TRUE;
669       if (state == PRE_STRING)
670         state = NOT_IN_QUOT;
671     } else if (cur_c == ',' && state != IN_QUOT && ! backslash) {
672       /* We saw a comma, and we're not in the middle of a quoted string
673          and it wasn't preceded by a backslash; it's the end of
674          the string we were working on...  */
675       slstr[j] = '\0';
676       sl = g_list_append(sl, slstr);
677
678       /* ...and the beginning of a new string.  */
679       state = PRE_STRING;
680       slstr = (gchar *) g_malloc(sizeof(gchar) * COL_MAX_LEN);
681       j = 0;
682     } else if (!isspace(cur_c) || state != PRE_STRING) {
683       /* Either this isn't a white-space character, or we've started a
684          string (i.e., already seen a non-white-space character for that
685          string and put it into the string).
686
687          The character is to be put into the string; do so if there's
688          room.  */
689       if (j < COL_MAX_LEN) {
690         slstr[j] = cur_c;
691         j++;
692       }
693
694       /* If it was backslash-escaped, we're done with the backslash escape.  */
695       backslash = FALSE;
696     }
697     i++;
698   }
699   return(sl);
700 }
701
702 #define MAX_FMT_PREF_LEN      1024
703 #define MAX_FMT_PREF_LINE_LEN   60
704 static gchar *
705 put_string_list(GList *sl)
706 {
707   static gchar  pref_str[MAX_FMT_PREF_LEN] = "";
708   GList        *clp = g_list_first(sl);
709   gchar        *str;
710   int           cur_pos = 0, cur_len = 0;
711   gchar        *quoted_str;
712   int           str_len;
713   gchar        *strp, *quoted_strp, c;
714   int           fmt_len;
715
716   while (clp) {
717     str = clp->data;
718
719     /* Allocate a buffer big enough to hold the entire string, with each
720        character quoted (that's the worst case).  */
721     str_len = strlen(str);
722     quoted_str = g_malloc(str_len*2 + 1);
723
724     /* Now quote any " or \ characters in it. */
725     strp = str;
726     quoted_strp = quoted_str;
727     while ((c = *strp++) != '\0') {
728       if (c == '"' || c == '\\') {
729         /* It has to be backslash-quoted.  */
730         *quoted_strp++ = '\\';
731       }
732       *quoted_strp++ = c;
733     }
734     *quoted_strp = '\0';
735
736     fmt_len = strlen(quoted_str) + 4;
737     if ((fmt_len + cur_len) < (MAX_FMT_PREF_LEN - 1)) {
738       if ((fmt_len + cur_pos) > MAX_FMT_PREF_LINE_LEN) {
739         /* Wrap the line.  */
740         cur_len--;
741         cur_pos = 0;
742         pref_str[cur_len] = '\n'; cur_len++;
743         pref_str[cur_len] = '\t'; cur_len++;
744       }
745       sprintf(&pref_str[cur_len], "\"%s\", ", quoted_str);
746       cur_pos += fmt_len;
747       cur_len += fmt_len;
748     }
749     g_free(quoted_str);
750     clp = clp->next;
751   }
752
753   /* If the string is at least two characters long, the last two characters
754      are ", ", and should be discarded, as there are no more items in the
755      string.  */
756   if (cur_len >= 2)
757     pref_str[cur_len - 2] = '\0';
758
759   return(pref_str);
760 }
761
762 static void
763 clear_string_list(GList *sl)
764 {
765   GList *l = sl;
766
767   while (l) {
768     g_free(l->data);
769     l = g_list_remove_link(l, l);
770   }
771 }
772
773 /*
774  * Takes a string, a pointer to an array of "enum_val_t"s, and a default gint
775  * value.
776  * The array must be terminated by an entry with a null "name" string.
777  * If the string matches a "name" strings in an entry, the value from that
778  * entry is returned. Otherwise, the default value that was passed as the
779  * third argument is returned.
780  */
781 gint
782 find_val_for_string(const char *needle, const enum_val_t *haystack,
783     gint default_value)
784 {
785         int i = 0;
786
787         while (haystack[i].name != NULL) {
788                 if (strcasecmp(needle, haystack[i].name) == 0) {
789                         return haystack[i].value;
790                 }
791                 i++;
792         }
793         return default_value;
794 }
795
796 /* Takes an string and a pointer to an array of strings, and a default int value.
797  * The array must be terminated by a NULL string. If the string is found in the array
798  * of strings, the index of that string in the array is returned. Otherwise, the
799  * default value that was passed as the third argument is returned.
800  */
801 static int
802 find_index_from_string_array(char *needle, char **haystack, int default_value)
803 {
804         int i = 0;
805
806         while (haystack[i] != NULL) {
807                 if (strcmp(needle, haystack[i]) == 0) {
808                         return i;
809                 }
810                 i++;
811         }
812         return default_value;
813 }
814
815 /* Preferences file format:
816  * - Configuration directives start at the beginning of the line, and
817  *   are terminated with a colon.
818  * - Directives can be continued on the next line by preceding them with
819  *   whitespace.
820  *
821  * Example:
822
823 # This is a comment line
824 print.command: lpr
825 print.file: /a/very/long/path/
826         to/ethereal-out.ps
827  *
828  */
829
830 #define MAX_VAR_LEN    48
831 #define MAX_VAL_LEN  1024
832
833 #define DEF_NUM_COLS    6
834
835 static void read_prefs_file(const char *pf_path, FILE *pf);
836
837 /* Read the preferences file, fill in "prefs", and return a pointer to it.
838
839    If we got an error (other than "it doesn't exist") trying to read
840    the global preferences file, stuff the errno into "*gpf_errno_return"
841    and a pointer to the path of the file into "*gpf_path_return", and
842    return NULL.
843
844    If we got an error (other than "it doesn't exist") trying to read
845    the user's preferences file, stuff the errno into "*pf_errno_return"
846    and a pointer to the path of the file into "*pf_path_return", and
847    return NULL. */
848 e_prefs *
849 read_prefs(int *gpf_errno_return, char **gpf_path_return,
850            int *pf_errno_return, char **pf_path_return)
851 {
852   int         i;
853   char       *pf_path;
854   FILE       *pf;
855   fmt_data   *cfmt;
856   gchar      *col_fmt[] = {"No.",      "%m", "Time",        "%t",
857                            "Source",   "%s", "Destination", "%d",
858                            "Protocol", "%p", "Info",        "%i"};
859
860   if (init_prefs) {
861     /* Initialize preferences to wired-in default values.
862        They may be overridded by the global preferences file or the
863        user's preferences file. */
864     init_prefs       = FALSE;
865     prefs.pr_format  = PR_FMT_TEXT;
866     prefs.pr_dest    = PR_DEST_CMD;
867     prefs.pr_file    = g_strdup("ethereal.out");
868     prefs.pr_cmd     = g_strdup("lpr");
869     prefs.col_list = NULL;
870     for (i = 0; i < DEF_NUM_COLS; i++) {
871       cfmt = (fmt_data *) g_malloc(sizeof(fmt_data));
872       cfmt->title = g_strdup(col_fmt[i * 2]);
873       cfmt->fmt   = g_strdup(col_fmt[(i * 2) + 1]);
874       prefs.col_list = g_list_append(prefs.col_list, cfmt);
875     }
876     prefs.num_cols  = DEF_NUM_COLS;
877     prefs.st_client_fg.pixel =     0;
878     prefs.st_client_fg.red   = 32767;
879     prefs.st_client_fg.green =     0;
880     prefs.st_client_fg.blue  =     0;
881     prefs.st_client_bg.pixel = 65535;
882     prefs.st_client_bg.red   = 65535;
883     prefs.st_client_bg.green = 65535;
884     prefs.st_client_bg.blue  = 65535;
885     prefs.st_server_fg.pixel =     0;
886     prefs.st_server_fg.red   =     0;
887     prefs.st_server_fg.green =     0;
888     prefs.st_server_fg.blue  = 32767;
889     prefs.st_server_bg.pixel = 65535;
890     prefs.st_server_bg.red   = 65535;
891     prefs.st_server_bg.green = 65535;
892     prefs.st_server_bg.blue  = 65535;
893     prefs.gui_scrollbar_on_right = TRUE;
894     prefs.gui_plist_sel_browse = FALSE;
895     prefs.gui_ptree_sel_browse = FALSE;
896     prefs.gui_altern_colors = FALSE;
897     prefs.gui_ptree_line_style = 0;
898     prefs.gui_ptree_expander_style = 1;
899     prefs.gui_hex_dump_highlight_style = 1;
900 #ifdef WIN32
901     prefs.gui_font_name = g_strdup("-*-lucida console-medium-r-*-*-*-100-*-*-*-*-*-*");
902 #else
903     /*
904      * XXX - for now, we make the initial font name a pattern that matches
905      * only ISO 8859/1 fonts, so that we don't match 2-byte fonts such
906      * as ISO 10646 fonts.
907      *
908      * Users in locales using other one-byte fonts will have to choose
909      * a different font from the preferences dialog - or put the font
910      * selection in the global preferences file to make that font the
911      * default for all users who don't explicitly specify a different
912      * font.
913      *
914      * Making this a font set rather than a font has two problems:
915      *
916      *  1) as far as I know, you can't select font sets with the
917      *     font selection dialog;
918      *
919      *  2) if you use a font set, the text to be drawn must be a
920      *     multi-byte string in the appropriate locale, but
921      *     Ethereal does *NOT* guarantee that's the case - in
922      *     the hex-dump window, each character in the text portion
923      *     of the display must be a *single* byte, and in the
924      *     packet-list and protocol-tree windows, text extracted
925      *     from the packet is not necessarily in the right format.
926      *
927      * "Doing this right" may, for the packet-list and protocol-tree
928      * windows, require that dissectors know what the locale is
929      * *AND* know what locale and text representation is used in
930      * the packets they're dissecting, and may be impossible in
931      * the hex-dump window (except by punting and displaying only
932      * ASCII characters).
933      *
934      * GTK+ 2.0 may simplify part of the problem, as it will, as I
935      * understand it, use UTF-8-encoded Unicode as its internal
936      * character set; however, we'd still have to know whatever
937      * character set and encoding is used in the packet (which
938      * may differ for different protocols, e.g. SMB might use
939      * PC code pages for some strings and Unicode for others, whilst
940      * NFS might use some UNIX character set encoding, e.g. ISO 8859/x,
941      * or one of the EUC character sets for Asian languages, or one
942      * of the other multi-byte character sets, or UTF-8, or...).
943      *
944      * I.e., as far as I can tell, "internationalizing" the packet-list,
945      * protocol-tree, and hex-dump windows involves a lot more than, say,
946      * just using font sets rather than fonts.
947      */
948     prefs.gui_font_name = g_strdup("-*-fixed-medium-r-semicondensed-*-*-120-*-*-*-*-iso8859-1");
949 #endif
950     prefs.gui_marked_fg.pixel        =     65535;
951     prefs.gui_marked_fg.red          =     65535;
952     prefs.gui_marked_fg.green        =     65535;
953     prefs.gui_marked_fg.blue         =     65535;
954     prefs.gui_marked_bg.pixel        =         0;
955     prefs.gui_marked_bg.red          =         0;
956     prefs.gui_marked_bg.green        =         0;
957     prefs.gui_marked_bg.blue         =         0;
958     prefs.gui_geometry_save_position =         0;
959     prefs.gui_geometry_save_size     =         1;
960     prefs.gui_geometry_main_x        =        20;
961     prefs.gui_geometry_main_y        =        20;
962     prefs.gui_geometry_main_width    = DEF_WIDTH;
963     prefs.gui_geometry_main_height   =        -1;
964
965 /* set the default values for the capture dialog box */
966     prefs.capture_device      = NULL;
967     prefs.capture_prom_mode   = TRUE;
968     prefs.capture_real_time   = FALSE;
969     prefs.capture_auto_scroll = FALSE;
970     prefs.name_resolve        = RESOLV_ALL ^ RESOLV_NETWORK;
971   }
972
973   /* Construct the pathname of the global preferences file. */
974   if (! gpf_path) {
975     gpf_path = (gchar *) g_malloc(strlen(get_datafile_dir()) +
976       strlen(GPF_NAME) + 2);
977     sprintf(gpf_path, "%s" G_DIR_SEPARATOR_S "%s",
978       get_datafile_dir(), GPF_NAME);
979   }
980
981   /* Read the global preferences file, if it exists. */
982   *gpf_path_return = NULL;
983   if ((pf = fopen(gpf_path, "r")) != NULL) {
984     /* We succeeded in opening it; read it. */
985     read_prefs_file(gpf_path, pf);
986     fclose(pf);
987   } else {
988     /* We failed to open it.  If we failed for some reason other than
989        "it doesn't exist", return the errno and the pathname, so our
990        caller can report the error. */
991     if (errno != ENOENT) {
992       *gpf_errno_return = errno;
993       *gpf_path_return = gpf_path;
994     }
995   }
996
997   /* Construct the pathname of the user's preferences file. */
998   pf_path = get_persconffile_path(PF_NAME, FALSE);
999
1000   /* Read the user's preferences file, if it exists. */
1001   *pf_path_return = NULL;
1002   if ((pf = fopen(pf_path, "r")) != NULL) {
1003     /* We succeeded in opening it; read it. */
1004     read_prefs_file(pf_path, pf);
1005     fclose(pf);
1006     g_free(pf_path);
1007     pf_path = NULL;
1008   } else {
1009     /* We failed to open it.  If we failed for some reason other than
1010        "it doesn't exist", return the errno and the pathname, so our
1011        caller can report the error. */
1012     if (errno != ENOENT) {
1013       *pf_errno_return = errno;
1014       *pf_path_return = pf_path;
1015     }
1016   }
1017
1018   return &prefs;
1019 }
1020
1021 static void
1022 read_prefs_file(const char *pf_path, FILE *pf)
1023 {
1024   enum { START, IN_VAR, PRE_VAL, IN_VAL, IN_SKIP };
1025   gchar     cur_var[MAX_VAR_LEN], cur_val[MAX_VAL_LEN];
1026   int       got_c, state = START;
1027   gboolean  got_val = FALSE;
1028   gint      var_len = 0, val_len = 0, fline = 1, pline = 1;
1029
1030   /*
1031    * Start out the counters of "mgcp.{tcp,udp}.port" entries we've
1032    * seen.
1033    */
1034   mgcp_tcp_port_count = 0;
1035   mgcp_udp_port_count = 0;
1036
1037   while ((got_c = getc(pf)) != EOF) {
1038     if (got_c == '\n') {
1039       state = START;
1040       fline++;
1041       continue;
1042     }
1043     if (var_len >= MAX_VAR_LEN) {
1044       g_warning ("%s line %d: Variable too long", pf_path, fline);
1045       state = IN_SKIP;
1046       var_len = 0;
1047       continue;
1048     }
1049     if (val_len >= MAX_VAL_LEN) {
1050       g_warning ("%s line %d: Value too long", pf_path, fline);
1051       state = IN_SKIP;
1052       var_len = 0;
1053       continue;
1054     }
1055
1056     switch (state) {
1057       case START:
1058         if (isalnum(got_c)) {
1059           if (var_len > 0) {
1060             if (got_val) {
1061               cur_var[var_len] = '\0';
1062               cur_val[val_len] = '\0';
1063               switch (set_pref(cur_var, cur_val)) {
1064
1065               case PREFS_SET_SYNTAX_ERR:
1066                 g_warning ("%s line %d: Syntax error", pf_path, pline);
1067                 break;
1068
1069               case PREFS_SET_NO_SUCH_PREF:
1070                 g_warning ("%s line %d: No such preference \"%s\"", pf_path,
1071                                 pline, cur_var);
1072                 break;
1073
1074               case PREFS_SET_OBSOLETE:
1075                 /* We silently ignore attempts to set these; it's
1076                    probably not the user's fault that it's in there -
1077                    they may have saved preferences with a release that
1078                    supported them. */
1079                 break;
1080               }
1081             } else {
1082               g_warning ("%s line %d: Incomplete preference", pf_path, pline);
1083             }
1084           }
1085           state      = IN_VAR;
1086           got_val    = FALSE;
1087           cur_var[0] = got_c;
1088           var_len    = 1;
1089           pline = fline;
1090         } else if (isspace(got_c) && var_len > 0 && got_val) {
1091           state = PRE_VAL;
1092         } else if (got_c == '#') {
1093           state = IN_SKIP;
1094         } else {
1095           g_warning ("%s line %d: Malformed line", pf_path, fline);
1096         }
1097         break;
1098       case IN_VAR:
1099         if (got_c != ':') {
1100           cur_var[var_len] = got_c;
1101           var_len++;
1102         } else {
1103           state   = PRE_VAL;
1104           val_len = 0;
1105           got_val = TRUE;
1106         }
1107         break;
1108       case PRE_VAL:
1109         if (!isspace(got_c)) {
1110           state = IN_VAL;
1111           cur_val[val_len] = got_c;
1112           val_len++;
1113         }
1114         break;
1115       case IN_VAL:
1116         if (got_c != '#')  {
1117           cur_val[val_len] = got_c;
1118           val_len++;
1119         } else {
1120           while (isspace((guchar)cur_val[val_len]) && val_len > 0)
1121             val_len--;
1122           state = IN_SKIP;
1123         }
1124         break;
1125     }
1126   }
1127   if (var_len > 0) {
1128     if (got_val) {
1129       cur_var[var_len] = '\0';
1130       cur_val[val_len] = '\0';
1131       switch (set_pref(cur_var, cur_val)) {
1132
1133       case PREFS_SET_SYNTAX_ERR:
1134         g_warning ("%s line %d: Syntax error", pf_path, pline);
1135         break;
1136
1137       case PREFS_SET_NO_SUCH_PREF:
1138         g_warning ("%s line %d: No such preference \"%s\"", pf_path,
1139                         pline, cur_var);
1140         break;
1141
1142       case PREFS_SET_OBSOLETE:
1143         /* We silently ignore attempts to set these; it's probably not
1144            the user's fault that it's in there - they may have saved
1145            preferences with a release that supported it. */
1146         break;
1147       }
1148     } else {
1149       g_warning ("%s line %d: Incomplete preference", pf_path, pline);
1150     }
1151   }
1152 }
1153
1154 /*
1155  * Given a string of the form "<pref name>:<pref value>", as might appear
1156  * as an argument to a "-o" option, parse it and set the preference in
1157  * question.  Return an indication of whether it succeeded or failed
1158  * in some fashion.
1159  */
1160 int
1161 prefs_set_pref(char *prefarg)
1162 {
1163         guchar *p, *colonp;
1164         int ret;
1165
1166         /*
1167          * Set the counters of "mgcp.{tcp,udp}.port" entries we've
1168          * seen to values that keep us from trying to interpret tham
1169          * as "mgcp.{tcp,udp}.gateway_port" or "mgcp.{tcp,udp}.callagent_port",
1170          * as, from the command line, we have no way of guessing which
1171          * the user had in mind.
1172          */
1173         mgcp_tcp_port_count = -1;
1174         mgcp_udp_port_count = -1;
1175
1176         colonp = strchr(prefarg, ':');
1177         if (colonp == NULL)
1178                 return PREFS_SET_SYNTAX_ERR;
1179
1180         p = colonp;
1181         *p++ = '\0';
1182
1183         /*
1184          * Skip over any white space (there probably won't be any, but
1185          * as we allow it in the preferences file, we might as well
1186          * allow it here).
1187          */
1188         while (isspace(*p))
1189                 p++;
1190         if (*p == '\0') {
1191                 /*
1192                  * Put the colon back, so if our caller uses, in an
1193                  * error message, the string they passed us, the message
1194                  * looks correct.
1195                  */
1196                 *colonp = ':';
1197                 return PREFS_SET_SYNTAX_ERR;
1198         }
1199
1200         ret = set_pref(prefarg, p);
1201         *colonp = ':';  /* put the colon back */
1202         return ret;
1203 }
1204
1205 #define PRS_PRINT_FMT    "print.format"
1206 #define PRS_PRINT_DEST   "print.destination"
1207 #define PRS_PRINT_FILE   "print.file"
1208 #define PRS_PRINT_CMD    "print.command"
1209 #define PRS_COL_FMT      "column.format"
1210 #define PRS_STREAM_CL_FG "stream.client.fg"
1211 #define PRS_STREAM_CL_BG "stream.client.bg"
1212 #define PRS_STREAM_SR_FG "stream.server.fg"
1213 #define PRS_STREAM_SR_BG "stream.server.bg"
1214 #define PRS_GUI_SCROLLBAR_ON_RIGHT "gui.scrollbar_on_right"
1215 #define PRS_GUI_PLIST_SEL_BROWSE "gui.packet_list_sel_browse"
1216 #define PRS_GUI_PTREE_SEL_BROWSE "gui.protocol_tree_sel_browse"
1217 #define PRS_GUI_ALTERN_COLORS "gui.tree_view_altern_colors"
1218 #define PRS_GUI_PTREE_LINE_STYLE "gui.protocol_tree_line_style"
1219 #define PRS_GUI_PTREE_EXPANDER_STYLE "gui.protocol_tree_expander_style"
1220 #define PRS_GUI_HEX_DUMP_HIGHLIGHT_STYLE "gui.hex_dump_highlight_style"
1221 #define PRS_GUI_FONT_NAME "gui.font_name"
1222 #define PRS_GUI_MARKED_FG "gui.marked_frame.fg"
1223 #define PRS_GUI_MARKED_BG "gui.marked_frame.bg"
1224 #define PRS_GUI_GEOMETRY_SAVE_POSITION "gui.geometry.save.position"
1225 #define PRS_GUI_GEOMETRY_SAVE_SIZE     "gui.geometry.save.size"
1226 #define PRS_GUI_GEOMETRY_MAIN_X        "gui.geometry.main.x"
1227 #define PRS_GUI_GEOMETRY_MAIN_Y        "gui.geometry.main.y"
1228 #define PRS_GUI_GEOMETRY_MAIN_WIDTH    "gui.geometry.main.width"
1229 #define PRS_GUI_GEOMETRY_MAIN_HEIGHT   "gui.geometry.main.height"
1230
1231 /*
1232  * This applies to more than just captures, so it's not "capture.name_resolve";
1233  * "capture.name_resolve" is supported on input for backwards compatibility.
1234  *
1235  * It's not a preference for a particular part of Ethereal, it's used all
1236  * over the place, so its name doesn't have two components.
1237  */
1238 #define PRS_NAME_RESOLVE "name_resolve"
1239 #define PRS_CAP_NAME_RESOLVE "capture.name_resolve"
1240
1241 /*  values for the capture dialog box */
1242 #define PRS_CAP_DEVICE      "capture.device"
1243 #define PRS_CAP_PROM_MODE   "capture.prom_mode"
1244 #define PRS_CAP_REAL_TIME   "capture.real_time_update"
1245 #define PRS_CAP_AUTO_SCROLL "capture.auto_scroll"
1246
1247 #define RED_COMPONENT(x)   ((((x) >> 16) & 0xff) * 65535 / 255)
1248 #define GREEN_COMPONENT(x) ((((x) >>  8) & 0xff) * 65535 / 255)
1249 #define BLUE_COMPONENT(x)   (((x)        & 0xff) * 65535 / 255)
1250
1251 static gchar *pr_formats[] = { "text", "postscript" };
1252 static gchar *pr_dests[]   = { "command", "file" };
1253
1254 typedef struct {
1255   char    letter;
1256   guint32 value;
1257 } name_resolve_opt_t;
1258
1259 static name_resolve_opt_t name_resolve_opt[] = {
1260   { 'm', RESOLV_MAC },
1261   { 'n', RESOLV_NETWORK },
1262   { 't', RESOLV_TRANSPORT },
1263 };
1264
1265 #define N_NAME_RESOLVE_OPT      (sizeof name_resolve_opt / sizeof name_resolve_opt[0])
1266
1267 static char *
1268 name_resolve_to_string(guint32 name_resolve)
1269 {
1270   static char string[N_NAME_RESOLVE_OPT+1];
1271   char *p;
1272   unsigned int i;
1273   gboolean all_opts_set = TRUE;
1274
1275   if (name_resolve == RESOLV_NONE)
1276     return "FALSE";
1277   p = &string[0];
1278   for (i = 0; i < N_NAME_RESOLVE_OPT; i++) {
1279     if (name_resolve & name_resolve_opt[i].value)
1280       *p++ =  name_resolve_opt[i].letter;
1281     else
1282       all_opts_set = FALSE;
1283   }
1284   *p = '\0';
1285   if (all_opts_set)
1286     return "TRUE";
1287   return string;
1288 }
1289
1290 char
1291 string_to_name_resolve(char *string, guint32 *name_resolve)
1292 {
1293   char c;
1294   unsigned int i;
1295
1296   *name_resolve = 0;
1297   while ((c = *string++) != '\0') {
1298     for (i = 0; i < N_NAME_RESOLVE_OPT; i++) {
1299       if (c == name_resolve_opt[i].letter) {
1300         *name_resolve |= name_resolve_opt[i].value;
1301         break;
1302       }
1303     }
1304     if (i == N_NAME_RESOLVE_OPT) {
1305       /*
1306        * Unrecognized letter.
1307        */
1308       return c;
1309     }
1310   }
1311   return '\0';
1312 }
1313
1314 static int
1315 set_pref(gchar *pref_name, gchar *value)
1316 {
1317   GList    *col_l, *col_l_elt;
1318   gint      llen;
1319   fmt_data *cfmt;
1320   unsigned long int cval;
1321   guint    uval;
1322   gboolean bval;
1323   gint     enum_val;
1324   char     *p;
1325   gchar    *dotp, *last_dotp;
1326   module_t *module;
1327   pref_t   *pref;
1328   gboolean had_a_dot;
1329
1330   if (strcmp(pref_name, PRS_PRINT_FMT) == 0) {
1331     if (strcmp(value, pr_formats[PR_FMT_TEXT]) == 0) {
1332       prefs.pr_format = PR_FMT_TEXT;
1333     } else if (strcmp(value, pr_formats[PR_FMT_PS]) == 0) {
1334       prefs.pr_format = PR_FMT_PS;
1335     } else {
1336       return PREFS_SET_SYNTAX_ERR;
1337     }
1338   } else if (strcmp(pref_name, PRS_PRINT_DEST) == 0) {
1339     if (strcmp(value, pr_dests[PR_DEST_CMD]) == 0) {
1340       prefs.pr_dest = PR_DEST_CMD;
1341     } else if (strcmp(value, pr_dests[PR_DEST_FILE]) == 0) {
1342       prefs.pr_dest = PR_DEST_FILE;
1343     } else {
1344       return PREFS_SET_SYNTAX_ERR;
1345     }
1346   } else if (strcmp(pref_name, PRS_PRINT_FILE) == 0) {
1347     if (prefs.pr_file) g_free(prefs.pr_file);
1348     prefs.pr_file = g_strdup(value);
1349   } else if (strcmp(pref_name, PRS_PRINT_CMD) == 0) {
1350     if (prefs.pr_cmd) g_free(prefs.pr_cmd);
1351     prefs.pr_cmd = g_strdup(value);
1352   } else if (strcmp(pref_name, PRS_COL_FMT) == 0) {
1353     col_l = get_string_list(value);
1354     if (col_l == NULL)
1355       return PREFS_SET_SYNTAX_ERR;
1356     if ((g_list_length(col_l) % 2) != 0) {
1357       /* A title didn't have a matching format.  */
1358       clear_string_list(col_l);
1359       return PREFS_SET_SYNTAX_ERR;
1360     }
1361     /* Check to make sure all column formats are valid.  */
1362     col_l_elt = g_list_first(col_l);
1363     while(col_l_elt) {
1364       /* Make sure the title isn't empty.  */
1365       if (strcmp(col_l_elt->data, "") == 0) {
1366         /* It is.  */
1367         clear_string_list(col_l);
1368         return PREFS_SET_SYNTAX_ERR;
1369       }
1370
1371       /* Go past the title.  */
1372       col_l_elt = col_l_elt->next;
1373
1374       /* Check the format.  */
1375       if (get_column_format_from_str(col_l_elt->data) == -1) {
1376         /* It's not a valid column format.  */
1377         clear_string_list(col_l);
1378         return PREFS_SET_SYNTAX_ERR;
1379       }
1380
1381       /* Go past the format.  */
1382       col_l_elt = col_l_elt->next;
1383     }
1384     free_col_info(&prefs);
1385     prefs.col_list = NULL;
1386     llen             = g_list_length(col_l);
1387     prefs.num_cols   = llen / 2;
1388     col_l_elt = g_list_first(col_l);
1389     while(col_l_elt) {
1390       cfmt = (fmt_data *) g_malloc(sizeof(fmt_data));
1391       cfmt->title    = g_strdup(col_l_elt->data);
1392       col_l_elt      = col_l_elt->next;
1393       cfmt->fmt      = g_strdup(col_l_elt->data);
1394       col_l_elt      = col_l_elt->next;
1395       prefs.col_list = g_list_append(prefs.col_list, cfmt);
1396     }
1397     clear_string_list(col_l);
1398   } else if (strcmp(pref_name, PRS_STREAM_CL_FG) == 0) {
1399     cval = strtoul(value, NULL, 16);
1400     prefs.st_client_fg.pixel = 0;
1401     prefs.st_client_fg.red   = RED_COMPONENT(cval);
1402     prefs.st_client_fg.green = GREEN_COMPONENT(cval);
1403     prefs.st_client_fg.blue  = BLUE_COMPONENT(cval);
1404   } else if (strcmp(pref_name, PRS_STREAM_CL_BG) == 0) {
1405     cval = strtoul(value, NULL, 16);
1406     prefs.st_client_bg.pixel = 0;
1407     prefs.st_client_bg.red   = RED_COMPONENT(cval);
1408     prefs.st_client_bg.green = GREEN_COMPONENT(cval);
1409     prefs.st_client_bg.blue  = BLUE_COMPONENT(cval);
1410   } else if (strcmp(pref_name, PRS_STREAM_SR_FG) == 0) {
1411     cval = strtoul(value, NULL, 16);
1412     prefs.st_server_fg.pixel = 0;
1413     prefs.st_server_fg.red   = RED_COMPONENT(cval);
1414     prefs.st_server_fg.green = GREEN_COMPONENT(cval);
1415     prefs.st_server_fg.blue  = BLUE_COMPONENT(cval);
1416   } else if (strcmp(pref_name, PRS_STREAM_SR_BG) == 0) {
1417     cval = strtoul(value, NULL, 16);
1418     prefs.st_server_bg.pixel = 0;
1419     prefs.st_server_bg.red   = RED_COMPONENT(cval);
1420     prefs.st_server_bg.green = GREEN_COMPONENT(cval);
1421     prefs.st_server_bg.blue  = BLUE_COMPONENT(cval);
1422   } else if (strcmp(pref_name, PRS_GUI_SCROLLBAR_ON_RIGHT) == 0) {
1423     if (strcasecmp(value, "true") == 0) {
1424             prefs.gui_scrollbar_on_right = TRUE;
1425     }
1426     else {
1427             prefs.gui_scrollbar_on_right = FALSE;
1428     }
1429   } else if (strcmp(pref_name, PRS_GUI_PLIST_SEL_BROWSE) == 0) {
1430     if (strcasecmp(value, "true") == 0) {
1431             prefs.gui_plist_sel_browse = TRUE;
1432     }
1433     else {
1434             prefs.gui_plist_sel_browse = FALSE;
1435     }
1436   } else if (strcmp(pref_name, PRS_GUI_PTREE_SEL_BROWSE) == 0) {
1437     if (strcasecmp(value, "true") == 0) {
1438             prefs.gui_ptree_sel_browse = TRUE;
1439     }
1440     else {
1441             prefs.gui_ptree_sel_browse = FALSE;
1442     }
1443   } else if (strcmp(pref_name, PRS_GUI_ALTERN_COLORS) == 0) {
1444     if (strcasecmp(value, "true") == 0) {
1445             prefs.gui_altern_colors = TRUE;
1446     }
1447     else {
1448             prefs.gui_altern_colors = FALSE;
1449     }
1450   } else if (strcmp(pref_name, PRS_GUI_PTREE_LINE_STYLE) == 0) {
1451           prefs.gui_ptree_line_style =
1452                   find_index_from_string_array(value, gui_ptree_line_style_text, 0);
1453   } else if (strcmp(pref_name, PRS_GUI_PTREE_EXPANDER_STYLE) == 0) {
1454           prefs.gui_ptree_expander_style =
1455                   find_index_from_string_array(value, gui_ptree_expander_style_text, 1);
1456   } else if (strcmp(pref_name, PRS_GUI_HEX_DUMP_HIGHLIGHT_STYLE) == 0) {
1457           prefs.gui_hex_dump_highlight_style =
1458                   find_index_from_string_array(value, gui_hex_dump_highlight_style_text, 1);
1459   } else if (strcmp(pref_name, PRS_GUI_FONT_NAME) == 0) {
1460           if (prefs.gui_font_name != NULL)
1461                 g_free(prefs.gui_font_name);
1462           prefs.gui_font_name = g_strdup(value);
1463   } else if (strcmp(pref_name, PRS_GUI_MARKED_FG) == 0) {
1464     cval = strtoul(value, NULL, 16);
1465     prefs.gui_marked_fg.pixel = 0;
1466     prefs.gui_marked_fg.red   = RED_COMPONENT(cval);
1467     prefs.gui_marked_fg.green = GREEN_COMPONENT(cval);
1468     prefs.gui_marked_fg.blue  = BLUE_COMPONENT(cval);
1469   } else if (strcmp(pref_name, PRS_GUI_MARKED_BG) == 0) {
1470     cval = strtoul(value, NULL, 16);
1471     prefs.gui_marked_bg.pixel = 0;
1472     prefs.gui_marked_bg.red   = RED_COMPONENT(cval);
1473     prefs.gui_marked_bg.green = GREEN_COMPONENT(cval);
1474     prefs.gui_marked_bg.blue  = BLUE_COMPONENT(cval);
1475   } else if (strcmp(pref_name, PRS_GUI_GEOMETRY_SAVE_POSITION) == 0) {
1476     if (strcasecmp(value, "true") == 0) {
1477             prefs.gui_geometry_save_position = TRUE;
1478     }
1479     else {
1480             prefs.gui_geometry_save_position = FALSE;
1481     }
1482   } else if (strcmp(pref_name, PRS_GUI_GEOMETRY_SAVE_SIZE) == 0) {
1483     if (strcasecmp(value, "true") == 0) {
1484             prefs.gui_geometry_save_size = TRUE;
1485     }
1486     else {
1487             prefs.gui_geometry_save_size = FALSE;
1488     }
1489   } else if (strcmp(pref_name, PRS_GUI_GEOMETRY_MAIN_X) == 0) {
1490     prefs.gui_geometry_main_x = strtol(value, NULL, 10);
1491   } else if (strcmp(pref_name, PRS_GUI_GEOMETRY_MAIN_Y) == 0) {
1492     prefs.gui_geometry_main_y = strtol(value, NULL, 10);
1493   } else if (strcmp(pref_name, PRS_GUI_GEOMETRY_MAIN_WIDTH) == 0) {
1494     prefs.gui_geometry_main_width = strtol(value, NULL, 10);
1495   } else if (strcmp(pref_name, PRS_GUI_GEOMETRY_MAIN_HEIGHT) == 0) {
1496     prefs.gui_geometry_main_height = strtol(value, NULL, 10);
1497
1498 /* handle the capture options */
1499   } else if (strcmp(pref_name, PRS_CAP_DEVICE) == 0) {
1500     if (prefs.capture_device != NULL)
1501       g_free(prefs.capture_device);
1502     prefs.capture_device = g_strdup(value);
1503   } else if (strcmp(pref_name, PRS_CAP_PROM_MODE) == 0) {
1504     prefs.capture_prom_mode = ((strcasecmp(value, "true") == 0)?TRUE:FALSE);
1505   } else if (strcmp(pref_name, PRS_CAP_REAL_TIME) == 0) {
1506     prefs.capture_real_time = ((strcasecmp(value, "true") == 0)?TRUE:FALSE);
1507   } else if (strcmp(pref_name, PRS_CAP_AUTO_SCROLL) == 0) {
1508     prefs.capture_auto_scroll = ((strcasecmp(value, "true") == 0)?TRUE:FALSE);
1509
1510 /* handle the global options */
1511   } else if (strcmp(pref_name, PRS_NAME_RESOLVE) == 0 ||
1512              strcmp(pref_name, PRS_CAP_NAME_RESOLVE) == 0) {
1513     /*
1514      * "TRUE" and "FALSE", for backwards compatibility, are synonyms for
1515      * RESOLV_ALL and RESOLV_NONE.
1516      *
1517      * Otherwise, we treat it as a list of name types we want to resolve.
1518      */
1519     if (strcasecmp(value, "true") == 0)
1520       prefs.name_resolve = RESOLV_ALL;
1521     else if (strcasecmp(value, "false") == 0)
1522       prefs.name_resolve = RESOLV_NONE;
1523     else {
1524       prefs.name_resolve = RESOLV_NONE; /* start out with none set */
1525       if (string_to_name_resolve(value, &prefs.name_resolve) != '\0')
1526         return PREFS_SET_SYNTAX_ERR;
1527     }
1528   } else {
1529     /* To which module does this preference belong? */
1530     module = NULL;
1531     last_dotp = pref_name;
1532     had_a_dot = FALSE;
1533     while (!module) {
1534         dotp = strchr(last_dotp, '.');
1535         if (dotp == NULL) {
1536             if (had_a_dot) {
1537               /* no such module */
1538               return PREFS_SET_NO_SUCH_PREF;
1539             }
1540             else {
1541               /* no ".", so no module/name separator */
1542               return PREFS_SET_SYNTAX_ERR;
1543             }
1544         }
1545         else {
1546             had_a_dot = TRUE;
1547         }
1548         *dotp = '\0';           /* separate module and preference name */
1549         module = find_module(pref_name);
1550
1551         /*
1552          * XXX - "Diameter" rather than "diameter" was used in earlier
1553          * versions of Ethereal; if we didn't find the module, and its name
1554          * was "Diameter", look for "diameter" instead.
1555          *
1556          * In addition, the BEEP protocol used to be the BXXP protocol,
1557          * so if we didn't find the module, and its name was "bxxp",
1558          * look for "beep" instead.
1559          *
1560          * Also, the preferences for GTP v0 and v1 were combined under
1561          * a single "gtp" heading.
1562          */
1563         if (module == NULL) {
1564           if (strcmp(pref_name, "Diameter") == 0)
1565             module = find_module("diameter");
1566           else if (strcmp(pref_name, "bxxp") == 0)
1567             module = find_module("beep");
1568           else if (strcmp(pref_name, "gtpv0") == 0 ||
1569                    strcmp(pref_name, "gtpv1") == 0)
1570             module = find_module("gtp");
1571         }
1572         *dotp = '.';            /* put the preference string back */
1573         dotp++;                 /* skip past separator to preference name */
1574         last_dotp = dotp;
1575     }
1576
1577     pref = find_preference(module, dotp);
1578
1579     if (pref == NULL) {
1580       if (strncmp(pref_name, "mgcp.", 5) == 0) {
1581         /*
1582          * XXX - "mgcp.display raw text toggle" and "mgcp.display dissect tree"
1583          * rather than "mgcp.display_raw_text" and "mgcp.display_dissect_tree"
1584          * were used in earlier versions of Ethereal; if we didn't find the
1585          * preference, it was an MGCP preference, and its name was
1586          * "display raw text toggle" or "display dissect tree", look for
1587          * "display_raw_text" or "display_dissect_tree" instead.
1588          *
1589          * "mgcp.tcp.port" and "mgcp.udp.port" are harder to handle, as both
1590          * the gateway and callagent ports were given those names; we interpret
1591          * the first as "mgcp.{tcp,udp}.gateway_port" and the second as
1592          * "mgcp.{tcp,udp}.callagent_port", as that's the order in which
1593          * they were registered by the MCCP dissector and thus that's the
1594          * order in which they were written to the preferences file.  (If
1595          * we're not reading the preferences file, but are handling stuff
1596          * from a "-o" command-line option, we have no clue which the user
1597          * had in mind - they should have used "mgcp.{tcp,udp}.gateway_port"
1598          * or "mgcp.{tcp,udp}.callagent_port" instead.)
1599          */
1600         if (strcmp(dotp, "display raw text toggle") == 0)
1601           pref = find_preference(module, "display_raw_text");
1602         else if (strcmp(dotp, "display dissect tree") == 0)
1603           pref = find_preference(module, "display_dissect_tree");
1604         else if (strcmp(dotp, "tcp.port") == 0) {
1605           mgcp_tcp_port_count++;
1606           if (mgcp_tcp_port_count == 1) {
1607             /* It's the first one */
1608             pref = find_preference(module, "tcp.gateway_port");
1609           } else if (mgcp_tcp_port_count == 2) {
1610             /* It's the second one */
1611             pref = find_preference(module, "tcp.callagent_port");
1612           }
1613           /* Otherwise it's from the command line, and we don't bother
1614              mapping it. */
1615         } else if (strcmp(dotp, "udp.port") == 0) {
1616           mgcp_udp_port_count++;
1617           if (mgcp_udp_port_count == 1) {
1618             /* It's the first one */
1619             pref = find_preference(module, "udp.gateway_port");
1620           } else if (mgcp_udp_port_count == 2) {
1621             /* It's the second one */
1622             pref = find_preference(module, "udp.callagent_port");
1623           }
1624           /* Otherwise it's from the command line, and we don't bother
1625              mapping it. */
1626         }
1627       } else if (strncmp(pref_name, "smb.", 4) == 0) {
1628         /* Handle old names for SMB preferences. */
1629         if (strcmp(dotp, "smb.trans.reassembly") == 0)
1630           pref = find_preference(module, "trans_reassembly");
1631         else if (strcmp(dotp, "smb.dcerpc.reassembly") == 0)
1632           pref = find_preference(module, "dcerpc_reassembly");
1633       } else if (strncmp(pref_name, "ndmp.", 5) == 0) {
1634         /* Handle old names for NDMP preferences. */
1635         if (strcmp(dotp, "ndmp.desegment") == 0)
1636           pref = find_preference(module, "desegment");
1637       } else if (strncmp(pref_name, "diameter.", 9) == 0) {
1638         /* Handle old names for Diameter preferences. */
1639         if (strcmp(dotp, "diameter.desegment") == 0)
1640           pref = find_preference(module, "desegment");
1641       } else if (strncmp(pref_name, "pcli.", 5) == 0) {
1642         /* Handle old names for PCLI preferences. */
1643         if (strcmp(dotp, "pcli.udp_port") == 0)
1644           pref = find_preference(module, "udp_port");
1645       } else if (strncmp(pref_name, "mapi.", 5) == 0) {
1646         /* Handle old names for MAPI preferences. */
1647         if (strcmp(dotp, "mapi_decrypt") == 0)
1648           pref = find_preference(module, "decrypt");
1649       } else if (strncmp(pref_name, "fc.", 3) == 0) {
1650         /* Handle old names for Fibre Channel preferences. */
1651         if (strcmp(dotp, "reassemble_fc") == 0)
1652           pref = find_preference(module, "reassemble");
1653         else if (strcmp(dotp, "fc_max_frame_size") == 0)
1654           pref = find_preference(module, "max_frame_size");
1655       } else if (strncmp(pref_name, "fcip.", 5) == 0) {
1656         /* Handle old names for Fibre Channel-over-IP preferences. */
1657         if (strcmp(dotp, "desegment_fcip_messages") == 0)
1658           pref = find_preference(module, "desegment");
1659         else if (strcmp(dotp, "fcip_port") == 0)
1660           pref = find_preference(module, "target_port");
1661       } else if (strncmp(pref_name, "gtp.", 4) == 0) {
1662         /* Handle old names for GTP preferences. */
1663         if (strcmp(dotp, "gtpv0_port") == 0)
1664           pref = find_preference(module, "v0_port");
1665         else if (strcmp(dotp, "gtpv1c_port") == 0)
1666           pref = find_preference(module, "v1c_port");
1667         else if (strcmp(dotp, "gtpv1u_port") == 0)
1668           pref = find_preference(module, "v1u_port");
1669         else if (strcmp(dotp, "gtp_dissect_tpdu") == 0)
1670           pref = find_preference(module, "dissect_tpdu");
1671         else if (strcmp(dotp, "gtpv0_dissect_cdr_as") == 0)
1672           pref = find_preference(module, "v0_dissect_cdr_as");
1673         else if (strcmp(dotp, "gtpv0_check_etsi") == 0)
1674           pref = find_preference(module, "v0_check_etsi");
1675         else if (strcmp(dotp, "gtpv1_check_etsi") == 0)
1676           pref = find_preference(module, "v1_check_etsi");
1677       } else if (strncmp(pref_name, "ip.", 3) == 0) {
1678         /* Handle old names for IP preferences. */
1679         if (strcmp(dotp, "ip_summary_in_tree") == 0)
1680           pref = find_preference(module, "summary_in_tree");
1681       } else if (strncmp(pref_name, "iscsi.", 6) == 0) {
1682         /* Handle old names for iSCSI preferences. */
1683         if (strcmp(dotp, "iscsi_port") == 0)
1684           pref = find_preference(module, "target_port");
1685       } else if (strncmp(pref_name, "lmp.", 4) == 0) {
1686         /* Handle old names for LMP preferences. */
1687         if (strcmp(dotp, "lmp_version") == 0)
1688           pref = find_preference(module, "version");
1689       } else if (strncmp(pref_name, "mtp3.", 5) == 0) {
1690         /* Handle old names for MTP3 preferences. */
1691         if (strcmp(dotp, "mtp3_standard") == 0)
1692           pref = find_preference(module, "standard");
1693       } else if (strncmp(pref_name, "nlm.", 4) == 0) {
1694         /* Handle old names for NLM preferences. */
1695         if (strcmp(dotp, "nlm_msg_res_matching") == 0)
1696           pref = find_preference(module, "msg_res_matching");
1697       } else if (strncmp(pref_name, "ppp.", 4) == 0) {
1698         /* Handle old names for PP preferences. */
1699         if (strcmp(dotp, "ppp_fcs") == 0)
1700           pref = find_preference(module, "fcs_type");
1701         else if (strcmp(dotp, "ppp_vj") == 0)
1702           pref = find_preference(module, "decompress_vj");
1703       } else if (strncmp(pref_name, "rsvp.", 5) == 0) {
1704         /* Handle old names for RSVP preferences. */
1705         if (strcmp(dotp, "rsvp_process_bundle") == 0)
1706           pref = find_preference(module, "process_bundle");
1707       } else if (strncmp(pref_name, "tcp.", 4) == 0) {
1708         /* Handle old names for TCP preferences. */
1709         if (strcmp(dotp, "tcp_summary_in_tree") == 0)
1710           pref = find_preference(module, "summary_in_tree");
1711         else if (strcmp(dotp, "tcp_analyze_sequence_numbers") == 0)
1712           pref = find_preference(module, "analyze_sequence_numbers");
1713         else if (strcmp(dotp, "tcp_relative_sequence_numbers") == 0)
1714           pref = find_preference(module, "relative_sequence_numbers");
1715       } else if (strncmp(pref_name, "udp.", 4) == 0) {
1716         /* Handle old names for UDP preferences. */
1717         if (strcmp(dotp, "udp_summary_in_tree") == 0)
1718           pref = find_preference(module, "summary_in_tree");
1719       }
1720     }
1721     if (pref == NULL)
1722       return PREFS_SET_NO_SUCH_PREF;    /* no such preference */
1723
1724     switch (pref->type) {
1725
1726     case PREF_UINT:
1727       uval = strtoul(value, &p, pref->info.base);
1728       if (p == value || *p != '\0')
1729         return PREFS_SET_SYNTAX_ERR;    /* number was bad */
1730       if (*pref->varp.uint != uval) {
1731         module->prefs_changed = TRUE;
1732         *pref->varp.uint = uval;
1733       }
1734       break;
1735
1736     case PREF_BOOL:
1737       /* XXX - give an error if it's neither "true" nor "false"? */
1738       if (strcasecmp(value, "true") == 0)
1739         bval = TRUE;
1740       else
1741         bval = FALSE;
1742       if (*pref->varp.boolp != bval) {
1743         module->prefs_changed = TRUE;
1744         *pref->varp.boolp = bval;
1745       }
1746       break;
1747
1748     case PREF_ENUM:
1749       /* XXX - give an error if it doesn't match? */
1750       enum_val = find_val_for_string(value,
1751                                         pref->info.enum_info.enumvals, 1);
1752       if (*pref->varp.enump != enum_val) {
1753         module->prefs_changed = TRUE;
1754         *pref->varp.enump = enum_val;
1755       }
1756       break;
1757
1758     case PREF_STRING:
1759       if (*pref->varp.string == NULL || strcmp(*pref->varp.string, value) != 0) {
1760         module->prefs_changed = TRUE;
1761         if (*pref->varp.string != NULL)
1762           g_free(*pref->varp.string);
1763         *pref->varp.string = g_strdup(value);
1764       }
1765       break;
1766
1767     case PREF_OBSOLETE:
1768       return PREFS_SET_OBSOLETE;        /* no such preference any more */
1769     }
1770   }
1771
1772   return PREFS_SET_OK;
1773 }
1774
1775 typedef struct {
1776         module_t *module;
1777         FILE    *pf;
1778 } write_pref_arg_t;
1779
1780 /*
1781  * Write out a single preference.
1782  */
1783 static void
1784 write_pref(gpointer data, gpointer user_data)
1785 {
1786         pref_t *pref = data;
1787         write_pref_arg_t *arg = user_data;
1788         const enum_val_t *enum_valp;
1789         const char *val_string;
1790
1791         if (pref->type == PREF_OBSOLETE) {
1792                 /*
1793                  * This preference is no longer supported; it's not a
1794                  * real preference, so we don't write it out (i.e., we
1795                  * treat it as if it weren't found in the list of
1796                  * preferences, and we weren't called in the first place).
1797                  */
1798                 return;
1799         }
1800
1801         fprintf(arg->pf, "\n# %s\n", pref->description);
1802
1803         switch (pref->type) {
1804
1805         case PREF_UINT:
1806                 switch (pref->info.base) {
1807
1808                 case 10:
1809                         fprintf(arg->pf, "# A decimal number.\n");
1810                         fprintf(arg->pf, "%s.%s: %u\n", arg->module->name,
1811                             pref->name, *pref->varp.uint);
1812                         break;
1813
1814                 case 8:
1815                         fprintf(arg->pf, "# An octal number.\n");
1816                         fprintf(arg->pf, "%s.%s: %#o\n", arg->module->name,
1817                             pref->name, *pref->varp.uint);
1818                         break;
1819
1820                 case 16:
1821                         fprintf(arg->pf, "# A hexadecimal number.\n");
1822                         fprintf(arg->pf, "%s.%s: %#x\n", arg->module->name,
1823                             pref->name, *pref->varp.uint);
1824                         break;
1825                 }
1826                 break;
1827
1828         case PREF_BOOL:
1829                 fprintf(arg->pf, "# TRUE or FALSE (case-insensitive).\n");
1830                 fprintf(arg->pf, "%s.%s: %s\n", arg->module->name, pref->name,
1831                     *pref->varp.boolp ? "TRUE" : "FALSE");
1832                 break;
1833
1834         case PREF_ENUM:
1835                 fprintf(arg->pf, "# One of: ");
1836                 enum_valp = pref->info.enum_info.enumvals;
1837                 val_string = NULL;
1838                 while (enum_valp->name != NULL) {
1839                         if (enum_valp->value == *pref->varp.enump)
1840                                 val_string = enum_valp->name;
1841                         fprintf(arg->pf, "%s", enum_valp->name);
1842                         enum_valp++;
1843                         if (enum_valp->name == NULL)
1844                                 fprintf(arg->pf, "\n");
1845                         else
1846                                 fprintf(arg->pf, ", ");
1847                 }
1848                 fprintf(arg->pf, "# (case-insensitive).\n");
1849                 fprintf(arg->pf, "%s.%s: %s\n", arg->module->name, pref->name,
1850                     val_string);
1851                 break;
1852
1853         case PREF_STRING:
1854                 fprintf(arg->pf, "# A string.\n");
1855                 fprintf(arg->pf, "%s.%s: %s\n", arg->module->name, pref->name,
1856                     *pref->varp.string);
1857                 break;
1858
1859         case PREF_OBSOLETE:
1860                 g_assert_not_reached();
1861                 break;
1862         }
1863 }
1864
1865 static void
1866 write_module_prefs(gpointer data, gpointer user_data)
1867 {
1868         write_pref_arg_t arg;
1869
1870         arg.module = data;
1871         arg.pf = user_data;
1872         g_list_foreach(arg.module->prefs, write_pref, &arg);
1873 }
1874
1875 /* Write out "prefs" to the user's preferences file, and return 0.
1876
1877    If we got an error, stuff a pointer to the path of the preferences file
1878    into "*pf_path_return", and return the errno. */
1879 int
1880 write_prefs(char **pf_path_return)
1881 {
1882   char        *pf_path;
1883   FILE        *pf;
1884   GList       *clp, *col_l;
1885   fmt_data    *cfmt;
1886
1887   /* To do:
1888    * - Split output lines longer than MAX_VAL_LEN
1889    * - Create a function for the preference directory check/creation
1890    *   so that duplication can be avoided with filter.c
1891    */
1892
1893   pf_path = get_persconffile_path(PF_NAME, TRUE);
1894   if ((pf = fopen(pf_path, "w")) == NULL) {
1895     *pf_path_return = pf_path;
1896     return errno;
1897   }
1898
1899   fputs("# Configuration file for Ethereal " VERSION ".\n"
1900     "#\n"
1901     "# This file is regenerated each time preferences are saved within\n"
1902     "# Ethereal.  Making manual changes should be safe, however.\n"
1903     "\n"
1904     "######## Printing ########\n"
1905     "\n", pf);
1906
1907   fprintf (pf, "# Can be one of \"text\" or \"postscript\".\n"
1908     "print.format: %s\n\n", pr_formats[prefs.pr_format]);
1909
1910   fprintf (pf, "# Can be one of \"command\" or \"file\".\n"
1911     "print.destination: %s\n\n", pr_dests[prefs.pr_dest]);
1912
1913   fprintf (pf, "# This is the file that gets written to when the "
1914     "destination is set to \"file\"\n"
1915     "%s: %s\n\n", PRS_PRINT_FILE, prefs.pr_file);
1916
1917   fprintf (pf, "# Output gets piped to this command when the destination "
1918     "is set to \"command\"\n"
1919     "%s: %s\n\n", PRS_PRINT_CMD, prefs.pr_cmd);
1920
1921   clp = prefs.col_list;
1922   col_l = NULL;
1923   while (clp) {
1924     cfmt = (fmt_data *) clp->data;
1925     col_l = g_list_append(col_l, cfmt->title);
1926     col_l = g_list_append(col_l, cfmt->fmt);
1927     clp = clp->next;
1928   }
1929   fprintf (pf, "# Packet list column format.  Each pair of strings consists "
1930     "of a column title \n# and its format.\n"
1931     "%s: %s\n\n", PRS_COL_FMT, put_string_list(col_l));
1932   /* This frees the list of strings, but not the strings to which it
1933      refers; that's what we want, as we haven't copied those strings,
1934      we just referred to them.  */
1935   g_list_free(col_l);
1936
1937   fprintf (pf, "# TCP stream window color preferences.  Each value is a six "
1938     "digit hexadecimal value in the form rrggbb.\n");
1939   fprintf (pf, "%s: %02x%02x%02x\n", PRS_STREAM_CL_FG,
1940     (prefs.st_client_fg.red * 255 / 65535),
1941     (prefs.st_client_fg.green * 255 / 65535),
1942     (prefs.st_client_fg.blue * 255 / 65535));
1943   fprintf (pf, "%s: %02x%02x%02x\n", PRS_STREAM_CL_BG,
1944     (prefs.st_client_bg.red * 255 / 65535),
1945     (prefs.st_client_bg.green * 255 / 65535),
1946     (prefs.st_client_bg.blue * 255 / 65535));
1947   fprintf (pf, "%s: %02x%02x%02x\n", PRS_STREAM_SR_FG,
1948     (prefs.st_server_fg.red * 255 / 65535),
1949     (prefs.st_server_fg.green * 255 / 65535),
1950     (prefs.st_server_fg.blue * 255 / 65535));
1951   fprintf (pf, "%s: %02x%02x%02x\n", PRS_STREAM_SR_BG,
1952     (prefs.st_server_bg.red * 255 / 65535),
1953     (prefs.st_server_bg.green * 255 / 65535),
1954     (prefs.st_server_bg.blue * 255 / 65535));
1955
1956   fprintf(pf, "\n# Vertical scrollbars should be on right side? TRUE/FALSE\n");
1957   fprintf(pf, PRS_GUI_SCROLLBAR_ON_RIGHT ": %s\n",
1958                   prefs.gui_scrollbar_on_right == TRUE ? "TRUE" : "FALSE");
1959
1960   fprintf(pf, "\n# Packet-list selection bar can be used to browse w/o selecting? TRUE/FALSE\n");
1961   fprintf(pf, PRS_GUI_PLIST_SEL_BROWSE ": %s\n",
1962                   prefs.gui_plist_sel_browse == TRUE ? "TRUE" : "FALSE");
1963
1964   fprintf(pf, "\n# Protocol-tree selection bar can be used to browse w/o selecting? TRUE/FALSE\n");
1965   fprintf(pf, PRS_GUI_PTREE_SEL_BROWSE ": %s\n",
1966                   prefs.gui_ptree_sel_browse == TRUE ? "TRUE" : "FALSE");
1967
1968   fprintf(pf, "\n# Alternating colors in TreeViews\n");
1969   fprintf(pf, PRS_GUI_ALTERN_COLORS ": %s\n",
1970                   prefs.gui_altern_colors == TRUE ? "TRUE" : "FALSE");
1971
1972   fprintf(pf, "\n# Protocol-tree line style. One of: NONE, SOLID, DOTTED, TABBED\n");
1973   fprintf(pf, PRS_GUI_PTREE_LINE_STYLE ": %s\n",
1974           gui_ptree_line_style_text[prefs.gui_ptree_line_style]);
1975
1976   fprintf(pf, "\n# Protocol-tree expander style. One of: NONE, SQUARE, TRIANGLE, CIRCULAR\n");
1977   fprintf(pf, PRS_GUI_PTREE_EXPANDER_STYLE ": %s\n",
1978                   gui_ptree_expander_style_text[prefs.gui_ptree_expander_style]);
1979
1980   fprintf(pf, "\n# Hex dump highlight style. One of: BOLD, INVERSE\n");
1981   fprintf(pf, PRS_GUI_HEX_DUMP_HIGHLIGHT_STYLE ": %s\n",
1982                   gui_hex_dump_highlight_style_text[prefs.gui_hex_dump_highlight_style]);
1983
1984   fprintf(pf, "\n# Font name for packet list, protocol tree, and hex dump panes.\n");
1985   fprintf(pf, PRS_GUI_FONT_NAME ": %s\n", prefs.gui_font_name);
1986
1987   fprintf (pf, "\n# Color preferences for a marked frame.  Each value is a six "
1988     "digit hexadecimal value in the form rrggbb.\n");
1989   fprintf (pf, "%s: %02x%02x%02x\n", PRS_GUI_MARKED_FG,
1990     (prefs.gui_marked_fg.red * 255 / 65535),
1991     (prefs.gui_marked_fg.green * 255 / 65535),
1992     (prefs.gui_marked_fg.blue * 255 / 65535));
1993   fprintf (pf, "%s: %02x%02x%02x\n", PRS_GUI_MARKED_BG,
1994     (prefs.gui_marked_bg.red * 255 / 65535),
1995     (prefs.gui_marked_bg.green * 255 / 65535),
1996     (prefs.gui_marked_bg.blue * 255 / 65535));
1997
1998   fprintf(pf, "\n# Save window position at exit? TRUE/FALSE\n");
1999   fprintf(pf, PRS_GUI_GEOMETRY_SAVE_POSITION ": %s\n",
2000                   prefs.gui_geometry_save_position == TRUE ? "TRUE" : "FALSE");
2001
2002   fprintf(pf, "\n# Save window size at exit? TRUE/FALSE\n");
2003   fprintf(pf, PRS_GUI_GEOMETRY_SAVE_SIZE ": %s\n",
2004                   prefs.gui_geometry_save_size == TRUE ? "TRUE" : "FALSE");
2005
2006   fprintf(pf, "\n# Main window geometry. Decimal integers.\n");
2007   fprintf(pf, PRS_GUI_GEOMETRY_MAIN_X ": %d\n", prefs.gui_geometry_main_x);
2008   fprintf(pf, PRS_GUI_GEOMETRY_MAIN_Y ": %d\n", prefs.gui_geometry_main_y);
2009   fprintf(pf, PRS_GUI_GEOMETRY_MAIN_WIDTH ": %d\n",
2010                   prefs.gui_geometry_main_width);
2011   fprintf(pf, PRS_GUI_GEOMETRY_MAIN_HEIGHT ": %d\n",
2012                   prefs.gui_geometry_main_height);
2013
2014   fprintf(pf, "\n# Resolve addresses to names? TRUE/FALSE/{list of address types to resolve}\n");
2015   fprintf(pf, PRS_NAME_RESOLVE ": %s\n",
2016                   name_resolve_to_string(prefs.name_resolve));
2017
2018 /* write the capture options */
2019   if (prefs.capture_device != NULL) {
2020     fprintf(pf, "\n# Default capture device\n");
2021     fprintf(pf, PRS_CAP_DEVICE ": %s\n", prefs.capture_device);
2022   }
2023
2024   fprintf(pf, "\n# Capture in promiscuous mode? TRUE/FALSE\n");
2025   fprintf(pf, PRS_CAP_PROM_MODE ": %s\n",
2026                   prefs.capture_prom_mode == TRUE ? "TRUE" : "FALSE");
2027
2028   fprintf(pf, "\n# Update packet list in real time during capture? TRUE/FALSE\n");
2029   fprintf(pf, PRS_CAP_REAL_TIME ": %s\n",
2030                   prefs.capture_real_time == TRUE ? "TRUE" : "FALSE");
2031
2032   fprintf(pf, "\n# scroll packet list during capture? TRUE/FALSE\n");
2033   fprintf(pf, PRS_CAP_AUTO_SCROLL ": %s\n",
2034                   prefs.capture_auto_scroll == TRUE ? "TRUE" : "FALSE");
2035
2036   g_list_foreach(modules, write_module_prefs, pf);
2037
2038   fclose(pf);
2039
2040   /* XXX - catch I/O errors (e.g. "ran out of disk space") and return
2041      an error indication, or maybe write to a new preferences file and
2042      rename that file on top of the old one only if there are not I/O
2043      errors. */
2044   return 0;
2045 }
2046
2047 /* Copy a set of preferences. */
2048 void
2049 copy_prefs(e_prefs *dest, e_prefs *src)
2050 {
2051   fmt_data *src_cfmt, *dest_cfmt;
2052   GList *entry;
2053
2054   dest->pr_format = src->pr_format;
2055   dest->pr_dest = src->pr_dest;
2056   dest->pr_file = g_strdup(src->pr_file);
2057   dest->pr_cmd = g_strdup(src->pr_cmd);
2058   dest->col_list = NULL;
2059   for (entry = src->col_list; entry != NULL; entry = g_list_next(entry)) {
2060     src_cfmt = entry->data;
2061     dest_cfmt = (fmt_data *) g_malloc(sizeof(fmt_data));
2062     dest_cfmt->title = g_strdup(src_cfmt->title);
2063     dest_cfmt->fmt = g_strdup(src_cfmt->fmt);
2064     dest->col_list = g_list_append(dest->col_list, dest_cfmt);
2065   }
2066   dest->num_cols = src->num_cols;
2067   dest->st_client_fg = src->st_client_fg;
2068   dest->st_client_bg = src->st_client_bg;
2069   dest->st_server_fg = src->st_server_fg;
2070   dest->st_server_bg = src->st_server_bg;
2071   dest->gui_scrollbar_on_right = src->gui_scrollbar_on_right;
2072   dest->gui_plist_sel_browse = src->gui_plist_sel_browse;
2073   dest->gui_ptree_sel_browse = src->gui_ptree_sel_browse;
2074   dest->gui_altern_colors = src->gui_altern_colors;
2075   dest->gui_ptree_line_style = src->gui_ptree_line_style;
2076   dest->gui_ptree_expander_style = src->gui_ptree_expander_style;
2077   dest->gui_hex_dump_highlight_style = src->gui_hex_dump_highlight_style;
2078   dest->gui_font_name = g_strdup(src->gui_font_name);
2079   dest->gui_marked_fg = src->gui_marked_fg;
2080   dest->gui_marked_bg = src->gui_marked_bg;
2081   dest->gui_geometry_save_position = src->gui_geometry_save_position;
2082   dest->gui_geometry_save_size = src->gui_geometry_save_size;
2083   dest->gui_geometry_main_x = src->gui_geometry_main_x;
2084   dest->gui_geometry_main_y = src->gui_geometry_main_y;
2085   dest->gui_geometry_main_width = src->gui_geometry_main_width;
2086   dest->gui_geometry_main_height = src->gui_geometry_main_height;
2087 /*  values for the capture dialog box */
2088   dest->capture_device = g_strdup(src->capture_device);
2089   dest->capture_prom_mode = src->capture_prom_mode;
2090   dest->capture_real_time = src->capture_real_time;
2091   dest->capture_auto_scroll = src->capture_auto_scroll;
2092   dest->name_resolve = src->name_resolve;
2093
2094 }
2095
2096 /* Free a set of preferences. */
2097 void
2098 free_prefs(e_prefs *pr)
2099 {
2100   if (pr->pr_file != NULL) {
2101     g_free(pr->pr_file);
2102     pr->pr_file = NULL;
2103   }
2104   if (pr->pr_cmd != NULL) {
2105     g_free(pr->pr_cmd);
2106     pr->pr_cmd = NULL;
2107   }
2108   free_col_info(pr);
2109   if (pr->gui_font_name != NULL) {
2110     g_free(pr->gui_font_name);
2111     pr->gui_font_name = NULL;
2112   }
2113   if (pr->capture_device != NULL) {
2114     g_free(pr->capture_device);
2115     pr->capture_device = NULL;
2116   }
2117 }
2118
2119 static void
2120 free_col_info(e_prefs *pr)
2121 {
2122   fmt_data *cfmt;
2123
2124   while (pr->col_list != NULL) {
2125     cfmt = pr->col_list->data;
2126     g_free(cfmt->title);
2127     g_free(cfmt->fmt);
2128     g_free(cfmt);
2129     pr->col_list = g_list_remove_link(pr->col_list, pr->col_list);
2130   }
2131   g_list_free(pr->col_list);
2132   pr->col_list = NULL;
2133 }