/* Keystroke Buffering * Copyright (C) 2010 Chris Hall (GMCH), Highwayman * * This file is part of GNU Zebra. * * GNU Zebra is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published * by the Free Software Foundation; either version 2, or (at your * option) any later version. * * GNU Zebra is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * General Public License for more details. * * You should have received a copy of the GNU General Public License * along with GNU Zebra; see the file COPYING. If not, write to the * Free Software Foundation, Inc., 59 Temple Place - Suite 330, * Boston, MA 02111-1307, USA. */ #include #include #include "keystroke.h" #include "list_util.h" #include "memory.h" #include "mqueue.h" #include "zassert.h" /*============================================================================== */ /*------------------------------------------------------------------------------ * Parsing of incoming keystrokes. * * At present this code only handles 8-bit characters, which one assumes are * ISO8859-1 (or similar). The encoding of keystrokes allows for characters * of up to 32-bits -- so could parse UTF-8 (or similar). * * Handles: * * 0. Nothing, returned as: * * type = ks_null * value = knull_eof * knull_not_eof * * len = 0 * truncated = false * broken = false * buf -- not used * * This is returned when there is nothing there. * * Note that this is NOT returned for NUL ('\0') characters. Those are * real characters (whose value just happens to be null). * * 1. Characters, returned as: * * type = ks_char * value = 0x0.. -- the character value * '\0' if truncated, malformed or EOF met * * len = 1..n => length of character representation, or * number of bytes in raw form if no good. * truncated => too long for buffers * broken => malformed or EOF met * buf -- if OK, the representation for the character (UTF-8 ?) * if truncated or broken, the raw bytes * * See notes below on the handling of '\r' and '\n'. * * 2. ESC X -- where X is single character, other than '['. * * Returned as: * * type = ks_esc * value = 0x0.. -- the character value X * ('\0' if hit EOF) * * len = 1 (or 0 if EOF met) * truncated = false * broken => EOF met * buf = copy of X (unless EOF met) * * 3. ESC [ ... X or CSI ... X -- ANSI escape * * Returned as: * * type = ks_csi * value = 0x0.. -- the character value X * ('\0' if hit EOF or malformed) * * len = number of bytes in buf (excluding '\0' terminator) * truncated => too long for buffers * broken => malformed or EOF met * buf -- bytes between ESC [ or CSI and terminating X * NB: '\0' terminated. * * Note: an ANSI escape is malformed if a byte outside the range * 0x20..0x7F is found before the terminating byte. The illegal * byte is deemed not to be part of the sequence. * * 4. Telnet command -- IAC X ... * * IAC IAC is treated as character 0xFF, so appears as a ks_char, or * possibly as a component of an ESC or other sequence. * * Returned as: * * type = ks_iac * value = 0x0.. -- the value of X * ('\0' if hit EOF) * * len = number of bytes in buf (0 if hit EOF after IAC) * truncated => too long for buffers * broken => malformed or EOF met * buf -- the X and any further bytes, * but *excluding* any terminating IAC SE * * Note: a Telnet command may appear at any time, including in the * middle of any of the above "real" keystrokes. * * This is made invisible to the "real" keystrokes, and the Telnet * command(s) will appear before the incomplete real keystroke in * the keystroke stream. * * Note: there are three forms of Telnet command, and one escape. * * 1) IAC IAC is an escape, and maps simply to the value 0xFF, * wherever it appears (except when reading an