presenting realtime output from a utility 
Author Message
 presenting realtime output from a utility

Hi,

I have been asked at my place of work to fix up a CD/DVD backup program
that was written at some point using tcl/Tk. The field complained that
there was no indication as to how long the backing up would take and I
have been asked to remedy that. My problem is my limited exposure to
tcl/Tk though I do have book I borrowed from a co-worker called "tcl and
the Tk toolkit".

The program runs a linux util called cdrecord which when it starts to
copy, outputs a line similar to "Track 01:  13 of  23 MB written (fifo
98%)". I think what I want to do is exec the line which runs the
cdrecord utility and some how get its output in a window that only has
enough room for one output line so that the last thing output by
cdrecord is seen and of course when cdrecord finishes the window can be
deleted - unless there is a simpler way to do this as I don't want to do
anything fancy.

Can anyone give me some pointers in order to speed up my completion of
this? I don't need anyone to do my work for me, but references to
commands I can look up in the book would be helpful or anything else
that might speed up the learning curve in order to get this done.

Thanks,
Ben

--
BTW. I can be contacted at Username:newsgroup4.replies.benaltw
Domain:xoxy.net



Sat, 08 Apr 2006 06:55:23 GMT  
 presenting realtime output from a utility

Quote:

> Can anyone give me some pointers in order to speed up my completion of
> this? I don't need anyone to do my work for me, but references to
> commands I can look up in the book would be helpful or anything else
> that might speed up the learning curve in order to get this done.

BTW. this is a snippet of the code I have determined I would need to
modify:

   set burnCmd [list exec /usr/bin/cdrecord -dao -v speed=8 \
      dev=2,0,0 -data  $isoFilePath]
    puts "Burn Command is\n $burnCmd"
    catch {eval $burnCmd} cmdOutput
    puts "$cmdOutput\n"

--
BTW. I can be contacted at Username:newsgroup4.replies.benaltw
Domain:xoxy.net



Sat, 08 Apr 2006 07:00:13 GMT  
 presenting realtime output from a utility

Quote:


>> Can anyone give me some pointers in order to speed up my completion of
>> this? I don't need anyone to do my work for me, but references to
>> commands I can look up in the book would be helpful or anything else
>> that might speed up the learning curve in order to get this done.

> BTW. this is a snippet of the code I have determined I would need to modify:

>   set burnCmd [list exec /usr/bin/cdrecord -dao -v speed=8 \
>      dev=2,0,0 -data  $isoFilePath]
>    puts "Burn Command is\n $burnCmd"
>    catch {eval $burnCmd} cmdOutput
>    puts "$cmdOutput\n"

change it to be :

        set fd [open "|/usr/bin/cdrecord ...." r]

then use
        filevent $fd readable "checkOutput $fd"

this will trigger the proc checkOutput to be called
every time data is rady to be read (if cdrecord puts
out a new line each time then use gets & then stick
this line into a label would be easiest.) In that proc
also check for eof, as that will be the end of your program.

check out http://wiki.tcl.tk for many concrete examples of
this.

Bruce



Sat, 08 Apr 2006 07:52:55 GMT  
 presenting realtime output from a utility

Quote:

> I have been asked at my place of work to fix up a CD/DVD backup
> program that was written at some point using tcl/Tk. The field
> complained that there was no indication as to how long the backing up
> would take and I have been asked to remedy that. [...]
> The program runs a linux util called cdrecord which when it starts to
> copy, outputs a line similar to "Track 01:  13 of  23 MB written (fifo
> 98%)". I think what I want to do is exec the line which runs the
> cdrecord utility and some how get its output in a window that only has
> enough room for one output line so that the last thing output by
> cdrecord is seen [...]

Procedures similar to the ones below work for me in WISH CD-Writer
<http://www.geocities.com/pa_mcclamrock/wishcd.tar.gz>, which displays
output from cdrecord line by line as it becomes available. They're
modified versions of procedures found in "ExecLog" by Brent Welch, an
example program in *Practical Programming in Tcl and Tk*. (The text
widget here, obviously, is called ".textout").

# Procedure to execute command and pipe output for display:

proc Pipe {command} {
        global output
        .textout mark set insert end
        if [catch {open "|$command |& cat"} output] {
                # Do this if you aren't getting a readable line:
                .textout insert insert $output
                .textout insert insert \n
                .textout see insert
                set command ""
        } else {
                # Get readable lines:
                fconfigure $output -buffering none -blocking 0
                fileevent $output readable Log
        }

Quote:
}

# Procedure to display piped output:

proc Log {} {
        global output
        if { [eof $output] } {
                catch {close $output}
                if {[.textout compare insert != "insert linestart"]} {
                        .textout insert insert \n
                }
                .textout insert insert "READY TO RUN\n"
                .textout see insert
                return
        } else {
                set putout [read $output]
                if { [string first "\b" $putout] != -1 } {
                        set putout [string map -nocase { \b "" } $putout]
                }
                .textout insert insert $putout output
                .textout see insert
        }

Quote:
}

David McClamrock


Sat, 08 Apr 2006 17:18:07 GMT  
 presenting realtime output from a utility

Quote:

>I have been asked at my place of work to fix up a CD/DVD backup program
>that was written at some point using tcl/Tk. The field complained that
>there was no indication as to how long the backing up would take and I
>have been asked to remedy that.

This probably won't help you solve the problem, but:

Instead of trying to display an indication of how long the
process will take (a very hard problem to solve), try to
display an indication of how long it's _already_ taken,
along with an indicator of how much has been completed
(a considerably easier problem).

--Joe English

  Sigh.



Sun, 09 Apr 2006 01:14:16 GMT  
 presenting realtime output from a utility

Quote:

> Procedures similar to the ones below work for me in WISH CD-Writer
> <http://www.geocities.com/pa_mcclamrock/wishcd.tar.gz>, which displays
> output from cdrecord line by line as it becomes available. They're
> modified versions of procedures found in "ExecLog" by Brent Welch, an
> example program in *Practical Programming in Tcl and Tk*. (The text
> widget here, obviously, is called ".textout").

Hi. I appreciate you posting that code. I tried it out without any
modifications and as you would suspect it choked on the .textout. I need
to learn to crawl before walking. I have to look into the fileevent
command. In the meanwhile (I only just started on this again now), I was
messing with the following code I wrote (below) to just get something
going without a gui. The problem is that it works fine on the command
line, but not in a script:

#! /usr/bin/wish -f
set f1 [open {| /usr/bin/cdrecord -dummy .....} r]

set s1 [gets $f1]
while { $s1 != "" } {
     sleep 1
     set s1 [gets $f1]
     puts $s1

Quote:
}

On the command line it works fine and in a script I get:
     Error in startup script: invalid command name "sleep"

Why should it know what sleep is on the command line but not when run
from a script? I was thinking if I got this working it would then be a
simple matter of creating a window that was updated with each pass of
the while.

Thanks,
Ben



Sun, 09 Apr 2006 08:02:44 GMT  
 presenting realtime output from a utility

Quote:


>> BTW. this is a snippet of the code I have determined I would need to
>> modify:

>>   set burnCmd [list exec /usr/bin/cdrecord -dao -v speed=8 \
>>      dev=2,0,0 -data  $isoFilePath]
>>    puts "Burn Command is\n $burnCmd"
>>    catch {eval $burnCmd} cmdOutput
>>    puts "$cmdOutput\n"

> change it to be :

>     set fd [open "|/usr/bin/cdrecord ...." r]

> then use
>     filevent $fd readable "checkOutput $fd"

> this will trigger the proc checkOutput to be called
> every time data is rady to be read (if cdrecord puts
> out a new line each time then use gets & then stick
> this line into a label would be easiest.) In that proc
> also check for eof, as that will be the end of your program.

> check out http://wiki.tcl.tk for many concrete examples of
> this.

Thanks. I'll check it out and see what I can do.

Ben

--
BTW. I can be contacted at Username:newsgroup4.replies.benaltw
Domain:xoxy.net



Sun, 09 Apr 2006 08:05:10 GMT  
 presenting realtime output from a utility

Quote:

> change it to be :

>     set fd [open "|/usr/bin/cdrecord ...." r]

> then use
>     filevent $fd readable "checkOutput $fd"

Great! I have it almost working without the gui now using fileevent. In
checkOutput I do a:
     set ret [gets $fd s1]
     if {$ret == -1} {
         return
     }

But then it hangs and if I replace return with exit, it exits the script
when I'd like it to continue running commands. If I replace the return
with a "close $fd" I get the following:

[snip]
/usr/bin/cdrecord: fifo had 2 puts and 2 gets.
/usr/bin/cdrecord: fifo was 0 times empty and 0 times full, min fill was
100%.
     while executing
"close $fd"
     (procedure "checkOutput" line 5)
     invoked from within
"checkOutput file5"

Could you tell me what I am doing wrong here?

Once I get that straight I'll just set a window with the new value the
variable is set to on each call to checkOutput.

Thanks,
Ben

Quote:
> check out http://wiki.tcl.tk for many concrete examples of
> this.

> Bruce

--
BTW. I can be contacted at Username:newsgroup4.replies.benaltw
Domain:xoxy.net


Sun, 09 Apr 2006 08:30:44 GMT  
 presenting realtime output from a utility
Hi Ben,

'sleep' is not a Tcl command, use 'after 1000' instead.
It works in interactive mode, because the 'unknown'
procedure, which is called when an unknown command is
found, tries to find this command outside of Tcl and
invoke it via exec. Thus it finds the shells sleep
command and executes it. This feature is disable in
a non interactive tclsh.

Best regards

Ulrich

Quote:


>> Procedures similar to the ones below work for me in WISH CD-Writer
>> <http://www.geocities.com/pa_mcclamrock/wishcd.tar.gz>, which displays
>> output from cdrecord line by line as it becomes available. They're
>> modified versions of procedures found in "ExecLog" by Brent Welch, an
>> example program in *Practical Programming in Tcl and Tk*. (The text
>> widget here, obviously, is called ".textout").

> Hi. I appreciate you posting that code. I tried it out without any
> modifications and as you would suspect it choked on the .textout. I need
> to learn to crawl before walking. I have to look into the fileevent
> command. In the meanwhile (I only just started on this again now), I was
> messing with the following code I wrote (below) to just get something
> going without a gui. The problem is that it works fine on the command
> line, but not in a script:

> #! /usr/bin/wish -f
> set f1 [open {| /usr/bin/cdrecord -dummy .....} r]

> set s1 [gets $f1]
> while { $s1 != "" } {
>     sleep 1
>     set s1 [gets $f1]
>     puts $s1
> }

> On the command line it works fine and in a script I get:
>     Error in startup script: invalid command name "sleep"

> Why should it know what sleep is on the command line but not when run
> from a script? I was thinking if I got this working it would then be a
> simple matter of creating a window that was updated with each pass of
> the while.

> Thanks,
> Ben

--

For those of you who don't get this e-mail, let me know and I'll re-send it.



Sun, 09 Apr 2006 16:32:04 GMT  
 presenting realtime output from a utility

| Great! I have it almost working without the gui now using
| fileevent. In checkOutput I do a:
|
|      set ret [gets $fd s1]
|      if {$ret == -1} {
|          return
|      }

You need to check for EOF and catch the `close'.
See http://mini.net/tcl/880.

R'



Sun, 09 Apr 2006 16:31:06 GMT  
 presenting realtime output from a utility

| Why should it know what sleep is on the command line but not when
| run from a script?

Because when invoked interactively, wish/tcl tries to execute an
unknown command via the shell.  It does not do this when called from a
script.

BTW, the TCL way of sleeping is `after'.  But this is not what you
want here, since your app is not `live' when TCL sleeps, so the screen
will not get refreshed properly.  Use fileevents.

R'



Sun, 09 Apr 2006 16:35:37 GMT  
 presenting realtime output from a utility
[...]

Quote:
>  with a "close $fd" I get the following:

>  [snip]
>  /usr/bin/cdrecord: fifo had 2 puts and 2 gets.
>  /usr/bin/cdrecord: fifo was 0 times empty and 0 times full, min fill was
>  100%.
>       while executing
>  "close $fd"
>       (procedure "checkOutput" line 5)
>       invoked from within
>  "checkOutput file5"

>  Could you tell me what I am doing wrong here?

It appears the cdrecord command sends some output to stderr.  Tcl
assumes something went wrong.  Please read http://wiki.tcl.tk/close

--
Glenn Jackman
NCF Sysadmin



Sun, 09 Apr 2006 21:26:38 GMT  
 
 [ 12 post ] 

 Relevant Pages 

1. realtime exec output and tk

2. How to capture output of a program in realtime

3. Presenting output from md5.digest like md5sum from GNU textutils

4. Using nawk with output from solaris utilities

5. Search Utility, Compare Utility

6. Find an Utility which make your plain ascii output to graphical output in Windows !!

7. how print output to screen and output file

8. Outputting info from awk and using the info to name the awk output file

9. Create polymorhpic output for I32 input (select output type)

10. rxvt + python problem: script output buffered and interactive output missing

11. HOW TO CAPTURE AN OUTPUT LINE OF A VHDL OUTPUT IN TCL

12. Any Realtime TPF programmers/newsgroups?

 

 
Powered by phpBB® Forum Software