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);
261 pod2text (const char *heading, const char *str)
265 fp = popen ("pod2text", "w");
267 /* pod2text failed, maybe not found, so let's just print the
268 * source instead, since that's better than doing nothing.
270 printf ("%s\n\n%s\n", heading, str);
273 fputs ("=head1 ", fp);
281 /* List is built in reverse order, so mount them in reverse order. */
283 mount_mps (struct mp *mp)
288 mount_mps (mp->next);
290 r = guestfs_mount (g, mp->device, mp->mountpoint);
292 r = guestfs_mount_ro (g, mp->device, mp->mountpoint);
299 add_drives (struct drv *drv)
304 add_drives (drv->next);
306 r = guestfs_add_drive (g, drv->filename);
308 r = guestfs_add_drive_ro (g, drv->filename);
326 #define FISH "><fs> "
328 static char *line_read = NULL;
333 #ifdef HAVE_LIBREADLINE
341 line_read = readline (prompt ? FISH : "");
343 if (line_read && *line_read)
344 add_history_line (line_read);
349 #endif /* HAVE_LIBREADLINE */
351 static char buf[8192];
354 if (prompt) printf (FISH);
355 line_read = fgets (buf, sizeof buf, stdin);
358 len = strlen (line_read);
359 if (len > 0 && buf[len-1] == '\n') buf[len-1] = '\0';
373 int global_exit_on_error = !prompt;
378 "Welcome to guestfish, the libguestfs filesystem interactive shell for\n"
379 "editing virtual machine filesystems.\n"
381 "Type: 'help' for help with commands\n"
382 " 'quit' to quit the shell\n"
386 exit_on_error = global_exit_on_error;
388 buf = rl_gets (prompt);
394 /* Skip any initial whitespace before the command. */
396 while (*buf && isspace (*buf))
401 /* If the next character is '#' then this is a comment. */
402 if (*buf == '#') continue;
404 /* If the next character is '!' then pass the whole lot to system(3). */
412 (WTERMSIG (r) == SIGINT || WTERMSIG (r) == SIGQUIT)) ||
413 WEXITSTATUS (r) != 0)
419 /* If the next character is '-' allow the command to fail without
420 * exiting on error (just for this one command though).
428 /* Get the command (cannot be quoted). */
429 len = strcspn (buf, " \t");
431 if (len == 0) continue;
435 if (buf[len] == '\0') {
442 p += strspn (p, " \t");
444 /* Get the parameters. */
445 while (*p && i < sizeof argv / sizeof argv[0]) {
446 /* Parameters which start with quotes or square brackets
447 * are treated specially. Bare parameters are delimited
452 len = strcspn (p, "\"");
453 if (p[len] == '\0') {
454 fprintf (stderr, _("guestfish: unterminated double quote\n"));
455 if (exit_on_error) exit (1);
458 if (p[len+1] && (p[len+1] != ' ' && p[len+1] != '\t')) {
459 fprintf (stderr, _("guestfish: command arguments not separated by whitespace\n"));
460 if (exit_on_error) exit (1);
464 pend = p[len+1] ? &p[len+2] : &p[len+1];
465 } else if (*p == '\'') {
467 len = strcspn (p, "'");
468 if (p[len] == '\0') {
469 fprintf (stderr, _("guestfish: unterminated single quote\n"));
470 if (exit_on_error) exit (1);
473 if (p[len+1] && (p[len+1] != ' ' && p[len+1] != '\t')) {
474 fprintf (stderr, _("guestfish: command arguments not separated by whitespace\n"));
475 if (exit_on_error) exit (1);
479 pend = p[len+1] ? &p[len+2] : &p[len+1];
481 } else if (*p == '[') {
485 while (*pend && c != 0) {
486 if (*pend == '[') c++;
487 else if (*pend == ']') c--;
491 fprintf (stderr, _("guestfish: unterminated \"[...]\" sequence\n"));
492 if (exit_on_error) exit (1);
495 if (*pend && (*pend != ' ' && *pend != '\t')) {
496 fprintf (stderr, _("guestfish: command arguments not separated by whitespace\n"));
497 if (exit_on_error) exit (1);
502 } else if (*p != ' ' && *p != '\t') {
503 len = strcspn (p, " \t");
510 fprintf (stderr, _("guestfish: internal error parsing string at '%s'\n"),
519 p += strspn (p, " \t");
522 if (i == sizeof argv / sizeof argv[0]) {
523 fprintf (stderr, _("guestfish: too many arguments\n"));
524 if (exit_on_error) exit (1);
531 if (issue_command (cmd, argv) == -1) {
532 if (exit_on_error) exit (1);
537 if (prompt) printf ("\n");
541 cmdline (char *argv[], int optind, int argc)
546 if (optind >= argc) return;
548 cmd = argv[optind++];
549 if (strcmp (cmd, ":") == 0) {
550 fprintf (stderr, _("guestfish: empty command on command line\n"));
553 params = &argv[optind];
555 /* Search for end of command list or ":" ... */
556 while (optind < argc && strcmp (argv[optind], ":") != 0)
559 if (optind == argc) {
560 if (issue_command (cmd, params) == -1) exit (1);
563 if (issue_command (cmd, params) == -1) exit (1);
564 cmdline (argv, optind+1, argc);
569 issue_command (const char *cmd, char *argv[])
573 for (argc = 0; argv[argc] != NULL; ++argc)
576 if (strcasecmp (cmd, "help") == 0) {
580 display_command (argv[0]);
583 else if (strcasecmp (cmd, "quit") == 0 ||
584 strcasecmp (cmd, "exit") == 0 ||
585 strcasecmp (cmd, "q") == 0) {
589 else if (strcasecmp (cmd, "alloc") == 0 ||
590 strcasecmp (cmd, "allocate") == 0)
591 return do_alloc (cmd, argc, argv);
592 else if (strcasecmp (cmd, "echo") == 0)
593 return do_echo (cmd, argc, argv);
594 else if (strcasecmp (cmd, "edit") == 0 ||
595 strcasecmp (cmd, "vi") == 0 ||
596 strcasecmp (cmd, "emacs") == 0)
597 return do_edit (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"));
618 /* actions are printed after this (see list_commands) */
622 display_builtin_command (const char *cmd)
624 /* help for actions is auto-generated, see display_command */
626 if (strcasecmp (cmd, "alloc") == 0 ||
627 strcasecmp (cmd, "allocate") == 0)
628 printf (_("alloc - allocate an image\n"
629 " alloc <filename> <size>\n"
631 " This creates an empty (zeroed) file of the given size,\n"
632 " and then adds so it can be further examined.\n"
634 " For more advanced image creation, see qemu-img utility.\n"
636 " Size can be specified (where <nn> means a number):\n"
637 " <nn> number of kilobytes\n"
638 " eg: 1440 standard 3.5\" floppy\n"
639 " <nn>K or <nn>KB number of kilobytes\n"
640 " <nn>M or <nn>MB number of megabytes\n"
641 " <nn>G or <nn>GB number of gigabytes\n"
642 " <nn>sects number of 512 byte sectors\n"));
643 else if (strcasecmp (cmd, "echo") == 0)
644 printf (_("echo - display a line of text\n"
645 " echo [<params> ...]\n"
647 " This echos the parameters to the terminal.\n"));
648 else if (strcasecmp (cmd, "edit") == 0 ||
649 strcasecmp (cmd, "vi") == 0 ||
650 strcasecmp (cmd, "emacs") == 0)
651 printf (_("edit - edit a file in the image\n"
654 " This is used to edit a file.\n"
656 " It is the equivalent of (and is implemented by)\n"
657 " running \"cat\", editing locally, and then \"write-file\".\n"
659 " Normally it uses $EDITOR, but if you use the aliases\n"
660 " \"vi\" or \"emacs\" you will get those editors.\n"
662 " NOTE: This will not work reliably for large files\n"
663 " (> 2 MB) or binary files containing \\0 bytes.\n"));
664 else if (strcasecmp (cmd, "help") == 0)
665 printf (_("help - display a list of commands or help on a command\n"
668 else if (strcasecmp (cmd, "quit") == 0 ||
669 strcasecmp (cmd, "exit") == 0 ||
670 strcasecmp (cmd, "q") == 0)
671 printf (_("quit - quit guestfish\n"
674 fprintf (stderr, _("%s: command not known, use -h to list all commands\n"),
679 free_strings (char **argv)
683 for (argc = 0; argv[argc] != NULL; ++argc)
689 print_strings (char * const * const argv)
693 for (argc = 0; argv[argc] != NULL; ++argc)
694 printf ("%s\n", argv[argc]);
698 print_table (char * const * const argv)
702 for (i = 0; argv[i] != NULL; i += 2)
703 printf ("%s: %s\n", argv[i], argv[i+1]);
707 is_true (const char *str)
710 strcasecmp (str, "0") != 0 &&
711 strcasecmp (str, "f") != 0 &&
712 strcasecmp (str, "false") != 0 &&
713 strcasecmp (str, "n") != 0 &&
714 strcasecmp (str, "no") != 0;
717 /* XXX We could improve list parsing. */
719 parse_string_list (const char *str)
722 const char *p, *pend;
726 for (i = 0; str[i]; ++i)
727 if (str[i] == ' ') argc++;
729 argv = malloc (sizeof (char *) * (argc+1));
730 if (argv == NULL) { perror ("malloc"); exit (1); }
735 pend = strchrnul (p, ' ');
736 argv[i] = strndup (p, pend-p);
738 p = *pend == ' ' ? pend+1 : pend;
745 #ifdef HAVE_LIBREADLINE
746 static char histfile[1024];
747 static int nr_history_lines = 0;
751 initialize_readline (void)
753 #ifdef HAVE_LIBREADLINE
756 home = getenv ("HOME");
758 snprintf (histfile, sizeof histfile, "%s/.guestfish", home);
760 (void) read_history (histfile);
763 rl_readline_name = "guestfish";
764 rl_attempted_completion_function = do_completion;
769 cleanup_readline (void)
771 #ifdef HAVE_LIBREADLINE
774 if (histfile[0] != '\0') {
775 fd = open (histfile, O_WRONLY|O_CREAT, 0644);
782 (void) append_history (nr_history_lines, histfile);
788 add_history_line (const char *line)
790 #ifdef HAVE_LIBREADLINE