X-Git-Url: http://git.annexia.org/?a=blobdiff_plain;f=fish%2Ffish.c;h=c55f54b1cfd4b1b9d2c9824809a731e18c1782f6;hb=3e9f4af1e1126b1e1f7a93379172dd095dd1b908;hp=566d769fb5bfb8f9cfca21cc5ea737324082c0e0;hpb=61a4db138e4f85033c655bf6b24df0949683c24c;p=libguestfs.git diff --git a/fish/fish.c b/fish/fish.c index 566d769..c55f54b 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 @@ -13,7 +13,7 @@ * * 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., 675 Mass Ave, Cambridge, MA 02139, USA. + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */ #include @@ -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,6 +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 @@ -68,11 +72,13 @@ 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; int quit = 0; int verbose = 0; int remote_control_listen = 0; @@ -83,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) @@ -97,18 +104,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); + if (progress_bars) { + bar = progress_bar_init (0); + if (!bar) { + perror ("progress_bar_init"); + exit (EXIT_FAILURE); + } + + 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 (); @@ -507,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 @@ -643,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); @@ -677,7 +699,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. */ @@ -708,6 +733,18 @@ parse_command_line (char *buf, int *exit_on_error_rtn) return pcmd; } + /* If the next two characters are "= '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 " 0 && line[n-1] == '\n') + line[n-1] = '\0'; + + pcmd = parse_command_line (line, &exit_on_error); + if (pcmd.status == -1 && exit_on_error) + exit (EXIT_FAILURE); + if (pcmd.status == 1) { + if (issue_command (pcmd.cmd, pcmd.argv, pcmd.pipe, exit_on_error) == -1) { + if (exit_on_error) exit (EXIT_FAILURE); + } + } + } + + free (line); + + if (pclose (pp) != 0) { + perror ("pclose"); + return -1; + } + + return 0; +} + static void cmdline (char *argv[], int optind, int argc) { @@ -878,7 +1038,8 @@ issue_command (const char *cmd, char *argv[], const char *pipecmd, 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++; @@ -1029,6 +1190,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) { @@ -1182,9 +1351,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; } @@ -1306,16 +1474,21 @@ xwrite (int fd, const void *v_buf, size_t len) return 0; } -/* Resolve the special "win:..." form for Windows-specific paths. - * This always returns a newly allocated string which is freed by the - * caller function in "cmds.c". +/* Resolve the special "win:..." form for Windows-specific paths. The + * generated code calls this for all device or path arguments. + * + * The function returns a newly allocated string, and the caller must + * free this string; else display an error and return NULL. */ +static char *win_prefix_drive_letter (char drive_letter, const char *path); + char * -resolve_win_path (const char *path) +win_prefix (const char *path) { char *ret; size_t i; + /* If there is not a "win:..." prefix on the path, return strdup'd string. */ if (STRCASENEQLEN (path, "win:", 4)) { ret = strdup (path); if (ret == NULL) @@ -1325,21 +1498,27 @@ resolve_win_path (const char *path) path += 4; - /* Drop drive letter, if it's "C:". */ - if (STRCASEEQLEN (path, "c:", 2)) - path += 2; - - if (!*path) { - ret = strdup ("/"); + /* If there is a drive letter, rewrite the path. */ + if (c_isalpha (path[0]) && path[1] == ':') { + char drive_letter = c_tolower (path[0]); + /* This returns the newly allocated string. */ + ret = win_prefix_drive_letter (drive_letter, path + 2); if (ret == NULL) + return NULL; + } + else if (!*path) { + ret = strdup ("/"); + if (ret == NULL) { perror ("strdup"); - return ret; + return NULL; + } } - - ret = strdup (path); - if (ret == NULL) { - perror ("strdup"); - return NULL; + else { + ret = strdup (path); + if (ret == NULL) { + perror ("strdup"); + return NULL; + } } /* Blindly convert any backslashes into forward slashes. Is this good? */ @@ -1354,6 +1533,82 @@ resolve_win_path (const char *path) return ret; } +static char * +win_prefix_drive_letter (char drive_letter, const char *path) +{ + char **roots = NULL; + char **drives = NULL; + char **mountpoints = NULL; + char *device, *mountpoint, *ret = NULL; + size_t i; + + /* Resolve the drive letter using the drive mappings table. */ + roots = guestfs_inspect_get_roots (g); + if (roots == NULL) + goto out; + if (roots[0] == NULL) { + fprintf (stderr, _("%s: to use Windows drive letters, you must inspect the guest (\"-i\" option or run \"inspect-os\" command)\n"), + program_name); + goto out; + } + drives = guestfs_inspect_get_drive_mappings (g, roots[0]); + if (drives == NULL || drives[0] == NULL) { + fprintf (stderr, _("%s: to use Windows drive letters, this must be a Windows guest\n"), + program_name); + goto out; + } + + device = NULL; + for (i = 0; drives[i] != NULL; i += 2) { + if (c_tolower (drives[i][0]) == drive_letter && drives[i][1] == '\0') { + device = drives[i+1]; + break; + } + } + + if (device == NULL) { + fprintf (stderr, _("%s: drive '%c:' not found. To list available drives do:\n inspect-get-drive-mappings %s\n"), + program_name, drive_letter, roots[0]); + goto out; + } + + /* This drive letter must be mounted somewhere (we won't do it). */ + mountpoints = guestfs_mountpoints (g); + if (mountpoints == NULL) + goto out; + + mountpoint = NULL; + for (i = 0; mountpoints[i] != NULL; i += 2) { + if (STREQ (mountpoints[i], device)) { + mountpoint = mountpoints[i+1]; + break; + } + } + + if (mountpoint == NULL) { + fprintf (stderr, _("%s: to access '%c:', mount %s first. One way to do this is:\n umount-all\n mount %s /\n"), + program_name, drive_letter, device, device); + goto out; + } + + /* Rewrite the path, eg. if C: => /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 "-<