X-Git-Url: http://git.annexia.org/?p=libguestfs.git;a=blobdiff_plain;f=fish%2Ffish.c;h=3104c407b6158726085e441994a0e7d650b9e5c1;hp=54fd27070d6f15eaf36e4ce4cc65723a2b81a52f;hb=eb209a3664906257b3dc87b6f7b04ff54e0d1288;hpb=74f7c9e4b7e27787e2052166eeedfbac5814200b diff --git a/fish/fish.c b/fish/fish.c index 54fd270..3104c40 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 @@ -30,7 +30,6 @@ #include #include #include -#include #ifdef HAVE_LIBREADLINE #include @@ -41,18 +40,31 @@ #include "fish.h" #include "options.h" +#include "progress.h" #include "c-ctype.h" #include "closeout.h" #include "progname.h" -static void set_up_terminal (void); +/* Return from parse_command_line. See description below. */ +struct parsed_command { + int status; + char *pipe; + char *cmd; + char *argv[64]; +}; + +static void user_cancel (int); static void prepare_drives (struct drv *drv); static int launch (void); static void interactive (void); 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 @@ -60,24 +72,27 @@ 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; +int remote_control_csh = 0; int remote_control = 0; -int exit_on_error = 1; int command_num = 0; 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) @@ -89,23 +104,15 @@ 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 <type = drv_N; + drv->device = NULL; + drv->nr_drives = -1; if (asprintf (&drv->N.filename, "test%d.img", next_prepared_drive++) == -1) { perror ("asprintf"); @@ -341,7 +355,6 @@ main (int argc, char *argv[]) } drv->N.data = create_prepared_file (optarg, drv->N.filename); drv->N.data_free = free_prep_data; - drv->N.device = NULL; /* filled in by add_drives */ drv->next = drvs; drvs = drv; break; @@ -358,6 +371,10 @@ main (int argc, char *argv[]) OPTION_V; break; + case 'w': + OPTION_w; + break; + case 'x': OPTION_x; break; @@ -370,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. @@ -468,18 +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) - 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 (); @@ -489,54 +544,17 @@ main (int argc, char *argv[]) cleanup_readline (); + if (progress_bars) + progress_bar_free (bar); + 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; -} - -void -pod2text (const char *name, const char *shortdesc, const char *str) -{ - FILE *fp; - - fp = popen ("pod2text", "w"); - if (fp == NULL) { - /* pod2text failed, maybe not found, so let's just print the - * source instead, since that's better than doing nothing. - */ - printf ("%s - %s\n\n%s\n", name, shortdesc, str); - return; - } - fprintf (fp, "=head1 NAME\n\n%s - %s\n\n", name, shortdesc); - fputs (str, fp); - pclose (fp); + if (g) + guestfs_user_cancel (g); } static void @@ -545,7 +563,7 @@ prepare_drives (struct drv *drv) if (drv) { prepare_drives (drv->next); if (drv->type == drv_N) - prepare_drive (drv->N.filename, drv->N.data, drv->N.device); + prepare_drive (drv->N.filename, drv->N.data, drv->device); } } @@ -614,12 +632,9 @@ static void script (int prompt) { char *buf; - char *cmd; - char *p, *pend; - char *argv[64]; - int len; int global_exit_on_error = !prompt; - int tilde_candidate; + int exit_on_error; + struct parsed_command pcmd; if (prompt) { printf (_("\n" @@ -638,8 +653,6 @@ script (int prompt) } while (!quit) { - char *pipe = NULL; - exit_on_error = global_exit_on_error; buf = rl_gets (prompt); @@ -648,171 +661,324 @@ script (int prompt) break; } - /* Skip any initial whitespace before the command. */ - again: - while (*buf && c_isspace (*buf)) - buf++; + input_lineno++; - if (!*buf) continue; + pcmd = parse_command_line (buf, &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); + } + } + } + if (prompt) printf ("\n"); +} + +/* Parse a command string, splitting at whitespace, handling '!', '#' etc. + * This destructively updates 'buf'. + * + * 'exit_on_error_rtn' is used to pass in the global exit_on_error + * setting and to return the local setting (eg. if the command begins + * with '-'). + * + * Returns in parsed_command.status: + * 1 = got a guestfish command (returned in cmd_rtn/argv_rtn/pipe_rtn) + * 0 = no guestfish command, but otherwise OK + * -1 = an error + */ +static struct parsed_command +parse_command_line (char *buf, int *exit_on_error_rtn) +{ + struct parsed_command pcmd; + char *p, *pend; + int len; + int tilde_candidate; + int r; + const size_t argv_len = sizeof pcmd.argv / sizeof pcmd.argv[0]; - /* If the next character is '#' then this is a comment. */ - if (*buf == '#') continue; + /* 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); - /* If the next character is '!' then pass the whole lot to system(3). */ - if (*buf == '!') { - int r; + again: + /* Skip any initial whitespace before the command. */ + while (*buf && c_isspace (*buf)) + buf++; - r = system (buf+1); - if (exit_on_error) { - if (r == -1 || - (WIFSIGNALED (r) && - (WTERMSIG (r) == SIGINT || WTERMSIG (r) == SIGQUIT)) || - WEXITSTATUS (r) != 0) - exit (EXIT_FAILURE); + if (!*buf) { + pcmd.status = 0; + return pcmd; + } + + /* If the next character is '#' then this is a comment. */ + if (*buf == '#') { + pcmd.status = 0; + return pcmd; + } + + /* If the next character is '!' then pass the whole lot to system(3). */ + if (*buf == '!') { + r = system (buf+1); + if (r == -1 || + (WIFSIGNALED (r) && + (WTERMSIG (r) == SIGINT || WTERMSIG (r) == SIGQUIT)) || + WEXITSTATUS (r) != 0) + pcmd.status = -1; + else + pcmd.status = 0; + 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; } - if (*pend && (*pend != ' ' && *pend != '\t')) { - fprintf (stderr, - _("%s: command arguments not separated by whitespace\n"), - program_name); - if (exit_on_error) exit (EXIT_FAILURE); - goto next_command; + 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; } - *(pend-1) = '\0'; - */ - } else if (*p != ' ' && *p != '\t') { - /* If the first character is a ~ then note that this parameter - * is a candidate for ~username expansion. NB this does not - * apply to quoted parameters. - */ - tilde_candidate = *p == '~'; - len = strcspn (p, " \t"); - if (p[len]) { - p[len] = '\0'; - pend = &p[len+1]; - } else - pend = &p[len]; - } else { - fprintf (stderr, _("%s: internal error parsing string at '%s'\n"), - program_name, p); - abort (); + 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 (!tilde_candidate) - argv[i] = p; - else - argv[i] = try_tilde_expansion (p); - i++; - p = pend; + if (!*p) { + fprintf (stderr, _("%s: unterminated double quote\n"), program_name); + return -1; + } - if (*p) - p += strspn (p, " \t"); - } + *p = '\0'; + return p - start; +} - if (i == sizeof argv / sizeof argv[0]) { - fprintf (stderr, _("%s: too many arguments\n"), program_name); - if (exit_on_error) exit (EXIT_FAILURE); - goto next_command; - } +/* Used to handle " 0 && line[n-1] == '\n') + line[n-1] = '\0'; - got_command: - if (issue_command (cmd, argv, pipe) == -1) { - if (exit_on_error) exit (EXIT_FAILURE); + 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); - next_command:; + if (pclose (pp) != 0) { + perror ("pclose"); + return -1; } - if (prompt) printf ("\n"); + + return 0; } static void @@ -820,6 +986,7 @@ cmdline (char *argv[], int optind, int argc) { const char *cmd; char **params; + int exit_on_error; exit_on_error = 1; @@ -846,25 +1013,31 @@ cmdline (char *argv[], int optind, int argc) optind++; if (optind == argc) { - if (issue_command (cmd, params, NULL) == -1 && exit_on_error) + if (issue_command (cmd, params, NULL, exit_on_error) == -1 && exit_on_error) exit (EXIT_FAILURE); } else { argv[optind] = NULL; - if (issue_command (cmd, params, NULL) == -1 && exit_on_error) + if (issue_command (cmd, params, NULL, exit_on_error) == -1 && exit_on_error) exit (EXIT_FAILURE); cmdline (argv, optind+1, argc); } } +/* Note: 'rc_exit_on_error_flag' is the exit_on_error flag that we + * pass to the remote server (when issuing --remote commands). It + * does not cause issue_command itself to exit on error. + */ int -issue_command (const char *cmd, char *argv[], const char *pipecmd) +issue_command (const char *cmd, char *argv[], const char *pipecmd, + int rc_exit_on_error_flag) { int argc; int stdout_saved_fd = -1; int pid = 0; - int i, r; + int r; - reset_progress_bar (); + if (progress_bars) + progress_bar_reset (bar); /* This counts the commands issued, starting at 1. */ command_num++; @@ -920,7 +1093,7 @@ issue_command (const char *cmd, char *argv[], const char *pipecmd) /* If --remote was set, then send this command to a remote process. */ if (remote_control) - r = rc_remote (remote_control, cmd, argc, argv, exit_on_error); + r = rc_remote (remote_control, cmd, argc, argv, rc_exit_on_error_flag); /* Otherwise execute it locally. */ else if (STRCASEEQ (cmd, "help")) { @@ -1015,6 +1188,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) { @@ -1168,9 +1349,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; } @@ -1292,16 +1472,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) @@ -1311,21 +1496,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? */ @@ -1340,6 +1531,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 "-< 0 && ret[len-1] == '\n') - ret[len-1] = '\0'; - - error: - /* Restore echo, close file descriptor. */ - if (tty && tcset) { - printf ("\n"); - tcsetattr (fileno (infp), TCSAFLUSH, &orig); - } + if (array_len < 4) + return; - if (infp != stdin) - fclose (infp); /* outfp == infp, so this is closed also */ + /*uint64_t proc_nr = array[0];*/ + /*uint64_t serial = array[1];*/ + uint64_t position = array[2]; + uint64_t total = array[3]; - return ret; + progress_bar_set (bar, position, total); }