summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLaurent Bercot <ska-skaware@skarnet.org>2021-02-02 16:59:14 +0000
committerLaurent Bercot <ska-skaware@skarnet.org>2021-02-02 16:59:14 +0000
commitf9b855a56b13708e2224efa54060b90539406383 (patch)
tree5d1b8d20bc152e5d76521fe5eaf455233045f3fa
parent631f809ce4a14ec6f1af5118b99adc514be7db60 (diff)
downloadexecline-f9b855a56b13708e2224efa54060b90539406383.tar.xz
Make if propagate failure code
-rw-r--r--doc/if.html17
-rw-r--r--src/execline/if.c13
2 files changed, 17 insertions, 13 deletions
diff --git a/doc/if.html b/doc/if.html
index 700ee59..99be53e 100644
--- a/doc/if.html
+++ b/doc/if.html
@@ -38,19 +38,22 @@
then waits for it to complete. </li>
<li> If <em>prog1</em> crashes, <tt>if</tt> prints an error message
then exits 128 plus the number of the signal that killed <em>prog1</em>. </li>
- <li> If <em>prog1</em> exits a non-zero status,
-<tt>if</tt> exits 1.</li>
+ <li> If <em>prog1</em> exits with a non-zero code, <tt>if</tt> exits with an
+<a href="exitcodes.html">approximation</a> of that code. </li>
<li> Else <tt>if</tt> execs into <em>prog2</em>. </li>
</ul>
<h2> Options </h2>
<ul>
- <li> <tt>-X</tt>&nbsp;: treat a crash of <em>prog1</em> as a non-zero ("false") exit.
- <li> <tt>-n</tt>&nbsp;: negate the test (exit on true, exec into <em>prog2</em> on false) </li>
- <li> <tt>-x</tt>&nbsp;<em>exitcode</em>&nbsp;: exit <em>exitcode</em> instead of 1 if the test fails. </li>
- <li> <tt>-t</tt>&nbsp;: exit 0 instead of 1 if the test fails.
-This is equivalent to <tt>-x 0</tt>. </li>
+ <li> <tt>-X</tt>&nbsp;: treat a crash of <em>prog1</em> as a non-zero ("false") exit. This
+is mostly useful in combination with <tt>-n</tt>. </li>
+ <li> <tt>-n</tt>&nbsp;: negate the test. If <em>prog1</em> exits true, then <tt>if</tt> will
+exit 1; else it will exec into <em>prog2</em>. </li>
+ <li> <tt>-x</tt>&nbsp;<em>exitcode</em>&nbsp;: if <tt>if</tt> needs to exit, it will exit <em>exitcode</em>
+instead of whatever else it would have exited. </li>
+ <li> <tt>-t</tt>&nbsp;: if <tt>if</tt> needs to exit, it will exit 0. This is equivalent
+to <tt>-x 0</tt>. </li>
</ul>
<h2> Notes </h2>
diff --git a/src/execline/if.c b/src/execline/if.c
index a37ecbc..8820dfa 100644
--- a/src/execline/if.c
+++ b/src/execline/if.c
@@ -11,12 +11,13 @@
#include <execline/execline.h>
#define USAGE "if [ -n ] [ -X ] [ -t | -x exitcode ] { command... }"
+#define dieusage() strerr_dieusage(100, USAGE) ;
int main (int argc, char const **argv, char const *const *envp)
{
int argc1, wstat ;
pid_t pid ;
- int not = 0, flagnormalcrash = 0 ;
+ int not = 0, fixed = 0, flagnormalcrash = 0 ;
unsigned short e = 1 ;
PROG = "if" ;
{
@@ -27,11 +28,11 @@ int main (int argc, char const **argv, char const *const *envp)
if (opt == -1) break ;
switch (opt)
{
- case 'n' : not = 1 ; break ;
+ case 'n' : not = 1 ; fixed = 1 ; break ;
case 'X' : flagnormalcrash = 1 ; break ;
- case 't' : e = 0 ; break ;
- case 'x' : if (ushort_scan(l.arg, &e)) break ;
- default : strerr_dieusage(100, USAGE) ;
+ case 't' : e = 0 ; fixed = 1 ; break ;
+ case 'x' : if (!ushort_scan(l.arg, &e)) dieusage() ; fixed = 1 ; break ;
+ default : dieusage() ;
}
}
argc -= l.ind ; argv += l.ind ;
@@ -49,6 +50,6 @@ int main (int argc, char const **argv, char const *const *envp)
fmt[uint_fmt(fmt, WTERMSIG(wstat))] = 0 ;
strerr_dief2x(128 + WTERMSIG(wstat), "child crashed with signal ", fmt) ;
}
- if (not == !wait_estatus(wstat)) return e ;
+ if (not == !wait_estatus(wstat)) return fixed ? e : wait_estatus(wstat) ;
xexec0_e(argv+argc1+1, envp) ;
}