hosho_parser.h
Escape Sequence Parser.
Copyright (c) 2016-2026 Michael Grey (liquibyte) liquibyte@gmail.com SPDX-License-Identifier: Artistic-2.0 Distributed under the Artistic License 2.0; see LICENSE for terms.
Splits the bytes a program writes to a terminal into characters to show and control functions to do. It knows the form of each control function (ECMA-48), not its meaning: the meaning is the handler's work.
Features:
- The state machine of Paul Flo Williams' "A parser for DEC's ANSI-compatible video terminals" (vt100.net/emu/dec_ansi_parser)
- UTF-8 input: a byte sequence that is not correct UTF-8 gives U+FFFD
- C0 controls, escape sequences, control sequences (CSI) with parameters and sub-parameters, operating system commands (OSC) and device control strings (DCS)
- Input in pieces of any size: a sequence can start in one call and end in a later one
Internal design notes:
- The UTF-8 decoder is in front of the state machine, so the machine reads code points. A decoded C1 control (U+0080..U+009F) is read as ESC and the matching byte 0x40..0x5F, as ECMA-48 defines it
- Differences from the Williams diagram, each from xterm's ctlseqs: ':' separates sub-parameters in a control sequence (SGR 38:2r:g:b); BEL also ends an operating system command; CAN, SUB and an ESC that does not start ESC \ cancel a string, they do not send it
- A device control string is collected and given whole, not byte by byte
- APC strings are collected as OSC strings are; SOS and PM strings are read to their end and dropped
#include "hosho_parser.h"
Types: HoshoSequence, HoshoParserHandler, HoshoParser
Constants: HOSHO_PARSER_PARAMETER_LIMIT, HOSHO_PARSER_INTERMEDIATE_LIMIT, HOSHO_PARSER_STRING_LIMIT, HOSHO_PARAMETER_DEFAULT, HOSHO_PARAMETER_MAXIMUM
Functions: hosho_parser_init, hosho_parser_feed, hosho_parser_reset, hosho_parser_free, hosho_sequence_parameter, hosho_sequence_is_subparameter
Types
HoshoSequence
One escape sequence, control sequence or device control string header.
| Field | Type | Description |
|---|---|---|
final_byte |
uint8_t |
the byte that ends the sequence and names the function |
prefix |
uint8_t |
the private marker '<', '=', '>' or '?' after CSI, or 0 |
intermediates[HOSHO_PARSER_INTERMEDIATE_LIMIT] |
uint8_t |
the intermediate bytes (0x20..0x2F), in order |
intermediate_count |
uint8_t |
how many of intermediates are set |
parameter_count |
uint8_t |
how many of parameters are set |
parameters[HOSHO_PARSER_PARAMETER_LIMIT] |
int32_t |
each parameter, or HOSHO_PARAMETER_DEFAULT |
subparameter_mask |
uint32_t |
bit n is set when parameter n came after ':', so it belongs to the one in front of it |
HoshoParserHandler
What the parser found. Each function can be NULL.
The parser calls these from hosho_parser_feed(), in the order of the input. The pointers it gives are good only during the call.
| Field | Type | Description |
|---|---|---|
print |
void (*)(void *user, uint32_t codepoint) |
A character to show. |
execute |
void (*)(void *user, uint8_t control) |
A C0 control character (0x00..0x1F) to do now, or DEL (0x7F), which most handlers ignore. |
escape |
void (*)(void *user, const HoshoSequence *sequence) |
An escape sequence: ESC, intermediates, final byte. A C1 control comes as its final byte 0x40..0x5F. |
control |
void (*)(void *user, const HoshoSequence *sequence) |
A control sequence: CSI, parameters, intermediates, final byte. |
operating_system |
void (*)(void *user, const char *text, size_t length, int bell_terminated) |
An operating system command: its text (UTF-8, NUL after it), and whether BEL ended it. |
device_control |
void (*)(void *user, const HoshoSequence *sequence, const char *data, size_t length) |
A device control string: its header and its data (NUL after it). |
application_command |
void (*)(void *user, const char *text, size_t length) |
An application program command (APC): its text, with a NUL after it. |
HoshoParser
The parser's state. Set it up with hosho_parser_init(); do not change the fields.
| Field | Type | Description |
|---|---|---|
handler |
const HoshoParserHandler * |
where the results go |
user |
void * |
given back to each handler function |
sequence |
HoshoSequence |
the sequence that is collected now |
string |
char * |
the OSC or DCS string that is collected now |
string_length |
size_t |
its bytes |
string_capacity |
size_t |
the bytes allocated for it |
string_overflow |
uint8_t |
the string went past HOSHO_PARSER_STRING_LIMIT, or memory ran out: it is dropped when it ends |
utf8_codepoint |
uint32_t |
the bits of a UTF-8 sequence read until now |
utf8_remaining |
uint8_t |
continuation bytes the UTF-8 sequence still needs |
utf8_lowest |
uint8_t |
the smallest value the next continuation byte can have |
utf8_highest |
uint8_t |
the largest value the next continuation byte can have |
state |
uint8_t |
the state of the machine |
parameter_open |
uint8_t |
a digit of the last parameter was read |
parameter_trailing |
uint8_t |
the last byte was a separator, so one more parameter follows |
subparameter_next |
uint8_t |
the next parameter comes after ':' |
collect_overflow |
uint8_t |
more intermediates or markers than fit: drop the sequence |
string_pending |
uint8_t |
ESC came in a string; '\' next ends the string, other bytes cancel it. |
Constants
HOSHO_PARSER_PARAMETER_LIMIT
#define HOSHO_PARSER_PARAMETER_LIMIT 32
The most parameters (sub-parameters included) a sequence keeps; more are dropped.
HOSHO_PARSER_INTERMEDIATE_LIMIT
#define HOSHO_PARSER_INTERMEDIATE_LIMIT 2
The most intermediate bytes a sequence keeps; a sequence with more is not given to the handler.
HOSHO_PARSER_STRING_LIMIT
#define HOSHO_PARSER_STRING_LIMIT (64u << 20)
The most bytes of an OSC, DCS or APC string (64 MiB, as for a picture's data); a longer string is dropped whole.
HOSHO_PARAMETER_DEFAULT
#define HOSHO_PARAMETER_DEFAULT (-1)
The value of a parameter that the sequence left out.
HOSHO_PARAMETER_MAXIMUM
#define HOSHO_PARAMETER_MAXIMUM 65535
The largest value a parameter can have; a larger number becomes this.
Functions
hosho_parser_init
void hosho_parser_init(
HoshoParser *parser,
const HoshoParserHandler *handler,
void *user);
Set a parser up.
| Parameter | Description |
|---|---|
parser |
The parser |
handler |
The functions that get the results; must stay valid while the parser is used |
user |
A pointer given back to each handler function |
hosho_parser_feed
void hosho_parser_feed(HoshoParser *parser, const uint8_t *bytes, size_t length);
Give input to the parser.
| Parameter | Description |
|---|---|
parser |
The parser |
bytes |
The bytes a program wrote to the terminal |
length |
How many bytes |
hosho_parser_reset
void hosho_parser_reset(HoshoParser *parser);
Go back to the first state and drop a sequence that is not complete.
| Parameter | Description |
|---|---|
parser |
The parser |
hosho_parser_free
void hosho_parser_free(HoshoParser *parser);
Release the parser's memory.
| Parameter | Description |
|---|---|
parser |
The parser |
hosho_sequence_parameter
int32_t hosho_sequence_parameter(
const HoshoSequence *sequence,
size_t index,
int32_t fallback);
Read one parameter of a sequence.
| Parameter | Description |
|---|---|
sequence |
The sequence |
index |
Which parameter, from 0 |
fallback |
The value to give when the sequence left the parameter out |
Returns: The parameter's value, or fallback
hosho_sequence_is_subparameter
int hosho_sequence_is_subparameter(const HoshoSequence *sequence, size_t index);
Say whether a parameter is a sub-parameter (came after ':')
| Parameter | Description |
|---|---|
sequence |
The sequence |
index |
Which parameter, from 0 |
Returns: 1 when it is a sub-parameter, 0 when it is not or does not exist
Hosho API
The whole library
From bytes to a screen
For the host that shows it
The program