summaryrefslogtreecommitdiffstats
path: root/src/args.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/args.c')
-rw-r--r--src/args.c206
1 files changed, 40 insertions, 166 deletions
diff --git a/src/args.c b/src/args.c
index 01f24e9..85c91cb 100644
--- a/src/args.c
+++ b/src/args.c
@@ -41,7 +41,6 @@
#include <limits.h>
#include <getopt.h>
#include <time.h>
-#include <regex.h>
#include "calcurse.h"
@@ -63,7 +62,7 @@ static void usage(void)
const char *arg_usage =
_("Usage: calcurse [-g|-h|-v] [-an] [-t[num]] [-i<file>] [-x[format]]\n"
" [-d <date>|<num>] [-s[date]] [-r[range]]\n"
- " [-c<file> | -D<dir>] [-S<regex>] [--status]\n"
+ " [-c<file>] [-D<dir>] [-S<regex>] [--status]\n"
" [--read-only]\n");
fputs(arg_usage, stdout);
}
@@ -113,9 +112,9 @@ static void help_arg(void)
" Don't save configuration nor appointments/todos. Use with care.\n"
"\nFiles:\n"
" -c <file>, --calendar <file>\n"
- " specify the calendar <file> to use (incompatible with '-D').\n"
+ " specify the calendar <file> to use (has precedence over '-D').\n"
"\n -D <dir>, --directory <dir>\n"
- " specify the data directory to use (incompatible with '-c').\n"
+ " specify the data directory to use.\n"
"\tIf not specified, the default directory is ~/.calcurse\n"
"\nNon-interactive:\n"
" -a, --appointment\n"
@@ -270,11 +269,9 @@ static void next_arg(void)
static void arg_print_date(long date)
{
char date_str[BUFSIZ];
- time_t t;
struct tm lt;
- t = date;
- localtime_r(&t, &lt);
+ localtime_r((time_t *)&date, &lt);
strftime(date_str, BUFSIZ, conf.output_datefmt, &lt);
fputs(date_str, stdout);
fputs(":\n", stdout);
@@ -289,123 +286,21 @@ static void arg_print_date(long date)
static int
app_arg(int add_line, struct date *day, long date, const char *fmt_apt,
const char *fmt_rapt, const char *fmt_ev, const char *fmt_rev,
- regex_t * regex)
+ regex_t *regex)
{
- llist_item_t *i, *j;
- long today;
- unsigned print_date = 1;
- int app_found = 0;
-
if (date == 0)
- today = get_sec_date(*day);
- else
- today = date;
+ date = get_sec_date(*day);
- /*
- * Calculate and print the selected date if there is an event for
- * that date and it is the first one, and then print all the events for
- * that date.
- */
- LLIST_FIND_FOREACH(&recur_elist, today, recur_event_inday, i) {
- struct recur_event *re = LLIST_GET_DATA(i);
- if (regex && regexec(regex, re->mesg, 0, 0, 0) != 0)
- continue;
+ int n = day_store_items(date, NULL, NULL, regex);
- app_found = 1;
- if (add_line) {
+ if (n > 0) {
+ if (add_line)
fputs("\n", stdout);
- add_line = 0;
- }
- if (print_date) {
- arg_print_date(today);
- print_date = 0;
- }
- print_recur_event(fmt_rev, today, re);
- }
-
- LLIST_FIND_FOREACH_CONT(&eventlist, today, event_inday, i) {
- struct event *ev = LLIST_TS_GET_DATA(i);
- if (regex && regexec(regex, ev->mesg, 0, 0, 0) != 0)
- continue;
-
- app_found = 1;
- if (add_line) {
- fputs("\n", stdout);
- add_line = 0;
- }
- if (print_date) {
- arg_print_date(today);
- print_date = 0;
- }
- print_event(fmt_ev, today, ev);
- }
-
- /* Same process is performed but this time on the appointments. */
- LLIST_TS_LOCK(&alist_p);
- LLIST_TS_LOCK(&recur_alist_p);
-
- /*
- * Iterate over regular appointments and recurrent ones simultaneously (fixes
- * http://lists.calcurse.org/bugs/msg00002.html).
- */
- i = LLIST_TS_FIND_FIRST(&alist_p, today, apoint_inday);
- j = LLIST_TS_FIND_FIRST(&recur_alist_p, today, recur_apoint_inday);
- while (i || j) {
- struct apoint *apt = LLIST_TS_GET_DATA(i);
- struct recur_apoint *ra = LLIST_TS_GET_DATA(j);
- unsigned occurrence;
-
- while (i && regex && regexec(regex, apt->mesg, 0, 0, 0) != 0) {
- i = LLIST_TS_FIND_NEXT(i, today, apoint_inday);
- apt = LLIST_TS_GET_DATA(i);
- }
-
- while (j && regex && regexec(regex, ra->mesg, 0, 0, 0) != 0) {
- j = LLIST_TS_FIND_NEXT(j, today, recur_apoint_inday);
- ra = LLIST_TS_GET_DATA(j);
- }
-
- if (apt && ra) {
- if (recur_apoint_find_occurrence(ra, today, &occurrence) &&
- apt->start <= occurrence)
- ra = NULL;
- else
- apt = NULL;
- }
-
- if (apt) {
- app_found = 1;
- if (add_line) {
- fputs("\n", stdout);
- add_line = 0;
- }
- if (print_date) {
- arg_print_date(today);
- print_date = 0;
- }
- print_apoint(fmt_apt, today, apt);
- i = LLIST_TS_FIND_NEXT(i, today, apoint_inday);
- } else if (ra) {
- app_found = 1;
- if (add_line) {
- fputs("\n", stdout);
- add_line = 0;
- }
- if (print_date) {
- arg_print_date(today);
- print_date = 0;
- }
- recur_apoint_find_occurrence(ra, today, &occurrence);
- print_recur_apoint(fmt_rapt, today, occurrence, ra);
- apt = NULL;
- j = LLIST_TS_FIND_NEXT(j, today, recur_apoint_inday);
- }
+ arg_print_date(date);
+ day_write_stdout(date, fmt_apt, fmt_rapt, fmt_ev, fmt_rev);
}
- LLIST_TS_UNLOCK(&recur_alist_p);
- LLIST_TS_UNLOCK(&alist_p);
-
- return app_found;
+ return n;
}
/*
@@ -443,10 +338,7 @@ date_arg(const char *ddate, int add_line, const char *fmt_apt,
const char *fmt_rapt, const char *fmt_ev, const char *fmt_rev,
regex_t * regex)
{
- int i;
struct date day;
- int numdays = 0, num_digit = 0;
- int arg_len = 0;
static struct tm t;
time_t timer;
@@ -454,25 +346,18 @@ date_arg(const char *ddate, int add_line, const char *fmt_apt,
* Check (with the argument length) if a date or a number of days
* was entered, and then call app_arg() to print appointments
*/
- arg_len = strlen(ddate);
- if (arg_len <= 4) { /* a number of days was entered */
- for (i = 0; i <= arg_len - 1; i++) {
- if (isdigit(ddate[i]))
- num_digit++;
- }
- if (num_digit == arg_len)
- numdays = atoi(ddate);
-
+ if (strlen(ddate) <= 4 && is_all_digit(ddate)) {
/*
- * Get current date, and print appointments for each day
- * in the chosen interval. app_found and add_line are used
- * to format the output correctly.
+ * A number of days was entered. Get current date and print appointments
+ * for each day in the chosen interval. app_found and add_line are used to
+ * format the output correctly.
*/
timer = time(NULL);
localtime_r(&timer, &t);
- display_app(&t, numdays, add_line, fmt_apt, fmt_rapt, fmt_ev, fmt_rev,
+ display_app(&t, atoi(ddate), add_line, fmt_apt, fmt_rapt, fmt_ev, fmt_rev,
regex);
- } else { /* a date was entered */
+ } else {
+ /* A date was entered. */
if (parse_date(ddate, conf.input_datefmt, (int *)&day.yyyy,
(int *)&day.mm, (int *)&day.dd, NULL)) {
app_arg(add_line, &day, 0, fmt_apt, fmt_rapt, fmt_ev, fmt_rev, regex);
@@ -498,7 +383,7 @@ date_arg_extended(const char *startday, const char *range, int add_line,
const char *fmt_apt, const char *fmt_rapt,
const char *fmt_ev, const char *fmt_rev, regex_t * regex)
{
- int i, numdays = 1, error = 0, arg_len = 0;
+ int numdays = 1, error = 0;
static struct tm t;
time_t timer;
@@ -506,13 +391,11 @@ date_arg_extended(const char *startday, const char *range, int add_line,
* Check arguments and extract information
*/
if (range != NULL) {
- arg_len = strlen(range);
- for (i = 0; i <= arg_len - 1; i++) {
- if (!isdigit(range[i]))
- error = 1;
- }
- if (!error)
+ if (is_all_digit(range)) {
numdays = atoi(range);
+ } else {
+ error = 1;
+ }
}
timer = time(NULL);
localtime_r(&timer, &t);
@@ -548,9 +431,7 @@ int parse_args(int argc, char **argv)
int unknown_flag = 0;
/* Command-line flags */
int aflag = 0; /* -a: print appointments for current day */
- int cflag = 0; /* -c: specify the calendar file to use */
int dflag = 0; /* -d: print appointments for a specified days */
- int Dflag = 0; /* -D: specify data directory to use */
int hflag = 0; /* -h: print help text */
int gflag = 0; /* -g: run garbage collector */
int iflag = 0; /* -i: import data */
@@ -619,7 +500,6 @@ int parse_args(int argc, char **argv)
load_data++;
break;
case 'c':
- cflag = 1;
multiple_flag++;
cfile = optarg;
load_data++;
@@ -631,7 +511,6 @@ int parse_args(int argc, char **argv)
ddate = optarg;
break;
case 'D':
- Dflag = 1;
datadir = optarg;
break;
case 'h':
@@ -741,11 +620,6 @@ int parse_args(int argc, char **argv)
usage_try();
return EXIT_FAILURE;
/* Incorrect arguments */
- } else if (Dflag && cflag) {
- fputs(_("Options '-D' and '-c' cannot be used at the same time\n"), stderr);
- usage();
- usage_try();
- return EXIT_FAILURE;
} else if (Sflag && !(aflag || dflag || rflag || sflag || tflag)) {
fputs(_("Option '-S' must be used with either '-d', '-r', '-s', "
"'-a' or '-t'\n"), stderr);
@@ -767,10 +641,10 @@ int parse_args(int argc, char **argv)
non_interactive = 1;
} else if (gflag) {
io_init(cfile, datadir);
- io_check_dir(path_dir, NULL);
- io_check_dir(path_notes, NULL);
- io_check_file(path_apts, NULL);
- io_check_file(path_todo, NULL);
+ io_check_dir(path_dir);
+ io_check_dir(path_notes);
+ io_check_file(path_apts);
+ io_check_file(path_todo);
io_load_app();
io_load_todo();
note_gc();
@@ -778,12 +652,12 @@ int parse_args(int argc, char **argv)
} else if (multiple_flag) {
if (load_data) {
io_init(cfile, datadir);
- io_check_dir(path_dir, NULL);
- io_check_dir(path_notes, NULL);
+ io_check_dir(path_dir);
+ io_check_dir(path_notes);
}
if (iflag) {
- io_check_file(path_apts, NULL);
- io_check_file(path_todo, NULL);
+ io_check_file(path_apts);
+ io_check_file(path_todo);
/* Get default pager in case we need to show a log file. */
vars_init();
io_load_app();
@@ -794,8 +668,8 @@ int parse_args(int argc, char **argv)
non_interactive = 1;
}
if (xflag) {
- io_check_file(path_apts, NULL);
- io_check_file(path_todo, NULL);
+ io_check_file(path_apts);
+ io_check_file(path_todo);
io_load_app();
io_load_todo();
io_export_data(xfmt);
@@ -803,20 +677,20 @@ int parse_args(int argc, char **argv)
return non_interactive;
}
if (tflag) {
- io_check_file(path_todo, NULL);
+ io_check_file(path_todo);
io_load_todo();
todo_arg(tnum, fmt_todo, preg);
non_interactive = 1;
}
if (nflag) {
- io_check_file(path_apts, NULL);
+ io_check_file(path_apts);
io_load_app();
next_arg();
non_interactive = 1;
}
if (dflag || rflag || sflag) {
- io_check_file(path_apts, NULL);
- io_check_file(path_conf, NULL);
+ io_check_file(path_apts);
+ io_check_file(path_conf);
io_load_app();
config_load(); /* To get output date format. */
if (dflag)
@@ -828,8 +702,8 @@ int parse_args(int argc, char **argv)
} else if (aflag) {
struct date day;
- io_check_file(path_apts, NULL);
- io_check_file(path_conf, NULL);
+ io_check_file(path_apts);
+ io_check_file(path_conf);
vars_init();
config_load(); /* To get output date format. */
io_load_app();