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