codekingpro/portable-devtools
114k
1# inplace --- load and invoke the inplace extension.2#3# Copyright (C) 2013, 2017, 2019 the Free Software Foundation, Inc.4#5# This file is part of GAWK, the GNU implementation of the6# AWK Programming Language.7#8# GAWK is free software; you can redistribute it and/or modify9# it under the terms of the GNU General Public License as published by10# the Free Software Foundation; either version 3 of the License, or11# (at your option) any later version.12#13# GAWK is distributed in the hope that it will be useful,14# but WITHOUT ANY WARRANTY; without even the implied warranty of15# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the16# GNU General Public License for more details.17#18# You should have received a copy of the GNU General Public License19# along with this program; if not, write to the Free Software20# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA21#22# Andrew J. Schorr, aschorr@telemetry-investments.com23# January 201324#25# Revised for namespaces26# Arnold Robbins, arnold@skeeve.com27# July 201728# June 2019, add backwards compatibility29#30# This file and how to use it are described in the manual.31# Therefore, we respectfully advise you to Read The Fine Manual32# for more information.33 34@load "inplace"35 36# Please set inplace::suffix to make a backup copy. For example, you may37# want to set inplace::suffix to .bak on the command line or in a BEGIN rule.38 39# Before there were namespaces in gawk, this extension used40# INPLACE_SUFFIX as the variable for making backup copies. We allow this41# too, so that any code that used the previous version continues to work.42 43# By default, each filename on the command line will be edited inplace.44# But you can selectively disable this by adding an inplace::enable=0 argument45# prior to files that you do not want to process this way. You can then46# reenable it later on the commandline by putting inplace::enable=1 before files47# that you wish to be subject to inplace editing.48 49# N.B. We call inplace::end() in the BEGINFILE and END rules so that any50# actions in an ENDFILE rule will be redirected as expected.51 52@namespace "inplace"53 54BEGIN {55 enable = 1 # enabled by default56}57 58BEGINFILE {59 sfx = (suffix ? suffix : awk::INPLACE_SUFFIX)60 if (filename != "")61 end(filename, sfx)62 if (enable)63 begin(filename = FILENAME, sfx)64 else65 filename = ""66}67 68END {69 if (filename != "")70 end(filename, (suffix ? suffix : awk::INPLACE_SUFFIX))71}72 