/* See LICENSE for licence details. */
#include "st.h"
/* Globals */
DC dc;
XWindow xw;
Term term;
Escseq escseq;
int cmdfd;
int running;
void
die(const char *errstr, ...) {
va_list ap;
va_start(ap, errstr);
vfprintf(stderr, errstr, ap);
va_end(ap);
exit(EXIT_FAILURE);
}
void
execsh(void) {
char *args[3] = {SHELL, "-i", NULL};
putenv("TERM=" TNAME);
execvp(SHELL, args);
}
void
xbell(void) { /* visual bell */
XRectangle r = { 0, 0, xw.w, xw.h };
XSetForeground(xw.dis, dc.gc, dc.col[BellCol]);
XFillRectangles(xw.dis, xw.win, dc.gc, &r, 1);
XFlush(xw.dis);
usleep(30000);
draw(SCredraw);
}
void
ttynew(void) {
int m, s;
pid_t pid;
char *pts;
if((m = posix_openpt(O_RDWR | O_NOCTTY)) < 0)
die("openpt");
if(grantpt(m) == -1)
die("grandpt");
if(unlockpt(m) == -1)
die("unlockpt");
if((pts = ptsname(m)) == NULL)
die("ptsname");
if((s = open(pts, O_RDWR | O_NOCTTY)) < 0)
die("slave open");
fcntl(s, F_SETFL, O_NDELAY);
switch(pid = fork()) {
case -1:
die("fork");
break;
case 0:
setsid(); /* create a new process group */
dup2(s, STDIN_FILENO);
dup2(s, STDOUT_FILENO);
dup2(s, STDERR_FILENO);
if(ioctl(s, TIOCSCTTY, NULL) < 0)
die("slave TTIOCSTTY");
execsh();
break;
default:
close(s);
cmdfd = m;
}
}
void
dump(char c) {
static int col;
fprintf(stderr, " %02x %c ", c, isprint(c)?c:'.');
if(++col % 10 == 0)
fprintf(stderr, "\n");
}
void
ttyread(void) {
char buf[BUFSIZ] = {0};
int ret;
switch(ret = read(cmdfd, buf, BUFSIZ)) {
case -1: /* error or exit */
/* XXX: be more precise */
running = 0;
break;
default:
tputs(buf, ret);
}
}
void
ttywrite(char *s, size_t n) {
if(write(cmdfd, s, n) == -1)
die("write error on tty.");
}
void
ttyresize(int x, int y) {
struct winsize w;
w.ws_row = term.row;
w.ws_col = term.col;
w.ws_xpixel = w.ws_ypixel = 0;
if(ioctl(cmdfd, TIOCSWINSZ, &w) < 0)
fprintf(stderr, "Couldn't set window size: %m\n");
}
int
escfinal(char c) {
if(escseq.len == 1)
switch(c) {
case '[':
case ']':
case '(':
return 0;
case '=':
case '>':
default:
return 1;
}
else if(BETWEEN(c, 0x40, 0x7E))
return 1;
return 0;
}
void
tcpos(int mode) {
static int x = 0;
static int y = 0;
if(mode == CSsave)
x = term.c.x, y = term.c.y;
else if(mode == CSload)
tmoveto(x, y);
}
void
tnew(int col, int row) { /* screen size */
term.row = row, term.col = col;
term.top = 0, term.bot = term.row - 1;
/* mode */
term.mode = TMwrap;
/* cursor */
term.c.attr.mode = ATnone;
term.c.attr.fg = DefaultFG;
term.c.attr.bg = DefaultBG;
term.c.x = term.c.y = 0;
term.c.hidden = 0;
/* allocate screen */
term.line = calloc(term.row, sizeof(Line));
for(row = 0 ; row < term.row; row++)
term.line[row] = calloc(term.col, sizeof(Glyph));
}
void
tscroll(void) {
Line temp = term.line[term.top];
int i;
for(i = term.top; i < term.bot; i++)
term.line[i] = term.line[i+1];
memset(temp, 0, sizeof(Glyph) * term.col);
term.line[term.bot] = temp;
xscroll();
}
void
tnewline(void) {
int y = term.c.y + 1;
if(y > term.bot) {
tscroll(), y = term.bot;
}
tmoveto(0, y);
}
int
escaddc(char c) {
escseq.buf[escseq.len++] = c;
if(escfinal(c) || escseq.len >= ESCSIZ) {
escparse(), eschandle();
return 0;
}
return 1;
}
void
escparse(void) {
/* int noarg = 1; */
char *p = escseq.buf;
escseq.narg = 0;
switch(escseq.pre = *p++) {
case '[': /* CSI */
if(*p == '?')
escseq.priv = 1, p++;
while(p < escseq.buf+escseq.len) {
while(isdigit(*p)) {
escseq.arg[escseq.narg] *= 10;
escseq.arg[escseq.narg] += *(p++) - '0'/*, noarg = 0 */;
}
if(*p == ';')
escseq.narg++, p++;
else {
escseq.mode = *p;
escseq.narg++;
return;
}
}
break;
case '(':
/* XXX: graphic character set */
break;
}
}
|