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);
}
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 buf[BUFSIZ], *newline;
@ -203,15 +203,15 @@ struct apoint *apoint_scan(FILE * f, struct tm start, struct tm end,
struct apoint *apt = NULL;
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_time(start.tm_hour, start.tm_min) ||
!check_time(end.tm_hour, end.tm_min),
_("date error in appointment"));
!check_time(end.tm_hour, end.tm_min))
return _("illegal date in appointment");
/* Read the appointment description */
if (!fgets(buf, sizeof buf, f))
return NULL;
return _("error in appointment description");
newline = strchr(buf, '\n');
if (newline)
@ -226,8 +226,8 @@ struct apoint *apoint_scan(FILE * f, struct tm start, struct tm end,
tstart = mktime(&start);
tend = mktime(&end);
EXIT_IF(tstart == -1 || tend == -1 || tstart > tend,
_("date error in appointment"));
if (tstart == -1 || tend == -1 || tstart > tend)
return _("date error in appointment");
/* Filter item. */
if (filter) {
@ -255,8 +255,7 @@ struct apoint *apoint_scan(FILE * f, struct tm start, struct tm end,
}
if (!apt)
apt = apoint_new(buf, note, tstart, tend - tstart, state);
return apt;
return NULL;
}
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_hash(struct apoint *);
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 *);
void apoint_delete(struct apoint *);
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_hash(struct event *);
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_paste_item(struct event *, time_t);
int event_dummy(struct day_item *);
@ -1051,10 +1051,10 @@ struct recur_event *recur_event_new(char *, char *, time_t, int,
struct rpt *);
char recur_def2char(enum recur_type);
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 *,
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 *);
char *recur_apoint_tostr(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 */
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)
{
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;
int cond;
EXIT_IF(!check_date(start.tm_year, start.tm_mon, start.tm_mday) ||
!check_time(start.tm_hour, start.tm_min),
_("date error in event"));
if (!check_date(start.tm_year, start.tm_mon, start.tm_mday) ||
!check_time(start.tm_hour, start.tm_min))
return _("illegal date in event");
/* Read the event description */
if (!fgets(buf, sizeof buf, f))
return NULL;
return _("error in appointment description");
nl = strchr(buf, '\n');
if (nl) {
@ -177,8 +177,9 @@ struct event *event_scan(FILE * f, struct tm start, int id, char *note,
start.tm_mon--;
tstart = mktime(&start);
EXIT_IF(tstart == -1, _("date error in the event\n"));
tend = tstart + DAYINSEC - 1;
if (tstart == -1)
return _("date error in event\n");
tend = ENDOFDAY(tstart);
/* Filter item. */
if (filter) {
@ -205,8 +206,7 @@ struct event *event_scan(FILE * f, struct tm start, int id, char *note,
}
if (!ev)
ev = event_new(buf, note, tstart, id);
return ev;
return NULL;
}
/* 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 note[MAX_NOTESIZ + 1], *notep;
unsigned line = 0;
char *scan_error;
t = time(NULL);
localtime_r(&t, &lt);
@ -573,6 +574,8 @@ void io_load_app(struct item_filter *filter)
for (;;) {
is_appointment = is_event = is_recursive = 0;
line++;
scan_error = NULL;
c = getc(data_file);
if (c == EOF)
break;
@ -691,26 +694,26 @@ void io_load_app(struct item_filter *filter)
io_load_error(path_apts, line,
_("syntax error in item state"));
if (is_recursive) {
recur_apoint_scan(data_file, start, end, state,
if (is_recursive)
scan_error = recur_apoint_scan(data_file, start, end, state,
notep, filter, &rpt);
} else {
apoint_scan(data_file, start, end, state,
else
scan_error = apoint_scan(data_file, start, end, state,
notep, filter);
}
} else if (is_event) {
ungetc(c, data_file);
if (is_recursive) {
recur_event_scan(data_file, start, id, notep,
if (is_recursive)
scan_error = recur_event_scan(data_file, start, id, notep,
filter, &rpt);
} else {
event_scan(data_file, start, id, notep, filter);
}
else
scan_error = event_scan(data_file, start, id, notep, filter);
} else {
io_load_error(path_apts, line,
_("wrong format in the appointment or event"));
/* NOTREACHED */
}
if (scan_error)
io_load_error(path_apts, line, scan_error);
}
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 */
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,
struct item_filter *filter,
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;
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_time(start.tm_hour, start.tm_min) ||
!check_time(end.tm_hour, end.tm_min),
_("date error in appointment"));
!check_time(end.tm_hour, end.tm_min))
return _("illegal date in appointment");
/* Read the appointment description */
if (!fgets(buf, sizeof buf, f))
return NULL;
return _("error in appointment description");
nl = strchr(buf, '\n');
if (nl) {
@ -420,8 +420,8 @@ struct recur_apoint *recur_apoint_scan(FILE *f, struct tm start, struct tm end,
tstart = mktime(&start);
tend = mktime(&end);
EXIT_IF(tstart == -1 || tend == -1 || tstart > tend,
_("date error in appointment"));
if (tstart == -1 || tend == -1 || tstart > tend)
return _("date error in appointment");
/* Filter item. */
if (filter) {
@ -451,12 +451,11 @@ struct recur_apoint *recur_apoint_scan(FILE *f, struct tm start, struct tm end,
if (!rapt)
rapt = recur_apoint_new(buf, note, tstart, tend - tstart, state,
rpt);
return rapt;
return NULL;
}
/* 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,
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;
int cond;
EXIT_IF(!check_date(start.tm_year, start.tm_mon, start.tm_mday) ||
!check_time(start.tm_hour, start.tm_min),
("date error in event"));
if (!check_date(start.tm_year, start.tm_mon, start.tm_mday) ||
!check_time(start.tm_hour, start.tm_min))
return _("illegel date in event");
/* Read the event description */
if (!fgets(buf, sizeof buf, f))
return NULL;
return _("error in appointment description");
nl = strchr(buf, '\n');
if (nl) {
@ -485,7 +484,8 @@ struct recur_event *recur_event_scan(FILE * f, struct tm start, int id,
start.tm_mon--;
tstart = mktime(&start);
EXIT_IF(tstart == -1, _("date error in event"));
if (tstart == -1)
return _("date error in event");
tend = ENDOFDAY(tstart);
/* Filter item. */
@ -514,8 +514,7 @@ struct recur_event *recur_event_scan(FILE * f, struct tm start, int id,
}
if (!rev)
rev = recur_event_new(buf, note, tstart, id, rpt);
return rev;
return NULL;
}
char *recur_apoint_tostr(struct recur_apoint *o)