iPXE
Functions
ansiesc.c File Reference

ANSI escape sequences. More...

#include <string.h>
#include <assert.h>
#include <ipxe/ansiesc.h>

Go to the source code of this file.

Functions

 FILE_LICENCE (GPL2_OR_LATER_OR_UBDL)
static void ansiesc_call_handler (struct ansiesc_context *ctx, unsigned int function, int count, int params[])
 Call ANSI escape sequence handler.
int ansiesc_process (struct ansiesc_context *ctx, int c)
 Process character that may be part of ANSI escape sequence.

Detailed Description

ANSI escape sequences.

Definition in file ansiesc.c.


Function Documentation

FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL  )
static void ansiesc_call_handler ( struct ansiesc_context ctx,
unsigned int  function,
int  count,
int  params[] 
) [static]

Call ANSI escape sequence handler.

Parameters:
ctxANSI escape sequence context
functionControl function identifier
countParameter count
paramsParameter list

Definition at line 44 of file ansiesc.c.

References ansiesc_handler::function, ansiesc_handler::handle, and ansiesc_context::handlers.

Referenced by ansiesc_process().

                                                  {
        struct ansiesc_handler *handlers = ctx->handlers;
        struct ansiesc_handler *handler;

        for ( handler = handlers ; handler->function ; handler++ ) {
                if ( handler->function == function ) {
                        handler->handle ( ctx, count, params );
                        break;
                }
        }
}
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;
        }
}