Improve data load error reporting

The last part of loading appointments and events is performed by four
"scan" functions called from io_load_app(). Failure in this part of data
load does not use io_load_error().

The four "scan" functions are changed to return an error message on
failure and NULL otherwise (the previous return value was not used).

Signed-off-by: Lukas Fleischer <lfleischer@calcurse.org>
This commit is contained in:
Lars Henriksen 2019-12-08 09:16:24 +01:00 committed by Lukas Fleischer
parent ce81c0fa63
commit bf3dba2ae2
5 changed files with 54 additions and 53 deletions

View File

@ -195,7 +195,7 @@ void apoint_write(struct apoint *o, FILE * f)
mem_free(str); mem_free(str);
} }
struct apoint *apoint_scan(FILE * f, struct tm start, struct tm end, char *apoint_scan(FILE * f, struct tm start, struct tm end,
char state, char *note, struct item_filter *filter) char state, char *note, struct item_filter *filter)
{ {
char buf[BUFSIZ], *newline; char buf[BUFSIZ], *newline;
@ -203,15 +203,15 @@ struct apoint *apoint_scan(FILE * f, struct tm start, struct tm end,
struct apoint *apt = NULL; struct apoint *apt = NULL;
int cond; int cond;
EXIT_IF(!check_date(start.tm_year, start.tm_mon, start.tm_mday) || if (!check_date(start.tm_year, start.tm_mon, start.tm_mday) ||
!check_date(end.tm_year, end.tm_mon, end.tm_mday) || !check_date(end.tm_year, end.tm_mon, end.tm_mday) ||
!check_time(start.tm_hour, start.tm_min) || !check_time(start.tm_hour, start.tm_min) ||
!check_time(end.tm_hour, end.tm_min), !check_time(end.tm_hour, end.tm_min))
_("date error in appointment")); return _("illegal date in appointment");
/* Read the appointment description */ /* Read the appointment description */
if (!fgets(buf, sizeof buf, f)) if (!fgets(buf, sizeof buf, f))
return NULL; return _("error in appointment description");
newline = strchr(buf, '\n'); newline = strchr(buf, '\n');
if (newline) if (newline)
@ -226,8 +226,8 @@ struct apoint *apoint_scan(FILE * f, struct tm start, struct tm end,
tstart = mktime(&start); tstart = mktime(&start);
tend = mktime(&end); tend = mktime(&end);
EXIT_IF(tstart == -1 || tend == -1 || tstart > tend, if (tstart == -1 || tend == -1 || tstart > tend)
_("date error in appointment")); return _("date error in appointment");
/* Filter item. */ /* Filter item. */
if (filter) { if (filter) {
@ -255,8 +255,7 @@ struct apoint *apoint_scan(FILE * f, struct tm start, struct tm end,
} }
if (!apt) if (!apt)
apt = apoint_new(buf, note, tstart, tend - tstart, state); apt = apoint_new(buf, note, tstart, tend - tstart, state);
return NULL;
return apt;
} }
void apoint_delete(struct apoint *apt) void apoint_delete(struct apoint *apt)

View File

@ -766,7 +766,7 @@ void apoint_sec2str(struct apoint *, time_t, char *, char *);
char *apoint_tostr(struct apoint *); char *apoint_tostr(struct apoint *);
char *apoint_hash(struct apoint *); char *apoint_hash(struct apoint *);
void apoint_write(struct apoint *, FILE *); void apoint_write(struct apoint *, FILE *);
struct apoint *apoint_scan(FILE *, struct tm, struct tm, char, char *, char *apoint_scan(FILE *, struct tm, struct tm, char, char *,
struct item_filter *); struct item_filter *);
void apoint_delete(struct apoint *); void apoint_delete(struct apoint *);
struct notify_app *apoint_check_next(struct notify_app *, time_t); struct notify_app *apoint_check_next(struct notify_app *, time_t);
@ -864,7 +864,7 @@ unsigned event_inday(struct event *, time_t *);
char *event_tostr(struct event *); char *event_tostr(struct event *);
char *event_hash(struct event *); char *event_hash(struct event *);
void event_write(struct event *, FILE *); void event_write(struct event *, FILE *);
struct event *event_scan(FILE *, struct tm, int, char *, struct item_filter *); char *event_scan(FILE *, struct tm, int, char *, struct item_filter *);
void event_delete(struct event *); void event_delete(struct event *);
void event_paste_item(struct event *, time_t); void event_paste_item(struct event *, time_t);
int event_dummy(struct day_item *); int event_dummy(struct day_item *);
@ -1051,10 +1051,10 @@ struct recur_event *recur_event_new(char *, char *, time_t, int,
struct rpt *); struct rpt *);
char recur_def2char(enum recur_type); char recur_def2char(enum recur_type);
int recur_char2def(char); int recur_char2def(char);
struct recur_apoint *recur_apoint_scan(FILE *, struct tm, struct tm, char, char *recur_apoint_scan(FILE *, struct tm, struct tm, char,
char *, struct item_filter *, char *, struct item_filter *,
struct rpt *); struct rpt *);
struct recur_event *recur_event_scan(FILE *, struct tm, int, char *, char *recur_event_scan(FILE *, struct tm, int, char *,
struct item_filter *, struct rpt *); struct item_filter *, struct rpt *);
char *recur_apoint_tostr(struct recur_apoint *); char *recur_apoint_tostr(struct recur_apoint *);
char *recur_apoint_hash(struct recur_apoint *); char *recur_apoint_hash(struct recur_apoint *);

View File

@ -149,7 +149,7 @@ void event_write(struct event *o, FILE * f)
} }
/* Load the events from file */ /* Load the events from file */
struct event *event_scan(FILE * f, struct tm start, int id, char *note, char *event_scan(FILE * f, struct tm start, int id, char *note,
struct item_filter *filter) struct item_filter *filter)
{ {
char buf[BUFSIZ], *nl; char buf[BUFSIZ], *nl;
@ -157,13 +157,13 @@ struct event *event_scan(FILE * f, struct tm start, int id, char *note,
struct event *ev = NULL; struct event *ev = NULL;
int cond; int cond;
EXIT_IF(!check_date(start.tm_year, start.tm_mon, start.tm_mday) || if (!check_date(start.tm_year, start.tm_mon, start.tm_mday) ||
!check_time(start.tm_hour, start.tm_min), !check_time(start.tm_hour, start.tm_min))
_("date error in event")); return _("illegal date in event");
/* Read the event description */ /* Read the event description */
if (!fgets(buf, sizeof buf, f)) if (!fgets(buf, sizeof buf, f))
return NULL; return _("error in appointment description");
nl = strchr(buf, '\n'); nl = strchr(buf, '\n');
if (nl) { if (nl) {
@ -177,8 +177,9 @@ struct event *event_scan(FILE * f, struct tm start, int id, char *note,
start.tm_mon--; start.tm_mon--;
tstart = mktime(&start); tstart = mktime(&start);
EXIT_IF(tstart == -1, _("date error in the event\n")); if (tstart == -1)
tend = tstart + DAYINSEC - 1; return _("date error in event\n");
tend = ENDOFDAY(tstart);
/* Filter item. */ /* Filter item. */
if (filter) { if (filter) {
@ -205,8 +206,7 @@ struct event *event_scan(FILE * f, struct tm start, int id, char *note,
} }
if (!ev) if (!ev)
ev = event_new(buf, note, tstart, id); ev = event_new(buf, note, tstart, id);
return NULL;
return ev;
} }
/* Delete an event from the list. */ /* Delete an event from the list. */

View File

@ -559,6 +559,7 @@ void io_load_app(struct item_filter *filter)
char type, state = 0L; char type, state = 0L;
char note[MAX_NOTESIZ + 1], *notep; char note[MAX_NOTESIZ + 1], *notep;
unsigned line = 0; unsigned line = 0;
char *scan_error;
t = time(NULL); t = time(NULL);
localtime_r(&t, &lt); localtime_r(&t, &lt);
@ -573,6 +574,8 @@ void io_load_app(struct item_filter *filter)
for (;;) { for (;;) {
is_appointment = is_event = is_recursive = 0; is_appointment = is_event = is_recursive = 0;
line++; line++;
scan_error = NULL;
c = getc(data_file); c = getc(data_file);
if (c == EOF) if (c == EOF)
break; break;
@ -691,26 +694,26 @@ void io_load_app(struct item_filter *filter)
io_load_error(path_apts, line, io_load_error(path_apts, line,
_("syntax error in item state")); _("syntax error in item state"));
if (is_recursive) { if (is_recursive)
recur_apoint_scan(data_file, start, end, state, scan_error = recur_apoint_scan(data_file, start, end, state,
notep, filter, &rpt); notep, filter, &rpt);
} else { else
apoint_scan(data_file, start, end, state, scan_error = apoint_scan(data_file, start, end, state,
notep, filter); notep, filter);
}
} else if (is_event) { } else if (is_event) {
ungetc(c, data_file); ungetc(c, data_file);
if (is_recursive) { if (is_recursive)
recur_event_scan(data_file, start, id, notep, scan_error = recur_event_scan(data_file, start, id, notep,
filter, &rpt); filter, &rpt);
} else { else
event_scan(data_file, start, id, notep, filter); scan_error = event_scan(data_file, start, id, notep, filter);
}
} else { } else {
io_load_error(path_apts, line, io_load_error(path_apts, line,
_("wrong format in the appointment or event")); _("wrong format in the appointment or event"));
/* NOTREACHED */ /* NOTREACHED */
} }
if (scan_error)
io_load_error(path_apts, line, scan_error);
} }
file_close(data_file, __FILE_POS__); file_close(data_file, __FILE_POS__);
} }

View File

@ -387,7 +387,7 @@ static void recur_exc_append(struct string *s, llist_t *lexc)
} }
/* Load the recursive appointment description */ /* Load the recursive appointment description */
struct recur_apoint *recur_apoint_scan(FILE *f, struct tm start, struct tm end, char *recur_apoint_scan(FILE *f, struct tm start, struct tm end,
char state, char *note, char state, char *note,
struct item_filter *filter, struct item_filter *filter,
struct rpt *rpt) struct rpt *rpt)
@ -397,15 +397,15 @@ struct recur_apoint *recur_apoint_scan(FILE *f, struct tm start, struct tm end,
struct recur_apoint *rapt = NULL; struct recur_apoint *rapt = NULL;
int cond; int cond;
EXIT_IF(!check_date(start.tm_year, start.tm_mon, start.tm_mday) || if (!check_date(start.tm_year, start.tm_mon, start.tm_mday) ||
!check_date(end.tm_year, end.tm_mon, end.tm_mday) || !check_date(end.tm_year, end.tm_mon, end.tm_mday) ||
!check_time(start.tm_hour, start.tm_min) || !check_time(start.tm_hour, start.tm_min) ||
!check_time(end.tm_hour, end.tm_min), !check_time(end.tm_hour, end.tm_min))
_("date error in appointment")); return _("illegal date in appointment");
/* Read the appointment description */ /* Read the appointment description */
if (!fgets(buf, sizeof buf, f)) if (!fgets(buf, sizeof buf, f))
return NULL; return _("error in appointment description");
nl = strchr(buf, '\n'); nl = strchr(buf, '\n');
if (nl) { if (nl) {
@ -420,8 +420,8 @@ struct recur_apoint *recur_apoint_scan(FILE *f, struct tm start, struct tm end,
tstart = mktime(&start); tstart = mktime(&start);
tend = mktime(&end); tend = mktime(&end);
EXIT_IF(tstart == -1 || tend == -1 || tstart > tend, if (tstart == -1 || tend == -1 || tstart > tend)
_("date error in appointment")); return _("date error in appointment");
/* Filter item. */ /* Filter item. */
if (filter) { if (filter) {
@ -451,12 +451,11 @@ struct recur_apoint *recur_apoint_scan(FILE *f, struct tm start, struct tm end,
if (!rapt) if (!rapt)
rapt = recur_apoint_new(buf, note, tstart, tend - tstart, state, rapt = recur_apoint_new(buf, note, tstart, tend - tstart, state,
rpt); rpt);
return NULL;
return rapt;
} }
/* Load the recursive events from file */ /* Load the recursive events from file */
struct recur_event *recur_event_scan(FILE * f, struct tm start, int id, char *recur_event_scan(FILE * f, struct tm start, int id,
char *note, struct item_filter *filter, char *note, struct item_filter *filter,
struct rpt *rpt) struct rpt *rpt)
{ {
@ -465,13 +464,13 @@ struct recur_event *recur_event_scan(FILE * f, struct tm start, int id,
struct recur_event *rev = NULL; struct recur_event *rev = NULL;
int cond; int cond;
EXIT_IF(!check_date(start.tm_year, start.tm_mon, start.tm_mday) || if (!check_date(start.tm_year, start.tm_mon, start.tm_mday) ||
!check_time(start.tm_hour, start.tm_min), !check_time(start.tm_hour, start.tm_min))
("date error in event")); return _("illegel date in event");
/* Read the event description */ /* Read the event description */
if (!fgets(buf, sizeof buf, f)) if (!fgets(buf, sizeof buf, f))
return NULL; return _("error in appointment description");
nl = strchr(buf, '\n'); nl = strchr(buf, '\n');
if (nl) { if (nl) {
@ -485,7 +484,8 @@ struct recur_event *recur_event_scan(FILE * f, struct tm start, int id,
start.tm_mon--; start.tm_mon--;
tstart = mktime(&start); tstart = mktime(&start);
EXIT_IF(tstart == -1, _("date error in event")); if (tstart == -1)
return _("date error in event");
tend = ENDOFDAY(tstart); tend = ENDOFDAY(tstart);
/* Filter item. */ /* Filter item. */
@ -514,8 +514,7 @@ struct recur_event *recur_event_scan(FILE * f, struct tm start, int id,
} }
if (!rev) if (!rev)
rev = recur_event_new(buf, note, tstart, id, rpt); rev = recur_event_new(buf, note, tstart, id, rpt);
return NULL;
return rev;
} }
char *recur_apoint_tostr(struct recur_apoint *o) char *recur_apoint_tostr(struct recur_apoint *o)