1 /* Copyright (C) 1989, 1991, 1993, 1994, 1995 Aladdin Enterprises. All rights reserved. */
4 /* Convert ANSI C function definitions to K&R ("traditional C") syntax */
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 Among other things, the copyright notice and this notice must be preserved
20 We explicitly state here what we believe is already implied by the GPL: if
21 the ansi2knr program is distributed as a separate set of sources and a
22 separate executable file which are aggregated on a storage medium together
23 with another program, this in itself does not bring the other program under
24 the GPL, nor does the mere fact that such a program or the procedures for
25 constructing it invoke the ansi2knr executable bring any other part of the
26 program under the GPL.
31 ansi2knr input_file [output_file]
32 * If no output_file is supplied, output goes to stdout.
33 * There are no error messages.
35 * ansi2knr recognizes function definitions by seeing a non-keyword
36 * identifier at the left margin, followed by a left parenthesis,
37 * with a right parenthesis as the last character on the line.
38 * It will recognize a multi-line header provided that the last character
39 * of the last line of the header is a right parenthesis,
40 * and no intervening line ends with a left or right brace or a semicolon.
41 * These algorithms ignore whitespace and comments, except that
42 * the function name must be the first thing on the line.
43 * The following constructs will confuse it:
44 * - Any other construct that starts at the left margin and
45 * follows the above syntax (such as a macro or function call).
46 * - Macros that tinker with the syntax of the function header.
50 * The original and principal author of ansi2knr is L. Peter Deutsch
51 * <ghost@aladdin.com>. Other authors are noted in the change history
52 * that follows (in reverse chronological order):
53 lpd 95-06-22 removed #ifndefs whose sole purpose was to define
54 undefined preprocessor symbols as 0; changed all #ifdefs
55 for configuration symbols to #ifs
56 lpd 95-04-05 changed copyright notice to make it clear that
57 including ansi2knr in a program does not bring the entire
59 lpd 94-12-18 added conditionals for systems where ctype macros
60 don't handle 8-bit characters properly, suggested by
61 Francois Pinard <pinard@iro.umontreal.ca>;
62 removed --varargs switch (this is now the default)
63 lpd 94-10-10 removed CONFIG_BROKETS conditional
64 lpd 94-07-16 added some conditionals to help GNU `configure',
65 suggested by Francois Pinard <pinard@iro.umontreal.ca>;
66 properly erase prototype args in function parameters,
67 contributed by Jim Avera <jima@netcom.com>;
68 correct error in writeblanks (it shouldn't erase EOLs)
69 lpd 89-xx-xx original version
72 /* Most of the conditionals here are to make ansi2knr work with */
73 /* the GNU configure machinery. */
85 For properly autoconfiguring ansi2knr, use AC_CONFIG_HEADER(config.h).
86 This will define HAVE_CONFIG_H and so, activate the following lines.
89 # if STDC_HEADERS || HAVE_STRING_H
95 #else /* not HAVE_CONFIG_H */
98 Without AC_CONFIG_HEADER, merely use <string.h> as in the original
99 Ghostscript distribution. This loses on older BSD systems.
104 #endif /* not HAVE_CONFIG_H */
110 malloc and free should be declared in stdlib.h,
111 but if you've got a K&R compiler, they probably aren't.
118 * The ctype macros don't always handle 8-bit characters correctly.
119 * Compensate for this here.
122 # undef HAVE_ISASCII /* just in case */
123 # define HAVE_ISASCII 1
126 #if STDC_HEADERS || !HAVE_ISASCII
127 # define is_ascii(c) 1
129 # define is_ascii(c) isascii(c)
132 #define is_space(c) (is_ascii(c) && isspace(c))
133 #define is_alpha(c) (is_ascii(c) && isalpha(c))
134 #define is_alnum(c) (is_ascii(c) && isalnum(c))
136 /* Scanning macros */
137 #define isidchar(ch) (is_alnum(ch) || (ch) == '_')
138 #define isidfirstchar(ch) (is_alpha(ch) || (ch) == '_')
140 /* Forward references */
146 /* The main program */
152 #define bufsize 5000 /* arbitrary size */
156 * In previous versions, ansi2knr recognized a --varargs switch.
157 * If this switch was supplied, ansi2knr would attempt to convert
158 * a ... argument to va_alist and va_dcl; if this switch was not
159 * supplied, ansi2knr would simply drop any such arguments.
160 * Now, ansi2knr always does this conversion, and we only
161 * check for this switch for backward compatibility.
163 int convert_varargs
= 1;
165 if ( argc
> 1 && argv
[1][0] == '-' )
166 { if ( !strcmp(argv
[1], "--varargs") )
167 { convert_varargs
= 1;
172 { fprintf(stderr
, "Unrecognized switch: %s\n", argv
[1]);
179 printf("Usage: ansi2knr input_file [output_file]\n");
185 out
= fopen(argv
[2], "w");
187 { fprintf(stderr
, "Cannot open output file %s\n", argv
[2]);
191 in
= fopen(argv
[1], "r");
193 { fprintf(stderr
, "Cannot open input file %s\n", argv
[1]);
196 fprintf(out
, "#line 1 \"%s\"\n", argv
[1]);
197 buf
= malloc(bufsize
);
199 while ( fgets(line
, (unsigned)(buf
+ bufsize
- line
), in
) != NULL
)
200 { switch ( test1(buf
) )
202 case 2: /* a function header */
203 convert1(buf
, out
, 1, convert_varargs
);
205 case 1: /* a function */
206 convert1(buf
, out
, 0, convert_varargs
);
208 case -1: /* maybe the start of a function */
209 line
= buf
+ strlen(buf
);
210 if ( line
!= buf
+ (bufsize
- 1) ) /* overflow check */
213 default: /* not a function */
219 if ( line
!= buf
) fputs(buf
, out
);
226 /* Skip over space and comments, in either direction. */
230 register int dir
; /* 1 for forward, -1 for backward */
232 { while ( is_space(*p
) ) p
+= dir
;
233 if ( !(*p
== '/' && p
[dir
] == '*') ) break;
235 while ( !(*p
== '*' && p
[dir
] == '/') )
236 { if ( *p
== 0 ) return p
; /* multi-line comment?? */
245 * Write blanks over part of a string.
246 * Don't overwrite end-of-line characters.
249 writeblanks(start
, end
)
253 for ( p
= start
; p
< end
; p
++ )
254 if ( *p
!= '\r' && *p
!= '\n' ) *p
= ' ';
258 * Test whether the string in buf is a function definition.
259 * The string may contain and/or end with a newline.
261 * 0 - definitely not a function definition;
262 * 1 - definitely a function definition;
263 * 2 - definitely a function prototype (NOT USED);
264 * -1 - may be the beginning of a function definition,
265 * append another line and look again.
266 * The reason we don't attempt to convert function prototypes is that
267 * Ghostscript's declaration-generating macros look too much like
268 * prototypes, and confuse the algorithms.
273 { register char *p
= buf
;
277 if ( !isidfirstchar(*p
) )
278 return 0; /* no name at left margin */
279 bend
= skipspace(buf
+ strlen(buf
) - 1, -1);
282 case ';': contin
= 0 /*2*/; break;
283 case ')': contin
= 1; break;
284 case '{': return 0; /* not a function */
285 case '}': return 0; /* not a function */
286 default: contin
= -1;
288 while ( isidchar(*p
) ) p
++;
292 return 0; /* not a function */
295 return 0; /* no parameters */
296 /* Check that the apparent function name isn't a keyword. */
297 /* We only need to check for keywords that could be followed */
298 /* by a left parenthesis (which, unfortunately, is most of them). */
299 { static char *words
[] =
300 { "asm", "auto", "case", "char", "const", "double",
301 "extern", "float", "for", "if", "int", "long",
302 "register", "return", "short", "signed", "sizeof",
303 "static", "switch", "typedef", "unsigned",
304 "void", "volatile", "while", 0
308 int len
= endfn
- buf
;
309 while ( (kp
= *key
) != 0 )
310 { if ( strlen(kp
) == len
&& !strncmp(kp
, buf
, len
) )
311 return 0; /* name is a keyword */
318 /* Convert a recognized function definition or header to K&R syntax. */
320 convert1(buf
, out
, header
, convert_varargs
)
323 int header
; /* Boolean */
324 int convert_varargs
; /* Boolean */
328 unsigned num_breaks
= 2; /* for testing */
333 /* Pre-ANSI implementations don't agree on whether strchr */
334 /* is called strchr or index, so we open-code it here. */
335 for ( endfn
= buf
; *(endfn
++) != '('; ) ;
337 breaks
= (char **)malloc(sizeof(char *) * num_breaks
* 2);
339 { /* Couldn't allocate break table, give up */
340 fprintf(stderr
, "Unable to allocate break table!\n");
344 btop
= breaks
+ num_breaks
* 2 - 2;
346 /* Parse the argument list */
353 { /* Filled up break table. */
354 /* Allocate a bigger one and start over. */
355 free((char *)breaks
);
360 /* Find the end of the argument */
361 for ( ; end
== NULL
; p
++ )
365 if ( !level
) end
= p
;
368 if ( !level
) lp
= p
;
372 if ( --level
< 0 ) end
= p
;
376 p
= skipspace(p
, 1) - 1;
382 /* Erase any embedded prototype parameters. */
384 writeblanks(lp
+ 1, rp
);
385 p
--; /* back up over terminator */
386 /* Find the name being declared. */
387 /* This is complicated because of procedure and */
388 /* array modifiers. */
390 { p
= skipspace(p
- 1, -1);
393 case ']': /* skip array dimension(s) */
394 case ')': /* skip procedure args OR name */
399 case ']': case ')': level
++; break;
400 case '[': case '(': level
--; break;
401 case '/': p
= skipspace(p
, -1) + 1; break;
405 if ( *p
== '(' && *skipspace(p
+ 1, 1) == '*' )
406 { /* We found the name being declared */
407 while ( !isidfirstchar(*p
) )
408 p
= skipspace(p
, 1) + 1;
415 found
: if ( *p
== '.' && p
[-1] == '.' && p
[-2] == '.' )
416 { if ( convert_varargs
)
417 { *bp
++ = "va_alist";
422 if ( bp
== breaks
+ 1 ) /* sole argument */
423 writeblanks(breaks
[0], p
);
425 writeblanks(bp
[-1] - 1, p
);
430 { while ( isidchar(*p
) ) p
--;
435 while ( *p
++ == ',' );
437 /* Make a special check for 'void' arglist */
438 if ( bp
== breaks
+2 )
439 { p
= skipspace(breaks
[0], 1);
440 if ( !strncmp(p
, "void", 4) )
441 { p
= skipspace(p
+4, 1);
442 if ( p
== breaks
[2] - 1 )
443 { bp
= breaks
; /* yup, pretend arglist is empty */
444 writeblanks(breaks
[0], p
+ 1);
448 /* Put out the function name and left parenthesis. */
450 while ( p
!= endfn
) putc(*p
, out
), p
++;
451 /* Put out the declaration. */
454 for ( p
= breaks
[0]; *p
; p
++ )
455 if ( *p
== '\r' || *p
== '\n' )
459 { for ( ap
= breaks
+1; ap
< bp
; ap
+= 2 )
461 while ( isidchar(*p
) )
467 /* Put out the argument declarations */
468 for ( ap
= breaks
+2; ap
<= bp
; ap
+= 2 )
472 fputs(breaks
[0], out
); /* any prior args */
473 fputs("va_dcl", out
); /* the final arg */
477 fputs(breaks
[0], out
);
479 free((char *)breaks
);