From 5968bafaf9f07faeb3c9eabbb495e653b782efc1 Mon Sep 17 00:00:00 2001 From: Marcel Telka Date: Sun, 21 Nov 2004 12:43:53 +0000 Subject: [PATCH] 2004-11-21 Marcel Telka * src/jtag.c (jtag_parse_stream, cleanup): New function. (jtag_parse_file): Replaced stream parsing algorithm with jtag_parse_stream() call. Removed support for special filename '-'. (main): Rewritten and improved startup sequence to distinguish between interactive invocation and stdin input (fixed bug 858535, thanks to Andrew Dyer for an idea). git-svn-id: https://urjtag.svn.sourceforge.net/svnroot/urjtag/trunk@623 b68d4a1b-bc3d-0410-92ed-d4ac073336b7 --- jtag/ChangeLog | 8 ++++ jtag/NEWS | 3 ++ jtag/src/jtag.c | 124 ++++++++++++++++++++++++++++++++---------------- 3 files changed, 93 insertions(+), 42 deletions(-) diff --git a/jtag/ChangeLog b/jtag/ChangeLog index 2d07355f..be53b287 100644 --- a/jtag/ChangeLog +++ b/jtag/ChangeLog @@ -1,3 +1,11 @@ +2004-11-21 Marcel Telka + + * src/jtag.c (jtag_parse_stream, cleanup): New function. + (jtag_parse_file): Replaced stream parsing algorithm with jtag_parse_stream() call. Removed support + for special filename '-'. + (main): Rewritten and improved startup sequence to distinguish between interactive invocation + and stdin input (fixed bug 858535, thanks to Andrew Dyer for an idea). + 2004-11-17 Marcel Telka * acinclude.m4: New file with VL_LIB_READLINE macro (removed dependency on ac-archive package). diff --git a/jtag/NEWS b/jtag/NEWS index 7415be87..b1249c59 100644 --- a/jtag/NEWS +++ b/jtag/NEWS @@ -28,6 +28,9 @@ See libbrux/NEWS for more news. * Fixed minor bugs (including bug 857039). * Fixed compiler error on Debian Woody (patch 986414, Martin Buck). * Added support for different Intel IXP425 frequency variants (patch 1030647, Trevor Man). + * Rewritten and improved startup sequence to distinguish between interactive invocation + and stdin input (fixed bug 858535, thanks to Andrew Dyer for an idea). + * Removed support for parameter '-' (stdin). * New translations: - French (Michel Robitaille) diff --git a/jtag/src/jtag.c b/jtag/src/jtag.c index 998c478e..2ee5a43a 100644 --- a/jtag/src/jtag.c +++ b/jtag/src/jtag.c @@ -27,6 +27,7 @@ #include #include #include +#include #include #include #include @@ -195,28 +196,35 @@ jtag_readline_loop( const char *prompt ) free( line ); } -int -jtag_parse_file( const char *filename ) +static int +jtag_parse_stream( FILE *f ) { - FILE *f; int go = 1; char *line = NULL; int n = 0; - if (strcmp( filename, "-" ) != 0) - f = fopen( filename, "r" ); - else - f = stdin; - if (!f) - return -1; - while (go && (getline( &line, &n, f ) != -1)) if ((strlen(line) > 0) && (line[0] != '#')) go = jtag_parse_line(line); free(line); - if (strcmp( filename, "-" ) != 0) - fclose(f); + + return go; +} + +int +jtag_parse_file( const char *filename ) +{ + FILE *f; + int go; + + f = fopen( filename, "r" ); + if (!f) + return -1; + + go = jtag_parse_stream( f ); + + fclose(f); return go; } @@ -248,10 +256,24 @@ jtag_parse_rc( void ) return go; } +static void +cleanup( void ) +{ + cfi_array_free( cfi_array ); + cfi_array = NULL; + + if (bus) { + bus_free( bus ); + bus = NULL; + } + chain_free( chain ); + chain = NULL; +} + int main( int argc, const char **argv ) { - int go = 1; + int go; int i; #ifdef ENABLE_NLS @@ -261,6 +283,41 @@ main( int argc, const char **argv ) textdomain( PACKAGE ); #endif /* ENABLE_NLS */ + /* input from files */ + if (argc > 1) { + for (i = 1; i < argc; i++) { + chain = chain_alloc(); + if (!chain) { + printf( _("Out of memory\n") ); + return -1; + } + + go = jtag_parse_file( argv[i] ); + cleanup(); + if (go < 0) { + printf( _("Unable to open file `%s'!\n"), argv[i] ); + break; + } + } + return 0; + } + + /* input from stdin */ + if (!isatty(0)) { + chain = chain_alloc(); + if (!chain) { + printf( _("Out of memory\n") ); + return -1; + } + + jtag_parse_stream( stdin ); + + cleanup(); + + return 0; + } + + /* interactive */ printf( _("%s\n" "Copyright (C) 2002, 2003 ETC s.r.o.\n" @@ -278,41 +335,24 @@ main( int argc, const char **argv ) printf( _("Warning: %s may damage your hardware! Type \"quit\" to exit!\n\n"), PACKAGE_NAME ); printf( _("Type \"help\" for help.\n\n") ); - for (i = 1; i < argc; i++) { - go = jtag_parse_file( argv[i] ); - if (go < 0) - printf( _("Unable to open file `%s'!\n"), argv[i] ); - if (!go) - break; - } - - if (go) { - /* Create ~/.jtag */ - jtag_create_jtagdir(); + /* Create ~/.jtag */ + jtag_create_jtagdir(); - /* Parse and execute the RC file */ - go = jtag_parse_rc(); + /* Parse and execute the RC file */ + go = jtag_parse_rc(); - if (go) { - /* Load history */ - jtag_load_history(); + if (go) { + /* Load history */ + jtag_load_history(); - /* main loop */ - jtag_readline_loop( "jtag> " ); + /* main loop */ + jtag_readline_loop( "jtag> " ); - /* Save history */ - jtag_save_history(); - } + /* Save history */ + jtag_save_history(); } - cfi_array_free( cfi_array ); - cfi_array = NULL; - - if (bus) { - bus_free( bus ); - bus = NULL; - } - chain_free( chain ); + cleanup(); return 0; }