summaryrefslogtreecommitdiff
path: root/src/io.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/io.c')
-rw-r--r--src/io.c209
1 files changed, 96 insertions, 113 deletions
diff --git a/src/io.c b/src/io.c
index 0cc394e..b8fccfd 100644
--- a/src/io.c
+++ b/src/io.c
@@ -151,7 +151,6 @@ static void progress_bar(progress_bar_t type, int progress)
static FILE *get_export_stream(enum export_type type)
{
FILE *stream;
- int cancel;
char *home, *stream_name;
const char *question = _("Choose the file used to export calcurse data:");
const char *wrong_name =
@@ -169,8 +168,7 @@ static FILE *get_export_stream(enum export_type type)
while (stream == NULL) {
status_mesg(question, "");
- cancel = updatestring(win[STA].p, &stream_name, 0, 1);
- if (cancel) {
+ if (updatestring(win[STA].p, &stream_name, 0, 1)) {
mem_free(stream_name);
return NULL;
}
@@ -231,7 +229,6 @@ void io_init(const char *cfile, const char *datadir)
snprintf(path_todo, BUFSIZ, "%s/" TODO_PATH_NAME, home);
snprintf(path_conf, BUFSIZ, "%s/" CONF_PATH_NAME, home);
snprintf(path_notes, BUFSIZ, "%s/" NOTES_DIR_NAME, home);
- snprintf(path_apts, BUFSIZ, "%s/" APTS_PATH_NAME, home);
snprintf(path_keys, BUFSIZ, "%s/" KEYS_PATH_NAME, home);
snprintf(path_cpid, BUFSIZ, "%s/" CPID_PATH_NAME, home);
snprintf(path_dpid, BUFSIZ, "%s/" DPID_PATH_NAME, home);
@@ -249,43 +246,48 @@ void io_init(const char *cfile, const char *datadir)
snprintf(path_dpid, BUFSIZ, "%s/" DPID_PATH, home);
snprintf(path_dmon_log, BUFSIZ, "%s/" DLOG_PATH, home);
snprintf(path_notes, BUFSIZ, "%s/" NOTES_DIR, home);
- if (cfile == NULL) {
- snprintf(path_apts, BUFSIZ, "%s/" APTS_PATH, home);
+ }
+
+ if (cfile == NULL) {
+ if (datadir != NULL) {
+ snprintf(path_apts, BUFSIZ, "%s/" APTS_PATH_NAME, home);
} else {
- snprintf(apts_file, BUFSIZ, "%s", cfile);
- strncpy(path_apts, apts_file, BUFSIZ);
- /* check if the file exists, otherwise create it */
- data_file = fopen(path_apts, "r");
- if (data_file == NULL) {
- printf(_("%s does not exist, create it now [y or n] ? "), path_apts);
- ch = getchar();
- switch (ch) {
- case 'N':
- case 'n':
- puts(_("aborting...\n"));
- exit_calcurse(EXIT_FAILURE);
- break;
-
- case 'Y':
- case 'y':
- data_file = fopen(path_apts, "w");
- if (data_file == NULL) {
- perror(path_apts);
- exit_calcurse(EXIT_FAILURE);
- } else {
- printf(_("%s successfully created\n"), path_apts);
- puts(_("starting interactive mode...\n"));
- }
- break;
-
- default:
- puts(_("aborting...\n"));
+ snprintf(path_apts, BUFSIZ, "%s/" APTS_PATH, home);
+ }
+ } else {
+ snprintf(apts_file, BUFSIZ, "%s", cfile);
+ strncpy(path_apts, apts_file, BUFSIZ);
+ /* check if the file exists, otherwise create it */
+ data_file = fopen(path_apts, "r");
+ if (data_file == NULL) {
+ printf(_("%s does not exist, create it now [y or n] ? "), path_apts);
+ ch = getchar();
+ switch (ch) {
+ case 'N':
+ case 'n':
+ puts(_("aborting...\n"));
+ exit_calcurse(EXIT_FAILURE);
+ break;
+
+ case 'Y':
+ case 'y':
+ data_file = fopen(path_apts, "w");
+ if (data_file == NULL) {
+ perror(path_apts);
exit_calcurse(EXIT_FAILURE);
- break;
+ } else {
+ printf(_("%s successfully created\n"), path_apts);
+ puts(_("starting interactive mode...\n"));
}
+ break;
+
+ default:
+ puts(_("aborting...\n"));
+ exit_calcurse(EXIT_FAILURE);
+ break;
}
- file_close(data_file, __FILE_POS__);
}
+ file_close(data_file, __FILE_POS__);
}
}
@@ -425,6 +427,12 @@ void io_save_cal(enum save_display display)
pthread_mutex_unlock(&io_save_mutex);
}
+static void io_load_error(const char *filename, unsigned line,
+ const char *mesg)
+{
+ EXIT("%s:%u: %s", filename, line, mesg);
+}
+
/*
* Check what type of data is written in the appointment file,
* and then load either: a new appointment, a new event, or a new
@@ -441,6 +449,7 @@ void io_load_app(void)
int freq;
char type, state = 0L;
char note[MAX_NOTESIZ + 1], *notep;
+ unsigned line = 0;
t = time(NULL);
localtime_r(&t, &lt);
@@ -452,6 +461,7 @@ void io_load_app(void)
for (;;) {
LLIST_INIT(&exc);
is_appointment = is_event = is_recursive = 0;
+ line++;
c = getc(data_file);
if (c == EOF)
break;
@@ -462,7 +472,7 @@ void io_load_app(void)
*/
if (fscanf(data_file, "%d / %d / %d ",
&start.tm_mon, &start.tm_mday, &start.tm_year) != 3)
- EXIT(_("syntax error in the item date"));
+ io_load_error(path_apts, line, _("syntax error in the item date"));
/* Read the next character : if it is an '@' then we have
* an appointment, else if it is an '[' we have en event.
@@ -474,7 +484,7 @@ void io_load_app(void)
else if (c == '[')
is_event = 1;
else
- EXIT(_("no event nor appointment found"));
+ io_load_error(path_apts, line, _("no event nor appointment found"));
/* Read the remaining informations. */
if (is_appointment) {
@@ -482,14 +492,16 @@ void io_load_app(void)
&start.tm_hour, &start.tm_min,
&end.tm_mon, &end.tm_mday, &end.tm_year,
&end.tm_hour, &end.tm_min) != 7)
- EXIT(_("syntax error in item time or duration"));
+ io_load_error(path_apts, line,
+ _("syntax error in item time or duration"));
} else if (is_event) {
if (fscanf(data_file, " %d ", &id) != 1 || getc(data_file) != ']')
- EXIT(_("syntax error in item identifier"));
+ io_load_error(path_apts, line, _("syntax error in item identifier"));
while ((c = getc(data_file)) == ' ') ;
ungetc(c, data_file);
} else {
- EXIT(_("wrong format in the appointment or event"));
+ io_load_error(path_apts, line,
+ _("wrong format in the appointment or event"));
/* NOTREACHED */
}
@@ -499,7 +511,7 @@ void io_load_app(void)
if (c == '{') {
is_recursive = 1;
if (fscanf(data_file, " %d%c ", &freq, &type) != 2)
- EXIT(_("syntax error in item repetition"));
+ io_load_error(path_apts, line, _("syntax error in item repetition"));
c = getc(data_file);
if (c == '}') { /* endless recurrent item */
@@ -509,7 +521,7 @@ void io_load_app(void)
} else if (c == '-' && getc(data_file) == '>') {
if (fscanf(data_file, " %d / %d / %d ", &until.tm_mon,
&until.tm_mday, &until.tm_year) != 3)
- EXIT(_("syntax error in item repetition"));
+ io_load_error(path_apts, line, _("syntax error in item repetition"));
c = getc(data_file);
if (c == '!') {
ungetc(c, data_file);
@@ -519,14 +531,15 @@ void io_load_app(void)
while ((c = getc(data_file)) == ' ') ;
ungetc(c, data_file);
} else
- EXIT(_("syntax error in item repetition"));
+ io_load_error(path_apts, line, _("syntax error in item repetition"));
} else if (c == '!') { /* endless item with exceptions */
ungetc(c, data_file);
recur_exc_scan(&exc, data_file);
c = getc(data_file);
until.tm_year = 0;
} else {
- EXIT(_("wrong format in the appointment or event"));
+ io_load_error(path_apts, line,
+ _("wrong format in the appointment or event"));
/* NOTREACHED */
}
} else
@@ -557,7 +570,7 @@ void io_load_app(void)
while ((c = getc(data_file)) == ' ') ;
ungetc(c, data_file);
} else
- EXIT(_("syntax error in item repetition"));
+ io_load_error(path_apts, line, _("syntax error in item repetition"));
if (is_recursive) {
recur_apoint_scan(data_file, start, end,
type, freq, until, notep, &exc, state);
@@ -571,7 +584,8 @@ void io_load_app(void)
event_scan(data_file, start, id, notep);
}
} else {
- EXIT(_("wrong format in the appointment or event"));
+ io_load_error(path_apts, line,
+ _("wrong format in the appointment or event"));
/* NOTREACHED */
}
}
@@ -586,17 +600,19 @@ void io_load_todo(void)
int nb_tod = 0;
int c, id;
char buf[BUFSIZ], e_todo[BUFSIZ], note[MAX_NOTESIZ + 1];
+ unsigned line = 0;
data_file = fopen(path_todo, "r");
EXIT_IF(data_file == NULL, _("failed to open todo file"));
for (;;) {
+ line++;
c = getc(data_file);
if (c == EOF)
break;
else if (c == '[') { /* new style with id */
if (fscanf(data_file, " %d ", &id) != 1 || getc(data_file) != ']')
- EXIT(_("syntax error in item identifier"));
+ io_load_error(path_todo, line, _("syntax error in item identifier"));
while ((c = getc(data_file)) == ' ') ;
ungetc(c, data_file);
} else {
@@ -777,10 +793,10 @@ void io_load_keys(const char *pager)
WARN_MSG(_("Some actions do not have any associated key bindings!"));
}
-void io_check_dir(char *dir, int *missing)
+int io_check_dir(const char *dir)
{
if (read_only)
- return;
+ return -1;
errno = 0;
if (mkdir(dir, 0700) != 0) {
@@ -788,45 +804,46 @@ void io_check_dir(char *dir, int *missing)
fprintf(stderr, _("FATAL ERROR: could not create %s: %s\n"), dir,
strerror(errno));
exit_calcurse(EXIT_FAILURE);
+ } else {
+ return 1;
}
} else {
- if (missing)
- (*missing)++;
+ return 0;
}
}
-unsigned io_file_exist(char *file)
+unsigned io_file_exist(const char *file)
{
FILE *fd;
- if (!file)
- return 0;
-
- if ((fd = fopen(file, "r")) == NULL)
+ if (file && (fd = fopen(file, "r")) != NULL) {
+ fclose(fd);
+ return 1;
+ }
+ else {
return 0;
-
- fclose(fd);
-
- return 1;
+ }
}
-void io_check_file(char *file, int *missing)
+int io_check_file(const char *file)
{
if (read_only)
- return;
+ return -1;
errno = 0;
- if (!io_file_exist(file)) {
+ if (io_file_exist(file)) {
+ return 1;
+ } else {
FILE *fd;
- if (missing)
- (*missing)++;
if ((fd = fopen(file, "w")) == NULL) {
fprintf(stderr, _("FATAL ERROR: could not create %s: %s\n"), file,
strerror(errno));
exit_calcurse(EXIT_FAILURE);
}
file_close(fd, __FILE_POS__);
+
+ return 0;
}
}
@@ -844,17 +861,15 @@ void io_check_file(char *file, int *missing)
*/
int io_check_data_files(void)
{
- int missing, missing_keys;
+ int missing = 0;
- missing = missing_keys = 0;
- errno = 0;
- io_check_dir(path_dir, &missing);
- io_check_dir(path_notes, &missing);
- io_check_file(path_todo, &missing);
- io_check_file(path_apts, &missing);
- io_check_file(path_conf, &missing);
- io_check_file(path_keys, &missing_keys);
- if (missing_keys) {
+ missing += io_check_dir(path_dir) ? 0 : 1;
+ missing += io_check_dir(path_notes) ? 0 : 1;
+ missing += io_check_file(path_todo) ? 0 : 1;
+ missing += io_check_file(path_apts) ? 0 : 1;
+ missing += io_check_file(path_conf) ? 0 : 1;
+
+ if (!io_check_file(path_keys)) {
missing++;
keys_dump_defaults(path_keys);
}
@@ -879,14 +894,13 @@ void io_startup_screen(int no_data_file)
/* Export calcurse data. */
void io_export_data(enum export_type type)
{
- FILE *stream;
+ FILE *stream = NULL;
const char *success = _("The data were successfully exported");
const char *enter = _("Press [ENTER] to continue");
if (type < IO_EXPORT_ICAL || type >= IO_EXPORT_NBTYPES)
EXIT(_("unknown export type"));
- stream = 0;
switch (ui_mode) {
case UI_CMDLINE:
stream = stdout;
@@ -913,46 +927,20 @@ void io_export_data(enum export_type type)
}
}
-/* Draws the export format selection bar */
-void io_export_bar(void)
-{
- int smlspc, spc;
-
- smlspc = 2;
- spc = 15;
-
- custom_apply_attr(win[STA].p, ATTR_HIGHEST);
- mvwaddstr(win[STA].p, 0, 2, "Q");
- mvwaddstr(win[STA].p, 1, 2, "I");
- mvwaddstr(win[STA].p, 0, 2 + spc, "P");
- custom_remove_attr(win[STA].p, ATTR_HIGHEST);
-
- mvwaddstr(win[STA].p, 0, 2 + smlspc, _("Exit"));
- mvwaddstr(win[STA].p, 1, 2 + smlspc, _("Ical"));
- mvwaddstr(win[STA].p, 0, 2 + spc + smlspc, _("Pcal"));
-
- wnoutrefresh(win[STA].p);
- wmove(win[STA].p, 0, 0);
- wins_doupdate();
-}
-
static FILE *get_import_stream(enum export_type type)
{
- FILE *stream;
+ FILE *stream = NULL;
char *stream_name;
const char *ask_fname = _("Enter the file name to import data from:");
const char *wrong_file =
_("The file cannot be accessed, please enter another file name.");
const char *press_enter = _("Press [ENTER] to continue.");
- int cancel;
- stream = NULL;
stream_name = mem_malloc(BUFSIZ);
memset(stream_name, 0, BUFSIZ);
while (stream == NULL) {
status_mesg(ask_fname, "");
- cancel = updatestring(win[STA].p, &stream_name, 0, 1);
- if (cancel) {
+ if (updatestring(win[STA].p, &stream_name, 0, 1)) {
mem_free(stream_name);
return NULL;
}
@@ -1090,13 +1078,10 @@ void io_log_print(struct io_file *log, int line, const char *msg)
void io_log_display(struct io_file *log, const char *msg, const char *pager)
{
- int ans;
-
RETURN_IF(log == NULL, _("No log file to display!"));
if (ui_mode == UI_CMDLINE) {
printf("\n%s [y/n] ", msg);
- ans = fgetc(stdin);
- if (ans == 'y') {
+ if (fgetc(stdin) == 'y') {
const char *arg[] = { pager, log->name, NULL };
int pid;
@@ -1125,9 +1110,7 @@ static pthread_t io_t_psave;
/* Thread used to periodically save data. */
static void *io_psave_thread(void *arg)
{
- int delay;
-
- delay = conf.periodic_save;
+ int delay = conf.periodic_save;
EXIT_IF(delay < 0, _("Invalid delay"));
for (;;) {