vt.c 19 KB

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