Next: , Previous: General files, Up: Files

5.17.3 Redirection

You can redirect the output of type and emit and all the words that use them (all output words that don't have an explicit target file) to an arbitrary file with the outfile-execute, used like this:

     : some-warning ( n -- )
         cr ." warning# " . ;
     : print-some-warning ( n -- )
         ['] some-warning stderr outfile-execute ;

After some-warning is executed, the original output direction is restored; this construct is safe against exceptions. Similarly, there is infile-execute for redirecting the input of key and its users (any input word that does not take a file explicitly).

outfile-execute       ... xt file-id – ...         gforth       “outfile-execute”

execute xt with the output of type etc. redirected to file-id.

infile-execute       ... xt file-id – ...         gforth       “infile-execute”

execute xt with the input of key etc. redirected to file-id.

If you do not want to redirect the input or output to a file, you can also make use of the fact that key, emit and type are deferred words (see Deferred Words). However, in that case you have to worry about the restoration and the protection against exceptions yourself; also, note that for redirecting the output in this way, you have to redirect both emit and type.