| 1 |
#!/usr/bin/perl |
| 2 |
# Linux: #!/usr/bin/perl |
| 3 |
# IRIX: #!/store/bin/perl |
| 4 |
# BeOS: #!/boot/home/config/bin/perl |
| 5 |
############################################################################ |
| 6 |
# |
| 7 |
# templant - William Gibson's "temple implant" |
| 8 |
# |
| 9 |
# This perl script parses source code files and updates all the templant |
| 10 |
# templates, header-inlines inline functions and more. |
| 11 |
# |
| 12 |
# Hacked in perl by Lars J. Aas <[email protected]>, 1998-2002 |
| 13 |
# |
| 14 |
# TEMPLANT ENV VARIABLES: |
| 15 |
# |
| 16 |
# TEMPLANT_INCLUDE - ":"-separated path list |
| 17 |
# |
| 18 |
# TEMPLANT OPTIONS: |
| 19 |
# |
| 20 |
# -d, --debug |
| 21 |
# -h, -?, --help, --usage |
| 22 |
# -u, --uncompact |
| 23 |
# -V, --version |
| 24 |
# --fold-templates |
| 25 |
# --inline |
| 26 |
# --fix-spacing |
| 27 |
# -I<dir> |
| 28 |
# |
| 29 |
# TEMPLANT DIRECTIVES: |
| 30 |
# |
| 31 |
# //$ INSERT TEMPLATE TemplateName( arguments ) |
| 32 |
# //$ BEGIN TEMPLATE TemplateName( arguments ) |
| 33 |
# //$ END TEMPLATE TemplateName |
| 34 |
# |
| 35 |
# //$ IMPORT INLINE filename |
| 36 |
# //$ BEGIN IMPORT INLINE filename |
| 37 |
# //$ END IMPORT INLINE filename |
| 38 |
# |
| 39 |
# //$ EXPORT INLINE |
| 40 |
# //$ BEGIN EXPORT INLINE |
| 41 |
# //$ END EXPORT INLINE |
| 42 |
# |
| 43 |
# //$ DEFINE varname substitution |
| 44 |
# //$ UNDEFINE varname |
| 45 |
# |
| 46 |
# /*$ EVAL( perl-expr ) */ |
| 47 |
# |
| 48 |
# //$ IF perl-expr |
| 49 |
# //$ ELSIF perl-expr |
| 50 |
# //$ ELSE |
| 51 |
# //$ ENDIF |
| 52 |
# |
| 53 |
# ... //$ IF perl-expr |
| 54 |
# |
| 55 |
# //$ APPEND string [append line to previous line] |
| 56 |
# //$ APPEND:string [append line to previous line] |
| 57 |
# [- no space stripping] |
| 58 |
# |
| 59 |
# //$ WARNING message |
| 60 |
# //$ ERROR message |
| 61 |
# |
| 62 |
# Alternately, //$ ... can be replaced with |
| 63 |
# |
| 64 |
# ##$ ... |
| 65 |
# /*$ ... $/ |
| 66 |
# |
| 67 |
############################################################################ |
| 68 |
# |
| 69 |
# TODO: |
| 70 |
# - avoid substitution on first argument of DEFINE and UNDEFINE |
| 71 |
# - IFDEF/IFNDEF |
| 72 |
# - CASE varname |
| 73 |
# match: |
| 74 |
# match: |
| 75 |
# match: |
| 76 |
# match: |
| 77 |
# ESAC |
| 78 |
# |
| 79 |
############################################################################ |
| 80 |
# |
| 81 |
# $Id$ |
| 82 |
# |
| 83 |
# $Log$ |
| 84 |
# Revision 1.10 2002/06/10 15:29:54 larsa |
| 85 |
# new CASE/ESAC directive, DEFINE/UNDEF fix, and some cosmetics |
| 86 |
# |
| 87 |
# Revision 1.9 2002/03/20 18:03:38 larsa |
| 88 |
# a new shorthand notation for single-line conditionals, new macros ERROR and WARNING |
| 89 |
# |
| 90 |
# Revision 1.8 2002/03/20 12:03:50 larsa |
| 91 |
# bugfix |
| 92 |
# |
| 93 |
# Revision 1.7 2002/03/20 08:50:07 larsa |
| 94 |
# additional macros DEFINE and UNDEFINE |
| 95 |
# |
| 96 |
# Revision 1.6 2002/03/19 14:00:19 larsa |
| 97 |
# support conditionals in templates, much like a C preprocessor |
| 98 |
# |
| 99 |
# Revision 1.5 2002/03/08 15:32:43 larsa |
| 100 |
# Support C comments around commands. |
| 101 |
# New command "/*$ EVAL(expr) */". |
| 102 |
# |
| 103 |
# Revision 1.4 2000/01/13 17:22:32 mortene |
| 104 |
# Updated headers by changing copyright time span from 1998-1999 |
| 105 |
# to 1998-2000. |
| 106 |
# |
| 107 |
# Revision 1.3 1999/12/05 09:41:48 mortene |
| 108 |
# Made it possible to recognize "##$" as a token prefix in addition to |
| 109 |
# "//$". Will be put in use later when "templanting" the Makefile.am |
| 110 |
# files with common rules. |
| 111 |
# |
| 112 |
# Revision 1.2 1999/09/23 06:31:49 mortene |
| 113 |
# Cleaned up the sources a bit to be more in conformance with the wanted |
| 114 |
# coding style. |
| 115 |
# |
| 116 |
# Revision 1.1.1.1 1999/06/30 23:51:05 mortene |
| 117 |
# Created |
| 118 |
# |
| 119 |
# Revision 1.3 1999/06/30 23:03:53 mortene |
| 120 |
# *** empty log message *** |
| 121 |
# |
| 122 |
# Revision 1.2 1999/06/29 21:31:48 mortene |
| 123 |
# *** empty log message *** |
| 124 |
# |
| 125 |
############################################################################ |
| 126 |
# |
| 127 |
# TODO: |
| 128 |
# o clean up and minimize code (sigh; 22KB and still growing) |
| 129 |
# o accept more whitespace (TABs) |
| 130 |
# o consider renaming script to "simplant" ;) |
| 131 |
# |
| 132 |
############################################################################ |
| 133 |
|
| 134 |
if ( @ARGV == 0 ) { |
| 135 |
&PrintUsage(); |
| 136 |
exit 0; |
| 137 |
} |
| 138 |
|
| 139 |
%OPTIONS = ( # option perl code |
| 140 |
'--help', '&PrintUsage(); exit 0;', |
| 141 |
'--usage', '&PrintUsage(); exit 0;', |
| 142 |
'-h', '&PrintUsage(); exit 0;', |
| 143 |
'-?', '&BrintUsage(); exit 0;', |
| 144 |
'--version', '&PrintVersion(); exit 0;', |
| 145 |
'-v', '&PrintVersion(); exit 0;', |
| 146 |
'--uncompact', '$compact = 0;', |
| 147 |
'-u', '$compact = 0;', |
| 148 |
'--debug', '$debug = 1;', |
| 149 |
'-d', '$debug = 1;', |
| 150 |
'--fold-templates', '$fold = 1;', |
| 151 |
'--inline', '$inline = 1;', |
| 152 |
'--fix-spacing', '$spacing = 1;', |
| 153 |
); |
| 154 |
|
| 155 |
############################################################################ |
| 156 |
# separate files from options |
| 157 |
|
| 158 |
@options = (); |
| 159 |
@files = (); |
| 160 |
|
| 161 |
foreach $arg ( @ARGV ) { |
| 162 |
if ( -f $arg ) { |
| 163 |
push( @files, $arg ); |
| 164 |
} else { |
| 165 |
push( @options, $arg ); # hopefully |
| 166 |
} |
| 167 |
} |
| 168 |
|
| 169 |
############################################################################ |
| 170 |
# set defaults |
| 171 |
|
| 172 |
$fold = 0; # fold templates |
| 173 |
$inline = 0; # inline functions |
| 174 |
$debug = 0; # dump debug output |
| 175 |
$compact = 1; # only wrap level-0 templates with comments |
| 176 |
$spacing = 0; # replace TABs with spaces, and remove WS at EOL |
| 177 |
$evaluations = 1; # replace /*$ EVAL(expr) */ parts |
| 178 |
|
| 179 |
$prefix = '(\#\#\$|//\$|/\*\$)'; |
| 180 |
$postfix = '( *| +\*/)'; |
| 181 |
|
| 182 |
@incpath = (); |
| 183 |
push(@incpath, ""); # $incpath[0] is set to the current file's directory |
| 184 |
push(@incpath, "."); # $incpath[1] is current directory |
| 185 |
|
| 186 |
############################################################################ |
| 187 |
# parse environment variables |
| 188 |
|
| 189 |
if ( defined $ENV{'TEMPLANT_INCLUDE'} ) { |
| 190 |
foreach $dir ( split( $ENV{'TEMPLANT_INCLUDE'}, /:/ ) ) { |
| 191 |
if ( ! -d $dir ) { |
| 192 |
print STDERR "WARNING: No directory '", $dir, |
| 193 |
"' (from \$TEMPLANT_INCLUDE).\n"; |
| 194 |
} else { |
| 195 |
push( @incpath, $dir ); |
| 196 |
} |
| 197 |
} |
| 198 |
} |
| 199 |
|
| 200 |
############################################################################ |
| 201 |
# parse options |
| 202 |
|
| 203 |
foreach $opt ( @options ) { |
| 204 |
if ( defined $OPTIONS{$opt} ) { |
| 205 |
eval $OPTIONS{$opt}; |
| 206 |
} elsif ( $opt =~ /^-I/o ) { |
| 207 |
$dir = substr($opt, 2); |
| 208 |
if ( ! -d $dir ) { |
| 209 |
print STDERR "WARNING: No directory '", $dir, |
| 210 |
"' (from command line option).\n"; |
| 211 |
} else { |
| 212 |
push(@incpath, $dir); |
| 213 |
} |
| 214 |
} else { |
| 215 |
print STDERR "ERROR: No option or file named '", $opt, "'\n"; |
| 216 |
exit( -1 ); |
| 217 |
} |
| 218 |
} |
| 219 |
|
| 220 |
############################################################################ |
| 221 |
# parse files |
| 222 |
|
| 223 |
file: |
| 224 |
foreach $file ( @files ) { |
| 225 |
%defines = (); |
| 226 |
print $file, ":"; |
| 227 |
|
| 228 |
if ( $file =~ /\.tpl$/ ) { |
| 229 |
print STDERR "ERROR: Won't modify template files ($file).\n"; |
| 230 |
next file; |
| 231 |
} elsif ( ! open( FILE, $file ) ) { |
| 232 |
print STDERR "ERROR: Unable to open '", $file, "': ", $!, "\n"; |
| 233 |
next file; |
| 234 |
} else { |
| 235 |
@origlines = <FILE>; |
| 236 |
close(FILE); |
| 237 |
@lines = @origlines; |
| 238 |
} |
| 239 |
|
| 240 |
if ( $file =~ m#/#o ) { # file probably not in current dir... |
| 241 |
$incpath[0] = $file; |
| 242 |
$incpath[0] =~ s#/[^/]*$##o; |
| 243 |
if ( ! -d $incpath[0] ) { |
| 244 |
print STDERR "WARNING: No directory named '$incpath[0]'.\n"; |
| 245 |
$incpath[0] = "."; |
| 246 |
} |
| 247 |
} else { |
| 248 |
$incpath[0] = "."; |
| 249 |
} |
| 250 |
|
| 251 |
$define = $file; |
| 252 |
$define =~ s/^.*\///og; # only filename |
| 253 |
$define =~ s/\..*//og; |
| 254 |
# $define =~ s/([a-z])([A-Z])/$1_$2/og; |
| 255 |
# $define =~ s/([^0-9]*)([0-9]+)([^0-9]*)/$1_$2_$3/og; |
| 256 |
$define =~ tr/a-z/A-Z/; |
| 257 |
$define = "COIN_" . $define . "_H_INLINED"; |
| 258 |
|
| 259 |
# update templates |
| 260 |
&FoldTemplates(); |
| 261 |
&UnfoldTemplates() if ( ! $fold ); |
| 262 |
|
| 263 |
# function inlining |
| 264 |
&FoldInlining(); |
| 265 |
if ( $inline ) { |
| 266 |
&UnfoldInlining(); |
| 267 |
} else { |
| 268 |
&FoldInliningSource(); |
| 269 |
} |
| 270 |
|
| 271 |
# strip trailing whitespace |
| 272 |
&FixSpacing() if ( $spacing == 1 ); |
| 273 |
|
| 274 |
# save if changed |
| 275 |
if ( &FileChanged() ) { |
| 276 |
printf "\r%-60s%-18s\n", $file . ":", "updating..."; |
| 277 |
system("mv $file $file.bak"); |
| 278 |
open(FILE, "> $file") || die $file, ": ", $!, "\n"; |
| 279 |
foreach $line ( @lines ) { |
| 280 |
print FILE $line; |
| 281 |
} |
| 282 |
close(FILE); |
| 283 |
} else { |
| 284 |
printf "\r%-60s%-18s\n", $file . ":", "no change..."; |
| 285 |
} |
| 286 |
} |
| 287 |
|
| 288 |
############################################################################ |
| 289 |
# subroutines (heavy use of globals, though) |
| 290 |
|
| 291 |
# folds all templates |
| 292 |
sub FoldTemplates { ######################################################## |
| 293 |
local($line, $i, $proto, $name, $temp); |
| 294 |
|
| 295 |
template: |
| 296 |
for ( $line = scalar(@lines) - 1; $line >= 0; $line-- ) { |
| 297 |
if ( $lines[$line] =~ m|^$prefix +BEGIN +TEMPLATE +|o ) { |
| 298 |
chop($proto = $lines[$line]); |
| 299 |
$proto =~ s|^$prefix +BEGIN +TEMPLATE +||o; |
| 300 |
$proto =~ s|$postfix$||o; |
| 301 |
$name = $proto; |
| 302 |
$name =~ s/^([a-zA-Z0-9_-]+)\(.*/$1/og; |
| 303 |
$i = $line + 1; |
| 304 |
while ( $i < scalar(@lines) ) { |
| 305 |
if ( $lines[$i] =~ m|^$prefix +END +TEMPLATE +|o ) { |
| 306 |
chop( $temp = $lines[$i] ); |
| 307 |
$temp =~ s|^$prefix +END +TEMPLATE +||o; |
| 308 |
$localprefixstr = $1; |
| 309 |
$temp =~ s|$postfix$||o; |
| 310 |
$localpostfixstr = $1; |
| 311 |
if ( $temp eq $name ) { |
| 312 |
splice(@lines, $line, ($i - $line + 1), |
| 313 |
("$localprefixstr INSERT TEMPLATE " . $proto . "$localpostfixstr\n")); |
| 314 |
next template; |
| 315 |
} else { |
| 316 |
print STDERR "ERROR: TEMPLATE BEGIN ($name) / END mismatch.\n"; |
| 317 |
exit(-1); |
| 318 |
} |
| 319 |
} |
| 320 |
$i++; |
| 321 |
} |
| 322 |
print STDERR "ERROR: TEMPLATE BEGIN ($name) but no END.\n"; |
| 323 |
exit( -1 ); |
| 324 |
} |
| 325 |
} |
| 326 |
} |
| 327 |
|
| 328 |
%defines = (); |
| 329 |
|
| 330 |
# unfolds all templates |
| 331 |
sub UnfoldTemplates { ###################################################### |
| 332 |
local($level) = (0); |
| 333 |
local($line, $proto, $name, $filename, $arguments, $templatevariables); |
| 334 |
local($i, $prototype, $prototypename); |
| 335 |
local(@template, @arguments, @templatevariables); |
| 336 |
local($templates); # for template, not file |
| 337 |
local($arg); |
| 338 |
local(@conditionals) = (); |
| 339 |
|
| 340 |
$level = $_[0] if ( @_ > 0 ); |
| 341 |
|
| 342 |
$templates = 0; |
| 343 |
for ( $line = 0; $line < @lines; $line++ ) { |
| 344 |
$directive = 0; |
| 345 |
if ( defined $casestring ) { |
| 346 |
$length = length($casestring); |
| 347 |
if ( $casestring eq substr($lines[$line], 0, $length) ) { |
| 348 |
$lines[$line] = substr($lines[$line], $length); |
| 349 |
} else { |
| 350 |
$directive = 1; |
| 351 |
} |
| 352 |
} |
| 353 |
# need to pick up define-name before it is substituted... |
| 354 |
$definename = ""; |
| 355 |
if ( $lines[$line] =~ m|^$prefix +DEFINE +([^ ]+) +(.*)$|o ) { |
| 356 |
$definename = $2; |
| 357 |
# ensure define name isn't split up on redefinition... |
| 358 |
$lines[$line] =~ s| (DEFINE +)([^ ]+) | \1# |o; |
| 359 |
} elsif ( $lines[$line] =~ m|^$prefix +UNDEFINE +([^ ]+)$|o ) { |
| 360 |
$definename = $2; |
| 361 |
chomp($definename); |
| 362 |
delete $defines{$definename}; |
| 363 |
# print STDERR "undefining \"$definename\"\n"; |
| 364 |
} |
| 365 |
foreach $key (keys %defines) { |
| 366 |
# print STDERR "substing \"$key\" with \"$defines{$key}\"\n"; |
| 367 |
$lines[$line] =~ s|$key|$defines{$key}|g; |
| 368 |
} |
| 369 |
$lines[$line] =~ s|/\*\$ *EVAL\((.*)\) *\*/|eval "$1"|eg; |
| 370 |
|
| 371 |
## we try to handle IF, ELSIF, ELSE, and ENDIF here |
| 372 |
if ( $lines[$line] =~ m|^$prefix +IF +|o ) { |
| 373 |
$condstring = $lines[$line]; |
| 374 |
$condstring =~ s|^$prefix +IF +||o; |
| 375 |
$conditional = eval("if ( $condstring ) { 1; } else { 0; }"); |
| 376 |
push(@conditionals, $conditional); |
| 377 |
# print STDERR "opening conditional ".scalar(@conditionals)."\n"; |
| 378 |
# print STDERR " => $conditional ($condstring)\n"; |
| 379 |
$directive = 1; |
| 380 |
} elsif ( $lines[$line] =~ m|^$prefix +ELSIF +|o ) { |
| 381 |
$condstring = $lines[$line]; |
| 382 |
$condstring =~ s|^$prefix +ELSIF +||o; |
| 383 |
$oldconditional = pop(@conditionals); |
| 384 |
# print STDERR "modifying conditional ".scalar(@conditionals)."\n"; |
| 385 |
if ( $oldconditional == 0 ) { |
| 386 |
$conditional = eval("if ( $condstring ) { 1; } else { 0; }"); |
| 387 |
# print STDERR " => $conditional ($condstring)\n"; |
| 388 |
} else { |
| 389 |
$conditional = -1; |
| 390 |
# print STDERR " => $conditional\n"; |
| 391 |
} |
| 392 |
push(@conditionals, $conditional); |
| 393 |
$directive = 1; |
| 394 |
} elsif ( $lines[$line] =~ m|^$prefix +ELSE *$|o ) { |
| 395 |
# print STDERR "toggling conditional ".scalar(@conditionals)."\n"; |
| 396 |
$oldconditional = pop(@conditionals); |
| 397 |
if ( $oldconditional == 0 ) { |
| 398 |
$conditional = 1; |
| 399 |
} else { |
| 400 |
$conditional = -1; |
| 401 |
} |
| 402 |
push(@conditionals, $conditional); |
| 403 |
# print STDERR " => $conditional\n"; |
| 404 |
$directive = 1; |
| 405 |
} elsif ( $lines[$line] =~ m|^$prefix +ENDIF *$|o ) { |
| 406 |
# print STDERR "closing conditional ".scalar(@conditionals)."\n"; |
| 407 |
$oldconditional = pop(@conditionals); |
| 408 |
$directive = 1; |
| 409 |
} elsif ( $lines[$line] =~ m|^$prefix +CASE +(.+)$|o ) { |
| 410 |
if ( defined $casestring ) { |
| 411 |
print STDERR "error: multiple CASE/ESAC conditions simultaneously\n"; |
| 412 |
exit(-1); |
| 413 |
} else { |
| 414 |
$casestring = $2; |
| 415 |
$directive = 1; |
| 416 |
} |
| 417 |
} elsif ( $lines[$line] =~ m|^$prefix +ESAC *$|o ) { |
| 418 |
$directive = 1; |
| 419 |
undef $casestring; |
| 420 |
} |
| 421 |
|
| 422 |
if ( $directive == 1 ) { |
| 423 |
splice(@lines, $line, 1); |
| 424 |
$line--; |
| 425 |
next; |
| 426 |
} |
| 427 |
|
| 428 |
if ( scalar(@conditionals) > 0 ) { |
| 429 |
$skip = 0; |
| 430 |
for ( $c = 0; $c < scalar(@conditionals) && !$skip; $c++ ) { |
| 431 |
$skip = 1 if $conditionals[$c] != 1; |
| 432 |
} |
| 433 |
if ( $skip == 1 ) { |
| 434 |
splice(@lines, $line, 1); |
| 435 |
$line--; |
| 436 |
next; |
| 437 |
} |
| 438 |
} |
| 439 |
|
| 440 |
# one-line shorthand conditional line |
| 441 |
if ( $lines[$line] =~ m|$prefix +IF +|o ) { |
| 442 |
$condstring = $lines[$line]; |
| 443 |
$condstring =~ s|.*$prefix +IF +||o; |
| 444 |
$conditional = eval("if ( $condstring ) { 1; } else { 0; }"); |
| 445 |
if ( $conditional == 0 ) { |
| 446 |
splice(@lines, $line, 1); |
| 447 |
$line--; |
| 448 |
next; |
| 449 |
} else { |
| 450 |
$lines[$line] =~ s| *$prefix +IF +.*$||o; |
| 451 |
} |
| 452 |
} |
| 453 |
|
| 454 |
if ( $lines[$line] =~ m|^$prefix +APPEND +|o ) { |
| 455 |
$appendstring = $lines[$line]; |
| 456 |
$appendstring =~ s|^$prefix +APPEND +||o; |
| 457 |
chomp($lines[$line-1]); |
| 458 |
$lines[$line-1] .= $appendstring; |
| 459 |
$directive = 1; |
| 460 |
} elsif ( $lines[$line] =~ m|^$prefix +APPEND:|o ) { |
| 461 |
$appendstring = $lines[$line]; |
| 462 |
$appendstring =~ s|^$prefix +APPEND:||o; |
| 463 |
chomp($lines[$line-1]); |
| 464 |
$lines[$line-1] .= $appendstring; |
| 465 |
$directive = 1; |
| 466 |
} |
| 467 |
|
| 468 |
|
| 469 |
if ( $lines[$line] =~ m|^$prefix +ERROR +|o ) { |
| 470 |
$errorstring = $lines[$line]; |
| 471 |
$errorstring =~ s|^$prefix +ERROR +||o; |
| 472 |
print STDERR "ERROR: $errorstring\n"; |
| 473 |
exit(-1); |
| 474 |
} elsif ( $lines[$line] =~ m|^$prefix +WARNING +|o ) { |
| 475 |
$errorstring = $lines[$line]; |
| 476 |
$errorstring =~ s|^$prefix +WARNING +||o; |
| 477 |
print STDERR "WARNING: $errorstring\n"; |
| 478 |
$directive = 1; |
| 479 |
} |
| 480 |
|
| 481 |
if ( $lines[$line] =~ m|^$prefix +DEFINE +|o ) { |
| 482 |
$lines[$line] =~ m|^$prefix +DEFINE +([^ ]+) +(.*)$|o; |
| 483 |
# picked up above |
| 484 |
# $definename = $2; |
| 485 |
$value = $3; |
| 486 |
# print STDERR "defining \"$definename\" to \"$value\"\n"; |
| 487 |
$defines{$definename} = $value; |
| 488 |
$directive = 1; |
| 489 |
} elsif ( $lines[$line] =~ m|^$prefix +UNDEFINE +|o ) { |
| 490 |
# everything is handled before the keyword substitution phase |
| 491 |
# $lines[$line] =~ m|^$prefix +UNDEFINE +([^ ]+)$|o; |
| 492 |
# picked up above |
| 493 |
# $definename = $2; |
| 494 |
# print STDERR "undefining \"$definename\"\n"; |
| 495 |
# delete $defines{$definename}; |
| 496 |
$directive = 1; |
| 497 |
} |
| 498 |
|
| 499 |
if ( $directive == 1 ) { |
| 500 |
splice(@lines, $line, 1); |
| 501 |
$line--; |
| 502 |
next; |
| 503 |
} |
| 504 |
|
| 505 |
if ( $lines[$line] =~ m|^$prefix +INSERT +TEMPLATE +|o ) { |
| 506 |
chop( $proto = $lines[$line] ); |
| 507 |
$proto =~ s|^$prefix +INSERT +TEMPLATE +||o; |
| 508 |
$prefixstr = $1; |
| 509 |
$proto =~ s|$postfix$||o; |
| 510 |
$postfixstr = $1; |
| 511 |
$name = $proto; |
| 512 |
$name =~ s/\(.*//o; |
| 513 |
|
| 514 |
$filename = &FindFile( $name . ".tpl" ); |
| 515 |
print "READING TEMPLATE '$filename'\n" if $debug; |
| 516 |
if ( ! -f $filename || ! open( TEMPLATE, $filename ) ) { |
| 517 |
print STDERR "ERROR: Unable to open '$filename' ($name).\n"; |
| 518 |
exit( -1 ); |
| 519 |
} else { |
| 520 |
@template = <TEMPLATE>; |
| 521 |
close( TEMPLATE ); |
| 522 |
} |
| 523 |
|
| 524 |
$arguments = $proto; |
| 525 |
$arguments =~ s/^[^\(]+\([ \t]*(.*)[ \t]*\)[^()]*$/$1/; |
| 526 |
@arguments = &SplitArgs( $arguments ); |
| 527 |
|
| 528 |
if ($debug) { |
| 529 |
foreach (@arguments) { |
| 530 |
print "argument: ``$_''\n"; |
| 531 |
} |
| 532 |
} |
| 533 |
|
| 534 |
if ( $template[0] !~ m|^$prefix +TEMPLATE +|o ) { |
| 535 |
print STDERR $filename, " is not a valid template file.\n"; |
| 536 |
exit( -1 ); |
| 537 |
} |
| 538 |
|
| 539 |
chop( $prototype = shift( @template ) ); |
| 540 |
$prototype =~ s|^$prefix +TEMPLATE +||o; |
| 541 |
# $prefixstr = $1; |
| 542 |
$prototype =~ s|$postfix$||o; |
| 543 |
# $postfixstr = $1; |
| 544 |
$prototypename = $prototype; |
| 545 |
$prototypename =~ s/\(.*//; |
| 546 |
if ( $prototypename ne $name ) { |
| 547 |
print STDERR $filename, " does not contain template '", |
| 548 |
$name, "'.\n"; |
| 549 |
exit( -1 ); |
| 550 |
} |
| 551 |
$templatevariables = $prototype; |
| 552 |
$templatevariables =~ s/^[^\(]*\((.*)\)[^()]*$/$1/; |
| 553 |
@templatevariables = &SplitArgs( $templatevariables ); |
| 554 |
foreach ( @templatevariables ) { |
| 555 |
s/^[ \t]+//; |
| 556 |
s/[ \t]+$//; |
| 557 |
} |
| 558 |
if ($debug) { |
| 559 |
foreach (@templatevariables) { |
| 560 |
print "templatevar: ``$_''\n"; |
| 561 |
} |
| 562 |
} |
| 563 |
if ( scalar( @templatevariables ) != scalar( @arguments ) ) { |
| 564 |
print STDERR $filename, ": template call in '", $file, |
| 565 |
"' does not match prototype.\n"; |
| 566 |
print STDERR "tv = ", scalar( @templatevariables ), |
| 567 |
", args = ", scalar( @arguments ), "\n"; |
| 568 |
exit( -1 ); |
| 569 |
} |
| 570 |
|
| 571 |
# substitute macro arguments |
| 572 |
for ( $index = 0; $index < scalar( @arguments ); $index++ ) { |
| 573 |
for ( $linenum = 0; $linenum < scalar( @template ); $linenum++ ) { |
| 574 |
$template[$linenum] =~ |
| 575 |
s/([^#])#[ \t]*$templatevariables[$index]/$1"$arguments[$index]"/g; |
| 576 |
$template[$linenum] =~ |
| 577 |
s/([ \t]*##[ \t]*)?$templatevariables[$index]([ \t]*##[ \t]*)?/$arguments[$index]/g; |
| 578 |
} |
| 579 |
} |
| 580 |
|
| 581 |
# insert template |
| 582 |
if ( $level == 0 || ! $compact ) { # add header and footer |
| 583 |
unshift( @template, "$prefixstr BEGIN TEMPLATE " . |
| 584 |
$name . "(" . join( ", ", @arguments ) . ")" . $postfixstr . "\n" ); |
| 585 |
push( @template, "$prefixstr END TEMPLATE " . $name . $postfixstr . "\n" ); |
| 586 |
} |
| 587 |
splice( @lines, $line, 1, @template ); |
| 588 |
$line += @template - 1; |
| 589 |
$templates += 1; |
| 590 |
} |
| 591 |
} |
| 592 |
&UnfoldTemplates( $level + 1 ) if ( $templates > 0 ); # tail recursion |
| 593 |
} |
| 594 |
|
| 595 |
# remove function inlining |
| 596 |
sub FoldInlining { ######################################################### |
| 597 |
local( $line, $i, $from, $temp ); |
| 598 |
|
| 599 |
session: |
| 600 |
for ( $line = @lines - 1; $line >= 0; $line-- ) { |
| 601 |
if ( $lines[$line] =~ m|^$prefix +BEGIN +IMPORT +INLINE +|o ) { |
| 602 |
chop( $from = $lines[$line] ); |
| 603 |
$from =~ s|^$prefix +BEGIN +IMPORT +INLINE +||o; |
| 604 |
for ( $i = $line + 1; $i < scalar( @lines ); $i++ ) { |
| 605 |
if ( $lines[$i] =~ m|^$prefix +END +IMPORT +INLINE +|o ) { |
| 606 |
chop( $temp = $lines[$i] ); |
| 607 |
$temp =~ s|^$prefix +END +IMPORT +INLINE +||o; |
| 608 |
if ( $from eq $temp ) { |
| 609 |
$lines[$line] =~ |
| 610 |
s|^$prefix +BEGIN +IMPORT +INLINE +|$prefix IMPORT INLINE |o; |
| 611 |
splice( @lines, $line + 1, $i - $line, () ); |
| 612 |
next session; |
| 613 |
} else { |
| 614 |
print STDERR "ERROR: BEGIN/END IMPORT mismatch.\n"; |
| 615 |
exit( -1 ); |
| 616 |
} |
| 617 |
} |
| 618 |
} |
| 619 |
print STDERR "ERROR: BEGIN IMPORT INLINE with no END.\n"; |
| 620 |
exit( -1 ); |
| 621 |
} |
| 622 |
} |
| 623 |
} |
| 624 |
|
| 625 |
# remove wrappers in source |
| 626 |
sub FoldInliningSource { ################################################### |
| 627 |
local( $line, $from, $fromfile, $i, $modified, $string ); |
| 628 |
local( @source ); |
| 629 |
|
| 630 |
@source = (); |
| 631 |
for ( $line = 0; $line < @lines; $line++ ) { |
| 632 |
if ( $lines[$line] =~ m|^$prefix +IMPORT +INLINE +|o ) { |
| 633 |
$modified = 0; |
| 634 |
chop( $from = $lines[$line] ); |
| 635 |
$from =~ s|^$prefix +IMPORT +INLINE +||o; |
| 636 |
$fromfile = &FindFile( $from ); |
| 637 |
if ( ! -f $fromfile || ! open( SOURCE, $fromfile ) ) { |
| 638 |
print "ERROR: Could not open file '$from'.\n"; |
| 639 |
exit( -1 ); |
| 640 |
} |
| 641 |
@source = <SOURCE>; |
| 642 |
close( SOURCE ); |
| 643 |
for ( $i = 0; $i < scalar( @source ); $i++ ) { |
| 644 |
if ( $source[$i] =~ m|^$prefix +BEGIN +EXPORT +INLINE *$|o ) { |
| 645 |
splice( @source, $i, 2, "$1 EXPORT INLINE\n" ); |
| 646 |
$modified = 1; |
| 647 |
$i -= 1; |
| 648 |
} elsif ( $source[$i] =~ m|^$prefix +END +EXPORT +INLINE *$|o ) { |
| 649 |
splice( @source, $i - 1, 2 ); |
| 650 |
$modified = 1; |
| 651 |
$i -= 2; |
| 652 |
} |
| 653 |
} |
| 654 |
if ( $modified == 1 ) { |
| 655 |
system( "mv $fromfile $fromfile.bak" ); |
| 656 |
if ( ! open( SOURCE, "> $fromfile" ) ) { |
| 657 |
print STDERR "ERROR: Could not write to '$from'.\n"; |
| 658 |
} else { |
| 659 |
foreach $string ( @source ) { |
| 660 |
print SOURCE $string; |
| 661 |
} |
| 662 |
close( SOURCE ); |
| 663 |
} |
| 664 |
} |
| 665 |
} |
| 666 |
} |
| 667 |
} |
| 668 |
|
| 669 |
# use function inlining |
| 670 |
sub UnfoldInlining { ####################################################### |
| 671 |
local( $line, $from, $fromfile, $i, $j, $c, $modified, $string ); |
| 672 |
local( @source, @inline ); |
| 673 |
|
| 674 |
$modified = 0; |
| 675 |
@source = (); |
| 676 |
@inline = (); |
| 677 |
line: |
| 678 |
for ( $line = 0; $line < @lines; $line++ ) { |
| 679 |
if ( $lines[$line] =~ m|^$prefix +IMPORT +INLINE +|o ) { |
| 680 |
chop( $from = $lines[$line] ); |
| 681 |
$from =~ s|^$prefix +IMPORT +INLINE +||o; |
| 682 |
$prefixstr = $1; |
| 683 |
$fromfile = &FindFile( $from ); |
| 684 |
if ( ! -f $fromfile || ! open( SOURCE, $fromfile ) ) { |
| 685 |
print STDERR "ERROR: Unable to open '$from'.\n"; |
| 686 |
next line; |
| 687 |
} |
| 688 |
@source = <SOURCE>; |
| 689 |
close( SOURCE ); |
| 690 |
@inline = (); |
| 691 |
for ( $i = 0; $i < scalar( @source ); $i++ ) { |
| 692 |
if ( $source[$i] =~ m|^$prefix +EXPORT +INLINE$|o ) { |
| 693 |
push( @inline, "\n" ) if ( $modified == 1 ); |
| 694 |
$modified = 0; |
| 695 |
$c = 0; |
| 696 |
for ( $j = $i + 1; $j < scalar( @source ); $j++ ) { |
| 697 |
$c += ( $source[$j] =~ s/\{/\{/g ); |
| 698 |
$modified = 1 if ( $c != 0 ); |
| 699 |
$c -= ( $source[$j] =~ s/\}/\}/g ); |
| 700 |
if ( $c == 0 && $modified == 1 ) { |
| 701 |
if ( $source[$i + 1] =~ /^[ \t]*inline/ ) { |
| 702 |
push( @inline, $source[$i+1] ); |
| 703 |
} else { |
| 704 |
push( @inline, "inline " . $source[$i + 1] ); |
| 705 |
} |
| 706 |
for ( $c = $i + 2; $c <= $j; $c++ ) { |
| 707 |
push( @inline, $source[$c] ); |
| 708 |
} |
| 709 |
goto contin; |
| 710 |
} |
| 711 |
} |
| 712 |
print "ERROR: Didn't find end of function.\n"; |
| 713 |
exit( -1 ); |
| 714 |
contin: |
| 715 |
$source[$i] = "$prefixstr BEGIN EXPORT INLINE\n"; |
| 716 |
splice( @source, $i + 1, 0, ( "#ifndef $define\n" ) ); |
| 717 |
$j++; |
| 718 |
splice( @source, $j + 1, 0, "#endif // ! $define\n" ); |
| 719 |
splice( @source, $j + 2, 0, "$prefixstr END EXPORT INLINE\n" ); |
| 720 |
} elsif ( $source[$i] =~ m|^$prefix +BEGIN +EXPORT +INLINE *$|o ) { |
| 721 |
for ( $j = $i + 3; $j < scalar( @source ); $j++ ) { |
| 722 |
if ( $source[$j] =~ m|^$prefix +END +EXPORT +INLINE *$|o ) { |
| 723 |
if ( scalar( @inline ) > 0 ) { |
| 724 |
push( @inline, "\n" ); |
| 725 |
} |
| 726 |
if ( $source[$i+1] =~ /^[ \t]*inli/ ) { |
| 727 |
push( @inline, $source[$i + 2] ); |
| 728 |
} else { |
| 729 |
push( @inline, "inline " . $source[$i + 2] ); |
| 730 |
} |
| 731 |
for ( $c = $i + 3; $c < ($j - 1); $c++ ) { |
| 732 |
push( @inline, $source[$c] ); |
| 733 |
} |
| 734 |
goto contin2; |
| 735 |
} |
| 736 |
} |
| 737 |
print "ERROR: Didn't find end of export area.\n"; |
| 738 |
exit( -1 ); |
| 739 |
contin2: |
| 740 |
} |
| 741 |
} |
| 742 |
if ( $modified ) { |
| 743 |
system( "mv $fromfile $fromfile.bak" ); |
| 744 |
if ( ! open( SOURCE, "> $fromfile" ) ) { |
| 745 |
print "ERROR: Could not write to '$from'.\n"; |
| 746 |
exit( -1 ); |
| 747 |
} |
| 748 |
foreach $string ( @source ) { |
| 749 |
print SOURCE $string; |
| 750 |
} |
| 751 |
close( SOURCE ); |
| 752 |
} |
| 753 |
unshift( @inline, "\n" ); |
| 754 |
unshift( @inline, "#define $define\n" ); |
| 755 |
unshift( @inline, "$prefixstr BEGIN IMPORT INLINE $from\n" ); |
| 756 |
push( @inline, "$prefixstr END IMPORT INLINE $from\n" ); |
| 757 |
splice( @lines, $line, 1, @inline ); |
| 758 |
} |
| 759 |
} |
| 760 |
} |
| 761 |
|
| 762 |
# search for file by name |
| 763 |
sub FindFile { ############################################################# |
| 764 |
local( $filename ) = ( @_[0] ); |
| 765 |
local( $i, $file, $return ); |
| 766 |
|
| 767 |
$return = ""; |
| 768 |
for ( $i = 0; $i < scalar( @incpath ); $i++ ) { |
| 769 |
$temp = $incpath[$i] . "/" . $filename; |
| 770 |
$temp =~ s/^\.\///g; |
| 771 |
$return = $temp if ( -f $temp && $return eq "" ); |
| 772 |
} |
| 773 |
$return; |
| 774 |
} |
| 775 |
|
| 776 |
# returns true if file has changed |
| 777 |
sub FileChanged { ########################################################## |
| 778 |
local( $line, $ret ); |
| 779 |
|
| 780 |
$ret = 1; |
| 781 |
if ( scalar( @lines ) == scalar( @origlines ) ) { |
| 782 |
$ret = 0; |
| 783 |
for ( $line = 0; $line < scalar( @lines ); $line++ ) { |
| 784 |
$ret = 1 if ( $lines[$line] ne $origlines[$line] ); |
| 785 |
} |
| 786 |
} |
| 787 |
$ret; |
| 788 |
} |
| 789 |
|
| 790 |
# splits arguments |
| 791 |
sub SplitArgs { ############################################################ |
| 792 |
local( $string ) = @_[0]; |
| 793 |
local( $stack, $open, $i, $mode, $character, @args ) = (); |
| 794 |
|
| 795 |
$stack = ""; |
| 796 |
@args = (); |
| 797 |
$open = 0; |
| 798 |
|
| 799 |
for ( $i = 0; $i < length( $string ); $i++ ) { |
| 800 |
$character = substr( $string, $i, 1 ); |
| 801 |
$mode = "-"; |
| 802 |
if ( length( $stack ) ) { |
| 803 |
$mode = substr( $stack, length($stack)-1, 1 ); |
| 804 |
if ( $mode eq "\"" ) { # inside string literal |
| 805 |
$i++ if $character eq "\\"; |
| 806 |
chop( $stack ) if $character eq "\""; |
| 807 |
next; |
| 808 |
} elsif ( $mode eq "'" ) { # inside integer literal |
| 809 |
$i++ if $character eq "\\"; |
| 810 |
chop( $stack ) if $character eq "'"; |
| 811 |
next; |
| 812 |
} |
| 813 |
} |
| 814 |
if ( $character eq "," ) { |
| 815 |
if ( $mode eq "-" ) { # bottom level - , separates arguments |
| 816 |
push( @args, substr( $string, $open, $i - $open ) ); |
| 817 |
$open = $i + 1; |
| 818 |
} |
| 819 |
} elsif ( $character eq "\"" ) { # string literal mode |
| 820 |
$stack .= "\""; |
| 821 |
} elsif ( $character eq "'" ) { # character literal mode |
| 822 |
$stack .= "'"; |
| 823 |
} elsif ( $character eq "(" ) { # parenthesis mode |
| 824 |
$stack .= "("; |
| 825 |
} elsif ( $character eq ")" ) { |
| 826 |
die "at position ", $i, " in \"", $string, "\": invalid mode.\n" |
| 827 |
if $mode ne "("; |
| 828 |
chop( $stack ); |
| 829 |
} elsif ( $character eq "{" ) { # curly brace mode |
| 830 |
$stack .= "{"; |
| 831 |
} elsif ( $character eq "}" ) { |
| 832 |
die "at position ", $i, " in \"", $string, "\": invalid mode.\n" |
| 833 |
if $mode ne "{"; |
| 834 |
chop( $stack ); |
| 835 |
} elsif ( $character eq "[" ) { # square bracket mode |
| 836 |
$stack .= "["; |
| 837 |
} elsif ( $character eq "]" ) { |
| 838 |
die "at position ", $i, " in \"", $string, "\": invalid mode.\n" |
| 839 |
if $mode ne "["; |
| 840 |
chop( $stack ); |
| 841 |
} |
| 842 |
} |
| 843 |
push( @args, substr( $string, $open ) ); |
| 844 |
foreach ( @args ) { |
| 845 |
s/^[ \t]*//g; |
| 846 |
s/[ \t]*$//g; |
| 847 |
} |
| 848 |
@args; |
| 849 |
} |
| 850 |
|
| 851 |
# evaluates expressions |
| 852 |
sub DoEvaluations { ######################################################## |
| 853 |
foreach $line ( @lines ) { |
| 854 |
$line =~ s|/\*\$ *EVAL\((.*)\) *\*/|eval "$1"|eg; |
| 855 |
} |
| 856 |
} |
| 857 |
|
| 858 |
# fixes some spacing |
| 859 |
sub FixSpacing { ########################################################### |
| 860 |
foreach $line ( @lines ) { |
| 861 |
$line =~ s/([^\\])[ \t]+$/$1/og; # remove unnecessary EOL whitespace |
| 862 |
while ( $line =~ /\t/ ) { # transform TABs to spaces |
| 863 |
( $pre ) = ( $line =~ /^([^\t]*)\t/ ); |
| 864 |
$spaces = 8 - ( length( $pre ) % 8 ); |
| 865 |
substr( $line, length( $pre ), 1 ) = " " x $spaces; |
| 866 |
} |
| 867 |
} |
| 868 |
} |
| 869 |
|
| 870 |
# prints program usage |
| 871 |
sub PrintUsage { ########################################################### |
| 872 |
print <<"END_OF_USAGE"; |
| 873 |
Usage: |
| 874 |
$0 [options] [files...] |
| 875 |
Options: |
| 876 |
--fold-templates : fold template definitions |
| 877 |
--inline : inline the inline functions |
| 878 |
--fix-spacing : convert TAB to spaces, and remove EOL whitespace |
| 879 |
-I<dir> : template include directory |
| 880 |
-d, --debug : dump debug output |
| 881 |
-h, -?, --usage : display usage information |
| 882 |
-u, --uncompact : don't compact template headers where possible |
| 883 |
-V, --version : display version information |
| 884 |
Environment: |
| 885 |
TEMPLANT_INCLUDE : include directories for templant |
| 886 |
Directives: |
| 887 |
[PREFIXSTR] INSERT TEMPLATE TemplateName( args ) |
| 888 |
[PREFIXSTR] IMPORT INLINE FilePath |
| 889 |
[PREFIXSTR] EXPORT INLINE |
| 890 |
|
| 891 |
([PREFIXSTR] can be either ``##\$'' or ``//\$'' or you can envelope the |
| 892 |
command with /*$ and */). |
| 893 |
END_OF_USAGE |
| 894 |
} |
| 895 |
|
| 896 |
# print CVS version |
| 897 |
sub PrintVersion { ######################################################### |
| 898 |
print '$Id$', "\n"; |
| 899 |
} |
| 900 |
|
| 901 |
############################################################################ |