Skip to content

Commit 10d062a

Browse files
committed
Updated the cli files.
Fixed an issue with io buffer sizing.
1 parent 13ea650 commit 10d062a

File tree

8 files changed

+114
-66
lines changed

8 files changed

+114
-66
lines changed

cli.py

+22-14
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,22 @@
1313
Notes:
1414
1515
Menu Tuple Format:
16-
(name, descr, submenu) - submenu
17-
(name, descr, leaf) - leaf command with generic <cr> help
18-
(name, descr, leaf, help) - leaf command with specific help
16+
(name, submenu, description) - submenu
17+
(name, leaf) - leaf command with generic <cr> help
18+
(name, leaf, help) - leaf command with specific argument help
1919
2020
Help Format:
21-
(parm, descr)
21+
(parm, descr)
22+
23+
Leaf Functions:
24+
25+
def leaf_function(ui, args):
26+
.....
27+
28+
ui: the ui object passed by the application to cli()
29+
args: the argument list from the command line
30+
31+
The general help for a leaf function is the docstring for that function.
2232
"""
2333
#-----------------------------------------------------------------------------
2434

@@ -45,8 +55,6 @@
4555
('<index>', 'recall history entry <index>'),
4656
)
4757

48-
help_fmt = ' %-20s: %s\n'
49-
5058
#-----------------------------------------------------------------------------
5159

5260
def split_index(s):
@@ -111,16 +119,15 @@ def display_function_help(self, help_info):
111119
"""display function help"""
112120
s = []
113121
for (parm, descr) in help_info:
114-
if parm is None:
115-
s.append(['', ''])
116-
elif descr is None:
117-
s.append([parm, ''])
118-
else:
119-
s.append([parm, ': %s' % descr])
120-
self.ui.put('%s\n' % util.display_cols(s, [16, 0]))
122+
p_str = (parm, '')[parm is None]
123+
d_fmt = (': %s', ' %s')[parm is None]
124+
d_str = (d_fmt % descr, '')[descr is None]
125+
s.append([' ', p_str, d_str])
126+
self.ui.put('%s\n' % util.display_cols(s, [0, 16, 0]))
121127

122128
def command_help(self, cmd, menu):
123129
"""display help results for a command at a menu level"""
130+
s = []
124131
for item in menu:
125132
name = item[0]
126133
if name.startswith(cmd):
@@ -130,7 +137,8 @@ def command_help(self, cmd, menu):
130137
else:
131138
# command: docstring is the help
132139
descr = item[1].__doc__
133-
self.ui.put(help_fmt % (name, descr))
140+
s.append([' ', name, ': %s' % descr])
141+
self.ui.put('%s\n' % util.display_cols(s, [0, 16, 0]))
134142

135143
def function_help(self, item):
136144
"""display help for a leaf function"""

iobuf.py

+40
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,14 @@ def wr32(self, data):
6767
self.emit16(data & 0xffff)
6868
self.emit16(data >> 16)
6969

70+
def has_rd(self, n):
71+
"""no read supported"""
72+
return False
73+
74+
def has_wr(self, n):
75+
"""wr32 supported"""
76+
return n == 32
77+
7078
#-----------------------------------------------------------------------------
7179

7280
class write_file(object):
@@ -101,6 +109,14 @@ def wr8(self, val):
101109
self.n += 1
102110
self.progress.update(self.n)
103111

112+
def has_rd(self, n):
113+
"""no read supported"""
114+
return False
115+
116+
def has_wr(self, n):
117+
"""wr8/16/32 supported"""
118+
return n == 32 or n == 16 or n == 8
119+
104120
#-----------------------------------------------------------------------------
105121

106122
class read_file(object):
@@ -153,6 +169,14 @@ def rd8(self):
153169
self.progress.update(self.n)
154170
return struct.unpack('B', val)[0]
155171

172+
def has_rd(self, n):
173+
"""rd8/16/32 supported"""
174+
return n == 32 or n == 16 or n == 8
175+
176+
def has_wr(self, n):
177+
"""no write supported"""
178+
return False
179+
156180
#-----------------------------------------------------------------------------
157181

158182
class verify_file(object):
@@ -190,6 +214,14 @@ def wr32(self, val):
190214
self.n += 4
191215
self.progress.update(self.n)
192216

217+
def has_rd(self, n):
218+
"""no read supported"""
219+
return False
220+
221+
def has_wr(self, n):
222+
"""wr32 supported"""
223+
return n == 32
224+
193225
#-----------------------------------------------------------------------------
194226

195227
class data_buffer(object):
@@ -250,6 +282,14 @@ def wr8(self, val):
250282
assert self.width == 8
251283
self.write(val)
252284

285+
def has_rd(self, n):
286+
"""rdN supported"""
287+
return n == self.width
288+
289+
def has_wr(self, n):
290+
"""wrN supported"""
291+
return n == self.width
292+
253293
def convert8(self, mode):
254294
"""convert the buffer to 8 bit values"""
255295
if self.width == 32:

jlink.py

+19-24
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
# ----------------------------------------------------------------------------
22

33
import sys
4-
import time
54
import os
65
import ctypes
76
import struct
@@ -23,9 +22,9 @@
2322
# map register names to jlink register numbers
2423

2524
regmap = {
26-
'r0':0,'r1':1,'r2':2,'r3':3,'r4':4,'r5':5,'r6':6,'r7':7,
27-
'r8':8,'r9':9,'r10':10,'r11':11,'r12':12,'r13':13,'r14':14,'r15':15,
28-
'lr':14,'pc':15,'psr':16,'msp':13,
25+
'r0':0, 'r1':1, 'r2':2, 'r3':3, 'r4':4, 'r5':5, 'r6':6, 'r7':7,
26+
'r8':8, 'r9':9, 'r10':10, 'r11':11, 'r12':12, 'r13':13, 'r14':14, 'r15':15,
27+
'lr':14, 'pc':15, 'psr':16, 'msp':13,
2928
# psp
3029
# primask
3130
# faultmask
@@ -75,8 +74,7 @@ def locate_library(libname, paths=sys.path, loader=None):
7574
if os.path.exists(library):
7675
sys.stderr.write('using %r\n' % library)
7776
return loader.LoadLibrary(library), library
78-
else:
79-
raise IOError('%s not found' % libname)
77+
raise IOError('%s not found' % libname)
8078

8179
def get_jlink_dll():
8280
# what kind of system am I?
@@ -90,8 +88,7 @@ def get_jlink_dll():
9088
raise Exception(repr(platform.architecture()))
9189

9290
# start with the script path
93-
search_path = [os.path.join(os.path.dirname(
94-
os.path.realpath(__file__)), libpath)]
91+
search_path = [os.path.join(os.path.dirname(os.path.realpath(__file__)), libpath)]
9592
search_path += sys.path[:] # copy sys.path list
9693

9794
# if environment variable is set, insert this path first
@@ -106,18 +103,16 @@ def get_jlink_dll():
106103
if sys.platform == 'win32':
107104
jlink, backend_info = locate_library('jlinkarm.dll', search_path)
108105
elif sys.platform == 'linux2':
109-
jlink, backend_info = locate_library(
110-
'libjlinkarm.so.5', search_path, ctypes.cdll)
106+
jlink, backend_info = locate_library('libjlinkarm.so.5', search_path, ctypes.cdll)
111107
elif sys.platform == 'darwin':
112-
jlink, backend_info = locate_library(
113-
'libjlinkarm.so.5.dylib', search_path, ctypes.cdll)
108+
jlink, backend_info = locate_library('libjlinkarm.so.5.dylib', search_path, ctypes.cdll)
114109
return jlink, backend_info
115110

116111
# ----------------------------------------------------------------------------
117112

118113
class JLink(object):
119114

120-
def __init__(self, idx = 0):
115+
def __init__(self, idx=0):
121116
self.usb_idx = idx
122117
# load the library
123118
self.jl, self.jlink_lib_name = get_jlink_dll()
@@ -291,7 +286,7 @@ def wrreg(self, reg, val):
291286
# void JLINKARM_WriteReg(int reg, uint32_t val);
292287
fn = self.jl.JLINKARM_WriteReg
293288
fn.restype = None
294-
fn.argtypes = [c_int,c_uint32]
289+
fn.argtypes = [c_int, c_uint32]
295290
fn(c_int(reg), c_uint32(val))
296291

297292
def cp15_is_present(self):
@@ -305,7 +300,7 @@ def rd_cp15(self, crn, op1, crm, op2):
305300
# int JLINKARM_CP15_ReadEx(uint32_t crn, uint32_t crm, uint32_t op1, uint32_t op2, uint32_t *val)
306301
fn = self.jl.JLINKARM_CP15_ReadEx
307302
fn.restype = c_int
308-
fn.argtypes = [c_uint32,c_uint32,c_uint32,c_uint32,c_void_p]
303+
fn.argtypes = [c_uint32, c_uint32, c_uint32, c_uint32, c_void_p]
309304
val = c_uint32()
310305
rc = fn(c_uint32(crn), c_uint32(crm), c_uint32(op1), c_uint32(op2), ctypes.byref(val))
311306
assert rc == 0, 'JLINKARM_CP15_ReadEx returned %d' % rc
@@ -315,7 +310,7 @@ def wr_cp15(self, crn, op1, crm, op2, val):
315310
# int JLINKARM_CP15_WriteEx(uint32_t crn, uint32_t crm, uint32_t op1, uint32_t op2, uint32_t val)
316311
fn = self.jl.JLINKARM_CP15_WriteEx
317312
fn.restype = c_int
318-
fn.argtypes = [c_uint32,c_uint32,c_uint32,c_uint32,c_uint32]
313+
fn.argtypes = [c_uint32, c_uint32, c_uint32, c_uint32, c_uint32]
319314
rc = fn(c_uint32(crn), c_uint32(crm), c_uint32(op1), c_uint32(op2), c_uint32(val))
320315
assert rc == 0, 'JLINKARM_CP15_WriteEx returned %d' % rc
321316

@@ -428,7 +423,7 @@ def __str__(self):
428423
class dbgio(object):
429424
"""JLink implementation of dbgio cpu interface"""
430425

431-
def __init__(self, idx = 0):
426+
def __init__(self, idx=0):
432427
"""no actual operations, record the selected usb device"""
433428
self.usb_idx = idx
434429
self.menu = (
@@ -446,7 +441,7 @@ def connect(self, cpu_name, itf):
446441
# setup the jlink interface
447442
self.jlink.exec_command('device=%s' % cpu_names[cpu_name])
448443
self.jlink.set_speed(4000)
449-
itf = {'swd':_JLINKARM_TIF_SWD,'jtag':_JLINKARM_TIF_JTAG}[itf]
444+
itf = {'swd':_JLINKARM_TIF_SWD, 'jtag':_JLINKARM_TIF_JTAG}[itf]
450445
self.jlink.tif_select(itf)
451446
self.jlink.jlink_connect()
452447

@@ -517,11 +512,11 @@ def rdmem8(self, adr, n, io):
517512

518513
def rdmem(self, adr, n, io):
519514
"""read a buffer from memory starting at adr"""
520-
if io.width == 32:
515+
if io.has_wr(32):
521516
self.rdmem32(adr, n, io)
522-
elif io.width == 16:
517+
elif io.has_wr(16):
523518
self.rdmem16(adr, n, io)
524-
elif io.width == 8:
519+
elif io.has_wr(8):
525520
self.rdmem8(adr, n, io)
526521
else:
527522
assert False, 'bad buffer width'
@@ -540,11 +535,11 @@ def wrmem8(self, adr, n, io):
540535

541536
def wrmem(self, adr, n, io):
542537
"""write a buffer to memory starting at adr"""
543-
if io.width == 32:
538+
if io.has_rd(32):
544539
self.wrmem32(adr, n, io)
545-
elif io.width == 16:
540+
elif io.has_rd(16):
546541
self.wrmem16(adr, n, io)
547-
elif io.width == 8:
542+
elif io.has_rd(8):
548543
self.wrmem8(adr, n, io)
549544
else:
550545
assert False, 'bad buffer width'

linenoise.py

+13-10
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@
6464

6565
def _getc(fd, timeout=-1):
6666
"""
67-
read a single character from a file (with timeout)
67+
read a single character string from a file (with timeout)
6868
timeout = 0 : return immediately
6969
timeout < 0 : wait for the character (block)
7070
timeout > 0 : wait for timeout seconds
@@ -78,7 +78,10 @@ def _getc(fd, timeout=-1):
7878
c = os.read(fd, 1)
7979
if c == '':
8080
return _KEY_NULL
81-
return c
81+
return c.decode('utf8')
82+
83+
def _puts(fd, s):
84+
return os.write(fd, s.encode('utf8'))
8285

8386
def would_block(fd, timeout):
8487
"""if fd is not readable within timeout seconds - return True"""
@@ -93,7 +96,7 @@ def would_block(fd, timeout):
9396
def get_cursor_position(ifd, ofd):
9497
"""Get the horizontal cursor position"""
9598
# query the cursor location
96-
if os.write(ofd, '\x1b[6n') != 4:
99+
if _puts(ofd, '\x1b[6n') != 4:
97100
return -1
98101
# read the response: ESC [ rows ; cols R
99102
# rows/cols are decimal number strings
@@ -131,14 +134,14 @@ def get_columns(ifd, ofd):
131134
if start < 0:
132135
return _DEFAULT_COLS
133136
# Go to right margin and get position
134-
if os.write(ofd, '\x1b[999C') != 6:
137+
if _puts(ofd, '\x1b[999C') != 6:
135138
return _DEFAULT_COLS
136139
cols = get_cursor_position(ifd, ofd)
137140
if cols < 0:
138141
return _DEFAULT_COLS
139142
# restore the position
140143
if cols > start:
141-
os.write(ofd, '\x1b[%dD' % (cols - start))
144+
_puts(ofd, '\x1b[%dD' % (cols - start))
142145
return cols
143146

144147
# -----------------------------------------------------------------------------
@@ -201,7 +204,7 @@ def refresh_show_hints(self):
201204
if bold and color < 0:
202205
color = 37
203206
if color >= 0 or bold:
204-
seq.append('\033[%d;%d;49m' % ((0,1)[bold], color))
207+
seq.append('\033[%d;%d;49m' % ((0, 1)[bold], color))
205208
seq.append(hint[:hlen])
206209
if color >= 0 or bold:
207210
seq.append('\033[0m')
@@ -234,7 +237,7 @@ def refresh_singleline(self):
234237
# Move cursor to original position
235238
seq.append('\r\x1b[%dC' % (plen + pos))
236239
# write it out
237-
os.write(self.ofd, ''.join(seq))
240+
_puts(self.ofd, ''.join(seq))
238241

239242
def refresh_multiline(self):
240243
"""multiline refresh"""
@@ -253,7 +256,7 @@ def refresh_multiline(self):
253256
logging.debug('go down %d' % (old_rows - rpos))
254257
seq.append('\x1b[%dB' % (old_rows - rpos))
255258
# Now for every row clear it, go up.
256-
for j in xrange(old_rows-1):
259+
for j in range(int(old_rows-1)):
257260
logging.debug('clear+up')
258261
seq.append('\r\x1b[0K\x1b[1A')
259262
# Clear the top line.
@@ -290,7 +293,7 @@ def refresh_multiline(self):
290293
logging.debug('\n')
291294
self.oldpos = self.pos
292295
# write it out
293-
os.write(self.ofd, ''.join(seq))
296+
_puts(self.ofd, ''.join(seq))
294297

295298
def refresh_line(self):
296299
"""refresh the edit line"""
@@ -397,7 +400,7 @@ def complete_line(self):
397400
idx = 0
398401
while not stop:
399402
if idx < len(lc):
400-
# show the completion
403+
# save the line buffer
401404
saved_buf = self.buf
402405
saved_pos = self.pos
403406
# show the completion

pycs

+3
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,9 @@ class user_interface(object):
159159
def put(self, s):
160160
sys.stdout.write(s)
161161

162+
def flush(self):
163+
sys.stdout.flush()
164+
162165
def poll(self):
163166
# poll the input during an operation
164167
# use a short timeout so we don't peg the cpu

0 commit comments

Comments
 (0)