Skip to content

Commit

Permalink
Fix ugly raw_input()-related stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
Arusekk committed Dec 6, 2019
1 parent f719329 commit fcc9642
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 29 deletions.
6 changes: 6 additions & 0 deletions docs/source/term.rst
Expand Up @@ -3,3 +3,9 @@

.. automodule:: pwnlib.term
:members:

Term Modules
-------------------

.. toctree::
term/readline
81 changes: 53 additions & 28 deletions pwnlib/term/readline.py
Expand Up @@ -3,6 +3,9 @@
from __future__ import absolute_import
from __future__ import division

import six
import sys

from pwnlib.term import keyconsts as kc
from pwnlib.term import keymap as km
from pwnlib.term import term
Expand Down Expand Up @@ -364,13 +367,18 @@ def go_end(*_):
'<any>' : handle_keypress,
})

def readline(_size = None, prompt = '', float = True, priority = 10):
def readline(_size=None, prompt='', float=True, priority=10):
# The argument _size is unused, but is there for compatibility
# with the existing readline

global buffer_handle, prompt_handle, suggest_handle, eof, \
show_suggestions

# XXX circular imports
from pwnlib.term import term_mode
if not term_mode:
print(prompt, end='', flush=True)
return sys.stdin.readline().rstrip('\n')
show_suggestions = False
eof = False
if prompt:
Expand Down Expand Up @@ -413,7 +421,46 @@ def readline(_size = None, prompt = '', float = True, priority = 10):
if shutdown_hook:
shutdown_hook()

def raw_input(prompt='', float=True):
r"""raw_input(prompt='', float=True)
Replacement for the built-in ``raw_input`` using ``pwnlib`` readline
implementation.
Arguments:
prompt(str): The prompt to show to the user.
float(bool): If set to `True`, prompt and input will float to the
bottom of the screen when `term.term_mode` is enabled.
"""
return readline(None, prompt, float)

def eval_input(prompt='', float=True):
"""eval_input(prompt='', float=True)
Replacement for the built-in python 2 - style ``input`` using
``pwnlib`` readline implementation, and `pwnlib.util.safeeval.expr`
instead of ``eval`` (!).
Arguments:
prompt(str): The prompt to show to the user.
float(bool): If set to ``True``, prompt and input will float to the
bottom of the screen when `term.term_mode` is enabled.
Example:
>>> try:
... saved_stdin = sys.stdin
... sys.stdin = io.StringIO("{'a':20}")
... eval_input("Favorite object? ")['a']
... finally:
... sys.stdin = saved_stdin
Favorite object? 20
"""
from pwnlib.util import safeeval
return safeeval.const(readline(None, prompt, float))

def init():
global safeeval
# defer imports until initialization
import sys
from six.moves import builtins
Expand All @@ -428,30 +475,8 @@ def __getattr__(self, k):
return self._fd.__getattribute__(k)
sys.stdin = Wrapper(sys.stdin)

def raw_input(prompt = '', float = True):
"""raw_input(prompt = '', float = True)
Replacement for the built-in `raw_input` using ``pwnlib``s readline
implementation.
Arguments:
prompt(str): The prompt to show to the user.
float(bool): If set to `True`, prompt and input will float to the
bottom of the screen when `term.term_mode` is enabled.
"""
return readline(None, prompt, float)
builtins.raw_input = raw_input

def input(prompt = '', float = True):
"""input(prompt = '', float = True)
Replacement for the built-in `input` using ``pwnlib``s readline
implementation, and `pwnlib.util.safeeval.expr` instead of `eval` (!).
Arguments:
prompt(str): The prompt to show to the user.
float(bool): If set to `True`, prompt and input will float to the
bottom of the screen when `term.term_mode` is enabled.
"""
return safeeval.const(readline(None, prompt, float))
builtins.input = input
if six.PY2:
builtins.raw_input = raw_input
builtins.input = eval_input
else:
builtins.input = raw_input
2 changes: 1 addition & 1 deletion pwnlib/term/term.py
Expand Up @@ -487,7 +487,7 @@ def output(s = '', float = False, priority = 10, frozen = False,
else:
is_floating = False
i = len(cells) - 1
while cells[i].float and i > 0:
while i > 0 and cells[i].float:
i -= 1
# put('xx %d\n' % i)
cell = Cell()
Expand Down
1 change: 1 addition & 0 deletions pwnlib/ui.py
Expand Up @@ -7,6 +7,7 @@

from pwnlib import term
from pwnlib.log import getLogger
from pwnlib.term.readline import raw_input

log = getLogger(__name__)

Expand Down

0 comments on commit fcc9642

Please sign in to comment.