home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / PERL4036.ZIP / c2ph.SH < prev    next >
Text File  |  1993-02-08  |  27KB  |  1,103 lines

  1. case $CONFIG in
  2. '')
  3.     if test ! -f config.sh; then
  4.     ln ../config.sh . || \
  5.     ln ../../config.sh . || \
  6.     ln ../../../config.sh . || \
  7.     (echo "Can't find config.sh."; exit 1)
  8.     fi
  9.     . config.sh
  10.     ;;
  11. esac
  12. : This forces SH files to create target in same directory as SH file.
  13. : This is so that make depend always knows where to find SH derivatives.
  14. case "$0" in
  15. */*) cd `expr X$0 : 'X\(.*\)/'` ;;
  16. esac
  17. echo "Extracting c2ph (with variable substitutions)"
  18. : This section of the file will have variable substitutions done on it.
  19. : Move anything that needs config subs from !NO!SUBS! section to !GROK!THIS!.
  20. : Protect any dollar signs and backticks that you do not want interpreted
  21. : by putting a backslash in front.  You may delete these comments.
  22. rm -f c2ph
  23. $spitshell >c2ph <<!GROK!THIS!
  24. #!$bin/perl
  25. #
  26. !GROK!THIS!
  27.  
  28. : In the following dollars and backticks do not need the extra backslash.
  29. $spitshell >>c2ph <<'!NO!SUBS!'
  30. #
  31. #   c2ph (aka pstruct)
  32. #   Tom Christiansen, <tchrist@convex.com>
  33. #   
  34. #   As pstruct, dump C structures as generated from 'cc -g -S' stabs.
  35. #   As c2ph, do this PLUS generate perl code for getting at the structures.
  36. #
  37. #   See the usage message for more.  If this isn't enough, read the code.
  38. #
  39.  
  40. $RCSID = '$RCSfile: c2ph.SH,v $$Revision: 4.0.1.2 $$Date: 92/06/08 11:56:08 $';
  41.  
  42.  
  43. ######################################################################
  44.  
  45. # some handy data definitions.   many of these can be reset later.
  46.  
  47. $bitorder = 'b';  # ascending; set to B for descending bit fields
  48.  
  49. %intrinsics = 
  50. %template = (
  51.     'char',             'c',
  52.     'unsigned char',         'C',
  53.     'short',            's',
  54.     'short int',        's',
  55.     'unsigned short',        'S',
  56.     'unsigned short int',    'S',
  57.     'short unsigned int',    'S',
  58.     'int',            'i',
  59.     'unsigned int',        'I',
  60.     'long',            'l',
  61.     'long int',            'l',
  62.     'unsigned long',        'L',
  63.     'unsigned long',        'L',
  64.     'long unsigned int',    'L',
  65.     'unsigned long int',    'L',
  66.     'long long',        'q',
  67.     'long long int',        'q',
  68.     'unsigned long long',    'Q',
  69.     'unsigned long long int',    'Q',
  70.     'float',            'f',
  71.     'double',            'd',
  72.     'pointer',            'p',
  73.     'null',            'x',
  74.     'neganull',            'X',
  75.     'bit',            $bitorder,
  76. ); 
  77.  
  78. &buildscrunchlist;
  79. delete $intrinsics{'neganull'};
  80. delete $intrinsics{'bit'};
  81. delete $intrinsics{'null'};
  82.  
  83. # use -s to recompute sizes
  84. %sizeof = (
  85.     'char',             '1',
  86.     'unsigned char',         '1',
  87.     'short',            '2',
  88.     'short int',        '2',
  89.     'unsigned short',        '2',
  90.     'unsigned short int',    '2',
  91.     'short unsigned int',    '2',
  92.     'int',            '4',
  93.     'unsigned int',        '4',
  94.     'long',            '4',
  95.     'long int',            '4',
  96.     'unsigned long',        '4',
  97.     'unsigned long int',    '4',
  98.     'long unsigned int',    '4',
  99.     'long long',        '8',
  100.     'long long int',        '8',
  101.     'unsigned long long',    '8',
  102.     'unsigned long long int',    '8',
  103.     'float',            '4',
  104.     'double',            '8',
  105.     'pointer',            '4',
  106. );
  107.  
  108. ($type_width, $member_width, $offset_width, $size_width) = (20, 20, 6, 5);
  109.  
  110. ($offset_fmt, $size_fmt) = ('d', 'd');
  111.  
  112. $indent = 2;
  113.  
  114. $CC = 'cc';
  115. $CFLAGS = '-g -S';
  116. $DEFINES = '';
  117.  
  118. $perl++ if $0 =~ m#/?c2ph$#;
  119.  
  120. require 'getopts.pl';
  121.  
  122. eval '$'.$1.'$2;' while $ARGV[0] =~ /^([A-Za-z_]+=)(.*)/ && shift;
  123.  
  124. &Getopts('aixdpvtnws:') || &usage(0);
  125.  
  126. $opt_d && $debug++;
  127. $opt_t && $trace++;
  128. $opt_p && $perl++;
  129. $opt_v && $verbose++;
  130. $opt_n && ($perl = 0);
  131.  
  132. if ($opt_w) {
  133.     ($type_width, $member_width, $offset_width) = (45, 35, 8);
  134. if ($opt_x) {
  135.     ($offset_fmt, $offset_width, $size_fmt, $size_width) = ( 'x', '08', 'x', 04 );
  136. }
  137.  
  138. eval '$'.$1.'$2;' while $ARGV[0] =~ /^([A-Za-z_]+=)(.*)/ && shift;
  139.  
  140. sub PLUMBER {
  141.     select(STDERR);
  142.     print "oops, apperent pager foulup\n";
  143.     $isatty++;
  144.     &usage(1);
  145.  
  146. sub usage {
  147.     local($oops) = @_;
  148.     unless (-t STDOUT) {
  149.     select(STDERR);
  150.     } elsif (!$oops) {
  151.     $isatty++;
  152.     $| = 1;
  153.     print "hit <RETURN> for further explanation: ";
  154.     <STDIN>;
  155.     open (PIPE, "|". ($ENV{PAGER} || 'more'));
  156.     $SIG{PIPE} = PLUMBER;
  157.     select(PIPE);
  158.     } 
  159.  
  160.     print "usage: $0 [-dpnP] [var=val] [files ...]\n";
  161.  
  162.     exit unless $isatty;
  163.  
  164.     print <<EOF;
  165.  
  166. Options:
  167.  
  168. -w    wide; short for: type_width=45 member_width=35 offset_width=8
  169. -x    hex; short for:  offset_fmt=x offset_width=08 size_fmt=x size_width=04
  170.  
  171. -n      do not generate perl code  (default when invoked as pstruct)
  172. -p      generate perl code         (default when invoked as c2ph)
  173. -v    generate perl code, with C decls as comments
  174.  
  175. -i    do NOT recompute sizes for intrinsic datatypes
  176. -a    dump information on intrinsics also
  177.  
  178. -t     trace execution
  179. -d    spew reams of debugging output
  180.  
  181. -slist  give comma-separated list a structures to dump
  182.  
  183.  
  184. Var Name        Default Value    Meaning
  185.  
  186. EOF
  187.  
  188.     &defvar('CC', 'which_compiler to call');
  189.     &defvar('CFLAGS', 'how to generate *.s files with stabs');
  190.     &defvar('DEFINES','any extra cflags or cpp defines, like -I, -D, -U');
  191.  
  192.     print "\n";
  193.  
  194.     &defvar('type_width', 'width of type field   (column 1)');
  195.     &defvar('member_width', 'width of member field (column 2)');
  196.     &defvar('offset_width', 'width of offset field (column 3)');
  197.     &defvar('size_width', 'width of size field   (column 4)');
  198.  
  199.     print "\n";
  200.  
  201.     &defvar('offset_fmt', 'sprintf format type for offset');
  202.     &defvar('size_fmt', 'sprintf format type for size');
  203.  
  204.     print "\n";
  205.  
  206.     &defvar('indent', 'how far to indent each nesting level');
  207.  
  208.    print <<'EOF';
  209.  
  210.     If any *.[ch] files are given, these will be catted together into
  211.     a temporary *.c file and sent through:
  212.         $CC $CFLAGS $DEFINES 
  213.     and the resulting *.s groped for stab information.  If no files are
  214.     supplied, then stdin is read directly with the assumption that it
  215.     contains stab information.  All other liens will be ignored.  At
  216.     most one *.s file should be supplied.
  217.  
  218. EOF
  219.     close PIPE;
  220.     exit 1;
  221.  
  222. sub defvar {
  223.     local($var, $msg) = @_;
  224.     printf "%-16s%-15s  %s\n", $var, eval "\$$var", $msg;
  225.  
  226. $recurse = 1;
  227.  
  228. if (@ARGV) {
  229.     if (grep(!/\.[csh]$/,@ARGV)) {
  230.     warn "Only *.[csh] files expected!\n";
  231.     &usage;
  232.     } 
  233.     elsif (grep(/\.s$/,@ARGV)) {
  234.     if (@ARGV > 1) { 
  235.         warn "Only one *.s file allowed!\n";
  236.         &usage;
  237.     }
  238.     } 
  239.     elsif (@ARGV == 1 && $ARGV[0] =~ /\.c$/) {
  240.     local($dir, $file) = $ARGV[0] =~ m#(.*/)?(.*)$#;
  241.     $chdir = "cd $dir; " if $dir;
  242.     &system("$chdir$CC $CFLAGS $DEFINES $file") && exit 1;
  243.     $ARGV[0] =~ s/\.c$/.s/;
  244.     } 
  245.     else {
  246.     $TMP = "/tmp/c2ph.$$.c";
  247.     &system("cat @ARGV > $TMP") && exit 1;
  248.     &system("cd /tmp; $CC $CFLAGS $DEFINES $TMP") && exit 1;
  249.     unlink $TMP;
  250.     $TMP =~ s/\.c$/.s/;
  251.     @ARGV = ($TMP);
  252.     } 
  253. }
  254.  
  255. if ($opt_s) {
  256.     for (split(/[\s,]+/, $opt_s)) {
  257.     $interested{$_}++;
  258.     } 
  259.  
  260.  
  261. $| = 1 if $debug;
  262.  
  263. main: {
  264.  
  265.     if ($trace) {
  266.     if (-t && !@ARGV) { 
  267.         print STDERR "reading from your keyboard: ";
  268.     } else {
  269.         print STDERR "reading from " . (@ARGV ? "@ARGV" : "<STDIN>").": ";
  270.     }
  271.     }
  272.  
  273. STAB: while (<>) {
  274.     if ($trace && !($. % 10)) {
  275.         $lineno = $..'';
  276.         print STDERR $lineno, "\b" x length($lineno);
  277.     } 
  278.     next unless /^\s*\.stabs\s+/;
  279.     $line = $_;
  280.     s/^\s*\.stabs\s+//; 
  281.     &stab; 
  282.     }
  283.     print STDERR "$.\n" if $trace;
  284.     unlink $TMP if $TMP;
  285.  
  286.     &compute_intrinsics if $perl && !$opt_i;
  287.  
  288.     print STDERR "resolving types\n" if $trace;
  289.  
  290.     &resolve_types;
  291.     &adjust_start_addrs;
  292.  
  293.     $sum = 2 + $type_width + $member_width;
  294.     $pmask1 = "%-${type_width}s %-${member_width}s"; 
  295.     $pmask2 = "%-${sum}s %${offset_width}${offset_fmt}%s %${size_width}${size_fmt}%s";
  296.  
  297.     if ($perl) {
  298.     # resolve template -- should be in stab define order, but even this isn't enough.
  299.     print STDERR "\nbuilding type templates: " if $trace;
  300.     for $i (reverse 0..$#type) {
  301.         next unless defined($name = $type[$i]);
  302.         next unless defined $struct{$name};
  303.         $build_recursed = 0;
  304.         &build_template($name) unless defined $template{&psou($name)} ||
  305.                     $opt_s && !$interested{$name};
  306.     } 
  307.     print STDERR "\n\n" if $trace;
  308.     }
  309.  
  310.     print STDERR "dumping structs: " if $trace;
  311.  
  312.  
  313.     foreach $name (sort keys %struct) {
  314.     next if $opt_s && !$interested{$name};
  315.     print STDERR "$name " if $trace;
  316.  
  317.     undef @sizeof;
  318.     undef @typedef;
  319.     undef @offsetof;
  320.     undef @indices;
  321.     undef @typeof;
  322.  
  323.     $mname = &munge($name);
  324.  
  325.     $fname = &psou($name);
  326.  
  327.     print "# " if $perl && $verbose;
  328.     $pcode = '';
  329.     print "$fname {\n" if !$perl || $verbose; 
  330.     $template{$fname} = &scrunch($template{$fname}) if $perl;
  331.     &pstruct($name,$name,0); 
  332.     print "# " if $perl && $verbose;
  333.     print "}\n" if !$perl || $verbose; 
  334.     print "\n" if $perl && $verbose;
  335.  
  336.     if ($perl) {
  337.         print "$pcode";
  338.  
  339.         printf("\nsub %-32s { %4d; }\n\n", "${mname}'struct", $countof{$name});
  340.  
  341.         print <<EOF;
  342. sub ${mname}'typedef { 
  343.     local(\$${mname}'index) = shift;
  344.     defined \$${mname}'index 
  345.     ? \$${mname}'typedef[\$${mname}'index] 
  346.     : \$${mname}'typedef;
  347. }
  348. EOF
  349.  
  350.         print <<EOF;
  351. sub ${mname}'sizeof { 
  352.     local(\$${mname}'index) = shift;
  353.     defined \$${mname}'index 
  354.     ? \$${mname}'sizeof[\$${mname}'index] 
  355.     : \$${mname}'sizeof;
  356. }
  357. EOF
  358.  
  359.         print <<EOF;
  360. sub ${mname}'offsetof { 
  361.     local(\$${mname}'index) = shift;
  362.     defined \$${mname}index 
  363.     ? \$${mname}'offsetof[\$${mname}'index] 
  364.     : \$${mname}'sizeof;
  365. }
  366. EOF
  367.  
  368.         print <<EOF;
  369. sub ${mname}'typeof { 
  370.     local(\$${mname}'index) = shift;
  371.     defined \$${mname}index 
  372.     ? \$${mname}'typeof[\$${mname}'index] 
  373.     : '$name';
  374. }
  375. EOF
  376.     
  377.  
  378.         print "\$${mname}'typedef = '" . &scrunch($template{$fname}) 
  379.         . "';\n";
  380.  
  381.         print "\$${mname}'sizeof = $sizeof{$name};\n\n";
  382.  
  383.  
  384.         print "\@${mname}'indices = (", &squishseq(@indices), ");\n";
  385.  
  386.         print "\n";
  387.  
  388.         print "\@${mname}'typedef[\@${mname}'indices] = (",
  389.             join("\n\t", '', @typedef), "\n    );\n\n";
  390.         print "\@${mname}'sizeof[\@${mname}'indices] = (",
  391.             join("\n\t", '', @sizeof), "\n    );\n\n";
  392.         print "\@${mname}'offsetof[\@${mname}'indices] = (",
  393.             join("\n\t", '', @offsetof), "\n    );\n\n";
  394.         print "\@${mname}'typeof[\@${mname}'indices] = (",
  395.             join("\n\t", '', @typeof), "\n    );\n\n";
  396.  
  397.         $template_printed{$fname}++;
  398.         $size_printed{$fname}++;
  399.     } 
  400.     print "\n";
  401.     }
  402.  
  403.     print STDERR "\n" if $trace;
  404.  
  405.     unless ($perl && $opt_a) { 
  406.     print "\n1;\n";
  407.     exit;
  408.     }
  409.  
  410.  
  411.  
  412.     foreach $name (sort bysizevalue keys %intrinsics) {
  413.     next if $size_printed{$name};
  414.     print '$',&munge($name),"'sizeof = ", $sizeof{$name}, ";\n";
  415.     }
  416.  
  417.     print "\n";
  418.  
  419.     sub bysizevalue { $sizeof{$a} <=> $sizeof{$b}; }
  420.  
  421.  
  422.     foreach $name (sort keys %intrinsics) {
  423.     print '$',&munge($name),"'typedef = '", $template{$name}, "';\n";
  424.     }
  425.  
  426.     print "\n1;\n";
  427.     
  428.     exit;
  429. }
  430.  
  431. ########################################################################################
  432.  
  433.  
  434. sub stab {
  435.     next unless /:[\$\w]+(\(\d+,\d+\))?=[\*\$\w]+/;  # (\d+,\d+) is for sun
  436.     s/"//                         || next;
  437.     s/",([x\d]+),([x\d]+),([x\d]+),.*//         || next;
  438.  
  439.     next if /^\s*$/;
  440.  
  441.     $size = $3 if $3;
  442.  
  443.  
  444.     $line = $_;
  445.  
  446.     if (($name, $pdecl) = /^([\$ \w]+):[tT]((\d+)(=[rufs*](\d+))+)$/) {
  447.     print "$name is a typedef for some funky pointers: $pdecl\n" if $debug;
  448.     &pdecl($pdecl);
  449.     next;
  450.     }
  451.  
  452.  
  453.  
  454.     if (/(([ \w]+):t(\d+|\(\d+,\d+\)))=r?(\d+|\(\d+,\d+\))(;\d+;\d+;)?/) {  
  455.     local($ident) = $2;
  456.     push(@intrinsics, $ident);
  457.     $typeno = &typeno($3);
  458.     $type[$typeno] = $ident;
  459.     print STDERR "intrinsic $ident in new type $typeno\n" if $debug; 
  460.     next;
  461.     }
  462.  
  463.     if (($name, $typeordef, $typeno, $extra, $struct, $_) 
  464.     = /^([\$ \w]+):([ustT])(\d+|\(\d+,\d+\))(=[rufs*](\d+))?(.*)$/) 
  465.     {
  466.     $typeno = &typeno($typeno);  # sun foolery
  467.     } 
  468.     elsif (/^[\$\w]+:/) {
  469.     next; # variable
  470.     }
  471.     else { 
  472.     warn "can't grok stab: <$_> in: $line " if $_;
  473.     next;
  474.     } 
  475.  
  476.     #warn "got size $size for $name\n";
  477.     $sizeof{$name} = $size if $size;
  478.  
  479.     s/;[-\d]*;[-\d]*;$//;  # we don't care about ranges
  480.  
  481.     $typenos{$name} = $typeno;
  482.  
  483.     unless (defined $type[$typeno]) {
  484.     &panic("type 0??") unless $typeno;
  485.     $type[$typeno] = $name unless defined $type[$typeno];
  486.     printf "new type $typeno is $name" if $debug;
  487.     if ($extra =~ /\*/ && defined $type[$struct]) {
  488.         print ", a typedef for a pointer to " , $type[$struct] if $debug;
  489.     }
  490.     } else {
  491.     printf "%s is type %d", $name, $typeno if $debug;
  492.     print ", a typedef for " , $type[$typeno] if $debug;
  493.     } 
  494.     print "\n" if $debug;
  495.     #next unless $extra =~ /[su*]/;
  496.  
  497.     #$type[$struct] = $name;
  498.  
  499.     if ($extra =~ /[us*]/) {
  500.     &sou($name, $extra);
  501.     $_ = &sdecl($name, $_, 0);
  502.     }
  503.     elsif (/^=ar/) {
  504.     print "it's a bare array typedef -- that's pretty sick\n" if $debug;
  505.     $_ = "$typeno$_";
  506.     $scripts = '';
  507.     $_ = &adecl($_,1);
  508.  
  509.     }
  510.     elsif (s/((\w+):t(\d+|\(\d+,\d+\)))?=r?(;\d+;\d+;)?//) {  # the ?'s are for gcc
  511.     push(@intrinsics, $2);
  512.     $typeno = &typeno($3);
  513.     $type[$typeno] = $2;
  514.     print STDERR "intrinsic $2 in new type $typeno\n" if $debug; 
  515.     }
  516.     elsif (s/^=e//) { # blessed by thy compiler; mine won't do this
  517.     &edecl;
  518.     } 
  519.     else {
  520.     warn "Funny remainder for $name on line $_ left in $line " if $_;
  521.     } 
  522. }
  523.  
  524. sub typeno {  # sun thinks types are (0,27) instead of just 27
  525.     local($_) = @_;
  526.     s/\(\d+,(\d+)\)/$1/;
  527.     $_;
  528.  
  529. sub pstruct {
  530.     local($what,$prefix,$base) = @_; 
  531.     local($field, $fieldname, $typeno, $count, $offset, $entry); 
  532.     local($fieldtype);
  533.     local($type, $tname); 
  534.     local($mytype, $mycount, $entry2);
  535.     local($struct_count) = 0;
  536.     local($pad, $revpad, $length, $prepad, $lastoffset, $lastlength, $fmt);
  537.     local($bits,$bytes);
  538.     local($template);
  539.  
  540.  
  541.     local($mname) = &munge($name);
  542.  
  543.     sub munge { 
  544.     local($_) = @_;
  545.     s/[\s\$\.]/_/g;
  546.     $_;
  547.     }
  548.  
  549.     local($sname) = &psou($what);
  550.  
  551.     $nesting++;
  552.  
  553.     for $field (split(/;/, $struct{$what})) {
  554.     $pad = $prepad = 0;
  555.     $entry = ''; 
  556.     ($fieldname, $typeno, $count, $offset, $length) = split(/,/, $field); 
  557.  
  558.     $type = $type[$typeno];
  559.  
  560.     $type =~ /([^[]*)(\[.*\])?/;
  561.     $mytype = $1;
  562.     $count .= $2;
  563.     $fieldtype = &psou($mytype);
  564.  
  565.     local($fname) = &psou($name);
  566.  
  567.     if ($build_templates) {
  568.  
  569.         $pad = ($offset - ($lastoffset + $lastlength))/8 
  570.         if defined $lastoffset;
  571.  
  572.         if (! $finished_template{$sname}) {
  573.         if ($isaunion{$what}) {
  574.             $template{$sname} .= 'X' x $revpad . ' '    if $revpad;
  575.         } else {
  576.             $template{$sname} .= 'x' x $pad    . ' '    if $pad;
  577.         }
  578.         }
  579.  
  580.         $template = &fetch_template($type) x 
  581.                 ($count ? &scripts2count($count) : 1);
  582.  
  583.         if (! $finished_template{$sname}) {
  584.         $template{$sname} .= $template;
  585.         }
  586.  
  587.         $revpad = $length/8 if $isaunion{$what};
  588.  
  589.         ($lastoffset, $lastlength) = ($offset, $length);
  590.  
  591.     } else { 
  592.         print '# ' if $perl && $verbose;
  593.         $entry = sprintf($pmask1,
  594.             ' ' x ($nesting * $indent) . $fieldtype,
  595.             "$prefix.$fieldname" . $count); 
  596.  
  597.         $entry =~ s/(\*+)( )/$2$1/; 
  598.  
  599.         printf $pmask2,
  600.             $entry,
  601.             ($base+$offset)/8,
  602.             ($bits = ($base+$offset)%8) ? ".$bits" : "  ",
  603.             $length/8,
  604.             ($bits = $length % 8) ? ".$bits": ""
  605.             if !$perl || $verbose;
  606.  
  607.  
  608.         if ($perl && $nesting == 1) {
  609.         $template = &scrunch(&fetch_template($type) x 
  610.                 ($count ? &scripts2count($count) : 1));
  611.         push(@sizeof, int($length/8) .",\t# $fieldname");
  612.         push(@offsetof, int($offset/8) .",\t# $fieldname");
  613.         push(@typedef, "'$template', \t# $fieldname");
  614.         $type =~ s/(struct|union) //;
  615.         push(@typeof, "'$type" . ($count ? $count : '') .
  616.             "',\t# $fieldname");
  617.         }
  618.  
  619.         print '  ', ' ' x $indent x $nesting, $template
  620.                 if $perl && $verbose;
  621.  
  622.         print "\n" if !$perl || $verbose;
  623.  
  624.     }    
  625.     if ($perl) {
  626.         local($mycount) = defined $struct{$mytype} ? $countof{$mytype} : 1;
  627.         $mycount *= &scripts2count($count) if $count;
  628.         if ($nesting==1 && !$build_templates) {
  629.         $pcode .= sprintf("sub %-32s { %4d; }\n", 
  630.             "${mname}'${fieldname}", $struct_count);
  631.         push(@indices, $struct_count);
  632.         }
  633.         $struct_count += $mycount;
  634.     } 
  635.  
  636.  
  637.     &pstruct($type, "$prefix.$fieldname", $base+$offset) 
  638.         if $recurse && defined $struct{$type}; 
  639.     }
  640.  
  641.     $countof{$what} = $struct_count unless defined $countof{$whati};
  642.  
  643.     $template{$sname} .= '$' if $build_templates;
  644.     $finished_template{$sname}++;
  645.  
  646.     if ($build_templates && !defined $sizeof{$name}) {
  647.     local($fmt) = &scrunch($template{$sname});
  648.     print STDERR "no size for $name, punting with $fmt..." if $debug;
  649.     eval '$sizeof{$name} = length(pack($fmt, ()))';
  650.     if ($@) {
  651.         chop $@;
  652.         warn "couldn't get size for \$name: $@";
  653.     } else {
  654.         print STDERR $sizeof{$name}, "\n" if $debUg;
  655.     }
  656.     } 
  657.  
  658.     --$nesting;
  659. }
  660.  
  661.  
  662. sub psize {
  663.     local($me) = @_; 
  664.     local($amstruct) = $struct{$me} ?  'struct ' : '';
  665.  
  666.     print '$sizeof{\'', $amstruct, $me, '\'} = '; 
  667.     printf "%d;\n", $sizeof{$me}; 
  668. }
  669.  
  670. sub pdecl {
  671.     local($pdecl) = @_;
  672.     local(@pdecls);
  673.     local($tname);
  674.  
  675.     warn "pdecl: $pdecl\n" if $debug;
  676.  
  677.     $pdecl =~ s/\(\d+,(\d+)\)/$1/g;
  678.     $pdecl =~ s/\*//g; 
  679.     @pdecls = split(/=/, $pdecl); 
  680.     $typeno = $pdecls[0];
  681.     $tname = pop @pdecls;
  682.  
  683.     if ($tname =~ s/^f//) { $tname = "$tname&"; } 
  684.     #else { $tname = "$tname*"; } 
  685.  
  686.     for (reverse @pdecls) {
  687.     $tname  .= s/^f// ? "&" : "*"; 
  688.     #$tname =~ s/^f(.*)/$1&/;
  689.     print "type[$_] is $tname\n" if $debug;
  690.     $type[$_] = $tname unless defined $type[$_];
  691.     } 
  692. }
  693.  
  694.  
  695.  
  696. sub adecl {
  697.     ($arraytype, $unknown, $lower, $upper) = ();
  698.     #local($typeno);
  699.     # global $typeno, @type
  700.     local($_, $typedef) = @_;
  701.  
  702.     while (s/^((\d+)=)?ar(\d+);//) {
  703.     ($arraytype, $unknown) = ($2, $3); 
  704.     if (s/^(\d+);(\d+);//) {
  705.         ($lower, $upper) = ($1, $2); 
  706.         $scripts .= '[' .  ($upper+1) . ']'; 
  707.     } else {
  708.         warn "can't find array bounds: $_"; 
  709.     } 
  710.     }
  711.     if (s/^([\d*f=]*),(\d+),(\d+);//) {
  712.     ($start, $length) = ($2, $3); 
  713.     local($whatis) = $1;
  714.     if ($whatis =~ /^(\d+)=/) {
  715.         $typeno = $1;
  716.         &pdecl($whatis);
  717.     } else {
  718.         $typeno = $whatis;
  719.     }
  720.     } elsif (s/^(\d+)(=[*suf]\d*)//) {
  721.     local($whatis) = $2; 
  722.  
  723.     if ($whatis =~ /[f*]/) {
  724.         &pdecl($whatis); 
  725.     } elsif ($whatis =~ /[su]/) {  # 
  726.         print "$prefix.$fieldname is an array$scripts anon structs; disgusting\n" 
  727.         if $debug;
  728.         #$type[$typeno] = $name unless defined $type[$typeno];
  729.         ##printf "new type $typeno is $name" if $debug;
  730.         $typeno = $1;
  731.         $type[$typeno] = "$prefix.$fieldname";
  732.         local($name) = $type[$typeno];
  733.         &sou($name, $whatis);
  734.         $_ = &sdecl($name, $_, $start+$offset);
  735.         1;
  736.         $start = $start{$name};
  737.         $offset = $sizeof{$name};
  738.         $length = $offset;
  739.     } else {
  740.         warn "what's this? $whatis in $line ";
  741.     } 
  742.     } elsif (/^\d+$/) {
  743.     $typeno = $_;
  744.     } else {
  745.     warn "bad array stab: $_ in $line ";
  746.     next STAB;
  747.     } 
  748.     #local($wasdef) = defined($type[$typeno]) && $debug;
  749.     #if ($typedef) { 
  750.     #print "redefining $type[$typeno] to " if $wasdef;
  751.     #$type[$typeno] = "$whatis$scripts"; # unless defined $type[$typeno];
  752.     #print "$type[$typeno]\n" if $wasdef;
  753.     #} else {
  754.     #$type[$arraytype] = $type[$typeno] unless defined $type[$arraytype];
  755.     #}
  756.     $type[$arraytype] = "$type[$typeno]$scripts" if defined $type[$typeno];
  757.     print "type[$arraytype] is $type[$arraytype]\n" if $debug;
  758.     print "$prefix.$fieldname is an array of $type[$arraytype]\n" if $debug;
  759.     $_;
  760. }
  761.  
  762.  
  763.  
  764. sub sdecl {
  765.     local($prefix, $_, $offset) = @_;
  766.  
  767.     local($fieldname, $scripts, $type, $arraytype, $unknown,
  768.     $whatis, $pdecl, $upper,$lower, $start,$length) = ();
  769.     local($typeno,$sou);
  770.  
  771.  
  772. SFIELD:
  773.     while (/^([^;]+);/) {
  774.     $scripts = '';
  775.     warn "sdecl $_\n" if $debug;
  776.     if (s/^([\$\w]+)://) { 
  777.         $fieldname = $1;
  778.     } elsif (s/(\d+)=([us])(\d+|\(\d+,\d+\))//) { # 
  779.         $typeno = &typeno($1);
  780.         $type[$typeno] = "$prefix.$fieldname";
  781.         local($name) = "$prefix.$fieldname";
  782.         &sou($name,$2);
  783.         $_ = &sdecl("$prefix.$fieldname", $_, $start+$offset);
  784.         $start = $start{$name};
  785.         $offset += $sizeof{$name};
  786.         #print "done with anon, start is $start, offset is $offset\n";
  787.         #next SFIELD;
  788.     } else  {
  789.         warn "weird field $_ of $line" if $debug;
  790.         next STAB;
  791.         #$fieldname = &gensym;
  792.         #$_ = &sdecl("$prefix.$fieldname", $_, $start+$offset);
  793.     }
  794.  
  795.     if (/^\d+=ar/) {
  796.         $_ = &adecl($_);
  797.     }
  798.     elsif (s/^(\d+|\(\d+,\d+\))?,(\d+),(\d+);//) {
  799.         ($start, $length) =  ($2, $3); 
  800.         &panic("no length?") unless $length;
  801.         $typeno = &typeno($1) if $1;
  802.     }
  803.     elsif (s/^((\d+|\(\d+,\d+\))(=[*f](\d+|\(\d+,\d+\)))+),(\d+),(\d+);//) {
  804.         ($pdecl, $start, $length) =  ($1,$5,$6); 
  805.         &pdecl($pdecl); 
  806.     }
  807.     elsif (s/(\d+)=([us])(\d+|\(\d+,\d+\))//) { # the dratted anon struct
  808.         ($typeno, $sou) = ($1, $2);
  809.         $typeno = &typeno($typeno);
  810.         if (defined($type[$typeno])) {
  811.         warn "now how did we get type $1 in $fieldname of $line?";
  812.         } else {
  813.         print "anon type $typeno is $prefix.$fieldname\n" if $debug;
  814.         $type[$typeno] = "$prefix.$fieldname" unless defined $type[$typeno];
  815.         };
  816.         local($name) = "$prefix.$fieldname";
  817.         &sou($name,$sou);
  818.         print "anon ".($isastruct{$name}) ? "struct":"union"." for $prefix.$fieldname\n" if $debug;
  819.         $type[$typeno] = "$prefix.$fieldname";
  820.         $_ = &sdecl("$prefix.$fieldname", $_, $start+$offset); 
  821.         $start = $start{$name};
  822.         $length = $sizeof{$name};
  823.     }
  824.     else {
  825.         warn "can't grok stab for $name ($_) in line $line "; 
  826.         next STAB; 
  827.     }
  828.  
  829.     &panic("no length for $prefix.$fieldname") unless $length;
  830.     $struct{$name} .= join(',', $fieldname, $typeno, $scripts, $start, $length) . ';';
  831.     }
  832.     if (s/;\d*,(\d+),(\d+);//) {
  833.     local($start, $size) = ($1, $2); 
  834.     $sizeof{$prefix} = $size;
  835.     print "start of $prefix is $start, size of $sizeof{$prefix}\n" if $debug; 
  836.     $start{$prefix} = $start; 
  837.     } 
  838.     $_;
  839. }
  840.  
  841. sub edecl {
  842.     s/;$//;
  843.     $enum{$name} = $_;
  844.     $_ = '';
  845.  
  846. sub resolve_types {
  847.     local($sou);
  848.     for $i (0 .. $#type) {
  849.     next unless defined $type[$i];
  850.     $_ = $type[$i];
  851.     unless (/\d/) {
  852.         print "type[$i] $type[$i]\n" if $debug;
  853.         next;
  854.     }
  855.     print "type[$i] $_ ==> " if $debug;
  856.     s/^(\d+)(\**)\&\*(\**)/"$2($3".&type($1) . ')()'/e;
  857.     s/^(\d+)\&/&type($1)/e; 
  858.     s/^(\d+)/&type($1)/e; 
  859.     s/(\*+)([^*]+)(\*+)/$1$3$2/;
  860.     s/\((\*+)(\w+)(\*+)\)/$3($1$2)/;
  861.     s/^(\d+)([\*\[].*)/&type($1).$2/e;
  862.     #s/(\d+)(\*|(\[[\[\]\d\*]+]\])+)/&type($1).$2/ge;
  863.     $type[$i] = $_;
  864.     print "$_\n" if $debug;
  865.     }
  866. }
  867. sub type { &psou($type[$_[0]] || "<UNDEFINED>"); } 
  868.  
  869. sub adjust_start_addrs {
  870.     for (sort keys %start) {
  871.     ($basename = $_) =~ s/\.[^.]+$//;
  872.     $start{$_} += $start{$basename};
  873.     print "start: $_ @ $start{$_}\n" if $debug;
  874.     }
  875. }
  876.  
  877. sub sou {
  878.     local($what, $_) = @_;
  879.     /u/ && $isaunion{$what}++;
  880.     /s/ && $isastruct{$what}++;
  881. }
  882.  
  883. sub psou {
  884.     local($what) = @_;
  885.     local($prefix) = '';
  886.     if ($isaunion{$what})  {
  887.     $prefix = 'union ';
  888.     } elsif ($isastruct{$what})  {
  889.     $prefix = 'struct ';
  890.     }
  891.     $prefix . $what;
  892. }
  893.  
  894. sub scrunch {
  895.     local($_) = @_;
  896.  
  897.     study;
  898.  
  899.     s/\$//g;
  900.     s/  / /g;
  901.     1 while s/(\w) \1/$1$1/g;
  902.  
  903.     # i wanna say this, but perl resists my efforts:
  904.     #       s/(\w)(\1+)/$2 . length($1)/ge;
  905.  
  906.     &quick_scrunch;
  907.  
  908.     s/ $//;
  909.  
  910.     $_;
  911. }
  912.  
  913. sub buildscrunchlist {
  914.     $scrunch_code = "sub quick_scrunch {\n";
  915.     for (values %intrinsics) {
  916.         $scrunch_code .= "\ts/($_{2,})/'$_' . length(\$1)/ge;\n";
  917.     } 
  918.     $scrunch_code .= "}\n";
  919.     print "$scrunch_code" if $debug;
  920.     eval $scrunch_code;
  921.     &panic("can't eval scrunch_code $@ \nscrunch_code") if $@;
  922.  
  923. sub fetch_template {
  924.     local($mytype) = @_;
  925.     local($fmt);
  926.     local($count) = 1;
  927.  
  928.     &panic("why do you care?") unless $perl;
  929.  
  930.     if ($mytype =~ s/(\[\d+\])+$//) {
  931.     $count .= $1;
  932.     } 
  933.  
  934.     if ($mytype =~ /\*/) {
  935.     $fmt = $template{'pointer'};
  936.     } 
  937.     elsif (defined $template{$mytype}) {
  938.     $fmt = $template{$mytype};
  939.     } 
  940.     elsif (defined $struct{$mytype}) {
  941.     if (!defined $template{&psou($mytype)}) {
  942.         &build_template($mytype) unless $mytype eq $name;
  943.     } 
  944.     elsif ($template{&psou($mytype)} !~ /\$$/) {
  945.         #warn "incomplete template for $mytype\n";
  946.     } 
  947.     $fmt = $template{&psou($mytype)} || '?';
  948.     } 
  949.     else {
  950.     warn "unknown fmt for $mytype\n";
  951.     $fmt = '?';
  952.     } 
  953.  
  954.     $fmt x $count . ' ';
  955. }
  956.  
  957. sub compute_intrinsics {
  958.     local($TMP) = "/tmp/c2ph-i.$$.c";
  959.     open (TMP, ">$TMP") || die "can't open $TMP: $!";
  960.     select(TMP);
  961.  
  962.     print STDERR "computing intrinsic sizes: " if $trace;
  963.  
  964.     undef %intrinsics;
  965.  
  966.     print <<'EOF';
  967. main() {
  968.     char *mask = "%d %s\n";
  969. EOF
  970.  
  971.     for $type (@intrinsics) {
  972.     next if $type eq 'void';
  973.     print <<"EOF";
  974.     printf(mask,sizeof($type), "$type");
  975. EOF
  976.     } 
  977.  
  978.     print <<'EOF';
  979.     printf(mask,sizeof(char *), "pointer");
  980.     exit(0);
  981. }
  982. EOF
  983.     close TMP;
  984.  
  985.     select(STDOUT);
  986.     open(PIPE, "cd /tmp && $CC $TMP && /tmp/a.out|");
  987.     while (<PIPE>) {
  988.     chop;
  989.     split(' ',$_,2);;
  990.     print "intrinsic $_[1] is size $_[0]\n" if $debug;
  991.     $sizeof{$_[1]} = $_[0];
  992.     $intrinsics{$_[1]} = $template{$_[0]};
  993.     } 
  994.     close(PIPE) || die "couldn't read intrinsics!";
  995.     unlink($TMP, '/tmp/a.out');
  996.     print STDERR "done\n" if $trace;
  997.  
  998. sub scripts2count {
  999.     local($_) = @_;
  1000.  
  1001.     s/^\[//;
  1002.     s/\]$//;
  1003.     s/\]\[/*/g;
  1004.     $_ = eval;
  1005.     &panic("$_: $@") if $@;
  1006.     $_;
  1007. }
  1008.  
  1009. sub system {
  1010.     print STDERR "@_\n" if $trace;
  1011.     system @_;
  1012.  
  1013. sub build_template { 
  1014.     local($name) = @_;
  1015.  
  1016.     &panic("already got a template for $name") if defined $template{$name};
  1017.  
  1018.     local($build_templates) = 1;
  1019.  
  1020.     local($lparen) = '(' x $build_recursed;
  1021.     local($rparen) = ')' x $build_recursed;
  1022.  
  1023.     print STDERR "$lparen$name$rparen " if $trace;
  1024.     $build_recursed++;
  1025.     &pstruct($name,$name,0);
  1026.     print STDERR "TEMPLATE for $name is ", $template{&psou($name)}, "\n" if $debug;
  1027.     --$build_recursed;
  1028. }
  1029.  
  1030.  
  1031. sub panic {
  1032.  
  1033.     select(STDERR);
  1034.  
  1035.     print "\npanic: @_\n";
  1036.  
  1037.     exit 1 if $] <= 4.003;  # caller broken
  1038.  
  1039.     local($i,$_);
  1040.     local($p,$f,$l,$s,$h,$a,@a,@sub);
  1041.     for ($i = 0; ($p,$f,$l,$s,$h,$w) = caller($i); $i++) {
  1042.     @a = @DB'args;
  1043.     for (@a) {
  1044.         if (/^StB\000/ && length($_) == length($_main{'_main'})) {
  1045.         $_ = sprintf("%s",$_);
  1046.         }
  1047.         else {
  1048.         s/'/\\'/g;
  1049.         s/([^\0]*)/'$1'/ unless /^-?[\d.]+$/;
  1050.         s/([\200-\377])/sprintf("M-%c",ord($1)&0177)/eg;
  1051.         s/([\0-\37\177])/sprintf("^%c",ord($1)^64)/eg;
  1052.         }
  1053.     }
  1054.     $w = $w ? '@ = ' : '$ = ';
  1055.     $a = $h ? '(' . join(', ', @a) . ')' : '';
  1056.     push(@sub, "$w&$s$a from file $f line $l\n");
  1057.     last if $signal;
  1058.     }
  1059.     for ($i=0; $i <= $#sub; $i++) {
  1060.     last if $signal;
  1061.     print $sub[$i];
  1062.     }
  1063.     exit 1;
  1064.  
  1065. sub squishseq {
  1066.     local($num);
  1067.     local($last) = -1e8;
  1068.     local($string);
  1069.     local($seq) = '..';
  1070.  
  1071.     while (defined($num = shift)) {
  1072.         if ($num == ($last + 1)) {
  1073.             $string .= $seq unless $inseq++;
  1074.             $last = $num;
  1075.             next;
  1076.         } elsif ($inseq) {
  1077.             $string .= $last unless $last == -1e8;
  1078.         }
  1079.  
  1080.         $string .= ',' if defined $string;
  1081.         $string .= $num;
  1082.         $last = $num;
  1083.         $inseq = 0;
  1084.     }
  1085.     $string .= $last if $inseq && $last != -e18;
  1086.     $string;
  1087. }
  1088. !NO!SUBS!
  1089. $eunicefix c2ph
  1090. rm -f pstruct
  1091. ln c2ph pstruct
  1092.