This project provides a portable, incremental parser for VT100/ANSI escape sequences. It is suitable for use in terminal emulators, logging tools, or any application that needs to interpret terminal control codes.
- Incremental, event-driven parsing of VT100/ANSI escape sequences
- No dynamic memory allocation required
- Supports CSI, OSC, DCS, PM, APC, and standard control/printable characters
- Robust handling of buffer/parameter overflows: string buffers (OSC/DCS/PM/APC) are truncated if too long, and CSI parameter arrays are limited to 16 entries (extra parameters are ignored)
- Simple callback-based API
- Thorough unit test coverage
All files are in the project root unless otherwise noted:
vt100.c,vt100.hLibrary source and headervt100_test.cUnit tests for the parserCMakeLists.txtCMake build systemDoxyfile.inDoxygen configuration templateLICENSE.mdLicenseREADME.mdThis file
You can use the provided build.sh script to perform a full clean build, install the library and header, run all tests, and show the installed files:
./build.shThis script will:
- Remove any previous build directory for a clean build
- Configure CMake with tests and documentation enabled
- Build the library, test binary, and documentation
- Install the static library and header to
build/install(see below) - Run the unit tests (output:
All tests passed!if successful) - Show the installed files using
tree -lh(orfindiftreeis not available)
You can also build manually with CMake. Optional documentation and test targets can be enabled using CMake options:
# Basic build (library only):
cmake -S . -B build
cmake --build build
# Build with tests and documentation:
cmake -S . -B build -DBUILD_TESTS=ON -DBUILD_DOCS=ON
cmake --build build
# Build only tests or docs:
cmake -S . -B build -DBUILD_TESTS=ON
cmake --build build
cmake -S . -B build -DBUILD_DOCS=ON
cmake --build build --target docTest binary: build/bin/vt100_test (not installed)
Static library: build/lib/libvt100.a
Documentation: build/docs/reference/html/index.html
When using build.sh or running cmake --install build, the following files are installed to build/install:
build/install/include/vt100.h(header)build/install/lib/libvt100.a(static library)
The test binary is not installed.
build/bin/vt100_testInclude vt100.h in your project and link against libvt100.a. Initialize a
parser, provide a callback, and feed input data incrementally:
#include "vt100.h"
void my_callback(vt100_parser_t *parser, const vt100_event_t *event, void *user)
{
// Handle events here
}
vt100_parser_t parser;
vt100_parser_init(&parser, my_callback, NULL);
vt100_parser_feed(&parser, data, len);- CSI parameters: Only the first 16 parameters are stored; extra parameters are ignored.
- OSC/DCS/PM/APC strings: If a string exceeds the buffer size (128 bytes
including null terminator), it is truncated and the event's
is_overflow/is_overflowedfield is set.
To handle XTerm mouse and clipboard events, check the event type in your callback:
void my_callback(vt100_parser_t *parser, const vt100_event_t *event, void *user)
{
switch (event->type) {
case VT100_EVENT_XTERM_MOUSE:
// Access event->data.xterm_mouse fields
break;
case VT100_EVENT_XTERM_CLIPBOARD:
// Access event->data.xterm_clipboard.data
break;
// ... handle other event types ...
}
}See the unit tests in vt100_test.c for more usage examples.
To generate API documentation (HTML) with CMake:
cmake --build build --target doc
# Open build/docs/reference/html/index.html in your browser- Standard C99 compiler (tested with gcc and clang)
- CMake 3.10+ (recommended)
make(optional, for legacy Makefile)clang-format(optional, for formatting)
This project is released under the MIT License.