Greg Hartman | 76d05dc | 2016-11-23 15:51:27 -0800 | [diff] [blame] | 1 | #ifndef _GPXE_ANSIESC_H |
| 2 | #define _GPXE_ANSIESC_H |
| 3 | |
| 4 | /** @file |
| 5 | * |
| 6 | * ANSI escape sequences |
| 7 | * |
| 8 | * ANSI X3.64 (aka ECMA-48 or ISO/IEC 6429, available from |
| 9 | * http://www.ecma-international.org/publications/files/ECMA-ST/Ecma-048.pdf) |
| 10 | * defines escape sequences consisting of: |
| 11 | * |
| 12 | * A Control Sequence Introducer (CSI) |
| 13 | * |
| 14 | * Zero or more Parameter Bytes (P) |
| 15 | * |
| 16 | * Zero or more Intermediate Bytes (I) |
| 17 | * |
| 18 | * A Final Byte (F) |
| 19 | * |
| 20 | * The CSI consists of ESC (0x1b) followed by "[" (0x5b). The |
| 21 | * Parameter Bytes, for a standardised (i.e. not private or |
| 22 | * experimental) sequence, consist of a list of ASCII decimal integers |
| 23 | * separated by semicolons. The Intermediate Bytes (in the range 0x20 |
| 24 | * to 0x2f) and the Final Byte (in the range 0x40 to 0x4f) determine |
| 25 | * the control function. |
| 26 | * |
| 27 | */ |
| 28 | |
| 29 | FILE_LICENCE ( GPL2_OR_LATER ); |
| 30 | |
| 31 | /** A handler for an escape sequence */ |
| 32 | struct ansiesc_handler { |
| 33 | /** The control function identifier |
| 34 | * |
| 35 | * The control function identifier consists of the |
| 36 | * Intermediate Bytes (if any) and the Final Byte. In |
| 37 | * practice, no more than one immediate byte is ever used, so |
| 38 | * the byte combination can be efficiently expressed as a |
| 39 | * single integer, in the obvious way (with the Final Byte |
| 40 | * being the least significant byte). |
| 41 | */ |
| 42 | unsigned int function; |
| 43 | /** Handle escape sequence |
| 44 | * |
| 45 | * @v count Parameter count |
| 46 | * @v params Parameter list |
| 47 | * |
| 48 | * A negative parameter value indicates that the parameter was |
| 49 | * omitted and that the default value for this control |
| 50 | * function should be used. |
| 51 | * |
| 52 | * Since all parameters are optional, there is no way to |
| 53 | * distinguish between "zero parameters" and "single parameter |
| 54 | * omitted". Consequently, the parameter list will always |
| 55 | * contain at least one item. |
| 56 | */ |
| 57 | void ( * handle ) ( unsigned int count, int params[] ); |
| 58 | }; |
| 59 | |
| 60 | /** Maximum number of parameters within a single escape sequence */ |
| 61 | #define ANSIESC_MAX_PARAMS 4 |
| 62 | |
| 63 | /** |
| 64 | * ANSI escape sequence context |
| 65 | * |
| 66 | * This provides temporary storage for processing escape sequences, |
| 67 | * and points to the list of escape sequence handlers. |
| 68 | */ |
| 69 | struct ansiesc_context { |
| 70 | /** Array of handlers |
| 71 | * |
| 72 | * Must be terminated by a handler with @c function set to |
| 73 | * zero. |
| 74 | */ |
| 75 | struct ansiesc_handler *handlers; |
| 76 | /** Parameter count |
| 77 | * |
| 78 | * Will be zero when not currently in an escape sequence. |
| 79 | */ |
| 80 | unsigned int count; |
| 81 | /** Parameter list */ |
| 82 | int params[ANSIESC_MAX_PARAMS]; |
| 83 | /** Control function identifier */ |
| 84 | unsigned int function; |
| 85 | }; |
| 86 | |
| 87 | /** Escape character */ |
| 88 | #define ESC 0x1b |
| 89 | |
| 90 | /** Control Sequence Introducer */ |
| 91 | #define CSI "\033[" |
| 92 | |
| 93 | /** |
| 94 | * @defgroup ansifuncs ANSI escape sequence function identifiers |
| 95 | * @{ |
| 96 | */ |
| 97 | |
| 98 | /** Cursor position */ |
| 99 | #define ANSIESC_CUP 'H' |
| 100 | |
| 101 | /** Erase in page */ |
| 102 | #define ANSIESC_ED 'J' |
| 103 | |
| 104 | /** Erase from cursor to end of page */ |
| 105 | #define ANSIESC_ED_TO_END 0 |
| 106 | |
| 107 | /** Erase from start of page to cursor */ |
| 108 | #define ANSIESC_ED_FROM_START 1 |
| 109 | |
| 110 | /** Erase whole page */ |
| 111 | #define ANSIESC_ED_ALL 2 |
| 112 | |
| 113 | /** Select graphic rendition */ |
| 114 | #define ANSIESC_SGR 'm' |
| 115 | |
| 116 | /** @} */ |
| 117 | |
| 118 | extern int ansiesc_process ( struct ansiesc_context *ctx, int c ); |
| 119 | |
| 120 | #endif /* _GPXE_ANSIESC_H */ |