POSIX terminal interface: Difference between revisions

Content deleted Content added
AnomieBOT (talk | contribs)
m Dating maintenance tags: {{Cn}}
Cewbot (talk | contribs)
m Fixing broken anchor: #C0 .28ASCII and derivatives.29→most alike anchor C0 and C1 control codes#ASCII
 
(41 intermediate revisions by 25 users not shown)
Line 1:
{{Short description|Computer terminal application programming interface}}
The '''POSIX terminal interface''' is the generalized abstraction, comprising both an [[Applicationapplication Programmingprogramming Interfaceinterface]] for programs, and a set of behavioural expectations for users of a [[terminal (telecommunication)|terminal]], as defined by the [[POSIX]] standard and the [[Single Unix Specification]]. It is a historical development from the terminal interfaces of BSD version 4 and [[Seventh Edition Unix]].
 
== General underlying concepts ==
Line 5 ⟶ 6:
=== Hardware ===
A multiplicity of I/O devices are regarded as "terminals" in Unix systems.{{sfn|Christian|1988|p=11}}{{sfn|Bourne|1983|p=6}} These include:
* [[serial device]]s connected by a [[serial port]] such as [[printer (computing)|printer]]s/[[teleprinter]]s, [[teletypewriter]]s, [[modem]]s supporting ''remote'' [[terminal (telecommunication)|terminal]]s via [[dial-up access]], and directly- connected ''local'' terminals{{sfn|Christian|1988|p=11}}{{sfn|Coffin|1991|p=820}}{{sfn|Coffin|1991|p=23–24}}{{sfn|Leffler|McKusick|Karels|Quarterman|1989|p=259}}
* [[display adapter]] and [[Keyboard (computing)|keyboard]] hardware directly incorporated into the system unit, taken together to form a local "console", which may be presented to users and to programs as a single CRT terminal or as multiple [[virtual terminal]]s{{sfn|Christian|1988|p=11}}
* software [[terminal emulator]]s, such as the [[xterm]], [[Konsole]], [[GNOME Terminal]], and [[Terminal (OS XmacOS)|Terminal]] programs, and network servers such as the [[rlogin]] daemon and the [[Secure Shell|SSH]] daemon, which make use of [[pseudoterminal]]s
 
=== Terminal intelligence and capabilities ===
 
==== Intelligence: terminals are dumb, not intelligent ====
Unlike its mainframe and minicomputer{{cncitation needed|date=April 2014}} contemporaries, the original Unix system was developed solely for ''dumb'' terminals, and that remains the case today.{{sfn|Coffin|1991|p=24}} A terminal is a character-oriented device, comprising streams of characters received from and sent to the device.{{sfn|Coffin|1991|p=24}}{{sfn|Leffler|McKusick|Karels|Quarterman|1989|p=37–38}} Although the streams of characters are structured, incorporating [[control character]]s, [[escape code]]s, and special characters, the I/O protocol is not structured as would be the I/O protocol of ''smart'', or ''[[intelligent terminal|intelligent]]'', terminals. There are no field format specifications. There's no block transmission of entire screens (input forms) of input data.
{{main|terminal intelligence}}
Unlike its mainframe and minicomputer{{cn|date=April 2014}} contemporaries, the original Unix system was developed solely for ''dumb'' terminals, and that remains the case today.{{sfn|Coffin|1991|p=24}} A terminal is a character-oriented device, comprising streams of characters received from and sent to the device.{{sfn|Coffin|1991|p=24}}{{sfn|Leffler|McKusick|Karels|Quarterman|1989|p=37–38}} Although the streams of characters are structured, incorporating [[control character]]s, [[escape code]]s, and special characters, the I/O protocol is not structured as would be the I/O protocol of ''smart'', or ''intelligent'', terminals. There are no field format specifications. There's no block transmission of entire screens (input forms) of input data.
 
By contrast mainframes and minicomputers in closed architectures commonly use [[Block-oriented terminal]]s.
Line 19 ⟶ 18:
==== Capabilities: terminfo, termcap, curses, et al. ====
{{main|terminal capabilities}}
The "capabilities" of a terminal comprise various dumb terminal features that are above and beyond what is available from a pure teletypewriter, which programs can make use of. They (mainly) comprise escape codes that can be sent to or received from the terminal. The escape codes sent to the terminal perform various functions that a CRT terminal (or software terminal emulator) is capable of that a teletypewriter is not, such as moving the terminal's cursor to positions on the screen, clearing and scrolling all or parts of the screen, turning on and off attached printer devices, programmable function keys, changing display colours and attributes (such as [[reverse video]]), and setting display title strings. The escape codes received from the terminal signify things such as [[function key]], [[arrow key]], and other special [[Typing|keystroke]]s ([[home key]], [[end key]], [[help key]], [[PgUp key]], [[PgDn key]], [[insert key]], [[delete key]], and so forth).{{sfn|Afzal|2008|p=419}}{{sfn|Frisch|2002|p=770}}
 
These capabilities are encoded in databases that are configured by a system administrator and accessed from programs via the [[terminfo]] library (which supersedes the older [[termcap]] library), upon which in turn are built libraries such as the [[curses (programming library)|curses]] and [[ncurses]] libraries. Application programs use the terminal capabilities to provide [[textual user interface]]s with windows, dialogue boxes, buttons, labels, input fields, menus, and so forth.{{sfn|Coffin|1991|p=115}}{{sfn|Coffin|1991|p=372}}
 
==== Controlling environment variables: <code>TERM</code> et al. ====
The particular set of capabilities for the terminal that a (terminal-aware) program's input and output uses is obtained from the database rather than hardwired into programs and libraries, and is controlled by the <code>TERM</code> [[environment variable]] (and, optionally for the termcap libraryand terminfo libraries, the <code>TERMCAP</code> environment variable), and, optionally for the terminfo library, the <code>TERMINFO</code> environment variablevariables, respectively).{{sfn|Coffin|1991|p=115}} This variable is set by whatever ''terminal monitor'' program spawns the programs that then use that terminal for its input and output, or sometimes explicitly. For example:
* The [[getty (Unix)|getty]] program (or equivalent) sets the <code>TERM</code> environment variable according to a system database (variously [[inittab]] or the configuration files for the [[ttymon]] or [[launchd]] programs) defining what local terminals are attached to what serial ports and what terminal types are provided by local virtual terminals or the local system console.
* A dial-up user on a remote terminal is not using the type of terminal that the system commonly expects on that dial-up line, and so manually sets the <code>TERM</code> environment variable immediately after login to the correct type. (More usually, the terminal type set by the getty program for the dial-up line, that the system administrator has determined to be used most often by dial-up users with remote terminals, matches the one used by the dial-up user and that user has no need to override the terminal type.)
* The [[Secure Shell|SSH]] server daemon (or equivalent such as the [[rlogin]] daemon) sets the <code>TERM</code> environment variable to the same terminal type as the SSH client.{{sfn|Coffin|1991|p=779}}
* The software terminal emulator, using a pseudoterminal, sets the <code>TERM</code> environment variable to specify the type of terminal that it is emulating. Emulated terminals often do not exactly match real terminal hardware, and terminal emulators have type names dedicated for their use. The xterm program (by default) sets <code>xterm</code> as the terminal type, for example.{{sfn|Coffin|1991|p=751&ndash;752}} The screen[[GNU Screen]] program sets <code>screen</code> as the terminal type.
 
=== Job control ===
Line 36 ⟶ 35:
=== Line disciplines ===
{{main|line discipline}}
Strictly speaking, in UnicesUnixes a terminal device comprises the underlying ''tty device driver'', responsible for the physical control of the device hardware via I/O instructions and handling device interrupt requests for character input and output, and the ''line discipline''. A line discipline is independent of the actual device hardware, and the same line discipline can be used for a [[terminal concentrator]] device responsible for multiple controlling terminals as for a pseudoterminal. In fact, the line discipline (or, in the case of BSD, AIX, and other systems, line ''disciplines'') are the same across all terminal devices. It is the line discipline that is responsible for local echo, line editing, processing of input modes, processing of output modes, and character mapping. All these things are independent of the actual hardware, dealing as they do in the simple abstractions provided by tty device drivers: transmit a character, receive a character, set various hardware states.{{sfn|Leffler|McKusick|Karels|Quarterman|1989|p=38}}{{sfn|Leffler|McKusick|Karels|Quarterman|1989|p=260&ndash;261}}
 
In [[Seventh Edition Unix]], [[BSD]] systems and derivatives including [[OS XmacOS]], and [[Linux]], each terminal device can be switched amongst multiple line disciplines.{{sfn|Leffler|McKusick|Karels|Quarterman|1989|p=262}} In the AT&amp;T [[STREAMS]] system, line disciplines are STREAMS modules that may be pushed onto and popped off a STREAMS I/O stack.{{sfn|Christian|1988|p=395}}
 
== History ==
The POSIX terminal interface is derived from the terminal interfaces of various Unix systems.
 
=== Early UnicesUnixes: Seventh Edition Unix ===
{{main|Seventh Edition Unix terminal interface}}
The terminal interface provided by Unix 32V and Seventh Edition Unix, and also presented by BSD version 4 as the ''old terminal driver'', was a simple one, largely geared towards teletypewriters as terminals. Input was entered a line at a time, with the terminal driver in the operating system (and not the terminals themselves) providing simple line editing capabilities. A buffer was maintained by the kernel in which editing took place. Applications reading terminal input would receive the contents of the buffer only when the {{keypress|return}} key was pressed on the terminal to end line editing. The {{keypress|@}} key sent from the terminal to the system would erase ("kill") the entire current contents of the editing buffer, and would be normally displayed as an '<tt>{{mono|@</tt>}}' symbol followed by a newline sequence to move the print position to a fresh blank line. The {{keypress|#}} key sent from the terminal to the system would erase the last character from the end of the editing buffer, and would be normally displayed as an '<tt>{{mono|#</tt>}}' symbol, which users would have to recognize as denoting a "rubout" of the preceding character (teletypewriters not being physically capable of erasing characters oneonce they have been printed on the paper).{{sfn|Bourne|1983|p=8}}{{sfn|Bourne|1983|p=130&ndash;131}}{{sfn|Bourne|1983|p=287}}{{sfn|Christian|1988|p=26}}{{sfn|Leffler|McKusick|Karels|Quarterman|1989|p=262}}
 
From a programming point of view, a terminal device had transmit and receive [[baud rate]]s, "erase" and "kill" characters (that performed line editing, as explained), "interrupt" and "quit" characters (generating [[Unix signal|signal]]s to all of the processes for which the terminal was a controlling terminal), "start" and "stop" characters (used for [[modem flow control]]), an "end of file" character (acting like a carriage return except discarded from the buffer by the <code>read()</code> system call and therefore potentially causing a zero-length result to be returned) and various ''basic mode flags'' determining whether [[local echo]] was emulated by the kernel's terminal driver, whether modem flow control was enabled, the lengths of various output delays, mapping for the carriage return character, and the three input modes.{{sfn|Bourne|1983|p=132&ndash;133}}
Line 55 ⟶ 54:
;cbreak mode:
{{main|cbreak mode}}
cbreak mode is one of two character-at-a-time modes. ([[Stephen R. Bourne]] jokingly referred to it {{harv|Bourne|1983|p=288}} as a "half-cooked" and therefore "rare" mode.) The line discipline performs no line editing, and the control sequences for line editing functions are treated as normal character input. Applications programs reading from the terminal receive characters immediately, as soon as they are available in the [[input queue]] to be read. However, the "interrupt" and "quit" control characters, as well as modem flow control characters, are still handled specially and stripped from the input stream.{{sfn|Bourne|1983|p=288}}{{sfn|Leffler|McKusick|Karels|Quarterman|1989|p=260}}
;raw mode:raw mode is the other of the two character-at-a-time modes. The line discipline performs no line editing, and the control sequences for both line editing functions and the various special characters ("interrupt", "quit", and flow control) are treated as normal character input. Applications programs reading from the terminal receive characters immediately, and receive the entire character stream unaltered, just as it came from the terminal device itself.{{sfn|Bourne|1983|p=132}}{{sfn|Bourne|1983|p=288}}{{sfn|Leffler|McKusick|Karels|Quarterman|1989|p=260}}
 
Line 61 ⟶ 60:
 
=== BSD: the advent of job control ===
{{see also|Job control (Unix)}}
{{main|BSD version 4 terminal interface}}
With the BSD UnicesUnixes came [[Job control (Unix)|job control]], and a ''new terminal driver'' with extended capabilities.{{sfn|Leffler|McKusick|Karels|Quarterman|1989|p=262}} These extensions comprised additional (again programmatically modifiable) special characters:
* The "suspend" and "delayed suspend" characters (by default {{keypress|Control|Z}} and {{keypress|Control|Y}} &mdash; ASCII [[Substitute character|<code>SUB</code>]] and [[End of Medium|<code>EM</code>]]) caused the generation of a new <code>[[SIGTSTP]]</code> signal to processes in the terminal's controlling process group.{{sfn|Leffler|McKusick|Karels|Quarterman|1989|p=260}}
* The "word erase", "literal next", and "reprint" characters (by default {{keypress|Control|W}}, {{keypress|Control|V}}, and {{keypress|Control|R}} &mdash; ASCII [[End Transmission Block character|<code>ETB</code>]], [[Synchronous idle|<code>SYN</code>]], and [[C0_and_C1_control_codesC0 and C1 control codes#C0_.28ASCII_and_derivatives.29ASCII|<code>DC2</code>]]) performed additional line editing functions. "word erase" erased the last word at the end of the line editing buffer. "literal next" allowed any special character to be entered into the line editing buffer (a function available, somewhat inconveniently, in Seventh Edition Unix via the backslash character). "reprint" caused the line discipline to reprint the current contents of the line editing buffer on a new line (useful for when another, background, process had generated output that had intermingled with line editing).{{sfn|Leffler|McKusick|Karels|Quarterman|1989|p=260}}
 
The programmatic interface for querying and modifying all of these extra modes and control characters was still the <code>ioctl()</code> system call, which its creators {{harv|Leffler|McKusick|Karels|Quarterman|1989|p=262}} described as a "rather cluttered interface". All of the original Seventh Edition Unix functionality was retained, and the new functionality was added via additional <code>ioctl()</code> operation codes, resulting in a programmatic interface that had clearly grown, and that presented some duplication of functionality.{{sfn|Leffler|McKusick|Karels|Quarterman|1989|p=262&ndash;263}}
 
=== System III and System V ===
[[System III]] introduced a new programming interface that combined Seventh Edition's separate <code>ioctl()</code> operations to get and set flags and to get and set control characters into calls that used a <code>termio</code> structure to hold both flags and control characters and that could get them in a single operation and set them in another single operation. It also split some of the flags from the Seventh Edition interface into multiple separate flags, and added some additional capabilities, although it did not support job control or the cooked-mode enhancements of 4BSD.<ref>{{cite web|title=System III tty(4) man page source|url=http://minnie.tuhs.org/cgi-bin/utree.pl?file=SysIII/usr/src/man/man4/tty.4|accessdateaccess-date=October 5, 2012}}</ref> For example, it replaced the "cooked", "cbreak", and "raw" modes of Seventh Edition with different abstractions. The recognition of signal-generating characters is independent of input mode, and there are only the two input modes: canonical and non-canonical. (This allows a terminal input mode not present in Seventh Edition and BSD: canonical mode with signal generation disabled.)
 
System III's successors, including [[System V]], used the same interface.
 
=== POSIX: Consolidation and abstraction ===
One of the major problems that the POSIX standard addressed with its definition of a general terminal interface was the plethora of programmatic interfaces. Although by the time of the standard the behaviour of terminals was fairly uniform from system to system, most UnicesUnixes having adopted the notions of line disciplines and the BSD job control capabilities, the programmatic interface to terminals via the <code>ioctl()</code> system call was a mess. Different UnicesUnixes supplied different <code>ioctl()</code> operations, with different (symbolic) names, and different flags. Portable source code had to contain a significant amount of conditional compilation to accommodate the differences across software platforms, even though they were all notionally Unix.{{sfn|Zlotnick|1991|p=157}}
 
The POSIX standard replaces the <code>ioctl()</code> system entirely, with a set of library functions (which, of course, may be ''implemented'' under the covers via platform-specific <code>ioctl()</code> operations) with standardized names and parameters. The <code>termio</code> data structure of System V Unix was used as a template for the POSIX <code>termios</code> data structure, whose fields were largely unchanged except that they now used alias data types for specifying the fields, allowing them to be easily ported across multiple processor architectures by implementors, rather than explicitly requiring the <code>unsigned short</code> and <code>char</code> data types of the C and C++ programming languages (which might be inconvenient sizes on some processor architectures).{{sfn|Zlotnick|1991|p=157}}{{sfn|Zlotnick|1991|p=163}}
 
POSIX also introduced support for job control, with the <code>termios</code> structure containing suspend and delayed-suspend characters in addition to the control characters supported by System III and System V. It did not add any of the cooked-mode extensions from BSD, although [[SunOS]] 4.x, [[System V Release 4]], [[Solaris (operating system)|Solaris]], [[HP-UX]], [[AIX]], newer BSDs, [[macOS]], and [[Linux]] have implemented them as extensions to <code>termios</code>.
 
== What the standard defines ==
 
=== Controlling terminals and process groups ===
{{further2further|[[controllingControlling terminal]]|[[processProcess group]]}}
Each [[process (computing)|process]] in the system has either a single ''controlling terminal'', or no controlling terminal at all. A process inherits its controlling terminal from its parent, and the only operations upon a process are acquiring a controlling terminal, by a process that has no controlling terminal, and relinquishing it, by a process that has a controlling terminal.{{sfn|Zlotnick|1991|p=157}}
 
Line 91 ⟶ 90:
 
=== The <code>termios</code> data structure ===
The data structure used by all of the terminal library calls is the <code>termios</code> structure,{{sfn|Zlotnick|1991|p=162}} whose C and C++ programming language definition is as follows:{{sfn|Zlotnick|1991|p=163}}<sourcesyntaxhighlight lang=cpp enclose=divc>struct termios {
tcflag_t c_iflag ; // Input modes
tcflag_t c_oflag ; // Output modes
Line 97 ⟶ 96:
tcflag_t c_lflag ; // Local modes
cc_t c_cc[NCCS] ; // Control characters
} ;</sourcesyntaxhighlight>
 
The order of the fields within the <code>termios</code> structure is not defined, and implementations are allowed to add non-standard fields.{{sfn|Zlotnick|1991|p=163}} Indeed, implementations have to add non-standard fields for recording input and output baud rates. These are recorded in the structure, in an implementation-defined form, and accessed via accessor functions, rather than by direct manipulation of the field values, as is the case for the standardized structure fields.{{sfn|Zlotnick|1991|p=166}}
 
The data type aliases <code>tcflag_t</code> and <code>cc_t</code>, as well as the symbolic constant <code>NCCS</code> and a whole load of symbolic constants for the various mode flags, control character names, and baud rates, are all defined in a standard header <code>termios.h</code>. (This is not to be confused with the similarly named header <code>termio.h</code> from System III and System V, which defines a similar <code>termio</code> structure and a lot of similarly named symbolic constants. This interface is specific to System III and System V, and code that uses it will not necessarily be portable to other systems.){{sfn|Zlotnick|1991|p=162&ndash;163}}
 
The structure's fields are (in summary, for details see the main article{{clarify|date=August 2018}}):
;<code>c_iflag</code>:input mode flags for controlling input parity, input newline translation, modem [[flow control (data)|flow control]], [[8-bit clean]]liness, and response to a (serial port's) [[universal asynchronous receiver/transmitter#Break condition|"break" condition]]{{sfn|Zlotnick|1991|p=163}}
;<code>c_oflag</code>:output mode flags for controlling implementation-defined output postprocessing, output newline translation, and output delays after various control characters have been sent{{sfn|Zlotnick|1991|p=164}}{{sfn|Leffler|McKusick|Karels|Quarterman|1989|p=260}}
Line 109 ⟶ 108:
;<code>c_lflag</code>:local control flags for controlling the line discipline rather than the terminal hardware: canonical mode, echo modes, signal-generation character recognition and handling, and enabling the generation of the <code>SIGTTOU</code> signal by the <code>write()</code> system call{{sfn|Zlotnick|1991|p=166}}
 
The library functions are (in summary, for details see the main article{{clarify|date=August 2018}}):
;<code>tcgetattr()</code>:query a terminal device's current attribute settings into a <code>termios</code> structure{{sfn|Zlotnick|1991|p=167}}
;<code>tcsetattr()</code>:set a terminal device's current attribute settings from a <code>termios</code> structure, optionally waiting for queued output to drain and flushing queued input{{sfn|Zlotnick|1991|p=167}}
Line 181 ⟶ 180:
 
== Sources ==
{{refbegin|1}}
* {{cite book|
ref=harv|
title=UNIX unbounded: a beginning approach|
first=Amir|last=Afzal|
Line 192 ⟶ 190:
}}
* {{cite book|
ref=harv|
title=The UNIX system|
series=International computer science series|
first=Stephen R.|last=Bourne|authorlinkauthor-link=Stephen R. Bourne|
publisher=Addison-Wesley|
year=1983|
Line 201 ⟶ 198:
}}
* {{cite book|
ref=harv|
title=The UNIX Operating System|
first=Kaare|
last=Christian|
publisher=John Wiley &amp; Sons|
edition=2nd|
year=1988|
isbn=978-0-471-84781-6|
url-access=registration|
}}
url=https://archive.org/details/unixoperatingsys0000chri}}
* {{cite book|
ref=harv|
title=UNIX system V release 4: the complete reference|
first=Stephen|
last=Coffin|
publisher=Osborne McGraw-Hill|
year=1991|
isbn=978-0-07-881653-6|
url=https://archive.org/details/unixsystemvrelea00coff}}
}}
* {{cite book|
title=Essential system administration| url=https://archive.org/details/essentialsystema00fris_0| url-access=registration|
ref=harv|
title=Essential system administration|
series=A nutshell handbook|
first=AEleenÆleen|last=Frisch|
edition=3rd|
publisher=O'Reilly Media, Inc.|
Line 228 ⟶ 225:
}}
* {{cite book|
ref=harv|
title=The Design and implementation of the 4.3BSD UNIX operating system|
chapter=Terminal Handling|
series=Addison-Wesley series in computer science|
first1=Samuel J.|
last1=Leffler|
first2=Marshall Kirk|
last2=McKusick|authorlink2
author-link2=Marshall Kirk McKusick|
first3=Michael J.|
last3=Karels|
first4=John S.|
last4=Quarterman|authorlink4
author-link4=John Quarterman|
publisher=Addison-Wesley|
year=1989|
isbn=978-0-201-06196-3|
url-access=registration|
}}
url=https://archive.org/details/designimplementa0000unse}}
* {{cite book|
ref=harv|
title=The POSIX.1 standard: a programmer's guide|
chapter=Controlling Terminal Devices|
first=Fred|
last=Zlotnick|
publisher=Benjamin/Cummings Pub. Co.|
year=1991|
isbn=978-0-8053-9605-8|
url-access=registration|
}}
url=https://archive.org/details/posix1standardpr0000zlot}}
{{refend}}
 
Line 254 ⟶ 258:
* {{cite journal|
title=11. General Terminal Interface|
workjournal=The Open Group Base Specifications|version=6|
publisher=[[The Open Group]]|
year=2004|
Line 260 ⟶ 264:
}}
* {{cite book|
ref=harv|
chapter=Terminal I/O|
title=POSIX programmer's guide: writing portable UNIX programs with the POSIX.1 standard|
series=Computer Science Series|
first=Donald A.|
last=Lewine|
publisher=O'Reilly Media, Inc.|
year=1991|
isbn=978-0-937175-73-6|
url=https://archive.org/details/posixprogrammers00lewi}}
}}
 
[[Category:Computer terminals]]