Redirect file output into variables 
Author Message
 Redirect file output into variables

This post is another version of the feature request
" http://www.*-*-*.com/ ;.

The PHP programming language provides output control functions
( http://www.*-*-*.com/ , e. g.
"ob_get_contents"). I would like to use similar functionality with
TCL.

1. http://www.*-*-*.com/
I suggest to add a parameter that specifies a variable name.
open $fileName $access $permissions $varName
     or
open -var $Name $fileName $access $permissions

2. http://www.*-*-*.com/
I suggest to add an interceptor and options to switch the output into
variables on and off on the fly.
fconfigure -setvar $Name $channelId
fconfigure -stopvar $Name $channelId
fconfigure -getcontents Buffer $channelId

3. http://www.*-*-*.com/
    http://www.*-*-*.com/
Can a memory channel be developed that can read from and write into
TCL variables?

4. http://www.*-*-*.com/
    http://www.*-*-*.com/
Can a virtual file system be created for that purpose?



Wed, 08 Mar 2006 00:25:33 GMT  
 Redirect file output into variables
Is anybody other than me interested in an easy cooperation between
variables and file systems?


Thu, 06 Apr 2006 18:42:00 GMT  
 Redirect file output into variables
:Is anybody other than me interested in an easy cooperation between
:variables and file systems?

  Can you explain more?  Opening files and redirecting their
contents to variables, lists, or arrays is done all the time
by Tcl programmers.  Same is true for writing vars, etc. to
files. Or do you mean something else?

--
  _ _    ____
 //\/\ike ||uxford



Thu, 06 Apr 2006 23:18:09 GMT  
 Redirect file output into variables

Quote:
> Can you explain more?  Opening files and redirecting their
> contents to variables, lists, or arrays is done all the time
> by Tcl programmers.  Same is true for writing vars, etc. to
> files. Or do you mean something else?

Did you read my first request for this topic?

I am thinking about the following application.
A limited execution environment will be created that forbids to
directly use standard file channels. For example, I must avoid that
output is written to STDOUT by calls to the TCL function "puts" before
the own data processing is finished. But these calls need a place to
store the output.
- How can the data be stored or redirected to a temporary file?
  or
- All file system calls will be intercepted to store the data into a
buffer that can be copied to a variable later.



Sat, 08 Apr 2006 23:55:04 GMT  
 Redirect file output into variables
Hi Markus,

There's another discussion going on about redirecting stdout.  Someone made
the very good suggestion to overload [puts] via the [rename] command.  Then
all calls to puts can do whatever you want.

Here's a sample puts that takes the data it receives and appends it to
global variables named after the channels to which the data was destined.
So [puts "Hello world"] would append "Hello world\n" to the variable
::stdout.

rename puts _puts
# to restore the "real" [puts]:
# rename _puts puts

proc puts { args } {

    switch [ llength $args ] {
        1 {
            set varname ::stdout
            set data [ lindex $args 0 ]
            set newline true
        }
        2 {
            if { [ lindex $args 0 ] == "-nonewline" } {
                set newline false
                set varname ::stdout
                set data [ lindex $args 1 ]
            } else {
                set varname "::[ lindex $args 0 ]"
                set newline true
                set data [ lindex $args 1 ]
            }
        }
        3 {
            if { [ lindex $args 0 ] == "-nonewline" } {
                set newline false
                set varname "::[ lindex $args 1 ]"
                set data [ lindex $args 2 ]"
            } else {
                error "wrong # args: should be \"puts ?-nonewline?
?channelId? string\""
            }
        }
        default {
            error "wrong # args: should be \"puts ?-nonewline? ?channelId?
string\""
        }
    }

    append $varname $data
    if { $newline } {
        append $varname "\n"
    }

Quote:
}

Regards,
Aric


Sun, 09 Apr 2006 03:58:19 GMT  
 Redirect file output into variables
Thanks for this suggestion.
The function "http://tcl.tk/man/tcl8.4/TclCmd/rename.htm"
(http://mini.net/tcl/rename) offers another solution. I see that you
must be careful with such wrapping.
1. Each command like [puts] can be replaced (http://mini.net/tcl/1570)
by an other implementation. It has got noticeable effects on the call
stack and error handling.

2. A unique name must be generated for the previous version.

3. All unknown command options should be forwared to the original
implementation. But I think that forwarding is not useful in some
cases.

4. Additional options will control the variable that is internally
used.

This technique will work in most situations. How do think about the
chances for the other techniques?



Mon, 10 Apr 2006 01:56:55 GMT  
 Redirect file output into variables

Quote:

>>Can you explain more?  Opening files and redirecting their
>>contents to variables, lists, or arrays is done all the time
>>by Tcl programmers.  Same is true for writing vars, etc. to
>>files. Or do you mean something else?

> Did you read my first request for this topic?

> I am thinking about the following application.
> A limited execution environment will be created that forbids to
> directly use standard file channels. For example, I must avoid that
> output is written to STDOUT by calls to the TCL function "puts" before
> the own data processing is finished. But these calls need a place to
> store the output.
> - How can the data be stored or redirected to a temporary file?
>   or
> - All file system calls will be intercepted to store the data into a
> buffer that can be copied to a variable later.

Hmm, i would use a safe interpreter for that..., as you say limited
execution environment...

proc buffered_puts {args} {
     global buffer
     switch -exact [llength $args] {
        0 { # handle error
        1 {
          append buffer(stdout) "[lindex $args 0]\n"
          }
        2 {
          if {[lindex $args 0] eq "-nonewline"} {
                append buffer(stdout) "[lindex $args 1]"
          } else {
                # check for error
                append buffer([lindex $args 0]) "[lindex $args 1]\n"
           }
          }
        3 {
          if {[lindex $args 0] eq "-nonewline"} {
                # check error
                append buffer([lindex $args 1]) "[lindex $args 2]"
          } else {
                # check for error
                append buffer([lindex $args 1]) "[lindex $args 2]\n"
           }
          }

        default {
                # handle error
        }
    }

Quote:
}

interp create safe foo
interp alias foo puts {} buffered_puts
# do similar things with read, gets etc.

set script "script you want to execute in a safe environment"
foo eval $script

# print the buffered output

Michael Schlenker



Mon, 10 Apr 2006 02:29:41 GMT  
 Redirect file output into variables
Does the suggested error handling conform to the original implementation?
- http://mini.net/tcl/error
- http://tcl.tk/man/tcl8.4/TclCmd/error.htm


Mon, 10 Apr 2006 20:28:28 GMT  
 Redirect file output into variables
Commands can be renamed in TCL. But what can be done in other
programming languages where the requested functionality can not be
achieved by renaming to wrap a previous implementation?


Mon, 10 Apr 2006 20:33:26 GMT  
 Redirect file output into variables

| Does the suggested error handling conform to the original
| implementation?
| - http://mini.net/tcl/error
| - http://tcl.tk/man/tcl8.4/TclCmd/error.htm

Mu.

IOW, what in those documents makes you raise the question?
R'



Mon, 10 Apr 2006 23:06:03 GMT  
 Redirect file output into variables

Quote:

> Does the suggested error handling conform to the original implementation?
> - http://mini.net/tcl/error
> - http://tcl.tk/man/tcl8.4/TclCmd/error.htm

With careful use of error and return -code error etc. it could be
possible to do it or come extremly close to the original error handling
of puts, didn't try yet. A safe slave cannot introspect the aliases it
gets from it's master so they look very much like builtins..

Michael



Tue, 11 Apr 2006 04:20:50 GMT  
 Redirect file output into variables

Quote:
> IOW, what in those documents makes you raise the question?

Please look at the feature request "Check description for variable
errorCode" (http://sourceforge.net/tracker/?func=detail&aid=776921&group_id=10894...).


Tue, 11 Apr 2006 15:00:44 GMT  
 Redirect file output into variables

| > IOW, what in those documents makes you raise the question?

Please forget about my posting.  My stupid newsreader made your
posting look like a new article, not a follow-up to some other
message.  And for a new article it did contain very few info ;-)

R'



Tue, 11 Apr 2006 18:07:34 GMT  
 
 [ 13 post ] 

 Relevant Pages 

1. redirecting print output to variable name in awk

2. How to redirect the standard output to a REXX-variable

3. Redirecting Output to files

4. Redirecting to TWO output files

5. Redirecting program output to file

6. how to redirect lpt1 to file-output

7. redirect g77 output into a file under windows xp

8. redirect output of functions to a string or file

9. redirect g77 under DOS 6.22- need output file

10. expect: redirect output into file called `uname -n'.txt

11. redirect exec output to a file

12. Redirecting output to a scratch file

 

 
Powered by phpBB® Forum Software