1999-10-29 09:44:44 +00:00
|
|
|
/*
|
|
|
|
* BIRD Client
|
|
|
|
*
|
2004-05-31 20:51:45 +00:00
|
|
|
* (c) 1999--2004 Martin Mares <mj@ucw.cz>
|
1999-10-29 09:44:44 +00:00
|
|
|
*
|
|
|
|
* Can be freely distributed and used under the terms of the GNU GPL.
|
|
|
|
*/
|
|
|
|
|
2000-01-19 15:07:00 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2000-02-15 12:18:37 +00:00
|
|
|
#include <fcntl.h>
|
2000-01-19 15:07:00 +00:00
|
|
|
#include <unistd.h>
|
2004-05-31 20:51:45 +00:00
|
|
|
#include <termios.h>
|
2000-02-15 12:18:37 +00:00
|
|
|
#include <errno.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <sys/un.h>
|
|
|
|
#include <sys/types.h>
|
2000-01-20 13:13:30 +00:00
|
|
|
#include <readline/readline.h>
|
|
|
|
#include <readline/history.h>
|
2009-07-14 23:47:29 +00:00
|
|
|
#include <curses.h>
|
2000-01-19 15:07:00 +00:00
|
|
|
|
1999-10-29 09:44:44 +00:00
|
|
|
#include "nest/bird.h"
|
2000-01-19 15:07:00 +00:00
|
|
|
#include "lib/resource.h"
|
2000-03-31 23:30:21 +00:00
|
|
|
#include "lib/string.h"
|
1999-10-29 09:44:44 +00:00
|
|
|
#include "client/client.h"
|
2000-06-16 23:12:47 +00:00
|
|
|
#include "sysdep/unix/unix.h"
|
1999-10-29 09:44:44 +00:00
|
|
|
|
2010-02-21 08:57:26 +00:00
|
|
|
static char *opt_list = "s:vr";
|
2000-02-15 12:18:37 +00:00
|
|
|
static int verbose;
|
2010-02-21 08:57:26 +00:00
|
|
|
static char *init_cmd;
|
|
|
|
static int once;
|
2013-02-12 12:15:01 +00:00
|
|
|
static int restricted;
|
2000-02-15 12:18:37 +00:00
|
|
|
|
2013-01-23 16:14:53 +00:00
|
|
|
extern char *server_path;
|
|
|
|
extern int server_fd;
|
|
|
|
extern byte server_read_buf[SERVER_READ_BUF_LEN];
|
|
|
|
extern byte *server_read_pos;
|
2000-02-15 12:18:37 +00:00
|
|
|
|
2013-01-23 16:14:53 +00:00
|
|
|
extern int input_initialized;
|
|
|
|
extern int input_hidden_end;
|
|
|
|
extern int cstate;
|
|
|
|
extern int nstate;
|
2009-07-14 23:47:29 +00:00
|
|
|
|
2013-01-23 16:14:53 +00:00
|
|
|
extern int num_lines, skip_input, interactive;
|
2000-02-15 12:18:37 +00:00
|
|
|
|
|
|
|
/*** Parsing of arguments ***/
|
2000-01-19 14:37:56 +00:00
|
|
|
|
2000-01-19 15:07:00 +00:00
|
|
|
static void
|
|
|
|
usage(void)
|
|
|
|
{
|
2010-02-21 08:57:26 +00:00
|
|
|
fprintf(stderr, "Usage: birdc [-s <control-socket>] [-v] [-r]\n");
|
2000-01-19 15:07:00 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
parse_args(int argc, char **argv)
|
|
|
|
{
|
|
|
|
int c;
|
|
|
|
|
|
|
|
while ((c = getopt(argc, argv, opt_list)) >= 0)
|
|
|
|
switch (c)
|
|
|
|
{
|
2000-02-15 12:18:37 +00:00
|
|
|
case 's':
|
|
|
|
server_path = optarg;
|
|
|
|
break;
|
|
|
|
case 'v':
|
|
|
|
verbose++;
|
|
|
|
break;
|
2010-02-21 08:57:26 +00:00
|
|
|
case 'r':
|
2013-02-12 12:15:01 +00:00
|
|
|
restricted = 1;
|
2010-02-21 08:57:26 +00:00
|
|
|
break;
|
2000-01-19 15:07:00 +00:00
|
|
|
default:
|
|
|
|
usage();
|
|
|
|
}
|
2010-02-21 08:57:26 +00:00
|
|
|
|
|
|
|
/* If some arguments are not options, we take it as commands */
|
2000-01-19 15:07:00 +00:00
|
|
|
if (optind < argc)
|
2010-02-21 08:57:26 +00:00
|
|
|
{
|
|
|
|
char *tmp;
|
|
|
|
int i;
|
|
|
|
int len = 0;
|
|
|
|
|
|
|
|
for (i = optind; i < argc; i++)
|
|
|
|
len += strlen(argv[i]) + 1;
|
|
|
|
|
|
|
|
tmp = init_cmd = malloc(len);
|
|
|
|
for (i = optind; i < argc; i++)
|
|
|
|
{
|
|
|
|
strcpy(tmp, argv[i]);
|
|
|
|
tmp += strlen(tmp);
|
|
|
|
*tmp++ = ' ';
|
|
|
|
}
|
2010-12-13 10:17:11 +00:00
|
|
|
tmp[-1] = 0;
|
2010-02-21 08:57:26 +00:00
|
|
|
|
|
|
|
once = 1;
|
|
|
|
}
|
2000-01-19 15:07:00 +00:00
|
|
|
}
|
2000-01-19 14:37:56 +00:00
|
|
|
|
2000-02-15 12:18:37 +00:00
|
|
|
/*** Input ***/
|
|
|
|
|
2000-02-17 23:37:16 +00:00
|
|
|
/* HACK: libreadline internals we need to access */
|
|
|
|
extern int _rl_vis_botlin;
|
|
|
|
extern void _rl_move_vert(int);
|
|
|
|
extern Function *rl_last_func;
|
|
|
|
|
2012-05-03 22:20:23 +00:00
|
|
|
static void
|
|
|
|
add_history_dedup(char *cmd)
|
|
|
|
{
|
|
|
|
/* Add history line if it differs from the last one */
|
|
|
|
HIST_ENTRY *he = history_get(history_length);
|
|
|
|
if (!he || strcmp(he->line, cmd))
|
|
|
|
add_history(cmd);
|
|
|
|
}
|
2009-07-14 23:47:29 +00:00
|
|
|
|
2013-01-23 16:14:53 +00:00
|
|
|
void got_line(char *cmd_buffer)
|
2000-02-15 12:18:37 +00:00
|
|
|
{
|
2000-02-27 22:00:19 +00:00
|
|
|
char *cmd;
|
|
|
|
|
2000-01-20 13:13:30 +00:00
|
|
|
if (!cmd_buffer)
|
2000-02-15 12:18:37 +00:00
|
|
|
{
|
|
|
|
cleanup();
|
|
|
|
exit(0);
|
|
|
|
}
|
2000-01-20 13:13:30 +00:00
|
|
|
if (cmd_buffer[0])
|
2000-02-15 12:18:37 +00:00
|
|
|
{
|
2000-02-27 22:00:19 +00:00
|
|
|
cmd = cmd_expand(cmd_buffer);
|
|
|
|
if (cmd)
|
2013-01-23 16:14:53 +00:00
|
|
|
{
|
|
|
|
add_history_dedup(cmd);
|
2009-07-14 23:47:29 +00:00
|
|
|
|
2013-01-23 16:14:53 +00:00
|
|
|
if (!handle_internal_command(cmd))
|
|
|
|
submit_server_command(cmd);
|
2009-07-14 23:47:29 +00:00
|
|
|
|
2013-01-23 16:14:53 +00:00
|
|
|
free(cmd);
|
|
|
|
}
|
2000-03-12 22:55:09 +00:00
|
|
|
else
|
2013-01-23 16:14:53 +00:00
|
|
|
add_history_dedup(cmd_buffer);
|
2000-02-15 12:18:37 +00:00
|
|
|
}
|
|
|
|
free(cmd_buffer);
|
|
|
|
}
|
|
|
|
|
2000-02-17 23:37:16 +00:00
|
|
|
void
|
|
|
|
input_start_list(void) /* Leave the currently edited line and make space for listing */
|
|
|
|
{
|
|
|
|
_rl_move_vert(_rl_vis_botlin);
|
2004-05-31 13:32:58 +00:00
|
|
|
#ifdef HAVE_RL_CRLF
|
2002-11-13 08:45:24 +00:00
|
|
|
rl_crlf();
|
2004-05-31 13:32:58 +00:00
|
|
|
#endif
|
2000-02-17 23:37:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
input_stop_list(void) /* Reprint the currently edited line after listing */
|
|
|
|
{
|
|
|
|
rl_on_new_line();
|
|
|
|
rl_redisplay();
|
|
|
|
}
|
|
|
|
|
2000-02-17 22:00:13 +00:00
|
|
|
static int
|
2004-06-05 09:26:48 +00:00
|
|
|
input_complete(int arg UNUSED, int key UNUSED)
|
2000-02-17 22:00:13 +00:00
|
|
|
{
|
2000-02-17 23:37:16 +00:00
|
|
|
static int complete_flag;
|
|
|
|
char buf[256];
|
|
|
|
|
|
|
|
if (rl_last_func != input_complete)
|
|
|
|
complete_flag = 0;
|
|
|
|
switch (cmd_complete(rl_line_buffer, rl_point, buf, complete_flag))
|
|
|
|
{
|
|
|
|
case 0:
|
|
|
|
complete_flag = 1;
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
rl_insert_text(buf);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
complete_flag = 1;
|
2004-05-31 13:32:58 +00:00
|
|
|
#ifdef HAVE_RL_DING
|
2002-11-13 08:45:24 +00:00
|
|
|
rl_ding();
|
2004-05-31 13:32:58 +00:00
|
|
|
#endif
|
2000-02-17 23:37:16 +00:00
|
|
|
}
|
2000-02-17 22:00:13 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2004-06-05 09:26:48 +00:00
|
|
|
input_help(int arg, int key UNUSED)
|
2000-02-17 22:00:13 +00:00
|
|
|
{
|
2009-03-14 11:43:10 +00:00
|
|
|
int i, in_string, in_bracket;
|
2000-02-17 22:00:13 +00:00
|
|
|
|
2000-02-17 23:37:16 +00:00
|
|
|
if (arg != 1)
|
2000-02-17 22:00:13 +00:00
|
|
|
return rl_insert(arg, '?');
|
2009-02-12 12:41:34 +00:00
|
|
|
|
2009-03-14 11:43:10 +00:00
|
|
|
in_string = in_bracket = 0;
|
2009-02-12 12:41:34 +00:00
|
|
|
for (i = 0; i < rl_point; i++)
|
2000-02-17 22:00:13 +00:00
|
|
|
{
|
2009-03-14 11:43:10 +00:00
|
|
|
|
2009-02-12 12:41:34 +00:00
|
|
|
if (rl_line_buffer[i] == '"')
|
|
|
|
in_string = ! in_string;
|
2009-03-14 11:43:10 +00:00
|
|
|
else if (! in_string)
|
|
|
|
{
|
|
|
|
if (rl_line_buffer[i] == '[')
|
|
|
|
in_bracket++;
|
|
|
|
else if (rl_line_buffer[i] == ']')
|
|
|
|
in_bracket--;
|
|
|
|
}
|
2000-02-17 22:00:13 +00:00
|
|
|
}
|
2009-02-12 12:41:34 +00:00
|
|
|
|
|
|
|
/* `?' inside string or path -> insert */
|
2009-03-14 11:43:10 +00:00
|
|
|
if (in_string || in_bracket)
|
2009-02-12 12:41:34 +00:00
|
|
|
return rl_insert(1, '?');
|
|
|
|
|
2000-02-17 23:37:16 +00:00
|
|
|
rl_begin_undo_group(); /* HACK: We want to display `?' at point position */
|
|
|
|
rl_insert_text("?");
|
2000-02-17 22:00:13 +00:00
|
|
|
rl_redisplay();
|
2000-02-17 23:37:16 +00:00
|
|
|
rl_end_undo_group();
|
|
|
|
input_start_list();
|
|
|
|
cmd_help(rl_line_buffer, rl_point);
|
|
|
|
rl_undo_command(1, 0);
|
|
|
|
input_stop_list();
|
2000-02-17 22:00:13 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2000-02-15 12:18:37 +00:00
|
|
|
static void
|
|
|
|
input_init(void)
|
|
|
|
{
|
|
|
|
rl_readline_name = "birdc";
|
2000-02-17 22:00:13 +00:00
|
|
|
rl_add_defun("bird-complete", input_complete, '\t');
|
|
|
|
rl_add_defun("bird-help", input_help, '?');
|
2000-02-15 12:18:37 +00:00
|
|
|
rl_callback_handler_install("bird> ", got_line);
|
|
|
|
input_initialized = 1;
|
2008-11-21 12:05:12 +00:00
|
|
|
// readline library does strange things when stdin is nonblocking.
|
|
|
|
// if (fcntl(0, F_SETFL, O_NONBLOCK) < 0)
|
|
|
|
// die("fcntl: %m");
|
2000-02-15 12:18:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
input_hide(void)
|
|
|
|
{
|
2009-07-14 23:47:29 +00:00
|
|
|
input_hidden_end = rl_end;
|
|
|
|
rl_end = 0;
|
|
|
|
rl_expand_prompt("");
|
|
|
|
rl_redisplay();
|
2000-02-15 12:18:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
input_reveal(void)
|
|
|
|
{
|
2009-07-14 23:47:29 +00:00
|
|
|
/* need this, otherwise some lib seems to eat pending output when
|
|
|
|
the prompt is displayed */
|
|
|
|
fflush(stdout);
|
|
|
|
tcdrain(fileno(stdout));
|
|
|
|
|
2000-02-15 12:18:37 +00:00
|
|
|
rl_end = input_hidden_end;
|
|
|
|
rl_expand_prompt("bird> ");
|
|
|
|
rl_forced_update_display();
|
|
|
|
}
|
|
|
|
|
2009-07-14 23:47:29 +00:00
|
|
|
void
|
|
|
|
update_state(void)
|
|
|
|
{
|
|
|
|
if (nstate == cstate)
|
|
|
|
return;
|
|
|
|
|
2013-02-12 12:15:01 +00:00
|
|
|
if (restricted)
|
|
|
|
{
|
|
|
|
submit_server_command("restrict");
|
|
|
|
restricted = 0;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2010-02-21 08:57:26 +00:00
|
|
|
if (init_cmd)
|
|
|
|
{
|
|
|
|
/* First transition - client received hello from BIRD
|
|
|
|
and there is waiting initial command */
|
|
|
|
submit_server_command(init_cmd);
|
|
|
|
init_cmd = NULL;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!init_cmd && once)
|
|
|
|
{
|
|
|
|
/* Initial command is finished and we want to exit */
|
|
|
|
cleanup();
|
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
|
2009-07-14 23:47:29 +00:00
|
|
|
if (nstate == STATE_PROMPT)
|
2010-02-21 13:34:53 +00:00
|
|
|
{
|
|
|
|
if (input_initialized)
|
|
|
|
input_reveal();
|
|
|
|
else
|
|
|
|
input_init();
|
|
|
|
}
|
2009-07-14 23:47:29 +00:00
|
|
|
|
|
|
|
if (nstate != STATE_PROMPT)
|
|
|
|
input_hide();
|
|
|
|
|
|
|
|
cstate = nstate;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
more(void)
|
|
|
|
{
|
|
|
|
printf("--More--\015");
|
|
|
|
fflush(stdout);
|
|
|
|
|
|
|
|
redo:
|
|
|
|
switch (getchar())
|
|
|
|
{
|
|
|
|
case 32:
|
|
|
|
num_lines = 2;
|
|
|
|
break;
|
|
|
|
case 13:
|
|
|
|
num_lines--;
|
|
|
|
break;
|
|
|
|
case 'q':
|
|
|
|
skip_input = 1;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
goto redo;
|
|
|
|
}
|
|
|
|
|
|
|
|
printf(" \015");
|
|
|
|
fflush(stdout);
|
|
|
|
}
|
|
|
|
|
2013-01-23 16:14:53 +00:00
|
|
|
void cleanup(void)
|
2000-02-15 12:18:37 +00:00
|
|
|
{
|
2013-01-23 16:14:53 +00:00
|
|
|
if (input_initialized)
|
2009-08-12 08:16:32 +00:00
|
|
|
{
|
2013-01-23 16:14:53 +00:00
|
|
|
input_initialized = 0;
|
|
|
|
input_hide();
|
|
|
|
rl_callback_handler_remove();
|
2009-08-12 08:16:32 +00:00
|
|
|
}
|
2000-02-15 12:18:37 +00:00
|
|
|
}
|
|
|
|
|
2008-11-21 11:59:03 +00:00
|
|
|
|
2013-01-23 16:14:53 +00:00
|
|
|
/*** Communication with server ***/
|
2000-02-15 12:18:37 +00:00
|
|
|
|
|
|
|
static fd_set select_fds;
|
|
|
|
|
|
|
|
static void
|
2009-07-14 23:47:29 +00:00
|
|
|
select_loop(void)
|
2000-02-15 12:18:37 +00:00
|
|
|
{
|
2008-11-21 11:59:03 +00:00
|
|
|
int rv;
|
2009-07-14 23:47:29 +00:00
|
|
|
while (1)
|
2000-02-15 12:18:37 +00:00
|
|
|
{
|
2000-05-29 22:16:04 +00:00
|
|
|
FD_ZERO(&select_fds);
|
2009-07-14 23:47:29 +00:00
|
|
|
|
|
|
|
if (cstate != STATE_CMD_USER)
|
|
|
|
FD_SET(server_fd, &select_fds);
|
|
|
|
if (cstate != STATE_CMD_SERVER)
|
2000-02-15 12:18:37 +00:00
|
|
|
FD_SET(0, &select_fds);
|
2008-11-21 11:59:03 +00:00
|
|
|
|
|
|
|
rv = select(server_fd+1, &select_fds, NULL, NULL, NULL);
|
|
|
|
if (rv < 0)
|
|
|
|
{
|
|
|
|
if (errno == EINTR)
|
|
|
|
continue;
|
|
|
|
else
|
|
|
|
die("select: %m");
|
|
|
|
}
|
|
|
|
|
2000-02-15 12:18:37 +00:00
|
|
|
if (FD_ISSET(server_fd, &select_fds))
|
|
|
|
{
|
|
|
|
server_read();
|
2009-07-14 23:47:29 +00:00
|
|
|
update_state();
|
2000-02-15 12:18:37 +00:00
|
|
|
}
|
2009-07-14 23:47:29 +00:00
|
|
|
|
2000-02-15 12:18:37 +00:00
|
|
|
if (FD_ISSET(0, &select_fds))
|
2009-07-14 23:47:29 +00:00
|
|
|
{
|
|
|
|
rl_callback_read_char();
|
|
|
|
update_state();
|
|
|
|
}
|
2000-02-15 12:18:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-01-23 16:14:53 +00:00
|
|
|
#define PRINTF(LEN, PARGS...) do { if (!skip_input) len = printf(PARGS); } while(0)
|
2008-11-21 11:59:03 +00:00
|
|
|
|
2013-01-23 16:14:53 +00:00
|
|
|
void server_got_reply(char *x)
|
|
|
|
{
|
|
|
|
int code;
|
|
|
|
int len = 0;
|
2008-11-21 11:59:03 +00:00
|
|
|
|
2013-01-23 16:14:53 +00:00
|
|
|
if (*x == '+') /* Async reply */
|
|
|
|
PRINTF(len, ">>> %s\n", x+1);
|
|
|
|
else if (x[0] == ' ') /* Continuation */
|
|
|
|
PRINTF(len, "%s%s\n", verbose ? " " : "", x+1);
|
|
|
|
else if (strlen(x) > 4 &&
|
|
|
|
sscanf(x, "%d", &code) == 1 && code >= 0 && code < 10000 &&
|
|
|
|
(x[4] == ' ' || x[4] == '-'))
|
|
|
|
{
|
|
|
|
if (code)
|
|
|
|
PRINTF(len, "%s\n", verbose ? x : x+5);
|
|
|
|
if (x[4] == ' ')
|
|
|
|
{
|
|
|
|
nstate = STATE_PROMPT;
|
|
|
|
skip_input = 0;
|
|
|
|
return;
|
|
|
|
}
|
2008-11-21 11:59:03 +00:00
|
|
|
}
|
2013-01-23 16:14:53 +00:00
|
|
|
else
|
|
|
|
PRINTF(len, "??? <%s>\n", x);
|
2008-11-21 11:59:03 +00:00
|
|
|
|
2013-01-23 16:14:53 +00:00
|
|
|
if (skip_input)
|
|
|
|
return;
|
2000-02-15 12:18:37 +00:00
|
|
|
|
2013-01-23 16:14:53 +00:00
|
|
|
if (interactive && input_initialized && (len > 0))
|
2000-02-15 12:18:37 +00:00
|
|
|
{
|
2013-01-23 16:14:53 +00:00
|
|
|
int lns = LINES ? LINES : 25;
|
|
|
|
int cls = COLS ? COLS : 80;
|
|
|
|
num_lines += (len + cls - 1) / cls; /* Divide and round up */
|
|
|
|
if ((num_lines >= lns) && (cstate == STATE_CMD_SERVER))
|
|
|
|
more();
|
2000-02-15 12:18:37 +00:00
|
|
|
}
|
2000-01-20 13:13:30 +00:00
|
|
|
}
|
|
|
|
|
1999-10-29 09:44:44 +00:00
|
|
|
int
|
|
|
|
main(int argc, char **argv)
|
|
|
|
{
|
2000-01-19 15:07:00 +00:00
|
|
|
#ifdef HAVE_LIBDMALLOC
|
|
|
|
if (!getenv("DMALLOC_OPTIONS"))
|
|
|
|
dmalloc_debug(0x2f03d00);
|
|
|
|
#endif
|
|
|
|
|
2009-07-14 23:47:29 +00:00
|
|
|
interactive = isatty(0);
|
2000-01-19 15:07:00 +00:00
|
|
|
parse_args(argc, argv);
|
2000-02-17 22:00:13 +00:00
|
|
|
cmd_build_tree();
|
2000-02-15 12:18:37 +00:00
|
|
|
server_connect();
|
2009-07-14 23:47:29 +00:00
|
|
|
select_loop();
|
2000-02-15 12:18:37 +00:00
|
|
|
return 0;
|
1999-10-29 09:44:44 +00:00
|
|
|
}
|