For Programmers: Free Programming Magazines  


Home > Archive > Tcl > May 2004 > creating a filedescriptor froma TCL variable









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author creating a filedescriptor froma TCL variable
Phil Dietz

2004-05-20, 6:32 pm

What's the best way -- without using the local filesystem -- to turn a
TCL variable into a filedescriptor ??

Someting like below ??


proc varIntoFd { data } {
if { [catch {open "| echo \"$data\"" w} fd ] } {
return ""
}
return $fd
}


set data "data I want accessible via a file descriptor..."
set fd [varIntoFd $data]

# a simple test to see if it works.
fcopy $fd stdout


I have a feeling binary data wont work...
Gerald W. Lester

2004-05-21, 1:32 am

Phil Dietz wrote:

> What's the best way -- without using the local filesystem -- to turn a
> TCL variable into a filedescriptor ??
>
> Someting like below ??
>
>
> proc varIntoFd { data } {
> if { [catch {open "| echo \"$data\"" w} fd ] } {
> return ""
> }
> return $fd
> }
>
>
> set data "data I want accessible via a file descriptor..."
> set fd [varIntoFd $data]
>
> # a simple test to see if it works.
> fcopy $fd stdout
>
>
> I have a feeling binary data wont work...


What you are asking makes no since.

Why do you want to do this?

--
+--------------------------------+---------------------------------------+
| Gerald W. Lester | "The man who fights for his ideals is |
| Gerald.Lester@cox.net | the man who is alive." -- Cervantes |
+--------------------------------+---------------------------------------+
SM Ryan

2004-05-21, 2:31 am

# > What's the best way -- without using the local filesystem -- to turn a
# > TCL variable into a filedescriptor ??

Do you really need a file descriptor, or a Tcl channel?

--
SM Ryan http://www.rawbw.com/~wyrmwif/
Why are we here?
whrp
Phil Dietz

2004-05-21, 11:35 am

"Donal K. Fellows" <donal.k.fellows@man.ac.uk> wrote in message news:<c8kh6b$22s1$1@godfrey.mcc.ac.uk>...
> You mean create a readable Tcl channel that pulls its data from a
> string? I'm not sure, but the memchan extension is probably a good place
> to start looking.


Sorry for the confusion. Yes, I'd like to be able to turn a variable
into a TCL channel of some sorts without using the filesystem or a 3rd
party package.

Like a pipe, but to itsself. There are packages like ipc pipes,
shared memory, etc. that would help, but I need standard TCL way if
possible.

Basically this library call reads a message and returns a TCL channel
id -- giving the appearance of a stream to the user, without using the
file system.

The echo trick is definately not portable...

memchan may do the trick, but I need a standard TCL way.
Michael Schlenker

2004-05-21, 11:35 am

Phil Dietz wrote:
> "Donal K. Fellows" <donal.k.fellows@man.ac.uk> wrote in message news:<c8kh6b$22s1$1@godfrey.mcc.ac.uk>...
>
>
> Sorry for the confusion. Yes, I'd like to be able to turn a variable
> into a TCL channel of some sorts without using the filesystem or a 3rd
> party package.
>
> Like a pipe, but to itsself. There are packages like ipc pipes,
> shared memory, etc. that would help, but I need standard TCL way if
> possible.
>
> Basically this library call reads a message and returns a TCL channel
> id -- giving the appearance of a stream to the user, without using the
> file system.
>
> The echo trick is definately not portable...
>
> memchan may do the trick, but I need a standard TCL way.

Using an extension is the standard Tcl way.

memchan does exactly what you want and is portable to unix and windows,
so go for it.

Michael
SM Ryan

2004-05-21, 4:33 pm

# Sorry for the confusion. Yes, I'd like to be able to turn a variable
# into a TCL channel of some sorts without using the filesystem or a 3rd
# party package.

I do
set channel [wyrm::data read $string]
It stores $string in an internal buffer, and feeds it on channel read
requests. You can glean source code from
http://www.rawbw.com/~wyrmwif/html/wyrm-data.html#6
if you want.

# memchan may do the trick, but I need a standard TCL way.

Standard Tcl is to use packages to extend the core.

--
SM Ryan http://www.rawbw.com/~wyrmwif/
Elvis was an artist. But that didn't stop him from joining the service
in time of war. That's why he is the king, and you're a shmuck.
lvirden@yahoo.com

2004-05-22, 2:31 am


According to Phil Dietz <pedietz@west.com>:
:What's the best way -- without using the local filesystem -- to turn a
:TCL variable into a filedescriptor ??

Are you are wanting to be able to read and write to strings
instead of to filesystems? Can you tell us why -
is it because the device you are going to be using
has no files?
Your code here is not doing that. A write to this file descriptor just
throws the data away, because echo doesn't do anything with
its stdin.


--
<URL: http://wiki.tcl.tk/ > In God we trust.
Even if explicitly stated to the contrary, nothing in this posting
should be construed as representing my employer's opinions.
<URL: mailto:lvirden@yahoo.com > <URL: http://www.purl.org/NET/lvirden/ >
Schofield

2004-05-22, 2:31 am

Phil Dietz wrote:

> "Donal K. Fellows" <donal.k.fellows@man.ac.uk> wrote in message news:<c8kh6b$22s1$1@godfrey.mcc.ac.uk>...
>
>
>
> Sorry for the confusion. Yes, I'd like to be able to turn a variable
> into a TCL channel of some sorts without using the filesystem or a 3rd
> party package.
>
> Like a pipe, but to itsself. There are packages like ipc pipes,
> shared memory, etc. that would help, but I need standard TCL way if
> possible.
>
> Basically this library call reads a message and returns a TCL channel
> id -- giving the appearance of a stream to the user, without using the
> file system.
>
> The echo trick is definately not portable...
>
> memchan may do the trick, but I need a standard TCL way.


If you don't want to use extensions to Tcl (a very reasonable thing... I
typically only use extensions as a last resort) then one "standard"
approach would be to redefine the commands that use channels. Most
obvious is puts, gets, and read; but you'd also need to take care of
s, open, close, fconfigure and the other "f" commands. You could then
just store data in variables. This approach would work fine if the user
is only using puts, gets, and read; but would be a pretty tedious thing
to do provide implementations for *every* command that handles channels.

Something like:

rename puts __puts
proc puts {args} {
# parse args for channel, -nonewline, and message text
set channel [extractChannelFromPutsArgs $args]
if {[isActualChannel $channel]} {
eval __puts $args
} else {
eval putsToFugaziChannel $args
}
}



Anyway, something to think about.

-- bryan
Jeff Hobbs

2004-05-24, 1:33 pm

Phil Dietz wrote:
> "Donal K. Fellows" <donal.k.fellows@man.ac.uk> wrote in message news:<c8kh6b$22s1$1@godfrey.mcc.ac.uk>...
>
>
>
> Sorry for the confusion. Yes, I'd like to be able to turn a variable
> into a TCL channel of some sorts without using the filesystem or a 3rd
> party package.


You still haven't created a plausible example. Are you aware that
the following is 100% valid Tcl code right now? ::

set input "this is the input\nand line 2 of input"
exec << $input cat

--
Jeff Hobbs, The Tcl Guy
http://www.ActiveState.com/, a division of Sophos
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com