2 /* Convert ANSI C function definitions to K&R ("traditional C") syntax */
5 ansi2knr is distributed in the hope that it will be useful, but WITHOUT ANY
6 WARRANTY. No author or distributor accepts responsibility to anyone for the
7 consequences of using it or for whether it serves any particular purpose or
8 works at all, unless he says so in writing. Refer to the GNU General Public
9 License (the "GPL") for full details.
11 Everyone is granted permission to copy, modify and redistribute ansi2knr,
12 but only under the conditions described in the GPL. A copy of this license
13 is supposed to have been given to you along with ansi2knr so you can know
14 your rights and responsibilities. It should be in a file named COPYLEFT.
15 [In the IJG distribution, the GPL appears below, not in a separate file.]
16 Among other things, the copyright notice and this notice must be preserved
19 We explicitly state here what we believe is already implied by the GPL: if
20 the ansi2knr program is distributed as a separate set of sources and a
21 separate executable file which are aggregated on a storage medium together
22 with another program, this in itself does not bring the other program under
23 the GPL, nor does the mere fact that such a program or the procedures for
24 constructing it invoke the ansi2knr executable bring any other part of the
25 program under the GPL.
29 ---------- Here is the GNU GPL file COPYLEFT, referred to above ----------
30 ----- These terms do NOT apply to the JPEG software itself; see README ------
32 GHOSTSCRIPT GENERAL PUBLIC LICENSE
33 (Clarified 11 Feb 1988)
35 Copyright (C) 1988 Richard M. Stallman
36 Everyone is permitted to copy and distribute verbatim copies of this
37 license, but changing it is not allowed. You can also use this wording
38 to make the terms for other programs.
40 The license agreements of most software companies keep you at the
41 mercy of those companies. By contrast, our general public license is
42 intended to give everyone the right to share Ghostscript. To make sure
43 that you get the rights we want you to have, we need to make
44 restrictions that forbid anyone to deny you these rights or to ask you
45 to surrender the rights. Hence this license agreement.
47 Specifically, we want to make sure that you have the right to give
48 away copies of Ghostscript, that you receive source code or else can get
49 it if you want it, that you can change Ghostscript or use pieces of it
50 in new free programs, and that you know you can do these things.
52 To make sure that everyone has such rights, we have to forbid you to
53 deprive anyone else of these rights. For example, if you distribute
54 copies of Ghostscript, you must give the recipients all the rights that
55 you have. You must make sure that they, too, receive or can get the
56 source code. And you must tell them their rights.
58 Also, for our own protection, we must make certain that everyone finds
59 out that there is no warranty for Ghostscript. If Ghostscript is
60 modified by someone else and passed on, we want its recipients to know
61 that what they have is not what we distributed, so that any problems
62 introduced by others will not reflect on our reputation.
64 Therefore we (Richard M. Stallman and the Free Software Foundation,
65 Inc.) make the following terms which say what you must do to be allowed
66 to distribute or change Ghostscript.
71 1. You may copy and distribute verbatim copies of Ghostscript source
72 code as you receive it, in any medium, provided that you conspicuously
73 and appropriately publish on each copy a valid copyright and license
74 notice "Copyright (C) 1989 Aladdin Enterprises. All rights reserved.
75 Distributed by Free Software Foundation, Inc." (or with whatever year is
76 appropriate); keep intact the notices on all files that refer to this
77 License Agreement and to the absence of any warranty; and give any other
78 recipients of the Ghostscript program a copy of this License Agreement
79 along with the program. You may charge a distribution fee for the
80 physical act of transferring a copy.
82 2. You may modify your copy or copies of Ghostscript or any portion of
83 it, and copy and distribute such modifications under the terms of
84 Paragraph 1 above, provided that you also do the following:
86 a) cause the modified files to carry prominent notices stating
87 that you changed the files and the date of any change; and
89 b) cause the whole of any work that you distribute or publish,
90 that in whole or in part contains or is a derivative of Ghostscript
91 or any part thereof, to be licensed at no charge to all third
92 parties on terms identical to those contained in this License
93 Agreement (except that you may choose to grant more extensive
94 warranty protection to some or all third parties, at your option).
96 c) You may charge a distribution fee for the physical act of
97 transferring a copy, and you may at your option offer warranty
98 protection in exchange for a fee.
100 Mere aggregation of another unrelated program with this program (or its
101 derivative) on a volume of a storage or distribution medium does not bring
102 the other program under the scope of these terms.
104 3. You may copy and distribute Ghostscript (or a portion or derivative
105 of it, under Paragraph 2) in object code or executable form under the
106 terms of Paragraphs 1 and 2 above provided that you also do one of the
109 a) accompany it with the complete corresponding machine-readable
110 source code, which must be distributed under the terms of
111 Paragraphs 1 and 2 above; or,
113 b) accompany it with a written offer, valid for at least three
114 years, to give any third party free (except for a nominal
115 shipping charge) a complete machine-readable copy of the
116 corresponding source code, to be distributed under the terms of
117 Paragraphs 1 and 2 above; or,
119 c) accompany it with the information you received as to where the
120 corresponding source code may be obtained. (This alternative is
121 allowed only for noncommercial distribution and only if you
122 received the program in object code or executable form alone.)
124 For an executable file, complete source code means all the source code for
125 all modules it contains; but, as a special exception, it need not include
126 source code for modules which are standard libraries that accompany the
127 operating system on which the executable file runs.
129 4. You may not copy, sublicense, distribute or transfer Ghostscript
130 except as expressly provided under this License Agreement. Any attempt
131 otherwise to copy, sublicense, distribute or transfer Ghostscript is
132 void and your rights to use the program under this License agreement
133 shall be automatically terminated. However, parties who have received
134 computer software programs from you with this License Agreement will not
135 have their licenses terminated so long as such parties remain in full
138 5. If you wish to incorporate parts of Ghostscript into other free
139 programs whose distribution conditions are different, write to the Free
140 Software Foundation at 675 Mass Ave, Cambridge, MA 02139. We have not
141 yet worked out a simple rule that can be stated here, but we will often
142 permit this. We will be guided by the two goals of preserving the free
143 status of all derivatives of our free software and of promoting the
144 sharing and reuse of software.
146 Your comments and suggestions about our licensing policies and our
147 software are welcome! Please contact the Free Software Foundation,
148 Inc., 675 Mass Ave, Cambridge, MA 02139, or call (617) 876-3296.
152 BECAUSE GHOSTSCRIPT IS LICENSED FREE OF CHARGE, WE PROVIDE ABSOLUTELY
153 NO WARRANTY, TO THE EXTENT PERMITTED BY APPLICABLE STATE LAW. EXCEPT
154 WHEN OTHERWISE STATED IN WRITING, FREE SOFTWARE FOUNDATION, INC, RICHARD
155 M. STALLMAN, ALADDIN ENTERPRISES, L. PETER DEUTSCH, AND/OR OTHER PARTIES
156 PROVIDE GHOSTSCRIPT "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER
157 EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
158 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE
159 ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF GHOSTSCRIPT IS WITH
160 YOU. SHOULD GHOSTSCRIPT PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL
161 NECESSARY SERVICING, REPAIR OR CORRECTION.
163 IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW WILL RICHARD M.
164 STALLMAN, THE FREE SOFTWARE FOUNDATION, INC., L. PETER DEUTSCH, ALADDIN
165 ENTERPRISES, AND/OR ANY OTHER PARTY WHO MAY MODIFY AND REDISTRIBUTE
166 GHOSTSCRIPT AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING
167 ANY LOST PROFITS, LOST MONIES, OR OTHER SPECIAL, INCIDENTAL OR
168 CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE
169 (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING RENDERED
170 INACCURATE OR LOSSES SUSTAINED BY THIRD PARTIES OR A FAILURE OF THE
171 PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS) GHOSTSCRIPT, EVEN IF YOU
172 HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES, OR FOR ANY CLAIM
175 -------------------- End of file COPYLEFT ------------------------------
180 ansi2knr input_file [output_file]
181 * If no output_file is supplied, output goes to stdout.
182 * There are no error messages.
184 * ansi2knr recognizes function definitions by seeing a non-keyword
185 * identifier at the left margin, followed by a left parenthesis,
186 * with a right parenthesis as the last character on the line,
187 * and with a left brace as the first token on the following line
188 * (ignoring possible intervening comments).
189 * It will recognize a multi-line header provided that no intervening
190 * line ends with a left or right brace or a semicolon.
191 * These algorithms ignore whitespace and comments, except that
192 * the function name must be the first thing on the line.
193 * The following constructs will confuse it:
194 * - Any other construct that starts at the left margin and
195 * follows the above syntax (such as a macro or function call).
196 * - Some macros that tinker with the syntax of the function header.
200 * The original and principal author of ansi2knr is L. Peter Deutsch
201 * <ghost@aladdin.com>. Other authors are noted in the change history
202 * that follows (in reverse chronological order):
203 lpd 96-01-21 added code to cope with not HAVE_CONFIG_H and with
204 compilers that don't understand void, as suggested by
206 lpd 96-01-15 changed to require that the first non-comment token
207 on the line following a function header be a left brace,
208 to reduce sensitivity to macros, as suggested by Tom Lane
210 lpd 95-06-22 removed #ifndefs whose sole purpose was to define
211 undefined preprocessor symbols as 0; changed all #ifdefs
212 for configuration symbols to #ifs
213 lpd 95-04-05 changed copyright notice to make it clear that
214 including ansi2knr in a program does not bring the entire
215 program under the GPL
216 lpd 94-12-18 added conditionals for systems where ctype macros
217 don't handle 8-bit characters properly, suggested by
218 Francois Pinard <pinard@iro.umontreal.ca>;
219 removed --varargs switch (this is now the default)
220 lpd 94-10-10 removed CONFIG_BROKETS conditional
221 lpd 94-07-16 added some conditionals to help GNU `configure',
222 suggested by Francois Pinard <pinard@iro.umontreal.ca>;
223 properly erase prototype args in function parameters,
224 contributed by Jim Avera <jima@netcom.com>;
225 correct error in writeblanks (it shouldn't erase EOLs)
226 lpd 89-xx-xx original version
229 /* Most of the conditionals here are to make ansi2knr work with */
230 /* or without the GNU configure machinery. */
242 For properly autoconfiguring ansi2knr, use AC_CONFIG_HEADER(config.h).
243 This will define HAVE_CONFIG_H and so, activate the following lines.
246 # if STDC_HEADERS || HAVE_STRING_H
249 # include <strings.h>
252 #else /* not HAVE_CONFIG_H */
254 /* Otherwise do it the hard way */
257 # include <strings.h>
260 extern int strlen(), strncmp();
266 #endif /* not HAVE_CONFIG_H */
272 malloc and free should be declared in stdlib.h,
273 but if you've got a K&R compiler, they probably aren't.
279 extern char *malloc();
282 extern char *malloc();
290 * The ctype macros don't always handle 8-bit characters correctly.
291 * Compensate for this here.
294 # undef HAVE_ISASCII /* just in case */
295 # define HAVE_ISASCII 1
298 #if STDC_HEADERS || !HAVE_ISASCII
299 # define is_ascii(c) 1
301 # define is_ascii(c) isascii(c)
304 #define is_space(c) (is_ascii(c) && isspace(c))
305 #define is_alpha(c) (is_ascii(c) && isalpha(c))
306 #define is_alnum(c) (is_ascii(c) && isalnum(c))
308 /* Scanning macros */
309 #define isidchar(ch) (is_alnum(ch) || (ch) == '_')
310 #define isidfirstchar(ch) (is_alpha(ch) || (ch) == '_')
312 /* Forward references */
318 /* The main program */
324 #define bufsize 5000 /* arbitrary size */
329 * In previous versions, ansi2knr recognized a --varargs switch.
330 * If this switch was supplied, ansi2knr would attempt to convert
331 * a ... argument to va_alist and va_dcl; if this switch was not
332 * supplied, ansi2knr would simply drop any such arguments.
333 * Now, ansi2knr always does this conversion, and we only
334 * check for this switch for backward compatibility.
336 int convert_varargs
= 1;
338 if ( argc
> 1 && argv
[1][0] == '-' )
339 { if ( !strcmp(argv
[1], "--varargs") )
340 { convert_varargs
= 1;
345 { fprintf(stderr
, "Unrecognized switch: %s\n", argv
[1]);
352 printf("Usage: ansi2knr input_file [output_file]\n");
358 out
= fopen(argv
[2], "w");
360 { fprintf(stderr
, "Cannot open output file %s\n", argv
[2]);
364 in
= fopen(argv
[1], "r");
366 { fprintf(stderr
, "Cannot open input file %s\n", argv
[1]);
369 fprintf(out
, "#line 1 \"%s\"\n", argv
[1]);
370 buf
= malloc(bufsize
);
372 while ( fgets(line
, (unsigned)(buf
+ bufsize
- line
), in
) != NULL
)
374 test
: line
+= strlen(line
);
375 switch ( test1(buf
) )
377 case 2: /* a function header */
378 convert1(buf
, out
, 1, convert_varargs
);
380 case 1: /* a function */
381 /* Check for a { at the start of the next line. */
383 f
: if ( line
>= buf
+ (bufsize
- 1) ) /* overflow check */
385 if ( fgets(line
, (unsigned)(buf
+ bufsize
- line
), in
) == NULL
)
387 switch ( *skipspace(more
, 1) )
390 /* Definitely a function header. */
391 convert1(buf
, out
, 0, convert_varargs
);
395 /* The next line was blank or a comment: */
396 /* keep scanning for a non-comment. */
397 line
+= strlen(line
);
400 /* buf isn't a function header, but */
408 case -1: /* maybe the start of a function */
409 if ( line
!= buf
+ (bufsize
- 1) ) /* overflow check */
412 default: /* not a function */
426 /* Skip over space and comments, in either direction. */
430 register int dir
; /* 1 for forward, -1 for backward */
432 { while ( is_space(*p
) )
434 if ( !(*p
== '/' && p
[dir
] == '*') )
437 while ( !(*p
== '*' && p
[dir
] == '/') )
439 return p
; /* multi-line comment?? */
448 * Write blanks over part of a string.
449 * Don't overwrite end-of-line characters.
452 writeblanks(start
, end
)
456 for ( p
= start
; p
< end
; p
++ )
457 if ( *p
!= '\r' && *p
!= '\n' )
463 * Test whether the string in buf is a function definition.
464 * The string may contain and/or end with a newline.
466 * 0 - definitely not a function definition;
467 * 1 - definitely a function definition;
468 * 2 - definitely a function prototype (NOT USED);
469 * -1 - may be the beginning of a function definition,
470 * append another line and look again.
471 * The reason we don't attempt to convert function prototypes is that
472 * Ghostscript's declaration-generating macros look too much like
473 * prototypes, and confuse the algorithms.
478 { register char *p
= buf
;
483 if ( !isidfirstchar(*p
) )
484 return 0; /* no name at left margin */
485 bend
= skipspace(buf
+ strlen(buf
) - 1, -1);
488 case ';': contin
= 0 /*2*/; break;
489 case ')': contin
= 1; break;
490 case '{': return 0; /* not a function */
491 case '}': return 0; /* not a function */
492 default: contin
= -1;
494 while ( isidchar(*p
) )
499 return 0; /* not a function */
502 return 0; /* no parameters */
503 /* Check that the apparent function name isn't a keyword. */
504 /* We only need to check for keywords that could be followed */
505 /* by a left parenthesis (which, unfortunately, is most of them). */
506 { static char *words
[] =
507 { "asm", "auto", "case", "char", "const", "double",
508 "extern", "float", "for", "if", "int", "long",
509 "register", "return", "short", "signed", "sizeof",
510 "static", "switch", "typedef", "unsigned",
511 "void", "volatile", "while", 0
515 int len
= endfn
- buf
;
517 while ( (kp
= *key
) != 0 )
518 { if ( strlen(kp
) == len
&& !strncmp(kp
, buf
, len
) )
519 return 0; /* name is a keyword */
526 /* Convert a recognized function definition or header to K&R syntax. */
528 convert1(buf
, out
, header
, convert_varargs
)
531 int header
; /* Boolean */
532 int convert_varargs
; /* Boolean */
536 unsigned num_breaks
= 2; /* for testing */
542 /* Pre-ANSI implementations don't agree on whether strchr */
543 /* is called strchr or index, so we open-code it here. */
544 for ( endfn
= buf
; *(endfn
++) != '('; )
547 breaks
= (char **)malloc(sizeof(char *) * num_breaks
* 2);
549 { /* Couldn't allocate break table, give up */
550 fprintf(stderr
, "Unable to allocate break table!\n");
554 btop
= breaks
+ num_breaks
* 2 - 2;
556 /* Parse the argument list */
564 { /* Filled up break table. */
565 /* Allocate a bigger one and start over. */
566 free((char *)breaks
);
571 /* Find the end of the argument */
572 for ( ; end
== NULL
; p
++ )
576 if ( !level
) end
= p
;
579 if ( !level
) lp
= p
;
583 if ( --level
< 0 ) end
= p
;
587 p
= skipspace(p
, 1) - 1;
593 /* Erase any embedded prototype parameters. */
595 writeblanks(lp
+ 1, rp
);
596 p
--; /* back up over terminator */
597 /* Find the name being declared. */
598 /* This is complicated because of procedure and */
599 /* array modifiers. */
601 { p
= skipspace(p
- 1, -1);
604 case ']': /* skip array dimension(s) */
605 case ')': /* skip procedure args OR name */
610 case ']': case ')': level
++; break;
611 case '[': case '(': level
--; break;
612 case '/': p
= skipspace(p
, -1) + 1; break;
616 if ( *p
== '(' && *skipspace(p
+ 1, 1) == '*' )
617 { /* We found the name being declared */
618 while ( !isidfirstchar(*p
) )
619 p
= skipspace(p
, 1) + 1;
627 found
: if ( *p
== '.' && p
[-1] == '.' && p
[-2] == '.' )
628 { if ( convert_varargs
)
629 { *bp
++ = "va_alist";
634 if ( bp
== breaks
+ 1 ) /* sole argument */
635 writeblanks(breaks
[0], p
);
637 writeblanks(bp
[-1] - 1, p
);
642 { while ( isidchar(*p
) ) p
--;
647 while ( *p
++ == ',' );
649 /* Make a special check for 'void' arglist */
650 if ( bp
== breaks
+2 )
651 { p
= skipspace(breaks
[0], 1);
652 if ( !strncmp(p
, "void", 4) )
653 { p
= skipspace(p
+4, 1);
654 if ( p
== breaks
[2] - 1 )
655 { bp
= breaks
; /* yup, pretend arglist is empty */
656 writeblanks(breaks
[0], p
+ 1);
660 /* Put out the function name and left parenthesis. */
662 while ( p
!= endfn
) putc(*p
, out
), p
++;
663 /* Put out the declaration. */
666 for ( p
= breaks
[0]; *p
; p
++ )
667 if ( *p
== '\r' || *p
== '\n' )
671 { for ( ap
= breaks
+1; ap
< bp
; ap
+= 2 )
673 while ( isidchar(*p
) )
679 /* Put out the argument declarations */
680 for ( ap
= breaks
+2; ap
<= bp
; ap
+= 2 )
684 fputs(breaks
[0], out
); /* any prior args */
685 fputs("va_dcl", out
); /* the final arg */
689 fputs(breaks
[0], out
);
691 free((char *)breaks
);