shithub: misc

ref: 2fa1f898d154e57225ba8a95c3c682de7dc3553f
dir: /9legacy/sam_label_command_riosnarf.diff/

View raw version
--- sys/src/cmd/samterm/main.c	Sat Jun 26 21:12:44 2021
+++ /sys/src/cmd/samterm/main.c	Fri Jul 23 21:49:09 2021
@@ -1,5 +1,6 @@
#include <u.h>
#include <libc.h>
+#include <bio.h>
#include <draw.h>
#include <thread.h>
#include <cursor.h>
@@ -24,6 +25,7 @@
long	modified = 0;		/* strange lookahead for menus */
char	hostlock = 1;
char	hasunlocked = 0;
+char	riosnarf = 0;
int	maxtab = 8;
int	autoindent;

@@ -35,6 +37,8 @@
	Rectangle r;
	Flayer *nwhich;

+	if(getenv("samriosnarf"))
+		riosnarf = 1;
	getscreen(argc, argv);
	iconinit();
	initio();
@@ -250,10 +254,27 @@
snarf(Text *t, int w)
{
	Flayer *l = &t->l[w];
+	int fd;
+	int n;
+	long p;

	if(l->p1>l->p0){
		snarflen = l->p1-l->p0;
-		outTsll(Tsnarf, t->tag, l->p0, l->p1);
+		if(riosnarf){
+			if((fd=open("/dev/snarf", OWRITE)) < 0){
+				fprint(2, "samterm:snarf: can't open /dev/snarf for writing");
+				return;
+			}
+			t = l->user1;
+			for(p=l->p0; p<l->p1; p+=n){
+				n = l->p1-p>BLOCKSIZE? BLOCKSIZE : l->p1-p;
+				rload(&t->rasp, p, p+n, nil);
+				if(fprint(fd, "%.*S", n, scratch) < 0)
+					break;
+			}
+			close(fd);
+		}else
+			outTsll(Tsnarf, t->tag, l->p0, l->p1);
	}
}

@@ -280,14 +301,75 @@
		hcheck(t->tag);
}

+#define HSIZE 3
+#define MAXCSTR (DATASIZE-HSIZE-sizeof(int)-2*sizeof(long)) /* Max cstring allowed by outTsllS */
void
paste(Text *t, int w)
{
-	if(snarflen){
-		cut(t, w, 0, 0);
-		t->lock++;
-		outTsl(Tpaste, t->tag, t->l[w].p0);
+	Rune *rstr, *rp;
+	Biobuf *bp;
+	int rstrsize;
+	int len; /* size in bytes of current rstr converted to UTF */
+	int m, n; /* n: number of runes already on rstr */
+	long offset, last;
+
+	if(!riosnarf){
+		if(snarflen){
+			cut(t, w, 0, 0);
+			t->lock++;
+			outTsllS(Tpaste, t->tag, t->l[w].p0, 0, L"");
+		}
+		return;
+	}
+	rstrsize = MAXCSTR;
+	if((bp=Bopen("/dev/snarf", OREAD)) == 0){
+		fprint(2, "samterm:paste: can't open /dev/snarf");
+		return;
+	}
+	offset = t->l[w].p0;
+	rp = rstr = alloc(rstrsize);
+	len = n = 0;
+	if((last=Bgetrune(bp)) <= 0)
+		return;
+	cut(t, w, 0, 0);
+	while((long)(*rp=last?last:Bgetrune(bp)) > 0){
+		last = 0;
+		if((len+=runelen(*rp)) > MAXCSTR){
+			Bungetrune(bp);
+			--n;
+			last = *(rp-1); /* can't Bungetrune again... */
+			*(rp-1) = 0;
+			if(t->lock == 0)
+				t->lock = 1;
+			if(Bgetrune(bp) <= 0){ /* Last chunk */
+				outTsllS(Tpaste, t->tag, t->l[w].p0, offset, rstr);
+				break;
+			}
+			Bungetrune(bp);
+			outTsllS(Tpaste, t->tag, -1, offset, rstr);
+			offset += n;
+			rp = rstr;
+			n = len = 0;
+			continue;
+		}
+		if(RUNESIZE*(rp-rstr+2) > rstrsize){
+			m = rp - rstr;
+			rstr = realloc(rstr, rstrsize+=MAXCSTR);
+				if(!rstr)
+					panic("realloc");
+			rp = rstr + m;
+		}
+		++rp;
+		++n;
+	}
+	*rp = 0;
+	if(*rstr){
+		if(t->lock == 0)
+			t->lock = 1;
+		outTsllS(Tpaste, t->tag, t->l[w].p0, offset, rstr);
	}
+	free(rstr);
+	Bterm(bp);
}

void
--- sys/src/cmd/samterm/mesg.c	Thu Mar 29 19:35:49 2012
+++ /sys/src/cmd/samterm/mesg.c	Fri Jul 23 19:23:28 2021
@@ -97,6 +97,7 @@
	int i, m;
	long l;
	Flayer *lp;
+	char *str, c;

	m = inshort(0);
	l = inlong(2);
@@ -107,6 +108,18 @@
		fprint(2, "type %d\n", type);
		panic("rcv unknown");

+	case Hlabel:
+		str = (char *)indata;
+		if((m = open("/dev/label", OWRITE)) < 0){
+			fprint(2, "samterm:label: can't open /dev/label for writing");
+			break;
+		}
+		while((c=*str)==' ' || c=='\t')
+			str++;
+		fprint(m, "%s", str);
+		close(m);
+		break;
+
	case Hversion:
		hversion = m;
		break;
@@ -432,6 +445,24 @@
	outstart(type);
	outshort(s1);
	outlong(l1);
+	c = buf;
+	while(*s)
+		c += runetochar(c, s++);
+	*c++ = 0;
+	outcopy(c-buf, (uchar *)buf);
+	outsend();
+}
+
+void
+outTsllS(Tmesg type, int s1, long l1, long l2, Rune *s)
+{
+	char buf[DATASIZE*3+1];
+	char *c;
+
+	outstart(type);
+	outshort(s1);
+	outlong(l1);
+	outlong(l2);
	c = buf;
	while(*s)
		c += runetochar(c, s++);
--- sys/src/cmd/samterm/samterm.h	Sat Jun 26 21:13:32 2021
+++ /sys/src/cmd/samterm/samterm.h	Fri Jul 23 11:59:07 2021
@@ -1,5 +1,6 @@
#define	SAMTERM

+#define	BLOCKSIZE	2048
#define	RUNESIZE	sizeof(Rune)
#define	MAXFILES	256
#define	READBUFSIZE (16*1024)
@@ -72,6 +73,7 @@
extern long	nscralloc;
extern char	hostlock;
extern char	hasunlocked;
+extern char	riosnarf;
extern long	snarflen;
extern Mousectl* mousectl;
extern Keyboardctl* keyboardctl;
@@ -167,6 +169,7 @@
void	outT0(Tmesg);
void	outTl(Tmesg, long);
void	outTslS(Tmesg, int, long, Rune*);
+void	outTsllS(Tmesg, int, long, long, Rune*);
void	outTsll(Tmesg, int, long, long);
void	outTsl(Tmesg, int, long);
void	outTsv(Tmesg, int, vlong);
--- sys/src/cmd/sam/cmd.c	Wed Apr 24 00:06:05 2013
+++ /sys/src/cmd/sam/cmd.c	Sun Jul 18 18:23:06 2021
@@ -17,6 +17,7 @@
	'g',	0,	1,	0,	'p',	aDot,	0,	0,	g_cmd,
	'i',	1,	0,	0,	0,	aDot,	0,	0,	i_cmd,
	'k',	0,	0,	0,	0,	aDot,	0,	0,	k_cmd,
+	'l',	0,	0,	0,	0,	aNo,	0,	linex,	l_cmd,
	'm',	0,	0,	1,	0,	aDot,	0,	0,	m_cmd,
	'n',	0,	0,	0,	0,	aNo,	0,	0,	n_cmd,
	'p',	0,	0,	0,	0,	aDot,	0,	0,	p_cmd,
--- sys/src/cmd/sam/disk.c	Thu Feb 28 21:08:58 2002
+++ /sys/src/cmd/sam/disk.c	Fri Jul 23 19:34:38 2021
@@ -48,7 +48,7 @@
	/* last bucket holds blocks of exactly Maxblock */
	if(ip)
		*ip = size/Blockincr;
-	return size * sizeof(Rune);
+	return size * RUNESIZE;
}

Block*
@@ -101,7 +101,7 @@
		b = disknewblock(d, n);
		*bp = b;
	}
-	if(pwrite(d->fd, r, n*sizeof(Rune), b->addr) != n*sizeof(Rune))
+	if(pwrite(d->fd, r, n*RUNESIZE, b->addr) != n*RUNESIZE)
		panic("write error to temp file");
	b->n = n;
}
@@ -113,6 +113,6 @@
		panic("internal error: diskread");

	ntosize(b->n, nil);	/* called only for sanity check on Maxblock */
-	if(pread(d->fd, r, n*sizeof(Rune), b->addr) != n*sizeof(Rune))
+	if(pread(d->fd, r, n*RUNESIZE, b->addr) != n*RUNESIZE)
		panic("read error from temp file");
}
--- sys/src/cmd/sam/file.c	Fri Jul 30 18:29:06 2010
+++ /sys/src/cmd/sam/file.c	Fri Jul 23 19:35:49 2021
@@ -35,7 +35,7 @@
enum
{
	Maxmerge = 50,
-	Undosize = sizeof(Undo)/sizeof(Rune),
+	Undosize = sizeof(Undo)/RUNESIZE,
};

static Merge	merge;
--- sys/src/cmd/sam/mesg.c	Fri Jan 12 17:02:55 2007
+++ /sys/src/cmd/sam/mesg.c	Fri Jul 23 19:50:39 2021
@@ -1,4 +1,5 @@
#include "sam.h"
+#include <bio.h>

Header	h;
uchar	indata[DATASIZE];
@@ -347,7 +348,28 @@
	case Tpaste:
		f = whichfile(inshort());
		p0 = inlong();
+		p = inlong();
		journaln(0, p0);
+
+		if(*(char*)inp != 0){
+			str = tmpcstr((char*)inp);
+			m = str->n;
+			loginsert(f, p, str->s, m);
+			freetmpstr(str);
+			if(fileupdate(f, FALSE, TRUE))
+				seq++;
+			/* there's another chunk? */
+			if(p0 < 0){
+				rcv();
+				break;
+			}
+			f->dot.r.p1 = p0;
+			f->dot.r.p2 = p+m;
+			f->tdot.p1 = -1; /* force telldot to tell (arguably a BUG) */
+			telldot(f);
+			outTs(Hunlockfile, f->tag);
+			break;
+		}
		for(l=0; l<snarfbuf.nc; l+=m){
			m = snarfbuf.nc-l;
			if(m>BLOCKSIZE)
@@ -359,7 +381,7 @@
			seq++;
		f->dot.r.p1 = p0;
		f->dot.r.p2 = p0+snarfbuf.nc;
-		f->tdot.p1 = -1; /* force telldot to tell (arguably a BUG) */
+		f->tdot.p1 = -1;
		telldot(f);
		outTs(Hunlockfile, f->tag);
		break;
@@ -477,7 +499,7 @@
			m = SNARFSIZE;
			dprint("?warning: snarf buffer truncated\n");
		}
-		rp = malloc(m*sizeof(Rune));
+		rp = malloc(m*RUNESIZE);
		if(rp){
			bufread(&snarfbuf, 0, rp, m);
			c = Strtoc(tmprstr(rp, m));
--- sys/src/cmd/sam/mesg.h	Fri Mar 18 22:33:33 2005
+++ /sys/src/cmd/sam/mesg.h	Wed Jul 21 22:46:19 2021
@@ -67,6 +67,7 @@
	Hack,		/* request acknowledgement */
	Hexit,
	Hplumb,		/* return plumb message to terminal - version 1 */
+	Hlabel,	/* set /dev/label on terminal */
	HMAX,
}Hmesg;
typedef struct Header{
--- sys/src/cmd/sam/parse.h	Thu Oct 27 15:36:34 2005
+++ /sys/src/cmd/sam/parse.h	Sat Jul 17 19:55:51 2021
@@ -55,13 +55,12 @@

int	nl_cmd(File*, Cmd*), a_cmd(File*, Cmd*), b_cmd(File*, Cmd*);
int	c_cmd(File*, Cmd*), cd_cmd(File*, Cmd*), d_cmd(File*, Cmd*);
-int	D_cmd(File*, Cmd*), e_cmd(File*, Cmd*);
-int	f_cmd(File*, Cmd*), g_cmd(File*, Cmd*), i_cmd(File*, Cmd*);
-int	k_cmd(File*, Cmd*), m_cmd(File*, Cmd*), n_cmd(File*, Cmd*);
-int	p_cmd(File*, Cmd*), q_cmd(File*, Cmd*);
-int	s_cmd(File*, Cmd*), u_cmd(File*, Cmd*), w_cmd(File*, Cmd*);
-int	x_cmd(File*, Cmd*), X_cmd(File*, Cmd*), plan9_cmd(File*, Cmd*);
-int	eq_cmd(File*, Cmd*);
+int	D_cmd(File*, Cmd*), e_cmd(File*, Cmd*), f_cmd(File*, Cmd*);
+int	g_cmd(File*, Cmd*), i_cmd(File*, Cmd*), k_cmd(File*, Cmd*);
+int	l_cmd(File*, Cmd*), m_cmd(File*, Cmd*), n_cmd(File*, Cmd*);
+int	p_cmd(File*, Cmd*), q_cmd(File*, Cmd*), s_cmd(File*, Cmd*);
+int	u_cmd(File*, Cmd*), w_cmd(File*, Cmd*), x_cmd(File*, Cmd*);
+int	X_cmd(File*, Cmd*), plan9_cmd(File*, Cmd*), eq_cmd(File*, Cmd*);


String	*getregexp(int);
--- sys/src/cmd/sam/sam.c	Tue Dec  6 17:05:45 2005
+++ /sys/src/cmd/sam/sam.c	Tue Jul 20 12:23:28 2021
@@ -25,6 +25,7 @@
int	termlocked;
char	*samterm = SAMTERM;
char	*rsamname = RSAM;
+char	*samtermlabel = nil;
File	*lastfile;
Disk	*disk;
long	seq;
@@ -57,6 +58,9 @@
	case 's':
		rsamname = EARGF(usage());
		break;
+	case 'l':
+		samtermlabel = EARGF(usage());
+		break;
	default:
		dprint("sam: unknown flag %c\n", ARGC());
		usage();
@@ -82,6 +86,11 @@
		startup(machine, Rflag, termargs, argv);
	notify(notifyf);
	getcurwd();
+	if(samtermlabel){
+		t=tmpcstr(samtermlabel);
+		outTS(Hlabel, t);
+		freetmpstr(t);
+	}
	if(argc>0){
		for(i=0; i<argc; i++){
			if(!setjmp(mainloop)){
@@ -107,7 +116,7 @@
void
usage(void)
{
-	dprint("usage: sam [-d] [-t samterm] [-s sam name] -r machine\n");
+	dprint("usage: sam [-d] [-t samterm] [-s sam name] -r machine -l label\n");
	exits("usage");
}

--- sys/src/cmd/sam/sam.h	Sat Jun 26 21:13:32 2021
+++ /sys/src/cmd/sam/sam.h	Fri Jul 23 19:38:48 2021
@@ -90,7 +90,7 @@
	Maxblock = 	16*1024,

	BUFSIZE = Maxblock,	/* size from fbufalloc() */
-	RBUFSIZE = BUFSIZE/sizeof(Rune),
+	RBUFSIZE = BUFSIZE/RUNESIZE,
};


@@ -220,9 +220,9 @@
uint	min(uint, uint);
void	cvttorunes(char*, int, Rune*, int*, int*, int*);

-#define	runemalloc(a)		(Rune*)emalloc((a)*sizeof(Rune))
-#define	runerealloc(a, b)	(Rune*)realloc((a), (b)*sizeof(Rune))
-#define	runemove(a, b, c)	memmove((a), (b), (c)*sizeof(Rune))
+#define	runemalloc(a)		(Rune*)emalloc((a)*RUNESIZE)
+#define	runerealloc(a, b)	(Rune*)realloc((a), (b)*RUNESIZE)
+#define	runemove(a, b, c)	memmove((a), (b), (c)*RUNESIZE)

int	alnum(int);
int	Read(int, void*, int);
--- sys/src/cmd/sam/xec.c	Tue Jun  6 02:55:54 2000
+++ /sys/src/cmd/sam/xec.c	Tue Jul 20 12:34:09 2021
@@ -26,7 +26,7 @@
	if(f && f->unread)
		load(f);
	if(f==0 && (cp->addr==0 || cp->addr->type!='"') &&
-	    !utfrune("bBnqUXY!", cp->cmdc) &&
+	    !utfrune("bBnqUXY!l", cp->cmdc) &&
	    cp->cmdc!=('c'|0x100) && !(cp->cmdc=='D' && cp->ctext))
		error(Enofile);
	i = lookup(cp->cmdc);
@@ -150,6 +150,14 @@
{
	USED(cp);
	f->mark = addr.r;
+	return TRUE;
+}
+
+int
+l_cmd(File *f, Cmd *cp)
+{
+	USED(f);
+	outTS(Hlabel, cp->ctext);
	return TRUE;
}