OpenCores
URL https://opencores.org/ocsvn/w11/w11/trunk

Subversion Repositories w11

[/] [w11/] [tags/] [w11a_V0.7/] [tools/] [bin/] [ti_rri] - Blame information for rev 29

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 27 wfjm
#! /usr/bin/env tclshcpp
2 10 wfjm
# -*- tcl -*-
3 29 wfjm
# $Id: ti_rri 631 2015-01-09 21:36:51Z mueller $
4 10 wfjm
#
5 29 wfjm
# Copyright 2011-2015 by Walter F.J. Mueller 
6 10 wfjm
#
7
# This program is free software; you may redistribute and/or modify it under
8
# the terms of the GNU General Public License as published by the Free
9
# Software Foundation, either version 2, or at your option any later version.
10
#
11
# This program is distributed in the hope that it will be useful, but
12
# WITHOUT ANY WARRANTY, without even the implied warranty of MERCHANTABILITY
13
# or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14
# for complete details.
15
#
16
#  Revision History:
17
# Date         Rev Version  Comment
18 29 wfjm
# 2015-01-09   631   1.2.1  use rlc get/set rather config
19 27 wfjm
# 2014-11-07   601   1.2    use tclshcpp (C++ based) rather tclsh
20 22 wfjm
# 2013-05-19   521   1.1.6  setup proper interactive handling; add --run reap
21 20 wfjm
# 2013-04-26   510   1.1.5  reorganize readline startup
22 19 wfjm
# 2013-04-12   504   1.1.4  add --pack; trailing '-' argv implies --int
23
# 2013-02-05   482   1.1.3  stop server is rls found
24
# 2013-01-27   478   1.1.2  use 'exec sh -c $cmd &' for --run implementation
25 17 wfjm
# 2013-01-02   467   1.1.1  call rlc close only when really open
26
# 2012-12-27   465   1.1    add --cuff support
27
# 2012-02-09   457   1.0.4  disable autoexec
28 16 wfjm
# 2011-12-19   440   1.0.3  re-organize option handling for --term and --fifo
29 15 wfjm
# 2011-12-04   435   1.0.2  add flow attribute to --term
30 12 wfjm
# 2011-04-22   379   1.0.1  check for RETROBASE; proper exit handling; help text
31 11 wfjm
# 2011-04-17   376   1.0    Initial version
32 10 wfjm
# 2011-03-19   371   0.1    First draft
33
#
34
#
35 19 wfjm
# --pack=pname,...
36 16 wfjm
# --fifo[=name,opts,...]
37
# --term[=name,baud,opts,...]
38 17 wfjm
# --cuff[=name,...]
39 10 wfjm
# --run=command
40
# --log=filename      ; default "-"
41
# --logl=n            ; default 2
42
# --dmpl=n            ; default 0
43
# --tiol=n            ; default 0
44
# --int
45
# --help
46
# --
47
#   tcl cmds
48
#   @...tcl
49
#
50
 
51 20 wfjm
set tirri_interactive 0
52
 
53 10 wfjm
array set opts {
54 19 wfjm
    pack_  ""
55 10 wfjm
    fifo   0
56
    fifo_  ""
57
    term   0
58
    term_  ""
59 17 wfjm
    cuff   0
60
    cuff_  ""
61 10 wfjm
    run_   ""
62
    log_   "-"
63
    logl_  2
64
    dmpl_  0
65
    tiol_  0
66
    int    0
67
    help   0
68
}
69
 
70
set clist {}
71
set optsendseen 0
72 12 wfjm
set runpid {}
73 10 wfjm
 
74 17 wfjm
# disable autoexec
75
set auto_noexec 1
76
 
77 12 wfjm
#
78
# cleanup handler
79
#   must be in a proc so that it can be called from tclreadline
80
#   must be defined before ::tclreadline::Loop called (all after ignored...)
81
#
82 20 wfjm
proc tirri_exit {{doexit 1}} {
83 12 wfjm
  global opts
84
  global runpid
85
 
86 19 wfjm
  # check for rlink server, stop it
87
  if { [info commands rls] eq "rls" } { rls server -stop }
88
 
89 12 wfjm
  # now close rlink connection
90 17 wfjm
  if { $opts(fifo) || $opts(term) || $opts(cuff) } {
91
    if { [rlc open] ne "" } { rlc close }
92 12 wfjm
  }
93
 
94
  # FIXME_code: should sync here with -run process run-down
95
  #             but no wait available in tcl (grr...)
96 22 wfjm
  if { "$runpid" ne "" } {
97 12 wfjm
    after 100;                          # currently just wait 100ms
98 22 wfjm
    rutil::waitpid $runpid
99 12 wfjm
  }
100 20 wfjm
  if { $doexit } {
101
    puts {};                            # \n to ensure shell prompt on new line
102
    exit
103
  }
104 12 wfjm
  return
105
}
106
 
107 10 wfjm
foreach arg $argv {
108
  if { $optsendseen } {
109
    lappend clist $arg
110
    continue
111
  }
112
  switch -regexp -- $arg {
113 19 wfjm
    ^--?pack=.+$  { regexp -- {=(.*)} $arg dummy opts(pack_) }
114 10 wfjm
    ^--?fifo=?.*$ { set opts(fifo) 1; regexp -- {=(.*)} $arg dummy opts(fifo_) }
115
    ^--?term=?.*$ { set opts(term) 1; regexp -- {=(.*)} $arg dummy opts(term_) }
116 17 wfjm
    ^--?cuff=?.*$ { set opts(cuff) 1; regexp -- {=(.*)} $arg dummy opts(cuff_) }
117 10 wfjm
    ^--?run=.+$   { regexp -- {=(.*)} $arg dummy opts(run_) }
118
    ^--?log=.+$   { regexp -- {=(.*)} $arg dummy opts(log_) }
119
    ^--?logl=.+$  { regexp -- {=(.*)} $arg dummy opts(logl_) }
120
    ^--?dmpl=.+$  { regexp -- {=(.*)} $arg dummy opts(dmpl_) }
121
    ^--?tiol=.+$  { regexp -- {=(.*)} $arg dummy opts(tiol_) }
122
    ^--?int$      { set opts(int) 1 }
123
    ^--?help$     { set opts(help) 1 }
124
    ^--$          { set optsendseen 1 }
125
    ^--.+$        { puts "-E: bad option $arg, see --help for proper usage"
126
                    return 1
127
                  }
128
    default       { lappend clist $arg }
129
  }
130
}
131
 
132 19 wfjm
# check whether last element in clist is plain '-'
133
if { [llength clist] } {
134
  if { [lindex $clist end] eq "-" } {
135
    set opts(int) 1
136
    set clist [lrange $clist 0 end-1]
137
  }
138
}
139
 
140 10 wfjm
if { $opts(help) } {
141 12 wfjm
  # use {} as defimiter here to avoid that escaping of all []
142
  puts {usage: ti_rri [OPTION]... [COMMAND]...}
143
  puts {}
144
  puts {Options:}
145 19 wfjm
  puts {  --pack=PLIST   load, with package require, additional packages}
146
  puts {                   PLIST is comma separated list of package names}
147 12 wfjm
  puts {  --run=CMD      exec's CMD as subprocess before the rlink port opened}
148
  puts {                 useful to start test benches, usually via 'tbw'}
149
  puts {  --fifo[=ARGS]  open fifo type rlink port. Optional arguments are:}
150 17 wfjm
  puts {                   --fifo=[NAME[,OPTS]]}
151 12 wfjm
  puts {  --term[=ARGS]  open term type rlink port. Optional arguments are:}
152 16 wfjm
  puts {                   --term=[NAME[,BAUD[,OPTS]]]}
153 17 wfjm
  puts {  --cuff[=ARGS]  open cuff type rlink port. Optional arguments are:}
154
  puts {                   --cuff=[NAME[,OPTS]]}
155 12 wfjm
  puts {  --log=FILE     set log file name. Default is to write to stdout.}
156 22 wfjm
  puts {  --logl=LVL     set log level, default is '2' allowed values 0-3.}
157 12 wfjm
  puts {  --dmpl=LVL     set dump level, default is '0', values like logl}
158 22 wfjm
  puts {  --tiol=LVL     set i/o trace level, default is '0', allowed 0-2.}
159 12 wfjm
  puts {  --int          enter interactive mode even when commands given}
160
  puts {  --help         display this help and exit}
161
  puts {  --             all following arguments are treated as tcl commands}
162
  puts {}
163
  puts {Command handling:}
164
  puts {  For arguments of the form '@.tcl' the respective file is}
165
  puts {  sourced. All other arguments are treated as Tcl commands and executed}
166
  puts {  with eval.}
167
  puts {}
168
  puts {For further details consults the ti_rri man page.}
169 10 wfjm
  return 0
170
}
171
 
172 12 wfjm
if {![info exists env(RETROBASE)]} {
173
  puts "-E: RETROBASE environment variable not defined"
174
  return 1
175
}
176
 
177 19 wfjm
# check consistency of connection open options
178 17 wfjm
set nopen 0;
179
if { $opts(fifo) } { incr nopen }
180
if { $opts(term) } { incr nopen }
181
if { $opts(cuff) } { incr nopen }
182
 
183
if { $nopen > 1 } {
184
  puts "-E: more than one of --fifo,--term,--cuff given, only one allowed"
185 10 wfjm
  return 1
186
}
187
 
188 22 wfjm
# setup auto path
189 10 wfjm
lappend auto_path [file join $env(RETROBASE) tools tcl]
190
lappend auto_path [file join $env(RETROBASE) tools lib]
191
 
192 22 wfjm
# setup default packages
193 10 wfjm
package require rutiltpp
194
package require rlinktpp
195 22 wfjm
package require rlink
196 10 wfjm
 
197 22 wfjm
# setup signal handling
198
rutil::sigaction -init
199
 
200
# setup connect and server objects
201 10 wfjm
rlinkconnect rlc
202 19 wfjm
rlinkserver rls rlc
203 10 wfjm
 
204 19 wfjm
# load additional packages (if -pack given)
205
if { $opts(pack_) ne "" } {
206
  foreach pack [split $opts(pack_) ","] {
207
    package require $pack
208
  }
209
}
210
 
211
 
212 10 wfjm
# setup logging
213
if { $opts(log_) ne "-" } {
214 29 wfjm
  rlc set logfile       $opts(log_)
215 10 wfjm
}
216 29 wfjm
rlc set printlevel $opts(logl_)
217
rlc set dumplevel  $opts(dmpl_)
218
rlc set tracelevel $opts(tiol_)
219 10 wfjm
 
220 12 wfjm
# first start, if specified with --run, the test bench
221 19 wfjm
# exec sh -c $cmd is used to execute a shell command including [], '',""
222
# in a direct exec the tcl expansion logic will interfere...
223
#
224 10 wfjm
if { $opts(run_) ne "" } {
225 19 wfjm
  if { [catch {exec sh -c $opts(run_) &} runpid] } {
226 10 wfjm
    puts "-E: failed to execute \"$opts(run_)\" with error message\n  $runpid"
227
    puts "aborting..."
228
    return 1
229
  }
230
}
231
 
232
# than open the rlink connection
233
# handle --fifo
234
if { $opts(fifo) } {
235
  set nlist [split $opts(fifo_) ","]
236
  set path [lindex $nlist 0]
237
  if {$path eq ""} {set path "rlink_cext_fifo"}
238
  set url "fifo:$path"
239 16 wfjm
  set delim "?"
240
  foreach opt [lrange $nlist 1 end] {
241
    if {$opt  ne ""} {append url "$delim$opt"}
242
    set delim ";"
243
  }
244
  # puts "-I: $url"
245 10 wfjm
  rlc open $url
246
}
247
 
248
# handle --term
249
if { $opts(term) } {
250
  set nlist [split $opts(term_) ","]
251
  set dev  [lindex $nlist 0]
252
  set baud [lindex $nlist 1]
253 11 wfjm
  if {$dev  eq ""} {set dev  "USB0"}
254 10 wfjm
  if {$baud eq ""} {set baud "115k"}
255 16 wfjm
  set url "term:$dev?baud=$baud"
256
  foreach opt [lrange $nlist 2 end] {
257
    if {$opt  ne ""} {append url ";$opt"}
258 11 wfjm
  }
259 15 wfjm
  # puts "-I: $url"
260 10 wfjm
  rlc open $url
261
}
262
 
263 17 wfjm
# handle --cuff
264
if { $opts(cuff) } {
265
  set nlist [split $opts(cuff_) ","]
266
  set path [lindex $nlist 0]
267
  set url "cuff:$path"
268
  set delim "?"
269
  foreach opt [lrange $nlist 1 end] {
270
    if {$opt  ne ""} {append url "$delim$opt"}
271
    set delim ";"
272
  }
273
  # puts "-I: $url"
274
  rlc open $url
275
}
276
 
277 10 wfjm
# setup simulation mode default
278
set rlink::sim_mode [rlink::isfifo]
279
 
280 20 wfjm
# if tclsh runs a script given on the command line or is invoked
281
# like here via a shebang the tcl_interactive is always set to 0
282
# so we have to check whether stdin/stdout is a terminal and set
283
# tcl_interactive accordingly
284
 
285 22 wfjm
set tcl_interactive [rutil::isatty STDIN]
286 20 wfjm
 
287
# determine whether interactive mode, if yes, initialize readline
288 22 wfjm
if {$tcl_interactive && ($opts(int) || [llength $clist] == 0) } {
289 20 wfjm
  set tirri_interactive 1
290
 
291
  package require tclreadline
292
  namespace eval tclreadline {
293
    proc prompt1 {} {
294
      set version [info tclversion]
295
      return "ti_rri > "
296
    }
297
  }
298
  ::tclreadline::readline eofchar {::tirri_exit; puts {}; exit}
299
}
300
 
301
# now execute all commands and scripts given as start-up arguments
302 10 wfjm
foreach cmd $clist {
303 20 wfjm
  # puts "executing: $cmd"
304 10 wfjm
  # handle @filename commands
305
  if { [regexp {^@(.+)} $cmd dummy filename] } {
306
    # handle @file.tcl --> source tcl file
307
    if { [regexp {\.tcl$} $filename] } {
308
      if { [catch {source $filename} errmsg] } {
309
        puts "-E: failed to source file \"$filename\" with error message:"
310
        if {[info exists errorInfo]} {puts $errorInfo} else {puts $errmsg}
311
        puts "aborting..."
312
        break
313
      }
314
    # handle @file.dat ect --> not yet supported
315
    } else {
316
      puts "-E: only tcl supported but $filename found"
317
      puts "aborting..."
318
      break
319
    }
320
 
321
  # handle normal tcl commands --> eval them
322
  } else {
323
    if { [catch {eval $cmd} errmsg] } {
324
      puts "-E: eval of \"$cmd\" failed with error message:"
325
      if {[info exists errorInfo]} {puts $errorInfo} else {puts $errmsg}
326
      puts "aborting..."
327
      break
328
    }
329
  }
330
}
331
 
332 22 wfjm
if { $tcl_interactive && $tirri_interactive } {
333 20 wfjm
  ::tclreadline::Loop
334 12 wfjm
} else {
335 20 wfjm
  tirri_exit 0
336 10 wfjm
}
337
 
338
return 0

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.