about summary refs log tree commit diff
path: root/mshow.c
diff options
context:
space:
mode:
authorLeah Neukirchen <leah@vuxu.org>2017-05-23 15:16:27 +0200
committerLeah Neukirchen <leah@vuxu.org>2017-05-23 15:16:27 +0200
commite8107b1889a620b6ba446953e698b30f1803b2cb (patch)
tree762870878b73df56171da14d0cec1f74a64ee0a8 /mshow.c
parente7ce5ecc78dc8c56f32cd839369066cf33997305 (diff)
downloadmblaze-e8107b1889a620b6ba446953e698b30f1803b2cb.tar.gz
mblaze-e8107b1889a620b6ba446953e698b30f1803b2cb.tar.xz
mblaze-e8107b1889a620b6ba446953e698b30f1803b2cb.zip
mshow: spawn a pager for interactive use
Diffstat (limited to 'mshow.c')
-rw-r--r--mshow.c23
1 files changed, 23 insertions, 0 deletions
diff --git a/mshow.c b/mshow.c
index 4c62410..a96d540 100644
--- a/mshow.c
+++ b/mshow.c
@@ -703,6 +703,8 @@ done:
 int
 main(int argc, char *argv[])
 {
+	pid_t pid1 = -1, pid2 = -1;
+
 	int c;
 	while ((c = getopt(argc, argv, "h:A:qrtHLx:O:Rn")) != -1)
 		switch(c) {
@@ -731,6 +733,22 @@ main(int argc, char *argv[])
 	if (!rflag && !Oflag && !Rflag)
 		safe_output = 1;
 
+	if (safe_output && isatty(1)) {
+		char *pg;
+		pg = getenv("MBLAZE_PAGER");
+		if (!pg)
+			pg = getenv("PAGER");
+		if (pg && *pg && strcmp(pg, "cat") != 0) {
+			pid2 = pipeto(pg);
+			if (pid2 < 0)
+				fprintf(stderr,
+				    "mshow: spawning pager '%s': %s\n",
+				    pg, strerror(errno));
+			else if (!getenv("MBLAZE_NOCOLOR"))
+				pid1 = pipeto("mcolor");  // ignore error
+		}
+	}
+
 	if (xflag) { // extract
 		extract(xflag, argc-optind, argv+optind, 0);
 	} else if (Oflag) { // extract to stdout
@@ -758,5 +776,10 @@ main(int argc, char *argv[])
 			blaze822_seq_setcur(newcur);
 	}
 
+	if (pid2 > 0)
+		pipeclose(pid2);
+	if (pid1 > 0)
+		pipeclose(pid1);
+
 	return 0;
 }