1999-10-29 09:44:44 +00:00
|
|
|
/*
|
|
|
|
* BIRD Client
|
|
|
|
*
|
2000-01-19 14:37:56 +00:00
|
|
|
* (c) 1999--2000 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>
|
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>
|
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
|
|
|
|
2000-02-15 12:18:37 +00:00
|
|
|
static char *opt_list = "s:v";
|
|
|
|
static int verbose;
|
|
|
|
|
2000-05-08 14:29:30 +00:00
|
|
|
static char *server_path = PATH_CONTROL_SOCKET;
|
2000-02-15 12:18:37 +00: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 14:37:56 +00:00
|
|
|
|
2000-01-19 15:07:00 +00:00
|
|
|
static void
|
|
|
|
usage(void)
|
|
|
|
{
|
2000-02-15 12:18:37 +00:00
|
|
|
fprintf(stderr, "Usage: birdc [-s <control-socket>] [-v]\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;
|
2000-01-19 15:07:00 +00:00
|
|
|
default:
|
|
|
|
usage();
|
|
|
|
}
|
|
|
|
if (optind < argc)
|
|
|
|
usage();
|
|
|
|
}
|
2000-01-19 14:37:56 +00:00
|
|
|
|
2000-02-15 12:18:37 +00:00
|
|
|
/*** Input ***/
|
|
|
|
|
|
|
|
static void server_send(char *);
|
2000-06-16 23:12:47 +00:00
|
|
|
static void select_loop(int);
|
2000-01-20 13:13:30 +00:00
|
|
|
|
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;
|
|
|
|
|
2000-05-31 22:39:06 +00: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 12:18:37 +00:00
|
|
|
static void
|
|
|
|
got_line(char *cmd_buffer)
|
|
|
|
{
|
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)
|
|
|
|
{
|
|
|
|
add_history(cmd);
|
2000-05-31 22:39:06 +00:00
|
|
|
if (!handle_internal_command(cmd))
|
2000-02-27 22:00:19 +00:00
|
|
|
{
|
2000-05-31 22:39:06 +00:00
|
|
|
server_send(cmd);
|
|
|
|
input_hidden = -1;
|
2000-06-16 23:12:47 +00:00
|
|
|
select_loop(0);
|
2000-05-31 22:39:06 +00:00
|
|
|
input_hidden = 0;
|
2000-02-27 22:00:19 +00:00
|
|
|
}
|
|
|
|
free(cmd);
|
|
|
|
}
|
2000-03-12 22:55:09 +00:00
|
|
|
else
|
|
|
|
add_history(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
|
|
|
|
input_complete(int arg, int key)
|
|
|
|
{
|
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
|
|
|
|
input_help(int arg, int key)
|
|
|
|
{
|
|
|
|
int i = 0;
|
|
|
|
|
2000-02-17 23:37:16 +00:00
|
|
|
if (arg != 1)
|
2000-02-17 22:00:13 +00:00
|
|
|
return rl_insert(arg, '?');
|
2000-02-17 23:37:16 +00:00
|
|
|
while (i < rl_point)
|
2000-02-17 22:00:13 +00:00
|
|
|
{
|
|
|
|
if (rl_line_buffer[i++] == '"')
|
|
|
|
do
|
|
|
|
{
|
2000-02-17 23:37:16 +00:00
|
|
|
if (i >= rl_point) /* `?' inside quoted string -> insert */
|
2000-02-17 22:00:13 +00:00
|
|
|
return rl_insert(1, '?');
|
|
|
|
}
|
|
|
|
while (rl_line_buffer[i++] != '"');
|
|
|
|
}
|
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;
|
|
|
|
if (fcntl(0, F_SETFL, O_NONBLOCK) < 0)
|
|
|
|
die("fcntl: %m");
|
|
|
|
}
|
|
|
|
|
|
|
|
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");
|
|
|
|
bzero(&sa, sizeof(sa));
|
|
|
|
sa.sun_family = AF_UNIX;
|
|
|
|
strcpy(sa.sun_path, server_path);
|
2000-06-09 07:30:22 +00:00
|
|
|
if (connect(server_fd, (struct sockaddr *) &sa, SUN_LEN(&sa)) < 0)
|
2000-02-15 12:18:37 +00: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-04 21:26:35 +00:00
|
|
|
printf("%s%s\n", verbose ? " " : "", x+1);
|
2000-02-15 12:18:37 +00: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);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
server_read(void)
|
|
|
|
{
|
|
|
|
int c;
|
|
|
|
byte *start, *p;
|
|
|
|
|
|
|
|
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)
|
|
|
|
die("Server read error: %m");
|
|
|
|
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-16 23:12:47 +00:00
|
|
|
select_loop(int mode)
|
2000-02-15 12:18:37 +00:00
|
|
|
{
|
|
|
|
server_reply = -1;
|
|
|
|
while (mode || server_reply < 0)
|
|
|
|
{
|
2000-05-29 22:16:04 +00:00
|
|
|
FD_ZERO(&select_fds);
|
2000-02-15 12:18:37 +00:00
|
|
|
FD_SET(server_fd, &select_fds);
|
|
|
|
if (mode)
|
|
|
|
FD_SET(0, &select_fds);
|
|
|
|
select(server_fd+1, &select_fds, NULL, NULL, NULL);
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
if (cnt < 0)
|
|
|
|
{
|
|
|
|
if (errno == -EAGAIN)
|
|
|
|
{
|
|
|
|
fd_set set;
|
|
|
|
FD_ZERO(&set);
|
|
|
|
do
|
|
|
|
{
|
|
|
|
FD_SET(server_fd, &set);
|
|
|
|
select(server_fd+1, NULL, &set, NULL, NULL);
|
|
|
|
}
|
|
|
|
while (!FD_ISSET(server_fd, &set));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
die("Server write error: %m");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
l -= cnt;
|
|
|
|
z += cnt;
|
|
|
|
}
|
|
|
|
}
|
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
|
|
|
|
|
|
|
|
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();
|
2000-06-16 23:12:47 +00:00
|
|
|
select_loop(0);
|
2000-01-19 15:07:00 +00:00
|
|
|
|
2000-02-15 12:18:37 +00:00
|
|
|
input_init();
|
|
|
|
|
2000-06-16 23:12:47 +00:00
|
|
|
select_loop(1);
|
2000-02-15 12:18:37 +00:00
|
|
|
return 0;
|
1999-10-29 09:44:44 +00:00
|
|
|
}
|