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

Subversion Repositories w11

[/] [w11/] [tags/] [w11a_V0.6/] [tools/] [bin/] [ti_rri] - Blame information for rev 24

Details | Compare with Previous | View Log

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

powered by: WebSVN 2.1.0

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