+}
+
+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(const 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);
+}
+
+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;
+ /* X stuff _before_ the line swapping (results in wrong line index) */
+ xscroll();
+ 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;
+}
+
+void
+tnewline(void) {
+ int y = term.c.y + 1;
+ if(y > term.bot)
+ tscroll(), y = term.bot;
+ tmoveto(0, y);
+}
+
+void
+csiparse(void) {
+ /* int noarg = 1; */
+ char *p = escseq.buf;
+
+ escseq.narg = 0;
+ 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+1 < ESCARGSIZ)
+ escseq.narg++, p++;
+ else {
+ escseq.mode = *p;
+ escseq.narg++;
+ return;
+ }
+ }
+}
+
+void
+tmoveto(int x, int y) {
+ term.c.x = x < 0 ? 0 : x >= term.col ? term.col-1 : x;
+ term.c.y = y < 0 ? 0 : y >= term.row ? term.row-1 : y;
+}
+
+void
+tcursor(int dir) {
+ int xf = term.c.x, yf = term.c.y;
+
+ switch(dir) {
+ case CSup:
+ yf--;
+ break;
+ case CSdown:
+ yf++;
+ break;
+ case CSleft:
+ xf--;
+ if(term.mode & TMwrap && xf < 0) {
+ xf = term.col-1, yf--;
+ if(yf < term.top)
+ yf = term.top, xf = 0;
+ }
+ break;
+ case CSright:
+ xf++;
+ if(term.mode & TMwrap && xf >= term.col) {
+ xf = 0, yf++;
+ if(yf > term.bot)
+ yf = term.bot, tscroll();
+ }
+ break;
+ }
+ tmoveto(xf, yf);
+}
+
+void
+tsetchar(char c) {
+ term.line[term.c.y][term.c.x] = term.c.attr;
+ term.line[term.c.y][term.c.x].c = c;
+ term.line[term.c.y][term.c.x].state |= CRset | CRupdate;
+}
+
+void
+tclearregion(int x1, int y1, int x2, int y2) {
+ int x, y;
+
+ LIMIT(x1, 0, term.col-1);
+ LIMIT(x2, 0, term.col-1);
+ LIMIT(y1, 0, term.row-1);
+ LIMIT(y2, 0, term.row-1);
+
+ /* XXX: could be optimized */
+ for(x = x1; x <= x2; x++)
+ for(y = y1; y <= y2; y++)
+ memset(&term.line[y][x], 0, sizeof(Glyph));
+
+ xclear(x1, y1, x2, y2);
+}
+
+void
+tdeletechar(int n) {
+ int src = term.c.x + n;
+ int dst = term.c.x;
+ int size = term.col - src;
+
+ if(src >= term.col) {
+ tclearregion(term.c.x, term.c.y, term.col-1, term.c.y);
+ return;
+ }
+ memmove(&term.line[term.c.y][dst], &term.line[term.c.y][src], size * sizeof(Glyph));
+ tclearregion(term.col-size, term.c.y, term.col-1, term.c.y);
+}
+
+void
+tinsertblank(int n) {
+ int src = term.c.x;
+ int dst = src + n;
+ int size = term.col - n - src;
+
+ if(dst >= term.col) {
+ tclearregion(term.c.x, term.c.y, term.col-1, term.c.y);
+ return;
+ }
+ memmove(&term.line[term.c.y][dst], &term.line[term.c.y][src], size * sizeof(Glyph));
+ tclearregion(src, term.c.y, dst, term.c.y);
+}
+
+void
+tsetlinestate(int n, int state) {
+ int i;
+ for(i = 0; i < term.col; i++)
+ term.line[n][i].state |= state;
+}
+
+void
+tinsertblankline (int n) {
+ int i;
+ Line blank;
+ int bot = term.bot;
+
+ if(term.c.y > term.bot)
+ bot = term.row - 1;
+ else if(term.c.y < term.top)
+ bot = term.top - 1;
+ if(term.c.y + n >= bot) {
+ tclearregion(0, term.c.y, term.col-1, bot);
+ return;
+ }
+ for(i = bot; i >= term.c.y+n; i--) {
+ /* swap deleted line <-> blanked line */
+ blank = term.line[i];
+ term.line[i] = term.line[i-n];
+ term.line[i-n] = blank;
+ /* blank it */
+ memset(blank, 0, term.col * sizeof(Glyph));
+ tsetlinestate(i, CRupdate);
+ tsetlinestate(i-n, CRupdate);
+ }
+}
+
+void
+tdeleteline(int n) {
+ int i;
+ Line blank;
+ int bot = term.bot;
+
+ if(term.c.y > term.bot)
+ bot = term.row - 1;
+ else if(term.c.y < term.top)
+ bot = term.top - 1;
+ if(term.c.y + n >= bot) {
+ tclearregion(0, term.c.y, term.col-1, bot);
+ return;
+ }
+ for(i = term.c.y; i <= bot-n; i++) {
+ /* swap deleted line <-> blanked line */
+ blank = term.line[i];
+ term.line[i] = term.line[i+n];
+ term.line[i+n] = blank;
+ /* blank it */
+ memset(blank, 0, term.col * sizeof(Glyph));
+ tsetlinestate(i, CRupdate);
+ tsetlinestate(i-n, CRupdate);
+ }
+}
+
+void
+tsetattr(int *attr, int l) {
+ int i;
+
+ for(i = 0; i < l; i++) {
+ switch(attr[i]) {
+ case 0:
+ term.c.attr.mode &= ~(ATreverse | ATunderline | ATbold);
+ term.c.attr.fg = DefaultFG;
+ term.c.attr.bg = DefaultBG;
+ break;
+ case 1:
+ term.c.attr.mode |= ATbold;
+ break;
+ case 4:
+ term.c.attr.mode |= ATunderline;
+ break;
+ case 7:
+ term.c.attr.mode |= ATreverse;
+ break;
+ case 8:
+ term.c.hidden = CShide;
+ break;
+ case 22:
+ term.c.attr.mode &= ~ATbold;
+ break;
+ case 24:
+ term.c.attr.mode &= ~ATunderline;
+ break;
+ case 27:
+ term.c.attr.mode &= ~ATreverse;
+ break;
+ case 39:
+ term.c.attr.fg = DefaultFG;
+ break;
+ case 49:
+ term.c.attr.fg = DefaultBG;
+ break;
+ default:
+ if(BETWEEN(attr[i], 30, 37))
+ term.c.attr.fg = attr[i] - 30;
+ else if(BETWEEN(attr[i], 40, 47))
+ term.c.attr.bg = attr[i] - 40;
+ break;
+ }
+ }
+}
+
+void
+tsetscroll(int t, int b) {
+ int temp;
+
+ LIMIT(t, 0, term.row-1);
+ LIMIT(b, 0, term.row-1);
+ if(t > b) {
+ temp = t;
+ t = b;
+ b = temp;
+ }
+ term.top = t;
+ term.bot = b;
+}
+
+void
+csihandle(void) {
+ if(escseq.priv)
+ csidump();
+ switch(escseq.mode) {
+ unknown:
+ default:
+ fprintf(stderr, "erresc: unknown sequence\n");
+ csidump();
+ /* die(""); */
+ break;
+ case '@': /* Insert <n> blank char */
+ DEFAULT(escseq.arg[0], 1);
+ tinsertblank(escseq.arg[0]);
+ break;
+ case 'A': /* Cursor <n> Up */
+ case 'e':
+ DEFAULT(escseq.arg[0], 1);
+ tmoveto(term.c.x, term.c.y-escseq.arg[0]);
+ break;
+ case 'B': /* Cursor <n> Down */
+ DEFAULT(escseq.arg[0], 1);
+ tmoveto(term.c.x, term.c.y+escseq.arg[0]);
+ break;
+ case 'C': /* Cursor <n> Forward */
+ case 'a':
+ DEFAULT(escseq.arg[0], 1);
+ tmoveto(term.c.x+escseq.arg[0], term.c.y);
+ break;
+ case 'D': /* Cursor <n> Backward */
+ DEFAULT(escseq.arg[0], 1);
+ tmoveto(term.c.x-escseq.arg[0], term.c.y);
+ break;
+ case 'E': /* Cursor <n> Down and first col */
+ DEFAULT(escseq.arg[0], 1);
+ tmoveto(0, term.c.y+escseq.arg[0]);
+ break;
+ case 'F': /* Cursor <n> Up and first col */
+ DEFAULT(escseq.arg[0], 1);
+ tmoveto(0, term.c.y-escseq.arg[0]);
+ break;
+ case 'G': /* Move to <col> */
+ case '`':
+ DEFAULT(escseq.arg[0], 1);
+ tmoveto(escseq.arg[0]-1, term.c.y);
+ break;
+ case 'H': /* Move to <row> <col> */
+ case 'f':
+ DEFAULT(escseq.arg[0], 1);
+ DEFAULT(escseq.arg[1], 1);
+ tmoveto(escseq.arg[1]-1, escseq.arg[0]-1);
+ break;
+ case 'J': /* Clear screen */
+ switch(escseq.arg[0]) {
+ case 0: /* below */
+ tclearregion(term.c.x, term.c.y, term.col-1, term.row-1);
+ break;
+ case 1: /* above */
+ tclearregion(0, 0, term.c.x, term.c.y);
+ break;
+ case 2: /* all */
+ tclearregion(0, 0, term.col-1, term.row-1);
+ break;
+ }
+ break;
+ case 'K': /* Clear line */
+ switch(escseq.arg[0]) {
+ case 0: /* right */
+ tclearregion(term.c.x, term.c.y, term.col-1, term.c.y);
+ break;
+ case 1: /* left */
+ tclearregion(0, term.c.y, term.c.x, term.c.y);
+ break;
+ case 2: /* all */
+ tclearregion(0, term.c.y, term.col-1, term.c.y);
+ break;
+ }
+ break;
+ case 'S':
+ case 'L': /* Insert <n> blank lines */
+ DEFAULT(escseq.arg[0], 1);
+ tinsertblankline(escseq.arg[0]);
+ break;
+ case 'l':
+ if(escseq.priv) {
+ switch(escseq.arg[0]) {
+ case 7:
+ term.mode &= ~TMwrap;
+ break;
+ case 25:
+ term.c.hidden = 1;
+ break;
+ }
+ }
+ break;
+ case 'M': /* Delete <n> lines */
+ DEFAULT(escseq.arg[0], 1);
+ tdeleteline(escseq.arg[0]);
+ break;
+ case 'X':
+ case 'P': /* Delete <n> char */
+ DEFAULT(escseq.arg[0], 1);
+ tdeletechar(escseq.arg[0]);
+ break;
+ case 'd': /* Move to <row> */
+ DEFAULT(escseq.arg[0], 1);
+ tmoveto(term.c.x, escseq.arg[0]-1);
+ break;
+ case 'h': /* Set terminal mode */
+ if(escseq.priv)
+ switch(escseq.arg[0]) {
+ case 7:
+ term.mode |= TMwrap;
+ break;
+ case 25:
+ term.c.hidden = 0;
+ break;
+ case 1034:
+ /* XXX: Interpret "meta" key, sets eighth bit. */
+ break;
+ }
+ break;
+ case 'm': /* Terminal attribute (color) */
+ tsetattr(escseq.arg, escseq.narg);
+ break;
+ case 'r':
+ if(escseq.priv)
+ goto unknown;
+ else {
+ DEFAULT(escseq.arg[0], 1);
+ DEFAULT(escseq.arg[1], term.row);
+ tsetscroll(escseq.arg[0]-1, escseq.arg[1]-1);
+ }
+ break;
+ case 's': /* Save cursor position */
+ tcpos(CSsave);
+ break;
+ case 'u': /* Load cursor position */
+ tcpos(CSload);
+ break;
+ }
+}
+
+void
+csidump(void) {
+ int i;
+ printf("ESC [ %s", escseq.priv ? "? " : "");
+ if(escseq.narg)
+ for(i = 0; i < escseq.narg; i++)
+ printf("%d ", escseq.arg[i]);
+ if(escseq.mode)
+ putchar(escseq.mode);
+ putchar('\n');
+}
+
+void
+csireset(void) {
+ memset(&escseq, 0, sizeof(escseq));
+}
+
+void
+tputtab(void) {
+ int space = TAB - term.c.x % TAB;
+
+ if(term.c.x + space >= term.col)
+ space--;
+
+ for(; space > 0; space--)
+ tcursor(CSright);
+}
+
+void
+tputc(char c) {
+#if 0
+ dump(c);
+#endif
+ if(term.esc & ESCin) {
+ if(term.esc & ESCcsi) {
+ escseq.buf[escseq.len++] = c;
+ if(BETWEEN(c, 0x40, 0x7E) || escseq.len >= ESCSIZ) {
+ term.esc = 0;
+ csiparse(), csihandle();
+ }
+ } else if (term.esc & ESCosc) {
+ if(c == ';') {
+ term.titlelen = 0;
+ term.esc = ESCin | ESCtitle;
+ }
+ } else if(term.esc & ESCtitle) {
+ if(c == '\a' || term.titlelen+1 >= TITLESIZ) {
+ term.esc = 0;
+ term.title[term.titlelen] = '\0';
+ XStoreName(xw.dis, xw.win, term.title);
+ } else {
+ term.title[term.titlelen++] = c;
+ }
+ } else if(term.esc & ESCcharset) {
+ printf("ESC ( %c\n", c);
+ switch(c) {
+ case '0': /* Line drawing crap */
+ term.c.attr.mode |= ATgfx;
+ break;
+ case 'B': /* Back to regular text */
+ term.c.attr.mode &= ~ATgfx;
+ break;
+ }
+ term.esc = 0;
+ } else {
+ switch(c) {
+ case '[':
+ term.esc |= ESCcsi;
+ break;
+ case ']':
+ term.esc |= ESCosc;
+ break;
+ case '(':
+ term.esc |= ESCcharset;
+ break;
+ case 'A':
+ tmoveto(term.c.x, term.c.y-1);
+ break;
+ case 'B':
+ tmoveto(term.c.x, term.c.y+1);
+ break;
+ case 'C':
+ tmoveto(term.c.x+1, term.c.y);
+ break;
+ case 'D':
+ tmoveto(term.c.x-1, term.c.y);
+ break;
+ default:
+ fprintf(stderr, "erresc: unknown sequence ESC %02X '%c'\n", c, isprint(c)?c:'.');
+ }
+ }
+ } else {
+ switch(c) {
+ case '\t':
+ tputtab();
+ break;
+ case '\b':
+ tcursor(CSleft);
+ break;
+ case '\r':
+ tmoveto(0, term.c.y);
+ break;
+ case '\n':
+ tnewline();
+ break;
+ case '\a':
+ xbell();
+ break;
+ case '\033':
+ csireset();
+ term.esc = ESCin;
+ break;
+ default:
+ tsetchar(c);
+ tcursor(CSright);
+ break;
+ }