diff options
author | Andreas Baumann <mail@andreasbaumann.cc> | 2014-11-15 13:32:38 +0100 |
---|---|---|
committer | Andreas Baumann <mail@andreasbaumann.cc> | 2014-11-15 13:32:38 +0100 |
commit | 8a008f55fa16b4388e49a21301bf84c7e4a2b2f7 (patch) | |
tree | 52168c9c4251d6213bcea53a4b950aaefaade3b7 /src/biruda.c | |
parent | 4b194132aa21b0477eecc074baa1045b7edca181 (diff) | |
download | biruda-8a008f55fa16b4388e49a21301bf84c7e4a2b2f7.tar.gz biruda-8a008f55fa16b4388e49a21301bf84c7e4a2b2f7.tar.bz2 |
cli interactive/pipe mode
Diffstat (limited to 'src/biruda.c')
-rw-r--r-- | src/biruda.c | 40 |
1 files changed, 27 insertions, 13 deletions
diff --git a/src/biruda.c b/src/biruda.c index a2fbc08..3934d55 100644 --- a/src/biruda.c +++ b/src/biruda.c @@ -225,6 +225,8 @@ static char *commands[] = { static bool print_colors = false; +static bool is_interactive = false; + static void completion_func( const char *buf, linenoiseCompletions *lc ) { unsigned int i; @@ -303,23 +305,31 @@ static int start_interactive( bool colors ) { char history_filename[1024]; - print_colors = colors; - - char *home = getenv( "HOME" ); - if( home != NULL ) { - snprintf( history_filename, sizeof( history_filename ), "%s/%s", home, HISTORY_FILE ); - linenoiseHistoryLoad( history_filename ); - linenoiseSetCompletionCallback( completion_func ); + is_interactive = isatty( fileno( stdin ) ); + print_colors = is_interactive ? colors : false; + + if( is_interactive ) { + char *home = getenv( "HOME" ); + if( home != NULL ) { + snprintf( history_filename, sizeof( history_filename ), "%s/%s", home, HISTORY_FILE ); + linenoiseHistoryLoad( history_filename ); + linenoiseSetCompletionCallback( completion_func ); + } } char *context = "biruda"; for( ;; ) { char buf[4096]; char *line; - - snprintf( buf, sizeof( buf ), "%s> ", context ); - if( ( line = linenoise( buf ) ) == NULL ) { - break; + + if( is_interactive ) { + snprintf( buf, sizeof( buf ), "%s> ", context ); + if( ( line = linenoise( buf ) ) == NULL ) { + break; + } + } else { + fgets( buf, sizeof( buf ), stdin ); + line = buf; } if( strlen( line ) > 1 ) { @@ -328,10 +338,14 @@ static int start_interactive( bool colors ) } } - linenoiseHistoryAdd( line ); + if( is_interactive ) { + linenoiseHistoryAdd( line ); + } if( strncasecmp( line, "quit", 4 ) == 0 ) { - linenoiseHistorySave( history_filename ); + if( is_interactive ) { + linenoiseHistorySave( history_filename ); + } return EXIT_SUCCESS; } else if( strncasecmp( line, "help", 4 ) == 0 ) { print_help( ); |