| 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 the |
| 6 |
# AWK Programming Language. |
| 7 |
# |
| 8 |
# GAWK is free software; you can redistribute it and/or modify |
| 9 |
# it under the terms of the GNU General Public License as published by |
| 10 |
# the Free Software Foundation; either version 3 of the License, or |
| 11 |
# (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 of |
| 15 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 |
# GNU General Public License for more details. |
| 17 |
# |
| 18 |
# You should have received a copy of the GNU General Public License |
| 19 |
# along with this program; if not, write to the Free Software |
| 20 |
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA |
| 21 |
# |
| 22 |
# Andrew J. Schorr, [email protected] |
| 23 |
# January 2013 |
| 24 |
# |
| 25 |
# Revised for namespaces |
| 26 |
# Arnold Robbins, [email protected] |
| 27 |
# July 2017 |
| 28 |
# June 2019, add backwards compatibility |
| 29 |
|
| 30 |
@load "inplace" |
| 31 |
|
| 32 |
# Please set inplace::suffix to make a backup copy. For example, you may |
| 33 |
# want to set inplace::suffix to .bak on the command line or in a BEGIN rule. |
| 34 |
|
| 35 |
# Before there were namespaces in gawk, this extension used |
| 36 |
# INPLACE_SUFFIX as the variable for making backup copies. We allow this |
| 37 |
# too, so that any code that used the previous version continues to work. |
| 38 |
|
| 39 |
# By default, each filename on the command line will be edited inplace. |
| 40 |
# But you can selectively disable this by adding an inplace::enable=0 argument |
| 41 |
# prior to files that you do not want to process this way. You can then |
| 42 |
# reenable it later on the commandline by putting inplace::enable=1 before files |
| 43 |
# that you wish to be subject to inplace editing. |
| 44 |
|
| 45 |
# N.B. We call inplace::end() in the BEGINFILE and END rules so that any |
| 46 |
# actions in an ENDFILE rule will be redirected as expected. |
| 47 |
|
| 48 |
@namespace "inplace" |
| 49 |
|
| 50 |
BEGIN { |
| 51 |
enable = 1 # enabled by default |
| 52 |
} |
| 53 |
|
| 54 |
BEGINFILE { |
| 55 |
sfx = (suffix ? suffix : awk::INPLACE_SUFFIX) |
| 56 |
if (filename != "") |
| 57 |
end(filename, sfx) |
| 58 |
if (enable) |
| 59 |
begin(filename = FILENAME, sfx) |
| 60 |
else |
| 61 |
filename = "" |
| 62 |
} |
| 63 |
|
| 64 |
END { |
| 65 |
if (filename != "") |
| 66 |
end(filename, (suffix ? suffix : awk::INPLACE_SUFFIX)) |
| 67 |
} |