annotate src/README.global-renaming @ 5673:900a0a8796c3 r21-5-32

XEmacs 21.5.32 "habanero" is released.
author Stephen J. Turnbull <stephen@xemacs.org>
date Fri, 03 Aug 2012 02:43:45 +0900
parents 2aa9cd456ae7
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
868
48eed784e93a [xemacs-hg @ 2002-06-05 12:00:40 by ben]
ben
parents:
diff changeset
1 README.global-renaming
48eed784e93a [xemacs-hg @ 2002-06-05 12:00:40 by ben]
ben
parents:
diff changeset
2
48eed784e93a [xemacs-hg @ 2002-06-05 12:00:40 by ben]
ben
parents:
diff changeset
3 This file documents the generic scripts that have been used to implement
48eed784e93a [xemacs-hg @ 2002-06-05 12:00:40 by ben]
ben
parents:
diff changeset
4 the recent type renamings, e.g. the "great integral type renaming" and the
48eed784e93a [xemacs-hg @ 2002-06-05 12:00:40 by ben]
ben
parents:
diff changeset
5 "text/char type renaming". More information about these changes can be
48eed784e93a [xemacs-hg @ 2002-06-05 12:00:40 by ben]
ben
parents:
diff changeset
6 found in the Internals manual.
48eed784e93a [xemacs-hg @ 2002-06-05 12:00:40 by ben]
ben
parents:
diff changeset
7
48eed784e93a [xemacs-hg @ 2002-06-05 12:00:40 by ben]
ben
parents:
diff changeset
8 A sample script to do such renaming is this (used in the great integral
48eed784e93a [xemacs-hg @ 2002-06-05 12:00:40 by ben]
ben
parents:
diff changeset
9 type renaming):
48eed784e93a [xemacs-hg @ 2002-06-05 12:00:40 by ben]
ben
parents:
diff changeset
10
48eed784e93a [xemacs-hg @ 2002-06-05 12:00:40 by ben]
ben
parents:
diff changeset
11 ----------------------------------- cut ------------------------------------
48eed784e93a [xemacs-hg @ 2002-06-05 12:00:40 by ben]
ben
parents:
diff changeset
12 files="*.[ch] s/*.h m/*.h config.h.in ../configure.in Makefile.in.in ../lib-src/*.[ch] ../lwlib/*.[ch]"
48eed784e93a [xemacs-hg @ 2002-06-05 12:00:40 by ben]
ben
parents:
diff changeset
13 gr Memory_Count Bytecount $files
48eed784e93a [xemacs-hg @ 2002-06-05 12:00:40 by ben]
ben
parents:
diff changeset
14 gr Lstream_Data_Count Bytecount $files
48eed784e93a [xemacs-hg @ 2002-06-05 12:00:40 by ben]
ben
parents:
diff changeset
15 gr Element_Count Elemcount $files
48eed784e93a [xemacs-hg @ 2002-06-05 12:00:40 by ben]
ben
parents:
diff changeset
16 gr Hash_Code Hashcode $files
48eed784e93a [xemacs-hg @ 2002-06-05 12:00:40 by ben]
ben
parents:
diff changeset
17 gr extcount bytecount $files
48eed784e93a [xemacs-hg @ 2002-06-05 12:00:40 by ben]
ben
parents:
diff changeset
18 gr bufpos charbpos $files
48eed784e93a [xemacs-hg @ 2002-06-05 12:00:40 by ben]
ben
parents:
diff changeset
19 gr bytind bytebpos $files
48eed784e93a [xemacs-hg @ 2002-06-05 12:00:40 by ben]
ben
parents:
diff changeset
20 gr memind membpos $files
48eed784e93a [xemacs-hg @ 2002-06-05 12:00:40 by ben]
ben
parents:
diff changeset
21 gr bufbyte intbyte $files
48eed784e93a [xemacs-hg @ 2002-06-05 12:00:40 by ben]
ben
parents:
diff changeset
22 gr Extcount Bytecount $files
48eed784e93a [xemacs-hg @ 2002-06-05 12:00:40 by ben]
ben
parents:
diff changeset
23 gr Bufpos Charbpos $files
48eed784e93a [xemacs-hg @ 2002-06-05 12:00:40 by ben]
ben
parents:
diff changeset
24 gr Bytind Bytebpos $files
48eed784e93a [xemacs-hg @ 2002-06-05 12:00:40 by ben]
ben
parents:
diff changeset
25 gr Memind Membpos $files
48eed784e93a [xemacs-hg @ 2002-06-05 12:00:40 by ben]
ben
parents:
diff changeset
26 gr Bufbyte Intbyte $files
48eed784e93a [xemacs-hg @ 2002-06-05 12:00:40 by ben]
ben
parents:
diff changeset
27 gr EXTCOUNT BYTECOUNT $files
48eed784e93a [xemacs-hg @ 2002-06-05 12:00:40 by ben]
ben
parents:
diff changeset
28 gr BUFPOS CHARBPOS $files
48eed784e93a [xemacs-hg @ 2002-06-05 12:00:40 by ben]
ben
parents:
diff changeset
29 gr BYTIND BYTEBPOS $files
48eed784e93a [xemacs-hg @ 2002-06-05 12:00:40 by ben]
ben
parents:
diff changeset
30 gr MEMIND MEMBPOS $files
48eed784e93a [xemacs-hg @ 2002-06-05 12:00:40 by ben]
ben
parents:
diff changeset
31 gr BUFBYTE INTBYTE $files
48eed784e93a [xemacs-hg @ 2002-06-05 12:00:40 by ben]
ben
parents:
diff changeset
32 gr MEMORY_COUNT BYTECOUNT $files
48eed784e93a [xemacs-hg @ 2002-06-05 12:00:40 by ben]
ben
parents:
diff changeset
33 gr LSTREAM_DATA_COUNT BYTECOUNT $files
48eed784e93a [xemacs-hg @ 2002-06-05 12:00:40 by ben]
ben
parents:
diff changeset
34 gr ELEMENT_COUNT ELEMCOUNT $files
48eed784e93a [xemacs-hg @ 2002-06-05 12:00:40 by ben]
ben
parents:
diff changeset
35 gr HASH_CODE HASHCODE $files
48eed784e93a [xemacs-hg @ 2002-06-05 12:00:40 by ben]
ben
parents:
diff changeset
36 ----------------------------------- cut ------------------------------------
48eed784e93a [xemacs-hg @ 2002-06-05 12:00:40 by ben]
ben
parents:
diff changeset
37
48eed784e93a [xemacs-hg @ 2002-06-05 12:00:40 by ben]
ben
parents:
diff changeset
38
48eed784e93a [xemacs-hg @ 2002-06-05 12:00:40 by ben]
ben
parents:
diff changeset
39 `fixtypes.sh' is a Bourne-shell script; it uses 'gr':
48eed784e93a [xemacs-hg @ 2002-06-05 12:00:40 by ben]
ben
parents:
diff changeset
40
48eed784e93a [xemacs-hg @ 2002-06-05 12:00:40 by ben]
ben
parents:
diff changeset
41
48eed784e93a [xemacs-hg @ 2002-06-05 12:00:40 by ben]
ben
parents:
diff changeset
42 ----------------------------------- cut ------------------------------------
48eed784e93a [xemacs-hg @ 2002-06-05 12:00:40 by ben]
ben
parents:
diff changeset
43 #!/bin/sh
48eed784e93a [xemacs-hg @ 2002-06-05 12:00:40 by ben]
ben
parents:
diff changeset
44
48eed784e93a [xemacs-hg @ 2002-06-05 12:00:40 by ben]
ben
parents:
diff changeset
45 # Usage is like this:
48eed784e93a [xemacs-hg @ 2002-06-05 12:00:40 by ben]
ben
parents:
diff changeset
46
48eed784e93a [xemacs-hg @ 2002-06-05 12:00:40 by ben]
ben
parents:
diff changeset
47 # gr FROM TO FILES ...
48eed784e93a [xemacs-hg @ 2002-06-05 12:00:40 by ben]
ben
parents:
diff changeset
48
48eed784e93a [xemacs-hg @ 2002-06-05 12:00:40 by ben]
ben
parents:
diff changeset
49 # globally replace FROM with TO in FILES. FROM and TO are regular expressions.
48eed784e93a [xemacs-hg @ 2002-06-05 12:00:40 by ben]
ben
parents:
diff changeset
50 # backup files are stored in the `backup' directory.
48eed784e93a [xemacs-hg @ 2002-06-05 12:00:40 by ben]
ben
parents:
diff changeset
51 from="$1"
48eed784e93a [xemacs-hg @ 2002-06-05 12:00:40 by ben]
ben
parents:
diff changeset
52 to="$2"
48eed784e93a [xemacs-hg @ 2002-06-05 12:00:40 by ben]
ben
parents:
diff changeset
53 shift 2
48eed784e93a [xemacs-hg @ 2002-06-05 12:00:40 by ben]
ben
parents:
diff changeset
54 echo ${1+"$@"} | xargs global-replace "s/$from/$to/g"
48eed784e93a [xemacs-hg @ 2002-06-05 12:00:40 by ben]
ben
parents:
diff changeset
55 ----------------------------------- cut ------------------------------------
48eed784e93a [xemacs-hg @ 2002-06-05 12:00:40 by ben]
ben
parents:
diff changeset
56
48eed784e93a [xemacs-hg @ 2002-06-05 12:00:40 by ben]
ben
parents:
diff changeset
57
48eed784e93a [xemacs-hg @ 2002-06-05 12:00:40 by ben]
ben
parents:
diff changeset
58 `gr' in turn uses a Perl script to do its real work, `global-replace',
48eed784e93a [xemacs-hg @ 2002-06-05 12:00:40 by ben]
ben
parents:
diff changeset
59 which follows:
48eed784e93a [xemacs-hg @ 2002-06-05 12:00:40 by ben]
ben
parents:
diff changeset
60
48eed784e93a [xemacs-hg @ 2002-06-05 12:00:40 by ben]
ben
parents:
diff changeset
61
48eed784e93a [xemacs-hg @ 2002-06-05 12:00:40 by ben]
ben
parents:
diff changeset
62 ----------------------------------- cut ------------------------------------
48eed784e93a [xemacs-hg @ 2002-06-05 12:00:40 by ben]
ben
parents:
diff changeset
63 : #-*- Perl -*-
48eed784e93a [xemacs-hg @ 2002-06-05 12:00:40 by ben]
ben
parents:
diff changeset
64
48eed784e93a [xemacs-hg @ 2002-06-05 12:00:40 by ben]
ben
parents:
diff changeset
65 ### global-replace --- modify the contents of a file by a Perl expression
48eed784e93a [xemacs-hg @ 2002-06-05 12:00:40 by ben]
ben
parents:
diff changeset
66
48eed784e93a [xemacs-hg @ 2002-06-05 12:00:40 by ben]
ben
parents:
diff changeset
67 ## Copyright (C) 1999 Martin Buchholz.
48eed784e93a [xemacs-hg @ 2002-06-05 12:00:40 by ben]
ben
parents:
diff changeset
68 ## Copyright (C) 2001, 2002 Ben Wing.
48eed784e93a [xemacs-hg @ 2002-06-05 12:00:40 by ben]
ben
parents:
diff changeset
69
48eed784e93a [xemacs-hg @ 2002-06-05 12:00:40 by ben]
ben
parents:
diff changeset
70 ## Authors: Martin Buchholz <martin@xemacs.org>, Ben Wing <ben@xemacs.org>
48eed784e93a [xemacs-hg @ 2002-06-05 12:00:40 by ben]
ben
parents:
diff changeset
71 ## Maintainer: Ben Wing <ben@xemacs.org>
48eed784e93a [xemacs-hg @ 2002-06-05 12:00:40 by ben]
ben
parents:
diff changeset
72 ## Current Version: 1.2, March 12, 2002
48eed784e93a [xemacs-hg @ 2002-06-05 12:00:40 by ben]
ben
parents:
diff changeset
73
5405
2aa9cd456ae7 Move src/ to GPLv3.
Mike Sperber <sperber@deinprogramm.de>
parents: 868
diff changeset
74 # This program is free software: you can redistribute it and/or modify it
2aa9cd456ae7 Move src/ to GPLv3.
Mike Sperber <sperber@deinprogramm.de>
parents: 868
diff changeset
75 # under the terms of the GNU General Public License as published by the
2aa9cd456ae7 Move src/ to GPLv3.
Mike Sperber <sperber@deinprogramm.de>
parents: 868
diff changeset
76 # Free Software Foundation, either version 3 of the License, or (at your
2aa9cd456ae7 Move src/ to GPLv3.
Mike Sperber <sperber@deinprogramm.de>
parents: 868
diff changeset
77 # option) any later version.
2aa9cd456ae7 Move src/ to GPLv3.
Mike Sperber <sperber@deinprogramm.de>
parents: 868
diff changeset
78 #
2aa9cd456ae7 Move src/ to GPLv3.
Mike Sperber <sperber@deinprogramm.de>
parents: 868
diff changeset
79 # This program is distributed in the hope that it will be useful, but WITHOUT
2aa9cd456ae7 Move src/ to GPLv3.
Mike Sperber <sperber@deinprogramm.de>
parents: 868
diff changeset
80 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
2aa9cd456ae7 Move src/ to GPLv3.
Mike Sperber <sperber@deinprogramm.de>
parents: 868
diff changeset
81 # FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
2aa9cd456ae7 Move src/ to GPLv3.
Mike Sperber <sperber@deinprogramm.de>
parents: 868
diff changeset
82 # for more details.
2aa9cd456ae7 Move src/ to GPLv3.
Mike Sperber <sperber@deinprogramm.de>
parents: 868
diff changeset
83 #
868
48eed784e93a [xemacs-hg @ 2002-06-05 12:00:40 by ben]
ben
parents:
diff changeset
84 # You should have received a copy of the GNU General Public License
5405
2aa9cd456ae7 Move src/ to GPLv3.
Mike Sperber <sperber@deinprogramm.de>
parents: 868
diff changeset
85 # along with this program. If not, see <http://www.gnu.org/licenses/>.
868
48eed784e93a [xemacs-hg @ 2002-06-05 12:00:40 by ben]
ben
parents:
diff changeset
86
48eed784e93a [xemacs-hg @ 2002-06-05 12:00:40 by ben]
ben
parents:
diff changeset
87 eval 'exec perl -w -S $0 ${1+"$@"}'
48eed784e93a [xemacs-hg @ 2002-06-05 12:00:40 by ben]
ben
parents:
diff changeset
88 if 0;
48eed784e93a [xemacs-hg @ 2002-06-05 12:00:40 by ben]
ben
parents:
diff changeset
89
48eed784e93a [xemacs-hg @ 2002-06-05 12:00:40 by ben]
ben
parents:
diff changeset
90 use strict;
48eed784e93a [xemacs-hg @ 2002-06-05 12:00:40 by ben]
ben
parents:
diff changeset
91 use FileHandle;
48eed784e93a [xemacs-hg @ 2002-06-05 12:00:40 by ben]
ben
parents:
diff changeset
92 use Carp;
48eed784e93a [xemacs-hg @ 2002-06-05 12:00:40 by ben]
ben
parents:
diff changeset
93 use Getopt::Long;
48eed784e93a [xemacs-hg @ 2002-06-05 12:00:40 by ben]
ben
parents:
diff changeset
94 use File::Basename;
48eed784e93a [xemacs-hg @ 2002-06-05 12:00:40 by ben]
ben
parents:
diff changeset
95
48eed784e93a [xemacs-hg @ 2002-06-05 12:00:40 by ben]
ben
parents:
diff changeset
96 (my $myName = $0) =~ s@.*/@@; my $usage="
48eed784e93a [xemacs-hg @ 2002-06-05 12:00:40 by ben]
ben
parents:
diff changeset
97 Usage: $myName [--help] [--backup-dir=DIR] [--line-mode] [--hunk-mode]
48eed784e93a [xemacs-hg @ 2002-06-05 12:00:40 by ben]
ben
parents:
diff changeset
98 PERLEXPR FILE ...
48eed784e93a [xemacs-hg @ 2002-06-05 12:00:40 by ben]
ben
parents:
diff changeset
99
48eed784e93a [xemacs-hg @ 2002-06-05 12:00:40 by ben]
ben
parents:
diff changeset
100 Globally modify a file, either line by line or in one big hunk.
48eed784e93a [xemacs-hg @ 2002-06-05 12:00:40 by ben]
ben
parents:
diff changeset
101
48eed784e93a [xemacs-hg @ 2002-06-05 12:00:40 by ben]
ben
parents:
diff changeset
102 Typical usage is like this:
48eed784e93a [xemacs-hg @ 2002-06-05 12:00:40 by ben]
ben
parents:
diff changeset
103
48eed784e93a [xemacs-hg @ 2002-06-05 12:00:40 by ben]
ben
parents:
diff changeset
104 [with GNU print, GNU xargs: guaranteed to handle spaces, quotes, etc.
48eed784e93a [xemacs-hg @ 2002-06-05 12:00:40 by ben]
ben
parents:
diff changeset
105 in file names]
48eed784e93a [xemacs-hg @ 2002-06-05 12:00:40 by ben]
ben
parents:
diff changeset
106
48eed784e93a [xemacs-hg @ 2002-06-05 12:00:40 by ben]
ben
parents:
diff changeset
107 find . -name '*.[ch]' -print0 | xargs -0 $0 's/\bCONST\b/const/g'\n
48eed784e93a [xemacs-hg @ 2002-06-05 12:00:40 by ben]
ben
parents:
diff changeset
108
48eed784e93a [xemacs-hg @ 2002-06-05 12:00:40 by ben]
ben
parents:
diff changeset
109 [with non-GNU print, xargs]
48eed784e93a [xemacs-hg @ 2002-06-05 12:00:40 by ben]
ben
parents:
diff changeset
110
48eed784e93a [xemacs-hg @ 2002-06-05 12:00:40 by ben]
ben
parents:
diff changeset
111 find . -name '*.[ch]' -print | xargs $0 's/\bCONST\b/const/g'\n
48eed784e93a [xemacs-hg @ 2002-06-05 12:00:40 by ben]
ben
parents:
diff changeset
112
48eed784e93a [xemacs-hg @ 2002-06-05 12:00:40 by ben]
ben
parents:
diff changeset
113
48eed784e93a [xemacs-hg @ 2002-06-05 12:00:40 by ben]
ben
parents:
diff changeset
114 The file is read in, either line by line (with --line-mode specified)
48eed784e93a [xemacs-hg @ 2002-06-05 12:00:40 by ben]
ben
parents:
diff changeset
115 or in one big hunk (with --hunk-mode specified; it's the default), and
48eed784e93a [xemacs-hg @ 2002-06-05 12:00:40 by ben]
ben
parents:
diff changeset
116 the Perl expression is then evalled with \$_ set to the line or hunk of
48eed784e93a [xemacs-hg @ 2002-06-05 12:00:40 by ben]
ben
parents:
diff changeset
117 text, including the terminating newline if there is one. It should
48eed784e93a [xemacs-hg @ 2002-06-05 12:00:40 by ben]
ben
parents:
diff changeset
118 destructively modify the value there, storing the changed result in \$_.
48eed784e93a [xemacs-hg @ 2002-06-05 12:00:40 by ben]
ben
parents:
diff changeset
119
48eed784e93a [xemacs-hg @ 2002-06-05 12:00:40 by ben]
ben
parents:
diff changeset
120 Files in which any modifications are made are backed up to the directory
48eed784e93a [xemacs-hg @ 2002-06-05 12:00:40 by ben]
ben
parents:
diff changeset
121 specified using --backup-dir, or to `backup.orig' by default. To disable
48eed784e93a [xemacs-hg @ 2002-06-05 12:00:40 by ben]
ben
parents:
diff changeset
122 this, use --backup-dir= with no argument.
48eed784e93a [xemacs-hg @ 2002-06-05 12:00:40 by ben]
ben
parents:
diff changeset
123
48eed784e93a [xemacs-hg @ 2002-06-05 12:00:40 by ben]
ben
parents:
diff changeset
124 Hunk mode is the default because it is MUCH MUCH faster than line-by-line.
48eed784e93a [xemacs-hg @ 2002-06-05 12:00:40 by ben]
ben
parents:
diff changeset
125 Use line-by-line only when it matters, e.g. you want to do a replacement
48eed784e93a [xemacs-hg @ 2002-06-05 12:00:40 by ben]
ben
parents:
diff changeset
126 only once per line (the default without the `g' argument). Conversely,
48eed784e93a [xemacs-hg @ 2002-06-05 12:00:40 by ben]
ben
parents:
diff changeset
127 when using hunk mode, *ALWAYS* use `g'; otherwise, you will only make one
48eed784e93a [xemacs-hg @ 2002-06-05 12:00:40 by ben]
ben
parents:
diff changeset
128 replacement in the entire file!
48eed784e93a [xemacs-hg @ 2002-06-05 12:00:40 by ben]
ben
parents:
diff changeset
129 ";
48eed784e93a [xemacs-hg @ 2002-06-05 12:00:40 by ben]
ben
parents:
diff changeset
130
48eed784e93a [xemacs-hg @ 2002-06-05 12:00:40 by ben]
ben
parents:
diff changeset
131 my %options = ();
48eed784e93a [xemacs-hg @ 2002-06-05 12:00:40 by ben]
ben
parents:
diff changeset
132 $Getopt::Long::ignorecase = 0;
48eed784e93a [xemacs-hg @ 2002-06-05 12:00:40 by ben]
ben
parents:
diff changeset
133 &GetOptions (
48eed784e93a [xemacs-hg @ 2002-06-05 12:00:40 by ben]
ben
parents:
diff changeset
134 \%options,
48eed784e93a [xemacs-hg @ 2002-06-05 12:00:40 by ben]
ben
parents:
diff changeset
135 'help', 'backup-dir=s', 'line-mode', 'hunk-mode',
48eed784e93a [xemacs-hg @ 2002-06-05 12:00:40 by ben]
ben
parents:
diff changeset
136 );
48eed784e93a [xemacs-hg @ 2002-06-05 12:00:40 by ben]
ben
parents:
diff changeset
137
48eed784e93a [xemacs-hg @ 2002-06-05 12:00:40 by ben]
ben
parents:
diff changeset
138
48eed784e93a [xemacs-hg @ 2002-06-05 12:00:40 by ben]
ben
parents:
diff changeset
139 die $usage if $options{"help"} or @ARGV <= 1;
48eed784e93a [xemacs-hg @ 2002-06-05 12:00:40 by ben]
ben
parents:
diff changeset
140 my $code = shift;
48eed784e93a [xemacs-hg @ 2002-06-05 12:00:40 by ben]
ben
parents:
diff changeset
141
48eed784e93a [xemacs-hg @ 2002-06-05 12:00:40 by ben]
ben
parents:
diff changeset
142 die $usage if grep (-d || ! -w, @ARGV);
48eed784e93a [xemacs-hg @ 2002-06-05 12:00:40 by ben]
ben
parents:
diff changeset
143
48eed784e93a [xemacs-hg @ 2002-06-05 12:00:40 by ben]
ben
parents:
diff changeset
144 sub SafeOpen {
48eed784e93a [xemacs-hg @ 2002-06-05 12:00:40 by ben]
ben
parents:
diff changeset
145 open ((my $fh = new FileHandle), $_[0]);
48eed784e93a [xemacs-hg @ 2002-06-05 12:00:40 by ben]
ben
parents:
diff changeset
146 confess "Can't open $_[0]: $!" if ! defined $fh;
48eed784e93a [xemacs-hg @ 2002-06-05 12:00:40 by ben]
ben
parents:
diff changeset
147 return $fh;
48eed784e93a [xemacs-hg @ 2002-06-05 12:00:40 by ben]
ben
parents:
diff changeset
148 }
48eed784e93a [xemacs-hg @ 2002-06-05 12:00:40 by ben]
ben
parents:
diff changeset
149
48eed784e93a [xemacs-hg @ 2002-06-05 12:00:40 by ben]
ben
parents:
diff changeset
150 sub SafeClose {
48eed784e93a [xemacs-hg @ 2002-06-05 12:00:40 by ben]
ben
parents:
diff changeset
151 close $_[0] or confess "Can't close $_[0]: $!";
48eed784e93a [xemacs-hg @ 2002-06-05 12:00:40 by ben]
ben
parents:
diff changeset
152 }
48eed784e93a [xemacs-hg @ 2002-06-05 12:00:40 by ben]
ben
parents:
diff changeset
153
48eed784e93a [xemacs-hg @ 2002-06-05 12:00:40 by ben]
ben
parents:
diff changeset
154 sub FileContents {
48eed784e93a [xemacs-hg @ 2002-06-05 12:00:40 by ben]
ben
parents:
diff changeset
155 my $fh = SafeOpen ("< $_[0]");
48eed784e93a [xemacs-hg @ 2002-06-05 12:00:40 by ben]
ben
parents:
diff changeset
156 my $olddollarslash = $/;
48eed784e93a [xemacs-hg @ 2002-06-05 12:00:40 by ben]
ben
parents:
diff changeset
157 local $/ = undef;
48eed784e93a [xemacs-hg @ 2002-06-05 12:00:40 by ben]
ben
parents:
diff changeset
158 my $contents = <$fh>;
48eed784e93a [xemacs-hg @ 2002-06-05 12:00:40 by ben]
ben
parents:
diff changeset
159 $/ = $olddollarslash;
48eed784e93a [xemacs-hg @ 2002-06-05 12:00:40 by ben]
ben
parents:
diff changeset
160 return $contents;
48eed784e93a [xemacs-hg @ 2002-06-05 12:00:40 by ben]
ben
parents:
diff changeset
161 }
48eed784e93a [xemacs-hg @ 2002-06-05 12:00:40 by ben]
ben
parents:
diff changeset
162
48eed784e93a [xemacs-hg @ 2002-06-05 12:00:40 by ben]
ben
parents:
diff changeset
163 sub WriteStringToFile {
48eed784e93a [xemacs-hg @ 2002-06-05 12:00:40 by ben]
ben
parents:
diff changeset
164 my $fh = SafeOpen ("> $_[0]");
48eed784e93a [xemacs-hg @ 2002-06-05 12:00:40 by ben]
ben
parents:
diff changeset
165 binmode $fh;
48eed784e93a [xemacs-hg @ 2002-06-05 12:00:40 by ben]
ben
parents:
diff changeset
166 print $fh $_[1] or confess "$_[0]: $!\n";
48eed784e93a [xemacs-hg @ 2002-06-05 12:00:40 by ben]
ben
parents:
diff changeset
167 SafeClose $fh;
48eed784e93a [xemacs-hg @ 2002-06-05 12:00:40 by ben]
ben
parents:
diff changeset
168 }
48eed784e93a [xemacs-hg @ 2002-06-05 12:00:40 by ben]
ben
parents:
diff changeset
169
48eed784e93a [xemacs-hg @ 2002-06-05 12:00:40 by ben]
ben
parents:
diff changeset
170 foreach my $file (@ARGV) {
48eed784e93a [xemacs-hg @ 2002-06-05 12:00:40 by ben]
ben
parents:
diff changeset
171 my $changed_p = 0;
48eed784e93a [xemacs-hg @ 2002-06-05 12:00:40 by ben]
ben
parents:
diff changeset
172 my $new_contents = "";
48eed784e93a [xemacs-hg @ 2002-06-05 12:00:40 by ben]
ben
parents:
diff changeset
173 if ($options{"line-mode"}) {
48eed784e93a [xemacs-hg @ 2002-06-05 12:00:40 by ben]
ben
parents:
diff changeset
174 my $fh = SafeOpen $file;
48eed784e93a [xemacs-hg @ 2002-06-05 12:00:40 by ben]
ben
parents:
diff changeset
175 while (<$fh>) {
48eed784e93a [xemacs-hg @ 2002-06-05 12:00:40 by ben]
ben
parents:
diff changeset
176 my $save_line = $_;
48eed784e93a [xemacs-hg @ 2002-06-05 12:00:40 by ben]
ben
parents:
diff changeset
177 eval $code;
48eed784e93a [xemacs-hg @ 2002-06-05 12:00:40 by ben]
ben
parents:
diff changeset
178 $changed_p = 1 if $save_line ne $_;
48eed784e93a [xemacs-hg @ 2002-06-05 12:00:40 by ben]
ben
parents:
diff changeset
179 $new_contents .= $_;
48eed784e93a [xemacs-hg @ 2002-06-05 12:00:40 by ben]
ben
parents:
diff changeset
180 }
48eed784e93a [xemacs-hg @ 2002-06-05 12:00:40 by ben]
ben
parents:
diff changeset
181 } else {
48eed784e93a [xemacs-hg @ 2002-06-05 12:00:40 by ben]
ben
parents:
diff changeset
182 my $orig_contents = $_ = FileContents $file;
48eed784e93a [xemacs-hg @ 2002-06-05 12:00:40 by ben]
ben
parents:
diff changeset
183 eval $code;
48eed784e93a [xemacs-hg @ 2002-06-05 12:00:40 by ben]
ben
parents:
diff changeset
184 if ($_ ne $orig_contents) {
48eed784e93a [xemacs-hg @ 2002-06-05 12:00:40 by ben]
ben
parents:
diff changeset
185 $changed_p = 1;
48eed784e93a [xemacs-hg @ 2002-06-05 12:00:40 by ben]
ben
parents:
diff changeset
186 $new_contents = $_;
48eed784e93a [xemacs-hg @ 2002-06-05 12:00:40 by ben]
ben
parents:
diff changeset
187 }
48eed784e93a [xemacs-hg @ 2002-06-05 12:00:40 by ben]
ben
parents:
diff changeset
188 }
48eed784e93a [xemacs-hg @ 2002-06-05 12:00:40 by ben]
ben
parents:
diff changeset
189
48eed784e93a [xemacs-hg @ 2002-06-05 12:00:40 by ben]
ben
parents:
diff changeset
190 if ($changed_p) {
48eed784e93a [xemacs-hg @ 2002-06-05 12:00:40 by ben]
ben
parents:
diff changeset
191 my $backdir = $options{"backup-dir"};
48eed784e93a [xemacs-hg @ 2002-06-05 12:00:40 by ben]
ben
parents:
diff changeset
192 $backdir = "backup.orig" if !defined ($backdir);
48eed784e93a [xemacs-hg @ 2002-06-05 12:00:40 by ben]
ben
parents:
diff changeset
193 if ($backdir) {
48eed784e93a [xemacs-hg @ 2002-06-05 12:00:40 by ben]
ben
parents:
diff changeset
194 my ($name, $path, $suffix) = fileparse ($file, "");
48eed784e93a [xemacs-hg @ 2002-06-05 12:00:40 by ben]
ben
parents:
diff changeset
195 my $backfulldir = $path . $backdir;
48eed784e93a [xemacs-hg @ 2002-06-05 12:00:40 by ben]
ben
parents:
diff changeset
196 my $backfile = "$backfulldir/$name";
48eed784e93a [xemacs-hg @ 2002-06-05 12:00:40 by ben]
ben
parents:
diff changeset
197 mkdir $backfulldir, 0755 unless -d $backfulldir;
48eed784e93a [xemacs-hg @ 2002-06-05 12:00:40 by ben]
ben
parents:
diff changeset
198 print "modifying $file (original saved in $backfile)\n";
48eed784e93a [xemacs-hg @ 2002-06-05 12:00:40 by ben]
ben
parents:
diff changeset
199 rename $file, $backfile;
48eed784e93a [xemacs-hg @ 2002-06-05 12:00:40 by ben]
ben
parents:
diff changeset
200 }
48eed784e93a [xemacs-hg @ 2002-06-05 12:00:40 by ben]
ben
parents:
diff changeset
201 WriteStringToFile ($file, $new_contents);
48eed784e93a [xemacs-hg @ 2002-06-05 12:00:40 by ben]
ben
parents:
diff changeset
202 }
48eed784e93a [xemacs-hg @ 2002-06-05 12:00:40 by ben]
ben
parents:
diff changeset
203 }
48eed784e93a [xemacs-hg @ 2002-06-05 12:00:40 by ben]
ben
parents:
diff changeset
204 ----------------------------------- cut ------------------------------------