| 1 |
#!/usr/bin/perl -w |
| 2 |
use strict; |
| 3 |
|
| 4 |
|
| 5 |
my %Ignore; |
| 6 |
my %Ignored; |
| 7 |
my %WinIgnore; |
| 8 |
my %Exclude; |
| 9 |
|
| 10 |
my $oops = 0; |
| 11 |
|
| 12 |
use Getopt::Std; |
| 13 |
my %opt; |
| 14 |
getopts('mt',\%opt); |
| 15 |
my @Files; |
| 16 |
|
| 17 |
sub openRO |
| 18 |
{ |
| 19 |
my ($fh,$file) = @_; |
| 20 |
if (-f $file && !-w $file) |
| 21 |
{ |
| 22 |
chmod(0666,$file) || warn "Cannot change permissions on $file:$!"; |
| 23 |
} |
| 24 |
open($fh,">","$file~") || return 0; |
| 25 |
push(@Files,$file); |
| 26 |
return 1; |
| 27 |
} |
| 28 |
|
| 29 |
END |
| 30 |
{ |
| 31 |
while (@Files) |
| 32 |
{ |
| 33 |
my $file = pop(@Files); |
| 34 |
if (-f $file) |
| 35 |
{ |
| 36 |
chmod(0444,"$file~") || warn "Cannot change permissions on $file:$!"; |
| 37 |
if (!rename("$file~",$file)) |
| 38 |
{ |
| 39 |
warn "Cannot rename $file to $file~ ($!), trying again with deleting $file before..."; |
| 40 |
unlink($file) || warn "Cannot delete $file:$!"; |
| 41 |
rename("$file~",$file) || warn "Cannot rename $file~ to $file ($!), expect major problems now..."; |
| 42 |
} |
| 43 |
} |
| 44 |
} |
| 45 |
} |
| 46 |
|
| 47 |
my $win_arch = shift; |
| 48 |
die "Unknown \$win_arch" unless $win_arch eq 'open32' |
| 49 |
or $win_arch eq 'pm' |
| 50 |
or $win_arch eq 'x' |
| 51 |
or $win_arch eq 'MSWin32'; |
| 52 |
my $xexcl = <<EOM; |
| 53 |
#if (defined(__WIN32__) || defined(__PM__)) && !defined(DO_X_EXCLUDE) |
| 54 |
# define DO_X_EXCLUDE |
| 55 |
#endif |
| 56 |
EOM |
| 57 |
|
| 58 |
sub Ignore |
| 59 |
{ |
| 60 |
my $cfile = shift; |
| 61 |
if (open(C,"<$cfile")) |
| 62 |
{ |
| 63 |
warn "Ignoring from $cfile\n"; |
| 64 |
while (<C>) |
| 65 |
{ |
| 66 |
if (/^([A-Za-z][A-Za-z0-9_]*)/) |
| 67 |
{ |
| 68 |
$Ignore{$1} = $cfile; |
| 69 |
} |
| 70 |
} |
| 71 |
close(C); |
| 72 |
} |
| 73 |
else |
| 74 |
{ |
| 75 |
warn "Cannot open $cfile:$!"; |
| 76 |
} |
| 77 |
} |
| 78 |
|
| 79 |
sub WinIgnore |
| 80 |
{ |
| 81 |
my $cfile = shift; |
| 82 |
if (open(C,"<$cfile")) |
| 83 |
{ |
| 84 |
warn "WinIgnoring from $cfile\n"; |
| 85 |
while (<C>) |
| 86 |
{ |
| 87 |
if (/^([A-Za-z][A-Za-z0-9_]*)/) |
| 88 |
{ |
| 89 |
$WinIgnore{$1} = $cfile; |
| 90 |
} |
| 91 |
} |
| 92 |
close(C); |
| 93 |
} |
| 94 |
else |
| 95 |
{ |
| 96 |
warn "Cannot open $cfile:$!"; |
| 97 |
} |
| 98 |
} |
| 99 |
|
| 100 |
sub Exclude |
| 101 |
{ |
| 102 |
my $cfile = shift; |
| 103 |
if (open(C,"<$cfile")) |
| 104 |
{ |
| 105 |
while (<C>) |
| 106 |
{ |
| 107 |
if (/{\s*\"[^\"]+\"\s*,\s*(\w+)\s*}/) |
| 108 |
{ |
| 109 |
$Exclude{$1} = $cfile; |
| 110 |
} |
| 111 |
} |
| 112 |
close(C); |
| 113 |
} |
| 114 |
else |
| 115 |
{ |
| 116 |
warn "Cannot open $cfile:$!"; |
| 117 |
} |
| 118 |
} |
| 119 |
|
| 120 |
sub Vfunc |
| 121 |
{ |
| 122 |
my $hfile = shift; |
| 123 |
my %VFunc = (); |
| 124 |
my %VVar = (); |
| 125 |
my %VError= (); |
| 126 |
my $errors = 0; |
| 127 |
my @ifdef = (''); |
| 128 |
open(H,"<$hfile") || die "Cannot open $hfile:$!"; |
| 129 |
my $gard = "\U$hfile"; |
| 130 |
$gard =~ s/\..*$//; |
| 131 |
$gard =~ s#/#_#g; |
| 132 |
|
| 133 |
while (<H>) |
| 134 |
{ |
| 135 |
if (/^\s*#\s*if/) |
| 136 |
{ |
| 137 |
s#//.*##; |
| 138 |
s#/\*.*?\*/# #g; |
| 139 |
s/\s+$//; |
| 140 |
s/^\s*#\s*ifndef\s+_$gard\b.*//; |
| 141 |
s/^\s*#\s*ifndef\s+_\w+_H_\b.*//; |
| 142 |
warn "'$gard' in '$_'" if /$gard/; |
| 143 |
push(@ifdef,$_); |
| 144 |
} |
| 145 |
elsif (/^\s*#\s*else/) |
| 146 |
{ |
| 147 |
s/\s+$//; |
| 148 |
#warn "$hfile:$.:$_\n"; |
| 149 |
$ifdef[-1] = $_; |
| 150 |
} |
| 151 |
elsif (/^\s*#\s*endif\b/) |
| 152 |
{ |
| 153 |
pop(@ifdef); |
| 154 |
} |
| 155 |
elsif (/^\s*(MOVEXT|COREXT|EXTERN|extern)\s*(.*?)\s*(\w+)\s+_ANSI_ARGS_\s*\((TCL_VARARGS)?\(/) |
| 156 |
{ |
| 157 |
my ($type,$name,$op) = ($2,$3,$4); |
| 158 |
if ($1 eq 'MOVEXT' || $1 eq 'COREXT') |
| 159 |
{ |
| 160 |
warn "$1 $name\n"; |
| 161 |
$oops++; |
| 162 |
$Ignore{$name} = $hfile; |
| 163 |
} |
| 164 |
$op = "" unless (defined $op); |
| 165 |
my $defn = "VFUNC($type,$name,V_$name,_ANSI_ARGS_($op("; |
| 166 |
$_ = $'; |
| 167 |
until (/\)\);\s*$/) |
| 168 |
{ |
| 169 |
$defn .= $_; |
| 170 |
$_ = <H>; |
| 171 |
if (/^\S/) |
| 172 |
{ |
| 173 |
chomp($_); |
| 174 |
die $_; |
| 175 |
} |
| 176 |
} |
| 177 |
s/\)\);\s*$/\)\)\)\n/; |
| 178 |
$defn .= $_; |
| 179 |
die "$hfile:$.:$ifdef[-1]\n" if $ifdef[-1] =~ /\belse\b/; |
| 180 |
if (exists($VFunc{$name}{$ifdef[-1]}) && $defn ne $VFunc{$name}{$ifdef[-1]}) |
| 181 |
{ |
| 182 |
warn "Function (@ifdef) $name is $defn and $VFunc{$name}{$ifdef[-1]}"; |
| 183 |
$errors++; |
| 184 |
} |
| 185 |
else |
| 186 |
{ |
| 187 |
$VFunc{$name}{$ifdef[-1]} = $defn; |
| 188 |
} |
| 189 |
} |
| 190 |
elsif (/^\s*(MOVEXT|COREXT|EXTERN|extern)\s*(.*?)\s*(\w+)\s*;/) |
| 191 |
{ |
| 192 |
my ($type,$name) = ($2,$3); |
| 193 |
if ($1 eq 'MOVEXT' || $1 eq 'COREXT') |
| 194 |
{ |
| 195 |
warn "$1 $name\n"; |
| 196 |
$oops++; |
| 197 |
$Ignore{$name} = $hfile; |
| 198 |
} |
| 199 |
my $defn = "VVAR($type,$name,V_$name)\n"; |
| 200 |
die "$hfile:$.:$ifdef[-1]\n" if $ifdef[-1] =~ /\belse\b/; |
| 201 |
if (exists $VVar{$name}{$ifdef[-1]}) |
| 202 |
{ |
| 203 |
warn "Variable (@ifdef) $name is $defn and $VVar{$name}{$ifdef[-1]}"; |
| 204 |
$errors++; |
| 205 |
} |
| 206 |
else |
| 207 |
{ |
| 208 |
$VVar{$name}{$ifdef[-1]} = $defn; |
| 209 |
} |
| 210 |
} |
| 211 |
elsif (/\b(EXTERN|extern)\s+[\w_]+\s+[\w_]+\[\];$/) |
| 212 |
{ |
| 213 |
|
| 214 |
} |
| 215 |
elsif (/\b(EXTERN|extern)\s*"C"\s*\{\s*$/) |
| 216 |
{ |
| 217 |
|
| 218 |
} |
| 219 |
elsif (/\b(EXTERN|extern)\b/) |
| 220 |
{ |
| 221 |
warn "$hfile:$.: $_" unless (/^\s*\#\s*define/); |
| 222 |
} |
| 223 |
} |
| 224 |
close(H); |
| 225 |
die "Multiple definitions\n" if $errors; |
| 226 |
|
| 227 |
|
| 228 |
if (keys %VFunc || keys %VVar) |
| 229 |
{ |
| 230 |
my $name = "\u\L${gard}\UV"; |
| 231 |
my $fdef = $hfile; |
| 232 |
$fdef =~ s/\..*$/.t/; |
| 233 |
my $mdef = $hfile; |
| 234 |
$mdef =~ s/\..*$/.m/; |
| 235 |
|
| 236 |
$mdef .= 'dmy' unless $opt{'m'}; |
| 237 |
$fdef .= 'dmy' unless $opt{'t'}; |
| 238 |
|
| 239 |
my $htfile = $hfile; |
| 240 |
$htfile =~ s/\..*$/_f.h/; |
| 241 |
unless (-r $htfile) |
| 242 |
{ |
| 243 |
openRO(\*C,$htfile) || die "Cannot open $htfile:$!"; |
| 244 |
print C "#ifndef ${gard}_VT\n"; |
| 245 |
print C "#define ${gard}_VT\n"; |
| 246 |
print C "typedef struct ${name}tab\n{\n"; |
| 247 |
print C " unsigned (*tabSize)(void);\n"; |
| 248 |
print C "#define VFUNC(type,name,mem,args) type (*mem) args;\n"; |
| 249 |
print C "#define VVAR(type,name,mem) type (*mem);\n"; |
| 250 |
print C "#include \"$fdef\"\n"; |
| 251 |
print C "#undef VFUNC\n"; |
| 252 |
print C "#undef VVAR\n"; |
| 253 |
print C "} ${name}tab;\n"; |
| 254 |
print C "extern ${name}tab *${name}ptr;\n"; |
| 255 |
print C "extern ${name}tab *${name}Get(void);\n"; |
| 256 |
print C "#endif /* ${gard}_VT */\n"; |
| 257 |
close(C); |
| 258 |
} |
| 259 |
|
| 260 |
my $cfile = $hfile; |
| 261 |
$cfile =~ s/\..*$/_f.c/; |
| 262 |
unless (-r $cfile) |
| 263 |
{ |
| 264 |
openRO(\*C,$cfile) || die "Cannot open $cfile:$!"; |
| 265 |
print C "#include \"$hfile\"\n"; |
| 266 |
print C "#include \"$htfile\"\n"; |
| 267 |
print C "static unsigned ${name}Size(void) { return sizeof(${name}tab);}\n"; |
| 268 |
print C "static ${name}tab ${name}table =\n{\n"; |
| 269 |
print C " ${name}Size,\n"; |
| 270 |
print C "#define VFUNC(type,name,mem,args) name,\n"; |
| 271 |
print C "#define VVAR(type,name,mem) &name,\n"; |
| 272 |
print C "#include \"$fdef\"\n"; |
| 273 |
print C "#undef VFUNC\n"; |
| 274 |
print C "#undef VVAR\n"; |
| 275 |
print C "};\n"; |
| 276 |
print C "${name}tab *${name}ptr;\n"; |
| 277 |
print C "${name}tab *${name}Get() { return ${name}ptr = &${name}table;}\n"; |
| 278 |
close(C); |
| 279 |
} |
| 280 |
|
| 281 |
print STDERR "$gard\n"; |
| 282 |
openRO(\*VFUNC,$fdef) || die "Cannot open $fdef:$!"; |
| 283 |
openRO(\*VMACRO,$mdef) || die "Cannot open $mdef:$!"; |
| 284 |
print VFUNC "#ifdef _$gard\n"; |
| 285 |
print VMACRO "#ifndef _${gard}_VM\n"; |
| 286 |
print VMACRO "#define _${gard}_VM\n"; |
| 287 |
print VMACRO "#include \"$htfile\"\n"; |
| 288 |
print VMACRO "#ifndef NO_VTABLES\n"; |
| 289 |
print VMACRO $xexcl if %WinIgnore; |
| 290 |
print VFUNC $xexcl if %WinIgnore; |
| 291 |
foreach my $func (sort keys %VVar) |
| 292 |
{ |
| 293 |
if (!exists($Exclude{$func}) && !exists($Ignore{$func})) |
| 294 |
{ |
| 295 |
foreach my $ifdef (sort keys %{$VVar{$func}}) |
| 296 |
{ |
| 297 |
print VFUNC "$ifdef\n" if ($ifdef); |
| 298 |
print VFUNC $VVar{$func}{$ifdef}; |
| 299 |
print VFUNC "#endif /* $ifdef */\n" if ($ifdef); |
| 300 |
} |
| 301 |
print VMACRO "#define $func (*${name}ptr->V_$func)\n"; |
| 302 |
} |
| 303 |
$Ignored{$func} = delete $Ignore{$func} if exists $Ignore{$func}; |
| 304 |
} |
| 305 |
foreach my $func (sort keys %VFunc) |
| 306 |
{ |
| 307 |
if (!exists($Exclude{$func}) && !exists($Ignore{$func})) |
| 308 |
{ |
| 309 |
print VFUNC "#ifndef DO_X_EXCLUDE\n" if exists($WinIgnore{$func}); |
| 310 |
print VFUNC "#ifndef $func\n"; |
| 311 |
foreach my $ifdef (sort keys %{$VFunc{$func}}) |
| 312 |
{ |
| 313 |
print VFUNC "$ifdef\n" if ($ifdef); |
| 314 |
print VFUNC $VFunc{$func}{$ifdef}; |
| 315 |
print VFUNC "#endif /* $ifdef */\n" if ($ifdef); |
| 316 |
} |
| 317 |
print VFUNC "#endif /* #ifndef $func */\n"; |
| 318 |
print VFUNC "#endif /* !DO_X_EXCLUDE */\n" if exists($WinIgnore{$func}); |
| 319 |
print VFUNC "\n"; |
| 320 |
|
| 321 |
print VMACRO "#ifndef DO_X_EXCLUDE\n" if exists($WinIgnore{$func}); |
| 322 |
print VMACRO "#ifndef $func\n"; |
| 323 |
print VMACRO "# define $func (*${name}ptr->V_$func)\n"; |
| 324 |
print VMACRO "#endif\n"; |
| 325 |
print VMACRO "#endif /* !DO_X_EXCLUDE */\n" if exists($WinIgnore{$func}); |
| 326 |
print VMACRO "\n"; |
| 327 |
} |
| 328 |
$Ignored{$func} = delete $Ignore{$func} if exists $Ignore{$func}; |
| 329 |
} |
| 330 |
print VMACRO "#endif /* NO_VTABLES */\n"; |
| 331 |
print VMACRO "#endif /* _${gard}_VM */\n"; |
| 332 |
close(VMACRO); |
| 333 |
print VFUNC "#endif /* _$gard */\n"; |
| 334 |
close(VFUNC); # Close this last - Makefile dependency |
| 335 |
|
| 336 |
unlink($mdef) unless $opt{'m'}; |
| 337 |
unlink($fdef) unless $opt{'t'}; |
| 338 |
} |
| 339 |
else |
| 340 |
{ |
| 341 |
die "No entries in $hfile\n"; |
| 342 |
} |
| 343 |
} |
| 344 |
|
| 345 |
foreach (<tk*Tab.c>) |
| 346 |
{ |
| 347 |
Exclude($_); |
| 348 |
} |
| 349 |
|
| 350 |
die "Usage: $0 <some.h>\n" if (@ARGV != 1); |
| 351 |
|
| 352 |
my $h = shift; |
| 353 |
my $x = $h; |
| 354 |
$x =~ s/\.h/.exc/; |
| 355 |
Ignore($x) if (-f $x); |
| 356 |
$x =~ s/\.exc/.excwin/; |
| 357 |
WinIgnore($x) if (-f $x); |
| 358 |
Vfunc($h); |
| 359 |
|
| 360 |
foreach my $s (sort keys %Ignore) |
| 361 |
{ |
| 362 |
warn "$s is not in $h\n"; |
| 363 |
$oops++; |
| 364 |
} |
| 365 |
|
| 366 |
if ($oops) |
| 367 |
{ |
| 368 |
$x = $h; |
| 369 |
$x =~ s/\.h/.exc/; |
| 370 |
rename($x,"$x.old") || die "Cannot rename $x to $x.old:$!"; |
| 371 |
open(EXC,">$x") || die "Cannot open $x:$!"; |
| 372 |
foreach my $s (sort keys %Ignored) |
| 373 |
{ |
| 374 |
print EXC $s,"\n"; |
| 375 |
} |
| 376 |
close(EXC); |
| 377 |
} |
| 378 |
|
| 379 |
__END__ |
| 380 |
|
| 381 |
=head1 NAME |
| 382 |
|
| 383 |
mkVFunc - Support for "nested" dynamic loading |
| 384 |
|
| 385 |
=head1 SYNOPSIS |
| 386 |
|
| 387 |
mkVFunc xxx.h |
| 388 |
|
| 389 |
=head1 DESCRIPTION |
| 390 |
|
| 391 |
B<perl/Tk> is designed so that B<Tk> can be dynamically loaded 'on top of' |
| 392 |
perl. That is the easy bit. What it also does is allow Tk::Xxxx to be |
| 393 |
dynamically loaded 'on top of' the B<perl/Tk> composite. Thus when |
| 394 |
you 'require Tk::HList' the shared object F<.../HList.so> needs to be |
| 395 |
able to call functions defined in perl I<and> functions defined in loadable |
| 396 |
.../Tk.so . Now functions in 'base executable' are a well known problem, |
| 397 |
and are solved by DynaLoader. However most of dynamic loading schemes |
| 398 |
cannot handle one loadable calling another loadable. |
| 399 |
|
| 400 |
Thus what Tk does is build a table of functions that should be callable. |
| 401 |
This table is auto-generated from the .h file by looking for |
| 402 |
'extern' (and EXTERN which is #defined to 'extern'). |
| 403 |
Thus any function marked as 'extern' is 'referenced' by the table. |
| 404 |
The address of the table is then stored in a perl variable when Tk is loaded. |
| 405 |
When HList is loaded it looks in the perl variable (via functions |
| 406 |
in perl - the 'base executable') to get the address of the table. |
| 407 |
|
| 408 |
The same utility that builds the table also builds a set of #define's. |
| 409 |
HList.c (and any other .c files which comprise HList) #include these |
| 410 |
#define's. So that |
| 411 |
|
| 412 |
Tk_SomeFunc(x,y,z) |
| 413 |
|
| 414 |
Is actually compiled as |
| 415 |
|
| 416 |
(*TkVptr->V_Tk_SomeFunc)(x,y,z) |
| 417 |
|
| 418 |
Where Tk_ptr is pointer to the table. |
| 419 |
|
| 420 |
See: |
| 421 |
|
| 422 |
Tk-b*/pTk/mkVFunc - perl script that produces tables |
| 423 |
/tk.h - basis from which table is generated |
| 424 |
/tk.m - #define's to include in sub-extension |
| 425 |
/tk_f.h - #included both sides. |
| 426 |
/tk_f.c - Actual table definition. |
| 427 |
/tk.t - 'shared' set of macros which produce table |
| 428 |
included in tk_f.c and tk_f.h |
| 429 |
/tkVMacro.h - Wrapper to include *.m files |
| 430 |
|
| 431 |
In addition to /tk* there are /tkInt*, /Lang* and /tix* |
| 432 |
|
| 433 |
=cut |