1 /* guestfish - the filesystem interactive shell
2 * Copyright (C) 2009 Red Hat Inc.
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 #define _GNU_SOURCE // for strchrnul
33 #ifdef HAVE_LIBREADLINE
34 #include <readline/readline.h>
35 #include <readline/history.h>
53 static void add_drives (struct drv *drv);
54 static void mount_mps (struct mp *mp);
55 static void interactive (void);
56 static void shell_script (void);
57 static void script (int prompt);
58 static void cmdline (char *argv[], int optind, int argc);
59 static int issue_command (const char *cmd, char *argv[]);
60 static void initialize_readline (void);
61 static void cleanup_readline (void);
62 static void add_history_line (const char *);
64 /* Currently open libguestfs handle. */
72 launch (guestfs_h *_g)
76 if (guestfs_is_config (g)) {
77 if (guestfs_launch (g) == -1)
79 if (guestfs_wait_ready (g) == -1)
89 _("guestfish: guest filesystem shell\n"
90 "guestfish lets you edit virtual machine filesystems\n"
91 "Copyright (C) 2009 Red Hat Inc.\n"
93 " guestfish [--options] cmd [: cmd : cmd ...]\n"
94 "or for interactive use:\n"
96 "or from a shell script:\n"
102 " -h|--cmd-help List available commands\n"
103 " -h|--cmd-help cmd Display detailed help on 'cmd'\n"
104 " -a|--add image Add image\n"
105 " -m|--mount dev[:mnt] Mount dev on mnt (if omitted, /)\n"
106 " -n|--no-sync Don't autosync\n"
107 " -r|--ro Mount read-only\n"
108 " -v|--verbose Verbose messages\n"
109 " -V|--version Display version and exit\n"
110 "For more information, see the manpage guestfish(1).\n"));
114 main (int argc, char *argv[])
116 static const char *options = "a:h::m:nrv?V";
117 static struct option long_options[] = {
118 { "add", 1, 0, 'a' },
119 { "cmd-help", 2, 0, 'h' },
120 { "help", 0, 0, '?' },
121 { "mount", 1, 0, 'm' },
122 { "no-sync", 0, 0, 'n' },
124 { "verbose", 0, 0, 'v' },
125 { "version", 0, 0, 'V' },
128 struct drv *drvs = NULL;
130 struct mp *mps = NULL;
135 initialize_readline ();
137 /* guestfs_create is meant to be a lightweight operation, so
138 * it's OK to do it early here.
140 g = guestfs_create ();
142 fprintf (stderr, _("guestfs_create: failed to create handle\n"));
146 guestfs_set_autosync (g, 1);
148 /* If developing, add ./appliance to the path. Note that libtools
149 * interferes with this because uninstalled guestfish is a shell
150 * script that runs the real program with an absolute path. Detect
153 * BUT if LIBGUESTFS_PATH environment variable is already set by
154 * the user, then don't override it.
156 if (getenv ("LIBGUESTFS_PATH") == NULL &&
158 (argv[0][0] != '/' || strstr (argv[0], "/.libs/lt-") != NULL))
159 guestfs_set_path (g, "appliance:" GUESTFS_DEFAULT_PATH);
162 c = getopt_long (argc, argv, options, long_options, NULL);
167 if (access (optarg, R_OK) != 0) {
171 drv = malloc (sizeof (struct drv));
176 drv->filename = optarg;
183 display_command (optarg);
184 else if (argv[optind] && argv[optind][0] != '-')
185 display_command (argv[optind++]);
191 mp = malloc (sizeof (struct mp));
196 p = strchr (optarg, ':');
199 mp->mountpoint = p+1;
201 mp->mountpoint = "/";
208 guestfs_set_autosync (g, 0);
217 guestfs_set_verbose (g, verbose);
221 printf ("guestfish %s\n", PACKAGE_VERSION);
229 fprintf (stderr, _("guestfish: unexpected command line option 0x%x\n"),
235 /* If we've got drives to add, add them now. */
238 /* If we've got mountpoints, we must launch the guest and mount them. */
240 if (launch (g) == -1) exit (1);
244 /* Interactive, shell script, or command(s) on the command line? */
245 if (optind >= argc) {
252 cmdline (argv, optind, argc);
260 pod2text (const char *heading, const char *str)
264 fp = popen ("pod2text", "w");
266 /* pod2text failed, maybe not found, so let's just print the
267 * source instead, since that's better than doing nothing.
269 printf ("%s\n\n%s\n", heading, str);
272 fputs ("=head1 ", fp);
279 /* List is built in reverse order, so mount them in reverse order. */
281 mount_mps (struct mp *mp)
286 mount_mps (mp->next);
288 r = guestfs_mount (g, mp->device, mp->mountpoint);
290 r = guestfs_mount_ro (g, mp->device, mp->mountpoint);
297 add_drives (struct drv *drv)
302 add_drives (drv->next);
304 r = guestfs_add_drive (g, drv->filename);
306 r = guestfs_add_drive_ro (g, drv->filename);
324 #define FISH "><fs> "
326 static char *line_read = NULL;
331 #ifdef HAVE_LIBREADLINE
339 line_read = readline (prompt ? FISH : "");
341 if (line_read && *line_read)
342 add_history_line (line_read);
347 #endif /* HAVE_LIBREADLINE */
349 static char buf[8192];
352 if (prompt) printf (FISH);
353 line_read = fgets (buf, sizeof buf, stdin);
356 len = strlen (line_read);
357 if (len > 0 && buf[len-1] == '\n') buf[len-1] = '\0';
371 int global_exit_on_error = !prompt;
376 "Welcome to guestfish, the libguestfs filesystem interactive shell for\n"
377 "editing virtual machine filesystems.\n"
379 "Type: 'help' for help with commands\n"
380 " 'quit' to quit the shell\n"
384 exit_on_error = global_exit_on_error;
386 buf = rl_gets (prompt);
392 /* Skip any initial whitespace before the command. */
394 while (*buf && isspace (*buf))
399 /* If the next character is '#' then this is a comment. */
400 if (*buf == '#') continue;
402 /* If the next character is '!' then pass the whole lot to system(3). */
410 (WTERMSIG (r) == SIGINT || WTERMSIG (r) == SIGQUIT)) ||
411 WEXITSTATUS (r) != 0)
417 /* If the next character is '-' allow the command to fail without
418 * exiting on error (just for this one command though).
426 /* Get the command (cannot be quoted). */
427 len = strcspn (buf, " \t");
429 if (len == 0) continue;
433 if (buf[len] == '\0') {
440 p += strspn (p, " \t");
442 /* Get the parameters. */
443 while (*p && i < sizeof argv / sizeof argv[0]) {
444 /* Parameters which start with quotes or square brackets
445 * are treated specially. Bare parameters are delimited
450 len = strcspn (p, "\"");
451 if (p[len] == '\0') {
452 fprintf (stderr, _("guestfish: unterminated double quote\n"));
453 if (exit_on_error) exit (1);
456 if (p[len+1] && (p[len+1] != ' ' && p[len+1] != '\t')) {
457 fprintf (stderr, _("guestfish: command arguments not separated by whitespace\n"));
458 if (exit_on_error) exit (1);
462 pend = p[len+1] ? &p[len+2] : &p[len+1];
463 } else if (*p == '\'') {
465 len = strcspn (p, "'");
466 if (p[len] == '\0') {
467 fprintf (stderr, _("guestfish: unterminated single quote\n"));
468 if (exit_on_error) exit (1);
471 if (p[len+1] && (p[len+1] != ' ' && p[len+1] != '\t')) {
472 fprintf (stderr, _("guestfish: command arguments not separated by whitespace\n"));
473 if (exit_on_error) exit (1);
477 pend = p[len+1] ? &p[len+2] : &p[len+1];
479 } else if (*p == '[') {
483 while (*pend && c != 0) {
484 if (*pend == '[') c++;
485 else if (*pend == ']') c--;
489 fprintf (stderr, _("guestfish: unterminated \"[...]\" sequence\n"));
490 if (exit_on_error) exit (1);
493 if (*pend && (*pend != ' ' && *pend != '\t')) {
494 fprintf (stderr, _("guestfish: command arguments not separated by whitespace\n"));
495 if (exit_on_error) exit (1);
500 } else if (*p != ' ' && *p != '\t') {
501 len = strcspn (p, " \t");
508 fprintf (stderr, _("guestfish: internal error parsing string at '%s'\n"),
517 p += strspn (p, " \t");
520 if (i == sizeof argv / sizeof argv[0]) {
521 fprintf (stderr, _("guestfish: too many arguments\n"));
522 if (exit_on_error) exit (1);
529 if (issue_command (cmd, argv) == -1) {
530 if (exit_on_error) exit (1);
535 if (prompt) printf ("\n");
539 cmdline (char *argv[], int optind, int argc)
544 if (optind >= argc) return;
546 cmd = argv[optind++];
547 if (strcmp (cmd, ":") == 0) {
548 fprintf (stderr, _("guestfish: empty command on command line\n"));
551 params = &argv[optind];
553 /* Search for end of command list or ":" ... */
554 while (optind < argc && strcmp (argv[optind], ":") != 0)
557 if (optind == argc) {
558 if (issue_command (cmd, params) == -1) exit (1);
561 if (issue_command (cmd, params) == -1) exit (1);
562 cmdline (argv, optind+1, argc);
567 issue_command (const char *cmd, char *argv[])
571 for (argc = 0; argv[argc] != NULL; ++argc)
574 if (strcasecmp (cmd, "help") == 0) {
578 display_command (argv[0]);
581 else if (strcasecmp (cmd, "quit") == 0 ||
582 strcasecmp (cmd, "exit") == 0 ||
583 strcasecmp (cmd, "q") == 0) {
587 else if (strcasecmp (cmd, "alloc") == 0 ||
588 strcasecmp (cmd, "allocate") == 0)
589 return do_alloc (cmd, argc, argv);
590 else if (strcasecmp (cmd, "echo") == 0)
591 return do_echo (cmd, argc, argv);
592 else if (strcasecmp (cmd, "edit") == 0 ||
593 strcasecmp (cmd, "vi") == 0 ||
594 strcasecmp (cmd, "emacs") == 0)
595 return do_edit (cmd, argc, argv);
596 else if (strcasecmp (cmd, "lcd") == 0)
597 return do_lcd (cmd, argc, argv);
599 return run_action (cmd, argc, argv);
603 list_builtin_commands (void)
605 /* help and quit should appear at the top */
606 printf ("%-20s %s\n",
607 "help", _("display a list of commands or help on a command"));
608 printf ("%-20s %s\n",
609 "quit", _("quit guestfish"));
611 printf ("%-20s %s\n",
612 "alloc", _("allocate an image"));
613 printf ("%-20s %s\n",
614 "echo", _("display a line of text"));
615 printf ("%-20s %s\n",
616 "edit", _("edit a file in the image"));
617 printf ("%-20s %s\n",
618 "lcd", _("local change directory"));
620 /* actions are printed after this (see list_commands) */
624 display_builtin_command (const char *cmd)
626 /* help for actions is auto-generated, see display_command */
628 if (strcasecmp (cmd, "alloc") == 0 ||
629 strcasecmp (cmd, "allocate") == 0)
630 printf (_("alloc - allocate an image\n"
631 " alloc <filename> <size>\n"
633 " This creates an empty (zeroed) file of the given size,\n"
634 " and then adds so it can be further examined.\n"
636 " For more advanced image creation, see qemu-img utility.\n"
638 " Size can be specified (where <nn> means a number):\n"
639 " <nn> number of kilobytes\n"
640 " eg: 1440 standard 3.5\" floppy\n"
641 " <nn>K or <nn>KB number of kilobytes\n"
642 " <nn>M or <nn>MB number of megabytes\n"
643 " <nn>G or <nn>GB number of gigabytes\n"
644 " <nn>sects number of 512 byte sectors\n"));
645 else if (strcasecmp (cmd, "echo") == 0)
646 printf (_("echo - display a line of text\n"
647 " echo [<params> ...]\n"
649 " This echos the parameters to the terminal.\n"));
650 else if (strcasecmp (cmd, "edit") == 0 ||
651 strcasecmp (cmd, "vi") == 0 ||
652 strcasecmp (cmd, "emacs") == 0)
653 printf (_("edit - edit a file in the image\n"
656 " This is used to edit a file.\n"
658 " It is the equivalent of (and is implemented by)\n"
659 " running \"cat\", editing locally, and then \"write-file\".\n"
661 " Normally it uses $EDITOR, but if you use the aliases\n"
662 " \"vi\" or \"emacs\" you will get those editors.\n"
664 " NOTE: This will not work reliably for large files\n"
665 " (> 2 MB) or binary files containing \\0 bytes.\n"));
666 else if (strcasecmp (cmd, "lcd") == 0)
667 printf (_("lcd - local change directory\n"
670 " Change guestfish's current directory. This command is\n"
671 " useful if you want to download files to a particular\n"
673 else if (strcasecmp (cmd, "help") == 0)
674 printf (_("help - display a list of commands or help on a command\n"
677 else if (strcasecmp (cmd, "quit") == 0 ||
678 strcasecmp (cmd, "exit") == 0 ||
679 strcasecmp (cmd, "q") == 0)
680 printf (_("quit - quit guestfish\n"
683 fprintf (stderr, _("%s: command not known, use -h to list all commands\n"),
688 free_strings (char **argv)
692 for (argc = 0; argv[argc] != NULL; ++argc)
698 print_strings (char * const * const argv)
702 for (argc = 0; argv[argc] != NULL; ++argc)
703 printf ("%s\n", argv[argc]);
707 print_table (char * const * const argv)
711 for (i = 0; argv[i] != NULL; i += 2)
712 printf ("%s: %s\n", argv[i], argv[i+1]);
716 is_true (const char *str)
719 strcasecmp (str, "0") != 0 &&
720 strcasecmp (str, "f") != 0 &&
721 strcasecmp (str, "false") != 0 &&
722 strcasecmp (str, "n") != 0 &&
723 strcasecmp (str, "no") != 0;
726 /* XXX We could improve list parsing. */
728 parse_string_list (const char *str)
731 const char *p, *pend;
735 for (i = 0; str[i]; ++i)
736 if (str[i] == ' ') argc++;
738 argv = malloc (sizeof (char *) * (argc+1));
739 if (argv == NULL) { perror ("malloc"); exit (1); }
744 pend = strchrnul (p, ' ');
745 argv[i] = strndup (p, pend-p);
747 p = *pend == ' ' ? pend+1 : pend;
754 #ifdef HAVE_LIBREADLINE
755 static char histfile[1024];
756 static int nr_history_lines = 0;
760 initialize_readline (void)
762 #ifdef HAVE_LIBREADLINE
765 home = getenv ("HOME");
767 snprintf (histfile, sizeof histfile, "%s/.guestfish", home);
769 (void) read_history (histfile);
772 rl_readline_name = "guestfish";
773 rl_attempted_completion_function = do_completion;
778 cleanup_readline (void)
780 #ifdef HAVE_LIBREADLINE
783 if (histfile[0] != '\0') {
784 fd = open (histfile, O_WRONLY|O_CREAT, 0644);
791 (void) append_history (nr_history_lines, histfile);
797 add_history_line (const char *line)
799 #ifdef HAVE_LIBREADLINE