From 680b08a9d69fcb3582203ca5e1e6f0ba88fea9e0 Mon Sep 17 00:00:00 2001
From: Laurent Bercot
Date: Tue, 24 Nov 2020 11:26:35 +0000
Subject: Better forstdin
No need for the complexity: the important distinction is between
"eof after reading something" and "eof right away". 0 is a natural
fit for eof after some data, and 1 is a natural fit for immediate eof.
Anything else can be scripted around this.
---
NEWS | 4 ++--
doc/forstdin.html | 11 +++++++----
doc/upgrade.html | 4 ++--
src/execline/forstdin.c | 13 +++++--------
4 files changed, 16 insertions(+), 16 deletions(-)
diff --git a/NEWS b/NEWS
index 6be3c4d..7ef56a9 100644
--- a/NEWS
+++ b/NEWS
@@ -3,8 +3,8 @@ Changelog for execline.
In 2.7.0.0
----------
- - forstdin QoL changes: new -e option to exit nonzero on EOF,
-and it now only splits on newlines by default.
+ - forstdin QoL changes: now it exits 1 if it doesn't read anything,
+and it only splits on newlines by default.
- New "default" directive to trap, replacing the irrelevant "timeout".
diff --git a/doc/forstdin.html b/doc/forstdin.html
index c7cc3d4..44939fe 100644
--- a/doc/forstdin.html
+++ b/doc/forstdin.html
@@ -30,7 +30,7 @@ run another program.
- forstdin [ -p | -o okcodes | -x breakcodes ] [ -e eofcode ] [ -n ] [ -C | -c ] [ -0 | -d delim ] variable loop...
+ forstdin [ -p | -o okcodes | -x breakcodes ] [ -E firsteofcode ] [ -e eofcode ] [ -n ] [ -C | -c ] [ -0 | -d delim ] variable loop...
@@ -39,7 +39,8 @@ run another program.
- For every argument x in the split output,
forstdin runs loop... as a child process, with
variable=x added to its environment.
- - forstdin then exits 0.
+
- forstdin then exits 0 if it has read something on stdin,
+and 1 if it hasn't read anything.
Options
@@ -63,8 +64,10 @@ not listed in okcodes, forstdin will exit immediately with an
option, but with inverted meaning - the listed exit codes are codes
that will make forstdin break the loop and exit, and the unlisted exit
codes will make it keep looping.
- -e eofcode : if forstdin reads EOF,
-exit eofcode. Default is 0.
+ -E firsteofcode : if forstdin encounters
+EOF on its first attempt to read data, exit firsteofcode. Default is 1.
+ -e eofcode : if forstdin has already
+read data, and encounters EOF, exit eofcode. Default is 0.
Other options are used to control
the substitution mechanism for every x. Of course, you can't
split x. The default delimiter for forstdin is a
diff --git a/doc/upgrade.html b/doc/upgrade.html
index 690c87e..d12b051 100644
--- a/doc/upgrade.html
+++ b/doc/upgrade.html
@@ -23,8 +23,8 @@
- skalibs
dependency bumped to 2.9.4.0.
- - forstdin gets a new -e
-option, and now only splits on newlines by default.
+ - forstdin now exits 1 on immediate EOF,
+and only splits on newlines by default.
- New default directive to trap,
replacing the timeout one, which was ill-suited to that program.
diff --git a/src/execline/forstdin.c b/src/execline/forstdin.c
index 323ef59..1a329f2 100644
--- a/src/execline/forstdin.c
+++ b/src/execline/forstdin.c
@@ -18,7 +18,7 @@
#include
#include
-#define USAGE "forstdin [ -p | -o okcode,okcode,... | -x breakcode,breakcode,... ] [ -e eofcode ] [ -n ] [ -C | -c ] [ -0 | -d delim ] var command..."
+#define USAGE "forstdin [ -p | -o okcode,okcode,... | -x breakcode,breakcode,... ] [ -n ] [ -C | -c ] [ -0 | -d delim ] var command..."
#define dieusage() strerr_dieusage(100, USAGE)
static genalloc pids = GENALLOC_ZERO ; /* pid_t */
@@ -51,14 +51,13 @@ int main (int argc, char const **argv, char const *const *envp)
size_t delimlen = 4 ;
size_t nbc = 0 ;
unsigned short okcodes[256] ;
- int crunch = 0, chomp = 0, not = 1 ;
- unsigned short eofcode = 0 ;
+ int crunch = 0, chomp = 0, not = 1, eofcode = 1 ;
PROG = "forstdin" ;
{
subgetopt_t l = SUBGETOPT_ZERO ;
for (;;)
{
- int opt = subgetopt_r(argc, argv, "pnCc0d:o:x:e:", &l) ;
+ int opt = subgetopt_r(argc, argv, "pnCc0d:o:x:", &l) ;
if (opt == -1) break ;
switch (opt)
{
@@ -81,9 +80,6 @@ int main (int argc, char const **argv, char const *const *envp)
not = 1 ;
if (!ushort_scanlist(okcodes, 256, l.arg, &nbc)) dieusage() ;
break ;
- case 'e' :
- if (!ushort_scan(l.arg, &eofcode)) dieusage() ;
- break ;
default : dieusage() ;
}
}
@@ -118,7 +114,6 @@ int main (int argc, char const **argv, char const *const *envp)
if (chomp) break ;
}
else modif.len-- ;
- if ((modif.len == modifstart) && crunch) continue ;
}
else
{
@@ -129,6 +124,8 @@ int main (int argc, char const **argv, char const *const *envp)
else strerr_diefu1sys(111, "netstring_get") ;
}
}
+ eofcode = 0 ;
+ if (crunch && modif.len == modifstart) continue ;
if (!stralloc_0(&modif)) strerr_diefu1sys(111, "stralloc_0") ;
if (!env_merge(newenv, envlen+2, envp, envlen, modif.s, modif.len))
strerr_diefu1sys(111, "merge environment") ;
--
cgit v1.3.1