Add pattern filter option

This adds a new item filter option --filter-pattern and removes the
whole -S parameter logic, while making -S an alias for --filter-pattern.

Signed-off-by: Lukas Fleischer <calcurse@cryptocrack.de>
This commit is contained in:
Lukas Fleischer 2014-08-06 09:26:46 +02:00
parent bfe73d0e5d
commit 9ce5861468
6 changed files with 51 additions and 56 deletions

View File

@ -198,6 +198,8 @@ struct apoint *apoint_scan(FILE * f, struct tm start, struct tm end,
if (filter) { if (filter) {
if (!(filter->type_mask & TYPE_MASK_APPT)) if (!(filter->type_mask & TYPE_MASK_APPT))
return NULL; return NULL;
if (filter->regex && regexec(filter->regex, buf, 0, 0, 0))
return NULL;
if (filter->start_from >= 0 && tstart < filter->start_from) if (filter->start_from >= 0 && tstart < filter->start_from)
return NULL; return NULL;
if (filter->start_to >= 0 && tstart > filter->start_to) if (filter->start_to >= 0 && tstart > filter->start_to)

View File

@ -47,6 +47,7 @@
/* Long options */ /* Long options */
enum { enum {
OPT_FILTER_TYPE = 1000, OPT_FILTER_TYPE = 1000,
OPT_FILTER_PATTERN,
OPT_FILTER_START_FROM, OPT_FILTER_START_FROM,
OPT_FILTER_START_TO, OPT_FILTER_START_TO,
OPT_FILTER_START_AFTER, OPT_FILTER_START_AFTER,
@ -214,10 +215,8 @@ static void status_arg(void)
* then only todo items that have this priority will be displayed. * then only todo items that have this priority will be displayed.
* If priority is < 0, all todos will be displayed. * If priority is < 0, all todos will be displayed.
* If priority == 0, only completed tasks will be displayed. * If priority == 0, only completed tasks will be displayed.
* If regex is not null, only the matching todos are printed.
*/ */
static void todo_arg(int priority, const char *format, regex_t * regex, static void todo_arg(int priority, const char *format, int *limit)
int *limit)
{ {
llist_item_t *i; llist_item_t *i;
int title = 1; int title = 1;
@ -239,8 +238,6 @@ static void todo_arg(int priority, const char *format, regex_t * regex,
if (*limit == 0) if (*limit == 0)
return; return;
struct todo *todo = LLIST_TS_GET_DATA(i); struct todo *todo = LLIST_TS_GET_DATA(i);
if (regex && regexec(regex, todo->mesg, 0, 0, 0) != 0)
continue;
if (todo->id < 0) { /* completed task */ if (todo->id < 0) { /* completed task */
if (priority == 0) { if (priority == 0) {
@ -304,19 +301,18 @@ static void arg_print_date(long date)
* Print appointments for given day and exit. * Print appointments for given day and exit.
* If no day is given, the given date is used. * If no day is given, the given date is used.
* If there is also no date given, current date is considered. * If there is also no date given, current date is considered.
* If regex is not null, only the matching appointments or events are printed.
*/ */
static int static int
app_arg(int add_line, struct date *day, long date, const char *fmt_apt, 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, const char *fmt_rapt, const char *fmt_ev, const char *fmt_rev,
regex_t * regex, int *limit) int *limit)
{ {
if (*limit == 0) if (*limit == 0)
return 0; return 0;
if (date == 0) if (date == 0)
date = get_sec_date(*day); date = get_sec_date(*day);
day_store_items(date, regex, 0); day_store_items(date, 0);
int n = day_item_count(0); int n = day_item_count(0);
@ -339,7 +335,7 @@ app_arg(int add_line, struct date *day, long date, const char *fmt_apt,
static void static void
display_app(struct tm *t, int numdays, int add_line, const char *fmt_apt, display_app(struct tm *t, int numdays, int add_line, const char *fmt_apt,
const char *fmt_rapt, const char *fmt_ev, const char *fmt_rev, const char *fmt_rapt, const char *fmt_ev, const char *fmt_rev,
regex_t * regex, int *limit) int *limit)
{ {
int i, app_found; int i, app_found;
struct date day; struct date day;
@ -350,7 +346,7 @@ display_app(struct tm *t, int numdays, int add_line, const char *fmt_apt,
day.yyyy = t->tm_year + 1900; day.yyyy = t->tm_year + 1900;
app_found = app_found =
app_arg(add_line, &day, 0, fmt_apt, fmt_rapt, fmt_ev, app_arg(add_line, &day, 0, fmt_apt, fmt_rapt, fmt_ev,
fmt_rev, regex, limit); fmt_rev, limit);
if (app_found) if (app_found)
add_line = 1; add_line = 1;
t->tm_mday++; t->tm_mday++;
@ -365,7 +361,7 @@ display_app(struct tm *t, int numdays, int add_line, const char *fmt_apt,
static void static void
date_arg(const char *ddate, int add_line, const char *fmt_apt, date_arg(const char *ddate, int add_line, const char *fmt_apt,
const char *fmt_rapt, const char *fmt_ev, const char *fmt_rev, const char *fmt_rapt, const char *fmt_ev, const char *fmt_rev,
regex_t * regex, int *limit) int *limit)
{ {
struct date day; struct date day;
static struct tm t; static struct tm t;
@ -384,13 +380,13 @@ date_arg(const char *ddate, int add_line, const char *fmt_apt,
timer = time(NULL); timer = time(NULL);
localtime_r(&timer, &t); localtime_r(&timer, &t);
display_app(&t, atoi(ddate), add_line, fmt_apt, fmt_rapt, display_app(&t, atoi(ddate), add_line, fmt_apt, fmt_rapt,
fmt_ev, fmt_rev, regex, limit); fmt_ev, fmt_rev, limit);
} else { } else {
/* A date was entered. */ /* A date was entered. */
if (parse_date(ddate, conf.input_datefmt, (int *)&day.yyyy, if (parse_date(ddate, conf.input_datefmt, (int *)&day.yyyy,
(int *)&day.mm, (int *)&day.dd, NULL)) { (int *)&day.mm, (int *)&day.dd, NULL)) {
app_arg(add_line, &day, 0, fmt_apt, fmt_rapt, app_arg(add_line, &day, 0, fmt_apt, fmt_rapt,
fmt_ev, fmt_rev, regex, limit); fmt_ev, fmt_rev, limit);
} else { } else {
fputs(_("Argument to the '-d' flag is not valid\n"), fputs(_("Argument to the '-d' flag is not valid\n"),
stderr); stderr);
@ -413,8 +409,7 @@ date_arg(const char *ddate, int add_line, const char *fmt_apt,
static void static void
date_arg_extended(const char *startday, const char *range, int add_line, date_arg_extended(const char *startday, const char *range, int add_line,
const char *fmt_apt, const char *fmt_rapt, const char *fmt_apt, const char *fmt_rapt,
const char *fmt_ev, const char *fmt_rev, regex_t * regex, const char *fmt_ev, const char *fmt_rev, int *limit)
int *limit)
{ {
int numdays = 1, error = 0; int numdays = 1, error = 0;
static struct tm t; static struct tm t;
@ -445,7 +440,7 @@ date_arg_extended(const char *startday, const char *range, int add_line,
} }
if (!error) { if (!error) {
display_app(&t, numdays, add_line, fmt_apt, fmt_rapt, display_app(&t, numdays, add_line, fmt_apt, fmt_rapt,
fmt_ev, fmt_rev, regex, limit); fmt_ev, fmt_rev, limit);
} else { } else {
fputs(_("Argument is not valid\n"), stderr); fputs(_("Argument is not valid\n"), stderr);
fprintf(stdout, fprintf(stdout,
@ -525,7 +520,7 @@ int parse_args(int argc, char **argv)
int vflag = 0; /* -v: print version number */ int vflag = 0; /* -v: print version number */
int xflag = 0; /* -x: export data */ int xflag = 0; /* -x: export data */
/* Filters */ /* Filters */
struct item_filter filter = { TYPE_MASK_ALL, -1, -1, -1, -1 }; struct item_filter filter = { TYPE_MASK_ALL, NULL, -1, -1, -1, -1 };
/* Format strings */ /* Format strings */
const char *fmt_apt = " - %S -> %E\n\t%m\n"; const char *fmt_apt = " - %S -> %E\n\t%m\n";
const char *fmt_rapt = " - %S -> %E\n\t%m\n"; const char *fmt_rapt = " - %S -> %E\n\t%m\n";
@ -539,7 +534,7 @@ int parse_args(int argc, char **argv)
const char *ddate = "", *cfile = NULL, *range = NULL, *startday = const char *ddate = "", *cfile = NULL, *range = NULL, *startday =
NULL; NULL;
const char *datadir = NULL, *ifile = NULL; const char *datadir = NULL, *ifile = NULL;
regex_t reg, *preg = NULL; regex_t reg;
/* Long options only */ /* Long options only */
int statusflag = 0; /* --status: get the status of running instances */ int statusflag = 0; /* --status: get the status of running instances */
@ -569,6 +564,7 @@ int parse_args(int argc, char **argv)
{"export", optional_argument, NULL, 'x'}, {"export", optional_argument, NULL, 'x'},
{"filter-type", required_argument, NULL, OPT_FILTER_TYPE}, {"filter-type", required_argument, NULL, OPT_FILTER_TYPE},
{"filter-pattern", required_argument, NULL, OPT_FILTER_PATTERN},
{"filter-start-from", required_argument, NULL, OPT_FILTER_START_FROM}, {"filter-start-from", required_argument, NULL, OPT_FILTER_START_FROM},
{"filter-start-to", required_argument, NULL, OPT_FILTER_START_TO}, {"filter-start-to", required_argument, NULL, OPT_FILTER_START_TO},
{"filter-start-after", required_argument, NULL, OPT_FILTER_START_AFTER}, {"filter-start-after", required_argument, NULL, OPT_FILTER_START_AFTER},
@ -643,14 +639,6 @@ int parse_args(int argc, char **argv)
load_data++; load_data++;
startday = optarg; startday = optarg;
break; break;
case 'S':
EXIT_IF(Sflag > 0,
_("Can not handle more than one regular expression."));
Sflag = 1;
if (regcomp(&reg, optarg, REG_EXTENDED))
EXIT(_("Could not compile regular expression."));
preg = &reg;
break;
case 't': case 't':
tflag = 1; tflag = 1;
multiple_flag++; multiple_flag++;
@ -696,6 +684,16 @@ int parse_args(int argc, char **argv)
EXIT_IF(filter.type_mask == 0, EXIT_IF(filter.type_mask == 0,
_("invalid filter mask")); _("invalid filter mask"));
break; break;
case 'S':
Sflag = 1;
/* FALLTHROUGH */
case OPT_FILTER_PATTERN:
EXIT_IF(filter.regex,
_("Can not handle more than one regular expression."));
if (regcomp(&reg, optarg, REG_EXTENDED))
EXIT(_("Could not compile regular expression."));
filter.regex = &reg;
break;
case OPT_FILTER_START_FROM: case OPT_FILTER_START_FROM:
filter.start_from = parse_datearg(optarg); filter.start_from = parse_datearg(optarg);
EXIT_IF(filter.start_from == -1, EXIT_IF(filter.start_from == -1,
@ -834,7 +832,7 @@ int parse_args(int argc, char **argv)
if (tflag) { if (tflag) {
io_check_file(path_todo); io_check_file(path_todo);
io_load_todo(); io_load_todo();
todo_arg(tnum, fmt_todo, preg, &limit); todo_arg(tnum, fmt_todo, &limit);
non_interactive = 1; non_interactive = 1;
} }
if (nflag) { if (nflag) {
@ -851,14 +849,13 @@ int parse_args(int argc, char **argv)
if (dflag) if (dflag)
date_arg(ddate, add_line, fmt_apt, date_arg(ddate, add_line, fmt_apt,
fmt_rapt, fmt_ev, fmt_rev, fmt_rapt, fmt_ev, fmt_rev,
preg, &limit); &limit);
if (rflag || sflag) if (rflag || sflag)
date_arg_extended(startday, range, date_arg_extended(startday, range,
add_line, add_line,
fmt_apt, fmt_apt,
fmt_rapt, fmt_ev, fmt_rapt, fmt_ev,
fmt_rev, preg, fmt_rev, &limit);
&limit);
non_interactive = 1; non_interactive = 1;
} else if (aflag) { } else if (aflag) {
struct date day; struct date day;
@ -870,7 +867,7 @@ int parse_args(int argc, char **argv)
io_load_app(&filter); io_load_app(&filter);
day.dd = day.mm = day.yyyy = 0; day.dd = day.mm = day.yyyy = 0;
app_arg(add_line, &day, 0, fmt_apt, fmt_rapt, app_arg(add_line, &day, 0, fmt_apt, fmt_rapt,
fmt_ev, fmt_rev, preg, &limit); fmt_ev, fmt_rev, &limit);
non_interactive = 1; non_interactive = 1;
} }
} else { } else {
@ -879,8 +876,9 @@ int parse_args(int argc, char **argv)
} }
} }
if (preg) /* Free filter parameters. */
regfree(preg); if (filter.regex)
regfree(filter.regex);
return non_interactive; return non_interactive;
} }

View File

@ -394,6 +394,7 @@ enum day_item_type {
/* Filter settings. */ /* Filter settings. */
struct item_filter { struct item_filter {
int type_mask; int type_mask;
regex_t *regex;
long start_from; long start_from;
long start_to; long start_to;
long end_from; long end_from;
@ -722,7 +723,7 @@ long day_item_get_duration(struct day_item *);
int day_item_get_state(struct day_item *); int day_item_get_state(struct day_item *);
void day_item_add_exc(struct day_item *, long); void day_item_add_exc(struct day_item *, long);
void day_item_fork(struct day_item *, struct day_item *); void day_item_fork(struct day_item *, struct day_item *);
void day_store_items(long, regex_t *, int); void day_store_items(long, int);
void day_process_storage(struct date *, unsigned); void day_process_storage(struct date *, unsigned);
void day_display_item_date(struct day_item *, WINDOW *, int, long, int, int); void day_display_item_date(struct day_item *, WINDOW *, int, long, int, int);
void day_display_item(struct day_item *, WINDOW *, int, int, int, int); void day_display_item(struct day_item *, WINDOW *, int, int, int, int);

View File

@ -218,7 +218,7 @@ void day_item_fork(struct day_item *day_in, struct day_item *day_out)
* dedicated to the selected day. * dedicated to the selected day.
* Returns the number of events for the selected day. * Returns the number of events for the selected day.
*/ */
static int day_store_events(long date, regex_t * regex) static int day_store_events(long date)
{ {
llist_item_t *i; llist_item_t *i;
union aptev_ptr p; union aptev_ptr p;
@ -227,9 +227,6 @@ static int day_store_events(long date, regex_t * regex)
LLIST_FIND_FOREACH_CONT(&eventlist, &date, event_inday, i) { LLIST_FIND_FOREACH_CONT(&eventlist, &date, event_inday, i) {
struct event *ev = LLIST_TS_GET_DATA(i); struct event *ev = LLIST_TS_GET_DATA(i);
if (regex && regexec(regex, ev->mesg, 0, 0, 0) != 0)
continue;
p.ev = ev; p.ev = ev;
day_add_item(EVNT, ev->day, p); day_add_item(EVNT, ev->day, p);
e_nb++; e_nb++;
@ -245,7 +242,7 @@ static int day_store_events(long date, regex_t * regex)
* dedicated to the selected day. * dedicated to the selected day.
* Returns the number of recurrent events for the selected day. * Returns the number of recurrent events for the selected day.
*/ */
static int day_store_recur_events(long date, regex_t * regex) static int day_store_recur_events(long date)
{ {
llist_item_t *i; llist_item_t *i;
union aptev_ptr p; union aptev_ptr p;
@ -254,9 +251,6 @@ static int day_store_recur_events(long date, regex_t * regex)
LLIST_FIND_FOREACH(&recur_elist, &date, recur_event_inday, i) { LLIST_FIND_FOREACH(&recur_elist, &date, recur_event_inday, i) {
struct recur_event *rev = LLIST_TS_GET_DATA(i); struct recur_event *rev = LLIST_TS_GET_DATA(i);
if (regex && regexec(regex, rev->mesg, 0, 0, 0) != 0)
continue;
p.rev = rev; p.rev = rev;
day_add_item(RECUR_EVNT, rev->day, p); day_add_item(RECUR_EVNT, rev->day, p);
e_nb++; e_nb++;
@ -272,7 +266,7 @@ static int day_store_recur_events(long date, regex_t * regex)
* structure dedicated to the selected day. * structure dedicated to the selected day.
* Returns the number of appointments for the selected day. * Returns the number of appointments for the selected day.
*/ */
static int day_store_apoints(long date, regex_t * regex) static int day_store_apoints(long date)
{ {
llist_item_t *i; llist_item_t *i;
union aptev_ptr p; union aptev_ptr p;
@ -282,9 +276,6 @@ static int day_store_apoints(long date, regex_t * regex)
LLIST_TS_FIND_FOREACH(&alist_p, &date, apoint_inday, i) { LLIST_TS_FIND_FOREACH(&alist_p, &date, apoint_inday, i) {
struct apoint *apt = LLIST_TS_GET_DATA(i); struct apoint *apt = LLIST_TS_GET_DATA(i);
if (regex && regexec(regex, apt->mesg, 0, 0, 0) != 0)
continue;
p.apt = apt; p.apt = apt;
if (apt->start >= date + DAYINSEC) if (apt->start >= date + DAYINSEC)
@ -305,7 +296,7 @@ static int day_store_apoints(long date, regex_t * regex)
* structure dedicated to the selected day. * structure dedicated to the selected day.
* Returns the number of recurrent appointments for the selected day. * Returns the number of recurrent appointments for the selected day.
*/ */
static int day_store_recur_apoints(long date, regex_t * regex) static int day_store_recur_apoints(long date)
{ {
llist_item_t *i; llist_item_t *i;
union aptev_ptr p; union aptev_ptr p;
@ -315,9 +306,6 @@ static int day_store_recur_apoints(long date, regex_t * regex)
LLIST_TS_FIND_FOREACH(&recur_alist_p, &date, recur_apoint_inday, i) { LLIST_TS_FIND_FOREACH(&recur_alist_p, &date, recur_apoint_inday, i) {
struct recur_apoint *rapt = LLIST_TS_GET_DATA(i); struct recur_apoint *rapt = LLIST_TS_GET_DATA(i);
if (regex && regexec(regex, rapt->mesg, 0, 0, 0) != 0)
continue;
p.rapt = rapt; p.rapt = rapt;
unsigned real_start; unsigned real_start;
@ -340,7 +328,7 @@ static int day_store_recur_apoints(long date, regex_t * regex)
* The number of events and appointments in the current day are also updated. * The number of events and appointments in the current day are also updated.
*/ */
void void
day_store_items(long date, regex_t * regex, int include_captions) day_store_items(long date, int include_captions)
{ {
unsigned apts, events; unsigned apts, events;
union aptev_ptr p = { NULL }; union aptev_ptr p = { NULL };
@ -351,10 +339,10 @@ day_store_items(long date, regex_t * regex, int include_captions)
if (include_captions) if (include_captions)
day_add_item(DAY_HEADING, 0, p); day_add_item(DAY_HEADING, 0, p);
events = day_store_recur_events(date, regex); events = day_store_recur_events(date);
events += day_store_events(date, regex); events += day_store_events(date);
apts = day_store_recur_apoints(date, regex); apts = day_store_recur_apoints(date);
apts += day_store_apoints(date, regex); apts += day_store_apoints(date);
if (include_captions && events > 0 && apts > 0) if (include_captions && events > 0 && apts > 0)
day_add_item(DAY_SEPARATOR, 0, p); day_add_item(DAY_SEPARATOR, 0, p);
@ -385,7 +373,7 @@ void day_process_storage(struct date *slctd_date, unsigned day_changed)
delwin(apad.ptrwin); delwin(apad.ptrwin);
/* Store the events and appointments (recursive and normal items). */ /* Store the events and appointments (recursive and normal items). */
day_store_items(date, NULL, 1); day_store_items(date, 1);
} }
/* /*

View File

@ -152,6 +152,8 @@ struct event *event_scan(FILE * f, struct tm start, int id, char *note,
if (filter) { if (filter) {
if (!(filter->type_mask & TYPE_MASK_EVNT)) if (!(filter->type_mask & TYPE_MASK_EVNT))
return NULL; return NULL;
if (filter->regex && regexec(filter->regex, buf, 0, 0, 0))
return NULL;
if (filter->start_from >= 0 && tstart < filter->start_from) if (filter->start_from >= 0 && tstart < filter->start_from)
return NULL; return NULL;
if (filter->start_to >= 0 && tstart > filter->start_to) if (filter->start_to >= 0 && tstart > filter->start_to)

View File

@ -380,6 +380,8 @@ struct recur_apoint *recur_apoint_scan(FILE * f, struct tm start,
if (filter) { if (filter) {
if (!(filter->type_mask & TYPE_MASK_RECUR_APPT)) if (!(filter->type_mask & TYPE_MASK_RECUR_APPT))
return NULL; return NULL;
if (filter->regex && regexec(filter->regex, buf, 0, 0, 0))
return NULL;
if (filter->start_from >= 0 && tstart < filter->start_from) if (filter->start_from >= 0 && tstart < filter->start_from)
return NULL; return NULL;
if (filter->start_to >= 0 && tstart > filter->start_to) if (filter->start_to >= 0 && tstart > filter->start_to)
@ -437,6 +439,8 @@ struct recur_event *recur_event_scan(FILE * f, struct tm start, int id,
if (filter) { if (filter) {
if (!(filter->type_mask & TYPE_MASK_RECUR_EVNT)) if (!(filter->type_mask & TYPE_MASK_RECUR_EVNT))
return NULL; return NULL;
if (filter->regex && regexec(filter->regex, buf, 0, 0, 0))
return NULL;
if (filter->start_from >= 0 && tstart < filter->start_from) if (filter->start_from >= 0 && tstart < filter->start_from)
return NULL; return NULL;
if (filter->start_to >= 0 && tstart > filter->start_to) if (filter->start_to >= 0 && tstart > filter->start_to)