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: Eli Zaretskii
Subject: Re: [bug-gawk] in-place edit request
Date: Wed, 26 Dec 2012 22:42:44 +0200

> Date: Wed, 26 Dec 2012 14:19:17 -0500
> From: "Andrew J. Schorr" <address@hidden>
> Cc: address@hidden, address@hidden
> 
> Hi Eli,
> 
> On Wed, Dec 26, 2012 at 09:03:32PM +0200, Eli Zaretskii wrote:
> > Sorry, I still don't see the light.  If all you want is let Gawk use
> > stdout "like it normally does", why not just use it?  Or just use
> > freopen to redirect stdout to a temporary file, then mv that file as
> > you intended?
> 
> It is easy to redirect stdout using freopen or dup2.  The difficult part
> is restoring it after processing the input file.  This is difficult
> if one uses freopen and requires the /dev/fd trick.  That is why
> I prefer the dup2 solution.

Thanks.

> Why do we want to restore stdout?  This may be required when processing
> multiple input files or when some print statements occur in a context where an
> input file is not being processed.

If this is about having 'print', 'printf' etc go to some other file in
some suitably marked portion of an Awk program, why manipulate stdout
at all? why not just introduce a command that would make 'print'
etc. use a stream that is different from stdout, a stream that is
connected to the file where you want the output to go?  Then you won't
have any problems restoring stdout in the first place.

> For example, consider this program:
> 
> gawk -i inplace '{print gensub(/foo/, "bar", "g")} END {print "all done"}' 
> /tmp/a /tmp/b
> 
> The final "all done" message should go to the terminal, not into the
> replacement new version of /tmp/b.  So we need to be able to restore the
> default stdout destination.

Make the implementation of 'print' use a separate stream, which will
be opened when the program says it needs this "redirection", then make
'print' use stdout again in time for your "all done" message.

IOW, set_stdout will open the file and tell 'printf' use that stream
instead of stdout, then reset_stdout will close the stream and tell
'print' get back to using stdout again.  Instead of futzing with
'stdout' and low-level file descriptors, let 'print' handle that.



reply via email to

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