/* Copyright (C) 1989, 1991 Aladdin Enterprises. All rights reserved.
Distributed by Free Software Foundation, Inc.
This file is part of Ghostscript.
Ghostscript is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY. No author or distributor accepts responsibility
to anyone for the consequences of using it or for whether it serves any
particular purpose or works at all, unless he says so in writing. Refer
to the Ghostscript General Public License for full details.
Everyone is granted permission to copy, modify and redistribute
Ghostscript, but only under the conditions described in the Ghostscript
General Public License. A copy of this license is supposed to have been
given to you along with Ghostscript so you can know your rights and
responsibilities. It should be in a file named COPYING. Among other
things, the copyright notice and this notice must be preserved on all
/* Convert ANSI function declarations to K&R syntax */
ansi2knr input_file output_file
* If no output_file is supplied, output goes to stdout.
* There are no error messages.
* ansi2knr recognizes functions by seeing a non-keyword identifier
* at the left margin, followed by a left parenthesis,
* with a right parenthesis as the last character on the line.
* It will recognize a multi-line header if the last character
* on each line but the last is a left parenthesis or comma.
* These algorithms ignore whitespace and comments, except that
* the function name must be the first thing on the line.
* The following constructs will confuse it:
- Any other construct that starts at the left margin and
follows the above syntax (such as a macro or function call).
- Macros that tinker with the syntax of the function header.
#define isidchar(ch) (isalnum(ch) || (ch) == '_')
#define isidfirstchar(ch) (isalpha(ch) || (ch) == '_')
#define bufsize 500 /* arbitrary size */
printf("Usage: ansi2knr input_file [output_file]\n");
out
= fopen(argv
[2], "w");
{ fprintf(stderr
, "Cannot open %s\n", argv
[2]);
in
= fopen(argv
[1], "r");
{ fprintf(stderr
, "Cannot open %s\n", argv
[1]);
fprintf(out
, "#line 1 \"%s\"\n", argv
[1]);
while ( fgets(line
, (unsigned)(buf
+ bufsize
- line
), in
) != NULL
)
case -1: /* maybe the start of a function */
line
= buf
+ strlen(buf
);
default: /* not a function */
if ( line
!= buf
) fputs(buf
, out
);
/* Skip over space and comments, in either direction. */
register int dir
; /* 1 for forward, -1 for backward */
{ while ( isspace(*p
) ) p
+= dir
;
if ( !(*p
== '/' && p
[dir
] == '*') ) break;
while ( !(*p
== '*' && p
[dir
] == '/') )
{ if ( *p
== 0 ) return p
; /* multi-line comment?? */
* Write blanks over part of a string.
for ( p
= start
; p
< end
; p
++ ) *p
= ' ';
* Test whether the string in buf is a function definition.
* The string may contain and/or end with a newline.
* 0 - definitely not a function definition;
* 1 - definitely a function definition;
* -1 - may be the beginning of a function definition,
* append another line and look again.
{ register char *p
= buf
;
if ( !isidfirstchar(*p
) )
return 0; /* no name at left margin */
bend
= skipspace(buf
+ strlen(buf
) - 1, -1);
case ')': contin
= 1; break;
case ',': contin
= -1; break;
default: return 0; /* not a function */
while ( isidchar(*p
) ) p
++;
return 0; /* not a function */
return 0; /* no parameters */
/* Check that the apparent function name isn't a keyword. */
/* We only need to check for keywords that could be followed */
/* by a left parenthesis (which, unfortunately, is most of them). */
{ "asm", "auto", "case", "char", "const", "double",
"extern", "float", "for", "if", "int", "long",
"register", "return", "short", "signed", "sizeof",
"static", "switch", "typedef", "unsigned",
"void", "volatile", "while", 0
while ( (kp
= *key
) != 0 )
{ if ( strlen(kp
) == len
&& !strncmp(kp
, buf
, len
) )
return 0; /* name is a keyword */
{ char *endfn
= strchr(buf
, '(') + 1;
unsigned num_breaks
= 2; /* for testing */
breaks
= (char **)malloc(sizeof(char *) * num_breaks
* 2);
{ /* Couldn't allocate break table, give up */
fprintf(stderr
, "Unable to allocate break table!\n");
btop
= breaks
+ num_breaks
* 2 - 2;
/* Parse the argument list */
{ /* Filled up break table. */
/* Allocate a bigger one and start over. */
/* Find the end of the argument */
for ( ; end
== NULL
; p
++ )
case ',': if ( !level
) end
= p
; break;
case '(': level
++; break;
case ')': if ( --level
< 0 ) end
= p
; break;
case '/': p
= skipspace(p
, 1) - 1; break;
p
--; /* back up over terminator */
/* Find the name being declared. */
/* This is complicated because of procedure and */
{ p
= skipspace(p
- 1, -1);
case ']': /* skip array dimension(s) */
case ')': /* skip procedure args OR name */
case ']': case ')': level
++; break;
case '[': case '(': level
--; break;
case '/': p
= skipspace(p
, -1) + 1; break;
if ( *p
== '(' && *skipspace(p
+ 1, 1) == '*' )
{ /* We found the name being declared */
while ( !isidfirstchar(*p
) )
found
: if ( *p
== '.' && p
[-1] == '.' && p
[-2] == '.' )
if ( bp
== breaks
+ 1 ) /* sole argument */
writeblanks(breaks
[0], p
);
writeblanks(bp
[-1] - 1, p
);
{ while ( isidchar(*p
) ) p
--;
/* Make a special check for 'void' arglist */
{ p
= skipspace(breaks
[0], 1);
if ( !strncmp(p
, "void", 4) )
if ( p
== breaks
[2] - 1 )
{ bp
= breaks
; /* yup, pretend arglist is empty */
writeblanks(breaks
[0], p
+ 1);
/* Put out the function name */
while ( p
!= endfn
) putc(*p
, out
), p
++;
/* Put out the declaration */
for ( ap
= breaks
+1; ap
< bp
; ap
+= 2 )
while ( isidchar(*p
) ) putc(*p
, out
), p
++;
if ( ap
< bp
- 1 ) fputs(", ", out
);
/* Put out the argument declarations */
for ( ap
= breaks
+2; ap
<= bp
; ap
+= 2 ) (*ap
)[-1] = ';';