809 lines · 12.4 KB · C
RawDownload
#include <u.h>#include <libc.h>#include <draw.h>#include <thread.h>#include <cursor.h>#include <mouse.h>#include <keyboard.h>#include <frame.h>#include <plumb.h>#include "flayer.h"#include "samterm.h" #define	HSIZE	3	/* Type + short count */Header	h;uchar	indata[DATASIZE+1];	/* room for NUL */uchar	outdata[DATASIZE];short	outcount;int	hversion;int	exiting; void	inmesg(Hmesg, int);int	inshort(int);long	inlong(int);vlong	invlong(int);void	hsetdot(int, long, long);void	hmoveto(int, long);void	hsetsnarf(int);void	hplumb(int);void	clrlock(void);int	snarfswap(char*, int, char**); voidrcv(void){	int c;	static int state = 0;	static int count = 0;	static int i = 0;	static int errs = 0; 	while((c=rcvchar()) != -1)		switch(state){		case 0:			h.type = c;			state++;			break; 		case 1:			h.count0 = c;			state++;			break; 		case 2:			h.count1 = c;			count = h.count0|(h.count1<<8);			i = 0;			if(count > DATASIZE){				if(++errs < 5){					dumperrmsg(count, h.type, h.count0, c);					state = 0;					continue;				}				fprint(2, "type %d count %d\n", h.type, count);				panic("count>DATASIZE");			}			if(count == 0)				goto zerocount;			state++;			break; 		case 3:			indata[i++] = c;			if(i == count){		zerocount:				indata[i] = 0;				inmesg(h.type, count);				state = count = 0;				continue;			}			break;		}} Text *whichtext(int tg){	int i; 	for(i=0; i<nname; i++)		if(tag[i] == tg)			return text[i];	panic("whichtext");	return 0;} voidinmesg(Hmesg type, int count){	Text *t;	int i, m;	long l;	Flayer *lp; 	m = inshort(0);	l = inlong(2);	switch(type){	case -1:		panic("rcv error");	default:		fprint(2, "type %d\n", type);		panic("rcv unknown"); 	case Hversion:		hversion = m;		break; 	case Hbindname:		l = invlong(2);		/* for 64-bit pointers */		if((i=whichmenu(m)) < 0)			break;		/* in case of a race, a bindname may already have occurred */		if((t=whichtext(m)) == 0)			t=(Text *)l;		else	/* let the old one win; clean up the new one */			while(((Text *)l)->nwin>0)				closeup(&((Text *)l)->l[((Text *)l)->front]);		text[i] = t;		text[i]->tag = m;		break; 	case Hcurrent:		if(whichmenu(m)<0)			break;		t = whichtext(m);		i = which && ((Text *)which->user1)==&cmd && m!=cmd.tag;		if(t==0 && (t = sweeptext(0, m))==0)			break;		if(t->l[t->front].textfn==0)			panic("Hcurrent");		lp = &t->l[t->front];		if(i){			flupfront(lp);			flborder(lp, 0);			work = lp;		}else			current(lp);		break; 	case Hmovname:		if((m=whichmenu(m)) < 0)			break;		t = text[m];		l = tag[m];		i = name[m][0];		text[m] = 0;	/* suppress panic in menudel */		menudel(m);		if(t == &cmd)			m = 0;		else{			if (nname>0 && text[0]==&cmd)				m = 1;			else m = 0;			for(; m<nname; m++)				if(strcmp((char*)indata+2, (char*)name[m]+1)<0)					break;		}		menuins(m, indata+2, t, i, (int)l);		break; 	case Hgrow:		if(whichmenu(m) >= 0)			hgrow(m, l, inlong(6), 1);		break; 	case Hnewname:		menuins(0, (uchar *)"", (Text *)0, ' ', m);		break; 	case Hcheck0:		i = whichmenu(m);		if(i>=0) {			t = text[i];			if(t)				t->lock++;			outTs(Tcheck, m);		}		break; 	case Hcheck:		i = whichmenu(m);		if(i>=0) {			t = text[i];			if(t && t->lock)				t->lock--;			hcheck(m);		}		break; 	case Hunlock:		clrlock();		break; 	case Hdata:		if(whichmenu(m) >= 0)			l += hdata(m, l, indata+6, count-6);	Checkscroll:		if(m == cmd.tag){			for(i=0; i<NL; i++){				lp = &cmd.l[i];				if(lp->textfn)					center(lp, l>=0? l : lp->p1);			}		}		break; 	case Horigin:		if(whichmenu(m) >= 0)			horigin(m, l);		break; 	case Hunlockfile:		if(whichmenu(m)>=0 && (t = whichtext(m))->lock){			--t->lock;			l = -1;			goto Checkscroll;		}		break; 	case Hsetdot:		if(whichmenu(m) >= 0)			hsetdot(m, l, inlong(6));		break; 	case Hgrowdata:		if(whichmenu(m)<0)			break;		hgrow(m, l, inlong(6), 0);		whichtext(m)->lock++;	/* fake the request */		l += hdata(m, l, indata+10, count-10);		goto Checkscroll; 	case Hmoveto:		if(whichmenu(m)>=0)			hmoveto(m, l);		break; 	case Hclean:		if((m = whichmenu(m)) >= 0)			name[m][0] = ' ';		break; 	case Hdirty:		if((m = whichmenu(m))>=0)			name[m][0] = '\'';		break; 	case Hdelname:		if((m=whichmenu(m)) >= 0)			menudel(m);		break; 	case Hcut:		if(whichmenu(m) >= 0)			hcut(m, l, inlong(6));		break; 	case Hclose:		if(whichmenu(m)<0 || (t = whichtext(m))==0)			break;		l = t->nwin;		for(i = 0,lp = t->l; l>0 && i<NL; i++,lp++)			if(lp->textfn){				closeup(lp);				--l;			}		break; 	case Hsetpat:		setpat((char *)indata);		break; 	case Hsetsnarf:		hsetsnarf(m);		break; 	case Hsnarflen:		snarflen = inlong(0);		break; 	case Hack:		outT0(Tack);		break; 	case Hexit:		exiting = 1;		outT0(Texit);		threadexitsall(nil);		break; 	case Hplumb:		hplumb(m);		break;	}} voidsetlock(void){	hostlock++;	setcursor(mousectl, cursor = &lockarrow);} voidclrlock(void){	hasunlocked = 1;	if(hostlock > 0)		hostlock--;	if(hostlock == 0)		setcursor(mousectl, cursor=(Cursor *)0);} voidstartfile(Text *t){	outTsv(Tstartfile, t->tag, (vlong)t);	/* for 64-bit pointers */	setlock();} voidstartnewfile(int type, Text *t){	t->tag = Untagged;	outTv(type, (vlong)t);			/* for 64-bit pointers */} intinshort(int n){	return indata[n]|(indata[n+1]<<8);} longinlong(int n){	return indata[n]|(indata[n+1]<<8)|		((long)indata[n+2]<<16)|((long)indata[n+3]<<24);} vlonginvlong(int n){	vlong v; 	v = (indata[n+7]<<24) | (indata[n+6]<<16) | (indata[n+5]<<8) | indata[n+4];	v = (v<<16) | (indata[n+3]<<8) | indata[n+2];	v = (v<<16) | (indata[n+1]<<8) | indata[n];	return v;} voidoutT0(Tmesg type){	outstart(type);	outsend();} voidoutTl(Tmesg type, long l){	outstart(type);	outlong(l);	outsend();} voidoutTs(Tmesg type, int s){	outstart(type);	outshort(s);	outsend();} voidoutTss(Tmesg type, int s1, int s2){	outstart(type);	outshort(s1);	outshort(s2);	outsend();} voidoutTsll(Tmesg type, int s1, long l1, long l2){	outstart(type);	outshort(s1);	outlong(l1);	outlong(l2);	outsend();} voidoutTsl(Tmesg type, int s1, long l1){	outstart(type);	outshort(s1);	outlong(l1);	outsend();} voidoutTsv(Tmesg type, int s1, vlong v1){	outstart(type);	outshort(s1);	outvlong(v1);	outsend();} voidoutTv(Tmesg type, vlong v1){	outstart(type);	outvlong(v1);	outsend();} voidoutTslS(Tmesg type, int s1, long l1, Rune *s){	char buf[DATASIZE*3+1];	char *c; 	outstart(type);	outshort(s1);	outlong(l1);	c = buf;	while(*s)		c += runetochar(c, s++);	*c++ = 0;	outcopy(c-buf, (uchar *)buf);	outsend();} voidoutTsls(Tmesg type, int s1, long l1, int s2){	outstart(type);	outshort(s1);	outlong(l1);	outshort(s2);	outsend();} voidoutstart(Tmesg type){	outdata[0] = type;	outcount = 0;} voidoutcopy(int count, uchar *data){	while(count--)		outdata[HSIZE+outcount++] = *data++;	} voidoutshort(int s){	uchar buf[2]; 	buf[0]=s;	buf[1]=s>>8;	outcopy(2, buf);} voidoutlong(long l){	uchar buf[4]; 	buf[0]=l;	buf[1]=l>>8;	buf[2]=l>>16;	buf[3]=l>>24;	outcopy(4, buf);} voidoutvlong(vlong v){	int i;	uchar buf[8]; 	for(i = 0; i < sizeof(buf); i++){		buf[i] = v;		v >>= 8;

Lines 1–500 of 809.