projects/pwm

changeset 40:e3ad9859c51d

Show only groups of matching entries when using ls with filters
author Guido Berhoerster <guido+pwm@berhoerster.name>
date Fri Aug 09 14:04:46 2019 +0200 (14 months ago)
parents 131c35592054
children 0af8d2d8cd1a
files cmd.c
line diff
     1.1 --- a/cmd.c	Thu Aug 08 22:48:09 2019 +0200
     1.2 +++ b/cmd.c	Fri Aug 09 14:04:46 2019 +0200
     1.3 @@ -237,6 +237,18 @@
     1.4  	return (0);
     1.5  }
     1.6  
     1.7 +static inline int
     1.8 +filter_matches(regex_t *filter_re, const char *s)
     1.9 +{
    1.10 +	if (filter_re == NULL) {
    1.11 +		return (1);
    1.12 +	}
    1.13 +	if (s == NULL) {
    1.14 +		return (0);
    1.15 +	}
    1.16 +	return (regexec(filter_re, s, 0, NULL, 0) == 0);
    1.17 +}
    1.18 +
    1.19  static enum cmd_return
    1.20  cmd_set(struct pwm_ctx *ctx, int argc, char *argv[])
    1.21  {
    1.22 @@ -423,14 +435,11 @@
    1.23  	int		errcode;
    1.24  	char		*errbuf;
    1.25  	size_t		errbuf_size;
    1.26 +	int		is_filtered;
    1.27  	struct pager	*pager = NULL;
    1.28  	union list_item	**list = NULL;
    1.29  	size_t		j;
    1.30 -	const char	*group;
    1.31 -	const char	*title;
    1.32 -	const char	*username;
    1.33 -	const char	*notes;
    1.34 -	const char	*url;
    1.35 +	const char	*group = NULL;
    1.36  	struct record	*record;
    1.37  
    1.38  	for (i = 1; i < argc; i++) {
    1.39 @@ -480,36 +489,37 @@
    1.40  		}
    1.41  	}
    1.42  
    1.43 +	is_filtered = ((group_re != NULL) || (title_re != NULL) ||
    1.44 +	    (username_re != NULL) || (notes_re != NULL) || (url_re != NULL));
    1.45  	pager = pager_create(STDOUT_FILENO);
    1.46  	list = pwfile_create_list(ctx);
    1.47  	for (j = 0; list[j] != NULL; j++) {
    1.48  		if (list[j]->any.type == ITEM_TYPE_GROUP) {
    1.49 -			pager_printf(pager, "[%s]\n", list[j]->group.group);
    1.50 -		} else {
    1.51 +			group = list[j]->group.group;
    1.52 +			if (!is_filtered) {
    1.53 +				pager_printf(pager, "[%s]\n", group);
    1.54 +			}
    1.55 +		} else if (is_filtered) {
    1.56  			record = pwfile_get_record(ctx, list[j]->record.id);
    1.57 -			group = (record->group != NULL) ? record->group : "";
    1.58 -			title = (record->title != NULL) ? record->title : "";
    1.59 -			username = (record->username != NULL) ?
    1.60 -			    record->username : "";
    1.61 -			notes = (record->notes != NULL) ? record->notes : "";
    1.62 -			url = (record->url != NULL) ? record->url : "";
    1.63 -			if (((group_re == NULL) ||
    1.64 -			    (regexec(group_re, group, 0, NULL, 0) == 0)) &&
    1.65 -			    ((title_re == NULL) ||
    1.66 -			    (regexec(title_re, title, 0, NULL, 0) == 0)) &&
    1.67 -			    ((username_re == NULL) ||
    1.68 -			    (regexec(username_re, username, 0, NULL,
    1.69 -			    0) == 0)) &&
    1.70 -			    ((notes_re == NULL) ||
    1.71 -			    (regexec(notes_re, notes, 0, NULL, 0) == 0)) &&
    1.72 -			    ((url_re == NULL) ||
    1.73 -			    (regexec(url_re, url, 0, NULL, 0) == 0))) {
    1.74 +			if (filter_matches(group_re, record->group) &&
    1.75 +			    filter_matches(title_re, record->title) &&
    1.76 +			    filter_matches(username_re, record->username) &&
    1.77 +			    filter_matches(notes_re, record->notes) &&
    1.78 +			    filter_matches(url_re, record->url)) {
    1.79 +				if (group != NULL) {
    1.80 +					pager_printf(pager, "[%s]\n", group);
    1.81 +					group = NULL;
    1.82 +				}
    1.83  				pager_printf(pager, "%4u %s\n",
    1.84  				    list[j]->record.id,
    1.85  				    (list[j]->record.title != NULL) ?
    1.86  				    list[j]->record.title : "");
    1.87  			}
    1.88  			pwfile_destroy_record(record);
    1.89 +		} else {
    1.90 +			pager_printf(pager, "%4u %s\n", list[j]->record.id,
    1.91 +			    (list[j]->record.title != NULL) ?
    1.92 +			    list[j]->record.title : "");
    1.93  		}
    1.94  	}
    1.95  	retval = (pager_show(pager) != IO_SIGNAL) ? CMD_OK : CMD_SIGNAL;