1999-10-29 17:44:44 +08:00
|
|
|
/*
|
|
|
|
* BIRD Client
|
|
|
|
*
|
2004-06-01 04:51:45 +08:00
|
|
|
* (c) 1999--2004 Martin Mares <mj@ucw.cz>
|
1999-10-29 17:44:44 +08:00
|
|
|
*
|
|
|
|
* Can be freely distributed and used under the terms of the GNU GPL.
|
|
|
|
*/
|
|
|
|
|
2000-01-19 23:07:00 +08:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2000-02-15 20:18:37 +08:00
|
|
|
#include <fcntl.h>
|
2000-01-19 23:07:00 +08:00
|
|
|
#include <unistd.h>
|
2004-06-01 04:51:45 +08:00
|
|
|
#include <termios.h>
|
2000-02-15 20:18:37 +08:00
|
|
|
#include <errno.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <sys/un.h>
|
|
|
|
#include <sys/types.h>
|
2000-01-20 21:13:30 +08:00
|
|
|
#include <readline/readline.h>
|
|
|
|
#include <readline/history.h>
|
2000-01-19 23:07:00 +08:00
|
|
|
|
1999-10-29 17:44:44 +08:00
|
|
|
#include "nest/bird.h"
|
2000-01-19 23:07:00 +08:00
|
|
|
#include "lib/resource.h"
|
2000-04-01 07:30:21 +08:00
|
|
|
#include "lib/string.h"
|
1999-10-29 17:44:44 +08:00
|
|
|
#include "client/client.h"
|
2000-06-17 07:12:47 +08:00
|
|
|
#include "sysdep/unix/unix.h"
|
1999-10-29 17:44:44 +08:00
|
|
|
|
2000-02-15 20:18:37 +08:00
|
|
|
static char *opt_list = "s:v";
|
|
|
|
static int verbose;
|
|
|
|
|
2000-05-08 22:29:30 +08:00
|
|
|
static char *server_path = PATH_CONTROL_SOCKET;
|
2000-02-15 20:18:37 +08:00
|
|
|
static int server_fd;
|
|
|
|
static int server_reply;
|
|
|
|
static byte server_read_buf[4096];
|
|
|
|
static byte *server_read_pos = server_read_buf;
|
|
|
|
|
|
|
|
static int input_initialized;
|
|
|
|
static int input_hidden;
|
|
|
|
static int input_hidden_end;
|
|
|
|
|
|
|
|
/*** Parsing of arguments ***/
|
2000-01-19 22:37:56 +08:00
|
|
|
|
2000-01-19 23:07:00 +08:00
|
|
|
static void
|
|
|
|
usage(void)
|
|
|
|
{
|
2000-02-15 20:18:37 +08:00
|
|
|
fprintf(stderr, "Usage: birdc [-s <control-socket>] [-v]\n");
|
2000-01-19 23:07:00 +08: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 20:18:37 +08:00
|
|
|
case 's':
|
|
|
|
server_path = optarg;
|
|
|
|
break;
|
|
|
|
case 'v':
|
|
|
|
verbose++;
|
|
|
|
break;
|
2000-01-19 23:07:00 +08:00
|
|
|
default:
|
|
|
|
usage();
|
|
|
|
}
|
|
|
|
if (optind < argc)
|
|
|
|
usage();
|
|
|
|
}
|
2000-01-19 22:37:56 +08:00
|
|
|
|
2000-02-15 20:18:37 +08:00
|
|
|
/*** Input ***/
|
|
|
|
|
|
|
|
static void server_send(char *);
|
2000-06-17 07:12:47 +08:00
|
|
|
static void select_loop(int);
|
2000-01-20 21:13:30 +08:00
|
|
|
|
2000-02-18 07:37:16 +08:00
|
|
|
/* HACK: libreadline internals we need to access */
|
|
|
|
extern int _rl_vis_botlin;
|
|
|
|
extern void _rl_move_vert(int);
|
|
|
|
extern Function *rl_last_func;
|
|
|
|
|
2000-06-01 06:39:06 +08:00
|
|
|
static int
|
|
|
|
handle_internal_command(char *cmd)
|
|
|
|
{
|
|
|
|
if (!strncmp(cmd, "exit", 4) || !strncmp(cmd, "quit", 4))
|
|
|
|
{
|
|
|
|
cleanup();
|
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
if (!strncmp(cmd, "help", 4))
|
|
|
|
{
|
|
|
|
puts("Press `?' for context sensitive help.");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2000-02-15 20:18:37 +08:00
|
|
|
static void
|
|
|
|
got_line(char *cmd_buffer)
|
|
|
|
{
|
2000-02-28 06:00:19 +08:00
|
|
|
char *cmd;
|
|
|
|
|
2000-01-20 21:13:30 +08:00
|
|
|
if (!cmd_buffer)
|
2000-02-15 20:18:37 +08:00
|
|
|
{
|
|
|
|
cleanup();
|
|
|
|
exit(0);
|
|
|
|
}
|
2000-01-20 21:13:30 +08:00
|
|
|
if (cmd_buffer[0])
|
2000-02-15 20:18:37 +08:00
|
|
|
{
|
2000-02-28 06:00:19 +08:00
|
|
|
cmd = cmd_expand(cmd_buffer);
|
|
|
|
if (cmd)
|
|
|
|
{
|
|
|
|
add_history(cmd);
|
2000-06-01 06:39:06 +08:00
|
|
|
if (!handle_internal_command(cmd))
|
2000-02-28 06:00:19 +08:00
|
|
|
{
|
2000-06-01 06:39:06 +08:00
|
|
|
server_send(cmd);
|
|
|
|
input_hidden = -1;
|
2000-06-17 07:12:47 +08:00
|
|
|
select_loop(0);
|
2000-06-01 06:39:06 +08:00
|
|
|
input_hidden = 0;
|
2000-02-28 06:00:19 +08:00
|
|
|
}
|
|
|
|
free(cmd);
|
|
|
|
}
|
2000-03-13 06:55:09 +08:00
|
|
|
else
|
|
|
|
add_history(cmd_buffer);
|
2000-02-15 20:18:37 +08:00
|
|
|
}
|
|
|
|
free(cmd_buffer);
|
|
|
|
}
|
|
|
|
|
2000-02-18 07:37:16 +08: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 21:32:58 +08:00
|
|
|
#ifdef HAVE_RL_CRLF
|
2002-11-13 16:45:24 +08:00
|
|
|
rl_crlf();
|
2004-05-31 21:32:58 +08:00
|
|
|
#endif
|
2000-02-18 07:37:16 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
input_stop_list(void) /* Reprint the currently edited line after listing */
|
|
|
|
{
|
|
|
|
rl_on_new_line();
|
|
|
|
rl_redisplay();
|
|
|
|
}
|
|
|
|
|
2000-02-18 06:00:13 +08:00
|
|
|
static int
|
2004-06-05 17:26:48 +08:00
|
|
|
input_complete(int arg UNUSED, int key UNUSED)
|
2000-02-18 06:00:13 +08:00
|
|
|
{
|
2000-02-18 07:37:16 +08: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 21:32:58 +08:00
|
|
|
#ifdef HAVE_RL_DING
|
2002-11-13 16:45:24 +08:00
|
|
|
rl_ding();
|
2004-05-31 21:32:58 +08:00
|
|
|
#endif
|
2000-02-18 07:37:16 +08:00
|
|
|
}
|
2000-02-18 06:00:13 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2004-06-05 17:26:48 +08:00
|
|
|
input_help(int arg, int key UNUSED)
|
2000-02-18 06:00:13 +08:00
|
|
|
{
|
2009-03-14 19:43:10 +08:00
|
|
|
int i, in_string, in_bracket;
|
2000-02-18 06:00:13 +08:00
|
|
|
|
2000-02-18 07:37:16 +08:00
|
|
|
if (arg != 1)
|
2000-02-18 06:00:13 +08:00
|
|
|
return rl_insert(arg, '?');
|
2009-02-12 20:41:34 +08:00
|
|
|
|
2009-03-14 19:43:10 +08:00
|
|
|
in_string = in_bracket = 0;
|
2009-02-12 20:41:34 +08:00
|
|
|
for (i = 0; i < rl_point; i++)
|
2000-02-18 06:00:13 +08:00
|
|
|
{
|
2009-03-14 19:43:10 +08:00
|
|
|
|
2009-02-12 20:41:34 +08:00
|
|
|
if (rl_line_buffer[i] == '"')
|
|
|
|
in_string = ! in_string;
|
2009-03-14 19:43:10 +08:00
|
|
|
else if (! in_string)
|
|
|
|
{
|
|
|
|
if (rl_line_buffer[i] == '[')
|
|
|
|
in_bracket++;
|
|
|
|
else if (rl_line_buffer[i] == ']')
|
|
|
|
in_bracket--;
|
|
|
|
}
|
2000-02-18 06:00:13 +08:00
|
|
|
}
|
2009-02-12 20:41:34 +08:00
|
|
|
|
|
|
|
/* `?' inside string or path -> insert */
|
2009-03-14 19:43:10 +08:00
|
|
|
if (in_string || in_bracket)
|
2009-02-12 20:41:34 +08:00
|
|
|
return rl_insert(1, '?');
|
|
|
|
|
2000-02-18 07:37:16 +08:00
|
|
|
rl_begin_undo_group(); /* HACK: We want to display `?' at point position */
|
|
|
|
rl_insert_text("?");
|
2000-02-18 06:00:13 +08:00
|
|
|
rl_redisplay();
|
2000-02-18 07:37:16 +08: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-18 06:00:13 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2000-02-15 20:18:37 +08:00
|
|
|
static void
|
|
|
|
input_init(void)
|
|
|
|
{
|
|
|
|
rl_readline_name = "birdc";
|
2000-02-18 06:00:13 +08:00
|
|
|
rl_add_defun("bird-complete", input_complete, '\t');
|
|
|
|
rl_add_defun("bird-help", input_help, '?');
|
2000-02-15 20:18:37 +08:00
|
|
|
rl_callback_handler_install("bird> ", got_line);
|
|
|
|
input_initialized = 1;
|
2008-11-21 20:05:12 +08:00
|
|
|
// readline library does strange things when stdin is nonblocking.
|
|
|
|
// if (fcntl(0, F_SETFL, O_NONBLOCK) < 0)
|
|
|
|
// die("fcntl: %m");
|
2000-02-15 20:18:37 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
input_hide(void)
|
|
|
|
{
|
|
|
|
if (input_hidden)
|
|
|
|
return;
|
|
|
|
if (rl_line_buffer)
|
|
|
|
{
|
|
|
|
input_hidden_end = rl_end;
|
|
|
|
rl_end = 0;
|
|
|
|
rl_expand_prompt("");
|
|
|
|
rl_redisplay();
|
|
|
|
input_hidden = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
input_reveal(void)
|
|
|
|
{
|
|
|
|
if (input_hidden <= 0)
|
|
|
|
return;
|
|
|
|
rl_end = input_hidden_end;
|
|
|
|
rl_expand_prompt("bird> ");
|
|
|
|
rl_forced_update_display();
|
|
|
|
input_hidden = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
cleanup(void)
|
|
|
|
{
|
|
|
|
if (input_initialized)
|
|
|
|
{
|
|
|
|
input_initialized = 0;
|
|
|
|
input_hide();
|
|
|
|
rl_callback_handler_remove();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*** Communication with server ***/
|
|
|
|
|
|
|
|
static void
|
|
|
|
server_connect(void)
|
|
|
|
{
|
|
|
|
struct sockaddr_un sa;
|
|
|
|
|
|
|
|
server_fd = socket(AF_UNIX, SOCK_STREAM, 0);
|
|
|
|
if (server_fd < 0)
|
|
|
|
die("Cannot create socket: %m");
|
2008-10-27 06:55:38 +08:00
|
|
|
|
|
|
|
if (strlen(server_path) >= sizeof(sa.sun_path))
|
|
|
|
die("server_connect: path too long");
|
|
|
|
|
2000-02-15 20:18:37 +08:00
|
|
|
bzero(&sa, sizeof(sa));
|
|
|
|
sa.sun_family = AF_UNIX;
|
2008-08-25 20:06:20 +08:00
|
|
|
strcpy(sa.sun_path, server_path);
|
2000-06-09 15:30:22 +08:00
|
|
|
if (connect(server_fd, (struct sockaddr *) &sa, SUN_LEN(&sa)) < 0)
|
2000-02-15 20:18:37 +08:00
|
|
|
die("Unable to connect to server control socket (%s): %m", server_path);
|
|
|
|
if (fcntl(server_fd, F_SETFL, O_NONBLOCK) < 0)
|
|
|
|
die("fcntl: %m");
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
server_got_reply(char *x)
|
|
|
|
{
|
|
|
|
int code;
|
|
|
|
|
|
|
|
input_hide();
|
|
|
|
if (*x == '+') /* Async reply */
|
|
|
|
printf(">>> %s\n", x+1);
|
|
|
|
else if (x[0] == ' ') /* Continuation */
|
2000-03-05 05:26:35 +08:00
|
|
|
printf("%s%s\n", verbose ? " " : "", x+1);
|
2000-02-15 20:18:37 +08:00
|
|
|
else if (strlen(x) > 4 &&
|
|
|
|
sscanf(x, "%d", &code) == 1 && code >= 0 && code < 10000 &&
|
|
|
|
(x[4] == ' ' || x[4] == '-'))
|
|
|
|
{
|
|
|
|
if (code)
|
|
|
|
printf("%s\n", verbose ? x : x+5);
|
|
|
|
if (x[4] == ' ')
|
|
|
|
server_reply = code;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
printf("??? <%s>\n", x);
|
2004-06-01 01:55:30 +08:00
|
|
|
/* need this, otherwise some lib seems to eat pending output when
|
|
|
|
the prompt is displayed */
|
|
|
|
fflush(stdout);
|
|
|
|
tcdrain(fileno(stdout));
|
2000-02-15 20:18:37 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
server_read(void)
|
|
|
|
{
|
|
|
|
int c;
|
|
|
|
byte *start, *p;
|
|
|
|
|
2008-11-21 19:59:03 +08:00
|
|
|
redo:
|
2000-02-15 20:18:37 +08:00
|
|
|
c = read(server_fd, server_read_pos, server_read_buf + sizeof(server_read_buf) - server_read_pos);
|
|
|
|
if (!c)
|
|
|
|
die("Connection closed by server.");
|
|
|
|
if (c < 0)
|
2008-11-21 19:59:03 +08:00
|
|
|
{
|
|
|
|
if (errno == EINTR)
|
|
|
|
goto redo;
|
|
|
|
else
|
|
|
|
die("Server read error: %m");
|
|
|
|
}
|
|
|
|
|
2000-02-15 20:18:37 +08:00
|
|
|
start = server_read_buf;
|
|
|
|
p = server_read_pos;
|
|
|
|
server_read_pos += c;
|
|
|
|
while (p < server_read_pos)
|
|
|
|
if (*p++ == '\n')
|
|
|
|
{
|
|
|
|
p[-1] = 0;
|
|
|
|
server_got_reply(start);
|
|
|
|
start = p;
|
|
|
|
}
|
|
|
|
if (start != server_read_buf)
|
|
|
|
{
|
|
|
|
int l = server_read_pos - start;
|
|
|
|
memmove(server_read_buf, start, l);
|
|
|
|
server_read_pos = server_read_buf + l;
|
|
|
|
}
|
|
|
|
else if (server_read_pos == server_read_buf + sizeof(server_read_buf))
|
|
|
|
{
|
|
|
|
strcpy(server_read_buf, "?<too-long>");
|
|
|
|
server_read_pos = server_read_buf + 11;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static fd_set select_fds;
|
|
|
|
|
|
|
|
static void
|
2000-06-17 07:12:47 +08:00
|
|
|
select_loop(int mode)
|
2000-02-15 20:18:37 +08:00
|
|
|
{
|
2008-11-21 19:59:03 +08:00
|
|
|
int rv;
|
2000-02-15 20:18:37 +08:00
|
|
|
server_reply = -1;
|
|
|
|
while (mode || server_reply < 0)
|
|
|
|
{
|
2000-05-30 06:16:04 +08:00
|
|
|
FD_ZERO(&select_fds);
|
2000-02-15 20:18:37 +08:00
|
|
|
FD_SET(server_fd, &select_fds);
|
|
|
|
if (mode)
|
|
|
|
FD_SET(0, &select_fds);
|
2008-11-21 19:59:03 +08: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 20:18:37 +08:00
|
|
|
if (FD_ISSET(server_fd, &select_fds))
|
|
|
|
{
|
|
|
|
server_read();
|
|
|
|
if (mode)
|
|
|
|
input_reveal();
|
|
|
|
}
|
|
|
|
if (FD_ISSET(0, &select_fds))
|
|
|
|
rl_callback_read_char();
|
|
|
|
}
|
|
|
|
input_reveal();
|
|
|
|
}
|
|
|
|
|
2008-11-21 19:59:03 +08:00
|
|
|
static void
|
|
|
|
wait_for_write(int fd)
|
|
|
|
{
|
|
|
|
while (1)
|
|
|
|
{
|
|
|
|
int rv;
|
|
|
|
fd_set set;
|
|
|
|
FD_ZERO(&set);
|
|
|
|
FD_SET(fd, &set);
|
|
|
|
|
|
|
|
rv = select(fd+1, NULL, &set, NULL, NULL);
|
|
|
|
if (rv < 0)
|
|
|
|
{
|
|
|
|
if (errno == EINTR)
|
|
|
|
continue;
|
|
|
|
else
|
|
|
|
die("select: %m");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (FD_ISSET(server_fd, &set))
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2000-02-15 20:18:37 +08:00
|
|
|
static void
|
|
|
|
server_send(char *cmd)
|
|
|
|
{
|
|
|
|
int l = strlen(cmd);
|
|
|
|
byte *z = alloca(l + 1);
|
|
|
|
|
|
|
|
memcpy(z, cmd, l);
|
|
|
|
z[l++] = '\n';
|
|
|
|
while (l)
|
|
|
|
{
|
|
|
|
int cnt = write(server_fd, z, l);
|
2008-11-21 19:59:03 +08:00
|
|
|
|
2000-02-15 20:18:37 +08:00
|
|
|
if (cnt < 0)
|
|
|
|
{
|
2008-11-21 19:59:03 +08:00
|
|
|
if (errno == EAGAIN)
|
|
|
|
wait_for_write(server_fd);
|
|
|
|
else if (errno == EINTR)
|
|
|
|
continue;
|
2000-02-15 20:18:37 +08:00
|
|
|
else
|
|
|
|
die("Server write error: %m");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
l -= cnt;
|
|
|
|
z += cnt;
|
|
|
|
}
|
|
|
|
}
|
2000-01-20 21:13:30 +08:00
|
|
|
}
|
|
|
|
|
1999-10-29 17:44:44 +08:00
|
|
|
int
|
|
|
|
main(int argc, char **argv)
|
|
|
|
{
|
2000-01-19 23:07:00 +08:00
|
|
|
#ifdef HAVE_LIBDMALLOC
|
|
|
|
if (!getenv("DMALLOC_OPTIONS"))
|
|
|
|
dmalloc_debug(0x2f03d00);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
parse_args(argc, argv);
|
2000-02-18 06:00:13 +08:00
|
|
|
cmd_build_tree();
|
2000-02-15 20:18:37 +08:00
|
|
|
server_connect();
|
2000-06-17 07:12:47 +08:00
|
|
|
select_loop(0);
|
2000-01-19 23:07:00 +08:00
|
|
|
|
2000-02-15 20:18:37 +08:00
|
|
|
input_init();
|
|
|
|
|
2000-06-17 07:12:47 +08:00
|
|
|
select_loop(1);
|
2000-02-15 20:18:37 +08:00
|
|
|
return 0;
|
1999-10-29 17:44:44 +08:00
|
|
|
}
|