2 * Routines for handling preferences
4 * $Id: prefs.c,v 1.12 1998/11/18 03:17:18 gerald Exp $
6 * Ethereal - Network traffic analyzer
7 * By Gerald Combs <gerald@zing.org>
8 * Copyright 1998 Gerald Combs
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.
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.
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.
30 #ifdef HAVE_SYS_TYPES_H
31 #include <sys/types.h>
50 /* Internal functions */
51 static int set_pref(gchar*, gchar*);
52 static void write_prefs();
53 static void prefs_main_ok_cb(GtkWidget *, gpointer);
54 static void prefs_main_save_cb(GtkWidget *, gpointer);
55 static void prefs_main_cancel_cb(GtkWidget *, gpointer);
56 static GList *get_string_list(gchar *);
57 static void clear_string_list(GList *);
60 static int init_prefs = 1;
62 #define PF_NAME "preferences"
64 #define E_PRINT_PAGE_KEY "printer_options_page"
65 #define E_FILTER_PAGE_KEY "filter_options_page"
66 #define E_COLUMN_PAGE_KEY "column_options_page"
68 static gchar *pf_path = NULL;
71 prefs_cb(GtkWidget *w, gpointer sp) {
72 GtkWidget *prefs_w, *main_vb, *top_hb, *bbox, *prefs_nb,
73 *ok_bt, *save_bt, *cancel_bt;
74 GtkWidget *print_pg, *filter_pg, *column_pg, *filter_te, *label;
75 /* GtkWidget *nlabel; */
76 gint start_page = (gint) sp;
78 prefs_w = gtk_window_new(GTK_WINDOW_TOPLEVEL);
79 gtk_window_set_title(GTK_WINDOW(prefs_w), "Ethereal: Preferences");
81 /* Container for each row of widgets */
82 main_vb = gtk_vbox_new(FALSE, 5);
83 gtk_container_border_width(GTK_CONTAINER(main_vb), 5);
84 gtk_container_add(GTK_CONTAINER(prefs_w), main_vb);
85 gtk_widget_show(main_vb);
87 /* Top row: Preferences notebook */
88 top_hb = gtk_hbox_new(FALSE, 1);
89 gtk_container_add(GTK_CONTAINER(main_vb), top_hb);
90 gtk_widget_show(top_hb);
92 prefs_nb = gtk_notebook_new();
93 gtk_container_add(GTK_CONTAINER(main_vb), prefs_nb);
94 gtk_widget_show(prefs_nb);
97 /* nlabel = gtk_label_new("Nothing here yet...");
98 gtk_widget_show (nlabel);
100 label = gtk_label_new ("General");
101 gtk_notebook_append_page (GTK_NOTEBOOK(prefs_nb), nlabel, label);
104 print_pg = printer_prefs_show();
105 gtk_object_set_data(GTK_OBJECT(prefs_w), E_PRINT_PAGE_KEY, print_pg);
106 label = gtk_label_new ("Printing");
107 gtk_notebook_append_page (GTK_NOTEBOOK(prefs_nb), print_pg, label);
110 filter_te = gtk_object_get_data(GTK_OBJECT(w), E_FILT_TE_PTR_KEY);
111 filter_pg = filter_prefs_show(filter_te);
113 gtk_widget_set_sensitive(filter_pg, FALSE);
116 /* Pass along the entry widget pointer from the calling widget */
117 gtk_object_set_data(GTK_OBJECT(filter_pg), E_FILT_TE_PTR_KEY, filter_te);
118 gtk_object_set_data(GTK_OBJECT(prefs_w), E_FILTER_PAGE_KEY, filter_pg);
119 label = gtk_label_new ("Filters");
120 gtk_notebook_append_page (GTK_NOTEBOOK(prefs_nb), filter_pg, label);
123 column_pg = column_prefs_show();
124 gtk_object_set_data(GTK_OBJECT(prefs_w), E_COLUMN_PAGE_KEY, column_pg);
125 label = gtk_label_new ("Columns");
126 gtk_notebook_append_page (GTK_NOTEBOOK(prefs_nb), column_pg, label);
128 /* Jump to the specified page, if it was supplied */
129 if (start_page > E_PR_PG_NONE)
130 gtk_notebook_set_page(GTK_NOTEBOOK(prefs_nb), start_page);
132 /* Button row: OK and cancel buttons */
133 bbox = gtk_hbutton_box_new();
134 gtk_button_box_set_layout (GTK_BUTTON_BOX (bbox), GTK_BUTTONBOX_END);
135 gtk_button_box_set_spacing(GTK_BUTTON_BOX(bbox), 5);
136 gtk_container_add(GTK_CONTAINER(main_vb), bbox);
137 gtk_widget_show(bbox);
139 ok_bt = gtk_button_new_with_label ("OK");
140 gtk_signal_connect_object(GTK_OBJECT(ok_bt), "clicked",
141 GTK_SIGNAL_FUNC(prefs_main_ok_cb), GTK_OBJECT(prefs_w));
142 GTK_WIDGET_SET_FLAGS(ok_bt, GTK_CAN_DEFAULT);
143 gtk_box_pack_start (GTK_BOX (bbox), ok_bt, TRUE, TRUE, 0);
144 gtk_widget_grab_default(ok_bt);
145 gtk_widget_show(ok_bt);
147 save_bt = gtk_button_new_with_label ("Save");
148 gtk_signal_connect_object(GTK_OBJECT(save_bt), "clicked",
149 GTK_SIGNAL_FUNC(prefs_main_save_cb), GTK_OBJECT(prefs_w));
150 GTK_WIDGET_SET_FLAGS(save_bt, GTK_CAN_DEFAULT);
151 gtk_box_pack_start (GTK_BOX (bbox), save_bt, TRUE, TRUE, 0);
152 gtk_widget_show(save_bt);
154 cancel_bt = gtk_button_new_with_label ("Cancel");
155 gtk_signal_connect_object(GTK_OBJECT(cancel_bt), "clicked",
156 GTK_SIGNAL_FUNC(prefs_main_cancel_cb), GTK_OBJECT(prefs_w));
157 GTK_WIDGET_SET_FLAGS(cancel_bt, GTK_CAN_DEFAULT);
158 gtk_box_pack_start (GTK_BOX (bbox), cancel_bt, TRUE, TRUE, 0);
159 gtk_widget_show(cancel_bt);
161 gtk_widget_show(prefs_w);
165 prefs_main_ok_cb(GtkWidget *w, gpointer win) {
167 printer_prefs_ok(gtk_object_get_data(GTK_OBJECT(win), E_PRINT_PAGE_KEY));
168 filter_prefs_ok(gtk_object_get_data(GTK_OBJECT(win), E_FILTER_PAGE_KEY));
169 column_prefs_ok(gtk_object_get_data(GTK_OBJECT(win), E_COLUMN_PAGE_KEY));
170 gtk_widget_destroy(GTK_WIDGET(win));
174 prefs_main_save_cb(GtkWidget *w, gpointer win) {
175 printer_prefs_save(gtk_object_get_data(GTK_OBJECT(win), E_PRINT_PAGE_KEY));
176 filter_prefs_save(gtk_object_get_data(GTK_OBJECT(win), E_FILTER_PAGE_KEY));
177 column_prefs_save(gtk_object_get_data(GTK_OBJECT(win), E_COLUMN_PAGE_KEY));
182 prefs_main_cancel_cb(GtkWidget *w, gpointer win) {
184 printer_prefs_cancel(gtk_object_get_data(GTK_OBJECT(win), E_PRINT_PAGE_KEY));
185 filter_prefs_cancel(gtk_object_get_data(GTK_OBJECT(win), E_FILTER_PAGE_KEY));
186 column_prefs_cancel(gtk_object_get_data(GTK_OBJECT(win), E_COLUMN_PAGE_KEY));
187 gtk_widget_destroy(GTK_WIDGET(win));
190 /* Parse through a list of comma-separated, quoted strings. Return a
191 list of the string data */
193 get_string_list(gchar *str) {
194 enum { PRE_QUOT, IN_QUOT, POST_QUOT };
196 gint state = PRE_QUOT, i = 0, j = 0;
197 gboolean backslash = FALSE;
198 gchar cur_c, *slstr = NULL;
201 while ((cur_c = str[i]) != '\0') {
202 if (cur_c == '"' && ! backslash) {
206 slstr = (gchar *) g_malloc(sizeof(gchar) * COL_MAX_LEN);
212 sl = g_list_append(sl, slstr);
215 clear_string_list(sl);
221 } else if (cur_c == '\\' && ! backslash) {
223 } else if (cur_c == ',' && state == POST_QUOT) {
225 } else if (state == IN_QUOT && j < COL_MAX_LEN) {
231 if (state != POST_QUOT) {
232 clear_string_list(sl);
238 clear_string_list(GList *sl) {
243 l = g_list_remove_link(l, l);
247 /* Preferences file format:
248 * - Configuration directives start at the beginning of the line, and
249 * are terminated with a colon.
250 * - Directives can be continued on the next line by preceding them with
255 # This is a comment line
257 print.file: /a/very/long/path/
262 #define MAX_VAR_LEN 32
263 #define MAX_VAL_LEN 1024
264 #define DEF_NUM_COLS 6
267 enum { START, IN_VAR, PRE_VAL, IN_VAL, IN_SKIP };
269 gchar cur_var[MAX_VAR_LEN], cur_val[MAX_VAL_LEN];
270 int got_c, state = START, i;
271 gint var_len = 0, val_len = 0, fline = 1, pline = 1;
272 gboolean got_val = FALSE;
274 gchar *col_fmt[] = {"No.", "%m", "Time", "%t",
275 "Source", "%s", "Destination", "%d",
276 "Protocol", "%p", "Info", "%i"};
279 /* Initialize preferences. With any luck, these values will be
280 overwritten below. */
283 prefs.pr_format = PR_FMT_TEXT;
284 prefs.pr_dest = PR_DEST_CMD;
285 prefs.pr_file = g_strdup("ethereal.out");
286 prefs.pr_cmd = g_strdup("lpr");
287 prefs.col_list = NULL;
288 for (i = 0; i < DEF_NUM_COLS; i++) {
289 cfmt = (fmt_data *) g_malloc(sizeof(fmt_data));
290 cfmt->title = g_strdup(col_fmt[i * 2]);
291 cfmt->fmt = g_strdup(col_fmt[(i * 2) + 1]);
292 prefs.col_list = g_list_append(prefs.col_list, cfmt);
294 prefs.num_cols = DEF_NUM_COLS;
298 pf_path = (gchar *) g_malloc(strlen(getenv("HOME")) + strlen(PF_DIR) +
299 strlen(PF_NAME) + 4);
300 sprintf(pf_path, "%s/%s/%s", getenv("HOME"), PF_DIR, PF_NAME);
303 if ((pf = fopen(pf_path, "r")) == NULL) {
304 if (errno != ENOENT) {
305 simple_dialog(ESD_TYPE_WARN, NULL,
306 "Can't open preferences file\n\"%s\".", pf_path);
311 while ((got_c = getc(pf)) != EOF) {
317 if (var_len >= MAX_VAR_LEN) {
318 g_warning ("%s line %d: Variable too long", pf_path, fline);
323 if (val_len >= MAX_VAL_LEN) {
324 g_warning ("%s line %d: Value too long", pf_path, fline);
332 if (isalnum(got_c)) {
335 cur_var[var_len] = '\0';
336 cur_val[val_len] = '\0';
337 if (! set_pref(cur_var, cur_val))
338 g_warning ("%s line %d: Bogus preference", pf_path, pline);
340 g_warning ("%s line %d: Incomplete preference", pf_path, pline);
348 } else if (isspace(got_c) && var_len > 0 && got_val) {
350 } else if (got_c == '#') {
353 g_warning ("%s line %d: Malformed line", pf_path, fline);
358 cur_var[var_len] = got_c;
367 if (!isspace(got_c)) {
369 cur_val[val_len] = got_c;
375 cur_val[val_len] = got_c;
378 while (isspace(cur_val[val_len]) && val_len > 0)
387 cur_var[var_len] = '\0';
388 cur_val[val_len] = '\0';
389 if (! set_pref(cur_var, cur_val))
390 g_warning ("%s line %d: Bogus preference", pf_path, pline);
392 g_warning ("%s line %d: Incomplete preference", pf_path, pline);
400 #define PRS_PRINT_FMT "print.format"
401 #define PRS_PRINT_DEST "print.destination"
402 #define PRS_PRINT_FILE "print.file"
403 #define PRS_PRINT_CMD "print.command"
404 #define PRS_COL_FMT "column.format"
406 static gchar *pr_formats[] = { "text", "postscript" };
407 static gchar *pr_dests[] = { "command", "file" };
410 set_pref(gchar *pref, gchar *value) {
415 if (strcmp(pref, PRS_PRINT_FMT) == 0) {
416 if (strcmp(value, pr_formats[PR_FMT_TEXT]) == 0) {
417 prefs.pr_format = PR_FMT_TEXT;
418 } else if (strcmp(value, pr_formats[PR_FMT_PS]) == 0) {
419 prefs.pr_format = PR_FMT_PS;
423 } else if (strcmp(pref, PRS_PRINT_DEST) == 0) {
424 if (strcmp(value, pr_dests[PR_DEST_CMD]) == 0) {
425 prefs.pr_dest = PR_DEST_CMD;
426 } else if (strcmp(value, pr_dests[PR_DEST_FILE]) == 0) {
427 prefs.pr_dest = PR_DEST_FILE;
431 } else if (strcmp(pref, PRS_PRINT_FILE) == 0) {
432 if (prefs.pr_file) g_free(prefs.pr_file);
433 prefs.pr_file = g_strdup(value);
434 } else if (strcmp(pref, PRS_PRINT_CMD) == 0) {
435 if (prefs.pr_cmd) g_free(prefs.pr_cmd);
436 prefs.pr_cmd = g_strdup(value);
437 } else if (strcmp(pref, PRS_COL_FMT) == 0) {
438 if ((col_l = get_string_list(value)) && (g_list_length(col_l) % 2) == 0) {
439 while (prefs.col_list) {
440 cfmt = prefs.col_list->data;
444 prefs.col_list = g_list_remove_link(prefs.col_list, prefs.col_list);
446 llen = g_list_length(col_l);
447 prefs.num_cols = llen / 2;
448 col_l = g_list_first(col_l);
450 cfmt = (fmt_data *) g_malloc(sizeof(fmt_data));
451 cfmt->title = g_strdup(col_l->data);
453 cfmt->fmt = g_strdup(col_l->data);
455 prefs.col_list = g_list_append(prefs.col_list, cfmt);
457 /* To do: else print some sort of error? */
459 clear_string_list(col_l);
473 * - Split output lines longer than MAX_VAL_LEN
474 * - Create a function for the preference directory check/creation
475 * so that duplication can be avoided with filter.c
479 pf_path = (gchar *) g_malloc(strlen(getenv("HOME")) + strlen(PF_DIR) +
480 strlen(PF_NAME) + 4);
483 sprintf(pf_path, "%s/%s", getenv("HOME"), PF_DIR);
484 if (stat(pf_path, &s_buf) != 0)
485 mkdir(pf_path, 0755);
487 sprintf(pf_path, "%s/%s/%s", getenv("HOME"), PF_DIR, PF_NAME);
488 if ((pf = fopen(pf_path, "w")) == NULL) {
489 simple_dialog(ESD_TYPE_WARN, NULL,
490 "Can't open preferences file\n\"%s\".", pf_path);
494 fputs("# Configuration file for Ethereal " VERSION ".\n"
496 "# This file is regenerated each time preferences are saved within\n"
497 "# Ethereal. Making manual changes should be safe, however.\n"
499 "######## Printing ########\n"
502 fprintf (pf, "# Can be one of \"text\" or \"postscript\".\n"
503 "print.format: %s\n\n", pr_formats[prefs.pr_format]);
505 fprintf (pf, "# Can be one of \"command\" or \"file\".\n"
506 "print.destination: %s\n\n", pr_dests[prefs.pr_dest]);
508 fprintf (pf, "# This is the file that gets written to when the "
509 "destination is set to \"file\"\n"
510 "%s: %s\n\n", PRS_PRINT_FILE, prefs.pr_file);
512 fprintf (pf, "# Output gets piped to this command when the destination "
513 "is set to \"command\"\n"
514 "%s: %s\n\n", PRS_PRINT_CMD, prefs.pr_cmd);
516 fprintf (pf, "# Packet list column format. Each pair of strings consists "
517 "of a column title \n# and its format.\n"
518 "%s: %s\n\n", PRS_COL_FMT, col_format_to_pref_str());