commit f2df83a7333cbb5211ee030110a32d51b9f35d55
parent 50ce17802279641e16eef36437c6e9417404a5af
Author: Hiltjo Posthuma <hiltjo@codemadness.org>
Date: Fri, 26 Jun 2020 12:35:57 +0200
rework reopening /dev/tty and reading from stdin
Use-cases:
* Read from stdin:
cat ~/.sfeed/feeds/xkcd | sfeed_curses
* Shell redirect file:
sfeed_curses < ~/.sfeed/feeds/xkcd
* Read from file:
sfeed_curses ~/.sfeed/feeds/xkcd
This was broken and now works:
find ~/.sfeed/thisweek -type f -maxdepth 1 ! -size 0 -print0 | \
xargs -L 999 -0 sfeed_curses
Diffstat:
1 file changed, 11 insertions(+), 8 deletions(-)
diff --git a/sfeed_curses.c b/sfeed_curses.c
@@ -1039,6 +1039,9 @@ loadfiles(int argc, char *argv[])
FILE *fp;
size_t i;
char *name;
+ int istty;
+
+ istty = isatty(ttyfd);
feeds = ecalloc(argc, sizeof(struct feed));
@@ -1053,14 +1056,6 @@ loadfiles(int argc, char *argv[])
if (!(fp = fdopen(ttyfd, "rb")))
err(1, "fdopen");
feed_load(&feeds[0], fp);
-
- /* close and re-attach to stdin */
- close(0); // TODO: fclose ?
- if ((ttyfd = open("/dev/tty", O_RDONLY)) == -1)
- err(1, "open: /dev/tty");
- if (dup2(ttyfd, 0) == -1)
- err(1, "dup2: /dev/tty");
-
nfeeds = 1;
totalnew += feeds[0].totalnew;
totalcount += feeds[0].total;
@@ -1084,6 +1079,14 @@ loadfiles(int argc, char *argv[])
/* load first items, because of first selection. */
feed_loadfile(&feeds[0], feeds[0].path);
}
+
+ if (!istty) {
+ close(ttyfd);
+ if ((ttyfd = open("/dev/tty", O_RDONLY)) == -1)
+ err(1, "open: /dev/tty");
+ if (dup2(ttyfd, 0) == -1)
+ err(1, "dup2: /dev/tty");
+ }
}
void