ansiesc.c
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 FILE_LICENCE ( GPL2_OR_LATER );
00020
00021 #include <string.h>
00022 #include <assert.h>
00023 #include <gpxe/ansiesc.h>
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039 static void ansiesc_call_handler ( struct ansiesc_handler *handlers,
00040 unsigned int function, int count,
00041 int params[] ) {
00042 struct ansiesc_handler *handler;
00043
00044 for ( handler = handlers ; handler->function ; handler++ ) {
00045 if ( handler->function == function ) {
00046 handler->handle ( count, params );
00047 break;
00048 }
00049 }
00050 }
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068 int ansiesc_process ( struct ansiesc_context *ctx, int c ) {
00069 if ( ctx->count == 0 ) {
00070 if ( c == ESC ) {
00071
00072 ctx->count = 1;
00073 memset ( ctx->params, 0xff, sizeof ( ctx->params ) );
00074 ctx->function = 0;
00075 return -1;
00076 } else {
00077
00078 return c;
00079 }
00080 } else {
00081 if ( c == '[' ) {
00082
00083 } else if ( ( c >= '0' ) && ( c <= '9' ) ) {
00084
00085 int *param = &ctx->params[ctx->count - 1];
00086 if ( *param < 0 )
00087 *param = 0;
00088 *param = ( ( *param * 10 ) + ( c - '0' ) );
00089 } else if ( c == ';' ) {
00090
00091 ctx->count++;
00092 if ( ctx->count > ( sizeof ( ctx->params ) /
00093 sizeof ( ctx->params[0] ) ) ) {
00094
00095 ctx->count = 0;
00096 DBG ( "Too many parameters in ANSI escape "
00097 "sequence\n" );
00098 }
00099 } else if ( ( c >= 0x20 ) && ( c <= 0x2f ) ) {
00100
00101 ctx->function <<= 8;
00102 ctx->function |= c;
00103 } else {
00104
00105
00106
00107 int count = ctx->count;
00108 ctx->count = 0;
00109 ctx->function <<= 8;
00110 ctx->function |= c;
00111 ansiesc_call_handler ( ctx->handlers, ctx->function,
00112 count, ctx->params );
00113 }
00114 return -1;
00115 }
00116 }