diff --git a/client/birdc.c b/client/birdc.c index 49da77d6..3a52c329 100644 --- a/client/birdc.c +++ b/client/birdc.c @@ -140,20 +140,25 @@ input_help(int arg, int key UNUSED) void input_init(void) { - retrieve_symbols(); - printf("BIRD Client " BIRD_VERSION " ready.\n"); + prompt_active = 0; - rl_readline_name = "birdc"; - rl_add_defun("bird-complete", input_complete, '\t'); - rl_add_defun("bird-help", input_help, '?'); - rl_callback_handler_install("bird> ", input_got_line); + if (interactive) + { + prompt_active = 1; + + retrieve_symbols(); + printf("BIRD Client " BIRD_VERSION " ready.\n"); + + rl_readline_name = "birdc"; + rl_add_defun("bird-complete", input_complete, '\t'); + rl_add_defun("bird-help", input_help, '?'); + rl_callback_handler_install("bird> ", input_got_line); + } // rl_get_screen_size(); term_lns = LINES; term_cls = COLS; - prompt_active = 1; - // readline library does strange things when stdin is nonblocking. // if (fcntl(0, F_SETFL, O_NONBLOCK) < 0) // die("fcntl: %m"); @@ -200,7 +205,10 @@ input_notify(int prompt) void input_read(void) { - rl_callback_read_char(); + if (interactive) + rl_callback_read_char(); + else + simple_input_read(); } void diff --git a/client/birdcl.c b/client/birdcl.c index 8e374924..eb748fdc 100644 --- a/client/birdcl.c +++ b/client/birdcl.c @@ -21,8 +21,6 @@ #include "client/client.h" #include "sysdep/unix/unix.h" -#define INPUT_BUF_LEN 2048 - struct termios tty_save; void @@ -41,59 +39,17 @@ void input_notify(int prompt) { /* No ncurses -> no status to reveal/hide, print prompt manually. */ - if (!prompt) + if (!prompt || !interactive) return; printf("bird> "); fflush(stdout); } - -static int -lastnb(char *str, int i) -{ - while (i--) - if ((str[i] != ' ') && (str[i] != '\t')) - return str[i]; - - return 0; -} - void input_read(void) { - char buf[INPUT_BUF_LEN]; - - if ((fgets(buf, INPUT_BUF_LEN, stdin) == NULL) || (buf[0] == 0)) - { - putchar('\n'); - cleanup(); - exit(0); - } - - int l = strlen(buf); - if ((l+1) == INPUT_BUF_LEN) - { - printf("Input too long.\n"); - return; - } - - if (buf[l-1] == '\n') - buf[--l] = '\0'; - - if (!interactive) - printf("%s\n", buf); - - if (l == 0) - return; - - if (lastnb(buf, l) == '?') - { - cmd_help(buf, strlen(buf)); - return; - } - - submit_command(buf); + simple_input_read(); } static struct termios stored_tty; diff --git a/client/client.c b/client/client.c index 85729010..e4bb568b 100644 --- a/client/client.c +++ b/client/client.c @@ -37,6 +37,7 @@ #include "client/reply_codes.h" #define SERVER_READ_BUF_LEN 4096 +#define INPUT_BUF_LEN 2048 static char *opt_list = "s:vr"; static int verbose, restricted, once; @@ -422,6 +423,51 @@ server_read(void) } } +static int +lastnb(char *str, int i) +{ + while (i--) + if ((str[i] != ' ') && (str[i] != '\t')) + return str[i]; + + return 0; +} + +void +simple_input_read(void) +{ + char buf[INPUT_BUF_LEN]; + + if ((fgets(buf, INPUT_BUF_LEN, stdin) == NULL) || (buf[0] == 0)) + { + if (interactive) + putchar('\n'); + cleanup(); + exit(0); + } + + int l = strlen(buf); + if ((l+1) == INPUT_BUF_LEN) + { + printf("Input too long.\n"); + return; + } + + if (buf[l-1] == '\n') + buf[--l] = '\0'; + + if (l == 0) + return; + + if (lastnb(buf, l) == '?') + { + cmd_help(buf, strlen(buf)); + return; + } + + submit_command(buf); +} + static void select_loop(void) { diff --git a/client/client.h b/client/client.h index 033ab7b9..a80273d9 100644 --- a/client/client.h +++ b/client/client.h @@ -68,5 +68,6 @@ void retrieve_symbols(void); void add_keywords_to_symbols(void); list *cli_get_symbol_list(void); uint cli_get_symbol_maxlen(void); +void simple_input_read(void); #endif