rename the project into madtty.
[apps/madtty.git] / inject_csi.c
1 /*
2 LICENSE INFORMATION:
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.
6
7 Please refer to the COPYING file for more information.
8
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.
13
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
17
18 Copyright (c) 2004 Bruno T. C. de Oliveira
19 */
20
21
22 #include "inject_csi.h"
23 #include "roteprivate.h"
24 #include <stdlib.h>
25 #include <string.h>
26 #include <stdio.h>
27
28 #define MAX_CSI_ES_PARAMS 32
29    
30 /* interprets a 'set attribute' (SGR) CSI escape sequence */
31 static void interpret_csi_SGR(RoteTerm *rt, int param[], int pcount) {
32    int i;
33
34    if (pcount == 0) {
35       /* special case: reset attributes */
36       rt->curattr = 0x70;
37       return;
38    }
39
40    for (i = 0; i < pcount; i++) {
41
42 // From http://vt100.net/docs/vt510-rm/SGR table 5-16
43 // 0    All attributes off
44 // 1    Bold
45 // 4    Underline
46 // 5    Blinking
47 // 7    Negative image
48 // 8    Invisible image
49 // 10   The ASCII character set is the current 7-bit
50 //      display character set (default) - SCO Console only.
51 // 11   Map Hex 00-7F of the PC character set codes
52 //      to the current 7-bit display character set
53 //      - SCO Console only.
54 // 12   Map Hex 80-FF of the current character set to
55 //      the current 7-bit display character set - SCO
56 //      Console only.
57 // 22   Bold off
58 // 24   Underline off
59 // 25   Blinking off
60 // 27   Negative image off
61 // 28   Invisible image off
62
63       if (param[i] == 0) rt->curattr = 0x70;
64       else if (param[i] == 1 || param[i] == 2 || param[i] == 4)  /* set bold */
65          ROTE_ATTR_MOD_BOLD(rt->curattr,1);
66       else if (param[i] == 5)  /* set blink */
67          ROTE_ATTR_MOD_BLINK(rt->curattr,1);
68       else if (param[i] == 7 || param[i] == 27) { /* reverse video */
69         int fg = ROTE_ATTR_FG(rt->curattr);
70         int bg = ROTE_ATTR_BG(rt->curattr);
71         ROTE_ATTR_MOD_FG(rt->curattr, bg);
72         ROTE_ATTR_MOD_BG(rt->curattr, fg);
73       }
74       else if (param[i] == 8) rt->curattr = 0x0;    /* invisible */
75       else if (param[i] == 22 || param[i] == 24) /* bold off */
76         ROTE_ATTR_MOD_BOLD(rt->curattr,0);
77       else if (param[i] == 25) /* blink off */
78         ROTE_ATTR_MOD_BLINK(rt->curattr,0);
79       else if (param[i] == 28) /* invisible off */
80         rt->curattr = 0x70;
81       else if (param[i] >= 30 && param[i] <= 37)    /* set fg */
82          ROTE_ATTR_MOD_FG(rt->curattr, param[i] - 30);
83       else if (param[i] >= 40 && param[i] <= 47)    /* set bg */
84          ROTE_ATTR_MOD_BG(rt->curattr, param[i] - 40);
85       else if (param[i] == 39)  /* reset foreground to default */
86          ROTE_ATTR_MOD_FG(rt->curattr, 7);
87       else if (param[i] == 49)  /* reset background to default */
88          ROTE_ATTR_MOD_BG(rt->curattr, 0);
89    }
90 }
91
92 /* interprets an 'erase display' (ED) escape sequence */
93 static void interpret_csi_ED(RoteTerm *rt, int param[], int pcount) {
94    int r, c;
95    int start_row, start_col, end_row, end_col;
96
97    /* decide range */
98    if (pcount && param[0] == 2) 
99                       start_row = 0, start_col = 0, end_row = rt->rows - 1,
100                       end_col = rt->cols - 1;
101
102    else if (pcount && param[0] == 1)
103                       start_row = 0, start_col = 0, end_row = rt->crow,
104                       end_col = rt->ccol;
105
106    else start_row = rt->crow, start_col = rt->ccol,
107         end_row = rt->rows - 1, end_col = rt->cols - 1;
108
109    /* clean range */
110    for (r = start_row; r <= end_row; r++) {
111       rt->line_dirty[r] = true;
112
113       for (c = (r == start_row ? start_col : 0);
114                              c <= (r == end_row ? end_col : rt->cols - 1);
115                              c++) {
116          rt->cells[r][c].ch = 0x20;
117          rt->cells[r][c].attr = rt->curattr;
118       }
119    }
120 }
121
122 /* interprets a 'move cursor' (CUP) escape sequence */
123 static void interpret_csi_CUP(RoteTerm *rt, int param[], int pcount) {
124    if (pcount == 0) {
125       /* special case */
126       rt->crow = rt->ccol = 0;
127       return;
128    }
129    else if (pcount < 2) return;  /* malformed */
130
131    rt->crow = param[0] - 1;  /* convert from 1-based to 0-based */
132    rt->ccol = param[1] - 1;  /* convert from 1-based to 0-based */
133
134    rt->curpos_dirty = true;
135
136    clamp_cursor_to_bounds(rt);
137 }
138
139 /* Interpret the 'relative mode' sequences: CUU, CUD, CUF, CUB, CNL,
140  * CPL, CHA, HPR, VPA, VPR, HPA */
141 static void interpret_csi_C(RoteTerm *rt, char verb, 
142                                                 int param[], int pcount) {
143    int n = (pcount && param[0] > 0) ? param[0] : 1;
144
145    switch (verb) {
146       case 'A':           rt->crow -= n; break;
147       case 'B': case 'e': rt->crow += n; break;
148       case 'C': case 'a': rt->ccol += n; break;
149       case 'D':           rt->ccol -= n; break;
150       case 'E':           rt->crow += n; rt->ccol = 0; break;
151       case 'F':           rt->crow -= n; rt->ccol = 0; break;
152       case 'G': case '`': rt->ccol  = param[0] - 1; break;
153       case 'd':           rt->crow  = param[0] - 1; break;
154    }
155
156    rt->curpos_dirty = true;
157    clamp_cursor_to_bounds(rt);
158 }
159
160 /* Interpret the 'erase line' escape sequence */
161 static void interpret_csi_EL(RoteTerm *rt, int param[], int pcount) {
162    int erase_start, erase_end, i;
163    int cmd = pcount ? param[0] : 0;
164
165    switch (cmd) {
166       case 1:  erase_start = 0;           erase_end = rt->ccol;     break;
167       case 2:  erase_start = 0;           erase_end = rt->cols - 1; break;
168       default: erase_start = rt->ccol;    erase_end = rt->cols - 1; break;
169    }
170
171    for (i = erase_start; i <= erase_end; i++) {
172       rt->cells[rt->crow][i].ch = 0x20; 
173       rt->cells[rt->crow][i].attr = rt->curattr;
174    }
175
176    rt->line_dirty[rt->crow] = true;
177 }
178
179 /* Interpret the 'insert blanks' sequence (ICH) */
180 static void interpret_csi_ICH(RoteTerm *rt, int param[], int pcount) {
181    int n = (pcount && param[0] > 0) ? param[0] : 1; 
182    int i;
183    for (i = rt->cols - 1; i >= rt->ccol + n; i--)
184       rt->cells[rt->crow][i] = rt->cells[rt->crow][i - n];
185    for (i = rt->ccol; i < rt->ccol + n; i++) {
186       rt->cells[rt->crow][i].ch = 0x20;
187       rt->cells[rt->crow][i].attr = rt->curattr;
188    }
189
190    rt->line_dirty[rt->crow] = true;
191 }
192
193 /* Interpret the 'delete chars' sequence (DCH) */
194 static void interpret_csi_DCH(RoteTerm *rt, int param[], int pcount) {
195    int n = (pcount && param[0] > 0) ? param[0] : 1; 
196    int i;
197    for (i = rt->ccol; i < rt->cols; i++) {
198      if (i + n < rt->cols)
199          rt->cells[rt->crow][i] = rt->cells[rt->crow][i + n];
200      else {
201          rt->cells[rt->crow][i].ch = 0x20;
202          rt->cells[rt->crow][i].attr = rt->curattr;
203      }
204    }
205
206    rt->line_dirty[rt->crow] = true;
207 }
208
209 /* Interpret an 'insert line' sequence (IL) */
210 static void interpret_csi_IL(RoteTerm *rt, int param[], int pcount) {
211    int n = (pcount && param[0] > 0) ? param[0] : 1;
212    int i, j;
213
214    for (i = rt->pd->scrollbottom; i >= rt->crow + n; i--) 
215       memcpy(rt->cells[i], rt->cells[i - n], sizeof(RoteCell) * rt->cols);
216
217    for (i = rt->crow; i < rt->crow + n && i <= rt->pd->scrollbottom; i++) {
218       rt->line_dirty[i] = true;
219       for (j = 0; j < rt->cols; j++) 
220          rt->cells[i][j].ch = 0x20, rt->cells[i][j].attr = rt->curattr;
221    }
222
223 }
224
225 /* Interpret a 'delete line' sequence (DL) */
226 static void interpret_csi_DL(RoteTerm *rt, int param[], int pcount) {
227    int n = (pcount && param[0] > 0) ? param[0] : 1;
228    int i, j;
229
230    for (i = rt->crow; i <= rt->pd->scrollbottom; i++) {
231       rt->line_dirty[i] = true;
232       if (i + n <= rt->pd->scrollbottom)
233          memcpy(rt->cells[i], rt->cells[i+n], sizeof(RoteCell) * rt->cols);
234       else {
235          for (j = 0; j < rt->cols; j++)
236             rt->cells[i][j].ch = 0x20, rt->cells[i][j].attr = rt->curattr;
237       }
238    }
239 }
240
241 /* Interpret an 'erase characters' (ECH) sequence */
242 static void interpret_csi_ECH(RoteTerm *rt, int param[], int pcount) {
243    int n = (pcount && param[0] > 0) ? param[0] : 1;
244    int i;
245
246    for (i = rt->ccol; i < rt->ccol + n && i < rt->cols; i++) {
247       rt->cells[rt->crow][i].ch = 0x20;
248       rt->cells[rt->crow][i].attr = rt->curattr;
249    }
250
251    rt->line_dirty[rt->crow] = true;
252 }
253         
254 /* Interpret a 'set scrolling region' (DECSTBM) sequence */
255 static void interpret_csi_DECSTBM(RoteTerm *rt, int param[], int pcount) {
256    int newtop, newbottom;
257    
258    if (!pcount) {
259       newtop = 0;
260       newbottom = rt->rows - 1;
261    }
262    else if (pcount < 2) return; /* malformed */
263    else {
264       newtop = param[0] - 1;
265       newbottom = param[1] - 1;
266    }
267
268    /* clamp to bounds */
269    if (newtop < 0) newtop = 0;
270    if (newtop >= rt->rows) newtop = rt->rows - 1;
271    if (newbottom < 0) newbottom = 0;
272    if (newbottom >= rt->rows) newbottom = rt->rows - 1;
273
274    /* check for range validity */
275    if (newtop > newbottom) return;
276    rt->pd->scrolltop = newtop;
277    rt->pd->scrollbottom = newbottom;
278 }
279          
280 static void interpret_csi_SAVECUR(RoteTerm *rt, int param[], int pcount) {
281    rt->pd->saved_x = rt->ccol;
282    rt->pd->saved_y = rt->crow;
283 }
284
285 static void interpret_csi_RESTORECUR(RoteTerm *rt, int param[], int pcount) {
286    rt->ccol = rt->pd->saved_x;
287    rt->crow = rt->pd->saved_y;
288    rt->curpos_dirty = true;
289 }
290
291 void rote_es_interpret_csi(RoteTerm *rt) {
292    static int csiparam[MAX_CSI_ES_PARAMS];
293    int param_count = 0;
294    const char *p = rt->pd->esbuf + 1;
295    char verb = rt->pd->esbuf[rt->pd->esbuf_len - 1];
296
297    if (!strncmp(rt->pd->esbuf, "[?", 2)) { /* private-mode CSI, ignore */
298       #ifdef DEBUG
299       fprintf(stderr, "Ignoring private-mode CSI: <%s>\n", rt->pd->esbuf);
300       #endif
301       return; 
302    }
303
304    /* parse numeric parameters */
305    while ((*p >= '0' && *p <= '9') || *p == ';') {
306       if (*p == ';') {
307          if (param_count >= MAX_CSI_ES_PARAMS) return; /* too long! */
308          csiparam[param_count++] = 0;
309       }
310       else {
311          if (param_count == 0) csiparam[param_count++] = 0;
312          csiparam[param_count - 1] *= 10;
313          csiparam[param_count - 1] += *p - '0';
314       }
315
316       p++;
317    }
318
319    /* delegate handling depending on command character (verb) */
320    switch (verb) {
321       case 'h':
322           if (param_count == 1 && csiparam[0] == 4) /* insert mode */ 
323               rt->insert = true;
324           break;
325       case 'l':
326           if (param_count == 1 && csiparam[0] == 4) /* replace mode */
327               rt->insert = false;
328           break;
329       case 'm': /* it's a 'set attribute' sequence */
330          interpret_csi_SGR(rt, csiparam, param_count); break;
331       case 'J': /* it's an 'erase display' sequence */
332          interpret_csi_ED(rt, csiparam, param_count); break;
333       case 'H': case 'f': /* it's a 'move cursor' sequence */
334          interpret_csi_CUP(rt, csiparam, param_count); break;
335       case 'A': case 'B': case 'C': case 'D': case 'E': case 'F': case 'G':
336       case 'e': case 'a': case 'd': case '`':
337          /* it is a 'relative move' */
338          interpret_csi_C(rt, verb, csiparam, param_count); break;
339       case 'K': /* erase line */
340          interpret_csi_EL(rt, csiparam, param_count); break;
341       case '@': /* insert characters */
342          interpret_csi_ICH(rt, csiparam, param_count); break;
343       case 'P': /* delete characters */
344          interpret_csi_DCH(rt, csiparam, param_count); break;
345       case 'L': /* insert lines */
346          interpret_csi_IL(rt, csiparam, param_count); break;
347       case 'M': /* delete lines */
348          interpret_csi_DL(rt, csiparam, param_count); break;
349       case 'X': /* erase chars */
350          interpret_csi_ECH(rt, csiparam, param_count); break;
351       case 'r': /* set scrolling region */
352          interpret_csi_DECSTBM(rt, csiparam, param_count); break;
353       case 's': /* save cursor location */
354          interpret_csi_SAVECUR(rt, csiparam, param_count); break;
355       case 'u': /* restore cursor location */
356          interpret_csi_RESTORECUR(rt, csiparam, param_count); break;
357       default:
358          #ifdef DEBUG
359          fprintf(stderr, "Unrecogized CSI: verb=%c <%s>\n", 
360                  verb, rt->pd->esbuf); 
361          #endif
362          break;
363    }
364 }
365