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
31 #ifdef HAVE_LIBREADLINE
32 #include <readline/readline.h>
33 #include <readline/history.h>
46 static void mount_mps (struct mp *mp);
47 static void interactive (void);
48 static void shell_script (void);
49 static void script (int prompt);
50 static void cmdline (char *argv[], int optind, int argc);
51 static int issue_command (const char *cmd, char *argv[]);
52 static void initialize_readline (void);
53 static void cleanup_readline (void);
54 static void add_history_line (const char *);
56 /* Currently open libguestfs handle. */
64 launch (guestfs_h *_g)
69 if (guestfs_launch (g) == -1)
71 if (guestfs_wait_ready (g) == -1)
82 "guestfish: guest filesystem shell\n"
83 "guestfish lets you edit virtual machine filesystems\n"
84 "Copyright (C) 2009 Red Hat Inc.\n"
86 " guestfish [--options] cmd [: cmd : cmd ...]\n"
87 "or for interactive use:\n"
89 "or from a shell script:\n"
95 " -h|--cmd-help List available commands\n"
96 " -h|--cmd-help cmd Display detailed help on 'cmd'\n"
97 " -a|--add image Add image\n"
98 " -m|--mount dev[:mnt] Mount dev on mnt (if omitted, /)\n"
99 " -n|--no-sync Don't autosync\n"
100 /*" --ro|-r All mounts are read-only\n"*/
101 " -v|--verbose Verbose messages\n"
102 "For more information, see the manpage guestfish(1).\n");
106 main (int argc, char *argv[])
108 static const char *options = "a:h::m:v?";
109 static struct option long_options[] = {
110 { "add", 1, 0, 'a' },
111 { "cmd-help", 2, 0, 'h' },
112 { "help", 0, 0, '?' },
113 { "mount", 1, 0, 'm' },
114 { "no-sync", 0, 0, 'n' },
115 { "verbose", 0, 0, 'v' },
118 struct mp *mps = NULL;
123 initialize_readline ();
125 /* guestfs_create is meant to be a lightweight operation, so
126 * it's OK to do it early here.
128 g = guestfs_create ();
130 fprintf (stderr, "guestfs_create: failed to create handle\n");
134 guestfs_set_autosync (g, 1);
136 /* If developing, add . to the path. Note that libtools interferes
137 * with this because uninstalled guestfish is a shell script that runs
138 * the real program with an absolute path. Detect that too.
140 * BUT if LIBGUESTFS_PATH environment variable is already set by
141 * the user, then don't override it.
143 if (getenv ("LIBGUESTFS_PATH") == NULL &&
145 (argv[0][0] != '/' || strstr (argv[0], "/.libs/lt-") != NULL))
146 guestfs_set_path (g, ".:" GUESTFS_DEFAULT_PATH);
149 c = getopt_long (argc, argv, options, long_options, NULL);
154 if (access (optarg, R_OK) != 0) {
158 if (guestfs_add_drive (g, optarg) == -1)
164 display_command (optarg);
165 else if (argv[optind] && argv[optind][0] != '-')
166 display_command (argv[optind++]);
172 mp = malloc (sizeof (struct mp));
177 p = strchr (optarg, ':');
180 mp->mountpoint = p+1;
182 mp->mountpoint = "/";
189 guestfs_set_autosync (g, 0);
194 guestfs_set_verbose (g, verbose);
202 fprintf (stderr, "guestfish: unexpected command line option 0x%x\n", c);
207 /* If we've got mountpoints, we must launch the guest and mount them. */
209 if (launch (g) == -1) exit (1);
213 /* Interactive, shell script, or command(s) on the command line? */
214 if (optind >= argc) {
221 cmdline (argv, optind, argc);
229 pod2text (const char *heading, const char *str)
233 fp = popen ("pod2text", "w");
235 /* pod2text failed, maybe not found, so let's just print the
236 * source instead, since that's better than doing nothing.
238 printf ("%s\n\n%s\n", heading, str);
241 fputs ("=head1 ", fp);
248 /* List is built in reverse order, so mount them in reverse order. */
250 mount_mps (struct mp *mp)
253 mount_mps (mp->next);
254 if (guestfs_mount (g, mp->device, mp->mountpoint) == -1)
271 #define FISH "><fs> "
273 static char *line_read = NULL;
278 #ifdef HAVE_LIBREADLINE
285 line_read = readline (prompt ? FISH : "");
287 if (prompt && line_read && *line_read)
288 add_history_line (line_read);
290 #else /* !HAVE_LIBREADLINE */
292 static char buf[8192];
295 if (prompt) printf (FISH);
296 line_read = fgets (buf, sizeof buf, stdin);
299 len = strlen (line_read);
300 if (len > 0 && buf[len-1] == '\n') buf[len-1] = '\0';
303 #endif /* !HAVE_LIBREADLINE */
319 "Welcome to guestfish, the libguestfs filesystem interactive shell for\n"
320 "editing virtual machine filesystems.\n"
322 "Type: 'help' for help with commands\n"
323 " 'quit' to quit the shell\n"
327 buf = rl_gets (prompt);
333 /* Skip any initial whitespace before the command. */
334 while (*buf && isspace (*buf))
337 /* Get the command (cannot be quoted). */
338 len = strcspn (buf, " \t");
340 if (len == 0) continue;
344 if (buf[len] == '\0') {
351 p += strspn (p, " \t");
353 /* Get the parameters. */
354 while (*p && i < sizeof argv / sizeof argv[0]) {
355 /* Parameters which start with quotes or square brackets
356 * are treated specially. Bare parameters are delimited
361 len = strcspn (p, "\"");
362 if (p[len] == '\0') {
363 fprintf (stderr, "guestfish: unterminated double quote\n");
364 if (!prompt) exit (1);
367 if (p[len+1] && (p[len+1] != ' ' && p[len+1] != '\t')) {
368 fprintf (stderr, "guestfish: command arguments not separated by whitespace\n");
369 if (!prompt) exit (1);
373 pend = p[len+1] ? &p[len+2] : &p[len+1];
374 } else if (*p == '\'') {
376 len = strcspn (p, "'");
377 if (p[len] == '\0') {
378 fprintf (stderr, "guestfish: unterminated single quote\n");
379 if (!prompt) exit (1);
382 if (p[len+1] && (p[len+1] != ' ' && p[len+1] != '\t')) {
383 fprintf (stderr, "guestfish: command arguments not separated by whitespace\n");
384 if (!prompt) exit (1);
388 pend = p[len+1] ? &p[len+2] : &p[len+1];
390 } else if (*p == '[') {
394 while (*pend && c != 0) {
395 if (*pend == '[') c++;
396 else if (*pend == ']') c--;
400 fprintf (stderr, "guestfish: unterminated \"[...]\" sequence\n");
401 if (!prompt) exit (1);
404 if (*pend && (*pend != ' ' && *pend != '\t')) {
405 fprintf (stderr, "guestfish: command arguments not separated by whitespace\n");
406 if (!prompt) exit (1);
411 } else if (*p != ' ' && *p != '\t') {
412 len = strcspn (p, " \t");
419 fprintf (stderr, "guestfish: internal error parsing string at '%s'\n",
428 p += strspn (p, " \t");
431 if (i == sizeof argv / sizeof argv[0]) {
432 fprintf (stderr, "guestfish: too many arguments\n");
433 if (!prompt) exit (1);
440 if (issue_command (cmd, argv) == -1) {
441 if (!prompt) exit (1);
446 if (prompt) printf ("\n");
450 cmdline (char *argv[], int optind, int argc)
455 if (optind >= argc) return;
457 cmd = argv[optind++];
458 if (strcmp (cmd, ":") == 0) {
459 fprintf (stderr, "guestfish: empty command on command line\n");
462 params = &argv[optind];
464 /* Search for end of command list or ":" ... */
465 while (optind < argc && strcmp (argv[optind], ":") != 0)
468 if (optind == argc) {
469 if (issue_command (cmd, params) == -1) exit (1);
472 if (issue_command (cmd, params) == -1) exit (1);
473 cmdline (argv, optind+1, argc);
478 issue_command (const char *cmd, char *argv[])
482 for (argc = 0; argv[argc] != NULL; ++argc)
485 if (strcasecmp (cmd, "help") == 0) {
489 display_command (argv[0]);
492 else if (strcasecmp (cmd, "quit") == 0 ||
493 strcasecmp (cmd, "exit") == 0 ||
494 strcasecmp (cmd, "q") == 0) {
498 else if (strcasecmp (cmd, "alloc") == 0 ||
499 strcasecmp (cmd, "allocate") == 0)
500 return do_alloc (cmd, argc, argv);
501 else if (strcasecmp (cmd, "edit") == 0 ||
502 strcasecmp (cmd, "vi") == 0 ||
503 strcasecmp (cmd, "emacs") == 0)
504 return do_edit (cmd, argc, argv);
506 return run_action (cmd, argc, argv);
510 list_builtin_commands (void)
512 /* help and quit should appear at the top */
513 printf ("%-20s %s\n",
514 "help", "display a list of commands or help on a command");
515 printf ("%-20s %s\n",
516 "quit", "quit guestfish");
518 printf ("%-20s %s\n",
519 "alloc", "allocate an image");
520 printf ("%-20s %s\n",
521 "edit", "edit a file in the image");
523 /* actions are printed after this (see list_commands) */
527 display_builtin_command (const char *cmd)
529 /* help for actions is auto-generated, see display_command */
531 if (strcasecmp (cmd, "alloc") == 0 ||
532 strcasecmp (cmd, "allocate") == 0)
533 printf ("alloc - allocate an image\n"
534 " alloc <filename> <size>\n"
536 " This creates an empty (zeroed) file of the given size,\n"
537 " and then adds so it can be further examined.\n"
539 " For more advanced image creation, see qemu-img utility.\n"
541 " Size can be specified (where <nn> means a number):\n"
542 " <nn> number of kilobytes\n"
543 " eg: 1440 standard 3.5\" floppy\n"
544 " <nn>K or <nn>KB number of kilobytes\n"
545 " <nn>M or <nn>MB number of megabytes\n"
546 " <nn>G or <nn>GB number of gigabytes\n"
547 " <nn>sects number of 512 byte sectors\n");
548 else if (strcasecmp (cmd, "edit") == 0 ||
549 strcasecmp (cmd, "vi") == 0 ||
550 strcasecmp (cmd, "emacs") == 0)
551 printf ("edit - edit a file in the image\n"
554 " This is used to edit a file.\n"
556 " It is the equivalent of (and is implemented by)\n"
557 " running \"cat\", editing locally, and then \"write-file\".\n"
559 " Normally it uses $EDITOR, but if you use the aliases\n"
560 " \"vi\" or \"emacs\" you will get those editors.\n"
562 " NOTE: This will not work reliably for large files\n"
563 " (> 2 MB) or binary files containing \\0 bytes.\n");
564 else if (strcasecmp (cmd, "help") == 0)
565 printf ("help - display a list of commands or help on a command\n"
568 else if (strcasecmp (cmd, "quit") == 0 ||
569 strcasecmp (cmd, "exit") == 0 ||
570 strcasecmp (cmd, "q") == 0)
571 printf ("quit - quit guestfish\n"
574 fprintf (stderr, "%s: command not known, use -h to list all commands\n",
579 free_strings (char **argv)
583 for (argc = 0; argv[argc] != NULL; ++argc)
589 print_strings (char * const * const argv)
593 for (argc = 0; argv[argc] != NULL; ++argc)
594 printf ("%s\n", argv[argc]);
598 print_table (char * const * const argv)
602 for (i = 0; argv[i] != NULL; i += 2)
603 printf ("%s: %s\n", argv[i], argv[i+1]);
607 is_true (const char *str)
610 strcasecmp (str, "0") != 0 &&
611 strcasecmp (str, "f") != 0 &&
612 strcasecmp (str, "false") != 0 &&
613 strcasecmp (str, "n") != 0 &&
614 strcasecmp (str, "no") != 0;
617 /* XXX We could improve list parsing. */
619 parse_string_list (const char *str)
622 const char *p, *pend;
626 for (i = 0; str[i]; ++i)
627 if (str[i] == ' ') argc++;
629 argv = malloc (sizeof (char *) * (argc+1));
630 if (argv == NULL) { perror ("malloc"); exit (1); }
635 pend = strchrnul (p, ' ');
636 argv[i] = strndup (p, pend-p);
638 p = *pend == ' ' ? pend+1 : pend;
645 #ifdef HAVE_LIBREADLINE
646 static char histfile[1024];
647 static int nr_history_lines = 0;
651 initialize_readline (void)
653 #ifdef HAVE_LIBREADLINE
656 home = getenv ("HOME");
658 snprintf (histfile, sizeof histfile, "%s/.guestfish", home);
660 (void) read_history (histfile);
663 rl_readline_name = "guestfish";
664 rl_attempted_completion_function = do_completion;
669 cleanup_readline (void)
671 #ifdef HAVE_LIBREADLINE
674 if (histfile[0] != '\0') {
675 fd = open (histfile, O_WRONLY|O_CREAT, 0644);
682 (void) append_history (nr_history_lines, histfile);
688 add_history_line (const char *line)
690 #ifdef HAVE_LIBREADLINE