//***************************************************************************** // handling escape sequences static int escape_process (microrl_t * pThis, char ch) { if (ch == '[') { pThis->escape_seq = _ESC_BRACKET; return 0; } else if (pThis->escape_seq == _ESC_BRACKET) { if (ch == 'A') { #ifdef _USE_HISTORY hist_search (pThis, _HIST_UP); #endif return 1; } else if (ch == 'B') { #ifdef _USE_HISTORY hist_search (pThis, _HIST_DOWN); #endif return 1; } else if (ch == 'C') { if (pThis->cursor < pThis->cmdlen) { terminal_move_cursor (pThis, 1); pThis->cursor++; } return 1; } else if (ch == 'D') { if (pThis->cursor > 0) { terminal_move_cursor (pThis, -1); pThis->cursor--; } return 1; } else if (ch == '7') { pThis->escape_seq = _ESC_HOME; return 0; } else if (ch == '8') { pThis->escape_seq = _ESC_END; return 0; } } else if (ch == '~') { if (pThis->escape_seq == _ESC_HOME) { terminal_reset_cursor (pThis); pThis->cursor = 0; return 1; } else if (pThis->escape_seq == _ESC_END) { terminal_move_cursor (pThis, pThis->cmdlen-pThis->cursor); pThis->cursor = pThis->cmdlen; return 1; } } /* unknown escape sequence, stop */ return 1; }
//***************************************************************************** // print cmdline to screen, replace '\0' to wihitespace static void terminal_print_line (microrl_t * pThis, int pos, int cursor) { pThis->print ("\033[K"); // delete all from cursor to end char nch [] = {0,0}; for (int i = pos; i < pThis->cmdlen; i++) { nch [0] = pThis->cmdline [i]; if (nch[0] == '\0') nch[0] = ' '; pThis->print (nch); } terminal_reset_cursor (pThis); terminal_move_cursor (pThis, cursor); }
void microrl_insert_char (microrl_t * pThis, int ch) { #ifdef _USE_ESC_SEQ if (pThis->escape) { if (escape_process(pThis, ch)) pThis->escape = 0; } else { #endif switch (ch) { //----------------------------------------------------- #ifdef _ENDL_CR case KEY_CR: new_line_handler(pThis); break; case KEY_LF: break; #elif defined(_ENDL_CRLF) case KEY_CR: pThis->tmpch = KEY_CR; break; case KEY_LF: if (pThis->tmpch == KEY_CR) new_line_handler(pThis); break; #elif defined(_ENDL_LFCR) case KEY_LF: pThis->tmpch = KEY_LF; break; case KEY_CR: if (pThis->tmpch == KEY_LF) new_line_handler(pThis); break; #else case KEY_CR: break; case KEY_LF: new_line_handler(pThis); break; #endif //----------------------------------------------------- #ifdef _USE_COMPLETE case KEY_HT: microrl_get_complite (pThis); break; #endif //----------------------------------------------------- case KEY_ESC: #ifdef _USE_ESC_SEQ pThis->escape = 1; #endif break; //----------------------------------------------------- case KEY_NAK: // ^U while (pThis->cursor > 0) { microrl_backspace (pThis); } terminal_print_line (pThis, 0, pThis->cursor); break; //----------------------------------------------------- case KEY_VT: // ^K pThis->print ("\033[K"); pThis->cmdlen = pThis->cursor; break; //----------------------------------------------------- case KEY_ENQ: // ^E terminal_move_cursor (pThis, pThis->cmdlen-pThis->cursor); pThis->cursor = pThis->cmdlen; break; //----------------------------------------------------- case KEY_SOH: // ^A terminal_reset_cursor (pThis); pThis->cursor = 0; break; //----------------------------------------------------- case KEY_ACK: // ^F if (pThis->cursor < pThis->cmdlen) { terminal_move_cursor (pThis, 1); pThis->cursor++; } break; //----------------------------------------------------- case KEY_STX: // ^B if (pThis->cursor) { terminal_move_cursor (pThis, -1); pThis->cursor--; } break; //----------------------------------------------------- case KEY_DLE: //^P #ifdef _USE_HISTORY hist_search (pThis, _HIST_UP); #endif break; //----------------------------------------------------- case KEY_SO: //^N #ifdef _USE_HISTORY hist_search (pThis, _HIST_DOWN); #endif break; //----------------------------------------------------- case KEY_DEL: // Backspace case KEY_BS: // ^U microrl_backspace (pThis); terminal_print_line (pThis, pThis->cursor, pThis->cursor); break; #ifdef _USE_CTLR_C case KEY_ETX: if (pThis->sigint != NULL) pThis->sigint(); break; #endif //----------------------------------------------------- default: if (((ch == ' ') && (pThis->cmdlen == 0)) || IS_CONTROL_CHAR(ch)) break; if (microrl_insert_text (pThis, (char*)&ch, 1) && echo) terminal_print_line (pThis, pThis->cursor-1, pThis->cursor); break; } #ifdef _USE_ESC_SEQ } #endif }