vt.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820
  1. /*
  2. * Known bugs:
  3. *
  4. * 1. We don't handle cursor movement characters inside escape sequences.
  5. * That is, ESC[2C moves two to the right, so ESC[2\bC is supposed to back
  6. * up one and then move two to the right.
  7. *
  8. * 2. We don't handle tabstops past nelem(tabcol) columns.
  9. *
  10. * 3. We don't respect requests to do reverse video for the whole screen.
  11. *
  12. * 4. We ignore the ESC#n codes, so that we don't do double-width nor
  13. * double-height lines, nor the ``fill the screen with E's'' confidence check.
  14. *
  15. * 5. Cursor key sequences aren't selected by keypad application mode.
  16. *
  17. * 6. "VT220" mode (-2) currently just switches the default cursor key
  18. * functions (same as -a); it's still just a VT100 emulation.
  19. *
  20. * 7. VT52 mode and a few other rarely used features are not implemented.
  21. */
  22. #include <u.h>
  23. #include <libc.h>
  24. #include <draw.h>
  25. #include <bio.h>
  26. #include <ctype.h>
  27. #include "cons.h"
  28. int wraparound = 1;
  29. int originrelative = 0;
  30. int tabcol[200];
  31. struct funckey vt100fk[NKEYS] = {
  32. { "up key", "\033OA", },
  33. { "down key", "\033OB", },
  34. { "left key", "\033OD", },
  35. { "right key", "\033OC", },
  36. };
  37. struct funckey ansifk[NKEYS] = {
  38. { "up key", "\033[A", },
  39. { "down key", "\033[B", },
  40. { "left key", "\033[D", },
  41. { "right key", "\033[C", },
  42. { "F1", "\033OP", },
  43. { "F2", "\033OQ", },
  44. { "F3", "\033OR", },
  45. { "F4", "\033OS", },
  46. { "F5", "\033OT", },
  47. { "F6", "\033OU", },
  48. { "F7", "\033OV", },
  49. { "F8", "\033OW", },
  50. { "F9", "\033OX", },
  51. { "F10", "\033OY", },
  52. { "F11", "\033OZ", },
  53. { "F12", "\033O1", },
  54. };
  55. struct funckey vt220fk[NKEYS] = {
  56. { "up key", "\033[A", },
  57. { "down key", "\033[B", },
  58. { "left key", "\033[D", },
  59. { "right key", "\033[C", },
  60. };
  61. char gmap[256] = {
  62. ['_'] ' ', /* blank */
  63. ['\\'] '*', /* diamond */
  64. ['a'] 'X', /* checkerboard */
  65. ['b'] '\t', /* HT */
  66. ['c'] '\x0C', /* FF */
  67. ['d'] '\r', /* CR */
  68. ['e'] '\n', /* LF */
  69. ['f'] 'o', /* degree */
  70. ['g'] '+', /* plus/minus */
  71. ['h'] '\n', /* NL, but close enough */
  72. ['i'] '\v', /* VT */
  73. ['j'] '+', /* lower right corner */
  74. ['k'] '+', /* upper right corner */
  75. ['l'] '+', /* upper left corner */
  76. ['m'] '+', /* lower left corner */
  77. ['n'] '+', /* crossing lines */
  78. ['o'] '-', /* horiz line - scan 1 */
  79. ['p'] '-', /* horiz line - scan 3 */
  80. ['q'] '-', /* horiz line - scan 5 */
  81. ['r'] '-', /* horiz line - scan 7 */
  82. ['s'] '-', /* horiz line - scan 9 */
  83. ['t'] '+', /* |- */
  84. ['u'] '+', /* -| */
  85. ['v'] '+', /* upside down T */
  86. ['w'] '+', /* rightside up T */
  87. ['x'] '|', /* vertical bar */
  88. ['y'] '<', /* less/equal */
  89. ['z'] '>', /* gtr/equal */
  90. ['{'] 'p', /* pi */
  91. ['|'] '!', /* not equal */
  92. ['}'] 'L', /* pound symbol */
  93. ['~'] '.', /* centered dot: · */
  94. };
  95. static void setattr(int argc, int *argv);
  96. void
  97. emulate(void)
  98. {
  99. char buf[BUFS+1];
  100. int n;
  101. int c;
  102. int operand[10];
  103. int noperand;
  104. int savex, savey, saveattribute, saveisgraphics;
  105. int isgraphics;
  106. int g0set, g1set;
  107. isgraphics = 0;
  108. g0set = 'B'; /* US ASCII */
  109. g1set = 'B'; /* US ASCII */
  110. savex = savey = 0;
  111. yscrmin = 0;
  112. yscrmax = ymax;
  113. saveattribute = 0;
  114. saveisgraphics = 0;
  115. /* set initial tab stops to DEC-standard 8-column spacing */
  116. for(c=0; (c+=8)<nelem(tabcol);)
  117. tabcol[c] = 1;
  118. for (;;) {
  119. if (y > ymax) {
  120. x = 0;
  121. newline();
  122. }
  123. buf[0] = get_next_char();
  124. buf[1] = '\0';
  125. switch(buf[0]) {
  126. case '\000':
  127. case '\001':
  128. case '\002':
  129. case '\003':
  130. case '\004':
  131. case '\005':
  132. case '\006':
  133. goto Default;
  134. case '\007': /* bell */
  135. ringbell();
  136. break;
  137. case '\010': /* backspace */
  138. if (x > 0)
  139. --x;
  140. break;
  141. case '\011': /* tab to next tab stop; if none, to right margin */
  142. for(c=x+1; c<nelem(tabcol) && !tabcol[c]; c++)
  143. ;
  144. if(c < nelem(tabcol))
  145. x = c;
  146. else
  147. x = xmax;
  148. break;
  149. case '\012': /* linefeed */
  150. case '\013':
  151. case '\014':
  152. newline();
  153. attribute = 0;
  154. if (ttystate[cs->raw].nlcr)
  155. x = 0;
  156. break;
  157. case '\015': /* carriage return */
  158. x = 0;
  159. attribute = 0;
  160. if (ttystate[cs->raw].crnl)
  161. newline();
  162. break;
  163. case '\016': /* SO: invoke G1 char set */
  164. isgraphics = (isdigit(g1set));
  165. break;
  166. case '\017': /* SI: invoke G0 char set */
  167. isgraphics = (isdigit(g0set));
  168. break;
  169. case '\020': /* DLE */
  170. case '\021': /* DC1 */
  171. case '\022': /* XON */
  172. case '\023': /* DC3 */
  173. case '\024': /* XOFF */
  174. case '\025': /* NAK */
  175. case '\026': /* SYN */
  176. case '\027': /* ETB */
  177. case '\030': /* CAN: cancel escape sequence, display checkerboard (not implemented) */
  178. case '\031': /* EM */
  179. case '\032': /* SUB: same as CAN */
  180. goto Default;
  181. ;
  182. /* ESC, \033, is handled below */
  183. case '\034': /* FS */
  184. case '\035': /* GS */
  185. case '\036': /* RS */
  186. case '\037': /* US */
  187. break;
  188. case '\177': /* delete: ignored */
  189. break;
  190. case '\033':
  191. switch(get_next_char()){
  192. /*
  193. * 1 - graphic processor option on (no-op; not installed)
  194. */
  195. case '1':
  196. break;
  197. /*
  198. * 2 - graphic processor option off (no-op; not installed)
  199. */
  200. case '2':
  201. break;
  202. /*
  203. * 7 - save cursor position.
  204. */
  205. case '7':
  206. savex = x;
  207. savey = y;
  208. saveattribute = attribute;
  209. saveisgraphics = isgraphics;
  210. break;
  211. /*
  212. * 8 - restore cursor position.
  213. */
  214. case '8':
  215. x = savex;
  216. y = savey;
  217. attribute = saveattribute;
  218. isgraphics = saveisgraphics;
  219. break;
  220. /*
  221. * c - Reset terminal.
  222. */
  223. case 'c':
  224. cursoron = 1;
  225. curson(0);
  226. ttystate[cs->raw].nlcr = 0;
  227. break;
  228. /*
  229. * D - active position down a line, scroll if at bottom margin.
  230. * (Original VT100 had a bug: tracked new-line/line-feed mode.)
  231. */
  232. case 'D':
  233. if(++y > yscrmax) {
  234. y = yscrmax;
  235. scroll(yscrmin+1, yscrmax+1, yscrmin, yscrmax);
  236. }
  237. break;
  238. /*
  239. * E - active position to start of next line, scroll if at bottom margin.
  240. */
  241. case 'E':
  242. x = 0;
  243. if(++y > yscrmax) {
  244. y = yscrmax;
  245. scroll(yscrmin+1, yscrmax+1, yscrmin, yscrmax);
  246. }
  247. break;
  248. /*
  249. * H - set tab stop at current column.
  250. * (This is cursor home in VT52 mode (not implemented).)
  251. */
  252. case 'H':
  253. if(x < nelem(tabcol))
  254. tabcol[x] = 1;
  255. break;
  256. /*
  257. * M - active position up a line, scroll if at top margin..
  258. */
  259. case 'M':
  260. if(--y < yscrmin) {
  261. y = yscrmin;
  262. scroll(yscrmin, yscrmax, yscrmin+1, yscrmin);
  263. }
  264. break;
  265. /*
  266. * Z - identification. the terminal
  267. * emulator will return the response
  268. * code for a generic VT100.
  269. */
  270. case 'Z':
  271. Ident:
  272. sendnchars2(7, "\033[?1;2c"); /* VT100 with AVO option */
  273. // sendnchars2(5, "\033[?6c"); /* VT102 (insert/delete-char, etc.) */
  274. break;
  275. /*
  276. * < - enter ANSI mode
  277. */
  278. case '<':
  279. break;
  280. /*
  281. * > - set numeric keypad mode on (not implemented)
  282. */
  283. case '>':
  284. break;
  285. /*
  286. * = - set numeric keypad mode off (not implemented)
  287. */
  288. case '=':
  289. break;
  290. /*
  291. * # - Takes a one-digit argument
  292. */
  293. case '#':
  294. switch(get_next_char()){
  295. case '3': /* Top half of double-height line */
  296. case '4': /* Bottom half of double-height line */
  297. case '5': /* Single-width single-height line */
  298. case '6': /* Double-width line */
  299. case '7': /* Screen print */
  300. case '8': /* Fill screen with E's */
  301. break;
  302. }
  303. break;
  304. /*
  305. * ( - switch G0 character set
  306. */
  307. case '(':
  308. g0set = get_next_char();
  309. break;
  310. /*
  311. * - switch G1 character set
  312. */
  313. case ')':
  314. g1set = get_next_char();
  315. break;
  316. /*
  317. * Received left bracket.
  318. */
  319. case '[':
  320. /*
  321. * A semi-colon or ? delimits arguments.
  322. */
  323. memset(operand, 0, sizeof(operand));
  324. operand[0] = number(buf, nil);
  325. noperand = 1;
  326. while(buf[0] == ';' || buf[0] == '?'){
  327. if(noperand < nelem(operand)) {
  328. noperand++;
  329. operand[noperand-1] = number(buf, nil);
  330. } else
  331. number(buf, nil);
  332. }
  333. /*
  334. * do escape2 stuff
  335. */
  336. switch(buf[0]){
  337. /*
  338. * c - same as ESC Z: what are you?
  339. */
  340. case 'c':
  341. goto Ident;
  342. /*
  343. * g - various tabstop manipulation
  344. */
  345. case 'g':
  346. switch(operand[0]){
  347. case 0: /* clear tab at current column */
  348. if(x < nelem(tabcol))
  349. tabcol[x] = 0;
  350. break;
  351. case 3: /* clear all tabs */
  352. memset(tabcol, 0, sizeof tabcol);
  353. break;
  354. }
  355. break;
  356. /*
  357. * l - clear various options.
  358. */
  359. case 'l':
  360. if(noperand == 1){
  361. switch(operand[0]){
  362. case 20: /* set line feed mode */
  363. ttystate[cs->raw].nlcr = 0;
  364. break;
  365. case 30: /* screen invisible (? not supported through VT220) */
  366. break;
  367. }
  368. }else while(--noperand > 0){
  369. switch(operand[noperand]){
  370. case 1: /* set cursor keys to send ANSI functions: ESC [ A..D */
  371. break;
  372. case 2: /* set VT52 mode (not implemented) */
  373. break;
  374. case 3: /* set 80 columns */
  375. setdim(-1, 80);
  376. break;
  377. case 4: /* set jump scrolling */
  378. break;
  379. case 5: /* set normal video on screen */
  380. break;
  381. case 6: /* set origin to absolute */
  382. print("OL\n");
  383. originrelative = 0;
  384. x = y = 0;
  385. break;
  386. case 7: /* reset auto-wrap mode */
  387. wraparound = 0;
  388. break;
  389. case 8: /* reset auto-repeat mode */
  390. break;
  391. case 9: /* reset interlacing mode */
  392. break;
  393. case 25: /* text cursor off (VT220) */
  394. cursoron = 0;
  395. cursoff();
  396. break;
  397. }
  398. }
  399. break;
  400. /*
  401. * h - set various options.
  402. */
  403. case 'h':
  404. //print("h%d:%d,%d\n", noperand, operand[0], operand[1]);
  405. if(noperand == 1){
  406. switch(operand[0]){
  407. case 20: /* set newline mode */
  408. ttystate[cs->raw].nlcr = 1;
  409. break;
  410. case 30: /* screen visible (? not supported through VT220) */
  411. break;
  412. }
  413. }else while(--noperand > 0){
  414. switch(operand[noperand]){
  415. case 1: /* set cursor keys to send application function: ESC O A..D */
  416. break;
  417. case 2: /* set ANSI */
  418. break;
  419. case 3: /* set 132 columns */
  420. setdim(-1, 132);
  421. break;
  422. case 4: /* set smooth scrolling */
  423. break;
  424. case 5: /* set screen to reverse video (not implemented) */
  425. break;
  426. case 6: /* set origin to relative */
  427. originrelative = 1;
  428. x = 0;
  429. y = yscrmin;
  430. break;
  431. case 7: /* set auto-wrap mode */
  432. wraparound = 1;
  433. break;
  434. case 8: /* set auto-repeat mode */
  435. break;
  436. case 9: /* set interlacing mode */
  437. break;
  438. case 25: /* text cursor on (VT220) */
  439. cursoron = 1;
  440. curson(0);
  441. break;
  442. }
  443. }
  444. break;
  445. /*
  446. * m - change character attributes.
  447. */
  448. case 'm':
  449. setattr(noperand, operand);
  450. break;
  451. /*
  452. * n - request various reports
  453. */
  454. case 'n':
  455. switch(operand[0]){
  456. case 5: /* status */
  457. sendnchars2(4, "\033[0n"); /* terminal ok */
  458. break;
  459. case 6: /* cursor position */
  460. sendnchars2(sprint(buf, "\033[%d;%dR",
  461. originrelative ? y+1 - yscrmin : y+1, x+1), buf);
  462. break;
  463. }
  464. break;
  465. /*
  466. * q - turn on list of LEDs; turn off others.
  467. */
  468. case 'q':
  469. // print("LED\n");
  470. break;
  471. /*
  472. * r - change scrolling region. operand[0] is
  473. * min scrolling region and operand[1] is max
  474. * scrolling region.
  475. */
  476. case 'r':
  477. yscrmin = 0;
  478. yscrmax = ymax;
  479. switch(noperand){
  480. case 2:
  481. yscrmax = operand[1]-1;
  482. if(yscrmax > ymax)
  483. yscrmax = ymax;
  484. case 1:
  485. yscrmin = operand[0]-1;
  486. if(yscrmin < 0)
  487. yscrmin = 0;
  488. }
  489. x = 0;
  490. y = yscrmin;
  491. break;
  492. /*
  493. * x - report terminal parameters
  494. */
  495. case 'x':
  496. sendnchars2(20, "\033[3;1;1;120;120;1;0x");
  497. break;
  498. /*
  499. * y - invoke confidence test
  500. */
  501. case 'y':
  502. break;
  503. /*
  504. * A - cursor up.
  505. */
  506. case 'A':
  507. if(operand[0] == 0)
  508. operand[0] = 1;
  509. y -= operand[0];
  510. if(y < yscrmin)
  511. y = yscrmin;
  512. olines -= operand[0];
  513. if(olines < 0)
  514. olines = 0;
  515. break;
  516. /*
  517. * B - cursor down
  518. */
  519. case 'B':
  520. if(operand[0] == 0)
  521. operand[0] = 1;
  522. y += operand[0];
  523. if(y > yscrmax)
  524. y=yscrmax;
  525. break;
  526. /*
  527. * C - cursor right
  528. */
  529. case 'C':
  530. if(operand[0] == 0)
  531. operand[0] = 1;
  532. x += operand[0];
  533. /*
  534. * VT-100-UG says not to go past the
  535. * right margin.
  536. */
  537. if(x > xmax)
  538. x=xmax;
  539. break;
  540. /*
  541. * D - cursor left
  542. */
  543. case 'D':
  544. if(operand[0] == 0)
  545. operand[0] = 1;
  546. x -= operand[0];
  547. if(x < 0)
  548. x = 0;
  549. break;
  550. /*
  551. * H and f - cursor motion. operand[0] is row and
  552. * operand[1] is column, origin 1.
  553. */
  554. case 'H':
  555. case 'f':
  556. x = operand[1] - 1;
  557. if(x < 0)
  558. x = 0;
  559. if(x > xmax)
  560. x = xmax;
  561. y = operand[0] - 1;
  562. if(y < 0)
  563. y = 0;
  564. if(originrelative){
  565. y += yscrmin;
  566. if(y > yscrmax)
  567. y = ymax;
  568. }else{
  569. if(y > yscrmax)
  570. y = yscrmax;
  571. }
  572. break;
  573. /*
  574. * J - clear some or all of the display.
  575. */
  576. case 'J':
  577. switch (operand[0]) {
  578. /*
  579. * operand 2: whole screen.
  580. */
  581. case 2:
  582. clear(Rpt(pt(0, 0), pt(xmax+1, ymax+1)));
  583. break;
  584. /*
  585. * operand 1: start of screen to active position, inclusive.
  586. */
  587. case 1:
  588. clear(Rpt(pt(0, 0), pt(xmax+1, y)));
  589. clear(Rpt(pt(0, y), pt(x+1, y+1)));
  590. break;
  591. /*
  592. * Default: active position to end of screen, inclusive.
  593. */
  594. default:
  595. clear(Rpt(pt(x, y), pt(xmax+1, y+1)));
  596. clear(Rpt(pt(0, y+1), pt(xmax+1, ymax+1)));
  597. break;
  598. }
  599. break;
  600. /*
  601. * K - clear some or all of the line.
  602. */
  603. case 'K':
  604. switch (operand[0]) {
  605. /*
  606. * operand 2: whole line.
  607. */
  608. case 2:
  609. clear(Rpt(pt(0, y), pt(xmax+1, y+1)));
  610. break;
  611. /*
  612. * operand 1: start of line to active position, inclusive.
  613. */
  614. case 1:
  615. clear(Rpt(pt(0, y), pt(x+1, y+1)));
  616. break;
  617. /*
  618. * Default: active position to end of line, inclusive.
  619. */
  620. default:
  621. clear(Rpt(pt(x, y), pt(xmax+1, y+1)));
  622. break;
  623. }
  624. break;
  625. /*
  626. * L - insert a line at cursor position (VT102 and later)
  627. */
  628. case 'L':
  629. scroll(y, yscrmax, y+1, y);
  630. break;
  631. /*
  632. * M - delete a line at cursor position (VT102 and later)
  633. */
  634. case 'M':
  635. scroll(y+1, yscrmax+1, y, yscrmax);
  636. break;
  637. case '=': /* ? not supported through VT220 */
  638. number(buf, nil);
  639. switch(buf[0]) {
  640. case 'h':
  641. case 'l':
  642. break;
  643. }
  644. break;
  645. /*
  646. * Anything else we ignore for now...
  647. */
  648. default:
  649. break;
  650. }
  651. break;
  652. /*
  653. * Collapse multiple '\033' to one.
  654. */
  655. case '\033':
  656. peekc = '\033';
  657. break;
  658. /*
  659. * Ignore other commands.
  660. */
  661. default:
  662. break;
  663. }
  664. break;
  665. default: /* ordinary char */
  666. Default:
  667. if(isgraphics && gmap[(uchar) buf[0]])
  668. buf[0] = gmap[(uchar) buf[0]];
  669. /* line wrap */
  670. if (x > xmax){
  671. if(wraparound){
  672. x = 0;
  673. newline();
  674. }else{
  675. continue;
  676. }
  677. }
  678. n = 1;
  679. c = 0;
  680. while (!cs->raw && host_avail() && x+n<=xmax && n<BUFS
  681. && (c = get_next_char())>=' ' && c<'\177') {
  682. buf[n++] = c;
  683. c = 0;
  684. }
  685. buf[n] = 0;
  686. // clear(Rpt(pt(x,y), pt(x+n, y+1)));
  687. drawstring(pt(x, y), buf, attribute);
  688. x += n;
  689. peekc = c;
  690. break;
  691. }
  692. }
  693. }
  694. static void
  695. setattr(int argc, int *argv)
  696. {
  697. int i;
  698. for(i=0; i<argc; i++) {
  699. switch(argv[i]) {
  700. case 0:
  701. attribute = 0;
  702. break;
  703. case 1:
  704. attribute |= THighIntensity;
  705. break;
  706. case 4:
  707. attribute |= TUnderline;
  708. break;
  709. case 5:
  710. attribute |= TBlink;
  711. break;
  712. case 7:
  713. attribute |= TReverse;
  714. break;
  715. case 8:
  716. attribute |= TInvisible;
  717. break;
  718. case 22:
  719. attribute &= ~THighIntensity;
  720. break;
  721. case 24:
  722. attribute &= ~TUnderline;
  723. break;
  724. case 25:
  725. attribute &= ~TBlink;
  726. break;
  727. case 27:
  728. attribute &= ~TReverse;
  729. break;
  730. case 28:
  731. attribute &= ~TInvisible;
  732. break;
  733. case 30:
  734. case 31:
  735. case 32:
  736. case 33:
  737. case 34:
  738. case 35:
  739. case 36:
  740. case 37:
  741. frgcolor = argv[i]-30;
  742. break;
  743. case 40:
  744. case 41:
  745. case 42:
  746. case 43:
  747. case 44:
  748. case 45:
  749. case 46:
  750. case 47:
  751. bckcolor = argv[i]-40;
  752. break;
  753. }
  754. }
  755. }