oksh-noxz

[fork] Portable OpenBSD ksh, based on the Public Domain Korn Shell (pdksh).
git clone https://noxz.tech/git/oksh-noxz.git
oksh-noxz

commit: 24c88e65cfaba30f24152ca4bb73a08be5973729
parent: 93e6a9c4f507bf2a18f4dbb9c77faeda9f7d9579
author: Brian Callahan <bcallah@openbsd.org>
date:   Wed, 9 Jan 2019 22:04:31 -0500
Update to OpenBSD ksh code from 2019-01-09.
MCVS/Entries74++++++++++----------
Mconfigure24+++++++
Medit.c3+-
Mhistory.c24+++++--
Mksh.112++--
Mmail.c18++---
Mmain.c19+++--
Mmisc.c13+++-
Msh.114++--
Msh.h3+-
Mtrap.c24+++----
11 files changed, 141 insertions(+), 87 deletions(-)
diff --git a/CVS/Entries b/CVS/Entries
@@ -1,46 +1,46 @@
 /config.h/1.19/Tue Jan 16 02:21:56 2018//
-/history.c/1.80/Tue Jan 16 02:21:56 2018//
 /io.c/1.36/Fri Jun  1 19:10:08 2018//
 /tty.c/1.17/Fri Jun  1 19:10:08 2018//
 /c_test.c/1.25/Sun Sep 23 17:58:57 2018//
-/edit.c/1.66/Sun Sep 23 17:59:17 2018//
 /emacs.c/1.85/Result of merge//
 /eval.c/1.63/Sun Sep 23 17:59:17 2018//
 /exec.c/1.73/Sun Sep 23 17:58:57 2018//
-/misc.c/1.70/Sun Sep 23 17:58:57 2018//
-/sh.h/1.73/Sun Sep 23 17:58:57 2018//
 /var.c/1.70/Sun Sep 23 17:59:17 2018//
-/main.c/1.93/Result of merge//
-/CONTRIBUTORS/1.10/Tue Oct  9 19:32:26 2018//
-/LEGAL/1.2/Tue Oct  9 19:32:26 2018//
-/Makefile/1.39/Tue Oct  9 19:32:35 2018//
-/NOTES/1.16/Tue Oct  9 19:32:26 2018//
-/PROJECTS/1.9/Tue Oct  9 19:32:26 2018//
-/README/1.16/Tue Oct  9 19:32:35 2018//
-/alloc.c/1.19/Tue Oct  9 19:32:26 2018//
-/c_ksh.c/1.61/Tue Oct  9 19:32:26 2018//
-/c_sh.c/1.63/Tue Oct  9 19:32:26 2018//
-/c_test.h/1.4/Tue Oct  9 19:32:26 2018//
-/c_ulimit.c/1.28/Tue Oct  9 19:32:26 2018//
-/edit.h/1.12/Tue Oct  9 19:32:26 2018//
-/expand.h/1.15/Tue Oct  9 19:32:26 2018//
-/expr.c/1.33/Tue Oct  9 19:32:26 2018//
-/jobs.c/1.60/Tue Oct  9 19:32:26 2018//
-/ksh.1/1.201/Tue Oct  9 19:32:26 2018//
-/lex.c/1.78/Tue Oct  9 19:32:26 2018//
-/lex.h/1.21/Tue Oct  9 19:32:26 2018//
-/mail.c/1.24/Tue Oct  9 19:32:26 2018//
-/path.c/1.22/Tue Oct  9 19:32:26 2018//
-/sh.1/1.149/Tue Oct  9 19:32:26 2018//
-/shf.c/1.33/Tue Oct  9 19:32:26 2018//
-/shf.h/1.8/Tue Oct  9 19:32:26 2018//
-/syn.c/1.39/Tue Oct  9 19:32:26 2018//
-/table.c/1.25/Tue Oct  9 19:32:26 2018//
-/table.h/1.15/Tue Oct  9 19:32:26 2018//
-/trap.c/1.32/Tue Oct  9 19:32:26 2018//
-/tree.c/1.34/Tue Oct  9 19:32:26 2018//
-/tree.h/1.12/Tue Oct  9 19:32:26 2018//
-/tty.h/1.6/Tue Oct  9 19:32:26 2018//
-/version.c/1.12/Tue Oct  9 19:32:26 2018//
-/vi.c/1.56/Tue Oct  9 19:32:26 2018//
+/CONTRIBUTORS/1.10/Thu Jan 10 02:43:53 2019//
+/LEGAL/1.2/Thu Jan 10 02:43:53 2019//
+/Makefile/1.39/Thu Jan 10 02:44:02 2019//
+/NOTES/1.16/Thu Jan 10 02:43:53 2019//
+/PROJECTS/1.9/Thu Jan 10 02:43:53 2019//
+/README/1.16/Thu Jan 10 02:44:02 2019//
+/alloc.c/1.19/Thu Jan 10 02:43:53 2019//
+/c_ksh.c/1.61/Thu Jan 10 02:43:53 2019//
+/c_sh.c/1.63/Thu Jan 10 02:43:53 2019//
+/c_test.h/1.4/Thu Jan 10 02:43:53 2019//
+/c_ulimit.c/1.28/Thu Jan 10 02:43:53 2019//
+/edit.c/1.67/Result of merge//
+/edit.h/1.12/Thu Jan 10 02:43:53 2019//
+/expand.h/1.15/Thu Jan 10 02:43:53 2019//
+/expr.c/1.33/Thu Jan 10 02:43:53 2019//
+/history.c/1.81/Result of merge//
+/jobs.c/1.60/Thu Jan 10 02:43:53 2019//
+/ksh.1/1.202/Thu Jan 10 02:44:02 2019//
+/lex.c/1.78/Thu Jan 10 02:43:53 2019//
+/lex.h/1.21/Thu Jan 10 02:43:53 2019//
+/mail.c/1.25/Thu Jan 10 02:44:02 2019//
+/main.c/1.96/Result of merge+Thu Jan 10 02:44:02 2019//
+/misc.c/1.72/Result of merge//
+/path.c/1.22/Thu Jan 10 02:43:53 2019//
+/sh.1/1.151/Thu Jan 10 02:44:02 2019//
+/sh.h/1.74/Result of merge//
+/shf.c/1.33/Thu Jan 10 02:43:53 2019//
+/shf.h/1.8/Thu Jan 10 02:43:53 2019//
+/syn.c/1.39/Thu Jan 10 02:43:53 2019//
+/table.c/1.25/Thu Jan 10 02:43:53 2019//
+/table.h/1.15/Thu Jan 10 02:43:53 2019//
+/trap.c/1.33/Thu Jan 10 02:44:02 2019//
+/tree.c/1.34/Thu Jan 10 02:43:53 2019//
+/tree.h/1.12/Thu Jan 10 02:43:53 2019//
+/tty.h/1.6/Thu Jan 10 02:43:53 2019//
+/version.c/1.12/Thu Jan 10 02:43:53 2019//
+/vi.c/1.56/Thu Jan 10 02:43:53 2019//
 D
diff --git a/configure b/configure
@@ -141,6 +141,21 @@ EOF
   fi
 }
 
+issetugidcheck() {
+  cat << EOF > conftest.c
+#include <unistd.h>
+int main(void){issetugid();return 0;}
+EOF
+  $cc $cflags $ldflags -o conftest conftest.c > /dev/null 2>&1
+  if [ $? -eq 0 ] ; then
+    rm -f conftest conftest.o conftest.c
+    return 0
+  else
+    rm -f conftest conftest.o conftest.c
+    return 1
+  fi
+}
+
 ncursescheck() {
   cat << EOF > conftest.c
 #include <term.h>
@@ -671,6 +686,15 @@ else
   echo "oksh will be built without screen clearing support"
 fi
 
+printf "checking for issetugid... "
+issetugidcheck
+if [ $? -eq 0 ] ; then
+  echo "yes"
+else
+  echo "#define issetugid(x) 0" >> pconfig.h
+  echo "no"
+fi
+
 printf "checking for pledge... "
 pledgecheck
 if [ $? -eq 0 ] ; then
diff --git a/edit.c b/edit.c
@@ -1,4 +1,4 @@
-/*	$OpenBSD: edit.c,v 1.66 2018/06/18 17:03:58 millert Exp $	*/
+/*	$OpenBSD: edit.c,v 1.67 2018/12/30 23:09:58 guenther Exp $	*/
 
 /*
  * Command line editing - common code
@@ -12,7 +12,6 @@
 
 #include <ctype.h>
 #include <errno.h>
-#include <libgen.h>
 #include <stdlib.h>
 #include <stdio.h>
 #include <string.h>
diff --git a/history.c b/history.c
@@ -1,4 +1,4 @@
-/*	$OpenBSD: history.c,v 1.80 2018/01/15 22:30:38 jca Exp $	*/
+/*	$OpenBSD: history.c,v 1.81 2018/11/20 07:02:23 martijn Exp $	*/
 
 /*
  * command history
@@ -53,6 +53,8 @@ static uint32_t	line_co;
 
 static struct stat last_sb;
 
+static volatile sig_atomic_t	c_fc_depth;
+
 int
 c_fc(char **wp)
 {
@@ -63,9 +65,8 @@ c_fc(char **wp)
 	int optc, ret;
 	char *first = NULL, *last = NULL;
 	char **hfirst, **hlast, **hp;
-	static int depth;
 
-	if (depth != 0) {
+	if (c_fc_depth != 0) {
 		bi_errorf("history function called recursively");
 		return 1;
 	}
@@ -151,9 +152,9 @@ c_fc(char **wp)
 		    hist_get_newest(false);
 		if (!hp)
 			return 1;
-		depth++;
+		c_fc_depth++;
 		ret = hist_replace(hp, pat, rep, gflag);
-		depth--;
+		c_fc_reset();
 		return ret;
 	}
 
@@ -273,13 +274,22 @@ c_fc(char **wp)
 		shf_close(shf);
 		*xp = '\0';
 		strip_nuls(Xstring(xs, xp), Xlength(xs, xp));
-		depth++;
+		c_fc_depth++;
 		ret = hist_execute(Xstring(xs, xp));
-		depth--;
+		c_fc_reset();
 		return ret;
 	}
 }
 
+/* Reset the c_fc depth counter.
+ * Made available for when an fc call is interrupted.
+ */
+void
+c_fc_reset(void)
+{
+	c_fc_depth = 0;
+}
+
 /* Save cmd in history, execute cmd (cmd gets trashed) */
 static int
 hist_execute(char *cmd)
diff --git a/ksh.1 b/ksh.1
@@ -1,8 +1,8 @@
-.\"	$OpenBSD: ksh.1,v 1.201 2018/06/18 17:03:58 millert Exp $
+.\"	$OpenBSD: ksh.1,v 1.202 2018/12/16 13:08:35 schwarze Exp $
 .\"
 .\"	Public Domain
 .\"
-.Dd $Mdocdate: June 18 2018 $
+.Dd $Mdocdate: December 16 2018 $
 .Dt KSH 1
 .Os
 .Sh NAME
@@ -540,7 +540,7 @@ if no
 .Ar list
 is executed, the exit status is zero.
 .It Xo Ic for Ar name
-.Oo Cm in Ar word No ... Oc ;
+.Op Cm in Op Ar word ... ;
 .Cm do Ar list ; Cm done
 .Xc
 For each
@@ -564,10 +564,10 @@ e.g.\&
 The exit status of a
 .Ic for
 statement is the last exit status of
-.Ar list ;
-if
+.Ar list .
+If there are no items,
 .Ar list
-is never executed, the exit status is zero.
+is not executed and the exit status is zero.
 .It Xo Ic if Ar list ;
 .Cm then Ar list ;
 .Oo Cm elif Ar list ;
diff --git a/mail.c b/mail.c
@@ -1,4 +1,4 @@
-/*	$OpenBSD: mail.c,v 1.24 2018/06/25 15:22:30 cheloha Exp $	*/
+/*	$OpenBSD: mail.c,v 1.25 2019/01/07 20:50:43 tedu Exp $	*/
 
 /*
  * Mailbox checking code by Robert J. Gibson, adapted for PD ksh by
@@ -47,6 +47,15 @@ mcheck(void)
 	struct stat	 stbuf;
 	static int	 first = 1;
 
+	if (mplist)
+		mbp = mplist;
+	else if ((vp = global("MAIL")) && (vp->flag & ISSET))
+		mbp = &mbox;
+	else
+		mbp = NULL;
+	if (mbp == NULL)
+		return;
+
 	clock_gettime(CLOCK_MONOTONIC, &now);
 	if (first) {
 		mlastchkd = now;
@@ -56,13 +65,6 @@ mcheck(void)
 	if (elapsed.tv_sec >= mailcheck_interval) {
 		mlastchkd = now;
 
-		if (mplist)
-			mbp = mplist;
-		else if ((vp = global("MAIL")) && (vp->flag & ISSET))
-			mbp = &mbox;
-		else
-			mbp = NULL;
-
 		while (mbp) {
 			if (mbp->mb_path && stat(mbp->mb_path, &stbuf) == 0 &&
 			    S_ISREG(stbuf.st_mode)) {
diff --git a/main.c b/main.c
@@ -1,4 +1,4 @@
-/*	$OpenBSD: main.c,v 1.93 2018/09/29 14:13:19 millert Exp $	*/
+/*	$OpenBSD: main.c,v 1.96 2018/11/20 07:02:23 martijn Exp $	*/
 
 /*
  * startup, main loop, environments and error handling
@@ -146,10 +146,18 @@ main(int argc, char *argv[])
 	kshname = argv[0];
 
 #ifdef HAVE_PLEDGE
-	if (pledge("stdio rpath wpath cpath fattr flock getpw proc exec tty",
-	    NULL) == -1) {
-		perror("pledge");
-		exit(1);
+	if (issetugid()) { /* could later drop privileges */
+		if (pledge("stdio rpath wpath cpath fattr flock getpw proc "
+		    "exec tty id", NULL) == -1) {
+			perror("pledge");
+			exit(1);
+		}
+	} else {
+		if (pledge("stdio rpath wpath cpath fattr flock getpw proc "
+		    "exec tty", NULL) == -1) {
+			perror("pledge");
+			exit(1);
+		}
 	}
 #endif
 
@@ -551,6 +559,7 @@ shell(Source *volatile s, volatile int toplevel)
 		case LERROR:
 		case LSHELL:
 			if (interactive) {
+				c_fc_reset();
 				if (i == LINTR)
 					shellf("\n");
 				/* Reset any eof that was read as part of a
diff --git a/misc.c b/misc.c
@@ -1,4 +1,4 @@
-/*	$OpenBSD: misc.c,v 1.70 2018/04/09 17:53:36 tobias Exp $	*/
+/*	$OpenBSD: misc.c,v 1.72 2018/11/20 08:12:26 deraadt Exp $	*/
 
 /*
  * Miscellaneous functions
@@ -16,6 +16,7 @@
 #include "charclass.h"
 
 short ctypes [UCHAR_MAX+1];	/* type bits for unsigned char */
+static int dropped_privileges;
 
 static int	do_gmatch(const unsigned char *, const unsigned char *,
 		    const unsigned char *, const unsigned char *);
@@ -290,12 +291,20 @@ change_flag(enum sh_flag f,
 		}
 	} else
 	/* Turning off -p? */
-	if (f == FPRIVILEGED && oldval && !newval) {
+	if (f == FPRIVILEGED && oldval && !newval && issetugid() &&
+	    !dropped_privileges) {
 		gid_t gid = getgid();
 
 		setresgid(gid, gid, gid);
 		setgroups(1, &gid);
 		setresuid(ksheuid, ksheuid, ksheuid);
+
+#ifdef HAVE_PLEDGE
+		if (pledge("stdio rpath wpath cpath fattr flock getpw proc "
+		    "exec tty", NULL) == -1)
+			bi_errorf("pledge fail");
+#endif
+		dropped_privileges = 1;
 	} else if (f == FPOSIX && newval) {
 		Flag(FBRACEEXPAND) = 0;
 	}
diff --git a/sh.1 b/sh.1
@@ -1,4 +1,4 @@
-.\"	$OpenBSD: sh.1,v 1.149 2018/09/28 18:32:39 millert Exp $
+.\"	$OpenBSD: sh.1,v 1.151 2018/12/16 13:08:35 schwarze Exp $
 .\"
 .\" Copyright (c) 2015 Jason McIntyre <jmc@openbsd.org>
 .\"
@@ -14,7 +14,7 @@
 .\" ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 .\" OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 .\"
-.Dd $Mdocdate: September 28 2018 $
+.Dd $Mdocdate: December 16 2018 $
 .Dt SH 1
 .Os
 .Sh NAME
@@ -495,7 +495,7 @@ to the index of the next variable to be processed.
 The string
 .Ar optstring
 contains a list of acceptable options;
-a colon following an option indicates it may take an argument.
+a colon following an option indicates it requires an argument.
 If an option not recognised by
 .Ar optstring
 is found,
@@ -1755,7 +1755,7 @@ A
 executes a series of commands for each item in a list.
 Its format is:
 .Bd -unfilled -offset indent
-.No for Ar name Op in Ar word ...
+.No for Ar name Op in Op Ar word ...
 do
 .No "   " Ar command
 .No "   " Ar ...
@@ -1773,8 +1773,10 @@ The construct
 .Qq in word ...
 can be omitted,
 which is equivalent to: in \&"$@\&".
-The exit status is zero if there are no items
-or otherwise the exit status of the last command executed.
+The exit status is that of the last command executed.
+If there are no items,
+.Ar command
+is not executed and the exit status is zero.
 .Pp
 A
 .Em while loop
diff --git a/sh.h b/sh.h
@@ -1,4 +1,4 @@
-/*	$OpenBSD: sh.h,v 1.73 2018/05/18 13:25:20 benno Exp $	*/
+/*	$OpenBSD: sh.h,v 1.74 2018/11/20 07:02:23 martijn Exp $	*/
 
 /*
  * Public Domain Bourne/Korn shell
@@ -449,6 +449,7 @@ void	hist_init(Source *);
 void	hist_finish(void);
 void	histsave(int, const char *, int);
 int	c_fc(char **);
+void	c_fc_reset(void);
 void	sethistcontrol(const char *);
 void	sethistsize(int);
 void	sethistfile(const char *);
diff --git a/trap.c b/trap.c
@@ -1,4 +1,4 @@
-/*	$OpenBSD: trap.c,v 1.32 2018/03/15 16:51:29 anton Exp $	*/
+/*	$OpenBSD: trap.c,v 1.33 2018/12/08 21:03:51 jca Exp $	*/
 
 /*
  * signal handling
@@ -92,20 +92,18 @@ gettrap(const char *name, int igncase)
 			return &sigtraps[n];
 		return NULL;
 	}
+
+	if (igncase && strncasecmp(name, "SIG", 3) == 0)
+		name += 3;
+	if (!igncase && strncmp(name, "SIG", 3) == 0)
+		name += 3;
+
 	for (p = sigtraps, i = NSIG+1; --i >= 0; p++)
 		if (p->name) {
-			if (igncase) {
-				if (p->name && (!strcasecmp(p->name, name) ||
-				    (strlen(name) > 3 && !strncasecmp("SIG",
-				    p->name, 3) &&
-				    !strcasecmp(p->name, name + 3))))
-					return p;
-			} else {
-				if (p->name && (!strcmp(p->name, name) ||
-				    (strlen(name) > 3 && !strncmp("SIG",
-				    p->name, 3) && !strcmp(p->name, name + 3))))
-					return p;
-			}
+			if (igncase && strcasecmp(p->name, name) == 0)
+				return p;
+			if (!igncase && strcmp(p->name, name) == 0)
+				return p;
 		}
 	return NULL;
 }