Rocksolid Light

Welcome to RetroBBS

mail  files  register  newsreader  groups  login

Message-ID:  

21 May, 2024: Computers section is temporarily disabled for maintenance. It will take several days before it's back.


devel / comp.lang.tcl / How to use 'dup' from TclX

SubjectAuthor
* How to use 'dup' from TclXHelmut Giese
`* Re: How to use 'dup' from TclXRich
 `* Re: How to use 'dup' from TclXAndreas Leitgeb
  `- Re: How to use 'dup' from TclXHelmut Giese

1
How to use 'dup' from TclX

<cbhnei1nu5kfqveh27f4m48r61c8h8cudp@4ax.com>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=22643&group=comp.lang.tcl#22643

  copy link   Newsgroups: comp.lang.tcl
Path: i2pn2.org!i2pn.org!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: hgiese@ratiosoft.com (Helmut Giese)
Newsgroups: comp.lang.tcl
Subject: How to use 'dup' from TclX
Date: Mon, 28 Aug 2023 00:02:29 +0200
Organization: ratiosoft
Lines: 33
Message-ID: <cbhnei1nu5kfqveh27f4m48r61c8h8cudp@4ax.com>
MIME-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit
Injection-Info: dont-email.me; posting-host="4e44a09b5efa2824bb08c3fa9bc8f0d7";
logging-data="1427046"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18PNqBM23pOXj7E2gS25JXX"
Cancel-Lock: sha1:bw7TGMkwaWdMnacuSSKmPrfzCYw=
X-Newsreader: Forte Free Agent 1.93/32.576 English (American)
 by: Helmut Giese - Sun, 27 Aug 2023 22:02 UTC

Hello out there,
I have some data that I use to analyse the behaviour of my program.
Now I thought it would be a good idea to include this data in my
repository.
But the data is generated by using 'puts' so I would need a way to
redirect 'puts' to 'stdout'.
I found 'dup' from TclX but apparently I cannot use it correctly. Here
is my (compressed) attempt:
---
package require Tclx

proc dumpTree {data} {
puts $data
}

proc process {data} {
set fName dump.txt
if {[file exists $fName]} {
file delete $fName
}
set fd [open $fName w]
dup $fd stdout
dumpTree $data
close $fd
} process "some data"
---
The file 'dump.txt' is generated but it is empty. Moreover running
this programm repeatedly produces the error
error deleting "dump.txt": permission denied

Any help on correcting my mistake(s) will be greatly appreciated.
Helmut

Re: How to use 'dup' from TclX

<ucgv67$1dccg$1@dont-email.me>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=22644&group=comp.lang.tcl#22644

  copy link   Newsgroups: comp.lang.tcl
Path: i2pn2.org!i2pn.org!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: rich@example.invalid (Rich)
Newsgroups: comp.lang.tcl
Subject: Re: How to use 'dup' from TclX
Date: Mon, 28 Aug 2023 02:01:43 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 42
Message-ID: <ucgv67$1dccg$1@dont-email.me>
References: <cbhnei1nu5kfqveh27f4m48r61c8h8cudp@4ax.com>
Injection-Date: Mon, 28 Aug 2023 02:01:43 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="4851348863bb32460652acdd213421ee";
logging-data="1487248"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX190+wpbqJewC8vrGnpaYlxe"
User-Agent: tin/2.6.1-20211226 ("Convalmore") (Linux/5.15.117 (x86_64))
Cancel-Lock: sha1:3gz8qVisF36nb9OQycNhWdpRTTc=
 by: Rich - Mon, 28 Aug 2023 02:01 UTC

Helmut Giese <hgiese@ratiosoft.com> wrote:
> Hello out there,
> But the data is generated by using 'puts' so I would need a way to
> redirect 'puts' to 'stdout'.

puts already outputs to stdout by default -- I think you meant
"redirect stdout into a file".

> I found 'dup' from TclX but apparently I cannot use it correctly.
> Here is my (compressed) attempt:

From what I read in the tclx manpage, your code looks to be using the
dup call correctly.

> proc dumpTree {data} {
> puts $data
> }

One suggestion for an alternative. Assuming you can modify the code,
write your 'dumpTree' this way:

proc dumpTree {data {fd stdout}} {
puts $fd $data
}

When you want the data to go to stdout, call it as:

dumptree $data

When you want to dump to a file:

set fd [open dump.txt {WRONLY CREAT TRUNC}]
dumptree $data $fd
close $fd

> The file 'dump.txt' is generated but it is empty. Moreover running
> this programm repeatedly produces the error
> error deleting "dump.txt": permission denied

Is the same user running it each time? This looks to be an OS
permissions issue that may have nothing to do with Tcl itself.

Re: How to use 'dup' from TclX

<slrnueplmm.6epk.avl@logic.at>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=22645&group=comp.lang.tcl#22645

  copy link   Newsgroups: comp.lang.tcl
Path: i2pn2.org!i2pn.org!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: avl@logic.at (Andreas Leitgeb)
Newsgroups: comp.lang.tcl
Subject: Re: How to use 'dup' from TclX
Date: Mon, 28 Aug 2023 17:18:14 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 34
Message-ID: <slrnueplmm.6epk.avl@logic.at>
References: <cbhnei1nu5kfqveh27f4m48r61c8h8cudp@4ax.com>
<ucgv67$1dccg$1@dont-email.me>
Reply-To: avl@logic.at
Injection-Date: Mon, 28 Aug 2023 17:18:14 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="c1744e51152f41dd065bfcd77af7f209";
logging-data="1894977"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19VsBDl7EZIYiC2s67P/AXg"
User-Agent: slrn/1.0.3 (Linux)
Cancel-Lock: sha1:1Mq/RHgX6sWK4GH29d27k/RGs90=
 by: Andreas Leitgeb - Mon, 28 Aug 2023 17:18 UTC

Rich <rich@example.invalid> wrote:
> Helmut Giese <hgiese@ratiosoft.com> wrote:
>> I found 'dup' from TclX but apparently I cannot use it correctly.
>> Here is my (compressed) attempt:
> From what I read in the tclx manpage, your code looks to be using the
> dup call correctly.

I'd probably first dup the original stdout to a new filehandle (one-arg
variant of dup returns new filehandle), then dup the file onto stdout,
and finally (after "dumptree") dup the previously created filehandle
back to stdout.

set dfd [open $dumpfile w]
set origout [dup stdout]; # make backup of application's stdout
dup $fdf stdout
dumptree "Hello World!"
close stdout; close $dfd ;# I think you need to close both!
dup $origout stdout
close $origout ;# close the backup-fd of stout. no need to keep them both

try that - I think your symptom could be caused by not closing
both filedescriptors to the file (namely both stdout and the one
originally returned from open)

Whether you actually need to backup and restore the original stdout
is your decision: does your script ever need to output something to
stdout after the call to "dumptree" ?

PS: another maybe-caveat: if the application is on Windows and
actually opening a window (using Tk, or maybe even just tclsh's
"console") then all the stdout-handling might be just screwed,
so that even redirecting a file-fd to stdout might just not work.
I don't have any experience with Tclx on Windows.

Re: How to use 'dup' from TclX

<uopreih1oktkdtmjnch1ubqtdh1vrgqpgk@4ax.com>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=22646&group=comp.lang.tcl#22646

  copy link   Newsgroups: comp.lang.tcl
Path: i2pn2.org!i2pn.org!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: hgiese@ratiosoft.com (Helmut Giese)
Newsgroups: comp.lang.tcl
Subject: Re: How to use 'dup' from TclX
Date: Tue, 29 Aug 2023 14:53:05 +0200
Organization: ratiosoft
Lines: 22
Message-ID: <uopreih1oktkdtmjnch1ubqtdh1vrgqpgk@4ax.com>
References: <cbhnei1nu5kfqveh27f4m48r61c8h8cudp@4ax.com> <ucgv67$1dccg$1@dont-email.me> <slrnueplmm.6epk.avl@logic.at>
MIME-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit
Injection-Info: dont-email.me; posting-host="09fe4ff25ca3d98f09d68d58536b0bd4";
logging-data="2388517"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+h/RJfkB7qQ5fFsxHSuGZc"
Cancel-Lock: sha1:Eve92kSsGPfoPPXfalNCv4Vdg8w=
X-Newsreader: Forte Free Agent 1.93/32.576 English (American)
 by: Helmut Giese - Tue, 29 Aug 2023 12:53 UTC

Hi Rich and Andreas,
many thanks to both of you.
>
>I'd probably first dup the original stdout to a new filehandle (one-arg
>variant of dup returns new filehandle), then dup the file onto stdout,
>and finally (after "dumptree") dup the previously created filehandle
>back to stdout.
>
> set dfd [open $dumpfile w]
> set origout [dup stdout]; # make backup of application's stdout
> dup $fdf stdout
> dumptree "Hello World!"
> close stdout; close $dfd ;# I think you need to close both!
> dup $origout stdout
> close $origout ;# close the backup-fd of stout. no need to keep them both
>
Andreas' observation to close stdout, too, was spot on. With this
augmentation my original program works as expected.
And even preserving stdout works on Windows, albeit not within TkCon
(but that is understandable since TkCon does some magic with stdout).
Again many thanks
Helmut


devel / comp.lang.tcl / How to use 'dup' from TclX

1
server_pubkey.txt

rocksolid light 0.9.81
clearnet tor