Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit a6ab42c

Browse files
committed
new file with getch() method which we'll use for good samaritan feature
1 parent 4bef12a commit a6ab42c

1 file changed

Lines changed: 94 additions & 0 deletions

File tree

lib/utils/getch.py

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
#!/usr/bin/env python
2+
3+
"""
4+
$Id$
5+
6+
This file is part of the sqlmap project, http://sqlmap.sourceforge.net.
7+
8+
sqlmap is free software; you can redistribute it and/or modify it under
9+
the terms of the GNU General Public License as published by the Free
10+
Software Foundation version 2 of the License.
11+
12+
sqlmap is distributed in the hope that it will be useful, but WITHOUT ANY
13+
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
14+
FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
15+
details.
16+
17+
You should have received a copy of the GNU General Public License along
18+
with sqlmap; if not, write to the Free Software Foundation, Inc., 51
19+
Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20+
"""
21+
22+
### Reference: http://code.activestate.com/recipes/134892/
23+
class _Getch:
24+
"""
25+
Gets a single character from standard input. Does not echo to
26+
the screen.
27+
"""
28+
def __init__(self):
29+
try:
30+
self.impl = _GetchWindows()
31+
except ImportError:
32+
try:
33+
self.impl = _GetchMacCarbon()
34+
except(AttributeError, ImportError):
35+
self.impl = _GetchUnix()
36+
37+
def __call__(self): return self.impl()
38+
39+
40+
class _GetchUnix:
41+
def __init__(self):
42+
import tty, sys
43+
44+
def __call__(self):
45+
import sys, tty, termios
46+
fd = sys.stdin.fileno()
47+
old_settings = termios.tcgetattr(fd)
48+
try:
49+
tty.setraw(sys.stdin.fileno())
50+
ch = sys.stdin.read(1)
51+
finally:
52+
termios.tcsetattr(fd, termios.TCSADRAIN, old_settings)
53+
return ch
54+
55+
56+
class _GetchWindows:
57+
def __init__(self):
58+
import msvcrt
59+
60+
def __call__(self):
61+
import msvcrt
62+
return msvcrt.getch()
63+
64+
65+
class _GetchMacCarbon:
66+
"""
67+
A function which returns the current ASCII key that is down;
68+
if no ASCII key is down, the null string is returned. The
69+
page http://www.mactech.com/macintosh-c/chap02-1.html was
70+
very helpful in figuring out how to do this.
71+
"""
72+
def __init__(self):
73+
import Carbon
74+
Carbon.Evt #see if it has this (in Unix, it doesn't)
75+
76+
def __call__(self):
77+
import Carbon
78+
if Carbon.Evt.EventAvail(0x0008)[0]==0: # 0x0008 is the keyDownMask
79+
return ''
80+
else:
81+
#
82+
# The event contains the following info:
83+
# (what,msg,when,where,mod)=Carbon.Evt.GetNextEvent(0x0008)[1]
84+
#
85+
# The message (msg) contains the ASCII char which is
86+
# extracted with the 0x000000FF charCodeMask; this
87+
# number is converted to an ASCII character with chr() and
88+
# returned
89+
#
90+
(what,msg,when,where,mod)=Carbon.Evt.GetNextEvent(0x0008)[1]
91+
return chr(msg & 0x000000FF)
92+
93+
94+
getch = _Getch()

0 commit comments

Comments
 (0)