3 This program is free software; you can redistribute it and/or
4 modify it under the terms of the GNU Lesser General Public
5 License (LGPL) as published by the Free Software Foundation.
7 Please refer to the COPYING file for more information.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 General Public License for more details.
14 You should have received a copy of the GNU Lesser General Public
15 License along with this program; if not, write to the Free Software
16 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 Copyright © 2004 Bruno T. C. de Oliveira
19 Copyright © 2006 Pierre Habouzit
27 #include "madtty_priv.h"
29 #define MAX_CSI_ES_PARAMS 32
31 static inline void clamp_cursor_to_bounds(RoteTerm *rt)
34 rt->curpos_dirty = true;
38 rt->curpos_dirty = true;
42 if (rt->crow >= rt->rows) {
43 rt->curpos_dirty = true;
44 rt->crow = rt->rows - 1;
46 if (rt->ccol >= rt->cols) {
47 rt->curpos_dirty = true;
48 rt->ccol = rt->cols - 1;
52 static void cursor_line_down(RoteTerm *rt)
57 rt->curpos_dirty = true;
58 if (rt->crow <= rt->pd->scrollbottom)
61 /* must scroll the scrolling region up by 1 line, and put cursor on
63 rt->crow = rt->pd->scrollbottom;
65 for (i = rt->pd->scrolltop; i < rt->pd->scrollbottom; i++) {
66 rt->line_dirty[i] = true;
67 memcpy(rt->cells[i], rt->cells[i+1], sizeof(RoteCell) * rt->cols);
70 rt->line_dirty[rt->pd->scrollbottom] = true;
72 /* clear last row of the scrolling region */
73 for (i = 0; i < rt->cols; i++) {
74 rt->cells[rt->pd->scrollbottom][i].ch = 0x20;
75 rt->cells[rt->pd->scrollbottom][i].attr = 0x70;
79 static void cursor_line_up(RoteTerm *rt)
84 rt->curpos_dirty = true;
85 if (rt->crow >= rt->pd->scrolltop)
88 /* must scroll the scrolling region up by 1 line, and put cursor on
90 rt->crow = rt->pd->scrolltop;
92 for (i = rt->pd->scrollbottom; i > rt->pd->scrolltop; i--) {
93 rt->line_dirty[i] = true;
94 memcpy(rt->cells[i], rt->cells[i-1], sizeof(RoteCell) * rt->cols);
97 rt->line_dirty[rt->pd->scrolltop] = true;
99 /* clear first row of the scrolling region */
100 for (i = 0; i < rt->cols; i++) {
101 rt->cells[rt->pd->scrolltop][i].ch = 0x20;
102 rt->cells[rt->pd->scrolltop][i].attr = 0x70;
106 static inline void put_normal_char(RoteTerm *rt, int c)
108 if (rt->ccol >= rt->cols) {
110 cursor_line_down(rt);
116 for(i = rt->cols - 1; i >= rt->ccol+1; i--)
117 rt->cells[rt->crow][i] = rt->cells[rt->crow][i-1];
120 rt->cells[rt->crow][rt->ccol].ch = c;
121 rt->cells[rt->crow][rt->ccol].attr = rt->curattr;
124 rt->line_dirty[rt->crow] = true;
125 rt->curpos_dirty = true;
128 static inline void put_graphmode_char(RoteTerm *rt, int c)
131 /* do some very pitiful translation to regular ascii chars */
133 case 'j': case 'k': case 'l': case 'm': case 'n': case 't':
134 case 'u': case 'v': case 'w':
142 put_normal_char(rt, nc);
145 static inline void new_escape_sequence(RoteTerm *rt)
147 rt->pd->escaped = true;
148 rt->pd->esbuf_len = 0;
149 rt->pd->esbuf[0] = '\0';
152 static inline void cancel_escape_sequence(RoteTerm *rt)
154 rt->pd->escaped = false;
155 rt->pd->esbuf_len = 0;
156 rt->pd->esbuf[0] = '\0';
159 static void handle_control_char(RoteTerm *rt, int c)
162 case '\r': /* carriage return */
166 case '\n': /* line feed */
168 cursor_line_down(rt);
169 rt->curpos_dirty = true;
172 case '\b': /* backspace */
175 rt->curpos_dirty = true;
179 rt->ccol = (rt->ccol + 8) & ~7;
180 clamp_cursor_to_bounds(rt);
183 case '\x1b': /* begin escape sequence (aborting previous one if any) */
184 new_escape_sequence(rt);
187 case '\x0e': /* enter graphical character mode */
188 rt->pd->graphmode = true;
191 case '\x0f': /* exit graphical character mode */
192 rt->pd->graphmode = false;
195 case '\x9b': /* CSI character. Equivalent to ESC [ */
196 new_escape_sequence(rt);
197 rt->pd->esbuf[rt->pd->esbuf_len++] = '[';
201 case '\x1a': /* these interrupt escape sequences */
202 cancel_escape_sequence(rt);
205 case '\a': /* bell */
206 /* do nothing for now... maybe a visual bell would be nice? */
211 static inline bool is_valid_csi_ender(int c)
213 return (c >= 'a' && c <= 'z')
214 || (c >= 'A' && c <= 'Z')
215 || (c == '@' || c == '`');
218 static void try_interpret_escape_seq(RoteTerm *rt)
220 char firstchar = rt->pd->esbuf[0];
221 char lastchar = rt->pd->esbuf[rt->pd->esbuf_len-1];
224 return; /* too early to do anything */
226 /* interpret ESC-M as reverse line-feed */
227 if (firstchar == 'M') {
229 cancel_escape_sequence(rt);
233 if (firstchar != '[' && firstchar != ']') {
234 /* unrecognized escape sequence. Let's forget about it. */
235 cancel_escape_sequence(rt);
239 if (firstchar == '[' && is_valid_csi_ender(lastchar)) {
240 /* we have a csi escape sequence: interpret it */
241 rote_es_interpret_csi(rt);
242 cancel_escape_sequence(rt);
243 } else if (firstchar == ']' && lastchar == '\a') {
244 /* we have an xterm escape sequence: interpret it */
246 /* rote_es_interpret_xterm_es(rt); -- TODO!*/
247 cancel_escape_sequence(rt);
250 /* if the escape sequence took up all available space and could
251 * not yet be parsed, abort it */
252 if (rt->pd->esbuf_len + 1 >= ESEQ_BUF_SIZE)
253 cancel_escape_sequence(rt);
256 void rote_vt_inject(RoteTerm *rt, const char *data, int len)
260 for (i = 0; i < len; i++, data++) {
262 continue; /* completely ignore NUL */
264 if (*data >= 1 && *data <= 31) {
265 handle_control_char(rt, *data);
269 if (rt->pd->escaped && rt->pd->esbuf_len < ESEQ_BUF_SIZE) {
270 /* append character to ongoing escape sequence */
271 rt->pd->esbuf[rt->pd->esbuf_len] = *data;
272 rt->pd->esbuf[++rt->pd->esbuf_len] = 0;
274 try_interpret_escape_seq(rt);
276 if (rt->pd->graphmode) {
277 put_graphmode_char(rt, *data);
279 put_normal_char(rt, *data);
284 /****************************************************************************/
286 /****************************************************************************/
288 /* interprets a 'set attribute' (SGR) CSI escape sequence */
289 static void interpret_csi_SGR(RoteTerm *rt, int param[], int pcount)
294 /* special case: reset attributes */
299 for (i = 0; i < pcount; i++) {
301 // From http://vt100.net/docs/vt510-rm/SGR table 5-16
302 // 0 All attributes off
308 // 10 The ASCII character set is the current 7-bit
309 // display character set (default) - SCO Console only.
310 // 11 Map Hex 00-7F of the PC character set codes
311 // to the current 7-bit display character set
312 // - SCO Console only.
313 // 12 Map Hex 80-FF of the current character set to
314 // the current 7-bit display character set - SCO
319 // 27 Negative image off
320 // 28 Invisible image off
322 if (param[i] == 0) rt->curattr = 0x70;
323 else if (param[i] == 1 || param[i] == 2 || param[i] == 4) /* set bold */
324 ROTE_ATTR_MOD_BOLD(rt->curattr,1);
325 else if (param[i] == 5) /* set blink */
326 ROTE_ATTR_MOD_BLINK(rt->curattr,1);
327 else if (param[i] == 7 || param[i] == 27) { /* reverse video */
328 int fg = ROTE_ATTR_FG(rt->curattr);
329 int bg = ROTE_ATTR_BG(rt->curattr);
330 ROTE_ATTR_MOD_FG(rt->curattr, bg);
331 ROTE_ATTR_MOD_BG(rt->curattr, fg);
333 else if (param[i] == 8) rt->curattr = 0x0; /* invisible */
334 else if (param[i] == 22 || param[i] == 24) /* bold off */
335 ROTE_ATTR_MOD_BOLD(rt->curattr,0);
336 else if (param[i] == 25) /* blink off */
337 ROTE_ATTR_MOD_BLINK(rt->curattr,0);
338 else if (param[i] == 28) /* invisible off */
340 else if (param[i] >= 30 && param[i] <= 37) /* set fg */
341 ROTE_ATTR_MOD_FG(rt->curattr, param[i] - 30);
342 else if (param[i] >= 40 && param[i] <= 47) /* set bg */
343 ROTE_ATTR_MOD_BG(rt->curattr, param[i] - 40);
344 else if (param[i] == 39) /* reset foreground to default */
345 ROTE_ATTR_MOD_FG(rt->curattr, 7);
346 else if (param[i] == 49) /* reset background to default */
347 ROTE_ATTR_MOD_BG(rt->curattr, 0);
351 /* interprets an 'erase display' (ED) escape sequence */
352 static void interpret_csi_ED(RoteTerm *rt, int param[], int pcount)
355 int start_row, start_col, end_row, end_col;
358 if (pcount && param[0] == 2) {
361 end_row = rt->rows - 1;
362 end_col = rt->cols - 1;
364 if (pcount && param[0] == 1) {
370 start_row = rt->crow;
371 start_col = rt->ccol;
372 end_row = rt->rows - 1;
373 end_col = rt->cols - 1;
377 for (r = start_row; r <= end_row; r++) {
378 rt->line_dirty[r] = true;
380 for (c = (r == start_row ? start_col : 0);
381 c <= (r == end_row ? end_col : rt->cols - 1);
384 rt->cells[r][c].ch = 0x20;
385 rt->cells[r][c].attr = rt->curattr;
390 /* interprets a 'move cursor' (CUP) escape sequence */
391 static void interpret_csi_CUP(RoteTerm *rt, int param[], int pcount)
395 rt->crow = rt->ccol = 0;
399 return; /* malformed */
402 rt->crow = param[0] - 1; /* convert from 1-based to 0-based */
403 rt->ccol = param[1] - 1; /* convert from 1-based to 0-based */
405 rt->curpos_dirty = true;
407 clamp_cursor_to_bounds(rt);
410 /* Interpret the 'relative mode' sequences: CUU, CUD, CUF, CUB, CNL,
411 * CPL, CHA, HPR, VPA, VPR, HPA */
412 static void interpret_csi_C(RoteTerm *rt, char verb, int param[], int pcount)
414 int n = (pcount && param[0] > 0) ? param[0] : 1;
417 case 'A': rt->crow -= n; break;
418 case 'B': case 'e': rt->crow += n; break;
419 case 'C': case 'a': rt->ccol += n; break;
420 case 'D': rt->ccol -= n; break;
421 case 'E': rt->crow += n; rt->ccol = 0; break;
422 case 'F': rt->crow -= n; rt->ccol = 0; break;
423 case 'G': case '`': rt->ccol = param[0] - 1; break;
424 case 'd': rt->crow = param[0] - 1; break;
427 rt->curpos_dirty = true;
428 clamp_cursor_to_bounds(rt);
431 /* Interpret the 'erase line' escape sequence */
432 static void interpret_csi_EL(RoteTerm *rt, int param[], int pcount)
434 int erase_start, erase_end, i;
435 int cmd = pcount ? param[0] : 0;
438 case 1: erase_start = 0; erase_end = rt->ccol; break;
439 case 2: erase_start = 0; erase_end = rt->cols - 1; break;
440 default: erase_start = rt->ccol; erase_end = rt->cols - 1; break;
443 for (i = erase_start; i <= erase_end; i++) {
444 rt->cells[rt->crow][i].ch = 0x20;
445 rt->cells[rt->crow][i].attr = rt->curattr;
448 rt->line_dirty[rt->crow] = true;
451 /* Interpret the 'insert blanks' sequence (ICH) */
452 static void interpret_csi_ICH(RoteTerm *rt, int param[], int pcount)
454 int n = (pcount && param[0] > 0) ? param[0] : 1;
457 for (i = rt->cols - 1; i >= rt->ccol + n; i--) {
458 rt->cells[rt->crow][i] = rt->cells[rt->crow][i - n];
461 for (i = rt->ccol; i < rt->ccol + n; i++) {
462 rt->cells[rt->crow][i].ch = 0x20;
463 rt->cells[rt->crow][i].attr = rt->curattr;
466 rt->line_dirty[rt->crow] = true;
469 /* Interpret the 'delete chars' sequence (DCH) */
470 static void interpret_csi_DCH(RoteTerm *rt, int param[], int pcount)
472 int n = (pcount && param[0] > 0) ? param[0] : 1;
475 for (i = rt->ccol; i < rt->cols; i++) {
476 if (i + n < rt->cols) {
477 rt->cells[rt->crow][i] = rt->cells[rt->crow][i + n];
479 rt->cells[rt->crow][i].ch = 0x20;
480 rt->cells[rt->crow][i].attr = rt->curattr;
484 rt->line_dirty[rt->crow] = true;
487 /* Interpret an 'insert line' sequence (IL) */
488 static void interpret_csi_IL(RoteTerm *rt, int param[], int pcount)
490 int n = (pcount && param[0] > 0) ? param[0] : 1;
493 for (i = rt->pd->scrollbottom; i >= rt->crow + n; i--) {
494 memcpy(rt->cells[i], rt->cells[i - n], sizeof(RoteCell) * rt->cols);
497 for (i = rt->crow; i < rt->crow + n && i <= rt->pd->scrollbottom; i++) {
498 rt->line_dirty[i] = true;
499 for (j = 0; j < rt->cols; j++) {
500 rt->cells[i][j].ch = 0x20, rt->cells[i][j].attr = rt->curattr;
506 /* Interpret a 'delete line' sequence (DL) */
507 static void interpret_csi_DL(RoteTerm *rt, int param[], int pcount)
509 int n = (pcount && param[0] > 0) ? param[0] : 1;
512 for (i = rt->crow; i <= rt->pd->scrollbottom; i++) {
513 rt->line_dirty[i] = true;
514 if (i + n <= rt->pd->scrollbottom) {
515 memcpy(rt->cells[i], rt->cells[i+n], sizeof(RoteCell) * rt->cols);
517 for (j = 0; j < rt->cols; j++) {
518 rt->cells[i][j].ch = 0x20, rt->cells[i][j].attr = rt->curattr;
524 /* Interpret an 'erase characters' (ECH) sequence */
525 static void interpret_csi_ECH(RoteTerm *rt, int param[], int pcount)
527 int n = (pcount && param[0] > 0) ? param[0] : 1;
530 for (i = rt->ccol; i < rt->ccol + n && i < rt->cols; i++) {
531 rt->cells[rt->crow][i].ch = 0x20;
532 rt->cells[rt->crow][i].attr = rt->curattr;
535 rt->line_dirty[rt->crow] = true;
538 /* Interpret a 'set scrolling region' (DECSTBM) sequence */
539 static void interpret_csi_DECSTBM(RoteTerm *rt, int param[], int pcount)
541 int newtop, newbottom;
545 newbottom = rt->rows - 1;
548 return; /* malformed */
551 newtop = param[0] - 1;
552 newbottom = param[1] - 1;
554 /* clamp to bounds */
557 if (newtop >= rt->rows)
558 newtop = rt->rows - 1;
561 if (newbottom >= rt->rows)
562 newbottom = rt->rows - 1;
564 /* check for range validity */
565 if (newtop > newbottom)
567 rt->pd->scrolltop = newtop;
568 rt->pd->scrollbottom = newbottom;
571 static void interpret_csi_SAVECUR(RoteTerm *rt, int param[], int pcount)
573 rt->pd->saved_x = rt->ccol;
574 rt->pd->saved_y = rt->crow;
577 static void interpret_csi_RESTORECUR(RoteTerm *rt, int param[], int pcount)
579 rt->ccol = rt->pd->saved_x;
580 rt->crow = rt->pd->saved_y;
581 rt->curpos_dirty = true;
584 void rote_es_interpret_csi(RoteTerm *rt)
586 static int csiparam[MAX_CSI_ES_PARAMS];
588 const char *p = rt->pd->esbuf + 1;
589 char verb = rt->pd->esbuf[rt->pd->esbuf_len - 1];
591 if (!strncmp(rt->pd->esbuf, "[?", 2)) { /* private-mode CSI, ignore */
595 /* parse numeric parameters */
596 while (isdigit((unsigned char)*p) || *p == ';') {
598 if (param_count >= MAX_CSI_ES_PARAMS) return; /* too long! */
599 csiparam[param_count++] = 0;
601 if (param_count == 0) csiparam[param_count++] = 0;
602 csiparam[param_count - 1] *= 10;
603 csiparam[param_count - 1] += *p - '0';
609 /* delegate handling depending on command character (verb) */
612 if (param_count == 1 && csiparam[0] == 4) /* insert mode */
616 if (param_count == 1 && csiparam[0] == 4) /* replace mode */
619 case 'm': /* it's a 'set attribute' sequence */
620 interpret_csi_SGR(rt, csiparam, param_count); break;
621 case 'J': /* it's an 'erase display' sequence */
622 interpret_csi_ED(rt, csiparam, param_count); break;
623 case 'H': case 'f': /* it's a 'move cursor' sequence */
624 interpret_csi_CUP(rt, csiparam, param_count); break;
625 case 'A': case 'B': case 'C': case 'D': case 'E': case 'F': case 'G':
626 case 'e': case 'a': case 'd': case '`':
627 /* it is a 'relative move' */
628 interpret_csi_C(rt, verb, csiparam, param_count); break;
629 case 'K': /* erase line */
630 interpret_csi_EL(rt, csiparam, param_count); break;
631 case '@': /* insert characters */
632 interpret_csi_ICH(rt, csiparam, param_count); break;
633 case 'P': /* delete characters */
634 interpret_csi_DCH(rt, csiparam, param_count); break;
635 case 'L': /* insert lines */
636 interpret_csi_IL(rt, csiparam, param_count); break;
637 case 'M': /* delete lines */
638 interpret_csi_DL(rt, csiparam, param_count); break;
639 case 'X': /* erase chars */
640 interpret_csi_ECH(rt, csiparam, param_count); break;
641 case 'r': /* set scrolling region */
642 interpret_csi_DECSTBM(rt, csiparam, param_count); break;
643 case 's': /* save cursor location */
644 interpret_csi_SAVECUR(rt, csiparam, param_count); break;
645 case 'u': /* restore cursor location */
646 interpret_csi_RESTORECUR(rt, csiparam, param_count); break;