From 1fbc253cbf9e01fd7ae89631f707c8fd08bb1864 Mon Sep 17 00:00:00 2001 From: Tom Tromey Date: Wed, 1 Apr 1998 04:10:25 +0000 Subject: [PATCH] new ansi2knr --- ChangeLog | 6 ++++ THANKS | 1 + ansi2knr.c | 94 ++++++++++++++++++++++++++++++-------------------- automake.in | 2 +- lib/ansi2knr.c | 94 ++++++++++++++++++++++++++++++-------------------- 5 files changed, 122 insertions(+), 75 deletions(-) diff --git a/ChangeLog b/ChangeLog index 880967cb..054ab13a 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,9 @@ +Tue Mar 31 21:07:42 1998 Tom Tromey + + * automake.in (handle_yacc_lex_cxx): Changed to use new version of + ansi2knr. + * ansi2knr.c: New version from L. Peter Deutsch. + Thu Mar 26 11:00:04 1998 Tom Tromey * m4/exeext.m4: Correctly eliminate bad cases when computing diff --git a/THANKS b/THANKS index b38d9cce..1ba9a323 100644 --- a/THANKS +++ b/THANKS @@ -40,6 +40,7 @@ Joshua Cowan jcowan@jcowan.reslife.okstate.edu Juergen A. Erhard jae@laden.ilk.de Karl Berry kb@cs.umb.edu Kevin Dalley kevin@aimnet.com +L. Peter Deutsch ghost@aladdin.com Maciej W. Rozycki macro@ds2.pg.gda.pl Marius Vollmer mvo@zagadka.ping.de Mark Eichin eichin@cygnus.com diff --git a/ansi2knr.c b/ansi2knr.c index 791eee04..3dd5086c 100644 --- a/ansi2knr.c +++ b/ansi2knr.c @@ -1,6 +1,3 @@ -/* ansi2knr.c */ -/* Convert ANSI C function definitions to K&R ("traditional C") syntax */ - /* ansi2knr is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY. No author or distributor accepts responsibility to anyone for the @@ -11,9 +8,10 @@ License (the "GPL") for full details. Everyone is granted permission to copy, modify and redistribute ansi2knr, but only under the conditions described in the GPL. A copy of this license is supposed to have been given to you along with ansi2knr so you can know -your rights and responsibilities. It should be in a file named COPYLEFT. -Among other things, the copyright notice and this notice must be preserved -on all copies. +your rights and responsibilities. It should be in a file named COPYLEFT, +or, if there is no file named COPYLEFT, a file named COPYING. Among other +things, the copyright notice and this notice must be preserved on all +copies. We explicitly state here what we believe is already implied by the GPL: if the ansi2knr program is distributed as a separate set of sources and a @@ -26,7 +24,10 @@ program under the GPL. /* * Usage: - ansi2knr input_file [output_file] + ansi2knr [--filename FILENAME] [INPUT_FILE [OUTPUT_FILE]] + * --filename provides the file name for the #line directive in the output, + * overriding input_file (if present). + * If no input_file is supplied, input is read from stdin. * If no output_file is supplied, output goes to stdout. * There are no error messages. * @@ -49,6 +50,11 @@ program under the GPL. * 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 97-12-08 made input_file optional; only closes input and/or + output file if not stdin or stdout respectively; prints + usage message on stderr rather than stdout; adds + --filename switch (changes suggested by + ) lpd 96-01-21 added code to cope with not HAVE_CONFIG_H and with compilers that don't understand void, as suggested by Tom Lane @@ -169,11 +175,15 @@ int main(argc, argv) int argc; char *argv[]; -{ FILE *in, *out; +{ FILE *in = stdin; + FILE *out = stdout; + char *filename = 0; #define bufsize 5000 /* arbitrary size */ char *buf; char *line; char *more; + char *usage = + "Usage: ansi2knr [--filename FILENAME] [INPUT_FILE [OUTPUT_FILE]]\n"; /* * In previous versions, ansi2knr recognized a --varargs switch. * If this switch was supplied, ansi2knr would attempt to convert @@ -184,41 +194,49 @@ main(argc, argv) */ int convert_varargs = 1; - if ( argc > 1 && argv[1][0] == '-' && argv[1][1] ) - { if ( !strcmp(argv[1], "--varargs") ) - { convert_varargs = 1; - argc--; - argv++; - } - else - { fprintf(stderr, "Unrecognized switch: %s\n", argv[1]); - exit(1); - } + while ( argc > 1 && argv[1][0] == '-' ) { + if ( !strcmp(argv[1], "--varargs") ) { + convert_varargs = 1; + argc--; + argv++; + continue; + } + if ( !strcmp(argv[1], "--filename") && argc > 2 ) { + filename = argv[2]; + argc -= 2; + argv += 2; + continue; } + fprintf(stderr, "Unrecognized switch: %s\n", argv[1]); + fprintf(stderr, usage); + exit(1); + } switch ( argc ) { default: - printf("Usage: ansi2knr input_file [output_file]\n"); + fprintf(stderr, usage); exit(0); - case 2: - out = stdout; - break; case 3: out = fopen(argv[2], "w"); - if ( out == NULL ) - { fprintf(stderr, "Cannot open output file %s\n", argv[2]); - exit(1); - } - } - if ( argv[1][0] == '-' && !argv[1][1] ) - in = stdin; - else - in = fopen(argv[1], "r"); - if ( in == NULL ) - { fprintf(stderr, "Cannot open input file %s\n", argv[1]); - exit(1); + if ( out == NULL ) { + fprintf(stderr, "Cannot open output file %s\n", argv[2]); + exit(1); + } + /* falls through */ + case 2: + in = fopen(argv[1], "r"); + if ( in == NULL ) { + fprintf(stderr, "Cannot open input file %s\n", argv[1]); + exit(1); + } + if ( filename == 0 ) + filename = argv[1]; + /* falls through */ + case 1: + break; } - fprintf(out, "#line 1 \"%s\"\n", argv[1]); + if ( filename ) + fprintf(out, "#line 1 \"%s\"\n", filename); buf = malloc(bufsize); line = buf; while ( fgets(line, (unsigned)(buf + bufsize - line), in) != NULL ) @@ -270,8 +288,10 @@ wl: fputs(buf, out); if ( line != buf ) fputs(buf, out); free(buf); - fclose(out); - fclose(in); + if ( out != stdout ) + fclose(out); + if ( in != stdin ) + fclose(in); return 0; } diff --git a/automake.in b/automake.in index 41ce3d5f..091ce4c0 100755 --- a/automake.in +++ b/automake.in @@ -1016,7 +1016,7 @@ sub handle_yacc_lex_cxx . '`if test -f $(srcdir)/' . $base . '.c' . '; then echo $(srcdir)/' . $base . '.c' . '; else echo ' . $base . '.c; fi` ' - . '| $(ANSI2KNR) - ' . $base . "_.c\n"); + . '| $(ANSI2KNR) ' . $base . "_.c\n"); push (@objects, $base . '_.o'); push (@objects, $base . '_.lo') if $seen_libtool; } diff --git a/lib/ansi2knr.c b/lib/ansi2knr.c index 791eee04..3dd5086c 100644 --- a/lib/ansi2knr.c +++ b/lib/ansi2knr.c @@ -1,6 +1,3 @@ -/* ansi2knr.c */ -/* Convert ANSI C function definitions to K&R ("traditional C") syntax */ - /* ansi2knr is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY. No author or distributor accepts responsibility to anyone for the @@ -11,9 +8,10 @@ License (the "GPL") for full details. Everyone is granted permission to copy, modify and redistribute ansi2knr, but only under the conditions described in the GPL. A copy of this license is supposed to have been given to you along with ansi2knr so you can know -your rights and responsibilities. It should be in a file named COPYLEFT. -Among other things, the copyright notice and this notice must be preserved -on all copies. +your rights and responsibilities. It should be in a file named COPYLEFT, +or, if there is no file named COPYLEFT, a file named COPYING. Among other +things, the copyright notice and this notice must be preserved on all +copies. We explicitly state here what we believe is already implied by the GPL: if the ansi2knr program is distributed as a separate set of sources and a @@ -26,7 +24,10 @@ program under the GPL. /* * Usage: - ansi2knr input_file [output_file] + ansi2knr [--filename FILENAME] [INPUT_FILE [OUTPUT_FILE]] + * --filename provides the file name for the #line directive in the output, + * overriding input_file (if present). + * If no input_file is supplied, input is read from stdin. * If no output_file is supplied, output goes to stdout. * There are no error messages. * @@ -49,6 +50,11 @@ program under the GPL. * 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 97-12-08 made input_file optional; only closes input and/or + output file if not stdin or stdout respectively; prints + usage message on stderr rather than stdout; adds + --filename switch (changes suggested by + ) lpd 96-01-21 added code to cope with not HAVE_CONFIG_H and with compilers that don't understand void, as suggested by Tom Lane @@ -169,11 +175,15 @@ int main(argc, argv) int argc; char *argv[]; -{ FILE *in, *out; +{ FILE *in = stdin; + FILE *out = stdout; + char *filename = 0; #define bufsize 5000 /* arbitrary size */ char *buf; char *line; char *more; + char *usage = + "Usage: ansi2knr [--filename FILENAME] [INPUT_FILE [OUTPUT_FILE]]\n"; /* * In previous versions, ansi2knr recognized a --varargs switch. * If this switch was supplied, ansi2knr would attempt to convert @@ -184,41 +194,49 @@ main(argc, argv) */ int convert_varargs = 1; - if ( argc > 1 && argv[1][0] == '-' && argv[1][1] ) - { if ( !strcmp(argv[1], "--varargs") ) - { convert_varargs = 1; - argc--; - argv++; - } - else - { fprintf(stderr, "Unrecognized switch: %s\n", argv[1]); - exit(1); - } + while ( argc > 1 && argv[1][0] == '-' ) { + if ( !strcmp(argv[1], "--varargs") ) { + convert_varargs = 1; + argc--; + argv++; + continue; + } + if ( !strcmp(argv[1], "--filename") && argc > 2 ) { + filename = argv[2]; + argc -= 2; + argv += 2; + continue; } + fprintf(stderr, "Unrecognized switch: %s\n", argv[1]); + fprintf(stderr, usage); + exit(1); + } switch ( argc ) { default: - printf("Usage: ansi2knr input_file [output_file]\n"); + fprintf(stderr, usage); exit(0); - case 2: - out = stdout; - break; case 3: out = fopen(argv[2], "w"); - if ( out == NULL ) - { fprintf(stderr, "Cannot open output file %s\n", argv[2]); - exit(1); - } - } - if ( argv[1][0] == '-' && !argv[1][1] ) - in = stdin; - else - in = fopen(argv[1], "r"); - if ( in == NULL ) - { fprintf(stderr, "Cannot open input file %s\n", argv[1]); - exit(1); + if ( out == NULL ) { + fprintf(stderr, "Cannot open output file %s\n", argv[2]); + exit(1); + } + /* falls through */ + case 2: + in = fopen(argv[1], "r"); + if ( in == NULL ) { + fprintf(stderr, "Cannot open input file %s\n", argv[1]); + exit(1); + } + if ( filename == 0 ) + filename = argv[1]; + /* falls through */ + case 1: + break; } - fprintf(out, "#line 1 \"%s\"\n", argv[1]); + if ( filename ) + fprintf(out, "#line 1 \"%s\"\n", filename); buf = malloc(bufsize); line = buf; while ( fgets(line, (unsigned)(buf + bufsize - line), in) != NULL ) @@ -270,8 +288,10 @@ wl: fputs(buf, out); if ( line != buf ) fputs(buf, out); free(buf); - fclose(out); - fclose(in); + if ( out != stdout ) + fclose(out); + if ( in != stdin ) + fclose(in); return 0; } -- 2.43.5