comparison cmd.c @ 17:a08ef0674d8e

Page long output in interactive mode
author Guido Berhoerster <guido+pwm@berhoerster.name>
date Sat, 12 Aug 2017 10:41:52 +0200
parents a07665727c19
children 1e39a251cbe9
comparison
equal deleted inserted replaced
16:a07665727c19 17:a08ef0674d8e
37 #include <string.h> 37 #include <string.h>
38 #include <time.h> 38 #include <time.h>
39 #include <unistd.h> 39 #include <unistd.h>
40 40
41 #include "cmd.h" 41 #include "cmd.h"
42 #include "pager.h"
42 #include "pw.h" 43 #include "pw.h"
43 #include "pwfile.h" 44 #include "pwfile.h"
44 #include "util.h" 45 #include "util.h"
45 46
46 #define TIME_FORMAT "%Y-%m-%dT%TZ" 47 #define TIME_FORMAT "%Y-%m-%dT%TZ"
234 235
235 static enum cmd_return 236 static enum cmd_return
236 cmd_info(struct pwm_ctx *ctx, int argc, char *argv[]) 237 cmd_info(struct pwm_ctx *ctx, int argc, char *argv[])
237 { 238 {
238 struct metadata *metadata; 239 struct metadata *metadata;
240 struct pager *pager;
239 struct tm *tm; 241 struct tm *tm;
240 char timebuf[TIME_SIZE]; 242 char timebuf[TIME_SIZE];
241 243
242 if (argc != 1) { 244 if (argc != 1) {
243 return (CMD_USAGE); 245 return (CMD_USAGE);
244 } 246 }
245 247
246 metadata = pwfile_get_metadata(ctx); 248 metadata = pwfile_get_metadata(ctx);
247 printf("Format: 0x%04x\n", metadata->version); 249
250 pager = pager_create(stdout);
251 pager_printf(pager, "Format: 0x%04x\n", metadata->version);
248 if (metadata->user != NULL) { 252 if (metadata->user != NULL) {
249 printf("User: %s\n", metadata->user); 253 pager_printf(pager, "User: %s\n", metadata->user);
250 } 254 }
251 if (metadata->user != NULL) { 255 if (metadata->user != NULL) {
252 printf("Host: %s\n", metadata->host); 256 pager_printf(pager, "Host: %s\n", metadata->host);
253 } 257 }
254 if (metadata->user != NULL) { 258 if (metadata->user != NULL) {
255 printf("Application: %s\n", metadata->application); 259 pager_printf(pager, "Application: %s\n", metadata->application);
256 } 260 }
257 tm = gmtime(&metadata->timestamp); 261 tm = gmtime(&metadata->timestamp);
258 strftime(timebuf, sizeof (timebuf), TIME_FORMAT, tm); 262 strftime(timebuf, sizeof (timebuf), TIME_FORMAT, tm);
259 printf("Last Saved: %s\n", timebuf); 263 pager_printf(pager, "Last Saved: %s\n", timebuf);
264 pager_show(pager);
265 pager_destroy(pager);
260 266
261 pwfile_destroy_metadata(metadata); 267 pwfile_destroy_metadata(metadata);
262 268
263 return (CMD_OK); 269 return (CMD_OK);
264 } 270 }
277 char *value; 283 char *value;
278 regex_t **repp; 284 regex_t **repp;
279 int errcode; 285 int errcode;
280 char *errbuf; 286 char *errbuf;
281 size_t errbuf_size; 287 size_t errbuf_size;
288 struct pager *pager = NULL;
282 union list_item **list = NULL; 289 union list_item **list = NULL;
283 size_t j; 290 size_t j;
284 struct record *record; 291 struct record *record;
285 292
286 for (i = 1; i < argc; i++) { 293 for (i = 1; i < argc; i++) {
332 339
333 goto out; 340 goto out;
334 } 341 }
335 } 342 }
336 343
344 pager = pager_create(stdout);
337 list = pwfile_create_list(ctx); 345 list = pwfile_create_list(ctx);
338 for (j = 0; list[j] != NULL; j++) { 346 for (j = 0; list[j] != NULL; j++) {
339 if (list[j]->any.type == ITEM_TYPE_GROUP) { 347 if (list[j]->any.type == ITEM_TYPE_GROUP) {
340 printf("[%s]\n", list[j]->group.group); 348 pager_printf(pager, "[%s]\n", list[j]->group.group);
341 } else { 349 } else {
342 record = pwfile_get_record(ctx, list[j]->record.id); 350 record = pwfile_get_record(ctx, list[j]->record.id);
343 if (((group_re == NULL) || (regexec(group_re, 351 if (((group_re == NULL) || (regexec(group_re,
344 record->group, 0, NULL, 0) == 0)) && 352 record->group, 0, NULL, 0) == 0)) &&
345 ((title_re == NULL) || (regexec(title_re, 353 ((title_re == NULL) || (regexec(title_re,
348 record->username, 0, NULL, 0) == 0)) && 356 record->username, 0, NULL, 0) == 0)) &&
349 ((notes_re == NULL) || (regexec(notes_re, 357 ((notes_re == NULL) || (regexec(notes_re,
350 record->notes, 0, NULL, 0) == 0)) && 358 record->notes, 0, NULL, 0) == 0)) &&
351 ((url_re == NULL) || (regexec(url_re, 359 ((url_re == NULL) || (regexec(url_re,
352 record->url, 0, NULL, 0) == 0))) { 360 record->url, 0, NULL, 0) == 0))) {
353 printf("%4u %s\n", list[j]->record.id, 361 pager_printf(pager, "%4u %s\n",
362 list[j]->record.id,
354 (list[j]->record.title != NULL) ? 363 (list[j]->record.title != NULL) ?
355 list[j]->record.title : ""); 364 list[j]->record.title : "");
356 } 365 }
357 pwfile_destroy_record(record); 366 pwfile_destroy_record(record);
358 } 367 }
359 } 368 }
369 pager_show(pager);
370
360 retval = CMD_OK; 371 retval = CMD_OK;
361 372
362 out: 373 out:
374 pager_destroy(pager);
375
363 if (group_re != NULL) { 376 if (group_re != NULL) {
364 regfree(group_re); 377 regfree(group_re);
365 free(group_re); 378 free(group_re);
366 } 379 }
367 if (title_re != NULL) { 380 if (title_re != NULL) {
380 regfree(url_re); 393 regfree(url_re);
381 free(url_re); 394 free(url_re);
382 } 395 }
383 396
384 pwfile_destroy_list(list); 397 pwfile_destroy_list(list);
398
385 399
386 return (retval); 400 return (retval);
387 } 401 }
388 402
389 static enum cmd_return 403 static enum cmd_return
654 } 668 }
655 669
656 return (CMD_OK); 670 return (CMD_OK);
657 } 671 }
658 672
659 static int
660 print_field(const char *label, const char *value, int show_label, FILE *fp)
661 {
662 fprintf(fp, "%s%s\n", show_label ? label : "", (value != NULL) ?
663 value : "");
664 if (ferror(fp)) {
665 warn("fprintf");
666 return (-1);
667 }
668 return (0);
669 }
670
671 static void 673 static void
672 print_record(struct record *record, int fields[], int show_labels, FILE *fp) 674 print_record(struct record *record, int fields[], int show_labels, FILE *fp)
673 { 675 {
676 struct pager *pager;
674 struct tm *tm; 677 struct tm *tm;
675 char timebuf[TIME_SIZE]; 678 char timebuf[TIME_SIZE];
676 679
680 pager = pager_create(fp);
677 if (fields[FIELD_TITLE]) { 681 if (fields[FIELD_TITLE]) {
678 if (print_field(field_labels[FIELD_TITLE], record->title, 682 pager_printf(pager, "%s%s\n", show_labels ?
679 show_labels, fp) != 0) { 683 field_labels[FIELD_TITLE] : "", (record->title != NULL) ?
680 return; 684 record->title : "");
681 }
682 } 685 }
683 if (fields[FIELD_GROUP]) { 686 if (fields[FIELD_GROUP]) {
684 if (print_field(field_labels[FIELD_GROUP], record->group, 687 pager_printf(pager, "%s%s\n", show_labels ?
685 show_labels, fp)) { 688 field_labels[FIELD_GROUP] : "", (record->group != NULL) ?
686 return; 689 record->group : "");
687 }
688 } 690 }
689 if (fields[FIELD_USERNAME]) { 691 if (fields[FIELD_USERNAME]) {
690 if (print_field(field_labels[FIELD_USERNAME], record->username, 692 pager_printf(pager, "%s%s\n", show_labels ?
691 show_labels, fp)) { 693 field_labels[FIELD_USERNAME] : "",
692 return; 694 (record->username != NULL) ? record->username : "");
693 }
694 } 695 }
695 if (fields[FIELD_PASSWORD]) { 696 if (fields[FIELD_PASSWORD]) {
696 if (print_field(field_labels[FIELD_PASSWORD], record->password, 697 pager_printf(pager, "%s%s\n", show_labels ?
697 show_labels, fp)) { 698 field_labels[FIELD_PASSWORD] : "",
698 return; 699 (record->password != NULL) ? record->password : "");
699 }
700 } 700 }
701 if (fields[FIELD_NOTES]) { 701 if (fields[FIELD_NOTES]) {
702 if (print_field(field_labels[FIELD_NOTES], record->notes, 702 pager_printf(pager, "%s%s\n", show_labels ?
703 show_labels, fp)) { 703 field_labels[FIELD_NOTES] : "", (record->notes != NULL) ?
704 return; 704 record->notes : "");
705 }
706 } 705 }
707 if (fields[FIELD_URL]) { 706 if (fields[FIELD_URL]) {
708 if (print_field(field_labels[FIELD_URL], record->url, 707 pager_printf(pager, "%s%s\n", show_labels ?
709 show_labels, fp)) { 708 field_labels[FIELD_URL] : "", (record->url != NULL) ?
710 return; 709 record->url : "");
711 }
712 } 710 }
713 if (fields[FIELD_CTIME]) { 711 if (fields[FIELD_CTIME]) {
714 tm = gmtime(&record->ctime); 712 tm = gmtime(&record->ctime);
715 strftime(timebuf, sizeof (timebuf), TIME_FORMAT, tm); 713 strftime(timebuf, sizeof (timebuf), TIME_FORMAT, tm);
716 if (print_field(field_labels[FIELD_CTIME], timebuf, 714 pager_printf(pager, "%s%s\n", show_labels ?
717 show_labels, fp)) { 715 field_labels[FIELD_CTIME] : "", timebuf);
718 return;
719 }
720 } 716 }
721 if (fields[FIELD_MTIME]) { 717 if (fields[FIELD_MTIME]) {
722 tm = gmtime(&record->mtime); 718 tm = gmtime(&record->mtime);
723 strftime(timebuf, sizeof (timebuf), TIME_FORMAT, tm); 719 strftime(timebuf, sizeof (timebuf), TIME_FORMAT, tm);
724 if (print_field(field_labels[FIELD_MTIME], timebuf, 720 pager_printf(pager, "%s%s\n", show_labels ?
725 show_labels, fp)) { 721 field_labels[FIELD_MTIME] : "", timebuf);
726 return; 722 }
727 } 723 pager_show(pager);
728 } 724 pager_destroy(pager);
729 } 725 }
730 726
731 static enum cmd_return 727 static enum cmd_return
732 cmd_show(struct pwm_ctx *ctx, int argc, char *argv[]) 728 cmd_show(struct pwm_ctx *ctx, int argc, char *argv[])
733 { 729 {
907 } 903 }
908 904
909 static enum cmd_return 905 static enum cmd_return
910 cmd_help(struct pwm_ctx *ctx, int argc, char *argv[]) 906 cmd_help(struct pwm_ctx *ctx, int argc, char *argv[])
911 { 907 {
908 struct pager *pager;
912 struct cmd *cmd; 909 struct cmd *cmd;
913 910
914 if (argc > 2) { 911 if (argc > 2) {
915 return (CMD_USAGE); 912 return (CMD_USAGE);
916 } 913 }
917 914
915 pager = pager_create(stdout);
918 if (argc == 2) { 916 if (argc == 2) {
919 for (cmd = cmds; cmd->cmd_func != NULL; cmd++) { 917 for (cmd = cmds; cmd->cmd_func != NULL; cmd++) {
920 if ((strcmp(argv[1], cmd->abbrev_cmd) == 0) || 918 if ((strcmp(argv[1], cmd->abbrev_cmd) == 0) ||
921 (strcmp(argv[1], cmd->full_cmd) == 0)) { 919 (strcmp(argv[1], cmd->full_cmd) == 0)) {
922 printf("%s\n", cmd->usage); 920 pager_printf(pager, "%s\n", cmd->usage);
923 break; 921 break;
924 } 922 }
925 } 923 }
926 } else { 924 } else {
927 printf("Commands:\n"); 925 printf("Commands:\n");
928 for (cmd = cmds; cmd->cmd_func != NULL; cmd++) { 926 for (cmd = cmds; cmd->cmd_func != NULL; cmd++) {
929 printf("%-2s %-16s %s\n", cmd->abbrev_cmd, 927 pager_printf(pager, "%-2s %-16s %s\n", cmd->abbrev_cmd,
930 cmd->full_cmd, cmd->description); 928 cmd->full_cmd, cmd->description);
931 } 929 }
932 } 930 }
931 pager_show(pager);
932 pager_destroy(pager);
933 933
934 return (CMD_OK); 934 return (CMD_OK);
935 } 935 }
936 936
937 static enum cmd_return 937 static enum cmd_return