1 hosho_pseudoterminal
liquibyte edited this page 2026-11-19 16:23:30 -06:00
This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

hosho_pseudoterminal.h

A Program on a Pseudoterminal.

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.

Starts a program (most often the user's shell) with a pseudoterminal as its terminal. The host reads what the program writes and gives it to hosho_terminal_feed(), and writes the keys the user types.

Features:

  • Start of a program, or of the user's shell, in a directory
  • TERM (xterm-256color) and COLORTERM (truecolor) set for the program, unless the caller gives its own; the host's COLUMNS, LINES and TERMCAP left out
  • The line discipline knows the input is UTF-8 (IUTF8), so an erase removes a whole character
  • Read and write that do not block, for an event loop with poll()
  • Tell the program the size of the screen
  • Collect the program's exit status

Internal design notes:

  • POSIX calls only: posix_openpt(), grantpt(), unlockpt(), fork()
  • The library sends no signal. hosho_pseudoterminal_close() closes the terminal, and the kernel then sends SIGHUP to the program, as when a terminal window closes
  • HoshoPseudoterminal.descriptor is for the host's poll() or select()
#include "hosho_pseudoterminal.h"

Types: HoshoPseudoterminal, HoshoShellStart

Constants: HOSHO_PSEUDOTERMINAL_TERM, HOSHO_SHELL_START_ARGUMENTS, HOSHO_SHELL_START_VARIABLES, HOSHO_SHELL_START_TEXT

Functions: hosho_pseudoterminal_spawn, hosho_pseudoterminal_spawn_environment, hosho_pseudoterminal_user_shell, hosho_pseudoterminal_shell_start, hosho_pseudoterminal_read, hosho_pseudoterminal_write, hosho_pseudoterminal_resize, hosho_pseudoterminal_wait, hosho_pseudoterminal_close

Types

HoshoPseudoterminal

One program on a pseudoterminal.

Field Type Description
descriptor int the host's end of the pseudoterminal, not blocking; -1 when closed
child pid_t the program's process id; 0 when there is none or its exit status was collected

HoshoShellStart

How to start a shell so that it reads Hosho's shell integration (the files of shell/) and the user changes no file.

Field Type Description
arguments[HOSHO_SHELL_START_ARGUMENTS+1] const char * the shell and its arguments, with NULL after the last
variables[HOSHO_SHELL_START_VARIABLES+1] const char * "NAME=value" entries for hosho_pseudoterminal_spawn_environment(), with NULL after the last
text[4][HOSHO_SHELL_START_TEXT] char the room the pointers above use

Constants

HOSHO_PSEUDOTERMINAL_TERM

#define HOSHO_PSEUDOTERMINAL_TERM "xterm-256color"

The value of TERM the program gets, unless the caller's variables have a TERM: the terminfo entry that describes what Hosho does.

HOSHO_SHELL_START_ARGUMENTS

#define HOSHO_SHELL_START_ARGUMENTS 3

The most arguments a shell's start has, the shell itself with them.

HOSHO_SHELL_START_VARIABLES

#define HOSHO_SHELL_START_VARIABLES 2

The most variables a shell's start has.

HOSHO_SHELL_START_TEXT

#define HOSHO_SHELL_START_TEXT 4352

The bytes of room for one text of a shell's start.

Functions

hosho_pseudoterminal_spawn

int hosho_pseudoterminal_spawn(
    HoshoPseudoterminal *pseudoterminal,
    const char *const *arguments,
    const char *directory,
    int rows,
    int columns);

Start a program on a new pseudoterminal.

Parameter Description
pseudoterminal Gets the descriptor and the process id
arguments The program and its arguments, with NULL after the last; NULL to start the user's shell
directory The directory the program starts in; NULL for the current one, which the program also gets when it cannot change to this one
rows Lines on the screen
columns Cells in a line

Returns: 0, or -1 with errno set when the pseudoterminal or the process could not be made

hosho_pseudoterminal_spawn_environment

int hosho_pseudoterminal_spawn_environment(
    HoshoPseudoterminal *pseudoterminal,
    const char *const *arguments,
    const char *const *variables,
    const char *directory,
    int rows,
    int columns);

Start a program on a new pseudoterminal, with variables of its own in its environment.

Parameter Description
pseudoterminal Gets the descriptor and the process id
arguments The program and its arguments, with NULL after the last; NULL to start the user's shell
variables "NAME=value" entries with NULL after the last, or NULL: the program gets them in place of the host's variables of these names, and a TERM or COLORTERM here in place of Hosho's
directory The directory the program starts in; NULL for the current one, which the program also gets when it cannot change to this one
rows Lines on the screen
columns Cells in a line

Returns: 0, or -1 with errno set when the pseudoterminal or the process could not be made

hosho_pseudoterminal_user_shell

const char *hosho_pseudoterminal_user_shell(void);

The user's shell: $SHELL, then the password database, then /bin/sh; each of the first two only when it is a full path.

Returns: The shell's path; it stays valid until the environment or the password database is read again

hosho_pseudoterminal_shell_start

int hosho_pseudoterminal_shell_start(
    const char *shell,
    const char *folder,
    HoshoShellStart *start);

Find how a shell starts with the shell integration.

bash gets –rcfile, zsh gets ZDOTDIR, fish gets –init-command; ksh93, mksh, OpenBSD's ksh, dash and sh get ENV (HOSHO_USER_ENV keeps the user's own). Each of these files reads the user's own files first. tcsh and csh read only the files in the home folder: the user adds a line to ~/.tcshrc (shell/hosho.tcsh says which).

Parameter Description
shell The shell's path; NULL, or a name that is not a full path, for the user's shell
folder The folder with the integration files (shell/); NULL or "" for no integration, and a folder too long for HoshoShellStart gives none either
start Gets the arguments and the variables; with no integration, the shell alone

Returns: 1 when the start reads the integration, 0 when it is the shell alone

hosho_pseudoterminal_read

ssize_t hosho_pseudoterminal_read(
    HoshoPseudoterminal *pseudoterminal,
    void *bytes,
    size_t size);

Read what the program wrote.

Parameter Description
pseudoterminal The pseudoterminal
bytes Where the bytes go
size The room in bytes

Returns: The number of bytes read; 0 when there is nothing to read now; -1 when the program closed the terminal (it ended) or on an error

hosho_pseudoterminal_write

ssize_t hosho_pseudoterminal_write(
    HoshoPseudoterminal *pseudoterminal,
    const void *bytes,
    size_t length);

Write to the program, as if typed.

Parameter Description
pseudoterminal The pseudoterminal
bytes The bytes
length How many bytes

Returns: The number of bytes written, which can be fewer than length (0 when the program reads nothing now: write the rest later); -1 on an error

hosho_pseudoterminal_resize

int hosho_pseudoterminal_resize(
    HoshoPseudoterminal *pseudoterminal,
    int rows,
    int columns,
    int pixel_width,
    int pixel_height);

Tell the program the size of the screen; it gets SIGWINCH.

Parameter Description
pseudoterminal The pseudoterminal
rows Lines on the screen
columns Cells in a line
pixel_width The width of the text area in pixels, or 0 when not known
pixel_height The height of the text area in pixels, or 0 when not known

Returns: 0, or -1 with errno set

hosho_pseudoterminal_wait

int hosho_pseudoterminal_wait(
    HoshoPseudoterminal *pseudoterminal,
    int *status,
    int block);

Collect the program's exit status.

Parameter Description
pseudoterminal The pseudoterminal
status Gets the status as waitpid() gives it; can be NULL
block 1 to wait until the program ends, 0 to only look

Returns: 1 when the program ended (now or before), 0 when it still runs, -1 on an error

hosho_pseudoterminal_close

void hosho_pseudoterminal_close(HoshoPseudoterminal *pseudoterminal);

Close the host's end of the pseudoterminal; the program gets SIGHUP.

The process stays until hosho_pseudoterminal_wait() collects its exit status.

Parameter Description
pseudoterminal The pseudoterminal