X-Git-Url: http://git.annexia.org/?p=libguestfs.git;a=blobdiff_plain;f=fish%2Ffish.c;h=7b45fec75d6c2ea3f34c34395de9946bb6f0acac;hp=a57472f8c6eb0c5c856f1a1739fac7750c6b1839;hb=22998927f91f123e7c35d3ebf49e5744f3d7eafe;hpb=33a2c184e12c0bdbf061a9f36c87d76c28444712 diff --git a/fish/fish.c b/fish/fish.c index a57472f..7b45fec 100644 --- a/fish/fish.c +++ b/fish/fish.c @@ -40,6 +40,7 @@ #include "fish.h" #include "options.h" +#include "progress.h" #include "c-ctype.h" #include "closeout.h" @@ -53,7 +54,7 @@ struct parsed_command { 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); @@ -61,7 +62,9 @@ static void shell_script (void); static void script (int prompt); 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 @@ -69,9 +72,10 @@ static void add_history_line (const char *); #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; @@ -85,9 +89,10 @@ int keys_from_stdin = 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) @@ -160,8 +165,6 @@ main (int argc, char *argv[]) parse_config (); - set_up_terminal (); - enum { HELP_OPTION = CHAR_MAX + 1 }; static const char *options = "a:c:d:Df:h::im:nN:rv?Vwx"; @@ -384,6 +387,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. @@ -482,19 +503,38 @@ 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) { + 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 (); @@ -504,36 +544,19 @@ main (int argc, char *argv[]) cleanup_readline (); + if (progress_bars) + progress_bar_free (bar); + + guestfs_close (g); + exit (EXIT_SUCCESS); } -/* The 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 @@ -640,6 +663,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); @@ -758,9 +783,8 @@ parse_command_line (char *buf, int *exit_on_error_rtn) */ if (*p == '"') { p++; - len = strcspn (p, "\""); - if (p[len] == '\0') { - fprintf (stderr, _("%s: unterminated double quote\n"), program_name); + len = parse_quoted_string (p); + if (len == -1) { pcmd.status = -1; return pcmd; } @@ -771,7 +795,6 @@ parse_command_line (char *buf, int *exit_on_error_rtn) pcmd.status = -1; return pcmd; } - p[len] = '\0'; pend = p[len+1] ? &p[len+2] : &p[len+1]; } else if (*p == '\'') { p++; @@ -835,6 +858,87 @@ parse_command_line (char *buf, int *exit_on_error_rtn) return pcmd; } +static int +hexdigit (char d) +{ + switch (d) { + case '0'...'9': return d - '0'; + case 'a'...'f': return d - 'a' + 10; + case 'A'...'F': return d - 'A' + 10; + default: return -1; + } +} + +/* Parse double-quoted strings, replacing backslash escape sequences + * with the true character. Since the string is returned in place, + * the escapes must make the string shorter. + */ +static int +parse_quoted_string (char *p) +{ + char *start = p; + + for (; *p && *p != '"'; p++) { + if (*p == '\\') { + int m = 1, c; + + switch (p[1]) { + case '\\': break; + case 'a': *p = '\a'; break; + case 'b': *p = '\b'; break; + case 'f': *p = '\f'; break; + case 'n': *p = '\n'; break; + case 'r': *p = '\r'; break; + case 't': *p = '\t'; break; + case 'v': *p = '\v'; break; + case '"': *p = '"'; break; + case '\'': *p = '\''; break; + case '?': *p = '?'; break; + + case '0'...'7': /* octal escape - always 3 digits */ + m = 3; + if (p[2] >= '0' && p[2] <= '7' && + p[3] >= '0' && p[3] <= '7') { + c = (p[1] - '0') * 0100 + (p[2] - '0') * 010 + (p[3] - '0'); + if (c < 1 || c > 255) + goto error; + *p = c; + } + else + goto error; + break; + + case 'x': /* hex escape - always 2 digits */ + m = 3; + if (c_isxdigit (p[2]) && c_isxdigit (p[3])) { + c = hexdigit (p[2]) * 0x10 + hexdigit (p[3]); + if (c < 1 || c > 255) + goto error; + *p = c; + } + else + goto error; + break; + + default: + error: + fprintf (stderr, _("%s: invalid escape sequence in string (starting at offset %d)\n"), + program_name, (int) (p - start)); + return -1; + } + memmove (p+1, p+1+m, strlen (p+1+m) + 1); + } + } + + if (!*p) { + fprintf (stderr, _("%s: unterminated double quote\n"), program_name); + return -1; + } + + *p = '\0'; + return p - start; +} + /* Used to handle "