home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / wxos2233.zip / wxOS2-2_3_3.zip / wxWindows-2.3.3 / src / png / ansi2knr.c < prev    next >
Text File  |  1999-04-17  |  23KB  |  696 lines

  1. /* ansi2knr.c */
  2.  
  3. /* Convert ANSI C function definitions to K&R ("traditional C") syntax
  4. Copyright (C) 1989 Aladdin Enterprises.  All rights reserved.
  5. Copyright (C) 1988 Richard M. Stallman
  6.  
  7. ansi2knr is distributed in the hope that it will be useful, but WITHOUT ANY
  8. WARRANTY.  No author or distributor accepts responsibility to anyone for the
  9. consequences of using it or for whether it serves any particular purpose or
  10. works at all, unless he says so in writing.  Refer to the GNU General Public
  11. License (the "GPL") for full details.
  12.  
  13. Everyone is granted permission to copy, modify and redistribute ansi2knr,
  14. but only under the conditions described in the GPL.  A copy of this license
  15. is supposed to have been given to you along with ansi2knr so you can know
  16. your rights and responsibilities.  It should be in a file named COPYLEFT.
  17. [In the LIBPNG distribution, the GPL appears below, not in a separate file.]
  18. Among other things, the copyright notice and this notice must be preserved
  19. on all copies.
  20.  
  21. We explicitly state here what we believe is already implied by the GPL: if
  22. the ansi2knr program is distributed as a separate source file and a
  23. separate executable file which are aggregated on a storage medium together
  24. with another program, this in itself does not bring the other program under
  25. the GPL, nor does the mere fact that such a program or the procedures for
  26. constructing it invoke the ansi2knr executable bring any other part of the
  27. program under the GPL.
  28. */
  29.  
  30. /*
  31. ---------- Here is the GNU GPL file COPYLEFT, referred to above ----------
  32. ----- These terms do NOT apply to the LIBPNG software itself; see README ------
  33.  
  34.             GHOSTSCRIPT GENERAL PUBLIC LICENSE
  35.             (Clarified 11 Feb 1988)
  36.  
  37.  Copyright (C) 1988 Richard M. Stallman
  38.  Everyone is permitted to copy and distribute verbatim copies of this
  39.  license, but changing it is not allowed.  You can also use this wording
  40.  to make the terms for other programs.
  41.  
  42.   The license agreements of most software companies keep you at the
  43. mercy of those companies.  By contrast, our general public license is
  44. intended to give everyone the right to share Ghostscript.  To make sure
  45. that you get the rights we want you to have, we need to make
  46. restrictions that forbid anyone to deny you these rights or to ask you
  47. to surrender the rights.  Hence this license agreement.
  48.  
  49.   Specifically, we want to make sure that you have the right to give
  50. away copies of Ghostscript, that you receive source code or else can get
  51. it if you want it, that you can change Ghostscript or use pieces of it
  52. in new free programs, and that you know you can do these things.
  53.  
  54.   To make sure that everyone has such rights, we have to forbid you to
  55. deprive anyone else of these rights.  For example, if you distribute
  56. copies of Ghostscript, you must give the recipients all the rights that
  57. you have.  You must make sure that they, too, receive or can get the
  58. source code.  And you must tell them their rights.
  59.  
  60.   Also, for our own protection, we must make certain that everyone finds
  61. out that there is no warranty for Ghostscript.  If Ghostscript is
  62. modified by someone else and passed on, we want its recipients to know
  63. that what they have is not what we distributed, so that any problems
  64. introduced by others will not reflect on our reputation.
  65.  
  66.   Therefore we (Richard M. Stallman and the Free Software Foundation,
  67. Inc.) make the following terms which say what you must do to be allowed
  68. to distribute or change Ghostscript.
  69.  
  70.  
  71.             COPYING POLICIES
  72.  
  73.   1. You may copy and distribute verbatim copies of Ghostscript source
  74. code as you receive it, in any medium, provided that you conspicuously
  75. and appropriately publish on each copy a valid copyright and license
  76. notice "Copyright (C) 1989 Aladdin Enterprises.  All rights reserved.
  77. Distributed by Free Software Foundation, Inc." (or with whatever year is
  78. appropriate); keep intact the notices on all files that refer to this
  79. License Agreement and to the absence of any warranty; and give any other
  80. recipients of the Ghostscript program a copy of this License Agreement
  81. along with the program.  You may charge a distribution fee for the
  82. physical act of transferring a copy.
  83.  
  84.   2. You may modify your copy or copies of Ghostscript or any portion of
  85. it, and copy and distribute such modifications under the terms of
  86. Paragraph 1 above, provided that you also do the following:
  87.  
  88.     a) cause the modified files to carry prominent notices stating
  89.     that you changed the files and the date of any change; and
  90.  
  91.     b) cause the whole of any work that you distribute or publish,
  92.     that in whole or in part contains or is a derivative of Ghostscript
  93.     or any part thereof, to be licensed at no charge to all third
  94.     parties on terms identical to those contained in this License
  95.     Agreement (except that you may choose to grant more extensive
  96.     warranty protection to some or all third parties, at your option).
  97.  
  98.     c) You may charge a distribution fee for the physical act of
  99.     transferring a copy, and you may at your option offer warranty
  100.     protection in exchange for a fee.
  101.  
  102. Mere aggregation of another unrelated program with this program (or its
  103. derivative) on a volume of a storage or distribution medium does not bring
  104. the other program under the scope of these terms.
  105.  
  106.   3. You may copy and distribute Ghostscript (or a portion or derivative
  107. of it, under Paragraph 2) in object code or executable form under the
  108. terms of Paragraphs 1 and 2 above provided that you also do one of the
  109. following:
  110.  
  111.     a) accompany it with the complete corresponding machine-readable
  112.     source code, which must be distributed under the terms of
  113.     Paragraphs 1 and 2 above; or,
  114.  
  115.     b) accompany it with a written offer, valid for at least three
  116.     years, to give any third party free (except for a nominal
  117.     shipping charge) a complete machine-readable copy of the
  118.     corresponding source code, to be distributed under the terms of
  119.     Paragraphs 1 and 2 above; or,
  120.  
  121.     c) accompany it with the information you received as to where the
  122.     corresponding source code may be obtained.  (This alternative is
  123.     allowed only for noncommercial distribution and only if you
  124.     received the program in object code or executable form alone.)
  125.  
  126. For an executable file, complete source code means all the source code for
  127. all modules it contains; but, as a special exception, it need not include
  128. source code for modules which are standard libraries that accompany the
  129. operating system on which the executable file runs.
  130.  
  131.   4. You may not copy, sublicense, distribute or transfer Ghostscript
  132. except as expressly provided under this License Agreement.  Any attempt
  133. otherwise to copy, sublicense, distribute or transfer Ghostscript is
  134. void and your rights to use the program under this License agreement
  135. shall be automatically terminated.  However, parties who have received
  136. computer software programs from you with this License Agreement will not
  137. have their licenses terminated so long as such parties remain in full
  138. compliance.
  139.  
  140.   5. If you wish to incorporate parts of Ghostscript into other free
  141. programs whose distribution conditions are different, write to the Free
  142. Software Foundation at 675 Mass Ave, Cambridge, MA 02139.  We have not
  143. yet worked out a simple rule that can be stated here, but we will often
  144. permit this.  We will be guided by the two goals of preserving the free
  145. status of all derivatives of our free software and of promoting the
  146. sharing and reuse of software.
  147.  
  148. Your comments and suggestions about our licensing policies and our
  149. software are welcome!  Please contact the Free Software Foundation,
  150. Inc., 675 Mass Ave, Cambridge, MA 02139, or call (617) 876-3296.
  151.  
  152.                NO WARRANTY
  153.  
  154.   BECAUSE GHOSTSCRIPT IS LICENSED FREE OF CHARGE, WE PROVIDE ABSOLUTELY
  155. NO WARRANTY, TO THE EXTENT PERMITTED BY APPLICABLE STATE LAW.  EXCEPT
  156. WHEN OTHERWISE STATED IN WRITING, FREE SOFTWARE FOUNDATION, INC, RICHARD
  157. M. STALLMAN, ALADDIN ENTERPRISES, L. PETER DEUTSCH, AND/OR OTHER PARTIES
  158. PROVIDE GHOSTSCRIPT "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER
  159. EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  160. WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.  THE
  161. ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF GHOSTSCRIPT IS WITH
  162. YOU.  SHOULD GHOSTSCRIPT PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL
  163. NECESSARY SERVICING, REPAIR OR CORRECTION.
  164.  
  165.   IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW WILL RICHARD M.
  166. STALLMAN, THE FREE SOFTWARE FOUNDATION, INC., L. PETER DEUTSCH, ALADDIN
  167. ENTERPRISES, AND/OR ANY OTHER PARTY WHO MAY MODIFY AND REDISTRIBUTE
  168. GHOSTSCRIPT AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING
  169. ANY LOST PROFITS, LOST MONIES, OR OTHER SPECIAL, INCIDENTAL OR
  170. CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE
  171. (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING RENDERED
  172. INACCURATE OR LOSSES SUSTAINED BY THIRD PARTIES OR A FAILURE OF THE
  173. PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS) GHOSTSCRIPT, EVEN IF YOU
  174. HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES, OR FOR ANY CLAIM
  175. BY ANY OTHER PARTY.
  176.  
  177. -------------------- End of file COPYLEFT ------------------------------
  178. */
  179.  
  180. /*
  181.  * Usage:
  182.     ansi2knr input_file [output_file]
  183.  * If no output_file is supplied, output goes to stdout.
  184.  * There are no error messages.
  185.  *
  186.  * ansi2knr recognizes function definitions by seeing a non-keyword
  187.  * identifier at the left margin, followed by a left parenthesis,
  188.  * with a right parenthesis as the last character on the line,
  189.  * and with a left brace as the first token on the following line
  190.  * (ignoring possible intervening comments).
  191.  * It will recognize a multi-line header provided that no intervening
  192.  * line ends with a left or right brace or a semicolon.
  193.  * These algorithms ignore whitespace and comments, except that
  194.  * the function name must be the first thing on the line.
  195.  * The following constructs will confuse it:
  196.  *    - Any other construct that starts at the left margin and
  197.  *        follows the above syntax (such as a macro or function call).
  198.  *    - Some macros that tinker with the syntax of the function header.
  199.  */
  200.  
  201. /*
  202.  * The original and principal author of ansi2knr is L. Peter Deutsch
  203.  * <ghost@aladdin.com>.  Other authors are noted in the change history
  204.  * that follows (in reverse chronological order):
  205.     lpd 96-01-21 added code to cope with not HAVE_CONFIG_H and with
  206.         compilers that don't understand void, as suggested by
  207.         Tom Lane
  208.     lpd 96-01-15 changed to require that the first non-comment token
  209.         on the line following a function header be a left brace,
  210.         to reduce sensitivity to macros, as suggested by Tom Lane
  211.         <tgl@sss.pgh.pa.us>
  212.     lpd 95-06-22 removed #ifndefs whose sole purpose was to define
  213.         undefined preprocessor symbols as 0; changed all #ifdefs
  214.         for configuration symbols to #ifs
  215.     lpd 95-04-05 changed copyright notice to make it clear that
  216.         including ansi2knr in a program does not bring the entire
  217.         program under the GPL
  218.     lpd 94-12-18 added conditionals for systems where ctype macros
  219.         don't handle 8-bit characters properly, suggested by
  220.         Francois Pinard <pinard@iro.umontreal.ca>;
  221.         removed --varargs switch (this is now the default)
  222.     lpd 94-10-10 removed CONFIG_BROKETS conditional
  223.     lpd 94-07-16 added some conditionals to help GNU `configure',
  224.         suggested by Francois Pinard <pinard@iro.umontreal.ca>;
  225.         properly erase prototype args in function parameters,
  226.         contributed by Jim Avera <jima@netcom.com>;
  227.         correct error in writeblanks (it shouldn't erase EOLs)
  228.     lpd 89-xx-xx original version
  229.  */
  230.  
  231. /* Most of the conditionals here are to make ansi2knr work with */
  232. /* or without the GNU configure machinery. */
  233.  
  234. #if HAVE_CONFIG_H
  235. # include <config.h>
  236. #endif
  237.  
  238. #include <stdio.h>
  239. #include <ctype.h>
  240.  
  241. #if HAVE_CONFIG_H
  242.  
  243. /*
  244.    For properly autoconfiguring ansi2knr, use AC_CONFIG_HEADER(config.h).
  245.    This will define HAVE_CONFIG_H and so, activate the following lines.
  246.  */
  247.  
  248. # if STDC_HEADERS || HAVE_STRING_H
  249. #  include <string.h>
  250. # else
  251. #  include <strings.h>
  252. # endif
  253.  
  254. #else /* not HAVE_CONFIG_H */
  255.  
  256. /* Otherwise do it the hard way */
  257.  
  258. # ifdef BSD
  259. #  include <strings.h>
  260. # else
  261. #  ifdef VMS
  262.     extern int strlen(), strncmp();
  263. #  else
  264. #   include <string.h>
  265. #  endif
  266. # endif
  267.  
  268. #endif /* not HAVE_CONFIG_H */
  269.  
  270. #if STDC_HEADERS
  271. # include <stdlib.h>
  272. #else
  273. /*
  274.    malloc and free should be declared in stdlib.h,
  275.    but if you've got a K&R compiler, they probably aren't.
  276.  */
  277. # ifdef MSDOS
  278. #  include <malloc.h>
  279. # else
  280. #  ifdef VMS
  281.      extern char *malloc();
  282.      extern void free();
  283. #  else
  284.      extern char *malloc();
  285.      extern int free();
  286. #  endif
  287. # endif
  288.  
  289. #endif
  290.  
  291. /*
  292.  * The ctype macros don't always handle 8-bit characters correctly.
  293.  * Compensate for this here.
  294.  */
  295. #ifdef isascii
  296. #  undef HAVE_ISASCII        /* just in case */
  297. #  define HAVE_ISASCII 1
  298. #else
  299. #endif
  300. #if STDC_HEADERS || !HAVE_ISASCII
  301. #  define is_ascii(c) 1
  302. #else
  303. #  define is_ascii(c) isascii(c)
  304. #endif
  305.  
  306. #define is_space(c) (is_ascii(c) && isspace(c))
  307. #define is_alpha(c) (is_ascii(c) && isalpha(c))
  308. #define is_alnum(c) (is_ascii(c) && isalnum(c))
  309.  
  310. /* Scanning macros */
  311. #define isidchar(ch) (is_alnum(ch) || (ch) == '_')
  312. #define isidfirstchar(ch) (is_alpha(ch) || (ch) == '_')
  313.  
  314. /* Forward references */
  315. char *skipspace();
  316. int writeblanks();
  317. int test1();
  318. int convert1();
  319.  
  320. /* The main program */
  321. int
  322. main(argc, argv)
  323.     int argc;
  324.     char *argv[];
  325. {    FILE *in, *out;
  326. #define bufsize 5000            /* arbitrary size */
  327.     char *buf;
  328.     char *line;
  329.     char *more;
  330.     /*
  331.      * In previous versions, ansi2knr recognized a --varargs switch.
  332.      * If this switch was supplied, ansi2knr would attempt to convert
  333.      * a ... argument to va_alist and va_dcl; if this switch was not
  334.      * supplied, ansi2knr would simply drop any such arguments.
  335.      * Now, ansi2knr always does this conversion, and we only
  336.      * check for this switch for backward compatibility.
  337.      */
  338.     int convert_varargs = 1;
  339.  
  340.     if ( argc > 1 && argv[1][0] == '-' )
  341.       {    if ( !strcmp(argv[1], "--varargs") )
  342.           {    convert_varargs = 1;
  343.             argc--;
  344.             argv++;
  345.           }
  346.         else
  347.           {    fprintf(stderr, "Unrecognized switch: %s\n", argv[1]);
  348.             exit(1);
  349.           }
  350.       }
  351.     switch ( argc )
  352.        {
  353.     default:
  354.         printf("Usage: ansi2knr input_file [output_file]\n");
  355.         exit(0);
  356.     case 2:
  357.         out = stdout;
  358.         break;
  359.     case 3:
  360.         out = fopen(argv[2], "w");
  361.         if ( out == NULL )
  362.            {    fprintf(stderr, "Cannot open output file %s\n", argv[2]);
  363.             exit(1);
  364.            }
  365.        }
  366.     in = fopen(argv[1], "r");
  367.     if ( in == NULL )
  368.        {    fprintf(stderr, "Cannot open input file %s\n", argv[1]);
  369.         exit(1);
  370.        }
  371.     fprintf(out, "#line 1 \"%s\"\n", argv[1]);
  372.     buf = malloc(bufsize);
  373.     line = buf;
  374.     while ( fgets(line, (unsigned)(buf + bufsize - line), in) != NULL )
  375.        {
  376. test:        line += strlen(line);
  377.         switch ( test1(buf) )
  378.            {
  379.         case 2:            /* a function header */
  380.             convert1(buf, out, 1, convert_varargs);
  381.             break;
  382.         case 1:            /* a function */
  383.             /* Check for a { at the start of the next line. */
  384.             more = ++line;
  385. f:            if ( line >= buf + (bufsize - 1) ) /* overflow check */
  386.               goto wl;
  387.             if ( fgets(line, (unsigned)(buf + bufsize - line), in) == NULL )
  388.               goto wl;
  389.             switch ( *skipspace(more, 1) )
  390.               {
  391.               case '{':
  392.                 /* Definitely a function header. */
  393.                 convert1(buf, out, 0, convert_varargs);
  394.                 fputs(more, out);
  395.                 break;
  396.               case 0:
  397.                 /* The next line was blank or a comment: */
  398.                 /* keep scanning for a non-comment. */
  399.                 line += strlen(line);
  400.                 goto f;
  401.               default:
  402.                 /* buf isn't a function header, but */
  403.                 /* more might be. */
  404.                 fputs(buf, out);
  405.                 strcpy(buf, more);
  406.                 line = buf;
  407.                 goto test;
  408.               }
  409.             break;
  410.         case -1:        /* maybe the start of a function */
  411.             if ( line != buf + (bufsize - 1) ) /* overflow check */
  412.               continue;
  413.             /* falls through */
  414.         default:        /* not a function */
  415. wl:            fputs(buf, out);
  416.             break;
  417.            }
  418.         line = buf;
  419.        }
  420.     if ( line != buf )
  421.       fputs(buf, out);
  422.     free(buf);
  423.     fclose(out);
  424.     fclose(in);
  425.     return 0;
  426. }
  427.  
  428. /* Skip over space and comments, in either direction. */
  429. char *
  430. skipspace(p, dir)
  431.     register char *p;
  432.     register int dir;            /* 1 for forward, -1 for backward */
  433. {    for ( ; ; )
  434.        {    while ( is_space(*p) )
  435.           p += dir;
  436.         if ( !(*p == '/' && p[dir] == '*') )
  437.           break;
  438.         p += dir;  p += dir;
  439.         while ( !(*p == '*' && p[dir] == '/') )
  440.            {    if ( *p == 0 )
  441.               return p;    /* multi-line comment?? */
  442.             p += dir;
  443.            }
  444.         p += dir;  p += dir;
  445.        }
  446.     return p;
  447. }
  448.  
  449. /*
  450.  * Write blanks over part of a string.
  451.  * Don't overwrite end-of-line characters.
  452.  */
  453. int
  454. writeblanks(start, end)
  455.     char *start;
  456.     char *end;
  457. {    char *p;
  458.     for ( p = start; p < end; p++ )
  459.       if ( *p != '\r' && *p != '\n' )
  460.         *p = ' ';
  461.     return 0;
  462. }
  463.  
  464. /*
  465.  * Test whether the string in buf is a function definition.
  466.  * The string may contain and/or end with a newline.
  467.  * Return as follows:
  468.  *    0 - definitely not a function definition;
  469.  *    1 - definitely a function definition;
  470.  *    2 - definitely a function prototype (NOT USED);
  471.  *    -1 - may be the beginning of a function definition,
  472.  *        append another line and look again.
  473.  * The reason we don't attempt to convert function prototypes is that
  474.  * Ghostscript's declaration-generating macros look too much like
  475.  * prototypes, and confuse the algorithms.
  476.  */
  477. int
  478. test1(buf)
  479.     char *buf;
  480. {    register char *p = buf;
  481.     char *bend;
  482.     char *endfn;
  483.     int contin;
  484.  
  485.     if ( !isidfirstchar(*p) )
  486.       return 0;        /* no name at left margin */
  487.     bend = skipspace(buf + strlen(buf) - 1, -1);
  488.     switch ( *bend )
  489.        {
  490.        case ';': contin = 0 /*2*/; break;
  491.        case ')': contin = 1; break;
  492.        case '{': return 0;        /* not a function */
  493.        case '}': return 0;        /* not a function */
  494.        default: contin = -1;
  495.        }
  496.     while ( isidchar(*p) )
  497.       p++;
  498.     endfn = p;
  499.     p = skipspace(p, 1);
  500.     if ( *p++ != '(' )
  501.       return 0;        /* not a function */
  502.     p = skipspace(p, 1);
  503.     if ( *p == ')' )
  504.       return 0;        /* no parameters */
  505.     /* Check that the apparent function name isn't a keyword. */
  506.     /* We only need to check for keywords that could be followed */
  507.     /* by a left parenthesis (which, unfortunately, is most of them). */
  508.        {    static char *words[] =
  509.            {    "asm", "auto", "case", "char", "const", "double",
  510.             "extern", "float", "for", "if", "int", "long",
  511.             "register", "return", "short", "signed", "sizeof",
  512.             "static", "switch", "typedef", "unsigned",
  513.             "void", "volatile", "while", 0
  514.            };
  515.         char **key = words;
  516.         char *kp;
  517.         int len = endfn - buf;
  518.  
  519.         while ( (kp = *key) != 0 )
  520.            {    if ( strlen(kp) == len && !strncmp(kp, buf, len) )
  521.               return 0;    /* name is a keyword */
  522.             key++;
  523.            }
  524.        }
  525.     return contin;
  526. }
  527.  
  528. /* Convert a recognized function definition or header to K&R syntax. */
  529. int
  530. convert1(buf, out, header, convert_varargs)
  531.     char *buf;
  532.     FILE *out;
  533.     int header;            /* Boolean */
  534.     int convert_varargs;    /* Boolean */
  535. {    char *endfn;
  536.     register char *p;
  537.     char **breaks;
  538.     unsigned num_breaks = 2;    /* for testing */
  539.     char **btop;
  540.     char **bp;
  541.     char **ap;
  542.     char *vararg = 0;
  543.  
  544.     /* Pre-ANSI implementations don't agree on whether strchr */
  545.     /* is called strchr or index, so we open-code it here. */
  546.     for ( endfn = buf; *(endfn++) != '('; )
  547.       ;
  548. top:    p = endfn;
  549.     breaks = (char **)malloc(sizeof(char *) * num_breaks * 2);
  550.     if ( breaks == 0 )
  551.        {    /* Couldn't allocate break table, give up */
  552.         fprintf(stderr, "Unable to allocate break table!\n");
  553.         fputs(buf, out);
  554.         return -1;
  555.        }
  556.     btop = breaks + num_breaks * 2 - 2;
  557.     bp = breaks;
  558.     /* Parse the argument list */
  559.     do
  560.        {    int level = 0;
  561.         char *lp = NULL;
  562.         char *rp;
  563.         char *end = NULL;
  564.  
  565.         if ( bp >= btop )
  566.            {    /* Filled up break table. */
  567.             /* Allocate a bigger one and start over. */
  568.             free((char *)breaks);
  569.             num_breaks <<= 1;
  570.             goto top;
  571.            }
  572.         *bp++ = p;
  573.         /* Find the end of the argument */
  574.         for ( ; end == NULL; p++ )
  575.            {    switch(*p)
  576.                {
  577.                case ',':
  578.                 if ( !level ) end = p;
  579.                 break;
  580.                case '(':
  581.                 if ( !level ) lp = p;
  582.                 level++;
  583.                 break;
  584.                case ')':
  585.                 if ( --level < 0 ) end = p;
  586.                 else rp = p;
  587.                 break;
  588.                case '/':
  589.                 p = skipspace(p, 1) - 1;
  590.                 break;
  591.                default:
  592.                 ;
  593.                }
  594.            }
  595.         /* Erase any embedded prototype parameters. */
  596.         if ( lp )
  597.           writeblanks(lp + 1, rp);
  598.         p--;            /* back up over terminator */
  599.         /* Find the name being declared. */
  600.         /* This is complicated because of procedure and */
  601.         /* array modifiers. */
  602.         for ( ; ; )
  603.            {    p = skipspace(p - 1, -1);
  604.             switch ( *p )
  605.                {
  606.                case ']':    /* skip array dimension(s) */
  607.                case ')':    /* skip procedure args OR name */
  608.                {    int level = 1;
  609.                 while ( level )
  610.                  switch ( *--p )
  611.                    {
  612.                    case ']': case ')': level++; break;
  613.                    case '[': case '(': level--; break;
  614.                    case '/': p = skipspace(p, -1) + 1; break;
  615.                    default: ;
  616.                    }
  617.                }
  618.                 if ( *p == '(' && *skipspace(p + 1, 1) == '*' )
  619.                    {    /* We found the name being declared */
  620.                     while ( !isidfirstchar(*p) )
  621.                       p = skipspace(p, 1) + 1;
  622.                     goto found;
  623.                    }
  624.                 break;
  625.                default:
  626.                 goto found;
  627.                }
  628.            }
  629. found:        if ( *p == '.' && p[-1] == '.' && p[-2] == '.' )
  630.           {    if ( convert_varargs )
  631.               {    *bp++ = "va_alist";
  632.                 vararg = p-2;
  633.               }
  634.             else
  635.               {    p++;
  636.                 if ( bp == breaks + 1 )    /* sole argument */
  637.                   writeblanks(breaks[0], p);
  638.                 else
  639.                   writeblanks(bp[-1] - 1, p);
  640.                 bp--;
  641.               }
  642.            }
  643.         else
  644.            {    while ( isidchar(*p) ) p--;
  645.             *bp++ = p+1;
  646.            }
  647.         p = end;
  648.        }
  649.     while ( *p++ == ',' );
  650.     *bp = p;
  651.     /* Make a special check for 'void' arglist */
  652.     if ( bp == breaks+2 )
  653.        {    p = skipspace(breaks[0], 1);
  654.         if ( !strncmp(p, "void", 4) )
  655.            {    p = skipspace(p+4, 1);
  656.             if ( p == breaks[2] - 1 )
  657.                {    bp = breaks;    /* yup, pretend arglist is empty */
  658.                 writeblanks(breaks[0], p + 1);
  659.                }
  660.            }
  661.        }
  662.     /* Put out the function name and left parenthesis. */
  663.     p = buf;
  664.     while ( p != endfn ) putc(*p, out), p++;
  665.     /* Put out the declaration. */
  666.     if ( header )
  667.       {    fputs(");", out);
  668.         for ( p = breaks[0]; *p; p++ )
  669.           if ( *p == '\r' || *p == '\n' )
  670.             putc(*p, out);
  671.       }
  672.     else
  673.       {    for ( ap = breaks+1; ap < bp; ap += 2 )
  674.           {    p = *ap;
  675.             while ( isidchar(*p) )
  676.               putc(*p, out), p++;
  677.             if ( ap < bp - 1 )
  678.               fputs(", ", out);
  679.           }
  680.         fputs(")  ", out);
  681.         /* Put out the argument declarations */
  682.         for ( ap = breaks+2; ap <= bp; ap += 2 )
  683.           (*ap)[-1] = ';';
  684.         if ( vararg != 0 )
  685.           {    *vararg = 0;
  686.             fputs(breaks[0], out);        /* any prior args */
  687.             fputs("va_dcl", out);        /* the final arg */
  688.             fputs(bp[0], out);
  689.           }
  690.         else
  691.           fputs(breaks[0], out);
  692.       }
  693.     free((char *)breaks);
  694.     return 0;
  695. }
  696.