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-04-05 changed copyright notice to make it clear that
54 including ansi2knr in a program does not bring the entire
56 lpd 94-12-18 added conditionals for systems where ctype macros
57 don't handle 8-bit characters properly, suggested by
58 Francois Pinard <pinard@iro.umontreal.ca>;
59 removed --varargs switch (this is now the default)
60 lpd 94-10-10 removed CONFIG_BROKETS conditional
61 lpd 94-07-16 added some conditionals to help GNU `configure',
62 suggested by Francois Pinard <pinard@iro.umontreal.ca>;
63 properly erase prototype args in function parameters,
64 contributed by Jim Avera <jima@netcom.com>;
65 correct error in writeblanks (it shouldn't erase EOLs)
66 lpd 89-xx-xx original version
69 /* Most of the conditionals here are to make ansi2knr work with */
70 /* the GNU configure machinery. */
82 For properly autoconfiguring ansi2knr, use AC_CONFIG_HEADER(config.h).
83 This will define HAVE_CONFIG_H and so, activate the following lines.
86 # if STDC_HEADERS || HAVE_STRING_H
92 #else /* not HAVE_CONFIG_H */
95 Without AC_CONFIG_HEADER, merely use <string.h> as in the original
96 Ghostscript distribution. This loses on older BSD systems.
101 #endif /* not HAVE_CONFIG_H */
107 malloc and free should be declared in stdlib.h,
108 but if you've got a K&R compiler, they probably aren't.
115 * The ctype macros don't always handle 8-bit characters correctly.
116 * Compensate for this here.
119 # define STDC_HEADERS 0
122 # undef HAVE_ISASCII /* just in case */
123 # define HAVE_ISASCII 1
125 # ifndef HAVE_ISASCII
126 # define HAVE_ISASCII 0
129 #if STDC_HEADERS || !HAVE_ISASCII
130 # define is_ascii(c) 1
132 # define is_ascii(c) isascii(c)
135 #define is_space(c) (is_ascii(c) && isspace(c))
136 #define is_alpha(c) (is_ascii(c) && isalpha(c))
137 #define is_alnum(c) (is_ascii(c) && isalnum(c))
139 /* Scanning macros */
140 #define isidchar(ch) (is_alnum(ch) || (ch) == '_')
141 #define isidfirstchar(ch) (is_alpha(ch) || (ch) == '_')
143 /* Forward references */
149 /* The main program */
155 #define bufsize 5000 /* arbitrary size */
159 * In previous versions, ansi2knr recognized a --varargs switch.
160 * If this switch was supplied, ansi2knr would attempt to convert
161 * a ... argument to va_alist and va_dcl; if this switch was not
162 * supplied, ansi2knr would simply drop any such arguments.
163 * Now, ansi2knr always does this conversion, and we only
164 * check for this switch for backward compatibility.
166 int convert_varargs
= 1;
168 if ( argc
> 1 && argv
[1][0] == '-' )
169 { if ( !strcmp(argv
[1], "--varargs") )
170 { convert_varargs
= 1;
175 { fprintf(stderr
, "Unrecognized switch: %s\n", argv
[1]);
182 printf("Usage: ansi2knr input_file [output_file]\n");
188 out
= fopen(argv
[2], "w");
190 { fprintf(stderr
, "Cannot open output file %s\n", argv
[2]);
194 in
= fopen(argv
[1], "r");
196 { fprintf(stderr
, "Cannot open input file %s\n", argv
[1]);
199 fprintf(out
, "#line 1 \"%s\"\n", argv
[1]);
200 buf
= malloc(bufsize
);
202 while ( fgets(line
, (unsigned)(buf
+ bufsize
- line
), in
) != NULL
)
203 { switch ( test1(buf
) )
205 case 2: /* a function header */
206 convert1(buf
, out
, 1, convert_varargs
);
208 case 1: /* a function */
209 convert1(buf
, out
, 0, convert_varargs
);
211 case -1: /* maybe the start of a function */
212 line
= buf
+ strlen(buf
);
213 if ( line
!= buf
+ (bufsize
- 1) ) /* overflow check */
216 default: /* not a function */
222 if ( line
!= buf
) fputs(buf
, out
);
229 /* Skip over space and comments, in either direction. */
233 register int dir
; /* 1 for forward, -1 for backward */
235 { while ( is_space(*p
) ) p
+= dir
;
236 if ( !(*p
== '/' && p
[dir
] == '*') ) break;
238 while ( !(*p
== '*' && p
[dir
] == '/') )
239 { if ( *p
== 0 ) return p
; /* multi-line comment?? */
248 * Write blanks over part of a string.
249 * Don't overwrite end-of-line characters.
252 writeblanks(start
, end
)
256 for ( p
= start
; p
< end
; p
++ )
257 if ( *p
!= '\r' && *p
!= '\n' ) *p
= ' ';
261 * Test whether the string in buf is a function definition.
262 * The string may contain and/or end with a newline.
264 * 0 - definitely not a function definition;
265 * 1 - definitely a function definition;
266 * 2 - definitely a function prototype (NOT USED);
267 * -1 - may be the beginning of a function definition,
268 * append another line and look again.
269 * The reason we don't attempt to convert function prototypes is that
270 * Ghostscript's declaration-generating macros look too much like
271 * prototypes, and confuse the algorithms.
276 { register char *p
= buf
;
280 if ( !isidfirstchar(*p
) )
281 return 0; /* no name at left margin */
282 bend
= skipspace(buf
+ strlen(buf
) - 1, -1);
285 case ';': contin
= 0 /*2*/; break;
286 case ')': contin
= 1; break;
287 case '{': return 0; /* not a function */
288 case '}': return 0; /* not a function */
289 default: contin
= -1;
291 while ( isidchar(*p
) ) p
++;
295 return 0; /* not a function */
298 return 0; /* no parameters */
299 /* Check that the apparent function name isn't a keyword. */
300 /* We only need to check for keywords that could be followed */
301 /* by a left parenthesis (which, unfortunately, is most of them). */
302 { static char *words
[] =
303 { "asm", "auto", "case", "char", "const", "double",
304 "extern", "float", "for", "if", "int", "long",
305 "register", "return", "short", "signed", "sizeof",
306 "static", "switch", "typedef", "unsigned",
307 "void", "volatile", "while", 0
311 int len
= endfn
- buf
;
312 while ( (kp
= *key
) != 0 )
313 { if ( strlen(kp
) == len
&& !strncmp(kp
, buf
, len
) )
314 return 0; /* name is a keyword */
321 /* Convert a recognized function definition or header to K&R syntax. */
323 convert1(buf
, out
, header
, convert_varargs
)
326 int header
; /* Boolean */
327 int convert_varargs
; /* Boolean */
331 unsigned num_breaks
= 2; /* for testing */
336 /* Pre-ANSI implementations don't agree on whether strchr */
337 /* is called strchr or index, so we open-code it here. */
338 for ( endfn
= buf
; *(endfn
++) != '('; ) ;
340 breaks
= (char **)malloc(sizeof(char *) * num_breaks
* 2);
342 { /* Couldn't allocate break table, give up */
343 fprintf(stderr
, "Unable to allocate break table!\n");
347 btop
= breaks
+ num_breaks
* 2 - 2;
349 /* Parse the argument list */
356 { /* Filled up break table. */
357 /* Allocate a bigger one and start over. */
358 free((char *)breaks
);
363 /* Find the end of the argument */
364 for ( ; end
== NULL
; p
++ )
368 if ( !level
) end
= p
;
371 if ( !level
) lp
= p
;
375 if ( --level
< 0 ) end
= p
;
379 p
= skipspace(p
, 1) - 1;
385 /* Erase any embedded prototype parameters. */
387 writeblanks(lp
+ 1, rp
);
388 p
--; /* back up over terminator */
389 /* Find the name being declared. */
390 /* This is complicated because of procedure and */
391 /* array modifiers. */
393 { p
= skipspace(p
- 1, -1);
396 case ']': /* skip array dimension(s) */
397 case ')': /* skip procedure args OR name */
402 case ']': case ')': level
++; break;
403 case '[': case '(': level
--; break;
404 case '/': p
= skipspace(p
, -1) + 1; break;
408 if ( *p
== '(' && *skipspace(p
+ 1, 1) == '*' )
409 { /* We found the name being declared */
410 while ( !isidfirstchar(*p
) )
411 p
= skipspace(p
, 1) + 1;
418 found
: if ( *p
== '.' && p
[-1] == '.' && p
[-2] == '.' )
419 { if ( convert_varargs
)
420 { *bp
++ = "va_alist";
425 if ( bp
== breaks
+ 1 ) /* sole argument */
426 writeblanks(breaks
[0], p
);
428 writeblanks(bp
[-1] - 1, p
);
433 { while ( isidchar(*p
) ) p
--;
438 while ( *p
++ == ',' );
440 /* Make a special check for 'void' arglist */
441 if ( bp
== breaks
+2 )
442 { p
= skipspace(breaks
[0], 1);
443 if ( !strncmp(p
, "void", 4) )
444 { p
= skipspace(p
+4, 1);
445 if ( p
== breaks
[2] - 1 )
446 { bp
= breaks
; /* yup, pretend arglist is empty */
447 writeblanks(breaks
[0], p
+ 1);
451 /* Put out the function name and left parenthesis. */
453 while ( p
!= endfn
) putc(*p
, out
), p
++;
454 /* Put out the declaration. */
457 for ( p
= breaks
[0]; *p
; p
++ )
458 if ( *p
== '\r' || *p
== '\n' )
462 { for ( ap
= breaks
+1; ap
< bp
; ap
+= 2 )
464 while ( isidchar(*p
) )
470 /* Put out the argument declarations */
471 for ( ap
= breaks
+2; ap
<= bp
; ap
+= 2 )
475 fputs(breaks
[0], out
); /* any prior args */
476 fputs("va_dcl", out
); /* the final arg */
480 fputs(breaks
[0], out
);
482 free((char *)breaks
);
486 --------------1BA817596D5A
487 Content
-Type
: text
/plain
; charset
=us
-ascii
488 Content
-Transfer
-Encoding
: 7bit
489 Content
-Disposition
: inline; filename
="ansi2knr.c"
491 /* Copyright (C) 1989, 1991, 1993, 1994, 1995 Aladdin Enterprises. All rights reserved. */
494 /* Convert ANSI C function definitions to K&R ("traditional C") syntax */
497 ansi2knr is distributed in the hope that it will be useful, but WITHOUT ANY
498 WARRANTY. No author or distributor accepts responsibility to anyone for the
499 consequences of using it or for whether it serves any particular purpose or
500 works at all, unless he says so in writing. Refer to the GNU General Public
501 License (the "GPL") for full details.
503 Everyone is granted permission to copy, modify and redistribute ansi2knr,
504 but only under the conditions described in the GPL. A copy of this license
505 is supposed to have been given to you along with ansi2knr so you can know
506 your rights and responsibilities. It should be in a file named COPYLEFT.
507 Among other things, the copyright notice and this notice must be preserved
510 We explicitly state here what we believe is already implied by the GPL: if
511 the ansi2knr program is distributed as a separate set of sources and a
512 separate executable file which are aggregated on a storage medium together
513 with another program, this in itself does not bring the other program under
514 the GPL, nor does the mere fact that such a program or the procedures for
515 constructing it invoke the ansi2knr executable bring any other part of the
516 program under the GPL.
521 ansi2knr input_file [output_file]
522 * If no output_file is supplied, output goes to stdout.
523 * There are no error messages.
525 * ansi2knr recognizes function definitions by seeing a non-keyword
526 * identifier at the left margin, followed by a left parenthesis,
527 * with a right parenthesis as the last character on the line.
528 * It will recognize a multi-line header provided that the last character
529 * of the last line of the header is a right parenthesis,
530 * and no intervening line ends with a left or right brace or a semicolon.
531 * These algorithms ignore whitespace and comments, except that
532 * the function name must be the first thing on the line.
533 * The following constructs will confuse it:
534 * - Any other construct that starts at the left margin and
535 * follows the above syntax (such as a macro or function call).
536 * - Macros that tinker with the syntax of the function header.
540 * The original and principal author of ansi2knr is L. Peter Deutsch
541 * <ghost@aladdin.com>. Other authors are noted in the change history
542 * that follows (in reverse chronological order):
543 lpd 95-04-05 changed copyright notice to make it clear that
544 including ansi2knr in a program does not bring the entire
545 program under the GPL
546 lpd 94-12-18 added conditionals for systems where ctype macros
547 don't handle 8-bit characters properly, suggested by
548 Francois Pinard <pinard@iro.umontreal.ca>;
549 removed --varargs switch (this is now the default)
550 lpd 94-10-10 removed CONFIG_BROKETS conditional
551 lpd 94-07-16 added some conditionals to help GNU `configure',
552 suggested by Francois Pinard <pinard@iro.umontreal.ca>;
553 properly erase prototype args in function parameters,
554 contributed by Jim Avera <jima@netcom.com>;
555 correct error in writeblanks (it shouldn't erase EOLs)
556 lpd 89-xx-xx original version
559 /* Most of the conditionals here are to make ansi2knr work with */
560 /* the GNU configure machinery. */
572 For properly autoconfiguring ansi2knr, use AC_CONFIG_HEADER(config.h).
573 This will define HAVE_CONFIG_H and so, activate the following lines.
576 # if STDC_HEADERS || HAVE_STRING_H
579 # include <strings.h>
582 #else /* not HAVE_CONFIG_H */
585 Without AC_CONFIG_HEADER, merely use <string.h> as in the original
586 Ghostscript distribution. This loses on older BSD systems.
591 #endif /* not HAVE_CONFIG_H */
597 malloc and free should be declared in stdlib.h,
598 but if you've got a K&R compiler, they probably aren't.
605 * The ctype macros don't always handle 8-bit characters correctly.
606 * Compensate for this here.
609 # define STDC_HEADERS 0
612 # undef HAVE_ISASCII /* just in case */
613 # define HAVE_ISASCII 1
615 # ifndef HAVE_ISASCII
616 # define HAVE_ISASCII 0
619 #if STDC_HEADERS || !HAVE_ISASCII
620 # define is_ascii(c) 1
622 # define is_ascii(c) isascii(c)
625 #define is_space(c) (is_ascii(c) && isspace(c))
626 #define is_alpha(c) (is_ascii(c) && isalpha(c))
627 #define is_alnum(c) (is_ascii(c) && isalnum(c))
629 /* Scanning macros */
630 #define isidchar(ch) (is_alnum(ch) || (ch) == '_')
631 #define isidfirstchar(ch) (is_alpha(ch) || (ch) == '_')
633 /* Forward references */
639 /* The main program */
645 #define bufsize 5000 /* arbitrary size */
649 * In previous versions, ansi2knr recognized a --varargs switch.
650 * If this switch was supplied, ansi2knr would attempt to convert
651 * a ... argument to va_alist and va_dcl; if this switch was not
652 * supplied, ansi2knr would simply drop any such arguments.
653 * Now, ansi2knr always does this conversion, and we only
654 * check for this switch for backward compatibility.
656 int convert_varargs
= 1;
658 if ( argc
> 1 && argv
[1][0] == '-' )
659 { if ( !strcmp(argv
[1], "--varargs") )
660 { convert_varargs
= 1;
665 { fprintf(stderr
, "Unrecognized switch: %s\n", argv
[1]);
672 printf("Usage: ansi2knr input_file [output_file]\n");
678 out
= fopen(argv
[2], "w");
680 { fprintf(stderr
, "Cannot open output file %s\n", argv
[2]);
684 in
= fopen(argv
[1], "r");
686 { fprintf(stderr
, "Cannot open input file %s\n", argv
[1]);
689 fprintf(out
, "#line 1 \"%s\"\n", argv
[1]);
690 buf
= malloc(bufsize
);
692 while ( fgets(line
, (unsigned)(buf
+ bufsize
- line
), in
) != NULL
)
693 { switch ( test1(buf
) )
695 case 2: /* a function header */
696 convert1(buf
, out
, 1, convert_varargs
);
698 case 1: /* a function */
699 convert1(buf
, out
, 0, convert_varargs
);
701 case -1: /* maybe the start of a function */
702 line
= buf
+ strlen(buf
);
703 if ( line
!= buf
+ (bufsize
- 1) ) /* overflow check */
706 default: /* not a function */
712 if ( line
!= buf
) fputs(buf
, out
);
719 /* Skip over space and comments, in either direction. */
723 register int dir
; /* 1 for forward, -1 for backward */
725 { while ( is_space(*p
) ) p
+= dir
;
726 if ( !(*p
== '/' && p
[dir
] == '*') ) break;
728 while ( !(*p
== '*' && p
[dir
] == '/') )
729 { if ( *p
== 0 ) return p
; /* multi-line comment?? */
738 * Write blanks over part of a string.
739 * Don't overwrite end-of-line characters.
742 writeblanks(start
, end
)
746 for ( p
= start
; p
< end
; p
++ )
747 if ( *p
!= '\r' && *p
!= '\n' ) *p
= ' ';
751 * Test whether the string in buf is a function definition.
752 * The string may contain and/or end with a newline.
754 * 0 - definitely not a function definition;
755 * 1 - definitely a function definition;
756 * 2 - definitely a function prototype (NOT USED);
757 * -1 - may be the beginning of a function definition,
758 * append another line and look again.
759 * The reason we don't attempt to convert function prototypes is that
760 * Ghostscript's declaration-generating macros look too much like
761 * prototypes, and confuse the algorithms.
766 { register char *p
= buf
;
770 if ( !isidfirstchar(*p
) )
771 return 0; /* no name at left margin */
772 bend
= skipspace(buf
+ strlen(buf
) - 1, -1);
775 case ';': contin
= 0 /*2*/; break;
776 case ')': contin
= 1; break;
777 case '{': return 0; /* not a function */
778 case '}': return 0; /* not a function */
779 default: contin
= -1;
781 while ( isidchar(*p
) ) p
++;
785 return 0; /* not a function */
788 return 0; /* no parameters */
789 /* Check that the apparent function name isn't a keyword. */
790 /* We only need to check for keywords that could be followed */
791 /* by a left parenthesis (which, unfortunately, is most of them). */
792 { static char *words
[] =
793 { "asm", "auto", "case", "char", "const", "double",
794 "extern", "float", "for", "if", "int", "long",
795 "register", "return", "short", "signed", "sizeof",
796 "static", "switch", "typedef", "unsigned",
797 "void", "volatile", "while", 0
801 int len
= endfn
- buf
;
802 while ( (kp
= *key
) != 0 )
803 { if ( strlen(kp
) == len
&& !strncmp(kp
, buf
, len
) )
804 return 0; /* name is a keyword */
811 /* Convert a recognized function definition or header to K&R syntax. */
813 convert1(buf
, out
, header
, convert_varargs
)
816 int header
; /* Boolean */
817 int convert_varargs
; /* Boolean */
821 unsigned num_breaks
= 2; /* for testing */
826 /* Pre-ANSI implementations don't agree on whether strchr */
827 /* is called strchr or index, so we open-code it here. */
828 for ( endfn
= buf
; *(endfn
++) != '('; ) ;
830 breaks
= (char **)malloc(sizeof(char *) * num_breaks
* 2);
832 { /* Couldn't allocate break table, give up */
833 fprintf(stderr
, "Unable to allocate break table!\n");
837 btop
= breaks
+ num_breaks
* 2 - 2;
839 /* Parse the argument list */
846 { /* Filled up break table. */
847 /* Allocate a bigger one and start over. */
848 free((char *)breaks
);
853 /* Find the end of the argument */
854 for ( ; end
== NULL
; p
++ )
858 if ( !level
) end
= p
;
861 if ( !level
) lp
= p
;
865 if ( --level
< 0 ) end
= p
;
869 p
= skipspace(p
, 1) - 1;
875 /* Erase any embedded prototype parameters. */
877 writeblanks(lp
+ 1, rp
);
878 p
--; /* back up over terminator */
879 /* Find the name being declared. */
880 /* This is complicated because of procedure and */
881 /* array modifiers. */
883 { p
= skipspace(p
- 1, -1);
886 case ']': /* skip array dimension(s) */
887 case ')': /* skip procedure args OR name */
892 case ']': case ')': level
++; break;
893 case '[': case '(': level
--; break;
894 case '/': p
= skipspace(p
, -1) + 1; break;
898 if ( *p
== '(' && *skipspace(p
+ 1, 1) == '*' )
899 { /* We found the name being declared */
900 while ( !isidfirstchar(*p
) )
901 p
= skipspace(p
, 1) + 1;
908 found
: if ( *p
== '.' && p
[-1] == '.' && p
[-2] == '.' )
909 { if ( convert_varargs
)
910 { *bp
++ = "va_alist";
915 if ( bp
== breaks
+ 1 ) /* sole argument */
916 writeblanks(breaks
[0], p
);
918 writeblanks(bp
[-1] - 1, p
);
923 { while ( isidchar(*p
) ) p
--;
928 while ( *p
++ == ',' );
930 /* Make a special check for 'void' arglist */
931 if ( bp
== breaks
+2 )
932 { p
= skipspace(breaks
[0], 1);
933 if ( !strncmp(p
, "void", 4) )
934 { p
= skipspace(p
+4, 1);
935 if ( p
== breaks
[2] - 1 )
936 { bp
= breaks
; /* yup, pretend arglist is empty */
937 writeblanks(breaks
[0], p
+ 1);
941 /* Put out the function name and left parenthesis. */
943 while ( p
!= endfn
) putc(*p
, out
), p
++;
944 /* Put out the declaration. */
947 for ( p
= breaks
[0]; *p
; p
++ )
948 if ( *p
== '\r' || *p
== '\n' )
952 { for ( ap
= breaks
+1; ap
< bp
; ap
+= 2 )
954 while ( isidchar(*p
) )
960 /* Put out the argument declarations */
961 for ( ap
= breaks
+2; ap
<= bp
; ap
+= 2 )
965 fputs(breaks
[0], out
); /* any prior args */
966 fputs("va_dcl", out
); /* the final arg */
970 fputs(breaks
[0], out
);
972 free((char *)breaks
);