2021-02-05 09:30:43 +00:00
|
|
|
#!/usr/bin/env python3
|
2021-01-14 18:56:45 +00:00
|
|
|
|
|
|
|
import atexit, serial, os, struct, code, traceback, readline, rlcompleter
|
|
|
|
from proxy import *
|
|
|
|
import __main__
|
|
|
|
import builtins
|
2021-05-01 06:11:23 +00:00
|
|
|
from proxyutils import *
|
2021-05-01 06:13:29 +00:00
|
|
|
from utils import *
|
2021-05-04 10:35:19 +00:00
|
|
|
import sysreg
|
2021-01-14 18:56:45 +00:00
|
|
|
|
|
|
|
class HistoryConsole(code.InteractiveConsole):
|
|
|
|
def __init__(self, locals=None, filename="<console>",
|
|
|
|
histfile=os.path.expanduser("~/.m1n1-history")):
|
|
|
|
code.InteractiveConsole.__init__(self, locals, filename)
|
2021-05-04 10:35:19 +00:00
|
|
|
self.histfile = histfile
|
2021-01-14 18:56:45 +00:00
|
|
|
self.init_history(histfile)
|
|
|
|
|
|
|
|
def init_history(self, histfile):
|
|
|
|
readline.parse_and_bind("tab: complete")
|
|
|
|
if hasattr(readline, "read_history_file"):
|
|
|
|
try:
|
|
|
|
readline.read_history_file(histfile)
|
|
|
|
except FileNotFoundError:
|
|
|
|
pass
|
|
|
|
|
2021-05-04 10:35:19 +00:00
|
|
|
def save_history(self):
|
|
|
|
readline.set_history_length(10000)
|
|
|
|
readline.write_history_file(self.histfile)
|
2021-01-14 18:56:45 +00:00
|
|
|
|
|
|
|
def showtraceback(self):
|
|
|
|
type, value, tb = sys.exc_info()
|
|
|
|
traceback.print_exception(type, value, tb)
|
|
|
|
|
2021-05-04 10:35:19 +00:00
|
|
|
class ExitConsole(SystemExit):
|
|
|
|
pass
|
2021-01-14 18:56:45 +00:00
|
|
|
|
2021-05-04 10:35:19 +00:00
|
|
|
def run_shell(locals, msg=None, exitmsg=None):
|
|
|
|
saved_display = sys.displayhook
|
|
|
|
try:
|
|
|
|
def display(val):
|
|
|
|
try:
|
|
|
|
global mon
|
|
|
|
mon.poll()
|
|
|
|
except NameError:
|
|
|
|
pass
|
|
|
|
if isinstance(val, int):
|
|
|
|
builtins._ = val
|
|
|
|
print(hex(val))
|
|
|
|
elif callable(val):
|
|
|
|
val()
|
|
|
|
else:
|
|
|
|
saved_display(val)
|
|
|
|
|
|
|
|
sys.displayhook = display
|
2021-01-14 18:56:45 +00:00
|
|
|
|
2021-05-04 10:35:19 +00:00
|
|
|
# convenience
|
|
|
|
locals["h"] = hex
|
|
|
|
locals["sysreg"] = sysreg
|
2021-01-14 18:56:45 +00:00
|
|
|
|
2021-05-04 10:35:19 +00:00
|
|
|
if "proxy" in locals and "p" not in locals:
|
|
|
|
locals["p"] = locals["proxy"]
|
|
|
|
if "utils" in locals and "u" not in locals:
|
|
|
|
locals["u"] = locals["utils"]
|
2021-01-14 18:56:45 +00:00
|
|
|
|
2021-05-12 12:21:27 +00:00
|
|
|
for obj in ("iface", "p", "u"):
|
2021-05-04 10:35:19 +00:00
|
|
|
if obj in locals:
|
|
|
|
for attr in dir(locals[obj]):
|
|
|
|
if attr not in locals:
|
2021-05-12 12:21:27 +00:00
|
|
|
v = getattr(locals[obj], attr)
|
|
|
|
if callable(v):
|
|
|
|
locals[attr] = v
|
|
|
|
|
|
|
|
for attr in dir(sysreg):
|
|
|
|
locals[attr] = getattr(sysreg, attr)
|
2021-01-14 18:56:45 +00:00
|
|
|
|
2021-05-04 10:35:19 +00:00
|
|
|
try:
|
|
|
|
con = HistoryConsole(locals)
|
|
|
|
con.interact(msg, exitmsg)
|
|
|
|
except ExitConsole as e:
|
|
|
|
if len(e.args):
|
|
|
|
return e.args[0]
|
|
|
|
else:
|
|
|
|
return
|
|
|
|
finally:
|
|
|
|
con.save_history()
|
2021-01-14 18:56:45 +00:00
|
|
|
|
2021-05-04 10:35:19 +00:00
|
|
|
finally:
|
|
|
|
sys.displayhook = saved_display
|
2021-01-14 18:56:45 +00:00
|
|
|
|
2021-05-04 10:35:19 +00:00
|
|
|
if __name__ == "__main__":
|
|
|
|
from setup import *
|
|
|
|
locals = __main__.__dict__
|
2021-01-14 18:56:45 +00:00
|
|
|
|
2021-05-04 10:35:19 +00:00
|
|
|
from tgtypes import *
|
2021-01-14 18:56:45 +00:00
|
|
|
|
2021-05-04 10:35:19 +00:00
|
|
|
run_shell(locals, msg="Have fun!")
|