shithub: riscv

Download patch

ref: 42a7fcabb040928915756bd686f042063d48d8ec
parent: 79fbfbff48c19e11c60317797e7b9f2903e69a37
author: cinap_lenrek <cinap_lenrek@felloff.net>
date: Sun May 1 20:34:23 EDT 2016

awk: bring back ENVIRON[] support

--- a/sys/src/cmd/awk/proto.h
+++ b/sys/src/cmd/awk/proto.h
@@ -77,7 +77,7 @@
 
 extern	void	syminit(void);
 extern	void	arginit(int, char **);
-extern	void	envinit(char **);
+extern	void	envinit(void);
 extern	Array	*makesymtab(int);
 extern	void	freesymtab(Cell *);
 extern	void	freeelem(Cell *, char *);
--- a/sys/src/cmd/awk/tran.c
+++ b/sys/src/cmd/awk/tran.c
@@ -113,24 +113,35 @@
 	}
 }
 
-void envinit(char **envp)	/* set up ENVIRON variable */
+void envinit(void)	/* set up ENVIRON variable */
 {
-	Cell *cp;
-	char *p;
+	int	fd, i, n;
+	char	*k, *v;
+	Dir	*buf;
 
-	cp = setsymtab("ENVIRON", "", 0.0, ARR, symtab);
 	ENVtab = makesymtab(NSYMTAB);
-	cp->sval = (char *) ENVtab;
-	for ( ; *envp; envp++) {
-		if ((p = strchr(*envp, '=')) == nil)
-			continue;
-		*p++ = 0;	/* split into two strings at = */
-		if (is_number(p))
-			setsymtab(*envp, p, atof(p), STR|NUM, ENVtab);
-		else
-			setsymtab(*envp, p, 0.0, STR, ENVtab);
-		p[-1] = '=';	/* restore in case env is passed down to a shell */
+	if ((fd = open("/env", OREAD)) < 0)
+		return;
+
+	buf = nil;
+	while((n = dirread(fd, &buf)) > 0) {
+		for (i = 0; i < n; i++) {
+			k = buf[i].name;
+			if(strncmp(k, "fn#", 3) == 0)
+				continue;
+			if ((v = getenv(k)) == nil)
+				continue;
+			if (is_number(v))
+				setsymtab(k, v, atof(v), STR|NUM, ENVtab);
+			else
+				setsymtab(k, v, 0.0, STR, ENVtab);
+			free(v);
+		}
+		free(buf);
+		buf = nil;
 	}
+
+	close(fd);
 }
 
 Array *makesymtab(int n)	/* make a new symbol table */
@@ -210,9 +221,15 @@
 	if (p == nil)
 		FATAL("out of space for symbol table at %s", n);
 	p->nval = tostring(n);
-	p->sval = s ? tostring(s) : tostring("");
 	p->fval = f;
-	p->tval = t;
+	if(tp == symtab && strcmp(n, "ENVIRON") == 0 && !safe) {
+		envinit();
+		p->sval = (char *) ENVtab;
+		p->tval = ARR;
+	} else {
+		p->sval = s ? tostring(s) : tostring("");
+		p->tval = t;
+	}
 	p->csub = CUNK;
 	p->ctype = OCELL;
 	tp->nelemt++;