annotate src/README.global-renaming @ 5464:e79916901603 r21-5-30

XEmacs 21.5.30 "garlic" is released.
author Stephen J. Turnbull <stephen@xemacs.org>
date Wed, 27 Apr 2011 01:24:28 +0900
parents 48eed784e93a
children 2aa9cd456ae7
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
48eed784e93a [xemacs-hg @ 2002-06-05 12:00:40 by ben]
ben
parents:
diff changeset
74 # This program is free software; you can redistribute it and/or modify
48eed784e93a [xemacs-hg @ 2002-06-05 12:00:40 by ben]
ben
parents:
diff changeset
75 # it under the terms of the GNU General Public License as published by
48eed784e93a [xemacs-hg @ 2002-06-05 12:00:40 by ben]
ben
parents:
diff changeset
76 # the Free Software Foundation; either version 2, or (at your option)
48eed784e93a [xemacs-hg @ 2002-06-05 12:00:40 by ben]
ben
parents:
diff changeset
77 # any later version.
48eed784e93a [xemacs-hg @ 2002-06-05 12:00:40 by ben]
ben
parents:
diff changeset
78 #
48eed784e93a [xemacs-hg @ 2002-06-05 12:00:40 by ben]
ben
parents:
diff changeset
79 # This program is distributed in the hope that it will be useful, but
48eed784e93a [xemacs-hg @ 2002-06-05 12:00:40 by ben]
ben
parents:
diff changeset
80 # WITHOUT ANY WARRANTY; without even the implied warranty of
48eed784e93a [xemacs-hg @ 2002-06-05 12:00:40 by ben]
ben
parents:
diff changeset
81 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
48eed784e93a [xemacs-hg @ 2002-06-05 12:00:40 by ben]
ben
parents:
diff changeset
82 # General Public License for more details.
48eed784e93a [xemacs-hg @ 2002-06-05 12:00:40 by ben]
ben
parents:
diff changeset
83 #
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
48eed784e93a [xemacs-hg @ 2002-06-05 12:00:40 by ben]
ben
parents:
diff changeset
85 # along with XEmacs; see the file COPYING. If not, write to the Free
48eed784e93a [xemacs-hg @ 2002-06-05 12:00:40 by ben]
ben
parents:
diff changeset
86 # Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
48eed784e93a [xemacs-hg @ 2002-06-05 12:00:40 by ben]
ben
parents:
diff changeset
87 # 02111-1307, USA.
48eed784e93a [xemacs-hg @ 2002-06-05 12:00:40 by ben]
ben
parents:
diff changeset
88
48eed784e93a [xemacs-hg @ 2002-06-05 12:00:40 by ben]
ben
parents:
diff changeset
89 eval 'exec perl -w -S $0 ${1+"$@"}'
48eed784e93a [xemacs-hg @ 2002-06-05 12:00:40 by ben]
ben
parents:
diff changeset
90 if 0;
48eed784e93a [xemacs-hg @ 2002-06-05 12:00:40 by ben]
ben
parents:
diff changeset
91
48eed784e93a [xemacs-hg @ 2002-06-05 12:00:40 by ben]
ben
parents:
diff changeset
92 use strict;
48eed784e93a [xemacs-hg @ 2002-06-05 12:00:40 by ben]
ben
parents:
diff changeset
93 use FileHandle;
48eed784e93a [xemacs-hg @ 2002-06-05 12:00:40 by ben]
ben
parents:
diff changeset
94 use Carp;
48eed784e93a [xemacs-hg @ 2002-06-05 12:00:40 by ben]
ben
parents:
diff changeset
95 use Getopt::Long;
48eed784e93a [xemacs-hg @ 2002-06-05 12:00:40 by ben]
ben
parents:
diff changeset
96 use File::Basename;
48eed784e93a [xemacs-hg @ 2002-06-05 12:00:40 by ben]
ben
parents:
diff changeset
97
48eed784e93a [xemacs-hg @ 2002-06-05 12:00:40 by ben]
ben
parents:
diff changeset
98 (my $myName = $0) =~ s@.*/@@; my $usage="
48eed784e93a [xemacs-hg @ 2002-06-05 12:00:40 by ben]
ben
parents:
diff changeset
99 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
100 PERLEXPR FILE ...
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 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
103
48eed784e93a [xemacs-hg @ 2002-06-05 12:00:40 by ben]
ben
parents:
diff changeset
104 Typical usage is like this:
48eed784e93a [xemacs-hg @ 2002-06-05 12:00:40 by ben]
ben
parents:
diff changeset
105
48eed784e93a [xemacs-hg @ 2002-06-05 12:00:40 by ben]
ben
parents:
diff changeset
106 [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
107 in file names]
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 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
110
48eed784e93a [xemacs-hg @ 2002-06-05 12:00:40 by ben]
ben
parents:
diff changeset
111 [with non-GNU print, xargs]
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 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
114
48eed784e93a [xemacs-hg @ 2002-06-05 12:00:40 by ben]
ben
parents:
diff changeset
115
48eed784e93a [xemacs-hg @ 2002-06-05 12:00:40 by ben]
ben
parents:
diff changeset
116 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
117 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
118 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
119 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
120 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
121
48eed784e93a [xemacs-hg @ 2002-06-05 12:00:40 by ben]
ben
parents:
diff changeset
122 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
123 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
124 this, use --backup-dir= with no argument.
48eed784e93a [xemacs-hg @ 2002-06-05 12:00:40 by ben]
ben
parents:
diff changeset
125
48eed784e93a [xemacs-hg @ 2002-06-05 12:00:40 by ben]
ben
parents:
diff changeset
126 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
127 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
128 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
129 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
130 replacement in the entire file!
48eed784e93a [xemacs-hg @ 2002-06-05 12:00:40 by ben]
ben
parents:
diff changeset
131 ";
48eed784e93a [xemacs-hg @ 2002-06-05 12:00:40 by ben]
ben
parents:
diff changeset
132
48eed784e93a [xemacs-hg @ 2002-06-05 12:00:40 by ben]
ben
parents:
diff changeset
133 my %options = ();
48eed784e93a [xemacs-hg @ 2002-06-05 12:00:40 by ben]
ben
parents:
diff changeset
134 $Getopt::Long::ignorecase = 0;
48eed784e93a [xemacs-hg @ 2002-06-05 12:00:40 by ben]
ben
parents:
diff changeset
135 &GetOptions (
48eed784e93a [xemacs-hg @ 2002-06-05 12:00:40 by ben]
ben
parents:
diff changeset
136 \%options,
48eed784e93a [xemacs-hg @ 2002-06-05 12:00:40 by ben]
ben
parents:
diff changeset
137 'help', 'backup-dir=s', 'line-mode', 'hunk-mode',
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
48eed784e93a [xemacs-hg @ 2002-06-05 12:00:40 by ben]
ben
parents:
diff changeset
140
48eed784e93a [xemacs-hg @ 2002-06-05 12:00:40 by ben]
ben
parents:
diff changeset
141 die $usage if $options{"help"} or @ARGV <= 1;
48eed784e93a [xemacs-hg @ 2002-06-05 12:00:40 by ben]
ben
parents:
diff changeset
142 my $code = shift;
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 die $usage if grep (-d || ! -w, @ARGV);
48eed784e93a [xemacs-hg @ 2002-06-05 12:00:40 by ben]
ben
parents:
diff changeset
145
48eed784e93a [xemacs-hg @ 2002-06-05 12:00:40 by ben]
ben
parents:
diff changeset
146 sub SafeOpen {
48eed784e93a [xemacs-hg @ 2002-06-05 12:00:40 by ben]
ben
parents:
diff changeset
147 open ((my $fh = new FileHandle), $_[0]);
48eed784e93a [xemacs-hg @ 2002-06-05 12:00:40 by ben]
ben
parents:
diff changeset
148 confess "Can't open $_[0]: $!" if ! defined $fh;
48eed784e93a [xemacs-hg @ 2002-06-05 12:00:40 by ben]
ben
parents:
diff changeset
149 return $fh;
48eed784e93a [xemacs-hg @ 2002-06-05 12:00:40 by ben]
ben
parents:
diff changeset
150 }
48eed784e93a [xemacs-hg @ 2002-06-05 12:00:40 by ben]
ben
parents:
diff changeset
151
48eed784e93a [xemacs-hg @ 2002-06-05 12:00:40 by ben]
ben
parents:
diff changeset
152 sub SafeClose {
48eed784e93a [xemacs-hg @ 2002-06-05 12:00:40 by ben]
ben
parents:
diff changeset
153 close $_[0] or confess "Can't close $_[0]: $!";
48eed784e93a [xemacs-hg @ 2002-06-05 12:00:40 by ben]
ben
parents:
diff changeset
154 }
48eed784e93a [xemacs-hg @ 2002-06-05 12:00:40 by ben]
ben
parents:
diff changeset
155
48eed784e93a [xemacs-hg @ 2002-06-05 12:00:40 by ben]
ben
parents:
diff changeset
156 sub FileContents {
48eed784e93a [xemacs-hg @ 2002-06-05 12:00:40 by ben]
ben
parents:
diff changeset
157 my $fh = SafeOpen ("< $_[0]");
48eed784e93a [xemacs-hg @ 2002-06-05 12:00:40 by ben]
ben
parents:
diff changeset
158 my $olddollarslash = $/;
48eed784e93a [xemacs-hg @ 2002-06-05 12:00:40 by ben]
ben
parents:
diff changeset
159 local $/ = undef;
48eed784e93a [xemacs-hg @ 2002-06-05 12:00:40 by ben]
ben
parents:
diff changeset
160 my $contents = <$fh>;
48eed784e93a [xemacs-hg @ 2002-06-05 12:00:40 by ben]
ben
parents:
diff changeset
161 $/ = $olddollarslash;
48eed784e93a [xemacs-hg @ 2002-06-05 12:00:40 by ben]
ben
parents:
diff changeset
162 return $contents;
48eed784e93a [xemacs-hg @ 2002-06-05 12:00:40 by ben]
ben
parents:
diff changeset
163 }
48eed784e93a [xemacs-hg @ 2002-06-05 12:00:40 by ben]
ben
parents:
diff changeset
164
48eed784e93a [xemacs-hg @ 2002-06-05 12:00:40 by ben]
ben
parents:
diff changeset
165 sub WriteStringToFile {
48eed784e93a [xemacs-hg @ 2002-06-05 12:00:40 by ben]
ben
parents:
diff changeset
166 my $fh = SafeOpen ("> $_[0]");
48eed784e93a [xemacs-hg @ 2002-06-05 12:00:40 by ben]
ben
parents:
diff changeset
167 binmode $fh;
48eed784e93a [xemacs-hg @ 2002-06-05 12:00:40 by ben]
ben
parents:
diff changeset
168 print $fh $_[1] or confess "$_[0]: $!\n";
48eed784e93a [xemacs-hg @ 2002-06-05 12:00:40 by ben]
ben
parents:
diff changeset
169 SafeClose $fh;
48eed784e93a [xemacs-hg @ 2002-06-05 12:00:40 by ben]
ben
parents:
diff changeset
170 }
48eed784e93a [xemacs-hg @ 2002-06-05 12:00:40 by ben]
ben
parents:
diff changeset
171
48eed784e93a [xemacs-hg @ 2002-06-05 12:00:40 by ben]
ben
parents:
diff changeset
172 foreach my $file (@ARGV) {
48eed784e93a [xemacs-hg @ 2002-06-05 12:00:40 by ben]
ben
parents:
diff changeset
173 my $changed_p = 0;
48eed784e93a [xemacs-hg @ 2002-06-05 12:00:40 by ben]
ben
parents:
diff changeset
174 my $new_contents = "";
48eed784e93a [xemacs-hg @ 2002-06-05 12:00:40 by ben]
ben
parents:
diff changeset
175 if ($options{"line-mode"}) {
48eed784e93a [xemacs-hg @ 2002-06-05 12:00:40 by ben]
ben
parents:
diff changeset
176 my $fh = SafeOpen $file;
48eed784e93a [xemacs-hg @ 2002-06-05 12:00:40 by ben]
ben
parents:
diff changeset
177 while (<$fh>) {
48eed784e93a [xemacs-hg @ 2002-06-05 12:00:40 by ben]
ben
parents:
diff changeset
178 my $save_line = $_;
48eed784e93a [xemacs-hg @ 2002-06-05 12:00:40 by ben]
ben
parents:
diff changeset
179 eval $code;
48eed784e93a [xemacs-hg @ 2002-06-05 12:00:40 by ben]
ben
parents:
diff changeset
180 $changed_p = 1 if $save_line ne $_;
48eed784e93a [xemacs-hg @ 2002-06-05 12:00:40 by ben]
ben
parents:
diff changeset
181 $new_contents .= $_;
48eed784e93a [xemacs-hg @ 2002-06-05 12:00:40 by ben]
ben
parents:
diff changeset
182 }
48eed784e93a [xemacs-hg @ 2002-06-05 12:00:40 by ben]
ben
parents:
diff changeset
183 } else {
48eed784e93a [xemacs-hg @ 2002-06-05 12:00:40 by ben]
ben
parents:
diff changeset
184 my $orig_contents = $_ = FileContents $file;
48eed784e93a [xemacs-hg @ 2002-06-05 12:00:40 by ben]
ben
parents:
diff changeset
185 eval $code;
48eed784e93a [xemacs-hg @ 2002-06-05 12:00:40 by ben]
ben
parents:
diff changeset
186 if ($_ ne $orig_contents) {
48eed784e93a [xemacs-hg @ 2002-06-05 12:00:40 by ben]
ben
parents:
diff changeset
187 $changed_p = 1;
48eed784e93a [xemacs-hg @ 2002-06-05 12:00:40 by ben]
ben
parents:
diff changeset
188 $new_contents = $_;
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 }
48eed784e93a [xemacs-hg @ 2002-06-05 12:00:40 by ben]
ben
parents:
diff changeset
191
48eed784e93a [xemacs-hg @ 2002-06-05 12:00:40 by ben]
ben
parents:
diff changeset
192 if ($changed_p) {
48eed784e93a [xemacs-hg @ 2002-06-05 12:00:40 by ben]
ben
parents:
diff changeset
193 my $backdir = $options{"backup-dir"};
48eed784e93a [xemacs-hg @ 2002-06-05 12:00:40 by ben]
ben
parents:
diff changeset
194 $backdir = "backup.orig" if !defined ($backdir);
48eed784e93a [xemacs-hg @ 2002-06-05 12:00:40 by ben]
ben
parents:
diff changeset
195 if ($backdir) {
48eed784e93a [xemacs-hg @ 2002-06-05 12:00:40 by ben]
ben
parents:
diff changeset
196 my ($name, $path, $suffix) = fileparse ($file, "");
48eed784e93a [xemacs-hg @ 2002-06-05 12:00:40 by ben]
ben
parents:
diff changeset
197 my $backfulldir = $path . $backdir;
48eed784e93a [xemacs-hg @ 2002-06-05 12:00:40 by ben]
ben
parents:
diff changeset
198 my $backfile = "$backfulldir/$name";
48eed784e93a [xemacs-hg @ 2002-06-05 12:00:40 by ben]
ben
parents:
diff changeset
199 mkdir $backfulldir, 0755 unless -d $backfulldir;
48eed784e93a [xemacs-hg @ 2002-06-05 12:00:40 by ben]
ben
parents:
diff changeset
200 print "modifying $file (original saved in $backfile)\n";
48eed784e93a [xemacs-hg @ 2002-06-05 12:00:40 by ben]
ben
parents:
diff changeset
201 rename $file, $backfile;
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 WriteStringToFile ($file, $new_contents);
48eed784e93a [xemacs-hg @ 2002-06-05 12:00:40 by ben]
ben
parents:
diff changeset
204 }
48eed784e93a [xemacs-hg @ 2002-06-05 12:00:40 by ben]
ben
parents:
diff changeset
205 }
48eed784e93a [xemacs-hg @ 2002-06-05 12:00:40 by ben]
ben
parents:
diff changeset
206 ----------------------------------- cut ------------------------------------