diff 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
line wrap: on
line diff
--- a/cmd.c	Tue Aug 08 10:47:04 2017 +0200
+++ b/cmd.c	Sat Aug 12 10:41:52 2017 +0200
@@ -39,6 +39,7 @@
 #include <unistd.h>
 
 #include "cmd.h"
+#include "pager.h"
 #include "pw.h"
 #include "pwfile.h"
 #include "util.h"
@@ -236,6 +237,7 @@
 cmd_info(struct pwm_ctx *ctx, int argc, char *argv[])
 {
 	struct metadata	*metadata;
+	struct pager	*pager;
 	struct tm	*tm;
 	char		timebuf[TIME_SIZE];
 
@@ -244,19 +246,23 @@
 	}
 
 	metadata = pwfile_get_metadata(ctx);
-	printf("Format:      0x%04x\n", metadata->version);
+
+	pager = pager_create(stdout);
+	pager_printf(pager, "Format:      0x%04x\n", metadata->version);
 	if (metadata->user != NULL) {
-		printf("User:        %s\n", metadata->user);
+		pager_printf(pager, "User:        %s\n", metadata->user);
 	}
 	if (metadata->user != NULL) {
-		printf("Host:        %s\n", metadata->host);
+		pager_printf(pager, "Host:        %s\n", metadata->host);
 	}
 	if (metadata->user != NULL) {
-		printf("Application: %s\n", metadata->application);
+		pager_printf(pager, "Application: %s\n", metadata->application);
 	}
 	tm = gmtime(&metadata->timestamp);
 	strftime(timebuf, sizeof (timebuf), TIME_FORMAT, tm);
-	printf("Last Saved:  %s\n", timebuf);
+	pager_printf(pager, "Last Saved:  %s\n", timebuf);
+	pager_show(pager);
+	pager_destroy(pager);
 
 	pwfile_destroy_metadata(metadata);
 
@@ -279,6 +285,7 @@
 	int		errcode;
 	char		*errbuf;
 	size_t		errbuf_size;
+	struct pager	*pager = NULL;
 	union list_item	**list = NULL;
 	size_t		j;
 	struct record	*record;
@@ -334,10 +341,11 @@
 		}
 	}
 
+	pager = pager_create(stdout);
 	list = pwfile_create_list(ctx);
 	for (j = 0; list[j] != NULL; j++) {
 		if (list[j]->any.type == ITEM_TYPE_GROUP) {
-			printf("[%s]\n", list[j]->group.group);
+			pager_printf(pager, "[%s]\n", list[j]->group.group);
 		} else {
 			record = pwfile_get_record(ctx, list[j]->record.id);
 			if (((group_re == NULL) || (regexec(group_re,
@@ -350,16 +358,21 @@
 			    record->notes, 0, NULL, 0) == 0)) &&
 			    ((url_re == NULL) || (regexec(url_re,
 			    record->url, 0, NULL, 0) == 0))) {
-				printf("%4u %s\n", list[j]->record.id,
+				pager_printf(pager, "%4u %s\n",
+				    list[j]->record.id,
 				    (list[j]->record.title != NULL) ?
 				    list[j]->record.title : "");
 			}
 			pwfile_destroy_record(record);
 		}
 	}
+	pager_show(pager);
+
 	retval = CMD_OK;
 
 out:
+	pager_destroy(pager);
+
 	if (group_re != NULL) {
 		regfree(group_re);
 		free(group_re);
@@ -383,6 +396,7 @@
 
 	pwfile_destroy_list(list);
 
+
 	return (retval);
 }
 
@@ -656,76 +670,58 @@
 	return (CMD_OK);
 }
 
-static int
-print_field(const char *label, const char *value, int show_label, FILE *fp)
-{
-	fprintf(fp, "%s%s\n", show_label ? label : "", (value != NULL) ?
-	    value : "");
-	if (ferror(fp)) {
-		warn("fprintf");
-		return (-1);
-	}
-	return (0);
-}
-
 static void
 print_record(struct record *record, int fields[], int show_labels, FILE *fp)
 {
+	struct pager	*pager;
 	struct tm	*tm;
 	char		timebuf[TIME_SIZE];
 
+	pager = pager_create(fp);
 	if (fields[FIELD_TITLE]) {
-		if (print_field(field_labels[FIELD_TITLE], record->title,
-		    show_labels, fp) != 0) {
-			return;
-		}
+		pager_printf(pager, "%s%s\n", show_labels ?
+		    field_labels[FIELD_TITLE] : "", (record->title != NULL) ?
+		    record->title : "");
 	}
 	if (fields[FIELD_GROUP]) {
-		if (print_field(field_labels[FIELD_GROUP], record->group,
-		    show_labels, fp)) {
-			return;
-		}
+		pager_printf(pager, "%s%s\n", show_labels ?
+		    field_labels[FIELD_GROUP] : "", (record->group != NULL) ?
+		    record->group : "");
 	}
 	if (fields[FIELD_USERNAME]) {
-		if (print_field(field_labels[FIELD_USERNAME], record->username,
-		    show_labels, fp)) {
-			return;
-		}
+		pager_printf(pager, "%s%s\n", show_labels ?
+		    field_labels[FIELD_USERNAME] : "",
+		    (record->username != NULL) ?  record->username : "");
 	}
 	if (fields[FIELD_PASSWORD]) {
-		if (print_field(field_labels[FIELD_PASSWORD], record->password,
-		    show_labels, fp)) {
-			return;
-		}
+		pager_printf(pager, "%s%s\n", show_labels ?
+		    field_labels[FIELD_PASSWORD] : "",
+		    (record->password != NULL) ?  record->password : "");
 	}
 	if (fields[FIELD_NOTES]) {
-		if (print_field(field_labels[FIELD_NOTES], record->notes,
-		    show_labels, fp)) {
-			return;
-		}
+		pager_printf(pager, "%s%s\n", show_labels ?
+		    field_labels[FIELD_NOTES] : "", (record->notes != NULL) ?
+		    record->notes : "");
 	}
 	if (fields[FIELD_URL]) {
-		if (print_field(field_labels[FIELD_URL], record->url,
-		    show_labels, fp)) {
-			return;
-		}
+		pager_printf(pager, "%s%s\n", show_labels ?
+		    field_labels[FIELD_URL] : "", (record->url != NULL) ?
+		    record->url : "");
 	}
 	if (fields[FIELD_CTIME]) {
 		tm = gmtime(&record->ctime);
 		strftime(timebuf, sizeof (timebuf), TIME_FORMAT, tm);
-		if (print_field(field_labels[FIELD_CTIME], timebuf,
-		    show_labels, fp)) {
-			return;
-		}
+		pager_printf(pager, "%s%s\n", show_labels ?
+		    field_labels[FIELD_CTIME] : "", timebuf);
 	}
 	if (fields[FIELD_MTIME]) {
 		tm = gmtime(&record->mtime);
 		strftime(timebuf, sizeof (timebuf), TIME_FORMAT, tm);
-		if (print_field(field_labels[FIELD_MTIME], timebuf,
-		    show_labels, fp)) {
-			return;
-		}
+		pager_printf(pager, "%s%s\n", show_labels ?
+		    field_labels[FIELD_MTIME] : "", timebuf);
 	}
+	pager_show(pager);
+	pager_destroy(pager);
 }
 
 static enum cmd_return
@@ -909,27 +905,31 @@
 static enum cmd_return
 cmd_help(struct pwm_ctx *ctx, int argc, char *argv[])
 {
+	struct pager	*pager;
 	struct cmd	*cmd;
 
 	if (argc > 2) {
 		return (CMD_USAGE);
 	}
 
+	pager = pager_create(stdout);
 	if (argc == 2) {
 		for (cmd = cmds; cmd->cmd_func != NULL; cmd++) {
 			if ((strcmp(argv[1], cmd->abbrev_cmd) == 0) ||
 			    (strcmp(argv[1], cmd->full_cmd) == 0)) {
-				printf("%s\n", cmd->usage);
+				pager_printf(pager, "%s\n", cmd->usage);
 				break;
 			}
 		}
 	} else {
 		printf("Commands:\n");
 		for (cmd = cmds; cmd->cmd_func != NULL; cmd++) {
-			printf("%-2s %-16s %s\n", cmd->abbrev_cmd,
+			pager_printf(pager, "%-2s %-16s %s\n", cmd->abbrev_cmd,
 			    cmd->full_cmd, cmd->description);
 		}
 	}
+	pager_show(pager);
+	pager_destroy(pager);
 
 	return (CMD_OK);
 }