AquilaOS documentation
How it works, and its code
Loading...
Searching...
No Matches
term.c File Reference

The terminal emulator of the virtual terminals. More...

#include <core/system.h>
#include <core/string.h>
#include <mm/kvmem.h>
#include <dev/vt.h>

Go to the source code of this file.

Data Structures

struct  [struct].unicode_extra

Macros

#define DEFAULT_FG   7
#define DEFAULT_BG   0

Enumerations

enum  {
  S_GROUND , S_ESC , S_CHARSET , S_HASH ,
  S_PERCENT , S_CSI , S_OSC , S_PALETTE ,
  S_STRING , S_STRING_ESC
}

Functions

static uint8_t unicode_glyph (uint32_t u)
 Returns the code page 437 glyph that shows a character best, or '?
static uint8_t rgb_color (int r, int g, int b)
 Returns the closest colour of the palette's 6 x 6 x 6 cube or greys, for a 24-bit colour.
static struct vt_cell * cell (struct vt *vt, int row, int col)
 Returns a cell of the screen.
static struct vt_cell blank (struct vt *vt)
 Returns an erased cell: it takes the current colours, as the Linux console does ("bce").
static void erase (struct vt *vt, int row, int from, int to)
 Erases the cells of a row from column from up to to.
static void scroll_up (struct vt *vt, int top, int bottom, int n)
 Scrolls rows top to bottom up by n rows: n erased rows come at the bottom.
static void scroll_down (struct vt *vt, int top, int bottom, int n)
 Scrolls rows top to bottom down by n rows: n erased rows come at the top.
static void move_to (struct vt *vt, int row, int col)
 Moves the cursor, within the screen, or within the scrolling region in origin mode.
static void move_rows (struct vt *vt, int n)
 Moves the cursor up or down n rows.
static void linefeed (struct vt *vt)
 Moves the cursor down a row, scrolling at the region's bottom.
static void reverse_index (struct vt *vt)
 Moves the cursor up a row, scrolling at the region's top.
static int tab_stop (struct vt *vt, int col)
 Returns whether there is a tab stop at a column.
static void tab_set (struct vt *vt, int col, int set)
 Sets or clears the tab stop at a column.
static void tab (struct vt *vt, int n)
 Moves the cursor n tab stops forward, or back if n is negative.
static void put_glyph (struct vt *vt, uint8_t ch)
 Puts a glyph at the cursor, in the current colours and attributes, and moves the cursor on, wrapping at the end of the line.
static void put_char (struct vt *vt, uint32_t u)
 Puts a character at the cursor, through the character set in use.
static void reply (struct vt *vt, const char *s)
 Queues a reply to a request (DSR, DA), which vt.c gives the terminal as input.
static void save_cursor (struct vt *vt, struct vt_cursor *save)
 Saves the cursor: its position, colours, character sets and modes.
static void restore_cursor (struct vt *vt, struct vt_cursor *save)
 Restores a saved cursor, within the screen.
static void clear_screen (struct vt *vt)
 Erases the whole screen.
static void alt_screen (struct vt *vt, int on)
 Shows the alternate screen (xterm's), cleared, or the main one again.
static void soft_reset (struct vt *vt)
 Sets the modes and the pen back to the defaults, but not the screen (DECSTR, a soft reset).
void vt_term_reset (struct vt *vt)
 Resets a terminal (RIS, ESC c): the defaults, the tab stops every 8 columns, the main screen cleared, and the cursor at the top left.
int vt_term_init (struct vt *vt, int rows, int cols)
 Sets a terminal's screens up, the main one and the alternate one, both cleared, and resets it.
static int param (struct vt *vt, int i, int def)
 Returns a sequence's parameter i, or def if it is 0 or missing.
static void sgr (struct vt *vt)
 Sets the pen's colours and attributes (SGR, CSI ... m).
static void decset (struct vt *vt, int set)
 Sets or resets DEC private modes.
static void csi (struct vt *vt, char f)
 Runs a CSI sequence whose final character is f, with its parameters.
static void esc (struct vt *vt, char c)
 Runs an escape sequence, ESC c, or starts a longer one.
static void control (struct vt *vt, char c)
 Runs a control character.
static void utf8 (struct vt *vt, uint8_t c)
 Decodes UTF-8, a byte at a time, and puts each character.
static int hexdigit (char c)
 Returns a hex digit's value, or -1.
static void term_char (struct vt *vt, uint8_t c)
 Takes one byte of what programs write, in the state of the sequence it may be in.
void vt_term_write (struct vt *vt, const char *buf, size_t size)
 Takes what programs write to a terminal, and changes its screen.

Variables

static const uint8_t dec_graphics []
static const uint16_t cp437 [128]
static const uint16_t cp437_symbols [32]
static const struct  unicode_extra []

Detailed Description

The terminal emulator of the virtual terminals.

It does with what programs write what the Linux console does (TERM=linux): control characters, escape and CSI sequences, colours and attributes, scrolling regions, character sets. It also has the parts of xterm that programs commonly use: the alternate screen, and 256 colours. Characters are shown with the 256 glyphs of code page 437: UTF-8 is decoded, and each character is shown with the closest glyph. The emulator only changes the terminal's screen in memory; vt.c draws it.

Definition in file term.c.

Macro Definition Documentation

◆ DEFAULT_BG

#define DEFAULT_BG   0

Definition at line 36 of file term.c.

Referenced by sgr(), soft_reset(), term_char(), and vt_term_init().

◆ DEFAULT_FG

#define DEFAULT_FG   7

Definition at line 35 of file term.c.

Referenced by sgr(), soft_reset(), term_char(), and vt_term_init().

Enumeration Type Documentation

◆ anonymous enum

anonymous enum
Enumerator
S_GROUND 
S_ESC 
S_CHARSET 
S_HASH 
S_PERCENT 
S_CSI 
S_OSC 
S_PALETTE 
S_STRING 
S_STRING_ESC 

Definition at line 22 of file term.c.

Function Documentation

◆ alt_screen()

void alt_screen ( struct vt * vt,
int on )
static

Shows the alternate screen (xterm's), cleared, or the main one again.

Definition at line 354 of file term.c.

References vt::alt, alt_screen(), clear_screen(), vt::main, and vt::screen.

Referenced by alt_screen(), and decset().

◆ blank()

struct vt_cell blank ( struct vt * vt)
inlinestatic

Returns an erased cell: it takes the current colours, as the Linux console does ("bce").

Definition at line 130 of file term.c.

References vt_cell::bg, blank(), vt_cell::fg, and vt::pen.

Referenced by blank(), and erase().

◆ cell()

struct vt_cell * cell ( struct vt * vt,
int row,
int col )
inlinestatic

Returns a cell of the screen.

Definition at line 123 of file term.c.

References vt::cols, and vt::screen.

Referenced by csi(), erase(), put_glyph(), scroll_down(), scroll_up(), and term_char().

◆ clear_screen()

void clear_screen ( struct vt * vt)
static

Erases the whole screen.

Definition at line 347 of file term.c.

References clear_screen(), vt::cols, erase(), and vt::rows.

Referenced by alt_screen(), clear_screen(), csi(), vt_term_init(), and vt_term_reset().

◆ control()

void control ( struct vt * vt,
char c )
static

Runs a control character.

Definition at line 769 of file term.c.

References vt::col, vt::gl, linefeed(), vt::newline, S_ESC, S_GROUND, vt::state, tab(), and vt::wrap_pending.

Referenced by term_char().

◆ csi()

void csi ( struct vt * vt,
char f )
static

◆ decset()

void decset ( struct vt * vt,
int set )
static

Sets or resets DEC private modes.

These are the CSI ? n h and CSI ? n l sequences.

Definition at line 520 of file term.c.

References alt_screen(), vt::autowrap, vt::cursor_keys, vt::cursor_visible, decset(), move_to(), vt::nparams, vt::origin, vt::params, restore_cursor(), vt::reverse_screen, save_cursor(), vt::saved_alt, and vt::top.

Referenced by csi(), and decset().

◆ erase()

void erase ( struct vt * vt,
int row,
int from,
int to )
static

Erases the cells of a row from column from up to to.

Definition at line 136 of file term.c.

References blank(), cell(), and erase().

Referenced by clear_screen(), csi(), erase(), scroll_down(), and scroll_up().

◆ esc()

void esc ( struct vt * vt,
char c )
static

Runs an escape sequence, ESC c, or starts a longer one.

Definition at line 720 of file term.c.

References vt::col, esc(), vt::intermediate, vt::keypad, linefeed(), vt::nparams, vt::params, vt::private, reply(), restore_cursor(), reverse_index(), S_CHARSET, S_CSI, S_GROUND, S_HASH, S_OSC, S_PERCENT, S_STRING, save_cursor(), vt::state, tab_set(), and vt_term_reset().

Referenced by esc(), and term_char().

◆ hexdigit()

int hexdigit ( char c)
static

Returns a hex digit's value, or -1.

Definition at line 846 of file term.c.

References hexdigit().

Referenced by hexdigit(), and term_char().

◆ linefeed()

void linefeed ( struct vt * vt)
static

Moves the cursor down a row, scrolling at the region's bottom.

Definition at line 202 of file term.c.

References vt::bottom, linefeed(), vt::row, vt::rows, scroll_up(), vt::top, and vt::wrap_pending.

Referenced by control(), esc(), linefeed(), and put_glyph().

◆ move_rows()

void move_rows ( struct vt * vt,
int n )
static

Moves the cursor up or down n rows.

It stops at the scrolling region's margins, if it starts within them.

Definition at line 186 of file term.c.

References vt::bottom, move_rows(), vt::row, vt::rows, vt::top, and vt::wrap_pending.

Referenced by csi(), and move_rows().

◆ move_to()

void move_to ( struct vt * vt,
int row,
int col )
static

Moves the cursor, within the screen, or within the scrolling region in origin mode.

Definition at line 174 of file term.c.

References vt::bottom, vt::col, vt::cols, move_to(), vt::origin, vt::row, vt::rows, vt::top, and vt::wrap_pending.

Referenced by csi(), decset(), move_to(), and term_char().

◆ param()

int param ( struct vt * vt,
int i,
int def )
static

Returns a sequence's parameter i, or def if it is 0 or missing.

Definition at line 447 of file term.c.

References vt::nparams, param(), and vt::params.

Referenced by csi(), and param().

◆ put_char()

void put_char ( struct vt * vt,
uint32_t u )
static

Puts a character at the cursor, through the character set in use.

Definition at line 284 of file term.c.

References vt::charset, dec_graphics, vt::gl, put_char(), put_glyph(), and unicode_glyph().

Referenced by put_char(), and utf8().

◆ put_glyph()

void put_glyph ( struct vt * vt,
uint8_t ch )
static

Puts a glyph at the cursor, in the current colours and attributes, and moves the cursor on, wrapping at the end of the line.

Definition at line 261 of file term.c.

References vt::autowrap, cell(), vt_cell::ch, vt::col, vt::cols, vt::insert, vt::last, linefeed(), memmove(), vt::pen, put_glyph(), vt::row, and vt::wrap_pending.

Referenced by csi(), put_char(), put_glyph(), and utf8().

◆ reply()

void reply ( struct vt * vt,
const char * s )
static

Queues a reply to a request (DSR, DA), which vt.c gives the terminal as input.

A reply that doesn't fit is dropped.

Definition at line 307 of file term.c.

References memcpy(), reply(), vt::reply, and vt::reply_len.

Referenced by csi(), esc(), and reply().

◆ restore_cursor()

void restore_cursor ( struct vt * vt,
struct vt_cursor * save )
static

◆ reverse_index()

void reverse_index ( struct vt * vt)
static

Moves the cursor up a row, scrolling at the region's top.

Definition at line 213 of file term.c.

References vt::bottom, reverse_index(), vt::row, scroll_down(), vt::top, and vt::wrap_pending.

Referenced by esc(), and reverse_index().

◆ rgb_color()

uint8_t rgb_color ( int r,
int g,
int b )
static

Returns the closest colour of the palette's 6 x 6 x 6 cube or greys, for a 24-bit colour.

Definition at line 106 of file term.c.

References rgb_color().

Referenced by rgb_color(), and sgr().

◆ save_cursor()

void save_cursor ( struct vt * vt,
struct vt_cursor * save )
static

Saves the cursor: its position, colours, character sets and modes.

Definition at line 319 of file term.c.

References vt::autowrap, vt::charset, vt_cursor::charset, vt::col, vt_cursor::col, vt::gl, vt_cursor::gl, vt::origin, vt_cursor::origin, vt::pen, vt_cursor::pen, vt::row, vt_cursor::row, save_cursor(), and vt_cursor::wrap.

Referenced by csi(), decset(), esc(), save_cursor(), and soft_reset().

◆ scroll_down()

void scroll_down ( struct vt * vt,
int top,
int bottom,
int n )
static

Scrolls rows top to bottom down by n rows: n erased rows come at the top.

Definition at line 160 of file term.c.

References cell(), vt::cols, erase(), memmove(), and scroll_down().

Referenced by csi(), reverse_index(), and scroll_down().

◆ scroll_up()

void scroll_up ( struct vt * vt,
int top,
int bottom,
int n )
static

Scrolls rows top to bottom up by n rows: n erased rows come at the bottom.

Definition at line 146 of file term.c.

References cell(), vt::cols, erase(), memmove(), and scroll_up().

Referenced by csi(), linefeed(), and scroll_up().

◆ sgr()

void sgr ( struct vt * vt)
static

Sets the pen's colours and attributes (SGR, CSI ... m).

Definition at line 453 of file term.c.

References vt_cell::attr, vt_cell::bg, DEFAULT_BG, DEFAULT_FG, vt_cell::fg, vt::nparams, vt::params, vt::pen, rgb_color(), sgr(), VT_BLINK, VT_BOLD, VT_DIM, VT_INVISIBLE, VT_ITALIC, VT_REVERSE, and VT_UNDERLINE.

Referenced by csi(), and sgr().

◆ soft_reset()

void soft_reset ( struct vt * vt)
static

Sets the modes and the pen back to the defaults, but not the screen (DECSTR, a soft reset).

Definition at line 366 of file term.c.

References vt::autowrap, vt::bottom, vt_cell::ch, vt::charset, vt::col, vt::cursor_keys, vt::cursor_visible, DEFAULT_BG, DEFAULT_FG, vt::gl, vt::insert, vt::keypad, vt::newline, vt::origin, vt::pen, vt::row, vt::rows, save_cursor(), vt::saved_alt, soft_reset(), vt::top, and vt::wrap_pending.

Referenced by csi(), soft_reset(), and vt_term_reset().

◆ tab()

void tab ( struct vt * vt,
int n )
static

Moves the cursor n tab stops forward, or back if n is negative.

Definition at line 242 of file term.c.

References vt::col, vt::cols, tab(), tab_stop(), and vt::wrap_pending.

Referenced by control(), csi(), and tab().

◆ tab_set()

void tab_set ( struct vt * vt,
int col,
int set )
static

Sets or clears the tab stop at a column.

Definition at line 230 of file term.c.

References tab_set(), and vt::tabs.

Referenced by csi(), esc(), tab_set(), and vt_term_reset().

◆ tab_stop()

int tab_stop ( struct vt * vt,
int col )
static

Returns whether there is a tab stop at a column.

Definition at line 224 of file term.c.

References tab_stop(), and vt::tabs.

Referenced by tab(), and tab_stop().

◆ term_char()

void term_char ( struct vt * vt,
uint8_t c )
static

Takes one byte of what programs write, in the state of the sequence it may be in.

Definition at line 855 of file term.c.

References cell(), vt_cell::ch, vt::charset, vt::cols, control(), csi(), DEFAULT_BG, DEFAULT_FG, esc(), hexdigit(), vt::intermediate, move_to(), vt::nparams, vt::params, vt::private, vt::rows, S_CHARSET, S_CSI, S_ESC, S_GROUND, S_HASH, S_OSC, S_PALETTE, S_PERCENT, S_STRING, S_STRING_ESC, vt::state, term_char(), and utf8().

Referenced by term_char(), and vt_term_write().

◆ unicode_glyph()

uint8_t unicode_glyph ( uint32_t u)
static

Returns the code page 437 glyph that shows a character best, or '?

'.

Definition at line 84 of file term.c.

References cp437, cp437_symbols, unicode_extra, and unicode_glyph().

Referenced by put_char(), and unicode_glyph().

◆ utf8()

void utf8 ( struct vt * vt,
uint8_t c )
static

Decodes UTF-8, a byte at a time, and puts each character.

A broken sequence shows '?'.

Definition at line 809 of file term.c.

References put_char(), put_glyph(), utf8(), vt::utf8, and vt::utf8_left.

Referenced by term_char(), and utf8().

◆ vt_term_init()

int vt_term_init ( struct vt * vt,
int rows,
int cols )

Sets a terminal's screens up, the main one and the alternate one, both cleared, and resets it.

Parameters
vtthe terminal
rowsthe screen's rows
colsthe screen's columns
Returns
0, or -ENOMEM

Definition at line 420 of file term.c.

References vt::alt, vt_cell::ch, clear_screen(), vt::cols, DEFAULT_BG, DEFAULT_FG, kfree(), kmalloc(), vt::main, NULL, vt::pen, vt::rows, vt::screen, vt_term_init(), and vt_term_reset().

Referenced by vt_setup(), and vt_term_init().

◆ vt_term_reset()

void vt_term_reset ( struct vt * vt)

Resets a terminal (RIS, ESC c): the defaults, the tab stops every 8 columns, the main screen cleared, and the cursor at the top left.

Parameters
vtthe terminal

Definition at line 395 of file term.c.

References clear_screen(), vt::col, vt::last, vt::main, vt::reverse_screen, vt::row, S_GROUND, vt::screen, soft_reset(), vt::state, tab_set(), vt::utf8_left, and vt_term_reset().

Referenced by esc(), vt_term_init(), and vt_term_reset().

◆ vt_term_write()

void vt_term_write ( struct vt * vt,
const char * buf,
size_t size )

Takes what programs write to a terminal, and changes its screen.

Parameters
vtthe terminal
bufwhat was written
sizeits size, in bytes

Definition at line 957 of file term.c.

References term_char(), and vt_term_write().

Referenced by vt_kmsg(), vt_term_write(), and vt_tty_output().

Variable Documentation

◆ cp437

const uint16_t cp437[128]
static
Initial value:
= {
0x00C7, 0x00FC, 0x00E9, 0x00E2, 0x00E4, 0x00E0, 0x00E5, 0x00E7,
0x00EA, 0x00EB, 0x00E8, 0x00EF, 0x00EE, 0x00EC, 0x00C4, 0x00C5,
0x00C9, 0x00E6, 0x00C6, 0x00F4, 0x00F6, 0x00F2, 0x00FB, 0x00F9,
0x00FF, 0x00D6, 0x00DC, 0x00A2, 0x00A3, 0x00A5, 0x20A7, 0x0192,
0x00E1, 0x00ED, 0x00F3, 0x00FA, 0x00F1, 0x00D1, 0x00AA, 0x00BA,
0x00BF, 0x2310, 0x00AC, 0x00BD, 0x00BC, 0x00A1, 0x00AB, 0x00BB,
0x2591, 0x2592, 0x2593, 0x2502, 0x2524, 0x2561, 0x2562, 0x2556,
0x2555, 0x2563, 0x2551, 0x2557, 0x255D, 0x255C, 0x255B, 0x2510,
0x2514, 0x2534, 0x252C, 0x251C, 0x2500, 0x253C, 0x255E, 0x255F,
0x255A, 0x2554, 0x2569, 0x2566, 0x2560, 0x2550, 0x256C, 0x2567,
0x2568, 0x2564, 0x2565, 0x2559, 0x2558, 0x2552, 0x2553, 0x256B,
0x256A, 0x2518, 0x250C, 0x2588, 0x2584, 0x258C, 0x2590, 0x2580,
0x03B1, 0x00DF, 0x0393, 0x03C0, 0x03A3, 0x03C3, 0x00B5, 0x03C4,
0x03A6, 0x0398, 0x03A9, 0x03B4, 0x221E, 0x03C6, 0x03B5, 0x2229,
0x2261, 0x00B1, 0x2265, 0x2264, 0x2320, 0x2321, 0x00F7, 0x2248,
0x00B0, 0x2219, 0x00B7, 0x221A, 0x207F, 0x00B2, 0x25A0, 0x00A0,
}

Definition at line 47 of file term.c.

Referenced by unicode_glyph().

◆ cp437_symbols

const uint16_t cp437_symbols[32]
static
Initial value:
= {
0x2302, 0x263A, 0x263B, 0x2665, 0x2666, 0x2663, 0x2660, 0x2022,
0x25D8, 0x25CB, 0x25D9, 0x2642, 0x2640, 0x266A, 0x266B, 0x263C,
0x25BA, 0x25C4, 0x2195, 0x203C, 0x00B6, 0x00A7, 0x25AC, 0x21A8,
0x2191, 0x2193, 0x2192, 0x2190, 0x221F, 0x2194, 0x25B2, 0x25BC,
}

Definition at line 67 of file term.c.

Referenced by unicode_glyph().

◆ dec_graphics

const uint8_t dec_graphics[]
static
Initial value:
= {
' ', 0x04, 0xB1, 0xF9, 0xF9, 0xF9, 0xF9, 0xF8,
0xF1, 0xB0, 0x0F, 0xD9, 0xBF, 0xDA, 0xC0, 0xC5,
0xC4, 0xC4, 0xC4, 0xC4, 0xC4, 0xC3, 0xB4, 0xC1,
0xC2, 0xB3, 0xF3, 0xF2, 0xE3, '!', 0x9C, 0xFA,
}

Definition at line 39 of file term.c.

Referenced by put_char().

◆ unicode_extra

const struct [struct].unicode_extra unicode_extra[]
Initial value:
= {
{0x25C6, 0x04},
{0x256D, 0xDA}, {0x256E, 0xBF}, {0x256F, 0xD9}, {0x2570, 0xC0},
{0x2018, '\''}, {0x2019, '\''}, {0x201C, '"'}, {0x201D, '"'},
{0x2010, '-'}, {0x2013, '-'}, {0x2014, '-'}, {0x2212, '-'},
{0x2026, 0xFA}, {0x00B4, '\''}, {0x00D7, 'x'},
}

Referenced by unicode_glyph().