]> git.ipfire.org Git - thirdparty/bird.git/commitdiff
BIRD Client: Don't print excessive lines in non-interactive mode
authorPavel Tvrdik <pawel.tvrdik@gmail.com>
Thu, 21 Apr 2016 07:29:15 +0000 (09:29 +0200)
committerPavel Tvrdik <pawel.tvrdik@gmail.com>
Thu, 21 Apr 2016 14:24:47 +0000 (16:24 +0200)
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.

client/birdc.c
client/birdcl.c
client/client.c
client/client.h

index 49da77d658b4049df64d35bdde2058d26d5bebf4..3a52c3293a4c8eb7c8e444f118d79de4743c5828 100644 (file)
@@ -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;
+
+  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_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
index 8e3749247e17eed3bea56246b1da751d8dc96e5f..eb748fdc2786e0ddd8ab3f49fb84997daa773ae9 100644 (file)
@@ -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;
index 85729010f5cfed84cd361ddab3fe91c4a27f6f8b..e4bb568b53dab8a72305c2511f52c6448233572c 100644 (file)
@@ -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)
 {
index 033ab7b9fdf276f21bcfdc0cc28593c5b74c2835..a80273d93ee2032f0f1eb0caa85bd9b0140d88ae 100644 (file)
@@ -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