From: Marcel T. <te...@us...> - 2004-11-21 12:44:08
|
Update of /cvsroot/openwince/jtag/src In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv5324/src Modified Files: jtag.c Log Message: 2004-11-21 Marcel Telka <ma...@te...> * 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). Index: jtag.c =================================================================== RCS file: /cvsroot/openwince/jtag/src/jtag.c,v retrieving revision 1.57 retrieving revision 1.58 diff -u -d -r1.57 -r1.58 --- jtag.c 13 Aug 2004 04:23:20 -0000 1.57 +++ jtag.c 21 Nov 2004 12:43:53 -0000 1.58 @@ -27,6 +27,7 @@ #include <stdio.h> #include <stdlib.h> #include <string.h> +#include <unistd.h> #include <sys/stat.h> #include <sys/types.h> #include <readline/readline.h> @@ -195,28 +196,35 @@ 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 @@ 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 @@ 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 @@ 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; } |