implement set -o pipefail

This commit is contained in:
tg 2013-05-02 20:28:15 +00:00
parent 0465f0a0de
commit ff42a866e9
4 changed files with 39 additions and 6 deletions

24
check.t
View File

@ -1,4 +1,4 @@
# $MirOS: src/bin/mksh/check.t,v 1.610 2013/05/02 20:21:38 tg Exp $
# $MirOS: src/bin/mksh/check.t,v 1.611 2013/05/02 20:28:10 tg Exp $
# $OpenBSD: bksl-nl.t,v 1.2 2001/01/28 23:04:56 niklas Exp $
# $OpenBSD: history.t,v 1.5 2001/01/28 23:04:56 niklas Exp $
# $OpenBSD: read.t,v 1.3 2003/03/10 03:48:16 david Exp $
@ -6294,6 +6294,28 @@ expected-stdout:
PIPESTATUS[0]=0
8 PIPESTATUS[0]=0 PIPESTATUS[1]=0 .
---
name: pipeline-4
description:
Check that "set -o pipefail" does what it's supposed to
stdin:
echo 1 "$("$__progname" -c '(exit 12) | (exit 23) | (exit 42); echo $?')" .
echo 2 "$("$__progname" -c '! (exit 12) | (exit 23) | (exit 42); echo $?')" .
echo 3 "$("$__progname" -o pipefail -c '(exit 12) | (exit 23) | (exit 42); echo $?')" .
echo 4 "$("$__progname" -o pipefail -c '! (exit 12) | (exit 23) | (exit 42); echo $?')" .
echo 5 "$("$__progname" -c '(exit 23) | (exit 42) | :; echo $?')" .
echo 6 "$("$__progname" -c '! (exit 23) | (exit 42) | :; echo $?')" .
echo 7 "$("$__progname" -o pipefail -c '(exit 23) | (exit 42) | :; echo $?')" .
echo 8 "$("$__progname" -o pipefail -c '! (exit 23) | (exit 42) | :; echo $?')" .
expected-stdout:
1 42 .
2 0 .
3 42 .
4 0 .
5 0 .
6 1 .
7 42 .
8 0 .
---
name: persist-history-1
description:
Check if persistent history saving works

7
jobs.c
View File

@ -1,7 +1,8 @@
/* $OpenBSD: jobs.c,v 1.38 2009/12/12 04:28:44 deraadt Exp $ */
/*-
* Copyright (c) 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2011, 2012
* Copyright (c) 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2011,
* 2012, 2013
* Thorsten Glaser <tg@mirbsd.org>
*
* Provided that these terms and disclaimer and all copyright notices
@ -22,7 +23,7 @@
#include "sh.h"
__RCSID("$MirOS: src/bin/mksh/jobs.c,v 1.95 2013/04/01 02:37:50 tg Exp $");
__RCSID("$MirOS: src/bin/mksh/jobs.c,v 1.96 2013/05/02 20:28:12 tg Exp $");
#if HAVE_KILLPG
#define mksh_killpg killpg
@ -1220,6 +1221,8 @@ j_waitj(Job *j,
ARRAY | INT_U | AINDEX;
got_array:
vp->val.i = proc_errorlevel(p);
if (Flag(FPIPEFAIL) && vp->val.i)
rv = vp->val.i;
p = p->next;
}
}

9
mksh.1
View File

@ -1,4 +1,4 @@
.\" $MirOS: src/bin/mksh/mksh.1,v 1.313 2013/05/02 15:33:30 tg Exp $
.\" $MirOS: src/bin/mksh/mksh.1,v 1.314 2013/05/02 20:28:13 tg Exp $
.\" $OpenBSD: ksh.1,v 1.146 2013/03/18 11:10:52 mpi Exp $
.\"-
.\" Copyright © 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009,
@ -514,7 +514,9 @@ token to form pipelines, in which the standard output of each command but the
last is piped (see
.Xr pipe 2 )
to the standard input of the following command.
The exit status of a pipeline is that of its last command.
The exit status of a pipeline is that of its last command, unless the
.Ic pipefail
option is set (see there).
All commands of a pipeline are executed in separate subshells;
this is allowed by POSIX but differs from both variants of
.At
@ -4177,6 +4179,9 @@ See the
and
.Ic pwd
commands above for more details.
.It Fl o Ic pipefail
Make the exit status of a pipeline (before logically complementing) the
rightmost non-zero errorlevel, or zero if all commands exited with zero.
.It Fl o Ic posix
Enable a somewhat more
.Px

View File

@ -1,5 +1,5 @@
#if defined(SHFLAGS_DEFNS)
__RCSID("$MirOS: src/bin/mksh/sh_flags.h,v 1.13 2013/05/02 20:21:45 tg Exp $");
__RCSID("$MirOS: src/bin/mksh/sh_flags.h,v 1.14 2013/05/02 20:28:15 tg Exp $");
#define FN(sname,cname,ochar,flags) /* nothing */
#elif defined(SHFLAGS_ENUMS)
#define FN(sname,cname,ochar,flags) cname,
@ -88,6 +88,9 @@ FN("nounset", FNOUNSET, 'u', OF_ANY)
/* ./. don't do logical cds/pwds (non-standard) */
FN("physical", FPHYSICAL, 0, OF_ANY)
/* ./. errorlevel of a pipeline is the rightmost nonzero value */
FN("pipefail", FPIPEFAIL, 0, OF_ANY)
/* ./. adhere more closely to POSIX even when undesirable */
FN("posix", FPOSIX, 0, OF_ANY)