Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sqlmapproject
GitHub Repository: sqlmapproject/sqlmap
Path: blob/master/lib/utils/getch.py
2989 views
1
#!/usr/bin/env python
2
3
"""
4
Copyright (c) 2006-2025 sqlmap developers (https://sqlmap.org)
5
See the file 'LICENSE' for copying permission
6
"""
7
8
class _Getch(object):
9
"""
10
Gets a single character from standard input. Does not echo to
11
the screen (reference: http://code.activestate.com/recipes/134892/)
12
"""
13
def __init__(self):
14
try:
15
self.impl = _GetchWindows()
16
except ImportError:
17
try:
18
self.impl = _GetchMacCarbon()
19
except (AttributeError, ImportError):
20
self.impl = _GetchUnix()
21
22
def __call__(self):
23
return self.impl()
24
25
class _GetchUnix(object):
26
def __init__(self):
27
__import__("tty")
28
29
def __call__(self):
30
import sys
31
import termios
32
import tty
33
34
fd = sys.stdin.fileno()
35
old_settings = termios.tcgetattr(fd)
36
try:
37
tty.setraw(sys.stdin.fileno())
38
ch = sys.stdin.read(1)
39
finally:
40
termios.tcsetattr(fd, termios.TCSADRAIN, old_settings)
41
return ch
42
43
class _GetchWindows(object):
44
def __init__(self):
45
__import__("msvcrt")
46
47
def __call__(self):
48
import msvcrt
49
return msvcrt.getch()
50
51
class _GetchMacCarbon(object):
52
"""
53
A function which returns the current ASCII key that is down;
54
if no ASCII key is down, the null string is returned. The
55
page http://www.mactech.com/macintosh-c/chap02-1.html was
56
very helpful in figuring out how to do this.
57
"""
58
def __init__(self):
59
import Carbon
60
61
getattr(Carbon, "Evt") # see if it has this (in Unix, it doesn't)
62
63
def __call__(self):
64
import Carbon
65
66
if Carbon.Evt.EventAvail(0x0008)[0] == 0: # 0x0008 is the keyDownMask
67
return ''
68
else:
69
#
70
# The event contains the following info:
71
# (what,msg,when,where,mod)=Carbon.Evt.GetNextEvent(0x0008)[1]
72
#
73
# The message (msg) contains the ASCII char which is
74
# extracted with the 0x000000FF charCodeMask; this
75
# number is converted to an ASCII character with chr() and
76
# returned
77
#
78
(what, msg, when, where, mod) = Carbon.Evt.GetNextEvent(0x0008)[1]
79
return chr(msg & 0x000000FF)
80
81
getch = _Getch()
82
83