1 /* Copyright (C) 1989, 1991, 1993, 1994 Aladdin Enterprises. All rights reserved. */
4 /* Convert ANSI function declarations to K&R syntax */
7 ansi2knr is distributed in the hope that it will be useful, but
8 WITHOUT ANY WARRANTY. No author or distributor accepts responsibility
9 to anyone for the consequences of using it or for whether it serves any
10 particular purpose or works at all, unless he says so in writing. Refer
11 to the GNU General Public License for full details.
13 Everyone is granted permission to copy, modify and redistribute
14 ansi2knr, but only under the conditions described in the GNU
15 General Public License. A copy of this license is supposed to have been
16 given to you along with ansi2knr so you can know your rights and
17 responsibilities. It should be in a file named COPYLEFT. Among other
18 things, the copyright notice and this notice must be preserved on all
24 ansi2knr [--varargs] input_file [output_file]
25 * If no output_file is supplied, output goes to stdout.
26 * There are no error messages.
28 * ansi2knr recognizes function definitions by seeing a non-keyword
29 * identifier at the left margin, followed by a left parenthesis,
30 * with a right parenthesis as the last character on the line.
31 * It will recognize a multi-line header provided that the last character
32 * of the last line of the header is a right parenthesis,
33 * and no intervening line ends with a left brace or a semicolon.
34 * These algorithms ignore whitespace and comments, except that
35 * the function name must be the first thing on the line.
36 * The following constructs will confuse it:
37 * - Any other construct that starts at the left margin and
38 * follows the above syntax (such as a macro or function call).
39 * - Macros that tinker with the syntax of the function header.
41 * If the --varargs switch is supplied, ansi2knr will attempt to
42 * convert a ... argument to va_alist and va_dcl. If this switch is not
43 * supplied, ansi2knr will simply drop any such arguments.
47 * The original and principal author of ansi2knr is L. Peter Deutsch
48 * <ghost@aladdin.com>. Other authors are noted in the change history
49 * that follows (in reverse chronological order):
50 lpd 94-10-10 removed CONFIG_BROKETS conditional
51 lpd 94-07-16 added some conditionals to help GNU `configure',
52 suggested by Francois Pinard <pinard@iro.umontreal.ca>;
53 properly erase prototype args in function parameters,
54 contributed by Jim Avera <jima@netcom.com>;
55 correct error in writeblanks (it shouldn't erase EOLs)
56 lpd 89-xx-xx original version
59 /* Most of the conditionals here are to make ansi2knr work with */
60 /* the GNU configure machinery. */
72 For properly autoconfiguring ansi2knr, use AC_CONFIG_HEADER(config.h).
73 This will define HAVE_CONFIG_H and so, activate the following lines.
76 # if STDC_HEADERS || HAVE_STRING_H
82 #else /* not HAVE_CONFIG_H */
85 Without AC_CONFIG_HEADER, merely use <string.h> as in the original
86 Ghostscript distribution. This loses on older BSD systems.
91 #endif /* not HAVE_CONFIG_H */
97 malloc and free should be declared in stdlib.h,
98 but if you've got a K&R compiler, they probably aren't.
104 /* Scanning macros */
105 #define isidchar(ch) (isalnum(ch) || (ch) == '_')
106 #define isidfirstchar(ch) (isalpha(ch) || (ch) == '_')
108 /* Forward references */
114 /* The main program */
120 #define bufsize 5000 /* arbitrary size */
123 int convert_varargs
= 0;
124 if ( argc
> 1 && argv
[1][0] == '-' )
125 { if ( !strcmp(argv
[1], "--varargs") )
126 { convert_varargs
= 1;
131 { fprintf(stderr
, "Unrecognized switch: %s\n", argv
[1]);
138 printf("Usage: ansi2knr [--varargs] input_file [output_file]\n");
144 out
= fopen(argv
[2], "w");
146 { fprintf(stderr
, "Cannot open output file %s\n", argv
[2]);
150 in
= fopen(argv
[1], "r");
152 { fprintf(stderr
, "Cannot open input file %s\n", argv
[1]);
155 fprintf(out
, "#line 1 \"%s\"\n", argv
[1]);
156 buf
= malloc(bufsize
);
158 while ( fgets(line
, (unsigned)(buf
+ bufsize
- line
), in
) != NULL
)
159 { switch ( test1(buf
) )
161 case 2: /* a function header */
162 convert1(buf
, out
, 1, convert_varargs
);
164 case 1: /* a function */
165 convert1(buf
, out
, 0, convert_varargs
);
167 case -1: /* maybe the start of a function */
168 line
= buf
+ strlen(buf
);
169 if ( line
!= buf
+ (bufsize
- 1) ) /* overflow check */
172 default: /* not a function */
178 if ( line
!= buf
) fputs(buf
, out
);
185 /* Skip over space and comments, in either direction. */
189 register int dir
; /* 1 for forward, -1 for backward */
191 { while ( isspace(*p
) ) p
+= dir
;
192 if ( !(*p
== '/' && p
[dir
] == '*') ) break;
194 while ( !(*p
== '*' && p
[dir
] == '/') )
195 { if ( *p
== 0 ) return p
; /* multi-line comment?? */
204 * Write blanks over part of a string.
205 * Don't overwrite end-of-line characters.
208 writeblanks(start
, end
)
212 for ( p
= start
; p
< end
; p
++ )
213 if ( *p
!= '\r' && *p
!= '\n' ) *p
= ' ';
217 * Test whether the string in buf is a function definition.
218 * The string may contain and/or end with a newline.
220 * 0 - definitely not a function definition;
221 * 1 - definitely a function definition;
222 * 2 - definitely a function prototype (NOT USED);
223 * -1 - may be the beginning of a function definition,
224 * append another line and look again.
225 * The reason we don't attempt to convert function prototypes is that
226 * Ghostscript's declaration-generating macros look too much like
227 * prototypes, and confuse the algorithms.
232 { register char *p
= buf
;
236 if ( !isidfirstchar(*p
) )
237 return 0; /* no name at left margin */
238 bend
= skipspace(buf
+ strlen(buf
) - 1, -1);
241 case ';': contin
= 0 /*2*/; break;
242 case ')': contin
= 1; break;
243 case '{': return 0; /* not a function */
244 default: contin
= -1;
246 while ( isidchar(*p
) ) p
++;
250 return 0; /* not a function */
253 return 0; /* no parameters */
254 /* Check that the apparent function name isn't a keyword. */
255 /* We only need to check for keywords that could be followed */
256 /* by a left parenthesis (which, unfortunately, is most of them). */
257 { static char *words
[] =
258 { "asm", "auto", "case", "char", "const", "double",
259 "extern", "float", "for", "if", "int", "long",
260 "register", "return", "short", "signed", "sizeof",
261 "static", "switch", "typedef", "unsigned",
262 "void", "volatile", "while", 0
266 int len
= endfn
- buf
;
267 while ( (kp
= *key
) != 0 )
268 { if ( strlen(kp
) == len
&& !strncmp(kp
, buf
, len
) )
269 return 0; /* name is a keyword */
276 /* Convert a recognized function definition or header to K&R syntax. */
278 convert1(buf
, out
, header
, convert_varargs
)
281 int header
; /* Boolean */
282 int convert_varargs
; /* Boolean */
286 unsigned num_breaks
= 2; /* for testing */
291 /* Pre-ANSI implementations don't agree on whether strchr */
292 /* is called strchr or index, so we open-code it here. */
293 for ( endfn
= buf
; *(endfn
++) != '('; ) ;
295 breaks
= (char **)malloc(sizeof(char *) * num_breaks
* 2);
297 { /* Couldn't allocate break table, give up */
298 fprintf(stderr
, "Unable to allocate break table!\n");
302 btop
= breaks
+ num_breaks
* 2 - 2;
304 /* Parse the argument list */
311 { /* Filled up break table. */
312 /* Allocate a bigger one and start over. */
313 free((char *)breaks
);
318 /* Find the end of the argument */
319 for ( ; end
== NULL
; p
++ )
323 if ( !level
) end
= p
;
326 if ( !level
) lp
= p
;
330 if ( --level
< 0 ) end
= p
;
334 p
= skipspace(p
, 1) - 1;
340 /* Erase any embedded prototype parameters. */
342 writeblanks(lp
+ 1, rp
);
343 p
--; /* back up over terminator */
344 /* Find the name being declared. */
345 /* This is complicated because of procedure and */
346 /* array modifiers. */
348 { p
= skipspace(p
- 1, -1);
351 case ']': /* skip array dimension(s) */
352 case ')': /* skip procedure args OR name */
357 case ']': case ')': level
++; break;
358 case '[': case '(': level
--; break;
359 case '/': p
= skipspace(p
, -1) + 1; break;
363 if ( *p
== '(' && *skipspace(p
+ 1, 1) == '*' )
364 { /* We found the name being declared */
365 while ( !isidfirstchar(*p
) )
366 p
= skipspace(p
, 1) + 1;
373 found
: if ( *p
== '.' && p
[-1] == '.' && p
[-2] == '.' )
374 { if ( convert_varargs
)
375 { *bp
++ = "va_alist";
380 if ( bp
== breaks
+ 1 ) /* sole argument */
381 writeblanks(breaks
[0], p
);
383 writeblanks(bp
[-1] - 1, p
);
388 { while ( isidchar(*p
) ) p
--;
393 while ( *p
++ == ',' );
395 /* Make a special check for 'void' arglist */
396 if ( bp
== breaks
+2 )
397 { p
= skipspace(breaks
[0], 1);
398 if ( !strncmp(p
, "void", 4) )
399 { p
= skipspace(p
+4, 1);
400 if ( p
== breaks
[2] - 1 )
401 { bp
= breaks
; /* yup, pretend arglist is empty */
402 writeblanks(breaks
[0], p
+ 1);
406 /* Put out the function name and left parenthesis. */
408 while ( p
!= endfn
) putc(*p
, out
), p
++;
409 /* Put out the declaration. */
412 for ( p
= breaks
[0]; *p
; p
++ )
413 if ( *p
== '\r' || *p
== '\n' )
417 { for ( ap
= breaks
+1; ap
< bp
; ap
+= 2 )
419 while ( isidchar(*p
) )
425 /* Put out the argument declarations */
426 for ( ap
= breaks
+2; ap
<= bp
; ap
+= 2 )
430 fputs(breaks
[0], out
); /* any prior args */
431 fputs("va_dcl", out
); /* the final arg */
435 fputs(breaks
[0], out
);
437 free((char *)breaks
);