/*
Bacula® - The Network Backup Solution
- Copyright (C) 2000-2009 Free Software Foundation Europe e.V.
+ Copyright (C) 2000-2011 Free Software Foundation Europe e.V.
The main author of Bacula is Kern Sibbald, with contributions from
many others, a complete list can be found in the file AUTHORS.
This program is Free Software; you can redistribute it and/or
- modify it under the terms of version two of the GNU General Public
+ modify it under the terms of version three of the GNU Affero General Public
License as published by the Free Software Foundation and included
in the file LICENSE.
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
- You should have received a copy of the GNU General Public License
+ You should have received a copy of the GNU Affero General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301, USA.
*
* Kern Sibbald, September MM
*
- * Version $Id$
*/
#include "bacula.h"
static int timeout = 0;
static int argc;
static int numdir;
-static int numcon;
static POOLMEM *args;
static char *argk[MAX_CMD_ARGS];
static char *argv[MAX_CMD_ARGS];
PROG_COPYRIGHT
"\nVersion: " VERSION " (" BDATE ") %s %s %s\n\n"
"Usage: bconsole [-s] [-c config_file] [-d debug_level]\n"
+" -D <dir> select a Director\n"
+" -l list Directors defined\n"
" -c <file> set configuration file to file\n"
" -d <nn> set debug level to <nn>\n"
" -dt print timestamp in debug output\n"
} else if (stat == 0) { /* timeout */
if (strcmp(prompt, "*") == 0) {
tid = start_bsock_timer(UA_sock, timeout);
- bnet_fsend(UA_sock, ".messages");
+ UA_sock->fsend(".messages");
stop_bsock_timer(tid);
} else {
continue;
}
} else {
- at_prompt = FALSE;
+ at_prompt = false;
/* @ => internal command for us */
if (UA_sock->msg[0] == '@') {
parse_args(UA_sock->msg, &args, &argc, argk, argv, MAX_CMD_ARGS);
continue;
}
tid = start_bsock_timer(UA_sock, timeout);
- if (!bnet_send(UA_sock)) { /* send command */
+ if (!UA_sock->send()) { /* send command */
stop_bsock_timer(tid);
break; /* error */
}
break;
}
tid = start_bsock_timer(UA_sock, timeout);
- while ((stat = bnet_recv(UA_sock)) >= 0) {
+ while ((stat = UA_sock->recv()) >= 0) {
if (at_prompt) {
if (!stop) {
sendit("\n");
if (is_bnet_stop(UA_sock)) {
break; /* error or term */
} else if (stat == BNET_SIGNAL) {
- if (UA_sock->msglen == BNET_PROMPT) {
+ if (UA_sock->msglen == BNET_SUB_PROMPT) {
at_prompt = true;
}
Dmsg1(100, "Got poll %s\n", bnet_sig_to_ascii(UA_sock));
typedef enum
{
- ITEM_ARG, /* item with simple list like .job */
+ ITEM_ARG, /* item with simple list like .jobs */
ITEM_HELP /* use help item=xxx and detect all arguments */
} cpl_item_t;
{"pool=", ".pool" },
{"fileset=", ".fileset" },
{"client=", ".client" },
- {"job=", ".job" },
+ {"job=", ".jobs" },
+ {"restore_job=",".jobs type=R" },
{"level=", ".level" },
{"storage=", ".storage" },
{"schedule=", ".schedule" },
{"mark", ".ls" },
{"m", ".ls" },
{"unmark", ".lsmark" },
+ {"catalog=", ".catalogs" },
{"actiononpurge=", ".actiononpurge" }
};
#define key_size ((int)(sizeof(cpl_keywords)/sizeof(struct cpl_keywords_t)))
return 1;
}
+/*
+ * Return 1 if OK
+ * 0 if no input
+ * -1 error (must stop)
+ */
int
get_cmd(FILE *input, const char *prompt, BSOCK *sock, int sec)
{
*/
line = readline((char *)prompt); /* cast needed for old readlines */
if (!line) {
- exit(1);
+ return -1; /* error return and exit */
}
strip_trailing_junk(line);
command = line;
sendit(_("Command logic problem\n"));
sock->msglen = 0;
sock->msg[0] = 0;
- return 0;
+ return 0; /* No input */
}
/*
actuallyfree(line); /* allocated by readline() malloc */
line = NULL;
}
- return 1;
+ return 1; /* OK */
}
#else /* no readline, do it ourselves */
int ret=0;
#ifdef HAVE_READLINE
-/* first, try to truncate the history file, and if it
- * fail, the file is probably not present, and we
+/*
+ * first, try to truncate the history file, and if it
+ * fails, the file is probably not present, and we
* can use write_history to create it
*/
} else {
ret = write_history(histfile);
}
-
#endif
return ret;
int ret=0;
#ifdef HAVE_READLINE
-
using_history();
ret = read_history(histfile);
/* Tell the completer that we want a complete . */
rl_completion_entry_function = dummy_completion_function;
rl_attempted_completion_function = readline_completion;
rl_filename_completion_desired = 0;
+ stifle_history(100);
#endif
return ret;
}
+bool select_director(const char *director, DIRRES **ret_dir, CONRES **ret_cons)
+{
+ int numcon=0, numdir=0;
+ int i=0, item=0;
+ BSOCK *UA_sock;
+ DIRRES *dir=NULL;
+ CONRES *cons=NULL;
+ struct sockaddr client_addr;
+ memset(&client_addr, 0, sizeof(client_addr));
+
+ *ret_cons = NULL;
+ *ret_dir = NULL;
+
+ LockRes();
+ numdir = 0;
+ foreach_res(dir, R_DIRECTOR) {
+ numdir++;
+ }
+ numcon = 0;
+ foreach_res(cons, R_CONSOLE) {
+ numcon++;
+ }
+ UnlockRes();
+
+ if (numdir == 1) { /* No choose */
+ dir = (DIRRES *)GetNextRes(R_DIRECTOR, NULL);
+ }
+
+ if (director) { /* Command line choice overwrite the no choose option */
+ LockRes();
+ foreach_res(dir, R_DIRECTOR) {
+ if (bstrcmp(dir->hdr.name, director)) {
+ break;
+ }
+ }
+ UnlockRes();
+ if (!dir) { /* Can't find Director used as argument */
+ senditf(_("Can't find %s in Director list\n"), director);
+ return 0;
+ }
+ }
+
+ if (!dir) { /* prompt for director */
+ UA_sock = init_bsock(NULL, 0, "", "", 0, &client_addr);
+try_again:
+ sendit(_("Available Directors:\n"));
+ LockRes();
+ numdir = 0;
+ foreach_res(dir, R_DIRECTOR) {
+ senditf( _("%2d: %s at %s:%d\n"), 1+numdir++, dir->hdr.name,
+ dir->address, dir->DIRport);
+ }
+ UnlockRes();
+ if (get_cmd(stdin, _("Select Director by entering a number: "),
+ UA_sock, 600) < 0)
+ {
+ (void)WSACleanup(); /* Cleanup Windows sockets */
+ return 0;
+ }
+ if (!is_a_number(UA_sock->msg)) {
+ senditf(_("%s is not a number. You must enter a number between "
+ "1 and %d\n"),
+ UA_sock->msg, numdir);
+ goto try_again;
+ }
+ item = atoi(UA_sock->msg);
+ if (item < 0 || item > numdir) {
+ senditf(_("You must enter a number between 1 and %d\n"), numdir);
+ goto try_again;
+ }
+ term_bsock(UA_sock);
+ LockRes();
+ for (i=0; i<item; i++) {
+ dir = (DIRRES *)GetNextRes(R_DIRECTOR, (RES *)dir);
+ }
+ UnlockRes();
+ }
+ LockRes();
+ /* Look for a console linked to this director */
+ for (i=0; i<numcon; i++) {
+ cons = (CONRES *)GetNextRes(R_CONSOLE, (RES *)cons);
+ if (cons->director && strcmp(cons->director, dir->hdr.name) == 0) {
+ break;
+ }
+ cons = NULL;
+ }
+ /* Look for the first non-linked console */
+ if (cons == NULL) {
+ for (i=0; i<numcon; i++) {
+ cons = (CONRES *)GetNextRes(R_CONSOLE, (RES *)cons);
+ if (cons->director == NULL)
+ break;
+ cons = NULL;
+ }
+ }
+
+ /* If no console, take first one */
+ if (!cons) {
+ cons = (CONRES *)GetNextRes(R_CONSOLE, (RES *)NULL);
+ }
+ UnlockRes();
+
+ *ret_dir = dir;
+ *ret_cons = cons;
+
+ return 1;
+}
+
/*********************************************************************
*
* Main Bacula Console -- User Interface Program
*/
int main(int argc, char *argv[])
{
- int ch, i, item;
+ int ch;
+ char *director=NULL;
+ bool list_directors=false;
bool no_signals = false;
bool test_config = false;
JCR jcr;
working_directory = "/tmp";
args = get_pool_memory(PM_FNAME);
- while ((ch = getopt(argc, argv, "bc:d:nstu:?")) != -1) {
+ while ((ch = getopt(argc, argv, "D:lbc:d:nstu:?")) != -1) {
switch (ch) {
+ case 'D': /* Director */
+ if (director) {
+ free(director);
+ }
+ director = bstrdup(optarg);
+ break;
+
+ case 'l':
+ list_directors = true;
+ test_config = true;
+ break;
+
case 'c': /* configuration file */
if (configfile != NULL) {
free(configfile);
con_init(stdin);
}
+ if (list_directors) {
+ LockRes();
+ foreach_res(dir, R_DIRECTOR) {
+ senditf("%s\n", dir->hdr.name);
+ }
+ UnlockRes();
+ }
+
if (test_config) {
terminate_console(0);
exit(0);
start_watchdog(); /* Start socket watchdog */
- LockRes();
- numdir = 0;
- foreach_res(dir, R_DIRECTOR) {
- numdir++;
- }
- numcon = 0;
- foreach_res(cons, R_CONSOLE) {
- numcon++;
- }
- UnlockRes();
-
- if (numdir > 1) {
- struct sockaddr client_addr;
- memset(&client_addr, 0, sizeof(client_addr));
- UA_sock = init_bsock(NULL, 0, "", "", 0, &client_addr);
-try_again:
- sendit(_("Available Directors:\n"));
- LockRes();
- numdir = 0;
- foreach_res(dir, R_DIRECTOR) {
- senditf( _("%2d: %s at %s:%d\n"), 1+numdir++, dir->hdr.name, dir->address,
- dir->DIRport);
- }
- UnlockRes();
- if (get_cmd(stdin, _("Select Director by entering a number: "), UA_sock, 600) < 0) {
- (void)WSACleanup(); /* Cleanup Windows sockets */
- return 1;
- }
- if (!is_a_number(UA_sock->msg)) {
- senditf(_("%s is not a number. You must enter a number between 1 and %d\n"),
- UA_sock->msg, numdir);
- goto try_again;
- }
- item = atoi(UA_sock->msg);
- if (item < 0 || item > numdir) {
- senditf(_("You must enter a number between 1 and %d\n"), numdir);
- goto try_again;
- }
- term_bsock(UA_sock);
- LockRes();
- for (i=0; i<item; i++) {
- dir = (DIRRES *)GetNextRes(R_DIRECTOR, (RES *)dir);
- }
- /* Look for a console linked to this director */
- for (i=0; i<numcon; i++) {
- cons = (CONRES *)GetNextRes(R_CONSOLE, (RES *)cons);
- if (cons->director && strcmp(cons->director, dir->hdr.name) == 0) {
- break;
- }
- cons = NULL;
- }
- /* Look for the first non-linked console */
- if (cons == NULL) {
- for (i=0; i<numcon; i++) {
- cons = (CONRES *)GetNextRes(R_CONSOLE, (RES *)cons);
- if (cons->director == NULL)
- break;
- cons = NULL;
- }
- }
- UnlockRes();
- }
- /* If no director, take first one */
- if (!dir) {
- LockRes();
- dir = (DIRRES *)GetNextRes(R_DIRECTOR, NULL);
- UnlockRes();
- }
- /* If no console, take first one */
- if (!cons) {
- LockRes();
- cons = (CONRES *)GetNextRes(R_CONSOLE, (RES *)NULL);
- UnlockRes();
+ if(!select_director(director, &dir, &cons)) {
+ return 1;
}
senditf(_("Connecting to Director %s:%d\n"), dir->address,dir->DIRport);
return OK;
}
+/* @version */
static int versioncmd(FILE *input, BSOCK *UA_sock)
{
senditf("Version: " VERSION " (" BDATE ") %s %s %s\n",
return 1;
}
+/* @input <input-filename> */
static int inputcmd(FILE *input, BSOCK *UA_sock)
{
FILE *fd;
return 1;
}
-/* Send output to both termina and specified file */
+/* @tee <output-filename> */
+/* Send output to both terminal and specified file */
static int teecmd(FILE *input, BSOCK *UA_sock)
{
teeout = true;
return do_outputcmd(input, UA_sock);
}
+/* @output <output-filename> */
/* Send output to specified "file" */
static int outputcmd(FILE *input, BSOCK *UA_sock)
{
}
/*
- * exec "some-command" [wait-seconds]
+ * @exec "some-command" [wait-seconds]
*/
static int execcmd(FILE *input, BSOCK *UA_sock)
{
}
+/* @echo xxx yyy */
static int echocmd(FILE *input, BSOCK *UA_sock)
{
for (int i=1; i < argc; i++) {
return 1;
}
+/* @quit */
static int quitcmd(FILE *input, BSOCK *UA_sock)
{
return 0;
}
+/* @help */
static int helpcmd(FILE *input, BSOCK *UA_sock)
{
int i;
}
+/* @sleep secs */
static int sleepcmd(FILE *input, BSOCK *UA_sock)
{
if (argc > 1) {
return 1;
}
-
+/* @time */
static int timecmd(FILE *input, BSOCK *UA_sock)
{
char sdt[50];