From: Tom Tromey Date: Fri, 29 Jan 1999 14:22:15 +0000 (+0000) Subject: * ansi2knr.c: New version from L. Peter Deutsch. X-Git-Tag: user-dep-gen-branchpoint~49 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=2e8398624b8021059ff989f5b9f22343a757e7e6;p=thirdparty%2Fautomake.git * ansi2knr.c: New version from L. Peter Deutsch. --- diff --git a/ChangeLog b/ChangeLog index 4faf4572c..768a28c11 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,7 @@ +1999-01-29 Tom Tromey + + * ansi2knr.c: New version from L. Peter Deutsch. + 1999-01-22 Tom Tromey * automake.in (require_file_internal): Correctly examine return diff --git a/ansi2knr.c b/ansi2knr.c index 49624346f..061f69678 100644 --- a/ansi2knr.c +++ b/ansi2knr.c @@ -1,6 +1,6 @@ -/* Copyright (C) 1989, 1997, 1998 Aladdin Enterprises. All rights reserved. */ +/* Copyright (C) 1989, 1997, 1998, 1999 Aladdin Enterprises. All rights reserved. */ -/*$Id: ansi2knr.c,v 1.10 1998/12/02 12:42:23 tromey Exp $*/ +/*$Id: ansi2knr.c,v 1.11 1999/01/29 14:22:15 tromey Exp $*/ /* Convert ANSI C function definitions to K&R ("traditional C") syntax */ /* @@ -51,13 +51,18 @@ program under the GPL. * 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). - * - Some macros that tinker with the syntax of the function header. + * - Some macros that tinker with the syntax of function headers. */ /* * The original and principal author of ansi2knr is L. Peter Deutsch * . Other authors are noted in the change history * that follows (in reverse chronological order): + lpd 1999-01-28 fixed two bugs: a '/' in an argument list caused an + endless loop; quoted strings within an argument list + confused the parser + lpd 1999-01-24 added a check for write errors on the output, + suggested by Jim Meyering lpd 1998-11-09 added further hack to recognize identifier(void) as being a procedure lpd 1998-10-23 added hack to recognize lines consisting of @@ -178,6 +183,7 @@ program under the GPL. /* Forward references */ char *skipspace(); +char *scanstring(); int writeblanks(); int test1(); int convert1(); @@ -190,6 +196,8 @@ main(argc, argv) { FILE *in = stdin; FILE *out = stdout; char *filename = 0; + char *program_name = argv[0]; + char *output_name = 0; #define bufsize 5000 /* arbitrary size */ char *buf; char *line; @@ -205,6 +213,7 @@ main(argc, argv) * check for this switch for backward compatibility. */ int convert_varargs = 1; + int output_error; while ( argc > 1 && argv[1][0] == '-' ) { if ( !strcmp(argv[1], "--varargs") ) { @@ -219,7 +228,8 @@ main(argc, argv) argv += 2; continue; } - fprintf(stderr, "Unrecognized switch: %s\n", argv[1]); + fprintf(stderr, "%s: Unrecognized switch: %s\n", program_name, + argv[1]); fprintf(stderr, usage); exit(1); } @@ -229,16 +239,19 @@ main(argc, argv) fprintf(stderr, usage); exit(0); case 3: - out = fopen(argv[2], "w"); + output_name = argv[2]; + out = fopen(output_name, "w"); if ( out == NULL ) { - fprintf(stderr, "Cannot open output file %s\n", argv[2]); + fprintf(stderr, "%s: Cannot open output file %s\n", + program_name, output_name); exit(1); } /* falls through */ case 2: in = fopen(argv[1], "r"); if ( in == NULL ) { - fprintf(stderr, "Cannot open input file %s\n", argv[1]); + fprintf(stderr, "%s: Cannot open input file %s\n", + program_name, argv[1]); exit(1); } if ( filename == 0 ) @@ -300,14 +313,24 @@ wl: fputs(buf, out); if ( line != buf ) fputs(buf, out); free(buf); - if ( out != stdout ) - fclose(out); + if ( output_name ) { + output_error = ferror(out); + output_error |= fclose(out); + } else { /* out == stdout */ + fflush(out); + output_error = ferror(out); + } + if ( output_error ) { + fprintf(stderr, "%s: error writing to %s\n", program_name, + (output_name ? output_name : "stdout")); + exit(1); + } if ( in != stdin ) fclose(in); return 0; } -/* Skip over space and comments, in either direction. */ +/* Skip over whitespace and comments, in either direction. */ char * skipspace(p, dir) register char *p; @@ -328,6 +351,17 @@ skipspace(p, dir) return p; } +/* Scan over a quoted string, in either direction. */ +char * +scanstring(p, dir) + register char *p; + register int dir; +{ + for (p += dir; ; p += dir) + if (*p == '"' && p[-dir] != '\\') + return p + dir; +} + /* * Write blanks over part of a string. * Don't overwrite end-of-line characters. @@ -500,8 +534,12 @@ top: p = endfn; else rp = p; break; case '/': - p = skipspace(p, 1) - 1; + if (p[1] == '*') + p = skipspace(p, 1) - 1; break; + case '"': + p = scanstring(p, 1) - 1; + break; default: ; } @@ -523,9 +561,19 @@ top: p = endfn; while ( level ) switch ( *--p ) { - case ']': case ')': level++; break; - case '[': case '(': level--; break; - case '/': p = skipspace(p, -1) + 1; break; + case ']': case ')': + level++; + break; + case '[': case '(': + level--; + break; + case '/': + if (p > buf && p[-1] == '*') + p = skipspace(p, -1) + 1; + break; + case '"': + p = scanstring(p, -1) + 1; + break; default: ; } } diff --git a/lib/ansi2knr.c b/lib/ansi2knr.c index 49624346f..061f69678 100644 --- a/lib/ansi2knr.c +++ b/lib/ansi2knr.c @@ -1,6 +1,6 @@ -/* Copyright (C) 1989, 1997, 1998 Aladdin Enterprises. All rights reserved. */ +/* Copyright (C) 1989, 1997, 1998, 1999 Aladdin Enterprises. All rights reserved. */ -/*$Id: ansi2knr.c,v 1.10 1998/12/02 12:42:23 tromey Exp $*/ +/*$Id: ansi2knr.c,v 1.11 1999/01/29 14:22:15 tromey Exp $*/ /* Convert ANSI C function definitions to K&R ("traditional C") syntax */ /* @@ -51,13 +51,18 @@ program under the GPL. * 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). - * - Some macros that tinker with the syntax of the function header. + * - Some macros that tinker with the syntax of function headers. */ /* * The original and principal author of ansi2knr is L. Peter Deutsch * . Other authors are noted in the change history * that follows (in reverse chronological order): + lpd 1999-01-28 fixed two bugs: a '/' in an argument list caused an + endless loop; quoted strings within an argument list + confused the parser + lpd 1999-01-24 added a check for write errors on the output, + suggested by Jim Meyering lpd 1998-11-09 added further hack to recognize identifier(void) as being a procedure lpd 1998-10-23 added hack to recognize lines consisting of @@ -178,6 +183,7 @@ program under the GPL. /* Forward references */ char *skipspace(); +char *scanstring(); int writeblanks(); int test1(); int convert1(); @@ -190,6 +196,8 @@ main(argc, argv) { FILE *in = stdin; FILE *out = stdout; char *filename = 0; + char *program_name = argv[0]; + char *output_name = 0; #define bufsize 5000 /* arbitrary size */ char *buf; char *line; @@ -205,6 +213,7 @@ main(argc, argv) * check for this switch for backward compatibility. */ int convert_varargs = 1; + int output_error; while ( argc > 1 && argv[1][0] == '-' ) { if ( !strcmp(argv[1], "--varargs") ) { @@ -219,7 +228,8 @@ main(argc, argv) argv += 2; continue; } - fprintf(stderr, "Unrecognized switch: %s\n", argv[1]); + fprintf(stderr, "%s: Unrecognized switch: %s\n", program_name, + argv[1]); fprintf(stderr, usage); exit(1); } @@ -229,16 +239,19 @@ main(argc, argv) fprintf(stderr, usage); exit(0); case 3: - out = fopen(argv[2], "w"); + output_name = argv[2]; + out = fopen(output_name, "w"); if ( out == NULL ) { - fprintf(stderr, "Cannot open output file %s\n", argv[2]); + fprintf(stderr, "%s: Cannot open output file %s\n", + program_name, output_name); exit(1); } /* falls through */ case 2: in = fopen(argv[1], "r"); if ( in == NULL ) { - fprintf(stderr, "Cannot open input file %s\n", argv[1]); + fprintf(stderr, "%s: Cannot open input file %s\n", + program_name, argv[1]); exit(1); } if ( filename == 0 ) @@ -300,14 +313,24 @@ wl: fputs(buf, out); if ( line != buf ) fputs(buf, out); free(buf); - if ( out != stdout ) - fclose(out); + if ( output_name ) { + output_error = ferror(out); + output_error |= fclose(out); + } else { /* out == stdout */ + fflush(out); + output_error = ferror(out); + } + if ( output_error ) { + fprintf(stderr, "%s: error writing to %s\n", program_name, + (output_name ? output_name : "stdout")); + exit(1); + } if ( in != stdin ) fclose(in); return 0; } -/* Skip over space and comments, in either direction. */ +/* Skip over whitespace and comments, in either direction. */ char * skipspace(p, dir) register char *p; @@ -328,6 +351,17 @@ skipspace(p, dir) return p; } +/* Scan over a quoted string, in either direction. */ +char * +scanstring(p, dir) + register char *p; + register int dir; +{ + for (p += dir; ; p += dir) + if (*p == '"' && p[-dir] != '\\') + return p + dir; +} + /* * Write blanks over part of a string. * Don't overwrite end-of-line characters. @@ -500,8 +534,12 @@ top: p = endfn; else rp = p; break; case '/': - p = skipspace(p, 1) - 1; + if (p[1] == '*') + p = skipspace(p, 1) - 1; break; + case '"': + p = scanstring(p, 1) - 1; + break; default: ; } @@ -523,9 +561,19 @@ top: p = endfn; while ( level ) switch ( *--p ) { - case ']': case ')': level++; break; - case '[': case '(': level--; break; - case '/': p = skipspace(p, -1) + 1; break; + case ']': case ')': + level++; + break; + case '[': case '(': + level--; + break; + case '/': + if (p > buf && p[-1] == '*') + p = skipspace(p, -1) + 1; + break; + case '"': + p = scanstring(p, -1) + 1; + break; default: ; } }