xref: /freebsd/sys/teken/libteken/teken.3 (revision 068b48d674ae8e2c007102173b93de9a9cb95ba9)
1ecc16c8dSEd Schouten.\" Copyright (c) 2011 Ed Schouten <ed@FreeBSD.org>
2ecc16c8dSEd Schouten.\" All rights reserved.
3ecc16c8dSEd Schouten.\"
4ecc16c8dSEd Schouten.\" Redistribution and use in source and binary forms, with or without
5ecc16c8dSEd Schouten.\" modification, are permitted provided that the following conditions
6ecc16c8dSEd Schouten.\" are met:
7ecc16c8dSEd Schouten.\" 1. Redistributions of source code must retain the above copyright
8ecc16c8dSEd Schouten.\"    notice, this list of conditions and the following disclaimer.
9ecc16c8dSEd Schouten.\" 2. Redistributions in binary form must reproduce the above copyright
10ecc16c8dSEd Schouten.\"    notice, this list of conditions and the following disclaimer in the
11ecc16c8dSEd Schouten.\"    documentation and/or other materials provided with the distribution.
12ecc16c8dSEd Schouten.\"
13ecc16c8dSEd Schouten.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
14ecc16c8dSEd Schouten.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15ecc16c8dSEd Schouten.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16ecc16c8dSEd Schouten.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
17ecc16c8dSEd Schouten.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18ecc16c8dSEd Schouten.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19ecc16c8dSEd Schouten.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20ecc16c8dSEd Schouten.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21ecc16c8dSEd Schouten.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22ecc16c8dSEd Schouten.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
23ecc16c8dSEd Schouten.\" SUCH DAMAGE.
24ecc16c8dSEd Schouten.\"
25ecc16c8dSEd Schouten.\" $FreeBSD$
26ecc16c8dSEd Schouten.\"
27ecc16c8dSEd Schouten.Dd May 9, 2011
28ecc16c8dSEd Schouten.Dt TEKEN 3
29ecc16c8dSEd Schouten.Os
30ecc16c8dSEd Schouten.Sh NAME
31ecc16c8dSEd Schouten.Nm teken
32ecc16c8dSEd Schouten.Nd xterm-like terminal emulation interface
33ecc16c8dSEd Schouten.Sh LIBRARY
34ecc16c8dSEd Schouten.Lb libteken
35ecc16c8dSEd Schouten.Sh SYNOPSIS
36ecc16c8dSEd Schouten.In teken.h
37ecc16c8dSEd Schouten.Ft void
38ecc16c8dSEd Schouten.Fn teken_init "teken_t *t" "const teken_funcs_t *funcs" "void *thunk"
39ecc16c8dSEd Schouten.Ft void
40ecc16c8dSEd Schouten.Fn teken_input "teken_t *t" "const void *buf" "size_t nbytes"
41ecc16c8dSEd Schouten.Ft const teken_pos_t *
42ecc16c8dSEd Schouten.Fn teken_get_winsize "teken_t *t"
43ecc16c8dSEd Schouten.Ft void
44ecc16c8dSEd Schouten.Fn teken_set_winsize "teken_t *t" "const teken_pos_t *size"
45ecc16c8dSEd Schouten.Ft const teken_pos_t *
46ecc16c8dSEd Schouten.Fn teken_get_cursor "teken_t *t"
47ecc16c8dSEd Schouten.Ft void
48ecc16c8dSEd Schouten.Fn teken_set_cursor "teken_t *t" "const teken_pos_t *pos"
49ecc16c8dSEd Schouten.Ft const teken_attr_t *
50ecc16c8dSEd Schouten.Fn teken_get_curattr "teken_t *t"
51ecc16c8dSEd Schouten.Ft void
52ecc16c8dSEd Schouten.Fn teken_set_curattr "teken_t *t" "const teken_attr_t *attr"
53ecc16c8dSEd Schouten.Ft const teken_attr_t *
54ecc16c8dSEd Schouten.Fn teken_get_defattr "teken_t *t"
55ecc16c8dSEd Schouten.Ft void
56ecc16c8dSEd Schouten.Fn teken_set_defattr "teken_t *t" "const teken_attr_t *attr"
57ecc16c8dSEd Schouten.Ft const char *
58ecc16c8dSEd Schouten.Fn teken_get_sequence "teken_t *t" "unsigned int id"
59ecc16c8dSEd Schouten.Ft teken_color_t
60ecc16c8dSEd Schouten.Fn teken_256to8 "teken_color_t color"
61ecc16c8dSEd Schouten.Ft void
62ecc16c8dSEd Schouten.Fn teken_get_defattr_cons25 "teken_t *t" "int *fg" "int *bg"
63ecc16c8dSEd Schouten.Ft void
64ecc16c8dSEd Schouten.Fn teken_set_8bit "teken_t *t"
65ecc16c8dSEd Schouten.Ft void
66ecc16c8dSEd Schouten.Fn teken_set_cons25 "teken_t *t"
67ecc16c8dSEd Schouten.Sh DESCRIPTION
68ecc16c8dSEd SchoutenThe
69ecc16c8dSEd Schouten.Nm
70ecc16c8dSEd Schoutenlibrary implements the input parser of a 256-color xterm-like terminal.
71ecc16c8dSEd SchoutenIt converts a stream of UTF-8 encoded characters into a series of
72ecc16c8dSEd Schoutenprimitive drawing instructions that can be used by a console driver or
73ecc16c8dSEd Schoutenterminal emulator to render a terminal application.
74ecc16c8dSEd Schouten.Pp
75ecc16c8dSEd SchoutenThe
76ecc16c8dSEd Schouten.Fn teken_init
77ecc16c8dSEd Schoutenfunction is used to initialize terminal state object
78ecc16c8dSEd Schouten.Fa t ,
79ecc16c8dSEd Schoutenhaving type
80ecc16c8dSEd Schouten.Vt teken_t .
81ecc16c8dSEd SchoutenThe supplied
82ecc16c8dSEd Schouten.Vt teken_funcs_t
83ecc16c8dSEd Schoutenstructure
84ecc16c8dSEd Schouten.Fa funcs
85ecc16c8dSEd Schoutencontains a set of callback functions, which are called when supplying
86ecc16c8dSEd Schoutendata to
87ecc16c8dSEd Schouten.Fn teken_input .
88ecc16c8dSEd SchoutenThe
89ecc16c8dSEd Schouten.Fa thunk
90ecc16c8dSEd Schoutenargument stores an arbitrary pointer, which is passed to each invocation
91ecc16c8dSEd Schoutenof the callback functions.
92ecc16c8dSEd Schouten.Pp
93ecc16c8dSEd SchoutenThe
94ecc16c8dSEd Schouten.Vt teken_funcs_t
95ecc16c8dSEd Schoutenstructure stores the following callbacks:
96ecc16c8dSEd Schouten.Bd -literal -offset indent
97ecc16c8dSEd Schoutentypedef struct {
98ecc16c8dSEd Schouten	tf_bell_t     *tf_bell;     /* Audible/visible bell. */
99ecc16c8dSEd Schouten	tf_cursor_t   *tf_cursor;   /* Move cursor to x/y. */
100ecc16c8dSEd Schouten	tf_putchar_t  *tf_putchar;  /* Put Unicode character at x/y. */
101ecc16c8dSEd Schouten	tf_fill_t     *tf_fill;     /* Fill rectangle with character. */
102ecc16c8dSEd Schouten	tf_copy_t     *tf_copy;     /* Copy rectangle to new location. */
103ecc16c8dSEd Schouten	tf_param_t    *tf_param;    /* Miscellaneous options. */
104ecc16c8dSEd Schouten	tf_respond_t  *tf_respond;  /* Send response string to user. */
105ecc16c8dSEd Schouten} teken_funcs_t;
106ecc16c8dSEd Schouten.Ed
107ecc16c8dSEd Schouten.Pp
108ecc16c8dSEd SchoutenAll callbacks must be provided, though unimplemented callbacks may some
109ecc16c8dSEd Schoutentimes be sufficient.
110ecc16c8dSEd SchoutenThe actual types of these callbacks can be found in
111ecc16c8dSEd Schouten.In teken.h .
112ecc16c8dSEd Schouten.Pp
113ecc16c8dSEd SchoutenBy default,
114ecc16c8dSEd Schouten.Fn teken_init
115ecc16c8dSEd Schouteninitializes the
116ecc16c8dSEd Schouten.Vt teken_t
117ecc16c8dSEd Schoutenstructure to emulate a terminal having 24 rows and 80 columns.
118ecc16c8dSEd SchoutenThe
119ecc16c8dSEd Schouten.Fn teken_get_winsize
120ecc16c8dSEd Schoutenand
121ecc16c8dSEd Schouten.Fn teken_set_winsize
122ecc16c8dSEd Schoutenfunctions can be used to obtain and modify the dimensions of the
123ecc16c8dSEd Schoutenterminal.
124ecc16c8dSEd Schouten.Pp
125ecc16c8dSEd SchoutenEven though the cursor position is normally controlled by input of data
126ecc16c8dSEd Schoutenthrough
127ecc16c8dSEd Schouten.Fn teken_input
128ecc16c8dSEd Schoutenand returned by the
129ecc16c8dSEd Schouten.Fn tf_cursor
130ecc16c8dSEd Schoutencallback, it can be obtained and modified manually using the
131ecc16c8dSEd Schouten.Fn teken_get_cursor
132ecc16c8dSEd Schoutenand
133ecc16c8dSEd Schouten.Fn teken_set_cursor
134ecc16c8dSEd Schoutenfunctions.
135ecc16c8dSEd SchoutenThe same holds for
136ecc16c8dSEd Schouten.Fn teken_get_curattr
137ecc16c8dSEd Schoutenand
138ecc16c8dSEd Schouten.Fn teken_set_curattr ,
139ecc16c8dSEd Schoutenwhich can be used to change the currently selected font attributes and
140ecc16c8dSEd Schoutenforeground and background color.
141ecc16c8dSEd Schouten.Pp
142ecc16c8dSEd SchoutenBy default,
143ecc16c8dSEd Schouten.Nm
144ecc16c8dSEd Schoutenemulates a white-on-black terminal, which means the default foreground
145ecc16c8dSEd Schoutencolor is white, while the background color is black.
146ecc16c8dSEd SchoutenThese defaults can be modified using
147ecc16c8dSEd Schouten.Fn teken_get_defattr
148ecc16c8dSEd Schoutenand
149ecc16c8dSEd Schouten.Fn teken_set_defattr .
150ecc16c8dSEd Schouten.Pp
151ecc16c8dSEd SchoutenThe
152ecc16c8dSEd Schouten.Fn teken_get_sequence
153ecc16c8dSEd Schoutenfunction is a utility function that can be used to obtain escape
154ecc16c8dSEd Schoutensequences of special keyboard keys, generated by user input.
155ecc16c8dSEd SchoutenThe
156ecc16c8dSEd Schouten.Fa id
157ecc16c8dSEd Schoutenparameter must be one of the
158ecc16c8dSEd Schouten.Dv TKEY_*
159ecc16c8dSEd Schoutenparameters listed in
160ecc16c8dSEd Schouten.In teken.h .
161ecc16c8dSEd Schouten.Sh LEGACY FEATURES
162ecc16c8dSEd SchoutenThis library also provides a set of functions that shouldn't be used in
163ecc16c8dSEd Schoutenany modern applications.
164ecc16c8dSEd Schouten.Pp
165ecc16c8dSEd SchoutenThe
166ecc16c8dSEd Schouten.Fn teken_256to8
167ecc16c8dSEd Schoutenfunction converts a color code to one of the 8 primary colors, allowing
168ecc16c8dSEd Schoutenthe terminal to be rendered on graphics hardware that only supports 8 or
169ecc16c8dSEd Schouten16 colors (e.g. VGA).
170ecc16c8dSEd Schouten.Pp
171ecc16c8dSEd SchoutenThe
172ecc16c8dSEd Schouten.Fn teken_get_defattr_cons25
173ecc16c8dSEd Schoutenfunction obtains the default terminal attributes as a pair of foreground
174ecc16c8dSEd Schoutenand background colors, using ANSI color numbering.
175ecc16c8dSEd Schouten.Pp
176ecc16c8dSEd SchoutenThe
177ecc16c8dSEd Schouten.Fn teken_set_8bit
178ecc16c8dSEd Schoutenfunction disables UTF-8 processing and switches to 8-bit character mode,
179ecc16c8dSEd Schoutenwhich can be used to support character sets like CP437 and ISO-8859-1.
180ecc16c8dSEd Schouten.Pp
181ecc16c8dSEd SchoutenThe
182ecc16c8dSEd Schouten.Fn teken_set_cons25
183ecc16c8dSEd Schoutenfunction switches terminal emulation to
184ecc16c8dSEd Schouten.Dv cons25 ,
185ecc16c8dSEd Schoutenwhich is used by versions of
186ecc16c8dSEd Schouten.Fx
187ecc16c8dSEd Schoutenprior to 9.0.
188ecc16c8dSEd Schouten.Sh SEE ALSO
189ecc16c8dSEd Schouten.Xr ncurses 3 ,
190ecc16c8dSEd Schouten.Xr termcap 3 ,
191*068b48d6SJoel Dahl.Xr syscons 4
192ecc16c8dSEd Schouten.Sh HISTORY
193ecc16c8dSEd SchoutenThe
194ecc16c8dSEd Schouten.Nm
195ecc16c8dSEd Schoutenlibrary appeared in
196ecc16c8dSEd Schouten.Fx 8.0 ,
197ecc16c8dSEd Schoutenthough it was only available and used inside the kernel.
198ecc16c8dSEd SchoutenIn
199ecc16c8dSEd Schouten.Fx 9.0 ,
200ecc16c8dSEd Schoutenthe
201ecc16c8dSEd Schouten.Nm
202ecc16c8dSEd Schoutenlibrary appeared in userspace.
203ecc16c8dSEd Schouten.Sh AUTHORS
204ecc16c8dSEd Schouten.An Ed Schouten Aq ed@FreeBSD.org
205135bce2aSUlrich Spörlein.Sh SECURITY CONSIDERATIONS
206135bce2aSUlrich SpörleinThe
207135bce2aSUlrich Spörlein.Fn tf_respond
208135bce2aSUlrich Spörleincallback is used to respond to device status requests commands generated
209135bce2aSUlrich Spörleinby an application.
210135bce2aSUlrich SpörleinIn the past, there have been various security issues, where a malicious
211135bce2aSUlrich Spörleinapplication sends a device status request before termination, causing
212135bce2aSUlrich Spörleinthe generated response to be interpreted by applications such as
213135bce2aSUlrich Spörlein.Xr sh 1 .
214135bce2aSUlrich Spörlein.Pp
215135bce2aSUlrich Spörlein.Nm
216135bce2aSUlrich Spörleinonly implements a small subset of responses which are unlikely to cause
217135bce2aSUlrich Spörleinany harm.
218135bce2aSUlrich SpörleinStill, it is advised to leave
219135bce2aSUlrich Spörlein.Fn tf_respond
220135bce2aSUlrich Spörleinunimplemented.
221