Section 21.3. Linux Console Capabilities

   


21.3. Linux Console Capabilities

The Linux console,[2] like most terminals, is modal: Its response to data depends on what mode it is in. By default, it prints on the screen the characters you send to it unless it receives an escape or control character. A control character simply causes some control action to be taken, but the next character is read normally; there is no change in processing mode. An escape character signals the beginning of an escape sequence and changes the processing mode to escape mode.

[2] This description owes a considerable amount of organization to an excellent usenet post to the comp.os.linux.announce newsgroup by Peter Jones on September 30, 1995.

For example, consider the following C string:

 "this is a line\na \033[1mbold\033[0m word\n" 


The console processes the string in the following sequence:

1.

Starting from the current cursor position, the console prints the words "this is a line".

2.

It encounters the linefeed (\n) control character, so (because Linux and Unix traditionally operate in a mode in which a linefeed signals a carriage return, as well) it moves the cursor to the beginning of the next line, scrolling the whole display up one line if the cursor was already on the bottom line.

3.

It displays the string "a " at the beginning of that line.

4.

It encounters the escape character, "\033", and moves into escape mode.

5.

It reads the "["character, and moves into Command Sequence Introduction (CSI) mode.

6.

In CSI mode, it reads a series of ASCII-coded decimal numbers separated by ";", which are called parameters, until a letter is encountered. The letter determines what action to take, modified by the data in the parameters. In this case, there is one parameter, "1", and the letter "m" means that the parameter is used to determine character rendition; the "1" sets the bold attribute on.

7.

It prints the string "bold" in a bold rendition.

8.

Another character rendition sequence follows, which resets all attributes to their default, so it prints " word" in a normal rendition.

9.

Finally, it encounters and processes another newline.

So, assuming that the cursor was at the beginning of a line to start with, the output from the entire string will look something like this:

 this is a line a bold word 


21.3.1. Control Characters

The console reads control characters immediately, acts on them, and then continues to read characters in normal mode.

In termcap and terminfo files and documentation, control characters are represented by ^c. We use that convention often in this book, because it will be more generally useful to you than octal C escape sequences. To find the numeric value of a control character, some systems provide a CTRL() macro in <termios.h>, but it is not standard on all systems. Instead, we provide our own version, CTRLCHAR():

 #define CTRLCHAR(ch) ((ch)&0x1F) 


It is used like this:

 if (c == CTRLCHAR('C')) {     /* control-C was pressed */ } 


The control characters understood by the Linux console are described in Table 21.1. The ^? character is actually '?'+0100, not '?'-0100, so it is not really a control-question-mark, but ^? is the standard name for it anyway. Its value is 0177 (octal), 127 (decimal), 7F (hexadecimal). You will not be able to use the CTRL macro just described to test for it. Instead, use the numeric value 127.

Table 21.1. Console Control Characters

Control Character

ASCII Name

Description

^G

BEL

Sounds a tone

^H

BS

Moves cursor to previous character without overwriting it if the cursor is not in the first column already

^I

HT

Horizontal tab; moves cursor to next tab stop

^J

LF

Line feed; moves cursor to next line, scrolls scrolling region if already at the bottom of the scrolling region

^K

VT

Vertical tab; treated like a line feed

^L

FF

Form feed; treated like a line feed

^M

CR

Carriage return; moves cursor to beginning of current line

^N

SO

Shift out; use alternate (G1) character set to display glyphs, display glyphs for control characters

^O

SI

Shift in; use normal (G0) character set to display glyphs, do not display glyphs for control characters

^X

CAN

Cancels any escape sequence in progress

^Z

SUB

Cancels any escape sequence in progress

^[

ESC

ESCape; begins an escape sequence

^?

DEL

Ignored

ALT-^[

n/a

Introduces a command sequence, described later


Note that the effect of some of these codes depends on the tty settings. Although the console itself is precisely documented here, the tty settings may alter what characters are sent. For instance, sending a ^J (LF) usually causes the tty layer also to send a ^M (CR), and ^? (DEL) can be set to send ^H (BS) instead.

The ALT-^[ character is not an ASCII character at all. It is an ESC character with the eighth bit set ASCII specifies only 7-bit characters. You can use it as a shortcut for entering the CSI sequence, but we recommend that you avoid it because it requires an 8-bit-clean communications link, which might keep your program from running remotely on another Linux system connected, perhaps, by a serial link that transmits only seven bits out of every byte.

For more information on the ASCII characters, see the ascii(7) online manual page. Similarly, the iso_8859_1(7) manual page covers the 8-bit ISO Latin 1 character set (more properly, ISO 8859 Latin Alphabet number 1); this newer standard is becoming the de facto replacement for ASCII, which is now officially called ISO 646-IRV.

21.3.2. Escape Sequences

There are several distinct types of escape sequences. The simplest type of escape sequence consists of the escape character (^[) followed by a single command character. (Although the escape character is represented in C strings as \033, it is represented as ^[ in termcap and terminfo files and documentation.) Five of those single command characters preface more complex escape sequences called command sequences, and the rest cause the console to take simple actions and immediately leave escape mode. The simple escape sequences are documented in Table 21.2

Table 21.2. Console Control Sequences

Escape Sequence

Description

^[M

Moves cursor up one line in current column, back-scrolling screen if necessary (reverse line feed)

^[D

Moves cursor down one line in current column, scrolling screen if necessary (line feed)

^[E

Carriage return and line feed

^[H

Sets tab stop in current column

^[7

Stores cursor position and attributes

^[8

Restores cursor position and attributes

^[>

Puts keypad in numeric mode (normal)

^[=

Puts keypad in application mode (act like DEC VT102 function keys)

^[c

Resets every terminal setting that can be set through control characters and escape sequences

^[Z

Requests terminal ID. Response is ^[[?6c, saying that the console faithfully emulates a DEC VT102 (it includes a large superset of the DEC VT102's capabilities)


Storing and restoring the cursor position (^[7 and ^[8) are not done on a stack; if you do two stores in a row, the second stored position overwrites the first stored position. Conversely, after storing the cursor position once, you can restore it as many times as you wish. Each time, the cursor will revert to the same location. When you restore the cursor position, you also restore character rendition attributes, current character set, and character set definitions, all described later in this chapter.

Cursor position is given in terms of a character cell address, an x,y pair of numbers that names one position on the screen. Character cell addresses on most terminals, including the Linux console, do not follow standard computer-science practice of counting from zero. The upper-left character on the screen is the origin and is addressed as character cell 1,1.

Note that control characters may be included in the middle of an escape sequence. For example, ^[^G8 first beeps and then restores the cursor position and attributes. The sequence ^[^X8 simply prints an 8.

21.3.3. Testing Sequences

To test most sequences, you merely need to log into a virtual console and run cat. Type the sequences you wish to test, and watch the results. For ^[, press the Escape key.

Terminal responses to commands such as the ^[Z terminal identification command or the CSIn command documented later show up as escape sequences that disappear in terminal handling. In cases in which you wish to see such a response, you can simply run

 cat > /tmp/somefile 


Then type the commands, followed by a return and a ^D. Use less, vi, Emacs, or some other program that can handle arbitrary characters to read /tmp/somefile, where you will find the responses directly following the sequences you typed.

21.3.4. Complex Escape Sequences

Five two-character escape sequences are really prefixes to longer, more-complex escape sequences, as shown in Table 21.3. We describe each of these sequences, in turn.

Table 21.3. Complex Console Escape Sequences

Escape Sequence

Description

^[[

Begins a CSI sequence (ALT-^[ is a synonym for this)

^[]

Begins a palette-setting sequence

^[%

Begins a UTF (UTF-8 wide-character Unicode) sequence

^[(

Chooses font mapping for G0 character set

^[)

Chooses font mapping for G1 character set

^[#8

DEC private test sequence; fills screen with E characters


CSI sequences have three or four parts.

1.

^[[ starts a CSI sequence, putting the terminal in CSI mode.

2.

For the h and l sequences only, you can include a ? character to allow you to set or clear DEC private modes (see page 525).

3.

You may provide up to 16 parameters. Parameters are decimal numbers separated by ; characters. 1;23;45 is a list of three parameters: 1, 23, and 45. (If the ; parameter separator is found after 16 parameters have already been read, the CSI sequence is terminated immediately and the terminal goes into normal mode, printing the rest of the sequence.)

4.

A command character terminates the sequence and determines how to interpret the parameters the terminal has already encountered.

The parameters are usually referred to as par1 through par16. If you do not set a parameter explicitly, its value is automatically set to 0 or 1, depending on what makes the most sense. The CSI command characters are documented in Table 21.4.

Table 21.4. CSI Sequences

Char

Description

h

Sets mode; see page 525

l

Clears mode; see page 525

n

par1 =5

Status report: Terminal responds with ^[[0n, which means "OK"

 

par1 =6

Cursor position report: Terminal responds with ^[[x;yR, where y is relative to the origin rather than the region if origin mode is selected (see Table 21.9)

G or '

Sets cursor horizontal position to column par1

A

Moves cursor vertical position up by par1 rows

B or e

Moves cursor vertical position down by par1 rows

C or a

Moves cursor horizontal position right by par1 columns

D

Moves cursor horizontal position left by par1 columns

E

Moves cursor to beginning of line and down par1 rows (1 by default)

F

Moves cursor to beginning of line and up par1 rows (1 by default)

d

Sets cursor vertical position to row par1

H or f

Sets cursor vertical position to row par1 and horizontal position to column par2 (both default to zero, moving the cursor to the origin)

J

par1=0

Clears from cursor to end of display

 

par1=1

Clears from origin to cursor

 

par1=2

Clears entire display

K

par1=0

Clears from cursor to end of line

 

par1=1

Clears from start of line to cursor

 

par1 =2

Clears entire line

L

Inserts par1 lines above the current line

M

Deletes par1 lines, starting with the current line

P

Deletes par1 characters at the current position, shifting the rest of the line left

c

Responds with ^[[?6c (synonym for ^[Z)

g

par1 =0

Clears tab in current column (default)

 

par1 =3

Clears all tabs

m

Character rendition sequence; see Table 21.7

q

Turns keyboard LED par1 on and others off (0 turns all off)

r

Sets scrolling region (applied only in DEC origin mode; see page 525):

 

par1

First line of region, must be between 1 (default) and par2 -1

 

par2

Last line of region, must be between par1 +1 and bottom line (default)

s

Stores cursor position and attributes (synonym for ^[7)

u

Restores cursor position and attributes (synonym for ^[8)

X

Erases up to par1 characters, up to the end of the current line

@

Erases up to par1 characters, up to the end of the current line

]

setterm sequences; see Table 21.10


Several sequences take arguments describing colors; they all use the same mapping from numbers to colors, documented in Table 21.5. Sequences that describe background colors accept only color numbers between 0 and 7; sequences that describe foreground colors accept numbers between 8 and 15 that describe bold or bright colors.

Table 21.5. Color Codes

N

Color

N

Bright Color

0

Black

8

Dark gray

1

Red

9

Bright red

2

Green

10

Bright green

3

Brown

11

Yellow

4

Blue

12

Bright blue

5

Magenta

13

Bright magenta

6

Cyan

14

Bright cyan

7

Gray

15

White


These colors are actually offsets into a table the color names in the table describe the default colors stored at those offsets. However, you can change those colors with a palette-setting sequence; the ^[]P sequence sets an individual palette entry, and the ^[]R sequence resets the palette to the system default palette. Palette entries are defined by seven hexadecimal digits that follow ^[]P, as documented in Table 21.6. So for each palette entry, you can provide a 24-bit color definition with eight bits for each color.

Table 21.6. Color Palette Components

Digit Number

Defines

1

Palette entry to redefine

2*16+3

Value of red component of palette entry

4*16+5

Value of green component of palette entry

6*16+7

Value of blue component of palette entry


The character rendition sequences denoted by the CSI m command may have up to 16 parameters, documented in Table 21.7, in any order. They are applied to the terminal in the order in which they are given, so if 0 to set the default rendition is followed by 1 to set bold, the result is a bold but not blinking, reverse video, or underlined character, regardless of the previous rendition settings.

Table 21.7. Character Rendition Parameters

par

Description

0

Default rendition: normal intensity, no underline, not reverse video, no blinking, and the default color scheme (white on black unless set otherwise by the setterm store sequence ^[[]8)

1

Bold intensity

2

Dim intensity

4

Enables underline

5

Enables blink

7

Enables reverse video

10

Selects primary font (ISO latin 1), does not display control characters, unsets bit 8 on output

11

Selects alternate font (IBM Codepage 437), displays control characters as graphics, unsets bit 8 on output

12

Selects alternate font (IBM Codepage 437), displays control characters as graphics, leaves bit 8 set on output

21 22

Normal intensity

24

Disables underline

25

Disables blink

27

Disables reverse video

30 - 37

Sets foreground color to par ||30; see Table 21.5

38

Enables underline and uses default foreground color

39

Disables underline and uses default foreground color

40 - 47

Sets background color to par ||40; see Table 21.5

49

Uses default background color


Related somewhat to the character rendition sequences are the mode sequences. There are two types of modes: ANSI modes and DEC private modes. The CSI h sequence sets ANSI modes, documented in Table 21.8, and the CSI l sequence clears them. More than one parameter can be included in a sequence. The CSI ?h sequence sets DEC private modes, documented in Table 21.9, and the CSI ?l sequence clears them. Again, more than one parameter can be included.

Table 21.8. ANSI Modes

par

Description

3

Displays control characters

4

Insert mode

20

CRLF mode (produces a carriage return when a linefeed is received)


Table 21.9. DEC Private Modes

par

Description

1

Cursor keys as application keys; in application mode, they are prefixed with ^[O instead of the usual ^[[

3

Unimplemented; may switch between 80- and 132-column mode in the future

5

Sets entire screen in inverse video

6

Sets DEC origin mode, in which scrolling regions are honored, and goes to the origin (of the current scrolling region, if any)

7

Sets autowrap mode (on by default), in which characters that would go beyond the screen cause an automatic CRLF. When autowrap mode is turned off, extra characters overwrite the right-most character on the current line

8

Sets keyboard auto-repeat mode (on by default)

9

Mouse reporting mode 1 (support may be provided by an external program)

25

Makes cursor visible (on by default)

1000

Mouse reporting mode 2 (support may be provided by an external program)


The setterm sequences are a set of CSI sequences with the command character ]. They are documented in Table 21.10.

Table 21.10. Console setterm Sequences

par

Description

1

Sets color to use to represent underline attribute to par2

2

Sets color to use to represent dim attribute to par2

8

Stores current setterm attributes as the defaults, making them the default character rendition attributes

9

Sets screen-blank interval to par2 minutes, but no more than 60 minutes. par2 set to 0 disables screen-blank

10

Sets the console bell frequency to par2 Hz or to the default pitch if par2 is unspecified

11

Sets the console bell duration to par2 milliseconds if par2 is spec-ified and is less than 2000. If par2 is not specified, resets the duration to the default

12

If console par2 is allocated, makes console par2 active (see Chapter 20)

13

Unblanks the screen

14

Sets VESA power-down interval to par2 minutes, but no more than 60 minutes. par2 set to 0 disables VESA power down


There is more to conversing with the console than telling it what to display; you also must recognize key sequences and know which keys they are attached to and although some of them are specified in the terminfo database, others are not. To make life even more interesting, the keyboard is modal. In application mode, the cursor keys produce different codes. As shown in Table 21.9, they are prefixed with ^[O instead of ^[[. This is to support legacy applications that assume that they are talking to DEC terminals.

The key sequences are documented in Table 21.11. Note that the function-key numbering has gaps and that it is designed so that people without F11 and F12 keys are not handicapped.

Table 21.11. Function-Key Encodings

Key Sequence

Key(s)

^[[[A

F1

^[[[B

F2

^[[[C

F3

^[[[D

F4

^[[[E

F5

^[[17~

F6

^[[18~

F7

^[[19~

F8

^[[20~

F9

^[[21~

F10

^[[23~

F11, Shift-F1, Shift-F11

^[[24~

F12, Shift-F2, Shift-F11

^[[25~

Shift-F3

^[[26~

Shift-F4

^[[28~

Shift-F5

^[[29~

Shift-F6

^[[31~

Shift-F7

^[[32~

Shift-F8

^[[33~

Shift-F9

^[[34~

Shift-F10

^[[A

Up arrow

^[[D

Left arrow

^[[B

Down arrow

^[[C

Right arrow

^[[1~

Home

^[[2~

Insert

^[[3~

Delete

^[[4~

End

^[[5~

Page Up

^[[6~

Page Down



       
    top
     


    Linux Application Development
    Linux Application Development (paperback) (2nd Edition)
    ISBN: 0321563220
    EAN: 2147483647
    Year: 2003
    Pages: 168

    flylib.com © 2008-2017.
    If you may any questions please contact us: flylib@qtcs.net