iPXE
Data Structures | Defines | Functions
ansiesc.h File Reference

ANSI escape sequences. More...

Go to the source code of this file.

Data Structures

struct  ansiesc_handler
 A handler for an escape sequence. More...
struct  ansiesc_context
 ANSI escape sequence context. More...

Defines

#define ANSIESC_MAX_PARAMS   5
 Maximum number of parameters within a single escape sequence.
#define ESC   0x1b
 Escape character.
#define CSI   "\033["
 Control Sequence Introducer.
#define ANSIESC_CUP   'H'
 Cursor position.
#define ANSIESC_ED   'J'
 Erase in page.
#define ANSIESC_ED_TO_END   0
 Erase from cursor to end of page.
#define ANSIESC_ED_FROM_START   1
 Erase from start of page to cursor.
#define ANSIESC_ED_ALL   2
 Erase whole page.
#define ANSIESC_SGR   'm'
 Select graphic rendition.
#define ANSIESC_LOG_PRIORITY   'p'
 Explicit log message priority.
#define ANSIESC_DECTCEM_SET   ( ( '?' << 8 ) | 'h' )
 Show cursor.
#define ANSIESC_DECTCEM_RESET   ( ( '?' << 8 ) | 'l' )
 Hide cursor.

Functions

 FILE_LICENCE (GPL2_OR_LATER_OR_UBDL)
int ansiesc_process (struct ansiesc_context *ctx, int c)
 Process character that may be part of ANSI escape sequence.

Detailed Description

ANSI escape sequences.

ANSI X3.64 (aka ECMA-48 or ISO/IEC 6429, available from http://www.ecma-international.org/publications/files/ECMA-ST/Ecma-048.pdf) defines escape sequences consisting of:

A Control Sequence Introducer (CSI)

Zero or more Parameter Bytes (P)

Zero or more Intermediate Bytes (I)

A Final Byte (F)

The CSI consists of ESC (0x1b) followed by "[" (0x5b). The Parameter Bytes, for a standardised (i.e. not private or experimental) sequence, consist of a list of ASCII decimal integers separated by semicolons. The Intermediate Bytes (in the range 0x20 to 0x2f) and the Final Byte (in the range 0x40 to 0x4f) determine the control function.

Definition in file ansiesc.h.


Define Documentation

#define ANSIESC_MAX_PARAMS   5

Maximum number of parameters within a single escape sequence.

Definition at line 65 of file ansiesc.h.

#define ESC   0x1b

Escape character.

Definition at line 92 of file ansiesc.h.

Referenced by ansiesc_process(), getkey(), login_ui(), menu_loop(), pxe_menu_prompt_and_select(), pxe_menu_select(), and usbkbd_map().

#define CSI   "\033["

Control Sequence Introducer.

Definition at line 95 of file ansiesc.h.

Referenced by ansicol_set(), and console_exec().


Function Documentation

FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL  )
int ansiesc_process ( struct ansiesc_context ctx,
int  c 
)

Process character that may be part of ANSI escape sequence.

Parameters:
ctxANSI escape sequence context
cCharacter
Return values:
cOriginal character if not part of escape sequence
<0Character was part of escape sequence

ANSI escape sequences will be plucked out of the character stream and interpreted; once complete they will be passed to the appropriate handler if one exists in this ANSI escape sequence context.

In the interests of code size, we are rather liberal about the sequences we are prepared to accept as valid.

Definition at line 74 of file ansiesc.c.

References ansiesc_call_handler(), ansiesc_context::count, count, DBG, ESC, ansiesc_context::function, memset(), param, and ansiesc_context::params.

Referenced by bios_putchar(), efi_putchar(), fbcon_putchar(), and line_putchar().

                                                           {

        if ( ctx->count == 0 ) {
                if ( c == ESC ) {
                        /* First byte of CSI : begin escape sequence */
                        ctx->count = 1;
                        memset ( ctx->params, 0xff, sizeof ( ctx->params ) );
                        ctx->function = 0;
                        return -1;
                } else {
                        /* Normal character */
                        return c;
                }
        } else {
                if ( c == '[' ) {
                        /* Second byte of CSI : do nothing */
                } else if ( ( c >= '0' ) && ( c <= '9' ) ) {
                        /* Parameter Byte : part of a parameter value */
                        int *param = &ctx->params[ctx->count - 1];
                        if ( *param < 0 )
                                *param = 0;
                        *param = ( ( *param * 10 ) + ( c - '0' ) );
                } else if ( c == ';' ) {
                        /* Parameter Byte : parameter delimiter */
                        ctx->count++;
                        if ( ctx->count > ( sizeof ( ctx->params ) /
                                            sizeof ( ctx->params[0] ) ) ) {
                                /* Excessive parameters : abort sequence */
                                ctx->count = 0;
                                DBG ( "Too many parameters in ANSI escape "
                                      "sequence\n" );
                        }
                } else if ( ( ( c >= 0x20 ) && ( c <= 0x2f ) ) ||
                            ( c == '?' ) ) {
                        /* Intermediate Byte */
                        ctx->function <<= 8;
                        ctx->function |= c;
                } else {
                        /* Treat as Final Byte.  Zero ctx->count before 
                         * calling handler to avoid potential infinite loops.
                         */
                        int count = ctx->count;
                        ctx->count = 0;
                        ctx->function <<= 8;
                        ctx->function |= c;
                        ansiesc_call_handler ( ctx, ctx->function,
                                               count, ctx->params );
                }
                return -1;
        }
}