More doc fixes
[automake.git] / ansi2knr.c
blobf581c071a62edeca756fdf012849bf93c7654931
1 /* Copyright (C) 1989, 1991, 1993, 1994, 1995 Aladdin Enterprises. All rights reserved. */
3 /* ansi2knr.c */
4 /* Convert ANSI C function definitions to K&R ("traditional C") syntax */
6 /*
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
18 on all copies.
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.
30 * Usage:
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
55 program under the GPL
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. */
72 #ifdef HAVE_CONFIG_H
73 # include <config.h>
74 #endif
76 #include <stdio.h>
77 #include <ctype.h>
79 #ifdef HAVE_CONFIG_H
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
87 # include <string.h>
88 # else
89 # include <strings.h>
90 # endif
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.
99 # include <string.h>
101 #endif /* not HAVE_CONFIG_H */
103 #ifdef STDC_HEADERS
104 # include <stdlib.h>
105 #else
107 malloc and free should be declared in stdlib.h,
108 but if you've got a K&R compiler, they probably aren't.
110 char *malloc();
111 void free();
112 #endif
115 * The ctype macros don't always handle 8-bit characters correctly.
116 * Compensate for this here.
118 #ifndef STDC_HEADERS
119 # define STDC_HEADERS 0
120 #endif
121 #ifdef isascii
122 # undef HAVE_ISASCII /* just in case */
123 # define HAVE_ISASCII 1
124 #else
125 # ifndef HAVE_ISASCII
126 # define HAVE_ISASCII 0
127 # endif
128 #endif
129 #if STDC_HEADERS || !HAVE_ISASCII
130 # define is_ascii(c) 1
131 #else
132 # define is_ascii(c) isascii(c)
133 #endif
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 */
144 char *skipspace();
145 void writeblanks();
146 int test1();
147 int convert1();
149 /* The main program */
151 main(argc, argv)
152 int argc;
153 char *argv[];
154 { FILE *in, *out;
155 #define bufsize 5000 /* arbitrary size */
156 char *buf;
157 char *line;
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;
171 argc--;
172 argv++;
174 else
175 { fprintf(stderr, "Unrecognized switch: %s\n", argv[1]);
176 exit(1);
179 switch ( argc )
181 default:
182 printf("Usage: ansi2knr input_file [output_file]\n");
183 exit(0);
184 case 2:
185 out = stdout;
186 break;
187 case 3:
188 out = fopen(argv[2], "w");
189 if ( out == NULL )
190 { fprintf(stderr, "Cannot open output file %s\n", argv[2]);
191 exit(1);
194 in = fopen(argv[1], "r");
195 if ( in == NULL )
196 { fprintf(stderr, "Cannot open input file %s\n", argv[1]);
197 exit(1);
199 fprintf(out, "#line 1 \"%s\"\n", argv[1]);
200 buf = malloc(bufsize);
201 line = buf;
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);
207 break;
208 case 1: /* a function */
209 convert1(buf, out, 0, convert_varargs);
210 break;
211 case -1: /* maybe the start of a function */
212 line = buf + strlen(buf);
213 if ( line != buf + (bufsize - 1) ) /* overflow check */
214 continue;
215 /* falls through */
216 default: /* not a function */
217 fputs(buf, out);
218 break;
220 line = buf;
222 if ( line != buf ) fputs(buf, out);
223 free(buf);
224 fclose(out);
225 fclose(in);
226 return 0;
229 /* Skip over space and comments, in either direction. */
230 char *
231 skipspace(p, dir)
232 register char *p;
233 register int dir; /* 1 for forward, -1 for backward */
234 { for ( ; ; )
235 { while ( is_space(*p) ) p += dir;
236 if ( !(*p == '/' && p[dir] == '*') ) break;
237 p += dir; p += dir;
238 while ( !(*p == '*' && p[dir] == '/') )
239 { if ( *p == 0 ) return p; /* multi-line comment?? */
240 p += dir;
242 p += dir; p += dir;
244 return p;
248 * Write blanks over part of a string.
249 * Don't overwrite end-of-line characters.
251 void
252 writeblanks(start, end)
253 char *start;
254 char *end;
255 { char *p;
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.
263 * Return as follows:
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.
274 test1(buf)
275 char *buf;
276 { register char *p = buf;
277 char *bend;
278 char *endfn;
279 int contin;
280 if ( !isidfirstchar(*p) )
281 return 0; /* no name at left margin */
282 bend = skipspace(buf + strlen(buf) - 1, -1);
283 switch ( *bend )
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++;
292 endfn = p;
293 p = skipspace(p, 1);
294 if ( *p++ != '(' )
295 return 0; /* not a function */
296 p = skipspace(p, 1);
297 if ( *p == ')' )
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
309 char **key = words;
310 char *kp;
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 */
315 key++;
318 return contin;
321 /* Convert a recognized function definition or header to K&R syntax. */
323 convert1(buf, out, header, convert_varargs)
324 char *buf;
325 FILE *out;
326 int header; /* Boolean */
327 int convert_varargs; /* Boolean */
328 { char *endfn;
329 register char *p;
330 char **breaks;
331 unsigned num_breaks = 2; /* for testing */
332 char **btop;
333 char **bp;
334 char **ap;
335 char *vararg = 0;
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++) != '('; ) ;
339 top: p = endfn;
340 breaks = (char **)malloc(sizeof(char *) * num_breaks * 2);
341 if ( breaks == 0 )
342 { /* Couldn't allocate break table, give up */
343 fprintf(stderr, "Unable to allocate break table!\n");
344 fputs(buf, out);
345 return -1;
347 btop = breaks + num_breaks * 2 - 2;
348 bp = breaks;
349 /* Parse the argument list */
351 { int level = 0;
352 char *lp = NULL;
353 char *rp;
354 char *end = NULL;
355 if ( bp >= btop )
356 { /* Filled up break table. */
357 /* Allocate a bigger one and start over. */
358 free((char *)breaks);
359 num_breaks <<= 1;
360 goto top;
362 *bp++ = p;
363 /* Find the end of the argument */
364 for ( ; end == NULL; p++ )
365 { switch(*p)
367 case ',':
368 if ( !level ) end = p;
369 break;
370 case '(':
371 if ( !level ) lp = p;
372 level++;
373 break;
374 case ')':
375 if ( --level < 0 ) end = p;
376 else rp = p;
377 break;
378 case '/':
379 p = skipspace(p, 1) - 1;
380 break;
381 default:
385 /* Erase any embedded prototype parameters. */
386 if ( lp )
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. */
392 for ( ; ; )
393 { p = skipspace(p - 1, -1);
394 switch ( *p )
396 case ']': /* skip array dimension(s) */
397 case ')': /* skip procedure args OR name */
398 { int level = 1;
399 while ( level )
400 switch ( *--p )
402 case ']': case ')': level++; break;
403 case '[': case '(': level--; break;
404 case '/': p = skipspace(p, -1) + 1; break;
405 default: ;
408 if ( *p == '(' && *skipspace(p + 1, 1) == '*' )
409 { /* We found the name being declared */
410 while ( !isidfirstchar(*p) )
411 p = skipspace(p, 1) + 1;
412 goto found;
414 break;
415 default: goto found;
418 found: if ( *p == '.' && p[-1] == '.' && p[-2] == '.' )
419 { if ( convert_varargs )
420 { *bp++ = "va_alist";
421 vararg = p-2;
423 else
424 { p++;
425 if ( bp == breaks + 1 ) /* sole argument */
426 writeblanks(breaks[0], p);
427 else
428 writeblanks(bp[-1] - 1, p);
429 bp--;
432 else
433 { while ( isidchar(*p) ) p--;
434 *bp++ = p+1;
436 p = end;
438 while ( *p++ == ',' );
439 *bp = 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. */
452 p = buf;
453 while ( p != endfn ) putc(*p, out), p++;
454 /* Put out the declaration. */
455 if ( header )
456 { fputs(");", out);
457 for ( p = breaks[0]; *p; p++ )
458 if ( *p == '\r' || *p == '\n' )
459 putc(*p, out);
461 else
462 { for ( ap = breaks+1; ap < bp; ap += 2 )
463 { p = *ap;
464 while ( isidchar(*p) )
465 putc(*p, out), p++;
466 if ( ap < bp - 1 )
467 fputs(", ", out);
469 fputs(") ", out);
470 /* Put out the argument declarations */
471 for ( ap = breaks+2; ap <= bp; ap += 2 )
472 (*ap)[-1] = ';';
473 if ( vararg != 0 )
474 { *vararg = 0;
475 fputs(breaks[0], out); /* any prior args */
476 fputs("va_dcl", out); /* the final arg */
477 fputs(bp[0], out);
479 else
480 fputs(breaks[0], out);
482 free((char *)breaks);
483 return 0;
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. */
493 /* ansi2knr.c */
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
508 on all copies.
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.
520 * Usage:
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. */
562 #ifdef HAVE_CONFIG_H
563 # include <config.h>
564 #endif
566 #include <stdio.h>
567 #include <ctype.h>
569 #ifdef HAVE_CONFIG_H
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
577 # include <string.h>
578 # else
579 # include <strings.h>
580 # endif
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.
589 # include <string.h>
591 #endif /* not HAVE_CONFIG_H */
593 #ifdef STDC_HEADERS
594 # include <stdlib.h>
595 #else
597 malloc and free should be declared in stdlib.h,
598 but if you've got a K&R compiler, they probably aren't.
600 char *malloc();
601 void free();
602 #endif
605 * The ctype macros don't always handle 8-bit characters correctly.
606 * Compensate for this here.
608 #ifndef STDC_HEADERS
609 # define STDC_HEADERS 0
610 #endif
611 #ifdef isascii
612 # undef HAVE_ISASCII /* just in case */
613 # define HAVE_ISASCII 1
614 #else
615 # ifndef HAVE_ISASCII
616 # define HAVE_ISASCII 0
617 # endif
618 #endif
619 #if STDC_HEADERS || !HAVE_ISASCII
620 # define is_ascii(c) 1
621 #else
622 # define is_ascii(c) isascii(c)
623 #endif
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 */
634 char *skipspace();
635 void writeblanks();
636 int test1();
637 int convert1();
639 /* The main program */
641 main(argc, argv)
642 int argc;
643 char *argv[];
644 { FILE *in, *out;
645 #define bufsize 5000 /* arbitrary size */
646 char *buf;
647 char *line;
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;
661 argc--;
662 argv++;
664 else
665 { fprintf(stderr, "Unrecognized switch: %s\n", argv[1]);
666 exit(1);
669 switch ( argc )
671 default:
672 printf("Usage: ansi2knr input_file [output_file]\n");
673 exit(0);
674 case 2:
675 out = stdout;
676 break;
677 case 3:
678 out = fopen(argv[2], "w");
679 if ( out == NULL )
680 { fprintf(stderr, "Cannot open output file %s\n", argv[2]);
681 exit(1);
684 in = fopen(argv[1], "r");
685 if ( in == NULL )
686 { fprintf(stderr, "Cannot open input file %s\n", argv[1]);
687 exit(1);
689 fprintf(out, "#line 1 \"%s\"\n", argv[1]);
690 buf = malloc(bufsize);
691 line = buf;
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);
697 break;
698 case 1: /* a function */
699 convert1(buf, out, 0, convert_varargs);
700 break;
701 case -1: /* maybe the start of a function */
702 line = buf + strlen(buf);
703 if ( line != buf + (bufsize - 1) ) /* overflow check */
704 continue;
705 /* falls through */
706 default: /* not a function */
707 fputs(buf, out);
708 break;
710 line = buf;
712 if ( line != buf ) fputs(buf, out);
713 free(buf);
714 fclose(out);
715 fclose(in);
716 return 0;
719 /* Skip over space and comments, in either direction. */
720 char *
721 skipspace(p, dir)
722 register char *p;
723 register int dir; /* 1 for forward, -1 for backward */
724 { for ( ; ; )
725 { while ( is_space(*p) ) p += dir;
726 if ( !(*p == '/' && p[dir] == '*') ) break;
727 p += dir; p += dir;
728 while ( !(*p == '*' && p[dir] == '/') )
729 { if ( *p == 0 ) return p; /* multi-line comment?? */
730 p += dir;
732 p += dir; p += dir;
734 return p;
738 * Write blanks over part of a string.
739 * Don't overwrite end-of-line characters.
741 void
742 writeblanks(start, end)
743 char *start;
744 char *end;
745 { char *p;
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.
753 * Return as follows:
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.
764 test1(buf)
765 char *buf;
766 { register char *p = buf;
767 char *bend;
768 char *endfn;
769 int contin;
770 if ( !isidfirstchar(*p) )
771 return 0; /* no name at left margin */
772 bend = skipspace(buf + strlen(buf) - 1, -1);
773 switch ( *bend )
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++;
782 endfn = p;
783 p = skipspace(p, 1);
784 if ( *p++ != '(' )
785 return 0; /* not a function */
786 p = skipspace(p, 1);
787 if ( *p == ')' )
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
799 char **key = words;
800 char *kp;
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 */
805 key++;
808 return contin;
811 /* Convert a recognized function definition or header to K&R syntax. */
813 convert1(buf, out, header, convert_varargs)
814 char *buf;
815 FILE *out;
816 int header; /* Boolean */
817 int convert_varargs; /* Boolean */
818 { char *endfn;
819 register char *p;
820 char **breaks;
821 unsigned num_breaks = 2; /* for testing */
822 char **btop;
823 char **bp;
824 char **ap;
825 char *vararg = 0;
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++) != '('; ) ;
829 top: p = endfn;
830 breaks = (char **)malloc(sizeof(char *) * num_breaks * 2);
831 if ( breaks == 0 )
832 { /* Couldn't allocate break table, give up */
833 fprintf(stderr, "Unable to allocate break table!\n");
834 fputs(buf, out);
835 return -1;
837 btop = breaks + num_breaks * 2 - 2;
838 bp = breaks;
839 /* Parse the argument list */
841 { int level = 0;
842 char *lp = NULL;
843 char *rp;
844 char *end = NULL;
845 if ( bp >= btop )
846 { /* Filled up break table. */
847 /* Allocate a bigger one and start over. */
848 free((char *)breaks);
849 num_breaks <<= 1;
850 goto top;
852 *bp++ = p;
853 /* Find the end of the argument */
854 for ( ; end == NULL; p++ )
855 { switch(*p)
857 case ',':
858 if ( !level ) end = p;
859 break;
860 case '(':
861 if ( !level ) lp = p;
862 level++;
863 break;
864 case ')':
865 if ( --level < 0 ) end = p;
866 else rp = p;
867 break;
868 case '/':
869 p = skipspace(p, 1) - 1;
870 break;
871 default:
875 /* Erase any embedded prototype parameters. */
876 if ( lp )
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. */
882 for ( ; ; )
883 { p = skipspace(p - 1, -1);
884 switch ( *p )
886 case ']': /* skip array dimension(s) */
887 case ')': /* skip procedure args OR name */
888 { int level = 1;
889 while ( level )
890 switch ( *--p )
892 case ']': case ')': level++; break;
893 case '[': case '(': level--; break;
894 case '/': p = skipspace(p, -1) + 1; break;
895 default: ;
898 if ( *p == '(' && *skipspace(p + 1, 1) == '*' )
899 { /* We found the name being declared */
900 while ( !isidfirstchar(*p) )
901 p = skipspace(p, 1) + 1;
902 goto found;
904 break;
905 default: goto found;
908 found: if ( *p == '.' && p[-1] == '.' && p[-2] == '.' )
909 { if ( convert_varargs )
910 { *bp++ = "va_alist";
911 vararg = p-2;
913 else
914 { p++;
915 if ( bp == breaks + 1 ) /* sole argument */
916 writeblanks(breaks[0], p);
917 else
918 writeblanks(bp[-1] - 1, p);
919 bp--;
922 else
923 { while ( isidchar(*p) ) p--;
924 *bp++ = p+1;
926 p = end;
928 while ( *p++ == ',' );
929 *bp = 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. */
942 p = buf;
943 while ( p != endfn ) putc(*p, out), p++;
944 /* Put out the declaration. */
945 if ( header )
946 { fputs(");", out);
947 for ( p = breaks[0]; *p; p++ )
948 if ( *p == '\r' || *p == '\n' )
949 putc(*p, out);
951 else
952 { for ( ap = breaks+1; ap < bp; ap += 2 )
953 { p = *ap;
954 while ( isidchar(*p) )
955 putc(*p, out), p++;
956 if ( ap < bp - 1 )
957 fputs(", ", out);
959 fputs(") ", out);
960 /* Put out the argument declarations */
961 for ( ap = breaks+2; ap <= bp; ap += 2 )
962 (*ap)[-1] = ';';
963 if ( vararg != 0 )
964 { *vararg = 0;
965 fputs(breaks[0], out); /* any prior args */
966 fputs("va_dcl", out); /* the final arg */
967 fputs(bp[0], out);
969 else
970 fputs(breaks[0], out);
972 free((char *)breaks);
973 return 0;