2004-03-26 01:59:35 +00:00
|
|
|
#include <u.h>
|
2003-10-11 02:45:02 +00:00
|
|
|
#include <sys/types.h>
|
|
|
|
|
#include <sys/stat.h>
|
|
|
|
|
#include <errno.h>
|
|
|
|
|
|
2003-09-30 17:47:44 +00:00
|
|
|
#include <u.h>
|
|
|
|
|
#include <libc.h>
|
|
|
|
|
#include <draw.h>
|
|
|
|
|
#include <thread.h>
|
|
|
|
|
#include <mouse.h>
|
|
|
|
|
#include <cursor.h>
|
|
|
|
|
#include <keyboard.h>
|
|
|
|
|
#include <frame.h>
|
2004-03-21 04:33:13 +00:00
|
|
|
#define Tversion Tversion9p
|
|
|
|
|
#define Twrite Twrite9p
|
|
|
|
|
#include <fcall.h>
|
|
|
|
|
#undef Tversion
|
|
|
|
|
#undef Twrite
|
2005-01-04 22:17:47 +00:00
|
|
|
#include <9pclient.h>
|
2004-03-02 23:11:58 +00:00
|
|
|
#include <plumb.h>
|
2003-09-30 17:47:44 +00:00
|
|
|
#include "flayer.h"
|
|
|
|
|
#include "samterm.h"
|
|
|
|
|
|
2003-10-11 02:45:02 +00:00
|
|
|
static char *exname;
|
2003-09-30 17:47:44 +00:00
|
|
|
|
2003-11-25 02:11:11 +00:00
|
|
|
#define STACK 16384
|
2003-11-24 22:39:06 +00:00
|
|
|
|
2005-01-27 06:10:35 +00:00
|
|
|
void
|
|
|
|
|
usage(void)
|
|
|
|
|
{
|
|
|
|
|
fprint(2, "usage: samterm -a -W winsize\n");
|
|
|
|
|
threadexitsall("usage");
|
|
|
|
|
}
|
|
|
|
|
|
2003-09-30 17:47:44 +00:00
|
|
|
void
|
|
|
|
|
getscreen(int argc, char **argv)
|
|
|
|
|
{
|
|
|
|
|
char *t;
|
|
|
|
|
|
2005-01-27 06:10:35 +00:00
|
|
|
ARGBEGIN{
|
|
|
|
|
case 'a':
|
|
|
|
|
autoindent = 1;
|
|
|
|
|
break;
|
|
|
|
|
case 'W':
|
|
|
|
|
winsize = EARGF(usage());
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
usage();
|
|
|
|
|
}ARGEND
|
2004-04-29 17:13:24 +00:00
|
|
|
|
2003-09-30 17:47:44 +00:00
|
|
|
if(initdraw(panic1, nil, "sam") < 0){
|
2003-10-11 02:45:02 +00:00
|
|
|
fprint(2, "samterm: initdraw: %r\n");
|
2003-09-30 17:47:44 +00:00
|
|
|
threadexitsall("init");
|
|
|
|
|
}
|
|
|
|
|
t = getenv("tabstop");
|
|
|
|
|
if(t != nil)
|
|
|
|
|
maxtab = strtoul(t, nil, 0);
|
|
|
|
|
draw(screen, screen->clipr, display->white, nil, ZP);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int
|
|
|
|
|
screensize(int *w, int *h)
|
|
|
|
|
{
|
|
|
|
|
int fd, n;
|
|
|
|
|
char buf[5*12+1];
|
|
|
|
|
|
|
|
|
|
fd = open("/dev/screen", OREAD);
|
|
|
|
|
if(fd < 0)
|
|
|
|
|
return 0;
|
|
|
|
|
n = read(fd, buf, sizeof(buf)-1);
|
|
|
|
|
close(fd);
|
|
|
|
|
if (n != sizeof(buf)-1)
|
|
|
|
|
return 0;
|
|
|
|
|
buf[n] = 0;
|
|
|
|
|
if (h) {
|
|
|
|
|
*h = atoi(buf+4*12)-atoi(buf+2*12);
|
|
|
|
|
if (*h < 0)
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
if (w) {
|
|
|
|
|
*w = atoi(buf+3*12)-atoi(buf+1*12);
|
|
|
|
|
if (*w < 0)
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int
|
|
|
|
|
snarfswap(char *fromsam, int nc, char **tosam)
|
|
|
|
|
{
|
2003-10-11 02:45:02 +00:00
|
|
|
char *s;
|
2003-09-30 17:47:44 +00:00
|
|
|
|
2003-10-11 02:45:02 +00:00
|
|
|
s = getsnarf();
|
|
|
|
|
putsnarf(fromsam);
|
|
|
|
|
*tosam = s;
|
|
|
|
|
return s ? strlen(s) : 0;
|
2003-09-30 17:47:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
dumperrmsg(int count, int type, int count0, int c)
|
|
|
|
|
{
|
|
|
|
|
fprint(2, "samterm: host mesg: count %d %ux %ux %ux %s...ignored\n",
|
|
|
|
|
count, type, count0, c, rcvstring());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
removeextern(void)
|
|
|
|
|
{
|
|
|
|
|
remove(exname);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Readbuf hostbuf[2];
|
|
|
|
|
Readbuf plumbbuf[2];
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
extproc(void *argv)
|
|
|
|
|
{
|
|
|
|
|
Channel *c;
|
2003-10-11 02:45:02 +00:00
|
|
|
int i, n, which, fd;
|
2003-09-30 17:47:44 +00:00
|
|
|
void **arg;
|
|
|
|
|
|
|
|
|
|
arg = argv;
|
|
|
|
|
c = arg[0];
|
2006-04-20 20:35:35 +00:00
|
|
|
fd = (int)(uintptr)arg[1];
|
2003-09-30 17:47:44 +00:00
|
|
|
|
|
|
|
|
i = 0;
|
|
|
|
|
for(;;){
|
2003-10-11 02:45:02 +00:00
|
|
|
i = 1-i; /* toggle */
|
2004-12-27 00:32:50 +00:00
|
|
|
n = read(fd, plumbbuf[i].data, sizeof plumbbuf[i].data);
|
|
|
|
|
if(0) fprint(2, "ext %d\n", n);
|
2003-09-30 17:47:44 +00:00
|
|
|
if(n <= 0){
|
|
|
|
|
fprint(2, "samterm: extern read error: %r\n");
|
2003-10-11 02:45:02 +00:00
|
|
|
threadexits("extern"); /* not a fatal error */
|
2003-09-30 17:47:44 +00:00
|
|
|
}
|
|
|
|
|
plumbbuf[i].n = n;
|
|
|
|
|
which = i;
|
|
|
|
|
send(c, &which);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
extstart(void)
|
|
|
|
|
{
|
2003-10-11 02:45:02 +00:00
|
|
|
char *user, *disp;
|
|
|
|
|
int fd, flags;
|
2003-09-30 17:47:44 +00:00
|
|
|
static void *arg[2];
|
|
|
|
|
|
2003-10-11 02:45:02 +00:00
|
|
|
user = getenv("USER");
|
|
|
|
|
if(user == nil)
|
2003-09-30 17:47:44 +00:00
|
|
|
return;
|
2003-10-11 02:45:02 +00:00
|
|
|
disp = getenv("DISPLAY");
|
|
|
|
|
if(disp)
|
|
|
|
|
exname = smprint("/tmp/.sam.%s.%s", user, disp);
|
|
|
|
|
else
|
|
|
|
|
exname = smprint("/tmp/.sam.%s", user);
|
|
|
|
|
if(exname == nil){
|
|
|
|
|
fprint(2, "not posting for B: out of memory\n");
|
2003-09-30 17:47:44 +00:00
|
|
|
return;
|
|
|
|
|
}
|
2003-10-11 02:45:02 +00:00
|
|
|
|
|
|
|
|
if(mkfifo(exname, 0600) < 0){
|
|
|
|
|
struct stat st;
|
|
|
|
|
if(errno != EEXIST || stat(exname, &st) < 0)
|
|
|
|
|
return;
|
|
|
|
|
if(!S_ISFIFO(st.st_mode)){
|
|
|
|
|
removeextern();
|
|
|
|
|
if(mkfifo(exname, 0600) < 0)
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2005-01-07 18:03:36 +00:00
|
|
|
fd = open(exname, OREAD|ONONBLOCK);
|
2003-10-11 02:45:02 +00:00
|
|
|
if(fd == -1){
|
|
|
|
|
removeextern();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2003-09-30 17:47:44 +00:00
|
|
|
/*
|
2003-10-11 02:45:02 +00:00
|
|
|
* Turn off no-delay and provide ourselves as a lingering
|
|
|
|
|
* writer so as not to get end of file on read.
|
2003-09-30 17:47:44 +00:00
|
|
|
*/
|
2003-10-11 02:45:02 +00:00
|
|
|
flags = fcntl(fd, F_GETFL, 0);
|
|
|
|
|
if(flags<0 || fcntl(fd, F_SETFL, flags&~O_NONBLOCK)<0
|
|
|
|
|
||open(exname, OWRITE) < 0){
|
|
|
|
|
close(fd);
|
|
|
|
|
removeextern();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2003-09-30 17:47:44 +00:00
|
|
|
plumbc = chancreate(sizeof(int), 0);
|
2004-12-27 00:32:50 +00:00
|
|
|
chansetname(plumbc, "plumbc");
|
2003-09-30 17:47:44 +00:00
|
|
|
arg[0] = plumbc;
|
2006-04-20 20:35:35 +00:00
|
|
|
arg[1] = (void*)(uintptr)fd;
|
2004-12-27 00:32:50 +00:00
|
|
|
proccreate(extproc, arg, STACK);
|
2003-09-30 17:47:44 +00:00
|
|
|
atexit(removeextern);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int
|
2004-03-02 23:11:58 +00:00
|
|
|
plumbformat(Plumbmsg *m, int i)
|
2003-09-30 17:47:44 +00:00
|
|
|
{
|
|
|
|
|
char *addr, *data, *act;
|
|
|
|
|
int n;
|
|
|
|
|
|
|
|
|
|
data = (char*)plumbbuf[i].data;
|
|
|
|
|
n = m->ndata;
|
2004-03-02 23:11:58 +00:00
|
|
|
if(n == 0 || 2+n+2 >= READBUFSIZE){
|
2003-09-30 17:47:44 +00:00
|
|
|
plumbfree(m);
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
act = plumblookup(m->attr, "action");
|
|
|
|
|
if(act!=nil && strcmp(act, "showfile")!=0){
|
|
|
|
|
/* can't handle other cases yet */
|
|
|
|
|
plumbfree(m);
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
addr = plumblookup(m->attr, "addr");
|
|
|
|
|
if(addr){
|
|
|
|
|
if(addr[0] == '\0')
|
|
|
|
|
addr = nil;
|
|
|
|
|
else
|
|
|
|
|
addr = strdup(addr); /* copy to safe storage; we'll overwrite data */
|
|
|
|
|
}
|
|
|
|
|
memmove(data, "B ", 2); /* we know there's enough room for this */
|
|
|
|
|
memmove(data+2, m->data, n);
|
|
|
|
|
n += 2;
|
|
|
|
|
if(data[n-1] != '\n')
|
|
|
|
|
data[n++] = '\n';
|
|
|
|
|
if(addr != nil){
|
|
|
|
|
if(n+strlen(addr)+1+1 <= READBUFSIZE)
|
|
|
|
|
n += sprint(data+n, "%s\n", addr);
|
|
|
|
|
free(addr);
|
|
|
|
|
}
|
|
|
|
|
plumbbuf[i].n = n;
|
|
|
|
|
plumbfree(m);
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
2004-03-21 04:33:13 +00:00
|
|
|
plumbproc(void *arg)
|
2003-09-30 17:47:44 +00:00
|
|
|
{
|
2005-01-04 22:17:47 +00:00
|
|
|
CFid *fid;
|
2004-03-21 04:33:13 +00:00
|
|
|
int i;
|
2004-03-02 23:11:58 +00:00
|
|
|
Plumbmsg *m;
|
2003-09-30 17:47:44 +00:00
|
|
|
|
2004-03-21 04:33:13 +00:00
|
|
|
fid = arg;
|
2003-09-30 17:47:44 +00:00
|
|
|
i = 0;
|
|
|
|
|
for(;;){
|
2004-03-21 04:33:13 +00:00
|
|
|
m = plumbrecvfid(fid);
|
2004-03-02 23:11:58 +00:00
|
|
|
if(m == nil){
|
2003-09-30 17:47:44 +00:00
|
|
|
fprint(2, "samterm: plumb read error: %r\n");
|
|
|
|
|
threadexits("plumb"); /* not a fatal error */
|
|
|
|
|
}
|
2004-03-02 23:11:58 +00:00
|
|
|
if(plumbformat(m, i)){
|
2004-03-21 04:33:13 +00:00
|
|
|
send(plumbc, &i);
|
2004-03-02 23:11:58 +00:00
|
|
|
i = 1-i; /* toggle */
|
2003-09-30 17:47:44 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int
|
|
|
|
|
plumbstart(void)
|
|
|
|
|
{
|
2005-01-04 22:17:47 +00:00
|
|
|
CFid *fid;
|
2003-09-30 17:47:44 +00:00
|
|
|
|
|
|
|
|
plumbfd = plumbopen("send", OWRITE|OCEXEC); /* not open is ok */
|
2004-03-21 04:33:13 +00:00
|
|
|
fid = plumbopenfid("edit", OREAD|OCEXEC);
|
|
|
|
|
if(fid == nil)
|
2003-09-30 17:47:44 +00:00
|
|
|
return -1;
|
|
|
|
|
plumbc = chancreate(sizeof(int), 0);
|
2004-12-27 00:32:50 +00:00
|
|
|
chansetname(plumbc, "plumbc");
|
2003-09-30 17:47:44 +00:00
|
|
|
if(plumbc == nil){
|
2004-03-21 04:33:13 +00:00
|
|
|
fsclose(fid);
|
2003-09-30 17:47:44 +00:00
|
|
|
return -1;
|
|
|
|
|
}
|
2004-03-21 04:33:13 +00:00
|
|
|
threadcreate(plumbproc, fid, STACK);
|
2003-09-30 17:47:44 +00:00
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
hostproc(void *arg)
|
|
|
|
|
{
|
|
|
|
|
Channel *c;
|
|
|
|
|
int i, n, which;
|
|
|
|
|
|
|
|
|
|
c = arg;
|
|
|
|
|
|
|
|
|
|
i = 0;
|
|
|
|
|
for(;;){
|
|
|
|
|
i = 1-i; /* toggle */
|
2004-12-27 00:32:50 +00:00
|
|
|
n = read(hostfd[0], hostbuf[i].data, sizeof hostbuf[i].data);
|
|
|
|
|
if(0) fprint(2, "hostproc %d\n", n);
|
2003-09-30 17:47:44 +00:00
|
|
|
if(n <= 0){
|
2005-01-20 22:59:50 +00:00
|
|
|
if(n == 0){
|
|
|
|
|
if(exiting)
|
|
|
|
|
threadexits(nil);
|
|
|
|
|
werrstr("unexpected eof");
|
|
|
|
|
}
|
2003-09-30 17:47:44 +00:00
|
|
|
fprint(2, "samterm: host read error: %r\n");
|
|
|
|
|
threadexitsall("host");
|
|
|
|
|
}
|
|
|
|
|
hostbuf[i].n = n;
|
|
|
|
|
which = i;
|
2004-12-27 00:32:50 +00:00
|
|
|
if(0) fprint(2, "hostproc send %d\n", which);
|
2003-09-30 17:47:44 +00:00
|
|
|
send(c, &which);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
hoststart(void)
|
|
|
|
|
{
|
|
|
|
|
hostc = chancreate(sizeof(int), 0);
|
2004-12-27 00:32:50 +00:00
|
|
|
chansetname(hostc, "hostc");
|
|
|
|
|
proccreate(hostproc, hostc, STACK);
|
2003-09-30 17:47:44 +00:00
|
|
|
}
|