home *** CD-ROM | disk | FTP | other *** search
/ Photo CD Demo 1 / Demo.bin / compresn / jpegv3sr / ansi2knr.c next >
Text File  |  1991-12-12  |  16KB  |  481 lines

  1. /*
  2.  * Received from Peter Deutsch (ghost@aladdin.com)
  3.  * Fri, 26 Apr 91 10:10:10 PDT
  4.  * Small portability improvements by Tom Lane
  5.  */
  6.  
  7. /* Copyright (C) 1989, 1991 Aladdin Enterprises.  All rights reserved.
  8.    Distributed by Free Software Foundation, Inc.
  9.  
  10. This file is part of Ghostscript.
  11.  
  12. Ghostscript is distributed in the hope that it will be useful, but
  13. WITHOUT ANY WARRANTY.  No author or distributor accepts responsibility
  14. to anyone for the consequences of using it or for whether it serves any
  15. particular purpose or works at all, unless he says so in writing.  Refer
  16. to the Ghostscript General Public License for full details.
  17.  
  18. Everyone is granted permission to copy, modify and redistribute
  19. Ghostscript, but only under the conditions described in the Ghostscript
  20. General Public License.  A copy of this license is supposed to have been
  21. given to you along with Ghostscript so you can know your rights and
  22. responsibilities.  It should be in a file named COPYING.  Among other
  23. things, the copyright notice and this notice must be preserved on all
  24. copies.  */
  25.  
  26. /*
  27. ---------- Here is the GhostScript file COPYING, referred to above ----------
  28. ----- These terms do NOT apply to the JPEG software itself; see README ------
  29.  
  30.             GHOSTSCRIPT GENERAL PUBLIC LICENSE
  31.             (Clarified 11 Feb 1988)
  32.  
  33.  Copyright (C) 1988 Richard M. Stallman
  34.  Everyone is permitted to copy and distribute verbatim copies of this
  35.  license, but changing it is not allowed.  You can also use this wording
  36.  to make the terms for other programs.
  37.  
  38.   The license agreements of most software companies keep you at the
  39. mercy of those companies.  By contrast, our general public license is
  40. intended to give everyone the right to share Ghostscript.  To make sure
  41. that you get the rights we want you to have, we need to make
  42. restrictions that forbid anyone to deny you these rights or to ask you
  43. to surrender the rights.  Hence this license agreement.
  44.  
  45.   Specifically, we want to make sure that you have the right to give
  46. away copies of Ghostscript, that you receive source code or else can get
  47. it if you want it, that you can change Ghostscript or use pieces of it
  48. in new free programs, and that you know you can do these things.
  49.  
  50.   To make sure that everyone has such rights, we have to forbid you to
  51. deprive anyone else of these rights.  For example, if you distribute
  52. copies of Ghostscript, you must give the recipients all the rights that
  53. you have.  You must make sure that they, too, receive or can get the
  54. source code.  And you must tell them their rights.
  55.  
  56.   Also, for our own protection, we must make certain that everyone finds
  57. out that there is no warranty for Ghostscript.  If Ghostscript is
  58. modified by someone else and passed on, we want its recipients to know
  59. that what they have is not what we distributed, so that any problems
  60. introduced by others will not reflect on our reputation.
  61.  
  62.   Therefore we (Richard M. Stallman and the Free Software Foundation,
  63. Inc.) make the following terms which say what you must do to be allowed
  64. to distribute or change Ghostscript.
  65.  
  66.  
  67.             COPYING POLICIES
  68.  
  69.   1. You may copy and distribute verbatim copies of Ghostscript source
  70. code as you receive it, in any medium, provided that you conspicuously
  71. and appropriately publish on each copy a valid copyright and license
  72. notice "Copyright (C) 1989 Aladdin Enterprises.  All rights reserved.
  73. Distributed by Free Software Foundation, Inc." (or with whatever year is
  74. appropriate); keep intact the notices on all files that refer to this
  75. License Agreement and to the absence of any warranty; and give any other
  76. recipients of the Ghostscript program a copy of this License Agreement
  77. along with the program.  You may charge a distribution fee for the
  78. physical act of transferring a copy.
  79.  
  80.   2. You may modify your copy or copies of Ghostscript or any portion of
  81. it, and copy and distribute such modifications under the terms of
  82. Paragraph 1 above, provided that you also do the following:
  83.  
  84.     a) cause the modified files to carry prominent notices stating
  85.     that you changed the files and the date of any change; and
  86.  
  87.     b) cause the whole of any work that you distribute or publish,
  88.     that in whole or in part contains or is a derivative of Ghostscript
  89.     or any part thereof, to be licensed at no charge to all third
  90.     parties on terms identical to those contained in this License
  91.     Agreement (except that you may choose to grant more extensive
  92.     warranty protection to some or all third parties, at your option).
  93.  
  94.     c) You may charge a distribution fee for the physical act of
  95.     transferring a copy, and you may at your option offer warranty
  96.     protection in exchange for a fee.
  97.  
  98. Mere aggregation of another unrelated program with this program (or its
  99. derivative) on a volume of a storage or distribution medium does not bring
  100. the other program under the scope of these terms.
  101.  
  102.   3. You may copy and distribute Ghostscript (or a portion or derivative
  103. of it, under Paragraph 2) in object code or executable form under the
  104. terms of Paragraphs 1 and 2 above provided that you also do one of the
  105. following:
  106.  
  107.     a) accompany it with the complete corresponding machine-readable
  108.     source code, which must be distributed under the terms of
  109.     Paragraphs 1 and 2 above; or,
  110.  
  111.     b) accompany it with a written offer, valid for at least three
  112.     years, to give any third party free (except for a nominal
  113.     shipping charge) a complete machine-readable copy of the
  114.     corresponding source code, to be distributed under the terms of
  115.     Paragraphs 1 and 2 above; or,
  116.  
  117.     c) accompany it with the information you received as to where the
  118.     corresponding source code may be obtained.  (This alternative is
  119.     allowed only for noncommercial distribution and only if you
  120.     received the program in object code or executable form alone.)
  121.  
  122. For an executable file, complete source code means all the source code for
  123. all modules it contains; but, as a special exception, it need not include
  124. source code for modules which are standard libraries that accompany the
  125. operating system on which the executable file runs.
  126.  
  127.   4. You may not copy, sublicense, distribute or transfer Ghostscript
  128. except as expressly provided under this License Agreement.  Any attempt
  129. otherwise to copy, sublicense, distribute or transfer Ghostscript is
  130. void and your rights to use the program under this License agreement
  131. shall be automatically terminated.  However, parties who have received
  132. computer software programs from you with this License Agreement will not
  133. have their licenses terminated so long as such parties remain in full
  134. compliance.
  135.  
  136.   5. If you wish to incorporate parts of Ghostscript into other free
  137. programs whose distribution conditions are different, write to the Free
  138. Software Foundation at 675 Mass Ave, Cambridge, MA 02139.  We have not
  139. yet worked out a simple rule that can be stated here, but we will often
  140. permit this.  We will be guided by the two goals of preserving the free
  141. status of all derivatives of our free software and of promoting the
  142. sharing and reuse of software.
  143.  
  144. Your comments and suggestions about our licensing policies and our
  145. software are welcome!  Please contact the Free Software Foundation,
  146. Inc., 675 Mass Ave, Cambridge, MA 02139, or call (617) 876-3296.
  147.  
  148.                NO WARRANTY
  149.  
  150.   BECAUSE GHOSTSCRIPT IS LICENSED FREE OF CHARGE, WE PROVIDE ABSOLUTELY
  151. NO WARRANTY, TO THE EXTENT PERMITTED BY APPLICABLE STATE LAW.  EXCEPT
  152. WHEN OTHERWISE STATED IN WRITING, FREE SOFTWARE FOUNDATION, INC, RICHARD
  153. M. STALLMAN, ALADDIN ENTERPRISES, L. PETER DEUTSCH, AND/OR OTHER PARTIES
  154. PROVIDE GHOSTSCRIPT "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER
  155. EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  156. WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.  THE
  157. ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF GHOSTSCRIPT IS WITH
  158. YOU.  SHOULD GHOSTSCRIPT PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL
  159. NECESSARY SERVICING, REPAIR OR CORRECTION.
  160.  
  161.   IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW WILL RICHARD M.
  162. STALLMAN, THE FREE SOFTWARE FOUNDATION, INC., L. PETER DEUTSCH, ALADDIN
  163. ENTERPRISES, AND/OR ANY OTHER PARTY WHO MAY MODIFY AND REDISTRIBUTE
  164. GHOSTSCRIPT AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING
  165. ANY LOST PROFITS, LOST MONIES, OR OTHER SPECIAL, INCIDENTAL OR
  166. CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE
  167. (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING RENDERED
  168. INACCURATE OR LOSSES SUSTAINED BY THIRD PARTIES OR A FAILURE OF THE
  169. PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS) GHOSTSCRIPT, EVEN IF YOU
  170. HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES, OR FOR ANY CLAIM
  171. BY ANY OTHER PARTY.
  172. -------------------- End of file COPYING ------------------------------
  173. */
  174.  
  175.  
  176. /* ansi2knr.c */
  177. /* Convert ANSI function declarations to K&R syntax */
  178.  
  179. #include <stdio.h>
  180. #include <ctype.h>
  181.  
  182. #ifdef BSD
  183. #include <strings.h>
  184. #define strchr index
  185. #else
  186. #ifdef VMS
  187.     extern char *strcat(), *strchr(), *strcpy(), *strupr();
  188.     extern int strcmp(), strlen(), strncmp();
  189. #else
  190. #include <string.h>
  191. #endif
  192. #endif
  193.  
  194. #ifdef MSDOS
  195. #include <malloc.h>
  196. #else
  197. #ifdef VMS
  198.      extern char *malloc();
  199.      extern void free();
  200. #else
  201.      extern char *malloc();
  202.      extern int free();
  203. #endif
  204. #endif
  205.  
  206. /* Usage:
  207.     ansi2knr input_file output_file
  208.  * If no output_file is supplied, output goes to stdout.
  209.  * There are no error messages.
  210.  *
  211.  * ansi2knr recognizes functions by seeing a non-keyword identifier
  212.  * at the left margin, followed by a left parenthesis,
  213.  * with a right parenthesis as the last character on the line.
  214.  * It will recognize a multi-line header if the last character
  215.  * on each line but the last is a left parenthesis or comma.
  216.  * These algorithms ignore whitespace and comments, except that
  217.  * the function name must be the first thing on the line.
  218.  * The following constructs will confuse it:
  219.     - Any other construct that starts at the left margin and
  220.         follows the above syntax (such as a macro or function call).
  221.     - Macros that tinker with the syntax of the function header.
  222.  */
  223.  
  224. /* Scanning macros */
  225. #define isidchar(ch) (isalnum(ch) || (ch) == '_')
  226. #define isidfirstchar(ch) (isalpha(ch) || (ch) == '_')
  227.  
  228. int
  229. main(argc, argv)
  230.     int argc;
  231.     char *argv[];
  232. {    FILE *in, *out;
  233. #define bufsize 500            /* arbitrary size */
  234.     char buf[bufsize+1];
  235.     char *line;
  236.     switch ( argc )
  237.        {
  238.     default:
  239.         printf("Usage: ansi2knr input_file [output_file]\n");
  240.         exit(0);
  241.     case 2:
  242.         out = stdout; break;
  243.     case 3:
  244.         out = fopen(argv[2], "w");
  245.         if ( out == NULL )
  246.            {    fprintf(stderr, "Cannot open %s\n", argv[2]);
  247.             exit(1);
  248.            }
  249.        }
  250.     in = fopen(argv[1], "r");
  251.     if ( in == NULL )
  252.        {    fprintf(stderr, "Cannot open %s\n", argv[1]);
  253.         exit(1);
  254.        }
  255.     fprintf(out, "#line 1 \"%s\"\n", argv[1]);
  256.     line = buf;
  257.     while ( fgets(line, (unsigned)(buf + bufsize - line), in) != NULL )
  258.        {    switch ( test1(buf) )
  259.            {
  260.         case 1:            /* a function */
  261.             convert1(buf, out);
  262.             break;
  263.         case -1:        /* maybe the start of a function */
  264.             line = buf + strlen(buf);
  265.             continue;
  266.         default:        /* not a function */
  267.             fputs(buf, out);
  268.             break;
  269.            }
  270.         line = buf;
  271.        }
  272.     if ( line != buf ) fputs(buf, out);
  273.     fclose(out);
  274.     fclose(in);
  275.     return 0;
  276. }
  277.  
  278. /* Skip over space and comments, in either direction. */
  279. char *
  280. skipspace(p, dir)
  281.     register char *p;
  282.     register int dir;            /* 1 for forward, -1 for backward */
  283. {    for ( ; ; )
  284.        {    while ( isspace(*p) ) p += dir;
  285.         if ( !(*p == '/' && p[dir] == '*') ) break;
  286.         p += dir;  p += dir;
  287.         while ( !(*p == '*' && p[dir] == '/') )
  288.            {    if ( *p == 0 ) return p;    /* multi-line comment?? */
  289.             p += dir;
  290.            }
  291.         p += dir;  p += dir;
  292.        }
  293.     return p;
  294. }
  295.  
  296. /*
  297.  * Write blanks over part of a string.
  298.  */
  299. int
  300. writeblanks(start, end)
  301.     char *start;
  302.     char *end;
  303. {    char *p;
  304.     for ( p = start; p < end; p++ ) *p = ' ';
  305.     return 0;
  306. }
  307.  
  308. /*
  309.  * Test whether the string in buf is a function definition.
  310.  * The string may contain and/or end with a newline.
  311.  * Return as follows:
  312.  *    0 - definitely not a function definition;
  313.  *    1 - definitely a function definition;
  314.  *    -1 - may be the beginning of a function definition,
  315.  *        append another line and look again.
  316.  */
  317. int
  318. test1(buf)
  319.     char *buf;
  320. {    register char *p = buf;
  321.     char *bend;
  322.     char *endfn;
  323.     int contin;
  324.     if ( !isidfirstchar(*p) )
  325.         return 0;        /* no name at left margin */
  326.     bend = skipspace(buf + strlen(buf) - 1, -1);
  327.     switch ( *bend )
  328.        {
  329.     case ')': contin = 1; break;
  330.     case '(':
  331.     case ',': contin = -1; break;
  332.     default: return 0;        /* not a function */
  333.        }
  334.     while ( isidchar(*p) ) p++;
  335.     endfn = p;
  336.     p = skipspace(p, 1);
  337.     if ( *p++ != '(' )
  338.         return 0;        /* not a function */
  339.     p = skipspace(p, 1);
  340.     if ( *p == ')' )
  341.         return 0;        /* no parameters */
  342.     /* Check that the apparent function name isn't a keyword. */
  343.     /* We only need to check for keywords that could be followed */
  344.     /* by a left parenthesis (which, unfortunately, is most of them). */
  345.        {    static char *words[] =
  346.            {    "asm", "auto", "case", "char", "const", "double",
  347.             "extern", "float", "for", "if", "int", "long",
  348.             "register", "return", "short", "signed", "sizeof",
  349.             "static", "switch", "typedef", "unsigned",
  350.             "void", "volatile", "while", 0
  351.            };
  352.         char **key = words;
  353.         char *kp;
  354.         int len = endfn - buf;
  355.         while ( (kp = *key) != 0 )
  356.            {    if ( strlen(kp) == len && !strncmp(kp, buf, len) )
  357.                 return 0;    /* name is a keyword */
  358.             key++;
  359.            }
  360.        }
  361.     return contin;
  362. }
  363.  
  364. int
  365. convert1(buf, out)
  366.     char *buf;
  367.     FILE *out;
  368. {    char *endfn = strchr(buf, '(') + 1;
  369.     register char *p;
  370.     char **breaks;
  371.     unsigned num_breaks = 2;    /* for testing */
  372.     char **btop;
  373.     char **bp;
  374.     char **ap;
  375. top:    p = endfn;
  376.     breaks = (char **)malloc(sizeof(char *) * num_breaks * 2);
  377.     if ( breaks == 0 )
  378.        {    /* Couldn't allocate break table, give up */
  379.         fprintf(stderr, "Unable to allocate break table!\n");
  380.         fputs(buf, out);
  381.         return -1;
  382.        }
  383.     btop = breaks + num_breaks * 2 - 2;
  384.     bp = breaks;
  385.     /* Parse the argument list */
  386.     do
  387.        {    int level = 0;
  388.         char *end = NULL;
  389.         if ( bp >= btop )
  390.            {    /* Filled up break table. */
  391.             /* Allocate a bigger one and start over. */
  392.             free((char *)breaks);
  393.             num_breaks <<= 1;
  394.             goto top;
  395.            }
  396.         *bp++ = p;
  397.         /* Find the end of the argument */
  398.         for ( ; end == NULL; p++ )
  399.            {    switch(*p)
  400.                {
  401.             case ',': if ( !level ) end = p; break;
  402.             case '(': level++; break;
  403.             case ')': if ( --level < 0 ) end = p; break;
  404.             case '/': p = skipspace(p, 1) - 1; break;
  405.             default: ;
  406.                }
  407.            }
  408.         p--;            /* back up over terminator */
  409.         /* Find the name being declared. */
  410.         /* This is complicated because of procedure and */
  411.         /* array modifiers. */
  412.         for ( ; ; )
  413.            {    p = skipspace(p - 1, -1);
  414.             switch ( *p )
  415.                {
  416.             case ']':    /* skip array dimension(s) */
  417.             case ')':    /* skip procedure args OR name */
  418.                {    int level = 1;
  419.                 while ( level )
  420.                  switch ( *--p )
  421.                    {
  422.                 case ']': case ')': level++; break;
  423.                 case '[': case '(': level--; break;
  424.                 case '/': p = skipspace(p, -1) + 1; break;
  425.                 default: ;
  426.                    }
  427.                }
  428.                 if ( *p == '(' && *skipspace(p + 1, 1) == '*' )
  429.                    {    /* We found the name being declared */
  430.                     while ( !isidfirstchar(*p) )
  431.                         p = skipspace(p, 1) + 1;
  432.                     goto found;
  433.                    }
  434.                 break;
  435.             default: goto found;
  436.                }
  437.            }
  438. found:        if ( *p == '.' && p[-1] == '.' && p[-2] == '.' )
  439.            {    p++;
  440.             if ( bp == breaks + 1 )    /* sole argument */
  441.                 writeblanks(breaks[0], p);
  442.             else
  443.                 writeblanks(bp[-1] - 1, p);
  444.             bp--;
  445.            }
  446.         else
  447.            {    while ( isidchar(*p) ) p--;
  448.             *bp++ = p+1;
  449.            }
  450.         p = end;
  451.        }
  452.     while ( *p++ == ',' );
  453.     *bp = p;
  454.     /* Make a special check for 'void' arglist */
  455.     if ( bp == breaks+2 )
  456.        {    p = skipspace(breaks[0], 1);
  457.         if ( !strncmp(p, "void", 4) )
  458.            {    p = skipspace(p+4, 1);
  459.             if ( p == breaks[2] - 1 )
  460.                {    bp = breaks;    /* yup, pretend arglist is empty */
  461.                 writeblanks(breaks[0], p + 1);
  462.                }
  463.            }
  464.        }
  465.     /* Put out the function name */
  466.     p = buf;
  467.     while ( p != endfn ) putc(*p, out), p++;
  468.     /* Put out the declaration */
  469.     for ( ap = breaks+1; ap < bp; ap += 2 )
  470.        {    p = *ap;
  471.         while ( isidchar(*p) ) putc(*p, out), p++;
  472.         if ( ap < bp - 1 ) fputs(", ", out);
  473.        }
  474.     fputs(")  ", out);
  475.     /* Put out the argument declarations */
  476.     for ( ap = breaks+2; ap <= bp; ap += 2 ) (*ap)[-1] = ';';
  477.     fputs(breaks[0], out);
  478.     free((char *)breaks);
  479.     return 0;
  480. }
  481.