From 508e8770c73d676d05df8dfab5f3c564ba15cf99 Mon Sep 17 00:00:00 2001 From: Oleg Hahm Date: Thu, 3 Apr 2014 16:14:38 +0200 Subject: [PATCH] pyterm: fix program exit procedure, disable SIGINT --- dist/tools/pyterm/pyterm.py | 34 +++++++++++++++++----------------- 1 file changed, 17 insertions(+), 17 deletions(-) diff --git a/dist/tools/pyterm/pyterm.py b/dist/tools/pyterm/pyterm.py index c23000c99c..961169555d 100755 --- a/dist/tools/pyterm/pyterm.py +++ b/dist/tools/pyterm/pyterm.py @@ -8,7 +8,7 @@ except ImportError: from twisted.internet import reactor from twisted.internet.protocol import Protocol, ReconnectingClientFactory -import cmd, serial, sys, threading, readline, time, logging, os, argparse, re, codecs +import cmd, serial, sys, threading, readline, time, logging, os, argparse, re, codecs, signal ### set some default options defaultport = "/dev/ttyUSB0" @@ -93,10 +93,11 @@ class SerCmd(cmd.Cmd): self.ser.setDTR(1) self.ser.setDTR(0) - def do_PYTERM_exit(self, line): + def do_PYTERM_exit(self, line, unused=None): + print("Exiting Pyterm") readline.write_history_file() if reactor.running: - reactor.stop() + reactor.callFromThread(reactor.stop) return True def do_PYTERM_save(self, line): @@ -252,7 +253,7 @@ class SerCmd(cmd.Cmd): class PytermProt(Protocol): def dataReceived(self, data): - stdout.write(data) + sys.stdout.write(data) def sendMessage(self, msg): self.transport.write("%d#%s\n" % (len(msg), msg)) @@ -281,6 +282,9 @@ class PytermClientFactory(ReconnectingClientFactory): ReconnectingClientFactory.clientConnectionFailed(self, connector, reason) +def __stop_reactor(signum, stackframe): + sys.stderr.write("Ctrl-C is disabled, type '/exit' instead\n") + if __name__ == "__main__": parser = argparse.ArgumentParser(description="Pyterm - The Python terminal program") @@ -305,16 +309,12 @@ if __name__ == "__main__": myshell = SerCmd(args.port, args.baudrate, args.directory, args.config) myshell.prompt = '' - try: - if args.server and args.tcp_port: - myfactory = PytermClientFactory() - reactor.connectTCP(args.server, args.tcp_port, myfactory) - myshell.factory = myfactory - reactor.callInThread(myshell.cmdloop, "Welcome to pyterm!\nType 'exit' to exit.") - reactor.run() - sys.exit(0) - else: - myshell.cmdloop("Welcome to pyterm!\nType 'exit' to exit.") - sys.exit(0) - except KeyboardInterrupt: - myshell.do_PYTERM_exit(0) + if args.server and args.tcp_port: + myfactory = PytermClientFactory() + reactor.connectTCP(args.server, args.tcp_port, myfactory) + myshell.factory = myfactory + reactor.callInThread(myshell.cmdloop, "Welcome to pyterm!\nType '/exit' to exit.") + signal.signal(signal.SIGINT, __stop_reactor) + reactor.run() + else: + myshell.cmdloop("Welcome to pyterm!\nType 'exit' to exit.")