]>
git.saurik.com Git - wxWidgets.git/blob - src/png/ansi2knr.c
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
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.
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
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.
31 ---------- Here is the GNU GPL file COPYLEFT, referred to above ----------
32 ----- These terms do NOT apply to the LIBPNG software itself; see README ------
34 GHOSTSCRIPT GENERAL PUBLIC LICENSE
35 (Clarified 11 Feb 1988)
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.
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.
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.
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.
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.
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.
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.
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:
88 a) cause the modified files to carry prominent notices stating
89 that you changed the files and the date of any change; and
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).
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.
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.
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
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,
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,
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.)
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.
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
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.
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.
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.
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
177 -------------------- End of file COPYLEFT ------------------------------
182 ansi2knr input_file [output_file]
183 * If no output_file is supplied, output goes to stdout.
184 * There are no error messages.
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.
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
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
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
231 /* Most of the conditionals here are to make ansi2knr work with */
232 /* or without the GNU configure machinery. */
244 For properly autoconfiguring ansi2knr, use AC_CONFIG_HEADER(config.h).
245 This will define HAVE_CONFIG_H and so, activate the following lines.
248 # if STDC_HEADERS || HAVE_STRING_H
251 # include <strings.h>
254 #else /* not HAVE_CONFIG_H */
256 /* Otherwise do it the hard way */
259 # include <strings.h>
262 extern int strlen(), strncmp();
268 #endif /* not HAVE_CONFIG_H */
274 malloc and free should be declared in stdlib.h,
275 but if you've got a K&R compiler, they probably aren't.
281 extern char *malloc();
284 extern char *malloc();
292 * The ctype macros don't always handle 8-bit characters correctly.
293 * Compensate for this here.
296 # undef HAVE_ISASCII /* just in case */
297 # define HAVE_ISASCII 1
300 #if STDC_HEADERS || !HAVE_ISASCII
301 # define is_ascii(c) 1
303 # define is_ascii(c) isascii(c)
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))
310 /* Scanning macros */
311 #define isidchar(ch) (is_alnum(ch) || (ch) == '_')
312 #define isidfirstchar(ch) (is_alpha(ch) || (ch) == '_')
314 /* Forward references */
320 /* The main program */
326 #define bufsize 5000 /* arbitrary size */
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.
338 int convert_varargs
= 1;
340 if ( argc
> 1 && argv
[1][0] == '-' )
341 { if ( !strcmp(argv
[1], "--varargs") )
342 { convert_varargs
= 1;
347 { fprintf(stderr
, "Unrecognized switch: %s\n", argv
[1]);
354 printf("Usage: ansi2knr input_file [output_file]\n");
360 out
= fopen(argv
[2], "w");
362 { fprintf(stderr
, "Cannot open output file %s\n", argv
[2]);
366 in
= fopen(argv
[1], "r");
368 { fprintf(stderr
, "Cannot open input file %s\n", argv
[1]);
371 fprintf(out
, "#line 1 \"%s\"\n", argv
[1]);
372 buf
= malloc(bufsize
);
374 while ( fgets(line
, (unsigned)(buf
+ bufsize
- line
), in
) != NULL
)
376 test
: line
+= strlen(line
);
377 switch ( test1(buf
) )
379 case 2: /* a function header */
380 convert1(buf
, out
, 1, convert_varargs
);
382 case 1: /* a function */
383 /* Check for a { at the start of the next line. */
385 f
: if ( line
>= buf
+ (bufsize
- 1) ) /* overflow check */
387 if ( fgets(line
, (unsigned)(buf
+ bufsize
- line
), in
) == NULL
)
389 switch ( *skipspace(more
, 1) )
392 /* Definitely a function header. */
393 convert1(buf
, out
, 0, convert_varargs
);
397 /* The next line was blank or a comment: */
398 /* keep scanning for a non-comment. */
399 line
+= strlen(line
);
402 /* buf isn't a function header, but */
410 case -1: /* maybe the start of a function */
411 if ( line
!= buf
+ (bufsize
- 1) ) /* overflow check */
414 default: /* not a function */
428 /* Skip over space and comments, in either direction. */
432 register int dir
; /* 1 for forward, -1 for backward */
434 { while ( is_space(*p
) )
436 if ( !(*p
== '/' && p
[dir
] == '*') )
439 while ( !(*p
== '*' && p
[dir
] == '/') )
441 return p
; /* multi-line comment?? */
450 * Write blanks over part of a string.
451 * Don't overwrite end-of-line characters.
454 writeblanks(start
, end
)
458 for ( p
= start
; p
< end
; p
++ )
459 if ( *p
!= '\r' && *p
!= '\n' )
465 * Test whether the string in buf is a function definition.
466 * The string may contain and/or end with a newline.
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.
480 { register char *p
= buf
;
485 if ( !isidfirstchar(*p
) )
486 return 0; /* no name at left margin */
487 bend
= skipspace(buf
+ strlen(buf
) - 1, -1);
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;
496 while ( isidchar(*p
) )
501 return 0; /* not a function */
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
517 int len
= endfn
- buf
;
519 while ( (kp
= *key
) != 0 )
520 { if ( strlen(kp
) == len
&& !strncmp(kp
, buf
, len
) )
521 return 0; /* name is a keyword */
528 /* Convert a recognized function definition or header to K&R syntax. */
530 convert1(buf
, out
, header
, convert_varargs
)
533 int header
; /* Boolean */
534 int convert_varargs
; /* Boolean */
538 unsigned num_breaks
= 2; /* for testing */
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
++) != '('; )
549 breaks
= (char **)malloc(sizeof(char *) * num_breaks
* 2);
551 { /* Couldn't allocate break table, give up */
552 fprintf(stderr
, "Unable to allocate break table!\n");
556 btop
= breaks
+ num_breaks
* 2 - 2;
558 /* Parse the argument list */
566 { /* Filled up break table. */
567 /* Allocate a bigger one and start over. */
568 free((char *)breaks
);
573 /* Find the end of the argument */
574 for ( ; end
== NULL
; p
++ )
578 if ( !level
) end
= p
;
581 if ( !level
) lp
= p
;
585 if ( --level
< 0 ) end
= p
;
589 p
= skipspace(p
, 1) - 1;
595 /* Erase any embedded prototype parameters. */
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. */
603 { p
= skipspace(p
- 1, -1);
606 case ']': /* skip array dimension(s) */
607 case ')': /* skip procedure args OR name */
612 case ']': case ')': level
++; break;
613 case '[': case '(': level
--; break;
614 case '/': p
= skipspace(p
, -1) + 1; break;
618 if ( *p
== '(' && *skipspace(p
+ 1, 1) == '*' )
619 { /* We found the name being declared */
620 while ( !isidfirstchar(*p
) )
621 p
= skipspace(p
, 1) + 1;
629 found
: if ( *p
== '.' && p
[-1] == '.' && p
[-2] == '.' )
630 { if ( convert_varargs
)
631 { *bp
++ = "va_alist";
636 if ( bp
== breaks
+ 1 ) /* sole argument */
637 writeblanks(breaks
[0], p
);
639 writeblanks(bp
[-1] - 1, p
);
644 { while ( isidchar(*p
) ) p
--;
649 while ( *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);
662 /* Put out the function name and left parenthesis. */
664 while ( p
!= endfn
) putc(*p
, out
), p
++;
665 /* Put out the declaration. */
668 for ( p
= breaks
[0]; *p
; p
++ )
669 if ( *p
== '\r' || *p
== '\n' )
673 { for ( ap
= breaks
+1; ap
< bp
; ap
+= 2 )
675 while ( isidchar(*p
) )
681 /* Put out the argument declarations */
682 for ( ap
= breaks
+2; ap
<= bp
; ap
+= 2 )
686 fputs(breaks
[0], out
); /* any prior args */
687 fputs("va_dcl", out
); /* the final arg */
691 fputs(breaks
[0], out
);
693 free((char *)breaks
);