X-Git-Url: http://git.annexia.org/?p=libguestfs.git;a=blobdiff_plain;f=fish%2Ffish.c;h=efc74b26571eb191d2ca448b96e588b02d06f509;hp=4a960dc0ad18e3a8e8832aa69b1d105fea0ae2a6;hb=319e946b92e175c05cdd1fdcb85c9b86f5631011;hpb=c3887285abbb3606822626ba396a51cb5df80d43 diff --git a/fish/fish.c b/fish/fish.c index 4a960dc..efc74b2 100644 --- a/fish/fish.c +++ b/fish/fish.c @@ -1,5 +1,5 @@ /* guestfish - the filesystem interactive shell - * Copyright (C) 2009-2010 Red Hat Inc. + * Copyright (C) 2009-2011 Red Hat Inc. * * 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 @@ -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); @@ -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 @@ -71,9 +74,10 @@ 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; int quit = 0; int verbose = 0; int remote_control_listen = 0; @@ -87,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) @@ -98,18 +105,9 @@ usage (int status) fprintf (stdout, _("%s: guest filesystem shell\n" "%s lets you edit virtual machine filesystems\n" - "Copyright (C) 2009-2010 Red Hat Inc.\n" + "Copyright (C) 2009-2011 Red Hat Inc.\n" "Usage:\n" " %s [--options] cmd [: cmd : cmd ...]\n" - " %s [--ro] -i -a disk-image\n" - " %s [--ro] -i -d libvirt-domain\n" - "or for interactive use:\n" - " %s\n" - "or from a shell script:\n" - " %s <= 0 ? override_progress_bars - : (optind >= argc && isatty (0)); + : (optind >= argc && is_interactive); if (progress_bars) - guestfs_set_progress_callback (g, progress_callback, NULL); + 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 (); @@ -511,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 *); @@ -644,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); @@ -678,7 +719,10 @@ parse_command_line (char *buf, int *exit_on_error_rtn) int r; const size_t argv_len = sizeof pcmd.argv / sizeof pcmd.argv[0]; - pcmd.pipe = NULL; + /* Note that pcmd.pipe must be set to NULL for correct usage. Other + * fields do not need to be, but this silences a gcc warning. + */ + memset (&pcmd, 0, sizeof pcmd); again: /* Skip any initial whitespace before the command. */ @@ -759,9 +803,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; } @@ -772,7 +815,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++; @@ -836,6 +878,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 " /c then C:/foo => /c/foo */ + if (asprintf (&ret, "%s%s%s", + mountpoint, STRNEQ (mountpoint, "/") ? "/" : "", path) == -1) { + perror ("asprintf"); + goto out; + } + + out: + if (roots) + free_strings (roots); + if (drives) + free_strings (drives); + if (mountpoints) + free_strings (mountpoints); + + return ret; +} + /* Resolve the special FileIn paths ("-" or "-<