teken.c revision 193184
1186681Sed/*- 2186681Sed * Copyright (c) 2008-2009 Ed Schouten <ed@FreeBSD.org> 3186681Sed * All rights reserved. 4186681Sed * 5186681Sed * Redistribution and use in source and binary forms, with or without 6186681Sed * modification, are permitted provided that the following conditions 7186681Sed * are met: 8186681Sed * 1. Redistributions of source code must retain the above copyright 9186681Sed * notice, this list of conditions and the following disclaimer. 10186681Sed * 2. Redistributions in binary form must reproduce the above copyright 11186681Sed * notice, this list of conditions and the following disclaimer in the 12186681Sed * documentation and/or other materials provided with the distribution. 13186681Sed * 14186681Sed * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15186681Sed * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16186681Sed * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17186681Sed * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18186681Sed * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19186681Sed * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20186681Sed * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21186681Sed * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22186681Sed * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23186681Sed * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24186681Sed * SUCH DAMAGE. 25186681Sed * 26186681Sed * $FreeBSD: head/sys/dev/syscons/teken/teken.c 193184 2009-05-31 19:35:41Z ed $ 27186681Sed */ 28186681Sed 29186681Sed#include <sys/cdefs.h> 30186681Sed#if defined(__FreeBSD__) && defined(_KERNEL) 31186681Sed#include <sys/param.h> 32186681Sed#include <sys/lock.h> 33186681Sed#include <sys/systm.h> 34186681Sed#define teken_assert(x) MPASS(x) 35186681Sed#define teken_printf(x,...) 36186681Sed#else /* !(__FreeBSD__ && _KERNEL) */ 37186681Sed#include <sys/types.h> 38186681Sed#include <assert.h> 39186681Sed#include <inttypes.h> 40186681Sed#include <stdio.h> 41186681Sed#include <string.h> 42186681Sed#define teken_assert(x) assert(x) 43186681Sed#define teken_printf(x,...) do { \ 44186681Sed if (df != NULL) \ 45186681Sed fprintf(df, x, ## __VA_ARGS__); \ 46186681Sed} while (0) 47186681Sed/* debug messages */ 48186681Sedstatic FILE *df; 49186681Sed#endif /* __FreeBSD__ && _KERNEL */ 50186681Sed 51186681Sed#include "teken.h" 52187469Sed 53186681Sed#ifdef TEKEN_UTF8 54186681Sed#include "teken_wcwidth.h" 55186681Sed#else /* !TEKEN_UTF8 */ 56187367Sed#ifdef TEKEN_XTERM 57187469Sed#define teken_wcwidth(c) ((c <= 0x1B) ? -1 : 1) 58187367Sed#else /* !TEKEN_XTERM */ 59187469Sed#define teken_wcwidth(c) (1) 60187367Sed#endif /* TEKEN_XTERM */ 61186681Sed#endif /* TEKEN_UTF8 */ 62186681Sed 63187469Sed#if defined(TEKEN_XTERM) && defined(TEKEN_UTF8) 64187469Sed#include "teken_scs.h" 65187469Sed#else /* !(TEKEN_XTERM && TEKEN_UTF8) */ 66187469Sed#define teken_scs_process(t, c) (c) 67187469Sed#define teken_scs_restore(t) 68187469Sed#define teken_scs_save(t) 69187469Sed#define teken_scs_set(t, g, ts) 70187469Sed#define teken_scs_switch(t, g) 71187469Sed#endif /* TEKEN_XTERM && TEKEN_UTF8 */ 72187469Sed 73186681Sed/* Private flags for teken_format_t. */ 74186681Sed#define TF_REVERSE 0x08 75186681Sed 76186681Sed/* Private flags for t_stateflags. */ 77186681Sed#define TS_FIRSTDIGIT 0x01 /* First numeric digit in escape sequence. */ 78186681Sed#define TS_INSERT 0x02 /* Insert mode. */ 79186681Sed#define TS_AUTOWRAP 0x04 /* Autowrap. */ 80186681Sed#define TS_ORIGIN 0x08 /* Origin mode. */ 81187367Sed#ifdef TEKEN_XTERM 82187367Sed#define TS_WRAPPED 0x10 /* Next character should be printed on col 0. */ 83187367Sed#else /* !TEKEN_XTERM */ 84186729Sed#define TS_WRAPPED 0x00 /* Simple line wrapping. */ 85187367Sed#endif /* TEKEN_XTERM */ 86186681Sed 87186681Sed/* Character that blanks a cell. */ 88186681Sed#define BLANK ' ' 89186681Sed 90186681Sedstatic teken_state_t teken_state_init; 91186681Sed 92186681Sed/* 93186681Sed * Wrappers for hooks. 94186681Sed */ 95186681Sed 96186681Sedstatic inline void 97186681Sedteken_funcs_bell(teken_t *t) 98186681Sed{ 99186681Sed 100186681Sed t->t_funcs->tf_bell(t->t_softc); 101186681Sed} 102186681Sed 103186681Sedstatic inline void 104186681Sedteken_funcs_cursor(teken_t *t) 105186681Sed{ 106186681Sed 107186681Sed teken_assert(t->t_cursor.tp_row < t->t_winsize.tp_row); 108186681Sed teken_assert(t->t_cursor.tp_col < t->t_winsize.tp_col); 109186681Sed 110186681Sed t->t_funcs->tf_cursor(t->t_softc, &t->t_cursor); 111186681Sed} 112186681Sed 113186681Sedstatic inline void 114186681Sedteken_funcs_putchar(teken_t *t, const teken_pos_t *p, teken_char_t c, 115186681Sed const teken_attr_t *a) 116186681Sed{ 117186681Sed teken_attr_t ta; 118186681Sed 119186681Sed teken_assert(p->tp_row < t->t_winsize.tp_row); 120186681Sed teken_assert(p->tp_col < t->t_winsize.tp_col); 121186681Sed 122186681Sed /* Apply inversion. */ 123186681Sed if (a->ta_format & TF_REVERSE) { 124186681Sed ta.ta_format = a->ta_format; 125186681Sed ta.ta_fgcolor = a->ta_bgcolor; 126186681Sed ta.ta_bgcolor = a->ta_fgcolor; 127186681Sed a = &ta; 128186681Sed } 129186681Sed 130186681Sed t->t_funcs->tf_putchar(t->t_softc, p, c, a); 131186681Sed} 132186681Sed 133186681Sedstatic inline void 134186681Sedteken_funcs_fill(teken_t *t, const teken_rect_t *r, 135186681Sed const teken_char_t c, const teken_attr_t *a) 136186681Sed{ 137186681Sed teken_attr_t ta; 138186681Sed 139186681Sed teken_assert(r->tr_end.tp_row > r->tr_begin.tp_row); 140186681Sed teken_assert(r->tr_end.tp_row <= t->t_winsize.tp_row); 141186681Sed teken_assert(r->tr_end.tp_col > r->tr_begin.tp_col); 142186681Sed teken_assert(r->tr_end.tp_col <= t->t_winsize.tp_col); 143186681Sed 144186681Sed /* Apply inversion. */ 145186681Sed if (a->ta_format & TF_REVERSE) { 146186681Sed ta.ta_format = a->ta_format; 147186681Sed ta.ta_fgcolor = a->ta_bgcolor; 148186681Sed ta.ta_bgcolor = a->ta_fgcolor; 149186681Sed a = &ta; 150186681Sed } 151186681Sed 152186681Sed t->t_funcs->tf_fill(t->t_softc, r, c, a); 153186681Sed} 154186681Sed 155186681Sedstatic inline void 156186681Sedteken_funcs_copy(teken_t *t, const teken_rect_t *r, const teken_pos_t *p) 157186681Sed{ 158186681Sed 159186681Sed teken_assert(r->tr_end.tp_row > r->tr_begin.tp_row); 160186681Sed teken_assert(r->tr_end.tp_row <= t->t_winsize.tp_row); 161186681Sed teken_assert(r->tr_end.tp_col > r->tr_begin.tp_col); 162186681Sed teken_assert(r->tr_end.tp_col <= t->t_winsize.tp_col); 163186681Sed teken_assert(p->tp_row + (r->tr_end.tp_row - r->tr_begin.tp_row) <= t->t_winsize.tp_row); 164186681Sed teken_assert(p->tp_col + (r->tr_end.tp_col - r->tr_begin.tp_col) <= t->t_winsize.tp_col); 165186681Sed 166186681Sed t->t_funcs->tf_copy(t->t_softc, r, p); 167186681Sed} 168186681Sed 169186681Sedstatic inline void 170193184Sedteken_funcs_param(teken_t *t, int cmd, unsigned int value) 171186681Sed{ 172186681Sed 173186681Sed t->t_funcs->tf_param(t->t_softc, cmd, value); 174186681Sed} 175186681Sed 176186681Sedstatic inline void 177186681Sedteken_funcs_respond(teken_t *t, const void *buf, size_t len) 178186681Sed{ 179186681Sed 180186681Sed t->t_funcs->tf_respond(t->t_softc, buf, len); 181186681Sed} 182186681Sed 183186681Sed#include "teken_subr.h" 184186681Sed#include "teken_subr_compat.h" 185186681Sed 186186681Sed/* 187186681Sed * Programming interface. 188186681Sed */ 189186681Sed 190186681Sedvoid 191186681Sedteken_init(teken_t *t, const teken_funcs_t *tf, void *softc) 192186681Sed{ 193186681Sed teken_pos_t tp = { .tp_row = 24, .tp_col = 80 }; 194186681Sed 195186681Sed#if !(defined(__FreeBSD__) && defined(_KERNEL)) 196186681Sed df = fopen("teken.log", "w"); 197186681Sed if (df != NULL) 198186681Sed setvbuf(df, NULL, _IOLBF, BUFSIZ); 199186681Sed#endif /* !(__FreeBSD__ && _KERNEL) */ 200186681Sed 201186681Sed t->t_funcs = tf; 202186681Sed t->t_softc = softc; 203186681Sed 204186681Sed t->t_nextstate = teken_state_init; 205186681Sed 206186681Sed t->t_defattr.ta_format = 0; 207186681Sed t->t_defattr.ta_fgcolor = TC_WHITE; 208186681Sed t->t_defattr.ta_bgcolor = TC_BLACK; 209186681Sed teken_subr_do_reset(t); 210186681Sed 211186681Sed#ifdef TEKEN_UTF8 212186681Sed t->t_utf8_left = 0; 213186681Sed#endif /* TEKEN_UTF8 */ 214186681Sed 215186681Sed teken_set_winsize(t, &tp); 216186681Sed} 217186681Sed 218186681Sedstatic void 219186681Sedteken_input_char(teken_t *t, teken_char_t c) 220186681Sed{ 221186681Sed 222186681Sed switch (c) { 223186681Sed case '\0': 224186681Sed break; 225186681Sed case '\a': 226186681Sed teken_subr_bell(t); 227186681Sed break; 228186681Sed case '\b': 229186681Sed teken_subr_backspace(t); 230186681Sed break; 231186681Sed case '\n': 232186681Sed case '\x0B': 233186681Sed teken_subr_newline(t); 234186681Sed break; 235186798Sed case '\x0C': 236186798Sed teken_subr_newpage(t); 237186798Sed break; 238187469Sed#if defined(TEKEN_XTERM) && defined(TEKEN_UTF8) 239187469Sed case '\x0E': 240187469Sed teken_scs_switch(t, 1); 241187469Sed break; 242187469Sed case '\x0F': 243187469Sed teken_scs_switch(t, 0); 244187469Sed break; 245187469Sed#endif /* TEKEN_XTERM && TEKEN_UTF8 */ 246186681Sed case '\r': 247186681Sed teken_subr_carriage_return(t); 248186681Sed break; 249186681Sed case '\t': 250186681Sed teken_subr_horizontal_tab(t); 251186681Sed break; 252186681Sed default: 253186681Sed t->t_nextstate(t, c); 254186681Sed break; 255186681Sed } 256186681Sed 257186681Sed /* Post-processing assertions. */ 258186681Sed teken_assert(t->t_cursor.tp_row >= t->t_originreg.ts_begin); 259186681Sed teken_assert(t->t_cursor.tp_row < t->t_originreg.ts_end); 260186681Sed teken_assert(t->t_cursor.tp_row < t->t_winsize.tp_row); 261186681Sed teken_assert(t->t_cursor.tp_col < t->t_winsize.tp_col); 262186681Sed teken_assert(t->t_saved_cursor.tp_row < t->t_winsize.tp_row); 263186681Sed teken_assert(t->t_saved_cursor.tp_col < t->t_winsize.tp_col); 264186681Sed teken_assert(t->t_scrollreg.ts_end <= t->t_winsize.tp_row); 265186681Sed teken_assert(t->t_scrollreg.ts_begin < t->t_scrollreg.ts_end); 266186681Sed /* Origin region has to be window size or the same as scrollreg. */ 267186681Sed teken_assert((t->t_originreg.ts_begin == t->t_scrollreg.ts_begin && 268186681Sed t->t_originreg.ts_end == t->t_scrollreg.ts_end) || 269186681Sed (t->t_originreg.ts_begin == 0 && 270186681Sed t->t_originreg.ts_end == t->t_winsize.tp_row)); 271186681Sed} 272186681Sed 273186681Sedstatic void 274186681Sedteken_input_byte(teken_t *t, unsigned char c) 275186681Sed{ 276186681Sed 277186681Sed#ifdef TEKEN_UTF8 278186681Sed /* 279186681Sed * UTF-8 handling. 280186681Sed */ 281186681Sed if ((c & 0x80) == 0x00) { 282186681Sed /* One-byte sequence. */ 283186681Sed t->t_utf8_left = 0; 284186681Sed teken_input_char(t, c); 285186681Sed } else if ((c & 0xe0) == 0xc0) { 286186681Sed /* Two-byte sequence. */ 287186681Sed t->t_utf8_left = 1; 288186681Sed t->t_utf8_partial = c & 0x1f; 289186681Sed } else if ((c & 0xf0) == 0xe0) { 290186681Sed /* Three-byte sequence. */ 291186681Sed t->t_utf8_left = 2; 292186681Sed t->t_utf8_partial = c & 0x0f; 293186681Sed } else if ((c & 0xf8) == 0xf0) { 294186681Sed /* Four-byte sequence. */ 295186681Sed t->t_utf8_left = 3; 296186681Sed t->t_utf8_partial = c & 0x07; 297186681Sed } else if ((c & 0xc0) == 0x80) { 298186681Sed if (t->t_utf8_left == 0) 299186681Sed return; 300186681Sed t->t_utf8_left--; 301186681Sed t->t_utf8_partial = (t->t_utf8_partial << 6) | (c & 0x3f); 302186681Sed if (t->t_utf8_left == 0) { 303186681Sed teken_printf("Got UTF-8 char %u\n", t->t_utf8_partial); 304186681Sed teken_input_char(t, t->t_utf8_partial); 305186681Sed } 306186681Sed } 307186681Sed#else /* !TEKEN_UTF8 */ 308186681Sed teken_input_char(t, c); 309186681Sed#endif /* TEKEN_UTF8 */ 310186681Sed} 311186681Sed 312186681Sedvoid 313186681Sedteken_input(teken_t *t, const void *buf, size_t len) 314186681Sed{ 315186681Sed const char *c = buf; 316186681Sed 317186681Sed while (len-- > 0) 318186681Sed teken_input_byte(t, *c++); 319186681Sed} 320186681Sed 321186681Sedvoid 322186681Sedteken_set_cursor(teken_t *t, const teken_pos_t *p) 323186681Sed{ 324186681Sed 325186681Sed /* XXX: bounds checking with originreg! */ 326186681Sed teken_assert(p->tp_row < t->t_winsize.tp_row); 327186681Sed teken_assert(p->tp_col < t->t_winsize.tp_col); 328186681Sed 329186681Sed t->t_cursor = *p; 330186681Sed} 331186681Sed 332188391Sedconst teken_attr_t * 333188391Sedteken_get_curattr(teken_t *t) 334188391Sed{ 335188391Sed 336188391Sed return (&t->t_curattr); 337188391Sed} 338188391Sed 339189617Sedvoid 340189617Sedteken_set_curattr(teken_t *t, const teken_attr_t *a) 341189617Sed{ 342189617Sed 343189617Sed t->t_curattr = *a; 344189617Sed} 345189617Sed 346188391Sedconst teken_attr_t * 347188391Sedteken_get_defattr(teken_t *t) 348188391Sed{ 349188391Sed 350188391Sed return (&t->t_defattr); 351188391Sed} 352188391Sed 353186681Sedvoid 354186681Sedteken_set_defattr(teken_t *t, const teken_attr_t *a) 355186681Sed{ 356186681Sed 357186681Sed t->t_curattr = t->t_saved_curattr = t->t_defattr = *a; 358186681Sed} 359186681Sed 360186681Sedvoid 361186681Sedteken_set_winsize(teken_t *t, const teken_pos_t *p) 362186681Sed{ 363186681Sed 364186681Sed t->t_winsize = *p; 365186681Sed /* XXX: bounds checking with cursor/etc! */ 366186681Sed t->t_scrollreg.ts_begin = 0; 367186681Sed t->t_scrollreg.ts_end = t->t_winsize.tp_row; 368186681Sed t->t_originreg = t->t_scrollreg; 369186681Sed} 370186681Sed 371186681Sed/* 372186681Sed * State machine. 373186681Sed */ 374186681Sed 375186681Sedstatic void 376186681Sedteken_state_switch(teken_t *t, teken_state_t *s) 377186681Sed{ 378186681Sed 379186681Sed t->t_nextstate = s; 380186681Sed t->t_curnum = 0; 381186681Sed t->t_stateflags |= TS_FIRSTDIGIT; 382186681Sed} 383186681Sed 384186681Sedstatic int 385186681Sedteken_state_numbers(teken_t *t, teken_char_t c) 386186681Sed{ 387186681Sed 388186681Sed teken_assert(t->t_curnum < T_NUMSIZE); 389186681Sed 390186681Sed if (c >= '0' && c <= '9') { 391186681Sed /* 392186681Sed * Don't do math with the default value of 1 when a 393186681Sed * custom number is inserted. 394186681Sed */ 395186681Sed if (t->t_stateflags & TS_FIRSTDIGIT) { 396186681Sed t->t_stateflags &= ~TS_FIRSTDIGIT; 397186681Sed t->t_nums[t->t_curnum] = 0; 398186681Sed } else { 399186681Sed t->t_nums[t->t_curnum] *= 10; 400186681Sed } 401186681Sed 402186681Sed t->t_nums[t->t_curnum] += c - '0'; 403186681Sed return (1); 404186681Sed } else if (c == ';') { 405186681Sed if (t->t_stateflags & TS_FIRSTDIGIT) 406186681Sed t->t_nums[t->t_curnum] = 0; 407186681Sed 408186681Sed /* Only allow a limited set of arguments. */ 409186681Sed if (++t->t_curnum == T_NUMSIZE) { 410186681Sed teken_state_switch(t, teken_state_init); 411186681Sed return (1); 412186681Sed } 413186681Sed 414186681Sed t->t_stateflags |= TS_FIRSTDIGIT; 415186681Sed return (1); 416186681Sed } else { 417186681Sed if (t->t_stateflags & TS_FIRSTDIGIT && t->t_curnum > 0) { 418186681Sed /* Finish off the last empty argument. */ 419186681Sed t->t_nums[t->t_curnum] = 0; 420186681Sed t->t_curnum++; 421186681Sed } else if ((t->t_stateflags & TS_FIRSTDIGIT) == 0) { 422186681Sed /* Also count the last argument. */ 423186681Sed t->t_curnum++; 424186681Sed } 425186681Sed } 426186681Sed 427186681Sed return (0); 428186681Sed} 429186681Sed 430186681Sed#include "teken_state.h" 431