]> git.sur5r.net Git - bacula/bacula/blobdiff - bacula/src/console/console.c
ifdef off conio.c if not configured -- should not be necessary
[bacula/bacula] / bacula / src / console / console.c
index 24c6e7a8aa4da6b683451b1a1a00e5cb1f3da43b..586594b4b29e5bd7047c7bcc49594de0d136b687 100644 (file)
  *   Bacula Console interface to the Director
  *
  *     Kern Sibbald, September MM
+ *
+ *     Version $Id$
  */
 
 /*
-   Copyright (C) 2000, 2001 Kern Sibbald and John Walker
+   Copyright (C) 2000-2003 Kern Sibbald and John Walker
 
-   This program is free software; you can redistribute it and/or
-   modify it under the terms of the GNU General Public License
-   as published by the Free Software Foundation; either version 2
-   of the License, or (at your option) any later version.
+   This library is free software; you can redistribute it and/or
+   modify it under the terms of the GNU Lesser General Public
+   License as published by the Free Software Foundation; either
+   version 2.1 of the License, or (at your option) any later version.
 
-   This program is distributed in the hope that it will be useful,
+   This library is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
-   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-   GNU General Public License for more details.
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   Lesser General Public License for more details.
+
+   You should have received a copy of the GNU Lesser General Public
+   License along with this library; if not, write to the Free
+   Software Foundation, Inc., 59 Temple Place, Suite 330, Boston,
+   MA 02111-1307, USA.
 
-   You should have received a copy of the GNU General Public License
-   along with this program; if not, write to the Free Software
-   Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
  */
 
-#include <termios.h>
 #include "bacula.h"
 #include "console_conf.h"
 #include "jcr.h"
-
 /* Imported functions */
 int authenticate_director(JCR *jcr, DIRRES *director);
 
        
 /* Exported variables */
 
-
 #ifdef HAVE_CYGWIN
 int rl_catch_signals;
 #else
 extern int rl_catch_signals;
 #endif
 
+#ifdef HAVE_CONIO
+extern int input_line(char *line, int len);
+extern void con_init(FILE *input);
+extern void con_term();
+extern void con_set_zed_keys();
+extern void t_sendl(char *buf, int len);
+extern void t_send(char *buf);
+extern void t_char(char c);
+#else
+#define con_init(x) 
+#define con_term()
+#define con_set_zed_keys();
+#endif
+
 /* Forward referenced functions */
 static void terminate_console(int sig);
 int get_cmd(FILE *input, char *prompt, BSOCK *sock, int sec);
+static int do_outputcmd(FILE *input, BSOCK *UA_sock);
+void senditf(char *fmt, ...);
+void sendit(char *buf);
 
 /* Static variables */
 static char *configfile = NULL;
 static BSOCK *UA_sock = NULL;
 static DIRRES *dir; 
 static FILE *output = stdout;
+int tee = 0;                         /* output to output and stdout */
+static int stop = FALSE;
+static int argc;
+static POOLMEM *args;
+static char *argk[MAX_CMD_ARGS];
+static char *argv[MAX_CMD_ARGS];
+
+/* Command prototypes */
+static int versioncmd(FILE *input, BSOCK *UA_sock);
+static int inputcmd(FILE *input, BSOCK *UA_sock);
+static int outputcmd(FILE *input, BSOCK *UA_sock);
+static int teecmd(FILE *input, BSOCK *UA_sock);
+static int quitcmd(FILE *input, BSOCK *UA_sock);
+static int timecmd(FILE *input, BSOCK *UA_sock);
+static int sleepcmd(FILE *input, BSOCK *UA_sock);
 
 
-#define CONFIG_FILE "./console.conf"   /* default configuration file */
+#define CONFIG_FILE "./bconsole.conf"   /* default configuration file */
 
 static void usage()
 {
-   fprintf(stderr,
-"\nVersion: " VERSION " (" DATE ")\n\n"
-"Usage: console [-s] [-c config_file] [-d debug_level] [config_file]\n"
+   fprintf(stderr, _(
+"\nVersion: " VERSION " (" BDATE ") %s %s %s\n\n"
+"Usage: bconsole [-s] [-c config_file] [-d debug_level] [config_file]\n"
 "       -c <file>   set configuration file to file\n"
 "       -dnn        set debug level to nn\n"
 "       -s          no signals\n"
 "       -t          test - read configuration and exit\n"
 "       -?          print this message.\n"  
-"\n");
+"\n"), HOST_OS, DISTNAME, DISTVER);
 
    exit(1);
 }
 
+
+void got_stop(int sig)
+{
+   stop = TRUE;
+}
+
+void got_continue(int sig)
+{
+   stop = FALSE;
+}
+
+void got_tout(int sig) 
+{
+// printf("Got tout\n");
+}
+
+void got_tin(int sig)
+{   
+// printf("Got tin\n");
+}
+
+static int zed_keyscmd(FILE *input, BSOCK *UA_sock)
+{
+   con_set_zed_keys();
+   return 1;
+}
+
+/*
+ * These are the @command
+ */
+struct cmdstruct { char *key; int (*func)(FILE *input, BSOCK *UA_sock); char *help; }; 
+static struct cmdstruct commands[] = {
+ { N_("input"),      inputcmd,     _("input from file")},
+ { N_("output"),     outputcmd,    _("output to file")},
+ { N_("quit"),       quitcmd,      _("quit")},
+ { N_("tee"),        teecmd,       _("output to file and terminal")},
+ { N_("sleep"),      sleepcmd,     _("sleep specified time")},
+ { N_("time"),       timecmd,      _("print current time")},
+ { N_("version"),    versioncmd,   _("print Console's version")},
+ { N_("exit"),       quitcmd,      _("exit = quit")},
+ { N_("zed_keyst"),  zed_keyscmd,  _("zed_keys = use zed keys instead of bash keys")},
+            };
+#define comsize (sizeof(commands)/sizeof(struct cmdstruct))
+
+static int do_a_command(FILE *input, BSOCK *UA_sock)
+{
+   unsigned int i;
+   int stat;
+   int found;
+   int len;
+   char *cmd;
+
+   found = 0;
+   stat = 1;
+
+   Dmsg1(120, "Command: %s\n", UA_sock->msg);
+   if (argc == 0) {
+      return 1;
+   }
+
+   cmd = argk[0]+1;
+   if (*cmd == '#') {                 /* comment */
+      return 1;
+   }
+   len = strlen(cmd);
+   for (i=0; i<comsize; i++) {    /* search for command */
+      if (strncasecmp(cmd,  _(commands[i].key), len) == 0) {
+        stat = (*commands[i].func)(input, UA_sock);   /* go execute command */
+        found = 1;
+        break;
+      }
+   }
+   if (!found) {
+      pm_strcat(&UA_sock->msg, _(": is an illegal command\n"));
+      UA_sock->msglen = strlen(UA_sock->msg);
+      sendit(UA_sock->msg);
+   }
+   return stat;
+}
+
+
 static void read_and_process_input(FILE *input, BSOCK *UA_sock) 
 {
    char *prompt = "*";
@@ -90,35 +205,54 @@ static void read_and_process_input(FILE *input, BSOCK *UA_sock)
         if (fgets(UA_sock->msg, len, input) == NULL) {
            stat = -1;
         } else {
+           sendit(UA_sock->msg);  /* echo to terminal */
            strip_trailing_junk(UA_sock->msg);
            UA_sock->msglen = strlen(UA_sock->msg);
            stat = 1;
         }
       }
-      if (stat < 0) { 
+      if (stat < 0) {
         break;                       /* error */
       } else if (stat == 0) {        /* timeout */
-         bnet_fsend(UA_sock, ".messages");
+         if (strcmp(prompt, "*") == 0) {
+            bnet_fsend(UA_sock, ".messages");
+        } else {
+           continue;
+        }
       } else {
         at_prompt = FALSE;
+        /* @ => internal command for us */
+         if (UA_sock->msg[0] == '@') {
+           parse_args(UA_sock->msg, &args, &argc, argk, argv, MAX_CMD_ARGS);
+           if (!do_a_command(input, UA_sock)) {
+              break;
+           }
+           continue;
+        }
         if (!bnet_send(UA_sock)) {   /* send command */
            break;                    /* error */
         }
       }
-      if (strcmp(UA_sock->msg, "quit") == 0 || strcmp(UA_sock->msg, "exit") == 0) {
+      if (strcmp(UA_sock->msg, ".quit") == 0 || strcmp(UA_sock->msg, ".exit") == 0) {
         break;
       }
-      while ((stat = bnet_recv(UA_sock)) > 0) {
+      while ((stat = bnet_recv(UA_sock)) >= 0) {
         if (at_prompt) {
-            fprintf(output, "\n");
+           if (!stop) {
+               sendit("\n");
+           }
            at_prompt = FALSE;
         }
-         printf("%s", UA_sock->msg);
+        if (!stop) {
+           sendit(UA_sock->msg);
+        }
       }
-      fflush(output);
-      if (stat < 0) {
-        break;                       /* error */
-      } else if (stat == 0) {
+      if (!stop) {
+        fflush(stdout);
+      }
+      if (is_bnet_stop(UA_sock)) {
+        break;                       /* error or term */
+      } else if (stat == BNET_SIGNAL) {
         if (UA_sock->msglen == BNET_PROMPT) {
            at_prompt = TRUE;
         }
@@ -141,42 +275,39 @@ int main(int argc, char *argv[])
    JCR jcr;
 
    init_stack_dump();
-   my_name_is(argc, argv, "console");
+   my_name_is(argc, argv, "bconsole");
+   textdomain("bacula-console");
+   init_msg(NULL, NULL);
    working_directory = "/tmp";
-
-   /*
-    * Ensure that every message is always printed
-    */
-   for (i=1; i<=M_MAX; i++) {
-      add_msg_dest(NULL, MD_STDOUT, i, NULL, NULL);
-   }
-
+   args = get_pool_memory(PM_FNAME);
 
    while ((ch = getopt(argc, argv, "bc:d:r:st?")) != -1) {
       switch (ch) {
-         case 'c':                    /* configuration file */
-           if (configfile != NULL)
-              free(configfile);
-           configfile = bstrdup(optarg);
-           break;
-
-         case 'd':
-           debug_level = atoi(optarg);
-           if (debug_level <= 0)
-              debug_level = 1;
-           break;
-
-         case 's':                    /* turn off signals */
-           no_signals = TRUE;
-           break;
-
-         case 't':
-           test_config = TRUE;
-           break;
-
-         case '?':
-        default:
-           usage();
+      case 'c':                    /* configuration file */
+        if (configfile != NULL) {
+           free(configfile);
+        }
+        configfile = bstrdup(optarg);
+        break;
+
+      case 'd':
+        debug_level = atoi(optarg);
+        if (debug_level <= 0) {
+           debug_level = 1;
+        }
+        break;
+
+      case 's':                    /* turn off signals */
+        no_signals = TRUE;
+        break;
+
+      case 't':
+        test_config = TRUE;
+        break;
+
+      case '?':
+      default:
+        usage();
 
       }  
    }
@@ -187,6 +318,10 @@ int main(int argc, char *argv[])
       init_signals(terminate_console);
    }
    signal(SIGCHLD, SIG_IGN);
+   signal(SIGTSTP, got_stop);
+   signal(SIGCONT, got_continue);
+   signal(SIGTTIN, got_tin);
+   signal(SIGTTOU, got_tout);
 
    if (argc) {
       usage();
@@ -205,8 +340,8 @@ int main(int argc, char *argv[])
    }
    UnlockRes();
    if (ndir == 0) {
-      Emsg1(M_ABORT, 0, "No Director resource defined in %s\n\
-Without that I don't how to speak to the Director :-(\n", configfile);
+      Emsg1(M_ERROR_TERM, 0, _("No Director resource defined in %s\n\
+Without that I don't how to speak to the Director :-(\n"), configfile);
    }
 
    if (test_config) {
@@ -216,23 +351,25 @@ Without that I don't how to speak to the Director :-(\n", configfile);
 
    memset(&jcr, 0, sizeof(jcr));
 
+   con_init(stdin);
+
    if (ndir > 1) {
-      UA_sock = init_bsock(0, "", "", 0);
+      UA_sock = init_bsock(NULL, 0, "", "", 0);
 try_again:
-      fprintf(output, "Available Directors:\n");
+      sendit(_("Available Directors:\n"));
       LockRes();
       ndir = 0;
       for (dir = NULL; (dir = (DIRRES *)GetNextRes(R_DIRECTOR, (RES *)dir)); ) {
-         fprintf(output, "%d  %s at %s:%d\n", 1+ndir++, dir->hdr.name, dir->address,
+         senditf( _("%d  %s at %s:%d\n"), 1+ndir++, dir->hdr.name, dir->address,
            dir->DIRport);
       }
       UnlockRes();
-      if (get_cmd(stdin, "Select Director: ", UA_sock, 600) < 0) {
+      if (get_cmd(stdin, _("Select Director: "), UA_sock, 600) < 0) {
         return 1;
       }
       item = atoi(UA_sock->msg);
       if (item < 0 || item > ndir) {
-         fprintf(output, "You must enter a number between 1 and %d\n", ndir);
+         senditf(_("You must enter a number between 1 and %d\n"), ndir);
         goto try_again;
       }
       LockRes();
@@ -249,8 +386,8 @@ try_again:
    }
       
 
-   Dmsg2(-1, "Connecting to Director %s:%d\n", dir->address,dir->DIRport);
-   UA_sock = bnet_connect(&jcr, 5, 15, "Director daemon", dir->address, 
+   senditf(_("Connecting to Director %s:%d\n"), dir->address,dir->DIRport);
+   UA_sock = bnet_connect(NULL, 5, 15, "Director daemon", dir->address, 
                          NULL, dir->DIRport, 0);
    if (UA_sock == NULL) {
       terminate_console(0);
@@ -258,13 +395,28 @@ try_again:
    }
    jcr.dir_bsock = UA_sock;
    if (!authenticate_director(&jcr, dir)) {
-      fprintf(stderr, "ERR%s", UA_sock->msg);
+      fprintf(stderr, "ERR=%s", UA_sock->msg);
       terminate_console(0);
       return 1;
    }
 
    Dmsg0(40, "Opened connection with Director daemon\n");
 
+   sendit(_("Enter a period to cancel a command.\n"));
+
+   char *env = getenv("HOME");
+   if (env) {
+      FILE *fd;
+      pm_strcpy(&UA_sock->msg, env);
+      pm_strcat(&UA_sock->msg, "/.bconsolerc");
+      fd = fopen(UA_sock->msg, "r");
+      if (fd) {
+        read_and_process_input(fd, UA_sock);
+        fclose(fd);
+      }
+   }
+
+
    read_and_process_input(stdin, UA_sock);
 
    if (UA_sock) {
@@ -282,16 +434,23 @@ static void terminate_console(int sig)
 {
    static int already_here = FALSE;
 
-   if (already_here)                 /* avoid recursive temination problems */
+   if (already_here) {               /* avoid recursive temination problems */
       exit(1);
+   }
    already_here = TRUE;
-   exit(0);
+   free_pool_memory(args);
+   con_term();
+   if (sig != 0) {
+      exit(1);
+   }
+   return;
 }
 
 #ifdef HAVE_READLINE
+#define READLINE_LIBRARY 1
 #undef free
-#include "readline/readline.h"
-#include "readline/history.h"
+#include "readline.h"
+#include "history.h"
 
 
 int 
@@ -299,15 +458,14 @@ get_cmd(FILE *input, char *prompt, BSOCK *sock, int sec)
 {
    char *line;
 
-   rl_catch_signals = 1;
+   rl_catch_signals = 0;             /* do it ourselves */
    line = readline(prompt);
 
    if (!line) {
       exit(1);
    }
-   strcpy(sock->msg, line);
-   strip_trailing_junk(sock->msg);
-   sock->msglen = strlen(sock->msg);
+   strip_trailing_junk(line);
+   sock->msglen = pm_strcpy(&sock->msg, line);
    if (sock->msglen) {
       add_history(sock->msg);
    }
@@ -328,21 +486,21 @@ wait_for_data(int fd, int sec)
    fd_set fdset;
    struct timeval tv;
 
-   FD_ZERO(&fdset);
-   FD_SET(fd, &fdset);
    tv.tv_sec = sec;
    tv.tv_usec = 0;
    for ( ;; ) {
+      FD_ZERO(&fdset);
+      FD_SET(fd, &fdset);
       switch(select(fd + 1, &fdset, NULL, NULL, &tv)) {
-        case 0:                         /* timeout */
-           return 0;
-        case -1:
-           if (errno == EINTR || errno == EAGAIN) {
-              continue;
-           }
-           return -1;                  /* error return */
-        default:
-           return 1;
+      case 0:                        /* timeout */
+        return 0;
+      case -1:
+        if (errno == EINTR || errno == EAGAIN) {
+           continue;
+        }
+        return -1;                  /* error return */
+      default:
+        return 1;
       }
    }
 }
@@ -358,19 +516,33 @@ int
 get_cmd(FILE *input, char *prompt, BSOCK *sock, int sec)
 {
    int len;  
-   fprintf(output, prompt);
-   fflush(output);
+   if (!stop) {
+      if (output == stdout || tee) {
+        sendit(prompt);
+      }
+   }
+again:
    switch (wait_for_data(fileno(input), sec)) {
-      case 0:
-        return 0;                    /* timeout */
-      case -1: 
-        return -1;                   /* error */
-      default:
-        len = sizeof_pool_memory(sock->msg) - 1;
-        if (fgets(sock->msg, len, input) == NULL) {
-           return -1;
-        }
+   case 0:
+      return 0;                   /* timeout */
+   case -1: 
+      return -1;                  /* error */
+   default:
+      len = sizeof_pool_memory(sock->msg) - 1;
+      if (stop) {
+        sleep(1);
+        goto again;
+      }
+#ifdef HAVE_CONIO
+      if (isatty(fileno(input))) {
+        input_line(sock->msg, len);
         break;
+      }
+#endif
+      if (fgets(sock->msg, len, input) == NULL) {
+        return -1;
+      }
+      break;
    }
    strip_trailing_junk(sock->msg);
    sock->msglen = strlen(sock->msg);
@@ -378,3 +550,148 @@ get_cmd(FILE *input, char *prompt, BSOCK *sock, int sec)
 }
 
 #endif
+
+static int versioncmd(FILE *input, BSOCK *UA_sock)
+{
+   senditf("Version: " VERSION " (" BDATE ") %s %s %s\n",
+      HOST_OS, DISTNAME, DISTVER);
+   return 1;
+}
+
+static int inputcmd(FILE *input, BSOCK *UA_sock)
+{
+   FILE *fd;
+
+   if (argc > 2) {
+      sendit(_("Too many arguments on input command.\n"));
+      return 1;
+   }
+   if (argc == 1) {
+      sendit(_("First argument to input command must be a filename.\n"));
+      return 1;
+   }
+   fd = fopen(argk[1], "r");
+   if (!fd) {
+      senditf(_("Cannot open file %s for input. ERR=%s\n"), 
+        argk[1], strerror(errno));
+      return 1; 
+   }
+   read_and_process_input(fd, UA_sock);
+   fclose(fd);
+   return 1;
+}
+
+static int teecmd(FILE *input, BSOCK *UA_sock)
+{
+   tee = 1;
+   return do_outputcmd(input, UA_sock);
+}
+
+static int outputcmd(FILE *input, BSOCK *UA_sock)
+{
+   tee = 0;
+   return do_outputcmd(input, UA_sock);
+}
+
+
+static int do_outputcmd(FILE *input, BSOCK *UA_sock)
+{
+   FILE *fd;
+   char *mode = "a+";
+
+   if (argc > 3) {
+      sendit(_("Too many arguments on output/tee command.\n"));
+      return 1;
+   }
+   if (argc == 1) {
+      if (output != stdout) {
+        fclose(output);
+        output = stdout;
+        tee = 0;
+      }
+      return 1;
+   }
+   if (argc == 3) {
+      mode = argk[2];
+   }
+   fd = fopen(argk[1], mode);
+   if (!fd) {
+      senditf(_("Cannot open file %s for output. ERR=%s\n"), 
+        argk[1], strerror(errno));
+      return 1; 
+   }
+   output = fd;
+   return 1;
+}
+
+static int quitcmd(FILE *input, BSOCK *UA_sock)
+{
+   return 0;
+}
+
+static int sleepcmd(FILE *input, BSOCK *UA_sock)
+{
+   if (argc > 1) {
+      sleep(atoi(argk[1]));
+   }
+   return 1;
+}
+
+
+static int timecmd(FILE *input, BSOCK *UA_sock)
+{
+   char sdt[50];
+   time_t ttime = time(NULL);
+   struct tm tm;
+   localtime_r(&ttime, &tm);
+   strftime(sdt, sizeof(sdt), "%d-%b-%Y %H:%M:%S", &tm);
+   sendit("\n");
+   return 1;
+}
+
+/*
+ * Send a line to the output file and or the terminal
+ */
+void senditf(char *fmt,...)
+{
+    char buf[3000];
+    va_list arg_ptr;
+
+    va_start(arg_ptr, fmt);
+    bvsnprintf(buf, sizeof(buf), (char *)fmt, arg_ptr);
+    va_end(arg_ptr);
+    sendit(buf);
+}
+
+void sendit(char *buf)
+{
+#ifdef HAVE_CONIO
+    if (output == stdout || tee) {
+       char *p, *q;    
+       /*
+        * Here, we convert every \n into \r\n because the
+       *  terminal is in raw mode when we are using 
+       *  conio.
+       */
+       for (p=q=buf; (p=strchr(q, '\n')); ) {
+         if (p-q > 0) {
+            t_sendl(q, p-q);
+         }
+          t_sendl("\r\n", 2);
+          q = ++p;                    /* point after \n */
+       }
+       if (*q) {
+         t_send(q);
+       }
+    }
+    if (output != stdout) {
+       fputs(buf, output);
+    }
+#else
+    fputs(buf, output);
+    if (tee) {
+       fputs(buf, stdout);
+    }
+    fflush(stdout);
+#endif
+}