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