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: Aharon Robbins
Subject: Re: [bug-gawk] in-place edit request
Date: Sun, 23 Dec 2012 19:10:49 +0200
User-agent: Heirloom mailx 12.5 6/20/10

Hi Andy.

> Date: Sun, 23 Dec 2012 10:23:36 -0500
> From: "Andrew J. Schorr" <address@hidden>
> To: Aharon Robbins <address@hidden>
> Cc: address@hidden, address@hidden
> Subject: Re: [bug-gawk] in-place edit request
>
> 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).

I'd have to review the code.  I think output_fp is only ever set to
stdout unless something is going on with 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.

<reference type="tv show" date="seventies" kind="obscure">
Ah grasshopper.  The Way Of The UNIX Is The Way:
</reference>

It's not that hard, actually.  First, save the open file:

        int save_fd = dup(fileno(stdout));

Then close and reopen:

        fp = freopen(path_from_user, "w", stdout)

then to restore

        char buf[100];  /* say */
        sprintf(buf, "/dev/fd/%d", save_fd);

        fp = freopen(buf, "w", stdout);
        close(save_fd);
        save_fd = -1;

Ta da!

Details left as an exercise for the reader.

Arnold



reply via email to

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