diff options
author | Lukas Fleischer <calcurse@cryptocrack.de> | 2011-12-09 20:48:39 +0100 |
---|---|---|
committer | Lukas Fleischer <calcurse@cryptocrack.de> | 2011-12-09 20:54:20 +0100 |
commit | 093b28ac398ffdc2733bc6f42fa58d2153576ad9 (patch) | |
tree | df01b2cf12579376fea9b84a8f0e0ccba1f708e8 /src/custom.c | |
parent | ef716e4a9267008061f1e6469182a4daaec1c05f (diff) | |
download | calcurse-093b28ac398ffdc2733bc6f42fa58d2153576ad9.zip |
Extract config file handlers into a separate file
We used custom_load_conf() to load the configuration file and
io_save_conf() to save configuration. Move these functions, including
all helpers, to a central location.
Signed-off-by: Lukas Fleischer <calcurse@cryptocrack.de>
Diffstat (limited to 'src/custom.c')
-rw-r--r-- | src/custom.c | 402 |
1 files changed, 0 insertions, 402 deletions
diff --git a/src/custom.c b/src/custom.c index 38ad8bc..7e5ded3 100644 --- a/src/custom.c +++ b/src/custom.c @@ -41,64 +41,6 @@ #include "calcurse.h" -/* Available configuration variables. */ -enum conf_var { - CUSTOM_CONF_AUTOSAVE, - CUSTOM_CONF_AUTOGC, - CUSTOM_CONF_PERIODICSAVE, - CUSTOM_CONF_CONFIRMQUIT, - CUSTOM_CONF_CONFIRMDELETE, - CUSTOM_CONF_SKIPSYSTEMDIALOGS, - CUSTOM_CONF_SKIPPROGRESSBAR, - CUSTOM_CONF_CALENDAR_DEFAULTVIEW, - CUSTOM_CONF_WEEKBEGINSONMONDAY, - CUSTOM_CONF_COLORTHEME, - CUSTOM_CONF_LAYOUT, - CUSTOM_CONF_SBAR_WIDTH, - CUSTOM_CONF_NOTIFYBARSHOW, - CUSTOM_CONF_NOTIFYBARDATE, - CUSTOM_CONF_NOTIFYBARCLOCK, - CUSTOM_CONF_NOTIFYBARWARNING, - CUSTOM_CONF_NOTIFYBARCOMMAND, - CUSTOM_CONF_NOTIFYALL, - CUSTOM_CONF_OUTPUTDATEFMT, - CUSTOM_CONF_INPUTDATEFMT, - CUSTOM_CONF_DMON_ENABLE, - CUSTOM_CONF_DMON_LOG, - CUSTOM_CONF_INVALID -}; - -struct conf_varname { - enum conf_var var; - const char *name; -}; - -static struct conf_varname conf_varmap[] = -{ - { CUSTOM_CONF_AUTOSAVE, "auto_save" }, - { CUSTOM_CONF_AUTOGC, "auto_gc" }, - { CUSTOM_CONF_PERIODICSAVE, "periodic_save" }, - { CUSTOM_CONF_CONFIRMQUIT, "confirm_quit" }, - { CUSTOM_CONF_CONFIRMDELETE, "confirm_delete" }, - { CUSTOM_CONF_SKIPSYSTEMDIALOGS, "skip_system_dialogs" }, - { CUSTOM_CONF_SKIPPROGRESSBAR, "skip_progress_bar" }, - { CUSTOM_CONF_CALENDAR_DEFAULTVIEW, "calendar_default_view" }, - { CUSTOM_CONF_WEEKBEGINSONMONDAY, "week_begins_on_monday" }, - { CUSTOM_CONF_COLORTHEME, "color-theme" }, - { CUSTOM_CONF_LAYOUT, "layout" }, - { CUSTOM_CONF_SBAR_WIDTH, "side-bar_width" }, - { CUSTOM_CONF_NOTIFYBARSHOW, "notify-bar_show" }, - { CUSTOM_CONF_NOTIFYBARDATE, "notify-bar_date" }, - { CUSTOM_CONF_NOTIFYBARCLOCK, "notify-bar_clock" }, - { CUSTOM_CONF_NOTIFYBARWARNING, "notify-bar_warning" }, - { CUSTOM_CONF_NOTIFYBARCOMMAND, "notify-bar_command" }, - { CUSTOM_CONF_NOTIFYALL, "notify-all" }, - { CUSTOM_CONF_OUTPUTDATEFMT, "output_datefmt" }, - { CUSTOM_CONF_INPUTDATEFMT, "input_datefmt" }, - { CUSTOM_CONF_DMON_ENABLE, "notify-daemon_enable" }, - { CUSTOM_CONF_DMON_LOG, "notify-daemon_log" } -}; - struct attribute { int color[7]; int nocolor[7]; @@ -106,134 +48,6 @@ struct attribute { static struct attribute attr; -static int -conf_parse_bool (unsigned *dest, char *val) -{ - if (strncmp (val, "yes", 4) == 0) - *dest = 1; - else if (strncmp (val, "no", 3) == 0) - *dest = 0; - else - return 0; - - return 1; -} - -static int -conf_parse_unsigned (unsigned *dest, char *val) -{ - if (is_all_digit (val)) - *dest = atoi (val); - else - return 0; - - return 1; -} - -static int -conf_parse_int (int *dest, char *val) -{ - if ((*val == '+' || *val == '-' || isdigit (*val)) && is_all_digit (val + 1)) - *dest = atoi (val); - else - return 0; - - return 1; -} - -/* - * Load user color theme from file. - * Need to handle calcurse versions prior to 1.8, where colors where handled - * differently (number between 1 and 8). - */ -static int -conf_parse_color (char *val) -{ -#define AWAITED_COLORS 2 - - int i, len, color_num; - char c[AWAITED_COLORS][BUFSIZ]; - int colr[AWAITED_COLORS]; - - len = strlen (val); - if (len > 1) - { - /* New version configuration */ - if (sscanf (val, "%s on %s", c[0], c[1]) != AWAITED_COLORS) - return 0; - - for (i = 0; i < AWAITED_COLORS; i++) - { - if (!strncmp (c[i], "black", 5)) - colr[i] = COLOR_BLACK; - else if (!strncmp (c[i], "red", 3)) - colr[i] = COLOR_RED; - else if (!strncmp (c[i], "green", 5)) - colr[i] = COLOR_GREEN; - else if (!strncmp (c[i], "yellow", 6)) - colr[i] = COLOR_YELLOW; - else if (!strncmp (c[i], "blue", 4)) - colr[i] = COLOR_BLUE; - else if (!strncmp (c[i], "magenta", 7)) - colr[i] = COLOR_MAGENTA; - else if (!strncmp (c[i], "cyan", 4)) - colr[i] = COLOR_CYAN; - else if (!strncmp (c[i], "white", 5)) - colr[i] = COLOR_WHITE; - else if (!strncmp (c[i], "default", 7)) - colr[i] = background; - else - return 0; - } - init_pair (COLR_CUSTOM, colr[0], colr[1]); - } - else if (len == 1) - { - /* Old version configuration */ - if (isdigit (*val)) - color_num = atoi (val); - else - return 0; - - switch (color_num) - { - case 0: - colorize = 0; - break; - case 1: - init_pair (COLR_CUSTOM, COLOR_RED, background); - break; - case 2: - init_pair (COLR_CUSTOM, COLOR_GREEN, background); - break; - case 3: - init_pair (COLR_CUSTOM, COLOR_BLUE, background); - break; - case 4: - init_pair (COLR_CUSTOM, COLOR_CYAN, background); - break; - case 5: - init_pair (COLR_CUSTOM, COLOR_YELLOW, background); - break; - case 6: - init_pair (COLR_CUSTOM, COLOR_BLACK, COLR_GREEN); - break; - case 7: - init_pair (COLR_CUSTOM, COLOR_BLACK, COLR_YELLOW); - break; - case 8: - init_pair (COLR_CUSTOM, COLOR_RED, COLR_BLUE); - break; - default: - return 0; - } - } - else - return 0; - - return 1; -} - /* * Define window attributes (for both color and non-color terminals): * ATTR_HIGHEST are for window titles @@ -282,170 +96,6 @@ custom_remove_attr (WINDOW *win, int attr_num) wattroff (win, attr.nocolor[attr_num]); } -/* Set a configuration variable. */ -static int -custom_set_conf (enum conf_var var, char *val) -{ - unsigned tmp; - - switch (var) - { - case CUSTOM_CONF_AUTOSAVE: - return conf_parse_bool (&conf.auto_save, val); - break; - case CUSTOM_CONF_AUTOGC: - return conf_parse_bool (&conf.auto_gc, val); - break; - case CUSTOM_CONF_PERIODICSAVE: - return conf_parse_unsigned (&conf.periodic_save, val); - break; - case CUSTOM_CONF_CONFIRMQUIT: - return conf_parse_bool (&conf.confirm_quit, val); - break; - case CUSTOM_CONF_CONFIRMDELETE: - return conf_parse_bool (&conf.confirm_delete, val); - break; - case CUSTOM_CONF_SKIPSYSTEMDIALOGS: - return conf_parse_bool (&conf.skip_system_dialogs, val); - break; - case CUSTOM_CONF_SKIPPROGRESSBAR: - return conf_parse_bool (&conf.skip_progress_bar, val); - break; - case CUSTOM_CONF_CALENDAR_DEFAULTVIEW: - calendar_set_view (atoi (val)); - break; - case CUSTOM_CONF_WEEKBEGINSONMONDAY: - return conf_parse_bool (&tmp, val); - if (tmp) - calendar_set_first_day_of_week (MONDAY); - else - calendar_set_first_day_of_week (SUNDAY); - break; - case CUSTOM_CONF_COLORTHEME: - return conf_parse_color (val); - break; - case CUSTOM_CONF_LAYOUT: - wins_set_layout (atoi (val)); - break; - case CUSTOM_CONF_SBAR_WIDTH: - wins_set_sbar_width (atoi (val)); - break; - case CUSTOM_CONF_NOTIFYBARSHOW: - return conf_parse_bool (&nbar.show, val); - break; - case CUSTOM_CONF_NOTIFYBARDATE: - strncpy (nbar.datefmt, val, strlen (val) + 1); - break; - case CUSTOM_CONF_NOTIFYBARCLOCK: - strncpy (nbar.timefmt, val, strlen (val) + 1); - break; - case CUSTOM_CONF_NOTIFYBARWARNING: - return conf_parse_int (&nbar.cntdwn, val); - break; - case CUSTOM_CONF_NOTIFYBARCOMMAND: - strncpy (nbar.cmd, val, strlen (val) + 1); - break; - case CUSTOM_CONF_NOTIFYALL: - return conf_parse_bool(&nbar.notify_all, val); - break; - case CUSTOM_CONF_OUTPUTDATEFMT: - if (val[0] != '\0') - strncpy (conf.output_datefmt, val, strlen (val) + 1); - break; - case CUSTOM_CONF_INPUTDATEFMT: - return conf_parse_int (&conf.input_datefmt, val); - if (conf.input_datefmt <= 0 || conf.input_datefmt >= DATE_FORMATS) - conf.input_datefmt = 1; - break; - case CUSTOM_CONF_DMON_ENABLE: - return conf_parse_bool (&dmon.enable, val); - break; - case CUSTOM_CONF_DMON_LOG: - return conf_parse_bool (&dmon.log, val); - break; - default: - return 0; - break; - } - - return 1; -} - -/* Load the user configuration. */ -void -custom_load_conf (void) -{ - FILE *data_file; - char *mesg_line1 = _("Failed to open config file"); - char *mesg_line2 = _("Press [ENTER] to continue"); - char buf[BUFSIZ], e_conf[BUFSIZ]; - int i; - char *name; - enum conf_var var; - char *val; - - data_file = fopen (path_conf, "r"); - if (data_file == NULL) - { - status_mesg (mesg_line1, mesg_line2); - wnoutrefresh (win[STA].p); - wins_doupdate (); - keys_getch (win[STA].p, NULL); - } - - pthread_mutex_lock (&nbar.mutex); - for (;;) - { - if (fgets (buf, sizeof buf, data_file) == NULL) - break; - io_extract_data (e_conf, buf, sizeof buf); - - if (*e_conf == '\0') - continue; - - name = e_conf; - val = strchr (e_conf, '='); - if (val) - { - *val = '\0'; - val++; - } - - var = CUSTOM_CONF_INVALID; - for (i = 0; i < sizeof (conf_varmap) / sizeof (struct conf_varname); i++) - { - if (strncmp (name, conf_varmap[i].name, BUFSIZ) == 0) - { - var = conf_varmap[i].var; - break; - } - } - - if (var == CUSTOM_CONF_INVALID) - { - EXIT (_("configuration variable unknown: \"%s\""), name); - /* NOTREACHED */ - } - - if (val && (*val == '\0' || *val == '\n')) - { - /* Backward compatibility mode. */ - if (fgets (buf, sizeof buf, data_file) == NULL) - break; - io_extract_data (e_conf, buf, sizeof buf); - val = e_conf; - } - - if (!val || !custom_set_conf (var, val)) - { - EXIT (_("wrong configuration variable format for \"%s\""), name); - /* NOTREACHED */ - } - } - file_close (data_file, __FILE_POS__); - pthread_mutex_unlock (&nbar.mutex); -} - /* Draws the configuration bar */ void custom_config_bar (void) @@ -963,58 +613,6 @@ custom_color_config (void) delwin (conf_win.p); } -/* - * Return a string defining the color theme in the form: - * foreground color 'on' background color - * in order to dump this data in the configuration file. - * Color numbers follow the ncurses library definitions. - * If ncurses library was compiled with --enable-ext-funcs, - * then default color is -1. - */ -void -custom_color_theme_name (char *theme_name) -{ -#define MAXCOLORS 8 -#define NBCOLORS 2 -#define DEFAULTCOLOR 255 -#define DEFAULTCOLOR_EXT -1 - - int i; - short color[NBCOLORS]; - char *color_name[NBCOLORS]; - char *default_color = "default"; - char *name[MAXCOLORS] = { - "black", - "red", - "green", - "yellow", - "blue", - "magenta", - "cyan", - "white" - }; - - if (!colorize) - strncpy (theme_name, "0", BUFSIZ); - else - { - pair_content (COLR_CUSTOM, &color[0], &color[1]); - for (i = 0; i < NBCOLORS; i++) - { - if ((color[i] == DEFAULTCOLOR) || (color[i] == DEFAULTCOLOR_EXT)) - color_name[i] = default_color; - else if (color[i] >= 0 && color[i] <= MAXCOLORS) - color_name[i] = name[color[i]]; - else - { - EXIT (_("unknown color")); - /* NOTREACHED */ - } - } - snprintf (theme_name, BUFSIZ, "%s on %s", color_name[0], color_name[1]); - } -} - /* Prints the general options. */ static int print_general_options (WINDOW *win) |