vt100_input.py 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520
  1. """
  2. Parser for VT100 input stream.
  3. """
  4. from __future__ import unicode_literals
  5. import os
  6. import re
  7. import six
  8. import termios
  9. import tty
  10. from six.moves import range
  11. from ..keys import Keys
  12. from ..key_binding.input_processor import KeyPress
  13. __all__ = (
  14. 'InputStream',
  15. 'raw_mode',
  16. 'cooked_mode',
  17. )
  18. _DEBUG_RENDERER_INPUT = False
  19. _DEBUG_RENDERER_INPUT_FILENAME = 'prompt-toolkit-render-input.log'
  20. # Regex matching any CPR response
  21. # (Note that we use '\Z' instead of '$', because '$' could include a trailing
  22. # newline.)
  23. _cpr_response_re = re.compile('^' + re.escape('\x1b[') + r'\d+;\d+R\Z')
  24. # Mouse events:
  25. # Typical: "Esc[MaB*" Urxvt: "Esc[96;14;13M" and for Xterm SGR: "Esc[<64;85;12M"
  26. _mouse_event_re = re.compile('^' + re.escape('\x1b[') + r'(<?[\d;]+[mM]|M...)\Z')
  27. # Regex matching any valid prefix of a CPR response.
  28. # (Note that it doesn't contain the last character, the 'R'. The prefix has to
  29. # be shorter.)
  30. _cpr_response_prefix_re = re.compile('^' + re.escape('\x1b[') + r'[\d;]*\Z')
  31. _mouse_event_prefix_re = re.compile('^' + re.escape('\x1b[') + r'(<?[\d;]*|M.{0,2})\Z')
  32. class _Flush(object):
  33. """ Helper object to indicate flush operation to the parser. """
  34. pass
  35. # Mapping of vt100 escape codes to Keys.
  36. ANSI_SEQUENCES = {
  37. '\x1b': Keys.Escape,
  38. '\x00': Keys.ControlSpace, # Control-Space (Also for Ctrl-@)
  39. '\x01': Keys.ControlA, # Control-A (home)
  40. '\x02': Keys.ControlB, # Control-B (emacs cursor left)
  41. '\x03': Keys.ControlC, # Control-C (interrupt)
  42. '\x04': Keys.ControlD, # Control-D (exit)
  43. '\x05': Keys.ControlE, # Contrel-E (end)
  44. '\x06': Keys.ControlF, # Control-F (cursor forward)
  45. '\x07': Keys.ControlG, # Control-G
  46. '\x08': Keys.ControlH, # Control-H (8) (Identical to '\b')
  47. '\x09': Keys.ControlI, # Control-I (9) (Identical to '\t')
  48. '\x0a': Keys.ControlJ, # Control-J (10) (Identical to '\n')
  49. '\x0b': Keys.ControlK, # Control-K (delete until end of line; vertical tab)
  50. '\x0c': Keys.ControlL, # Control-L (clear; form feed)
  51. '\x0d': Keys.ControlM, # Control-M (13) (Identical to '\r')
  52. '\x0e': Keys.ControlN, # Control-N (14) (history forward)
  53. '\x0f': Keys.ControlO, # Control-O (15)
  54. '\x10': Keys.ControlP, # Control-P (16) (history back)
  55. '\x11': Keys.ControlQ, # Control-Q
  56. '\x12': Keys.ControlR, # Control-R (18) (reverse search)
  57. '\x13': Keys.ControlS, # Control-S (19) (forward search)
  58. '\x14': Keys.ControlT, # Control-T
  59. '\x15': Keys.ControlU, # Control-U
  60. '\x16': Keys.ControlV, # Control-V
  61. '\x17': Keys.ControlW, # Control-W
  62. '\x18': Keys.ControlX, # Control-X
  63. '\x19': Keys.ControlY, # Control-Y (25)
  64. '\x1a': Keys.ControlZ, # Control-Z
  65. '\x1c': Keys.ControlBackslash, # Both Control-\ and Ctrl-|
  66. '\x1d': Keys.ControlSquareClose, # Control-]
  67. '\x1e': Keys.ControlCircumflex, # Control-^
  68. '\x1f': Keys.ControlUnderscore, # Control-underscore (Also for Ctrl-hypen.)
  69. '\x7f': Keys.Backspace, # (127) Backspace
  70. '\x1b[A': Keys.Up,
  71. '\x1b[B': Keys.Down,
  72. '\x1b[C': Keys.Right,
  73. '\x1b[D': Keys.Left,
  74. '\x1b[H': Keys.Home,
  75. '\x1bOH': Keys.Home,
  76. '\x1b[F': Keys.End,
  77. '\x1bOF': Keys.End,
  78. '\x1b[3~': Keys.Delete,
  79. '\x1b[3;2~': Keys.ShiftDelete, # xterm, gnome-terminal.
  80. '\x1b[3;5~': Keys.ControlDelete, # xterm, gnome-terminal.
  81. '\x1b[1~': Keys.Home, # tmux
  82. '\x1b[4~': Keys.End, # tmux
  83. '\x1b[5~': Keys.PageUp,
  84. '\x1b[6~': Keys.PageDown,
  85. '\x1b[7~': Keys.Home, # xrvt
  86. '\x1b[8~': Keys.End, # xrvt
  87. '\x1b[Z': Keys.BackTab, # shift + tab
  88. '\x1b[2~': Keys.Insert,
  89. '\x1bOP': Keys.F1,
  90. '\x1bOQ': Keys.F2,
  91. '\x1bOR': Keys.F3,
  92. '\x1bOS': Keys.F4,
  93. '\x1b[[A': Keys.F1, # Linux console.
  94. '\x1b[[B': Keys.F2, # Linux console.
  95. '\x1b[[C': Keys.F3, # Linux console.
  96. '\x1b[[D': Keys.F4, # Linux console.
  97. '\x1b[[E': Keys.F5, # Linux console.
  98. '\x1b[11~': Keys.F1, # rxvt-unicode
  99. '\x1b[12~': Keys.F2, # rxvt-unicode
  100. '\x1b[13~': Keys.F3, # rxvt-unicode
  101. '\x1b[14~': Keys.F4, # rxvt-unicode
  102. '\x1b[15~': Keys.F5,
  103. '\x1b[17~': Keys.F6,
  104. '\x1b[18~': Keys.F7,
  105. '\x1b[19~': Keys.F8,
  106. '\x1b[20~': Keys.F9,
  107. '\x1b[21~': Keys.F10,
  108. '\x1b[23~': Keys.F11,
  109. '\x1b[24~': Keys.F12,
  110. '\x1b[25~': Keys.F13,
  111. '\x1b[26~': Keys.F14,
  112. '\x1b[28~': Keys.F15,
  113. '\x1b[29~': Keys.F16,
  114. '\x1b[31~': Keys.F17,
  115. '\x1b[32~': Keys.F18,
  116. '\x1b[33~': Keys.F19,
  117. '\x1b[34~': Keys.F20,
  118. # Xterm
  119. '\x1b[1;2P': Keys.F13,
  120. '\x1b[1;2Q': Keys.F14,
  121. # '\x1b[1;2R': Keys.F15, # Conflicts with CPR response.
  122. '\x1b[1;2S': Keys.F16,
  123. '\x1b[15;2~': Keys.F17,
  124. '\x1b[17;2~': Keys.F18,
  125. '\x1b[18;2~': Keys.F19,
  126. '\x1b[19;2~': Keys.F20,
  127. '\x1b[20;2~': Keys.F21,
  128. '\x1b[21;2~': Keys.F22,
  129. '\x1b[23;2~': Keys.F23,
  130. '\x1b[24;2~': Keys.F24,
  131. '\x1b[1;5A': Keys.ControlUp, # Cursor Mode
  132. '\x1b[1;5B': Keys.ControlDown, # Cursor Mode
  133. '\x1b[1;5C': Keys.ControlRight, # Cursor Mode
  134. '\x1b[1;5D': Keys.ControlLeft, # Cursor Mode
  135. '\x1b[1;2A': Keys.ShiftUp,
  136. '\x1b[1;2B': Keys.ShiftDown,
  137. '\x1b[1;2C': Keys.ShiftRight,
  138. '\x1b[1;2D': Keys.ShiftLeft,
  139. # Tmux sends following keystrokes when control+arrow is pressed, but for
  140. # Emacs ansi-term sends the same sequences for normal arrow keys. Consider
  141. # it a normal arrow press, because that's more important.
  142. '\x1bOA': Keys.Up,
  143. '\x1bOB': Keys.Down,
  144. '\x1bOC': Keys.Right,
  145. '\x1bOD': Keys.Left,
  146. '\x1b[5A': Keys.ControlUp,
  147. '\x1b[5B': Keys.ControlDown,
  148. '\x1b[5C': Keys.ControlRight,
  149. '\x1b[5D': Keys.ControlLeft,
  150. '\x1bOc': Keys.ControlRight, # rxvt
  151. '\x1bOd': Keys.ControlLeft, # rxvt
  152. '\x1b[200~': Keys.BracketedPaste, # Start of bracketed paste.
  153. # Meta + arrow keys. Several terminals handle this differently.
  154. # The following sequences are for xterm and gnome-terminal.
  155. # (Iterm sends ESC followed by the normal arrow_up/down/left/right
  156. # sequences, and the OSX Terminal sends ESCb and ESCf for "alt
  157. # arrow_left" and "alt arrow_right." We don't handle these
  158. # explicitely, in here, because would could not distinguesh between
  159. # pressing ESC (to go to Vi navigation mode), followed by just the
  160. # 'b' or 'f' key. These combinations are handled in
  161. # the input processor.)
  162. '\x1b[1;3D': (Keys.Escape, Keys.Left),
  163. '\x1b[1;3C': (Keys.Escape, Keys.Right),
  164. '\x1b[1;3A': (Keys.Escape, Keys.Up),
  165. '\x1b[1;3B': (Keys.Escape, Keys.Down),
  166. # Sequences generated by numpad 5. Not sure what it means. (It doesn't
  167. # appear in 'infocmp'. Just ignore.
  168. '\x1b[E': Keys.Ignore, # Xterm.
  169. '\x1b[G': Keys.Ignore, # Linux console.
  170. }
  171. class _IsPrefixOfLongerMatchCache(dict):
  172. """
  173. Dictiory that maps input sequences to a boolean indicating whether there is
  174. any key that start with this characters.
  175. """
  176. def __missing__(self, prefix):
  177. # (hard coded) If this could be a prefix of a CPR response, return
  178. # True.
  179. if (_cpr_response_prefix_re.match(prefix) or _mouse_event_prefix_re.match(prefix)):
  180. result = True
  181. else:
  182. # If this could be a prefix of anything else, also return True.
  183. result = any(v for k, v in ANSI_SEQUENCES.items() if k.startswith(prefix) and k != prefix)
  184. self[prefix] = result
  185. return result
  186. _IS_PREFIX_OF_LONGER_MATCH_CACHE = _IsPrefixOfLongerMatchCache()
  187. class InputStream(object):
  188. """
  189. Parser for VT100 input stream.
  190. Feed the data through the `feed` method and the correct callbacks of the
  191. `input_processor` will be called.
  192. ::
  193. def callback(key):
  194. pass
  195. i = InputStream(callback)
  196. i.feed('data\x01...')
  197. :attr input_processor: :class:`~prompt_toolkit.key_binding.InputProcessor` instance.
  198. """
  199. # Lookup table of ANSI escape sequences for a VT100 terminal
  200. # Hint: in order to know what sequences your terminal writes to stdin, run
  201. # "od -c" and start typing.
  202. def __init__(self, feed_key_callback):
  203. assert callable(feed_key_callback)
  204. self.feed_key_callback = feed_key_callback
  205. self.reset()
  206. if _DEBUG_RENDERER_INPUT:
  207. self.LOG = open(_DEBUG_RENDERER_INPUT_FILENAME, 'ab')
  208. def reset(self, request=False):
  209. self._in_bracketed_paste = False
  210. self._start_parser()
  211. def _start_parser(self):
  212. """
  213. Start the parser coroutine.
  214. """
  215. self._input_parser = self._input_parser_generator()
  216. self._input_parser.send(None)
  217. def _get_match(self, prefix):
  218. """
  219. Return the key that maps to this prefix.
  220. """
  221. # (hard coded) If we match a CPR response, return Keys.CPRResponse.
  222. # (This one doesn't fit in the ANSI_SEQUENCES, because it contains
  223. # integer variables.)
  224. if _cpr_response_re.match(prefix):
  225. return Keys.CPRResponse
  226. elif _mouse_event_re.match(prefix):
  227. return Keys.Vt100MouseEvent
  228. # Otherwise, use the mappings.
  229. try:
  230. return ANSI_SEQUENCES[prefix]
  231. except KeyError:
  232. return None
  233. def _input_parser_generator(self):
  234. """
  235. Coroutine (state machine) for the input parser.
  236. """
  237. prefix = ''
  238. retry = False
  239. flush = False
  240. while True:
  241. flush = False
  242. if retry:
  243. retry = False
  244. else:
  245. # Get next character.
  246. c = yield
  247. if c == _Flush:
  248. flush = True
  249. else:
  250. prefix += c
  251. # If we have some data, check for matches.
  252. if prefix:
  253. is_prefix_of_longer_match = _IS_PREFIX_OF_LONGER_MATCH_CACHE[prefix]
  254. match = self._get_match(prefix)
  255. # Exact matches found, call handlers..
  256. if (flush or not is_prefix_of_longer_match) and match:
  257. self._call_handler(match, prefix)
  258. prefix = ''
  259. # No exact match found.
  260. elif (flush or not is_prefix_of_longer_match) and not match:
  261. found = False
  262. retry = True
  263. # Loop over the input, try the longest match first and
  264. # shift.
  265. for i in range(len(prefix), 0, -1):
  266. match= self._get_match(prefix[:i])
  267. if match:
  268. self._call_handler(match, prefix[:i])
  269. prefix = prefix[i:]
  270. found = True
  271. if not found:
  272. self._call_handler(prefix[0], prefix[0])
  273. prefix = prefix[1:]
  274. def _call_handler(self, key, insert_text):
  275. """
  276. Callback to handler.
  277. """
  278. if isinstance(key, tuple):
  279. for k in key:
  280. self._call_handler(k, insert_text)
  281. else:
  282. if key == Keys.BracketedPaste:
  283. self._in_bracketed_paste = True
  284. self._paste_buffer = ''
  285. else:
  286. self.feed_key_callback(KeyPress(key, insert_text))
  287. def feed(self, data):
  288. """
  289. Feed the input stream.
  290. :param data: Input string (unicode).
  291. """
  292. assert isinstance(data, six.text_type)
  293. if _DEBUG_RENDERER_INPUT:
  294. self.LOG.write(repr(data).encode('utf-8') + b'\n')
  295. self.LOG.flush()
  296. # Handle bracketed paste. (We bypass the parser that matches all other
  297. # key presses and keep reading input until we see the end mark.)
  298. # This is much faster then parsing character by character.
  299. if self._in_bracketed_paste:
  300. self._paste_buffer += data
  301. end_mark = '\x1b[201~'
  302. if end_mark in self._paste_buffer:
  303. end_index = self._paste_buffer.index(end_mark)
  304. # Feed content to key bindings.
  305. paste_content = self._paste_buffer[:end_index]
  306. self.feed_key_callback(KeyPress(Keys.BracketedPaste, paste_content))
  307. # Quit bracketed paste mode and handle remaining input.
  308. self._in_bracketed_paste = False
  309. remaining = self._paste_buffer[end_index + len(end_mark):]
  310. self._paste_buffer = ''
  311. self.feed(remaining)
  312. # Handle normal input character by character.
  313. else:
  314. for i, c in enumerate(data):
  315. if self._in_bracketed_paste:
  316. # Quit loop and process from this position when the parser
  317. # entered bracketed paste.
  318. self.feed(data[i:])
  319. break
  320. else:
  321. # Replace \r by \n. (Some clients send \r instead of \n
  322. # when enter is pressed. E.g. telnet and some other
  323. # terminals.)
  324. # XXX: We should remove this in a future version. It *is*
  325. # now possible to recognise the difference.
  326. # (We remove ICRNL/INLCR/IGNCR below.)
  327. # However, this breaks IPython and maybe other applications,
  328. # because they bind ControlJ (\n) for handling the Enter key.
  329. # When this is removed, replace Enter=ControlJ by
  330. # Enter=ControlM in keys.py.
  331. if c == '\r':
  332. c = '\n'
  333. self._input_parser.send(c)
  334. def flush(self):
  335. """
  336. Flush the buffer of the input stream.
  337. This will allow us to handle the escape key (or maybe meta) sooner.
  338. The input received by the escape key is actually the same as the first
  339. characters of e.g. Arrow-Up, so without knowing what follows the escape
  340. sequence, we don't know whether escape has been pressed, or whether
  341. it's something else. This flush function should be called after a
  342. timeout, and processes everything that's still in the buffer as-is, so
  343. without assuming any characters will folow.
  344. """
  345. self._input_parser.send(_Flush)
  346. def feed_and_flush(self, data):
  347. """
  348. Wrapper around ``feed`` and ``flush``.
  349. """
  350. self.feed(data)
  351. self.flush()
  352. class raw_mode(object):
  353. """
  354. ::
  355. with raw_mode(stdin):
  356. ''' the pseudo-terminal stdin is now used in raw mode '''
  357. We ignore errors when executing `tcgetattr` fails.
  358. """
  359. # There are several reasons for ignoring errors:
  360. # 1. To avoid the "Inappropriate ioctl for device" crash if somebody would
  361. # execute this code (In a Python REPL, for instance):
  362. #
  363. # import os; f = open(os.devnull); os.dup2(f.fileno(), 0)
  364. #
  365. # The result is that the eventloop will stop correctly, because it has
  366. # to logic to quit when stdin is closed. However, we should not fail at
  367. # this point. See:
  368. # https://github.com/jonathanslenders/python-prompt-toolkit/pull/393
  369. # https://github.com/jonathanslenders/python-prompt-toolkit/issues/392
  370. # 2. Related, when stdin is an SSH pipe, and no full terminal was allocated.
  371. # See: https://github.com/jonathanslenders/python-prompt-toolkit/pull/165
  372. def __init__(self, fileno):
  373. self.fileno = fileno
  374. try:
  375. self.attrs_before = termios.tcgetattr(fileno)
  376. except termios.error:
  377. # Ignore attribute errors.
  378. self.attrs_before = None
  379. def __enter__(self):
  380. # NOTE: On os X systems, using pty.setraw() fails. Therefor we are using this:
  381. try:
  382. newattr = termios.tcgetattr(self.fileno)
  383. except termios.error:
  384. pass
  385. else:
  386. newattr[tty.LFLAG] = self._patch_lflag(newattr[tty.LFLAG])
  387. newattr[tty.IFLAG] = self._patch_iflag(newattr[tty.IFLAG])
  388. # VMIN defines the number of characters read at a time in
  389. # non-canonical mode. It seems to default to 1 on Linux, but on
  390. # Solaris and derived operating systems it defaults to 4. (This is
  391. # because the VMIN slot is the same as the VEOF slot, which
  392. # defaults to ASCII EOT = Ctrl-D = 4.)
  393. newattr[tty.CC][termios.VMIN] = 1
  394. termios.tcsetattr(self.fileno, termios.TCSANOW, newattr)
  395. # Put the terminal in cursor mode. (Instead of application mode.)
  396. os.write(self.fileno, b'\x1b[?1l')
  397. @classmethod
  398. def _patch_lflag(cls, attrs):
  399. return attrs & ~(termios.ECHO | termios.ICANON | termios.IEXTEN | termios.ISIG)
  400. @classmethod
  401. def _patch_iflag(cls, attrs):
  402. return attrs & ~(
  403. # Disable XON/XOFF flow control on output and input.
  404. # (Don't capture Ctrl-S and Ctrl-Q.)
  405. # Like executing: "stty -ixon."
  406. termios.IXON | termios.IXOFF |
  407. # Don't translate carriage return into newline on input.
  408. termios.ICRNL | termios.INLCR | termios.IGNCR
  409. )
  410. def __exit__(self, *a, **kw):
  411. if self.attrs_before is not None:
  412. try:
  413. termios.tcsetattr(self.fileno, termios.TCSANOW, self.attrs_before)
  414. except termios.error:
  415. pass
  416. # # Put the terminal in application mode.
  417. # self._stdout.write('\x1b[?1h')
  418. class cooked_mode(raw_mode):
  419. """
  420. The opposide of ``raw_mode``, used when we need cooked mode inside a
  421. `raw_mode` block. Used in `CommandLineInterface.run_in_terminal`.::
  422. with cooked_mode(stdin):
  423. ''' the pseudo-terminal stdin is now used in cooked mode. '''
  424. """
  425. @classmethod
  426. def _patch_lflag(cls, attrs):
  427. return attrs | (termios.ECHO | termios.ICANON | termios.IEXTEN | termios.ISIG)
  428. @classmethod
  429. def _patch_iflag(cls, attrs):
  430. # Turn the ICRNL flag back on. (Without this, calling `input()` in
  431. # run_in_terminal doesn't work and displays ^M instead. Ptpython
  432. # evaluates commands using `run_in_terminal`, so it's important that
  433. # they translate ^M back into ^J.)
  434. return attrs | termios.ICRNL