These changes are the raw update to qemu-2.6.
[kvmfornfv.git] / qemu / roms / ipxe / src / core / ansiesc.c
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
26 #include <string.h>
27 #include <assert.h>
28 #include <ipxe/ansiesc.h>
29
30 /** @file
31  *
32  * ANSI escape sequences
33  *
34  */
35
36 /**
37  * Call ANSI escape sequence handler
38  *
39  * @v ctx               ANSI escape sequence context
40  * @v function          Control function identifier
41  * @v count             Parameter count
42  * @v params            Parameter list
43  */
44 static void ansiesc_call_handler ( struct ansiesc_context *ctx,
45                                    unsigned int function, int count,
46                                    int params[] ) {
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 }
57
58 /**
59  * Process character that may be part of ANSI escape sequence
60  *
61  * @v ctx               ANSI escape sequence context
62  * @v c                 Character
63  * @ret c               Original character if not part of escape sequence
64  * @ret <0              Character was part of escape sequence
65  *
66  * ANSI escape sequences will be plucked out of the character stream
67  * and interpreted; once complete they will be passed to the
68  * appropriate handler if one exists in this ANSI escape sequence
69  * context.
70  *
71  * In the interests of code size, we are rather liberal about the
72  * sequences we are prepared to accept as valid.
73  */
74 int ansiesc_process ( struct ansiesc_context *ctx, int c ) {
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 }