| 1 |
|
|---|
| 2 |
|
|---|
| 3 |
|
|---|
| 4 |
|
|---|
| 5 |
|
|---|
| 6 |
|
|---|
| 7 |
|
|---|
| 8 |
|
|---|
| 9 |
|
|---|
| 10 |
|
|---|
| 11 |
|
|---|
| 12 |
|
|---|
| 13 |
|
|---|
| 14 |
|
|---|
| 15 |
|
|---|
| 16 |
|
|---|
| 17 |
|
|---|
| 18 |
|
|---|
| 19 |
|
|---|
| 20 |
|
|---|
| 21 |
|
|---|
| 22 |
|
|---|
| 23 |
|
|---|
| 24 |
|
|---|
| 25 |
|
|---|
| 26 |
|
|---|
| 27 |
|
|---|
| 28 |
|
|---|
| 29 |
|
|---|
| 30 |
|
|---|
| 31 |
|
|---|
| 32 |
|
|---|
| 33 |
|
|---|
| 34 |
|
|---|
| 35 |
|
|---|
| 36 |
die "Usage: update.pl old-file new-file\n" unless $#ARGV == 1; |
|---|
| 37 |
|
|---|
| 38 |
$old = $ARGV[0]; |
|---|
| 39 |
$bak = $old . ".bak"; |
|---|
| 40 |
$new = $ARGV[1]; |
|---|
| 41 |
|
|---|
| 42 |
|
|---|
| 43 |
|
|---|
| 44 |
if (-r $old) { |
|---|
| 45 |
print "*** Backing up $old to $bak...\n"; |
|---|
| 46 |
die "update.pl: Unable to open $old\n" unless open(OLD,"$old"); |
|---|
| 47 |
die "update.pl: Unable to open $bak\n" unless open(BAK,">$bak"); |
|---|
| 48 |
print BAK <OLD>; |
|---|
| 49 |
close(BAK); |
|---|
| 50 |
close(OLD); |
|---|
| 51 |
} |
|---|
| 52 |
|
|---|
| 53 |
|
|---|
| 54 |
if (-r $old) { |
|---|
| 55 |
print "*** Reading your settings from $old...\n"; |
|---|
| 56 |
die "update.pl: Unable to open $old\n" unless open(OLD,"$old"); |
|---|
| 57 |
while (<OLD>) { |
|---|
| 58 |
|
|---|
| 59 |
|
|---|
| 60 |
|
|---|
| 61 |
|
|---|
| 62 |
s#/\*\s*\*/##; |
|---|
| 63 |
s#[ \t]+([\r\n]*)$#$1#; |
|---|
| 64 |
if ( /^#define\s+([A-Z0-9_-]+).*\\$/ ) { |
|---|
| 65 |
|
|---|
| 66 |
chop($next = <OLD>); |
|---|
| 67 |
$defs{$1} = $next; |
|---|
| 68 |
$comment{$1} = $comment; |
|---|
| 69 |
} elsif ( m!^#define\s+([A-Z0-9_-]+)\s+(.+)\s+(/\*.*\*/)! |
|---|
| 70 |
) { |
|---|
| 71 |
|
|---|
| 72 |
$name = $1; |
|---|
| 73 |
$comment{$name} = $3; |
|---|
| 74 |
$defs{$name} = $2; |
|---|
| 75 |
undef $comment; |
|---|
| 76 |
} elsif ( m!^#define\s+([A-Z0-9_-]+)\s+(.+)! |
|---|
| 77 |
) { |
|---|
| 78 |
|
|---|
| 79 |
$defs{$1} = $2; |
|---|
| 80 |
$comment{$1} = $comment; |
|---|
| 81 |
} elsif ( /^#undef\s+([A-Z0-9_-]+)/ |
|---|
| 82 |
) { |
|---|
| 83 |
|
|---|
| 84 |
$defs{$1} = 'undef'; |
|---|
| 85 |
$comment{$1} = $comment; |
|---|
| 86 |
} elsif ( m!^/\*\s*#define\s+([A-Z0-9_-]+)\s+(.+)\s+(/\*.*\*/)! |
|---|
| 87 |
) { |
|---|
| 88 |
|
|---|
| 89 |
$name = $1; |
|---|
| 90 |
$comment{$name} = $3; |
|---|
| 91 |
$cvaldef{$name}++; |
|---|
| 92 |
$defs{$name} = $2; |
|---|
| 93 |
undef $comment; |
|---|
| 94 |
} elsif ( /^(\/\*)*\s*#define\s+([A-Z0-9-][A-Z0-9_-]+)/ |
|---|
| 95 |
) { |
|---|
| 96 |
|
|---|
| 97 |
$defs{$2} = ($1 eq "/*") ? 'undef' : 'define'; |
|---|
| 98 |
$comment{$2} = $comment; |
|---|
| 99 |
} else { |
|---|
| 100 |
if (m#^\s*/\*#) { |
|---|
| 101 |
|
|---|
| 102 |
$incomment = 1; |
|---|
| 103 |
undef $comment; |
|---|
| 104 |
} |
|---|
| 105 |
if ($incomment) { |
|---|
| 106 |
$comment = $comment . $_; |
|---|
| 107 |
if (m#\*/\s+$#) { |
|---|
| 108 |
|
|---|
| 109 |
$incomment = 0; |
|---|
| 110 |
} |
|---|
| 111 |
} |
|---|
| 112 |
} |
|---|
| 113 |
} |
|---|
| 114 |
close(OLD); |
|---|
| 115 |
} |
|---|
| 116 |
undef $comment; $incomment = 0; |
|---|
| 117 |
|
|---|
| 118 |
|
|---|
| 119 |
|
|---|
| 120 |
|
|---|
| 121 |
if ($settings = $ENV{'DEFINE'}) { |
|---|
| 122 |
print "\n*** Found a DEFINE environment variable - applying settings...\n"; |
|---|
| 123 |
@pairs = split ' ', $settings; |
|---|
| 124 |
foreach (@pairs) { |
|---|
| 125 |
if (($d,$v) = /(.+)=(.+)/) { |
|---|
| 126 |
$defs{$d} = $v; |
|---|
| 127 |
} else { |
|---|
| 128 |
$defs{$_} = 'define'; |
|---|
| 129 |
} |
|---|
| 130 |
} |
|---|
| 131 |
} |
|---|
| 132 |
if ($settings = $ENV{'UNDEFINE'}) { |
|---|
| 133 |
print "\n*** Found an UNDEFINE environment variable - applying settings...\n"; |
|---|
| 134 |
@pairs = split ' ', $settings; |
|---|
| 135 |
foreach (@pairs) { |
|---|
| 136 |
$defs{$_} = 'undef'; |
|---|
| 137 |
} |
|---|
| 138 |
} |
|---|
| 139 |
|
|---|
| 140 |
|
|---|
| 141 |
|
|---|
| 142 |
|
|---|
| 143 |
print "*** Updating $old from $new...\n"; |
|---|
| 144 |
die "update.pl: Unable to open $old\n" unless open(OLD,">$old"); |
|---|
| 145 |
die "update.pl: Unable to open $new\n" unless open(NEW,"$new"); |
|---|
| 146 |
$_ = <NEW>; |
|---|
| 147 |
while ($next = <NEW>) { |
|---|
| 148 |
|
|---|
| 149 |
|
|---|
| 150 |
s#[ \t]+([\r\n]*)$#$1#; |
|---|
| 151 |
if ( /^#define\s+([A-Z0-9_-]+).*\\$/ |
|---|
| 152 |
) { |
|---|
| 153 |
|
|---|
| 154 |
print OLD "#define $1 \\\n"; |
|---|
| 155 |
&ask_value($1,$next) if (!defined($defs{$1})); |
|---|
| 156 |
print OLD $defs{$1}; |
|---|
| 157 |
delete $defs{$1}; |
|---|
| 158 |
$next = <NEW>; |
|---|
| 159 |
} elsif ( /^#define\s+([A-Z0-9-][A-Z0-9_-]+)\s+\/\*\s*\*\//) { |
|---|
| 160 |
|
|---|
| 161 |
print OLD defined($defs{$1}) ? &def($1) |
|---|
| 162 |
: &def(&ask_simple($1,'define')); |
|---|
| 163 |
} elsif ( m!^(/\*\s*)?#define\s+([A-Z0-9_-]+)\s+(.+)\s+(/\*.*\*/)!) { |
|---|
| 164 |
|
|---|
| 165 |
$maybeundef = $1; |
|---|
| 166 |
$maybecomment = $4; $name = $2; |
|---|
| 167 |
$olddef = $def = $3; |
|---|
| 168 |
$comment = $maybecomment if ($maybecomment =~ /\w/); |
|---|
| 169 |
$def = "undef" if $maybeundef =~ /./; |
|---|
| 170 |
print OLD defined($defs{$name}) |
|---|
| 171 |
? &def($name,$comment) : &def(&ask_value($name,$def),$maybecomment,$def eq "undef" ? $olddef : $def); |
|---|
| 172 |
} elsif ( m!^#define\s+([A-Z0-9_-]+)\s+(.+)!) { |
|---|
| 173 |
|
|---|
| 174 |
print OLD defined($defs{$1}) ? &def($1) : &def(&ask_value($1,$2)); |
|---|
| 175 |
} elsif ( /^#undef\s+([A-Z0-9_-]+)/ |
|---|
| 176 |
) { |
|---|
| 177 |
print OLD defined($defs{$1}) ? &def($1) |
|---|
| 178 |
: &def(&ask_simple($1,'undef')); |
|---|
| 179 |
} elsif ( /^(\/\*)*\s*#define\s+([A-Z0-9-][A-Z0-9_-]+)/ |
|---|
| 180 |
) { |
|---|
| 181 |
|
|---|
| 182 |
print OLD defined($defs{$2}) ? |
|---|
| 183 |
&def($2) |
|---|
| 184 |
: &def(&ask_simple($2,($1 eq "/*" ? 'undef': 'define'))); |
|---|
| 185 |
} else { |
|---|
| 186 |
if (m#^\s*/\*#) { |
|---|
| 187 |
|
|---|
| 188 |
$incomment = 1; |
|---|
| 189 |
undef $comment; |
|---|
| 190 |
} |
|---|
| 191 |
if ($incomment) { |
|---|
| 192 |
$comment = $comment . $_; |
|---|
| 193 |
if (m#\*/\s+$#) { |
|---|
| 194 |
|
|---|
| 195 |
$incomment = 0; |
|---|
| 196 |
} |
|---|
| 197 |
} |
|---|
| 198 |
print OLD; |
|---|
| 199 |
} |
|---|
| 200 |
$_ = $next; |
|---|
| 201 |
} |
|---|
| 202 |
|
|---|
| 203 |
|
|---|
| 204 |
$final = $_; |
|---|
| 205 |
close(NEW); |
|---|
| 206 |
|
|---|
| 207 |
|
|---|
| 208 |
|
|---|
| 209 |
print "\n*** Checking for leftover defines from $old...\n"; |
|---|
| 210 |
foreach $d (keys %defs) { |
|---|
| 211 |
print "\nI found: $d\n"; |
|---|
| 212 |
if ($defs{$d} eq 'undef') { |
|---|
| 213 |
print "Currently undefined\n"; |
|---|
| 214 |
} elsif ($defs{$d} eq 'define') { |
|---|
| 215 |
print "Currently defined\n"; |
|---|
| 216 |
} else { |
|---|
| 217 |
print "Definition: $defs{$d}\n"; |
|---|
| 218 |
} |
|---|
| 219 |
print $comment{$d}; |
|---|
| 220 |
print "\n"; |
|---|
| 221 |
print "If this is a define that you hacked in, you probably should retain it.\n"; |
|---|
| 222 |
print "If not, it's probably an obsolete define from an earlier patchlevel,\n"; |
|---|
| 223 |
print "and you need not retain it.\n"; |
|---|
| 224 |
print "Do you want to retain this in your $old file? [y] "; |
|---|
| 225 |
$yn = <STDIN>; |
|---|
| 226 |
if ($yn !~ /^[Nn]/) { |
|---|
| 227 |
print "Retaining definition. It will appear at the end of $old.\n"; |
|---|
| 228 |
@retained = (@retained, $d); |
|---|
| 229 |
print OLD $comment{$d}; |
|---|
| 230 |
print OLD &def($d); |
|---|
| 231 |
print OLD "\n"; |
|---|
| 232 |
} else { |
|---|
| 233 |
print "Deleting definition.\n"; |
|---|
| 234 |
@deleted = (@deleted, $d); |
|---|
| 235 |
} |
|---|
| 236 |
} |
|---|
| 237 |
|
|---|
| 238 |
print OLD $final; |
|---|
| 239 |
|
|---|
| 240 |
close(OLD); |
|---|
| 241 |
|
|---|
| 242 |
print "\nSummary of changes:\n"; |
|---|
| 243 |
print "New options from $new: ",join(" ",@newoptions),"\n"; |
|---|
| 244 |
print "Old options retained: ",join(" ",@retained),"\n"; |
|---|
| 245 |
print "Old options deleted: ",join(" ",@deleted),"\n"; |
|---|
| 246 |
print "If this is wrong, you can recover $old from $bak.\n"; |
|---|
| 247 |
print "Done!\n"; |
|---|
| 248 |
exit 0; |
|---|
| 249 |
|
|---|
| 250 |
|
|---|
| 251 |
|
|---|
| 252 |
|
|---|
| 253 |
|
|---|
| 254 |
|
|---|
| 255 |
|
|---|
| 256 |
sub def { |
|---|
| 257 |
|
|---|
| 258 |
local($d,$c,$oldval) = @_; |
|---|
| 259 |
local($df) = $defs{$d}; |
|---|
| 260 |
delete $defs{$d}; |
|---|
| 261 |
$d =~ s/^\s+//; |
|---|
| 262 |
$d =~ s/\s+$//; |
|---|
| 263 |
$c =~ s/^\s+//; |
|---|
| 264 |
$c =~ s/\s+$//; |
|---|
| 265 |
$df =~ s/^\s+//; |
|---|
| 266 |
$df =~ s/\s+$//; |
|---|
| 267 |
if ($df eq 'undef') { |
|---|
| 268 |
if (defined($oldval) and $oldval) { |
|---|
| 269 |
$oldval =~ s/^\s+//; |
|---|
| 270 |
$oldval =~ s/\s+$//; |
|---|
| 271 |
return "/* #define $d\t$oldval /* */\n"; |
|---|
| 272 |
} else { |
|---|
| 273 |
return "/* #define $d /* */\n"; |
|---|
| 274 |
} |
|---|
| 275 |
} |
|---|
| 276 |
return "#define $d /* */\n" if ($df eq 'define'); |
|---|
| 277 |
return "/* #define $d\t$df\t$c\n" if ($cvaldef{$d} and $c); |
|---|
| 278 |
return "#define $d\t$df\t$c\n" if ($c); |
|---|
| 279 |
return "#define $d\t$df\n"; |
|---|
| 280 |
} |
|---|
| 281 |
|
|---|
| 282 |
|
|---|
| 283 |
|
|---|
| 284 |
|
|---|
| 285 |
|
|---|
| 286 |
|
|---|
| 287 |
|
|---|
| 288 |
sub ask_simple { |
|---|
| 289 |
local($d,$s) = @_; |
|---|
| 290 |
local($yn); |
|---|
| 291 |
print "\nNew option: $d\n"; |
|---|
| 292 |
print $comment; |
|---|
| 293 |
$s = ($s eq 'define') ? 'y' : 'n'; |
|---|
| 294 |
while (1) { |
|---|
| 295 |
print "Define this option? [$s] "; |
|---|
| 296 |
$yn = <STDIN>; |
|---|
| 297 |
$yn = $s if $yn =~ /^$/; |
|---|
| 298 |
last if $yn =~ /^[YyNn]/; |
|---|
| 299 |
} |
|---|
| 300 |
$defs{$d} = ($yn =~ /^[Yy]/) ? 'define' : 'undef'; |
|---|
| 301 |
@newoptions = (@newoptions,$d); |
|---|
| 302 |
return $d; |
|---|
| 303 |
} |
|---|
| 304 |
|
|---|
| 305 |
|
|---|
| 306 |
|
|---|
| 307 |
|
|---|
| 308 |
|
|---|
| 309 |
|
|---|
| 310 |
sub ask_value { |
|---|
| 311 |
local($d,$s) = @_; |
|---|
| 312 |
local($val); |
|---|
| 313 |
print "\nNew option: $d\n"; |
|---|
| 314 |
print "$comment\n" unless ($comment =~ /^\s*\/\*\s*\*\/\s*$/); |
|---|
| 315 |
print "Default value: $s\n"; |
|---|
| 316 |
print "Value for this option? (undef to undefine) [$s] "; |
|---|
| 317 |
$val = <STDIN>; |
|---|
| 318 |
$val = $s if $val =~ /^$/; |
|---|
| 319 |
$defs{$d} = $val; |
|---|
| 320 |
@newoptions = (@newoptions,$d); |
|---|
| 321 |
return $d; |
|---|
| 322 |
} |
|---|
| 323 |
|
|---|