From 1efbc64922486a6036cce04fedc34ef514b251c5 Mon Sep 17 00:00:00 2001 From: Stefan Rueger Date: Wed, 13 Jul 2022 11:38:43 +0100 Subject: [PATCH] Add terminal_setup_update_progress() library interface to term.c This enables the new quell terminal command to switch on and off progress reports to the terminal. The code for this was moved from main.c to term.c. It can be used as library call for other frontends than main.c --- src/main.c | 80 ++------------------------------------------------- src/term.c | 84 +++++++++++++++++++++++++++++++++++++++++++++++++++++- src/term.h | 1 + 3 files changed, 86 insertions(+), 79 deletions(-) diff --git a/src/main.c b/src/main.c index 1d00ae67..b6fd2926 100644 --- a/src/main.c +++ b/src/main.c @@ -135,72 +135,6 @@ static void usage(void) } -static void update_progress_tty (int percent, double etime, char *hdr) -{ - static char hashes[51]; - static char *header; - static int last = 0; - int i; - - setvbuf(stderr, (char*)NULL, _IONBF, 0); - - hashes[50] = 0; - - memset (hashes, ' ', 50); - for (i=0; i>1)*2; - - setvbuf(stderr, (char*)NULL, _IONBF, 0); - - if (hdr) { - avrdude_message(MSG_INFO, "\n%s | ", hdr); - last = 0; - done = 0; - } - else { - while ((cnt > last) && (done == 0)) { - avrdude_message(MSG_INFO, "#"); - cnt -= 2; - } - } - - if ((percent == 100) && (done == 0)) { - avrdude_message(MSG_INFO, " | 100%% %0.2fs\n\n", etime); - last = 0; - done = 1; - } - else - last = (percent>>1)*2; /* Make last a multiple of 2. */ - - setvbuf(stderr, (char*)NULL, _IOLBF, 0); -} - static void list_programmers_callback(const char *name, const char *desc, const char *cfgname, int cfglineno, void *cookie) @@ -759,18 +693,8 @@ int main(int argc, char * argv []) } #endif - if (quell_progress == 0) { - if (isatty (STDERR_FILENO)) - update_progress = update_progress_tty; - else { - update_progress = update_progress_no_tty; - /* disable all buffering of stderr for compatibility with - software that captures and redirects output to a GUI - i.e. Programmers Notepad */ - setvbuf( stderr, NULL, _IONBF, 0 ); - setvbuf( stdout, NULL, _IONBF, 0 ); - } - } + if (quell_progress == 0) + terminal_setup_update_progress(); /* * Print out an identifying string so folks can tell what version diff --git a/src/term.c b/src/term.c index 8cd48b99..ec95fcec 100644 --- a/src/term.c +++ b/src/term.c @@ -26,6 +26,7 @@ #include #include #include +#include #include #if defined(HAVE_LIBREADLINE) @@ -345,7 +346,6 @@ static int cmd_dump(PROGRAMMER * pgm, struct avrpart * p, } - // Convert the next n hex digits of s to a hex number static unsigned int tohex(const unsigned char *s, unsigned int n) { int ret, c; @@ -1294,6 +1294,8 @@ static int cmd_quell(PROGRAMMER * pgm, struct avrpart * p, if(quell_progress > 0) update_progress = NULL; + else + terminal_setup_update_progress(); return 0; } @@ -1499,3 +1501,83 @@ int terminal_mode(PROGRAMMER * pgm, struct avrpart * p) return rc; } + + +static void update_progress_tty (int percent, double etime, char *hdr) +{ + static char hashes[51]; + static char *header; + static int last = 0; + int i; + + setvbuf(stderr, (char*)NULL, _IONBF, 0); + + hashes[50] = 0; + + memset (hashes, ' ', 50); + for (i=0; i>1)*2; + + setvbuf(stderr, (char*)NULL, _IONBF, 0); + + if (hdr) { + avrdude_message(MSG_INFO, "\n%s | ", hdr); + last = 0; + done = 0; + } + else { + while ((cnt > last) && (done == 0)) { + avrdude_message(MSG_INFO, "#"); + cnt -= 2; + } + } + + if ((percent == 100) && (done == 0)) { + avrdude_message(MSG_INFO, " | 100%% %0.2fs\n\n", etime); + last = 0; + done = 1; + } + else + last = (percent>>1)*2; /* Make last a multiple of 2. */ + + setvbuf(stderr, (char*)NULL, _IOLBF, 0); +} + +void terminal_setup_update_progress() { + if (isatty (STDERR_FILENO)) + update_progress = update_progress_tty; + else { + update_progress = update_progress_no_tty; + /* disable all buffering of stderr for compatibility with + software that captures and redirects output to a GUI + i.e. Programmers Notepad */ + setvbuf( stderr, NULL, _IONBF, 0 ); + setvbuf( stdout, NULL, _IONBF, 0 ); + } +} diff --git a/src/term.h b/src/term.h index f114d4b0..61c4916c 100644 --- a/src/term.h +++ b/src/term.h @@ -34,6 +34,7 @@ typedef enum { int terminal_mode(PROGRAMMER * pgm, struct avrpart * p); char * terminal_get_input(const char *prompt); +void terminal_setup_update_progress(); #ifdef __cplusplus }