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 (c) 2004 Bruno T. C. de Oliveira
22 #include "inject_csi.h"
23 #include "roteprivate.h"
27 #define MAX_CSI_ES_PARAMS 32
29 static inline void clamp_cursor_to_bounds(RoteTerm *rt) {
30 if (rt->crow < 0) rt->curpos_dirty = true, rt->crow = 0;
31 if (rt->ccol < 0) rt->curpos_dirty = true, rt->ccol = 0;
33 if (rt->crow >= rt->rows)
34 rt->curpos_dirty = true, rt->crow = rt->rows - 1;
36 if (rt->ccol >= rt->cols)
37 rt->curpos_dirty = true, rt->ccol = rt->cols - 1;
40 /* interprets a 'set attribute' (SGR) CSI escape sequence */
41 static void interpret_csi_SGR(RoteTerm *rt, int param[], int pcount) {
45 /* special case: reset attributes */
50 for (i = 0; i < pcount; i++) {
51 if (param[i] == 0) rt->curattr = 0x70;
52 else if (param[i] == 1 || param[i] == 2 || param[i] == 4) /* set bold */
54 else if (param[i] == 5) rt->curattr |= 0x08; /* set blink */
55 else if (param[i] == 7) rt->curattr = 0x07; /* reverse video */
56 else if (param[i] >= 30 && param[i] <= 37) /* set fg */
57 ROTE_ATTR_MOD_FG(rt->curattr, param[i] - 30);
58 else if (param[i] >= 40 && param[i] <= 47) /* set bg */
59 ROTE_ATTR_MOD_BG(rt->curattr, param[i] - 40);
60 else if (param[i] == 39) /* reset foreground to default */
61 ROTE_ATTR_MOD_FG(rt->curattr, 7);
62 else if (param[i] == 49) /* reset background to default */
63 ROTE_ATTR_MOD_BG(rt->curattr, 0);
67 /* interprets an 'erase display' (ED) escape sequence */
68 static void interpret_csi_ED(RoteTerm *rt, int param[], int pcount) {
70 int start_row, start_col, end_row, end_col;
73 if (pcount && param[0] == 2)
74 start_row = 0, start_col = 0, end_row = rt->rows - 1,
75 end_col = rt->cols - 1;
77 else if (pcount && param[0] == 1)
78 start_row = 0, start_col = 0, end_row = rt->crow,
81 else start_row = rt->crow, start_col = rt->ccol,
82 end_row = rt->rows - 1, end_col = rt->cols - 1;
85 for (r = start_row; r <= end_row; r++) {
86 rt->line_dirty[r] = true;
88 for (c = (r == start_row ? start_col : 0);
89 c <= (r == end_row ? end_col : rt->cols - 1);
91 rt->cells[r][c].ch = 0x20;
92 rt->cells[r][c].attr = rt->curattr;
97 /* interprets a 'move cursor' (CUP) escape sequence */
98 static void interpret_csi_CUP(RoteTerm *rt, int param[], int pcount) {
101 rt->crow = rt->ccol = 0;
104 else if (pcount < 2) return; /* malformed */
106 rt->crow = param[0] - 1; /* convert from 1-based to 0-based */
107 rt->ccol = param[1] - 1; /* convert from 1-based to 0-based */
109 rt->curpos_dirty = true;
111 clamp_cursor_to_bounds(rt);
114 /* Interpret the 'relative mode' sequences: CUU, CUD, CUF, CUB, CNL,
115 * CPL, CHA, HPR, VPA, VPR, HPA */
116 static void interpret_csi_C(RoteTerm *rt, char verb,
117 int param[], int pcount) {
118 int n = (pcount && param[0] > 0) ? param[0] : 1;
121 case 'A': rt->crow -= n; break;
122 case 'B': case 'e': rt->crow += n; break;
123 case 'C': case 'a': rt->ccol += n; break;
124 case 'D': rt->ccol -= n; break;
125 case 'E': rt->crow += n; rt->ccol = 0; break;
126 case 'F': rt->crow -= n; rt->ccol = 0; break;
127 case 'G': case '`': rt->ccol = param[0] - 1; break;
128 case 'd': rt->crow = param[0] - 1; break;
131 rt->curpos_dirty = true;
132 clamp_cursor_to_bounds(rt);
135 /* Interpret the 'erase line' escape sequence */
136 static void interpret_csi_EL(RoteTerm *rt, int param[], int pcount) {
137 int erase_start, erase_end, i;
138 int cmd = pcount ? param[0] : 0;
141 case 1: erase_start = 0; erase_end = rt->ccol; break;
142 case 2: erase_start = 0; erase_end = rt->cols - 1; break;
143 default: erase_start = rt->ccol; erase_end = rt->cols - 1; break;
146 for (i = erase_start; i <= erase_end; i++) {
147 rt->cells[rt->crow][i].ch = 0x20;
148 rt->cells[rt->crow][i].attr = rt->curattr;
151 rt->line_dirty[rt->crow] = true;
154 /* Interpret the 'insert blanks' sequence (ICH) */
155 static void interpret_csi_ICH(RoteTerm *rt, int param[], int pcount) {
156 int n = (pcount && param[0] > 0) ? param[0] : 1;
158 for (i = rt->cols - 1; i >= rt->ccol + n; i--)
159 rt->cells[rt->crow][i] = rt->cells[rt->crow][i - n];
160 for (i = rt->ccol; i < rt->ccol + n; i++) {
161 rt->cells[rt->crow][i].ch = 0x20;
162 rt->cells[rt->crow][i].attr = rt->curattr;
165 rt->line_dirty[rt->crow] = true;
168 /* Interpret the 'delete chars' sequence (DCH) */
169 static void interpret_csi_DCH(RoteTerm *rt, int param[], int pcount) {
170 int n = (pcount && param[0] > 0) ? param[0] : 1;
172 for (i = rt->ccol; i < rt->cols; i++) {
173 if (i + n < rt->cols)
174 rt->cells[rt->crow][i] = rt->cells[rt->crow][i + n];
176 rt->cells[rt->crow][i].ch = 0x20;
177 rt->cells[rt->crow][i].attr = rt->curattr;
181 rt->line_dirty[rt->crow] = true;
184 /* Interpret an 'insert line' sequence (IL) */
185 static void interpret_csi_IL(RoteTerm *rt, int param[], int pcount) {
186 int n = (pcount && param[0] > 0) ? param[0] : 1;
189 for (i = rt->pd->scrollbottom; i >= rt->crow + n; i--)
190 memcpy(rt->cells[i], rt->cells[i - n], sizeof(RoteCell) * rt->cols);
192 for (i = rt->crow; i < rt->crow + n && i <= rt->pd->scrollbottom; i++) {
193 rt->line_dirty[i] = true;
194 for (j = 0; j < rt->cols; j++)
195 rt->cells[i][j].ch = 0x20, rt->cells[i][j].attr = rt->curattr;
200 /* Interpret a 'delete line' sequence (DL) */
201 static void interpret_csi_DL(RoteTerm *rt, int param[], int pcount) {
202 int n = (pcount && param[0] > 0) ? param[0] : 1;
205 for (i = rt->crow; i <= rt->pd->scrollbottom; i++) {
206 rt->line_dirty[i] = true;
207 if (i + n <= rt->pd->scrollbottom)
208 memcpy(rt->cells[i], rt->cells[i+n], sizeof(RoteCell) * rt->cols);
210 for (j = 0; j < rt->cols; j++)
211 rt->cells[i][j].ch = 0x20, rt->cells[i][j].attr = rt->curattr;
216 /* Interpret an 'erase characters' (ECH) sequence */
217 static void interpret_csi_ECH(RoteTerm *rt, int param[], int pcount) {
218 int n = (pcount && param[0] > 0) ? param[0] : 1;
221 for (i = rt->ccol; i < rt->ccol + n && i < rt->cols; i++) {
222 rt->cells[rt->crow][i].ch = 0x20;
223 rt->cells[rt->crow][i].attr = rt->curattr;
226 rt->line_dirty[rt->crow] = true;
229 /* Interpret a 'set scrolling region' (DECSTBM) sequence */
230 static void interpret_csi_DECSTBM(RoteTerm *rt, int param[], int pcount) {
231 int newtop, newbottom;
235 newbottom = rt->rows - 1;
237 else if (pcount < 2) return; /* malformed */
239 newtop = param[0] - 1;
240 newbottom = param[1] - 1;
243 /* clamp to bounds */
244 if (newtop < 0) newtop = 0;
245 if (newtop >= rt->rows) newtop = rt->rows - 1;
246 if (newbottom < 0) newbottom = 0;
247 if (newbottom >= rt->rows) newbottom = rt->rows - 1;
249 /* check for range validity */
250 if (newtop > newbottom) return;
251 rt->pd->scrolltop = newtop;
252 rt->pd->scrollbottom = newbottom;
255 static void interpret_csi_SAVECUR(RoteTerm *rt, int param[], int pcount) {
256 rt->pd->saved_x = rt->ccol;
257 rt->pd->saved_y = rt->crow;
260 static void interpret_csi_RESTORECUR(RoteTerm *rt, int param[], int pcount) {
261 rt->ccol = rt->pd->saved_x;
262 rt->crow = rt->pd->saved_y;
263 rt->curpos_dirty = true;
266 void rote_es_interpret_csi(RoteTerm *rt) {
267 static int csiparam[MAX_CSI_ES_PARAMS];
269 const char *p = rt->pd->esbuf + 1;
270 char verb = rt->pd->esbuf[rt->pd->esbuf_len - 1];
272 if (!strncmp(rt->pd->esbuf, "[?", 2)) { /* private-mode CSI, ignore */
274 fprintf(stderr, "Ignoring private-mode CSI: <%s>\n", rt->pd->esbuf);
279 /* parse numeric parameters */
280 while ((*p >= '0' && *p <= '9') || *p == ';') {
282 if (param_count >= MAX_CSI_ES_PARAMS) return; /* too long! */
283 csiparam[param_count++] = 0;
286 if (param_count == 0) csiparam[param_count++] = 0;
287 csiparam[param_count - 1] *= 10;
288 csiparam[param_count - 1] += *p - '0';
294 /* delegate handling depending on command character (verb) */
296 case 'm': /* it's a 'set attribute' sequence */
297 interpret_csi_SGR(rt, csiparam, param_count); break;
298 case 'J': /* it's an 'erase display' sequence */
299 interpret_csi_ED(rt, csiparam, param_count); break;
300 case 'H': case 'f': /* it's a 'move cursor' sequence */
301 interpret_csi_CUP(rt, csiparam, param_count); break;
302 case 'A': case 'B': case 'C': case 'D': case 'E': case 'F': case 'G':
303 case 'e': case 'a': case 'd': case '`':
304 /* it is a 'relative move' */
305 interpret_csi_C(rt, verb, csiparam, param_count); break;
306 case 'K': /* erase line */
307 interpret_csi_EL(rt, csiparam, param_count); break;
308 case '@': /* insert characters */
309 interpret_csi_ICH(rt, csiparam, param_count); break;
310 case 'P': /* delete characters */
311 interpret_csi_DCH(rt, csiparam, param_count); break;
312 case 'L': /* insert lines */
313 interpret_csi_IL(rt, csiparam, param_count); break;
314 case 'M': /* delete lines */
315 interpret_csi_DL(rt, csiparam, param_count); break;
316 case 'X': /* erase chars */
317 interpret_csi_ECH(rt, csiparam, param_count); break;
318 case 'r': /* set scrolling region */
319 interpret_csi_DECSTBM(rt, csiparam, param_count); break;
320 case 's': /* save cursor location */
321 interpret_csi_SAVECUR(rt, csiparam, param_count); break;
322 case 'u': /* restore cursor location */
323 interpret_csi_RESTORECUR(rt, csiparam, param_count); break;
326 fprintf(stderr, "Unrecogized CSI: <%s>\n", rt->pd->esbuf); break;