#include "fish.h"
#include "options.h"
+#include "progress.h"
#include "c-ctype.h"
#include "closeout.h"
char *argv[64];
};
-static void set_up_terminal (void);
+static void user_cancel (int);
static void prepare_drives (struct drv *drv);
static int launch (void);
static void interactive (void);
static struct parsed_command parse_command_line (char *buf, int *exit_on_error_rtn);
static int parse_quoted_string (char *p);
static int execute_and_inline (const char *cmd, int exit_on_error);
+static void error_cb (guestfs_h *g, void *data, const char *msg);
static void initialize_readline (void);
static void cleanup_readline (void);
#ifdef HAVE_LIBREADLINE
#endif
static int override_progress_bars = -1;
+static struct progress_bar *bar = NULL;
/* Currently open libguestfs handle. */
-guestfs_h *g;
+guestfs_h *g = NULL;
int read_only = 0;
int live = 0;
int echo_keys = 0;
const char *libvirt_uri = NULL;
int inspector = 0;
-int utf8_mode = 0;
-int have_terminfo = 0;
int progress_bars = 0;
+int is_interactive = 0;
+const char *input_file = NULL;
+int input_lineno = 0;
static void __attribute__((noreturn))
usage (int status)
parse_config ();
- set_up_terminal ();
-
enum { HELP_OPTION = CHAR_MAX + 1 };
static const char *options = "a:c:d:Df:h::im:nN:rv?Vwx";
}
}
+ /* Decide here if this will be an interactive session. We have to
+ * do this as soon as possible after processing the command line
+ * args.
+ */
+ is_interactive = !file && isatty (0);
+
+ /* Register a ^C handler. We have to do this before launch could
+ * possibly be called below.
+ */
+ if (is_interactive) {
+ memset (&sa, 0, sizeof sa);
+ sa.sa_handler = user_cancel;
+ sa.sa_flags = SA_RESTART;
+ sigaction (SIGINT, &sa, NULL);
+
+ guestfs_set_pgroup (g, 1);
+ }
+
/* Old-style -i syntax? Since -a/-d/-N and -i was disallowed
* previously, if we have -i without any drives but with something
* on the command line, it must be old-style syntax.
}
}
+ /* Get the name of the input file, for error messages, and replace
+ * the default error handler.
+ */
+ if (!is_interactive) {
+ if (file)
+ input_file = file;
+ else
+ input_file = "*stdin*";
+ guestfs_set_error_handler (g, error_cb, NULL);
+ }
+ input_lineno = 0;
+
/* Decide if we display progress bars. */
progress_bars =
override_progress_bars >= 0
? override_progress_bars
- : (optind >= argc && isatty (0));
+ : (optind >= argc && is_interactive);
+
+ if (progress_bars) {
+ bar = progress_bar_init (0);
+ if (!bar) {
+ perror ("progress_bar_init");
+ exit (EXIT_FAILURE);
+ }
- if (progress_bars)
guestfs_set_event_callback (g, progress_callback,
GUESTFS_EVENT_PROGRESS, 0, NULL);
+ }
/* Interactive, shell script, or command(s) on the command line? */
if (optind >= argc) {
- if (isatty (0))
+ if (is_interactive)
interactive ();
else
shell_script ();
cleanup_readline ();
+ if (progress_bars)
+ progress_bar_free (bar);
+
exit (EXIT_SUCCESS);
}
-/* The <term.h> header file which defines this has "issues". */
-extern int tgetent (char *, const char *);
-
static void
-set_up_terminal (void)
+user_cancel (int sig)
{
- /* http://www.cl.cam.ac.uk/~mgk25/unicode.html#activate */
- utf8_mode = STREQ (nl_langinfo (CODESET), "UTF-8");
-
- char *term = getenv ("TERM");
- if (term == NULL) {
- //fprintf (stderr, _("guestfish: TERM (terminal type) not defined.\n"));
- return;
- }
-
- int r = tgetent (NULL, term);
- if (r == -1) {
- fprintf (stderr, _("guestfish: could not access termcap or terminfo database.\n"));
- return;
- }
- if (r == 0) {
- fprintf (stderr, _("guestfish: terminal type \"%s\" not defined.\n"),
- term);
- return;
- }
-
- have_terminfo = 1;
+ if (g)
+ guestfs_user_cancel (g);
}
static void
break;
}
+ input_lineno++;
+
pcmd = parse_command_line (buf, &exit_on_error);
if (pcmd.status == -1 && exit_on_error)
exit (EXIT_FAILURE);
free (line);
- if (pclose (pp) == -1) {
+ if (pclose (pp) != 0) {
perror ("pclose");
return -1;
}
int pid = 0;
int r;
- reset_progress_bar ();
+ if (progress_bars)
+ progress_bar_reset (bar);
/* This counts the commands issued, starting at 1. */
command_num++;
"For complete documentation: man guestfish\n"));
}
+/* Error callback. This replaces the standard libguestfs error handler. */
+static void
+error_cb (guestfs_h *g, void *data, const char *msg)
+{
+ fprintf (stderr, _("%s:%d: libguestfs: error: %s\n"),
+ input_file, input_lineno, msg);
+}
+
void
free_strings (char **argv)
{
/* We've reached the end of a token. We shouldn't still be in quotes. */
if (in_quote) {
fprintf (stderr, _("Runaway quote in string \"%s\"\n"), str);
-
free_n_strings (argv, argv_len);
-
+ free (tok);
return NULL;
}
}
return ret;
}
+
+/* Callback which displays a progress bar. */
+void
+progress_callback (guestfs_h *g, void *data,
+ uint64_t event, int event_handle, int flags,
+ const char *buf, size_t buf_len,
+ const uint64_t *array, size_t array_len)
+{
+ if (array_len < 4)
+ return;
+
+ /*uint64_t proc_nr = array[0];*/
+ /*uint64_t serial = array[1];*/
+ uint64_t position = array[2];
+ uint64_t total = array[3];
+
+ progress_bar_set (bar, position, total);
+}