6.18.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-0.7 “outfile-execute”

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

outfile-id ( – file-id  ) gforth-0.2 “outfile-id”

File-id is used by emit, type, and any output word that does not take a file-id as input. By default outfile-id produces the process’s stdout, unless changed with outfile-execute.

infile-execute ( ... xt file-id – ...  ) gforth-0.7 “infile-execute”

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

infile-id ( – file-id  ) gforth-0.4 “infile-id”

File-id is used by key, ?key, and anything that refers to the "user input device". By default infile-id produces the process’s stdin, unless changed with infile-execute.

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.