bug-gawk
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [bug-gawk] in-place edit request


From: j . eh
Subject: Re: [bug-gawk] in-place edit request
Date: Sun, 23 Dec 2012 12:07:56 -0600
User-agent: Mutt/1.4.1i

On Sun, Dec 23, 2012 at 10:23:36AM -0500, Andrew J. Schorr wrote:
> Hi Arnold,
> 
> On Sun, Dec 23, 2012 at 12:33:22PM +0200, Aharon Robbins wrote:
> > I'm not familiar with the perl select function. Can you describe what
> > it does?
> 
> I'm no Perl hacker, but "perldoc -f select" says this:
> 
>       select FILEHANDLE
> 
>       Returns the currently selected filehandle.  If FILEHANDLE is supplied,
>       sets the new current default filehandle for output.  This has two
>       effects: first, a "write" or a "print" without a filehandle default to
>       this FILEHANDLE.  Second, references to variables related to output
>       will refer to this output channel.
> 
> I think it may be equivalent to assigning output_fp in the gawk code
> (as is done by the debug.c:set_gawk_output function).


If I recall, I was using it to send script output to a different
terminal. It was helpful when writing the code for the debugger. 


> 
> > It may be that in-place editing can be entirely implemented by using
> > BEGINFILE, ENDFILE, and a small extension function that simply uses
> > freopen(3) on stdout.
> > 
> > Since that has zero effect on the gawk code base, I'm all for it. :-)
> 
> I had also hoped this could be done with a small extension function.  The
> problem with freopen is that it closes the existing stdout.  I don't
> see how one can then switch back to the previous stdout after redirection
> is no longer desired.  If stdout were an lvalue, one could simply
> reassign it, but that does not seem to be the case.  Thus, output_fp
> seems like the best bet to me.  Unfortunately, that would require exposing
> the set_gawk_output function through the extension API or adding a new
> builtin gawk function.
> 
> Can anybody see another way to do this?  Maybe I am missing something.

If stdout is connected to a terminal, may be reopening /dev/tty or /dev/fd/1
will work.

 OR

use dup() to duplicate stdout first before freopen(). I don't think gawk
uses printf or even fprintf for output. But, stdout may be hardcoded
in places (e.g close). In that case, if someone is really ambitious can try

        *stdout = *fp

and/or use fgetpos/fsetpos for extra insurance.


HTH.

John





reply via email to

[Prev in Thread] Current Thread [Next in Thread]