From octave-maintainers-request at bevo dot che dot wisc dot edu Mon Dec 15 17:56:43 2003 Subject: capture command output From: Paul Kienzle To: octave-maintainers at bevo dot che dot wisc dot edu Date: Mon, 15 Dec 2003 18:56:17 -0500 Hi, For a few reasons, I want to be able to run a command and capture its output without it going to the screen. One is for parallel octave --- if you execute stuff on another processor, you want to be able to send the screen output back to the controlling terminal. Another is for embedded octave --- if you execute an octave command in tcl/tk, you want to be able to put its output in a tcl/tk window. A third reason is testing --- I want to be able to run the test example and display a summary on the screen and have the results in a file. Using pager tricks, I can hack this (see the "capture" command below), but I would rather not go through the file system. Having written a few DLD functions, I know that all screen output goes through octave_stdout. I was assuming this was simply a variable that I could cache, replace with a my own stream, and restore when I was done. Alas, it is #define octave_stdout (octave_pager_stream::stream ()) and of course the interesting bits of the class octave_pager_stream are private. Any suggestions how we can do this from C++ and from the script level? Error messages can already be buffered, but warnings cannot. Thanks, Paul Kienzle pkienzle at users dot sf dot net PS, Is there any way to override 'private'? E.g., by having your own copy of the header files with the word private replace by public whereever it occurs? Or does the C++ ABI do something special when storing privates? function [ ret ] = capture (cmd) pso = page_screen_output; pager = PAGER; poi = page_output_immediately; tmpfile = sprintf("/tmp/capture%d",getpid); unwind_protect PAGER = ["cat >",tmpfile]; page_screen_output = 1; page_output_immediately = 1; evalin('caller',cmd); unwind_protect_cleanup PAGER = pager; page_output_immediately = poi; page_screen_output = pso; end_unwind_protect pause(0.1); fid=fopen(tmpfile,"r"); ret = setstr(fread(fid, Inf, "char")'); fclose(fid); unlink(tmpfile); endfunction