#include #include "ansi_parser.h" #include "screen.h" /* Ragel constants block */ %%{ machine ansi; write data; }%% static volatile int cs = -1; volatile u32 ansi_parser_char_cnt = 0; void ICACHE_FLASH_ATTR ansi_parser_reset(void) { if (cs != ansi_start) { cs = ansi_start; apars_reset_utf8buffer(); ansi_warn("Parser timeout, state reset"); } } #define HISTORY_LEN 16 #if DEBUG_ANSI static char history[HISTORY_LEN + 1]; #endif void ICACHE_FLASH_ATTR apars_handle_badseq(void) { #if DEBUG_ANSI char buf1[HISTORY_LEN*3+2]; char buf2[HISTORY_LEN*3+2]; char *b1 = buf1; char *b2 = buf2; char c; for(int i=0;i 127) c = '.'; b2 += sprintf(b2, "%c ", c); } ansi_dbg("Context: %s", buf2); ansi_dbg(" %s", buf1); #endif } /** * \brief Linear ANSI chars stream parser * * Parses a stream of bytes using a Ragel parser. The defined * grammar does not use 'unget', so the entire buffer is * always processed in a linear manner. * * \attention -> but always check the Ragel output for 'p--' * or 'p -=', that means trouble. * * \param newdata - array of new chars to process * \param len - length of the newdata buffer */ void ICACHE_FLASH_ATTR ansi_parser(const char *newdata, size_t len) { // The CSI code is built here static char csi_leading; //!< Leading char, 0 if none static int csi_ni; //!< Number of the active digit static int csi_cnt; //!< Digit count static int csi_n[CSI_N_MAX]; //!< Param digits static char csi_char; //!< CSI action char (end) static char osc_buffer[OSC_CHAR_MAX]; static int osc_bi; ansi_parser_char_cnt++; if (len == 0) len = strlen(newdata); // Load new data to Ragel vars const char *p = newdata; const char *eof = NULL; const char *pe = newdata + len; // Init Ragel on the first run if (cs == -1) { %% write init; #if DEBUG_ANSI memset(history, 0, sizeof(history)); #endif } #if DEBUG_ANSI for(int i=len; i] @SHORT_code) | ([()*+-./] @CharsetCmd_start) | (' ' [FG] @SetXCtrls) ) )+ $!errBadSeq; write exec; #*/ }%% } // 'ESC k blah OSC_end' is a shortcut for setting title (k is defined in GNU screen as Title Definition String)