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: a7bc2bd8e2621cc0157c424a7d57d51a971d0708
parent: 203506e928606056da4bee720fa0b1c921957f20
author: Brian Callahan <bcallah@openbsd.org>
date:   Wed, 22 Jul 2020 16:35:01 -0400
Make cross compiling a real possibility, with instructions.
MREADME.md17++
Masprintf.c16+-
Mconfigure212+++++++++++++-------
Mportable.h14+-
4 files changed, 169 insertions(+), 90 deletions(-)
diff --git a/README.md b/README.md
@@ -83,6 +83,23 @@ $ ./configure
 $ make && sudo make install
 ```
 
+Cross compiling
+---------------
+Cross compiling can be achieved by running `configure` as follows:
+```
+CC=/path/to/cross/cc CFLAGS="any needed cflags" LDFLAGS="any needed ldflags" ./configure --no-thanks
+```
+
+This will skip all `configure` checks and write out a generic `Makefile`
+a `pconfig.h` with nearly no options turned on. If using a cross gcc or
+clang, this very well may just work (with all compat compiled in). You
+can edit these files to reflect your system before running `make`.
+
+All environment variables and configure flags are respected when using
+`--no-thanks`. Further specifying `--no-link` after `--no-thanks` will
+only compile the source files into object files, to be transfered onto
+the target machine and linked there.
+
 License
 -------
 The main Korn shell files are public domain (see `LEGAL`).
diff --git a/asprintf.c b/asprintf.c
@@ -27,18 +27,6 @@
 #include <stdio.h> /* for vsnprintf */
 #include <stdlib.h>
 
-#ifndef VA_COPY
-# ifdef HAVE_VA_COPY
-#  define VA_COPY(dest, src) va_copy(dest, src)
-# else
-#  ifdef HAVE___VA_COPY
-#   define VA_COPY(dest, src) __va_copy(dest, src)
-#  else
-#   define VA_COPY(dest, src) (dest) = (src)
-#  endif
-# endif
-#endif
-
 #define INIT_SZ	128
 
 int
@@ -52,7 +40,7 @@ vasprintf(char **str, const char *fmt, va_list ap)
 	if ((string = malloc(INIT_SZ)) == NULL)
 		goto fail;
 
-	VA_COPY(ap2, ap);
+	va_copy(ap2, ap);
 	ret = vsnprintf(string, INIT_SZ, fmt, ap2);
 	va_end(ap2);
 	if (ret >= 0 && ret < INIT_SZ) { /* succeeded with initial alloc */
@@ -66,7 +54,7 @@ vasprintf(char **str, const char *fmt, va_list ap)
 			free(string);
 			goto fail;
 		}
-		VA_COPY(ap2, ap);
+		va_copy(ap2, ap);
 		ret = vsnprintf(newstr, len, fmt, ap2);
 		va_end(ap2);
 		if (ret < 0 || (size_t)ret >= len) { /* failed with realloc'ed string */
diff --git a/configure b/configure
@@ -3,6 +3,88 @@
 # This configure script written by Brian Callahan <bcallah@openbsd.org>
 # and released into the Public Domain.
 
+Makefile() {
+  if [ -z "$cc" ] ; then
+    cc=$CC
+  fi
+cat << EOF > Makefile
+# This Makefile automatically generated by configure.
+
+CC =		$cc
+CFLAGS =	$cflags -std=gnu99
+EOF
+
+if [ ! -z "$ldflags" ] ; then
+cat << EOF >> Makefile
+LDFLAGS =	$ldflags
+EOF
+fi
+
+cat << EOF >> Makefile
+
+PREFIX =	$prefix
+BINDIR =	$bindir
+MANDIR =	$mandir
+
+PROG =	$instprog
+OBJS =	alloc.o asprintf.o c_ksh.o c_sh.o c_test.o c_ulimit.o edit.o \\
+	emacs.o eval.o exec.o expr.o history.o io.o jobs.o lex.o mail.o \\
+	main.o misc.o path.o shf.o syn.o table.o trap.o tree.o tty.o var.o \\
+	version.o vi.o confstr.o reallocarray.o siglist.o signame.o \\
+	strlcat.o strlcpy.o strtonum.o unvis.o vis.o
+
+all: \${PROG}
+
+\${PROG}: \${OBJS}
+EOF
+
+if [ $compileonly -eq 0 ] ; then
+cat << EOF >> Makefile
+	\${CC} \${LDFLAGS} -o \${PROG} \${OBJS} $libs
+EOF
+fi
+
+cat << EOF >> Makefile
+
+install:
+	install -d \${DESTDIR}\${BINDIR}
+	install -d \${DESTDIR}\${MANDIR}/man1
+	install -c -s -m 755 \${PROG} \${DESTDIR}\${BINDIR}
+	install -c -m 644 oksh.1 \${DESTDIR}\${MANDIR}/man1/\${PROG}.1
+EOF
+
+if [ $instsh -ne 0 ] ; then
+cat << EOF >> Makefile
+	install -d \${DESTDIR}\${BINDIR}
+	install -d \${DESTDIR}\${MANDIR}/man1
+	install -c -s -m 755 \${PROG} \${DESTDIR}\${BINDIR}/sh
+	install -c -m 644 sh.1 \${DESTDIR}\${MANDIR}/man1
+EOF
+fi
+
+cat << EOF >> Makefile
+	@echo "Please add $bindir/$instprog to /etc/shells"
+EOF
+
+if [ $instsh -ne 0 ] ; then
+cat << EOF >> Makefile
+	@echo "Please add $bindir/sh to /etc/shells"
+EOF
+fi
+
+cat << EOF >> Makefile
+
+test:
+	echo "No tests"
+
+clean:
+	rm -f \${PROG} \${OBJS}
+
+distclean: clean
+	rm -f Makefile pconfig.h
+EOF
+}
+
 asprintfcheck() {
   cat << EOF > conftest.c
 #include <stdio.h>
@@ -237,7 +319,7 @@ EOF
 noreturncheck() {
   cat << EOF > conftest.c
 #include <stdlib.h>
-__attribute__((noreturn)) usage(void){exit(1);}int main(void){usage();return 0;}
+__attribute__((__noreturn__)) usage(void){exit(1);}int main(void){usage();return 0;}
 EOF
   $cc $cflags $ldflags -o conftest conftest.c > /dev/null 2>&1
   if [ $? -eq 0 ] ; then
@@ -534,6 +616,8 @@ else
   prefix="/usr/local"
 fi
 
+doconfigure=1
+compileonly=0
 bindirset=0
 mandirset=0
 bindir="$prefix/bin"
@@ -602,6 +686,12 @@ do
 	  lto=0
 	fi
 	;;
+    --no-thanks)
+	doconfigure=0
+	;;
+    --no-link)
+	compileonly=1
+	;;
     --help|-h)
 	echo "Usage: configure [options]"
 	echo ""
@@ -624,6 +714,10 @@ do
 	echo "Install additional sh executable [default=no]"
 	printf "  --enable-static         "
 	echo "Statically link executables [default=no]"
+	printf "  --no-thanks             "
+	echo "Skip all configure checks"
+	printf "  --no-link               "
+	echo "Compile only, do not link (requires --no-thanks)"
 	exit 1
 	;;
     *)
@@ -647,6 +741,53 @@ if [ $static -ne 0 ] ; then
   ldflags="${ldflags}-static"
 fi
 
+if [ $compileonly -eq 1 ] ; then
+  if [ $doconfigure -eq 1 ] ; then
+    echo "ERROR: --no-link requires --no-thanks"
+    exit 1
+  fi
+fi
+
+if [ $doconfigure -eq 0 ] ; then
+  echo "OK, I trust you."
+  echo "I'll assume you have gcc/clang and little else."
+  echo "Edit Makefile and pconfig.h if needed."
+  echo "Good luck!"
+  cat << EOF > pconfig.h
+/* This file automatically generated by configure.  */
+
+#ifndef __dead
+#define __dead __attribute__((__noreturn__))
+#endif
+
+/* #define __attribute__(x) */
+
+/* #define HAVE_ASPRINTF */
+/* #define HAVE_CONFSTR */
+#define NO_CURSES
+/* #define HAVE_PLEDGE */
+/* #define HAVE_REALLOCARRAY */
+/* #define HAVE_SETRESGID */
+/* #define HAVE_SETRESUID */
+/* #define HAVE_SIG_T */
+/* #define HAVE_SRAND_DETERMINISTIC */
+/* #define HAVE_ST_MTIM */
+#define HAVE_ST_MTIMESPEC
+/* #define HAVE_STRAVIS */
+/* #define HAVE_STRLCAT */
+/* #define HAVE_STRLCPY */
+/* #define HAVE_STRTONUM */
+/* #define HAVE_STRUNVIS */
+/* #define HAVE_SIGLIST */
+/* #define HAVE_SIGNAME */
+/* #define HAVE_TIMERADD */
+/* #define HAVE_TIMERSUB */
+EOF
+  Makefile
+
+  exit 0
+fi
+
 printf "checking for C compiler... "
 cccheck
 if [ $? -ne 0 ] ; then
@@ -977,72 +1118,5 @@ else
 fi
 
 printf "creating Makefile... "
-cat << EOF > Makefile
-# This Makefile automatically generated by configure.
-
-CC =		$cc
-CFLAGS =	$cflags
-EOF
-
-if [ ! -z "$ldflags" ] ; then
-cat << EOF >> Makefile
-LDFLAGS =	$ldflags
-EOF
-fi
-
-cat << EOF >> Makefile
-
-PREFIX =	$prefix
-BINDIR =	$bindir
-MANDIR =	$mandir
-
-PROG =	$instprog
-OBJS =	alloc.o asprintf.o c_ksh.o c_sh.o c_test.o c_ulimit.o edit.o \\
-	emacs.o eval.o exec.o expr.o history.o io.o jobs.o lex.o mail.o \\
-	main.o misc.o path.o shf.o syn.o table.o trap.o tree.o tty.o var.o \\
-	version.o vi.o confstr.o reallocarray.o siglist.o signame.o \\
-	strlcat.o strlcpy.o strtonum.o unvis.o vis.o
-
-all: \${PROG}
-
-\${PROG}: \${OBJS}
-	\${CC} \${LDFLAGS} -o \${PROG} \${OBJS} $libs
-
-install:
-	install -d \${DESTDIR}\${BINDIR}
-	install -d \${DESTDIR}\${MANDIR}/man1
-	install -c -s -m 755 \${PROG} \${DESTDIR}\${BINDIR}
-	install -c -m 644 oksh.1 \${DESTDIR}\${MANDIR}/man1/\${PROG}.1
-EOF
-
-if [ $instsh -ne 0 ] ; then
-cat << EOF >> Makefile
-	install -d \${DESTDIR}\${BINDIR}
-	install -d \${DESTDIR}\${MANDIR}/man1
-	install -c -s -m 755 \${PROG} \${DESTDIR}\${BINDIR}/sh
-	install -c -m 644 sh.1 \${DESTDIR}\${MANDIR}/man1
-EOF
-fi
-
-cat << EOF >> Makefile
-	@echo "Please add $bindir/$instprog to /etc/shells"
-EOF
-
-if [ $instsh -ne 0 ] ; then
-cat << EOF >> Makefile
-	@echo "Please add $bindir/sh to /etc/shells"
-EOF
-fi
-
-cat << EOF >> Makefile
-
-test:
-	echo "No tests"
-
-clean:
-	rm -f \${PROG} \${OBJS}
-
-distclean: clean
-	rm -f Makefile pconfig.h
-EOF
+Makefile
 echo "done"
diff --git a/portable.h b/portable.h
@@ -153,9 +153,14 @@
 #endif /* !HAVE_TIMERSUB */
 
 /* struct stat compatibility */
-#if !defined(HAVE_ST_MTIM) && defined(HAVE_ST_MTIMESPEC)
+#ifndef HAVE_ST_MTIM
+#ifndef HAVE_ST_MTIMESPEC
+#define st_mtim st_mtime
+#define timespeccmp(tsp, usp, cmp) (tsp) cmp (usp)
+#else
 #define st_mtim	st_mtimespec
-#endif /* !HAVE_ST_MTIM && HAVE_ST_MTIMESPEC */
+#endif /* !HAVE_ST_TIMESPEC */
+#endif /* !HAVE_ST_MTIM */
 
 /* Cygwin already has a sys_signame but we want to use our own */
 #ifdef __CYGWIN__
@@ -189,11 +194,6 @@
         } while (0)
 #endif
 
-#if !defined(HAVE_ST_MTIM) && !defined(HAVE_ST_MTIMESPEC)
-#define timespeccmp(tsp, usp, cmp) (tsp) cmp (usp)
-#define st_mtim st_mtime
-#endif /* !HAVE_ST_MTIM && !HAVE_ST_TIMESPEC */
-
 /*
  * Prototypes
  */