Something went wrong. Try again.
forked from cz-nic/bird
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162/* * BIRD Client - Light variant I/O * * (c) 1999--2004 Martin Mares <mj@ucw.cz> * (c) 2013 Tomas Hlavacek <tomas.hlavacek@nic.cz> * * Can be freely distributed and used under the terms of the GNU GPL. */
#include <stdio.h>#include <stdlib.h>#include <unistd.h>#include <termios.h>#include <errno.h>
#include <sys/ioctl.h>#include <signal.h>
#include "nest/bird.h"#include "lib/resource.h"#include "lib/string.h"#include "client/client.h"#include "sysdep/unix/unix.h"
#define INPUT_BUF_LEN 2048
struct termios tty_save;
voidinput_start_list(void){ /* Empty in non-ncurses version. */}
voidinput_stop_list(void){ /* Empty in non-ncurses version. */}
voidinput_notify(int prompt){ /* No ncurses -> no status to reveal/hide, print prompt manually. */ if (!prompt) return;
printf("bird> "); fflush(stdout);}
static intlastnb(char *str, int i){ while (i--) if ((str[i] != ' ') && (str[i] != '\t')) return str[i];
return 0;}
voidinput_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);}
static struct termios stored_tty;static int more_active = 0;
voidmore_begin(void){ static struct termios tty;
tty = stored_tty; tty.c_lflag &= (~ECHO); tty.c_lflag &= (~ICANON);
if (tcsetattr (0, TCSANOW, &tty) < 0) DIE("tcsetattr");
more_active = 1;}
voidmore_end(void){ more_active = 0;
if (tcsetattr (0, TCSANOW, &stored_tty) < 0) DIE("tcsetattr");}
static voidsig_handler(int signal UNUSED){ cleanup(); exit(0);}
voidinput_init(void){ if (!interactive) return;
if (tcgetattr(0, &stored_tty) < 0) DIE("tcgetattr");
if (signal(SIGINT, sig_handler) == SIG_IGN) signal(SIGINT, SIG_IGN); if (signal(SIGTERM, sig_handler) == SIG_IGN) signal(SIGTERM, SIG_IGN);
struct winsize tws; if (ioctl(0, TIOCGWINSZ, &tws) == 0) { term_lns = tws.ws_row; term_cls = tws.ws_col; }}
voidcleanup(void){ if (more_active) more_end();}