Bsoft  1.9
Bernard's software package
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
AppMain.tcl
Go to the documentation of this file.
1 #!/bin/sh
2 # the next line restarts using wish \
3 exec wish "$0" "$@"
4 
5 # brun
6 #
7 # Script to run Bsoft programs from an interactive window
8 # Bernard Heymann 19990822-20030527
9 
10 catch { exec which bimg} result
11 set bsoftbin [file dirname $result]
12 
13 eval destroy [winfo child .]
14 wm title . "Bsoft program interface"
15 set font {Helvetica 14}
16 set bigfont {Helvetica 24}
17 set smallfont {Helvetica 10}
18 
19 #----------------------------------------------------------------
20 # The path to the source code and libraries
21 #----------------------------------------------------------------
22 set Bsoft [exec printenv BSOFT]
23 if { [string length $Bsoft] < 2 } {
24  set Bsoft /usr/local/bsoft
25 }
26 set bshow_script $Bsoft/tcltk
27 set bshow_lib $Bsoft/lib
28 
29 #----------------------------------------------------------------
30 # Loading procedures in other script files
31 #----------------------------------------------------------------
32 source $bshow_script/show_info.tcl
33 
34 # execute the current command
35 
36 proc executecommand { } {
37  set thecmd [.cmd get]
38  set p [lindex $thecmd 0]
39  if [ file executable $p] {
40  puts "Executing: $thecmd"
41  catch "exec $thecmd" result
42 # puts $result
43  showInfo $result
44  }
45 }
46 
47 # getfile
48 # Edits a file name entry
49 #
50 # Arguments:
51 # i the file number
52 
53 proc getfile { i } {
54  set name [tk_getOpenFile]
55  if { $name > " " } {
56  .file.$i.name delete 0 end
57  .file.$i.name insert 0 $name
58  }
59 }
60 
61 # constructcmd
62 # Constructs the command line from a list containing the program name,
63 # the options, and the file names
64 
65 proc constructcmd { } {
66  set cmd [lindex [.cmd get] 0]
67  foreach opt [.opt.list cget -text] {
68  upvar [.opt.$opt.label cget -variable] on
69  if { $on == 1 } {
70  append cmd " " $opt " " [.opt.$opt.e1 get]
71  }
72  }
73  foreach i [.file.list cget -text] {
74  upvar [.file.$i.label cget -variable] on
75  if { $on == 1 } {
76  append cmd " " [.file.$i.name get]
77  }
78  }
79  .cmd delete 0 end
80  .cmd insert 0 $cmd
81 }
82 
83 # Select the program and get the options from the program
84 #
85 # Argument:
86 # p program name
87 
88 proc select_program { p } {
89  global font
90  global smallfont
91  destroy .usage
92  destroy .file
93  destroy .opt
94  frame .file
95  frame .opt
96  .cmd delete 0 end
97 # set p [ string range $p [expr 1 + [string last "\/" $p]] [string length $p] ]
98  if { [ string first "brun" $p] > -1 } { return}
99  .cmd insert 0 $p
100  if ![ file executable [lindex $p 0]] { return}
101 
102 # set fd [open $p r]
103 # set id [read $fd 2]
104 # close $fd
105 # if [ string match "#!" $id ] { return }
106 
107  text .t -relief sunken -bd 2 -setgrid 1 -height 30
108  pack .t -expand yes -fill both
109  catch { exec $p} result
110  .t insert 0.0 $result
111 
112  set ib 0.0
113  set ib [ .t search -nocase "usage" $ib end]
114  set use [ .t get "$ib linestart" "$ib lineend"]
115  label .usage -font $font -justify left -text $use
116  pack .usage -side top -pady 2
117 
118  set f " "
119  set i 1
120  set flist ""
121 # Remove the first two words
122  set use [ string range $use [string first " " $use] [string length $use]]
123  set use [ string trimleft $use]
124  set use [ string range $use [string first " " $use] [string length $use]]
125 # set use [ string trimleft $use ]
126 # Find the options indicator
127  set ind [ string first "\]" $use]
128  if { $ind < 0 } { set ind 0}
129 # Find all file indicators at the end of the line
130  while 1 {
131  set use [ string range $use [expr $ind + 1] [string length $use]]
132  set use [ string trimleft $use]
133  if { $use < " " } { break}
134  set ind [ string first " " $use]
135  if { $ind < 0 } { set ind [ string length $use]}
136  set f [ string range $use 0 [expr $ind-1]]
137  frame .file.$i
138  checkbutton .file.$i.label -text "$f" -variable $f -relief flat \
139  -command "constructcmd"
140  .file.$i.label deselect
141  entry .file.$i.name -width 50
142  button .file.$i.button -text "Browse ..." -command "getfile $i"
143  pack .file.$i.label .file.$i.name .file.$i.button -side left -pady 1 -padx 4
144  pack .file.$i -side top -fill x
145  append flist " " $i
146  incr i
147  }
148  label .file.list -text $flist
149  if { $i > 1 } { pack .file -side top -pady 2}
150 
151 # Find all options defined by starting with an "-"
152  label .opt.head -font $font -justify left -text "Options:"
153  pack .opt.head -side top -fill x -expand 1
154  set olist ""
155  set ib 1.0
156  while 1 {
157  set ib [ .t search -regexp -- {^\-[A-Za-z]} "$ib +2 chars" end]
158  if { $ib == "" } { break}
159  set iv [.t search " " "$ib" "$ib lineend"]
160  set b [.t get $ib $iv]
161  set idesc [.t search " " "$iv +1 chars" "$ib lineend"]
162  set v [.t get "$iv +1 chars" $idesc]
163  set idesc [ .t search -regexp -- {[A-Za-z]} "$idesc +1 chars" "$idesc lineend"]
164  set d [.t get "$idesc wordstart" "$idesc lineend"]
165  frame .opt.$b
166  label .opt.$b.descr -font $smallfont -justify left -text $d
167  entry .opt.$b.e1 -width [string length $v]
168  checkbutton .opt.$b.label -text "$b" -variable $b -relief flat \
169  -command "constructcmd"
170  .opt.$b.label deselect
171  if { $v > " " } {
172  .opt.$b.e1 insert 0 $v
173  pack .opt.$b.label .opt.$b.e1 .opt.$b.descr -side left -pady 1 -padx 4
174  } else {
175  .opt.$b.e1 insert 0 ""
176  pack .opt.$b.label .opt.$b.descr -side left -pady 1 -padx 4
177  }
178  pack .opt.$b -side top -fill x
179  append olist " " $b
180  }
181  label .opt.list -text $olist
182  pack .opt -side left -pady 2
183  destroy .t
184 }
185 
186 # Main script body - initialization
187 entry .cmd -width 100
188 .cmd insert 0 "Select a program and fill in the options"
189 pack .cmd -side top -fill x -expand 1
190 
191 frame .buttons
192 pack .buttons -side top -fill x -pady 2m
193 menubutton .buttons.program -text "Program" -underline 0 -direction below \
194  -menu .buttons.program.m -relief raised -width 20
195 menu .buttons.program.m -tearoff 0
196 set file_list [lsort [glob [file join $bsoftbin/*]]]
197 foreach p [split $file_list] {
198  set prog [file tail $p]
199  if { [file executable $p] && [file isfile $p] } {
200  .buttons.program.m add command -label $prog -command "select_program $p"
201  }
202 }
203 button .buttons.run -text Execute -command executecommand
204 button .buttons.dismiss -text Dismiss -command "destroy ."
205 pack .buttons.program .buttons.run .buttons.dismiss -side left -expand 1
206 
207 frame .file
208 label .file.list -text ""
209 frame .opt
210 label .opt.list -text ""