shithub: trueawk

Download patch

ref: 8da361bb89c76cf969d6c85f4364cb84475938cc
parent: d7f37646965ee26214da0e1d97f3c1d54349ae2d
author: qwx <qwx@sciops.net>
date: Sat Oct 11 07:58:19 EDT 2025

9front ape port

- reuse ape lrand for random()
- redefine RAND_MAX to be what the maintainers assume it to be
- use %ll instead of %j for printf formats
- initialize array in get_str_val in case snprintf doesn't write
  any data (T.overflow)
- fork rc instead of ape/sh with system()

--- a/awk.h
+++ b/awk.h
@@ -271,4 +271,5 @@
 } fa;
 
 
+#include "plan9.h"
 #include "proto.h"
--- a/awkgram.y
+++ b/awkgram.y
@@ -25,6 +25,7 @@
 %{
 #include <stdio.h>
 #include <string.h>
+#include <stdlib.h>
 #include "awk.h"
 
 void checkdup(Node *list, Cell *item);
--- a/lib.c
+++ b/lib.c
@@ -25,7 +25,7 @@
 #define DEBUG
 #include <stdio.h>
 #include <string.h>
-#include <strings.h>
+//#include <strings.h>
 #include <ctype.h>
 #include <errno.h>
 #include <stdlib.h>
--- /dev/null
+++ b/mkfile
@@ -1,0 +1,46 @@
+</$objtype/mkfile
+TARG=trueawk
+BIN=/$objtype/bin
+
+YFILES=awkgram.y
+OFILES=\
+	awkgram.$O\
+	b.$O\
+	lex.$O\
+	lib.$O\
+	main.$O\
+	parse.$O\
+	plan9.$O\
+	proctab.$O\
+	rand.$O\
+	run.$O\
+	tran.$O\
+
+HFILES=\
+	awk.h\
+	plan9.h\
+	proto.h\
+	awkgram.tab.h\
+
+default:V: all
+
+</sys/src/cmd/mkone
+
+YFLAGS=$YFLAGS -S -s awkgram
+CC=pcc
+CFLAGS=-c -D_POSIX_SOURCE -D_BSD_EXTENSION -D_PLAN9_SOURCE
+CLEANFILES=awkgram.c awkgram.tab.h proctab.c $cputype.maketab maketab.$O
+#LDFLAGS=-p
+
+awkgram.tab.h awkgram.c:	$YFILES
+	$YACC -o awkgram.c $YFLAGS $prereq
+
+proctab.c:	$cputype.maketab
+	./$cputype.maketab awkgram.tab.h >proctab.c
+
+$cputype.maketab:	awkgram.tab.h maketab.c
+	objtype=$cputype mk maketab.$cputype
+
+maketab.$objtype:V:
+	$CC $CFLAGS maketab.c
+	$LD $LDFLAGS -o $objtype.maketab maketab.$O
--- /dev/null
+++ b/plan9.c
@@ -1,0 +1,44 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <ctype.h>
+#include <sys/wait.h>
+#include <utf.h>
+#include "awk.h"
+
+wint_t
+towupper(wint_t r)
+{
+	return toupperrune(r);
+}
+
+wint_t
+towlower(wint_t r)
+{
+	return tolowerrune(r);
+}
+
+int
+system(const char *s)	/* ape using ape/sh breaks tests */
+{
+	int w, status;
+	pid_t pid;
+
+	if(!s)
+		return 1;
+	pid = fork();
+	if(pid == 0) {
+		execl("/bin/rc", "rc", "-c", s, NULL);
+		_exit(1);
+	}
+	if(pid < 0)
+		return -1;
+	for(;;) {
+		w = wait(&status);
+		if(w == -1 || w == pid)
+			break;
+	}
+	if(w == -1)
+		return w;
+	return status;
+}
--- /dev/null
+++ b/plan9.h
@@ -1,0 +1,17 @@
+//#define __DJGPP__
+#include <bsd.h>
+
+#define __attribute__(x)
+typedef size_t wint_t;
+
+wint_t	towupper(wint_t);
+wint_t	towlower(wint_t);
+
+long	random(void);
+void	srandom(unsigned long);
+
+/* RAND_MAX is implementation-defined but assumed to be 2^31 */
+#undef RAND_MAX
+#define	RAND_MAX	2147483647
+
+#define signbit(x) (x == -0.0 || x < 0.0)
--- /dev/null
+++ b/rand.c
@@ -1,0 +1,67 @@
+/* /sys/src/ape/lib/ap/gen/rand.c */
+#include	<stdlib.h>
+
+/*
+ *	algorithm by
+ *	D. P. Mitchell & J. A. Reeds
+ */
+#define	LEN	607
+#define	TAP	273
+#define	MASK	0x7fffffffL
+#define	A	48271
+#define	M	2147483647
+#define	Q	44488
+#define	R	3399
+
+typedef unsigned long	ulong;
+
+static	ulong	rng_vec[LEN];
+static	ulong*	rng_tap = rng_vec;
+static	ulong*	rng_feed = 0;
+
+void
+srandom(ulong seed)
+{
+	long lo, hi, x;
+	int i;
+
+	rng_tap = rng_vec;
+	rng_feed = rng_vec+LEN-TAP;
+	seed = seed%M;
+	if(seed == 0)
+		seed = 89482311;
+	x = seed;
+	/*
+	 *	Initialize by x[n+1] = 48271 * x[n] mod (2**31 - 1)
+	 */
+	for(i = -20; i < LEN; i++) {
+		hi = x / Q;
+		lo = x % Q;
+		x = A*lo - R*hi;
+		if(x < 0)
+			x += M;
+		if(i >= 0)
+			rng_vec[i] = x;
+	}
+}
+
+long
+random(void)
+{
+	ulong x;
+
+	rng_tap--;
+        if(rng_tap < rng_vec) {
+                if(rng_feed == 0) {
+			srand(1);
+			rng_tap--;
+		}
+                rng_tap += LEN;
+        }
+	rng_feed--;
+        if(rng_feed < rng_vec)
+                rng_feed += LEN;
+	x = (*rng_feed + *rng_tap) & MASK;
+	*rng_feed = x;
+        return x;
+}
--- a/run.c
+++ b/run.c
@@ -26,7 +26,7 @@
 #include <stdio.h>
 #include <ctype.h>
 #include <errno.h>
-#include <wctype.h>
+//#include <wctype.h>
 #include <fcntl.h>
 #include <setjmp.h>
 #include <limits.h>
@@ -1147,7 +1147,7 @@
 		*t = '\0';
 		if (fmtwd < 0)
 			fmtwd = -fmtwd;
-		adjbuf(&buf, &bufsize, fmtwd+1+p-buf, recsize, &p, "format4");
+		adjbuf(&buf, &bufsize, fmtwd+2+p-buf, recsize, &p, "format4");
 		switch (*s) {
 		case 'a': case 'A':
 			if (have_a_format)
@@ -1160,9 +1160,10 @@
 			break;
 		case 'd': case 'i': case 'o': case 'x': case 'X': case 'u':
 			flag = (*s == 'd' || *s == 'i') ? 'd' : 'u';
-			*(t-1) = 'j';
-			*t = *s;
-			*++t = '\0';
+			*(t-1) = 'l';
+			*t++ = 'l';
+			*t++ = *s;
+			*t = '\0';
 			break;
 		case 's':
 			flag = 's';
--- a/tran.c
+++ b/tran.c
@@ -421,9 +421,11 @@
 		return NULL;
 }
 
+/* T.overflow: ape snprintf silenty breaks if there
+ * already are FOPEN_MAX files open */
 static char *get_str_val(Cell *vp, char **fmt)        /* get string val of a Cell */
 {
-	char s[256];
+	char s[256] = {0};
 	double dtemp;
 	const char *p;
 
--