Path: seismo!harvard!think!mit-eddie!genrad!panda!sources-request From: sources-request@panda.UUCP Newsgroups: mod.sources Subject: vtem - a VT100 emulator based on termcap Message-ID: <1348@panda.UUCP> Date: 27 Jan 86 15:23:36 GMT Sender: jpn@panda.UUCP Lines: 1160 Keywords: VT100 BSD4.2 pseudo terminal Approved: jpn@panda.UUCP Mod.sources: Volume 3, Issue 97 Submitted by: seismo!enea!erix!erisun!leif (Leif Samuelsson) # This is a shell archive. Remove anything before this line, # then unpack it by saving it in a file and typing "sh file". # # Wrapped by erisun!leif on Fri Jan 24 14:06:57 MET 1986 # Contents: vtem.1 Makefile vtem.h vtem.c term.c out.c echo x - vtem.1 sed 's/^@//' > "vtem.1" <<'@//E*O*F vtem.1//' @.TH VTEM 1 1986-01-24 @.SH NAME vtem - a VT100 emulator based on termcap @.SH SYNOPSIS @.I vtem @.SH DESCRIPTION @.I vtem runs on BSD4.2 and works by starting a sub-shell and then translating output escape sequences. It's function is limited by the capabilities listed in the termcap database and the capabilities of the actual terminal that it is being run on. It has been tested with Per Lindberg's excellent verifier "vttest" and has been found to give acceptable results on the following terminals: Sun windows @.br PC-Kermit in h19 emulation mode. @.br VT100 (!) On a Sun, you can use the following Suntools rootmenu entry: @.nf "VT100 Tool" shelltool -Ww 80 -Wh 24 -Wl " VT100 Tool" vtem @.fi @.SH BUGS Release 1.0 of vtem has the following bugs and limitations: Region scroll will only work on terminals with cs or al/dl. It will never write in the last position of the screen, because of the unpredictable behaviour of many terminals. VT102 capabilities are not yet implemented. No soft scroll. 132 width not supported. Input is not translated, which means no function key emulation. No double height or double width lines. No alternate character set, meaning no graphics. No printer support. No status reports, answerback messages, etc. No SETUP mode. No LEDs. @.SH AUTHOR Leif Samuelsson @.br leif@erisun.UUCP or ..enea!erix!erisun!leif @//E*O*F vtem.1// chmod u=rw,g=r,o=r vtem.1 echo x - Makefile sed 's/^@//' > "Makefile" <<'@//E*O*F Makefile//' CFLAGS= -O vtem: vtem.o term.o out.o $(CC) $(CFLAGS) -o vtem vtem.o term.o out.o -ltermcap @.c.o: ; $(CC) $(CFLAGS) -c $*.c shar: ; shar -v vtem.1 Makefile vtem.h vtem.c term.c out.c > vtem.shar @//E*O*F Makefile// chmod u=rw,g=r,o=r Makefile echo x - vtem.h sed 's/^@//' > "vtem.h" <<'@//E*O*F vtem.h//' #include #include #include #include #include #include #define FALSE 0 #define TRUE 1 typedef short Bool; extern char *getenv(); extern putchar_x(); extern struct sgttyb oldb, newb; extern struct tchars oldtchars, newtchars; extern struct ltchars oldltchars, newltchars; extern int oldlb, newlb, oldl, newl; extern int master; extern Bool BS; extern int CO, LI; extern char *AL, *BC, *BL, *CD, *CL, *CE, *CM, *CR, *CS, *DL, *DO, *KE, *KS, *MB, *MD, *ME, *MR, *ND, *NL, *SE, *SO, *SR, *TI, *TE, *UE, *UP, *US, *MAL, *MDL; #define tputs_x(s) (tputs(s, 0, putchar_x)) #define backspace() (tputs_x(BC)) #define clear_screen() (tputs_x(CL)) #define set_cursor(c, r) (tputs_x(tgoto(CM, (c), (r)))) #define linefeed() (tputs_x(NL)) #define cr() (tputs_x(CR)) @//E*O*F vtem.h// chmod u=rw,g=r,o=r vtem.h echo x - vtem.c sed 's/^@//' > "vtem.c" <<'@//E*O*F vtem.c//' /* * vt - A termcap driven VT100 emulator for BSD Unix * * Version 1.0 * * Public domain software. * Written by Leif Samuelsson (leif@erisun) in December, 1985 */ #include "vtem.h" #include int master, slave, child; char linec, linen; done() { union wait status; if (wait3(&status, WNOHANG, 0) != child) return; setupterm(0); exit(0); } main() { /* Strategy: Start three processes, one for input, one for output * and one shell. */ setup_master(); setupterm(TRUE); signal(SIGCHLD, done); if (child = fork()) handle_input(); else { if (fork()) handle_output(); else start_shell(); } } handle_input() { char buf[BUFSIZ]; int i; while (i = read(0, buf, BUFSIZ)) write(master, buf, i); setupterm(0); exit(0); } start_shell() { int t; char *shell, *tail, *rindex(); if ((shell = getenv("SHELL")) == (char *) 0) shell = "/bin/sh"; if ((tail = rindex(shell, '/')) == (char *) 0) tail = "sh"; else tail++; if ((t = open("/dev/tty", 2)) >= 0) { ioctl(t, TIOCNOTTY, (char *)0); close(t); } setup_slave(); close(master); dup2(slave, 0); dup2(slave, 1); dup2(slave, 2); close(slave); execl(shell, tail, "-i", 0); perror(shell); fail(); } fail() { kill(0, SIGTERM); setupterm(0); exit(0); } setup_master() { char line[11]; for (linec = 'p'; linec <= 's'; linec++) { sprintf(line, "/dev/pty%c0", linec); if (access(line, 0) != 0) break; for (linen = 0; linen < 16; linen++) { sprintf(line, "/dev/pty%c%1x", linec, linen); master = open(line, 2); if (master >= 0) { return; } } } fprintf(stderr, "Can't find a pty\n"); fail(); } setup_slave() { char line[11]; sprintf(line, "/dev/tty%c%1x", linec, linen); slave = open(line, 2); if (slave < 0) { perror(line); fail(); } ioctl(slave, TIOCSETP, (char *)&oldb); ioctl(slave, TIOCSETC, (char *)&oldtchars); ioctl(slave, TIOCSLTC, (char *)&oldltchars); ioctl(slave, TIOCLSET, (char *)&oldlb); ioctl(slave, TIOCSETD, (char *)&oldl); } @//E*O*F vtem.c// chmod u=rw,g=r,o=r vtem.c echo x - term.c sed 's/^@//' > "term.c" <<'@//E*O*F term.c//' /* * vt - A termcap driven VT100 emulator for BSD Unix * * Version 1.0 * * Public domain software. * Written by Leif Samuelsson (leif@erisun) in December, 1985 */ /* This module contains termcap and tty routines */ #include "vtem.h" extern char *tgetstr(), *tgoto(); /* Variables for saving original terminal parameters */ struct sgttyb oldb, newb; struct tchars oldtchars, newtchars = { -1, -1, -1, -1, -1, -1 }; struct ltchars oldltchars, newltchars = { -1, -1, -1, -1, -1, -1 }; int oldlb, newlb, oldl, newl; /* Terminal attributes */ char tbuf[1024]; Bool BS; int CO, LI; char *AL, *BC, *BL, *CD, *CL, *CE, *CM, *CR, *CS, *DL, *DO, *KE, *KS, *MB, *MD, *ME, *MR, *ND, *NL, *SE, *SO, *SR, *TI, *TE, *UE, *UP, *US, *MAL, *MDL; putchar_x(c) char c; { putchar(c); } ttycbreak() { ioctl(0, TIOCGETP, &oldb); ioctl(0, TIOCGETC, &oldtchars); ioctl(0, TIOCGETD, &oldl); ioctl(0, TIOCGLTC, &oldltchars); ioctl(0, TIOCLGET, &oldlb); newb = oldb; newb.sg_flags |= CBREAK; newb.sg_flags &= ~(CRMOD | ECHO); ioctl(0, TIOCSETP, &newb); ioctl(0, TIOCSETC, &newtchars); ioctl(0, TIOCSLTC, &newltchars); signal(SIGINT,SIG_IGN); } /* gettermtype - Finds terminal type and reads termcap entry for it. */ gettermtype() { char tptr[1024]; char *termtyp; char *tbufptr; termtyp=getenv("TERM"); switch(tgetent(tptr,termtyp)) { case -1: printf("Can't read termcap\n"); exit(1); case 0: printf("Can't find your terminal type (%s) in termcap\n", termtyp); exit(1); } tbufptr=tbuf; AL = tgetstr("al", &tbufptr); BC = tgetstr("bc", &tbufptr); BL = tgetstr("bl", &tbufptr); if (!BL) BL = "\007"; BS = tgetflag("bs",&tbufptr); if (!BC && BS) BC = "\b"; CD = tgetstr("cd", &tbufptr); CL = tgetstr("cl", &tbufptr); CE = tgetstr("ce", &tbufptr); CM = tgetstr("cm", &tbufptr); CR = tgetstr("cr", &tbufptr); if (!CR) CR = "\r"; CS = tgetstr("cs", &tbufptr); DL = tgetstr("dl", &tbufptr); DO = tgetstr("do", &tbufptr); if (!DO) DO = "\n"; KE = tgetstr("ke", &tbufptr); KS = tgetstr("ks", &tbufptr); MB = tgetstr("mb", &tbufptr); ME = tgetstr("me", &tbufptr); MR = tgetstr("mr", &tbufptr); ND = tgetstr("nd", &tbufptr); NL = tgetstr("nl", &tbufptr); if (!NL) NL = "\n"; SO = tgetstr("so", &tbufptr); SE = tgetstr("se", &tbufptr); SR = tgetstr("sr", &tbufptr); TI = tgetstr("ti", &tbufptr); TE = tgetstr("te", &tbufptr); UE = tgetstr("ue", &tbufptr); UP = tgetstr("up", &tbufptr); US = tgetstr("us", &tbufptr); MAL = tgetstr("AL", &tbufptr); MDL = tgetstr("DL", &tbufptr); CO = tgetnum("co"); LI = tgetnum("li"); if(!TI) { TI = tgetstr("vs", &tbufptr); TE = tgetstr("ve", &tbufptr); } if (CO < 80 || LI < 24) { printf("Sorry, but vtem requires 24 by 80 lines.\r\n"); exit(1); } if (!CM) { printf("Sorry, but vtem requires cursor motion capability (cm).\r\n"); exit(1); } if (!CL) { printf("Sorry, but vtem requires clear screen capability (cl).\r\n"); exit(1); } if (!UP) { printf("Sorry, but vtem requires cursor up capability (up).\r\n"); exit(1); } } setupterm(flg) /* If flg==TRUE, set line in cbreak mode and */ Bool flg; /* initialize the terminal,otherwise restore */ { if (flg) { ttycbreak(); if (TI) tputs_x(TI); /* start CM mode */ } else { if (KE) tputs_x(KE); /* Restores Keypad */ if (TE) tputs_x(TE); /* exit CM mode */ ioctl(0, TIOCSETP, &oldb); ioctl(0, TIOCSETC, &oldtchars); ioctl(0, TIOCSLTC, &oldltchars); } } /* clear_bos - clear from beginning of screen to cursor */ clear_bos(c,r) int c,r; { register int i; for (i=0; i "out.c" <<'@//E*O*F out.c//' /* * vt - A termcap driven VT100 emulator for BSD Unix * * Version 1.0 * * Public domain software. * Written by Leif Samuelsson (leif@erisun) in December, 1985 */ #include "vtem.h" typedef Bool int; static int row, col, save_row, save_col, top_margin, bottom_margin; static Bool blink, bold, reverse, underline, save_blink, save_bold, save_reverse, save_underline, origin_mode, vt52_mode, wrap; static short tabs[80]; static FILE *f; static int arg[10], argno; /* arow is absolute row, taking top_margin into account */ #define arow (row + (origin_mode ? (top_margin - 1) : 0)) /* nextch - read output and interpret control characters. * Return first non-control character. */ int nextch() { register int ch; while ((ch = getc(f)) != EOF) { switch (ch) { case '\0': /* Ignore nulls and DELs */ case '\177': break; case '\007': /* Bell */ ring_bell(); break; case '\b': /* BackSpace */ if (col > 1) { col--; backspace(); } break; case '\t': /* Tab */ while (col < 80 && !tabs[col++]); set_cursor(col-1, arow-1); break; case '\n': /* Line Feed */ do_linefeed(); break; case '\r': /* Carriage Return */ if (col > 1) { col = 1; cr(); } break; case '\016': /* Ignore shift in/out */ case '\017': break; default: return(ch); } if (f->_cnt == 0) fflush(stdout); } return(ch); } /* handle_output - Main loop of output process. * Reads and dispatches characters from output stream. */ handle_output() { register int ch; gettermtype(); (void) close(0); if ((f = fdopen(master, "r")) == (FILE *) 0) { fprintf(stderr, "handle_output: Can't read from shell\r\n"); exit(1); } do_reset(); while ((ch = nextch()) != EOF) { if (ch == '\033') { /* Escape character */ if (vt52_mode) do_vt52_escape(); else do_ansi_escape(); } else if (ch >= ' ') { /* Printing character */ if (col == 81) { if (wrap) { col = 1; set_cursor(col-1, arow-1); do_linefeed(); } else { col = 80; set_cursor(col-1, arow-1); } } if (col == 80) { if (arow != 24) putchar(ch); /* Must ignore last pos */ set_cursor(col-1, arow-1); if (wrap) col++; } else { putchar(ch); col++; } } if (f->_cnt == 0) fflush(stdout); } fclose(f); exit(0); } /* do_ansi_escape - reads and interprets an ANSI escape sequence */ do_ansi_escape() { register int ch; if ((ch = nextch()) == EOF) return; switch (ch) { case '#': do_hash(); break; case '(': case ')': do_character_sets(); break; case '7': save_row = row; save_col = col; save_blink = blink; save_bold = bold; save_reverse = reverse; save_underline = underline; break; case '8': if (save_row > 0) { row = save_row; col = save_col; set_cursor(col-1, arow-1); if (blink = save_blink) start_blink(); if (bold = save_bold) start_bold(); if (reverse = save_reverse) start_reverse(); if (underline = save_underline) start_underline(); } break; case 'D': do_linefeed(); break; case 'E': if (col > 1) { col = 1; cr(); } do_linefeed(); break; case 'H': tabs[col-1] = 1; break; case 'M': do_reverse_lf(); break; case '[': do_csi(); break; case 'c': do_reset(); break; } } /* do_csi - the real ANSI interpreter */ do_csi() { register int i, ch; int private; if ((ch = nextch()) == EOF) return; /* Check if private VT100 esc sequence */ private = 0; if (ch == '?') { private++; if ((ch = nextch()) == EOF) return; } /* Parse arguments */ argno = 0; while ((ch >= '0' && ch <= '9') || ch == ';') { arg[argno] = 0; while (ch >= '0' && ch <= '9') { arg[argno] = arg[argno] * 10 + (ch - '0'); if ((ch = nextch()) == EOF) return; } if (ch == ';') if ((ch = nextch()) == EOF) return; argno++; } if (private) { if (argno != 1) return; switch (ch) { case 'h': switch (arg[0]) { case 6: origin_mode++; break; case 7: wrap++; break; } break; case 'l': switch (arg[0]) { case 2: vt52_mode = 1; break; case 6: origin_mode = 0; break; case 7: wrap = 0; break; } break; } } else { switch (ch) { case 'A': i = (argno == 1 && arg[0] > 0) ? arg[0] : 1; while (i-- && row > 1) { cursor_up(); row--; } break; case 'B': i = (argno == 1 && arg[0] > 0) ? arg[0] : 1; while (i-- && row < bottom_margin-top_margin+1) { cursor_down(); row++; } break; case 'C': i = (argno == 1 && arg[0] > 0) ? arg[0] : 1; while (i-- && col < 80) { cursor_right(); col++; } break; case 'D': i = (argno == 1 && arg[0] > 0) ? arg[0] : 1; while (i-- && col > 1) { backspace(); col--; } break; case 'H': case 'f': do_set_cursor(); break; case 'J': do_erase_in_display(); break; case 'K': do_erase_in_line(); break; case 'L': do_insert_line(); break; case 'M': do_delete_line(); break; case 'g': do_clear_tabs(); break; case 'm': do_attributes(); break; case 'r': do_set_scroll_region(); } } } /* do_vt52_escape - interprets VT52 escape sequences */ do_vt52_escape() { register int ch; if ((ch = nextch()) == EOF) return; switch (ch) { case '<': vt52_mode = 0; break; case 'A': if (row > 1) { cursor_up(); row--; } break; case 'B': if (row < bottom_margin-top_margin+1) { cursor_down(); row++; } break; case 'C': if (col < 80) { cursor_right(); col++; } break; case 'D': if (col > 1) { backspace(); col--; } break; case 'H': row = col = 1; set_cursor(col-1, arow-1); break; case 'I': do_reverse_lf(); break; case 'J': clear_eos(); break; case 'K': clear_eol(col-1, arow-1); break; case 'Y': do_vt52_set_cursor(); break; } } do_set_cursor() { if (arg[0] == 0) arg[0]++; if (arg[1] == 0) arg[1]++; Kwitch (argno) { case 0: arg[0] = 1; argno++; /* Fall through */ case 1: arg[1] = 1; /* Correct? */ argno++; /* Fall through... */ case 2: row = arg[0]; col = arg[1]; set_cursor(col-1, arow-1); break; } } do_vt52_set_cursor() { register int ch1, ch2; if ((ch1 = nextch()) == EOF) return; if ((ch2 = nextch()) == EOF) return; ch1 -= 0x1f; ch2 -= 0x1f; if (ch1 >= 1 && ch1 <= 24 && ch2 >= 1 && ch2 <= 80) { row = ch1; col = ch2; set_cursor(col-1, arow-1); } } do_erase_in_display() { switch (argno) { case 0: arg[0] = 0; argno++; /* Fall through */ case 1: switch (arg[0]) { case 0: clear_eos(); break; case 1: clear_bos(col-1, arow-1); break; case 2: clear_screen(); set_cursor(col-1, arow-1); break; } break; } } do_erase_in_line() { switch(argno) { case 0: arg[0] = 0; argno++; /* fall through */ case 1: switch (arg[0]) { case 0: clear_eol(col-1, arow-1); break; case 1: clear_bol(col-1, arow-1); break; case 2: cr(); clear_eol(0, arow-1); set_cursor(col-1, arow-1); break; } break; } } do_clear_tabs() { register int i; if (argno == 0) arg[argno++] = 0; switch (arg[0]) { case 0: tabs[col-1] = 0; break; case 3: for (i = 0; i<80; i++) tabs[i] = 0; break; } } do_attributes() { register int i; if (argno == 0) { arg[0] = 0; argno++; } for (i=0; i 1) { row--; reverse_lf(); } } do_hash() { register int ch, i, j; if ((ch = nextch()) == EOF) return; switch(ch) { case '8': for (i=1; i<=24; i++) { set_cursor(0, i-1); for (j=1; j <= ((i==24)?79:80); j++) putchar('E'); } row = col = 1; set_cursor(col-1, arow-1); /* Correct? */ break; } } /* do_characters_sets - Not implemented */ do_character_sets() { nextch(); /* Ignore for now */ } /* do_reset - Reset emulator and screen */ do_reset() { register int i; clear_screen(); row = 1; col = 1; top_margin = 1; bottom_margin = 24; origin_mode = 0; vt52_mode = 0; wrap = 1; save_row = -1; /* So we know we haven't saved */ for (i=0; i<80; i++) tabs[i] = ((i/8)*8 == i); } /* The following are routines for VT102 compatibility */ do_insert_line() { /* Not yet implemented */ } do_delete_line() { /* Not yet implemented */ } @//E*O*F out.c// chmod u=rw,g=r,o=r out.c exit 0