File: pty_terminal.c

package info (click to toggle)
brltty 6.7-3.1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 31,240 kB
  • sloc: ansic: 148,859; java: 13,418; sh: 9,623; xml: 5,699; tcl: 2,634; makefile: 2,333; awk: 713; lisp: 366; python: 321; ml: 301
file content (841 lines) | stat: -rw-r--r-- 19,815 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
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
65
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
144
145
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
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
399
400
401
402
403
404
405
406
407
408
409
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
716
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
742
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
838
839
840
841
/*
 * BRLTTY - A background process providing access to the console screen (when in
 *          text mode) for a blind person using a refreshable braille display.
 *
 * Copyright (C) 1995-2024 by The BRLTTY Developers.
 *
 * BRLTTY comes with ABSOLUTELY NO WARRANTY.
 *
 * This is free software, placed under the terms of the
 * GNU Lesser General Public License, as published by the Free Software
 * Foundation; either version 2.1 of the License, or (at your option) any
 * later version. Please see the file LICENSE-LGPL for details.
 *
 * Web Page: http://brltty.app/
 *
 * This software is maintained by Dave Mielke <dave@mielke.cc>.
 */

/* unimplemented output actions
 * enacs=\E(B\E)0 - enable alternate charset mode
 * hts=\EH - set tab
 * kmous=\E[M - mouse event
 * tbc=\E[3g - clear all tabs
 * u6=\E[%i%d;%dR - user string 6
 * u7=\E[6n - user string 7
 * u8=\E[?1;2c - user string 8
 * u9=\E[c - user string 9
 */

#include "prologue.h"

#include <string.h>

#include "log.h"
#include "strfmt.h"
#include "pty_terminal.h"
#include "pty_screen.h"
#include "scr_types.h"
#include "ascii.h"

static unsigned char terminalLogLevel = LOG_DEBUG;
static unsigned char logInput = 0;
static unsigned char logOutput = 0;
static unsigned char logSequences = 0;
static unsigned char logUnexpected = 0;

void
ptySetTerminalLogLevel (unsigned char level) {
  terminalLogLevel = level;
  ptySetScreenLogLevel(level);
}

void
ptySetLogTerminalInput (int yes) {
  logInput = yes;
}

void
ptySetLogTerminalOutput (int yes) {
  logOutput = yes;
}

void
ptySetLogTerminalSequences (int yes) {
  logSequences = yes;
}

void
ptySetLogUnexpectedTerminalIO (int yes) {
  logUnexpected = yes;
}

static const char ptyTerminalType[] = "screen";

const char *
ptyGetTerminalType (void) {
  return ptyTerminalType;
}

static unsigned char insertMode = 0;
static unsigned char alternateCharsetMode = 0;
static unsigned char keypadTransmitMode = 0;
static unsigned char bracketedPasteMode = 0;
static unsigned char absoluteCursorAddressingMode = 0;

int
ptyBeginTerminal (PtyObject *pty, int driverDirectives) {
  insertMode = 0;
  alternateCharsetMode = 0;
  keypadTransmitMode = 0;
  bracketedPasteMode = 0;
  absoluteCursorAddressingMode = 0;

  return ptyBeginScreen(pty, driverDirectives);
}

void
ptyEndTerminal (void) {
  ptyEndScreen();
}

static void
soundAlert (void) {
  beep();
}

static void
showAlert (void) {
  flash();
}

int
ptyProcessTerminalInput (PtyObject *pty) {
  int character = getch();

  if (logInput) {
    const char *name = keyname(character);
    if (!name) name = "unknown";
    logMessage(terminalLogLevel, "input: 0X%02X (%s)", character, name);
  }

  if (character > UINT8_MAX) {
    ScreenKey key = 0;

    #define KEY(from, to) case KEY_##from: key = SCR_KEY_##to; break;
    switch (character) {
      KEY(ENTER    , ENTER)
      KEY(BACKSPACE, BACKSPACE)

      KEY(LEFT     , CURSOR_LEFT)
      KEY(RIGHT    , CURSOR_RIGHT)
      KEY(UP       , CURSOR_UP)
      KEY(DOWN     , CURSOR_DOWN)

      KEY(PPAGE    , PAGE_UP)
      KEY(NPAGE    , PAGE_DOWN)
      KEY(HOME     , HOME)
      KEY(END      , END)
      KEY(IC       , INSERT)
      KEY(DC       , DELETE)

      KEY(F( 1)    , F1)
      KEY(F( 2)    , F2)
      KEY(F( 3)    , F3)
      KEY(F( 4)    , F4)
      KEY(F( 5)    , F5)
      KEY(F( 6)    , F6)
      KEY(F( 7)    , F7)
      KEY(F( 8)    , F8)
      KEY(F( 9)    , F9)
      KEY(F(10)    , F10)
      KEY(F(11)    , F11)
      KEY(F(12)    , F12)
    }
    #undef KEY

    if (key) {
      if (!ptyWriteInputCharacter(pty, key, keypadTransmitMode)) {
        return 0;
      }
    } else if (logUnexpected) {
      const char *name = keyname(character);
      if (!name) name = "unknown";
      logMessage(terminalLogLevel, "unexpected input: 0X%02X (%s)", character, name);
    }

    return 1;
  }

  char byte = character;
  return ptyWriteInputData(pty, &byte,1);
}

static unsigned char outputByteBuffer[0X40];
static unsigned char outputByteCount;

static void
logUnexpectedSequence (void) {
  if (logUnexpected) {
    logBytes(
      terminalLogLevel, "unexpected sequence",
      outputByteBuffer, outputByteCount
    );
  }
}

typedef enum {
  OPS_BASIC,
  OPS_ESCAPE,
  OPS_BRACKET,
  OPS_NUMBER,
  OPS_DIGIT,
  OPS_ACTION,
} OutputParserState;

static OutputParserState outputParserState;
static unsigned char outputParserQuestionMark;

static unsigned int outputParserNumber;
static unsigned int outputParserNumberArray[9];
static unsigned char outputParserNumberCount;

static void
addOutputParserNumber (unsigned int number) {
  if (outputParserNumberCount < ARRAY_COUNT(outputParserNumberArray)) {
    outputParserNumberArray[outputParserNumberCount++] = number;
  }
}

static unsigned int
getOutputActionCount (void) {
  if (outputParserNumberCount == 0) return 1;
  return outputParserNumberArray[0];
}

static void
logOutputAction (const char *name, const char *description) {
  if (logSequences) {
    char prefix[0X100];
    STR_BEGIN(prefix, sizeof(prefix));
    STR_PRINTF("action: %s", name);

    for (unsigned int i=0; i<outputParserNumberCount; i+=1) {
      STR_PRINTF(" %u", outputParserNumberArray[i]);
    }

    if (description && *description) STR_PRINTF(" (%s)", description);
    STR_END;
    logBytes(terminalLogLevel, "%s", outputByteBuffer, outputByteCount, prefix);
  }
}

typedef enum {
  OBP_DONE,
  OBP_CONTINUE,
  OBP_REPROCESS,
  OBP_UNEXPECTED,
} OutputByteParserResult;

typedef OutputByteParserResult OutputByteParser (unsigned char byte);

static OutputByteParserResult
parseOutputByte_BASIC (unsigned char byte) {
  outputParserQuestionMark = 0;
  outputParserNumberCount = 0;

  switch (byte) {
    case ASCII_ESC:
      outputParserState = OPS_ESCAPE;
      return OBP_CONTINUE;

    case ASCII_BEL:
      logOutputAction("bel", "audible alert");
      soundAlert();
      return OBP_DONE;

    case ASCII_BS:
      logOutputAction("cub1", "cursor left 1");
      ptyMoveCursorLeft(1);
      return OBP_DONE;

    case ASCII_HT:
      logOutputAction("ht", "tab forward");
      ptyTabForward();
      return OBP_DONE;

    case ASCII_LF:
      if (ptyAmWithinScrollRegion()) {
        logOutputAction("ind", "move down 1");
        ptyMoveDown1();
      } else {
        logOutputAction("cud1", "cursor down 1");
        ptyMoveCursorDown(1);
      }
      return OBP_DONE;

    case ASCII_CR:
      logOutputAction("cr", "carriage return");
      ptySetCursorColumn(0);
      return OBP_DONE;

    case ASCII_SO:
      logOutputAction("smacs", "alternate charset on");
      alternateCharsetMode = 1;
      return OBP_DONE;

    case ASCII_SI:
      logOutputAction("rmacs", "alternate charset off");
      alternateCharsetMode = 0;
      return OBP_DONE;

    default: {
      if (logOutput) {
        logMessage(terminalLogLevel, "output: 0X%02X", byte);
      }

      if (insertMode) ptyInsertCharacters(1);
      ptyAddCharacter(byte);
      return OBP_DONE;
    }
  }
}

static OutputByteParserResult
parseOutputByte_ESCAPE (unsigned char byte) {
  switch (byte) {
    case '[':
      outputParserState = OPS_BRACKET;
      return OBP_CONTINUE;

    case '=':
      logOutputAction("smkx", "keypad transmit on");
      keypadTransmitMode = 1;
      return OBP_DONE;

    case '>':
      logOutputAction("rmkx", "keypad transmit off");
      keypadTransmitMode = 0;
      return OBP_DONE;

    case 'E':
      logOutputAction("nel", "new line");
      ptySetCursorColumn(0);
      ptyMoveDown1();
      return OBP_DONE;

    case 'M':
      if (ptyAmWithinScrollRegion()) {
        logOutputAction("ri", "move up 1");
        ptyMoveUp1();
      } else {
        logOutputAction("cuu1", "cursor up 1");
        ptyMoveCursorUp(1);
      }
      return OBP_DONE;

    case 'c':
      logOutputAction("clear", "clear screen");
      ptySetCursorPosition(0, 0);
      ptyClearToEndOfDisplay();
      return OBP_DONE;

    case 'g':
      logOutputAction("flash", "visual alert");
      showAlert();
      return OBP_DONE;

    case '7':
      logOutputAction("sc", "save cursor position");
      ptySaveCursorPosition();
      return OBP_DONE;

    case '8':
      logOutputAction("rc", "restore cursor position");
      ptyRestoreCursorPosition();
      return OBP_DONE;
  }

  return OBP_UNEXPECTED;
}

static OutputByteParserResult
parseOutputByte_BRACKET (unsigned char byte) {
  outputParserState = OPS_NUMBER;
  if (outputParserQuestionMark) return OBP_REPROCESS;
  if (byte != '?') return OBP_REPROCESS;

  outputParserQuestionMark = 1;
  outputParserState = OPS_BRACKET;
  return OBP_CONTINUE;
}

static OutputByteParserResult
parseOutputByte_NUMBER (unsigned char byte) {
  if (iswdigit(byte)) {
    outputParserNumber = 0;
    outputParserState = OPS_DIGIT;
  } else {
    outputParserState = OPS_ACTION;
  }

  return OBP_REPROCESS;
}

static OutputByteParserResult
parseOutputByte_DIGIT (unsigned char byte) {
  if (iswdigit(byte)) {
    outputParserNumber *= 10;
    outputParserNumber += byte - '0';
    return OBP_CONTINUE;
  }

  addOutputParserNumber(outputParserNumber);
  outputParserNumber = 0;
  if (byte == ';') return OBP_CONTINUE;

  outputParserState = OPS_ACTION;
  return OBP_REPROCESS;
}

static OutputByteParserResult
performBracketAction_h (unsigned char byte) {
  if (outputParserNumberCount == 1) {
    switch (outputParserNumberArray[0]) {
      case 4:
        logOutputAction("smir", "insert on");
        insertMode = 1;
        return OBP_DONE;

      case 34:
        logOutputAction("cnorm", "cursor normal visibility");
        ptySetCursorVisibility(1);
        return OBP_DONE;
    }
  }

  return OBP_UNEXPECTED;
}

static OutputByteParserResult
performBracketAction_l (unsigned char byte) {
  if (outputParserNumberCount == 1) {
    switch (outputParserNumberArray[0]) {
      case 4:
        logOutputAction("rmir", "insert off");
        insertMode = 0;
        return OBP_DONE;

      case 34:
        logOutputAction("cvvis", "cursor very visile");
        ptySetCursorVisibility(2);
        return OBP_DONE;
    }
  }

  return OBP_UNEXPECTED;
}

static OutputByteParserResult
performBracketAction_m (unsigned char byte) {
  if (outputParserNumberCount == 0) addOutputParserNumber(0);

  for (unsigned int index=0; index<outputParserNumberCount; index+=1) {
    unsigned int number = outputParserNumberArray[index];

    switch (number / 10) {
      {
        const char *name;
        const char *description;
        void (*setColor) (int color);
        int color;

      case 3:
        name = "setaf";
        description = "foreground color";
        setColor = ptySetForegroundColor;
        goto doColor;

      case 4:
        name = "setab";
        description = "background color";
        setColor = ptySetBackgroundColor;
        goto doColor;

      doColor:
        color = number % 10;
        if (color == 8) return OBP_UNEXPECTED;
        if (color == 9) color = -1;

        logOutputAction(name, description);
        setColor(color);
        continue;
      }
    }

    switch (number) {
      case 0:
        logOutputAction("sgr0", "all attributes off");
        ptySetAttributes(0);
        continue;

      case 1:
        logOutputAction("bold", "bold on");
        ptyAddAttributes(A_BOLD);
        continue;

      case 2:
        logOutputAction("dim", "dim on");
        ptyAddAttributes(A_DIM);
        continue;

      case 3:
        logOutputAction("smso", "standout on");
        ptyAddAttributes(A_STANDOUT);
        continue;

      case 4:
        logOutputAction("smul", "underline on");
        ptyAddAttributes(A_UNDERLINE);
        continue;

      case 5:
        logOutputAction("blink", "blink on");
        ptyAddAttributes(A_BLINK);
        continue;

      case 7:
        logOutputAction("rev", "reverse video on");
        ptyAddAttributes(A_REVERSE);
        continue;

      case 22:
        logOutputAction("normal", "bold/dim off");
        ptyRemoveAttributes(A_BOLD | A_DIM);
        continue;

      case 23:
        logOutputAction("rmso", "standout off");
        ptyRemoveAttributes(A_STANDOUT);
        continue;

      case 24:
        logOutputAction("rmul", "underline off");
        ptyRemoveAttributes(A_UNDERLINE);
        continue;

      case 25:
        logOutputAction("unblink", "blink off");
        ptyRemoveAttributes(A_BLINK);
        continue;

      case 27:
        logOutputAction("unrev", "reverse video off");
        ptyRemoveAttributes(A_REVERSE);
        continue;
    }

    return OBP_UNEXPECTED;
  }

  return OBP_DONE;
}

static OutputByteParserResult
performBracketAction (unsigned char byte) {
  switch (byte) {
    case 'A':
      logOutputAction("cuu", "cursor up");
      ptyMoveCursorUp(getOutputActionCount());
      return OBP_DONE;

    case 'B':
      logOutputAction("cud", "cursor down");
      ptyMoveCursorDown(getOutputActionCount());
      return OBP_DONE;

    case 'C':
      logOutputAction("cuf", "cursor right");
      ptyMoveCursorRight(getOutputActionCount());
      return OBP_DONE;

    case 'D':
      logOutputAction("cub", "cursor left");
      ptyMoveCursorLeft(getOutputActionCount());
      return OBP_DONE;

    case 'G': {
      if (outputParserNumberCount != 1) return OBP_UNEXPECTED   ;
      unsigned int *column = &outputParserNumberArray[0];
      if (!(*column)--) return OBP_UNEXPECTED;

      logOutputAction("hpa", "set cursor column");
      ptySetCursorColumn(*column);
      return OBP_DONE;
    }

    case 'H': {
      if (outputParserNumberCount == 0) {
        addOutputParserNumber(1);
        addOutputParserNumber(1);
      } else if (outputParserNumberCount != 2) {
        return OBP_UNEXPECTED;
      }

      unsigned int *row = &outputParserNumberArray[0];
      unsigned int *column = &outputParserNumberArray[1];

      if (!(*row)--) return OBP_UNEXPECTED;
      if (!(*column)--) return OBP_UNEXPECTED;

      logOutputAction("cup", "set cursor position");
      ptySetCursorPosition(*row, *column);
      return OBP_DONE;
    }

    case 'J':
      if (outputParserNumberCount != 0) return OBP_UNEXPECTED;
      logOutputAction("ed", "clear to end of display");
      ptyClearToEndOfDisplay();
      return OBP_DONE;

    case 'K': {
      if (outputParserNumberCount == 0) addOutputParserNumber(0);
      if (outputParserNumberCount != 1) return OBP_UNEXPECTED;

      switch (outputParserNumberArray[0]) {
        case 0:
          logOutputAction("el", "clear to end of line");
          ptyClearToEndOfLine();
          return OBP_DONE;

        case 1:
          logOutputAction("el1", "clear to beginning of line");
          ptyClearToBeginningOfLine();
          return OBP_DONE;
      }

      break;
    }

    case 'L':
      logOutputAction("il", "insert lines");
      ptyInsertLines(getOutputActionCount());
      return OBP_DONE;

    case 'M':
      logOutputAction("dl", "delete lines");
      ptyDeleteLines(getOutputActionCount());
      return OBP_DONE;

    case 'P':
      logOutputAction("dch", "delete characters");
      ptyDeleteCharacters(getOutputActionCount());
      return OBP_DONE;

    case 'S':
      logOutputAction("indn", "scroll forward");
      ptyScrollUp(getOutputActionCount());
      return OBP_DONE;

    case 'T':
      logOutputAction("rin", "scroll backward");
      ptyScrollDown(getOutputActionCount());
      return OBP_DONE;

    case 'Z':
      logOutputAction("cbt", "tab backward");
      ptyTabBackward();
      return OBP_DONE;

    case 'd': {
      if (outputParserNumberCount != 1) return OBP_UNEXPECTED   ;
      unsigned int *row = &outputParserNumberArray[0];
      if (!(*row)--) return OBP_UNEXPECTED;

      logOutputAction("vpa", "set cursor row");
      ptySetCursorRow(*row);
      return OBP_DONE;
    }

    case 'h':
      return performBracketAction_h(byte);

    case 'l':
      return performBracketAction_l(byte);

    case 'm':
      return performBracketAction_m(byte);

    case 'r': {
      if (outputParserNumberCount != 2) return OBP_UNEXPECTED;

      unsigned int *top = &outputParserNumberArray[0];
      unsigned int *bottom = &outputParserNumberArray[1];

      if (!(*top)--) return OBP_UNEXPECTED;
      if (!(*bottom)--) return OBP_UNEXPECTED;

      logOutputAction("csr", "set scroll region");
      ptySetScrollRegion(*top, *bottom);
      return OBP_DONE;
    }

    case '@':
      logOutputAction("ic", "insert characters");
      ptyInsertCharacters(getOutputActionCount());
      return OBP_DONE;
  }

  return OBP_UNEXPECTED;
}

static OutputByteParserResult
performQuestionMarkAction_h (unsigned char byte) {
  if (outputParserNumberCount == 1) {
    switch (outputParserNumberArray[0]) {
      case 1:
        logOutputAction("smkx", "keypad transmit on");
        keypadTransmitMode = 1;
        return OBP_DONE;

      case 25:
        logOutputAction("cnorm", "cursor normal visibility");
        ptySetCursorVisibility(1);
        return OBP_DONE;

      case 1049:
        logOutputAction("smcup", "absolute cursor addressing on");
        absoluteCursorAddressingMode = 1;
        return OBP_DONE;

      case 2004:
        logOutputAction("smbp", "bracketed paste on");
        bracketedPasteMode = 1;
        return OBP_DONE;
    }
  }

  return OBP_UNEXPECTED;
}

static OutputByteParserResult
performQuestionMarkAction_l (unsigned char byte) {
  if (outputParserNumberCount == 1) {
    switch (outputParserNumberArray[0]) {
      case 1:
        logOutputAction("rmkx", "keypad transmit off");
        keypadTransmitMode = 0;
        return OBP_DONE;

      case 25:
        logOutputAction("civis", "cursor invisible");
        ptySetCursorVisibility(0);
        return OBP_DONE;

      case 1049:
        logOutputAction("rmcup", "absolute cursor addressing off");
        absoluteCursorAddressingMode = 0;
        return OBP_DONE;

      case 2004:
        logOutputAction("rmbp", "bracketed paste off");
        bracketedPasteMode = 0;
        return OBP_DONE;
    }
  }

  return OBP_UNEXPECTED;
}

static OutputByteParserResult
performQuestionMarkAction (unsigned char byte) {
  switch (byte) {
    case 'h':
      return performQuestionMarkAction_h(byte);

    case 'l':
      return performQuestionMarkAction_l(byte);
  }

  return OBP_UNEXPECTED;
}

static OutputByteParserResult
parseOutputByte_ACTION (unsigned char byte) {
  if (outputParserQuestionMark) {
    return performQuestionMarkAction(byte);
  } else {
    return performBracketAction(byte);
  }
}

typedef struct {
  OutputByteParser *parseOutputByte;
  const char *name;
} OutputParserStateEntry;

#define OPS(state) \
[OPS_##state] = { \
  .parseOutputByte = parseOutputByte_##state, \
  .name = #state, \
}

static const OutputParserStateEntry outputParserStateTable[] = {
  OPS(BASIC),
  OPS(ESCAPE),
  OPS(BRACKET),
  OPS(NUMBER),
  OPS(DIGIT),
  OPS(ACTION),
};
#undef OPS

static int
parseOutputByte (unsigned char byte) {
  if (outputParserState == OPS_BASIC) {
    outputByteCount = 0;
  }

  if (outputByteCount < ARRAY_COUNT(outputByteBuffer)) {
    outputByteBuffer[outputByteCount++] = byte;
  }

  while (1) {
    OutputByteParserResult result = outputParserStateTable[outputParserState].parseOutputByte(byte);

    switch (result) {
      case OBP_REPROCESS:
        continue;

      case OBP_UNEXPECTED:
        logUnexpectedSequence();
        /* fall through */

      case OBP_DONE:
        outputParserState = OPS_BASIC;
        return 1;

      case OBP_CONTINUE:
        return 0;
    }
  }
}

int
ptyProcessTerminalOutput (const unsigned char *bytes, size_t count) {
  int wantRefresh = 0;

  const unsigned char *byte = bytes;
  const unsigned char *end = byte + count;

  while (byte < end) {
    wantRefresh = parseOutputByte(*byte++);
  }

  if (wantRefresh) {
    ptyRefreshScreen();
  }

  return 1;
}