From af7d04cd04cbe21efdc1f5831d41bfcdb42410fa Mon Sep 17 00:00:00 2001 From: Philippe Teuwen Date: Thu, 3 Feb 2011 17:17:29 +0000 Subject: [PATCH] tamashell: accepts script as arg, this makes shebang possible --- examples/pn53x-tamashell.1 | 5 +++ examples/pn53x-tamashell.c | 70 +++++++++++++++++++++++--------------- 2 files changed, 48 insertions(+), 27 deletions(-) diff --git a/examples/pn53x-tamashell.1 b/examples/pn53x-tamashell.1 index 5a3139d..328b1e7 100644 --- a/examples/pn53x-tamashell.1 +++ b/examples/pn53x-tamashell.1 @@ -3,6 +3,7 @@ pn53x-tamashell \- PN53x TAMA communication demonstration shell .SH SYNOPSIS .B pn53x-tamashell +.IR [script] .SH DESCRIPTION .B pn53x-tamashell is a simple interactive tool to send so called TAMA commands @@ -50,6 +51,10 @@ Same thing, with a script: Rx: Command Not Acceptable > Bye! +.SH OPTIONS +.IR script +Script file with tama commands + .SH BUGS Please report any bugs on the .B libnfc diff --git a/examples/pn53x-tamashell.c b/examples/pn53x-tamashell.c index 9b26a83..5ec7c19 100644 --- a/examples/pn53x-tamashell.c +++ b/examples/pn53x-tamashell.c @@ -36,15 +36,12 @@ # include "config.h" #endif // HAVE_CONFIG_H -#if defined(HAVE_READLINE) -# include -# include -# include -#else # define _GNU_SOURCE // for getline on system with glibc < 2.10 # define _POSIX_C_SOURCE 200809L // for getline on system with glibc >= 2.10 # include - extern FILE* stdin; +#if defined(HAVE_READLINE) +# include +# include #endif //HAVE_READLINE #include @@ -68,6 +65,15 @@ int main(int argc, const char* argv[]) byte_t abtTx[MAX_FRAME_LEN] = { 0xD4 }; size_t szRx; size_t szTx; + extern FILE* stdin; + FILE* input = NULL; + + if (argc >= 2) { + if((input=fopen(argv[1], "r"))==NULL) { + ERR ("%s", "Cannot open file."); + return EXIT_FAILURE; + } + } // Try to open the NFC reader pnd = nfc_connect(NULL); @@ -78,7 +84,6 @@ int main(int argc, const char* argv[]) } printf ("Connected to NFC reader: %s\n", pnd->acName); - nfc_initiator_init(pnd); char * cmd; @@ -86,27 +91,35 @@ int main(int argc, const char* argv[]) while(1) { int offset=0; #if defined(HAVE_READLINE) - cmd=readline(prompt); - // NULL if ctrl-d - if (cmd==NULL) { - printf("Bye!\n"); - break; + if (input==NULL) { // means we use stdin + cmd=readline(prompt); + // NULL if ctrl-d + if (cmd==NULL) { + printf("Bye!\n"); + break; + } + add_history(cmd); + } else { +#endif //HAVE_READLINE + cmd = NULL; + printf("%s", prompt); + fflush(0); + size_t n; + int s; + if (input != NULL) { + s = getline(&cmd, &n, input); + } else { + s = getline(&cmd, &n, stdin); + } + if (s <= 0) { + printf("Bye!\n"); + free(cmd); + break; + } + // FIXME print only if read from redirected stdin (i.e. script) + printf("%s", cmd); +#if defined(HAVE_READLINE) } - add_history(cmd); -#else - cmd = NULL; - printf("%s", prompt); - fflush(0); - size_t n; - extern FILE* stdin; - int s = getline(&cmd, &n, stdin); - if (s <= 0) { - printf("Bye!\n"); - free(cmd); - break; - } - // FIXME print only if read from redirected stdin (i.e. script) - printf("%s", cmd); #endif //HAVE_READLINE if (cmd[0]=='q') { printf("Bye!\n"); @@ -151,6 +164,9 @@ int main(int argc, const char* argv[]) free(cmd); } + if (input != NULL) { + fclose(input); + } nfc_disconnect(pnd); return 1; }