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 " -D|--no-dest-paths Don't tab-complete paths from guest fs\n"
106 " -m|--mount dev[:mnt] Mount dev on mnt (if omitted, /)\n"
107 " -n|--no-sync Don't autosync\n"
108 " -r|--ro Mount read-only\n"
109 " -v|--verbose Verbose messages\n"
110 " -V|--version Display version and exit\n"
111 "For more information, see the manpage guestfish(1).\n"));
115 main (int argc, char *argv[])
117 static const char *options = "a:h::m:nrv?V";
118 static struct option long_options[] = {
119 { "add", 1, 0, 'a' },
120 { "cmd-help", 2, 0, 'h' },
121 { "help", 0, 0, '?' },
122 { "mount", 1, 0, 'm' },
123 { "no-dest-paths", 0, 0, 'D' },
124 { "no-sync", 0, 0, 'n' },
126 { "verbose", 0, 0, 'v' },
127 { "version", 0, 0, 'V' },
130 struct drv *drvs = NULL;
132 struct mp *mps = NULL;
137 initialize_readline ();
139 /* guestfs_create is meant to be a lightweight operation, so
140 * it's OK to do it early here.
142 g = guestfs_create ();
144 fprintf (stderr, _("guestfs_create: failed to create handle\n"));
148 guestfs_set_autosync (g, 1);
150 /* If developing, add ./appliance to the path. Note that libtools
151 * interferes with this because uninstalled guestfish is a shell
152 * script that runs the real program with an absolute path. Detect
155 * BUT if LIBGUESTFS_PATH environment variable is already set by
156 * the user, then don't override it.
158 if (getenv ("LIBGUESTFS_PATH") == NULL &&
160 (argv[0][0] != '/' || strstr (argv[0], "/.libs/lt-") != NULL))
161 guestfs_set_path (g, "appliance:" GUESTFS_DEFAULT_PATH);
164 c = getopt_long (argc, argv, options, long_options, NULL);
169 if (access (optarg, R_OK) != 0) {
173 drv = malloc (sizeof (struct drv));
178 drv->filename = optarg;
184 complete_dest_paths = 0;
189 display_command (optarg);
190 else if (argv[optind] && argv[optind][0] != '-')
191 display_command (argv[optind++]);
197 mp = malloc (sizeof (struct mp));
202 p = strchr (optarg, ':');
205 mp->mountpoint = p+1;
207 mp->mountpoint = "/";
214 guestfs_set_autosync (g, 0);
223 guestfs_set_verbose (g, verbose);
227 printf ("guestfish %s\n", PACKAGE_VERSION);
235 fprintf (stderr, _("guestfish: unexpected command line option 0x%x\n"),
241 /* If we've got drives to add, add them now. */
244 /* If we've got mountpoints, we must launch the guest and mount them. */
246 if (launch (g) == -1) exit (1);
250 /* Interactive, shell script, or command(s) on the command line? */
251 if (optind >= argc) {
258 cmdline (argv, optind, argc);
266 pod2text (const char *heading, const char *str)
270 fp = popen ("pod2text", "w");
272 /* pod2text failed, maybe not found, so let's just print the
273 * source instead, since that's better than doing nothing.
275 printf ("%s\n\n%s\n", heading, str);
278 fputs ("=head1 ", fp);
285 /* List is built in reverse order, so mount them in reverse order. */
287 mount_mps (struct mp *mp)
292 mount_mps (mp->next);
294 r = guestfs_mount (g, mp->device, mp->mountpoint);
296 r = guestfs_mount_ro (g, mp->device, mp->mountpoint);
303 add_drives (struct drv *drv)
308 add_drives (drv->next);
310 r = guestfs_add_drive (g, drv->filename);
312 r = guestfs_add_drive_ro (g, drv->filename);
330 #define FISH "><fs> "
332 static char *line_read = NULL;
337 #ifdef HAVE_LIBREADLINE
345 line_read = readline (prompt ? FISH : "");
347 if (line_read && *line_read)
348 add_history_line (line_read);
353 #endif /* HAVE_LIBREADLINE */
355 static char buf[8192];
358 if (prompt) printf (FISH);
359 line_read = fgets (buf, sizeof buf, stdin);
362 len = strlen (line_read);
363 if (len > 0 && buf[len-1] == '\n') buf[len-1] = '\0';
377 int global_exit_on_error = !prompt;
382 "Welcome to guestfish, the libguestfs filesystem interactive shell for\n"
383 "editing virtual machine filesystems.\n"
385 "Type: 'help' for help with commands\n"
386 " 'quit' to quit the shell\n"
390 exit_on_error = global_exit_on_error;
392 buf = rl_gets (prompt);
398 /* Skip any initial whitespace before the command. */
400 while (*buf && isspace (*buf))
405 /* If the next character is '#' then this is a comment. */
406 if (*buf == '#') continue;
408 /* If the next character is '!' then pass the whole lot to system(3). */
416 (WTERMSIG (r) == SIGINT || WTERMSIG (r) == SIGQUIT)) ||
417 WEXITSTATUS (r) != 0)
423 /* If the next character is '-' allow the command to fail without
424 * exiting on error (just for this one command though).
432 /* Get the command (cannot be quoted). */
433 len = strcspn (buf, " \t");
435 if (len == 0) continue;
439 if (buf[len] == '\0') {
446 p += strspn (p, " \t");
448 /* Get the parameters. */
449 while (*p && i < sizeof argv / sizeof argv[0]) {
450 /* Parameters which start with quotes or square brackets
451 * are treated specially. Bare parameters are delimited
456 len = strcspn (p, "\"");
457 if (p[len] == '\0') {
458 fprintf (stderr, _("guestfish: unterminated double quote\n"));
459 if (exit_on_error) exit (1);
462 if (p[len+1] && (p[len+1] != ' ' && p[len+1] != '\t')) {
463 fprintf (stderr, _("guestfish: command arguments not separated by whitespace\n"));
464 if (exit_on_error) exit (1);
468 pend = p[len+1] ? &p[len+2] : &p[len+1];
469 } else if (*p == '\'') {
471 len = strcspn (p, "'");
472 if (p[len] == '\0') {
473 fprintf (stderr, _("guestfish: unterminated single quote\n"));
474 if (exit_on_error) exit (1);
477 if (p[len+1] && (p[len+1] != ' ' && p[len+1] != '\t')) {
478 fprintf (stderr, _("guestfish: command arguments not separated by whitespace\n"));
479 if (exit_on_error) exit (1);
483 pend = p[len+1] ? &p[len+2] : &p[len+1];
485 } else if (*p == '[') {
489 while (*pend && c != 0) {
490 if (*pend == '[') c++;
491 else if (*pend == ']') c--;
495 fprintf (stderr, _("guestfish: unterminated \"[...]\" sequence\n"));
496 if (exit_on_error) exit (1);
499 if (*pend && (*pend != ' ' && *pend != '\t')) {
500 fprintf (stderr, _("guestfish: command arguments not separated by whitespace\n"));
501 if (exit_on_error) exit (1);
506 } else if (*p != ' ' && *p != '\t') {
507 len = strcspn (p, " \t");
514 fprintf (stderr, _("guestfish: internal error parsing string at '%s'\n"),
523 p += strspn (p, " \t");
526 if (i == sizeof argv / sizeof argv[0]) {
527 fprintf (stderr, _("guestfish: too many arguments\n"));
528 if (exit_on_error) exit (1);
535 if (issue_command (cmd, argv) == -1) {
536 if (exit_on_error) exit (1);
541 if (prompt) printf ("\n");
545 cmdline (char *argv[], int optind, int argc)
550 if (optind >= argc) return;
552 cmd = argv[optind++];
553 if (strcmp (cmd, ":") == 0) {
554 fprintf (stderr, _("guestfish: empty command on command line\n"));
557 params = &argv[optind];
559 /* Search for end of command list or ":" ... */
560 while (optind < argc && strcmp (argv[optind], ":") != 0)
563 if (optind == argc) {
564 if (issue_command (cmd, params) == -1) exit (1);
567 if (issue_command (cmd, params) == -1) exit (1);
568 cmdline (argv, optind+1, argc);
573 issue_command (const char *cmd, char *argv[])
577 for (argc = 0; argv[argc] != NULL; ++argc)
580 if (strcasecmp (cmd, "help") == 0) {
584 display_command (argv[0]);
587 else if (strcasecmp (cmd, "quit") == 0 ||
588 strcasecmp (cmd, "exit") == 0 ||
589 strcasecmp (cmd, "q") == 0) {
593 else if (strcasecmp (cmd, "alloc") == 0 ||
594 strcasecmp (cmd, "allocate") == 0)
595 return do_alloc (cmd, argc, argv);
596 else if (strcasecmp (cmd, "echo") == 0)
597 return do_echo (cmd, argc, argv);
598 else if (strcasecmp (cmd, "edit") == 0 ||
599 strcasecmp (cmd, "vi") == 0 ||
600 strcasecmp (cmd, "emacs") == 0)
601 return do_edit (cmd, argc, argv);
602 else if (strcasecmp (cmd, "lcd") == 0)
603 return do_lcd (cmd, argc, argv);
605 return run_action (cmd, argc, argv);
609 list_builtin_commands (void)
611 /* help and quit should appear at the top */
612 printf ("%-20s %s\n",
613 "help", _("display a list of commands or help on a command"));
614 printf ("%-20s %s\n",
615 "quit", _("quit guestfish"));
617 printf ("%-20s %s\n",
618 "alloc", _("allocate an image"));
619 printf ("%-20s %s\n",
620 "echo", _("display a line of text"));
621 printf ("%-20s %s\n",
622 "edit", _("edit a file in the image"));
623 printf ("%-20s %s\n",
624 "lcd", _("local change directory"));
626 /* actions are printed after this (see list_commands) */
630 display_builtin_command (const char *cmd)
632 /* help for actions is auto-generated, see display_command */
634 if (strcasecmp (cmd, "alloc") == 0 ||
635 strcasecmp (cmd, "allocate") == 0)
636 printf (_("alloc - allocate an image\n"
637 " alloc <filename> <size>\n"
639 " This creates an empty (zeroed) file of the given size,\n"
640 " and then adds so it can be further examined.\n"
642 " For more advanced image creation, see qemu-img utility.\n"
644 " Size can be specified (where <nn> means a number):\n"
645 " <nn> number of kilobytes\n"
646 " eg: 1440 standard 3.5\" floppy\n"
647 " <nn>K or <nn>KB number of kilobytes\n"
648 " <nn>M or <nn>MB number of megabytes\n"
649 " <nn>G or <nn>GB number of gigabytes\n"
650 " <nn>sects number of 512 byte sectors\n"));
651 else if (strcasecmp (cmd, "echo") == 0)
652 printf (_("echo - display a line of text\n"
653 " echo [<params> ...]\n"
655 " This echos the parameters to the terminal.\n"));
656 else if (strcasecmp (cmd, "edit") == 0 ||
657 strcasecmp (cmd, "vi") == 0 ||
658 strcasecmp (cmd, "emacs") == 0)
659 printf (_("edit - edit a file in the image\n"
662 " This is used to edit a file.\n"
664 " It is the equivalent of (and is implemented by)\n"
665 " running \"cat\", editing locally, and then \"write-file\".\n"
667 " Normally it uses $EDITOR, but if you use the aliases\n"
668 " \"vi\" or \"emacs\" you will get those editors.\n"
670 " NOTE: This will not work reliably for large files\n"
671 " (> 2 MB) or binary files containing \\0 bytes.\n"));
672 else if (strcasecmp (cmd, "lcd") == 0)
673 printf (_("lcd - local change directory\n"
676 " Change guestfish's current directory. This command is\n"
677 " useful if you want to download files to a particular\n"
679 else if (strcasecmp (cmd, "help") == 0)
680 printf (_("help - display a list of commands or help on a command\n"
683 else if (strcasecmp (cmd, "quit") == 0 ||
684 strcasecmp (cmd, "exit") == 0 ||
685 strcasecmp (cmd, "q") == 0)
686 printf (_("quit - quit guestfish\n"
689 fprintf (stderr, _("%s: command not known, use -h to list all commands\n"),
694 free_strings (char **argv)
698 for (argc = 0; argv[argc] != NULL; ++argc)
704 count_strings (char * const * const argv)
708 for (c = 0; argv[c]; ++c)
714 print_strings (char * const * const argv)
718 for (argc = 0; argv[argc] != NULL; ++argc)
719 printf ("%s\n", argv[argc]);
723 print_table (char * const * const argv)
727 for (i = 0; argv[i] != NULL; i += 2)
728 printf ("%s: %s\n", argv[i], argv[i+1]);
732 is_true (const char *str)
735 strcasecmp (str, "0") != 0 &&
736 strcasecmp (str, "f") != 0 &&
737 strcasecmp (str, "false") != 0 &&
738 strcasecmp (str, "n") != 0 &&
739 strcasecmp (str, "no") != 0;
742 /* XXX We could improve list parsing. */
744 parse_string_list (const char *str)
747 const char *p, *pend;
751 for (i = 0; str[i]; ++i)
752 if (str[i] == ' ') argc++;
754 argv = malloc (sizeof (char *) * (argc+1));
755 if (argv == NULL) { perror ("malloc"); exit (1); }
760 pend = strchrnul (p, ' ');
761 argv[i] = strndup (p, pend-p);
763 p = *pend == ' ' ? pend+1 : pend;
770 #ifdef HAVE_LIBREADLINE
771 static char histfile[1024];
772 static int nr_history_lines = 0;
776 initialize_readline (void)
778 #ifdef HAVE_LIBREADLINE
781 home = getenv ("HOME");
783 snprintf (histfile, sizeof histfile, "%s/.guestfish", home);
785 (void) read_history (histfile);
788 rl_readline_name = "guestfish";
789 rl_attempted_completion_function = do_completion;
794 cleanup_readline (void)
796 #ifdef HAVE_LIBREADLINE
799 if (histfile[0] != '\0') {
800 fd = open (histfile, O_WRONLY|O_CREAT, 0644);
807 (void) append_history (nr_history_lines, histfile);
813 add_history_line (const char *line)
815 #ifdef HAVE_LIBREADLINE