home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / perl_utl.zip / c2ph.cmd < prev    next >
OS/2 REXX Batch file  |  1997-11-28  |  38KB  |  1,357 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. #
  7. #   c2ph (aka pstruct)
  8. #   Tom Christiansen, <tchrist@convex.com>
  9. #
  10. #   As pstruct, dump C structures as generated from 'cc -g -S' stabs.
  11. #   As c2ph, do this PLUS generate perl code for getting at the structures.
  12. #
  13. #   See the usage message for more.  If this isn't enough, read the code.
  14. #
  15.  
  16. =head1 NAME
  17.  
  18. c2ph, pstruct - Dump C structures as generated from C<cc -g -S> stabs
  19.  
  20. =head1 SYNOPSIS
  21.  
  22.     c2ph [-dpnP] [var=val] [files ...]
  23.  
  24. =head2 OPTIONS
  25.  
  26.     Options:
  27.  
  28.     -w    wide; short for: type_width=45 member_width=35 offset_width=8
  29.     -x    hex; short for:  offset_fmt=x offset_width=08 size_fmt=x size_width=04
  30.  
  31.     -n    do not generate perl code  (default when invoked as pstruct)
  32.     -p    generate perl code         (default when invoked as c2ph)
  33.     -v    generate perl code, with C decls as comments
  34.  
  35.     -i    do NOT recompute sizes for intrinsic datatypes
  36.     -a    dump information on intrinsics also
  37.  
  38.     -t    trace execution
  39.     -d    spew reams of debugging output
  40.  
  41.     -slist  give comma-separated list a structures to dump
  42.  
  43. =head1 DESCRIPTION
  44.  
  45. The following is the old c2ph.doc documentation by Tom Christiansen
  46. <tchrist@perl.com>
  47. Date: 25 Jul 91 08:10:21 GMT
  48.  
  49. Once upon a time, I wrote a program called pstruct.  It was a perl
  50. program that tried to parse out C structures and display their member
  51. offsets for you.  This was especially useful for people looking at
  52. binary dumps or poking around the kernel.
  53.  
  54. Pstruct was not a pretty program.  Neither was it particularly robust.
  55. The problem, you see, was that the C compiler was much better at parsing
  56. C than I could ever hope to be.
  57.  
  58. So I got smart:  I decided to be lazy and let the C compiler parse the C,
  59. which would spit out debugger stabs for me to read.  These were much
  60. easier to parse.  It's still not a pretty program, but at least it's more
  61. robust.
  62.  
  63. Pstruct takes any .c or .h files, or preferably .s ones, since that's
  64. the format it is going to massage them into anyway, and spits out
  65. listings like this:
  66.  
  67.  struct tty {
  68.    int                          tty.t_locker                         000      4
  69.    int                          tty.t_mutex_index                    004      4
  70.    struct tty *                 tty.t_tp_virt                        008      4
  71.    struct clist                 tty.t_rawq                           00c     20
  72.      int                        tty.t_rawq.c_cc                      00c      4
  73.      int                        tty.t_rawq.c_cmax                    010      4
  74.      int                        tty.t_rawq.c_cfx                     014      4
  75.      int                        tty.t_rawq.c_clx                     018      4
  76.      struct tty *               tty.t_rawq.c_tp_cpu                  01c      4
  77.      struct tty *               tty.t_rawq.c_tp_iop                  020      4
  78.      unsigned char *            tty.t_rawq.c_buf_cpu                 024      4
  79.      unsigned char *            tty.t_rawq.c_buf_iop                 028      4
  80.    struct clist                 tty.t_canq                           02c     20
  81.      int                        tty.t_canq.c_cc                      02c      4
  82.      int                        tty.t_canq.c_cmax                    030      4
  83.      int                        tty.t_canq.c_cfx                     034      4
  84.      int                        tty.t_canq.c_clx                     038      4
  85.      struct tty *               tty.t_canq.c_tp_cpu                  03c      4
  86.      struct tty *               tty.t_canq.c_tp_iop                  040      4
  87.      unsigned char *            tty.t_canq.c_buf_cpu                 044      4
  88.      unsigned char *            tty.t_canq.c_buf_iop                 048      4
  89.    struct clist                 tty.t_outq                           04c     20
  90.      int                        tty.t_outq.c_cc                      04c      4
  91.      int                        tty.t_outq.c_cmax                    050      4
  92.      int                        tty.t_outq.c_cfx                     054      4
  93.      int                        tty.t_outq.c_clx                     058      4
  94.      struct tty *               tty.t_outq.c_tp_cpu                  05c      4
  95.      struct tty *               tty.t_outq.c_tp_iop                  060      4
  96.      unsigned char *            tty.t_outq.c_buf_cpu                 064      4
  97.      unsigned char *            tty.t_outq.c_buf_iop                 068      4
  98.    (*int)()                     tty.t_oproc_cpu                      06c      4
  99.    (*int)()                     tty.t_oproc_iop                      070      4
  100.    (*int)()                     tty.t_stopproc_cpu                   074      4
  101.    (*int)()                     tty.t_stopproc_iop                   078      4
  102.    struct thread *              tty.t_rsel                           07c      4
  103.  
  104. etc.
  105.  
  106.  
  107. Actually, this was generated by a particular set of options.  You can control
  108. the formatting of each column, whether you prefer wide or fat, hex or decimal,
  109. leading zeroes or whatever.
  110.  
  111. All you need to be able to use this is a C compiler than generates
  112. BSD/GCC-style stabs.  The B<-g> option on native BSD compilers and GCC
  113. should get this for you.
  114.  
  115. To learn more, just type a bogus option, like B<-\?>, and a long usage message
  116. will be provided.  There are a fair number of possibilities.
  117.  
  118. If you're only a C programmer, than this is the end of the message for you.
  119. You can quit right now, and if you care to, save off the source and run it
  120. when you feel like it.  Or not.
  121.  
  122.  
  123.  
  124. But if you're a perl programmer, then for you I have something much more
  125. wondrous than just a structure offset printer.
  126.  
  127. You see, if you call pstruct by its other incybernation, c2ph, you have a code
  128. generator that translates C code into perl code!  Well, structure and union
  129. declarations at least, but that's quite a bit.
  130.  
  131. Prior to this point, anyone programming in perl who wanted to interact
  132. with C programs, like the kernel, was forced to guess the layouts of
  133. the C strutures, and then hardwire these into his program.  Of course,
  134. when you took your wonderfully crafted program to a system where the
  135. sgtty structure was laid out differently, you program broke.  Which is
  136. a shame.
  137.  
  138. We've had Larry's h2ph translator, which helped, but that only works on
  139. cpp symbols, not real C, which was also very much needed.  What I offer
  140. you is a symbolic way of getting at all the C structures.  I've couched
  141. them in terms of packages and functions.  Consider the following program:
  142.  
  143.     #!/usr/local/bin/perl
  144.  
  145.     require 'syscall.ph';
  146.     require 'sys/time.ph';
  147.     require 'sys/resource.ph';
  148.  
  149.     $ru = "\0" x &rusage'sizeof();
  150.  
  151.     syscall(&SYS_getrusage, &RUSAGE_SELF, $ru)      && die "getrusage: $!";
  152.  
  153.     @ru = unpack($t = &rusage'typedef(), $ru);
  154.  
  155.     $utime =  $ru[ &rusage'ru_utime + &timeval'tv_sec  ]
  156.        + ($ru[ &rusage'ru_utime + &timeval'tv_usec ]) / 1e6;
  157.  
  158.     $stime =  $ru[ &rusage'ru_stime + &timeval'tv_sec  ]
  159.        + ($ru[ &rusage'ru_stime + &timeval'tv_usec ]) / 1e6;
  160.  
  161.     printf "you have used %8.3fs+%8.3fu seconds.\n", $utime, $stime;
  162.  
  163.  
  164. As you see, the name of the package is the name of the structure.  Regular
  165. fields are just their own names.  Plus the following accessor functions are
  166. provided for your convenience:
  167.  
  168.     struct    This takes no arguments, and is merely the number of first-level
  169.         elements in the structure.  You would use this for indexing
  170.         into arrays of structures, perhaps like this
  171.  
  172.  
  173.             $usec = $u[ &user'u_utimer
  174.                 + (&ITIMER_VIRTUAL * &itimerval'struct)
  175.                 + &itimerval'it_value
  176.                 + &timeval'tv_usec
  177.                   ];
  178.  
  179.     sizeof       Returns the bytes in the structure, or the member if
  180.              you pass it an argument, such as
  181.  
  182.             &rusage'sizeof(&rusage'ru_utime)
  183.  
  184.     typedef      This is the perl format definition for passing to pack and
  185.              unpack.  If you ask for the typedef of a nothing, you get
  186.              the whole structure, otherwise you get that of the member
  187.              you ask for.  Padding is taken care of, as is the magic to
  188.              guarantee that a union is unpacked into all its aliases.
  189.              Bitfields are not quite yet supported however.
  190.  
  191.     offsetof    This function is the byte offset into the array of that
  192.         member.  You may wish to use this for indexing directly
  193.         into the packed structure with vec() if you're too lazy
  194.         to unpack it.
  195.  
  196.     typeof    Not to be confused with the typedef accessor function, this
  197.         one returns the C type of that field.  This would allow
  198.         you to print out a nice structured pretty print of some
  199.         structure without knoning anything about it beforehand.
  200.         No args to this one is a noop.  Someday I'll post such
  201.         a thing to dump out your u structure for you.
  202.  
  203.  
  204. The way I see this being used is like basically this:
  205.  
  206.     % h2ph <some_include_file.h  >  /usr/lib/perl/tmp.ph
  207.     % c2ph  some_include_file.h  >> /usr/lib/perl/tmp.ph
  208.     % install
  209.  
  210. It's a little tricker with c2ph because you have to get the includes right.
  211. I can't know this for your system, but it's not usually too terribly difficult.
  212.  
  213. The code isn't pretty as I mentioned  -- I never thought it would be a 1000-
  214. line program when I started, or I might not have begun. :-)  But I would have
  215. been less cavalier in how the parts of the program communicated with each
  216. other, etc.  It might also have helped if I didn't have to divine the makeup
  217. of the stabs on the fly, and then account for micro differences between my
  218. compiler and gcc.
  219.  
  220. Anyway, here it is.  Should run on perl v4 or greater.  Maybe less.
  221.  
  222.  
  223.  --tom
  224.  
  225. =cut
  226.  
  227. $RCSID = '$Id: c2ph,v 1.7 95/10/28 10:41:47 tchrist Exp Locker: tchrist $';
  228.  
  229.  
  230. ######################################################################
  231.  
  232. # some handy data definitions.   many of these can be reset later.
  233.  
  234. $bitorder = 'b';  # ascending; set to B for descending bit fields
  235.  
  236. %intrinsics =
  237. %template = (
  238.     'char',             'c',
  239.     'unsigned char',         'C',
  240.     'short',            's',
  241.     'short int',        's',
  242.     'unsigned short',        'S',
  243.     'unsigned short int',    'S',
  244.     'short unsigned int',    'S',
  245.     'int',            'i',
  246.     'unsigned int',        'I',
  247.     'long',            'l',
  248.     'long int',            'l',
  249.     'unsigned long',        'L',
  250.     'unsigned long',        'L',
  251.     'long unsigned int',    'L',
  252.     'unsigned long int',    'L',
  253.     'long long',        'q',
  254.     'long long int',        'q',
  255.     'unsigned long long',    'Q',
  256.     'unsigned long long int',    'Q',
  257.     'float',            'f',
  258.     'double',            'd',
  259.     'pointer',            'p',
  260.     'null',            'x',
  261.     'neganull',            'X',
  262.     'bit',            $bitorder,
  263. );
  264.  
  265. &buildscrunchlist;
  266. delete $intrinsics{'neganull'};
  267. delete $intrinsics{'bit'};
  268. delete $intrinsics{'null'};
  269.  
  270. # use -s to recompute sizes
  271. %sizeof = (
  272.     'char',             '1',
  273.     'unsigned char',         '1',
  274.     'short',            '2',
  275.     'short int',        '2',
  276.     'unsigned short',        '2',
  277.     'unsigned short int',    '2',
  278.     'short unsigned int',    '2',
  279.     'int',            '4',
  280.     'unsigned int',        '4',
  281.     'long',            '4',
  282.     'long int',            '4',
  283.     'unsigned long',        '4',
  284.     'unsigned long int',    '4',
  285.     'long unsigned int',    '4',
  286.     'long long',        '8',
  287.     'long long int',        '8',
  288.     'unsigned long long',    '8',
  289.     'unsigned long long int',    '8',
  290.     'float',            '4',
  291.     'double',            '8',
  292.     'pointer',            '4',
  293. );
  294.  
  295. ($type_width, $member_width, $offset_width, $size_width) = (20, 20, 6, 5);
  296.  
  297. ($offset_fmt, $size_fmt) = ('d', 'd');
  298.  
  299. $indent = 2;
  300.  
  301. $CC = 'cc';
  302. $CFLAGS = '-g -S';
  303. $DEFINES = '';
  304.  
  305. $perl++ if $0 =~ m#/?c2ph$#;
  306.  
  307. require 'getopts.pl';
  308.  
  309. eval '$'.$1.'$2;' while $ARGV[0] =~ /^([A-Za-z_]+=)(.*)/ && shift;
  310.  
  311. &Getopts('aixdpvtnws:') || &usage(0);
  312.  
  313. $opt_d && $debug++;
  314. $opt_t && $trace++;
  315. $opt_p && $perl++;
  316. $opt_v && $verbose++;
  317. $opt_n && ($perl = 0);
  318.  
  319. if ($opt_w) {
  320.     ($type_width, $member_width, $offset_width) = (45, 35, 8);
  321. }
  322. if ($opt_x) {
  323.     ($offset_fmt, $offset_width, $size_fmt, $size_width) = ( 'x', '08', 'x', 04 );
  324. }
  325.  
  326. eval '$'.$1.'$2;' while $ARGV[0] =~ /^([A-Za-z_]+=)(.*)/ && shift;
  327.  
  328. sub PLUMBER {
  329.     select(STDERR);
  330.     print "oops, apperent pager foulup\n";
  331.     $isatty++;
  332.     &usage(1);
  333. }
  334.  
  335. sub usage {
  336.     local($oops) = @_;
  337.     unless (-t STDOUT) {
  338.     select(STDERR);
  339.     } elsif (!$oops) {
  340.     $isatty++;
  341.     $| = 1;
  342.     print "hit <RETURN> for further explanation: ";
  343.     <STDIN>;
  344.     open (PIPE, "|". ($ENV{PAGER} || 'more'));
  345.     $SIG{PIPE} = PLUMBER;
  346.     select(PIPE);
  347.     }
  348.  
  349.     print "usage: $0 [-dpnP] [var=val] [files ...]\n";
  350.  
  351.     exit unless $isatty;
  352.  
  353.     print <<EOF;
  354.  
  355. Options:
  356.  
  357. -w    wide; short for: type_width=45 member_width=35 offset_width=8
  358. -x    hex; short for:  offset_fmt=x offset_width=08 size_fmt=x size_width=04
  359.  
  360. -n      do not generate perl code  (default when invoked as pstruct)
  361. -p      generate perl code         (default when invoked as c2ph)
  362. -v    generate perl code, with C decls as comments
  363.  
  364. -i    do NOT recompute sizes for intrinsic datatypes
  365. -a    dump information on intrinsics also
  366.  
  367. -t     trace execution
  368. -d    spew reams of debugging output
  369.  
  370. -slist  give comma-separated list a structures to dump
  371.  
  372.  
  373. Var Name        Default Value    Meaning
  374.  
  375. EOF
  376.  
  377.     &defvar('CC', 'which_compiler to call');
  378.     &defvar('CFLAGS', 'how to generate *.s files with stabs');
  379.     &defvar('DEFINES','any extra cflags or cpp defines, like -I, -D, -U');
  380.  
  381.     print "\n";
  382.  
  383.     &defvar('type_width', 'width of type field   (column 1)');
  384.     &defvar('member_width', 'width of member field (column 2)');
  385.     &defvar('offset_width', 'width of offset field (column 3)');
  386.     &defvar('size_width', 'width of size field   (column 4)');
  387.  
  388.     print "\n";
  389.  
  390.     &defvar('offset_fmt', 'sprintf format type for offset');
  391.     &defvar('size_fmt', 'sprintf format type for size');
  392.  
  393.     print "\n";
  394.  
  395.     &defvar('indent', 'how far to indent each nesting level');
  396.  
  397.    print <<'EOF';
  398.  
  399.     If any *.[ch] files are given, these will be catted together into
  400.     a temporary *.c file and sent through:
  401.         $CC $CFLAGS $DEFINES
  402.     and the resulting *.s groped for stab information.  If no files are
  403.     supplied, then stdin is read directly with the assumption that it
  404.     contains stab information.  All other liens will be ignored.  At
  405.     most one *.s file should be supplied.
  406.  
  407. EOF
  408.     close PIPE;
  409.     exit 1;
  410. }
  411.  
  412. sub defvar {
  413.     local($var, $msg) = @_;
  414.     printf "%-16s%-15s  %s\n", $var, eval "\$$var", $msg;
  415. }
  416.  
  417. $recurse = 1;
  418.  
  419. if (@ARGV) {
  420.     if (grep(!/\.[csh]$/,@ARGV)) {
  421.     warn "Only *.[csh] files expected!\n";
  422.     &usage;
  423.     }
  424.     elsif (grep(/\.s$/,@ARGV)) {
  425.     if (@ARGV > 1) {
  426.         warn "Only one *.s file allowed!\n";
  427.         &usage;
  428.     }
  429.     }
  430.     elsif (@ARGV == 1 && $ARGV[0] =~ /\.c$/) {
  431.     local($dir, $file) = $ARGV[0] =~ m#(.*/)?(.*)$#;
  432.     $chdir = "cd $dir; " if $dir;
  433.     &system("$chdir$CC $CFLAGS $DEFINES $file") && exit 1;
  434.     $ARGV[0] =~ s/\.c$/.s/;
  435.     }
  436.     else {
  437.     $TMP = "/tmp/c2ph.$$.c";
  438.     &system("cat @ARGV > $TMP") && exit 1;
  439.     &system("cd /tmp; $CC $CFLAGS $DEFINES $TMP") && exit 1;
  440.     unlink $TMP;
  441.     $TMP =~ s/\.c$/.s/;
  442.     @ARGV = ($TMP);
  443.     }
  444. }
  445.  
  446. if ($opt_s) {
  447.     for (split(/[\s,]+/, $opt_s)) {
  448.     $interested{$_}++;
  449.     }
  450. }
  451.  
  452.  
  453. $| = 1 if $debug;
  454.  
  455. main: {
  456.  
  457.     if ($trace) {
  458.     if (-t && !@ARGV) {
  459.         print STDERR "reading from your keyboard: ";
  460.     } else {
  461.         print STDERR "reading from " . (@ARGV ? "@ARGV" : "<STDIN>").": ";
  462.     }
  463.     }
  464.  
  465. STAB: while (<>) {
  466.     if ($trace && !($. % 10)) {
  467.         $lineno = $..'';
  468.         print STDERR $lineno, "\b" x length($lineno);
  469.     }
  470.     next unless /^\s*\.stabs\s+/;
  471.     $line = $_;
  472.     s/^\s*\.stabs\s+//;
  473.     if (s/\\\\"[d,]+$//) {
  474.         $saveline .= $line;
  475.         $savebar  = $_;
  476.         next STAB;
  477.     }
  478.     if ($saveline) {
  479.         s/^"//;
  480.         $_ = $savebar . $_;
  481.         $line = $saveline;
  482.     }
  483.     &stab;
  484.     $savebar = $saveline = undef;
  485.     }
  486.     print STDERR "$.\n" if $trace;
  487.     unlink $TMP if $TMP;
  488.  
  489.     &compute_intrinsics if $perl && !$opt_i;
  490.  
  491.     print STDERR "resolving types\n" if $trace;
  492.  
  493.     &resolve_types;
  494.     &adjust_start_addrs;
  495.  
  496.     $sum = 2 + $type_width + $member_width;
  497.     $pmask1 = "%-${type_width}s %-${member_width}s";
  498.     $pmask2 = "%-${sum}s %${offset_width}${offset_fmt}%s %${size_width}${size_fmt}%s";
  499.  
  500.  
  501.  
  502.     if ($perl) {
  503.     # resolve template -- should be in stab define order, but even this isn't enough.
  504.     print STDERR "\nbuilding type templates: " if $trace;
  505.     for $i (reverse 0..$#type) {
  506.         next unless defined($name = $type[$i]);
  507.         next unless defined $struct{$name};
  508.         ($iname = $name) =~ s/\..*//;
  509.         $build_recursed = 0;
  510.         &build_template($name) unless defined $template{&psou($name)} ||
  511.                     $opt_s && !$interested{$iname};
  512.     }
  513.     print STDERR "\n\n" if $trace;
  514.     }
  515.  
  516.     print STDERR "dumping structs: " if $trace;
  517.  
  518.     local($iam);
  519.  
  520.  
  521.  
  522.     foreach $name (sort keys %struct) {
  523.     ($iname = $name) =~ s/\..*//;
  524.     next if $opt_s && !$interested{$iname};
  525.     print STDERR "$name " if $trace;
  526.  
  527.     undef @sizeof;
  528.     undef @typedef;
  529.     undef @offsetof;
  530.     undef @indices;
  531.     undef @typeof;
  532.     undef @fieldnames;
  533.  
  534.     $mname = &munge($name);
  535.  
  536.     $fname = &psou($name);
  537.  
  538.     print "# " if $perl && $verbose;
  539.     $pcode = '';
  540.     print "$fname {\n" if !$perl || $verbose;
  541.     $template{$fname} = &scrunch($template{$fname}) if $perl;
  542.     &pstruct($name,$name,0);
  543.     print "# " if $perl && $verbose;
  544.     print "}\n" if !$perl || $verbose;
  545.     print "\n" if $perl && $verbose;
  546.  
  547.     if ($perl) {
  548.         print "$pcode";
  549.  
  550.         printf("\nsub %-32s { %4d; }\n\n", "${mname}'struct", $countof{$name});
  551.  
  552.         print <<EOF;
  553. sub ${mname}'typedef {
  554.     local(\$${mname}'index) = shift;
  555.     defined \$${mname}'index
  556.     ? \$${mname}'typedef[\$${mname}'index]
  557.     : \$${mname}'typedef;
  558. }
  559. EOF
  560.  
  561.         print <<EOF;
  562. sub ${mname}'sizeof {
  563.     local(\$${mname}'index) = shift;
  564.     defined \$${mname}'index
  565.     ? \$${mname}'sizeof[\$${mname}'index]
  566.     : \$${mname}'sizeof;
  567. }
  568. EOF
  569.  
  570.         print <<EOF;
  571. sub ${mname}'offsetof {
  572.     local(\$${mname}'index) = shift;
  573.     defined \$${mname}index
  574.     ? \$${mname}'offsetof[\$${mname}'index]
  575.     : \$${mname}'sizeof;
  576. }
  577. EOF
  578.  
  579.         print <<EOF;
  580. sub ${mname}'typeof {
  581.     local(\$${mname}'index) = shift;
  582.     defined \$${mname}index
  583.     ? \$${mname}'typeof[\$${mname}'index]
  584.     : '$name';
  585. }
  586. EOF
  587.  
  588.         print <<EOF;
  589. sub ${mname}'fieldnames {
  590.     \@${mname}'fieldnames;
  591. }
  592. EOF
  593.  
  594.     $iam = ($isastruct{$name} && 's') || ($isaunion{$name} && 'u');
  595.  
  596.         print <<EOF;
  597. sub ${mname}'isastruct {
  598.     '$iam';
  599. }
  600. EOF
  601.  
  602.         print "\$${mname}'typedef = '" . &scrunch($template{$fname})
  603.         . "';\n";
  604.  
  605.         print "\$${mname}'sizeof = $sizeof{$name};\n\n";
  606.  
  607.  
  608.         print "\@${mname}'indices = (", &squishseq(@indices), ");\n";
  609.  
  610.         print "\n";
  611.  
  612.         print "\@${mname}'typedef[\@${mname}'indices] = (",
  613.             join("\n\t", '', @typedef), "\n    );\n\n";
  614.         print "\@${mname}'sizeof[\@${mname}'indices] = (",
  615.             join("\n\t", '', @sizeof), "\n    );\n\n";
  616.         print "\@${mname}'offsetof[\@${mname}'indices] = (",
  617.             join("\n\t", '', @offsetof), "\n    );\n\n";
  618.         print "\@${mname}'typeof[\@${mname}'indices] = (",
  619.             join("\n\t", '', @typeof), "\n    );\n\n";
  620.         print "\@${mname}'fieldnames[\@${mname}'indices] = (",
  621.             join("\n\t", '', @fieldnames), "\n    );\n\n";
  622.  
  623.         $template_printed{$fname}++;
  624.         $size_printed{$fname}++;
  625.     }
  626.     print "\n";
  627.     }
  628.  
  629.     print STDERR "\n" if $trace;
  630.  
  631.     unless ($perl && $opt_a) {
  632.     print "\n1;\n" if $perl;
  633.     exit;
  634.     }
  635.  
  636.  
  637.  
  638.     foreach $name (sort bysizevalue keys %intrinsics) {
  639.     next if $size_printed{$name};
  640.     print '$',&munge($name),"'sizeof = ", $sizeof{$name}, ";\n";
  641.     }
  642.  
  643.     print "\n";
  644.  
  645.     sub bysizevalue { $sizeof{$a} <=> $sizeof{$b}; }
  646.  
  647.  
  648.     foreach $name (sort keys %intrinsics) {
  649.     print '$',&munge($name),"'typedef = '", $template{$name}, "';\n";
  650.     }
  651.  
  652.     print "\n1;\n" if $perl;
  653.  
  654.     exit;
  655. }
  656.  
  657. ########################################################################################
  658.  
  659.  
  660. sub stab {
  661.     next unless $continued || /:[\$\w]+(\(\d+,\d+\))?=[\*\$\w]+/;  # (\d+,\d+) is for sun
  662.     s/"//                         || next;
  663.     s/",([x\d]+),([x\d]+),([x\d]+),.*//         || next;
  664.  
  665.     next if /^\s*$/;
  666.  
  667.     $size = $3 if $3;
  668.     $_ = $continued . $_ if length($continued);
  669.     if (s/\\\\$//) {
  670.       # if last 2 chars of string are '\\' then stab is continued
  671.       # in next stab entry
  672.       chop;
  673.       $continued = $_;
  674.       next;
  675.     }
  676.     $continued = '';
  677.  
  678.  
  679.     $line = $_;
  680.  
  681.     if (($name, $pdecl) = /^([\$ \w]+):[tT]((\d+)(=[rufs*](\d+))+)$/) {
  682.     print "$name is a typedef for some funky pointers: $pdecl\n" if $debug;
  683.     &pdecl($pdecl);
  684.     next;
  685.     }
  686.  
  687.  
  688.  
  689.     if (/(([ \w]+):t(\d+|\(\d+,\d+\)))=r?(\d+|\(\d+,\d+\))(;\d+;\d+;)?/) {
  690.     local($ident) = $2;
  691.     push(@intrinsics, $ident);
  692.     $typeno = &typeno($3);
  693.     $type[$typeno] = $ident;
  694.     print STDERR "intrinsic $ident in new type $typeno\n" if $debug;
  695.     next;
  696.     }
  697.  
  698.     if (($name, $typeordef, $typeno, $extra, $struct, $_)
  699.     = /^([\$ \w]+):([ustT])(\d+|\(\d+,\d+\))(=[rufs*](\d+))?(.*)$/)
  700.     {
  701.     $typeno = &typeno($typeno);  # sun foolery
  702.     }
  703.     elsif (/^[\$\w]+:/) {
  704.     next; # variable
  705.     }
  706.     else {
  707.     warn "can't grok stab: <$_> in: $line " if $_;
  708.     next;
  709.     }
  710.  
  711.     #warn "got size $size for $name\n";
  712.     $sizeof{$name} = $size if $size;
  713.  
  714.     s/;[-\d]*;[-\d]*;$//;  # we don't care about ranges
  715.  
  716.     $typenos{$name} = $typeno;
  717.  
  718.     unless (defined $type[$typeno]) {
  719.     &panic("type 0??") unless $typeno;
  720.     $type[$typeno] = $name unless defined $type[$typeno];
  721.     printf "new type $typeno is $name" if $debug;
  722.     if ($extra =~ /\*/ && defined $type[$struct]) {
  723.         print ", a typedef for a pointer to " , $type[$struct] if $debug;
  724.     }
  725.     } else {
  726.     printf "%s is type %d", $name, $typeno if $debug;
  727.     print ", a typedef for " , $type[$typeno] if $debug;
  728.     }
  729.     print "\n" if $debug;
  730.     #next unless $extra =~ /[su*]/;
  731.  
  732.     #$type[$struct] = $name;
  733.  
  734.     if ($extra =~ /[us*]/) {
  735.     &sou($name, $extra);
  736.     $_ = &sdecl($name, $_, 0);
  737.     }
  738.     elsif (/^=ar/) {
  739.     print "it's a bare array typedef -- that's pretty sick\n" if $debug;
  740.     $_ = "$typeno$_";
  741.     $scripts = '';
  742.     $_ = &adecl($_,1);
  743.  
  744.     }
  745.     elsif (s/((\w+):t(\d+|\(\d+,\d+\)))?=r?(;\d+;\d+;)?//) {  # the ?'s are for gcc
  746.     push(@intrinsics, $2);
  747.     $typeno = &typeno($3);
  748.     $type[$typeno] = $2;
  749.     print STDERR "intrinsic $2 in new type $typeno\n" if $debug;
  750.     }
  751.     elsif (s/^=e//) { # blessed be thy compiler; mine won't do this
  752.     &edecl;
  753.     }
  754.     else {
  755.     warn "Funny remainder for $name on line $_ left in $line " if $_;
  756.     }
  757. }
  758.  
  759. sub typeno {  # sun thinks types are (0,27) instead of just 27
  760.     local($_) = @_;
  761.     s/\(\d+,(\d+)\)/$1/;
  762.     $_;
  763. }
  764.  
  765. sub pstruct {
  766.     local($what,$prefix,$base) = @_;
  767.     local($field, $fieldname, $typeno, $count, $offset, $entry);
  768.     local($fieldtype);
  769.     local($type, $tname);
  770.     local($mytype, $mycount, $entry2);
  771.     local($struct_count) = 0;
  772.     local($pad, $revpad, $length, $prepad, $lastoffset, $lastlength, $fmt);
  773.     local($bits,$bytes);
  774.     local($template);
  775.  
  776.  
  777.     local($mname) = &munge($name);
  778.  
  779.     sub munge {
  780.     local($_) = @_;
  781.     s/[\s\$\.]/_/g;
  782.     $_;
  783.     }
  784.  
  785.     local($sname) = &psou($what);
  786.  
  787.     $nesting++;
  788.  
  789.     for $field (split(/;/, $struct{$what})) {
  790.     $pad = $prepad = 0;
  791.     $entry = '';
  792.     ($fieldname, $typeno, $count, $offset, $length) = split(/,/, $field);
  793.  
  794.     $type = $type[$typeno];
  795.  
  796.     $type =~ /([^[]*)(\[.*\])?/;
  797.     $mytype = $1;
  798.     $count .= $2;
  799.     $fieldtype = &psou($mytype);
  800.  
  801.     local($fname) = &psou($name);
  802.  
  803.     if ($build_templates) {
  804.  
  805.         $pad = ($offset - ($lastoffset + $lastlength))/8
  806.         if defined $lastoffset;
  807.  
  808.         if (! $finished_template{$sname}) {
  809.         if ($isaunion{$what}) {
  810.             $template{$sname} .= 'X' x $revpad . ' '    if $revpad;
  811.         } else {
  812.             $template{$sname} .= 'x' x $pad    . ' '    if $pad;
  813.         }
  814.         }
  815.  
  816.         $template = &fetch_template($type);
  817.         &repeat_template($template,$count);
  818.  
  819.         if (! $finished_template{$sname}) {
  820.         $template{$sname} .= $template;
  821.         }
  822.  
  823.         $revpad = $length/8 if $isaunion{$what};
  824.  
  825.         ($lastoffset, $lastlength) = ($offset, $length);
  826.  
  827.     } else {
  828.         print '# ' if $perl && $verbose;
  829.         $entry = sprintf($pmask1,
  830.             ' ' x ($nesting * $indent) . $fieldtype,
  831.             "$prefix.$fieldname" . $count);
  832.  
  833.         $entry =~ s/(\*+)( )/$2$1/;
  834.  
  835.         printf $pmask2,
  836.             $entry,
  837.             ($base+$offset)/8,
  838.             ($bits = ($base+$offset)%8) ? ".$bits" : "  ",
  839.             $length/8,
  840.             ($bits = $length % 8) ? ".$bits": ""
  841.             if !$perl || $verbose;
  842.  
  843.         if ($perl) {
  844.         $template = &fetch_template($type);
  845.         &repeat_template($template,$count);
  846.         }
  847.  
  848.         if ($perl && $nesting == 1) {
  849.  
  850.         push(@sizeof, int($length/8) .",\t# $fieldname");
  851.         push(@offsetof, int($offset/8) .",\t# $fieldname");
  852.         local($little) = &scrunch($template);
  853.         push(@typedef, "'$little', \t# $fieldname");
  854.         $type =~ s/(struct|union) //;
  855.         push(@typeof, "'$mytype" . ($count ? $count : '') .
  856.             "',\t# $fieldname");
  857.         push(@fieldnames, "'$fieldname',");
  858.         }
  859.  
  860.         print '  ', ' ' x $indent x $nesting, $template
  861.                 if $perl && $verbose;
  862.  
  863.         print "\n" if !$perl || $verbose;
  864.  
  865.     }
  866.     if ($perl) {
  867.         local($mycount) = defined $struct{$mytype} ? $countof{$mytype} : 1;
  868.         $mycount *= &scripts2count($count) if $count;
  869.         if ($nesting==1 && !$build_templates) {
  870.         $pcode .= sprintf("sub %-32s { %4d; }\n",
  871.             "${mname}'${fieldname}", $struct_count);
  872.         push(@indices, $struct_count);
  873.         }
  874.         $struct_count += $mycount;
  875.     }
  876.  
  877.  
  878.     &pstruct($type, "$prefix.$fieldname", $base+$offset)
  879.         if $recurse && defined $struct{$type};
  880.     }
  881.  
  882.     $countof{$what} = $struct_count unless defined $countof{$whati};
  883.  
  884.     $template{$sname} .= '$' if $build_templates;
  885.     $finished_template{$sname}++;
  886.  
  887.     if ($build_templates && !defined $sizeof{$name}) {
  888.     local($fmt) = &scrunch($template{$sname});
  889.     print STDERR "no size for $name, punting with $fmt..." if $debug;
  890.     eval '$sizeof{$name} = length(pack($fmt, ()))';
  891.     if ($@) {
  892.         chop $@;
  893.         warn "couldn't get size for \$name: $@";
  894.     } else {
  895.         print STDERR $sizeof{$name}, "\n" if $debUg;
  896.     }
  897.     }
  898.  
  899.     --$nesting;
  900. }
  901.  
  902.  
  903. sub psize {
  904.     local($me) = @_;
  905.     local($amstruct) = $struct{$me} ?  'struct ' : '';
  906.  
  907.     print '$sizeof{\'', $amstruct, $me, '\'} = ';
  908.     printf "%d;\n", $sizeof{$me};
  909. }
  910.  
  911. sub pdecl {
  912.     local($pdecl) = @_;
  913.     local(@pdecls);
  914.     local($tname);
  915.  
  916.     warn "pdecl: $pdecl\n" if $debug;
  917.  
  918.     $pdecl =~ s/\(\d+,(\d+)\)/$1/g;
  919.     $pdecl =~ s/\*//g;
  920.     @pdecls = split(/=/, $pdecl);
  921.     $typeno = $pdecls[0];
  922.     $tname = pop @pdecls;
  923.  
  924.     if ($tname =~ s/^f//) { $tname = "$tname&"; }
  925.     #else { $tname = "$tname*"; }
  926.  
  927.     for (reverse @pdecls) {
  928.     $tname  .= s/^f// ? "&" : "*";
  929.     #$tname =~ s/^f(.*)/$1&/;
  930.     print "type[$_] is $tname\n" if $debug;
  931.     $type[$_] = $tname unless defined $type[$_];
  932.     }
  933. }
  934.  
  935.  
  936.  
  937. sub adecl {
  938.     ($arraytype, $unknown, $lower, $upper) = ();
  939.     #local($typeno);
  940.     # global $typeno, @type
  941.     local($_, $typedef) = @_;
  942.  
  943.     while (s/^((\d+|\(\d+,\d+\))=)?ar(\d+|\(\d+,\d+\));//) {
  944.     ($arraytype, $unknown) = ($2, $3);
  945.     $arraytype = &typeno($arraytype);
  946.     $unknown = &typeno($unknown);
  947.     if (s/^(\d+);(\d+);//) {
  948.         ($lower, $upper) = ($1, $2);
  949.         $scripts .= '[' .  ($upper+1) . ']';
  950.     } else {
  951.         warn "can't find array bounds: $_";
  952.     }
  953.     }
  954.     if (s/^([(,)\d*f=]*),(\d+),(\d+);//) {
  955.     ($start, $length) = ($2, $3);
  956.     $whatis = $1;
  957.     if ($whatis =~ /^(\d+|\(\d+,\d+\))=/) {
  958.         $typeno = &typeno($1);
  959.         &pdecl($whatis);
  960.     } else {
  961.         $typeno = &typeno($whatis);
  962.     }
  963.     } elsif (s/^(\d+)(=[*suf]\d*)//) {
  964.     local($whatis) = $2;
  965.  
  966.     if ($whatis =~ /[f*]/) {
  967.         &pdecl($whatis);
  968.     } elsif ($whatis =~ /[su]/) {  #
  969.         print "$prefix.$fieldname is an array$scripts anon structs; disgusting\n"
  970.         if $debug;
  971.         #$type[$typeno] = $name unless defined $type[$typeno];
  972.         ##printf "new type $typeno is $name" if $debug;
  973.         $typeno = $1;
  974.         $type[$typeno] = "$prefix.$fieldname";
  975.         local($name) = $type[$typeno];
  976.         &sou($name, $whatis);
  977.         $_ = &sdecl($name, $_, $start+$offset);
  978.         1;
  979.         $start = $start{$name};
  980.         $offset = $sizeof{$name};
  981.         $length = $offset;
  982.     } else {
  983.         warn "what's this? $whatis in $line ";
  984.     }
  985.     } elsif (/^\d+$/) {
  986.     $typeno = $_;
  987.     } else {
  988.     warn "bad array stab: $_ in $line ";
  989.     next STAB;
  990.     }
  991.     #local($wasdef) = defined($type[$typeno]) && $debug;
  992.     #if ($typedef) {
  993.     #print "redefining $type[$typeno] to " if $wasdef;
  994.     #$type[$typeno] = "$whatis$scripts"; # unless defined $type[$typeno];
  995.     #print "$type[$typeno]\n" if $wasdef;
  996.     #} else {
  997.     #$type[$arraytype] = $type[$typeno] unless defined $type[$arraytype];
  998.     #}
  999.     $type[$arraytype] = "$type[$typeno]$scripts" if defined $type[$typeno];
  1000.     print "type[$arraytype] is $type[$arraytype]\n" if $debug;
  1001.     print "$prefix.$fieldname is an array of $type[$arraytype]\n" if $debug;
  1002.     $_;
  1003. }
  1004.  
  1005.  
  1006.  
  1007. sub sdecl {
  1008.     local($prefix, $_, $offset) = @_;
  1009.  
  1010.     local($fieldname, $scripts, $type, $arraytype, $unknown,
  1011.     $whatis, $pdecl, $upper,$lower, $start,$length) = ();
  1012.     local($typeno,$sou);
  1013.  
  1014.  
  1015. SFIELD:
  1016.     while (/^([^;]+);/) {
  1017.     $scripts = '';
  1018.     warn "sdecl $_\n" if $debug;
  1019.     if (s/^([\$\w]+)://) {
  1020.         $fieldname = $1;
  1021.     } elsif (s/(\d+)=([us])(\d+|\(\d+,\d+\))//) { #
  1022.         $typeno = &typeno($1);
  1023.         $type[$typeno] = "$prefix.$fieldname";
  1024.         local($name) = "$prefix.$fieldname";
  1025.         &sou($name,$2);
  1026.         $_ = &sdecl("$prefix.$fieldname", $_, $start+$offset);
  1027.         $start = $start{$name};
  1028.         $offset += $sizeof{$name};
  1029.         #print "done with anon, start is $start, offset is $offset\n";
  1030.         #next SFIELD;
  1031.     } else  {
  1032.         warn "weird field $_ of $line" if $debug;
  1033.         next STAB;
  1034.         #$fieldname = &gensym;
  1035.         #$_ = &sdecl("$prefix.$fieldname", $_, $start+$offset);
  1036.     }
  1037.  
  1038.     if (/^(\d+|\(\d+,\d+\))=ar/) {
  1039.         $_ = &adecl($_);
  1040.     }
  1041.     elsif (s/^(\d+|\(\d+,\d+\))?,(\d+),(\d+);//) {
  1042.           ($start, $length) =  ($2, $3);
  1043.           &panic("no length?") unless $length;
  1044.           $typeno = &typeno($1) if $1;
  1045.         }
  1046.         elsif (s/^(\d+)=xs\w+:,(\d+),(\d+);//) {
  1047.         ($start, $length) =  ($2, $3);
  1048.         &panic("no length?") unless $length;
  1049.         $typeno = &typeno($1) if $1;
  1050.     }
  1051.     elsif (s/^((\d+|\(\d+,\d+\))(=[*f](\d+|\(\d+,\d+\)))+),(\d+),(\d+);//) {
  1052.         ($pdecl, $start, $length) =  ($1,$5,$6);
  1053.         &pdecl($pdecl);
  1054.     }
  1055.     elsif (s/(\d+)=([us])(\d+|\(\d+,\d+\))//) { # the dratted anon struct
  1056.         ($typeno, $sou) = ($1, $2);
  1057.         $typeno = &typeno($typeno);
  1058.         if (defined($type[$typeno])) {
  1059.         warn "now how did we get type $1 in $fieldname of $line?";
  1060.         } else {
  1061.         print "anon type $typeno is $prefix.$fieldname\n" if $debug;
  1062.         $type[$typeno] = "$prefix.$fieldname" unless defined $type[$typeno];
  1063.         };
  1064.         local($name) = "$prefix.$fieldname";
  1065.         &sou($name,$sou);
  1066.         print "anon ".($isastruct{$name}) ? "struct":"union"." for $prefix.$fieldname\n" if $debug;
  1067.         $type[$typeno] = "$prefix.$fieldname";
  1068.         $_ = &sdecl("$prefix.$fieldname", $_, $start+$offset);
  1069.         $start = $start{$name};
  1070.         $length = $sizeof{$name};
  1071.     }
  1072.     else {
  1073.         warn "can't grok stab for $name ($_) in line $line ";
  1074.         next STAB;
  1075.     }
  1076.  
  1077.     &panic("no length for $prefix.$fieldname") unless $length;
  1078.     $struct{$name} .= join(',', $fieldname, $typeno, $scripts, $start, $length) . ';';
  1079.     }
  1080.     if (s/;\d*,(\d+),(\d+);//) {
  1081.     local($start, $size) = ($1, $2);
  1082.     $sizeof{$prefix} = $size;
  1083.     print "start of $prefix is $start, size of $sizeof{$prefix}\n" if $debug;
  1084.     $start{$prefix} = $start;
  1085.     }
  1086.     $_;
  1087. }
  1088.  
  1089. sub edecl {
  1090.     s/;$//;
  1091.     $enum{$name} = $_;
  1092.     $_ = '';
  1093. }
  1094.  
  1095. sub resolve_types {
  1096.     local($sou);
  1097.     for $i (0 .. $#type) {
  1098.     next unless defined $type[$i];
  1099.     $_ = $type[$i];
  1100.     unless (/\d/) {
  1101.         print "type[$i] $type[$i]\n" if $debug;
  1102.         next;
  1103.     }
  1104.     print "type[$i] $_ ==> " if $debug;
  1105.     s/^(\d+)(\**)\&\*(\**)/"$2($3".&type($1) . ')()'/e;
  1106.     s/^(\d+)\&/&type($1)/e;
  1107.     s/^(\d+)/&type($1)/e;
  1108.     s/(\*+)([^*]+)(\*+)/$1$3$2/;
  1109.     s/\((\*+)(\w+)(\*+)\)/$3($1$2)/;
  1110.     s/^(\d+)([\*\[].*)/&type($1).$2/e;
  1111.     #s/(\d+)(\*|(\[[\[\]\d\*]+]\])+)/&type($1).$2/ge;
  1112.     $type[$i] = $_;
  1113.     print "$_\n" if $debug;
  1114.     }
  1115. }
  1116. sub type { &psou($type[$_[0]] || "<UNDEFINED>"); }
  1117.  
  1118. sub adjust_start_addrs {
  1119.     for (sort keys %start) {
  1120.     ($basename = $_) =~ s/\.[^.]+$//;
  1121.     $start{$_} += $start{$basename};
  1122.     print "start: $_ @ $start{$_}\n" if $debug;
  1123.     }
  1124. }
  1125.  
  1126. sub sou {
  1127.     local($what, $_) = @_;
  1128.     /u/ && $isaunion{$what}++;
  1129.     /s/ && $isastruct{$what}++;
  1130. }
  1131.  
  1132. sub psou {
  1133.     local($what) = @_;
  1134.     local($prefix) = '';
  1135.     if ($isaunion{$what})  {
  1136.     $prefix = 'union ';
  1137.     } elsif ($isastruct{$what})  {
  1138.     $prefix = 'struct ';
  1139.     }
  1140.     $prefix . $what;
  1141. }
  1142.  
  1143. sub scrunch {
  1144.     local($_) = @_;
  1145.  
  1146.     return '' if $_ eq '';
  1147.  
  1148.     study;
  1149.  
  1150.     s/\$//g;
  1151.     s/  / /g;
  1152.     1 while s/(\w) \1/$1$1/g;
  1153.  
  1154.     # i wanna say this, but perl resists my efforts:
  1155.     #       s/(\w)(\1+)/$2 . length($1)/ge;
  1156.  
  1157.     &quick_scrunch;
  1158.  
  1159.     s/ $//;
  1160.  
  1161.     $_;
  1162. }
  1163.  
  1164. sub buildscrunchlist {
  1165.     $scrunch_code = "sub quick_scrunch {\n";
  1166.     for (values %intrinsics) {
  1167.         $scrunch_code .= "\ts/(${_}{2,})/'$_' . length(\$1)/ge;\n";
  1168.     }
  1169.     $scrunch_code .= "}\n";
  1170.     print "$scrunch_code" if $debug;
  1171.     eval $scrunch_code;
  1172.     &panic("can't eval scrunch_code $@ \nscrunch_code") if $@;
  1173. }
  1174.  
  1175. sub fetch_template {
  1176.     local($mytype) = @_;
  1177.     local($fmt);
  1178.     local($count) = 1;
  1179.  
  1180.     &panic("why do you care?") unless $perl;
  1181.  
  1182.     if ($mytype =~ s/(\[\d+\])+$//) {
  1183.     $count .= $1;
  1184.     }
  1185.  
  1186.     if ($mytype =~ /\*/) {
  1187.     $fmt = $template{'pointer'};
  1188.     }
  1189.     elsif (defined $template{$mytype}) {
  1190.     $fmt = $template{$mytype};
  1191.     }
  1192.     elsif (defined $struct{$mytype}) {
  1193.     if (!defined $template{&psou($mytype)}) {
  1194.         &build_template($mytype) unless $mytype eq $name;
  1195.     }
  1196.     elsif ($template{&psou($mytype)} !~ /\$$/) {
  1197.         #warn "incomplete template for $mytype\n";
  1198.     }
  1199.     $fmt = $template{&psou($mytype)} || '?';
  1200.     }
  1201.     else {
  1202.     warn "unknown fmt for $mytype\n";
  1203.     $fmt = '?';
  1204.     }
  1205.  
  1206.     $fmt x $count . ' ';
  1207. }
  1208.  
  1209. sub compute_intrinsics {
  1210.     local($TMP) = "/tmp/c2ph-i.$$.c";
  1211.     open (TMP, ">$TMP") || die "can't open $TMP: $!";
  1212.     select(TMP);
  1213.  
  1214.     print STDERR "computing intrinsic sizes: " if $trace;
  1215.  
  1216.     undef %intrinsics;
  1217.  
  1218.     print <<'EOF';
  1219. main() {
  1220.     char *mask = "%d %s\n";
  1221. EOF
  1222.  
  1223.     for $type (@intrinsics) {
  1224.     next if !$type || $type eq 'void' || $type =~ /complex/; # sun stuff
  1225.     print <<"EOF";
  1226.     printf(mask,sizeof($type), "$type");
  1227. EOF
  1228.     }
  1229.  
  1230.     print <<'EOF';
  1231.     printf(mask,sizeof(char *), "pointer");
  1232.     exit(0);
  1233. }
  1234. EOF
  1235.     close TMP;
  1236.  
  1237.     select(STDOUT);
  1238.     open(PIPE, "cd /tmp && $CC $TMP && /tmp/a.out|");
  1239.     while (<PIPE>) {
  1240.     chop;
  1241.     split(' ',$_,2);;
  1242.     print "intrinsic $_[1] is size $_[0]\n" if $debug;
  1243.     $sizeof{$_[1]} = $_[0];
  1244.     $intrinsics{$_[1]} = $template{$_[0]};
  1245.     }
  1246.     close(PIPE) || die "couldn't read intrinsics!";
  1247.     unlink($TMP, '/tmp/a.out');
  1248.     print STDERR "done\n" if $trace;
  1249. }
  1250.  
  1251. sub scripts2count {
  1252.     local($_) = @_;
  1253.  
  1254.     s/^\[//;
  1255.     s/\]$//;
  1256.     s/\]\[/*/g;
  1257.     $_ = eval;
  1258.     &panic("$_: $@") if $@;
  1259.     $_;
  1260. }
  1261.  
  1262. sub system {
  1263.     print STDERR "@_\n" if $trace;
  1264.     system @_;
  1265. }
  1266.  
  1267. sub build_template {
  1268.     local($name) = @_;
  1269.  
  1270.     &panic("already got a template for $name") if defined $template{$name};
  1271.  
  1272.     local($build_templates) = 1;
  1273.  
  1274.     local($lparen) = '(' x $build_recursed;
  1275.     local($rparen) = ')' x $build_recursed;
  1276.  
  1277.     print STDERR "$lparen$name$rparen " if $trace;
  1278.     $build_recursed++;
  1279.     &pstruct($name,$name,0);
  1280.     print STDERR "TEMPLATE for $name is ", $template{&psou($name)}, "\n" if $debug;
  1281.     --$build_recursed;
  1282. }
  1283.  
  1284.  
  1285. sub panic {
  1286.  
  1287.     select(STDERR);
  1288.  
  1289.     print "\npanic: @_\n";
  1290.  
  1291.     exit 1 if $] <= 4.003;  # caller broken
  1292.  
  1293.     local($i,$_);
  1294.     local($p,$f,$l,$s,$h,$a,@a,@sub);
  1295.     for ($i = 0; ($p,$f,$l,$s,$h,$w) = caller($i); $i++) {
  1296.     @a = @DB'args;
  1297.     for (@a) {
  1298.         if (/^StB\000/ && length($_) == length($_main{'_main'})) {
  1299.         $_ = sprintf("%s",$_);
  1300.         }
  1301.         else {
  1302.         s/'/\\'/g;
  1303.         s/([^\0]*)/'$1'/ unless /^-?[\d.]+$/;
  1304.         s/([\200-\377])/sprintf("M-%c",ord($1)&0177)/eg;
  1305.         s/([\0-\37\177])/sprintf("^%c",ord($1)^64)/eg;
  1306.         }
  1307.     }
  1308.     $w = $w ? '@ = ' : '$ = ';
  1309.     $a = $h ? '(' . join(', ', @a) . ')' : '';
  1310.     push(@sub, "$w&$s$a from file $f line $l\n");
  1311.     last if $signal;
  1312.     }
  1313.     for ($i=0; $i <= $#sub; $i++) {
  1314.     last if $signal;
  1315.     print $sub[$i];
  1316.     }
  1317.     exit 1;
  1318. }
  1319.  
  1320. sub squishseq {
  1321.     local($num);
  1322.     local($last) = -1e8;
  1323.     local($string);
  1324.     local($seq) = '..';
  1325.  
  1326.     while (defined($num = shift)) {
  1327.         if ($num == ($last + 1)) {
  1328.             $string .= $seq unless $inseq++;
  1329.             $last = $num;
  1330.             next;
  1331.         } elsif ($inseq) {
  1332.             $string .= $last unless $last == -1e8;
  1333.         }
  1334.  
  1335.         $string .= ',' if defined $string;
  1336.         $string .= $num;
  1337.         $last = $num;
  1338.         $inseq = 0;
  1339.     }
  1340.     $string .= $last if $inseq && $last != -e18;
  1341.     $string;
  1342. }
  1343.  
  1344. sub repeat_template {
  1345.     #  local($template, $scripts) = @_;  have to change caller's values
  1346.  
  1347.     if ( $_[1] ) {
  1348.     local($ncount) = &scripts2count($_[1]);
  1349.     if ($_[0] =~ /^\s*c\s*$/i) {
  1350.         $_[0] = "A$ncount ";
  1351.         $_[1] = '';
  1352.     } else {
  1353.         $_[0] = $template x $ncount;
  1354.     }
  1355.     }
  1356. }
  1357.