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