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 unchanged lines hidden (view full) --- 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/teken/teken.c 197115 2009-09-12 10:34:34Z 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) --- 9 unchanged lines hidden (view full) --- 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#include "teken_wcwidth.h" |
53 |
54#ifdef TEKEN_XTERM |
55#include "teken_scs.h" |
56#else /* !TEKEN_XTERM */ |
57#define teken_scs_process(t, c) (c) 58#define teken_scs_restore(t) 59#define teken_scs_save(t) 60#define teken_scs_set(t, g, ts) 61#define teken_scs_switch(t, g) |
62#endif /* TEKEN_XTERM */ |
63 64/* Private flags for t_stateflags. */ 65#define TS_FIRSTDIGIT 0x01 /* First numeric digit in escape sequence. */ 66#define TS_INSERT 0x02 /* Insert mode. */ 67#define TS_AUTOWRAP 0x04 /* Autowrap. */ 68#define TS_ORIGIN 0x08 /* Origin mode. */ 69#ifdef TEKEN_XTERM 70#define TS_WRAPPED 0x10 /* Next character should be printed on col 0. */ --- 102 unchanged lines hidden (view full) --- 173 174 t->t_nextstate = teken_state_init; 175 176 t->t_defattr.ta_format = 0; 177 t->t_defattr.ta_fgcolor = TC_WHITE; 178 t->t_defattr.ta_bgcolor = TC_BLACK; 179 teken_subr_do_reset(t); 180 |
181 t->t_utf8_left = 0; |
182 183 teken_set_winsize(t, &tp); 184} 185 186static void 187teken_input_char(teken_t *t, teken_char_t c) 188{ 189 --- 8 unchanged lines hidden (view full) --- 198 break; 199 case '\n': 200 case '\x0B': 201 teken_subr_newline(t); 202 break; 203 case '\x0C': 204 teken_subr_newpage(t); 205 break; |
206#ifdef TEKEN_XTERM |
207 case '\x0E': 208 teken_scs_switch(t, 1); 209 break; 210 case '\x0F': 211 teken_scs_switch(t, 0); 212 break; |
213#endif /* TEKEN_XTERM */ |
214 case '\r': 215 teken_subr_carriage_return(t); 216 break; 217 case '\t': 218 teken_subr_horizontal_tab(t); 219 break; 220 default: 221 t->t_nextstate(t, c); --- 15 unchanged lines hidden (view full) --- 237 (t->t_originreg.ts_begin == 0 && 238 t->t_originreg.ts_end == t->t_winsize.tp_row)); 239} 240 241static void 242teken_input_byte(teken_t *t, unsigned char c) 243{ 244 |
245 /* 246 * UTF-8 handling. 247 */ |
248 if (t->t_utf8_left == -1) { 249 /* UTF-8 disabled. */ 250 teken_input_char(t, c); 251 } else if ((c & 0x80) == 0x00) { |
252 /* One-byte sequence. */ 253 t->t_utf8_left = 0; 254 teken_input_char(t, c); 255 } else if ((c & 0xe0) == 0xc0) { 256 /* Two-byte sequence. */ 257 t->t_utf8_left = 1; 258 t->t_utf8_partial = c & 0x1f; 259 } else if ((c & 0xf0) == 0xe0) { --- 9 unchanged lines hidden (view full) --- 269 return; 270 t->t_utf8_left--; 271 t->t_utf8_partial = (t->t_utf8_partial << 6) | (c & 0x3f); 272 if (t->t_utf8_left == 0) { 273 teken_printf("Got UTF-8 char %x\n", t->t_utf8_partial); 274 teken_input_char(t, t->t_utf8_partial); 275 } 276 } |
277} 278 279void 280teken_input(teken_t *t, const void *buf, size_t len) 281{ 282 const char *c = buf; 283 284 while (len-- > 0) --- 42 unchanged lines hidden (view full) --- 327void 328teken_set_winsize(teken_t *t, const teken_pos_t *p) 329{ 330 331 t->t_winsize = *p; 332 teken_subr_do_reset(t); 333} 334 |
335void 336teken_set_8bit(teken_t *t) 337{ 338 339 t->t_utf8_left = -1; 340} 341 |
342/* 343 * State machine. 344 */ 345 346static void 347teken_state_switch(teken_t *t, teken_state_t *s) 348{ 349 --- 52 unchanged lines hidden --- |