2 * Copyright (C) 2006 Michael Brown <mbrown@fensystems.co.uk>.
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.
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.
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., 675 Mass Ave, Cambridge, MA 02139, USA.
19 FILE_LICENCE ( GPL2_OR_LATER
);
23 #include <gpxe/ansiesc.h>
27 * ANSI escape sequences
32 * Call ANSI escape sequence handler
34 * @v handlers List of escape sequence handlers
35 * @v function Control function identifier
36 * @v count Parameter count
37 * @v params Parameter list
39 static void ansiesc_call_handler ( struct ansiesc_handler
*handlers
,
40 unsigned int function
, int count
,
42 struct ansiesc_handler
*handler
;
44 for ( handler
= handlers
; handler
->function
; handler
++ ) {
45 if ( handler
->function
== function
) {
46 handler
->handle ( count
, params
);
53 * Process character that may be part of ANSI escape sequence
55 * @v ctx ANSI escape sequence context
57 * @ret c Original character if not part of escape sequence
58 * @ret <0 Character was part of escape sequence
60 * ANSI escape sequences will be plucked out of the character stream
61 * and interpreted; once complete they will be passed to the
62 * appropriate handler if one exists in this ANSI escape sequence
65 * In the interests of code size, we are rather liberal about the
66 * sequences we are prepared to accept as valid.
68 int ansiesc_process ( struct ansiesc_context
*ctx
, int c
) {
69 if ( ctx
->count
== 0 ) {
71 /* First byte of CSI : begin escape sequence */
73 memset ( ctx
->params
, 0xff, sizeof ( ctx
->params
) );
77 /* Normal character */
82 /* Second byte of CSI : do nothing */
83 } else if ( ( c
>= '0' ) && ( c
<= '9' ) ) {
84 /* Parameter Byte : part of a parameter value */
85 int *param
= &ctx
->params
[ctx
->count
- 1];
88 *param
= ( ( *param
* 10 ) + ( c
- '0' ) );
89 } else if ( c
== ';' ) {
90 /* Parameter Byte : parameter delimiter */
92 if ( ctx
->count
> ( sizeof ( ctx
->params
) /
93 sizeof ( ctx
->params
[0] ) ) ) {
94 /* Excessive parameters : abort sequence */
96 DBG ( "Too many parameters in ANSI escape "
99 } else if ( ( c
>= 0x20 ) && ( c
<= 0x2f ) ) {
100 /* Intermediate Byte */
104 /* Treat as Final Byte. Zero ctx->count before
105 * calling handler to avoid potential infinite loops.
107 int count
= ctx
->count
;
111 ansiesc_call_handler ( ctx
->handlers
, ctx
->function
,
112 count
, ctx
->params
);