iPXE
ansiesc.c
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2006 Michael Brown <mbrown@fensystems.co.uk>.
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License as
6  * published by the Free Software Foundation; either version 2 of the
7  * License, or any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17  * 02110-1301, USA.
18  *
19  * You can also choose to distribute this program under the terms of
20  * the Unmodified Binary Distribution Licence (as given in the file
21  * COPYING.UBDL), provided that you have satisfied its requirements.
22  */
23 
24 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
25 FILE_SECBOOT ( PERMITTED );
26 
27 #include <string.h>
28 #include <assert.h>
29 #include <ipxe/ansiesc.h>
30 
31 /** @file
32  *
33  * ANSI escape sequences
34  *
35  */
36 
37 /**
38  * Call ANSI escape sequence handler
39  *
40  * @v ctx ANSI escape sequence context
41  * @v function Control function identifier
42  * @v count Parameter count
43  * @v params Parameter list
44  */
46  unsigned int function, int count,
47  int params[] ) {
48  struct ansiesc_handler *handlers = ctx->handlers;
49  struct ansiesc_handler *handler;
50 
51  for ( handler = handlers ; handler->function ; handler++ ) {
52  if ( handler->function == function ) {
53  handler->handle ( ctx, count, params );
54  break;
55  }
56  }
57 }
58 
59 /**
60  * Process character that may be part of ANSI escape sequence
61  *
62  * @v ctx ANSI escape sequence context
63  * @v c Character
64  * @ret c Original character if not part of escape sequence
65  * @ret <0 Character was part of escape sequence
66  *
67  * ANSI escape sequences will be plucked out of the character stream
68  * and interpreted; once complete they will be passed to the
69  * appropriate handler if one exists in this ANSI escape sequence
70  * context.
71  *
72  * In the interests of code size, we are rather liberal about the
73  * sequences we are prepared to accept as valid.
74  */
75 int ansiesc_process ( struct ansiesc_context *ctx, int c ) {
76 
77  if ( ctx->count == 0 ) {
78  if ( c == ESC ) {
79  /* First byte of CSI : begin escape sequence */
80  ctx->count = 1;
81  memset ( ctx->params, 0xff, sizeof ( ctx->params ) );
82  ctx->function = 0;
83  return -1;
84  } else {
85  /* Normal character */
86  return c;
87  }
88  } else {
89  if ( c == '[' ) {
90  /* Second byte of CSI : do nothing */
91  } else if ( ( c >= '0' ) && ( c <= '9' ) ) {
92  /* Parameter Byte : part of a parameter value */
93  int *param = &ctx->params[ctx->count - 1];
94  if ( *param < 0 )
95  *param = 0;
96  *param = ( ( *param * 10 ) + ( c - '0' ) );
97  } else if ( c == ';' ) {
98  /* Parameter Byte : parameter delimiter */
99  ctx->count++;
100  if ( ctx->count > ( sizeof ( ctx->params ) /
101  sizeof ( ctx->params[0] ) ) ) {
102  /* Excessive parameters : abort sequence */
103  ctx->count = 0;
104  DBG ( "Too many parameters in ANSI escape "
105  "sequence\n" );
106  }
107  } else if ( ( ( c >= 0x20 ) && ( c <= 0x2f ) ) ||
108  ( c == '?' ) ) {
109  /* Intermediate Byte */
110  ctx->function <<= 8;
111  ctx->function |= c;
112  } else {
113  /* Treat as Final Byte. Zero ctx->count before
114  * calling handler to avoid potential infinite loops.
115  */
116  int count = ctx->count;
117  ctx->count = 0;
118  ctx->function <<= 8;
119  ctx->function |= c;
120  ansiesc_call_handler ( ctx, ctx->function,
121  count, ctx->params );
122  }
123  return -1;
124  }
125 }
FILE_SECBOOT(PERMITTED)
A handler for an escape sequence.
Definition: ansiesc.h:35
void(* handle)(struct ansiesc_context *ctx, unsigned int count, int params[])
Handle escape sequence.
Definition: ansiesc.h:61
unsigned int function
The control function identifier.
Definition: ansiesc.h:45
ANSI escape sequence context.
Definition: ansiesc.h:74
struct golan_eq_context ctx
Definition: CIB_PRM.h:28
int ansiesc_process(struct ansiesc_context *ctx, int c)
Process character that may be part of ANSI escape sequence.
Definition: ansiesc.c:75
Assertions.
ANSI escape sequences.
static unsigned int count
Number of entries.
Definition: dwmac.h:225
#define ESC
Escape character.
Definition: ansiesc.h:93
struct hv_monitor_parameter param[4][32]
Parameters.
Definition: hyperv.h:24
FILE_LICENCE(GPL2_OR_LATER_OR_UBDL)
#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:45
String functions.
void * memset(void *dest, int character, size_t len) __nonnull