+1999-01-29 Tom Tromey <tromey@cygnus.com>
+
+ * ansi2knr.c: New version from L. Peter Deutsch.
+
1999-01-22 Tom Tromey <tromey@cygnus.com>
* automake.in (require_file_internal): Correctly examine return
-/* Copyright (C) 1989, 1997, 1998 Aladdin Enterprises. All rights reserved. */
+/* Copyright (C) 1989, 1997, 1998, 1999 Aladdin Enterprises. All rights reserved. */
/*$Id: ansi2knr.c $*/
/* Convert ANSI C function definitions to K&R ("traditional C") syntax */
* 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
* <ghost@aladdin.com>. 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 <meyering@ascend.com>
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
/* Forward references */
char *skipspace();
+char *scanstring();
int writeblanks();
int test1();
int convert1();
{ 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;
* 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") ) {
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);
}
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 )
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;
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.
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:
;
}
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: ;
}
}
-/* Copyright (C) 1989, 1997, 1998 Aladdin Enterprises. All rights reserved. */
+/* Copyright (C) 1989, 1997, 1998, 1999 Aladdin Enterprises. All rights reserved. */
/*$Id: ansi2knr.c $*/
/* Convert ANSI C function definitions to K&R ("traditional C") syntax */
* 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
* <ghost@aladdin.com>. 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 <meyering@ascend.com>
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
/* Forward references */
char *skipspace();
+char *scanstring();
int writeblanks();
int test1();
int convert1();
{ 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;
* 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") ) {
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);
}
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 )
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;
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.
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:
;
}
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: ;
}
}