Re: Redirect file output into variables

From: Michael Schlenker (schlenk_at_uni-oldenburg.de)
Date: 10/22/03


Date: Wed, 22 Oct 2003 20:29:41 +0200

Markus Elfring wrote:

>>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
        }
    }
}

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



Relevant Pages

  • Re: New Meds...
    ... It's the DEA who wants the 3 month script writing. ... down isn't safe for what we would need one for. ... we get three month supplies of meds from THEIR mail in pharmacies, ... I still only see my doctor every 3 months. ...
    (alt.support.chronic-pain)
  • TIP #227: Interface to Get and Set the Return Options of an Interpreter
    ... This TIP proposes new public C routines to allow the same access to ... the *return* command has aready been extended to accept all ... The ability to set custom return options in the interp has been limited ... to the script level though. ...
    (comp.lang.tcl)
  • Re: [PHP] Passing variables between pages
    ... You always have the option to write one huge script for any application. ... we use sessions to pass variables between scripts -- but we could ... So just defining $foo saying include 'steptwo.php' ... I do use this technique inside of my templating class though. ...
    (php.general)
  • Re: Slave interpreters, aliases, and upvar
    ... If I import a namespace command into another namespace, ... variable foo "this is namespace foo"; ... It seems that when the master alias is invoked none of the ... set interp ...
    (comp.lang.tcl)
  • Re: websh - safe interps and other questions
    ... Any reason why safe interp's would not work? ... No reason why a safe interp would not work, but since the safe interp you'd invoke will not contain any websh functionality, it is relatively useless, not just safe... ... (Note however, that usually a subsequent request is not handled by the same interp, so be very careful when passing info from request to request...) ...
    (comp.lang.tcl)