home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / perl_utl.zip / h2xs.cmd < prev    next >
OS/2 REXX Batch file  |  1997-11-28  |  22KB  |  857 lines

  1. extproc perl -S
  2. #!f:/perllib/bin/perl
  3.     eval 'exec f:/perllib/bin/perl -S $0 ${1+"$@"}'
  4.     if $running_under_some_shell;
  5.  
  6. =head1 NAME
  7.  
  8. h2xs - convert .h C header files to Perl extensions
  9.  
  10. =head1 SYNOPSIS
  11.  
  12. B<h2xs> [B<-AOPXcdf>] [B<-v> version] [B<-n> module_name] [B<-p> prefix] [B<-s> sub] [headerfile [extra_libraries]]
  13.  
  14. B<h2xs> B<-h>
  15.  
  16. =head1 DESCRIPTION
  17.  
  18. I<h2xs> builds a Perl extension from any C header file.  The extension will
  19. include functions which can be used to retrieve the value of any #define
  20. statement which was in the C header.
  21.  
  22. The I<module_name> will be used for the name of the extension.  If
  23. module_name is not supplied then the name of the header file will be used,
  24. with the first character capitalized.
  25.  
  26. If the extension might need extra libraries, they should be included
  27. here.  The extension Makefile.PL will take care of checking whether
  28. the libraries actually exist and how they should be loaded.
  29. The extra libraries should be specified in the form -lm -lposix, etc,
  30. just as on the cc command line.  By default, the Makefile.PL will
  31. search through the library path determined by Configure.  That path
  32. can be augmented by including arguments of the form B<-L/another/library/path>
  33. in the extra-libraries argument.
  34.  
  35. =head1 OPTIONS
  36.  
  37. =over 5
  38.  
  39. =item B<-A>
  40.  
  41. Omit all autoload facilities.  This is the same as B<-c> but also removes the
  42. S<C<require AutoLoader>> statement from the .pm file.
  43.  
  44. =item B<-F>
  45.  
  46. Additional flags to specify to C preprocessor when scanning header for
  47. function declarations. Should not be used without B<-x>.
  48.  
  49. =item B<-O>
  50.  
  51. Allows a pre-existing extension directory to be overwritten.
  52.  
  53. =item B<-P>
  54.  
  55. Omit the autogenerated stub POD section. 
  56.  
  57. =item B<-X>
  58.  
  59. Omit the XS portion.  Used to generate templates for a module which is not
  60. XS-based.
  61.  
  62. =item B<-c>
  63.  
  64. Omit C<constant()> from the .xs file and corresponding specialised
  65. C<AUTOLOAD> from the .pm file.
  66.  
  67. =item B<-d>
  68.  
  69. Turn on debugging messages.
  70.  
  71. =item B<-f>
  72.  
  73. Allows an extension to be created for a header even if that header is
  74. not found in /usr/include.
  75.  
  76. =item B<-h>
  77.  
  78. Print the usage, help and version for this h2xs and exit.
  79.  
  80. =item B<-n> I<module_name>
  81.  
  82. Specifies a name to be used for the extension, e.g., S<-n RPC::DCE>
  83.  
  84. =item B<-p> I<prefix>
  85.  
  86. Specify a prefix which should be removed from the Perl function names, e.g., S<-p sec_rgy_> 
  87. This sets up the XS B<PREFIX> keyword and removes the prefix from functions that are
  88. autoloaded via the C<constant()> mechansim.
  89.  
  90. =item B<-s> I<sub1,sub2>
  91.  
  92. Create a perl subroutine for the specified macros rather than autoload with the constant() subroutine.
  93. These macros are assumed to have a return type of B<char *>, e.g., S<-s sec_rgy_wildcard_name,sec_rgy_wildcard_sid>.
  94.  
  95. =item B<-v> I<version>
  96.  
  97. Specify a version number for this extension.  This version number is added
  98. to the templates.  The default is 0.01.
  99.  
  100. =item B<-x>
  101.  
  102. Automatically generate XSUBs basing on function declarations in the
  103. header file.  The package C<C::Scan> should be installed. If this
  104. option is specified, the name of the header file may look like
  105. C<NAME1,NAME2>. In this case NAME1 is used instead of the specified string,
  106. but XSUBs are emitted only for the declarations included from file NAME2.
  107.  
  108. Note that some types of arguments/return-values for functions may
  109. result in XSUB-declarations/typemap-entries which need
  110. hand-editing. Such may be objects which cannot be converted from/to a
  111. pointer (like C<long long>), pointers to functions, or arrays.
  112.  
  113. =back
  114.  
  115. =head1 EXAMPLES
  116.  
  117.  
  118.     # Default behavior, extension is Rusers
  119.     h2xs rpcsvc/rusers
  120.  
  121.     # Same, but extension is RUSERS
  122.     h2xs -n RUSERS rpcsvc/rusers
  123.  
  124.     # Extension is rpcsvc::rusers. Still finds <rpcsvc/rusers.h>
  125.     h2xs rpcsvc::rusers
  126.  
  127.     # Extension is ONC::RPC.  Still finds <rpcsvc/rusers.h>
  128.     h2xs -n ONC::RPC rpcsvc/rusers
  129.  
  130.     # Without constant() or AUTOLOAD
  131.     h2xs -c rpcsvc/rusers
  132.  
  133.     # Creates templates for an extension named RPC
  134.     h2xs -cfn RPC
  135.  
  136.     # Extension is ONC::RPC.
  137.     h2xs -cfn ONC::RPC
  138.  
  139.     # Makefile.PL will look for library -lrpc in 
  140.     # additional directory /opt/net/lib
  141.     h2xs rpcsvc/rusers -L/opt/net/lib -lrpc
  142.  
  143.         # Extension is DCE::rgynbase
  144.         # prefix "sec_rgy_" is dropped from perl function names
  145.         h2xs -n DCE::rgynbase -p sec_rgy_ dce/rgynbase
  146.  
  147.         # Extension is DCE::rgynbase
  148.         # prefix "sec_rgy_" is dropped from perl function names
  149.         # subroutines are created for sec_rgy_wildcard_name and sec_rgy_wildcard_sid
  150.         h2xs -n DCE::rgynbase -p sec_rgy_ \
  151.         -s sec_rgy_wildcard_name,sec_rgy_wildcard_sid dce/rgynbase
  152.  
  153.     # Make XS without defines in perl.h, but with function declarations
  154.     # visible from perl.h. Name of the extension is perl1.
  155.     # When scanning perl.h, define -DEXT=extern -DdEXT= -DINIT(x)=
  156.     # Extra backslashes below because the string is passed to shell.
  157.     # Note that a directory with perl header files would 
  158.     #  be added automatically to include path.
  159.     h2xs -xAn perl1 -F "-DEXT=extern -DdEXT= -DINIT\(x\)=" perl.h
  160.  
  161.     # Same with function declaration in proto.h as visible from perl.h.
  162.     h2xs -xAn perl2 perl.h,proto.h
  163.  
  164. =head1 ENVIRONMENT
  165.  
  166. No environment variables are used.
  167.  
  168. =head1 AUTHOR
  169.  
  170. Larry Wall and others
  171.  
  172. =head1 SEE ALSO
  173.  
  174. L<perl>, L<perlxstut>, L<ExtUtils::MakeMaker>, and L<AutoLoader>.
  175.  
  176. =head1 DIAGNOSTICS
  177.  
  178. The usual warnings if it cannot read or write the files involved.
  179.  
  180. =cut
  181.  
  182. my( $H2XS_VERSION ) = ' $Revision: 1.18 $ ' =~ /\$Revision:\s+([^\s]+)/;
  183. my $TEMPLATE_VERSION = '0.01';
  184.  
  185. use Getopt::Std;
  186.  
  187. sub usage{
  188.     warn "@_\n" if @_;
  189.     die "h2xs [-AOPXcdfh] [-v version] [-n module_name] [-p prefix] [-s subs] [headerfile [extra_libraries]]
  190. version: $H2XS_VERSION
  191.     -A   Omit all autoloading facilities (implies -c).
  192.     -F   Additional flags for C preprocessor (used with -x).
  193.     -O   Allow overwriting of a pre-existing extension directory.
  194.     -P   Omit the stub POD section.
  195.     -X   Omit the XS portion.
  196.     -c   Omit the constant() function and specialised AUTOLOAD from the XS file.
  197.     -d   Turn on debugging messages.
  198.     -f   Force creation of the extension even if the C header does not exist.
  199.     -h   Display this help message
  200.     -n   Specify a name to use for the extension (recommended).
  201.     -p   Specify a prefix which should be removed from the Perl function names.
  202.     -s   Create subroutines for specified macros.
  203.     -v   Specify a version number for this extension.
  204.     -x   Autogenerate XSUBs using C::Scan.
  205. extra_libraries
  206.          are any libraries that might be needed for loading the
  207.          extension, e.g. -lm would try to link in the math library.
  208. ";
  209. }
  210.  
  211.  
  212. getopts("AF:OPXcdfhn:p:s:v:x") || usage;
  213.  
  214. usage if $opt_h;
  215.  
  216. if( $opt_v ){
  217.     $TEMPLATE_VERSION = $opt_v;
  218. }
  219. $opt_c = 1 if $opt_A;
  220. %const_xsub = map { $_,1 } split(/,+/, $opt_s) if $opt_s;
  221.  
  222. $path_h    = shift;
  223. $extralibs = "@ARGV";
  224.  
  225. usage "Must supply header file or module name\n"
  226.     unless ($path_h or $opt_n);
  227.  
  228.  
  229. if( $path_h ){
  230.     $name = $path_h;
  231.     if( $path_h =~ s#::#/#g && $opt_n ){
  232.     warn "Nesting of headerfile ignored with -n\n";
  233.     }
  234.     $path_h .= ".h" unless $path_h =~ /\.h$/;
  235.     $fullpath = $path_h;
  236.     $path_h =~ s/,.*$// if $opt_x;
  237.     if ($^O eq 'VMS') {  # Consider overrides of default location
  238.     if ($path_h !~ m![:>\[]!) {
  239.         my($hadsys) = ($path_h =~ s!^sys/!!i);
  240.         if ($ENV{'DECC$System_Include'})     { $path_h = "DECC\$System_Include:$path_h";    }
  241.         elsif ($ENV{'DECC$Library_Include'}) { $path_h = "DECC\$Library_Include:$path_h";   }
  242.         elsif ($ENV{'GNU_CC_Include'})       { $path_h = 'GNU_CC_Include:' .
  243.                                                 ($hadsys ? '[vms]' : '[000000]') . $path_h; }
  244.         elsif ($ENV{'VAXC$Include'})         { $path_h = "VAXC\$_Include:$path_h";          }
  245.         else                                 { $path_h = "Sys\$Library:$path_h";            }
  246.     }
  247.     }
  248.     elsif ($^O eq 'os2') {
  249.     $path_h = "/usr/include/$path_h" 
  250.       if $path_h !~ m#^([a-z]:)?[./]#i and -r "/usr/include/$path_h"; 
  251.     }
  252.     else { 
  253.       $path_h = "/usr/include/$path_h" 
  254.     if $path_h !~ m#^[./]# and -r "/usr/include/$path_h"; 
  255.     }
  256.  
  257.     if (!$opt_c) {
  258.       die "Can't find $path_h\n" if ( ! $opt_f && ! -f $path_h );
  259.       # Scan the header file (we should deal with nested header files)
  260.       # Record the names of simple #define constants into const_names
  261.       # Function prototypes are not (currently) processed.
  262.       open(CH, "<$path_h") || die "Can't open $path_h: $!\n";
  263.       while (<CH>) {
  264.     if (/^#[ \t]*define\s+([\$\w]+)\b\s*[^("]/) {
  265.         print "Matched $_ ($1)\n" if $opt_d;
  266.         $_ = $1;
  267.         next if /^_.*_h_*$/i; # special case, but for what?
  268.         if (defined $opt_p) {
  269.           if (!/^$opt_p(\d)/) {
  270.         ++$prefix{$_} if s/^$opt_p//;
  271.           }
  272.           else {
  273.         warn "can't remove $opt_p prefix from '$_'!\n";
  274.           }
  275.         }
  276.         $const_names{$_}++;
  277.       }
  278.       }
  279.       close(CH);
  280.       @const_names = sort keys %const_names;
  281.     }
  282. }
  283.  
  284.  
  285. $module = $opt_n || do {
  286.     $name =~ s/\.h$//;
  287.     if( $name !~ /::/ ){
  288.         $name =~ s#^.*/##;
  289.         $name = "\u$name";
  290.     }
  291.     $name;
  292. };
  293.  
  294. (chdir 'ext', $ext = 'ext/') if -d 'ext';
  295.  
  296. if( $module =~ /::/ ){
  297.     $nested = 1;
  298.     @modparts = split(/::/,$module);
  299.     $modfname = $modparts[-1];
  300.     $modpname = join('/',@modparts);
  301. }
  302. else {
  303.     $nested = 0;
  304.     @modparts = ();
  305.     $modfname = $modpname = $module;
  306. }
  307.  
  308.  
  309. if ($opt_O) {
  310.     warn "Overwriting existing $ext$modpname!!!\n" if -e $modpname;
  311. } else {
  312.     die "Won't overwrite existing $ext$modpname\n" if -e $modpname;
  313. }
  314. if( $nested ){
  315.     $modpath = "";
  316.     foreach (@modparts){
  317.         mkdir("$modpath$_", 0777);
  318.         $modpath .= "$_/";
  319.     }
  320. }
  321. mkdir($modpname, 0777);
  322. chdir($modpname) || die "Can't chdir $ext$modpname: $!\n";
  323.  
  324. my %types_seen;
  325. my %std_types;
  326. my $fdecls;
  327. my $fdecls_parsed;
  328.  
  329. if( ! $opt_X ){  # use XS, unless it was disabled
  330.   open(XS, ">$modfname.xs") || die "Can't create $ext$modpname/$modfname.xs: $!\n";
  331.   if ($opt_x) {
  332.     require C::Scan;        # Run-time directive
  333.     require Config;        # Run-time directive
  334.     warn "Scanning typemaps...\n";
  335.     get_typemap();
  336.     my $c;
  337.     my $filter;
  338.     my $filename = $path_h;
  339.     my $addflags = $opt_F || '';
  340.     if ($fullpath =~ /,/) {
  341.       $filename = $`;
  342.       $filter = $';
  343.     }
  344.     warn "Scanning $filename for functions...\n";
  345.     $c = new C::Scan 'filename' => $filename, 'filename_filter' => $filter,
  346.     'add_cppflags' => $addflags;
  347.     $c->set('includeDirs' => ["$Config::Config{archlib}/CORE"]);
  348.     
  349.     $fdecls_parsed = $c->get('parsed_fdecls');
  350.     $fdecls = $c->get('fdecls');
  351.   }
  352. }
  353.  
  354. open(PM, ">$modfname.pm") || die "Can't create $ext$modpname/$modfname.pm: $!\n";
  355.  
  356. $" = "\n\t";
  357. warn "Writing $ext$modpname/$modfname.pm\n";
  358.  
  359. print PM <<"END";
  360. package $module;
  361.  
  362. use strict;
  363. END
  364.  
  365. if( $opt_X || $opt_c || $opt_A ){
  366.     # we won't have our own AUTOLOAD(), so won't have $AUTOLOAD
  367.     print PM <<'END';
  368. use vars qw($VERSION @ISA @EXPORT @EXPORT_OK);
  369. END
  370. }
  371. else{
  372.     # we'll have an AUTOLOAD(), and it will have $AUTOLOAD and
  373.     # will want Carp.
  374.     print PM <<'END';
  375. use Carp;
  376. use vars qw($VERSION @ISA @EXPORT @EXPORT_OK $AUTOLOAD);
  377. END
  378. }
  379.  
  380. print PM <<'END';
  381.  
  382. require Exporter;
  383. END
  384.  
  385. print PM <<"END" if ! $opt_X;  # use DynaLoader, unless XS was disabled
  386. require DynaLoader;
  387. END
  388.  
  389. # require autoloader if XS is disabled.
  390. # if XS is enabled, require autoloader unless autoloading is disabled.
  391. if( ($opt_X && (! $opt_A)) || (!$opt_X) ) {
  392.     print PM <<"END";
  393. require AutoLoader;
  394. END
  395. }
  396.  
  397. if( $opt_X || ($opt_c && ! $opt_A) ){
  398.     # we won't have our own AUTOLOAD(), so we'll inherit it.
  399.     if( ! $opt_X ) { # use DynaLoader, unless XS was disabled
  400.         print PM <<"END";
  401.  
  402. \@ISA = qw(Exporter AutoLoader DynaLoader);
  403. END
  404.     }
  405.     else{
  406.         print PM <<"END";
  407.  
  408. \@ISA = qw(Exporter AutoLoader);
  409. END
  410.     }
  411. }
  412. else{
  413.     # 1) we have our own AUTOLOAD(), so don't need to inherit it.
  414.     # or
  415.     # 2) we don't want autoloading mentioned.
  416.     if( ! $opt_X ){ # use DynaLoader, unless XS was disabled
  417.         print PM <<"END";
  418.  
  419. \@ISA = qw(Exporter DynaLoader);
  420. END
  421.     }
  422.     else{
  423.         print PM <<"END";
  424.  
  425. \@ISA = qw(Exporter);
  426. END
  427.     }
  428. }
  429.  
  430. print PM<<"END";
  431. # Items to export into callers namespace by default. Note: do not export
  432. # names by default without a very good reason. Use EXPORT_OK instead.
  433. # Do not simply export all your public functions/methods/constants.
  434. \@EXPORT = qw(
  435.     @const_names
  436. );
  437. \$VERSION = '$TEMPLATE_VERSION';
  438.  
  439. END
  440.  
  441. print PM <<"END" unless $opt_c or $opt_X;
  442. sub AUTOLOAD {
  443.     # This AUTOLOAD is used to 'autoload' constants from the constant()
  444.     # XS function.  If a constant is not found then control is passed
  445.     # to the AUTOLOAD in AutoLoader.
  446.  
  447.     my \$constname;
  448.     (\$constname = \$AUTOLOAD) =~ s/.*:://;
  449.     my \$val = constant(\$constname, \@_ ? \$_[0] : 0);
  450.     if (\$! != 0) {
  451.     if (\$! =~ /Invalid/) {
  452.         \$AutoLoader::AUTOLOAD = \$AUTOLOAD;
  453.         goto &AutoLoader::AUTOLOAD;
  454.     }
  455.     else {
  456.         croak "Your vendor has not defined $module macro \$constname";
  457.     }
  458.     }
  459.     eval "sub \$AUTOLOAD { \$val }";
  460.     goto &\$AUTOLOAD;
  461. }
  462.  
  463. END
  464.  
  465. if( ! $opt_X ){ # print bootstrap, unless XS is disabled
  466.     print PM <<"END";
  467. bootstrap $module \$VERSION;
  468. END
  469. }
  470.  
  471. if( $opt_P ){ # if POD is disabled
  472.     $after = '__END__';
  473. }
  474. else {
  475.     $after = '=cut';
  476. }
  477.  
  478. print PM <<"END";
  479.  
  480. # Preloaded methods go here.
  481.  
  482. # Autoload methods go after $after, and are processed by the autosplit program.
  483.  
  484. 1;
  485. __END__
  486. END
  487.  
  488. $author = "A. U. Thor";
  489. $email = 'a.u.thor@a.galaxy.far.far.away';
  490.  
  491. my $const_doc = '';
  492. my $fdecl_doc = '';
  493. if (@const_names and not $opt_P) {
  494.   $const_doc = <<EOD;
  495. \n=head1 Exported constants
  496.  
  497.   @{[join "\n  ", @const_names]}
  498.  
  499. EOD
  500. }
  501. if (defined $fdecls and @$fdecls and not $opt_P) {
  502.   $fdecl_doc = <<EOD;
  503. \n=head1 Exported functions
  504.  
  505.   @{[join "\n  ", @$fdecls]}
  506.  
  507. EOD
  508. }
  509.  
  510. $pod = <<"END" unless $opt_P;
  511. ## Below is the stub of documentation for your module. You better edit it!
  512. #
  513. #=head1 NAME
  514. #
  515. #$module - Perl extension for blah blah blah
  516. #
  517. #=head1 SYNOPSIS
  518. #
  519. #  use $module;
  520. #  blah blah blah
  521. #
  522. #=head1 DESCRIPTION
  523. #
  524. #Stub documentation for $module was created by h2xs. It looks like the
  525. #author of the extension was negligent enough to leave the stub
  526. #unedited.
  527. #
  528. #Blah blah blah.
  529. #$const_doc$fdecl_doc
  530. #=head1 AUTHOR
  531. #
  532. #$author, $email
  533. #
  534. #=head1 SEE ALSO
  535. #
  536. #perl(1).
  537. #
  538. #=cut
  539. END
  540.  
  541. $pod =~ s/^\#//gm unless $opt_P;
  542. print PM $pod unless $opt_P;
  543.  
  544. close PM;
  545.  
  546.  
  547. if( ! $opt_X ){ # print XS, unless it is disabled
  548. warn "Writing $ext$modpname/$modfname.xs\n";
  549.  
  550. print XS <<"END";
  551. #ifdef __cplusplus
  552. extern "C" {
  553. #endif
  554. #include "EXTERN.h"
  555. #include "perl.h"
  556. #include "XSUB.h"
  557. #ifdef __cplusplus
  558. }
  559. #endif
  560.  
  561. END
  562. if( $path_h ){
  563.     my($h) = $path_h;
  564.     $h =~ s#^/usr/include/##;
  565.     if ($^O eq 'VMS') { $h =~ s#.*vms\]#sys/# or $h =~ s#.*[:>\]]##; }
  566. print XS <<"END";
  567. #include <$h>
  568.  
  569. END
  570. }
  571.  
  572. if( ! $opt_c ){
  573. print XS <<"END";
  574. static int
  575. not_here(s)
  576. char *s;
  577. {
  578.     croak("$module::%s not implemented on this architecture", s);
  579.     return -1;
  580. }
  581.  
  582. static double
  583. constant(name, arg)
  584. char *name;
  585. int arg;
  586. {
  587.     errno = 0;
  588.     switch (*name) {
  589. END
  590.  
  591. my(@AZ, @az, @under);
  592.  
  593. foreach(@const_names){
  594.     @AZ = 'A' .. 'Z' if !@AZ && /^[A-Z]/;
  595.     @az = 'a' .. 'z' if !@az && /^[a-z]/;
  596.     @under = '_'  if !@under && /^_/;
  597. }
  598.  
  599. foreach $letter (@AZ, @az, @under) {
  600.  
  601.     last if $letter eq 'a' && !@const_names;
  602.  
  603.     print XS "    case '$letter':\n";
  604.     my($name);
  605.     while (substr($const_names[0],0,1) eq $letter) {
  606.     $name = shift(@const_names);
  607.     $macro = $prefix{$name} ? "$opt_p$name" : $name;
  608.     next if $const_xsub{$macro};
  609.     print XS <<"END";
  610.     if (strEQ(name, "$name"))
  611. #ifdef $macro
  612.         return $macro;
  613. #else
  614.         goto not_there;
  615. #endif
  616. END
  617.     }
  618.     print XS <<"END";
  619.     break;
  620. END
  621. }
  622. print XS <<"END";
  623.     }
  624.     errno = EINVAL;
  625.     return 0;
  626.  
  627. not_there:
  628.     errno = ENOENT;
  629.     return 0;
  630. }
  631.  
  632. END
  633. }
  634.  
  635. $prefix = "PREFIX = $opt_p" if defined $opt_p;
  636. # Now switch from C to XS by issuing the first MODULE declaration:
  637. print XS <<"END";
  638.  
  639. MODULE = $module        PACKAGE = $module        $prefix
  640.  
  641. END
  642.  
  643. foreach (sort keys %const_xsub) {
  644.     print XS <<"END";
  645. char *
  646. $_()
  647.  
  648.     CODE:
  649. #ifdef $_
  650.     RETVAL = $_;
  651. #else
  652.     croak("Your vendor has not defined the $module macro $_");
  653. #endif
  654.  
  655.     OUTPUT:
  656.     RETVAL
  657.  
  658. END
  659. }
  660.  
  661. # If a constant() function was written then output a corresponding
  662. # XS declaration:
  663. print XS <<"END" unless $opt_c;
  664.  
  665. double
  666. constant(name,arg)
  667.     char *        name
  668.     int        arg
  669.  
  670. END
  671.  
  672. my %seen_decl;
  673.  
  674.  
  675. sub print_decl {
  676.   my $fh = shift;
  677.   my $decl = shift;
  678.   my ($type, $name, $args) = @$decl;
  679.   return if $seen_decl{$name}++; # Need to do the same for docs as well?
  680.  
  681.   my @argnames = map {$_->[1]} @$args;
  682.   my @argtypes = map { normalize_type( $_->[0] ) } @$args;
  683.   my @argarrays = map { $_->[4] || '' } @$args;
  684.   my $numargs = @$args;
  685.   if ($numargs and $argtypes[-1] eq '...') {
  686.     $numargs--;
  687.     $argnames[-1] = '...';
  688.   }
  689.   local $" = ', ';
  690.   $type = normalize_type($type);
  691.   
  692.   print $fh <<"EOP";
  693.  
  694. $type
  695. $name(@argnames)
  696. EOP
  697.  
  698.   for $arg (0 .. $numargs - 1) {
  699.     print $fh <<"EOP";
  700.     $argtypes[$arg]    $argnames[$arg]$argarrays[$arg]
  701. EOP
  702.   }
  703. }
  704.  
  705. # Should be called before any actual call to normalize_type().
  706. sub get_typemap {
  707.   # We do not want to read ./typemap by obvios reasons.
  708.   my @tm =  qw(../../../typemap ../../typemap ../typemap);
  709.   my $stdtypemap =  "$Config::Config{privlib}/ExtUtils/typemap";
  710.   unshift @tm, $stdtypemap;
  711.   my $proto_re = "[" . quotemeta('\$%&*@;') . "]" ;
  712.   my $image;
  713.   
  714.   foreach $typemap (@tm) {
  715.     next unless -e $typemap ;
  716.     # skip directories, binary files etc.
  717.     warn " Scanning $typemap\n";
  718.     warn("Warning: ignoring non-text typemap file '$typemap'\n"), next 
  719.       unless -T $typemap ;
  720.     open(TYPEMAP, $typemap) 
  721.       or warn ("Warning: could not open typemap file '$typemap': $!\n"), next;
  722.     my $mode = 'Typemap';
  723.     while (<TYPEMAP>) {
  724.       next if /^\s*\#/;
  725.       if (/^INPUT\s*$/)   { $mode = 'Input'; next; }
  726.       elsif (/^OUTPUT\s*$/)  { $mode = 'Output'; next; }
  727.       elsif (/^TYPEMAP\s*$/) { $mode = 'Typemap'; next; }
  728.       elsif ($mode eq 'Typemap') {
  729.     next if /^\s*($|\#)/ ;
  730.     if ( ($type, $image) = 
  731.          /^\s*(.*?\S)\s+(\S+)\s*($proto_re*)\s*$/o
  732.          # This may reference undefined functions:
  733.          and not ($image eq 'T_PACKED' and $typemap eq $stdtypemap)) {
  734.       normalize_type($type);
  735.     }
  736.       }
  737.     }
  738.     close(TYPEMAP) or die "Cannot close $typemap: $!";
  739.   }
  740.   %std_types = %types_seen;
  741.   %types_seen = ();
  742. }
  743.  
  744.  
  745. sub normalize_type {
  746.   my $ignore_mods = '(?:\b(?:__const__|static|inline|__inline__)\b\s*)*';
  747.   my $type = shift;
  748.   $type =~ s/$ignore_mods//go;
  749.   $type =~ s/([\]\[()])/ \1 /g;
  750.   $type =~ s/\s+/ /g;
  751.   $type =~ s/\s+$//;
  752.   $type =~ s/^\s+//;
  753.   $type =~ s/\b\*/ */g;
  754.   $type =~ s/\*\b/* /g;
  755.   $type =~ s/\*\s+(?=\*)/*/g;
  756.   $types_seen{$type}++ 
  757.     unless $type eq '...' or $type eq 'void' or $std_types{$type};
  758.   $type;
  759. }
  760.  
  761. if ($opt_x) {
  762.     for $decl (@$fdecls_parsed) { print_decl(\*XS, $decl) }
  763. }
  764.  
  765. close XS;
  766.  
  767. if (%types_seen) {
  768.   my $type;
  769.   warn "Writing $ext$modpname/typemap\n";
  770.   open TM, ">typemap" or die "Cannot open typemap file for write: $!";
  771.  
  772.   for $type (keys %types_seen) {
  773.     print TM $type, "\t" x (6 - int((length $type)/8)), "T_PTROBJ\n"
  774.   }
  775.  
  776.   close TM or die "Cannot close typemap file for write: $!";
  777. }
  778.  
  779. } # if( ! $opt_X )
  780.  
  781. warn "Writing $ext$modpname/Makefile.PL\n";
  782. open(PL, ">Makefile.PL") || die "Can't create $ext$modpname/Makefile.PL: $!\n";
  783.  
  784. print PL <<'END';
  785. use ExtUtils::MakeMaker;
  786. # See lib/ExtUtils/MakeMaker.pm for details of how to influence
  787. # the contents of the Makefile that is written.
  788. END
  789. print PL "WriteMakefile(\n";
  790. print PL "    'NAME'    => '$module',\n";
  791. print PL "    'VERSION_FROM' => '$modfname.pm', # finds \$VERSION\n"; 
  792. if( ! $opt_X ){ # print C stuff, unless XS is disabled
  793.   print PL "    'LIBS'    => ['$extralibs'],   # e.g., '-lm' \n";
  794.   print PL "    'DEFINE'    => '',     # e.g., '-DHAVE_SOMETHING' \n";
  795.   print PL "    'INC'    => '',     # e.g., '-I/usr/include/other' \n";
  796. }
  797. print PL ");\n";
  798. close(PL) || die "Can't close $ext$modpname/Makefile.PL: $!\n";
  799.  
  800. warn "Writing $ext$modpname/test.pl\n";
  801. open(EX, ">test.pl") || die "Can't create $ext$modpname/test.pl: $!\n";
  802. print EX <<'_END_';
  803. # Before `make install' is performed this script should be runnable with
  804. # `make test'. After `make install' it should work as `perl test.pl'
  805.  
  806. ######################### We start with some black magic to print on failure.
  807.  
  808. # Change 1..1 below to 1..last_test_to_print .
  809. # (It may become useful if the test is moved to ./t subdirectory.)
  810.  
  811. BEGIN { $| = 1; print "1..1\n"; }
  812. END {print "not ok 1\n" unless $loaded;}
  813. _END_
  814. print EX <<_END_;
  815. use $module;
  816. _END_
  817. print EX <<'_END_';
  818. $loaded = 1;
  819. print "ok 1\n";
  820.  
  821. ######################### End of black magic.
  822.  
  823. # Insert your test code below (better if it prints "ok 13"
  824. # (correspondingly "not ok 13") depending on the success of chunk 13
  825. # of the test code):
  826.  
  827. _END_
  828. close(EX) || die "Can't close $ext$modpname/test.pl: $!\n";
  829.  
  830. warn "Writing $ext$modpname/Changes\n";
  831. open(EX, ">Changes") || die "Can't create $ext$modpname/Changes: $!\n";
  832. print EX "Revision history for Perl extension $module.\n\n";
  833. print EX "$TEMPLATE_VERSION  ",scalar localtime,"\n";
  834. print EX "\t- original version; created by h2xs $H2XS_VERSION\n\n";
  835. close(EX) || die "Can't close $ext$modpname/Changes: $!\n";
  836.  
  837. warn "Writing $ext$modpname/MANIFEST\n";
  838. open(MANI,'>MANIFEST') or die "Can't create MANIFEST: $!";
  839. @files = <*>;
  840. if (!@files) {
  841.   eval {opendir(D,'.');};
  842.   unless ($@) { @files = readdir(D); closedir(D); }
  843. }
  844. if (!@files) { @files = map {chomp && $_} `ls`; }
  845. if ($^O eq 'VMS') {
  846.   foreach (@files) {
  847.     # Clip trailing '.' for portability -- non-VMS OSs don't expect it
  848.     s%\.$%%;
  849.     # Fix up for case-sensitive file systems
  850.     s/$modfname/$modfname/i && next;
  851.     $_ = "\U$_" if $_ eq 'manifest' or $_ eq 'changes';
  852.     $_ = 'Makefile.PL' if $_ eq 'makefile.pl';
  853.   }
  854. }
  855. print MANI join("\n",@files), "\n";
  856. close MANI;
  857.