1 /* Copyright (C) 1989, 1997 Aladdin Enterprises. All rights reserved. */
3 /* hacked up by uo to get around automake-1.3-problem */
5 /*$Id: ansi2knr.c,v 1.5 1998/12/27 16:09:19 uwe Exp $*/
6 /* Convert ANSI C function definitions to K&R ("traditional C") syntax */
9 ansi2knr is distributed in the hope that it will be useful, but WITHOUT ANY
10 WARRANTY. No author or distributor accepts responsibility to anyone for the
11 consequences of using it or for whether it serves any particular purpose or
12 works at all, unless he says so in writing. Refer to the GNU General Public
13 License (the "GPL") for full details.
15 Everyone is granted permission to copy, modify and redistribute ansi2knr,
16 but only under the conditions described in the GPL. A copy of this license
17 is supposed to have been given to you along with ansi2knr so you can know
18 your rights and responsibilities. It should be in a file named COPYLEFT,
19 or, if there is no file named COPYLEFT, a file named COPYING. Among other
20 things, the copyright notice and this notice must be preserved on all
23 We explicitly state here what we believe is already implied by the GPL: if
24 the ansi2knr program is distributed as a separate set of sources and a
25 separate executable file which are aggregated on a storage medium together
26 with another program, this in itself does not bring the other program under
27 the GPL, nor does the mere fact that such a program or the procedures for
28 constructing it invoke the ansi2knr executable bring any other part of the
29 program under the GPL.
33 * ----- not valid at the moment -- uwe ------
35 ansi2knr [--filename FILENAME] [INPUT_FILE [OUTPUT_FILE]]
36 * --filename provides the file name for the #line directive in the output,
37 * overriding input_file (if present).
38 * If no input_file is supplied, input is read from stdin.
39 * If no output_file is supplied, output goes to stdout.
40 * There are no error messages.
42 * ansi2knr recognizes function definitions by seeing a non-keyword
43 * identifier at the left margin, followed by a left parenthesis,
44 * with a right parenthesis as the last character on the line,
45 * and with a left brace as the first token on the following line
46 * (ignoring possible intervening comments).
47 * It will recognize a multi-line header provided that no intervening
48 * line ends with a left or right brace or a semicolon.
49 * These algorithms ignore whitespace and comments, except that
50 * the function name must be the first thing on the line.
51 * The following constructs will confuse it:
52 * - Any other construct that starts at the left margin and
53 * follows the above syntax (such as a macro or function call).
54 * - Some macros that tinker with the syntax of the function header.
58 * The original and principal author of ansi2knr is L. Peter Deutsch
59 * <ghost@aladdin.com>. Other authors are noted in the change history
60 * that follows (in reverse chronological order):
61 lpd 97-12-08 made input_file optional; only closes input and/or
62 output file if not stdin or stdout respectively; prints
63 usage message on stderr rather than stdout; adds
64 --filename switch (changes suggested by
65 <ceder@lysator.liu.se>)
66 lpd 96-01-21 added code to cope with not HAVE_CONFIG_H and with
67 compilers that don't understand void, as suggested by
69 lpd 96-01-15 changed to require that the first non-comment token
70 on the line following a function header be a left brace,
71 to reduce sensitivity to macros, as suggested by Tom Lane
73 lpd 95-06-22 removed #ifndefs whose sole purpose was to define
74 undefined preprocessor symbols as 0; changed all #ifdefs
75 for configuration symbols to #ifs
76 lpd 95-04-05 changed copyright notice to make it clear that
77 including ansi2knr in a program does not bring the entire
79 lpd 94-12-18 added conditionals for systems where ctype macros
80 don't handle 8-bit characters properly, suggested by
81 Francois Pinard <pinard@iro.umontreal.ca>;
82 removed --varargs switch (this is now the default)
83 lpd 94-10-10 removed CONFIG_BROKETS conditional
84 lpd 94-07-16 added some conditionals to help GNU `configure',
85 suggested by Francois Pinard <pinard@iro.umontreal.ca>;
86 properly erase prototype args in function parameters,
87 contributed by Jim Avera <jima@netcom.com>;
88 correct error in writeblanks (it shouldn't erase EOLs)
89 lpd 89-xx-xx original version
92 /* Most of the conditionals here are to make ansi2knr work with */
93 /* or without the GNU configure machinery. */
105 For properly autoconfiguring ansi2knr, use AC_CONFIG_HEADER(config.h).
106 This will define HAVE_CONFIG_H and so, activate the following lines.
109 # if STDC_HEADERS || HAVE_STRING_H
112 # include <strings.h>
115 #else /* not HAVE_CONFIG_H */
117 /* Otherwise do it the hard way */
120 # include <strings.h>
123 extern int strlen(), strncmp();
129 #endif /* not HAVE_CONFIG_H */
135 malloc and free should be declared in stdlib.h,
136 but if you've got a K&R compiler, they probably aren't.
142 extern char *malloc();
145 extern char *malloc();
153 * The ctype macros don't always handle 8-bit characters correctly.
154 * Compensate for this here.
157 # undef HAVE_ISASCII /* just in case */
158 # define HAVE_ISASCII 1
161 #if STDC_HEADERS || !HAVE_ISASCII
162 # define is_ascii(c) 1
164 # define is_ascii(c) isascii(c)
167 #define is_space(c) (is_ascii(c) && isspace(c))
168 #define is_alpha(c) (is_ascii(c) && isalpha(c))
169 #define is_alnum(c) (is_ascii(c) && isalnum(c))
171 /* Scanning macros */
172 #define isidchar(ch) (is_alnum(ch) || (ch) == '_')
173 #define isidfirstchar(ch) (is_alpha(ch) || (ch) == '_')
175 /* Forward references */
181 /* The main program */
189 #define bufsize 5000 /* arbitrary size */
194 "Usage: ansi2knr [--filename FILENAME] [INPUT_FILE [OUTPUT_FILE]]\n";
196 * In previous versions, ansi2knr recognized a --varargs switch.
197 * If this switch was supplied, ansi2knr would attempt to convert
198 * a ... argument to va_alist and va_dcl; if this switch was not
199 * supplied, ansi2knr would simply drop any such arguments.
200 * Now, ansi2knr always does this conversion, and we only
201 * check for this switch for backward compatibility.
203 int convert_varargs = 0;
205 while ( argc > 1 && argv[1][0] == '-' ) {
206 if ( !strcmp(argv[1], "--varargs") ) {
212 if ( !strcmp(argv[1], "--filename") && argc > 2 ) {
218 fprintf(stderr, "Unrecognized switch: %s\n", argv[1]);
219 fprintf(stderr, usage);
225 fprintf(stderr, usage);
228 out = fopen(argv[1], "w");
230 fprintf(stderr, "Cannot open output file %s\n", argv[2]);
238 fprintf(out, "#line 1 \"%s\"\n", filename);
239 buf = malloc(bufsize);
241 while ( fgets(line, (unsigned)(buf + bufsize - line), in) != NULL )
243 test: line += strlen(line);
244 switch ( test1(buf) )
246 case 2: /* a function header */
247 convert1(buf, out, 1, convert_varargs);
249 case 1: /* a function */
250 /* Check for a { at the start of the next line. */
252 f: if ( line >= buf + (bufsize - 1) ) /* overflow check */
254 if ( fgets(line, (unsigned)(buf + bufsize - line), in) == NULL )
256 switch ( *skipspace(more, 1) )
259 /* Definitely a function header. */
260 convert1(buf, out, 0, convert_varargs);
264 /* The next line was blank or a comment: */
265 /* keep scanning for a non-comment. */
266 line += strlen(line);
269 /* buf isn't a function header, but */
277 case -1: /* maybe the start of a function */
278 if ( line != buf + (bufsize - 1) ) /* overflow check */
281 default: /* not a function */
297 /* Skip over space and comments, in either direction. */
301 register int dir; /* 1 for forward, -1 for backward */
303 { while ( is_space(*p) )
305 if ( !(*p == '/' && p[dir] == '*') )
308 while ( !(*p == '*' && p[dir] == '/') )
310 return p; /* multi-line comment?? */
319 * Write blanks over part of a string.
320 * Don't overwrite end-of-line characters.
323 writeblanks(start, end)
327 for ( p = start; p < end; p++ )
328 if ( *p != '\r' && *p != '\n' )
334 * Test whether the string in buf is a function definition.
335 * The string may contain and/or end with a newline.
337 * 0 - definitely not a function definition;
338 * 1 - definitely a function definition;
339 * 2 - definitely a function prototype (NOT USED);
340 * -1 - may be the beginning of a function definition,
341 * append another line and look again.
342 * The reason we don't attempt to convert function prototypes is that
343 * Ghostscript's declaration-generating macros look too much like
344 * prototypes, and confuse the algorithms.
349 { register char *p = buf;
354 if ( !isidfirstchar(*p) )
355 return 0; /* no name at left margin */
356 bend = skipspace(buf + strlen(buf) - 1, -1);
359 case ';': contin = 0 /*2*/; break;
360 case ')': contin = 1; break;
361 case '{': return 0; /* not a function */
362 case '}': return 0; /* not a function */
363 default: contin = -1;
365 while ( isidchar(*p) )
370 return 0; /* not a function */
373 return 0; /* no parameters */
374 /* Check that the apparent function name isn't a keyword. */
375 /* We only need to check for keywords that could be followed */
376 /* by a left parenthesis (which, unfortunately, is most of them). */
377 { static char *words[] =
378 { "asm", "auto", "case", "char", "const", "double",
379 "extern", "float", "for", "if", "int", "long",
380 "register", "return", "short", "signed", "sizeof",
381 "static", "switch", "typedef", "unsigned",
382 "void", "volatile", "while", 0
386 int len = endfn - buf;
388 while ( (kp = *key) != 0 )
389 { if ( strlen(kp) == len && !strncmp(kp, buf, len) )
390 return 0; /* name is a keyword */
397 /* Convert a recognized function definition or header to K&R syntax. */
399 convert1(buf, out, header, convert_varargs)
402 int header; /* Boolean */
403 int convert_varargs; /* Boolean */
407 * The breaks table contains pointers to the beginning and end
411 unsigned num_breaks = 2; /* for testing */
417 /* Pre-ANSI implementations don't agree on whether strchr */
418 /* is called strchr or index, so we open-code it here. */
419 for ( endfn = buf; *(endfn++) != '('; )
422 breaks = (char **)malloc(sizeof(char *) * num_breaks * 2);
424 { /* Couldn't allocate break table, give up */
425 fprintf(stderr, "Unable to allocate break table!\n");
429 btop = breaks + num_breaks * 2 - 2;
431 /* Parse the argument list */
439 { /* Filled up break table. */
440 /* Allocate a bigger one and start over. */
441 free((char *)breaks);
446 /* Find the end of the argument */
447 for ( ; end == NULL; p++ )
451 if ( !level ) end = p;
454 if ( !level ) lp = p;
458 if ( --level < 0 ) end = p;
462 p = skipspace(p, 1) - 1;
468 /* Erase any embedded prototype parameters. */
470 writeblanks(lp + 1, rp);
471 p--; /* back up over terminator */
472 /* Find the name being declared. */
473 /* This is complicated because of procedure and */
474 /* array modifiers. */
476 { p = skipspace(p - 1, -1);
479 case ']': /* skip array dimension(s) */
480 case ')': /* skip procedure args OR name */
485 case ']': case ')': level++; break;
486 case '[': case '(': level--; break;
487 case '/': p = skipspace(p, -1) + 1; break;
491 if ( *p == '(' && *skipspace(p + 1, 1) == '*' )
492 { /* We found the name being declared */
493 while ( !isidfirstchar(*p) )
494 p = skipspace(p, 1) + 1;
502 found: if ( *p == '.' && p[-1] == '.' && p[-2] == '.' )
503 { if ( convert_varargs )
504 { *bp++ = "va_alist";
509 if ( bp == breaks + 1 ) /* sole argument */
510 writeblanks(breaks[0], p);
512 writeblanks(bp[-1] - 1, p);
517 { while ( isidchar(*p) ) p--;
522 while ( *p++ == ',' );
524 /* Make a special check for 'void' arglist */
525 if ( bp == breaks+2 )
526 { p = skipspace(breaks[0], 1);
527 if ( !strncmp(p, "void", 4) )
528 { p = skipspace(p+4, 1);
529 if ( p == breaks[2] - 1 )
530 { bp = breaks; /* yup, pretend arglist is empty */
531 writeblanks(breaks[0], p + 1);
535 /* Put out the function name and left parenthesis. */
537 while ( p != endfn ) putc(*p, out), p++;
538 /* Put out the declaration. */
541 for ( p = breaks[0]; *p; p++ )
542 if ( *p == '\r' || *p == '\n' )
546 { for ( ap = breaks+1; ap < bp; ap += 2 )
548 while ( isidchar(*p) )
554 /* Put out the argument declarations */
555 for ( ap = breaks+2; ap <= bp; ap += 2 )
559 fputs(breaks[0], out); /* any prior args */
560 fputs("va_dcl", out); /* the final arg */
564 fputs(breaks[0], out);
566 free((char *)breaks);