datpath: Avoid reporting half updated statistics.
[sliver-openvswitch.git] / extras / ezio / terminal.c
1 /* Copyright (c) 2008, 2009, 2010 Nicira Networks, Inc.
2  *
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at:
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15
16 #include <config.h>
17 #include "terminal.h"
18 #include <assert.h>
19 #include <errno.h>
20 #include <inttypes.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <unistd.h>
25 #include "dynamic-string.h"
26 #include "ezio.h"
27 #include "poll-loop.h"
28 #include "util.h"
29 #include "vlog.h"
30
31 VLOG_DEFINE_THIS_MODULE(terminal)
32
33 /* UTF-8 decoding. */
34 static struct utf8_reader *utf8_reader_create(void);
35 static void utf8_reader_destroy(struct utf8_reader *);
36 static int utf8_reader_read(struct utf8_reader *, uint8_t c);
37
38 /* ANSI escape sequence decoding. */
39 struct ansi_sequence {
40     int n_args;
41 #define ANSI_MAX_ARGS 16
42     int args[ANSI_MAX_ARGS];
43     int function;
44 };
45
46 static struct ansi_decoder *ansi_decoder_create(void);
47 static void ansi_decoder_destroy(struct ansi_decoder *);
48 static int ansi_decoder_put(struct ansi_decoder *, uint8_t c);
49 static const struct ansi_sequence *ansi_decoder_get(struct ansi_decoder *);
50 \f
51 /* Terminal emulation. */
52 struct terminal {
53     struct ansi_decoder *ansi;
54     struct utf8_reader *utf8;
55     enum { EZIO, UTF8 } encoding;
56 };
57
58 static void recv_byte(struct terminal *term, struct ezio *ezio, uint8_t c);
59
60 struct terminal *
61 terminal_create(void)
62 {
63     struct terminal *term = xmalloc(sizeof *term);
64     term->ansi = ansi_decoder_create();
65     term->utf8 = utf8_reader_create();
66     term->encoding = UTF8;
67     return term;
68 }
69
70 void
71 terminal_destroy(struct terminal *term)
72 {
73     if (term) {
74         utf8_reader_destroy(term->utf8);
75         ansi_decoder_destroy(term->ansi);
76         free(term);
77     }
78 }
79
80 int
81 terminal_run(struct terminal *term, struct ezio *ezio, int input_fd)
82 {
83     char input[512];
84     int n;
85
86     n = read(input_fd, input, sizeof input);
87     if (n > 0) {
88         int i;
89
90         for (i = 0; i < n; i++) {
91             recv_byte(term, ezio, input[i]);
92         }
93         return 0;
94     } else {
95         return !n ? EOF : errno == EAGAIN ? 0 : errno;
96     }
97 }
98
99 void
100 terminal_wait(struct terminal *term OVS_UNUSED, int input_fd)
101 {
102     poll_fd_wait(input_fd, POLLIN);
103 }
104 \f
105 static void recv_ansi_sequence(const struct ansi_sequence *, struct ezio *);
106 static void recv_control(uint8_t c, struct ezio *);
107 static void recv_character(uint8_t byte, struct ezio *);
108 static int unicode_to_ezio(uint16_t unicode);
109 static int default_arg(int value, int default_value);
110 static int range(int value, int min, int max);
111 static void clear_elements(uint8_t *p, size_t size, int pos, int clear_type);
112 static void define_icon(struct ezio *e, const int *args);
113 static void clear_icon(struct ezio *e, int icon_nr);
114 static void set_cursor(struct ezio *e, int visibility);
115
116 static void
117 recv_byte(struct terminal *term, struct ezio *ezio, uint8_t c)
118 {
119     int retval;
120
121     /* Decode and interpret ANSI escape sequences. */
122     retval = ansi_decoder_put(term->ansi, c);
123     if (retval <= 0) {
124         if (retval < 0) {
125             recv_ansi_sequence(ansi_decoder_get(term->ansi), ezio);
126             return;
127         }
128         return;
129     }
130
131     /* Encoding selection. */
132     if (c == 0x0e) {
133         /* Shift Out. */
134         term->encoding = EZIO;
135         return;
136     } else if (c == 0x0f) {
137         /* Shift In. */
138         term->encoding = UTF8;
139         return;
140     }
141
142     if (term->encoding == UTF8) {
143         int unicode, ezchar;
144
145         /* Convert UTF-8 input to Unicode code point. */
146         unicode = utf8_reader_read(term->utf8, c);
147         if (unicode < 0) {
148             return;
149         }
150
151         /* Convert Unicode code point to EZIO encoding. */
152         ezchar = unicode_to_ezio(unicode);
153         if (ezchar >= 0) {
154             if (ezchar & 0xff00) {
155                 recv_character(ezchar >> 8, ezio);
156             }
157             recv_character(ezchar, ezio);
158         } else if (unicode < 0x100) {
159             recv_control(unicode, ezio);
160         } else {
161             /* Unsupported Unicode code point. */
162             return;
163         }
164     } else {
165         if (c >= 0x80 && c < 0x87) {
166             c &= 0x07;
167         }
168         if (c != 0xfe) {
169             recv_character(c, ezio);
170         }
171     }
172 }
173
174 static void
175 log_ansi_sequence(const struct ansi_sequence *seq, struct ezio *e)
176 {
177     struct sequence {
178         int function;
179         const char *name;
180     };
181     static const struct sequence sequences[] = {
182         {0x5a, "CBT: Cursor Backward Tabulation"},
183         {0x47, "CHA: Cursor Character Absolute"},
184         {0x49, "CHT: Cursor Forward Tabulation"},
185         {0x45, "CNL: Cursor Next Line"},
186         {0x46, "CPL: Cursor Preceding Line"},
187         {0x44, "CUB: Cursor Left"},
188         {0x42, "CUD: Cursor Down"},
189         {0x43, "CUF: Cursor Right"},
190         {0x48, "CUP: Cursor Position"},
191         {0x41, "CUU: Cursor Up"},
192         {0x50, "DCH: Delete Character"},
193         {0x4d, "DL: Delete Line"},
194         {0x58, "ECH: Erase Character"},
195         {0x4a, "ED: Erase in Page"},
196         {0x4b, "EL: Erase in Line"},
197         {0x40, "ICH: Insert Character"},
198         {0x4c, "IL: Insert Line"},
199         {0x4500, "NEL: Next Line"},
200         {0x4d00, "RI: Reverse Line Feed"},
201         {0x6300, "RIS: Reset to Initial State"},
202         {0x54, "SD: Scroll Down"},
203         {0x240, "SL: Scroll Left"},
204         {0x241, "SR: Scroll Right"},
205         {0x53, "SU: Scroll Up"},
206         {0x70, "DICO: Define Icon"},
207         {0x71, "CICO: Clear Icon"},
208         {0x72, "Set cursor visibility"},
209     };
210     const struct sequence *s;
211     struct ds ds;
212     int i;
213
214     ds_init(&ds);
215     for (s = sequences; s < &sequences[ARRAY_SIZE(sequences)]; s++) {
216         if (s->function == seq->function) {
217             ds_put_cstr(&ds, s->name);
218             goto found;
219         }
220     }
221     ds_put_format(&ds, "0x%02x", s->function);
222     if (s->function < 0x100) {
223         ds_put_format(&ds, "(%02d/%02d)", s->function / 16, s->function % 16);
224     }
225
226 found:
227     for (i = 0; i < seq->n_args; i++) {
228         ds_put_format(&ds, ", %d", seq->args[i]);
229     }
230     VLOG_DBG("%s (cursor:%d,%d)", ds_cstr(&ds), e->x, e->y);
231     ds_destroy(&ds);
232 }
233
234 static void
235 recv_ansi_sequence(const struct ansi_sequence *seq, struct ezio *e)
236 {
237 #define ARG1(DEFAULT) default_arg(seq->args[0], DEFAULT)
238 #define ARG2(DEFAULT) default_arg(seq->args[1], DEFAULT)
239     if (VLOG_IS_DBG_ENABLED()) {
240         log_ansi_sequence(seq, e);
241     }
242     switch (seq->function) {
243     case 0x5a: /* CBT: Cursor Backward Tabulation. */
244         e->x = 8 * (e->x / 8 - ARG1(1));
245         break;
246     case 0x47: /* CHA: Cursor Character Absolute. */
247         e->x = ARG1(1) - 1;
248         break;
249     case 0x49: /* CHT: Cursor Forward Tabulation. */
250         e->x = 8 * (e->x / 8 + ARG1(1));
251         break;
252     case 0x45: /* CNL: Cursor Next Line. */
253         e->x = 0;
254         e->y += ARG1(1);
255         break;
256     case 0x46: /* CPL: Cursor Preceding Line. */
257         e->x = 0;
258         e->y -= ARG1(1);
259         break;
260     case 0x44: /* CUB: Cursor Left. */
261         e->x -= ARG1(1);
262         break;
263     case 0x42: /* CUD: Cursor Down. */
264         e->y += ARG1(1);
265         break;
266     case 0x43: /* CUF: Cursor Right. */
267         e->x += ARG1(1);
268         break;
269     case 0x48: /* CUP: Cursor Position. */
270         e->y = ARG1(1) - 1;
271         e->x = ARG2(1) - 1;
272         break;
273     case 0x41: /* CUU: Cursor Up. */
274         e->y -= ARG1(1);
275         break;
276     case 0x50: /* DCH: Delete Character. */
277         ezio_delete_char(e, e->x, e->y, ARG1(1));
278         break;
279     case 0x4d: /* DL: Delete Line. */
280         ezio_delete_line(e, e->y, ARG1(1));
281         break;
282     case 0x58: /* ECH: Erase Character. */
283         memset(&e->chars[e->y][e->x], ' ', MIN(ARG1(1), 40 - e->x));
284         break;
285     case 0x4a: /* ED: Erase in Page. */
286         clear_elements(&e->chars[0][0], 2 * 40, e->x + 40 * e->y, ARG1(0));
287         break;
288     case 0x4b: /* EL: Erase in Line. */
289         clear_elements(&e->chars[e->y][0], 40, e->x, ARG1(0));
290         break;
291     case 0x40: /* ICH: Insert Character. */
292         ezio_insert_char(e, e->x, e->y, ARG1(1));
293         break;
294     case 0x4c: /* IL: Insert Line. */
295         ezio_insert_line(e, e->y, ARG1(1));
296         break;
297     case 0x4500: /* NEL: Next Line. */
298         e->x = 0;
299         e->y++;
300         break;
301     case 0x4d00: /* RI: Reverse Line Feed. */
302         e->y--;
303         break;
304     case 0x6300: /* RIS: Reset to Initial State. */
305         ezio_init(e);
306         break;
307     case 0x54: /* SD: Scroll Down. */
308         ezio_scroll_down(e, ARG1(1));
309         break;
310     case 0x240: /* SL: Scroll Left. */
311         ezio_scroll_left(e, ARG1(1));
312         break;
313     case 0x241: /* SR: Scroll Right. */
314         ezio_scroll_right(e, ARG1(1));
315         break;
316     case 0x53: /* SU: Scroll Up. */
317         ezio_scroll_up(e, ARG1(1));
318         break;
319
320         /* Private sequences. */
321     case 0x70: /* DICO: Define Icon. */
322         define_icon(e, seq->args);
323         break;
324     case 0x71: /* CICO: Clear Icon. */
325         clear_icon(e, ARG1(0));
326         break;
327     case 0x72: /* Set cursor visibility. */
328         set_cursor(e, ARG1(1));
329         break;
330     }
331     e->x = range(e->x, 0, 40);
332     e->y = range(e->y, 0, 1);
333     VLOG_DBG("cursor:%d,%d", e->x, e->y);
334 }
335
336 static void
337 recv_control(uint8_t c, struct ezio *e)
338 {
339     switch (c) {
340     case '\b':
341         if (e->x > 0) {
342             --e->x;
343         }
344         break;
345
346     case '\t':
347         e->x = ROUND_UP(e->x + 1, 8);
348         if (e->x > 40) {
349             ezio_newline(e);
350         }
351         break;
352
353     case '\n':
354         ezio_line_feed(e);
355         break;
356
357     case '\f':
358         ezio_clear(e);
359         break;
360
361     case '\r':
362         e->x = 0;
363         break;
364
365     default:
366         VLOG_DBG("Unhandled control character 0x%02"PRIx8, c);
367     }
368 }
369
370 static void
371 recv_character(uint8_t byte, struct ezio *e)
372 {
373     if (e->x >= 40) {
374         ezio_newline(e);
375     }
376     ezio_put_char(e, e->x++, e->y, byte);
377 }
378
379 static int
380 default_arg(int value, int default_value)
381 {
382     return value >= 0 ? value : default_value;
383 }
384
385 static int
386 range(int value, int min, int max)
387 {
388     return value < min ? min : value > max ? max : value;
389 }
390
391 static void
392 clear_elements(uint8_t *p, size_t size, int pos, int clear_type)
393 {
394     switch (clear_type) {
395     case 0:
396         /* Clear from 'pos' to end. */
397         memset(p + pos, ' ', size - pos);
398         break;
399     case 1:
400         /* Clear from beginning to 'pos'. */
401         memset(p, ' ', pos + 1);
402         break;
403     case 2:
404         /* Clear all. */
405         memset(p, ' ', size);
406         break;
407     }
408 }
409
410 static void
411 define_icon(struct ezio *e, const int *args)
412 {
413     int icon_nr;
414     int row;
415
416     icon_nr = args[0];
417     if (icon_nr < 0 || icon_nr > 7) {
418         return;
419     }
420
421     for (row = 0; row < 8; row++) {
422         e->icons[icon_nr][row] = default_arg(args[row + 1], 0) & 0x1f;
423     }
424 }
425
426 static void
427 clear_icon(struct ezio *e, int icon_nr)
428 {
429     if (icon_nr >= 0 && icon_nr <= 7) {
430         ezio_set_default_icon(e, icon_nr);
431     }
432 }
433
434 static void
435 set_cursor(struct ezio *e, int visibility)
436 {
437     switch (visibility) {
438     case 1:
439         e->show_cursor = e->blink_cursor = false;
440         break;
441     case 2:
442         e->show_cursor = true;
443         e->blink_cursor = false;
444         break;
445     case 3:
446         e->show_cursor = e->blink_cursor = true;
447         break;
448     }
449 }
450
451 static int
452 unicode_to_ezio(uint16_t unicode)
453 {
454     switch (unicode) {
455         /* Most ASCII characters map one-to-one. */
456     case 0x0020 ... 0x005b:
457     case 0x005d ... 0x007d:
458         return unicode;
459
460         /* A few ASCII characters have to be simulated with icons. */
461     case 0x005c: return 0x06; /* BACKSLASH */
462     case 0x007e: return 0x07; /* TILDE */
463
464         /* EZIO extended characters equivalents in Unicode - Japanese. */
465     case 0x00a5: return '\\';   /* YEN SIGN */
466     case 0x3002: return 0xa1;   /* IDEOGRAPHIC FULL STOP */
467     case 0x300c: return 0xa2;   /* LEFT CORNER BRACKET */
468     case 0x300d: return 0xa3;   /* RIGHT CORNER BRACKET */
469     case 0x3001: return 0xa4;   /* IDEOGRAPHIC COMMA */
470     case 0x30fb: return 0xa5;   /* KATAKANA MIDDLE DOT */
471     case 0x30f2: return 0xa6;   /* KATAKANA LETTER WO */
472     case 0x30a1: return 0xa7;   /* KATAKANA LETTER SMALL A */
473     case 0x30a3: return 0xa8;   /* KATAKANA LETTER SMALL I */
474     case 0x30a5: return 0xa9;   /* KATAKANA LETTER SMALL U */
475     case 0x30a7: return 0xaa;   /* KATAKANA LETTER SMALL E */
476     case 0x30a9: return 0xab;   /* KATAKANA LETTER SMALL O */
477     case 0x30e3: return 0xac;   /* KATAKANA LETTER SMALL YA */
478     case 0x30e5: return 0xad;   /* KATAKANA LETTER SMALL YU */
479     case 0x30e7: return 0xae;   /* KATAKANA LETTER SMALL YO */
480     case 0x30c3: return 0xaf;   /* KATAKANA LETTER SMALL TU = SMALL TSU */
481         case 0x30fc: return 0xb0;   /* KATAKANA-HIRAGANA PROLONGED SOUND MARK */
482     case 0x30a2: return 0xb1;   /* KATAKANA LETTER A */
483     case 0x30a4: return 0xb2;   /* KATAKANA LETTER I */
484     case 0x30a6: return 0xb3;   /* KATAKANA LETTER U */
485     case 0x30a8: return 0xb4;   /* KATAKANA LETTER E */
486     case 0x30aa: return 0xb5;   /* KATAKANA LETTER O */
487     case 0x30ab: return 0xb6;   /* KATAKANA LETTER KA */
488     case 0x30ac: return 0xb6de; /* KATAKANA LETTER GA */
489     case 0x30ad: return 0xb7;   /* KATAKANA LETTER KI */
490     case 0x30ae: return 0xb7de; /* KATAKANA LETTER GI */
491     case 0x30af: return 0xb8;   /* KATAKANA LETTER KU */
492     case 0x30b0: return 0xb8de; /* KATAKANA LETTER GU */
493     case 0x30b1: return 0xb9;   /* KATAKANA LETTER KE */
494     case 0x30b2: return 0xb9de; /* KATAKANA LETTER GE */
495     case 0x30b3: return 0xba;   /* KATAKANA LETTER KO */
496     case 0x30b4: return 0xbade; /* KATAKANA LETTER GO */
497     case 0x30b5: return 0xbb;   /* KATAKANA LETTER SA */
498     case 0x30b6: return 0xbbde; /* KATAKANA LETTER ZA */
499     case 0x30b7: return 0xbc;   /* KATAKANA LETTER SI = SHI */
500     case 0x30b8: return 0xbcde; /* KATAKANA LETTER ZI = JI */
501     case 0x30b9: return 0xbd;   /* KATAKANA LETTER SU */
502     case 0x30ba: return 0xbdde; /* KATAKANA LETTER ZU */
503     case 0x30bb: return 0xbe;   /* KATAKANA LETTER SE */
504     case 0x30bc: return 0xbede; /* KATAKANA LETTER ZE */
505     case 0x30bd: return 0xbf;   /* KATAKANA LETTER SO */
506     case 0x30be: return 0xbfde; /* KATAKANA LETTER ZO */
507     case 0x30bf: return 0xc0;   /* KATAKANA LETTER TA */
508     case 0x30c0: return 0xc0de; /* KATAKANA LETTER DA */
509     case 0x30c1: return 0xc1;   /* KATAKANA LETTER TI = CHI */
510     case 0x30c2: return 0xc1de; /* KATAKANA LETTER DI = JI */
511     case 0x30c4: return 0xc2;   /* KATAKANA LETTER TU = TSU */
512     case 0x30c5: return 0xc2de; /* KATAKANA LETTER DU = ZU */
513     case 0x30c6: return 0xc3;   /* KATAKANA LETTER TE */
514     case 0x30c7: return 0xc3de; /* KATAKANA LETTER DE */
515     case 0x30c8: return 0xc4;   /* KATAKANA LETTER TO */
516     case 0x30c9: return 0xc4de; /* KATAKANA LETTER DO */
517     case 0x30ca: return 0xc5;   /* KATAKANA LETTER NA */
518     case 0x30cb: return 0xc6;   /* KATAKANA LETTER NI */
519     case 0x30cc: return 0xc7;   /* KATAKANA LETTER NU */
520     case 0x30cd: return 0xc8;   /* KATAKANA LETTER NE */
521     case 0x30ce: return 0xc9;   /* KATAKANA LETTER NO */
522     case 0x30cf: return 0xca;   /* KATAKANA LETTER HA */
523     case 0x30d0: return 0xcade; /* KATAKANA LETTER BA */
524     case 0x30d1: return 0xcadf; /* KATAKANA LETTER PA */
525     case 0x30d2: return 0xcb;   /* KATAKANA LETTER HI */
526     case 0x30d3: return 0xcbde; /* KATAKANA LETTER BI */
527     case 0x30d4: return 0xcbdf; /* KATAKANA LETTER PI */
528     case 0x30d5: return 0xcc;   /* KATAKANA LETTER HU = FU */
529     case 0x30d6: return 0xccde; /* KATAKANA LETTER BU */
530     case 0x30d7: return 0xccdf; /* KATAKANA LETTER PU */
531     case 0x30d8: return 0xcd;   /* KATAKANA LETTER HE */
532     case 0x30d9: return 0xcdde; /* KATAKANA LETTER BE */
533     case 0x30da: return 0xcddf; /* KATAKANA LETTER PE */
534     case 0x30db: return 0xce;   /* KATAKANA LETTER HO */
535     case 0x30dc: return 0xcede; /* KATAKANA LETTER BO */
536     case 0x30dd: return 0xcedf; /* KATAKANA LETTER PO */
537     case 0x30de: return 0xcf;   /* KATAKANA LETTER MA */
538     case 0x30df: return 0xd0;   /* KATAKANA LETTER MI */
539     case 0x30e0: return 0xd1;   /* KATAKANA LETTER MU */
540     case 0x30e1: return 0xd2;   /* KATAKANA LETTER ME */
541     case 0x30e2: return 0xd3;   /* KATAKANA LETTER MO */
542     case 0x30e4: return 0xd4;   /* KATAKANA LETTER YA */
543     case 0x30e6: return 0xd5;   /* KATAKANA LETTER YU */
544     case 0x30e8: return 0xd6;   /* KATAKANA LETTER YO */
545     case 0x30e9: return 0xd7;   /* KATAKANA LETTER RA */
546     case 0x30ea: return 0xd8;   /* KATAKANA LETTER RI */
547     case 0x30eb: return 0xd9;   /* KATAKANA LETTER RU */
548     case 0x30ec: return 0xda;   /* KATAKANA LETTER RE */
549     case 0x30ed: return 0xdb;   /* KATAKANA LETTER RO */
550     case 0x30ef: return 0xdc;   /* KATAKANA LETTER WA */
551     case 0x30f3: return 0xdd;   /* KATAKANA LETTER N */
552     case 0x30f4: return 0xb3de; /* KATAKANA LETTER VU */
553     case 0x30f7: return 0xdcde; /* KATAKANA LETTER VA */
554     case 0x3099: return 0xde;   /* COMBINING KATAKANA-HIRAGANA VOICED SOUND
555                                  * MARK */
556     case 0x309a: return 0xdf;   /* COMBINING KATAKANA-HIRAGANA SEMI-VOICED
557                                  * SOUND MARK */
558     case 0x309b: return 0xde;   /* KATAKANA-HIRAGANA VOICED SOUND MARK */
559         case 0x309c: return 0xdf;   /* KATAKANA-HIRAGANA SEMI-VOICED SOUND MARK */
560
561         /* EZIO extended characters equivalents in Unicode - other. */
562     case 0x2192: return 0x7e; /* RIGHTWARDS ARROW */
563     case 0x2190: return 0x7f; /* LEFTWARDS ARROW */
564     case 0x03b1: return 0xe0; /* GREEK SMALL LETTER ALPHA */
565     case 0x00e4: return 0xe1; /* LATIN SMALL LETTER A WITH DIAERESIS */
566     case 0x03b2: return 0xe2; /* GREEK SMALL LETTER BETA */
567     case 0x03b5: return 0xe3; /* GREEK SMALL LETTER EPSILON */
568     case 0x03bc: return 0xe4; /* GREEK SMALL LETTER MU */
569     case 0x03c6: return 0xe5; /* GREEK SMALL LETTER PHI */
570     case 0x03c1: return 0xe6; /* GREEK SMALL LETTER RHO */
571                               /* 0xe7 is 'g'. */
572     case 0x221a: return 0xe8; /* SQUARE ROOT = radical sign */
573                               /* 0xe9 is an unrecognizable symbol. */
574                               /* 0xea is 'j'. */
575                               /* 0xeb is an unrecognizable symbol.*/
576     case 0x00a2: return 0xec; /* CENT SIGN */
577     case 0x00a3: return 0xed; /* POUND SIGN */
578     case 0x00f1: return 0xee; /* LATIN SMALL LETTER N WITH TILDE */
579     case 0x00f6: return 0xef; /* LATIN SMALL LETTER O WITH DIAERESIS */
580                               /* 0xf0 is 'p'. */
581                               /* 0xf1 is 'q'. */
582     case 0x03b8: return 0xf2; /* GREEK SMALL LETTER THETA */
583     case 0x221e: return 0xf3; /* INFINITY */
584     case 0x03a9: return 0xf4; /* GREEK CAPITAL LETTER OMEGA */
585     case 0x00fc: return 0xf5; /* LATIN SMALL LETTER U WITH DIAERESIS */
586     case 0x03a3: return 0xf6; /* GREEK CAPITAL LETTER SIGMA */
587     case 0x03c0: return 0xf7; /* GREEK SMALL LETTER PI */
588                               /* 0xf8 is x-macron (the sample mean). */
589                               /* 0xf9 is 'y'. */
590     case 0x5343: return 0xfa; /* thousand */
591     case 0x4e07: return 0xfb; /* ten thousand */
592     case 0x5186: return 0xfc; /* yen */
593     case 0x00f7: return 0xfd; /* DIVISION SIGN */
594     case 0x2588: return 0xff; /* FULL BLOCK = solid */
595
596         /* EZIO icons (from the Unicode Private Use corporate subarea). */
597     case 0xf8f8: return 0x00;
598     case 0xf8f9: return 0x01;
599     case 0xf8fa: return 0x02;
600     case 0xf8fb: return 0x03;
601     case 0xf8fc: return 0x04;
602     case 0xf8fd: return 0x05;
603     case 0xf8fe: return 0x06;
604     case 0xf8ff: return 0x07;
605
606         /* No mappings for anything else. */
607     default: return -1;
608     }
609 }
610 \f
611 /* UTF-8 decoder. */
612
613 #define UTF_STATES                              \
614     UTF_STATE(UTF8_INIT, 0x00, 0xf4, UTF8_INIT) \
615     UTF_STATE(UTF8_3,    0x80, 0xbf, UTF8_2)    \
616     UTF_STATE(UTF8_2,    0x80, 0xbf, UTF8_1)    \
617     UTF_STATE(UTF8_1,    0x80, 0xbf, UTF8_INIT) \
618     UTF_STATE(UTF8_E0,   0xa0, 0xbf, UTF8_1)    \
619     UTF_STATE(UTF8_ED,   0x80, 0x9f, UTF8_1)    \
620     UTF_STATE(UTF8_F0,   0x90, 0xbf, UTF8_INIT) \
621     UTF_STATE(UTF8_F4,   0x80, 0x8f, UTF8_INIT)
622
623 enum state {
624 #define UTF_STATE(NAME, MIN, MAX, NEXT) NAME,
625     UTF_STATES
626 #undef UTF_STATE
627 };
628
629 struct state_info {
630     uint8_t min, max;
631     enum state next;
632 };
633
634 static const struct state_info states[] = {
635 #define UTF_STATE(NAME, MIN, MAX, NEXT) {MIN, MAX, NEXT},
636     UTF_STATES
637 #undef UTF_STATE
638 };
639
640 struct utf8_reader {
641     int cp;
642     enum state state;
643 };
644
645 struct utf8_reader *
646 utf8_reader_create(void)
647 {
648     struct utf8_reader *r = xmalloc(sizeof *r);
649     r->state = UTF8_INIT;
650     return r;
651 }
652
653 void
654 utf8_reader_destroy(struct utf8_reader *r)
655 {
656     free(r);
657 }
658
659 int
660 utf8_reader_read(struct utf8_reader *r, uint8_t c)
661 {
662     const struct state_info *s = &states[r->state];
663     if (c >= s->min && c <= s->max) {
664         if (r->state == UTF8_INIT) {
665             if (c < 0x80) {
666                 return c;
667             } else if (c >= 0xc2 && c <= 0xdf) {
668                 r->cp = c & 0x1f;
669                 r->state = UTF8_1;
670                 return -1;
671             } else if (c >= 0xe0 && c <= 0xef) {
672                 r->cp = c & 0x0f;
673                 r->state = c == 0xe0 ? UTF8_E0 : c == 0xed ? UTF8_ED : UTF8_2;
674                 return -1;
675             } else if (c >= 0xf0 && c <= 0xf4) {
676                 r->cp = c & 0x07;
677                 r->state = c == 0xf0 ? UTF8_F0 : c == 0xf4 ? UTF8_F4 : UTF8_3;
678                 return -1;
679             }
680         } else {
681             r->cp = (r->cp << 6) | (c & 0x3f);
682             r->state = s->next;
683             return r->state == UTF8_INIT ? r->cp : -1;
684         }
685     }
686
687     /* Invalid UTF-8 sequence.  Return the Unicode general substitute
688      * REPLACEMENT CHARACTER. */
689     r->state = UTF8_INIT;
690     return 0xfffd;
691 }
692 \f
693 /* ANSI control sequence decoder. */
694
695 /* States are named for what we are looking for in that state. */
696 enum ansi_state {
697     ANSI_ESC,                      /* Looking for ESC. */
698     ANSI_CSI,                      /* Looking for [ (to complete CSI). */
699     ANSI_PARAMETER,                /* Looking for parameter. */
700     ANSI_INTERMEDIATE,             /* Looking for intermediate byte. */
701     ANSI_FINAL,                    /* Looking for final byte. */
702     ANSI_COMPLETE                  /* Got an entire escape sequence. */
703 };
704
705 struct ansi_decoder {
706     enum ansi_state state;
707     struct ansi_sequence seq;
708     int c;
709 };
710
711 struct ansi_decoder *
712 ansi_decoder_create(void)
713 {
714     struct ansi_decoder *d = xmalloc(sizeof *d);
715     d->state = ANSI_ESC;
716     return d;
717 }
718
719 void
720 ansi_decoder_destroy(struct ansi_decoder *d)
721 {
722     free(d);
723 }
724
725 int
726 ansi_decoder_put(struct ansi_decoder *d, uint8_t c)
727 {
728     if (c == 27) {
729         /* Escape always starts a new escape sequence, aborting an incomplete
730          * one if necessary. */
731         if (d->state != ANSI_ESC) {
732             VLOG_DBG("Unexpected escape inside escape sequence");
733         }
734         d->state = ANSI_CSI;
735         return 0;
736     }
737
738     switch (d->state) {
739     case ANSI_ESC:
740         return 1;
741
742     case ANSI_CSI:
743         if (c == '[') {
744             d->state = ANSI_PARAMETER;
745             d->seq.n_args = 0;
746             d->seq.function = 0;
747         } else if (c >= 0x40 && c <= 0x5f) {
748             d->state = ANSI_COMPLETE;
749             d->seq.n_args = 0;
750             d->seq.function = 0;
751             d->seq.function = c << 8;
752             return -1;
753         } else {
754             d->state = ANSI_ESC;
755         }
756         break;
757
758     case ANSI_PARAMETER:
759         if (c >= '0' && c <= '9') {
760             int *arg;
761             if (d->seq.n_args == 0) {
762                 d->seq.args[d->seq.n_args++] = 0;
763             } else if (d->seq.n_args > ANSI_MAX_ARGS) {
764                 break;
765             }
766             arg = &d->seq.args[d->seq.n_args - 1];
767             if (*arg == -1) {
768                 *arg = 0;
769             }
770             *arg = *arg * 10 + (c - '0');
771             break;
772         } else if (c == ';') {
773             if (d->seq.n_args < ANSI_MAX_ARGS) {
774                 d->seq.args[d->seq.n_args] = -1;
775             }
776             d->seq.n_args++;
777             break;
778         }
779         d->state = ANSI_INTERMEDIATE;
780         /* Fall through. */
781
782     case ANSI_INTERMEDIATE:
783         if (c >= 0x20 && c <= 0x2f) {
784             d->seq.function = d->seq.function * 16 + (c - 0x20);
785             break;
786         }
787         d->state = ANSI_FINAL;
788         /* Fall through. */
789
790     case ANSI_FINAL:
791         if (c >= 0x40 && c <= 0x7e) {
792             d->seq.function = d->seq.function * 256 + c;
793             d->state = ANSI_COMPLETE;
794             return -1;
795         } else {
796             /* Invalid sequence. */
797             d->state = ANSI_ESC;
798         }
799         break;
800
801     case ANSI_COMPLETE:
802         NOT_REACHED();
803     }
804     return 0;
805 }
806
807 const struct ansi_sequence *
808 ansi_decoder_get(struct ansi_decoder *d)
809 {
810     assert(d->state == ANSI_COMPLETE);
811     d->state = ANSI_ESC;
812     if (d->seq.n_args < ANSI_MAX_ARGS) {
813         int i;
814         for (i = d->seq.n_args; i < ANSI_MAX_ARGS; i++) {
815             d->seq.args[i] = -1;
816         }
817     } else {
818         d->seq.n_args = ANSI_MAX_ARGS;
819     }
820     return &d->seq;
821 }