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 "inject_csi.h"
27 #include "roteprivate.h"
29 #define MAX_CSI_ES_PARAMS 32
31 /* interprets a 'set attribute' (SGR) CSI escape sequence */
32 static void interpret_csi_SGR(RoteTerm *rt, int param[], int pcount)
37 /* special case: reset attributes */
42 for (i = 0; i < pcount; i++) {
44 // From http://vt100.net/docs/vt510-rm/SGR table 5-16
45 // 0 All attributes off
51 // 10 The ASCII character set is the current 7-bit
52 // display character set (default) - SCO Console only.
53 // 11 Map Hex 00-7F of the PC character set codes
54 // to the current 7-bit display character set
55 // - SCO Console only.
56 // 12 Map Hex 80-FF of the current character set to
57 // the current 7-bit display character set - SCO
62 // 27 Negative image off
63 // 28 Invisible image off
65 if (param[i] == 0) rt->curattr = 0x70;
66 else if (param[i] == 1 || param[i] == 2 || param[i] == 4) /* set bold */
67 ROTE_ATTR_MOD_BOLD(rt->curattr,1);
68 else if (param[i] == 5) /* set blink */
69 ROTE_ATTR_MOD_BLINK(rt->curattr,1);
70 else if (param[i] == 7 || param[i] == 27) { /* reverse video */
71 int fg = ROTE_ATTR_FG(rt->curattr);
72 int bg = ROTE_ATTR_BG(rt->curattr);
73 ROTE_ATTR_MOD_FG(rt->curattr, bg);
74 ROTE_ATTR_MOD_BG(rt->curattr, fg);
76 else if (param[i] == 8) rt->curattr = 0x0; /* invisible */
77 else if (param[i] == 22 || param[i] == 24) /* bold off */
78 ROTE_ATTR_MOD_BOLD(rt->curattr,0);
79 else if (param[i] == 25) /* blink off */
80 ROTE_ATTR_MOD_BLINK(rt->curattr,0);
81 else if (param[i] == 28) /* invisible off */
83 else if (param[i] >= 30 && param[i] <= 37) /* set fg */
84 ROTE_ATTR_MOD_FG(rt->curattr, param[i] - 30);
85 else if (param[i] >= 40 && param[i] <= 47) /* set bg */
86 ROTE_ATTR_MOD_BG(rt->curattr, param[i] - 40);
87 else if (param[i] == 39) /* reset foreground to default */
88 ROTE_ATTR_MOD_FG(rt->curattr, 7);
89 else if (param[i] == 49) /* reset background to default */
90 ROTE_ATTR_MOD_BG(rt->curattr, 0);
94 /* interprets an 'erase display' (ED) escape sequence */
95 static void interpret_csi_ED(RoteTerm *rt, int param[], int pcount)
98 int start_row, start_col, end_row, end_col;
101 if (pcount && param[0] == 2)
102 start_row = 0, start_col = 0, end_row = rt->rows - 1,
103 end_col = rt->cols - 1;
105 else if (pcount && param[0] == 1)
106 start_row = 0, start_col = 0, end_row = rt->crow,
109 else start_row = rt->crow, start_col = rt->ccol,
110 end_row = rt->rows - 1, end_col = rt->cols - 1;
113 for (r = start_row; r <= end_row; r++) {
114 rt->line_dirty[r] = true;
116 for (c = (r == start_row ? start_col : 0);
117 c <= (r == end_row ? end_col : rt->cols - 1);
119 rt->cells[r][c].ch = 0x20;
120 rt->cells[r][c].attr = rt->curattr;
125 /* interprets a 'move cursor' (CUP) escape sequence */
126 static void interpret_csi_CUP(RoteTerm *rt, int param[], int pcount)
130 rt->crow = rt->ccol = 0;
133 else if (pcount < 2) return; /* malformed */
135 rt->crow = param[0] - 1; /* convert from 1-based to 0-based */
136 rt->ccol = param[1] - 1; /* convert from 1-based to 0-based */
138 rt->curpos_dirty = true;
140 clamp_cursor_to_bounds(rt);
143 /* Interpret the 'relative mode' sequences: CUU, CUD, CUF, CUB, CNL,
144 * CPL, CHA, HPR, VPA, VPR, HPA */
145 static void interpret_csi_C(RoteTerm *rt, char verb,
146 int param[], int pcount) {
147 int n = (pcount && param[0] > 0) ? param[0] : 1;
150 case 'A': rt->crow -= n; break;
151 case 'B': case 'e': rt->crow += n; break;
152 case 'C': case 'a': rt->ccol += n; break;
153 case 'D': rt->ccol -= n; break;
154 case 'E': rt->crow += n; rt->ccol = 0; break;
155 case 'F': rt->crow -= n; rt->ccol = 0; break;
156 case 'G': case '`': rt->ccol = param[0] - 1; break;
157 case 'd': rt->crow = param[0] - 1; break;
160 rt->curpos_dirty = true;
161 clamp_cursor_to_bounds(rt);
164 /* Interpret the 'erase line' escape sequence */
165 static void interpret_csi_EL(RoteTerm *rt, int param[], int pcount)
167 int erase_start, erase_end, i;
168 int cmd = pcount ? param[0] : 0;
171 case 1: erase_start = 0; erase_end = rt->ccol; break;
172 case 2: erase_start = 0; erase_end = rt->cols - 1; break;
173 default: erase_start = rt->ccol; erase_end = rt->cols - 1; break;
176 for (i = erase_start; i <= erase_end; i++) {
177 rt->cells[rt->crow][i].ch = 0x20;
178 rt->cells[rt->crow][i].attr = rt->curattr;
181 rt->line_dirty[rt->crow] = true;
184 /* Interpret the 'insert blanks' sequence (ICH) */
185 static void interpret_csi_ICH(RoteTerm *rt, int param[], int pcount)
187 int n = (pcount && param[0] > 0) ? param[0] : 1;
189 for (i = rt->cols - 1; i >= rt->ccol + n; i--)
190 rt->cells[rt->crow][i] = rt->cells[rt->crow][i - n];
191 for (i = rt->ccol; i < rt->ccol + n; i++) {
192 rt->cells[rt->crow][i].ch = 0x20;
193 rt->cells[rt->crow][i].attr = rt->curattr;
196 rt->line_dirty[rt->crow] = true;
199 /* Interpret the 'delete chars' sequence (DCH) */
200 static void interpret_csi_DCH(RoteTerm *rt, int param[], int pcount)
202 int n = (pcount && param[0] > 0) ? param[0] : 1;
204 for (i = rt->ccol; i < rt->cols; i++) {
205 if (i + n < rt->cols)
206 rt->cells[rt->crow][i] = rt->cells[rt->crow][i + n];
208 rt->cells[rt->crow][i].ch = 0x20;
209 rt->cells[rt->crow][i].attr = rt->curattr;
213 rt->line_dirty[rt->crow] = true;
216 /* Interpret an 'insert line' sequence (IL) */
217 static void interpret_csi_IL(RoteTerm *rt, int param[], int pcount)
219 int n = (pcount && param[0] > 0) ? param[0] : 1;
222 for (i = rt->pd->scrollbottom; i >= rt->crow + n; i--)
223 memcpy(rt->cells[i], rt->cells[i - n], sizeof(RoteCell) * rt->cols);
225 for (i = rt->crow; i < rt->crow + n && i <= rt->pd->scrollbottom; i++) {
226 rt->line_dirty[i] = true;
227 for (j = 0; j < rt->cols; j++)
228 rt->cells[i][j].ch = 0x20, rt->cells[i][j].attr = rt->curattr;
233 /* Interpret a 'delete line' sequence (DL) */
234 static void interpret_csi_DL(RoteTerm *rt, int param[], int pcount)
236 int n = (pcount && param[0] > 0) ? param[0] : 1;
239 for (i = rt->crow; i <= rt->pd->scrollbottom; i++) {
240 rt->line_dirty[i] = true;
241 if (i + n <= rt->pd->scrollbottom)
242 memcpy(rt->cells[i], rt->cells[i+n], sizeof(RoteCell) * rt->cols);
244 for (j = 0; j < rt->cols; j++)
245 rt->cells[i][j].ch = 0x20, rt->cells[i][j].attr = rt->curattr;
250 /* Interpret an 'erase characters' (ECH) sequence */
251 static void interpret_csi_ECH(RoteTerm *rt, int param[], int pcount)
253 int n = (pcount && param[0] > 0) ? param[0] : 1;
256 for (i = rt->ccol; i < rt->ccol + n && i < rt->cols; i++) {
257 rt->cells[rt->crow][i].ch = 0x20;
258 rt->cells[rt->crow][i].attr = rt->curattr;
261 rt->line_dirty[rt->crow] = true;
264 /* Interpret a 'set scrolling region' (DECSTBM) sequence */
265 static void interpret_csi_DECSTBM(RoteTerm *rt, int param[], int pcount)
267 int newtop, newbottom;
271 newbottom = rt->rows - 1;
273 else if (pcount < 2) return; /* malformed */
275 newtop = param[0] - 1;
276 newbottom = param[1] - 1;
279 /* clamp to bounds */
280 if (newtop < 0) newtop = 0;
281 if (newtop >= rt->rows) newtop = rt->rows - 1;
282 if (newbottom < 0) newbottom = 0;
283 if (newbottom >= rt->rows) newbottom = rt->rows - 1;
285 /* check for range validity */
286 if (newtop > newbottom) return;
287 rt->pd->scrolltop = newtop;
288 rt->pd->scrollbottom = newbottom;
291 static void interpret_csi_SAVECUR(RoteTerm *rt, int param[], int pcount)
293 rt->pd->saved_x = rt->ccol;
294 rt->pd->saved_y = rt->crow;
297 static void interpret_csi_RESTORECUR(RoteTerm *rt, int param[], int pcount)
299 rt->ccol = rt->pd->saved_x;
300 rt->crow = rt->pd->saved_y;
301 rt->curpos_dirty = true;
304 void rote_es_interpret_csi(RoteTerm *rt)
306 static int csiparam[MAX_CSI_ES_PARAMS];
308 const char *p = rt->pd->esbuf + 1;
309 char verb = rt->pd->esbuf[rt->pd->esbuf_len - 1];
311 if (!strncmp(rt->pd->esbuf, "[?", 2)) { /* private-mode CSI, ignore */
313 fprintf(stderr, "Ignoring private-mode CSI: <%s>\n", rt->pd->esbuf);
318 /* parse numeric parameters */
319 while ((*p >= '0' && *p <= '9') || *p == ';') {
321 if (param_count >= MAX_CSI_ES_PARAMS) return; /* too long! */
322 csiparam[param_count++] = 0;
325 if (param_count == 0) csiparam[param_count++] = 0;
326 csiparam[param_count - 1] *= 10;
327 csiparam[param_count - 1] += *p - '0';
333 /* delegate handling depending on command character (verb) */
336 if (param_count == 1 && csiparam[0] == 4) /* insert mode */
340 if (param_count == 1 && csiparam[0] == 4) /* replace mode */
343 case 'm': /* it's a 'set attribute' sequence */
344 interpret_csi_SGR(rt, csiparam, param_count); break;
345 case 'J': /* it's an 'erase display' sequence */
346 interpret_csi_ED(rt, csiparam, param_count); break;
347 case 'H': case 'f': /* it's a 'move cursor' sequence */
348 interpret_csi_CUP(rt, csiparam, param_count); break;
349 case 'A': case 'B': case 'C': case 'D': case 'E': case 'F': case 'G':
350 case 'e': case 'a': case 'd': case '`':
351 /* it is a 'relative move' */
352 interpret_csi_C(rt, verb, csiparam, param_count); break;
353 case 'K': /* erase line */
354 interpret_csi_EL(rt, csiparam, param_count); break;
355 case '@': /* insert characters */
356 interpret_csi_ICH(rt, csiparam, param_count); break;
357 case 'P': /* delete characters */
358 interpret_csi_DCH(rt, csiparam, param_count); break;
359 case 'L': /* insert lines */
360 interpret_csi_IL(rt, csiparam, param_count); break;
361 case 'M': /* delete lines */
362 interpret_csi_DL(rt, csiparam, param_count); break;
363 case 'X': /* erase chars */
364 interpret_csi_ECH(rt, csiparam, param_count); break;
365 case 'r': /* set scrolling region */
366 interpret_csi_DECSTBM(rt, csiparam, param_count); break;
367 case 's': /* save cursor location */
368 interpret_csi_SAVECUR(rt, csiparam, param_count); break;
369 case 'u': /* restore cursor location */
370 interpret_csi_RESTORECUR(rt, csiparam, param_count); break;
373 fprintf(stderr, "Unrecogized CSI: verb=%c <%s>\n",
374 verb, rt->pd->esbuf);