Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Vector[0] = C
- Vector[1] = 5
- Vector[0] = m
- Vector[1] = 0
- Vector[2] = 44
- Vector[3] = 32
- #ifndef SequenceParser_H
- #define SequenceParser_H
- #include "safe_queue.hpp"
- #include "message_queue.hpp"
- #include <boost/smart_ptr/shared_ptr.hpp>
- #include <boost/smart_ptr/weak_ptr.hpp>
- #include <string>
- #include <vector>
- // Forward Deceleration
- class Session;
- typedef boost::shared_ptr<Session> session_ptr;
- typedef boost::weak_ptr<Session> session_weak_ptr;
- /**
- * @class SequenceDecoder
- * @author Michael Griffin
- * @date 12/20/2015
- * @file sequence_decoder.hpp
- * @brief Processes incoming data into a message queue.
- */
- class SequenceDecoder
- {
- public:
- SequenceDecoder(session_ptr session);
- ~SequenceDecoder();
- /**
- * @brief Queue the Pasred Data back to the Session for Display
- * @return
- */
- bool sessionQueue();
- // {Main Execution Method}
- // Validate and Decode ESC Sequences
- void decodeEscSequenceData(std::string &input_string);
- private:
- // Handle to Session for Sending Responses to Client.
- session_weak_ptr m_weak_session;
- // Holds Individual Sequences
- MessageQueue m_message_queue;
- // Sequence Parser State
- enum
- {
- SEQ_NORMAL = 0, // Normal Text Data
- SEQ_START = 1, // Start of ESC Sequence
- SEQ_PROCESSING = 2, // Processing for Complete Sequence
- SEQ_DONE = 3, // ESC Sequence Completed OK.
- SEQ_ERROR = 4 // Bad Sequence, Kill it!
- };
- int unsigned m_sequence_state;
- unsigned char m_sequence;
- int m_parameter;
- bool m_is_sequence;
- bool m_is_parameter;
- bool m_is_invalid_sequence;
- bool m_is_sequence_completed;
- int m_sequence_level;
- // Holds the intern data while we build the sequence,
- // This is needed if inputString doesn't have a complete sequence
- // Then we need to append so that the string will have the
- // Original first half of the already parsed sequence.
- std::string m_sequence_builder;
- //Holds the breakdown of the entire sequence
- std::vector<int> m_sequence_params;
- // This string contains normal data passed through the sequence
- // Parser, At the end of a processing loop, this data is passed
- // for writing to the screen.
- std::string m_valid_output_data;
- std::string::size_type m_escape_position;
- /**
- * @brief Level 0 Parsing check for Start of CSI or Alternate ESC Sequences
- */
- void processSequenceLevel0();
- /**
- * @brief Level 1 Parsing Comes After ESC[ = CSI.
- * Numbers and Separators are found in the middle of sequences as Parameters
- * Switch Statement catch the end of a Valid Sequence.
- *
- * Any non-supported sequences can have certain characters after the CSI
- * and These are parsed so that they are skipped and marked Invalid.
- */
- void processSequenceLevel1();
- /**
- * @brief Level 2 Parsing Catches (2) Different Sequence Styles and Comes After ESC[ = CSI.
- * Specifically for ? preceding sequencing, and ' ' Space D ending Sequence
- * For syncterm font switching.
- *
- * Numbers and Separators are found in the middle of sequences as Parameters
- * Switch Statement catch the end of a Valid Sequence.
- *
- * Any non-supported sequences can have certain characters after the CSI
- * and These are parsed so that they are skipped and marked Invalid.
- */
- void processSequenceLevel2();
- /**
- * @brief Decode and Validate Escapce Sequences.
- */
- void validateSequence();
- public:
- /**
- * @brief Reset the Sequence Parser to refresh the screen
- */
- void resetParser();
- };
- typedef boost::shared_ptr<SequenceDecoder> sequence_decoder_ptr;
- #endif
- #include "sequence_decoder.hpp"
- #include "session.hpp"
- #include <iostream>
- #include <string>
- #include <vector>
- #include <sstream>
- // Initialize Class Variables
- SequenceDecoder::SequenceDecoder(session_ptr session)
- : m_weak_session(session)
- , m_sequence_state(SEQ_NORMAL)
- , m_sequence(0)
- , m_parameter(0)
- , m_is_sequence(false)
- , m_is_parameter(false)
- , m_is_invalid_sequence(false)
- , m_is_sequence_completed(false)
- , m_sequence_level(0)
- {
- std::cout << "SequenceDecoder Created" << std::endl;
- }
- SequenceDecoder::~SequenceDecoder()
- {
- std::cout << "~SequenceDecoder" << std::endl;
- std::vector<int>().swap(m_sequence_params); // Clear Parameters
- m_message_queue.clear(); // Clear Message Structure
- }
- /**
- * @brief Level 0 Parsing check for Start of CSI or Alternate ESC Sequences
- */
- void SequenceDecoder::processSequenceLevel0()
- {
- switch(m_sequence)
- {
- case '[': // Control Sequence Introduction ( CSI is 0x9b).
- break;
- // Xterm Sequences Not implemented, pass through
- case ' ':
- //ESC SP F 7-bit controls (S7C1T).
- //ESC SP G 8-bit controls (S8C1T).
- //ESC SP L Set ANSI conformance level 1 (dpANS X3.134.1).
- //ESC SP M Set ANSI conformance level 2 (dpANS X3.134.1).
- //ESC SP N Set ANSI conformance level 3 (dpANS X3.134.1).
- case '#':
- //ESC # 3 DEC double-height line, top half (DECDHL).
- //ESC # 4 DEC double-height line, bottom half (DECDHL).
- //ESC # 5 DEC single-width line (DECSWL).
- //ESC # 6 DEC double-width line (DECDWL).
- //ESC # 8 DEC Screen Alignment Test (DECALN).
- case '%':
- //ESC % @ Select default character set. That is ISO 8859-1 (ISO 2022).
- //ESC % G Select UTF-8 character set (ISO 2022).
- case '(':
- //ESC ( C
- //C = U ? CP437 Character Set. (Not shown on most! MF)
- //C = 0 ? DEC Special Character and Line Drawing Set.
- //C = < ? DEC Supplementary (VT200).
- //C = % 5 ? DEC Supplementary Graphics (VT300).
- //C = > ? DEC Technical (VT300).
- //C = A ? United Kingdom (UK).
- //C = B ? United States (USASCII).
- //C = 4 ? Dutch.
- //C = C or 5 ? Finnish.
- //C = R or f ? French.
- //C = Q or 9 ? French Canadian (VT200, VT300).
- //C = K ? German.
- //C = Y ? Italian.
- //C = ` , E or 6 ? Norwegian/Danish.
- //C = % 6 ? Portuguese (VT300).
- //C = Z ? Spanish.
- //C = H or 7 ? Swedish.
- //C = = ? Swiss.
- case ')':
- //Designate G1 Character Set (ISO 2022, VT100).
- //The same character sets apply as for ESC ( C.
- case '?':
- // Set/Reset Modes
- // Invalid, should have CSI = ESC[ preceding ?
- // Xterm C1 (8-Bit) Control Characters
- case 'D': // Index ( IND is 0x84).
- case 'E': // Next Line ( NEL is 0x85).
- case 'H': // Tab Set ( HTS is 0x88).
- case 'M': // Reverse Index ( RI is 0x8d).
- case 'N': // Single Shift Select of G2 Character Set ( SS2 is 0x8e). This affects next character only.
- case 'O': // Single Shift Select of G3 Character Set ( SS3 is 0x8f). This affects next character only.
- case 'P': // Device Control String ( DCS is 0x90).
- case 'V': // Start of Guarded Area ( SPA is 0x96).
- case 'W': // End of Guarded Area ( EPA is 0x97).
- case 'X': // Start of String ( SOS is 0x98).
- case 'Z': // Return Terminal ID (DECID is 0x9a). Obsolete form of CSI c (DA).
- case ']': // Operating System Command ( OSC is 0x9d).
- case '^': // Privacy Message ( PM is 0x9e).
- case '_': // Application Program Command ( APC is 0x9f).
- case '\': // String Terminator ( ST is 0x9c).
- case '*':
- //ESC * C Designate G2 Character Set (ISO 2022, VT220).
- //The same character sets apply as for ESC ( C.
- case '+':
- //ESC + C Designate G3 Character Set (ISO 2022, VT220).
- //The same character sets apply as for ESC ( C.
- case '-':
- //ESC - C Designate G1 Character Set (VT300).
- //The same character sets apply as for ESC ( C.
- case '.':
- //ESC . C Designate G2 Character Set (VT300).
- //The same character sets apply as for ESC ( C.
- case '/':
- //ESC / C Designate G3 Character Set (VT300).
- //These work for 96-character sets only.
- //C = A ? ISO Latin-1 Supplemental.
- case '6': // ESC 6 Back Index (DECBI), VT420 and up.
- case '7': // ESC 7 Save Cursor (DECSC).
- case '8': // ESC 8 Restore Cursor (DECRC).
- case '9': // ESC 9 Forward Index (DECFI), VT420 and up.
- case '=': // ESC = Application Keypad (DECKPAM).
- case '>': // ESC > Normal Keypad (DECKPNM).
- case 'F': // ESC F Cursor to lower left corner of screen. This is enabled by the hpLowerleftBugCompat resource.
- case 'c': // ESC c Full Reset (RIS).
- case 'l': // ESC l Memory Lock (per HP terminals). Locks memory above the cursor.
- case 'm': // ESC m Memory Unlock (per HP terminals).
- case 'n': // ESC n Invoke the G2 Character Set as GL (LS2).
- case 'o': // ESC o Invoke the G3 Character Set as GL (LS3).
- case '|': // ESC | Invoke the G3 Character Set as GR (LS3R).
- case '}': // ESC } Invoke the G2 Character Set as GR (LS2R).
- case '~': // ESC ~ Invoke the G1 Character Set as GR (LS1R).
- case '