iPXE
ansiesc.h
Go to the documentation of this file.
00001 #ifndef _IPXE_ANSIESC_H
00002 #define _IPXE_ANSIESC_H
00003 
00004 /** @file
00005  *
00006  * ANSI escape sequences
00007  *
00008  * ANSI X3.64 (aka ECMA-48 or ISO/IEC 6429, available from
00009  * http://www.ecma-international.org/publications/files/ECMA-ST/Ecma-048.pdf)
00010  * defines escape sequences consisting of:
00011  *
00012  *     A Control Sequence Introducer (CSI)
00013  *
00014  *     Zero or more Parameter Bytes (P)
00015  *
00016  *     Zero or more Intermediate Bytes (I)
00017  *
00018  *     A Final Byte (F)
00019  *
00020  * The CSI consists of ESC (0x1b) followed by "[" (0x5b).  The
00021  * Parameter Bytes, for a standardised (i.e. not private or
00022  * experimental) sequence, consist of a list of ASCII decimal integers
00023  * separated by semicolons.  The Intermediate Bytes (in the range 0x20
00024  * to 0x2f) and the Final Byte (in the range 0x40 to 0x4f) determine
00025  * the control function.
00026  * 
00027  */
00028 
00029 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
00030 
00031 struct ansiesc_context;
00032 
00033 /** A handler for an escape sequence */
00034 struct ansiesc_handler {
00035         /** The control function identifier
00036          *
00037          * The control function identifier consists of the
00038          * Intermediate Bytes (if any) and the Final Byte.  In
00039          * practice, no more than one immediate byte is ever used, so
00040          * the byte combination can be efficiently expressed as a
00041          * single integer, in the obvious way (with the Final Byte
00042          * being the least significant byte).
00043          */
00044         unsigned int function;
00045         /** Handle escape sequence
00046          *
00047          * @v ctx               ANSI escape context
00048          * @v count             Parameter count
00049          * @v params            Parameter list
00050          *
00051          * A negative parameter value indicates that the parameter was
00052          * omitted and that the default value for this control
00053          * function should be used.
00054          *
00055          * Since all parameters are optional, there is no way to
00056          * distinguish between "zero parameters" and "single parameter
00057          * omitted".  Consequently, the parameter list will always
00058          * contain at least one item.
00059          */
00060         void ( * handle ) ( struct ansiesc_context *ctx, unsigned int count,
00061                             int params[] );
00062 };
00063 
00064 /** Maximum number of parameters within a single escape sequence */
00065 #define ANSIESC_MAX_PARAMS 5
00066 
00067 /**
00068  * ANSI escape sequence context
00069  *
00070  * This provides temporary storage for processing escape sequences,
00071  * and points to the list of escape sequence handlers.
00072  */
00073 struct ansiesc_context {
00074         /** Array of handlers
00075          *
00076          * Must be terminated by a handler with @c function set to
00077          * zero.
00078          */
00079         struct ansiesc_handler *handlers;
00080         /** Parameter count
00081          *
00082          * Will be zero when not currently in an escape sequence.
00083          */
00084         unsigned int count;
00085         /** Parameter list */ 
00086         int params[ANSIESC_MAX_PARAMS];
00087         /** Control function identifier */
00088         unsigned int function;
00089 };
00090 
00091 /** Escape character */
00092 #define ESC 0x1b
00093 
00094 /** Control Sequence Introducer */
00095 #define CSI "\033["
00096 
00097 /**
00098  * @defgroup ansifuncs ANSI escape sequence function identifiers
00099  * @{
00100  */
00101 
00102 /** Cursor position */
00103 #define ANSIESC_CUP 'H'
00104 
00105 /** Erase in page */
00106 #define ANSIESC_ED 'J'
00107 
00108 /** Erase from cursor to end of page */
00109 #define ANSIESC_ED_TO_END 0
00110 
00111 /** Erase from start of page to cursor */
00112 #define ANSIESC_ED_FROM_START 1
00113 
00114 /** Erase whole page */
00115 #define ANSIESC_ED_ALL 2
00116 
00117 /** Select graphic rendition */
00118 #define ANSIESC_SGR 'm'
00119 
00120 /** Explicit log message priority
00121  *
00122  * This is an iPXE private sequence identifier.  (The range 'p' to '~'
00123  * is reserved for private sequences.)
00124  */
00125 #define ANSIESC_LOG_PRIORITY 'p'
00126 
00127 /** Show cursor */
00128 #define ANSIESC_DECTCEM_SET ( ( '?' << 8 ) | 'h' )
00129 
00130 /** Hide cursor */
00131 #define ANSIESC_DECTCEM_RESET ( ( '?' << 8 ) | 'l' )
00132 
00133 /** @} */
00134 
00135 extern int ansiesc_process ( struct ansiesc_context *ctx, int c );
00136 
00137 #endif /* _IPXE_ANSIESC_H */