summaryrefslogtreecommitdiffstats
path: root/src/custom.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/custom.c')
-rw-r--r--src/custom.c365
1 files changed, 196 insertions, 169 deletions
diff --git a/src/custom.c b/src/custom.c
index 1f75978..747d4d8 100644
--- a/src/custom.c
+++ b/src/custom.c
@@ -37,12 +37,12 @@
#include <string.h>
#include <stdlib.h>
#include <math.h>
+#include <ctype.h>
#include "calcurse.h"
/* Available configuration variables. */
enum conf_var {
- CUSTOM_CONF_NOVARIABLE,
CUSTOM_CONF_AUTOSAVE,
CUSTOM_CONF_PERIODICSAVE,
CUSTOM_CONF_CONFIRMQUIT,
@@ -63,7 +63,36 @@ enum conf_var {
CUSTOM_CONF_INPUTDATEFMT,
CUSTOM_CONF_DMON_ENABLE,
CUSTOM_CONF_DMON_LOG,
- CUSTOM_CONF_VARIABLES
+ 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_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_OUTPUTDATEFMT, "output_datefmt" },
+ { CUSTOM_CONF_INPUTDATEFMT, "input_datefmt" },
+ { CUSTOM_CONF_DMON_ENABLE, "notify-daemon_enable" },
+ { CUSTOM_CONF_DMON_LOG, "notify-daemon_log" }
};
struct attribute {
@@ -73,18 +102,39 @@ struct attribute {
static struct attribute attr;
-static unsigned
-fill_config_var (char *string)
+static int
+conf_parse_bool (unsigned *dest, char *val)
{
- if (strncmp (string, "yes", 3) == 0)
- return 1;
- else if (strncmp (string, "no", 2) == 0)
+ 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
- {
- EXIT (_("wrong configuration variable format."));
- return 0;
- }
+ 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;
}
/*
@@ -92,8 +142,8 @@ fill_config_var (char *string)
* Need to handle calcurse versions prior to 1.8, where colors where handled
* differently (number between 1 and 8).
*/
-static void
-custom_load_color (char *color, int background)
+static int
+conf_parse_color (char *val)
{
#define AWAITED_COLORS 2
@@ -101,15 +151,12 @@ custom_load_color (char *color, int background)
char c[AWAITED_COLORS][BUFSIZ];
int colr[AWAITED_COLORS];
- len = strlen (color);
+ len = strlen (val);
if (len > 1)
{
/* New version configuration */
- if (sscanf (color, "%s on %s", c[0], c[1]) != AWAITED_COLORS)
- {
- EXIT (_("missing colors in config file"));
- /* NOTREACHED */
- }
+ if (sscanf (val, "%s on %s", c[0], c[1]) != AWAITED_COLORS)
+ return 0;
for (i = 0; i < AWAITED_COLORS; i++)
{
@@ -132,17 +179,17 @@ custom_load_color (char *color, int background)
else if (!strncmp (c[i], "default", 7))
colr[i] = background;
else
- {
- EXIT (_("wrong color name"));
- /* NOTREACHED */
- }
+ return 0;
}
init_pair (COLR_CUSTOM, colr[0], colr[1]);
}
- else if (len > 0 && len < 2)
+ else if (len == 1)
{
/* Old version configuration */
- color_num = atoi (color);
+ if (isdigit (*val))
+ color_num = atoi (val);
+ else
+ return 0;
switch (color_num)
{
@@ -174,15 +221,13 @@ custom_load_color (char *color, int background)
init_pair (COLR_CUSTOM, COLOR_RED, COLR_BLUE);
break;
default:
- EXIT (_("wrong color number"));
- /* NOTREACHED */
+ return 0;
}
}
else
- {
- EXIT (_("wrong configuration variable format"));
- /* NOTREACHED */
- }
+ return 0;
+
+ return 1;
}
/*
@@ -233,15 +278,101 @@ custom_remove_attr (WINDOW *win, int attr_num)
wattroff (win, attr.nocolor[attr_num]);
}
+/* Set a configuration variable. */
+static int
+custom_set_conf (struct conf *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_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:
+ (void)strncpy (nbar.datefmt, val, strlen (val) + 1);
+ break;
+ case CUSTOM_CONF_NOTIFYBARCLOCK:
+ (void)strncpy (nbar.timefmt, val, strlen (val) + 1);
+ break;
+ case CUSTOM_CONF_NOTIFYBARWARNING:
+ return conf_parse_int (&nbar.cntdwn, val);
+ break;
+ case CUSTOM_CONF_NOTIFYBARCOMMAND:
+ (void)strncpy (nbar.cmd, val, strlen (val) + 1);
+ break;
+ case CUSTOM_CONF_OUTPUTDATEFMT:
+ if (val[0] != '\0')
+ (void)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 (struct conf *conf, int background)
+custom_load_conf (struct conf *conf)
{
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)
@@ -251,154 +382,50 @@ custom_load_conf (struct conf *conf, int background)
wins_doupdate ();
(void)keys_getch (win[STA].p);
}
- var = CUSTOM_CONF_NOVARIABLE;
+
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 = strtok (e_conf, "=");
+ val = strtok (NULL, "\0");
+
+ var = CUSTOM_CONF_INVALID;
+ for (i = 0; i < sizeof (conf_varmap) / sizeof (struct conf_varname); i++)
{
- break;
+ if (strncmp (name, conf_varmap[i].name, BUFSIZ) == 0)
+ {
+ var = conf_varmap[i].var;
+ break;
+ }
}
- io_extract_data (e_conf, buf, sizeof buf);
- switch (var)
+ if (var == CUSTOM_CONF_INVALID)
{
- case CUSTOM_CONF_NOVARIABLE:
- break;
- case CUSTOM_CONF_AUTOSAVE:
- conf->auto_save = fill_config_var (e_conf);
- var = 0;
- break;
- case CUSTOM_CONF_PERIODICSAVE:
- if (atoi (e_conf) < 0)
- conf->periodic_save = 0;
- else
- conf->periodic_save = atoi (e_conf);
- var = 0;
- break;
- case CUSTOM_CONF_CONFIRMQUIT:
- conf->confirm_quit = fill_config_var (e_conf);
- var = 0;
- break;
- case CUSTOM_CONF_CONFIRMDELETE:
- conf->confirm_delete = fill_config_var (e_conf);
- var = 0;
- break;
- case CUSTOM_CONF_SKIPSYSTEMDIALOGS:
- conf->skip_system_dialogs = fill_config_var (e_conf);
- var = 0;
- break;
- case CUSTOM_CONF_SKIPPROGRESSBAR:
- conf->skip_progress_bar = fill_config_var (e_conf);
- var = 0;
- break;
- case CUSTOM_CONF_CALENDAR_DEFAULTVIEW:
- calendar_set_view (atoi (e_conf));
- var = 0;
- break;
- case CUSTOM_CONF_WEEKBEGINSONMONDAY:
- if (fill_config_var (e_conf))
- calendar_set_first_day_of_week (MONDAY);
- else
- calendar_set_first_day_of_week (SUNDAY);
- var = 0;
- break;
- case CUSTOM_CONF_COLORTHEME:
- custom_load_color (e_conf, background);
- var = 0;
- break;
- case CUSTOM_CONF_LAYOUT:
- wins_set_layout (atoi (e_conf));
- var = 0;
- break;
- case CUSTOM_CONF_SBAR_WIDTH:
- wins_set_sbar_width (atoi (e_conf));
- var = 0;
- break;
- case CUSTOM_CONF_NOTIFYBARSHOW:
- nbar.show = fill_config_var (e_conf);
- var = 0;
- break;
- case CUSTOM_CONF_NOTIFYBARDATE:
- (void)strncpy (nbar.datefmt, e_conf, strlen (e_conf) + 1);
- var = 0;
- break;
- case CUSTOM_CONF_NOTIFYBARCLOCK:
- (void)strncpy (nbar.timefmt, e_conf, strlen (e_conf) + 1);
- var = 0;
- break;
- case CUSTOM_CONF_NOTIFYBARWARNING:
- nbar.cntdwn = atoi (e_conf);
- var = 0;
- break;
- case CUSTOM_CONF_NOTIFYBARCOMMAND:
- (void)strncpy (nbar.cmd, e_conf, strlen (e_conf) + 1);
- var = 0;
- break;
- case CUSTOM_CONF_OUTPUTDATEFMT:
- if (e_conf[0] != '\0')
- (void)strncpy (conf->output_datefmt, e_conf, strlen (e_conf) + 1);
- var = 0;
- break;
- case CUSTOM_CONF_INPUTDATEFMT:
- conf->input_datefmt = atoi (e_conf);
- if (conf->input_datefmt <= 0 || conf->input_datefmt >= DATE_FORMATS)
- conf->input_datefmt = 1;
- var = 0;
- break;
- case CUSTOM_CONF_DMON_ENABLE:
- dmon.enable = fill_config_var (e_conf);
- var = 0;
- break;
- case CUSTOM_CONF_DMON_LOG:
- dmon.log = fill_config_var (e_conf);
- var = 0;
- break;
- default:
- EXIT (_("configuration variable unknown"));
+ EXIT (_("configuration variable unknown: \"%s\""), name);
/* NOTREACHED */
}
- if (strncmp (e_conf, "auto_save=", 10) == 0)
- var = CUSTOM_CONF_AUTOSAVE;
- else if (strncmp (e_conf, "periodic_save=", 14) == 0)
- var = CUSTOM_CONF_PERIODICSAVE;
- else if (strncmp (e_conf, "confirm_quit=", 13) == 0)
- var = CUSTOM_CONF_CONFIRMQUIT;
- else if (strncmp (e_conf, "confirm_delete=", 15) == 0)
- var = CUSTOM_CONF_CONFIRMDELETE;
- else if (strncmp (e_conf, "skip_system_dialogs=", 20) == 0)
- var = CUSTOM_CONF_SKIPSYSTEMDIALOGS;
- else if (strncmp (e_conf, "skip_progress_bar=", 18) == 0)
- var = CUSTOM_CONF_SKIPPROGRESSBAR;
- else if (strncmp (e_conf, "calendar_default_view=", 22) == 0)
- var = CUSTOM_CONF_CALENDAR_DEFAULTVIEW;
- else if (strncmp (e_conf, "week_begins_on_monday=", 22) == 0)
- var = CUSTOM_CONF_WEEKBEGINSONMONDAY;
- else if (strncmp (e_conf, "color-theme=", 12) == 0)
- var = CUSTOM_CONF_COLORTHEME;
- else if (strncmp (e_conf, "layout=", 7) == 0)
- var = CUSTOM_CONF_LAYOUT;
- else if (strncmp (e_conf, "side-bar_width=", 15) == 0)
- var = CUSTOM_CONF_SBAR_WIDTH;
- else if (strncmp (e_conf, "notify-bar_show=", 16) == 0)
- var = CUSTOM_CONF_NOTIFYBARSHOW;
- else if (strncmp (e_conf, "notify-bar_date=", 16) == 0)
- var = CUSTOM_CONF_NOTIFYBARDATE;
- else if (strncmp (e_conf, "notify-bar_clock=", 17) == 0)
- var = CUSTOM_CONF_NOTIFYBARCLOCK;
- else if (strncmp (e_conf, "notify-bar_warning=", 19) == 0)
- var = CUSTOM_CONF_NOTIFYBARWARNING;
- else if (strncmp (e_conf, "notify-bar_command=", 19) == 0)
- var = CUSTOM_CONF_NOTIFYBARCOMMAND;
- else if (strncmp (e_conf, "output_datefmt=", 15) == 0)
- var = CUSTOM_CONF_OUTPUTDATEFMT;
- else if (strncmp (e_conf, "input_datefmt=", 14) == 0)
- var = CUSTOM_CONF_INPUTDATEFMT;
- else if (strncmp (e_conf, "notify-daemon_enable=", 21) == 0)
- var = CUSTOM_CONF_DMON_ENABLE;
- else if (strncmp (e_conf, "notify-daemon_log=", 18) == 0)
- var = CUSTOM_CONF_DMON_LOG;
+ 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 (conf, var, val))
+ {
+ EXIT (_("wrong configuration variable format for \"%s\""), name);
+ /* NOTREACHED */
+ }
}
file_close (data_file, __FILE_POS__);
pthread_mutex_unlock (&nbar.mutex);