diff options
author | Gerald Combs <gerald@wireshark.org> | 1998-10-28 21:38:11 +0000 |
---|---|---|
committer | Gerald Combs <gerald@wireshark.org> | 1998-10-28 21:38:11 +0000 |
commit | 06e5aae0e3946b453377f7d824ef43961dfa47aa (patch) | |
tree | b372423ae62551ea536da9d5edca5d65efa00353 /prefs.c | |
parent | 3b9dda5d6bd434cc5f75b30b08b4ac2d5d33f04c (diff) | |
download | wireshark-06e5aae0e3946b453377f7d824ef43961dfa47aa.tar.gz wireshark-06e5aae0e3946b453377f7d824ef43961dfa47aa.tar.bz2 wireshark-06e5aae0e3946b453377f7d824ef43961dfa47aa.zip |
* Added preference file saves and reads.
svn path=/trunk/; revision=75
Diffstat (limited to 'prefs.c')
-rw-r--r-- | prefs.c | 219 |
1 files changed, 216 insertions, 3 deletions
@@ -1,7 +1,7 @@ /* prefs.c * Routines for handling preferences * - * $Id: prefs.c,v 1.7 1998/10/16 01:18:33 gerald Exp $ + * $Id: prefs.c,v 1.8 1998/10/28 21:38:09 gerald Exp $ * * Ethereal - Network traffic analyzer * By Gerald Combs <gerald@zing.org> @@ -36,11 +36,24 @@ #include "ethereal.h" #include "packet.h" #include "file.h" +#include "prefs.h" #include "print.h" #include "filter.h" -#include "prefs.h" +#include "util.h" + +/* Internal functions */ +static int set_prefs(gchar*, gchar*); +static void write_prefs(); +static void prefs_main_ok_cb(GtkWidget *, gpointer); +static void prefs_main_save_cb(GtkWidget *, gpointer); +static void prefs_main_cancel_cb(GtkWidget *, gpointer); + +e_prefs prefs; +static int init_prefs = 1; + +#define PF_NAME ".ethereal/preferences" -extern capture_file cf; +static gchar *pf_path = NULL; void prefs_cb(GtkWidget *w, gpointer sp) { @@ -137,7 +150,9 @@ prefs_main_ok_cb(GtkWidget *w, gpointer win) { void prefs_main_save_cb(GtkWidget *w, gpointer win) { + printer_prefs_save(gtk_object_get_data(GTK_OBJECT(win), E_PRINT_PAGE_KEY)); filter_prefs_save(gtk_object_get_data(GTK_OBJECT(win), E_FILTER_PAGE_KEY)); + write_prefs(); } void @@ -148,3 +163,201 @@ prefs_main_cancel_cb(GtkWidget *w, gpointer win) { gtk_widget_destroy(GTK_WIDGET(win)); } +/* Preferences file format: + * - Configuration directives start at the beginning of the line, and + * are terminated with a colon. + * - Directives can be continued on the next line by preceding them with + * whitespace. + * + * Example: + +# This is a comment line +print.command: lpr +print.file: /a/very/long/path/ + to/ethereal-out.ps + * + */ + +#define MAX_VAR_LEN 32 +#define MAX_VAL_LEN 256 +void +read_prefs() { + enum { START, IN_VAR, PRE_VAL, IN_VAL, IN_SKIP }; + FILE *pf; + gchar cur_var[MAX_VAR_LEN], cur_val[MAX_VAL_LEN]; + int got_c, state = START; + gint var_len = 0, val_len = 0, fline = 1, pline = 1; + + /* Initialize preferences. With any luck, these values will be + overwritten below. */ + if (init_prefs) { + init_prefs = 0; + prefs.pr_format = PR_FMT_TEXT; + prefs.pr_dest = PR_DEST_CMD; + prefs.pr_file = g_strdup("ethereal.out"); + prefs.pr_cmd = g_strdup("lpr"); + } + + if (! pf_path) { + pf_path = (gchar *) g_malloc(strlen(getenv("HOME")) + strlen(PF_NAME) + 4); + sprintf(pf_path, "%s/%s", getenv("HOME"), PF_NAME); + } + + if ((pf = fopen(pf_path, "r")) == NULL) { + simple_dialog(ESD_TYPE_WARN, NULL, + "Can't open preferences file\n\"%s\"."); + return; + } + + while ((got_c = getc(pf)) != EOF) { + if (got_c == '\n') { + state = START; + fline++; + continue; + } + if (var_len >= MAX_VAR_LEN) { + g_warning ("%s line %d: Variable too long", pf_path, fline); + state = IN_SKIP; + var_len = 0; + continue; + } + if (val_len >= MAX_VAL_LEN) { + g_warning ("%s line %d: Value too long", pf_path, fline); + state = IN_SKIP; + var_len = 0; + continue; + } + + switch (state) { + case START: + if (isalnum(got_c)) { + state = IN_VAR; + if (var_len > 0) { + cur_var[var_len] = '\0'; + cur_val[val_len] = '\0'; + if (! set_pref(cur_var, cur_val)) + g_warning ("%s line %d: Bogus preference", pf_path, pline); + } + cur_var[0] = got_c; + var_len = 1; + pline = fline; + } else if (isspace(got_c) && var_len > 0) { + state = PRE_VAL; + } else if (got_c == '#') { + state = IN_SKIP; + } else { + g_warning ("%s line %d: Malformed line", pf_path, fline); + } + break; + case IN_VAR: + if (got_c != ':') { + cur_var[var_len] = got_c; + var_len++; + } else { + state = PRE_VAL; + val_len = 0; + } + break; + case PRE_VAL: + if (!isspace(got_c)) { + state = IN_VAL; + cur_val[val_len] = got_c; + val_len++; + } + break; + case IN_VAL: + cur_val[val_len] = got_c; + val_len++; + break; + } + } + if (var_len > 0) { + cur_var[var_len] = '\0'; + cur_val[val_len] = '\0'; + if (! set_pref(cur_var, cur_val)) + g_warning ("%s line %d: Bogus preference", pf_path, pline); + } + fclose(pf); +} + +#define PRS_PRINT_FMT "print.format" +#define PRS_PRINT_DEST "print.destination" +#define PRS_PRINT_FILE "print.file" +#define PRS_PRINT_CMD "print.command" + +static gchar *pr_formats[] = { "text", "postscript" }; +static gchar *pr_dests[] = { "command", "file" }; + +int +set_pref(gchar *pref, gchar *value) { + + if (strcmp(pref, PRS_PRINT_FMT) == 0) { + if (strcmp(value, pr_formats[PR_FMT_TEXT]) == 0) { + prefs.pr_format = PR_FMT_TEXT; + } else if (strcmp(value, pr_formats[PR_FMT_PS]) == 0) { + prefs.pr_format = PR_FMT_PS; + } else { + return 0; + } + } else if (strcmp(pref, PRS_PRINT_DEST) == 0) { + if (strcmp(value, pr_dests[PR_DEST_CMD]) == 0) { + prefs.pr_dest = PR_DEST_CMD; + } else if (strcmp(value, pr_dests[PR_DEST_FILE]) == 0) { + prefs.pr_dest = PR_DEST_FILE; + } else { + return 0; + } + } else if (strcmp(pref, PRS_PRINT_FILE) == 0) { + if (prefs.pr_file) g_free(prefs.pr_file); + prefs.pr_file = g_strdup(value); + } else if (strcmp(pref, PRS_PRINT_CMD) == 0) { + if (prefs.pr_cmd) g_free(prefs.pr_cmd); + prefs.pr_cmd = g_strdup(value); + } else { + return 0; + } + + return 1; +} + +void +write_prefs() { + FILE *pf; + + /* To do: Split output lines longer than MAX_VAL_LEN */ + + if (! pf_path) { + pf_path = (gchar *) g_malloc(strlen(getenv("HOME")) + strlen(PF_NAME) + 4); + sprintf(pf_path, "%s/%s", getenv("HOME"), PF_NAME); + } + + if ((pf = fopen(pf_path, "w")) == NULL) { + simple_dialog(ESD_TYPE_WARN, NULL, + "Can't open preferences file\n\"%s\"."); + return; + } + + fputs("# Configuration file for Ethereal " VERSION ".\n" + "#\n" + "# This file is regenerated each time preferences are saved within\n" + "# Ethereal. Making manual changes should be safe, however.\n" + "\n" + "######## Printing ########\n" + "\n", pf); + + fprintf (pf, "# Can be one of \"text\" or \"postscript\".\n" + "print.format: %s\n\n", pr_formats[prefs.pr_format]); + + fprintf (pf, "# Can be one of \"command\" or \"file\".\n" + "print.destination: %s\n\n", pr_dests[prefs.pr_dest]); + + fprintf (pf, "# This is the file that gets written to when the " + "destination is set to \"file\"\n" + "%s: %s\n\n", PRS_PRINT_FILE, prefs.pr_file); + + fprintf (pf, "# Output gets piped to this command when the destination " + "is set to \"command\"\n" + "%s: %s\n", PRS_PRINT_CMD, prefs.pr_cmd); + + fclose(pf); +} |