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: Andrew J. Schorr
Subject: Re: [bug-gawk] in-place edit request
Date: Wed, 26 Dec 2012 10:47:47 -0500
User-agent: Mutt/1.5.21 (2010-09-15)

Hi,

On Tue, Dec 25, 2012 at 11:08:50PM +0200, Aharon Robbins wrote:
> The below is a start. It works OK on a terminal but I suspect there
> could be issues with a regular file; one should probably do fgetpos / fsetpos
> at save / restore, as John suggested, also (after the fflush).

If using the dup/dup2 approach to operate underneath stdio, I don't think there
should be a problem.  According to the man page, the dup call copies the
current file position pointer.

> In short, I think this is the right path for getting an in-place extension.
> The shell script wrapper is then
> 
>       exec gawk -i inplace.awk "$@"
> 
> and inplace.awk becomes something like
> 
>       @load "setstdout"
>       BEGINFILE {
>               set_stdout(FILENAME "." PROCINFO["pid"])
>       }
>       ENDFILE {
>               reset_stdout()
>               system(sprintf("mv %s %s.bak && mv %s.%d %s",
>                       FILENAME, FILENAME,
>                       FILENAME, PROCINFO["pid"], FILENAME))
>       }
> 
> If you have time to give it a go, that'd be great.  If it works, we
> can get it into the dist, even.

This should work for normal cases.  Assaf raised the question of
robustness with respect to filenames that contain spaces and/or start
with "-".  One possibility is take a different approach like so:

@load "inplace"

# the user should set INPLACE_SUFFIX if he wants a backup copy.  For example,
# there could be a BEGIN rule that sets INPLACE_SUFFIX to ".bak".  Should
# that be the default?  Sed does not make a backup copy by default...

BEGINFILE {
   inplace_begin(FILENAME, INPLACE_SUFFIX)
}
ENDFILE {
   inplace_end(FILENAME, INPLACE_SUFFIX)
}

The inplace_end function could then use the rename system call to avoid
problems with filenames.  Of course, this assumes that rename works on all
platforms; is rename portable?  The extension can also take more care to
preserve file permissions, and possibly use mkstemp to make sure the temporary
file has a unique name.  In the sed code, they use unlink to remove
the temporary file if the rename call fails.  Is that desirable?
It seems as if "unlink" may not work on VMS, since it's implemented in
vms/vms_misc.c using "delete".  Then again, "unlink" is already used
in the bundled fileop and rwarray extensions...

Regards,
Andy



reply via email to

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