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