code: 9ferno

Download patch

ref: 5f298c01b68e8c626e6fae21c63327fa9c76007c
parent: ed77f1550337e186ee740e42a1506fc61a5385c5
author: 9ferno <gophone2015@gmail.com>
date: Mon Feb 14 12:15:27 EST 2022

working forth interpreter

--- a/os/port/devbin.c
+++ b/os/port/devbin.c
@@ -7,6 +7,7 @@
 
 #define	WHICHFILE(q) ((q).path)
 #define	SOURCEFD(q)	((q).vers)
+int debug = 0;
 
 /*
 	An fd created here might be used by different processes at the same time
@@ -190,13 +191,17 @@
 		up->fgrp->fd[sourcefd]->mode != ORDWR)
 		error(Ebadarg);
 
-	/* check if this fd has been opened for reading */
+	/* find any other buffering file that has opened
+		this source fd, so the Buffer is used
+		by both to keep the pointers in sync */
 	bin = nil;
-	for(nfd = 0; nfd < up->fgrp->maxfd; nfd++){
+	for(nfd = 0; nfd <= up->fgrp->maxfd; nfd++){
 		if(up->fgrp->fd[nfd] != nil &&
 			up->fgrp->fd[nfd]->type == bintype &&
+			up->fgrp->fd[nfd]->aux != nil &&
 			((Binreader*)up->fgrp->fd[nfd]->aux)->bin->sourcefd == sourcefd){
 			bin = ((Binreader*)up->fgrp->fd[nfd]->aux)->bin;
+			DBG("binopen found bin chanpath(c) %s br %p bin %p\n", chanpath(c), up->fgrp->fd[nfd]->aux, bin);
 			qlock(bin);
 			incref(bin);
 			qunlock(bin);	
@@ -211,10 +216,13 @@
 			panic("exhausted memory");
 		br->bin = bin;
 		bin->eof=0;
+		bin->sourcefd = sourcefd;
 		incref(bin);
+		DBG("binopen new bin chanpath(c) %s br %p bin %p\n", chanpath(c), br, bin);
 	} else
 		br->bin = bin;
 
+	DBG("binopen chanpath(c) %s br %p bin %p\n", chanpath(c), br, bin);
 	whichfile = WHICHFILE(c->qid);
 	for(s = searchers; s != nil; s++){
 		if(whichfile == s->whichfile){
@@ -277,20 +285,36 @@
 
 	if(bin->readp == bin->writep){
 		bin->readp = bin->writep = bin->buf;
+		DBG("refill: same readp 0x%p writep 0x%p\n",
+				bin->readp, bin->writep);
 	}else if(bin->readp > bin->buf){
 		n = bin->writep-bin->readp;
 		memmove(bin->buf, bin->readp, n);
 		bin->writep = bin->buf+n;
 		bin->readp = bin->buf;
+		DBG("refill: move readp 0x%p writep 0x%p\n",
+				bin->readp, bin->writep);
 	}
 
+	DBG("refill: read devtab[n].name %s devtab[n].dc %d"
+		"	bin->sourcechan->type %d bin->sourcechan->offset %lld\n",
+		devtab[bin->sourcechan->type]->name, devtab[bin->sourcechan->type]->dc,
+		bin->sourcechan->type, bin->sourcechan->offset);
 	nr = devtab[bin->sourcechan->type]->read(bin->sourcechan, bin->writep,
 												 bin->bufsize-(bin->writep-bin->readp),
 												 bin->sourcechan->offset);
+	DBG("refill: read nr %d bin->sourcechan->offset %lld\n", nr, bin->sourcechan->offset);
 	if(nr == 0)
 		bin->eof = 1;
 
 	bin->writep += nr;
+
+	/* check the notes of rread in os/port/sysfile.c for details */
+	lock(bin->sourcechan);
+	bin->sourcechan->devoffset += nr;
+	bin->sourcechan->offset += nr;
+	unlock(bin->sourcechan);
+
 	return nr;
 }
 
@@ -322,6 +346,8 @@
 	if(n > bin->bufsize)
 		error(Ebadarg);
 
+	DBG("binread: starting chanpath(c) %s %s readp 0x%p writep 0x%p maxn %d\n",
+		chanpath(c), s->name, bin->readp, bin->writep, n);
 	qlock(bin);
 	if(s->whichfile == Qbufferlength){
 		rv = snprint(nlenstr, 10, "%lld\n", bin->writep-bin->readp);
@@ -359,6 +385,8 @@
 		bin->readp = nextreadp;
 		rv = nsend;
 Exit:
+	DBG("binread: ending chanpath(c) %s %s readp 0x%p writep 0x%p maxn %d\n",
+		chanpath(c), s->name, bin->readp, bin->writep, n);
 	qunlock(bin);
 	return rv;
 }
@@ -458,8 +486,8 @@
 
 	/* find ending delimiter */
 	for(n=0; p<writep && n < maxn; p++){
-		*nextreadp = p;
 		if(*p == ' ' || *p == '	' || *p == '\n'){
+			*nextreadp = p+1; /* skip this for the next read */
 			return n;
 		}
 		n++;
@@ -466,8 +494,10 @@
 	}
 
 	/* no delimiter found in maxn bytes, send maxn */
-	if(n == maxn)
+	if(n == maxn){
+		*nextreadp += maxn;
 		return maxn;
+	}
 
 	/* no delimiter found before writep */
 	return 0;
@@ -502,15 +532,19 @@
 
 	/* find ending delimiter */
 	for(n=0; p<writep && n < maxn; p++){
-		*nextreadp = p;
-		if(*p == '\n')
+		DBG("linefn: read p 0x%p *p %d\n", p, *p);
+		if(*p == '\n'){
+			*nextreadp = p+1; /* skip this for the next read */
 			return n;
+		}
 		n++;
 	}
 
 	/* no delimiter found in maxn bytes, send maxn */
-	if(n == maxn)
+	if(n == maxn){
+		*nextreadp += maxn;
 		return maxn;
+	}
 
 	/* no delimiter found before writep */
 	return 0;
@@ -531,7 +565,6 @@
 
 	/* find ending delimiter */
 	for(n=0; p<writep && n < maxn; p++){
-		*nextreadp = p;
 		if(*p == c){
 			*nextreadp = p+1; /* skip this for the next read */
 			return n;
@@ -540,8 +573,10 @@
 	}
 
 	/* no delimiter found in maxn bytes, send maxn */
-	if(n == maxn)
+	if(n == maxn){
+		*nextreadp += maxn;
 		return maxn;
+	}
 
 	return 0;
 }
--- a/os/port/devforth.c
+++ b/os/port/devforth.c
@@ -242,7 +242,6 @@
 	h = fmem+DICTIONARY;
 	dtop = nil;
 	vh = fmem+FORTHVARS;
-debug = 1;
 	DBG("loadforthdictionary fmem 0x%p\n"
 			"	here 0x%p dtop 0x%p there 0x%p\n"
 			"	(intptr*)(fmem + DTOP) 0x%p *(intptr*)(fmem + DTOP) 0x%zX\n"
@@ -261,38 +260,38 @@
 		}else if(f->what == Here && f->type == Absolute){
 			h = fmem+DICTIONARY+f->here;
 			*(intptr*)h = f->p;
-			DBG("	%s 0x%zX: 0x%zX %lld: %lld\n", f->desc, h, *(intptr*)h, h, *(intptr*)h);
+			DBG("	%s 0x%zX: 0x%zX %lld: %lld\n", f->desc, (intptr)h, *(intptr*)h, (intptr)h, *(intptr*)h);
 		}else if(f->what == Here && f->type == Absoluteptr){
 			h = fmem+DICTIONARY+f->here;
 			*(intptr*)h = (intptr)f->ptr;
-			DBG("	%s 0x%zX: 0x%zX %lld: %lld\n", f->desc, h, *(intptr*)h, h, *(intptr*)h);
+			DBG("	%s 0x%zX: 0x%zX %lld: %lld\n", f->desc, (intptr)h, *(intptr*)h, (intptr)h, *(intptr*)h);
 		}else if(f->what == Here && f->type == Byte){
 			h = fmem+DICTIONARY+f->here;
 			*(s8*)h = f->b;
-			DBG("	%s 0x%zX: 0x%d %lld: %d\n", f->desc, h, *(char*)h, h, *(char*)h);
+			DBG("	%s 0x%zX: 0x%d %lld: %d\n", f->desc, (intptr)h, *(char*)h, (intptr)h, *(char*)h);
 		}else if(f->what == Here && f->type == Chars){
 			h = fmem+DICTIONARY+f->here;
 			len = strlen(f->str);
 			strncpy((s8*)h, f->str, len);
-			DBG("	%s 0x%zX: %s %lld: %s\n", f->desc, h, (char*)h, h, (char*)h);
+			DBG("	%s 0x%zX: %s %lld: %s\n", f->desc, (intptr)h, (char*)h, (intptr)h, (char*)h);
 		}else if(f->what == Here && f->type == Relative){
 			h = fmem+DICTIONARY+f->here;
 			*(intptr*)h = (intptr)fmem+f->p;
-			DBG("	%s 0x%zX: 0x%zX %lld: %lld\n", f->desc, h, *(intptr*)h, h, *(intptr*)h);
+			DBG("	%s 0x%zX: 0x%zX %lld: %lld\n", f->desc, (intptr)h, *(intptr*)h, (intptr)h, *(intptr*)h);
 		}else if(f->what == Here && f->type == Relativedictionary){
 			h = fmem+DICTIONARY+f->here;
 			*(intptr*)h = (intptr)fmem+DICTIONARY+f->p;
-			DBG("	%s 0x%zX: 0x%zX %lld: %lld\n", f->desc, h, *(intptr*)h, h, *(intptr*)h);
+			DBG("	%s 0x%zX: 0x%zX %lld: %lld\n", f->desc, (intptr)h, *(intptr*)h, (intptr)h, *(intptr*)h);
 		}else if(f->what == Here && f->type == Relativevar){
 			h = fmem+DICTIONARY+f->here;
 			*(intptr*)h = (intptr)fmem+f->p;
-			DBG("	%s 0x%zX: 0x%zX %lld: %lld\n", f->desc, h, *(intptr*)h, h, *(intptr*)h);
+			DBG("	%s 0x%zX: 0x%zX %lld: %lld\n", f->desc, (intptr)h, *(intptr*)h, (intptr)h, *(intptr*)h);
 		}else if(f->what == Here && f->type == End){
 			h = fmem+DICTIONARY+f->here;
 			DBG("	%s 0x%zX %lld\n", f->desc, h, h);
 		}else if(f->what == There && f->type == End){
 			vh = fmem+FORTHVARS+f->there;
-			DBG("	%s 0x%zX %lld\n", f->desc, vh, vh);
+			DBG("	%s 0x%zX %lld\n", f->desc, (intptr)vh, (intptr)vh);
 		}else if(f->what == Dtop){
 			dtop = (u8*)fmem+DICTIONARY+f->p;
 			DBG("	%s 0x%zX %lld\n", f->desc, (intptr)dtop, (intptr)dtop);
--- a/os/port/devproc.c
+++ b/os/port/devproc.c
@@ -584,7 +584,7 @@
 static int
 procargs(Proc *p, char *buf, int nbuf)
 {
-/*	int j, k, m;
+	/*int j, k, m;*/
 	char *a;
 	int n;
 
@@ -592,7 +592,9 @@
 	if(p->setargs)
 		return snprint(buf, nbuf, "%s [%s]", p->text, p->args);
 	n = p->nargs;
-	for(j = 0; j < nbuf - 1; j += m){
+	strncpy(buf, a, n); /* using varables to avoid warnings */
+	return n;
+/*	for(j = 0; j < nbuf - 1; j += m){
 		if(n <= 0)
 			break;
 		if(j != 0)
@@ -602,8 +604,7 @@
 		a += k;
 		n -= k;
 	}
-	return j; */
-	return 0;
+	return j;*/
 }
 
 static int
@@ -1204,13 +1205,13 @@
 
 	switch(QID(c->qid)){
 	case Qargs:
-/*		if(offset != 0 || n >= sizeof(buf))
+		if(offset != 0 || n >= sizeof(buf))
 			error(Etoobig);
 		memmove(buf, va, n);
 		buf[n] = 0;
 		kstrdup(&p->args, buf);
 		p->nargs = 0;
-		p->setargs = 1;*/
+		p->setargs = 1;
 		break;
 
 	case Qmem: