teken.c revision 186729
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: head/sys/dev/syscons/teken/teken.c 186729 2009-01-03 22:51:54Z ed $ 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 */ 48static FILE *df; 49#endif /* __FreeBSD__ && _KERNEL */ 50 51#include "teken.h" 52#ifdef TEKEN_UTF8 53#include "teken_wcwidth.h" 54#else /* !TEKEN_UTF8 */ 55static inline int 56teken_wcwidth(teken_char_t c) 57{ 58 59 return (c <= 0x1B) ? -1 : 1; 60} 61#endif /* TEKEN_UTF8 */ 62 63/* Private flags for teken_format_t. */ 64#define TF_REVERSE 0x08 65 66/* Private flags for t_stateflags. */ 67#define TS_FIRSTDIGIT 0x01 /* First numeric digit in escape sequence. */ 68#define TS_INSERT 0x02 /* Insert mode. */ 69#define TS_AUTOWRAP 0x04 /* Autowrap. */ 70#define TS_ORIGIN 0x08 /* Origin mode. */ 71#ifdef TEKEN_CONS25 72#define TS_WRAPPED 0x00 /* Simple line wrapping. */ 73#else /* !TEKEN_CONS25 */ 74#define TS_WRAPPED 0x10 /* Next character should be printed on col 0. */ 75#endif /* TEKEN_CONS25 */ 76 77/* Character that blanks a cell. */ 78#define BLANK ' ' 79 80static teken_state_t teken_state_init; 81 82/* 83 * Wrappers for hooks. 84 */ 85 86static inline void 87teken_funcs_bell(teken_t *t) 88{ 89 90 t->t_funcs->tf_bell(t->t_softc); 91} 92 93static inline void 94teken_funcs_cursor(teken_t *t) 95{ 96 97 teken_assert(t->t_cursor.tp_row < t->t_winsize.tp_row); 98 teken_assert(t->t_cursor.tp_col < t->t_winsize.tp_col); 99 100 t->t_funcs->tf_cursor(t->t_softc, &t->t_cursor); 101} 102 103static inline void 104teken_funcs_putchar(teken_t *t, const teken_pos_t *p, teken_char_t c, 105 const teken_attr_t *a) 106{ 107 teken_attr_t ta; 108 109 teken_assert(p->tp_row < t->t_winsize.tp_row); 110 teken_assert(p->tp_col < t->t_winsize.tp_col); 111 112 /* Apply inversion. */ 113 if (a->ta_format & TF_REVERSE) { 114 ta.ta_format = a->ta_format; 115 ta.ta_fgcolor = a->ta_bgcolor; 116 ta.ta_bgcolor = a->ta_fgcolor; 117 a = &ta; 118 } 119 120 t->t_funcs->tf_putchar(t->t_softc, p, c, a); 121} 122 123static inline void 124teken_funcs_fill(teken_t *t, const teken_rect_t *r, 125 const teken_char_t c, const teken_attr_t *a) 126{ 127 teken_attr_t ta; 128 129 teken_assert(r->tr_end.tp_row > r->tr_begin.tp_row); 130 teken_assert(r->tr_end.tp_row <= t->t_winsize.tp_row); 131 teken_assert(r->tr_end.tp_col > r->tr_begin.tp_col); 132 teken_assert(r->tr_end.tp_col <= t->t_winsize.tp_col); 133 134 /* Apply inversion. */ 135 if (a->ta_format & TF_REVERSE) { 136 ta.ta_format = a->ta_format; 137 ta.ta_fgcolor = a->ta_bgcolor; 138 ta.ta_bgcolor = a->ta_fgcolor; 139 a = &ta; 140 } 141 142 t->t_funcs->tf_fill(t->t_softc, r, c, a); 143} 144 145static inline void 146teken_funcs_copy(teken_t *t, const teken_rect_t *r, const teken_pos_t *p) 147{ 148 149 teken_assert(r->tr_end.tp_row > r->tr_begin.tp_row); 150 teken_assert(r->tr_end.tp_row <= t->t_winsize.tp_row); 151 teken_assert(r->tr_end.tp_col > r->tr_begin.tp_col); 152 teken_assert(r->tr_end.tp_col <= t->t_winsize.tp_col); 153 teken_assert(p->tp_row + (r->tr_end.tp_row - r->tr_begin.tp_row) <= t->t_winsize.tp_row); 154 teken_assert(p->tp_col + (r->tr_end.tp_col - r->tr_begin.tp_col) <= t->t_winsize.tp_col); 155 156 t->t_funcs->tf_copy(t->t_softc, r, p); 157} 158 159static inline void 160teken_funcs_param(teken_t *t, int cmd, int value) 161{ 162 163 t->t_funcs->tf_param(t->t_softc, cmd, value); 164} 165 166static inline void 167teken_funcs_respond(teken_t *t, const void *buf, size_t len) 168{ 169 170 t->t_funcs->tf_respond(t->t_softc, buf, len); 171} 172 173#include "teken_subr.h" 174#include "teken_subr_compat.h" 175 176/* 177 * Programming interface. 178 */ 179 180void 181teken_init(teken_t *t, const teken_funcs_t *tf, void *softc) 182{ 183 teken_pos_t tp = { .tp_row = 24, .tp_col = 80 }; 184 185#if !(defined(__FreeBSD__) && defined(_KERNEL)) 186 df = fopen("teken.log", "w"); 187 if (df != NULL) 188 setvbuf(df, NULL, _IOLBF, BUFSIZ); 189#endif /* !(__FreeBSD__ && _KERNEL) */ 190 191 t->t_funcs = tf; 192 t->t_softc = softc; 193 194 t->t_nextstate = teken_state_init; 195 196 t->t_defattr.ta_format = 0; 197 t->t_defattr.ta_fgcolor = TC_WHITE; 198 t->t_defattr.ta_bgcolor = TC_BLACK; 199 teken_subr_do_reset(t); 200 201#ifdef TEKEN_UTF8 202 t->t_utf8_left = 0; 203#endif /* TEKEN_UTF8 */ 204 205 teken_set_winsize(t, &tp); 206} 207 208static void 209teken_input_char(teken_t *t, teken_char_t c) 210{ 211 212 switch (c) { 213 case '\0': 214 break; 215 case '\a': 216 teken_subr_bell(t); 217 break; 218 case '\b': 219 teken_subr_backspace(t); 220 break; 221 case '\n': 222 case '\x0B': 223 teken_subr_newline(t); 224 break; 225 case '\r': 226 teken_subr_carriage_return(t); 227 break; 228 case '\t': 229 teken_subr_horizontal_tab(t); 230 break; 231 default: 232 t->t_nextstate(t, c); 233 break; 234 } 235 236 /* Post-processing assertions. */ 237 teken_assert(t->t_cursor.tp_row >= t->t_originreg.ts_begin); 238 teken_assert(t->t_cursor.tp_row < t->t_originreg.ts_end); 239 teken_assert(t->t_cursor.tp_row < t->t_winsize.tp_row); 240 teken_assert(t->t_cursor.tp_col < t->t_winsize.tp_col); 241 teken_assert(t->t_saved_cursor.tp_row < t->t_winsize.tp_row); 242 teken_assert(t->t_saved_cursor.tp_col < t->t_winsize.tp_col); 243 teken_assert(t->t_scrollreg.ts_end <= t->t_winsize.tp_row); 244 teken_assert(t->t_scrollreg.ts_begin < t->t_scrollreg.ts_end); 245 /* Origin region has to be window size or the same as scrollreg. */ 246 teken_assert((t->t_originreg.ts_begin == t->t_scrollreg.ts_begin && 247 t->t_originreg.ts_end == t->t_scrollreg.ts_end) || 248 (t->t_originreg.ts_begin == 0 && 249 t->t_originreg.ts_end == t->t_winsize.tp_row)); 250} 251 252static void 253teken_input_byte(teken_t *t, unsigned char c) 254{ 255 256#ifdef TEKEN_UTF8 257 /* 258 * UTF-8 handling. 259 */ 260 if ((c & 0x80) == 0x00) { 261 /* One-byte sequence. */ 262 t->t_utf8_left = 0; 263 teken_input_char(t, c); 264 } else if ((c & 0xe0) == 0xc0) { 265 /* Two-byte sequence. */ 266 t->t_utf8_left = 1; 267 t->t_utf8_partial = c & 0x1f; 268 } else if ((c & 0xf0) == 0xe0) { 269 /* Three-byte sequence. */ 270 t->t_utf8_left = 2; 271 t->t_utf8_partial = c & 0x0f; 272 } else if ((c & 0xf8) == 0xf0) { 273 /* Four-byte sequence. */ 274 t->t_utf8_left = 3; 275 t->t_utf8_partial = c & 0x07; 276 } else if ((c & 0xc0) == 0x80) { 277 if (t->t_utf8_left == 0) 278 return; 279 t->t_utf8_left--; 280 t->t_utf8_partial = (t->t_utf8_partial << 6) | (c & 0x3f); 281 if (t->t_utf8_left == 0) { 282 teken_printf("Got UTF-8 char %u\n", t->t_utf8_partial); 283 teken_input_char(t, t->t_utf8_partial); 284 } 285 } 286#else /* !TEKEN_UTF8 */ 287 teken_input_char(t, c); 288#endif /* TEKEN_UTF8 */ 289} 290 291void 292teken_input(teken_t *t, const void *buf, size_t len) 293{ 294 const char *c = buf; 295 296 while (len-- > 0) 297 teken_input_byte(t, *c++); 298} 299 300void 301teken_set_cursor(teken_t *t, const teken_pos_t *p) 302{ 303 304 /* XXX: bounds checking with originreg! */ 305 teken_assert(p->tp_row < t->t_winsize.tp_row); 306 teken_assert(p->tp_col < t->t_winsize.tp_col); 307 308 t->t_cursor = *p; 309} 310 311void 312teken_set_defattr(teken_t *t, const teken_attr_t *a) 313{ 314 315 t->t_curattr = t->t_saved_curattr = t->t_defattr = *a; 316} 317 318void 319teken_set_winsize(teken_t *t, const teken_pos_t *p) 320{ 321 322 teken_assert(p->tp_col <= T_NUMCOL); 323 324 t->t_winsize = *p; 325 /* XXX: bounds checking with cursor/etc! */ 326 t->t_scrollreg.ts_begin = 0; 327 t->t_scrollreg.ts_end = t->t_winsize.tp_row; 328 t->t_originreg = t->t_scrollreg; 329} 330 331/* 332 * State machine. 333 */ 334 335static void 336teken_state_switch(teken_t *t, teken_state_t *s) 337{ 338 339 t->t_nextstate = s; 340 t->t_curnum = 0; 341 t->t_stateflags |= TS_FIRSTDIGIT; 342} 343 344static int 345teken_state_numbers(teken_t *t, teken_char_t c) 346{ 347 348 teken_assert(t->t_curnum < T_NUMSIZE); 349 350 if (c >= '0' && c <= '9') { 351 /* 352 * Don't do math with the default value of 1 when a 353 * custom number is inserted. 354 */ 355 if (t->t_stateflags & TS_FIRSTDIGIT) { 356 t->t_stateflags &= ~TS_FIRSTDIGIT; 357 t->t_nums[t->t_curnum] = 0; 358 } else { 359 t->t_nums[t->t_curnum] *= 10; 360 } 361 362 t->t_nums[t->t_curnum] += c - '0'; 363 return (1); 364 } else if (c == ';') { 365 if (t->t_stateflags & TS_FIRSTDIGIT) 366 t->t_nums[t->t_curnum] = 0; 367 368 /* Only allow a limited set of arguments. */ 369 if (++t->t_curnum == T_NUMSIZE) { 370 teken_state_switch(t, teken_state_init); 371 return (1); 372 } 373 374 t->t_stateflags |= TS_FIRSTDIGIT; 375 return (1); 376 } else { 377 if (t->t_stateflags & TS_FIRSTDIGIT && t->t_curnum > 0) { 378 /* Finish off the last empty argument. */ 379 t->t_nums[t->t_curnum] = 0; 380 t->t_curnum++; 381 } else if ((t->t_stateflags & TS_FIRSTDIGIT) == 0) { 382 /* Also count the last argument. */ 383 t->t_curnum++; 384 } 385 } 386 387 return (0); 388} 389 390#include "teken_state.h" 391