0
0
mirror of https://gitlab.nic.cz/labs/bird.git synced 2024-12-23 02:01:55 +00:00

BIRD Client: Don't print excessive lines in non-interactive mode

Do not print out excessive lines with BIRD version and CLI prompt in
non-interactive mode.

Share birdcl input_read() code for birdc non-interactive mode.
This commit is contained in:
Pavel Tvrdik 2016-04-21 09:29:15 +02:00
parent c0b101a1de
commit f603be4643
4 changed files with 66 additions and 55 deletions

View File

@ -140,20 +140,25 @@ input_help(int arg, int key UNUSED)
void void
input_init(void) input_init(void)
{ {
retrieve_symbols(); prompt_active = 0;
printf("BIRD Client " BIRD_VERSION " ready.\n");
rl_readline_name = "birdc"; if (interactive)
rl_add_defun("bird-complete", input_complete, '\t'); {
rl_add_defun("bird-help", input_help, '?'); prompt_active = 1;
rl_callback_handler_install("bird> ", input_got_line);
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(); // rl_get_screen_size();
term_lns = LINES; term_lns = LINES;
term_cls = COLS; term_cls = COLS;
prompt_active = 1;
// readline library does strange things when stdin is nonblocking. // readline library does strange things when stdin is nonblocking.
// if (fcntl(0, F_SETFL, O_NONBLOCK) < 0) // if (fcntl(0, F_SETFL, O_NONBLOCK) < 0)
// die("fcntl: %m"); // die("fcntl: %m");
@ -200,7 +205,10 @@ input_notify(int prompt)
void void
input_read(void) input_read(void)
{ {
rl_callback_read_char(); if (interactive)
rl_callback_read_char();
else
simple_input_read();
} }
void void

View File

@ -21,8 +21,6 @@
#include "client/client.h" #include "client/client.h"
#include "sysdep/unix/unix.h" #include "sysdep/unix/unix.h"
#define INPUT_BUF_LEN 2048
struct termios tty_save; struct termios tty_save;
void void
@ -41,59 +39,17 @@ void
input_notify(int prompt) input_notify(int prompt)
{ {
/* No ncurses -> no status to reveal/hide, print prompt manually. */ /* No ncurses -> no status to reveal/hide, print prompt manually. */
if (!prompt) if (!prompt || !interactive)
return; return;
printf("bird> "); printf("bird> ");
fflush(stdout); fflush(stdout);
} }
static int
lastnb(char *str, int i)
{
while (i--)
if ((str[i] != ' ') && (str[i] != '\t'))
return str[i];
return 0;
}
void void
input_read(void) input_read(void)
{ {
char buf[INPUT_BUF_LEN]; simple_input_read();
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);
} }
static struct termios stored_tty; static struct termios stored_tty;

View File

@ -37,6 +37,7 @@
#include "client/reply_codes.h" #include "client/reply_codes.h"
#define SERVER_READ_BUF_LEN 4096 #define SERVER_READ_BUF_LEN 4096
#define INPUT_BUF_LEN 2048
static char *opt_list = "s:vr"; static char *opt_list = "s:vr";
static int verbose, restricted, once; 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 static void
select_loop(void) select_loop(void)
{ {

View File

@ -68,5 +68,6 @@ void retrieve_symbols(void);
void add_keywords_to_symbols(void); void add_keywords_to_symbols(void);
list *cli_get_symbol_list(void); list *cli_get_symbol_list(void);
uint cli_get_symbol_maxlen(void); uint cli_get_symbol_maxlen(void);
void simple_input_read(void);
#endif #endif