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
26 #include "roteprivate.h"
28 #define MAX_CSI_ES_PARAMS 32
30 static inline void clamp_cursor_to_bounds(RoteTerm *rt)
32 if (rt->crow < 0) rt->curpos_dirty = true, rt->crow = 0;
33 if (rt->ccol < 0) rt->curpos_dirty = true, rt->ccol = 0;
35 if (rt->crow >= rt->rows)
36 rt->curpos_dirty = true, rt->crow = rt->rows - 1;
38 if (rt->ccol >= rt->cols)
39 rt->curpos_dirty = true, rt->ccol = rt->cols - 1;
42 static void cursor_line_down(RoteTerm *rt)
46 rt->curpos_dirty = true;
47 if (rt->crow <= rt->pd->scrollbottom) return;
49 /* must scroll the scrolling region up by 1 line, and put cursor on
51 rt->crow = rt->pd->scrollbottom;
53 for (i = rt->pd->scrolltop; i < rt->pd->scrollbottom; i++) {
54 rt->line_dirty[i] = true;
55 memcpy(rt->cells[i], rt->cells[i+1], sizeof(RoteCell) * rt->cols);
58 rt->line_dirty[rt->pd->scrollbottom] = true;
60 /* clear last row of the scrolling region */
61 for (i = 0; i < rt->cols; i++) {
62 rt->cells[rt->pd->scrollbottom][i].ch = 0x20;
63 rt->cells[rt->pd->scrollbottom][i].attr = 0x70;
68 static void cursor_line_up(RoteTerm *rt)
72 rt->curpos_dirty = true;
73 if (rt->crow >= rt->pd->scrolltop) return;
75 /* must scroll the scrolling region up by 1 line, and put cursor on
77 rt->crow = rt->pd->scrolltop;
79 for (i = rt->pd->scrollbottom; i > rt->pd->scrolltop; i--) {
80 rt->line_dirty[i] = true;
81 memcpy(rt->cells[i], rt->cells[i-1], sizeof(RoteCell) * rt->cols);
84 rt->line_dirty[rt->pd->scrolltop] = true;
86 /* clear first row of the scrolling region */
87 for (i = 0; i < rt->cols; i++) {
88 rt->cells[rt->pd->scrolltop][i].ch = 0x20;
89 rt->cells[rt->pd->scrolltop][i].attr = 0x70;
94 static inline void put_normal_char(RoteTerm *rt, char c)
96 if (rt->ccol >= rt->cols) {
104 for(i = rt->cols - 1; i >= rt->ccol+1; i--)
105 rt->cells[rt->crow][i] = rt->cells[rt->crow][i-1];
108 rt->cells[rt->crow][rt->ccol].ch = c;
109 rt->cells[rt->crow][rt->ccol].attr = rt->curattr;
112 rt->line_dirty[rt->crow] = true;
113 rt->curpos_dirty = true;
116 static inline void put_graphmode_char(RoteTerm *rt, char c)
119 /* do some very pitiful translation to regular ascii chars */
121 case 'j': case 'k': case 'l': case 'm': case 'n': case 't':
122 case 'u': case 'v': case 'w':
130 put_normal_char(rt, nc);
133 static inline void new_escape_sequence(RoteTerm *rt)
135 rt->pd->escaped = true;
136 rt->pd->esbuf_len = 0;
137 rt->pd->esbuf[0] = '\0';
140 static inline void cancel_escape_sequence(RoteTerm *rt)
142 rt->pd->escaped = false;
143 rt->pd->esbuf_len = 0;
144 rt->pd->esbuf[0] = '\0';
147 static void handle_control_char(RoteTerm *rt, char c)
150 case '\r': rt->ccol = 0; break; /* carriage return */
151 case '\n': /* line feed */
152 rt->ccol = 0; cursor_line_down(rt);
153 rt->curpos_dirty = true;
155 case '\b': /* backspace */
156 if (rt->ccol > 0) rt->ccol--;
157 rt->curpos_dirty = true;
160 rt->ccol += 8 - (rt->ccol % 8);
161 clamp_cursor_to_bounds(rt);
163 case '\x1B': /* begin escape sequence (aborting previous one if any) */
164 new_escape_sequence(rt);
166 case '\x0E': /* enter graphical character mode */
167 rt->pd->graphmode = true;
169 case '\x0F': /* exit graphical character mode */
170 rt->pd->graphmode = false;
172 case '\x9B': /* CSI character. Equivalent to ESC [ */
173 new_escape_sequence(rt);
174 rt->pd->esbuf[rt->pd->esbuf_len++] = '[';
176 case '\x18': case '\x1A': /* these interrupt escape sequences */
177 cancel_escape_sequence(rt);
179 case '\a': /* bell */
180 /* do nothing for now... maybe a visual bell would be nice? */
184 fprintf(stderr, "Unrecognized control char: %d (^%c)\n", c, c + '@');
190 static inline bool is_valid_csi_ender(char c)
192 return (c >= 'a' && c <= 'z') ||
193 (c >= 'A' && c <= 'Z') ||
194 c == '@' || c == '`';
197 static void try_interpret_escape_seq(RoteTerm *rt)
199 char firstchar = rt->pd->esbuf[0];
200 char lastchar = rt->pd->esbuf[rt->pd->esbuf_len-1];
202 if (!firstchar) return; /* too early to do anything */
204 if (rt->pd->handler) {
205 /* call custom handler */
207 fprintf(stderr, "Calling custom handler for ES <%s>.\n", rt->pd->esbuf);
210 int answer = (*(rt->pd->handler))(rt, rt->pd->esbuf);
211 if (answer == ROTE_HANDLERESULT_OK) {
212 /* successfully handled */
214 fprintf(stderr, "Handler returned OK. Done with escape sequence.\n");
217 cancel_escape_sequence(rt);
220 else if (answer == ROTE_HANDLERESULT_NOTYET) {
221 /* handler might handle it when more characters are appended to
222 * it. So for now we don't interpret it */
224 fprintf(stderr, "Handler returned NOTYET. Waiting for more chars.\n");
230 /* If we got here then answer == ROTE_HANDLERESULT_NOWAY */
231 /* handler said it can't handle that escape sequence,
232 * but we can still try handling it ourselves, so
233 * we proceed normally. */
235 fprintf(stderr, "Handler returned NOWAY. Trying our handlers.\n");
239 /* interpret ESC-M as reverse line-feed */
240 if (firstchar == 'M') {
242 cancel_escape_sequence(rt);
246 if (firstchar != '[' && firstchar != ']') {
247 /* unrecognized escape sequence. Let's forget about it. */
249 fprintf(stderr, "Unrecognized ES: <%s>\n", rt->pd->esbuf);
252 cancel_escape_sequence(rt);
256 if (firstchar == '[' && is_valid_csi_ender(lastchar)) {
257 /* we have a csi escape sequence: interpret it */
258 rote_es_interpret_csi(rt);
259 cancel_escape_sequence(rt);
261 else if (firstchar == ']' && lastchar == '\a') {
262 /* we have an xterm escape sequence: interpret it */
264 /* rote_es_interpret_xterm_es(rt); -- TODO!*/
266 fprintf(stderr, "Ignored XTerm ES.\n");
268 cancel_escape_sequence(rt);
271 /* if the escape sequence took up all available space and could
272 * not yet be parsed, abort it */
273 if (rt->pd->esbuf_len + 1 >= ESEQ_BUF_SIZE) cancel_escape_sequence(rt);
276 void rote_vt_inject(RoteTerm *rt, const char *data, int len)
279 for (i = 0; i < len; i++, data++) {
280 if (*data == 0) continue; /* completely ignore NUL */
281 if (*data >= 1 && *data <= 31) {
282 handle_control_char(rt, *data);
286 if (rt->pd->escaped && rt->pd->esbuf_len < ESEQ_BUF_SIZE) {
287 /* append character to ongoing escape sequence */
288 rt->pd->esbuf[rt->pd->esbuf_len] = *data;
289 rt->pd->esbuf[++rt->pd->esbuf_len] = 0;
291 try_interpret_escape_seq(rt);
293 else if (rt->pd->graphmode)
294 put_graphmode_char(rt, *data);
296 put_normal_char(rt, *data);
300 /****************************************************************************/
302 /****************************************************************************/
304 /* interprets a 'set attribute' (SGR) CSI escape sequence */
305 static void interpret_csi_SGR(RoteTerm *rt, int param[], int pcount)
310 /* special case: reset attributes */
315 for (i = 0; i < pcount; i++) {
317 // From http://vt100.net/docs/vt510-rm/SGR table 5-16
318 // 0 All attributes off
324 // 10 The ASCII character set is the current 7-bit
325 // display character set (default) - SCO Console only.
326 // 11 Map Hex 00-7F of the PC character set codes
327 // to the current 7-bit display character set
328 // - SCO Console only.
329 // 12 Map Hex 80-FF of the current character set to
330 // the current 7-bit display character set - SCO
335 // 27 Negative image off
336 // 28 Invisible image off
338 if (param[i] == 0) rt->curattr = 0x70;
339 else if (param[i] == 1 || param[i] == 2 || param[i] == 4) /* set bold */
340 ROTE_ATTR_MOD_BOLD(rt->curattr,1);
341 else if (param[i] == 5) /* set blink */
342 ROTE_ATTR_MOD_BLINK(rt->curattr,1);
343 else if (param[i] == 7 || param[i] == 27) { /* reverse video */
344 int fg = ROTE_ATTR_FG(rt->curattr);
345 int bg = ROTE_ATTR_BG(rt->curattr);
346 ROTE_ATTR_MOD_FG(rt->curattr, bg);
347 ROTE_ATTR_MOD_BG(rt->curattr, fg);
349 else if (param[i] == 8) rt->curattr = 0x0; /* invisible */
350 else if (param[i] == 22 || param[i] == 24) /* bold off */
351 ROTE_ATTR_MOD_BOLD(rt->curattr,0);
352 else if (param[i] == 25) /* blink off */
353 ROTE_ATTR_MOD_BLINK(rt->curattr,0);
354 else if (param[i] == 28) /* invisible off */
356 else if (param[i] >= 30 && param[i] <= 37) /* set fg */
357 ROTE_ATTR_MOD_FG(rt->curattr, param[i] - 30);
358 else if (param[i] >= 40 && param[i] <= 47) /* set bg */
359 ROTE_ATTR_MOD_BG(rt->curattr, param[i] - 40);
360 else if (param[i] == 39) /* reset foreground to default */
361 ROTE_ATTR_MOD_FG(rt->curattr, 7);
362 else if (param[i] == 49) /* reset background to default */
363 ROTE_ATTR_MOD_BG(rt->curattr, 0);
367 /* interprets an 'erase display' (ED) escape sequence */
368 static void interpret_csi_ED(RoteTerm *rt, int param[], int pcount)
371 int start_row, start_col, end_row, end_col;
374 if (pcount && param[0] == 2)
375 start_row = 0, start_col = 0, end_row = rt->rows - 1,
376 end_col = rt->cols - 1;
378 else if (pcount && param[0] == 1)
379 start_row = 0, start_col = 0, end_row = rt->crow,
382 else start_row = rt->crow, start_col = rt->ccol,
383 end_row = rt->rows - 1, end_col = rt->cols - 1;
386 for (r = start_row; r <= end_row; r++) {
387 rt->line_dirty[r] = true;
389 for (c = (r == start_row ? start_col : 0);
390 c <= (r == end_row ? end_col : rt->cols - 1);
392 rt->cells[r][c].ch = 0x20;
393 rt->cells[r][c].attr = rt->curattr;
398 /* interprets a 'move cursor' (CUP) escape sequence */
399 static void interpret_csi_CUP(RoteTerm *rt, int param[], int pcount)
403 rt->crow = rt->ccol = 0;
406 else if (pcount < 2) return; /* malformed */
408 rt->crow = param[0] - 1; /* convert from 1-based to 0-based */
409 rt->ccol = param[1] - 1; /* convert from 1-based to 0-based */
411 rt->curpos_dirty = true;
413 clamp_cursor_to_bounds(rt);
416 /* Interpret the 'relative mode' sequences: CUU, CUD, CUF, CUB, CNL,
417 * CPL, CHA, HPR, VPA, VPR, HPA */
418 static void interpret_csi_C(RoteTerm *rt, char verb,
419 int param[], int pcount) {
420 int n = (pcount && param[0] > 0) ? param[0] : 1;
423 case 'A': rt->crow -= n; break;
424 case 'B': case 'e': rt->crow += n; break;
425 case 'C': case 'a': rt->ccol += n; break;
426 case 'D': rt->ccol -= n; break;
427 case 'E': rt->crow += n; rt->ccol = 0; break;
428 case 'F': rt->crow -= n; rt->ccol = 0; break;
429 case 'G': case '`': rt->ccol = param[0] - 1; break;
430 case 'd': rt->crow = param[0] - 1; break;
433 rt->curpos_dirty = true;
434 clamp_cursor_to_bounds(rt);
437 /* Interpret the 'erase line' escape sequence */
438 static void interpret_csi_EL(RoteTerm *rt, int param[], int pcount)
440 int erase_start, erase_end, i;
441 int cmd = pcount ? param[0] : 0;
444 case 1: erase_start = 0; erase_end = rt->ccol; break;
445 case 2: erase_start = 0; erase_end = rt->cols - 1; break;
446 default: erase_start = rt->ccol; erase_end = rt->cols - 1; break;
449 for (i = erase_start; i <= erase_end; i++) {
450 rt->cells[rt->crow][i].ch = 0x20;
451 rt->cells[rt->crow][i].attr = rt->curattr;
454 rt->line_dirty[rt->crow] = true;
457 /* Interpret the 'insert blanks' sequence (ICH) */
458 static void interpret_csi_ICH(RoteTerm *rt, int param[], int pcount)
460 int n = (pcount && param[0] > 0) ? param[0] : 1;
462 for (i = rt->cols - 1; i >= rt->ccol + n; i--)
463 rt->cells[rt->crow][i] = rt->cells[rt->crow][i - n];
464 for (i = rt->ccol; i < rt->ccol + n; i++) {
465 rt->cells[rt->crow][i].ch = 0x20;
466 rt->cells[rt->crow][i].attr = rt->curattr;
469 rt->line_dirty[rt->crow] = true;
472 /* Interpret the 'delete chars' sequence (DCH) */
473 static void interpret_csi_DCH(RoteTerm *rt, int param[], int pcount)
475 int n = (pcount && param[0] > 0) ? param[0] : 1;
477 for (i = rt->ccol; i < rt->cols; i++) {
478 if (i + n < rt->cols)
479 rt->cells[rt->crow][i] = rt->cells[rt->crow][i + n];
481 rt->cells[rt->crow][i].ch = 0x20;
482 rt->cells[rt->crow][i].attr = rt->curattr;
486 rt->line_dirty[rt->crow] = true;
489 /* Interpret an 'insert line' sequence (IL) */
490 static void interpret_csi_IL(RoteTerm *rt, int param[], int pcount)
492 int n = (pcount && param[0] > 0) ? param[0] : 1;
495 for (i = rt->pd->scrollbottom; i >= rt->crow + n; i--)
496 memcpy(rt->cells[i], rt->cells[i - n], sizeof(RoteCell) * rt->cols);
498 for (i = rt->crow; i < rt->crow + n && i <= rt->pd->scrollbottom; i++) {
499 rt->line_dirty[i] = true;
500 for (j = 0; j < rt->cols; j++)
501 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;
523 /* Interpret an 'erase characters' (ECH) sequence */
524 static void interpret_csi_ECH(RoteTerm *rt, int param[], int pcount)
526 int n = (pcount && param[0] > 0) ? param[0] : 1;
529 for (i = rt->ccol; i < rt->ccol + n && i < rt->cols; i++) {
530 rt->cells[rt->crow][i].ch = 0x20;
531 rt->cells[rt->crow][i].attr = rt->curattr;
534 rt->line_dirty[rt->crow] = true;
537 /* Interpret a 'set scrolling region' (DECSTBM) sequence */
538 static void interpret_csi_DECSTBM(RoteTerm *rt, int param[], int pcount)
540 int newtop, newbottom;
544 newbottom = rt->rows - 1;
546 else if (pcount < 2) return; /* malformed */
548 newtop = param[0] - 1;
549 newbottom = param[1] - 1;
552 /* clamp to bounds */
553 if (newtop < 0) newtop = 0;
554 if (newtop >= rt->rows) newtop = rt->rows - 1;
555 if (newbottom < 0) newbottom = 0;
556 if (newbottom >= rt->rows) newbottom = rt->rows - 1;
558 /* check for range validity */
559 if (newtop > newbottom) return;
560 rt->pd->scrolltop = newtop;
561 rt->pd->scrollbottom = newbottom;
564 static void interpret_csi_SAVECUR(RoteTerm *rt, int param[], int pcount)
566 rt->pd->saved_x = rt->ccol;
567 rt->pd->saved_y = rt->crow;
570 static void interpret_csi_RESTORECUR(RoteTerm *rt, int param[], int pcount)
572 rt->ccol = rt->pd->saved_x;
573 rt->crow = rt->pd->saved_y;
574 rt->curpos_dirty = true;
577 void rote_es_interpret_csi(RoteTerm *rt)
579 static int csiparam[MAX_CSI_ES_PARAMS];
581 const char *p = rt->pd->esbuf + 1;
582 char verb = rt->pd->esbuf[rt->pd->esbuf_len - 1];
584 if (!strncmp(rt->pd->esbuf, "[?", 2)) { /* private-mode CSI, ignore */
586 fprintf(stderr, "Ignoring private-mode CSI: <%s>\n", rt->pd->esbuf);
591 /* parse numeric parameters */
592 while ((*p >= '0' && *p <= '9') || *p == ';') {
594 if (param_count >= MAX_CSI_ES_PARAMS) return; /* too long! */
595 csiparam[param_count++] = 0;
598 if (param_count == 0) csiparam[param_count++] = 0;
599 csiparam[param_count - 1] *= 10;
600 csiparam[param_count - 1] += *p - '0';
606 /* delegate handling depending on command character (verb) */
609 if (param_count == 1 && csiparam[0] == 4) /* insert mode */
613 if (param_count == 1 && csiparam[0] == 4) /* replace mode */
616 case 'm': /* it's a 'set attribute' sequence */
617 interpret_csi_SGR(rt, csiparam, param_count); break;
618 case 'J': /* it's an 'erase display' sequence */
619 interpret_csi_ED(rt, csiparam, param_count); break;
620 case 'H': case 'f': /* it's a 'move cursor' sequence */
621 interpret_csi_CUP(rt, csiparam, param_count); break;
622 case 'A': case 'B': case 'C': case 'D': case 'E': case 'F': case 'G':
623 case 'e': case 'a': case 'd': case '`':
624 /* it is a 'relative move' */
625 interpret_csi_C(rt, verb, csiparam, param_count); break;
626 case 'K': /* erase line */
627 interpret_csi_EL(rt, csiparam, param_count); break;
628 case '@': /* insert characters */
629 interpret_csi_ICH(rt, csiparam, param_count); break;
630 case 'P': /* delete characters */
631 interpret_csi_DCH(rt, csiparam, param_count); break;
632 case 'L': /* insert lines */
633 interpret_csi_IL(rt, csiparam, param_count); break;
634 case 'M': /* delete lines */
635 interpret_csi_DL(rt, csiparam, param_count); break;
636 case 'X': /* erase chars */
637 interpret_csi_ECH(rt, csiparam, param_count); break;
638 case 'r': /* set scrolling region */
639 interpret_csi_DECSTBM(rt, csiparam, param_count); break;
640 case 's': /* save cursor location */
641 interpret_csi_SAVECUR(rt, csiparam, param_count); break;
642 case 'u': /* restore cursor location */
643 interpret_csi_RESTORECUR(rt, csiparam, param_count); break;
646 fprintf(stderr, "Unrecogized CSI: verb=%c <%s>\n",
647 verb, rt->pd->esbuf);