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