Added "Cisco Discovery Protocol" Hooks
[obnox/wireshark/wip.git] / prefs.c
1 /* prefs.c
2  * Routines for handling preferences
3  *
4  * $Id: prefs.c,v 1.13 1998/12/17 05:42:29 gram Exp $
5  *
6  * Ethereal - Network traffic analyzer
7  * By Gerald Combs <gerald@zing.org>
8  * Copyright 1998 Gerald Combs
9  *
10  * 
11  * This program is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU General Public License
13  * as published by the Free Software Foundation; either version 2
14  * of the License, or (at your option) any later version.
15  * 
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  * 
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software
23  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
24  */
25
26 #ifdef HAVE_CONFIG_H
27 # include "config.h"
28 #endif
29
30 #ifdef HAVE_SYS_TYPES_H
31 #include <sys/types.h>
32 #endif
33
34 #include <gtk/gtk.h>
35
36 #include <stdlib.h>
37 #include <ctype.h>
38 #include <errno.h>
39 #include <unistd.h>
40 #include <sys/stat.h>
41
42 #include "ethereal.h"
43 #include "packet.h"
44 #include "file.h"
45 #include "prefs.h"
46 #include "column.h"
47 #include "print.h"
48 #include "filter.h"
49 #include "util.h"
50
51 /* Internal functions */
52 static int    set_pref(gchar*, gchar*);
53 static void   write_prefs();
54 static void   prefs_main_ok_cb(GtkWidget *, gpointer);
55 static void   prefs_main_save_cb(GtkWidget *, gpointer);
56 static void   prefs_main_cancel_cb(GtkWidget *, gpointer);
57 static GList *get_string_list(gchar *);
58 static void   clear_string_list(GList *);
59
60 e_prefs prefs;
61 static int init_prefs = 1;
62
63 #define PF_NAME "preferences"
64
65 #define E_PRINT_PAGE_KEY  "printer_options_page"
66 #define E_FILTER_PAGE_KEY "filter_options_page"
67 #define E_COLUMN_PAGE_KEY "column_options_page"
68
69 static gchar *pf_path = NULL;
70
71 void
72 prefs_cb(GtkWidget *w, gpointer sp) {
73   GtkWidget *prefs_w, *main_vb, *top_hb, *bbox, *prefs_nb,
74             *ok_bt, *save_bt, *cancel_bt;
75   GtkWidget *print_pg, *filter_pg, *column_pg, *filter_te, *label;
76 /*  GtkWidget *nlabel; */
77   gint       start_page = (gint) sp;
78
79   prefs_w = gtk_window_new(GTK_WINDOW_TOPLEVEL);
80   gtk_window_set_title(GTK_WINDOW(prefs_w), "Ethereal: Preferences");
81   
82   /* Container for each row of widgets */
83   main_vb = gtk_vbox_new(FALSE, 5);
84   gtk_container_border_width(GTK_CONTAINER(main_vb), 5);
85   gtk_container_add(GTK_CONTAINER(prefs_w), main_vb);
86   gtk_widget_show(main_vb);
87   
88   /* Top row: Preferences notebook */
89   top_hb = gtk_hbox_new(FALSE, 1);
90   gtk_container_add(GTK_CONTAINER(main_vb), top_hb);
91   gtk_widget_show(top_hb);
92   
93   prefs_nb = gtk_notebook_new();
94   gtk_container_add(GTK_CONTAINER(main_vb), prefs_nb);
95   gtk_widget_show(prefs_nb);
96   
97   /* General prefs */
98 /*   nlabel = gtk_label_new("Nothing here yet...");
99   gtk_widget_show (nlabel);
100
101   label = gtk_label_new ("General");
102   gtk_notebook_append_page (GTK_NOTEBOOK(prefs_nb), nlabel, label);
103  */  
104   /* Printing prefs */
105   print_pg = printer_prefs_show();
106   gtk_object_set_data(GTK_OBJECT(prefs_w), E_PRINT_PAGE_KEY, print_pg);
107   label = gtk_label_new ("Printing");
108   gtk_notebook_append_page (GTK_NOTEBOOK(prefs_nb), print_pg, label);
109     
110   /* Filter prefs */
111   filter_te = gtk_object_get_data(GTK_OBJECT(w), E_FILT_TE_PTR_KEY);
112   filter_pg = filter_prefs_show(filter_te);
113 #ifdef WITH_WIRETAP
114   gtk_widget_set_sensitive(filter_pg, FALSE);
115 #endif
116   
117   /* Pass along the entry widget pointer from the calling widget */
118   gtk_object_set_data(GTK_OBJECT(filter_pg), E_FILT_TE_PTR_KEY, filter_te);
119   gtk_object_set_data(GTK_OBJECT(prefs_w), E_FILTER_PAGE_KEY, filter_pg);
120   label = gtk_label_new ("Filters");
121   gtk_notebook_append_page (GTK_NOTEBOOK(prefs_nb), filter_pg, label);
122
123   /* Column prefs */
124   column_pg = column_prefs_show();
125   gtk_object_set_data(GTK_OBJECT(prefs_w), E_COLUMN_PAGE_KEY, column_pg);
126   label = gtk_label_new ("Columns");
127   gtk_notebook_append_page (GTK_NOTEBOOK(prefs_nb), column_pg, label);
128   
129   /* Jump to the specified page, if it was supplied */
130   if (start_page > E_PR_PG_NONE)
131     gtk_notebook_set_page(GTK_NOTEBOOK(prefs_nb), start_page);
132     
133   /* Button row: OK and cancel buttons */
134   bbox = gtk_hbutton_box_new();
135   gtk_button_box_set_layout (GTK_BUTTON_BOX (bbox), GTK_BUTTONBOX_END);
136   gtk_button_box_set_spacing(GTK_BUTTON_BOX(bbox), 5);
137   gtk_container_add(GTK_CONTAINER(main_vb), bbox);
138   gtk_widget_show(bbox);
139   
140   ok_bt = gtk_button_new_with_label ("OK");
141   gtk_signal_connect_object(GTK_OBJECT(ok_bt), "clicked",
142     GTK_SIGNAL_FUNC(prefs_main_ok_cb), GTK_OBJECT(prefs_w));
143   GTK_WIDGET_SET_FLAGS(ok_bt, GTK_CAN_DEFAULT);
144   gtk_box_pack_start (GTK_BOX (bbox), ok_bt, TRUE, TRUE, 0);
145   gtk_widget_grab_default(ok_bt);
146   gtk_widget_show(ok_bt);
147
148   save_bt = gtk_button_new_with_label ("Save");
149   gtk_signal_connect_object(GTK_OBJECT(save_bt), "clicked",
150     GTK_SIGNAL_FUNC(prefs_main_save_cb), GTK_OBJECT(prefs_w));
151   GTK_WIDGET_SET_FLAGS(save_bt, GTK_CAN_DEFAULT);
152   gtk_box_pack_start (GTK_BOX (bbox), save_bt, TRUE, TRUE, 0);
153   gtk_widget_show(save_bt);
154   
155   cancel_bt = gtk_button_new_with_label ("Cancel");
156   gtk_signal_connect_object(GTK_OBJECT(cancel_bt), "clicked",
157     GTK_SIGNAL_FUNC(prefs_main_cancel_cb), GTK_OBJECT(prefs_w));
158   GTK_WIDGET_SET_FLAGS(cancel_bt, GTK_CAN_DEFAULT);
159   gtk_box_pack_start (GTK_BOX (bbox), cancel_bt, TRUE, TRUE, 0);
160   gtk_widget_show(cancel_bt);
161
162   gtk_widget_show(prefs_w);
163 }
164
165 void
166 prefs_main_ok_cb(GtkWidget *w, gpointer win) {
167   
168   printer_prefs_ok(gtk_object_get_data(GTK_OBJECT(win), E_PRINT_PAGE_KEY));
169   filter_prefs_ok(gtk_object_get_data(GTK_OBJECT(win), E_FILTER_PAGE_KEY));
170   column_prefs_ok(gtk_object_get_data(GTK_OBJECT(win), E_COLUMN_PAGE_KEY));
171   gtk_widget_destroy(GTK_WIDGET(win));
172 }
173
174 void
175 prefs_main_save_cb(GtkWidget *w, gpointer win) {
176   printer_prefs_save(gtk_object_get_data(GTK_OBJECT(win), E_PRINT_PAGE_KEY));
177   filter_prefs_save(gtk_object_get_data(GTK_OBJECT(win), E_FILTER_PAGE_KEY));
178   column_prefs_save(gtk_object_get_data(GTK_OBJECT(win), E_COLUMN_PAGE_KEY));
179   write_prefs();
180 }
181
182 void
183 prefs_main_cancel_cb(GtkWidget *w, gpointer win) {
184
185   printer_prefs_cancel(gtk_object_get_data(GTK_OBJECT(win), E_PRINT_PAGE_KEY));
186   filter_prefs_cancel(gtk_object_get_data(GTK_OBJECT(win), E_FILTER_PAGE_KEY));
187   column_prefs_cancel(gtk_object_get_data(GTK_OBJECT(win), E_COLUMN_PAGE_KEY));
188   gtk_widget_destroy(GTK_WIDGET(win));
189 }
190
191 /* Parse through a list of comma-separated, quoted strings.  Return a
192    list of the string data */
193 static GList *
194 get_string_list(gchar *str) {
195   enum { PRE_QUOT, IN_QUOT, POST_QUOT };
196
197   gint      state = PRE_QUOT, i = 0, j = 0;
198   gboolean  backslash = FALSE;
199   gchar     cur_c, *slstr = NULL;
200   GList    *sl = NULL;
201   
202   while ((cur_c = str[i]) != '\0') {
203     if (cur_c == '"' && ! backslash) {
204       switch (state) {
205         case PRE_QUOT:
206           state = IN_QUOT;
207           slstr = (gchar *) g_malloc(sizeof(gchar) * COL_MAX_LEN);
208           j = 0;
209           break;
210         case IN_QUOT:
211           state  = POST_QUOT;
212           slstr[j] = '\0';
213           sl = g_list_append(sl, slstr);
214           break;
215         case POST_QUOT:
216           clear_string_list(sl);
217           return NULL;
218           break;
219         default:
220           break;
221       }
222     } else if (cur_c == '\\' && ! backslash) {
223       backslash = TRUE;
224     } else if (cur_c == ',' && state == POST_QUOT) {
225       state = PRE_QUOT;
226     } else if (state == IN_QUOT && j < COL_MAX_LEN) {
227       slstr[j] = str[i];
228       j++;
229     }
230     i++;
231   }
232   if (state != POST_QUOT) {
233     clear_string_list(sl);
234   }
235   return(sl);
236 }
237
238 void
239 clear_string_list(GList *sl) {
240   GList *l = sl;
241   
242   while (l) {
243     g_free(l->data);
244     l = g_list_remove_link(l, l);
245   }
246 }
247
248 /* Preferences file format:
249  * - Configuration directives start at the beginning of the line, and 
250  *   are terminated with a colon.
251  * - Directives can be continued on the next line by preceding them with
252  *   whitespace.
253  *
254  * Example:
255
256 # This is a comment line
257 print.command: lpr
258 print.file: /a/very/long/path/
259         to/ethereal-out.ps
260  *
261  */
262
263 #define MAX_VAR_LEN    32
264 #define MAX_VAL_LEN  1024
265 #define DEF_NUM_COLS    6
266 e_prefs *
267 read_prefs() {
268   enum { START, IN_VAR, PRE_VAL, IN_VAL, IN_SKIP };
269   FILE     *pf;
270   gchar     cur_var[MAX_VAR_LEN], cur_val[MAX_VAL_LEN];
271   int       got_c, state = START, i;
272   gint      var_len = 0, val_len = 0, fline = 1, pline = 1;
273   gboolean  got_val = FALSE;
274   fmt_data *cfmt;
275   gchar    *col_fmt[] = {"No.",      "%m", "Time",        "%t",
276                          "Source",   "%s", "Destination", "%d",
277                          "Protocol", "%p", "Info",        "%i"};
278
279   
280   /* Initialize preferences.  With any luck, these values will be
281      overwritten below. */
282   if (init_prefs) {
283     init_prefs       = 0;
284     prefs.pr_format  = PR_FMT_TEXT;
285     prefs.pr_dest    = PR_DEST_CMD;
286     prefs.pr_file    = g_strdup("ethereal.out");
287     prefs.pr_cmd     = g_strdup("lpr");
288     prefs.col_list = NULL;
289     for (i = 0; i < DEF_NUM_COLS; i++) {
290       cfmt = (fmt_data *) g_malloc(sizeof(fmt_data));
291       cfmt->title = g_strdup(col_fmt[i * 2]);
292       cfmt->fmt   = g_strdup(col_fmt[(i * 2) + 1]);
293       prefs.col_list = g_list_append(prefs.col_list, cfmt);
294     }
295     prefs.num_cols  = DEF_NUM_COLS;
296   }
297
298   if (! pf_path) {
299     pf_path = (gchar *) g_malloc(strlen(getenv("HOME")) + strlen(PF_DIR) +
300       strlen(PF_NAME) + 4);
301     sprintf(pf_path, "%s/%s/%s", getenv("HOME"), PF_DIR, PF_NAME);
302   }
303     
304   if ((pf = fopen(pf_path, "r")) == NULL) {
305     if (errno != ENOENT) {
306       simple_dialog(ESD_TYPE_WARN, NULL,
307         "Can't open preferences file\n\"%s\".", pf_path);
308     }
309     return &prefs;
310   }
311     
312   while ((got_c = getc(pf)) != EOF) {
313     if (got_c == '\n') {
314       state = START;
315       fline++;
316       continue;
317     }
318     if (var_len >= MAX_VAR_LEN) {
319       g_warning ("%s line %d: Variable too long", pf_path, fline);
320       state = IN_SKIP;
321       var_len = 0;
322       continue;
323     }
324     if (val_len >= MAX_VAL_LEN) {
325       g_warning ("%s line %d: Value too long", pf_path, fline);
326       state = IN_SKIP;
327       var_len = 0;
328       continue;
329     }
330     
331     switch (state) {
332       case START:
333         if (isalnum(got_c)) {
334           if (var_len > 0) {
335             if (got_val) {
336               cur_var[var_len] = '\0';
337               cur_val[val_len] = '\0';
338               if (! set_pref(cur_var, cur_val))
339                 g_warning ("%s line %d: Bogus preference", pf_path, pline);
340             } else {
341               g_warning ("%s line %d: Incomplete preference", pf_path, pline);
342             }
343           }
344           state      = IN_VAR;
345           got_val    = FALSE;
346           cur_var[0] = got_c;
347           var_len    = 1;
348           pline = fline;
349         } else if (isspace(got_c) && var_len > 0 && got_val) {
350           state = PRE_VAL;
351         } else if (got_c == '#') {
352           state = IN_SKIP;
353         } else {
354           g_warning ("%s line %d: Malformed line", pf_path, fline);
355         }
356         break;
357       case IN_VAR:
358         if (got_c != ':') {
359           cur_var[var_len] = got_c;
360           var_len++;
361         } else {
362           state   = PRE_VAL;
363           val_len = 0;
364           got_val = TRUE;
365         }
366         break;
367       case PRE_VAL:
368         if (!isspace(got_c)) {
369           state = IN_VAL;
370           cur_val[val_len] = got_c;
371           val_len++;
372         }
373         break;
374       case IN_VAL:
375         if (got_c != '#')  {
376           cur_val[val_len] = got_c;
377           val_len++;
378         } else {
379           while (isspace(cur_val[val_len]) && val_len > 0)
380             val_len--;
381           state = IN_SKIP;
382         }
383         break;
384     }
385   }
386   if (var_len > 0) {
387     if (got_val) {
388       cur_var[var_len] = '\0';
389       cur_val[val_len] = '\0';
390       if (! set_pref(cur_var, cur_val))
391         g_warning ("%s line %d: Bogus preference", pf_path, pline);
392     } else {
393       g_warning ("%s line %d: Incomplete preference", pf_path, pline);
394     }
395   }
396   fclose(pf);
397   
398   return &prefs;
399 }
400
401 #define PRS_PRINT_FMT  "print.format"
402 #define PRS_PRINT_DEST "print.destination"
403 #define PRS_PRINT_FILE "print.file"
404 #define PRS_PRINT_CMD  "print.command"
405 #define PRS_COL_FMT    "column.format"
406
407 static gchar *pr_formats[] = { "text", "postscript" };
408 static gchar *pr_dests[]   = { "command", "file" };
409
410 int
411 set_pref(gchar *pref, gchar *value) {
412   GList    *col_l;
413   gint      llen;
414   fmt_data *cfmt;
415
416   if (strcmp(pref, PRS_PRINT_FMT) == 0) {
417     if (strcmp(value, pr_formats[PR_FMT_TEXT]) == 0) {
418       prefs.pr_format = PR_FMT_TEXT;
419     } else if (strcmp(value, pr_formats[PR_FMT_PS]) == 0) {
420       prefs.pr_format = PR_FMT_PS;
421     } else {
422       return 0;
423     }
424   } else if (strcmp(pref, PRS_PRINT_DEST) == 0) {
425     if (strcmp(value, pr_dests[PR_DEST_CMD]) == 0) {
426       prefs.pr_dest = PR_DEST_CMD;
427     } else if (strcmp(value, pr_dests[PR_DEST_FILE]) == 0) {
428       prefs.pr_dest = PR_DEST_FILE;
429     } else {
430       return 0;
431     }
432   } else if (strcmp(pref, PRS_PRINT_FILE) == 0) {
433     if (prefs.pr_file) g_free(prefs.pr_file);
434     prefs.pr_file = g_strdup(value);
435   } else if (strcmp(pref, PRS_PRINT_CMD) == 0) {
436     if (prefs.pr_cmd) g_free(prefs.pr_cmd);
437     prefs.pr_cmd = g_strdup(value);
438   } else if (strcmp(pref, PRS_COL_FMT) == 0) {
439     if ((col_l = get_string_list(value)) && (g_list_length(col_l) % 2) == 0) {
440       while (prefs.col_list) {
441         cfmt = prefs.col_list->data;
442         g_free(cfmt->title);
443         g_free(cfmt->fmt);
444         g_free(cfmt);
445         prefs.col_list = g_list_remove_link(prefs.col_list, prefs.col_list);
446       }
447       llen             = g_list_length(col_l);
448       prefs.num_cols   = llen / 2;
449       col_l = g_list_first(col_l);
450       while(col_l) {
451         cfmt = (fmt_data *) g_malloc(sizeof(fmt_data));
452         cfmt->title    = g_strdup(col_l->data);
453         col_l          = col_l->next;
454         cfmt->fmt      = g_strdup(col_l->data);
455         col_l          = col_l->next;
456         prefs.col_list = g_list_append(prefs.col_list, cfmt);
457       }
458       /* To do: else print some sort of error? */
459     }
460     clear_string_list(col_l);
461   } else {
462     return 0;
463   }
464   
465   return 1;
466 }
467
468 void
469 write_prefs() {
470   FILE        *pf;
471   struct stat  s_buf;
472   
473   /* To do:
474    * - Split output lines longer than MAX_VAL_LEN
475    * - Create a function for the preference directory check/creation
476    *   so that duplication can be avoided with filter.c
477    */
478
479   if (! pf_path) {
480     pf_path = (gchar *) g_malloc(strlen(getenv("HOME")) + strlen(PF_DIR) +
481       strlen(PF_NAME) + 4);
482   }
483
484   sprintf(pf_path, "%s/%s", getenv("HOME"), PF_DIR);
485   if (stat(pf_path, &s_buf) != 0)
486     mkdir(pf_path, 0755);
487
488   sprintf(pf_path, "%s/%s/%s", getenv("HOME"), PF_DIR, PF_NAME);
489   if ((pf = fopen(pf_path, "w")) == NULL) {
490      simple_dialog(ESD_TYPE_WARN, NULL,
491       "Can't open preferences file\n\"%s\".", pf_path);
492    return;
493  }
494     
495   fputs("# Configuration file for Ethereal " VERSION ".\n"
496     "#\n"
497     "# This file is regenerated each time preferences are saved within\n"
498     "# Ethereal.  Making manual changes should be safe, however.\n"
499     "\n"
500     "######## Printing ########\n"
501     "\n", pf);
502
503   fprintf (pf, "# Can be one of \"text\" or \"postscript\".\n"
504     "print.format: %s\n\n", pr_formats[prefs.pr_format]);
505
506   fprintf (pf, "# Can be one of \"command\" or \"file\".\n"
507     "print.destination: %s\n\n", pr_dests[prefs.pr_dest]);
508
509   fprintf (pf, "# This is the file that gets written to when the "
510     "destination is set to \"file\"\n"
511     "%s: %s\n\n", PRS_PRINT_FILE, prefs.pr_file);
512
513   fprintf (pf, "# Output gets piped to this command when the destination "
514     "is set to \"command\"\n"
515     "%s: %s\n\n", PRS_PRINT_CMD, prefs.pr_cmd);
516
517   fprintf (pf, "# Packet list column format.  Each pair of strings consists "
518     "of a column title \n# and its format.\n"
519     "%s: %s\n\n", PRS_COL_FMT, col_format_to_pref_str());
520
521   fclose(pf);
522 }