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