home *** CD-ROM | disk | FTP | other *** search
/ Resource Library: Multimedia / Resource Library: Multimedia.iso / _archive / utils / graphics / viewers / jpeg / msdos / jpeg.lzh / ansi2knr.c next >
Encoding:
Text File  |  1991-10-02  |  15.9 KB  |  478 lines

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