/* See LICENSE for licence details. */
#include "st.h"
/* Globals */
DC dc;
XWindow xw;
Term term;
Escseq escseq;
int cmdfd;
pid_t pid;
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);
/* usleep(30000); */
draw(SCredraw);
}
void
sigchld(int a) {
int stat = 0;
if(waitpid(pid, &stat, 0) < 0)
die("Waiting for pid %hd failed: %s\n", pid, SERRNO);
if(WIFEXITED(stat))
exit(WEXITSTATUS(stat));
else
exit(EXIT_FAILURE);
}
void
ttynew(void) {
int m, s;
char *pts;
if((m = posix_openpt(O_RDWR | O_NOCTTY)) < 0)
die("openpt failed: %s\n", SERRNO);
if(grantpt(m) < 0)
die("grandpt failed: %s\n", SERRNO);
if(unlockpt(m) < 0)
die("unlockpt failed: %s\n", SERRNO);
if(!(pts = ptsname(m)))
die("ptsname failed: %s\n", SERRNO);
if((s = open(pts, O_RDWR | O_NOCTTY)) < 0)
die("Couldn't open slave: %s\n", SERRNO);
fcntl(s, F_SETFL, O_NDELAY);
switch(pid = fork()) {
case -1:
die("fork failed\n");
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("ioctl TTIOCSTTY failed: %s\n", SERRNO);
execsh();
break;
default:
close(s);
cmdfd = m;
signal(SIGCHLD, sigchld);
}
}
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:
die("Couldn't read from shell: %s\n", SERRNO);
break;
default:
tputs(buf, ret);
}
}
void
ttywrite(char *s, size_t n) {
if(write(cmdfd, s, n) == -1)
die("write error on tty: %s\n", SERRNO);
}
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: %s\n", SERRNO);
}
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
|