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 void initialize_readline (void);
60 static void cleanup_readline (void);
61 static void add_history_line (const char *);
63 /* Currently open libguestfs handle. */
71 launch (guestfs_h *_g)
75 if (guestfs_is_config (g)) {
76 if (guestfs_launch (g) == -1)
78 if (guestfs_wait_ready (g) == -1)
88 _("guestfish: guest filesystem shell\n"
89 "guestfish lets you edit virtual machine filesystems\n"
90 "Copyright (C) 2009 Red Hat Inc.\n"
92 " guestfish [--options] cmd [: cmd : cmd ...]\n"
93 "or for interactive use:\n"
95 "or from a shell script:\n"
101 " -h|--cmd-help List available commands\n"
102 " -h|--cmd-help cmd Display detailed help on 'cmd'\n"
103 " -a|--add image Add image\n"
104 " -D|--no-dest-paths Don't tab-complete paths from guest fs\n"
105 " -f|--file file Read commands from file\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:f:h::m:nrv?V";
118 static struct option long_options[] = {
119 { "add", 1, 0, 'a' },
120 { "cmd-help", 2, 0, 'h' },
121 { "file", 1, 0, 'f' },
122 { "help", 0, 0, '?' },
123 { "mount", 1, 0, 'm' },
124 { "no-dest-paths", 0, 0, 'D' },
125 { "no-sync", 0, 0, 'n' },
127 { "verbose", 0, 0, 'v' },
128 { "version", 0, 0, 'V' },
131 struct drv *drvs = NULL;
133 struct mp *mps = NULL;
135 char *p, *file = NULL;
138 initialize_readline ();
140 /* guestfs_create is meant to be a lightweight operation, so
141 * it's OK to do it early here.
143 g = guestfs_create ();
145 fprintf (stderr, _("guestfs_create: failed to create handle\n"));
149 guestfs_set_autosync (g, 1);
151 /* If developing, add ./appliance to the path. Note that libtools
152 * interferes with this because uninstalled guestfish is a shell
153 * script that runs the real program with an absolute path. Detect
156 * BUT if LIBGUESTFS_PATH environment variable is already set by
157 * the user, then don't override it.
159 if (getenv ("LIBGUESTFS_PATH") == NULL &&
161 (argv[0][0] != '/' || strstr (argv[0], "/.libs/lt-") != NULL))
162 guestfs_set_path (g, "appliance:" GUESTFS_DEFAULT_PATH);
165 c = getopt_long (argc, argv, options, long_options, NULL);
170 if (access (optarg, R_OK) != 0) {
174 drv = malloc (sizeof (struct drv));
179 drv->filename = optarg;
185 complete_dest_paths = 0;
190 fprintf (stderr, _("guestfish: only one -f parameter can be given\n"));
198 display_command (optarg);
199 else if (argv[optind] && argv[optind][0] != '-')
200 display_command (argv[optind++]);
206 mp = malloc (sizeof (struct mp));
211 p = strchr (optarg, ':');
214 mp->mountpoint = p+1;
216 mp->mountpoint = "/";
223 guestfs_set_autosync (g, 0);
232 guestfs_set_verbose (g, verbose);
236 printf ("guestfish %s\n", PACKAGE_VERSION);
244 fprintf (stderr, _("guestfish: unexpected command line option 0x%x\n"),
250 /* If we've got drives to add, add them now. */
253 /* If we've got mountpoints, we must launch the guest and mount them. */
255 if (launch (g) == -1) exit (1);
259 /* -f (file) parameter? */
262 if (open (file, O_RDONLY) == -1) {
268 /* Interactive, shell script, or command(s) on the command line? */
269 if (optind >= argc) {
276 cmdline (argv, optind, argc);
284 pod2text (const char *heading, const char *str)
288 fp = popen ("pod2text", "w");
290 /* pod2text failed, maybe not found, so let's just print the
291 * source instead, since that's better than doing nothing.
293 printf ("%s\n\n%s\n", heading, str);
296 fputs ("=head1 ", fp);
303 /* List is built in reverse order, so mount them in reverse order. */
305 mount_mps (struct mp *mp)
310 mount_mps (mp->next);
312 r = guestfs_mount (g, mp->device, mp->mountpoint);
314 r = guestfs_mount_ro (g, mp->device, mp->mountpoint);
321 add_drives (struct drv *drv)
326 add_drives (drv->next);
328 r = guestfs_add_drive (g, drv->filename);
330 r = guestfs_add_drive_ro (g, drv->filename);
348 #define FISH "><fs> "
350 static char *line_read = NULL;
355 #ifdef HAVE_LIBREADLINE
363 line_read = readline (prompt ? FISH : "");
365 if (line_read && *line_read)
366 add_history_line (line_read);
371 #endif /* HAVE_LIBREADLINE */
373 static char buf[8192];
376 if (prompt) printf (FISH);
377 line_read = fgets (buf, sizeof buf, stdin);
380 len = strlen (line_read);
381 if (len > 0 && buf[len-1] == '\n') buf[len-1] = '\0';
395 int global_exit_on_error = !prompt;
400 "Welcome to guestfish, the libguestfs filesystem interactive shell for\n"
401 "editing virtual machine filesystems.\n"
403 "Type: 'help' for help with commands\n"
404 " 'quit' to quit the shell\n"
408 exit_on_error = global_exit_on_error;
410 buf = rl_gets (prompt);
416 /* Skip any initial whitespace before the command. */
418 while (*buf && isspace (*buf))
423 /* If the next character is '#' then this is a comment. */
424 if (*buf == '#') continue;
426 /* If the next character is '!' then pass the whole lot to system(3). */
434 (WTERMSIG (r) == SIGINT || WTERMSIG (r) == SIGQUIT)) ||
435 WEXITSTATUS (r) != 0)
441 /* If the next character is '-' allow the command to fail without
442 * exiting on error (just for this one command though).
450 /* Get the command (cannot be quoted). */
451 len = strcspn (buf, " \t");
453 if (len == 0) continue;
457 if (buf[len] == '\0') {
464 p += strspn (p, " \t");
466 /* Get the parameters. */
467 while (*p && i < sizeof argv / sizeof argv[0]) {
468 /* Parameters which start with quotes or square brackets
469 * are treated specially. Bare parameters are delimited
474 len = strcspn (p, "\"");
475 if (p[len] == '\0') {
476 fprintf (stderr, _("guestfish: unterminated double quote\n"));
477 if (exit_on_error) exit (1);
480 if (p[len+1] && (p[len+1] != ' ' && p[len+1] != '\t')) {
481 fprintf (stderr, _("guestfish: command arguments not separated by whitespace\n"));
482 if (exit_on_error) exit (1);
486 pend = p[len+1] ? &p[len+2] : &p[len+1];
487 } else if (*p == '\'') {
489 len = strcspn (p, "'");
490 if (p[len] == '\0') {
491 fprintf (stderr, _("guestfish: unterminated single quote\n"));
492 if (exit_on_error) exit (1);
495 if (p[len+1] && (p[len+1] != ' ' && p[len+1] != '\t')) {
496 fprintf (stderr, _("guestfish: command arguments not separated by whitespace\n"));
497 if (exit_on_error) exit (1);
501 pend = p[len+1] ? &p[len+2] : &p[len+1];
503 } else if (*p == '[') {
507 while (*pend && c != 0) {
508 if (*pend == '[') c++;
509 else if (*pend == ']') c--;
513 fprintf (stderr, _("guestfish: unterminated \"[...]\" sequence\n"));
514 if (exit_on_error) exit (1);
517 if (*pend && (*pend != ' ' && *pend != '\t')) {
518 fprintf (stderr, _("guestfish: command arguments not separated by whitespace\n"));
519 if (exit_on_error) exit (1);
524 } else if (*p != ' ' && *p != '\t') {
525 len = strcspn (p, " \t");
532 fprintf (stderr, _("guestfish: internal error parsing string at '%s'\n"),
541 p += strspn (p, " \t");
544 if (i == sizeof argv / sizeof argv[0]) {
545 fprintf (stderr, _("guestfish: too many arguments\n"));
546 if (exit_on_error) exit (1);
553 if (issue_command (cmd, argv) == -1) {
554 if (exit_on_error) exit (1);
559 if (prompt) printf ("\n");
563 cmdline (char *argv[], int optind, int argc)
568 if (optind >= argc) return;
570 cmd = argv[optind++];
571 if (strcmp (cmd, ":") == 0) {
572 fprintf (stderr, _("guestfish: empty command on command line\n"));
575 params = &argv[optind];
577 /* Search for end of command list or ":" ... */
578 while (optind < argc && strcmp (argv[optind], ":") != 0)
581 if (optind == argc) {
582 if (issue_command (cmd, params) == -1) exit (1);
585 if (issue_command (cmd, params) == -1) exit (1);
586 cmdline (argv, optind+1, argc);
591 issue_command (const char *cmd, char *argv[])
595 for (argc = 0; argv[argc] != NULL; ++argc)
598 if (strcasecmp (cmd, "help") == 0) {
602 display_command (argv[0]);
605 else if (strcasecmp (cmd, "quit") == 0 ||
606 strcasecmp (cmd, "exit") == 0 ||
607 strcasecmp (cmd, "q") == 0) {
611 else if (strcasecmp (cmd, "alloc") == 0 ||
612 strcasecmp (cmd, "allocate") == 0)
613 return do_alloc (cmd, argc, argv);
614 else if (strcasecmp (cmd, "echo") == 0)
615 return do_echo (cmd, argc, argv);
616 else if (strcasecmp (cmd, "edit") == 0 ||
617 strcasecmp (cmd, "vi") == 0 ||
618 strcasecmp (cmd, "emacs") == 0)
619 return do_edit (cmd, argc, argv);
620 else if (strcasecmp (cmd, "lcd") == 0)
621 return do_lcd (cmd, argc, argv);
622 else if (strcasecmp (cmd, "glob") == 0)
623 return do_glob (cmd, argc, argv);
625 return run_action (cmd, argc, argv);
629 list_builtin_commands (void)
631 /* help and quit should appear at the top */
632 printf ("%-20s %s\n",
633 "help", _("display a list of commands or help on a command"));
634 printf ("%-20s %s\n",
635 "quit", _("quit guestfish"));
637 printf ("%-20s %s\n",
638 "alloc", _("allocate an image"));
639 printf ("%-20s %s\n",
640 "echo", _("display a line of text"));
641 printf ("%-20s %s\n",
642 "edit", _("edit a file in the image"));
643 printf ("%-20s %s\n",
644 "lcd", _("local change directory"));
645 printf ("%-20s %s\n",
646 "glob", _("expand wildcards in command"));
648 /* actions are printed after this (see list_commands) */
652 display_builtin_command (const char *cmd)
654 /* help for actions is auto-generated, see display_command */
656 if (strcasecmp (cmd, "alloc") == 0 ||
657 strcasecmp (cmd, "allocate") == 0)
658 printf (_("alloc - allocate an image\n"
659 " alloc <filename> <size>\n"
661 " This creates an empty (zeroed) file of the given size,\n"
662 " and then adds so it can be further examined.\n"
664 " For more advanced image creation, see qemu-img utility.\n"
666 " Size can be specified (where <nn> means a number):\n"
667 " <nn> number of kilobytes\n"
668 " eg: 1440 standard 3.5\" floppy\n"
669 " <nn>K or <nn>KB number of kilobytes\n"
670 " <nn>M or <nn>MB number of megabytes\n"
671 " <nn>G or <nn>GB number of gigabytes\n"
672 " <nn>sects number of 512 byte sectors\n"));
673 else if (strcasecmp (cmd, "echo") == 0)
674 printf (_("echo - display a line of text\n"
675 " echo [<params> ...]\n"
677 " This echos the parameters to the terminal.\n"));
678 else if (strcasecmp (cmd, "edit") == 0 ||
679 strcasecmp (cmd, "vi") == 0 ||
680 strcasecmp (cmd, "emacs") == 0)
681 printf (_("edit - edit a file in the image\n"
684 " This is used to edit a file.\n"
686 " It is the equivalent of (and is implemented by)\n"
687 " running \"cat\", editing locally, and then \"write-file\".\n"
689 " Normally it uses $EDITOR, but if you use the aliases\n"
690 " \"vi\" or \"emacs\" you will get those editors.\n"
692 " NOTE: This will not work reliably for large files\n"
693 " (> 2 MB) or binary files containing \\0 bytes.\n"));
694 else if (strcasecmp (cmd, "lcd") == 0)
695 printf (_("lcd - local change directory\n"
698 " Change guestfish's current directory. This command is\n"
699 " useful if you want to download files to a particular\n"
701 else if (strcasecmp (cmd, "glob") == 0)
702 printf (_("glob - expand wildcards in command\n"
703 " glob <command> [<args> ...]\n"
705 " Glob runs <command> with wildcards expanded in any\n"
706 " command args. Note that the command is run repeatedly\n"
707 " once for each expanded argument.\n"));
708 else if (strcasecmp (cmd, "help") == 0)
709 printf (_("help - display a list of commands or help on a command\n"
712 else if (strcasecmp (cmd, "quit") == 0 ||
713 strcasecmp (cmd, "exit") == 0 ||
714 strcasecmp (cmd, "q") == 0)
715 printf (_("quit - quit guestfish\n"
718 fprintf (stderr, _("%s: command not known, use -h to list all commands\n"),
723 free_strings (char **argv)
727 for (argc = 0; argv[argc] != NULL; ++argc)
733 count_strings (char * const * const argv)
737 for (c = 0; argv[c]; ++c)
743 print_strings (char * const * const argv)
747 for (argc = 0; argv[argc] != NULL; ++argc)
748 printf ("%s\n", argv[argc]);
752 print_table (char * const * const argv)
756 for (i = 0; argv[i] != NULL; i += 2)
757 printf ("%s: %s\n", argv[i], argv[i+1]);
761 is_true (const char *str)
764 strcasecmp (str, "0") != 0 &&
765 strcasecmp (str, "f") != 0 &&
766 strcasecmp (str, "false") != 0 &&
767 strcasecmp (str, "n") != 0 &&
768 strcasecmp (str, "no") != 0;
771 /* XXX We could improve list parsing. */
773 parse_string_list (const char *str)
776 const char *p, *pend;
780 for (i = 0; str[i]; ++i)
781 if (str[i] == ' ') argc++;
783 argv = malloc (sizeof (char *) * (argc+1));
784 if (argv == NULL) { perror ("malloc"); exit (1); }
789 pend = strchrnul (p, ' ');
790 argv[i] = strndup (p, pend-p);
792 p = *pend == ' ' ? pend+1 : pend;
799 #ifdef HAVE_LIBREADLINE
800 static char histfile[1024];
801 static int nr_history_lines = 0;
805 initialize_readline (void)
807 #ifdef HAVE_LIBREADLINE
810 home = getenv ("HOME");
812 snprintf (histfile, sizeof histfile, "%s/.guestfish", home);
814 (void) read_history (histfile);
817 rl_readline_name = "guestfish";
818 rl_attempted_completion_function = do_completion;
823 cleanup_readline (void)
825 #ifdef HAVE_LIBREADLINE
828 if (histfile[0] != '\0') {
829 fd = open (histfile, O_WRONLY|O_CREAT, 0644);
836 (void) append_history (nr_history_lines, histfile);
842 add_history_line (const char *line)
844 #ifdef HAVE_LIBREADLINE