A C library that is a terminal without a display. It runs a program on a pseudoterminal, reads what the program writes, and keeps the screen that program expects. No display, no toolkit — only libc.
  • C 95.1%
  • Python 2.3%
  • Shell 2.2%
  • Makefile 0.4%
Find a file
liquibyte c8aa7dec77
Hosho 1.5: a new package
- src/hosho.h: HOSHO_VERSION 1.5; doxygen/Doxyfile: PROJECT_NUMBER 1.5.
2026-11-17 19:10:29 -06:00
artwork
demo
doc
shell
src
themes
tools
LICENSE
logo.png
Makefile
README.md

Hosho

A C library that is a terminal without a display. It runs a program on a pseudoterminal, reads what the program writes, and keeps the screen that program expects. No display, no toolkit — only libc.

Hosho (奉書) is the Japanese paper that official orders were written on: a terminal is where commands are given. It is one of the washi names of the tools around Kami (紙, paper).

A host draws the screen and sends the keys. The host can be a widget of any toolkit, so a terminal can sit inside another program.

What it does

Layer Header What it gives
Width hosho_width.h How many cells a character fills: 0, 1 or 2. Tables made from the Unicode data files.
Parser hosho_parser.h The bytes a program writes, split into characters and control functions: C0 controls, escape sequences, control sequences with parameters and sub-parameters, OSC and DCS strings. UTF-8 input.
Screen hosho_screen.h The grid of cells, the alternate buffer and the scrollback. Wide characters, combining marks, hyperlinks, pictures, a "changed" mark on each line. A new width wraps the text again.
Sixel hosho_sixel.h The data of a sixel picture as pixels.
Inflate hosho_inflate.h Data a program compressed with zlib (pictures of the Kitty graphics protocol), made whole.
Bidi hosho_bidi.h The order in which a line with Hebrew or Arabic is shown, by the Unicode Bidirectional Algorithm.
Terminal hosho_terminal.h What each control function does: cursor, margins on four sides, tab stops, erase, insert, delete, rectangle functions, protected cells, colors and attributes, character sets, modes, reports, title, palette, clipboard, shell marks, the status line, printing, user-defined keys, the DEC locator, the window functions, VT52 mode, pictures (sixel, iTerm2 inline images, the Kitty graphics protocol), notifications and progress, the Kitty keyboard protocol's flags. It answers as a VT420 with color.
Text hosho_text.h The text of a range of cells; search through the scrollback; the link under a cell (a hyperlink, an address, a file name with line and column); the lines of one command with its output.
Input hosho_input.h A key, a mouse event, a focus change or a paste as the bytes the program reads, by the modes the program set.
Theme hosho_theme.h Terminal themes: each color of the display, the 16 palette colors, a style for each text attribute, font and cursor settings. Read from a file, completed by fallbacks, written with every key.
Pseudoterminal hosho_pseudoterminal.h A program (or the user's shell) started on a pseudoterminal: read, write, resize, exit status.

hosho.h includes all of it.

Where the behavior comes from

Hosho is written from public standards and manuals, not from the source of another terminal:

  • ECMA-48, Control Functions for Coded Character Sets
  • Paul Flo Williams, A parser for DEC's ANSI-compatible video terminals
  • The DEC VT100, VT102, VT220, VT320, VT420 and VT510 manuals
  • XTerm Control Sequences (ctlseqs)
  • The terminfo entry xterm-256color, which is the TERM a program gets
  • The Unicode Character Database (East Asian Width, general categories, emoji, bidirectional classes and mirror characters) and UAX #9, the Unicode Bidirectional Algorithm
  • The Kitty keyboard, graphics and notification protocol documents, and iTerm2's inline images document
  • RFC 1950 and RFC 1951 (zlib and deflate), for compressed pictures
  • The terminal descriptions xterm-sun, xterm-hp, xterm-sco, rxvt and linux, for the keys of those keyboards
  • X11's rgb.txt, for color names

reference/SOURCES.md says where each one is. The documents themselves are in reference/ on the author's machine and are not part of the repository.

Using it

#include "hosho.h"

/* The terminal answers a report request: send the answer to the program */
static void host_reply(void *user, const char *bytes, size_t length) {
	hosho_pseudoterminal_write(user, bytes, length);
}
static const HoshoTerminalHost host = { .reply = host_reply };

HoshoPseudoterminal pseudoterminal;
hosho_pseudoterminal_spawn(&pseudoterminal, NULL, NULL, 24, 80);       /* the user's shell */
HoshoTerminal *terminal = hosho_terminal_create(24, 80, 10000, &host, &pseudoterminal);

/* In the event loop, when pseudoterminal.descriptor can be read */
char bytes[65536];
ssize_t count = hosho_pseudoterminal_read(&pseudoterminal, bytes, sizeof(bytes));
if (count > 0) hosho_terminal_feed(terminal, bytes, (size_t)count);

/* Draw the lines that changed */
for (int row = 0; row < terminal->screen.rows; row++) {
	const HoshoLine *line = hosho_screen_line(&terminal->screen, row);
	if (!line->dirty) continue;
	for (int column = 0; column < terminal->screen.columns; column++) {
		const HoshoCell *cell = &line->cells[column];
		uint32_t foreground = hosho_terminal_color(terminal, cell->foreground, 0);
		uint32_t background = hosho_terminal_color(terminal, cell->background, 1);
		/* hosho_screen_cell_text(), cell->attributes, cell->underline ... */
	}
}
hosho_screen_clear_dirty(&terminal->screen);

/* What the user types goes to the program */
char keys[HOSHO_INPUT_SIZE];
size_t length = hosho_input_key(terminal, HOSHO_KEY_UP, HOSHO_MODIFIER_CONTROL, keys);
hosho_pseudoterminal_write(&pseudoterminal, keys, length);
length = hosho_input_character(terminal, 'c', HOSHO_MODIFIER_CONTROL, keys);   /* Control+C */
hosho_pseudoterminal_write(&pseudoterminal, keys, length);

/* The window changed size */
hosho_terminal_resize(terminal, rows, columns);
hosho_pseudoterminal_resize(&pseudoterminal, rows, columns, 0, 0);

A demo

make demo builds demo/hosho_demo: a terminal window. It is an X11 program built on Xtk, with the terminal widget (demo/hosho_widget.c) in its window. It runs your shell, or the program you name.

make demo                          # needs Xtk built in ../xtk (XTK_DIR=... for another place)
demo/hosho_demo                    # your shell
demo/hosho_demo -t Washi -r 40 -c 120 -f "DejaVu Sans Mono" -s 16
demo/hosho_demo -t my_theme.conf   # a theme file; it is read again when it changes
demo/hosho_demo vim src/hosho.h    # one program; the window closes when it ends
demo/hosho_demo -d ls --color      # -d prints the last screen as text at the end
In the window What it does
Keys, function keys, Shift/Alt/Control Go to the program
Mouse Goes to a program that asks for it (vim, htop, mc); hold Shift to select text then
Drag, double click, triple click Select characters, a word, a line; the selection is the PRIMARY selection
Middle button, Shift+Insert Paste the PRIMARY selection
Control+Shift+C, Control+Shift+V Copy to and paste from the clipboard
Wheel, Shift+Page Up, Shift+Page Down, scrollbar Look at the scrollback
Control+Shift+Up, Control+Shift+Down Go to the prompt above or below (with shell integration)
Click on a prompt's mark in the margin Select the output of that command
Double click on a prompt's mark Hide or show the output of that command
Control and the pointer on a link The link is underlined: a hyperlink a program made, an address, or a file name such as src/main.c:120:8
Control and a click on a link Open it (the demo gives it to xdg-open; a file name is found in the shell's directory)
Control+Shift+F Search: type the text, Enter for the match before, Shift+Enter for the one after, Escape to close
Control+Shift+O A letter on each link in view; type a letter to open that link
Control+Shift+Space Select with the keyboard: arrows or h j k l, w b, 0 $, g G, Page Up and Down; v starts a selection by characters, V by lines; y or Enter copies; Escape closes
Control+Plus, Control+Minus, Control+0 Font size: larger, smaller, as the theme says
Control+Break Sends the answerback message (answerback in the theme), as the key of a VT100 does
A paste of more than one line Asks first when the program does not mark pastes (a shell with bracketed paste does, so it does not ask); paste-confirm = 0 in the theme turns the question off

Themes

A terminal theme is a file in the form of Unryu's editor themes. A theme can have three lines: each key it leaves out takes a fallback.

[theme]
name = My Theme

[terminal]
background = #14110F
text = #E6DFD3
cursor = #C8553D
; the background of each line with a prompt
prompt-line = #1D1916

[palette]
red = #E06C75

[attributes]
; bold text with no color of its own: white, in the regular face
bold = #FFFFFF -
; italic text: its own color, drawn italic
italic = - - italic

[rules]
; the words error, failed and fatal in red and bold, in upper or lower case
error = #FF5555 - bold /error|failed|fatal/i
; "warning:" on a dark yellow background
warning = #FFD700 #3A3000 /warning:/

[terminal-widget]
font-family = DejaVu Sans Mono
font-size = 15
cursor-shape = bar
cursor-blink = 1
line-spacing = 2
Section What it holds
[terminal] The view: background, text, selection, cursor, link, the margin of the shell's marks, a prompt's line, the output of a failed command, the matches of a search. Each background key has a key for the text on it.
[palette] The 16 colors programs name: black to white, bright-black to bright-white.
[attributes] A style, foreground background [bold] [italic], for text a program marks bold, faint, italic, underline or blink.
[rules] A style for text in a program's output that a pattern finds: name = foreground background [bold] [italic] /pattern/. The pattern is an extended regular expression; an i after the last / makes it ignore case. A rule's color is for text with no color of its own.
[terminal-widget] font-family, font-size, cursor-shape, cursor-blink, padding, gutter-width, line-spacing, scrollback-lines, bold-bright, clipboard-read, clipboard-write, paste-confirm, answerback, keyboard, window-ops, print-command, bell, minimum-contrast, notify-command-seconds, log-file, background-image, background-opacity, ligatures.

themes/ has the four built-in themes (Hosho Dark, Hosho Light, Sumi, Washi) with every key and a line of words above each one. demo/hosho_demo -t NAME -W writes such a file for any theme.

An editor theme of Unryu is a terminal theme too. A file with no [terminal] section gives the terminal its [editor] colors (background, text, selection, caret), so the terminal looks as the editor does:

demo/hosho_demo -t ../unryu/editor_themes/Gruvbox.conf

The demo reads a theme file again when the file changes: save the file in an editor and the terminal shows the change.

The host's items in the menu

The widget's menu (the right mouse button) has the terminal's own items. A host adds its items after them with hosho_widget_add_menu_item(): a label, the keys the menu shows beside it, and a function. The host's items come below a separator, and hosho_widget_set_menu_item_enabled() makes one grey. Hanshi adds New Tab, Close Tab and Preferences this way; the widget's menu did not change for it.

Shell integration

A shell can tell the terminal where each prompt starts and how each command ended (OSC 133). Hosho then:

  • draws a mark beside each prompt, in the theme's color for a command that ended well, failed, or still runs;
  • can draw each prompt's line on its own background (prompt-line);
  • goes from prompt to prompt with Control+Shift+Up and Control+Shift+Down;
  • can draw the output of a failed command on its own background (output-failure);
  • selects the output of one command with a click on its mark, and hides it with a double click;
  • knows the shell's directory (OSC 7), so a file name in a compiler's output can be opened.

Hosho also knows what the shell does: it waits at a prompt, text waits at the prompt that was not sent, or a command runs (hosho_terminal_shell_state(), and the host's shell_state function when it changes). A host can show this while its terminal is out of view. The host tells the terminal what the user types (hosho_terminal_typed()); the widget does this by itself.

shell/ has a file for each shell. hosho_pseudoterminal_shell_start() gives the arguments and the variables that make a shell read its file, and the user changes no file (hosho_widget_start_shell() in the widget). Each file reads the user's own files first.

The shell How it reads the file What it tells
bash --rcfile shell/hosho_rcfile.bash All
zsh ZDOTDIR=shell/zsh; .zshenv there gives ZDOTDIR back All
fish --init-command "source shell/hosho.fish"; fish 4 sends most marks by itself All
ksh93 ENV=shell/hosho.sh, which reads hosho.ksh93 All
mksh, OpenBSD's ksh, dash, the sh of FreeBSD and NetBSD ENV=shell/hosho.sh Not that a command starts: see below
tcsh Not by itself: source /path/to/hosho/shell/hosho.tcsh at the end of ~/.tcshrc All but the end of the prompt

A shell with no way to run something before a command says so in each prompt (OSC 133 ; A ; hosho=infer). Hosho then takes Return after text at the prompt as the start of a command. A shell that does not say where its prompt ends (no OSC 133 ; B) has that place at the cursor when the user types the first character.

Checked with bash 5.2, zsh 5.9, fish 4.0, ksh93u+m 1.0, mksh R59, tcsh 6.24 and dash 0.5.12. The parts for OpenBSD's ksh and the sh of FreeBSD and NetBSD were written from the source of those shells and were not run there.

To use a file in another terminal, read it from the shell's own file, for example source /path/to/hosho/shell/hosho.bash in ~/.bashrc. To try it and change no file:

demo/hosho_demo bash --rcfile shell/hosho_rcfile.bash

The keyboard

By default the keyboard is a PC's, as in xterm: Home and End send CSI H and CSI F, and the numeric keypad's digits are digits. Programs expect this.

keyboard = vt220 in the theme's [terminal-widget] section makes it a VT220's: Home and End are Find and Select (CSI 1 ~, CSI 4 ~), the keypad sends its own codes in application mode (ESC O p to ESC O y for the digits), and the keypad's plus is the VT220's comma. A program can also switch this itself (mode 1061, as in xterm). Use it for a program that needs a real VT.

The keyboard setting can also be sun, hp, sco, rxvt or linux: the function keys, Home, End, Insert, Delete, Page Up and Page Down then send the codes of that keyboard or terminal (with no modifier held). A program can choose the first three itself (modes 1051 to 1053). The widget then starts programs with the TERM of that keyboard (xterm-sun, xterm-hp, xterm-sco, rxvt-256color or rxvt, linux), so that they read its keys, when the system has that terminal description; if not, TERM stays xterm-256color.

F13 to F20, on a keyboard that has them, send what xterm sends: F1 to F8 with Shift (CSI 1 ; 2 P for F13), or with the VT220 keyboard CSI 25 ~ to CSI 34 ~.

A program can ask for the Kitty keyboard protocol (CSI > flags u). Then Escape and keys with Alt or Control say what they are, and, as the program asks: keys that repeat and keys that go up, the key with Shift and the key of the US layout, each key as an escape code, and the text a key types.

The menu and the mouse

The right mouse button opens a menu: Copy, Copy as HTML, Paste, Select All, Find, Open Link, Copy Link, Copy Command Output, Hide or Show Command Output, the text's size, Clear Scrollback, Reset Terminal. When a program asked for the mouse, hold Shift for the menu and for a selection.

Mouse What it does
Drag Selects text. Two clicks select words, three select lines.
Control and drag Selects a rectangle of cells. (Alt and drag is the window manager's: it moves the window.)
Control and click Opens the link under the pointer, when the button goes up with no drag.
Click in the margin Selects a command's output; two clicks hide or show it.
Middle button Pastes the selection.
Wheel Moves the view through the scrollback.

In the search bar (Control+Shift+F), Alt+C makes upper and lower case different, and Alt+R makes the text a pattern (an extended regular expression).

Pictures

A program can show pictures in three ways: sixel, iTerm2's inline images (OSC 1337 ; File=...) and the Kitty graphics protocol (APC G). Of the Kitty protocol Hosho has: pixels as RGB, RGBA or PNG, sent in the command itself (in parts too, and compressed with zlib); a picture kept by its number and shown again; a size in cells; delete. It does not read a file or shared memory that a program names.

What a program sends can be hostile, and taking a picture apart is where a bug lets it in. So the host can do that, in a jail: the terminal hands picture files (PNG and the others) to the host's image_decode, sixel data to its sixel_decode and compressed data to its inflate. A host with no sixel_decode or inflate leaves those to the terminal's own readers. The widget does all three in Xtk's jailed worker (xtk_worker.h): a process of the program's own that can read no file, write nothing, reach no network and start or signal nothing, with every answer checked before it is shown. A program that uses the widget adds the widget's jobs and starts the worker at the top of main():

hosho_widget_add_worker_jobs();
if (xtk_worker_is_worker(argc, argv)) return xtk_worker_main();

Raw pixels (the Kitty protocol's f=24 and f=32 without compression) need no taking apart: the terminal copies them itself, after checking their size. Without the jail (Linux 5.13 or later with Landlock, on x86-64 or ARM64) the widget shows only those; every picture file, sixel picture and compressed picture is left out.

Other languages

A line with Hebrew or Arabic is shown in its reading order for a person (the Unicode Bidirectional Algorithm); a program can switch this off (CSI 8 l). With mode 2501 a line that starts with such text goes from the right edge. The widget draws each run of text through Xtk's shaper (xtk_shape): Arabic letters get their joined forms, and a font's ligatures are drawn (-> as an arrow in a programming font). ligatures = 0 in the theme switches the ligatures off. Each letter stays in its cell, so joined letters of a font that is not monospaced can have small gaps.

Emoji are drawn in color when a color emoji font is installed. What a reader sees as one character (a grapheme cluster: a family, a flag, an emoji with a skin tone, a letter with its marks) is kept in one cell, or two for a wide one, and is one picture. U+FE0F makes a narrow emoji two cells wide. A program that counts each character's width for itself, as wcwidth() does, can switch this off with CSI ? 2027 l.

What a program can tell the user

What How What Hosho does
A notification OSC 9, OSC 777;notify, OSC 99 The host gets the title and the text. The demo runs notify-send.
A long command ended The shell's marks (OSC 133) A notification, when the command ran notify-command-seconds or more and the window does not have the keyboard.
Progress of a job OSC 9;4;state;percent A thin bar at the top of the view; the host gets the numbers too.
The bell BEL The theme's bell: sound, visual (a frame for a moment), both or none. The demo marks its window for the window manager.
A pointer shape OSC 22 The pointer over the terminal changes.

log-file in the theme names a file that gets the text programs show, from the start. ~/ at the start of it, and of background-image, is the home folder.

The clipboard

A program can ask to read or set the clipboard (OSC 52). Hosho refuses, unless the theme has clipboard-write = 1 or clipboard-read = 1 in [terminal-widget]. Permit the read only when you trust each program you run: a program that reads the clipboard sees what you copied elsewhere.

The window and the printer

A program can ask to move, size, raise or iconify the window, to change the number of lines and columns (CSI 8 t, DECSLPP, DECSCPP, DECSNLS), and to read the window's title, icon name and position (the window functions of CSI t). Hosho refuses, unless the theme has window-ops = 1 in [terminal-widget]. A host then gets the requests through the widget's window callbacks; the demo does them. Without it a program can still ask the size of the text area and of a cell (CSI 14 t, CSI 16 t, CSI 18 t, CSI 19 t) and save and restore the title (CSI 22 t, CSI 23 t): these change nothing and tell nothing of the user's desktop.

A program can print (the media copy functions: the screen, a line, each line as the cursor leaves it, or the program's bytes as they are). The theme's print-command names the shell command that gets the printed text on its standard input, for example print-command = lpr. With no command the terminal tells programs that it has no printer, and printed text is dropped.

The status line

A program can ask for a status line (DECSSDT): a row below the screen. Type 1 is the terminal's own line with the cursor's position; type 2 is a line the program writes (DECSASD chooses if text goes to the status line or to the page). The demo makes its window one row higher for it.

Building

make            # libhosho.a (KEEP_OBJS=1 keeps the object files)
make demo       # demo/hosho_demo (see above); the only part that needs Xtk and X11
make tables     # src/hosho_width_tables.h again, from reference/unicode
make install    # headers to $(PREFIX)/include/hosho, the library to $(PREFIX)/lib, themes and shell
                # integration to $(PREFIX)/share/hosho (PREFIX=/usr)
make clean

A program can also compile the files it uses from src/ straight in.

Built with -Wall -Wextra -Wpedantic -Wconversion -Wshadow -Werror under -std=gnu17. It needs a POSIX system with pseudoterminals (Linux, the BSDs).

The Debian package (hosho_<version>_amd64.deb, on the project's release page) installs the same with PREFIX=/usr.

Documentation

Every public type, field, constant and function in src/*.h has a Doxygen comment. The project's wiki has guides (the overview, embedding the widget, writing a host, the host functions, themes, input, shell integration, pictures and the control functions), made from doc/*.html, and one page for each header, made from those comments. HOSHO_VERSION in hosho.h is the library's version.

License

Artistic License 2.0.