How to bind a widget to a Virtuell Event - events

I want so register multiple widgets to one virtual event. This is the idea of the code. But I do not find out, how I can send down from the toplevel "." to any widgets, where the virtual event is registered.
1 #!/bin/sh
2 # \
3 exec tclsh "$0" "$#"
4 package require Tk
5 proc send { } {
6 puts "send event"
7 event generate ***HERE IS MY PROBLEM *** <<TEST>>
8 }
9 label .l1 -text label1
10 label .l2 -text label2
11 button .b1 -text event -command send
12 bind .l1 <<TEST>> {puts "here 1"}
13 bind .l2 <<TEST>> {puts "here 2"}
14 grid .l1
15 grid .l2
16 grid .b1
Found this solution:
7 event generate . <<TEST>>
...
14 set tags [bindtags .]
15 bindtags . [list {*}$tags .l1 .l2]
Will be there a house keeping necessary after "destroy .l1" ?

Related

How to display both a form and a checklist using dialog tool

Hi I am trying to generate a form in dialog tool that includes both a form and a checklist.
If I put this in a file named form.txt:
--title "New Data"
--form "Data File" 10 100 3
"Name: " 1 1 "" 1 25 40 40
"Reg. Number: " 2 1 "" 2 25 40 40
"ID: " 3 1 "" 3 25 40 40
--checklist "Foo/Bar" 10 100 2
1 "Is foo" OFF
2 "Is bar" OFF
And I invoke dialog using:
dialog --file form.txt
I get both forms sequentially displayed, is there any possibility to display both at the same time (the form above the checklist).
I am using FreeBSD dialog, in case that it is relevant -which I presume it is not-.

How to Add Page Numbers Opposite The Bound Edge with Postscript / Ghostscript

I have a 2,000+ page PDF that I need to add page numbers to. The PDF is already setup for binding. Meaning there is extra margin on right and lefthand pages to accommodate the binding. I found the following solution from Brian M. Hunt, but it always places the page number in the same position. I know I can modify the, "sub 20 sub 20" value to change the position, but how do I make the position conditional so that righthand pages have the number near the right edge and lefthand pages have the number near the left edge?
gs \
-dBATCH -dNOPAUSE \
-sDEVICE=pdfwrite -dPDFSETTINGS=/prepress \
-sOutputFile=/path/to/merged.pdf \
-c 'globaldict /MyPageCount 1 put << /EndPage {exch pop 0 eq dup {/Helvetica 12 selectfont MyPageCount =string cvs dup stringwidth pop currentpagedevice /PageSize get 0 get exch sub 20 sub 20 moveto show globaldict /MyPageCount MyPageCount 1 add put } if } bind >> setpagedevice'
-f input1.pdf -f input2.pdf
You already have MyPageCount, so you know the page number. Using that you can determine if the page count is odd or even. Then you just select different positional logic.
Something like :
MyPageCount 2 mod 1 eq
{
%%insert odd numbered page positioning
}{
%%insert even numbered page positioning
} ifelse
[add simplified program]
gs -q -sDEVICE=pdfwrite -dBATCH -dNOPAUSE -dPDFX -sOutputFile=step_4.pdf \
-c 'globaldict /MyPageCount 1 put
<<
/EndPage {
exch pop 0 eq dup {
/Helvetica 12 selectfont MyPageCount 256 string cvs dup stringwidth pop currentpagedevice
/PageSize get 0 get exch sub
MyPageCount 2 mod 1 eq {
60
}
{
505
} ifelse
sub 36 moveto
show globaldict
/MyPageCount MyPageCount 1 add put
} if
} bind
>>
setpagedevice' \
-f "test_in.pdf"
Note that =string is unique to Ghostscript (ie won't work on any other PostScript interpreter) and pulls a pre-defined 256 character string from userdict. There are dangers to using this that I doubt you realise, because its a subtlety of PostScript. I have instead replaced it with the portable (ie standard PostScript) construction 256 string which has the same effect for your purposes, but creates a scratch string that can't be reused unexpectedly, unlike =string.
You have set -dPDFX (to create a PDF/X-3 compliant file) but you haven't supplied a PDF/X definition file, this means that the resulting file may not be PDF/X compliant. The process is documented here
Thanks to KenS's suggestion I was able to get the page numbers added with the following script.
gs -q -sDEVICE=pdfwrite -dBATCH -dNOPAUSE -dPDFX -sOutputFile=step_4.pdf \
-c 'globaldict /MyPageCount 1 put
<<
/EndPage {
MyPageCount 2 mod 1 eq
{
exch pop 0 eq dup {
/Helvetica 12 selectfont MyPageCount =string cvs dup stringwidth pop currentpagedevice
/PageSize get 0 get exch sub 60 sub 36 moveto show globaldict
/MyPageCount MyPageCount 1 add put
} if
}{
exch pop 0 eq dup {
/Helvetica 12 selectfont MyPageCount =string cvs dup stringwidth pop currentpagedevice
/PageSize get 0 get exch sub 505 sub 36 moveto show globaldict
/MyPageCount MyPageCount 1 add put
} if
} ifelse
} bind
>>
setpagedevice' \
-f "test_in.pdf"

Bash. Return two function levels (two nested calls)

I need to know if Bash has some solution for my case. I need after some conditions to do a "double return". I mean, to perform a return of a function and also return the parent function to skip the rest of the code of that parent function.
I know that I can do a conditional using function return values to achieve this. But I'd like to know if in Bash exist something like "break 2" for functions. I don't want if possible to modify the code of the parent function because as you can imagine, in my real script there are dozens of functions and I don't want to modify all of them.
Example:
#!/bin/bash
function sublevelone() {
echo "sublevelone"
# Return 2, or break 2 or something :D
}
function main() {
sublevelone
echo "This is the part of the code to being avoid executed"
}
main
I don't know what the bash experts will think, but this works at least for simple cases:
multireturn(){
[ -n "$1" ] && poplevel="$1"
if [ "$poplevel" -ge 0 ]; then
trap multireturn DEBUG
shopt -s extdebug
(( poplevel-- ))
return 2
else
shopt -u extdebug
trap - DEBUG
return 0
fi
}
This makes use of the DEBUG trap and the extdebug flag:
extdebug
If set at shell invocation, arrange to execute the
debugger profile before the shell starts, identical to
the --debugger option. If set after invocation, behav-
ior intended for use by debuggers is enabled:
1. The -F option to the declare builtin displays the
source file name and line number corresponding to
each function name supplied as an argument.
2. If the command run by the DEBUG trap returns a
non-zero value, the next command is skipped and
not executed.
3. If the command run by the DEBUG trap returns a
value of 2, and the shell is executing in a sub-
routine (a shell function or a shell script exe-
cuted by the . or source builtins), the shell
simulates a call to return.
4. BASH_ARGC and BASH_ARGV are updated as described
in their descriptions above.
5. Function tracing is enabled: command substitu-
tion, shell functions, and subshells invoked with
( command ) inherit the DEBUG and RETURN traps.
6. Error tracing is enabled: command substitution,
shell functions, and subshells invoked with (
command ) inherit the ERR trap.
Example usage:
#!/bin/bash
multireturn(){
[ -n "$1" ] && poplevel="$1"
if [ "$poplevel" -ge 0 ]; then
trap multireturn DEBUG
shopt -s extdebug
(( poplevel-- ))
return 2
else
shopt -u extdebug
trap - DEBUG
return 0
fi
}
# define 8 levels of function calls
# (level N prints output, calls level N+1, then prints more output)
for i in $(seq 1 8); do
eval \
'level'$i'(){
echo -n " '$i'"
level'$((i+1))'
echo -n "('$i')"
}'
done
# final level calls multireturn
level9(){
echo -n " 9"
multireturn $n
echo -n "(9)"
}
# test various skip amounts
for i in $(seq 0 10); do
echo -n "$i:"
n=$i
level1
echo .
done
echo
echo done
Result:
0: 1 2 3 4 5 6 7 8 9(9)(8)(7)(6)(5)(4)(3)(2)(1).
1: 1 2 3 4 5 6 7 8 9(8)(7)(6)(5)(4)(3)(2)(1).
2: 1 2 3 4 5 6 7 8 9(7)(6)(5)(4)(3)(2)(1).
3: 1 2 3 4 5 6 7 8 9(6)(5)(4)(3)(2)(1).
4: 1 2 3 4 5 6 7 8 9(5)(4)(3)(2)(1).
5: 1 2 3 4 5 6 7 8 9(4)(3)(2)(1).
6: 1 2 3 4 5 6 7 8 9(3)(2)(1).
7: 1 2 3 4 5 6 7 8 9(2)(1).
8: 1 2 3 4 5 6 7 8 9(1).
9: 1 2 3 4 5 6 7 8 9.
10: 1 2 3 4 5 6 7 8 9
done
This is kinda whacky but if you use parentheses to define levelone, it will execute the function in a subshell and then you can exit out of that shell from an inner function. That said, I think it's more appropriate to use return to send back value that you check for in the parent function.
#!/bin/bash
function leveltwo() {
echo "two"
exit
}
function levelone() (
echo "one"
leveltwo
echo "three"
)
levelone
echo "four"
Will print:
one
two
four
Why don't you return an exit status from the function and add an if-statement to main like you normally would in any other programming/scripting language?
#!/bin/bash
function sublevelone() {
echo "sublevelone"
[ 0 -eq 1 ]
return $? # Returns 1 in this case because 0 != 1
}
function main() {
sublevelone
[ $? -eq 0 ] || return 1 # Return in case of error
echo "This is the part of the code to being avoid executed"
}
main
exit 0
Just a short version of #jhnc answer for exactly 2-level return from a function:
trap 'trap "shopt -u extdebug; trap - DEBUG; return 0" DEBUG; return 2' DEBUG
shopt -s extdebug
return

expect_background affecting other expect's behavior

I'm writting an expect script to automate some tests. To detect when a general error occurs (which can happen at any phase of the test), instead of repeating the same pattern (for instance, "Rebooting system") on each expect command, I decided to use expect_background for that kind of condition.
What I understand about the expect_background command is that it is checked against all input text before the currently active expect. So, if both expect_background and expect are waiting for the same pattern, I expect (with the pun, please ;-) ) both actions to be triggered. For instance:
#!/usr/bin/expect -f
spawn ./counter.sh
expect_background {
"5" { puts "expect-bg"; }
}
expect {
"9" { puts "expect-9"; }
}
expect {
"20" { puts "expect-20"; }
}
The ./counter.sh script counts from 1 to 20 with intervals of 1 second. According to the behavior I described above, "expect-bg" should be printed at 5 and 15, expect-9 at 9, and "expect-20" at 20. But here is what I get:
$ ./expect_bg.expect
spawn ./counter.sh
1
2
3
4
5
expect-bg
6
7
8
9
10
11
12
13
14
15
expect-bg
16
17
18
19
20
Why is that happening? And how to I get the behavior described?
I can produce your desired output with expect_before and exp_continue
#!/usr/bin/expect -f
spawn sh counter.sh
expect_before {
5 {puts "expect_before"; exp_continue}
}
expect {
9 {puts "exp_9"}
}
expect {
20 {puts "exp_20"}
}
$ expect counter.exp
spawn sh counter.sh
1
2
3
4
5
expect_before
6
7
8
9
exp_9
10
11
12
13
14
15
expect_before
16
17
18
19
20
exp_20
When you have
expect_before {
5 {puts "expect_before"; exp_continue}
}
expect {
9 {puts "exp_9"}
}
you effectively have this:
expect {
5 {puts "expect_before"; exp_continue}
9 {puts "exp_9"}
}
If you omit the exp_continue, that expect statement ends with the first 5, so you never get a chance to look for 9.

How to split a variable in two args with exec in TCL?

I want to create a simple Console in Tcl/Tk
I have two problems. First changing every * with a [glob *] but also, when my entry contains "ls -a" it doesn't understand that ls is the command and -a the first arg.
How can I manage to do that ?
Thanks
proc execute {} {
# ajoute le contenu de .add_frame.add_entry
set value [.add_frame.add_entry get]
if {[string compare "$value" ""] == 1} {
.text insert end "\n\n% $value\n"
.text insert end [exec $value]
.add_frame.add_entry delete 0 end
}
}
frame .add_frame
label .add_frame.add_label -text "Nouvel élément : "
entry .add_frame.add_entry
button .add_frame.add_button -text "Executer" -command execute
button .add_frame.exit_button -text "Quitter" -command exit
bind .add_frame.add_entry <Return> execute
bind .add_frame.add_entry <KP_Enter> execute
bind . <Escape> exit
bind . <Control-q> exit
pack .add_frame.add_label -side left
pack .add_frame.exit_button -side right
pack .add_frame.add_button -side right
pack .add_frame.add_entry -fill x -expand true
pack .add_frame -side top -fill x
text .text
.text insert end "% Tcl/Tk Console"
pack .text -side bottom -fill both -expand true
The simple answer in Tcl 8.5 is to use this:
exec {*}$value
In 8.4 and before, that syntax didn't exist. That meant that many people wrote this:
eval exec $value
But in reality, the safe version was one of these:
eval exec [lrange $value 0 end]
eval [linsert $value 0 exec]
Of course, if the $value is coming straight from the user then you're better off using a system shell to evaluate it since more users expect that sort of syntax:
exec /usr/bin/bash -c $value

Resources