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