]> CyberLeo.Net >> Repos - FreeBSD/releng/8.1.git/blob - sys/dev/syscons/teken/teken.c
Copy stable/8 to releng/8.1 in preparation for 8.1-RC1.
[FreeBSD/releng/8.1.git] / sys / dev / syscons / teken / teken.c
1 /*-
2  * Copyright (c) 2008-2009 Ed Schouten <ed@FreeBSD.org>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  *
26  * $FreeBSD$
27  */
28
29 #include <sys/cdefs.h>
30 #if defined(__FreeBSD__) && defined(_KERNEL)
31 #include <sys/param.h>
32 #include <sys/lock.h>
33 #include <sys/systm.h>
34 #define teken_assert(x)         MPASS(x)
35 #define teken_printf(x,...)
36 #else /* !(__FreeBSD__ && _KERNEL) */
37 #include <sys/types.h>
38 #include <assert.h>
39 #include <inttypes.h>
40 #include <stdio.h>
41 #include <string.h>
42 #define teken_assert(x)         assert(x)
43 #define teken_printf(x,...)     do { \
44         if (df != NULL) \
45                 fprintf(df, x, ## __VA_ARGS__); \
46 } while (0)
47 /* debug messages */
48 static FILE *df;
49 #endif /* __FreeBSD__ && _KERNEL */
50
51 #include "teken.h"
52
53 #ifdef TEKEN_UTF8
54 #include "teken_wcwidth.h"
55 #else /* !TEKEN_UTF8 */
56 #ifdef TEKEN_XTERM
57 #define teken_wcwidth(c)        ((c <= 0x1B) ? -1 : 1)
58 #else /* !TEKEN_XTERM */
59 #define teken_wcwidth(c)        (1)
60 #endif /* TEKEN_XTERM */
61 #endif /* TEKEN_UTF8 */
62
63 #if defined(TEKEN_XTERM) && defined(TEKEN_UTF8)
64 #include "teken_scs.h"
65 #else /* !(TEKEN_XTERM && TEKEN_UTF8) */
66 #define teken_scs_process(t, c) (c)
67 #define teken_scs_restore(t)
68 #define teken_scs_save(t)
69 #define teken_scs_set(t, g, ts)
70 #define teken_scs_switch(t, g)
71 #endif /* TEKEN_XTERM && TEKEN_UTF8 */
72
73 /* Private flags for teken_format_t. */
74 #define TF_REVERSE      0x08
75
76 /* Private flags for t_stateflags. */
77 #define TS_FIRSTDIGIT   0x01    /* First numeric digit in escape sequence. */
78 #define TS_INSERT       0x02    /* Insert mode. */
79 #define TS_AUTOWRAP     0x04    /* Autowrap. */
80 #define TS_ORIGIN       0x08    /* Origin mode. */
81 #ifdef TEKEN_XTERM
82 #define TS_WRAPPED      0x10    /* Next character should be printed on col 0. */
83 #else /* !TEKEN_XTERM */
84 #define TS_WRAPPED      0x00    /* Simple line wrapping. */
85 #endif /* TEKEN_XTERM */
86
87 /* Character that blanks a cell. */
88 #define BLANK   ' '
89
90 static teken_state_t    teken_state_init;
91
92 /*
93  * Wrappers for hooks.
94  */
95
96 static inline void
97 teken_funcs_bell(teken_t *t)
98 {
99
100         t->t_funcs->tf_bell(t->t_softc);
101 }
102
103 static inline void
104 teken_funcs_cursor(teken_t *t)
105 {
106
107         teken_assert(t->t_cursor.tp_row < t->t_winsize.tp_row);
108         teken_assert(t->t_cursor.tp_col < t->t_winsize.tp_col);
109
110         t->t_funcs->tf_cursor(t->t_softc, &t->t_cursor);
111 }
112
113 static inline void
114 teken_funcs_putchar(teken_t *t, const teken_pos_t *p, teken_char_t c,
115     const teken_attr_t *a)
116 {
117         teken_attr_t ta;
118
119         teken_assert(p->tp_row < t->t_winsize.tp_row);
120         teken_assert(p->tp_col < t->t_winsize.tp_col);
121
122         /* Apply inversion. */
123         if (a->ta_format & TF_REVERSE) {
124                 ta.ta_format = a->ta_format;
125                 ta.ta_fgcolor = a->ta_bgcolor;
126                 ta.ta_bgcolor = a->ta_fgcolor;
127                 a = &ta;
128         }
129
130         t->t_funcs->tf_putchar(t->t_softc, p, c, a);
131 }
132
133 static inline void
134 teken_funcs_fill(teken_t *t, const teken_rect_t *r,
135     const teken_char_t c, const teken_attr_t *a)
136 {
137         teken_attr_t ta;
138
139         teken_assert(r->tr_end.tp_row > r->tr_begin.tp_row);
140         teken_assert(r->tr_end.tp_row <= t->t_winsize.tp_row);
141         teken_assert(r->tr_end.tp_col > r->tr_begin.tp_col);
142         teken_assert(r->tr_end.tp_col <= t->t_winsize.tp_col);
143
144         /* Apply inversion. */
145         if (a->ta_format & TF_REVERSE) {
146                 ta.ta_format = a->ta_format;
147                 ta.ta_fgcolor = a->ta_bgcolor;
148                 ta.ta_bgcolor = a->ta_fgcolor;
149                 a = &ta;
150         }
151
152         t->t_funcs->tf_fill(t->t_softc, r, c, a);
153 }
154
155 static inline void
156 teken_funcs_copy(teken_t *t, const teken_rect_t *r, const teken_pos_t *p)
157 {
158
159         teken_assert(r->tr_end.tp_row > r->tr_begin.tp_row);
160         teken_assert(r->tr_end.tp_row <= t->t_winsize.tp_row);
161         teken_assert(r->tr_end.tp_col > r->tr_begin.tp_col);
162         teken_assert(r->tr_end.tp_col <= t->t_winsize.tp_col);
163         teken_assert(p->tp_row + (r->tr_end.tp_row - r->tr_begin.tp_row) <= t->t_winsize.tp_row);
164         teken_assert(p->tp_col + (r->tr_end.tp_col - r->tr_begin.tp_col) <= t->t_winsize.tp_col);
165
166         t->t_funcs->tf_copy(t->t_softc, r, p);
167 }
168
169 static inline void
170 teken_funcs_param(teken_t *t, int cmd, unsigned int value)
171 {
172
173         t->t_funcs->tf_param(t->t_softc, cmd, value);
174 }
175
176 static inline void
177 teken_funcs_respond(teken_t *t, const void *buf, size_t len)
178 {
179
180         t->t_funcs->tf_respond(t->t_softc, buf, len);
181 }
182
183 #include "teken_subr.h"
184 #include "teken_subr_compat.h"
185
186 /*
187  * Programming interface.
188  */
189
190 void
191 teken_init(teken_t *t, const teken_funcs_t *tf, void *softc)
192 {
193         teken_pos_t tp = { .tp_row = 24, .tp_col = 80 };
194
195 #if !(defined(__FreeBSD__) && defined(_KERNEL))
196         df = fopen("teken.log", "w");
197         if (df != NULL)
198                 setvbuf(df, NULL, _IOLBF, BUFSIZ);
199 #endif /* !(__FreeBSD__ && _KERNEL) */
200
201         t->t_funcs = tf;
202         t->t_softc = softc;
203
204         t->t_nextstate = teken_state_init;
205
206         t->t_defattr.ta_format = 0;
207         t->t_defattr.ta_fgcolor = TC_WHITE;
208         t->t_defattr.ta_bgcolor = TC_BLACK;
209         teken_subr_do_reset(t);
210
211 #ifdef TEKEN_UTF8
212         t->t_utf8_left = 0;
213 #endif /* TEKEN_UTF8 */
214
215         teken_set_winsize(t, &tp);
216 }
217
218 static void
219 teken_input_char(teken_t *t, teken_char_t c)
220 {
221
222         switch (c) {
223         case '\0':
224                 break;
225         case '\a':
226                 teken_subr_bell(t);
227                 break;
228         case '\b':
229                 teken_subr_backspace(t);
230                 break;
231         case '\n':
232         case '\x0B':
233                 teken_subr_newline(t);
234                 break;
235         case '\x0C':
236                 teken_subr_newpage(t);
237                 break;
238 #if defined(TEKEN_XTERM) && defined(TEKEN_UTF8)
239         case '\x0E':
240                 teken_scs_switch(t, 1);
241                 break;
242         case '\x0F':
243                 teken_scs_switch(t, 0);
244                 break;
245 #endif /* TEKEN_XTERM && TEKEN_UTF8 */
246         case '\r':
247                 teken_subr_carriage_return(t);
248                 break;
249         case '\t':
250                 teken_subr_horizontal_tab(t);
251                 break;
252         default:
253                 t->t_nextstate(t, c);
254                 break;
255         }
256
257         /* Post-processing assertions. */
258         teken_assert(t->t_cursor.tp_row >= t->t_originreg.ts_begin);
259         teken_assert(t->t_cursor.tp_row < t->t_originreg.ts_end);
260         teken_assert(t->t_cursor.tp_row < t->t_winsize.tp_row);
261         teken_assert(t->t_cursor.tp_col < t->t_winsize.tp_col);
262         teken_assert(t->t_saved_cursor.tp_row < t->t_winsize.tp_row);
263         teken_assert(t->t_saved_cursor.tp_col < t->t_winsize.tp_col);
264         teken_assert(t->t_scrollreg.ts_end <= t->t_winsize.tp_row);
265         teken_assert(t->t_scrollreg.ts_begin < t->t_scrollreg.ts_end);
266         /* Origin region has to be window size or the same as scrollreg. */
267         teken_assert((t->t_originreg.ts_begin == t->t_scrollreg.ts_begin &&
268             t->t_originreg.ts_end == t->t_scrollreg.ts_end) ||
269             (t->t_originreg.ts_begin == 0 &&
270             t->t_originreg.ts_end == t->t_winsize.tp_row));
271 }
272
273 static void
274 teken_input_byte(teken_t *t, unsigned char c)
275 {
276
277 #ifdef TEKEN_UTF8
278         /*
279          * UTF-8 handling.
280          */
281         if ((c & 0x80) == 0x00) {
282                 /* One-byte sequence. */
283                 t->t_utf8_left = 0;
284                 teken_input_char(t, c);
285         } else if ((c & 0xe0) == 0xc0) {
286                 /* Two-byte sequence. */
287                 t->t_utf8_left = 1;
288                 t->t_utf8_partial = c & 0x1f;
289         } else if ((c & 0xf0) == 0xe0) {
290                 /* Three-byte sequence. */
291                 t->t_utf8_left = 2;
292                 t->t_utf8_partial = c & 0x0f;
293         } else if ((c & 0xf8) == 0xf0) {
294                 /* Four-byte sequence. */
295                 t->t_utf8_left = 3;
296                 t->t_utf8_partial = c & 0x07;
297         } else if ((c & 0xc0) == 0x80) {
298                 if (t->t_utf8_left == 0)
299                         return;
300                 t->t_utf8_left--;
301                 t->t_utf8_partial = (t->t_utf8_partial << 6) | (c & 0x3f);
302                 if (t->t_utf8_left == 0) {
303                         teken_printf("Got UTF-8 char %x\n", t->t_utf8_partial);
304                         teken_input_char(t, t->t_utf8_partial);
305                 }
306         }
307 #else /* !TEKEN_UTF8 */
308         teken_input_char(t, c);
309 #endif /* TEKEN_UTF8 */
310 }
311
312 void
313 teken_input(teken_t *t, const void *buf, size_t len)
314 {
315         const char *c = buf;
316
317         while (len-- > 0)
318                 teken_input_byte(t, *c++);
319 }
320
321 void
322 teken_set_cursor(teken_t *t, const teken_pos_t *p)
323 {
324
325         /* XXX: bounds checking with originreg! */
326         teken_assert(p->tp_row < t->t_winsize.tp_row);
327         teken_assert(p->tp_col < t->t_winsize.tp_col);
328
329         t->t_cursor = *p;
330 }
331
332 const teken_attr_t *
333 teken_get_curattr(teken_t *t)
334 {
335
336         return (&t->t_curattr);
337 }
338
339 void
340 teken_set_curattr(teken_t *t, const teken_attr_t *a)
341 {
342
343         t->t_curattr = *a;
344 }
345
346 const teken_attr_t *
347 teken_get_defattr(teken_t *t)
348 {
349
350         return (&t->t_defattr);
351 }
352
353 void
354 teken_set_defattr(teken_t *t, const teken_attr_t *a)
355 {
356
357         t->t_curattr = t->t_saved_curattr = t->t_defattr = *a;
358 }
359
360 void
361 teken_set_winsize(teken_t *t, const teken_pos_t *p)
362 {
363
364         t->t_winsize = *p;
365         /* XXX: bounds checking with cursor/etc! */
366         t->t_scrollreg.ts_begin = 0;
367         t->t_scrollreg.ts_end = t->t_winsize.tp_row;
368         t->t_originreg = t->t_scrollreg;
369 }
370
371 /*
372  * State machine.
373  */
374
375 static void
376 teken_state_switch(teken_t *t, teken_state_t *s)
377 {
378
379         t->t_nextstate = s;
380         t->t_curnum = 0;
381         t->t_stateflags |= TS_FIRSTDIGIT;
382 }
383
384 static int
385 teken_state_numbers(teken_t *t, teken_char_t c)
386 {
387
388         teken_assert(t->t_curnum < T_NUMSIZE);
389
390         if (c >= '0' && c <= '9') {
391                 /*
392                  * Don't do math with the default value of 1 when a
393                  * custom number is inserted.
394                  */
395                 if (t->t_stateflags & TS_FIRSTDIGIT) {
396                         t->t_stateflags &= ~TS_FIRSTDIGIT;
397                         t->t_nums[t->t_curnum] = 0;
398                 } else {
399                         t->t_nums[t->t_curnum] *= 10;
400                 }
401
402                 t->t_nums[t->t_curnum] += c - '0';
403                 return (1);
404         } else if (c == ';') {
405                 if (t->t_stateflags & TS_FIRSTDIGIT)
406                         t->t_nums[t->t_curnum] = 0;
407
408                 /* Only allow a limited set of arguments. */
409                 if (++t->t_curnum == T_NUMSIZE) {
410                         teken_state_switch(t, teken_state_init);
411                         return (1);
412                 }
413
414                 t->t_stateflags |= TS_FIRSTDIGIT;
415                 return (1);
416         } else {
417                 if (t->t_stateflags & TS_FIRSTDIGIT && t->t_curnum > 0) {
418                         /* Finish off the last empty argument. */
419                         t->t_nums[t->t_curnum] = 0;
420                         t->t_curnum++;
421                 } else if ((t->t_stateflags & TS_FIRSTDIGIT) == 0) {
422                         /* Also count the last argument. */
423                         t->t_curnum++;
424                 }
425         }
426
427         return (0);
428 }
429
430 #include "teken_state.h"