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