code: drawterm

Download patch

ref: c38bce1159b6edee799618f49f700ac3616fd2b0
parent: 78a654c8a70d4c041c8860820142ea89f167b8b4
author: cinap_lenrek <cinap_lenrek@felloff.net>
date: Sat May 28 14:49:39 EDT 2016

add -G flag to disable graphics mode

--- a/Make.win32
+++ b/Make.win32
@@ -17,7 +17,6 @@
 IP=win32
 OS=win32
 GUI=win32
-LDFLAGS=-mwindows
 LDADD=-lkernel32 -ladvapi32 -lgdi32 -lmpr -lwsock32 -lmsvcrt -lmingw32 -lwinmm
 TARG=drawterm.exe
 XOFILES=glenda-t.$O
--- a/cpu.c
+++ b/cpu.c
@@ -26,6 +26,7 @@
 static int	aanfilter;
 static int	norcpu;
 static int	nokbd;
+static int	nogfx;
 
 static char	*ealgs = "rc4_256 sha1";
 
@@ -39,14 +40,13 @@
 void
 exits(char *s)
 {
-	print("\ngoodbye\n");
-	for(;;) osyield();
+	exit(1);
 }
 
 void
 usage(void)
 {
-	fprint(2, "usage: drawterm [-BOp] [-h host | -c cpuserver] [-a authserver] [-s secstore] [-u user] [-r root]\n");
+	fprint(2, "usage: drawterm [-GBOp] [-h host | -c cpuserver] [-a authserver] [-s secstore] [-u user] [-r root]\n");
 	exits("usage");
 }
 
@@ -242,6 +242,9 @@
 	case 'u':
 		user = EARGF(usage());
 		break;
+	case 'G':
+		nogfx = 1;
+		/* wet floor */
 	case 'B':
 		nokbd = 1;
 		break;
@@ -258,6 +261,14 @@
 	if(argc != 0)
 		usage();
 
+	if(!nogfx) {
+		screeninit();
+		if(bind("#m", "/dev", MBEFORE) < 0)
+			panic("bind #m: %r");
+		if(bind("#i", "/dev", MBEFORE) < 0)
+			panic("bind #i: %r");
+	}
+
 	if(bind("/root", "/", MAFTER) < 0)
 		panic("bind /root: %r");
 
@@ -428,10 +439,7 @@
 int
 authdial(char *net, char *dom)
 {
-	int fd;
-	fd = dial(netmkaddr(authserver, "tcp", "567"), 0, 0, 0);
-	//print("authdial %d\n", fd);
-	return fd;
+	return dial(netmkaddr(authserver, "tcp", "567"), 0, 0, 0);
 }
 
 static int
--- a/gui-win32/screen.c
+++ b/gui-win32/screen.c
@@ -52,6 +52,7 @@
 	int fmt;
 	int dx, dy;
 
+	FreeConsole();
 	memimageinit();
 	if(depth == 0)
 		depth = GetDeviceCaps(GetDC(NULL), BITSPIXEL);
@@ -647,10 +648,4 @@
 
 	CloseClipboard();
 	return n;
-}
-
-int
-atlocalconsole(void)
-{
-	return 1;
 }
--- a/gui-x11/x11.c
+++ b/gui-x11/x11.c
@@ -1002,40 +1002,6 @@
 	/* no-op */
 }
 
-int
-atlocalconsole(void)
-{
-	char *p, *q;
-	char buf[128];
-
-	p = getenv("DRAWTERM_ATLOCALCONSOLE");
-	if(p && atoi(p) == 1)
-		return 1;
-
-	p = getenv("DISPLAY");
-	if(p == nil)
-		return 0;
-
-	/* extract host part */
-	q = strchr(p, ':');
-	if(q == nil)
-		return 0;
-	*q = 0;
-
-	if(strcmp(p, "") == 0)
-		return 1;
-
-	/* try to match against system name (i.e. for ssh) */
-	if(gethostname(buf, sizeof buf) == 0){
-		if(strcmp(p, buf) == 0)
-			return 1;
-		if(strncmp(p, buf, strlen(p)) == 0 && buf[strlen(p)]=='.')
-			return 1;
-	}
-
-	return 0;
-}
-
 /*
  * Cut and paste.  Just couldn't stand to make this simple...
  */
--- a/kern/Makefile
+++ b/kern/Makefile
@@ -37,7 +37,6 @@
 	qio.$O\
 	qlock.$O\
 	term.$O\
-	uart.$O\
 	waserror.$O\
 	$(OS).$O
 
--- a/kern/devcons.c
+++ b/kern/devcons.c
@@ -8,16 +8,17 @@
 
 #include	<authsrv.h>
 
+#undef write
+#undef read
+
 void	(*consdebug)(void) = 0;
 void	(*screenputs)(char*, int) = 0;
 
 Queue*	kbdq;			/* unprocessed console input */
 Queue*	lineq;			/* processed console input */
-Queue*	serialoq;		/* serial console output */
 Queue*	kprintoq;		/* console output, for /dev/kprint */
 long	kprintinuse;		/* test and set whether /dev/kprint is open */
 Lock	kprintlock;
-int	iprintscreenputs = 0;
 
 int	panicking;
 
@@ -100,25 +101,9 @@
 	qnoblock(kbdq, 1);
 }
 
-int
-consactive(void)
-{
-	if(serialoq)
-		return qlen(serialoq) > 0;
-	return 0;
-}
-
 void
 prflush(void)
 {
-/*
-	ulong now;
-
-	now = m->ticks;
-	while(consactive())
-		if(m->ticks - now >= HZ)
-			break;
-*/
 }
 
 /*
@@ -146,6 +131,8 @@
 			qiwrite(kprintoq, str, n);
 	}else if(screenputs != 0)
 		screenputs(str, n);
+	else
+		write(1, str, n);
 }
 
 void
@@ -154,8 +141,6 @@
 	putstrn0(str, n, 0);
 }
 
-int noprint;
-
 int
 print(char *fmt, ...)
 {
@@ -163,9 +148,6 @@
 	va_list arg;
 	char buf[PRINTSIZE];
 
-	if(noprint)
-		return -1;
-
 	va_start(arg, fmt);
 	n = vseprint(buf, buf+sizeof(buf), fmt, arg) - buf;
 	va_end(arg);
@@ -193,7 +175,8 @@
 	n = vseprint(buf+strlen(buf), buf+sizeof(buf), fmt, arg) - buf;
 	va_end(arg);
 	buf[n] = '\n';
-	uartputs(buf, n+1);
+	if(screenputs != 0)
+		write(2, buf, n+1);
 	if(consdebug)
 		(*consdebug)();
 	spllo();
@@ -262,35 +245,6 @@
 }
 
 static void
-echoserialoq(char *buf, int n)
-{
-	char *e, *p;
-	char ebuf[128];
-	int x;
-
-	p = ebuf;
-	e = ebuf + sizeof(ebuf) - 4;
-	while(n-- > 0){
-		if(p >= e){
-			qiwrite(serialoq, ebuf, p - ebuf);
-			p = ebuf;
-		}
-		x = *buf++;
-		if(x == '\n'){
-			*p++ = '\r';
-			*p++ = '\n';
-		} else if(x == 0x15){
-			*p++ = '^';
-			*p++ = 'U';
-			*p++ = '\n';
-		} else
-			*p++ = x;
-	}
-	if(p != ebuf)
-		qiwrite(serialoq, ebuf, p - ebuf);
-}
-
-static void
 echo(char *buf, int n)
 {
 	static int ctrlt;
@@ -368,34 +322,10 @@
 		return;
 	if(screenputs != 0)
 		echoscreen(buf, n);
-	if(serialoq)
-		echoserialoq(buf, n);
+	else
+		write(1, buf, n);
 }
 
-/*
- *  Called by a uart interrupt for console input.
- *
- *  turn '\r' into '\n' before putting it into the queue.
- */
-int
-kbdcr2nl(Queue *q, int ch)
-{
-	char *next;
-
-	USED(q);
-	ilock(&kbd.lockputc);		/* just a mutex */
-	if(ch == '\r' && !kbd.raw)
-		ch = '\n';
-	next = kbd.iw+1;
-	if(next >= kbd.ie)
-		next = kbd.istage;
-	if(next != kbd.ir){
-		*kbd.iw = ch;
-		kbd.iw = next;
-	}
-	iunlock(&kbd.lockputc);
-	return 0;
-}
 static
 void
 _kbdputc(int c)
@@ -409,8 +339,6 @@
 	if(n == 0)
 		return;
 	echo(buf, n);
-//	kbd.c = r;
-//	qproduce(kbdq, buf, n);
 }
 
 /* _kbdputc, but with compose translation */
@@ -545,11 +473,6 @@
 {
 	todinit();
 	randominit();
-	/*
-	 * at 115200 baud, the 1024 char buffer takes 56 ms to process,
-	 * processing it every 22 ms should be fine
-	 */
-/*	addclock0link(kbdputcclock, 22); */
 }
 
 static Chan*
@@ -645,6 +568,23 @@
 	}
 }
 
+static int
+readcons(Queue *q, char *buf, int n)
+{
+	while(screenputs==0 && !qcanread(q)){
+		int c;
+
+		if(!isatty(0))
+			return read(0, buf, n);
+		if((c = _getch()) == -1)
+			break;
+		if(c == '\r')
+			c = '\n';
+		kbdputc(q, c);
+	}
+	return qread(q, buf, n);
+}
+
 static long
 consread(Chan *c, void *buf, long n, vlong off)
 {
@@ -672,7 +612,7 @@
 			else {
 				/* read as much as possible */
 				do {
-					i = qread(kbdq, cbuf, n);
+					i = readcons(kbdq, cbuf, n);
 					cbuf += i;
 					n -= i;
 				} while (n>0 && qcanread(kbdq));
@@ -680,7 +620,7 @@
 			}
 		} else {
 			while(!qcanread(lineq)) {
-				qread(kbdq, &kbd.line[kbd.x], 1);
+				readcons(kbdq, &kbd.line[kbd.x], 1);
 				ch = kbd.line[kbd.x];
 				eol = 0;
 				switch(ch){
@@ -1163,10 +1103,9 @@
 	va_start(arg, fmt);
 	n = vseprint(buf, buf+sizeof(buf), fmt, arg) - buf;
 	va_end(arg);
-	if(screenputs != 0 && iprintscreenputs)
-		screenputs(buf, n);
-#undef write
 	write(2, buf, n);
+	if(screenputs != 0)
+		screenputs(buf, n);
 	splx(s);
 
 	return n;
--- a/kern/uart.c
+++ /dev/null
@@ -1,15 +1,0 @@
-#include "u.h"
-#include "lib.h"
-#include "dat.h"
-#include "fns.h"
-#include "error.h"
-
-extern int panicking;
-void
-uartputs(char *s, int n)
-{
-	if(panicking)
-		write(1, s, n); 
-}
-
-
--- a/main.c
+++ b/main.c
@@ -40,7 +40,6 @@
 	osinit();
 	procinit0();
 	printinit();
-	screeninit();
 
 	chandevreset();
 	chandevinit();
@@ -48,10 +47,6 @@
 
 	if(bind("#c", "/dev", MBEFORE) < 0)
 		panic("bind #c: %r");
-	if(bind("#m", "/dev", MBEFORE) < 0)
-		panic("bind #m: %r");
-	if(bind("#i", "/dev", MBEFORE) < 0)
-		panic("bind #i: %r");
 	if(bind("#I", "/net", MBEFORE) < 0)
 		panic("bind #I: %r");
 	if(bind("#U", "/root", MREPL) < 0)