head 1.20; access; symbols SHTOOL_2_0_8:1.20 SHTOOL_2_0_7:1.17 SHTOOL_2_0_6:1.16 SHTOOL_2_0_5:1.16 SHTOOL_2_0_4:1.16 SHTOOL_2_0_3:1.15 SHTOOL_2_0_2:1.15 SHTOOL_2_0_1:1.14 SHTOOL_2_0_0:1.14 SHTOOL_2_0b3:1.13 SHTOOL_2_0b2:1.12 SHTOOL_2_0b1:1.12 SHTOOL_1_6:1.5.0.2 SHTOOL_1_6_2:1.5 SHTOOL_1_6_1:1.5 SHTOOL_1_6_0:1.4; locks; strict; comment @# @; 1.20 date 2008.04.02.20.13.44; author rse; state Exp; branches; next 1.19; commitid eB5Wirx8MdMyDzXs; 1.19 date 2008.04.02.20.12.04; author rse; state Exp; branches; next 1.18; commitid xd2Jq3h5PA50DzXs; 1.18 date 2007.08.31.13.15.12; author rse; state Exp; branches; next 1.17; commitid mKfPYw7c1nDseUvs; 1.17 date 2007.05.19.19.09.39; author rse; state Exp; branches; next 1.16; commitid OPUF1UBbllBTwzis; 1.16 date 2006.02.04.09.43.25; author rse; state Exp; branches; next 1.15; commitid oWk7BmnyAitJtfkr; 1.15 date 2005.06.15.08.14.36; author rse; state Exp; branches; next 1.14; 1.14 date 2004.07.02.13.06.36; author rse; state Exp; branches; next 1.13; 1.13 date 2004.05.08.19.09.58; author rse; state Exp; branches; next 1.12; 1.12 date 2004.02.17.08.54.14; author thl; state Exp; branches; next 1.11; 1.11 date 2004.02.12.16.06.27; author rse; state Exp; branches; next 1.10; 1.10 date 2004.01.01.16.54.20; author rse; state Exp; branches; next 1.9; 1.9 date 2003.08.13.14.20.34; author rse; state Exp; branches; next 1.8; 1.8 date 2003.05.16.08.46.26; author rse; state Exp; branches; next 1.7; 1.7 date 2003.02.11.13.00.50; author rse; state Exp; branches; next 1.6; 1.6 date 2003.02.11.12.56.40; author rse; state Exp; branches; next 1.5; 1.5 date 2002.02.23.09.18.03; author rse; state Exp; branches; next 1.4; 1.4 date 2002.01.02.17.04.56; author rse; state Exp; branches; next 1.3; 1.3 date 2001.12.31.15.06.04; author rse; state Exp; branches; next 1.2; 1.2 date 2001.09.23.10.58.10; author rse; state Exp; branches; next 1.1; 1.1 date 2001.06.14.19.02.54; author rse; state Exp; branches; next ; desc @@ 1.20 log @adjust copyright for year 2008 @ text @## ## subst -- Apply sed(1) substitution operations ## Copyright (c) 2001-2008 Ralf S. Engelschall ## ## This file is part of shtool and free software; you can redistribute ## it and/or modify it under the terms of the GNU General Public ## License as published by the Free Software Foundation; either version ## 2 of the License, or (at your option) any later version. ## ## This file is distributed in the hope that it will be useful, ## but WITHOUT ANY WARRANTY; without even the implied warranty of ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU ## General Public License for more details. ## ## You should have received a copy of the GNU General Public License ## along with this program; if not, write to the Free Software ## Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, ## USA, or contact Ralf S. Engelschall . ## str_tool="subst" str_usage="[-v|--verbose] [-t|--trace] [-n|--nop] [-w|--warning] [-q|--quiet] [-s|--stealth] [-i|--interactive] [-b|--backup ] [-e|--exec ] [-f|--file ] [] [...]" gen_tmpfile=yes arg_spec="0+" opt_spec="v.t.n.w.q.s.i.b:e+f:" opt_alias="v:verbose,t:trace,n:nop,w:warning,q:quiet,s:stealth,i:interactive,b:backup,e:exec,f:file" opt_v=no opt_t=no opt_n=no opt_w=no opt_q=no opt_s=no opt_i=no opt_b="" opt_e="" opt_f="" . ./sh.common # remember optional list of file(s) files="$*" files_num="$#" # parameter consistency check if [ $# -eq 0 ] && [ ".$opt_b" != . ]; then echo "$msgprefix:Error: option -b cannot be applied to stdin" 1>&2 shtool_exit 1 fi if [ $# -eq 0 ] && [ ".$opt_s" = .yes ]; then echo "$msgprefix:Error: option -s cannot be applied to stdin" 1>&2 shtool_exit 1 fi # build underlying sed(1) command sedcmd='sed' if [ ".$opt_e" != . ]; then OIFS="$IFS"; IFS="$ASC_NL"; set -- $opt_e; IFS="$OIFS" for e do sedcmd="$sedcmd -e '$e'" done elif [ ".$opt_f" != . ]; then if [ ! -f $opt_f ]; then echo "$msgprefix:Error: command file \`$opt_f' not found or not a regular file" 1>&2 shtool_exit 1 fi sedcmd="$sedcmd -f '$opt_f'" else echo "$msgprefix:Error: either -e option(s) or -f option required" 1>&2 shtool_exit 1 fi # determine extension for original file orig=".orig" if [ ".$opt_b" != . ]; then orig="$opt_b" fi # apply sed(1) operation(s) if [ ".$files" != . ]; then # apply operation(s) to files substdone=no for file in $files; do test ".$file" = . && continue if [ ! -f $file ]; then echo "$msgprefix:Warning: file \`$file' not found or not a regular file" 1>&2 continue fi # handle interactive mode if [ ".$opt_i" = .yes ]; then eval "$sedcmd <$file >$file.new" skip=no if cmp $file $file.new >/dev/null 2>&1; then rm -f $file.new skip=yes else (diff -U1 $file $file.new >$tmpfile) 2>/dev/null if [ ".`cat $tmpfile`" = . ]; then (diff -C1 $file $file.new >$tmpfile) 2>/dev/null if [ ".`cat $tmpfile`" = . ]; then echo "$msgprefix:Warning: unable to show difference for file \`$file'" 1>&2 cp /dev/null $tmpfile fi fi rm -f $file.new cat $tmpfile echo dummy | awk '{ printf("%s", TEXT); }' TEXT=">>> Apply [Y/n]: " read input if [ ".$input" != .Y ] &&\ [ ".$input" != .y ] &&\ [ ".$input" != . ]; then skip=yes fi fi if [ ".$skip" = .yes ]; then if [ ".$opt_v" = .yes ]; then echo "file \`$file' -- skipped" 1>&2 fi continue fi fi # apply sed(1) operation(s) if [ ".$opt_v" = .yes ]; then echo "patching \`$file'" 1>&2 fi if [ ".$opt_t" = .yes ]; then echo "\$ cp -p $file $file$orig" echo "\$ chmod u+w $file" echo "\$ $sedcmd <$file$orig >$file" fi if [ ".$opt_n" = .no ]; then cp -p $file $file$orig chmod u+w $file >/dev/null 2>&1 || true eval "$sedcmd <$file$orig >$file" fi # optionally fix timestamp if [ ".$opt_s" = .yes ]; then if [ ".$opt_t" = .yes ]; then echo "\$ touch -r $file$orig $file" fi if [ ".$opt_n" = .no ]; then touch -r $file$orig $file fi fi # optionally check whether any content change actually occurred if [ ".$opt_q" = .no ]; then if cmp $file$orig $file >/dev/null 2>&1; then if [ ".$opt_w" = .yes ]; then echo "$msgprefix:Warning: substitution resulted in no content change on file \"$file\"" 1>&2 fi else substdone=yes fi fi # optionally remove preserved original file if [ ".$opt_b" = . ]; then if [ ".$opt_t" = .yes ]; then echo "\$ rm -f $file$orig" fi if [ ".$opt_n" = .no ]; then rm -f $file$orig fi fi done if [ ".$opt_q" = .no ] && [ ".$opt_w" = .no ]; then if [ ".$substdone" = .no ]; then if [ ".$files_num" = .1 ]; then echo "$msgprefix:Warning: substitution resulted in no content change on file \"$file\"" 1>&2 else echo "$msgprefix:Warning: substitution resulted in no content change on any file" 1>&2 fi fi fi else # apply operation(s) to stdin/stdout if [ ".$opt_v" = .yes ]; then echo "patching " 1>&2 fi if [ ".$opt_t" = .yes ]; then echo "\$ $sedcmd" fi if [ ".$opt_n" = .no ]; then eval "$sedcmd" fi fi shtool_exit 0 ## ## manual page ## =pod =head1 NAME B - B sed(1) substitution operations =head1 SYNOPSIS B [B<-v>|B<--verbose>] [B<-t>|B<--trace>] [B<-n>|B<--nop>] [B<-w>|B<--warning>] [B<-q>|B<--quiet>] [B<-s>|B<--stealth>] [B<-i>|B<--interactive>] [B<-b>|B<--backup> I] [B<-e>|B<--exec> I] [B<-f>|B<--file> I] [I] [I ...] =head1 DESCRIPTION This command applies one or more sed(1) substitution operations to F or any number of files. =head1 OPTIONS The following command line options are available. =over 4 =item B<-v>, B<--verbose> Display some processing information. =item B<-t>, B<--trace> Enable the output of the essential shell commands which are executed. =item B<-n>, B<--nop> No operation mode. Actual execution of the essential shell commands which would be executed is suppressed. =item B<-w>, B<--warning> Show warning on substitution operation resulting in no content change on I file. The default is to show a warning on substitution operations resulted in no content change on I files. =item B<-q>, B<--quiet> Suppress warning on substitution operation resulting in no content change. =item B<-s>, B<--stealth> Stealth operation. Preserve timestamp on I. =item B<-i>, B<--interactive> Enter interactive mode where the user has to approve each operation. =item B<-b>, B<--backup> I Preserve backup of original file using file name extension I. Default is to overwrite the original file. =item B<-e>, B<--exec> I Specify sed(1) command directly. =item B<-f>, B<--file> I Read sed(1) command from I. =back =head1 EXAMPLE # shell script shtool subst -i -e 's;(c) \([0-9]*\)-2000;(c) \1-2001;' *.[ch] # RPM spec-file %install shtool subst -v -n \ -e 's;^\(prefix=\).*;\1 $RPM_BUILD_ROOT%{_prefix};g' \ -e 's;^\(sysconfdir=\).*;\1 $RPM_BUILD_ROOT%{_prefix}/etc;g' \ `find . -name Makefile -print` make install =head1 HISTORY The B B command was originally written by Ralf S. Engelschall Erse@@engelschall.comE in 2001 for B. It was prompted by the need to have a uniform and convenient patching frontend to sed(1) operations in the B package specifications. =head1 SEE ALSO shtool(1), sed(1). =cut @ 1.19 log @remove trailing spaces @ text @d3 1 a3 1 ## Copyright (c) 2001-2007 Ralf S. Engelschall @ 1.18 log @fix formatting error @ text @d149 1 a149 1 # optionally check whether any content change actually occurred @ 1.17 log @Adjust copyright for year 2007. @ text @d263 2 a264 2 Preserve backup of original file using file name extension I . Default is to overwrite the original file . @ 1.16 log @Adjust copyright for year 2006 @ text @d3 1 a3 1 ## Copyright (c) 2001-2006 Ralf S. Engelschall @ 1.15 log @Adjust copyright for year 2005 @ text @d3 1 a3 1 ## Copyright (c) 2001-2005 Ralf S. Engelschall @ 1.14 log @Add -w/--warning option to "shtool subst" command which shows warnings on substitution operation resulting in no content change on every file. The new default is now to show a warning on substitution operations resulted in no content change on all files only. @ text @d3 1 a3 1 ## Copyright (c) 2001-2004 Ralf S. Engelschall @ 1.13 log @Add option "-q" to "shtool subst" for suppressing a new warning on substitution operation resulting in no content change. @ text @d22 1 a22 1 str_usage="[-v|--verbose] [-t|--trace] [-n|--nop] [-q|--quiet] [-s|--stealth] [-i|--interactive] [-b|--backup ] [-e|--exec ] [-f|--file ] [] [...]" d25 2 a26 2 opt_spec="v.t.n.q.s.i.b:e+f:" opt_alias="v:verbose,t:trace,n:nop,q:quiet,s:stealth,i:interactive,b:backup,e:exec,f:file" d30 1 d42 1 d82 1 d152 5 a156 1 echo "$msgprefix:Warning: substitution operation results in no content change" 1>&2 d170 9 d210 1 d243 6 @ 1.12 log @correct spelling: privileges, convenient; @ text @d22 1 a22 1 str_usage="[-v|--verbose] [-t|--trace] [-n|--nop] [-s|--stealth] [-i|--interactive] [-b|--backup ] [-e|--exec ] [-f|--file ] [] [...]" d25 2 a26 2 opt_spec="v.t.n.s.i.b:e+f:" opt_alias="v:verbose,t:trace,n:nop,s:stealth,i:interactive,b:backup,e:exec,f:file" d30 1 d146 7 d194 1 d225 4 @ 1.11 log @Embedded the command documentation from shtool.pod into the individual sh.xxxx scripts and this way split the large shtool(1) manual page into individual manual pages shtool-xxxx(1). Submitted partly by: Thomas Lotterer @ text @d257 1 a257 1 It was prompted by the need to have a uniform and convinient patching @ 1.10 log @Bump copyright year for 2004 @ text @a3 1 ## Originally written for OpenPKG's rpmtool d57 1 a57 1 sedcmd="$sedcmd -e '$e'" d167 98 @ 1.9 log @Replace "[ x -o/-a x ]" constructs with "[ x ] ||/&& [ x ]" and "egrep"/"fgrep" constructs with compatible "grep" or "case" constructs. This way GNU shtool is now more POSIX 1003.1-2001 compliant. Submitted by: Paul Eggert @ text @d3 1 a3 1 ## Copyright (c) 2001-2003 Ralf S. Engelschall @ 1.8 log @When patching a file with "shtool subst" try to make it writeable first in case it has permissions set differently. @ text @d43 1 a43 1 if [ $# -eq 0 -a ".$opt_b" != . ]; then d47 1 a47 1 if [ $# -eq 0 -a ".$opt_s" = .yes ]; then @ 1.7 log @Bump year in copyright messages to cover new year 2003. @ text @d127 1 d132 1 @ 1.6 log @Fixed error exit in all commands by introducing an "shtool_exit" function (which correctly cleans up temporary files) and replacing all "exit" calls with "shtool_exit". THIS THE FIRST TIME INTRODUCED THE USAGE OF A SHELL FUNCTION TO THE SHTOOL CODE. This is known to break on _very_ anchient and broken Bourne-Shell implementations and is considered to be no longer a real problem in the year 2003. Especially there is no alternative way anyway. @ text @d3 1 a3 1 ## Copyright (c) 2001-2002 Ralf S. Engelschall @ 1.5 log @Port to POSIX 1003.1-2001 (SUSv3) standard: sh.echo, sh.version: "head -1" -> "sed -e 'q'" sh.path: "sort -u | tail -1" -> "sort -r -u | sed -e q" sh.subst: diff -u1 -> diff -U1 Submitted by: Paul Eggert @ text @d45 1 a45 1 exit 1 d49 1 a49 1 exit 1 d63 1 a63 1 exit 1 d68 1 a68 1 exit 1 @ 1.4 log @better error for directories @ text @d95 1 a95 1 (diff -u1 $file $file.new >$tmpfile) 2>/dev/null @ 1.3 log @bump copyright years @ text @d62 1 a62 1 echo "$msgprefix:Error: command file \`$opt_f' not found" 1>&2 d83 1 a83 1 echo "$msgprefix:Warning: file \`$file' not found" 1>&2 @ 1.2 log @*** empty log message *** @ text @d3 1 a3 1 ## Copyright (c) 2001 Ralf S. Engelschall @ 1.1 log @*** empty log message *** @ text @d23 1 a23 1 str_usage="[-v] [-t] [-n] [-s] [-i] [-b] [-e] [-f] [] [...]" d27 1 @