11#!/usr/bin/env python3
22# -*- coding: utf-8 -*-
3- # Copyright (C) 2009, 2013-2017 Rocky Bernstein
3+ # Copyright (C) 2009, 2013-2017, 2021 Rocky Bernstein
44#
55# This program is free software; you can redistribute it and/or modify
66# it under the terms of the GNU General Public License as published by
2424from trepan .interfaces import comcodes as Mcomcodes
2525
2626from optparse import OptionParser
27- from trepan .version import VERSION
27+ from trepan .version import __version__
2828
2929
3030def process_options (pkg_version , sys_argv , option_list = None ):
@@ -34,61 +34,89 @@ def process_options(pkg_version, sys_argv, option_list=None):
3434
3535 The options dicionary from opt_parser is return. sys_argv is
3636 also updated."""
37- usage_str = """%prog [debugger-options]]
37+ usage_str = """%prog [debugger-options]]
3838
3939 Client connection to an out-of-process trepan3k debugger session"""
4040
4141 # serverChoices = ('TCP','FIFO', None) # we use PID for now.
4242
43- optparser = OptionParser (usage = usage_str , option_list = option_list ,
44- version = "%%prog version %s" % pkg_version )
45-
46- optparser .add_option ("-H" , "--host" , dest = "host" , default = '127.0.0.1' ,
47- action = "store" , type = 'string' , metavar = 'IP-OR-HOST' ,
48- help = "connect IP or host name." )
49- optparser .add_option ("-P" , "--port" , dest = "port" , default = 1027 ,
50- action = "store" , type = 'int' , metavar = 'NUMBER' ,
51- help = "Use TCP port number NUMBER for "
52- "out-of-process connections." )
53- optparser .add_option ("--pid" , dest = "pid" , default = 0 ,
54- action = "store" , type = 'int' , metavar = 'NUMBER' ,
55- help = "Use PID to get FIFO names for "
56- "out-of-process connections." )
43+ optparser = OptionParser (
44+ usage = usage_str ,
45+ option_list = option_list ,
46+ version = "%%prog version %s" % pkg_version ,
47+ )
48+
49+ optparser .add_option (
50+ "-H" ,
51+ "--host" ,
52+ dest = "host" ,
53+ default = "127.0.0.1" ,
54+ action = "store" ,
55+ type = "string" ,
56+ metavar = "IP-OR-HOST" ,
57+ help = "connect IP or host name." ,
58+ )
59+ optparser .add_option (
60+ "-P" ,
61+ "--port" ,
62+ dest = "port" ,
63+ default = 1027 ,
64+ action = "store" ,
65+ type = "int" ,
66+ metavar = "NUMBER" ,
67+ help = "Use TCP port number NUMBER for " "out-of-process connections." ,
68+ )
69+ optparser .add_option (
70+ "--pid" ,
71+ dest = "pid" ,
72+ default = 0 ,
73+ action = "store" ,
74+ type = "int" ,
75+ metavar = "NUMBER" ,
76+ help = "Use PID to get FIFO names for " "out-of-process connections." ,
77+ )
5778
5879 optparser .disable_interspersed_args ()
5980
6081 sys .argv = list (sys_argv )
6182 (opts , sys .argv ) = optparser .parse_args ()
6283 return opts , sys .argv
6384
85+
6486#
6587# Connects to a debugger in server mode
6688#
6789
6890# DEFAULT_CLIENT_CONNECTION_OPTS = {'open': True, 'IO': 'FIFO'}
69- DEFAULT_CLIENT_CONNECTION_OPTS = {'open' : True , 'IO' : 'TCP' ,
70- 'HOST' : '127.0.0.1' , 'PORT' : 1027 }
91+ DEFAULT_CLIENT_CONNECTION_OPTS = {
92+ "open" : True ,
93+ "IO" : "TCP" ,
94+ "HOST" : "127.0.0.1" ,
95+ "PORT" : 1027 ,
96+ }
97+
98+
7199def start_client (connection_opts ):
72100 intf = Mclient .ClientInterface (connection_opts = connection_opts )
73101 # debugger.interface.append(intf)
74102 intf .msg ("Connected." )
75- done = False
103+ done = False
76104 while not done :
77105 control , remote_msg = intf .read_remote ()
78106 # print 'c, r', control, remote_msg
79107 if Mcomcodes .PRINT == control :
80- print (remote_msg , end = ' ' )
108+ print (remote_msg , end = " " )
81109 pass
82110 elif control in [Mcomcodes .CONFIRM_TRUE , Mcomcodes .CONFIRM_FALSE ]:
83- default = ( Mcomcodes .CONFIRM_TRUE == control )
84- if intf .confirm (remote_msg .rstrip (' \n ' ), default ):
85- msg = 'Y'
111+ default = Mcomcodes .CONFIRM_TRUE == control
112+ if intf .confirm (remote_msg .rstrip (" \n " ), default ):
113+ msg = "Y"
86114 else :
87- msg = 'N'
115+ msg = "N"
88116 pass
89117 intf .write_remote (Mcomcodes .CONFIRM_REPLY , msg )
90118 elif Mcomcodes .PROMPT == control :
91- msg = intf .read_command (' (Trepan*) ' ).strip ()
119+ msg = intf .read_command (" (Trepan*) " ).strip ()
92120 intf .write_remote (Mcomcodes .CONFIRM_REPLY , msg )
93121 elif Mcomcodes .QUIT == control :
94122 print ("trepan3kc: That's all, folks..." )
@@ -97,42 +125,41 @@ def start_client(connection_opts):
97125 elif Mcomcodes .RESTART == control :
98126 # FIXME need to save stuff like port # and
99127 # and for FIFO we need new pid.
100- if ' TCP' == connection_opts ['IO' ]:
101- print (' Restarting...' )
128+ if " TCP" == connection_opts ["IO" ]:
129+ print (" Restarting..." )
102130 intf .inout .close ()
103131 time .sleep (1 )
104132 intf .inout .open ()
105133 else :
106134 print ("Don't know how to hard-restart FIFO..." )
107- done = True
135+ done = True
108136 pass
109137 break
110138 else :
111139 print ("!! Weird status code received '%s'" % control )
112- print (remote_msg , end = ' ' )
140+ print (remote_msg , end = " " )
113141 pass
114142 pass
115143 intf .close ()
116144 return
117145
118146
119147def run (opts , sys_argv ):
120- if hasattr (opts , ' pid' ) and opts .pid > 0 :
121- remote_opts = {' open' : opts .pid , 'IO' : ' FIFO' }
148+ if hasattr (opts , " pid" ) and opts .pid > 0 :
149+ remote_opts = {" open" : opts .pid , "IO" : " FIFO" }
122150 else :
123- remote_opts = {'open' : True , 'IO' : 'TCP' , 'PORT' : opts .port ,
124- 'HOST' : opts .host }
151+ remote_opts = {"open" : True , "IO" : "TCP" , "PORT" : opts .port , "HOST" : opts .host }
125152 start_client (remote_opts )
126153 return
127154
128155
129156def main ():
130- opts , sys_argv = process_options (VERSION , sys .argv )
157+ opts , sys_argv = process_options (__version__ , sys .argv )
131158 # print(opts)
132159 run (opts , sys_argv )
133160 return
134161
135162
136- if __name__ == ' __main__' :
163+ if __name__ == " __main__" :
137164 main ()
138165 pass
0 commit comments