📄 vt100.c
字号:
/* * VT100.C ANSI/VT102 emulator code. * This code was integrated to the Minicom communications * package, but has been reworked to allow usage as a separate * module. * * (C) 1993 Miquel van Smoorenburg. */#include <sys/types.h>#if defined (_POSIX_SOURCE) || defined(_BSD43)# include <stdlib.h># include <unistd.h># undef NULL#endif#include <time.h>#include <stdio.h>#include <signal.h>#include <setjmp.h>#include <string.h>#include "window.h"#include "vt100.h"/* * The global variable esc_s holds the escape sequence status: * 0 - normal * 1 - ESC * 2 - ESC [ * 3 - ESC [ ? * 4 - ESC ( * 5 - ESC ) * 6 - ESC # * 7 - ESC P */static int esc_s = 0;#define ESC 27/* Structure to hold escape sequences. */struct escseq { int code; char *vt100_st; char *vt100_app; char *ansi;};/* Escape sequences for different terminal types. */static struct escseq vt_keys[] = { { K_F1, "OP", "OP", "OP" }, { K_F2, "OQ", "OQ", "OQ" }, { K_F3, "OR", "OR", "OR" }, { K_F4, "OS", "OS", "OS" }, { K_F5, "", "", "OT" }, { K_F6, "", "", "OU" }, { K_F7, "", "", "OV" }, { K_F8, "", "", "OW" }, { K_F9, "", "", "OX" }, { K_F10, "", "", "OY" }, { K_HOME, "[H", "[H", "[H" }, { K_PGUP, "", "", "[V" }, { K_UP, "[A", "OA", "[A" }, { K_LT, "[D", "OD", "[D" }, { K_RT, "[C", "OC", "[C" }, { K_DN, "[B", "OB", "[B" }, { K_END, "[K", "[K", "[Y" }, { K_PGDN, "", "", "[U" }, { K_INS, "", "", "0" }, { K_DEL, "\177", "\177", "\177" }, { 0, NULL, NULL, NULL },};#ifndef _SELECT/* This is cheaper. */# define v_termout termout#endif#ifdef _SELECTstatic int vt_echo; /* Local echo on/off. */#endifstatic int vt_type = ANSI; /* Terminal type. */static int vt_wrap = 0; /* Line wrap on/off */static int vt_addlf = 0; /* Add linefeed on/off */static int vt_fg; /* Standard foreground color. */static int vt_bg; /* Standard background color. */static int vt_keypad; /* Keypad mode. */static int vt_cursor; /* cursor key mode. */static int vt_bs = 8; /* Code that backspace key sends. */WIN *vt_win = NIL_WIN; /* Output window. */static int vt_docap; /* Capture on/off. */static FILE *vt_capfp; /* Capture file. */static void (*vt_keyb)(); /* Gets called for NORMAL/APPL switch. */static void (*termout)(); /* Gets called to output a string. */static int escparms[8]; /* Cumulated escape sequence. */static int ptr = -2; /* Index into escparms array. */short newy1 = 0; /* Current size of scrolling region. */short newy2 = 23;/* Saved color and posistions */static short savex = 0, savey = 0, saveattr = A_NORMAL, savecol = 112;/* * Initialize the emulator once. */void vt_install(fun1, fun2, win)void (*fun1)();void (*fun2)();WIN *win;{ termout = fun1; vt_keyb = fun2; vt_win = win;}/* Set characteristics of emulator. */void vt_init(type, fg, bg, wrap, add)int type;int fg;int bg;int wrap;int add;{ vt_type = type; vt_fg = fg; vt_bg = bg; if (wrap >= 0) vt_win->wrap = vt_wrap = wrap; vt_addlf = add; newy1 = 0; newy2 = vt_win->ys - 1; wresetregion(vt_win); vt_keypad = NORMAL; vt_cursor = NORMAL; vt_echo = 0; ptr = -2; esc_s = 0; if (vt_keyb) vt_keyb(vt_keypad, vt_cursor); wsetfgcol(vt_win, vt_fg); wsetbgcol(vt_win, vt_bg);}/* Change some things on the fly. */void vt_set(addlf, wrap, capfp, docap, bscode, echo, cursor)int addlf;int wrap;FILE *capfp;int docap;int bscode;int echo;int cursor;{ if (addlf >= 0) vt_addlf = addlf; if (wrap >= 0) vt_win->wrap = vt_wrap = wrap; if (capfp != (FILE *)0) vt_capfp = capfp; if (docap >= 0) vt_docap = docap; if (bscode >= 0) vt_bs = bscode; if (echo >= 0) vt_echo = echo; if (cursor >= 0) vt_cursor = cursor;}/* Output a string to the modem. */#ifdef _SELECTstatic void v_termout(s)char *s;{ char *p; if (vt_echo) { for(p = s; *p; p++) vt_out(*p); wflush(); } termout(s);}#endif/* * Escape code handling. *//* * ESC was seen the last time. Process the next character. */static void state1(c)int c;{ short x, y, f; switch(c) { case '[': /* ESC [ */ esc_s = 2; return; case '(': /* ESC ( */ esc_s = 4; return; case ')': /* ESC ) */ esc_s = 5; return; case '#': /* ESC # */ esc_s = 6; return; case 'P': /* ESC P (DCS, Device Control String) */ esc_s = 7; return; case 'D': /* Cursor down */ case 'M': /* Cursor up */ x = vt_win->curx; if (c == 'D') { /* Down. */ y = vt_win->cury + 1; if (y == newy2 + 1) wscroll(vt_win, S_UP); else if (vt_win->cury < vt_win->ys) wlocate(vt_win, x, y); } if (c == 'M') { /* Up. */ y = vt_win->cury - 1; if (y == newy1 - 1) wscroll(vt_win, S_DOWN); else if (y >= 0) wlocate(vt_win, x, y); } break; case 'E': /* CR + NL */ wputs(vt_win, "\r\n"); break; case '7': /* Save attributes and cursor position */ case 's': savex = vt_win->curx; savey = vt_win->cury; saveattr = vt_win->attr; savecol = vt_win->color; break; case '8': /* Restore them */ case 'u': vt_win->color = savecol; /* HACK should use wsetfgcol etc */ wsetattr(vt_win, saveattr); wlocate(vt_win, savex, savey); break; case '=': /* Keypad into applications mode */ vt_keypad = APPL; if (vt_keyb) vt_keyb(vt_keypad, vt_cursor); break; case '>': /* Keypad into numeric mode */ vt_keypad = NORMAL; if (vt_keyb) vt_keyb(vt_keypad, vt_cursor); break; case 'Z': /* Report terminal type */ if (vt_type == VT100) v_termout("\033[?1;0c"); else v_termout("\033[?c"); break; case 'c': /* Reset to initial state */ f = A_NORMAL; wsetattr(vt_win, f); wlocate(vt_win, 0, 0); vt_win->wrap = (vt_type != VT100); if (vt_wrap != -1) vt_win->wrap = vt_wrap; vt_init(vt_type, vt_fg, vt_bg, vt_win->wrap, 0); break; case 'N': /* G2 character set for next character only*/ case 'O': /* G3 " " */ case 'H': /* Set tab in current position */ case '<': /* Exit vt52 mode */ default: /* ALL IGNORED */ break; } esc_s = 0; return;}/* * ESC [ ... was seen the last time. Process next character. */static void state2(c)int c;{ short x, y, attr, f; char temp[16]; char did_esc = 1; /* See if a number follows */ if (c >= '0' && c <= '9') { if (ptr < 0) ptr = 0; escparms[ptr] = 10*(escparms[ptr]) + c - '0'; return; } /* Separation between numbers ? */ if (c == ';') { if (ptr < 15 && ptr >= 0) ptr++; return; } /* ESC [ something-without-argument? */ if (ptr < 0) switch(c) { case '?': /* ESC [ ? */ esc_s = 3; return; case 'K': /* Clear to end of line */ wclreol(vt_win); break; case 'J': /* Clear to end of screen */ wclreos(vt_win); break; case 'c': /* Identify Terminal Type */ if (vt_type == VT100) v_termout("\033[?1;0c"); else v_termout("\033[?c"); break; case 'i': /* print page */ case 'g': /* clear tab stop */ /* IGNORED */ break; case 's': /* Save attributes and cursor position */ savex = vt_win->curx; savey = vt_win->cury; saveattr = vt_win->attr; savecol = vt_win->color; break; case 'u': /* Restore them */ vt_win->color = savecol; /* HACK should use wsetfgcol etc */ wsetattr(vt_win, saveattr); wlocate(vt_win, savex, savey); break; default: /* We did not find it. Maybe it's a variable-argument func. */ did_esc = 0; break; } if (ptr < 0 && did_esc) { esc_s = 0; return; } /* ESC [ one-argument-only something ? */ if (ptr == 0) switch(c) { case 'K': /* Line erasing */ switch(escparms[0]) { case 0: wclreol(vt_win); break; case 1: wclrbol(vt_win); break; case 2: wclrel(vt_win); break; } break; case 'J': /* Screen erasing */ x = vt_win->color; y = vt_win->attr; if (vt_type == ANSI) { wsetattr(vt_win, A_NORMAL); wsetfgcol(vt_win, WHITE); wsetbgcol(vt_win, BLACK); } switch(escparms[0]) { case 0: wclreos(vt_win); break; case 1: wclrbos(vt_win); break; case 2: winclr(vt_win); break; } if (vt_type == ANSI) { vt_win->color = x; vt_win->attr = y; } break; case 'n': /* Requests / Reports */ switch(escparms[0]) { case 5: /* Status */ v_termout("\033[0n"); break; case 6: /* Cursor Position */ sprintf(temp, "\033[%d;%dR", vt_win->cury + 1, vt_win->curx + 1); v_termout(temp); break; } break; case 'c': /* Identify Terminal Type */ if (vt_type == VT100) { v_termout("\033[?1;0c"); break; } v_termout("\033[?c"); break; case 'g': /* Tabulation */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -