comparison cmd.c @ 40:e3ad9859c51d

Show only groups of matching entries when using ls with filters
author Guido Berhoerster <guido+pwm@berhoerster.name>
date Fri, 09 Aug 2019 14:04:46 +0200
parents 8b55f7b1c6b3
children 0af8d2d8cd1a
comparison
equal deleted inserted replaced
39:131c35592054 40:e3ad9859c51d
235 *idp = (unsigned int)x; 235 *idp = (unsigned int)x;
236 236
237 return (0); 237 return (0);
238 } 238 }
239 239
240 static inline int
241 filter_matches(regex_t *filter_re, const char *s)
242 {
243 if (filter_re == NULL) {
244 return (1);
245 }
246 if (s == NULL) {
247 return (0);
248 }
249 return (regexec(filter_re, s, 0, NULL, 0) == 0);
250 }
251
240 static enum cmd_return 252 static enum cmd_return
241 cmd_set(struct pwm_ctx *ctx, int argc, char *argv[]) 253 cmd_set(struct pwm_ctx *ctx, int argc, char *argv[])
242 { 254 {
243 enum option_type type; 255 enum option_type type;
244 char *value; 256 char *value;
421 char *value; 433 char *value;
422 regex_t **repp; 434 regex_t **repp;
423 int errcode; 435 int errcode;
424 char *errbuf; 436 char *errbuf;
425 size_t errbuf_size; 437 size_t errbuf_size;
438 int is_filtered;
426 struct pager *pager = NULL; 439 struct pager *pager = NULL;
427 union list_item **list = NULL; 440 union list_item **list = NULL;
428 size_t j; 441 size_t j;
429 const char *group; 442 const char *group = NULL;
430 const char *title;
431 const char *username;
432 const char *notes;
433 const char *url;
434 struct record *record; 443 struct record *record;
435 444
436 for (i = 1; i < argc; i++) { 445 for (i = 1; i < argc; i++) {
437 type = parse_arg(argv[i], field_namev, '~', &value); 446 type = parse_arg(argv[i], field_namev, '~', &value);
438 if ((type >= 0) && ((value == NULL) || (value[0] == '\0'))) { 447 if ((type >= 0) && ((value == NULL) || (value[0] == '\0'))) {
478 487
479 goto out; 488 goto out;
480 } 489 }
481 } 490 }
482 491
492 is_filtered = ((group_re != NULL) || (title_re != NULL) ||
493 (username_re != NULL) || (notes_re != NULL) || (url_re != NULL));
483 pager = pager_create(STDOUT_FILENO); 494 pager = pager_create(STDOUT_FILENO);
484 list = pwfile_create_list(ctx); 495 list = pwfile_create_list(ctx);
485 for (j = 0; list[j] != NULL; j++) { 496 for (j = 0; list[j] != NULL; j++) {
486 if (list[j]->any.type == ITEM_TYPE_GROUP) { 497 if (list[j]->any.type == ITEM_TYPE_GROUP) {
487 pager_printf(pager, "[%s]\n", list[j]->group.group); 498 group = list[j]->group.group;
488 } else { 499 if (!is_filtered) {
500 pager_printf(pager, "[%s]\n", group);
501 }
502 } else if (is_filtered) {
489 record = pwfile_get_record(ctx, list[j]->record.id); 503 record = pwfile_get_record(ctx, list[j]->record.id);
490 group = (record->group != NULL) ? record->group : ""; 504 if (filter_matches(group_re, record->group) &&
491 title = (record->title != NULL) ? record->title : ""; 505 filter_matches(title_re, record->title) &&
492 username = (record->username != NULL) ? 506 filter_matches(username_re, record->username) &&
493 record->username : ""; 507 filter_matches(notes_re, record->notes) &&
494 notes = (record->notes != NULL) ? record->notes : ""; 508 filter_matches(url_re, record->url)) {
495 url = (record->url != NULL) ? record->url : ""; 509 if (group != NULL) {
496 if (((group_re == NULL) || 510 pager_printf(pager, "[%s]\n", group);
497 (regexec(group_re, group, 0, NULL, 0) == 0)) && 511 group = NULL;
498 ((title_re == NULL) || 512 }
499 (regexec(title_re, title, 0, NULL, 0) == 0)) &&
500 ((username_re == NULL) ||
501 (regexec(username_re, username, 0, NULL,
502 0) == 0)) &&
503 ((notes_re == NULL) ||
504 (regexec(notes_re, notes, 0, NULL, 0) == 0)) &&
505 ((url_re == NULL) ||
506 (regexec(url_re, url, 0, NULL, 0) == 0))) {
507 pager_printf(pager, "%4u %s\n", 513 pager_printf(pager, "%4u %s\n",
508 list[j]->record.id, 514 list[j]->record.id,
509 (list[j]->record.title != NULL) ? 515 (list[j]->record.title != NULL) ?
510 list[j]->record.title : ""); 516 list[j]->record.title : "");
511 } 517 }
512 pwfile_destroy_record(record); 518 pwfile_destroy_record(record);
519 } else {
520 pager_printf(pager, "%4u %s\n", list[j]->record.id,
521 (list[j]->record.title != NULL) ?
522 list[j]->record.title : "");
513 } 523 }
514 } 524 }
515 retval = (pager_show(pager) != IO_SIGNAL) ? CMD_OK : CMD_SIGNAL; 525 retval = (pager_show(pager) != IO_SIGNAL) ? CMD_OK : CMD_SIGNAL;
516 526
517 out: 527 out: