reindent
[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 {
34     int i;
35
36     if (pcount == 0) {
37         /* special case: reset attributes */
38         rt->curattr = 0x70;
39         return;
40     }
41
42     for (i = 0; i < pcount; i++) {
43
44         // From http://vt100.net/docs/vt510-rm/SGR table 5-16
45         // 0    All attributes off
46         // 1    Bold
47         // 4    Underline
48         // 5    Blinking
49         // 7    Negative image
50         // 8    Invisible image
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
58         //      Console only.
59         // 22   Bold off
60         // 24   Underline off
61         // 25   Blinking off
62         // 27   Negative image off
63         // 28   Invisible image off
64
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);
75         }
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 */
82             rt->curattr = 0x70;
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);
91     }
92 }
93
94 /* interprets an 'erase display' (ED) escape sequence */
95 static void interpret_csi_ED(RoteTerm *rt, int param[], int pcount)
96 {
97     int r, c;
98     int start_row, start_col, end_row, end_col;
99
100     /* decide range */
101     if (pcount && param[0] == 2) 
102         start_row = 0, start_col = 0, end_row = rt->rows - 1,
103                   end_col = rt->cols - 1;
104
105     else if (pcount && param[0] == 1)
106         start_row = 0, start_col = 0, end_row = rt->crow,
107                   end_col = rt->ccol;
108
109     else start_row = rt->crow, start_col = rt->ccol,
110         end_row = rt->rows - 1, end_col = rt->cols - 1;
111
112     /* clean range */
113     for (r = start_row; r <= end_row; r++) {
114         rt->line_dirty[r] = true;
115
116         for (c = (r == start_row ? start_col : 0);
117              c <= (r == end_row ? end_col : rt->cols - 1);
118              c++) {
119             rt->cells[r][c].ch = 0x20;
120             rt->cells[r][c].attr = rt->curattr;
121         }
122     }
123 }
124
125 /* interprets a 'move cursor' (CUP) escape sequence */
126 static void interpret_csi_CUP(RoteTerm *rt, int param[], int pcount)
127 {
128     if (pcount == 0) {
129         /* special case */
130         rt->crow = rt->ccol = 0;
131         return;
132     }
133     else if (pcount < 2) return;  /* malformed */
134
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 */
137
138     rt->curpos_dirty = true;
139
140     clamp_cursor_to_bounds(rt);
141 }
142
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;
148
149     switch (verb) {
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;
158     }
159
160     rt->curpos_dirty = true;
161     clamp_cursor_to_bounds(rt);
162 }
163
164 /* Interpret the 'erase line' escape sequence */
165 static void interpret_csi_EL(RoteTerm *rt, int param[], int pcount)
166 {
167     int erase_start, erase_end, i;
168     int cmd = pcount ? param[0] : 0;
169
170     switch (cmd) {
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;
174     }
175
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;
179     }
180
181     rt->line_dirty[rt->crow] = true;
182 }
183
184 /* Interpret the 'insert blanks' sequence (ICH) */
185 static void interpret_csi_ICH(RoteTerm *rt, int param[], int pcount)
186 {
187     int n = (pcount && param[0] > 0) ? param[0] : 1; 
188     int i;
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;
194     }
195
196     rt->line_dirty[rt->crow] = true;
197 }
198
199 /* Interpret the 'delete chars' sequence (DCH) */
200 static void interpret_csi_DCH(RoteTerm *rt, int param[], int pcount)
201 {
202     int n = (pcount && param[0] > 0) ? param[0] : 1; 
203     int i;
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];
207         else {
208             rt->cells[rt->crow][i].ch = 0x20;
209             rt->cells[rt->crow][i].attr = rt->curattr;
210         }
211     }
212
213     rt->line_dirty[rt->crow] = true;
214 }
215
216 /* Interpret an 'insert line' sequence (IL) */
217 static void interpret_csi_IL(RoteTerm *rt, int param[], int pcount)
218 {
219     int n = (pcount && param[0] > 0) ? param[0] : 1;
220     int i, j;
221
222     for (i = rt->pd->scrollbottom; i >= rt->crow + n; i--) 
223         memcpy(rt->cells[i], rt->cells[i - n], sizeof(RoteCell) * rt->cols);
224
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;
229     }
230
231 }
232
233 /* Interpret a 'delete line' sequence (DL) */
234 static void interpret_csi_DL(RoteTerm *rt, int param[], int pcount)
235 {
236     int n = (pcount && param[0] > 0) ? param[0] : 1;
237     int i, j;
238
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);
243         else {
244             for (j = 0; j < rt->cols; j++)
245                 rt->cells[i][j].ch = 0x20, rt->cells[i][j].attr = rt->curattr;
246         }
247     }
248 }
249
250 /* Interpret an 'erase characters' (ECH) sequence */
251 static void interpret_csi_ECH(RoteTerm *rt, int param[], int pcount)
252 {
253     int n = (pcount && param[0] > 0) ? param[0] : 1;
254     int i;
255
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;
259     }
260
261     rt->line_dirty[rt->crow] = true;
262 }
263
264 /* Interpret a 'set scrolling region' (DECSTBM) sequence */
265 static void interpret_csi_DECSTBM(RoteTerm *rt, int param[], int pcount)
266 {
267     int newtop, newbottom;
268
269     if (!pcount) {
270         newtop = 0;
271         newbottom = rt->rows - 1;
272     }
273     else if (pcount < 2) return; /* malformed */
274     else {
275         newtop = param[0] - 1;
276         newbottom = param[1] - 1;
277     }
278
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;
284
285     /* check for range validity */
286     if (newtop > newbottom) return;
287     rt->pd->scrolltop = newtop;
288     rt->pd->scrollbottom = newbottom;
289 }
290
291 static void interpret_csi_SAVECUR(RoteTerm *rt, int param[], int pcount)
292 {
293     rt->pd->saved_x = rt->ccol;
294     rt->pd->saved_y = rt->crow;
295 }
296
297 static void interpret_csi_RESTORECUR(RoteTerm *rt, int param[], int pcount)
298 {
299     rt->ccol = rt->pd->saved_x;
300     rt->crow = rt->pd->saved_y;
301     rt->curpos_dirty = true;
302 }
303
304 void rote_es_interpret_csi(RoteTerm *rt)
305 {
306     static int csiparam[MAX_CSI_ES_PARAMS];
307     int param_count = 0;
308     const char *p = rt->pd->esbuf + 1;
309     char verb = rt->pd->esbuf[rt->pd->esbuf_len - 1];
310
311     if (!strncmp(rt->pd->esbuf, "[?", 2)) { /* private-mode CSI, ignore */
312 #ifdef DEBUG
313         fprintf(stderr, "Ignoring private-mode CSI: <%s>\n", rt->pd->esbuf);
314 #endif
315         return; 
316     }
317
318     /* parse numeric parameters */
319     while ((*p >= '0' && *p <= '9') || *p == ';') {
320         if (*p == ';') {
321             if (param_count >= MAX_CSI_ES_PARAMS) return; /* too long! */
322             csiparam[param_count++] = 0;
323         }
324         else {
325             if (param_count == 0) csiparam[param_count++] = 0;
326             csiparam[param_count - 1] *= 10;
327             csiparam[param_count - 1] += *p - '0';
328         }
329
330         p++;
331     }
332
333     /* delegate handling depending on command character (verb) */
334     switch (verb) {
335       case 'h':
336         if (param_count == 1 && csiparam[0] == 4) /* insert mode */ 
337             rt->insert = true;
338         break;
339       case 'l':
340         if (param_count == 1 && csiparam[0] == 4) /* replace mode */
341             rt->insert = false;
342         break;
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;
371       default:
372 #ifdef DEBUG
373         fprintf(stderr, "Unrecogized CSI: verb=%c <%s>\n", 
374                 verb, rt->pd->esbuf); 
375 #endif
376         break;
377     }
378 }
379