Advertisement
Guest User

Untitled

a guest
Feb 7th, 2016
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 56.38 KB | None | 0 0
  1. Vector[0] = C
  2. Vector[1] = 5
  3.  
  4. Vector[0] = m
  5. Vector[1] = 0
  6. Vector[2] = 44
  7. Vector[3] = 32
  8.  
  9. #ifndef SequenceParser_H
  10. #define SequenceParser_H
  11.  
  12. #include "safe_queue.hpp"
  13. #include "message_queue.hpp"
  14.  
  15. #include <boost/smart_ptr/shared_ptr.hpp>
  16. #include <boost/smart_ptr/weak_ptr.hpp>
  17.  
  18. #include <string>
  19. #include <vector>
  20.  
  21. // Forward Deceleration
  22. class Session;
  23. typedef boost::shared_ptr<Session> session_ptr;
  24. typedef boost::weak_ptr<Session> session_weak_ptr;
  25.  
  26. /**
  27. * @class SequenceDecoder
  28. * @author Michael Griffin
  29. * @date 12/20/2015
  30. * @file sequence_decoder.hpp
  31. * @brief Processes incoming data into a message queue.
  32. */
  33. class SequenceDecoder
  34. {
  35. public:
  36.  
  37. SequenceDecoder(session_ptr session);
  38. ~SequenceDecoder();
  39.  
  40. /**
  41. * @brief Queue the Pasred Data back to the Session for Display
  42. * @return
  43. */
  44. bool sessionQueue();
  45.  
  46. // {Main Execution Method}
  47. // Validate and Decode ESC Sequences
  48. void decodeEscSequenceData(std::string &input_string);
  49.  
  50. private:
  51.  
  52. // Handle to Session for Sending Responses to Client.
  53. session_weak_ptr m_weak_session;
  54.  
  55. // Holds Individual Sequences
  56. MessageQueue m_message_queue;
  57.  
  58. // Sequence Parser State
  59. enum
  60. {
  61. SEQ_NORMAL = 0, // Normal Text Data
  62. SEQ_START = 1, // Start of ESC Sequence
  63. SEQ_PROCESSING = 2, // Processing for Complete Sequence
  64. SEQ_DONE = 3, // ESC Sequence Completed OK.
  65. SEQ_ERROR = 4 // Bad Sequence, Kill it!
  66. };
  67.  
  68. int unsigned m_sequence_state;
  69. unsigned char m_sequence;
  70. int m_parameter;
  71. bool m_is_sequence;
  72. bool m_is_parameter;
  73. bool m_is_invalid_sequence;
  74. bool m_is_sequence_completed;
  75. int m_sequence_level;
  76.  
  77. // Holds the intern data while we build the sequence,
  78. // This is needed if inputString doesn't have a complete sequence
  79. // Then we need to append so that the string will have the
  80. // Original first half of the already parsed sequence.
  81. std::string m_sequence_builder;
  82.  
  83. //Holds the breakdown of the entire sequence
  84. std::vector<int> m_sequence_params;
  85.  
  86. // This string contains normal data passed through the sequence
  87. // Parser, At the end of a processing loop, this data is passed
  88. // for writing to the screen.
  89. std::string m_valid_output_data;
  90. std::string::size_type m_escape_position;
  91.  
  92. /**
  93. * @brief Level 0 Parsing check for Start of CSI or Alternate ESC Sequences
  94. */
  95. void processSequenceLevel0();
  96.  
  97. /**
  98. * @brief Level 1 Parsing Comes After ESC[ = CSI.
  99. * Numbers and Separators are found in the middle of sequences as Parameters
  100. * Switch Statement catch the end of a Valid Sequence.
  101. *
  102. * Any non-supported sequences can have certain characters after the CSI
  103. * and These are parsed so that they are skipped and marked Invalid.
  104. */
  105. void processSequenceLevel1();
  106.  
  107. /**
  108. * @brief Level 2 Parsing Catches (2) Different Sequence Styles and Comes After ESC[ = CSI.
  109. * Specifically for ? preceding sequencing, and ' ' Space D ending Sequence
  110. * For syncterm font switching.
  111. *
  112. * Numbers and Separators are found in the middle of sequences as Parameters
  113. * Switch Statement catch the end of a Valid Sequence.
  114. *
  115. * Any non-supported sequences can have certain characters after the CSI
  116. * and These are parsed so that they are skipped and marked Invalid.
  117. */
  118. void processSequenceLevel2();
  119.  
  120. /**
  121. * @brief Decode and Validate Escapce Sequences.
  122. */
  123. void validateSequence();
  124.  
  125. public:
  126. /**
  127. * @brief Reset the Sequence Parser to refresh the screen
  128. */
  129. void resetParser();
  130.  
  131. };
  132.  
  133. typedef boost::shared_ptr<SequenceDecoder> sequence_decoder_ptr;
  134.  
  135. #endif
  136.  
  137. #include "sequence_decoder.hpp"
  138. #include "session.hpp"
  139.  
  140. #include <iostream>
  141. #include <string>
  142. #include <vector>
  143. #include <sstream>
  144.  
  145. // Initialize Class Variables
  146. SequenceDecoder::SequenceDecoder(session_ptr session)
  147. : m_weak_session(session)
  148. , m_sequence_state(SEQ_NORMAL)
  149. , m_sequence(0)
  150. , m_parameter(0)
  151. , m_is_sequence(false)
  152. , m_is_parameter(false)
  153. , m_is_invalid_sequence(false)
  154. , m_is_sequence_completed(false)
  155. , m_sequence_level(0)
  156. {
  157. std::cout << "SequenceDecoder Created" << std::endl;
  158. }
  159.  
  160. SequenceDecoder::~SequenceDecoder()
  161. {
  162. std::cout << "~SequenceDecoder" << std::endl;
  163. std::vector<int>().swap(m_sequence_params); // Clear Parameters
  164. m_message_queue.clear(); // Clear Message Structure
  165. }
  166.  
  167. /**
  168. * @brief Level 0 Parsing check for Start of CSI or Alternate ESC Sequences
  169. */
  170. void SequenceDecoder::processSequenceLevel0()
  171. {
  172. switch(m_sequence)
  173. {
  174. case '[': // Control Sequence Introduction ( CSI is 0x9b).
  175. break;
  176.  
  177. // Xterm Sequences Not implemented, pass through
  178. case ' ':
  179. //ESC SP F 7-bit controls (S7C1T).
  180. //ESC SP G 8-bit controls (S8C1T).
  181. //ESC SP L Set ANSI conformance level 1 (dpANS X3.134.1).
  182. //ESC SP M Set ANSI conformance level 2 (dpANS X3.134.1).
  183. //ESC SP N Set ANSI conformance level 3 (dpANS X3.134.1).
  184. case '#':
  185. //ESC # 3 DEC double-height line, top half (DECDHL).
  186. //ESC # 4 DEC double-height line, bottom half (DECDHL).
  187. //ESC # 5 DEC single-width line (DECSWL).
  188. //ESC # 6 DEC double-width line (DECDWL).
  189. //ESC # 8 DEC Screen Alignment Test (DECALN).
  190. case '%':
  191. //ESC % @ Select default character set. That is ISO 8859-1 (ISO 2022).
  192. //ESC % G Select UTF-8 character set (ISO 2022).
  193. case '(':
  194. //ESC ( C
  195. //C = U ? CP437 Character Set. (Not shown on most! MF)
  196. //C = 0 ? DEC Special Character and Line Drawing Set.
  197. //C = < ? DEC Supplementary (VT200).
  198. //C = % 5 ? DEC Supplementary Graphics (VT300).
  199. //C = > ? DEC Technical (VT300).
  200. //C = A ? United Kingdom (UK).
  201. //C = B ? United States (USASCII).
  202. //C = 4 ? Dutch.
  203. //C = C or 5 ? Finnish.
  204. //C = R or f ? French.
  205. //C = Q or 9 ? French Canadian (VT200, VT300).
  206. //C = K ? German.
  207. //C = Y ? Italian.
  208. //C = ` , E or 6 ? Norwegian/Danish.
  209. //C = % 6 ? Portuguese (VT300).
  210. //C = Z ? Spanish.
  211. //C = H or 7 ? Swedish.
  212. //C = = ? Swiss.
  213. case ')':
  214. //Designate G1 Character Set (ISO 2022, VT100).
  215. //The same character sets apply as for ESC ( C.
  216. case '?':
  217. // Set/Reset Modes
  218. // Invalid, should have CSI = ESC[ preceding ?
  219.  
  220. // Xterm C1 (8-Bit) Control Characters
  221. case 'D': // Index ( IND is 0x84).
  222. case 'E': // Next Line ( NEL is 0x85).
  223. case 'H': // Tab Set ( HTS is 0x88).
  224. case 'M': // Reverse Index ( RI is 0x8d).
  225. case 'N': // Single Shift Select of G2 Character Set ( SS2 is 0x8e). This affects next character only.
  226. case 'O': // Single Shift Select of G3 Character Set ( SS3 is 0x8f). This affects next character only.
  227. case 'P': // Device Control String ( DCS is 0x90).
  228. case 'V': // Start of Guarded Area ( SPA is 0x96).
  229. case 'W': // End of Guarded Area ( EPA is 0x97).
  230. case 'X': // Start of String ( SOS is 0x98).
  231. case 'Z': // Return Terminal ID (DECID is 0x9a). Obsolete form of CSI c (DA).
  232. case ']': // Operating System Command ( OSC is 0x9d).
  233. case '^': // Privacy Message ( PM is 0x9e).
  234. case '_': // Application Program Command ( APC is 0x9f).
  235. case '\': // String Terminator ( ST is 0x9c).
  236. case '*':
  237. //ESC * C Designate G2 Character Set (ISO 2022, VT220).
  238. //The same character sets apply as for ESC ( C.
  239. case '+':
  240. //ESC + C Designate G3 Character Set (ISO 2022, VT220).
  241. //The same character sets apply as for ESC ( C.
  242. case '-':
  243. //ESC - C Designate G1 Character Set (VT300).
  244. //The same character sets apply as for ESC ( C.
  245. case '.':
  246. //ESC . C Designate G2 Character Set (VT300).
  247. //The same character sets apply as for ESC ( C.
  248. case '/':
  249. //ESC / C Designate G3 Character Set (VT300).
  250. //These work for 96-character sets only.
  251. //C = A ? ISO Latin-1 Supplemental.
  252. case '6': // ESC 6 Back Index (DECBI), VT420 and up.
  253. case '7': // ESC 7 Save Cursor (DECSC).
  254. case '8': // ESC 8 Restore Cursor (DECRC).
  255. case '9': // ESC 9 Forward Index (DECFI), VT420 and up.
  256. case '=': // ESC = Application Keypad (DECKPAM).
  257. case '>': // ESC > Normal Keypad (DECKPNM).
  258. case 'F': // ESC F Cursor to lower left corner of screen. This is enabled by the hpLowerleftBugCompat resource.
  259. case 'c': // ESC c Full Reset (RIS).
  260. case 'l': // ESC l Memory Lock (per HP terminals). Locks memory above the cursor.
  261. case 'm': // ESC m Memory Unlock (per HP terminals).
  262. case 'n': // ESC n Invoke the G2 Character Set as GL (LS2).
  263. case 'o': // ESC o Invoke the G3 Character Set as GL (LS3).
  264. case '|': // ESC | Invoke the G3 Character Set as GR (LS3R).
  265. case '}': // ESC } Invoke the G2 Character Set as GR (LS2R).
  266. case '~': // ESC ~ Invoke the G1 Character Set as GR (LS1R).
  267. case '': // Catch any NULL characters after ESC
  268. case 'x1b': // catch any double ESC's from bad servers
  269. m_is_invalid_sequence = true;
  270. break;
  271.  
  272. default:
  273. // Nothing Matched, Shouldn't get here.
  274. m_is_invalid_sequence = true;
  275. break;
  276. }
  277. }
  278.  
  279. /**
  280. * @brief Level 1 Parsing Comes After ESC[ = CSI.
  281. * Numbers and Separators are found in the middle of sequences as Parameters
  282. * Switch Statement catch the end of a Valid Sequence.
  283. *
  284. * Any non-supported sequences can have certain characters after the CSI
  285. * and These are parsed so that they are skipped and marked Invalid.
  286. */
  287. void SequenceDecoder::processSequenceLevel1()
  288. {
  289. // If we get here, only valid Sequences are ESC [ Then next Character.
  290. // First Check for Parameters in Sequence
  291. if(std::isdigit(m_sequence)) // Mark for Parameter
  292. {
  293. // Continue to next sequence
  294. return;
  295. }
  296. else if(m_sequence == ';') // Mark for Multi-Parameter
  297. {
  298. // Continue to Next Sequence
  299. return;
  300. }
  301.  
  302. // Catch Valid ESC Sequence Terminators.
  303. switch(m_sequence)
  304. {
  305. case '@': // Insert P s (Blank) Character(s) (default = 1) (ICH).
  306. case 'A': // Cursor Up P s Times (default = 1) (CUU)
  307. case 'B': // Cursor Down P s Times (default = 1) (CUD)
  308. case 'C': // Cursor Forward P s Times (default = 1) (CUF)
  309. case 'D': // Cursor Backward P s Times (default = 1) (CUB)
  310.  
  311. case 'E': // Cursor Next Line P s Times (default = 1) (CNL)
  312. case 'F': // Cursor Preceding Line P s Times (default = 1) (CPL)
  313. case 'G': // Cursor Character Absolute [column] (default = [row,1]) (CHA)
  314. case 'H': // Cursor Position [row;column] (default = [1,1]) (CUP)
  315.  
  316. case 'I': // Cursor Forward Tabulation P s tab stops (default = 1) (CHT).
  317. case 'J':
  318. // Erase in Display (ED). - DECSED has ESC[?J
  319. //P s = 0 ? Erase Below (default).
  320. //P s = 1 ? Erase Above.
  321. //P s = 2 ? Erase All.
  322. //P s = 3 ? Erase Saved Lines (xterm).
  323. case 'K':
  324. // Erase in Line (EL). - DECSED has ESC[?K
  325. //P s = 0 ? Erase to Right (default).
  326. //P s = 1 ? Erase to Left.
  327. //P s = 2 ? Erase All.
  328. case 'L': // Insert P s Line(s) (default = 1) (IL).
  329. case 'M': // Delete P s Line(s) (default = 1) (DL).
  330. case 'P': // Delete P s Character(s) (default = 1) (DCH).
  331. case 'S': // Scroll up P s lines (default = 1) (SU).
  332. case 'T': // Scroll down P s lines (default = 1) (SD).
  333. case 'X': // Erase P s Character(s) (default = 1) (ECH).
  334. case 'Z': // Cursor Backward Tabulation P s tab stops (default = 1) (CBT).
  335.  
  336. case '`': // Character Position Absolute [column] (default = [row,1]) (HPA).
  337. case 'a': // Character Position Relative [columns] (default = [row,col+1]) (HPR).
  338. case 'b': // Repeat the preceding graphic character P s times (REP).
  339. case 'c':
  340. // Send Device Attributes (Primary DA).
  341. //P s = 0 or omitted ? request attributes from terminal. The response depends on the decTerminalID resource setting.
  342. //? CSI ? 1 ; 2 c (‘‘VT100 with Advanced Video Option’’)
  343. //? CSI ? 1 ; 0 c (‘‘VT101 with No Options’’)
  344. //? CSI ? 6 c (‘‘VT102’’)
  345. //? CSI ? 6 2 ; P s c (‘‘VT220’’)
  346. //? CSI ? 6 3 ; P s c (‘‘VT320’’)
  347. //? CSI ? 6 4 ; P s c (‘‘VT420’’)
  348. // The VT100-style response parameters do not mean anything by themselves. VT220 (and higher) parameters do, telling the host what features the terminal supports:
  349. //P s = 1 ? 132-columns.
  350. //P s = 2 ? Printer.
  351. //P s = 3 ? ReGIS graphics.
  352. //P s = 4 ? Sixel graphics.
  353. //P s = 6 ? Selective erase.
  354. //P s = 8 ? User-defined keys.
  355. //P s = 9 ? National Replacement Character sets.
  356. //P s = 1 5 ? Technical characters.
  357. //P s = 1 8 ? User windows.
  358. //P s = 2 1 ? Horizontal scrolling.
  359. //P s = 2 2 ? ANSI color, e.g., VT525.
  360. //P s = 2 9 ? ANSI text locator (i.e., DEC Locator mode).
  361. case 'd': // Line Position Absolute [row] (default = [1,column]) (VPA).
  362. case 'e': // Line Position Relative [rows] (default = [row+1,column]) (VPR).
  363. case 'f': // equivalent to 'H'
  364. case 'g': // Tab Clear (TBC).
  365. case 'h':
  366. // Set Mode (SM).
  367. //P s = 2 ? Keyboard Action Mode (AM).
  368. //P s = 4 ? Insert Mode (IRM).
  369. //P s = 1 2 ? Send/receive (SRM).
  370. //P s = 2 0 ? Automatic Newline (LNM).
  371.  
  372. // DEC Private Mode Set (DECSET). ESC [ ? h
  373. //P s = 1 ? Application Cursor Keys (DECCKM).
  374. //P s = 2 ? Designate USASCII for character sets G0-G3 (DECANM), and set VT100 mode.
  375. //P s = 3 ? 132 Column Mode (DECCOLM).
  376. //P s = 4 ? Smooth (Slow) Scroll (DECSCLM).
  377. //P s = 5 ? Reverse Video (DECSCNM).
  378. //P s = 6 ? Origin Mode (DECOM).
  379. //P s = 7 ? Wraparound Mode (DECAWM).
  380. //P s = 8 ? Auto-repeat Keys (DECARM).
  381. //P s = 9 ? Send Mouse X & Y on button press. See the section Mouse Tracking. This is the X10 xterm mouse protocol.
  382. //P s = 1 0 ? Show toolbar (rxvt).
  383. //P s = 1 2 ? Start Blinking Cursor (att610).
  384. //P s = 1 8 ? Print form feed (DECPFF).
  385. //P s = 1 9 ? Set print extent to full screen (DECPEX).
  386. //P s = 2 5 ? Show Cursor (DECTCEM).
  387. //P s = 3 0 ? Show scrollbar (rxvt).
  388. //P s = 3 5 ? Enable font-shifting functions (rxvt).
  389. //P s = 3 8 ? Enter Tektronix Mode (DECTEK).
  390. //P s = 4 0 ? Allow 80 ? 132 Mode.
  391. //P s = 4 1 ? more(1) fix (see curses resource).
  392. //P s = 4 2 ? Enable National Replacement Character sets (DECNRCM).
  393. //P s = 4 4 ? Turn On Margin Bell.
  394. //P s = 4 5 ? Reverse-wraparound Mode.
  395. //P s = 4 6 ? Start Logging. This is normally disabled by a compile-time option.
  396. //P s = 4 7 ? Use Alternate Screen Buffer. (This may be disabled by the titeInhibit resource).
  397. //P s = 6 6 ? Application keypad (DECNKM).
  398. //P s = 6 7 ? Backarrow key sends backspace (DECBKM).
  399. //P s = 6 9 ? Enable left and right margin mode (DECLRMM), VT420 and up.
  400. //P s = 9 5 ? Do not clear screen when DECCOLM is set/reset (DECNCSM), VT510 and up.
  401. //P s = 1 0 0 0 ? Send Mouse X & Y on button press and release. See the section Mouse Tracking. This is the X11 xterm mouse protocol.
  402. //P s = 1 0 0 1 ? Use Hilite Mouse Tracking.
  403. //P s = 1 0 0 2 ? Use Cell Motion Mouse Tracking.
  404. //P s = 1 0 0 3 ? Use All Motion Mouse Tracking.
  405. //P s = 1 0 0 4 ? Send FocusIn/FocusOut events.
  406. //P s = 1 0 0 5 ? Enable UTF-8 Mouse Mode.
  407. //P s = 1 0 0 6 ? Enable SGR Mouse Mode.
  408. //P s = 1 0 0 7 ? Enable Alternate Scroll Mode.
  409. //P s = 1 0 1 0 ? Scroll to bottom on tty output (rxvt).
  410. //P s = 1 0 1 1 ? Scroll to bottom on key press (rxvt).
  411. //P s = 1 0 1 5 ? Enable urxvt Mouse Mode.
  412. //P s = 1 0 3 4 ? Interpret "meta" key, sets eighth bit. (enables the eightBitInput resource).
  413. //P s = 1 0 3 5 ? Enable special modifiers for Alt and NumLock keys. (This enables the numLock resource).
  414. //P s = 1 0 3 6 ? Send ESC when Meta modifies a key. (This enables the metaSendsEscape resource).
  415. //P s = 1 0 3 7 ? Send DEL from the editing-keypad Delete key.
  416. //P s = 1 0 3 9 ? Send ESC when Alt modifies a key. (This enables the altSendsEscape resource).
  417. //P s = 1 0 4 0 ? Keep selection even if not highlighted. (This enables the keepSelection resource).
  418. //P s = 1 0 4 1 ? Use the CLIPBOARD selection. (This enables the selectToClipboard resource).
  419. //P s = 1 0 4 2 ? Enable Urgency window manager hint when Control-G is received. (This enables the bellIsUrgent resource).
  420. //P s = 1 0 4 3 ? Enable raising of the window when Control-G is received. (enables the popOnBell resource).
  421. //P s = 1 0 4 7 ? Use Alternate Screen Buffer. (This may be disabled by the titeInhibit resource).
  422. //P s = 1 0 4 8 ? Save cursor as in DECSC. (This may be disabled by the titeInhibit resource).
  423. //P s = 1 0 4 9 ? Save cursor as in DECSC and use Alternate Screen Buffer, clearing it first. (This may be disabled by the titeInhibit resource). This combines the effects of the 1 0 4 7 and 1 0 4 8 modes. Use this with terminfo-based applications rather than the 4 7 mode.
  424. //P s = 1 0 5 0 ? Set terminfo/termcap function-key mode.
  425. //P s = 1 0 5 1 ? Set Sun function-key mode.
  426. //P s = 1 0 5 2 ? Set HP function-key mode.
  427. //P s = 1 0 5 3 ? Set SCO function-key mode.
  428. //P s = 1 0 6 0 ? Set legacy keyboard emulation (X11R6).
  429. //P s = 1 0 6 1 ? Set VT220 keyboard emulation.
  430. //P s = 2 0 0 4 ? Set bracketed paste mode.
  431. case 'i':
  432. // Media Copy (MC). (DEC-specific) ESC [ ? i
  433. //P s = 0 ? Print screen (default).
  434. //P s = 4 ? Turn off printer controller mode.
  435. //P s = 5 ? Turn on printer controller mode.
  436.  
  437. case 'l':
  438. // Reset Mode (RM).
  439. //P s = 2 ? Keyboard Action Mode (AM).
  440. //P s = 4 ? Replace Mode (IRM).
  441. //P s = 1 2 ? Send/receive (SRM).
  442. //P s = 2 0 ? Normal Linefeed (LNM).
  443.  
  444. // DEC Private Mode Reset (DECRST).
  445. //P s = 1 ? Normal Cursor Keys (DECCKM).
  446. //P s = 2 ? Designate VT52 mode (DECANM).
  447. //P s = 3 ? 80 Column Mode (DECCOLM).
  448. //P s = 4 ? Jump (Fast) Scroll (DECSCLM).
  449. //P s = 5 ? Normal Video (DECSCNM).
  450. //P s = 6 ? Normal Cursor Mode (DECOM).
  451. //P s = 7 ? No Wraparound Mode (DECAWM).
  452. //P s = 8 ? No Auto-repeat Keys (DECARM).
  453. //P s = 9 ? Don’t send Mouse X & Y on button press.
  454. //P s = 1 0 ? Hide toolbar (rxvt).
  455. //P s = 1 2 ? Stop Blinking Cursor (att610).
  456. //P s = 1 8 ? Don’t print form feed (DECPFF).
  457. //P s = 1 9 ? Limit print to scrolling region (DECPEX).
  458. //P s = 2 5 ? Hide Cursor (DECTCEM).
  459. //P s = 3 0 ? Don’t show scrollbar (rxvt).
  460. //P s = 3 5 ? Disable font-shifting functions (rxvt).
  461. //P s = 4 0 ? Disallow 80 ? 132 Mode.
  462. //P s = 4 1 ? No more(1) fix (see curses resource).
  463. //P s = 4 2 ? Disable National Replacement Character sets (DECNRCM).
  464. //P s = 4 4 ? Turn Off Margin Bell.
  465. //P s = 4 5 ? No Reverse-wraparound Mode.
  466. //P s = 4 6 ? Stop Logging. (This is normally disabled by a compile-time option).
  467. //P s = 4 7 ? Use Normal Screen Buffer.
  468. //P s = 6 6 ? Numeric keypad (DECNKM).
  469. //P s = 6 7 ? Backarrow key sends delete (DECBKM).
  470. //P s = 6 9 ? Disable left and right margin mode (DECLRMM), VT420 and up.
  471. //P s = 9 5 ? Clear screen when DECCOLM is set/reset (DECNCSM), VT510 and up.
  472. //P s = 1 0 0 0 ? Don’t send Mouse X & Y on button press and release. See the section Mouse Tracking.
  473. //P s = 1 0 0 1 ? Don’t use Hilite Mouse Tracking.
  474. //P s = 1 0 0 2 ? Don’t use Cell Motion Mouse Tracking.
  475. //P s = 1 0 0 3 ? Don’t use All Motion Mouse Tracking.
  476. //P s = 1 0 0 4 ? Don’t send FocusIn/FocusOut events.
  477. //P s = 1 0 0 5 ? Disable UTF-8 Mouse Mode.
  478. //P s = 1 0 0 6 ? Disable SGR Mouse Mode.
  479. //P s = 1 0 0 7 ? Disable Alternate Scroll Mode.
  480. //P s = 1 0 1 0 ? Don’t scroll to bottom on tty output (rxvt).
  481. //P s = 1 0 1 1 ? Don’t scroll to bottom on key press (rxvt).
  482. //P s = 1 0 1 5 ? Disable urxvt Mouse Mode.
  483. //P s = 1 0 3 4 ? Don’t interpret "meta" key. (This disables the eightBitInput resource).
  484. //P s = 1 0 3 5 ? Disable special modifiers for Alt and NumLock keys. (This disables the numLock resource).
  485. //P s = 1 0 3 6 ? Don’t send ESC when Meta modifies a key. (This disables the metaSendsEscape resource).
  486. //P s = 1 0 3 7 ? Send VT220 Remove from the editing-keypad Delete key.
  487. //P s = 1 0 3 9 ? Don’t send ESC when Alt modifies a key. (This disables the altSendsEscape resource).
  488. //P s = 1 0 4 0 ? Do not keep selection when not highlighted. (This disables the keepSelection resource).
  489. //P s = 1 0 4 1 ? Use the PRIMARY selection. (This disables the selectToClipboard resource).
  490. //P s = 1 0 4 2 ? Disable Urgency window manager hint when Control-G is received. (This disables the bellIsUrgent resource).
  491. //P s = 1 0 4 3 ? Disable raising of the window when Control-G is received. (This disables the popOnBell resource).
  492. //P s = 1 0 4 7 ? Use Normal Screen Buffer, clearing screen first if in the Alternate Screen. (This may be disabled by the titeInhibit resource).
  493. //P s = 1 0 4 8 ? Restore cursor as in DECRC. (This may be disabled by the titeInhibit resource).
  494. //P s = 1 0 4 9 ? Use Normal Screen Buffer and restore cursor as in DECRC. (This may be disabled by the titeInhibit resource). This combines the effects of the 1 0 4 7 and 1 0 4 8 modes. Use this with terminfo-based applications rather than the 4 7 mode.
  495. //P s = 1 0 5 0 ? Reset terminfo/termcap function-key mode.
  496. //P s = 1 0 5 1 ? Reset Sun function-key mode.
  497. //P s = 1 0 5 2 ? Reset HP function-key mode.
  498. //P s = 1 0 5 3 ? Reset SCO function-key mode.
  499. //P s = 1 0 6 0 ? Reset legacy keyboard emulation (X11R6).
  500. //P s = 1 0 6 1 ? Reset keyboard emulation to Sun/PC style.
  501. //P s = 2 0 0 4 ? Reset bracketed paste mode.
  502. case 'm':
  503. // Character Attributes (SGR).
  504.  
  505. //P s = 0 ? Normal (default).
  506. //P s = 1 ? Bold.
  507. //P s = 2 ? Faint, decreased intensity (ISO 6429).
  508. //P s = 3 ? Italicized (ISO 6429).
  509. //P s = 4 ? Underlined.
  510. //P s = 5 ? Blink (appears as Bold).
  511. //P s = 7 ? Inverse.
  512. //P s = 8 ? Invisible, i.e., hidden (VT300).
  513. //P s = 9 ? Crossed-out characters (ISO 6429).
  514. //P s = 2 1 ? Doubly-underlined (ISO 6429).
  515. //P s = 2 2 ? Normal (neither bold nor faint).
  516. //P s = 2 3 ? Not italicized (ISO 6429).
  517. //P s = 2 4 ? Not underlined.
  518. //P s = 2 5 ? Steady (not blinking).
  519. //P s = 2 7 ? Positive (not inverse).
  520. //P s = 2 8 ? Visible, i.e., not hidden (VT300).
  521. //P s = 2 9 ? Not crossed-out (ISO 6429).
  522. //P s = 3 0 ? Set foreground color to Black.
  523. //P s = 3 1 ? Set foreground color to Red.
  524. //P s = 3 2 ? Set foreground color to Green.
  525. //P s = 3 3 ? Set foreground color to Yellow.
  526. //P s = 3 4 ? Set foreground color to Blue.
  527. //P s = 3 5 ? Set foreground color to Magenta.
  528. //P s = 3 6 ? Set foreground color to Cyan.
  529. //P s = 3 7 ? Set foreground color to White.
  530. //P s = 3 9 ? Set foreground color to default (original).
  531. //P s = 4 0 ? Set background color to Black.
  532. //P s = 4 1 ? Set background color to Red.
  533. //P s = 4 2 ? Set background color to Green.
  534. //P s = 4 3 ? Set background color to Yellow.
  535. //P s = 4 4 ? Set background color to Blue.
  536. //P s = 4 5 ? Set background color to Magenta.
  537. //P s = 4 6 ? Set background color to Cyan.
  538. //P s = 4 7 ? Set background color to White.
  539. //P s = 4 9 ? Set background color to default (original).
  540.  
  541. // If 16-color support is compiled, the following apply. Assume that xterm’s resources are set so that the ISO color codes are the first 8 of a set of 16.
  542. // Then the aixterm colors are the bright versions of the ISO colors:
  543. //P s = 9 0 ? Set foreground color to Black.
  544. //P s = 9 1 ? Set foreground color to Red.
  545. //P s = 9 2 ? Set foreground color to Green.
  546. //P s = 9 3 ? Set foreground color to Yellow.
  547. //P s = 9 4 ? Set foreground color to Blue.
  548. //P s = 9 5 ? Set foreground color to Magenta.
  549. //P s = 9 6 ? Set foreground color to Cyan.
  550. //P s = 9 7 ? Set foreground color to White.
  551. //P s = 1 0 0 ? Set background color to Black.
  552. //P s = 1 0 1 ? Set background color to Red.
  553. //P s = 1 0 2 ? Set background color to Green.
  554. //P s = 1 0 3 ? Set background color to Yellow.
  555. //P s = 1 0 4 ? Set background color to Blue.
  556. //P s = 1 0 5 ? Set background color to Magenta.
  557. //P s = 1 0 6 ? Set background color to Cyan.
  558. //P s = 1 0 7 ? Set background color to White.
  559.  
  560. // If xterm is compiled with the 16-color support disabled, it supports the following, from rxvt:
  561. //P s = 1 0 0 ? Set foreground and background color to default.
  562. // Xterm maintains a color palette whose entries are identified by an index beginning with zero. If 88- or 256-color support is compiled, the following apply:
  563.  
  564. //All parameters are decimal integers.
  565. //RGB values range from zero (0) to 255.
  566. //ISO-8613-3 can be interpreted in more than one way; xterm allows the semicolons in this control to be replaced by colons (but after the first colon, colons must be used).
  567.  
  568. // These ISO-8613-3 controls are supported:
  569. //P s = 3 8 ; 2 ; P r ; P g ; P b ? Set foreground color to the closest match in xterm’s palette for the given RGB P r /P g /P b .
  570. //P s = 3 8 ; 5 ; P s ? Set foreground color to the second P s .
  571. //P s = 4 8 ; 2 ; P r ; P g ; P b ? Set background color to the closest match in xterm’s palette for the given RGB P r /P g /P b .
  572. //P s = 4 8 ; 5 ; P s ? Set background color to the second P s .
  573.  
  574. case 'n':
  575. // Device Status Report (DSR).
  576. //P s = 5 ? Status Report. Result (‘‘OK’’) is
  577. //CSI 0 n
  578. //P s = 6 ? Report Cursor Position (CPR) [row;column]. Result is
  579. //CSI r ; c R
  580.  
  581. // Note: it is possible for this sequence to be sent by a function
  582. // key. For example, with the default keyboard configuration
  583. // the shifted F1 key may send (with shift-, control-, alt-modifiers)
  584. //CSI 1 ; 2 R , or
  585. //CSI 1 ; 5 R , or
  586. //CSI 1 ; 6 R , etc.
  587.  
  588. case 'p':
  589. case 'q':
  590. // Load LEDs (DECLL).
  591. //P s = 0 ? Clear all LEDS (default).
  592. //P s = 1 ? Light Num Lock.
  593. //P s = 2 ? Light Caps Lock.
  594. //P s = 3 ? Light Scroll Lock.
  595. //P s = 2 1 ? Extinguish Num Lock.
  596. //P s = 2 2 ? Extinguish Caps Lock.
  597. //P s = 2 3 ? Extinguish Scroll Lock.
  598.  
  599. case 'r': // Set Scrolling Region [top;bottom] (default = full size of window) (DECSTBM).
  600. case 's': // Save cursor position (ANSI.SYS), available only when DECLRMM is disabled.
  601. case 't': // Window manipulation (from dtterm, as well as extensions).
  602.  
  603. case 'u': // Restore cursor position (ANSI.SYS).
  604. case 'x':
  605. // Request Terminal Parameters (DECREQTPARM).
  606. // if P s is a "0" (default) or "1", and xterm is emulating VT100,
  607. // the control sequence elicits a response of the same form whose
  608. // parameters describe the terminal:
  609. //P s ? the given P s incremented by 2.
  610. //P n = 1 ? no parity.
  611. //P n = 1 ? eight bits.
  612. //P n = 1 ? 2 8 transmit 38.4k baud.
  613. //P n = 1 ? 2 8 receive 38.4k baud.
  614. //P n = 1 ? clock multiplier.
  615. //P n = 0 ? STP flags.
  616. case '!': // Soft terminal reset (DECSTR).
  617. // Valid Sequences Ended.
  618. m_is_sequence_completed = true;
  619. break;
  620.  
  621. // Unsupported Text and Keyboard Modifiers.
  622. // These are Preceding Modifiers ie after ESC [ >
  623. case '>':
  624. case '$':
  625. case '"':
  626. case '*':
  627. m_is_invalid_sequence = true;
  628. break;
  629.  
  630. case '?': // -- Switch to next sequenceLevel
  631. // Ie.. handle ESC[?7h, ESC[?7l, and other ? DEC Sequences.
  632. // These Sequences DEC Level Sequences and need extra Parsing.
  633. case ' ': // Need to Precheck SyncTerm Font switching ' D' Space D.
  634. ++m_sequence_level;
  635. break;
  636. default:
  637. // Nothing Matched, Shouldn't get here.
  638. m_is_invalid_sequence = true;
  639. break;
  640. }
  641. }
  642.  
  643. /**
  644. * @brief Level 2 Parsing Catches (2) Different Sequence Styles and Comes After ESC[ = CSI.
  645. * Specifically for ? preceding sequencing, and ' ' Space D ending Sequence
  646. * For syncterm font switching.
  647. *
  648. * Numbers and Separators are found in the middle of sequences as Parameters
  649. * Switch Statement catch the end of a Valid Sequence.
  650. *
  651. * Any non-supported sequences can have certain characters after the CSI
  652. * and These are parsed so that they are skipped and marked Invalid.
  653. */
  654. void SequenceDecoder::processSequenceLevel2()
  655. {
  656. // If the last addition to sequence is a space and we are now here.
  657. // The only valid terminator = 'D' for Sycnterm Font Switching.
  658. if(m_sequence_builder[m_sequence_builder.size()-2] == ' ' && m_sequence != 'D')
  659. {
  660. m_is_invalid_sequence = true;
  661. return;
  662. }
  663.  
  664. // If we get here, only valid Sequences are ESC [ Then next Character.
  665. // First Check for Parameters in Sequence
  666. if(std::isdigit(m_sequence)) // Mark for Parameter
  667. {
  668. // Continue to next sequence
  669. return;
  670. }
  671. else if(m_sequence == ';') // Mark for Multi-Parameter
  672. {
  673. // Continue to Next Sequence
  674. return;
  675. }
  676.  
  677. // Catch Valid ESC Sequence Terminators.
  678. switch(m_sequence)
  679. {
  680. case 'D':
  681. // SyncTerm Font Switching Sequences
  682. // SyncTerm Sequences have a Space before the D,
  683. // If a space is found, the sequence is passed from the previous level.
  684. // ESC [0;0 D
  685.  
  686. // First we are catching DEC Style ESC[? Parameters here.
  687. case 'J':
  688. // Erase in Display (ED). - DECSED has ESC[?J
  689. //P s = 0 ? Erase Below (default).
  690. //P s = 1 ? Erase Above.
  691. //P s = 2 ? Erase All.
  692. case 'K':
  693. // Erase in Line (EL). - DECSED has ESC[?K
  694. //P s = 0 ? Erase to Right (default).
  695. //P s = 1 ? Erase to Left.
  696. //P s = 2 ? Erase All.
  697. case 'h':
  698. // DEC Private Mode Set (DECSET). ESC [ ? h
  699. //P s = 1 ? Application Cursor Keys (DECCKM).
  700. //P s = 2 ? Designate USASCII for character sets G0-G3 (DECANM), and set VT100 mode.
  701. //P s = 3 ? 132 Column Mode (DECCOLM).
  702. //P s = 4 ? Smooth (Slow) Scroll (DECSCLM).
  703. //P s = 5 ? Reverse Video (DECSCNM).
  704. //P s = 6 ? Origin Mode (DECOM).
  705. //P s = 7 ? Wraparound Mode (DECAWM).
  706. //P s = 8 ? Auto-repeat Keys (DECARM).
  707. //P s = 9 ? Send Mouse X & Y on button press. See the section Mouse Tracking. This is the X10 xterm mouse protocol.
  708. //P s = 1 0 ? Show toolbar (rxvt).
  709. //P s = 1 2 ? Start Blinking Cursor (att610).
  710. //P s = 1 8 ? Print form feed (DECPFF).
  711. //P s = 1 9 ? Set print extent to full screen (DECPEX).
  712. //P s = 2 5 ? Show Cursor (DECTCEM).
  713. //P s = 3 0 ? Show scrollbar (rxvt).
  714. //P s = 3 5 ? Enable font-shifting functions (rxvt).
  715. //P s = 3 8 ? Enter Tektronix Mode (DECTEK).
  716. //P s = 4 0 ? Allow 80 ? 132 Mode.
  717. //P s = 4 1 ? more(1) fix (see curses resource).
  718. //P s = 4 2 ? Enable National Replacement Character sets (DECNRCM).
  719. //P s = 4 4 ? Turn On Margin Bell.
  720. //P s = 4 5 ? Reverse-wraparound Mode.
  721. //P s = 4 6 ? Start Logging. This is normally disabled by a compile-time option.
  722. //P s = 4 7 ? Use Alternate Screen Buffer. (This may be disabled by the titeInhibit resource).
  723. //P s = 6 6 ? Application keypad (DECNKM).
  724. //P s = 6 7 ? Backarrow key sends backspace (DECBKM).
  725. //P s = 6 9 ? Enable left and right margin mode (DECLRMM), VT420 and up.
  726. //P s = 9 5 ? Do not clear screen when DECCOLM is set/reset (DECNCSM), VT510 and up.
  727. //P s = 1 0 0 0 ? Send Mouse X & Y on button press and release. See the section Mouse Tracking. This is the X11 xterm mouse protocol.
  728. //P s = 1 0 0 1 ? Use Hilite Mouse Tracking.
  729. //P s = 1 0 0 2 ? Use Cell Motion Mouse Tracking.
  730. //P s = 1 0 0 3 ? Use All Motion Mouse Tracking.
  731. //P s = 1 0 0 4 ? Send FocusIn/FocusOut events.
  732. //P s = 1 0 0 5 ? Enable UTF-8 Mouse Mode.
  733. //P s = 1 0 0 6 ? Enable SGR Mouse Mode.
  734. //P s = 1 0 0 7 ? Enable Alternate Scroll Mode.
  735. //P s = 1 0 1 0 ? Scroll to bottom on tty output (rxvt).
  736. //P s = 1 0 1 1 ? Scroll to bottom on key press (rxvt).
  737. //P s = 1 0 1 5 ? Enable urxvt Mouse Mode.
  738. //P s = 1 0 3 4 ? Interpret "meta" key, sets eighth bit. (enables the eightBitInput resource).
  739. //P s = 1 0 3 5 ? Enable special modifiers for Alt and NumLock keys. (This enables the numLock resource).
  740. //P s = 1 0 3 6 ? Send ESC when Meta modifies a key. (This enables the metaSendsEscape resource).
  741. //P s = 1 0 3 7 ? Send DEL from the editing-keypad Delete key.
  742. //P s = 1 0 3 9 ? Send ESC when Alt modifies a key. (This enables the altSendsEscape resource).
  743. //P s = 1 0 4 0 ? Keep selection even if not highlighted. (This enables the keepSelection resource).
  744. //P s = 1 0 4 1 ? Use the CLIPBOARD selection. (This enables the selectToClipboard resource).
  745. //P s = 1 0 4 2 ? Enable Urgency window manager hint when Control-G is received. (This enables the bellIsUrgent resource).
  746. //P s = 1 0 4 3 ? Enable raising of the window when Control-G is received. (enables the popOnBell resource).
  747. //P s = 1 0 4 7 ? Use Alternate Screen Buffer. (This may be disabled by the titeInhibit resource).
  748. //P s = 1 0 4 8 ? Save cursor as in DECSC. (This may be disabled by the titeInhibit resource).
  749. //P s = 1 0 4 9 ? Save cursor as in DECSC and use Alternate Screen Buffer, clearing it first. (This may be disabled by the titeInhibit resource). This combines the effects of the 1 0 4 7 and 1 0 4 8 modes. Use this with terminfo-based applications rather than the 4 7 mode.
  750. //P s = 1 0 5 0 ? Set terminfo/termcap function-key mode.
  751. //P s = 1 0 5 1 ? Set Sun function-key mode.
  752. //P s = 1 0 5 2 ? Set HP function-key mode.
  753. //P s = 1 0 5 3 ? Set SCO function-key mode.
  754. //P s = 1 0 6 0 ? Set legacy keyboard emulation (X11R6).
  755. //P s = 1 0 6 1 ? Set VT220 keyboard emulation.
  756. //P s = 2 0 0 4 ? Set bracketed paste mode.
  757. case 'i':
  758. // Media Copy (MC, DEC-specific).
  759. //P s = 1 ? Print line containing cursor.
  760. //P s = 4 ? Turn off autoprint mode.
  761. //P s = 5 ? Turn on autoprint mode.
  762. //P s = 1 0 ? Print composed display, ignores DECPEX.
  763. //P s = 1 1 ? Print all pages.
  764.  
  765. case 'l':
  766. // DEC Private Mode Reset (DECRST).
  767. //P s = 1 ? Normal Cursor Keys (DECCKM).
  768. //P s = 2 ? Designate VT52 mode (DECANM).
  769. //P s = 3 ? 80 Column Mode (DECCOLM).
  770. //P s = 4 ? Jump (Fast) Scroll (DECSCLM).
  771. //P s = 5 ? Normal Video (DECSCNM).
  772. //P s = 6 ? Normal Cursor Mode (DECOM).
  773. //P s = 7 ? No Wraparound Mode (DECAWM).
  774. //P s = 8 ? No Auto-repeat Keys (DECARM).
  775. //P s = 9 ? Don’t send Mouse X & Y on button press.
  776. //P s = 1 0 ? Hide toolbar (rxvt).
  777. //P s = 1 2 ? Stop Blinking Cursor (att610).
  778. //P s = 1 8 ? Don’t print form feed (DECPFF).
  779. //P s = 1 9 ? Limit print to scrolling region (DECPEX).
  780. //P s = 2 5 ? Hide Cursor (DECTCEM).
  781. //P s = 3 0 ? Don’t show scrollbar (rxvt).
  782. //P s = 3 5 ? Disable font-shifting functions (rxvt).
  783. //P s = 4 0 ? Disallow 80 ? 132 Mode.
  784. //P s = 4 1 ? No more(1) fix (see curses resource).
  785. //P s = 4 2 ? Disable National Replacement Character sets (DECNRCM).
  786. //P s = 4 4 ? Turn Off Margin Bell.
  787. //P s = 4 5 ? No Reverse-wraparound Mode.
  788. //P s = 4 6 ? Stop Logging. (This is normally disabled by a compile-time option).
  789. //P s = 4 7 ? Use Normal Screen Buffer.
  790. //P s = 6 6 ? Numeric keypad (DECNKM).
  791. //P s = 6 7 ? Backarrow key sends delete (DECBKM).
  792. //P s = 6 9 ? Disable left and right margin mode (DECLRMM), VT420 and up.
  793. //P s = 9 5 ? Clear screen when DECCOLM is set/reset (DECNCSM), VT510 and up.
  794. //P s = 1 0 0 0 ? Don’t send Mouse X & Y on button press and release. See the section Mouse Tracking.
  795. //P s = 1 0 0 1 ? Don’t use Hilite Mouse Tracking.
  796. //P s = 1 0 0 2 ? Don’t use Cell Motion Mouse Tracking.
  797. //P s = 1 0 0 3 ? Don’t use All Motion Mouse Tracking.
  798. //P s = 1 0 0 4 ? Don’t send FocusIn/FocusOut events.
  799. //P s = 1 0 0 5 ? Disable UTF-8 Mouse Mode.
  800. //P s = 1 0 0 6 ? Disable SGR Mouse Mode.
  801. //P s = 1 0 0 7 ? Disable Alternate Scroll Mode.
  802. //P s = 1 0 1 0 ? Don’t scroll to bottom on tty output (rxvt).
  803. //P s = 1 0 1 1 ? Don’t scroll to bottom on key press (rxvt).
  804. //P s = 1 0 1 5 ? Disable urxvt Mouse Mode.
  805. //P s = 1 0 3 4 ? Don’t interpret "meta" key. (This disables the eightBitInput resource).
  806. //P s = 1 0 3 5 ? Disable special modifiers for Alt and NumLock keys. (This disables the numLock resource).
  807. //P s = 1 0 3 6 ? Don’t send ESC when Meta modifies a key. (This disables the metaSendsEscape resource).
  808. //P s = 1 0 3 7 ? Send VT220 Remove from the editing-keypad Delete key.
  809. //P s = 1 0 3 9 ? Don’t send ESC when Alt modifies a key. (This disables the altSendsEscape resource).
  810. //P s = 1 0 4 0 ? Do not keep selection when not highlighted. (This disables the keepSelection resource).
  811. //P s = 1 0 4 1 ? Use the PRIMARY selection. (This disables the selectToClipboard resource).
  812. //P s = 1 0 4 2 ? Disable Urgency window manager hint when Control-G is received. (This disables the bellIsUrgent resource).
  813. //P s = 1 0 4 3 ? Disable raising of the window when Control-G is received. (This disables the popOnBell resource).
  814. //P s = 1 0 4 7 ? Use Normal Screen Buffer, clearing screen first if in the Alternate Screen. (This may be disabled by the titeInhibit resource).
  815. //P s = 1 0 4 8 ? Restore cursor as in DECRC. (This may be disabled by the titeInhibit resource).
  816. //P s = 1 0 4 9 ? Use Normal Screen Buffer and restore cursor as in DECRC. (This may be disabled by the titeInhibit resource). This combines the effects of the 1 0 4 7 and 1 0 4 8 modes. Use this with terminfo-based applications rather than the 4 7 mode.
  817. //P s = 1 0 5 0 ? Reset terminfo/termcap function-key mode.
  818. //P s = 1 0 5 1 ? Reset Sun function-key mode.
  819. //P s = 1 0 5 2 ? Reset HP function-key mode.
  820. //P s = 1 0 5 3 ? Reset SCO function-key mode.
  821. //P s = 1 0 6 0 ? Reset legacy keyboard emulation (X11R6).
  822. //P s = 1 0 6 1 ? Reset keyboard emulation to Sun/PC style.
  823. //P s = 2 0 0 4 ? Reset bracketed paste mode.
  824.  
  825. case 'n':
  826. // Device Status Report (DSR, DEC-specific).
  827. //P s = 6 ? Report Cursor Position (DECXCPR) [row;column] as CSI ? r ; c R (assumes the default page, i.e., "1").
  828. //P s = 1 5 ? Report Printer status as CSI ? 1 0 n (ready). or CSI ? 1 1 n (not ready).
  829. //P s = 2 5 ? Report UDK status as CSI ? 2 0 n (unlocked) or CSI ? 2 1 n (locked).
  830. //P s = 2 6 ? Report Keyboard status as
  831. //CSI ? 2 7 ; 1 ; 0 ; 0 n (North American).
  832. //The last two parameters apply to VT400 & up, and denote keyboard ready and LK01 respectively.
  833. //P s = 5 3 ? Report Locator status as CSI ? 5 3 n Locator available, if compiled-in, or CSI ? 5 0 n No Locator, if not.
  834. //P s = 5 5 ? Report Locator status as CSI ? 5 3 n Locator available, if compiled-in, or CSI ? 5 0 n No Locator, if not.
  835. //P s = 5 6 ? Report Locator type as CSI ? 5 7 ; 1 n Mouse, if compiled-in, or CSI ? 5 7 ; 0 n Cannot identify, if not.
  836. //P s = 6 2 ? Report macro space (DECMSR) as CSI P n * {
  837. //P s = 6 3 ? Report memory checksum (DECCKSR) as DCS P t ! x x x x ST
  838. //P t is the request id (from an optional parameter to the request).
  839. //The x’s are hexadecimal digits 0-9 and A-F.
  840. //P s = 7 5 ? Report data integrity as CSI ? 7 0 n (ready, no errors)
  841. //P s = 8 5 ? Report multi-session configuration as CSI ? 8 3 n (not configured for multiple-session operation).
  842.  
  843. case 'p':
  844. case 'q':
  845. // Load LEDs (DECLL).
  846. //P s = 0 ? Clear all LEDS (default).
  847. //P s = 1 ? Light Num Lock.
  848. //P s = 2 ? Light Caps Lock.
  849. //P s = 3 ? Light Scroll Lock.
  850. //P s = 2 1 ? Extinguish Num Lock.
  851. //P s = 2 2 ? Extinguish Caps Lock.
  852. //P s = 2 3 ? Extinguish Scroll Lock.
  853.  
  854. case 'r': // Restore DEC Private Mode Values. The value of P s previously saved is restored. P s values are the same as for DECSET.
  855. case 's': // Save DEC Private Mode Values. P s values are the same as for DECSET.
  856. case '!': // Soft terminal reset (DECSTR).
  857. // Valid Sequences Ended.
  858. m_is_sequence_completed = true;
  859. break;
  860.  
  861. // Unsupported Text and Keyboard Modifiers.
  862. // These are Preceding Modifiers ie after ESC [ >
  863. case '>':
  864. case '$':
  865. case '"':
  866. case '*':
  867. case ' ':
  868. m_is_invalid_sequence = true;
  869. break;
  870.  
  871. default:
  872. // Nothing Matched, Shouldn't get here.
  873. m_is_invalid_sequence = true;
  874. break;
  875. }
  876. }
  877.  
  878. /**
  879. * @brief Decode and Validate Escapce Sequences.
  880. */
  881. void SequenceDecoder::validateSequence()
  882. {
  883. // Check and clear vector for fresh sequence
  884. // We only Validate on complete sequences, so we can clear here
  885. // to make sure each run.
  886. if(m_sequence_params.size() > 0)
  887. std::vector<int>().swap(m_sequence_params);
  888.  
  889. // If we get there, we have full CSI with possible ; ; separators.
  890. try
  891. {
  892. // Remove ESC [ then get Terminator
  893. m_sequence_builder.erase(0,2);
  894. int sequenceTerminator =
  895. m_sequence_builder[m_sequence_builder.size()-1];
  896.  
  897. // First Parameter is always the Sequence Terminator.
  898. m_sequence_params.push_back(sequenceTerminator);
  899.  
  900. // Remove Sequence Terminator from string to text for parameters.
  901. m_sequence_builder.erase(m_sequence_builder.size()-1,1);
  902. }
  903. catch(std::exception &e)
  904. {
  905. std::cout << "Exception removing ESC and Terminator Sequences: "
  906. << e.what() << std::endl;
  907. }
  908.  
  909. // Split String by ';' character to separate parameters if it exists.
  910. if(m_sequence_builder.empty())
  911. {
  912. // We have no parameters, just terminator for single sequence.
  913. return;
  914. }
  915.  
  916. // If we have a parameter separator, then tokenize the parameters.
  917. std::string::size_type result = m_sequence_builder.find(";",0);
  918. if(result != std::string::npos)
  919. {
  920. std::istringstream inStream(m_sequence_builder);
  921. std::istringstream tokenStream;
  922. std::string token;
  923.  
  924. // 0;33;47 Splits 0 33 47
  925. // ignores spaces.
  926. while(std::getline(inStream, token, ';'))
  927. {
  928. tokenStream.clear();
  929. if(token.size() == 0)
  930. {
  931. token = "-1"; // Empty fields are set to -1 for implied parms.
  932. }
  933. tokenStream.str(token); // Token to Stream
  934. if((tokenStream >> m_parameter).fail()) // String to Int
  935. {
  936. // Skip this character.
  937. std::cout << "Exception String to Int Parameter Failure."
  938. << std::endl;
  939. continue;
  940. }
  941. m_sequence_params.push_back(m_parameter); // Add to Parameters Vector.
  942. }
  943. }
  944. else
  945. {
  946. // First check for ? DEC Sequence Starter
  947. if(m_sequence_builder[0] == '?')
  948. {
  949. try
  950. {
  951. m_sequence_params.push_back('?'); // Add to Parameters Vector.
  952. m_sequence_builder.erase(0,1); // Remove ?
  953. }
  954. catch(std::exception &e)
  955. {
  956. std::cout << "Exception removing ? sequence starter: "
  957. << e.what() << std::endl;
  958. }
  959. }
  960. // No separator, translate the 1-3 digit code that should be present.
  961. std::istringstream tokenStream;
  962. tokenStream.str(m_sequence_builder); // String to Stream
  963. if((tokenStream >> m_parameter).fail()) // String to Int
  964. {
  965. // Skip this character.
  966. std::cout << "Exception String to Int Parameter Failure."
  967. << std::endl;
  968. }
  969. else
  970. {
  971. m_sequence_params.push_back(m_parameter); // Add to Parameters Vector.
  972. }
  973. }
  974. }
  975.  
  976. /**
  977. * @brief Queue the Pasred Data back to the Session for Display
  978. * @return
  979. */
  980. bool SequenceDecoder::sessionQueue()
  981. {
  982. // Handle to Session Instance
  983. session_ptr session = m_weak_session.lock();
  984. if(session)
  985. {
  986. session->m_data_queue.enqueue(m_message_queue);
  987. m_message_queue.clear();
  988. }
  989. else
  990. {
  991. return false;
  992. }
  993. return true;
  994. }
  995. /*
  996. * Takes String input and parses for ESC Control Sequences
  997. * State machine stays actives waiting for complete control sequences
  998. * before pushing any actions forwards. Normal Text data is passed through.
  999. */
  1000. void SequenceDecoder::decodeEscSequenceData(std::string &input_string)
  1001. {
  1002. // Clear Queue out for each run.
  1003. m_message_queue.clear();
  1004.  
  1005. //Parse entire string and remove any double ESC Sequences.
  1006. std::string::size_type result = 0;
  1007. while(result != std::string::npos)
  1008. {
  1009. result = input_string.find("x1bx1b");
  1010. if(result != std::string::npos)
  1011. {
  1012. try
  1013. {
  1014. //input_string.replace(result,1,"^");
  1015. // Pass through, remove double ESC!
  1016. input_string.replace(result,1,"");
  1017. }
  1018. catch(std::exception &e)
  1019. {
  1020. std::cout << "Exception replacing double ESC characters: "
  1021. << e.what() << std::endl;
  1022. m_sequence_state = SEQ_ERROR; // Reset The State
  1023. }
  1024. }
  1025. }
  1026.  
  1027. // Now loop entire string and find valid escape sequences.
  1028. for(std::string::size_type i = 0; i < input_string.size(); i++)
  1029. {
  1030. // Get next Input Sequence
  1031. m_sequence = input_string[i];
  1032.  
  1033. // Remove Bell from displaying. Annoying in Shell!
  1034. // When not displaying, we'll push this to console so it beeps!
  1035. if(m_sequence == 'x07')
  1036. {
  1037. continue;
  1038. }
  1039.  
  1040. // Check for Start of Sequence, if we hit a sequence we then need
  1041. // To send all Valid Output that Proceeds this sequence so everything
  1042. // is FIFO with regards to how incoming data is handled.
  1043. if(m_sequence == 'x1b')
  1044. {
  1045. m_sequence_state = SEQ_START;
  1046. }
  1047.  
  1048. // Pre-Handle the Parser State
  1049. switch(m_sequence_state)
  1050. {
  1051. // Normal Text Data, Append to Buffer
  1052. case SEQ_NORMAL:
  1053. m_valid_output_data += m_sequence;
  1054. break;
  1055.  
  1056. // Start of Sequence, Push ValidOutputData Buffer Through
  1057. // Then switch to Processing State, So we have FIFO Data
  1058. // Parsing.
  1059. case SEQ_START:
  1060. //TheAnsiParser::Instance()->textInput(validOutputData);
  1061. // Build a Message Queue with PassThrough Text Data.
  1062. {
  1063. m_message_queue.m_text = m_valid_output_data;
  1064.  
  1065. // Queue the Data back to the Session
  1066. if (!sessionQueue())
  1067. return;
  1068.  
  1069. m_valid_output_data.erase();
  1070. m_sequence_state = SEQ_PROCESSING;
  1071. m_sequence_builder += m_sequence;
  1072. }
  1073. break;
  1074.  
  1075. case SEQ_PROCESSING:
  1076. // Were in the middle of processing a control sequence
  1077. // Keep is going till we get DONE!
  1078. m_sequence_builder += m_sequence;
  1079. break;
  1080.  
  1081. case SEQ_ERROR:
  1082. // Validating the Sequence Bombed, Clear the vector and continue.
  1083. std::vector<int>().swap(m_sequence_params); // Clear for next run.
  1084. m_sequence_builder.erase(); // Clear Any Prebuilt.
  1085. m_sequence_state = SEQ_NORMAL;
  1086. continue;
  1087. }
  1088.  
  1089. if(m_sequence == 'x1b' && !m_is_sequence_completed)
  1090. {
  1091. // Error Checking, If were in the middle of an incomplete sequence
  1092. // Then we shouldn't get an ESC before we complete the sequence!
  1093. if(m_is_sequence)
  1094. {
  1095. m_escape_position = i;
  1096. m_is_sequence = true;
  1097. continue;
  1098. }
  1099.  
  1100. // Each Level is the next character after the ESC
  1101. // Check the first few levels to make sure we have a valid CSI
  1102. if(m_is_sequence && !m_is_invalid_sequence)
  1103. {
  1104. switch(m_sequence_level)
  1105. {
  1106. case 0: // Process first char after ESC '['
  1107. processSequenceLevel0();
  1108. // Move to next Level if valid.
  1109. if(!m_is_invalid_sequence)
  1110. {
  1111. ++m_sequence_level;
  1112. }
  1113. break;
  1114.  
  1115. case 1: // Process second char after ESC [ 'c'
  1116. processSequenceLevel1();
  1117. break;
  1118.  
  1119. case 2: // Process 2nd char after ESC [ '?' DEC Sequences
  1120. processSequenceLevel2();
  1121. break;
  1122. }
  1123. }
  1124.  
  1125. // Handle Completed Control Sequences
  1126. if(m_is_sequence_completed)
  1127. {
  1128. // Copy just this sequence into string for validation
  1129. try
  1130. {
  1131. //escapeSequence =
  1132. // input_string.substr(escapePosition,(i+1)-escapePosition);
  1133.  
  1134. // Break up the sequence into separate parameters for
  1135. // The Screen Parser.
  1136. validateSequence();
  1137. m_sequence_state = SEQ_DONE;
  1138. }
  1139. catch(std::exception &e)
  1140. {
  1141. std::cout << "Exception substring on escapeSequence: "
  1142. << e.what() << std::endl;
  1143. m_sequence_state = SEQ_ERROR;
  1144. }
  1145.  
  1146. // If the sequence is completed, The Parse the parameters and
  1147. // Setup for ANSI Parser and Drawing to Screen.
  1148. m_is_sequence_completed = false;
  1149. m_is_sequence = false;
  1150. m_is_invalid_sequence = false;
  1151. m_sequence_level = 0;
  1152. }
  1153. // Invalid Sequences, Replace ESC with ^ characters for display
  1154. // Then move on.
  1155. if(m_is_invalid_sequence)
  1156. {
  1157. for (unsigned char c : m_sequence_builder)
  1158. {
  1159. std::cout << "*** Invalid ESC SeqBuilder[0]: [" << int(c) << "]" << std::endl;
  1160. }
  1161. std::cout << "*** Invalid ESC Seq: [" << m_sequence << "]" << std::endl;
  1162. std::cout << "*** Invalid ESC Seq Level: [" << m_sequence_level << "]" << std::endl;
  1163.  
  1164. m_is_sequence = false;
  1165. m_is_invalid_sequence = false;
  1166. m_sequence_level = 0;
  1167.  
  1168. // First grab the entire sequence parsed so far, from ESC position
  1169. // Place it into validOutputData to display on the screen,
  1170. // We don't want To reset the loop because it will increment,
  1171. // so we grab all characters parsed so far.
  1172. try
  1173. {
  1174. m_valid_output_data += m_sequence_builder;
  1175. m_sequence_builder.erase();
  1176. }
  1177. catch(std::exception &e)
  1178. {
  1179. std::cout << "Exception substring on ESC Position: "
  1180. << e.what() << std::endl;
  1181. }
  1182. // Reset the State
  1183. m_sequence_state = SEQ_ERROR;
  1184. }
  1185.  
  1186. // Post-Handle the Parser State
  1187. switch(m_sequence_state)
  1188. {
  1189. case SEQ_NORMAL:
  1190. // If we are reset to this state, reset the level
  1191. break;
  1192.  
  1193. case SEQ_PROCESSING:
  1194. // Were in the middle of processing a control sequence
  1195. // Keep is going till we get DONE!
  1196. break;
  1197.  
  1198. case SEQ_DONE:
  1199. // We hit an entire sequence, pass the vector
  1200. /*
  1201. * Build Message Queue Broken Down Sequences
  1202. */
  1203. {
  1204. m_message_queue.m_queueParams.swap(m_sequence_params);
  1205.  
  1206. // Queue the Data back to the Session
  1207. if (!sessionQueue())
  1208. {
  1209. return;
  1210. }
  1211.  
  1212. std::vector<int>().swap(m_sequence_params); // Clear for next run.
  1213. m_sequence_state = SEQ_NORMAL; // Reset The State
  1214. m_sequence_builder.erase();
  1215. }
  1216. break;
  1217.  
  1218. case SEQ_ERROR:
  1219. // Validating the Sequence Bombed, Clear the vector and continue.
  1220. std::vector<int>().swap(m_sequence_params); // Clear for next run.
  1221. m_sequence_state = SEQ_NORMAL; // Reset The State
  1222. m_sequence_builder.erase();
  1223. break;
  1224. }
  1225. }
  1226.  
  1227. // Catch any echo'ed input that passed through with no sequences
  1228. if(m_sequence_state == SEQ_NORMAL && m_valid_output_data.size() > 0)
  1229. {
  1230. /*
  1231. * Build Queue with Passthrough Message Test
  1232. */
  1233. m_message_queue.m_text = m_valid_output_data;
  1234.  
  1235. // Queue the Data back to the Session
  1236. if (!sessionQueue())
  1237. {
  1238. return;
  1239. }
  1240. m_valid_output_data.erase();
  1241. }
  1242. }
  1243.  
  1244. /**
  1245. * @brief Reset the Sequence Parser to refresh the screen
  1246. */
  1247. void SequenceDecoder::resetParser()
  1248. {
  1249. // Handle to Session Instatnce
  1250. session_ptr session = m_weak_session.lock();
  1251. if(session)
  1252. {
  1253. session->m_sequence_parser->reset();
  1254. }
  1255. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement