X-Git-Url: http://git.annexia.org/?a=blobdiff_plain;f=fish%2Ffish.c;h=1bc84471c157320237086c51ca0ef129fa411f91;hb=0cd5b9ac15c62eb570ec74d19b4aebde4990fd82;hp=4e45ceaf9999b28ec31d669552004c83136f00ce;hpb=617e7f6bafa7de2303c08e1715004aae3141c389;p=libguestfs.git diff --git a/fish/fish.c b/fish/fish.c index 4e45cea..1bc8447 100644 --- a/fish/fish.c +++ b/fish/fish.c @@ -53,6 +53,7 @@ struct parsed_command { char *argv[64]; }; +static void user_cancel (int); static void set_up_terminal (void); static void prepare_drives (struct drv *drv); static int launch (void); @@ -63,6 +64,7 @@ static void cmdline (char *argv[], int optind, int argc); 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 @@ -72,7 +74,7 @@ static void add_history_line (const char *); static int override_progress_bars = -1; /* Currently open libguestfs handle. */ -guestfs_h *g; +guestfs_h *g = NULL; int read_only = 0; int live = 0; @@ -89,6 +91,9 @@ 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) @@ -385,6 +390,24 @@ main (int argc, char *argv[]) } } + /* 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. @@ -483,11 +506,23 @@ main (int argc, char *argv[]) } } + /* 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) guestfs_set_event_callback (g, progress_callback, @@ -495,7 +530,7 @@ main (int argc, char *argv[]) /* Interactive, shell script, or command(s) on the command line? */ if (optind >= argc) { - if (isatty (0)) + if (is_interactive) interactive (); else shell_script (); @@ -508,6 +543,13 @@ main (int argc, char *argv[]) exit (EXIT_SUCCESS); } +static void +user_cancel (int sig) +{ + if (g) + guestfs_user_cancel (g); +} + /* The header file which defines this has "issues". */ extern int tgetent (char *, const char *); @@ -641,6 +683,8 @@ script (int prompt) break; } + input_lineno++; + pcmd = parse_command_line (buf, &exit_on_error); if (pcmd.status == -1 && exit_on_error) exit (EXIT_FAILURE); @@ -951,7 +995,7 @@ execute_and_inline (const char *cmd, int global_exit_on_error) free (line); - if (pclose (pp) == -1) { + if (pclose (pp) != 0) { perror ("pclose"); return -1; } @@ -1165,6 +1209,14 @@ extended_help_message (void) "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) { @@ -1318,9 +1370,8 @@ parse_string_list (const char *str) /* 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; }