teken.c revision 188391
11590Srgrimes/*- 21590Srgrimes * Copyright (c) 2008-2009 Ed Schouten <ed@FreeBSD.org> 31590Srgrimes * All rights reserved. 41590Srgrimes * 51590Srgrimes * Redistribution and use in source and binary forms, with or without 61590Srgrimes * modification, are permitted provided that the following conditions 71590Srgrimes * are met: 81590Srgrimes * 1. Redistributions of source code must retain the above copyright 91590Srgrimes * notice, this list of conditions and the following disclaimer. 101590Srgrimes * 2. Redistributions in binary form must reproduce the above copyright 111590Srgrimes * notice, this list of conditions and the following disclaimer in the 12263142Seadler * documentation and/or other materials provided with the distribution. 131590Srgrimes * 141590Srgrimes * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 151590Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 161590Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 171590Srgrimes * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 181590Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 191590Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 201590Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 211590Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 221590Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 231590Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 241590Srgrimes * SUCH DAMAGE. 251590Srgrimes * 261590Srgrimes * $FreeBSD: head/sys/dev/syscons/teken/teken.c 188391 2009-02-09 15:55:21Z ed $ 271590Srgrimes */ 281590Srgrimes 291590Srgrimes#include <sys/cdefs.h> 301590Srgrimes#if defined(__FreeBSD__) && defined(_KERNEL) 311590Srgrimes#include <sys/param.h> 321590Srgrimes#include <sys/lock.h> 331590Srgrimes#include <sys/systm.h> 341590Srgrimes#define teken_assert(x) MPASS(x) 351590Srgrimes#define teken_printf(x,...) 361590Srgrimes#else /* !(__FreeBSD__ && _KERNEL) */ 371590Srgrimes#include <sys/types.h> 381590Srgrimes#include <assert.h> 391590Srgrimes#include <inttypes.h> 401590Srgrimes#include <stdio.h> 411590Srgrimes#include <string.h> 421590Srgrimes#define teken_assert(x) assert(x) 431590Srgrimes#define teken_printf(x,...) do { \ 441590Srgrimes if (df != NULL) \ 451590Srgrimes fprintf(df, x, ## __VA_ARGS__); \ 461590Srgrimes} while (0) 471590Srgrimes/* debug messages */ 481590Srgrimesstatic FILE *df; 491590Srgrimes#endif /* __FreeBSD__ && _KERNEL */ 501590Srgrimes 511590Srgrimes#include "teken.h" 521590Srgrimes 531590Srgrimes#ifdef TEKEN_UTF8 541590Srgrimes#include "teken_wcwidth.h" 551590Srgrimes#else /* !TEKEN_UTF8 */ 561590Srgrimes#ifdef TEKEN_XTERM 571590Srgrimes#define teken_wcwidth(c) ((c <= 0x1B) ? -1 : 1) 581590Srgrimes#else /* !TEKEN_XTERM */ 591590Srgrimes#define teken_wcwidth(c) (1) 601590Srgrimes#endif /* TEKEN_XTERM */ 611590Srgrimes#endif /* TEKEN_UTF8 */ 621590Srgrimes 631590Srgrimes#if defined(TEKEN_XTERM) && defined(TEKEN_UTF8) 641590Srgrimes#include "teken_scs.h" 651590Srgrimes#else /* !(TEKEN_XTERM && TEKEN_UTF8) */ 661590Srgrimes#define teken_scs_process(t, c) (c) 671590Srgrimes#define teken_scs_restore(t) 681590Srgrimes#define teken_scs_save(t) 691590Srgrimes#define teken_scs_set(t, g, ts) 701590Srgrimes#define teken_scs_switch(t, g) 711590Srgrimes#endif /* TEKEN_XTERM && TEKEN_UTF8 */ 721590Srgrimes 731590Srgrimes/* Private flags for teken_format_t. */ 741590Srgrimes#define TF_REVERSE 0x08 751590Srgrimes 761590Srgrimes/* Private flags for t_stateflags. */ 771590Srgrimes#define TS_FIRSTDIGIT 0x01 /* First numeric digit in escape sequence. */ 781590Srgrimes#define TS_INSERT 0x02 /* Insert mode. */ 791590Srgrimes#define TS_AUTOWRAP 0x04 /* Autowrap. */ 801590Srgrimes#define TS_ORIGIN 0x08 /* Origin mode. */ 811590Srgrimes#ifdef TEKEN_XTERM 821590Srgrimes#define TS_WRAPPED 0x10 /* Next character should be printed on col 0. */ 831590Srgrimes#else /* !TEKEN_XTERM */ 841590Srgrimes#define TS_WRAPPED 0x00 /* Simple line wrapping. */ 851590Srgrimes#endif /* TEKEN_XTERM */ 861590Srgrimes 871590Srgrimes/* Character that blanks a cell. */ 881590Srgrimes#define BLANK ' ' 891590Srgrimes 901590Srgrimesstatic teken_state_t teken_state_init; 911590Srgrimes 921590Srgrimes/* 931590Srgrimes * Wrappers for hooks. 941590Srgrimes */ 951590Srgrimes 961590Srgrimesstatic inline void 971590Srgrimesteken_funcs_bell(teken_t *t) 981590Srgrimes{ 991590Srgrimes 1001590Srgrimes t->t_funcs->tf_bell(t->t_softc); 1011590Srgrimes} 1021590Srgrimes 1031590Srgrimesstatic inline void 1041590Srgrimesteken_funcs_cursor(teken_t *t) 1051590Srgrimes{ 1061590Srgrimes 1071590Srgrimes teken_assert(t->t_cursor.tp_row < t->t_winsize.tp_row); 1081590Srgrimes teken_assert(t->t_cursor.tp_col < t->t_winsize.tp_col); 1091590Srgrimes 1101590Srgrimes t->t_funcs->tf_cursor(t->t_softc, &t->t_cursor); 1111590Srgrimes} 1121590Srgrimes 1131590Srgrimesstatic inline void 1141590Srgrimesteken_funcs_putchar(teken_t *t, const teken_pos_t *p, teken_char_t c, 1151590Srgrimes const teken_attr_t *a) 1161590Srgrimes{ 1171590Srgrimes teken_attr_t ta; 1181590Srgrimes 1191590Srgrimes teken_assert(p->tp_row < t->t_winsize.tp_row); 1201590Srgrimes teken_assert(p->tp_col < t->t_winsize.tp_col); 1211590Srgrimes 1221590Srgrimes /* Apply inversion. */ 1231590Srgrimes if (a->ta_format & TF_REVERSE) { 1241590Srgrimes ta.ta_format = a->ta_format; 1251590Srgrimes ta.ta_fgcolor = a->ta_bgcolor; 1261590Srgrimes ta.ta_bgcolor = a->ta_fgcolor; 1271590Srgrimes a = &ta; 1281590Srgrimes } 1291590Srgrimes 1301590Srgrimes t->t_funcs->tf_putchar(t->t_softc, p, c, a); 1311590Srgrimes} 1321590Srgrimes 1331590Srgrimesstatic inline void 1341590Srgrimesteken_funcs_fill(teken_t *t, const teken_rect_t *r, 1351590Srgrimes const teken_char_t c, const teken_attr_t *a) 1361590Srgrimes{ 1371590Srgrimes teken_attr_t ta; 1381590Srgrimes 1391590Srgrimes teken_assert(r->tr_end.tp_row > r->tr_begin.tp_row); 1401590Srgrimes teken_assert(r->tr_end.tp_row <= t->t_winsize.tp_row); 1411590Srgrimes teken_assert(r->tr_end.tp_col > r->tr_begin.tp_col); 1421590Srgrimes teken_assert(r->tr_end.tp_col <= t->t_winsize.tp_col); 1431590Srgrimes 1441590Srgrimes /* Apply inversion. */ 1451590Srgrimes if (a->ta_format & TF_REVERSE) { 1461590Srgrimes ta.ta_format = a->ta_format; 1471590Srgrimes ta.ta_fgcolor = a->ta_bgcolor; 1481590Srgrimes ta.ta_bgcolor = a->ta_fgcolor; 1491590Srgrimes a = &ta; 1501590Srgrimes } 1511590Srgrimes 1521590Srgrimes t->t_funcs->tf_fill(t->t_softc, r, c, a); 1531590Srgrimes} 1541590Srgrimes 1551590Srgrimesstatic inline void 1561590Srgrimesteken_funcs_copy(teken_t *t, const teken_rect_t *r, const teken_pos_t *p) 1571590Srgrimes{ 1581590Srgrimes 1591590Srgrimes teken_assert(r->tr_end.tp_row > r->tr_begin.tp_row); 1601590Srgrimes teken_assert(r->tr_end.tp_row <= t->t_winsize.tp_row); 1611590Srgrimes teken_assert(r->tr_end.tp_col > r->tr_begin.tp_col); 1621590Srgrimes teken_assert(r->tr_end.tp_col <= t->t_winsize.tp_col); 1631590Srgrimes teken_assert(p->tp_row + (r->tr_end.tp_row - r->tr_begin.tp_row) <= t->t_winsize.tp_row); 1641590Srgrimes teken_assert(p->tp_col + (r->tr_end.tp_col - r->tr_begin.tp_col) <= t->t_winsize.tp_col); 1651590Srgrimes 1661590Srgrimes t->t_funcs->tf_copy(t->t_softc, r, p); 1671590Srgrimes} 1681590Srgrimes 1691590Srgrimesstatic inline void 1701590Srgrimesteken_funcs_param(teken_t *t, int cmd, int value) 1711590Srgrimes{ 1721590Srgrimes 1731590Srgrimes t->t_funcs->tf_param(t->t_softc, cmd, value); 1741590Srgrimes} 1751590Srgrimes 1761590Srgrimesstatic inline void 1771590Srgrimesteken_funcs_respond(teken_t *t, const void *buf, size_t len) 1781590Srgrimes{ 1791590Srgrimes 1801590Srgrimes t->t_funcs->tf_respond(t->t_softc, buf, len); 1811590Srgrimes} 1821590Srgrimes 1831590Srgrimes#include "teken_subr.h" 1841590Srgrimes#include "teken_subr_compat.h" 18528969Swosch 1861590Srgrimes/* 1871590Srgrimes * Programming interface. 1881590Srgrimes */ 1891590Srgrimes 1901590Srgrimesvoid 1911590Srgrimesteken_init(teken_t *t, const teken_funcs_t *tf, void *softc) 1921590Srgrimes{ 1931590Srgrimes teken_pos_t tp = { .tp_row = 24, .tp_col = 80 }; 1941590Srgrimes 1951590Srgrimes#if !(defined(__FreeBSD__) && defined(_KERNEL)) 1961590Srgrimes df = fopen("teken.log", "w"); 1971590Srgrimes if (df != NULL) 1981590Srgrimes setvbuf(df, NULL, _IOLBF, BUFSIZ); 1991590Srgrimes#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 218static void 219teken_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 273static void 274teken_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 %u\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 312void 313teken_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 321void 322teken_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 332const teken_attr_t * 333teken_get_curattr(teken_t *t) 334{ 335 336 return (&t->t_curattr); 337} 338 339const teken_attr_t * 340teken_get_defattr(teken_t *t) 341{ 342 343 return (&t->t_defattr); 344} 345 346void 347teken_set_defattr(teken_t *t, const teken_attr_t *a) 348{ 349 350 t->t_curattr = t->t_saved_curattr = t->t_defattr = *a; 351} 352 353void 354teken_set_winsize(teken_t *t, const teken_pos_t *p) 355{ 356 357 teken_assert(p->tp_col <= T_NUMCOL); 358 359 t->t_winsize = *p; 360 /* XXX: bounds checking with cursor/etc! */ 361 t->t_scrollreg.ts_begin = 0; 362 t->t_scrollreg.ts_end = t->t_winsize.tp_row; 363 t->t_originreg = t->t_scrollreg; 364} 365 366/* 367 * State machine. 368 */ 369 370static void 371teken_state_switch(teken_t *t, teken_state_t *s) 372{ 373 374 t->t_nextstate = s; 375 t->t_curnum = 0; 376 t->t_stateflags |= TS_FIRSTDIGIT; 377} 378 379static int 380teken_state_numbers(teken_t *t, teken_char_t c) 381{ 382 383 teken_assert(t->t_curnum < T_NUMSIZE); 384 385 if (c >= '0' && c <= '9') { 386 /* 387 * Don't do math with the default value of 1 when a 388 * custom number is inserted. 389 */ 390 if (t->t_stateflags & TS_FIRSTDIGIT) { 391 t->t_stateflags &= ~TS_FIRSTDIGIT; 392 t->t_nums[t->t_curnum] = 0; 393 } else { 394 t->t_nums[t->t_curnum] *= 10; 395 } 396 397 t->t_nums[t->t_curnum] += c - '0'; 398 return (1); 399 } else if (c == ';') { 400 if (t->t_stateflags & TS_FIRSTDIGIT) 401 t->t_nums[t->t_curnum] = 0; 402 403 /* Only allow a limited set of arguments. */ 404 if (++t->t_curnum == T_NUMSIZE) { 405 teken_state_switch(t, teken_state_init); 406 return (1); 407 } 408 409 t->t_stateflags |= TS_FIRSTDIGIT; 410 return (1); 411 } else { 412 if (t->t_stateflags & TS_FIRSTDIGIT && t->t_curnum > 0) { 413 /* Finish off the last empty argument. */ 414 t->t_nums[t->t_curnum] = 0; 415 t->t_curnum++; 416 } else if ((t->t_stateflags & TS_FIRSTDIGIT) == 0) { 417 /* Also count the last argument. */ 418 t->t_curnum++; 419 } 420 } 421 422 return (0); 423} 424 425#include "teken_state.h" 426