2 * Routines for handling preferences
4 * $Id: prefs.c,v 1.23 1999/12/02 04:30:03 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>
56 /* Internal functions */
57 static int set_pref(gchar*, gchar*);
58 static GList *get_string_list(gchar *);
59 static void clear_string_list(GList *);
61 #define PF_NAME "preferences"
63 static int init_prefs = 1;
64 static gchar *pf_path = NULL;
66 /* Parse through a list of comma-separated, quoted strings. Return a
67 list of the string data */
69 get_string_list(gchar *str) {
70 enum { PRE_QUOT, IN_QUOT, POST_QUOT };
72 gint state = PRE_QUOT, i = 0, j = 0;
73 gboolean backslash = FALSE;
74 gchar cur_c, *slstr = NULL;
77 while ((cur_c = str[i]) != '\0') {
78 if (cur_c == '"' && ! backslash) {
82 slstr = (gchar *) g_malloc(sizeof(gchar) * COL_MAX_LEN);
88 sl = g_list_append(sl, slstr);
91 clear_string_list(sl);
97 } else if (cur_c == '\\' && ! backslash) {
99 } else if (cur_c == ',' && state == POST_QUOT) {
101 } else if (state == IN_QUOT && j < COL_MAX_LEN) {
107 if (state != POST_QUOT) {
108 clear_string_list(sl);
114 clear_string_list(GList *sl) {
119 l = g_list_remove_link(l, l);
123 /* Preferences file format:
124 * - Configuration directives start at the beginning of the line, and
125 * are terminated with a colon.
126 * - Directives can be continued on the next line by preceding them with
131 # This is a comment line
133 print.file: /a/very/long/path/
138 #define MAX_VAR_LEN 32
139 #define MAX_VAL_LEN 1024
140 #define DEF_NUM_COLS 6
142 read_prefs(char **pf_path_return) {
143 enum { START, IN_VAR, PRE_VAL, IN_VAL, IN_SKIP };
145 gchar cur_var[MAX_VAR_LEN], cur_val[MAX_VAL_LEN];
146 int got_c, state = START, i;
147 gint var_len = 0, val_len = 0, fline = 1, pline = 1;
148 gboolean got_val = FALSE;
150 gchar *col_fmt[] = {"No.", "%m", "Time", "%t",
151 "Source", "%s", "Destination", "%d",
152 "Protocol", "%p", "Info", "%i"};
155 /* Initialize preferences. With any luck, these values will be
156 overwritten below. */
159 prefs.pr_format = PR_FMT_TEXT;
160 prefs.pr_dest = PR_DEST_CMD;
161 prefs.pr_file = g_strdup("ethereal.out");
162 prefs.pr_cmd = g_strdup("lpr");
163 prefs.col_list = NULL;
164 for (i = 0; i < DEF_NUM_COLS; i++) {
165 cfmt = (fmt_data *) g_malloc(sizeof(fmt_data));
166 cfmt->title = g_strdup(col_fmt[i * 2]);
167 cfmt->fmt = g_strdup(col_fmt[(i * 2) + 1]);
168 prefs.col_list = g_list_append(prefs.col_list, cfmt);
170 prefs.num_cols = DEF_NUM_COLS;
171 prefs.st_client_fg.pixel = 0;
172 prefs.st_client_fg.red = 32767;
173 prefs.st_client_fg.green = 0;
174 prefs.st_client_fg.blue = 0;
175 prefs.st_client_bg.pixel = 65535;
176 prefs.st_client_bg.red = 65535;
177 prefs.st_client_bg.green = 65535;
178 prefs.st_client_bg.blue = 65535;
179 prefs.st_server_fg.pixel = 0;
180 prefs.st_server_fg.red = 0;
181 prefs.st_server_fg.green = 0;
182 prefs.st_server_fg.blue = 32767;
183 prefs.st_server_bg.pixel = 65535;
184 prefs.st_server_bg.red = 65535;
185 prefs.st_server_bg.green = 65535;
186 prefs.st_server_bg.blue = 65535;
190 pf_path = (gchar *) g_malloc(strlen(getenv("HOME")) + strlen(PF_DIR) +
191 strlen(PF_NAME) + 4);
192 sprintf(pf_path, "%s/%s/%s", getenv("HOME"), PF_DIR, PF_NAME);
195 *pf_path_return = NULL;
196 if ((pf = fopen(pf_path, "r")) == NULL) {
198 *pf_path_return = pf_path;
202 while ((got_c = getc(pf)) != EOF) {
208 if (var_len >= MAX_VAR_LEN) {
209 g_warning ("%s line %d: Variable too long", pf_path, fline);
214 if (val_len >= MAX_VAL_LEN) {
215 g_warning ("%s line %d: Value too long", pf_path, fline);
223 if (isalnum(got_c)) {
226 cur_var[var_len] = '\0';
227 cur_val[val_len] = '\0';
228 if (! set_pref(cur_var, cur_val))
229 g_warning ("%s line %d: Bogus preference", pf_path, pline);
231 g_warning ("%s line %d: Incomplete preference", pf_path, pline);
239 } else if (isspace(got_c) && var_len > 0 && got_val) {
241 } else if (got_c == '#') {
244 g_warning ("%s line %d: Malformed line", pf_path, fline);
249 cur_var[var_len] = got_c;
258 if (!isspace(got_c)) {
260 cur_val[val_len] = got_c;
266 cur_val[val_len] = got_c;
269 while (isspace(cur_val[val_len]) && val_len > 0)
278 cur_var[var_len] = '\0';
279 cur_val[val_len] = '\0';
280 if (! set_pref(cur_var, cur_val))
281 g_warning ("%s line %d: Bogus preference", pf_path, pline);
283 g_warning ("%s line %d: Incomplete preference", pf_path, pline);
291 #define PRS_PRINT_FMT "print.format"
292 #define PRS_PRINT_DEST "print.destination"
293 #define PRS_PRINT_FILE "print.file"
294 #define PRS_PRINT_CMD "print.command"
295 #define PRS_COL_FMT "column.format"
296 #define PRS_STREAM_CL_FG "stream.client.fg"
297 #define PRS_STREAM_CL_BG "stream.client.bg"
298 #define PRS_STREAM_SR_FG "stream.server.fg"
299 #define PRS_STREAM_SR_BG "stream.server.bg"
301 #define RED_COMPONENT(x) ((((x) >> 16) & 0xff) * 65535 / 255)
302 #define GREEN_COMPONENT(x) ((((x) >> 8) & 0xff) * 65535 / 255)
303 #define BLUE_COMPONENT(x) (((x) & 0xff) * 65535 / 255)
305 static gchar *pr_formats[] = { "text", "postscript" };
306 static gchar *pr_dests[] = { "command", "file" };
309 set_pref(gchar *pref, gchar *value) {
313 unsigned long int cval;
315 if (strcmp(pref, PRS_PRINT_FMT) == 0) {
316 if (strcmp(value, pr_formats[PR_FMT_TEXT]) == 0) {
317 prefs.pr_format = PR_FMT_TEXT;
318 } else if (strcmp(value, pr_formats[PR_FMT_PS]) == 0) {
319 prefs.pr_format = PR_FMT_PS;
323 } else if (strcmp(pref, PRS_PRINT_DEST) == 0) {
324 if (strcmp(value, pr_dests[PR_DEST_CMD]) == 0) {
325 prefs.pr_dest = PR_DEST_CMD;
326 } else if (strcmp(value, pr_dests[PR_DEST_FILE]) == 0) {
327 prefs.pr_dest = PR_DEST_FILE;
331 } else if (strcmp(pref, PRS_PRINT_FILE) == 0) {
332 if (prefs.pr_file) g_free(prefs.pr_file);
333 prefs.pr_file = g_strdup(value);
334 } else if (strcmp(pref, PRS_PRINT_CMD) == 0) {
335 if (prefs.pr_cmd) g_free(prefs.pr_cmd);
336 prefs.pr_cmd = g_strdup(value);
337 } else if (strcmp(pref, PRS_COL_FMT) == 0) {
338 if ((col_l = get_string_list(value)) && (g_list_length(col_l) % 2) == 0) {
339 while (prefs.col_list) {
340 cfmt = prefs.col_list->data;
344 prefs.col_list = g_list_remove_link(prefs.col_list, prefs.col_list);
346 llen = g_list_length(col_l);
347 prefs.num_cols = llen / 2;
348 col_l = g_list_first(col_l);
350 cfmt = (fmt_data *) g_malloc(sizeof(fmt_data));
351 cfmt->title = g_strdup(col_l->data);
353 cfmt->fmt = g_strdup(col_l->data);
355 prefs.col_list = g_list_append(prefs.col_list, cfmt);
357 /* To do: else print some sort of error? */
359 clear_string_list(col_l);
360 } else if (strcmp(pref, PRS_STREAM_CL_FG) == 0) {
361 cval = strtoul(value, NULL, 16);
362 prefs.st_client_fg.pixel = 0;
363 prefs.st_client_fg.red = RED_COMPONENT(cval);
364 prefs.st_client_fg.green = GREEN_COMPONENT(cval);
365 prefs.st_client_fg.blue = BLUE_COMPONENT(cval);
366 } else if (strcmp(pref, PRS_STREAM_CL_BG) == 0) {
367 cval = strtoul(value, NULL, 16);
368 prefs.st_client_bg.pixel = 0;
369 prefs.st_client_bg.red = RED_COMPONENT(cval);
370 prefs.st_client_bg.green = GREEN_COMPONENT(cval);
371 prefs.st_client_bg.blue = BLUE_COMPONENT(cval);
372 } else if (strcmp(pref, PRS_STREAM_SR_FG) == 0) {
373 cval = strtoul(value, NULL, 16);
374 prefs.st_server_fg.pixel = 0;
375 prefs.st_server_fg.red = RED_COMPONENT(cval);
376 prefs.st_server_fg.green = GREEN_COMPONENT(cval);
377 prefs.st_server_fg.blue = BLUE_COMPONENT(cval);
378 } else if (strcmp(pref, PRS_STREAM_SR_BG) == 0) {
379 cval = strtoul(value, NULL, 16);
380 prefs.st_server_bg.pixel = 0;
381 prefs.st_server_bg.red = RED_COMPONENT(cval);
382 prefs.st_server_bg.green = GREEN_COMPONENT(cval);
383 prefs.st_server_bg.blue = BLUE_COMPONENT(cval);
397 * - Split output lines longer than MAX_VAL_LEN
398 * - Create a function for the preference directory check/creation
399 * so that duplication can be avoided with filter.c
403 pf_path = (gchar *) g_malloc(strlen(getenv("HOME")) + strlen(PF_DIR) +
404 strlen(PF_NAME) + 4);
407 sprintf(pf_path, "%s/%s", getenv("HOME"), PF_DIR);
408 if (stat(pf_path, &s_buf) != 0)
412 mkdir(pf_path, 0755);
415 sprintf(pf_path, "%s/%s/%s", getenv("HOME"), PF_DIR, PF_NAME);
416 if ((pf = fopen(pf_path, "w")) == NULL) {
417 simple_dialog(ESD_TYPE_WARN, NULL,
418 "Can't open preferences file\n\"%s\".", pf_path);
422 fputs("# Configuration file for Ethereal " VERSION ".\n"
424 "# This file is regenerated each time preferences are saved within\n"
425 "# Ethereal. Making manual changes should be safe, however.\n"
427 "######## Printing ########\n"
430 fprintf (pf, "# Can be one of \"text\" or \"postscript\".\n"
431 "print.format: %s\n\n", pr_formats[prefs.pr_format]);
433 fprintf (pf, "# Can be one of \"command\" or \"file\".\n"
434 "print.destination: %s\n\n", pr_dests[prefs.pr_dest]);
436 fprintf (pf, "# This is the file that gets written to when the "
437 "destination is set to \"file\"\n"
438 "%s: %s\n\n", PRS_PRINT_FILE, prefs.pr_file);
440 fprintf (pf, "# Output gets piped to this command when the destination "
441 "is set to \"command\"\n"
442 "%s: %s\n\n", PRS_PRINT_CMD, prefs.pr_cmd);
444 fprintf (pf, "# Packet list column format. Each pair of strings consists "
445 "of a column title \n# and its format.\n"
446 "%s: %s\n\n", PRS_COL_FMT, col_format_to_pref_str());
448 fprintf (pf, "# TCP stream window color preferences. Each value is a six "
449 "digit hexadecimal value in the form rrggbb.\n");
450 fprintf (pf, "%s: %02x%02x%02x\n", PRS_STREAM_CL_FG,
451 (prefs.st_client_fg.red * 255 / 65535),
452 (prefs.st_client_fg.green * 255 / 65535),
453 (prefs.st_client_fg.blue * 255 / 65535));
454 fprintf (pf, "%s: %02x%02x%02x\n", PRS_STREAM_CL_BG,
455 (prefs.st_client_bg.red * 255 / 65535),
456 (prefs.st_client_bg.green * 255 / 65535),
457 (prefs.st_client_bg.blue * 255 / 65535));
458 fprintf (pf, "%s: %02x%02x%02x\n", PRS_STREAM_SR_FG,
459 (prefs.st_server_fg.red * 255 / 65535),
460 (prefs.st_server_fg.green * 255 / 65535),
461 (prefs.st_server_fg.blue * 255 / 65535));
462 fprintf (pf, "%s: %02x%02x%02x\n", PRS_STREAM_SR_BG,
463 (prefs.st_server_bg.red * 255 / 65535),
464 (prefs.st_server_bg.green * 255 / 65535),
465 (prefs.st_server_bg.blue * 255 / 65535));