parser.cc 61.9 KB
Newer Older
1 2
/*
 * Copyright © 2015 David Herrmann <dh.herrmann@gmail.com>
3
 * Copyright © 2017, 2018 Christian Persch
4
 *
5 6 7 8 9 10 11 12 13 14 15 16
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 3 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
17 18 19 20
 */

#include "config.h"

21 22
#include "parser.hh"

23 24 25 26 27
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cerrno>

28
#include <glib.h>
29

30 31
#include "parser-charset-tables.hh"

32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
#define WARN(num,str) do { } while (0)
#define hweight32(v) (__builtin_popcount(v))
#define kzalloc(n,v) calloc((n),1)
#define kfree(ptr) free(ptr)

/*
 * Terminal Parser
 * This file contains a bunch of UTF-8 helpers and the main ctlseq-parser. The
 * parser is a simple state-machine that correctly parses all CSI, DCS, OSC, ST
 * control sequences and generic escape sequences.
 * The parser itself does not perform any actions but lets the caller react to
 * detected sequences.
 */

#define VTE_PARSER_ST_MAX (4096)

struct vte_parser {
        struct vte_seq seq;
        size_t st_alloc;
        unsigned int state;
};

/*
 * Command Parser
 * The ctl-seq parser "vte_parser" only detects whole sequences, it does not
 * detect the specific command. Once a sequence is parsed, the command-parsers
 * are used to figure out their meaning.
 */

static unsigned int vte_parse_host_control(const struct vte_seq *seq)
{
        switch (seq->terminator) {
        case 0x00: /* NUL */
65
                return VTE_CMD_NUL;
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143
        case 0x05: /* ENQ */
                return VTE_CMD_ENQ;
        case 0x07: /* BEL */
                return VTE_CMD_BEL;
        case 0x08: /* BS */
                return VTE_CMD_BS;
        case 0x09: /* HT */
                return VTE_CMD_HT;
        case 0x0a: /* LF */
                return VTE_CMD_LF;
        case 0x0b: /* VT */
                return VTE_CMD_VT;
        case 0x0c: /* FF */
                return VTE_CMD_FF;
        case 0x0d: /* CR */
                return VTE_CMD_CR;
        case 0x0e: /* SO */
                return VTE_CMD_SO;
        case 0x0f: /* SI */
                return VTE_CMD_SI;
        case 0x11: /* DC1 */
                return VTE_CMD_DC1;
        case 0x13: /* DC3 */
                return VTE_CMD_DC3;
        case 0x18: /* CAN */
                /* this is already handled by the state-machine */
                break;
        case 0x1a: /* SUB */
                return VTE_CMD_SUB;
        case 0x1b: /* ESC */
                /* this is already handled by the state-machine */
                break;
        case 0x1f: /* DEL */
                /* this is already handled by the state-machine */
                break;
        case 0x84: /* IND */
                return VTE_CMD_IND;
        case 0x85: /* NEL */
                return VTE_CMD_NEL;
        case 0x88: /* HTS */
                return VTE_CMD_HTS;
        case 0x8d: /* RI */
                return VTE_CMD_RI;
        case 0x8e: /* SS2 */
                return VTE_CMD_SS2;
        case 0x8f: /* SS3 */
                return VTE_CMD_SS3;
        case 0x90: /* DCS */
                /* this is already handled by the state-machine */
                break;
        case 0x96: /* SPA */
                return VTE_CMD_SPA;
        case 0x97: /* EPA */
                return VTE_CMD_EPA;
        case 0x98: /* SOS */
                /* this is already handled by the state-machine */
                break;
        case 0x9a: /* DECID */
                return VTE_CMD_DECID;
        case 0x9b: /* CSI */
                /* this is already handled by the state-machine */
                break;
        case 0x9c: /* ST */
                return VTE_CMD_ST;
        case 0x9d: /* OSC */
                /* this is already handled by the state-machine */
                break;
        case 0x9e: /* PM */
                /* this is already handled by the state-machine */
                break;
        case 0x9f: /* APC */
                /* this is already handled by the state-machine */
                break;
        }

        return VTE_CMD_NONE;
}

144 145
static unsigned int vte_parse_charset_94(uint32_t raw,
                                         unsigned int flags)
146
{
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184
        assert (raw >= 0x30 && raw < 0x7f);

        if (flags & VTE_SEQ_FLAG_SPACE)
                return VTE_CHARSET_DRCS;

        switch (flags) {
        case 0:
                if (raw < (0x30 + G_N_ELEMENTS(charset_graphic_94)))
                        return charset_graphic_94[raw - 0x30];
                break;

        case VTE_SEQ_FLAG_BANG:
                if (raw >= 0x40 && (raw < 0x40 + G_N_ELEMENTS(charset_graphic_94_with_2_1)))
                        return charset_graphic_94_with_2_1[raw - 0x40];
                break;

        case VTE_SEQ_FLAG_DQUOTE:
                if (raw < (0x30 + G_N_ELEMENTS(charset_graphic_94_with_2_2)))
                        return charset_graphic_94_with_2_2[raw - 0x30];
                break;

        case VTE_SEQ_FLAG_HASH:
                break;

        case VTE_SEQ_FLAG_CASH:
                if (raw < (0x30 + G_N_ELEMENTS(charset_graphic_94_n)))
                        return charset_graphic_94_n[raw - 0x30];
                break;

        case VTE_SEQ_FLAG_PERCENT:
                if (raw < (0x30 + G_N_ELEMENTS(charset_graphic_94_with_2_5)))
                        return charset_graphic_94_with_2_5[raw - 0x30];
                break;

        case VTE_SEQ_FLAG_AND:
                if (raw < (0x30 + G_N_ELEMENTS(charset_graphic_94_with_2_6)))
                        return charset_graphic_94_with_2_6[raw - 0x30];
                break;
185
        }
186 187

        return VTE_CHARSET_NONE;
188 189
}

190 191
static unsigned int vte_parse_charset_96(uint32_t raw,
                                         unsigned int flags)
192
{
193
        assert (raw >= 0x30 && raw < 0x7f);
194

195 196 197 198 199 200
        if (flags == 0) { /* Graphic 96-set */
                if (raw < (0x30 + G_N_ELEMENTS(charset_graphic_96)))
                        return charset_graphic_96[raw - 0x30];
        } else if (flags & VTE_SEQ_FLAG_SPACE) { /* DRCS */
                return VTE_CHARSET_DRCS;
        }
201

202 203
        return VTE_CHARSET_NONE;
}
204

205 206 207 208
static unsigned int vte_parse_charset_ocs(uint32_t raw,
                                          unsigned int flags)
{
        assert (raw >= 0x30 && raw < 0x7f);
209

210 211 212 213 214 215 216 217
        if (flags == VTE_SEQ_FLAG_PERCENT) {
                /* OCS with standard return */
                if (raw >= 0x40 && raw < (0x40 + G_N_ELEMENTS(charset_ocs_with_return)))
                        return charset_ocs_with_return[raw - 0x40];
        } else if (flags == (VTE_SEQ_FLAG_PERCENT | VTE_SEQ_FLAG_SLASH)) {
                /* OCS without standard return */
                if (raw >= 0x40 && raw < (0x40 + G_N_ELEMENTS(charset_ocs_without_return)))
                        return charset_ocs_without_return[raw - 0x40];
218 219
        }

220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246
        return VTE_CHARSET_NONE;
}

static unsigned int vte_parse_charset_control(uint32_t raw,
                                              unsigned int flags)
{
        assert (raw >= 0x30 && raw < 0x7f);

        if (flags == VTE_SEQ_FLAG_BANG) { /* C0 controls */
                if (raw >= 0x40 && raw < (0x40 + G_N_ELEMENTS(charset_control_c0)))
                        return charset_control_c0[raw - 0x40];
        } else if (flags == VTE_SEQ_FLAG_DQUOTE) { /* C1 controls */
                if (raw >= 0x40 && raw < (0x40 + G_N_ELEMENTS(charset_control_c1)))
                        return charset_control_c1[raw - 0x40];
        }

        return VTE_CHARSET_NONE;
}

static unsigned int vte_parse_host_escape(const struct vte_seq *seq,
                                          unsigned int *cs_out)
{
        unsigned int const flags = seq->intermediates;

        if (flags == 0) {
                switch (seq->terminator) {
                case '6': /* DECBI */
247
                        return VTE_CMD_DECBI;
248
                case '7': /* DECSC */
249
                        return VTE_CMD_DECSC;
250
                case '8': /* DECRC */
251
                        return VTE_CMD_DECRC;
252
                case '9': /* DECFI */
253
                        return VTE_CMD_DECFI;
254
                case '<': /* DECANM */
255
                        return VTE_CMD_DECANM;
256
                case '=': /* DECKPAM */
257
                        return VTE_CMD_DECKPAM;
258
                case '>': /* DECKPNM */
259
                        return VTE_CMD_DECKPNM;
260
                case 'D': /* IND */
261
                        return VTE_CMD_IND;
262
                case 'E': /* NEL */
263
                        return VTE_CMD_NEL;
264
                case 'F': /* Cursor to lower-left corner of screen */
265
                        return VTE_CMD_XTERM_CLLHP;
266
                case 'H': /* HTS */
267
                        return VTE_CMD_HTS;
268
                case 'M': /* RI */
269
                        return VTE_CMD_RI;
270
                case 'N': /* SS2 */
271
                        return VTE_CMD_SS2;
272
                case 'O': /* SS3 */
273
                        return VTE_CMD_SS3;
274
                case 'P': /* DCS */
275 276
                        /* this is already handled by the state-machine */
                        break;
277
                case 'V': /* SPA */
278
                        return VTE_CMD_SPA;
279
                case 'W': /* EPA */
280
                        return VTE_CMD_EPA;
281
                case 'X': /* SOS */
282 283
                        /* this is already handled by the state-machine */
                        break;
284
                case 'Z': /* DECID */
285
                        return VTE_CMD_DECID;
286
                case '[': /* CSI */
287 288
                        /* this is already handled by the state-machine */
                        break;
289
                case '\\': /* ST */
290
                        return VTE_CMD_ST;
291
                case ']': /* OSC */
292 293
                        /* this is already handled by the state-machine */
                        break;
294
                case '^': /* PM */
295 296
                        /* this is already handled by the state-machine */
                        break;
297
                case '_': /* APC */
298 299
                        /* this is already handled by the state-machine */
                        break;
300
                case 'c': /* RIS */
301
                        return VTE_CMD_RIS;
302 303 304
                case 'd': /* CMD */
                        return VTE_CMD_CMD;
                case 'l': /* Memory lock */
305
                        return VTE_CMD_XTERM_MLHP;
306
                case 'm': /* Memory unlock */
307
                        return VTE_CMD_XTERM_MUHP;
308
                case 'n': /* LS2 */
309
                        return VTE_CMD_LS2;
310
                case 'o': /* LS3 */
311
                        return VTE_CMD_LS3;
312
                case '|': /* LS3R */
313
                        return VTE_CMD_LS3R;
314
                case '}': /* LS2R */
315
                        return VTE_CMD_LS2R;
316
                case '~': /* LS1R */
317
                        return VTE_CMD_LS1R;
318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398
                }

                return VTE_CMD_NONE;
        }

        unsigned int const g_designators =
                VTE_SEQ_FLAG_POPEN | VTE_SEQ_FLAG_PCLOSE |
                VTE_SEQ_FLAG_MULT  | VTE_SEQ_FLAG_PLUS   |
                VTE_SEQ_FLAG_MINUS | VTE_SEQ_FLAG_DOT    |
                VTE_SEQ_FLAG_SLASH;

        if (hweight32(flags & g_designators) == 1) {
                unsigned int const remaining_flags = flags & ~g_designators;
                int cmd = (remaining_flags & VTE_SEQ_FLAG_CASH) ? VTE_CMD_GnDMm : VTE_CMD_GnDm;
                int cs = VTE_CHARSET_NONE;

                switch (flags & g_designators) {
                case VTE_SEQ_FLAG_POPEN:
                case VTE_SEQ_FLAG_PCLOSE:
                case VTE_SEQ_FLAG_MULT:
                case VTE_SEQ_FLAG_PLUS:
                        cs = vte_parse_charset_94(seq->terminator, remaining_flags);
                        break;
                case VTE_SEQ_FLAG_SLASH:
                        if (remaining_flags == VTE_SEQ_FLAG_PERCENT) { /* DOCS */
                                cmd = VTE_CMD_DOCS;
                                cs = vte_parse_charset_ocs(seq->terminator, /* all */ flags);
                                break;
                        }
                        /* fallthrough */
                case VTE_SEQ_FLAG_MINUS:
                case VTE_SEQ_FLAG_DOT:
                        cs = vte_parse_charset_96(seq->terminator, remaining_flags);
                        break;
                }

                if (cs_out)
                        *cs_out = cs;

                return cmd;
        }

        switch (flags) {
        case VTE_SEQ_FLAG_SPACE: /* ACS */
                return VTE_CMD_ACS;

        case VTE_SEQ_FLAG_BANG: /* C0-designate */
        case VTE_SEQ_FLAG_DQUOTE: /* C1-designate */
                if (cs_out)
                        *cs_out = vte_parse_charset_control(seq->terminator, flags);
                return VTE_CMD_CnD;

        case VTE_SEQ_FLAG_HASH:
                switch (seq->terminator) {
                case '3': /* DECDHL top-half */
                        return VTE_CMD_DECDHL_TH;
                case '4': /* DECDHL bottom-half */
                        return VTE_CMD_DECDHL_BH;
                case '5': /* DECSWL */
                        return VTE_CMD_DECSWL;
                case '6': /* DECDWL */
                        return VTE_CMD_DECDWL;
                case '8': /* DECALN */
                        return VTE_CMD_DECALN;
                }
                break;

        case VTE_SEQ_FLAG_CASH:
                /* For compatibility with an earlier version of ISO-2022,
                 * ESC 2/4 4/0, ESC 2/4 4/1 and ESC 2/4 4/2 designate G0
                 * sets (i.e., without the 2/8 as 2nd intermediate byte).
                 */
                switch (seq->terminator) {
                case '@':
                case 'A':
                case 'B': /* G0-designate multibyte charset */
                        if (cs_out)
                                *cs_out = vte_parse_charset_94(seq->terminator,
                                                               flags /* | VTE_SEQ_FLAG_POPEN */);
                        return VTE_CMD_GnDMm;
                }
399
                break;
400 401 402 403 404 405 406 407 408 409

        case VTE_SEQ_FLAG_PERCENT: /* DOCS */
        case VTE_SEQ_FLAG_PERCENT | VTE_SEQ_FLAG_SLASH: /* DOCS, but already handled above */
                if (cs_out)
                        *cs_out = vte_parse_charset_ocs(seq->terminator, flags);

                return VTE_CMD_DOCS;

        case VTE_SEQ_FLAG_AND: /* IRR */
                return VTE_CMD_IRR;
410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715
        }

        return VTE_CMD_NONE;
}

static unsigned int vte_parse_host_csi(const struct vte_seq *seq)
{
        unsigned int flags;

        flags = seq->intermediates;

        switch (seq->terminator) {
        case 'A':
                if (flags == 0) /* CUU */
                        return VTE_CMD_CUU;
                break;
        case 'a':
                if (flags == 0) /* HPR */
                        return VTE_CMD_HPR;
                break;
        case 'B':
                if (flags == 0) /* CUD */
                        return VTE_CMD_CUD;
                break;
        case 'b':
                if (flags == 0) /* REP */
                        return VTE_CMD_REP;
                break;
        case 'C':
                if (flags == 0) /* CUF */
                        return VTE_CMD_CUF;
                break;
        case 'c':
                if (flags == 0) /* DA1 */
                        return VTE_CMD_DA1;
                else if (flags == VTE_SEQ_FLAG_GT) /* DA2 */
                        return VTE_CMD_DA2;
                else if (flags == VTE_SEQ_FLAG_EQUAL) /* DA3 */
                        return VTE_CMD_DA3;
                break;
        case 'D':
                if (flags == 0) /* CUB */
                        return VTE_CMD_CUB;
                break;
        case 'd':
                if (flags == 0) /* VPA */
                        return VTE_CMD_VPA;
                break;
        case 'E':
                if (flags == 0) /* CNL */
                        return VTE_CMD_CNL;
                break;
        case 'e':
                if (flags == 0) /* VPR */
                        return VTE_CMD_VPR;
                break;
        case 'F':
                if (flags == 0) /* CPL */
                        return VTE_CMD_CPL;
                break;
        case 'f':
                if (flags == 0) /* HVP */
                        return VTE_CMD_HVP;
                break;
        case 'G':
                if (flags == 0) /* CHA */
                        return VTE_CMD_CHA;
                break;
        case 'g':
                if (flags == 0) /* TBC */
                        return VTE_CMD_TBC;
                else if (flags == VTE_SEQ_FLAG_MULT) /* DECLFKC */
                        return VTE_CMD_DECLFKC;
                break;
        case 'H':
                if (flags == 0) /* CUP */
                        return VTE_CMD_CUP;
                break;
        case 'h':
                if (flags == 0) /* SM ANSI */
                        return VTE_CMD_SM_ANSI;
                else if (flags == VTE_SEQ_FLAG_WHAT) /* SM DEC */
                        return VTE_CMD_SM_DEC;
                break;
        case 'I':
                if (flags == 0) /* CHT */
                        return VTE_CMD_CHT;
                break;
        case 'i':
                if (flags == 0) /* MC ANSI */
                        return VTE_CMD_MC_ANSI;
                else if (flags == VTE_SEQ_FLAG_WHAT) /* MC DEC */
                        return VTE_CMD_MC_DEC;
                break;
        case 'J':
                if (flags == 0) /* ED */
                        return VTE_CMD_ED;
                else if (flags == VTE_SEQ_FLAG_WHAT) /* DECSED */
                        return VTE_CMD_DECSED;
                break;
        case 'K':
                if (flags == 0) /* EL */
                        return VTE_CMD_EL;
                else if (flags == VTE_SEQ_FLAG_WHAT) /* DECSEL */
                        return VTE_CMD_DECSEL;
                break;
        case 'L':
                if (flags == 0) /* IL */
                        return VTE_CMD_IL;
                break;
        case 'l':
                if (flags == 0) /* RM ANSI */
                        return VTE_CMD_RM_ANSI;
                else if (flags == VTE_SEQ_FLAG_WHAT) /* RM DEC */
                        return VTE_CMD_RM_DEC;
                break;
        case 'M':
                if (flags == 0) /* DL */
                        return VTE_CMD_DL;
                break;
        case 'm':
                if (flags == 0) /* SGR */
                        return VTE_CMD_SGR;
                else if (flags == VTE_SEQ_FLAG_GT) /* XTERM SMR */
                        return VTE_CMD_XTERM_SRV;
                break;
        case 'n':
                if (flags == 0) /* DSR ANSI */
                        return VTE_CMD_DSR_ANSI;
                else if (flags == VTE_SEQ_FLAG_GT) /* XTERM RMR */
                        return VTE_CMD_XTERM_RRV;
                else if (flags == VTE_SEQ_FLAG_WHAT) /* DSR DEC */
                        return VTE_CMD_DSR_DEC;
                break;
        case 'P':
                if (flags == 0) /* DCH */
                        return VTE_CMD_DCH;
                else if (flags == VTE_SEQ_FLAG_SPACE) /* PPA */
                        return VTE_CMD_PPA;
                break;
        case 'p':
                if (flags == 0) /* DECSSL */
                        return VTE_CMD_DECSSL;
                else if (flags == VTE_SEQ_FLAG_SPACE) /* DECSSCLS */
                        return VTE_CMD_DECSSCLS;
                else if (flags == VTE_SEQ_FLAG_BANG) /* DECSTR */
                        return VTE_CMD_DECSTR;
                else if (flags == VTE_SEQ_FLAG_DQUOTE) /* DECSCL */
                        return VTE_CMD_DECSCL;
                else if (flags == VTE_SEQ_FLAG_CASH) /* DECRQM-ANSI */
                        return VTE_CMD_DECRQM_ANSI;
                else if (flags == (VTE_SEQ_FLAG_CASH |
                                   VTE_SEQ_FLAG_WHAT)) /* DECRQM-DEC */
                        return VTE_CMD_DECRQM_DEC;
                else if (flags == VTE_SEQ_FLAG_PCLOSE) /* DECSDPT */
                        return VTE_CMD_DECSDPT;
                else if (flags == VTE_SEQ_FLAG_MULT) /* DECSPPCS */
                        return VTE_CMD_DECSPPCS;
                else if (flags == VTE_SEQ_FLAG_PLUS) /* DECSR */
                        return VTE_CMD_DECSR;
                else if (flags == VTE_SEQ_FLAG_COMMA) /* DECLTOD */
                        return VTE_CMD_DECLTOD;
                else if (flags == VTE_SEQ_FLAG_GT) /* XTERM SPM */
                        return VTE_CMD_XTERM_SPM;
                break;
        case 'Q':
                if (flags == VTE_SEQ_FLAG_SPACE) /* PPR */
                        return VTE_CMD_PPR;
                break;
        case 'q':
                if (flags == 0) /* DECLL */
                        return VTE_CMD_DECLL;
                else if (flags == VTE_SEQ_FLAG_SPACE) /* DECSCUSR */
                        return VTE_CMD_DECSCUSR;
                else if (flags == VTE_SEQ_FLAG_DQUOTE) /* DECSCA */
                        return VTE_CMD_DECSCA;
                else if (flags == VTE_SEQ_FLAG_CASH) /* DECSDDT */
                        return VTE_CMD_DECSDDT;
                else if (flags == VTE_SEQ_FLAG_MULT) /* DECSRC */
                        return VTE_CMD_DECSR;
                else if (flags == VTE_SEQ_FLAG_PLUS) /* DECELF */
                        return VTE_CMD_DECELF;
                else if (flags == VTE_SEQ_FLAG_COMMA) /* DECTID */
                        return VTE_CMD_DECTID;
                break;
        case 'R':
                if (flags == VTE_SEQ_FLAG_SPACE) /* PPB */
                        return VTE_CMD_PPB;
                break;
        case 'r':
                if (flags == 0) {
                        /* DECSTBM */
                        return VTE_CMD_DECSTBM;
                } else if (flags == VTE_SEQ_FLAG_SPACE) {
                        /* DECSKCV */
                        return VTE_CMD_DECSKCV;
                } else if (flags == VTE_SEQ_FLAG_CASH) {
                        /* DECCARA */
                        return VTE_CMD_DECCARA;
                } else if (flags == VTE_SEQ_FLAG_MULT) {
                        /* DECSCS */
                        return VTE_CMD_DECSCS;
                } else if (flags == VTE_SEQ_FLAG_PLUS) {
                        /* DECSMKR */
                        return VTE_CMD_DECSMKR;
                } else if (flags == VTE_SEQ_FLAG_WHAT) {
                        /*
                         * There's a conflict between DECPCTERM and XTERM-RPM.
                         * XTERM-RPM takes a single argument, DECPCTERM takes 2.
                         * Split both up and forward the call to the closer
                         * match.
                         */
                        if (seq->n_args <= 1) /* XTERM RPM */
                                return VTE_CMD_XTERM_RPM;
                        else if (seq->n_args >= 2) /* DECPCTERM */
                                return VTE_CMD_DECPCTERM;
                }
                break;
        case 'S':
                if (flags == 0) /* SU */
                        return VTE_CMD_SU;
                else if (flags == VTE_SEQ_FLAG_WHAT) /* XTERM SGFX */
                        return VTE_CMD_XTERM_SGFX;
                break;
        case 's':
                if (flags == 0) {
                        /*
                         * There's a conflict between DECSLRM and SC-ANSI which
                         * cannot be resolved without knowing the state of
                         * DECLRMM. We leave that decision up to the caller.
                         */
                        return VTE_CMD_DECSLRM_OR_SC;
                } else if (flags == VTE_SEQ_FLAG_CASH) {
                        /* DECSPRTT */
                        return VTE_CMD_DECSPRTT;
                } else if (flags == VTE_SEQ_FLAG_MULT) {
                        /* DECSFC */
                        return VTE_CMD_DECSFC;
                } else if (flags == VTE_SEQ_FLAG_WHAT) {
                        /* XTERM SPM */
                        return VTE_CMD_XTERM_SPM;
                }
                break;
        case 'T':
                if (flags == 0) {
                        /*
                         * There's a conflict between SD and XTERM IHMT that we
                         * have to resolve by checking the parameter count.
                         * XTERM_IHMT needs exactly 5 arguments, SD takes 0 or
                         * 1. We're conservative here and give both a wider
                         * range to allow unused arguments (compat...).
                         */
                        if (seq->n_args < 5) {
                                /* SD */
                                return VTE_CMD_SD;
                        } else if (seq->n_args >= 5) {
                                /* XTERM IHMT */
                                return VTE_CMD_XTERM_IHMT;
                        }
                } else if (flags == VTE_SEQ_FLAG_GT) {
                        /* XTERM RTM */
                        return VTE_CMD_XTERM_RTM;
                }
                break;
        case 't':
                if (flags == 0) {
                        /*
                         * There's a conflict between XTERM_WM and DECSLPP. We
                         * cannot resolve it as some combinations are valid for
                         * both. We go with XTERM_WM for now.
                         *
                         * TODO: Figure out how to resolve that conflict and
                         *       return VTE_CMD_DECSLPP if possible.
                         */
                        return VTE_CMD_XTERM_WM; /* XTERM WM */
                } else if (flags == VTE_SEQ_FLAG_SPACE) {
                        /* DECSWBV */
                        return VTE_CMD_DECSWBV;
                } else if (flags == VTE_SEQ_FLAG_DQUOTE) {
                        /* DECSRFR */
                        return VTE_CMD_DECSRFR;
                } else if (flags == VTE_SEQ_FLAG_CASH) {
                        /* DECRARA */
                        return VTE_CMD_DECRARA;
                } else if (flags == VTE_SEQ_FLAG_GT) {
                        /* XTERM STM */
                        return VTE_CMD_XTERM_STM;
                }
                break;
        case 'U':
                if (flags == 0) /* NP */
                        return VTE_CMD_NP;
                break;
        case 'u':
                if (flags == 0) {
                        /* RC */
                        return VTE_CMD_RC;
                } else if (flags == VTE_SEQ_FLAG_SPACE) {
                        /* DECSMBV */
                        return VTE_CMD_DECSMBV;
                } else if (flags == VTE_SEQ_FLAG_DQUOTE) {
                        /* DECSTRL */
                        return VTE_CMD_DECSTRL;
                } else if (flags == VTE_SEQ_FLAG_WHAT) {
                        /* DECRQUPSS */
                        return VTE_CMD_DECRQUPSS;
716
                } else if (vte_seq_arg_value(seq->args[0]) == 1 && flags == VTE_SEQ_FLAG_CASH) {
717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741
                        /* DECRQTSR */
                        return VTE_CMD_DECRQTSR;
                } else if (flags == VTE_SEQ_FLAG_MULT) {
                        /* DECSCP */
                        return VTE_CMD_DECSCP;
                } else if (flags == VTE_SEQ_FLAG_COMMA) {
                        /* DECRQKT */
                        return VTE_CMD_DECRQKT;
                }
                break;
        case 'V':
                if (flags == 0) /* PP */
                        return VTE_CMD_PP;
                break;
        case 'v':
                if (flags == VTE_SEQ_FLAG_SPACE) /* DECSLCK */
                        return VTE_CMD_DECSLCK;
                else if (flags == VTE_SEQ_FLAG_DQUOTE) /* DECRQDE */
                        return VTE_CMD_DECRQDE;
                else if (flags == VTE_SEQ_FLAG_CASH) /* DECCRA */
                        return VTE_CMD_DECCRA;
                else if (flags == VTE_SEQ_FLAG_COMMA) /* DECRPKT */
                        return VTE_CMD_DECRPKT;
                break;
        case 'W':
742
                if (vte_seq_arg_value(seq->args[0]) == 5 && flags == VTE_SEQ_FLAG_WHAT) {
743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837
                        /* DECST8C */
                        return VTE_CMD_DECST8C;
                }
                break;
        case 'w':
                if (flags == VTE_SEQ_FLAG_CASH) /* DECRQPSR */
                        return VTE_CMD_DECRQPSR;
                else if (flags == VTE_SEQ_FLAG_SQUOTE) /* DECEFR */
                        return VTE_CMD_DECEFR;
                else if (flags == VTE_SEQ_FLAG_PLUS) /* DECSPP */
                        return VTE_CMD_DECSPP;
                break;
        case 'X':
                if (flags == 0) /* ECH */
                        return VTE_CMD_ECH;
                break;
        case 'x':
                if (flags == 0) /* DECREQTPARM */
                        return VTE_CMD_DECREQTPARM;
                else if (flags == VTE_SEQ_FLAG_CASH) /* DECFRA */
                        return VTE_CMD_DECFRA;
                else if (flags == VTE_SEQ_FLAG_MULT) /* DECSACE */
                        return VTE_CMD_DECSACE;
                else if (flags == VTE_SEQ_FLAG_PLUS) /* DECRQPKFM */
                        return VTE_CMD_DECRQPKFM;
                break;
        case 'y':
                if (flags == 0) /* DECTST */
                        return VTE_CMD_DECTST;
                else if (flags == VTE_SEQ_FLAG_MULT) /* DECRQCRA */
                        return VTE_CMD_DECRQCRA;
                else if (flags == VTE_SEQ_FLAG_PLUS) /* DECPKFMR */
                        return VTE_CMD_DECPKFMR;
                break;
        case 'Z':
                if (flags == 0) /* CBT */
                        return VTE_CMD_CBT;
                break;
        case 'z':
                if (flags == VTE_SEQ_FLAG_CASH) /* DECERA */
                        return VTE_CMD_DECERA;
                else if (flags == VTE_SEQ_FLAG_SQUOTE) /* DECELR */
                        return VTE_CMD_DECELR;
                else if (flags == VTE_SEQ_FLAG_MULT) /* DECINVM */
                        return VTE_CMD_DECINVM;
                else if (flags == VTE_SEQ_FLAG_PLUS) /* DECPKA */
                        return VTE_CMD_DECPKA;
                break;
        case '@':
                if (flags == 0) /* ICH */
                        return VTE_CMD_ICH;
                break;
        case '`':
                if (flags == 0) /* HPA */
                        return VTE_CMD_HPA;
                break;
        case '{':
                if (flags == VTE_SEQ_FLAG_CASH) /* DECSERA */
                        return VTE_CMD_DECSERA;
                else if (flags == VTE_SEQ_FLAG_SQUOTE) /* DECSLE */
                        return VTE_CMD_DECSLE;
                break;
        case '|':
                if (flags == VTE_SEQ_FLAG_CASH) /* DECSCPP */
                        return VTE_CMD_DECSCPP;
                else if (flags == VTE_SEQ_FLAG_SQUOTE) /* DECRQLP */
                        return VTE_CMD_DECRQLP;
                else if (flags == VTE_SEQ_FLAG_MULT) /* DECSNLS */
                        return VTE_CMD_DECSNLS;
                break;
        case '}':
                if (flags == VTE_SEQ_FLAG_SPACE) /* DECKBD */
                        return VTE_CMD_DECKBD;
                else if (flags == VTE_SEQ_FLAG_CASH) /* DECSASD */
                        return VTE_CMD_DECSASD;
                else if (flags == VTE_SEQ_FLAG_SQUOTE) /* DECIC */
                        return VTE_CMD_DECIC;
                break;
        case '~':
                if (flags == VTE_SEQ_FLAG_SPACE) /* DECTME */
                        return VTE_CMD_DECTME;
                else if (flags == VTE_SEQ_FLAG_CASH) /* DECSSDT */
                        return VTE_CMD_DECSSDT;
                else if (flags == VTE_SEQ_FLAG_SQUOTE) /* DECDC */
                        return VTE_CMD_DECDC;
                break;
        }

        return VTE_CMD_NONE;
}

/*
 * State Machine
 * This parser controls the parser-state and returns any detected sequence to
 * the caller. The parser is based on this state-diagram from Paul Williams:
838
 *   https://vt100.net/emu/
839 840 841 842 843 844
 * It was written from scratch and extended where needed.
 * This parser is fully compatible up to the vt500 series. We expect UCS-4 as
 * input. It's the callers responsibility to do any UTF-8 parsing.
 */

enum parser_state {
845 846 847 848
        STATE_NONE,             /* placeholder */
        STATE_GROUND,           /* initial state and ground */
        STATE_ESC,              /* ESC sequence was started */
        STATE_ESC_INT,          /* intermediate escape characters */
849 850
        STATE_CSI_ENTRY,        /* starting CSI sequence */
        STATE_CSI_PARAM,        /* CSI parameters */
851 852
        STATE_CSI_INT,          /* intermediate CSI characters */
        STATE_CSI_IGNORE,       /* CSI error; ignore this CSI sequence */
853 854
        STATE_DCS_ENTRY,        /* starting DCS sequence */
        STATE_DCS_PARAM,        /* DCS parameters */
855 856 857 858
        STATE_DCS_INT,          /* intermediate DCS characters */
        STATE_DCS_PASS,         /* DCS data passthrough */
        STATE_DCS_IGNORE,       /* DCS error; ignore this DCS sequence */
        STATE_OSC_STRING,       /* parsing OSC sequence */
859 860 861 862 863
        STATE_ST_IGNORE,        /* unimplemented seq; ignore until ST */
        STATE_N,
};

enum parser_action {
864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880
        ACTION_NONE,            /* placeholder */
        ACTION_CLEAR,           /* clear parameters */
        ACTION_IGNORE,          /* ignore the character entirely */
        ACTION_PRINT,           /* print the character on the console */
        ACTION_EXECUTE,         /* execute single control character (C0/C1) */
        ACTION_COLLECT,         /* collect intermediate character */
        ACTION_PARAM,           /* collect parameter character */
        ACTION_ESC_DISPATCH,    /* dispatch escape sequence */
        ACTION_CSI_DISPATCH,    /* dispatch CSI sequence */
        ACTION_DCS_START,       /* start of DCS data */
        ACTION_DCS_COLLECT,     /* collect DCS data */
        ACTION_DCS_CONSUME,     /* consume DCS terminator */
        ACTION_DCS_DISPATCH,    /* dispatch DCS sequence */
        ACTION_OSC_START,       /* start of OSC data */
        ACTION_OSC_COLLECT,     /* collect OSC data */
        ACTION_OSC_CONSUME,     /* consume OSC terminator */
        ACTION_OSC_DISPATCH,    /* dispatch OSC sequence */
881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934
        ACTION_N,
};

/**
 * vte_parser_new() - Allocate parser object
 * @out:        output variable for new parser object
 *
 * Return: 0 on success, negative error code on failure.
 */
int vte_parser_new(struct vte_parser **out)
{
        struct vte_parser *parser;

        parser = (struct vte_parser*)kzalloc(sizeof(*parser), GFP_KERNEL);
        if (!parser)
                return -ENOMEM;

        parser->st_alloc = 64;
        parser->seq.st = (char*)kzalloc(parser->st_alloc + 1, GFP_KERNEL);
        if (!parser->seq.st) {
                kfree(parser);
                return -ENOMEM;
        }

        *out = parser;
        return 0;
}

/**
 * vte_parser_free() - Free parser object
 * @parser:        parser object to free, or NULL
 *
 * Return: NULL is returned.
 */
struct vte_parser *vte_parser_free(struct vte_parser *parser)
{
        if (!parser)
                return NULL;

        kfree(parser->seq.st);
        kfree(parser);
        return NULL;
}

static inline void parser_clear(struct vte_parser *parser)
{
        unsigned int i;

        parser->seq.command = VTE_CMD_NONE;
        parser->seq.terminator = 0;
        parser->seq.intermediates = 0;
        parser->seq.charset = VTE_CHARSET_NONE;
        parser->seq.n_args = 0;
        for (i = 0; i < VTE_PARSER_ARG_MAX; ++i)
935
                parser->seq.args[i] = VTE_SEQ_ARG_INIT_DEFAULT;
936 937 938 939 940

        parser->seq.n_st = 0;
        parser->seq.st[0] = 0;
}

941
static int parser_ignore(struct vte_parser *parser, uint32_t raw)
942 943 944 945 946 947 948 949 950 951
{
        parser_clear(parser);
        parser->seq.type = VTE_SEQ_IGNORE;
        parser->seq.command = VTE_CMD_NONE;
        parser->seq.terminator = raw;
        parser->seq.charset = VTE_CHARSET_NONE;

        return parser->seq.type;
}

952
static int parser_print(struct vte_parser *parser, uint32_t raw)
953 954 955 956 957 958 959 960 961 962
{
        parser_clear(parser);
        parser->seq.type = VTE_SEQ_GRAPHIC;
        parser->seq.command = VTE_CMD_GRAPHIC;
        parser->seq.terminator = raw;
        parser->seq.charset = VTE_CHARSET_NONE;

        return parser->seq.type;
}

963
static int parser_execute(struct vte_parser *parser, uint32_t raw)
964 965 966 967 968 969 970 971 972 973
{
        parser->seq.type = VTE_SEQ_CONTROL;
        parser->seq.command = VTE_CMD_GRAPHIC;
        parser->seq.terminator = raw;
        parser->seq.charset = VTE_CHARSET_NONE;
        parser->seq.command = vte_parse_host_control(&parser->seq);

        return parser->seq.type;
}

974
static void parser_collect(struct vte_parser *parser, uint32_t raw)
975 976 977 978 979 980 981 982 983 984 985 986 987 988
{
        /*
         * Usually, characters from 0x30 to 0x3f are only allowed as leading
         * markers (or as part of the parameters), characters from 0x20 to 0x2f
         * are only allowed as trailing markers. However, our state-machine
         * already verifies those restrictions so we can handle them the same
         * way here. Note that we safely allow markers to be specified multiple
         * times.
         */

        if (raw >= 0x20 && raw <= 0x3f)
                parser->seq.intermediates |= 1 << (raw - 0x20);
}

989
static void parser_param(struct vte_parser *parser, uint32_t raw)
990 991
{
        if (raw == ';') {
992 993
                if (parser->seq.n_args < VTE_PARSER_ARG_MAX) {
                        vte_seq_arg_finish(&parser->seq.args[parser->seq.n_args]);
994
                        ++parser->seq.n_args;
995
                }
996 997 998 999 1000 1001 1002 1003

                return;
        }

        if (parser->seq.n_args >= VTE_PARSER_ARG_MAX)
                return;

        if (raw >= '0' && raw <= '9') {
1004
                vte_seq_arg_push(&parser->seq.args[parser->seq.n_args], raw);
1005 1006 1007
        }
}

1008
static int parser_esc(struct vte_parser *parser, uint32_t raw)
1009 1010 1011 1012 1013 1014
{
        parser->seq.type = VTE_SEQ_ESCAPE;
        parser->seq.command = VTE_CMD_NONE;
        parser->seq.terminator = raw;
        parser->seq.charset = VTE_CHARSET_NONE;
        parser->seq.command = vte_parse_host_escape(&parser->seq,
1015
                                                    &parser->seq.charset);
1016 1017 1018 1019

        return parser->seq.type;
}

1020
static int parser_csi(struct vte_parser *parser, uint32_t raw)
1021 1022 1023 1024 1025 1026
{
        /* parser->seq is cleared during CSI-ENTER state, thus there's no need
         * to clear invalid fields here. */

        if (parser->seq.n_args < VTE_PARSER_ARG_MAX) {
                if (parser->seq.n_args > 0 ||
1027 1028
                    vte_seq_arg_started(parser->seq.args[parser->seq.n_args])) {
                        vte_seq_arg_finish(&parser->seq.args[parser->seq.n_args]);
1029
                        ++parser->seq.n_args;
1030
                }
1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043
        }

        parser->seq.type = VTE_SEQ_CSI;
        parser->seq.command = VTE_CMD_NONE;
        parser->seq.terminator = raw;
        parser->seq.charset = VTE_CHARSET_NONE;
        parser->seq.command = vte_parse_host_csi(&parser->seq);

        return parser->seq.type;
}

/* perform state transition and dispatch related actions */
static int parser_transition(struct vte_parser *parser,
1044
                             uint32_t raw,
1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102
                             unsigned int state,
                             unsigned int action)
{
        if (state != STATE_NONE)
                parser->state = state;

        switch (action) {
        case ACTION_NONE:
                return VTE_SEQ_NONE;
        case ACTION_CLEAR:
                parser_clear(parser);
                return VTE_SEQ_NONE;
        case ACTION_IGNORE:
                return parser_ignore(parser, raw);
        case ACTION_PRINT:
                return parser_print(parser, raw);
        case ACTION_EXECUTE:
                return parser_execute(parser, raw);
        case ACTION_COLLECT:
                parser_collect(parser, raw);
                return VTE_SEQ_NONE;
        case ACTION_PARAM:
                parser_param(parser, raw);
                return VTE_SEQ_NONE;
        case ACTION_ESC_DISPATCH:
                return parser_esc(parser, raw);
        case ACTION_CSI_DISPATCH:
                return parser_csi(parser, raw);
        case ACTION_DCS_START:
                /* not implemented */
                return VTE_SEQ_NONE;
        case ACTION_DCS_COLLECT:
                /* not implemented */
                return VTE_SEQ_NONE;
        case ACTION_DCS_CONSUME:
                /* not implemented */
                return VTE_SEQ_NONE;
        case ACTION_DCS_DISPATCH:
                /* not implemented */
                return VTE_SEQ_NONE;
        case ACTION_OSC_START:
                /* not implemented */
                return VTE_SEQ_NONE;
        case ACTION_OSC_COLLECT:
                /* not implemented */
                return VTE_SEQ_NONE;
        case ACTION_OSC_CONSUME:
                /* not implemented */
                return VTE_SEQ_NONE;
        case ACTION_OSC_DISPATCH:
                /* not implemented */
                return VTE_SEQ_NONE;
        default:
                WARN(1, "invalid vte-parser action");
                return VTE_SEQ_NONE;
        }
}

1103
static int parser_feed_to_state(struct vte_parser *parser, uint32_t raw)
1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436
{
        switch (parser->state) {
        case STATE_NONE:
                /*
                 * During initialization, parser->state is cleared. Treat this
                 * as STATE_GROUND. We will then never get to STATE_NONE again.
                 */
        case STATE_GROUND:
                switch (raw) {
                case 0x00 ... 0x1f:        /* C0 */
                case 0x80 ... 0x9b:        /* C1 \ { ST } */
                case 0x9d ... 0x9f:
                        return parser_transition(parser, raw, STATE_NONE,
                                                 ACTION_EXECUTE);
                case 0x9c:                /* ST */
                        return parser_transition(parser, raw, STATE_NONE,
                                                 ACTION_IGNORE);
                }

                return parser_transition(parser, raw,
                                         STATE_NONE, ACTION_PRINT);
        case STATE_ESC:
                switch (raw) {
                case 0x00 ... 0x1f:        /* C0 */
                        return parser_transition(parser, raw, STATE_NONE,
                                                 ACTION_EXECUTE);
                case 0x20 ... 0x2f:        /* [' ' - '\'] */
                        return parser_transition(parser, raw, STATE_ESC_INT,
                                                 ACTION_COLLECT);
                case 0x30 ... 0x4f:        /* ['0' - '~'] \ */
                case 0x51 ... 0x57:        /* { 'P', 'X', '[', ']', '^', '_' } */
                case 0x59 ... 0x5a:
                case 0x5c:
                case 0x60 ... 0x7e:
                        return parser_transition(parser, raw, STATE_GROUND,
                                                 ACTION_ESC_DISPATCH);
                case 0x50:                /* 'P' */
                        return parser_transition(parser, raw, STATE_DCS_ENTRY,
                                                 ACTION_CLEAR);
                case 0x5b:                /* '[' */
                        return parser_transition(parser, raw, STATE_CSI_ENTRY,
                                                 ACTION_CLEAR);
                case 0x5d:                /* ']' */
                        return parser_transition(parser, raw, STATE_OSC_STRING,
                                                 ACTION_CLEAR);
                case 0x58:                /* 'X' */
                case 0x5e:                /* '^' */
                case 0x5f:                /* '_' */
                        return parser_transition(parser, raw, STATE_ST_IGNORE,
                                                 ACTION_NONE);
                case 0x7f:                /* DEL */
                        return parser_transition(parser, raw, STATE_NONE,
                                                 ACTION_IGNORE);
                case 0x9c:                /* ST */
                        return parser_transition(parser, raw, STATE_GROUND,
                                                 ACTION_IGNORE);
                }

                return parser_transition(parser, raw,
                                         STATE_ESC_INT, ACTION_COLLECT);
        case STATE_ESC_INT:
                switch (raw) {
                case 0x00 ... 0x1f:        /* C0 */
                        return parser_transition(parser, raw, STATE_NONE,
                                                 ACTION_EXECUTE);
                case 0x20 ... 0x2f:        /* [' ' - '\'] */
                        return parser_transition(parser, raw, STATE_NONE,
                                                 ACTION_COLLECT);
                case 0x30 ... 0x7e:        /* ['0' - '~'] */
                        return parser_transition(parser, raw, STATE_GROUND,
                                                 ACTION_ESC_DISPATCH);
                case 0x7f:                /* DEL */
                        return parser_transition(parser, raw, STATE_NONE,
                                                 ACTION_IGNORE);
                case 0x9c:                /* ST */
                        return parser_transition(parser, raw, STATE_GROUND,
                                                 ACTION_IGNORE);
                }

                return parser_transition(parser, raw,
                                         STATE_NONE, ACTION_COLLECT);
        case STATE_CSI_ENTRY:
                switch (raw) {
                case 0x00 ... 0x1f:        /* C0 */
                        return parser_transition(parser, raw, STATE_NONE,
                                                 ACTION_EXECUTE);
                case 0x20 ... 0x2f:        /* [' ' - '\'] */
                        return parser_transition(parser, raw, STATE_CSI_INT,
                                                 ACTION_COLLECT);
                case 0x3a:                /* ':' */
                        return parser_transition(parser, raw, STATE_CSI_IGNORE,
                                                 ACTION_NONE);
                case 0x30 ... 0x39:        /* ['0' - '9'] */
                case 0x3b:                /* ';' */
                        return parser_transition(parser, raw, STATE_CSI_PARAM,
                                                 ACTION_PARAM);
                case 0x3c ... 0x3f:        /* ['<' - '?'] */
                        return parser_transition(parser, raw, STATE_CSI_PARAM,
                                                 ACTION_COLLECT);
                case 0x40 ... 0x7e:        /* ['@' - '~'] */
                        return parser_transition(parser, raw, STATE_GROUND,
                                                 ACTION_CSI_DISPATCH);
                case 0x7f:                /* DEL */
                        return parser_transition(parser, raw, STATE_NONE,
                                                 ACTION_IGNORE);
                case 0x9c:                /* ST */
                        return parser_transition(parser, raw, STATE_GROUND,
                                                 ACTION_IGNORE);
                }

                return parser_transition(parser, raw,
                                         STATE_CSI_IGNORE, ACTION_NONE);
        case STATE_CSI_PARAM:
                switch (raw) {
                case 0x00 ... 0x1f:        /* C0 */
                        return parser_transition(parser, raw, STATE_NONE,
                                                 ACTION_EXECUTE);
                case 0x20 ... 0x2f:        /* [' ' - '\'] */
                        return parser_transition(parser, raw, STATE_CSI_INT,
                                                 ACTION_COLLECT);
                case 0x30 ... 0x39:        /* ['0' - '9'] */
                case 0x3b:                /* ';' */
                        return parser_transition(parser, raw, STATE_NONE,
                                                 ACTION_PARAM);
                case 0x3a:                /* ':' */
                case 0x3c ... 0x3f:        /* ['<' - '?'] */
                        return parser_transition(parser, raw, STATE_CSI_IGNORE,
                                                 ACTION_NONE);
                case 0x40 ... 0x7e:        /* ['@' - '~'] */
                        return parser_transition(parser, raw, STATE_GROUND,
                                                 ACTION_CSI_DISPATCH);
                case 0x7f:                /* DEL */
                        return parser_transition(parser, raw, STATE_NONE,
                                                 ACTION_IGNORE);
                case 0x9c:                /* ST */
                        return parser_transition(parser, raw, STATE_GROUND,
                                                 ACTION_IGNORE);
                }

                return parser_transition(parser, raw,
                                         STATE_CSI_IGNORE, ACTION_NONE);
        case STATE_CSI_INT:
                switch (raw) {
                case 0x00 ... 0x1f:        /* C0 */
                        return parser_transition(parser, raw, STATE_NONE,
                                                 ACTION_EXECUTE);
                case 0x20 ... 0x2f:        /* [' ' - '\'] */
                        return parser_transition(parser, raw, STATE_NONE,
                                                 ACTION_COLLECT);
                case 0x30 ... 0x3f:        /* ['0' - '?'] */
                        return parser_transition(parser, raw, STATE_CSI_IGNORE,
                                                 ACTION_NONE);
                case 0x40 ... 0x7e:        /* ['@' - '~'] */
                        return parser_transition(parser, raw, STATE_GROUND,
                                                 ACTION_CSI_DISPATCH);
                case 0x7f:                /* DEL */
                        return parser_transition(parser, raw, STATE_NONE,
                                                 ACTION_IGNORE);
                case 0x9c:                /* ST */
                        return parser_transition(parser, raw, STATE_GROUND,
                                                 ACTION_IGNORE);
                }

                return parser_transition(parser, raw,
                                         STATE_CSI_IGNORE, ACTION_NONE);
        case STATE_CSI_IGNORE:
                switch (raw) {
                case 0x00 ... 0x1f:        /* C0 */
                        return parser_transition(parser, raw, STATE_NONE,
                                                 ACTION_EXECUTE);
                case 0x20 ... 0x3f:        /* [' ' - '?'] */
                        return parser_transition(parser, raw, STATE_NONE,
                                                 ACTION_NONE);
                case 0x40 ... 0x7e:        /* ['@' - '~'] */
                        return parser_transition(parser, raw, STATE_GROUND,
                                                 ACTION_NONE);
                case 0x7f:                /* DEL */
                        return parser_transition(parser, raw, STATE_NONE,
                                                 ACTION_IGNORE);
                case 0x9c:                /* ST */
                        return parser_transition(parser, raw, STATE_GROUND,
                                                 ACTION_IGNORE);
                }

                return parser_transition(parser, raw,
                                         STATE_NONE, ACTION_NONE);
        case STATE_DCS_ENTRY:
                switch (raw) {
                case 0x00 ... 0x1f:        /* C0 */
                        return parser_transition(parser, raw, STATE_NONE,
                                                 ACTION_IGNORE);
                case 0x20 ... 0x2f:        /* [' ' - '\'] */
                        return parser_transition(parser, raw, STATE_DCS_INT,
                                                 ACTION_COLLECT);
                case 0x3a:                /* ':' */
                        return parser_transition(parser, raw, STATE_DCS_IGNORE,
                                                 ACTION_NONE);
                case 0x30 ... 0x39:        /* ['0' - '9'] */
                case 0x3b:                /* ';' */
                        return parser_transition(parser, raw, STATE_DCS_PARAM,
                                                 ACTION_PARAM);
                case 0x3c ... 0x3f:        /* ['<' - '?'] */
                        return parser_transition(parser, raw, STATE_DCS_PARAM,
                                                 ACTION_COLLECT);
                case 0x40 ... 0x7e:        /* ['@' - '~'] */
                        return parser_transition(parser, raw, STATE_DCS_PASS,
                                                 ACTION_DCS_CONSUME);
                case 0x7f:                /* DEL */
                        return parser_transition(parser, raw, STATE_NONE,
                                                 ACTION_IGNORE);
                case 0x9c:                /* ST */
                        return parser_transition(parser, raw, STATE_GROUND,
                                                 ACTION_IGNORE);
                }

                return parser_transition(parser, raw,
                                         STATE_DCS_PASS, ACTION_DCS_CONSUME);
        case STATE_DCS_PARAM:
                switch (raw) {
                case 0x00 ... 0x1f:        /* C0 */
                        return parser_transition(parser, raw, STATE_NONE,
                                                 ACTION_IGNORE);
                case 0x20 ... 0x2f:        /* [' ' - '\'] */
                        return parser_transition(parser, raw, STATE_DCS_INT,
                                                 ACTION_COLLECT);
                case 0x30 ... 0x39:        /* ['0' - '9'] */
                case 0x3b:                /* ';' */
                        return parser_transition(parser, raw, STATE_NONE,
                                                 ACTION_PARAM);
                case 0x3a:                /* ':' */
                case 0x3c ... 0x3f:        /* ['<' - '?'] */
                        return parser_transition(parser, raw, STATE_DCS_IGNORE,
                                                 ACTION_NONE);
                case 0x40 ... 0x7e:        /* ['@' - '~'] */
                        return parser_transition(parser, raw, STATE_DCS_PASS,
                                                 ACTION_DCS_CONSUME);
                case 0x7f:                /* DEL */
                        return parser_transition(parser, raw, STATE_NONE,
                                                 ACTION_IGNORE);
                case 0x9c:                /* ST */
                        return parser_transition(parser, raw, STATE_GROUND,
                                                 ACTION_IGNORE);
                }

                return parser_transition(parser, raw,
                                         STATE_DCS_PASS, ACTION_DCS_CONSUME);
        case STATE_DCS_INT:
                switch (raw) {
                case 0x00 ... 0x1f:        /* C0 */
                        return parser_transition(parser, raw, STATE_NONE,
                                                 ACTION_IGNORE);
                case 0x20 ... 0x2f:        /* [' ' - '\'] */
                        return parser_transition(parser, raw, STATE_NONE,
                                                 ACTION_COLLECT);
                case 0x30 ... 0x3f:        /* ['0' - '?'] */
                        return parser_transition(parser, raw, STATE_DCS_IGNORE,
                                                 ACTION_NONE);
                case 0x40 ... 0x7e:        /* ['@' - '~'] */
                        return parser_transition(parser, raw, STATE_DCS_PASS,
                                                 ACTION_DCS_CONSUME);
                case 0x7f:                /* DEL */
                        return parser_transition(parser, raw, STATE_NONE,
                                                 ACTION_IGNORE);
                case 0x9c:                /* ST */
                        return parser_transition(parser, raw, STATE_GROUND,
                                                 ACTION_IGNORE);
                }

                return parser_transition(parser, raw,
                                         STATE_DCS_PASS, ACTION_DCS_CONSUME);
        case STATE_DCS_PASS:
                switch (raw) {
                case 0x00 ... 0x7e:        /* ASCII \ { DEL } */
                        return parser_transition(parser, raw, STATE_NONE,
                                                 ACTION_DCS_COLLECT);
                case 0x7f:                /* DEL */
                        return parser_transition(parser, raw, STATE_NONE,
                                                 ACTION_IGNORE);
                case 0x9c:                /* ST */
                        return parser_transition(parser, raw, STATE_GROUND,
                                                 ACTION_DCS_DISPATCH);
                }

                return parser_transition(parser, raw,
                                         STATE_NONE, ACTION_DCS_COLLECT);
        case STATE_DCS_IGNORE:
                switch (raw) {
                case 0x00 ... 0x7f:        /* ASCII */
                        return parser_transition(parser, raw, STATE_NONE,
                                                 ACTION_IGNORE);
                case 0x9c:                /* ST */
                        return parser_transition(parser, raw, STATE_GROUND,
                                                 ACTION_NONE);
                }

                return parser_transition(parser, raw,
                                         STATE_NONE, ACTION_NONE);
        case STATE_OSC_STRING:
                switch (raw) {
                case 0x00 ... 0x06:        /* C0 \ { BEL } */
                case 0x08 ... 0x1f:
                        return parser_transition(parser, raw, STATE_NONE,
                                                 ACTION_IGNORE);
                case 0x20 ... 0x7f:        /* [' ' - DEL] */
                        return parser_transition(parser, raw, STATE_NONE,
                                                 ACTION_OSC_COLLECT);
                case 0x07:                /* BEL */
                case 0x9c:                /* ST */
                        return parser_transition(parser, raw, STATE_GROUND,
                                                 ACTION_OSC_DISPATCH);
                }

                return parser_transition(parser, raw,
                                         STATE_NONE, ACTION_OSC_COLLECT);
        case STATE_ST_IGNORE:
                switch (raw) {
                case 0x00 ... 0x7f:        /* ASCII */
                        return parser_transition(parser, raw,
                                                 STATE_NONE, ACTION_IGNORE);
                case 0x9c:                /* ST */
                        return parser_transition(parser, raw,
                                                 STATE_GROUND, ACTION_IGNORE);
                }

                return parser_transition(parser, raw,
                                         STATE_NONE, ACTION_NONE);
        }

        WARN(1, "bad vte-parser state");
        return -EINVAL;
}

int vte_parser_feed(struct vte_parser *parser,
1437 1438
                    /* const */ struct vte_seq **seq_out,
                    uint32_t raw)
1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492
{
        int ret;

        /*
         * Notes:
         *  * DEC treats GR codes as GL. We don't do that as we require UTF-8
         *    as charset and, thus, it doesn't make sense to treat GR special.
         *  * During control sequences, unexpected C1 codes cancel the sequence
         *    and immediately start a new one. C0 codes, however, may or may not
         *    be ignored/executed depending on the sequence.
         */

        switch (raw) {
        case 0x18:                /* CAN */
                ret = parser_transition(parser, raw,
                                        STATE_GROUND, ACTION_IGNORE);
                break;
        case 0x1a:                /* SUB */
                ret = parser_transition(parser, raw,
                                        STATE_GROUND, ACTION_EXECUTE);
                break;
        case 0x80 ... 0x8f:        /* C1 \ {DCS, SOS, CSI, ST, OSC, PM, APC} */
        case 0x91 ... 0x97:
        case 0x99 ... 0x9a:
                ret = parser_transition(parser, raw,
                                        STATE_GROUND, ACTION_EXECUTE);
                break;
        case 0x1b:                /* ESC */
                ret = parser_transition(parser, raw,
                                        STATE_ESC, ACTION_CLEAR);
                break;
        case 0x98:                /* SOS */
        case 0x9e:                /* PM */
        case 0x9f:                /* APC */
                ret = parser_transition(parser, raw,
                                        STATE_ST_IGNORE, ACTION_NONE);
                break;
        case 0x90:                /* DCS */
                ret = parser_transition(parser, raw,
                                        STATE_DCS_ENTRY, ACTION_CLEAR);
                break;
        case 0x9d:                /* OSC */
                ret = parser_transition(parser, raw,
                                        STATE_OSC_STRING, ACTION_CLEAR);
                break;
        case 0x9b:                /* CSI */
                ret = parser_transition(parser, raw,
                                        STATE_CSI_ENTRY, ACTION_CLEAR);
                break;
        default:
                ret = parser_feed_to_state(parser, raw);
                break;
        }

1493
        if (G_UNLIKELY(ret < 0))
1494 1495 1496 1497 1498 1499
                *seq_out = NULL;
        else
                *seq_out = &parser->seq;

        return ret;
}
1500 1501 1502 1503 1504 1505

void vte_parser_reset(struct vte_parser *parser)
{
        /* const */ struct vte_seq *seq;
        vte_parser_feed(parser, &seq, 0x18 /* CAN */);
}