1 /* Copyright (c) 2008, 2009 Nicira Networks, Inc.
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:
7 * http://www.apache.org/licenses/LICENSE-2.0
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.
25 #include "dynamic-string.h"
27 #include "poll-loop.h"
30 #define THIS_MODULE VLM_terminal
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);
38 /* ANSI escape sequence decoding. */
39 struct ansi_sequence {
41 #define ANSI_MAX_ARGS 16
42 int args[ANSI_MAX_ARGS];
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 *);
51 /* Terminal emulation. */
53 struct ansi_decoder *ansi;
54 struct utf8_reader *utf8;
55 enum { EZIO, UTF8 } encoding;
58 static void recv_byte(struct terminal *term, struct ezio *ezio, uint8_t c);
63 struct terminal *term = xmalloc(sizeof *term);
64 term->ansi = ansi_decoder_create();
65 term->utf8 = utf8_reader_create();
66 term->encoding = UTF8;
71 terminal_destroy(struct terminal *term)
74 utf8_reader_destroy(term->utf8);
75 ansi_decoder_destroy(term->ansi);
81 terminal_run(struct terminal *term, struct ezio *ezio, int input_fd)
86 n = read(input_fd, input, sizeof input);
90 for (i = 0; i < n; i++) {
91 recv_byte(term, ezio, input[i]);
95 return !n ? EOF : errno == EAGAIN ? 0 : errno;
100 terminal_wait(struct terminal *term UNUSED, int input_fd)
102 poll_fd_wait(input_fd, POLLIN);
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);
117 recv_byte(struct terminal *term, struct ezio *ezio, uint8_t c)
121 /* Decode and interpret ANSI escape sequences. */
122 retval = ansi_decoder_put(term->ansi, c);
125 recv_ansi_sequence(ansi_decoder_get(term->ansi), ezio);
131 /* Encoding selection. */
134 term->encoding = EZIO;
136 } else if (c == 0x0f) {
138 term->encoding = UTF8;
142 if (term->encoding == UTF8) {
145 /* Convert UTF-8 input to Unicode code point. */
146 unicode = utf8_reader_read(term->utf8, c);
151 /* Convert Unicode code point to EZIO encoding. */
152 ezchar = unicode_to_ezio(unicode);
154 if (ezchar & 0xff00) {
155 recv_character(ezchar >> 8, ezio);
157 recv_character(ezchar, ezio);
158 } else if (unicode < 0x100) {
159 recv_control(unicode, ezio);
161 /* Unsupported Unicode code point. */
165 if (c >= 0x80 && c < 0x87) {
169 recv_character(c, ezio);
175 log_ansi_sequence(const struct ansi_sequence *seq, struct ezio *e)
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"},
210 const struct sequence *s;
215 for (s = sequences; s < &sequences[ARRAY_SIZE(sequences)]; s++) {
216 if (s->function == seq->function) {
217 ds_put_cstr(&ds, s->name);
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);
227 for (i = 0; i < seq->n_args; i++) {
228 ds_put_format(&ds, ", %d", seq->args[i]);
230 VLOG_DBG("%s (cursor:%d,%d)", ds_cstr(&ds), e->x, e->y);
235 recv_ansi_sequence(const struct ansi_sequence *seq, struct ezio *e)
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);
242 switch (seq->function) {
243 case 0x5a: /* CBT: Cursor Backward Tabulation. */
244 e->x = 8 * (e->x / 8 - ARG1(1));
246 case 0x47: /* CHA: Cursor Character Absolute. */
249 case 0x49: /* CHT: Cursor Forward Tabulation. */
250 e->x = 8 * (e->x / 8 + ARG1(1));
252 case 0x45: /* CNL: Cursor Next Line. */
256 case 0x46: /* CPL: Cursor Preceding Line. */
260 case 0x44: /* CUB: Cursor Left. */
263 case 0x42: /* CUD: Cursor Down. */
266 case 0x43: /* CUF: Cursor Right. */
269 case 0x48: /* CUP: Cursor Position. */
273 case 0x41: /* CUU: Cursor Up. */
276 case 0x50: /* DCH: Delete Character. */
277 ezio_delete_char(e, e->x, e->y, ARG1(1));
279 case 0x4d: /* DL: Delete Line. */
280 ezio_delete_line(e, e->y, ARG1(1));
282 case 0x58: /* ECH: Erase Character. */
283 memset(&e->chars[e->y][e->x], ' ', MIN(ARG1(1), 40 - e->x));
285 case 0x4a: /* ED: Erase in Page. */
286 clear_elements(&e->chars[0][0], 2 * 40, e->x + 40 * e->y, ARG1(0));
288 case 0x4b: /* EL: Erase in Line. */
289 clear_elements(&e->chars[e->y][0], 40, e->x, ARG1(0));
291 case 0x40: /* ICH: Insert Character. */
292 ezio_insert_char(e, e->x, e->y, ARG1(1));
294 case 0x4c: /* IL: Insert Line. */
295 ezio_insert_line(e, e->y, ARG1(1));
297 case 0x4500: /* NEL: Next Line. */
301 case 0x4d00: /* RI: Reverse Line Feed. */
304 case 0x6300: /* RIS: Reset to Initial State. */
307 case 0x54: /* SD: Scroll Down. */
308 ezio_scroll_down(e, ARG1(1));
310 case 0x240: /* SL: Scroll Left. */
311 ezio_scroll_left(e, ARG1(1));
313 case 0x241: /* SR: Scroll Right. */
314 ezio_scroll_right(e, ARG1(1));
316 case 0x53: /* SU: Scroll Up. */
317 ezio_scroll_up(e, ARG1(1));
320 /* Private sequences. */
321 case 0x70: /* DICO: Define Icon. */
322 define_icon(e, seq->args);
324 case 0x71: /* CICO: Clear Icon. */
325 clear_icon(e, ARG1(0));
327 case 0x72: /* Set cursor visibility. */
328 set_cursor(e, ARG1(1));
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);
337 recv_control(uint8_t c, struct ezio *e)
347 e->x = ROUND_UP(e->x + 1, 8);
366 VLOG_DBG("Unhandled control character 0x%02"PRIx8, c);
371 recv_character(uint8_t byte, struct ezio *e)
376 ezio_put_char(e, e->x++, e->y, byte);
380 default_arg(int value, int default_value)
382 return value >= 0 ? value : default_value;
386 range(int value, int min, int max)
388 return value < min ? min : value > max ? max : value;
392 clear_elements(uint8_t *p, size_t size, int pos, int clear_type)
394 switch (clear_type) {
396 /* Clear from 'pos' to end. */
397 memset(p + pos, ' ', size - pos);
400 /* Clear from beginning to 'pos'. */
401 memset(p, ' ', pos + 1);
405 memset(p, ' ', size);
411 define_icon(struct ezio *e, const int *args)
417 if (icon_nr < 0 || icon_nr > 7) {
421 for (row = 0; row < 8; row++) {
422 e->icons[icon_nr][row] = default_arg(args[row + 1], 0) & 0x1f;
427 clear_icon(struct ezio *e, int icon_nr)
429 if (icon_nr >= 0 && icon_nr <= 7) {
430 ezio_set_default_icon(e, icon_nr);
435 set_cursor(struct ezio *e, int visibility)
437 switch (visibility) {
439 e->show_cursor = e->blink_cursor = false;
442 e->show_cursor = true;
443 e->blink_cursor = false;
446 e->show_cursor = e->blink_cursor = true;
452 unicode_to_ezio(uint16_t unicode)
455 /* Most ASCII characters map one-to-one. */
456 case 0x0020 ... 0x005b:
457 case 0x005d ... 0x007d:
460 /* A few ASCII characters have to be simulated with icons. */
461 case 0x005c: return 0x06; /* BACKSLASH */
462 case 0x007e: return 0x07; /* TILDE */
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
556 case 0x309a: return 0xdf; /* COMBINING KATAKANA-HIRAGANA SEMI-VOICED
558 case 0x309b: return 0xde; /* KATAKANA-HIRAGANA VOICED SOUND MARK */
559 case 0x309c: return 0xdf; /* KATAKANA-HIRAGANA SEMI-VOICED SOUND MARK */
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 */
572 case 0x221a: return 0xe8; /* SQUARE ROOT = radical sign */
573 /* 0xe9 is an unrecognizable symbol. */
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 */
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). */
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 */
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;
606 /* No mappings for anything else. */
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)
624 #define UTF_STATE(NAME, MIN, MAX, NEXT) NAME,
634 static const struct state_info states[] = {
635 #define UTF_STATE(NAME, MIN, MAX, NEXT) {MIN, MAX, NEXT},
646 utf8_reader_create(void)
648 struct utf8_reader *r = xmalloc(sizeof *r);
649 r->state = UTF8_INIT;
654 utf8_reader_destroy(struct utf8_reader *r)
660 utf8_reader_read(struct utf8_reader *r, uint8_t c)
662 const struct state_info *s = &states[r->state];
663 if (c >= s->min && c <= s->max) {
664 if (r->state == UTF8_INIT) {
667 } else if (c >= 0xc2 && c <= 0xdf) {
671 } else if (c >= 0xe0 && c <= 0xef) {
673 r->state = c == 0xe0 ? UTF8_E0 : c == 0xed ? UTF8_ED : UTF8_2;
675 } else if (c >= 0xf0 && c <= 0xf4) {
677 r->state = c == 0xf0 ? UTF8_F0 : c == 0xf4 ? UTF8_F4 : UTF8_3;
681 r->cp = (r->cp << 6) | (c & 0x3f);
683 return r->state == UTF8_INIT ? r->cp : -1;
687 /* Invalid UTF-8 sequence. Return the Unicode general substitute
688 * REPLACEMENT CHARACTER. */
689 r->state = UTF8_INIT;
693 /* ANSI control sequence decoder. */
695 /* States are named for what we are looking for in that 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. */
705 struct ansi_decoder {
706 enum ansi_state state;
707 struct ansi_sequence seq;
711 struct ansi_decoder *
712 ansi_decoder_create(void)
714 struct ansi_decoder *d = xmalloc(sizeof *d);
720 ansi_decoder_destroy(struct ansi_decoder *d)
726 ansi_decoder_put(struct ansi_decoder *d, uint8_t c)
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");
744 d->state = ANSI_PARAMETER;
747 } else if (c >= 0x40 && c <= 0x5f) {
748 d->state = ANSI_COMPLETE;
751 d->seq.function = c << 8;
759 if (c >= '0' && c <= '9') {
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) {
766 arg = &d->seq.args[d->seq.n_args - 1];
770 *arg = *arg * 10 + (c - '0');
772 } else if (c == ';') {
773 if (d->seq.n_args < ANSI_MAX_ARGS) {
774 d->seq.args[d->seq.n_args] = -1;
779 d->state = ANSI_INTERMEDIATE;
782 case ANSI_INTERMEDIATE:
783 if (c >= 0x20 && c <= 0x2f) {
784 d->seq.function = d->seq.function * 16 + (c - 0x20);
787 d->state = ANSI_FINAL;
791 if (c >= 0x40 && c <= 0x7e) {
792 d->seq.function = d->seq.function * 256 + c;
793 d->state = ANSI_COMPLETE;
796 /* Invalid sequence. */
807 const struct ansi_sequence *
808 ansi_decoder_get(struct ansi_decoder *d)
810 assert(d->state == ANSI_COMPLETE);
812 if (d->seq.n_args < ANSI_MAX_ARGS) {
814 for (i = d->seq.n_args; i < ANSI_MAX_ARGS; i++) {
818 d->seq.n_args = ANSI_MAX_ARGS;