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