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. More...
 
int ansiesc_process (struct ansiesc_context *ctx, int c)
 Process character that may be part of ANSI escape sequence. More...
 

Detailed Description

ANSI escape sequences.

Definition in file ansiesc.c.

Function Documentation

◆ FILE_LICENCE()

FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL  )

◆ ansiesc_call_handler()

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.

46  {
47  struct ansiesc_handler *handlers = ctx->handlers;
48  struct ansiesc_handler *handler;
49 
50  for ( handler = handlers ; handler->function ; handler++ ) {
51  if ( handler->function == function ) {
52  handler->handle ( ctx, count, params );
53  break;
54  }
55  }
56 }
A handler for an escape sequence.
Definition: ansiesc.h:34
void(* handle)(struct ansiesc_context *ctx, unsigned int count, int params[])
Handle escape sequence.
Definition: ansiesc.h:60
unsigned int function
The control function identifier.
Definition: ansiesc.h:44
struct golan_eq_context ctx
Definition: CIB_PRM.h:28
uint16_t count
Number of entries.
Definition: ena.h:22

References count, ctx, ansiesc_handler::function, and ansiesc_handler::handle.

Referenced by ansiesc_process().

◆ ansiesc_process()

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.

74  {
75 
76  if ( ctx->count == 0 ) {
77  if ( c == ESC ) {
78  /* First byte of CSI : begin escape sequence */
79  ctx->count = 1;
80  memset ( ctx->params, 0xff, sizeof ( ctx->params ) );
81  ctx->function = 0;
82  return -1;
83  } else {
84  /* Normal character */
85  return c;
86  }
87  } else {
88  if ( c == '[' ) {
89  /* Second byte of CSI : do nothing */
90  } else if ( ( c >= '0' ) && ( c <= '9' ) ) {
91  /* Parameter Byte : part of a parameter value */
92  int *param = &ctx->params[ctx->count - 1];
93  if ( *param < 0 )
94  *param = 0;
95  *param = ( ( *param * 10 ) + ( c - '0' ) );
96  } else if ( c == ';' ) {
97  /* Parameter Byte : parameter delimiter */
98  ctx->count++;
99  if ( ctx->count > ( sizeof ( ctx->params ) /
100  sizeof ( ctx->params[0] ) ) ) {
101  /* Excessive parameters : abort sequence */
102  ctx->count = 0;
103  DBG ( "Too many parameters in ANSI escape "
104  "sequence\n" );
105  }
106  } else if ( ( ( c >= 0x20 ) && ( c <= 0x2f ) ) ||
107  ( c == '?' ) ) {
108  /* Intermediate Byte */
109  ctx->function <<= 8;
110  ctx->function |= c;
111  } else {
112  /* Treat as Final Byte. Zero ctx->count before
113  * calling handler to avoid potential infinite loops.
114  */
115  int count = ctx->count;
116  ctx->count = 0;
117  ctx->function <<= 8;
118  ctx->function |= c;
119  ansiesc_call_handler ( ctx, ctx->function,
120  count, ctx->params );
121  }
122  return -1;
123  }
124 }
static __always_inline void off_t int c
Definition: librm.h:173
struct golan_eq_context ctx
Definition: CIB_PRM.h:28
uint16_t count
Number of entries.
Definition: ena.h:22
#define ESC
Escape character.
Definition: ansiesc.h:92
struct hv_monitor_parameter param[4][32]
Parameters.
Definition: hyperv.h:24
#define DBG(...)
Print a debugging message.
Definition: compiler.h:498
static void ansiesc_call_handler(struct ansiesc_context *ctx, unsigned int function, int count, int params[])
Call ANSI escape sequence handler.
Definition: ansiesc.c:44
void * memset(void *dest, int character, size_t len) __nonnull

References ansiesc_call_handler(), c, count, ctx, DBG, ESC, memset(), and param.

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