X-Git-Url: http://git.annexia.org/?a=blobdiff_plain;f=fish%2Ffish.c;h=a57472f8c6eb0c5c856f1a1739fac7750c6b1839;hb=5c3c7e8825341e18c9449976f8a321a04cc78d79;hp=566d769fb5bfb8f9cfca21cc5ea737324082c0e0;hpb=61a4db138e4f85033c655bf6b24df0949683c24c;p=libguestfs.git diff --git a/fish/fish.c b/fish/fish.c index 566d769..a57472f 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 @@ -61,6 +61,7 @@ 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 execute_and_inline (const char *cmd, int exit_on_error); static void initialize_readline (void); static void cleanup_readline (void); #ifdef HAVE_LIBREADLINE @@ -73,6 +74,7 @@ static int override_progress_bars = -1; guestfs_h *g; int read_only = 0; +int live = 0; int quit = 0; int verbose = 0; int remote_control_listen = 0; @@ -97,18 +99,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 <= argc && isatty (0)); 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) { @@ -677,7 +674,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 +708,18 @@ parse_command_line (char *buf, int *exit_on_error_rtn) return pcmd; } + /* If the next two characters are " 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) == -1) { + perror ("pclose"); + return -1; + } + + return 0; +} + static void cmdline (char *argv[], int optind, int argc) { @@ -1306,16 +1362,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 +1386,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 +1421,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 "-<