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.
30 #include <sys/types.h>
34 #ifdef HAVE_LIBREADLINE
35 #include <readline/readline.h>
36 #include <readline/history.h>
57 static void add_drives (struct drv *drv);
58 static void mount_mps (struct mp *mp);
59 static void interactive (void);
60 static void shell_script (void);
61 static void script (int prompt);
62 static void cmdline (char *argv[], int optind, int argc);
63 static void initialize_readline (void);
64 static void cleanup_readline (void);
65 #ifdef HAVE_LIBREADLINE
66 static void add_history_line (const char *);
69 /* Currently open libguestfs handle. */
75 int remote_control_listen = 0;
76 int remote_control = 0;
77 int exit_on_error = 1;
81 launch (guestfs_h *_g)
85 if (guestfs_is_config (g)) {
86 if (guestfs_launch (g) == -1)
92 static void __attribute__((noreturn))
95 if (status != EXIT_SUCCESS)
96 fprintf (stderr, _("Try `%s --help' for more information.\n"),
100 _("%s: guest filesystem shell\n"
101 "%s lets you edit virtual machine filesystems\n"
102 "Copyright (C) 2009-2010 Red Hat Inc.\n"
104 " %s [--options] cmd [: cmd : cmd ...]\n"
105 " %s -i libvirt-domain\n"
106 " %s -i disk-image(s)\n"
107 "or for interactive use:\n"
109 "or from a shell script:\n"
115 " -h|--cmd-help List available commands\n"
116 " -h|--cmd-help cmd Display detailed help on 'cmd'\n"
117 " -a|--add image Add image\n"
118 " -D|--no-dest-paths Don't tab-complete paths from guest fs\n"
119 " -f|--file file Read commands from file\n"
120 " -i|--inspector Run virt-inspector to get disk mountpoints\n"
121 " --listen Listen for remote commands\n"
122 " -m|--mount dev[:mnt] Mount dev on mnt (if omitted, /)\n"
123 " -n|--no-sync Don't autosync\n"
124 " --remote[=pid] Send commands to remote %s\n"
125 " -r|--ro Mount read-only\n"
126 " --selinux Enable SELinux support\n"
127 " -v|--verbose Verbose messages\n"
128 " -x Echo each command before executing it\n"
129 " -V|--version Display version and exit\n"
130 "For more information, see the manpage %s(1).\n"),
131 program_name, program_name, program_name,
132 program_name, program_name, program_name,
133 program_name, program_name, program_name);
139 main (int argc, char *argv[])
141 /* Set global program name that is not polluted with libtool artifacts. */
142 set_program_name (argv[0]);
144 atexit (close_stdout);
146 setlocale (LC_ALL, "");
147 bindtextdomain (PACKAGE, LOCALEBASEDIR);
148 textdomain (PACKAGE);
150 enum { HELP_OPTION = CHAR_MAX + 1 };
152 static const char *options = "a:Df:h::im:nrv?Vx";
153 static const struct option long_options[] = {
154 { "add", 1, 0, 'a' },
155 { "cmd-help", 2, 0, 'h' },
156 { "file", 1, 0, 'f' },
157 { "help", 0, 0, HELP_OPTION },
158 { "inspector", 0, 0, 'i' },
159 { "listen", 0, 0, 0 },
160 { "mount", 1, 0, 'm' },
161 { "no-dest-paths", 0, 0, 'D' },
162 { "no-sync", 0, 0, 'n' },
163 { "remote", 2, 0, 0 },
165 { "selinux", 0, 0, 0 },
166 { "verbose", 0, 0, 'v' },
167 { "version", 0, 0, 'V' },
170 struct drv *drvs = NULL;
172 struct mp *mps = NULL;
174 char *p, *file = NULL;
180 initialize_readline ();
182 memset (&sa, 0, sizeof sa);
183 sa.sa_handler = SIG_IGN;
184 sa.sa_flags = SA_RESTART;
185 sigaction (SIGPIPE, &sa, NULL);
187 /* guestfs_create is meant to be a lightweight operation, so
188 * it's OK to do it early here.
190 g = guestfs_create ();
192 fprintf (stderr, _("guestfs_create: failed to create handle\n"));
196 guestfs_set_autosync (g, 1);
198 /* If developing, add ./appliance to the path. Note that libtools
199 * interferes with this because uninstalled guestfish is a shell
200 * script that runs the real program with an absolute path. Detect
203 * BUT if LIBGUESTFS_PATH environment variable is already set by
204 * the user, then don't override it.
206 if (getenv ("LIBGUESTFS_PATH") == NULL &&
208 (argv[0][0] != '/' || strstr (argv[0], "/.libs/lt-") != NULL))
209 guestfs_set_path (g, "appliance:" GUESTFS_DEFAULT_PATH);
211 /* CAUTION: we are careful to modify argv[0] here, only after
212 * using it just above.
214 * getopt_long uses argv[0], so give it the sanitized name. Save a copy
215 * of the original, in case it's needed in virt-inspector mode, below.
217 char *real_argv0 = argv[0];
218 argv[0] = bad_cast (program_name);
221 c = getopt_long (argc, argv, options, long_options, &option_index);
225 case 0: /* options which are long only */
226 if (STREQ (long_options[option_index].name, "listen"))
227 remote_control_listen = 1;
228 else if (STREQ (long_options[option_index].name, "remote")) {
230 if (sscanf (optarg, "%d", &remote_control) != 1) {
231 fprintf (stderr, _("%s: --listen=PID: PID was not a number: %s\n"),
232 program_name, optarg);
236 p = getenv ("GUESTFISH_PID");
237 if (!p || sscanf (p, "%d", &remote_control) != 1) {
238 fprintf (stderr, _("%s: remote: $GUESTFISH_PID must be set"
239 " to the PID of the remote process\n"),
244 } else if (STREQ (long_options[option_index].name, "selinux")) {
245 guestfs_set_selinux (g, 1);
247 fprintf (stderr, _("%s: unknown long option: %s (%d)\n"),
248 program_name, long_options[option_index].name, option_index);
254 if (access (optarg, R_OK) != 0) {
258 drv = malloc (sizeof (struct drv));
263 drv->filename = optarg;
269 complete_dest_paths = 0;
274 fprintf (stderr, _("%s: only one -f parameter can be given\n"),
285 r = display_command (optarg);
286 else if (argv[optind] && argv[optind][0] != '-')
287 r = display_command (argv[optind++]);
291 exit (r == 0 ? EXIT_SUCCESS : EXIT_FAILURE);
299 mp = malloc (sizeof (struct mp));
304 p = strchr (optarg, ':');
307 mp->mountpoint = p+1;
309 mp->mountpoint = bad_cast ("/");
316 guestfs_set_autosync (g, 0);
325 guestfs_set_verbose (g, verbose);
329 struct guestfs_version *v = guestfs_version (g);
330 printf ("%s %"PRIi64".%"PRIi64".%"PRIi64"%s\n", program_name,
331 v->major, v->minor, v->release, v->extra);
336 guestfs_set_trace (g, 1);
340 usage (EXIT_SUCCESS);
343 usage (EXIT_FAILURE);
347 /* Inspector mode invalidates most of the other arguments. */
352 if (drvs || mps || remote_control_listen || remote_control ||
353 guestfs_get_selinux (g)) {
354 fprintf (stderr, _("%s: cannot use -i option with -a, -m,"
355 " --listen, --remote or --selinux\n"),
359 if (optind >= argc) {
361 _("%s: -i requires a libvirt domain or path(s) to disk image(s)\n"),
366 strcpy (cmd, "a=`virt-inspector");
367 while (optind < argc) {
368 if (strlen (cmd) + strlen (argv[optind]) + strlen (real_argv0) + 60
371 _("%s: virt-inspector command too long for fixed-size buffer\n"),
376 strcat (cmd, argv[optind]);
382 strcat (cmd, " --ro-fish");
384 strcat (cmd, " --fish");
386 sprintf (&cmd[strlen(cmd)], "` && %s $a", real_argv0);
388 if (guestfs_get_verbose (g))
390 if (!guestfs_get_autosync (g))
392 if (guestfs_get_trace (g))
397 "%s -i: running virt-inspector command:\n%s\n", program_name, cmd);
404 exit (WEXITSTATUS (r));
407 /* If we've got drives to add, add them now. */
410 /* If we've got mountpoints, we must launch the guest and mount them. */
412 /* RHBZ#612178: If --listen flag is given, then we will fork into
413 * the background in rc_listen(). However you can't do this while
414 * holding a libguestfs handle open because the recovery process
415 * will think the main program has died and kill qemu. Therefore
416 * don't use the recovery process for this case. (A better
417 * solution would be to call launch () etc after the fork, but
418 * that greatly complicates the code here).
420 if (remote_control_listen)
421 guestfs_set_recovery_proc (g, 0);
423 if (launch (g) == -1) exit (EXIT_FAILURE);
427 /* Remote control? */
428 if (remote_control_listen && remote_control) {
430 _("%s: cannot use --listen and --remote options at the same time\n"),
435 if (remote_control_listen) {
438 _("%s: extra parameters on the command line with --listen flag\n"),
444 _("%s: cannot use --listen and --file options at the same time\n"),
451 /* -f (file) parameter? */
454 if (open (file, O_RDONLY) == -1) {
460 /* Interactive, shell script, or command(s) on the command line? */
461 if (optind >= argc) {
468 cmdline (argv, optind, argc);
476 pod2text (const char *name, const char *shortdesc, const char *str)
480 fp = popen ("pod2text", "w");
482 /* pod2text failed, maybe not found, so let's just print the
483 * source instead, since that's better than doing nothing.
485 printf ("%s - %s\n\n%s\n", name, shortdesc, str);
488 fprintf (fp, "=head1 NAME\n\n%s - %s\n\n", name, shortdesc);
493 /* List is built in reverse order, so mount them in reverse order. */
495 mount_mps (struct mp *mp)
500 mount_mps (mp->next);
502 /* Don't use guestfs_mount here because that will default to mount
503 * options -o sync,noatime. For more information, see guestfs(3)
504 * section "LIBGUESTFS GOTCHAS".
506 const char *options = read_only ? "ro" : "";
507 r = guestfs_mount_options (g, options, mp->device, mp->mountpoint);
514 add_drives (struct drv *drv)
519 add_drives (drv->next);
521 r = guestfs_add_drive (g, drv->filename);
523 r = guestfs_add_drive_ro (g, drv->filename);
541 #define FISH "><fs> "
543 static char *line_read = NULL;
548 #ifdef HAVE_LIBREADLINE
556 line_read = readline (prompt ? FISH : "");
558 if (line_read && *line_read)
559 add_history_line (line_read);
564 #endif /* HAVE_LIBREADLINE */
566 static char buf[8192];
569 if (prompt) printf (FISH);
570 line_read = fgets (buf, sizeof buf, stdin);
573 len = strlen (line_read);
574 if (len > 0 && buf[len-1] == '\n') buf[len-1] = '\0';
588 int global_exit_on_error = !prompt;
593 "Welcome to guestfish, the libguestfs filesystem interactive shell for\n"
594 "editing virtual machine filesystems.\n"
596 "Type: 'help' for help with commands\n"
597 " 'quit' to quit the shell\n"
603 exit_on_error = global_exit_on_error;
605 buf = rl_gets (prompt);
611 /* Skip any initial whitespace before the command. */
613 while (*buf && c_isspace (*buf))
618 /* If the next character is '#' then this is a comment. */
619 if (*buf == '#') continue;
621 /* If the next character is '!' then pass the whole lot to system(3). */
629 (WTERMSIG (r) == SIGINT || WTERMSIG (r) == SIGQUIT)) ||
630 WEXITSTATUS (r) != 0)
636 /* If the next character is '-' allow the command to fail without
637 * exiting on error (just for this one command though).
645 /* Get the command (cannot be quoted). */
646 len = strcspn (buf, " \t");
648 if (len == 0) continue;
652 if (buf[len] == '\0') {
659 p += strspn (p, " \t");
661 /* Get the parameters. */
662 while (*p && i < sizeof argv / sizeof argv[0]) {
665 /* Parameters which start with quotes or pipes are treated
666 * specially. Bare parameters are delimited by whitespace.
670 len = strcspn (p, "\"");
671 if (p[len] == '\0') {
672 fprintf (stderr, _("%s: unterminated double quote\n"), program_name);
673 if (exit_on_error) exit (EXIT_FAILURE);
676 if (p[len+1] && (p[len+1] != ' ' && p[len+1] != '\t')) {
678 _("%s: command arguments not separated by whitespace\n"),
680 if (exit_on_error) exit (EXIT_FAILURE);
684 pend = p[len+1] ? &p[len+2] : &p[len+1];
685 } else if (*p == '\'') {
687 len = strcspn (p, "'");
688 if (p[len] == '\0') {
689 fprintf (stderr, _("%s: unterminated single quote\n"), program_name);
690 if (exit_on_error) exit (EXIT_FAILURE);
693 if (p[len+1] && (p[len+1] != ' ' && p[len+1] != '\t')) {
695 _("%s: command arguments not separated by whitespace\n"),
697 if (exit_on_error) exit (EXIT_FAILURE);
701 pend = p[len+1] ? &p[len+2] : &p[len+1];
702 } else if (*p == '|') {
707 } else if (*p == '[') {
711 while (*pend && c != 0) {
712 if (*pend == '[') c++;
713 else if (*pend == ']') c--;
718 _("%s: unterminated \"[...]\" sequence\n"), program_name);
719 if (exit_on_error) exit (EXIT_FAILURE);
722 if (*pend && (*pend != ' ' && *pend != '\t')) {
724 _("%s: command arguments not separated by whitespace\n"),
726 if (exit_on_error) exit (EXIT_FAILURE);
731 } else if (*p != ' ' && *p != '\t') {
732 /* If the first character is a ~ then note that this parameter
733 * is a candidate for ~username expansion. NB this does not
734 * apply to quoted parameters.
736 tilde_candidate = *p == '~';
737 len = strcspn (p, " \t");
744 fprintf (stderr, _("%s: internal error parsing string at '%s'\n"),
749 if (!tilde_candidate)
752 argv[i] = try_tilde_expansion (p);
757 p += strspn (p, " \t");
760 if (i == sizeof argv / sizeof argv[0]) {
761 fprintf (stderr, _("%s: too many arguments\n"), program_name);
762 if (exit_on_error) exit (EXIT_FAILURE);
769 if (issue_command (cmd, argv, pipe) == -1) {
770 if (exit_on_error) exit (EXIT_FAILURE);
775 if (prompt) printf ("\n");
779 cmdline (char *argv[], int optind, int argc)
786 if (optind >= argc) return;
788 cmd = argv[optind++];
789 if (STREQ (cmd, ":")) {
790 fprintf (stderr, _("%s: empty command on command line\n"), program_name);
794 /* Allow -cmd on the command line to mean (temporarily) override
795 * the normal exit on error (RHBZ#578407).
802 params = &argv[optind];
804 /* Search for end of command list or ":" ... */
805 while (optind < argc && STRNEQ (argv[optind], ":"))
808 if (optind == argc) {
809 if (issue_command (cmd, params, NULL) == -1 && exit_on_error)
813 if (issue_command (cmd, params, NULL) == -1 && exit_on_error)
815 cmdline (argv, optind+1, argc);
820 issue_command (const char *cmd, char *argv[], const char *pipecmd)
823 int stdout_saved_fd = -1;
827 /* This counts the commands issued, starting at 1. */
830 /* For | ... commands. Annoyingly we can't use popen(3) here. */
834 if (fflush (stdout) == EOF) {
835 perror ("failed to flush standard output");
839 perror ("pipe failed");
848 if (pid == 0) { /* Child process. */
850 if (dup2 (fd[0], 0) < 0) {
851 perror ("dup2 of stdin failed");
855 r = system (pipecmd);
860 _exit (WEXITSTATUS (r));
863 if ((stdout_saved_fd = dup (1)) < 0) {
864 perror ("failed to dup stdout");
868 if (dup2 (fd[1], 1) < 0) {
869 perror ("failed to dup stdout");
870 close (stdout_saved_fd);
876 for (argc = 0; argv[argc] != NULL; ++argc)
879 /* If --remote was set, then send this command to a remote process. */
881 r = rc_remote (remote_control, cmd, argc, argv, exit_on_error);
883 /* Otherwise execute it locally. */
884 else if (STRCASEEQ (cmd, "help")) {
889 r = display_command (argv[0]);
891 else if (STRCASEEQ (cmd, "quit") ||
892 STRCASEEQ (cmd, "exit") ||
893 STRCASEEQ (cmd, "q")) {
897 else if (STRCASEEQ (cmd, "alloc") ||
898 STRCASEEQ (cmd, "allocate"))
899 r = do_alloc (cmd, argc, argv);
900 else if (STRCASEEQ (cmd, "echo"))
901 r = do_echo (cmd, argc, argv);
902 else if (STRCASEEQ (cmd, "edit") ||
903 STRCASEEQ (cmd, "vi") ||
904 STRCASEEQ (cmd, "emacs"))
905 r = do_edit (cmd, argc, argv);
906 else if (STRCASEEQ (cmd, "lcd"))
907 r = do_lcd (cmd, argc, argv);
908 else if (STRCASEEQ (cmd, "glob"))
909 r = do_glob (cmd, argc, argv);
910 else if (STRCASEEQ (cmd, "more") ||
911 STRCASEEQ (cmd, "less"))
912 r = do_more (cmd, argc, argv);
913 else if (STRCASEEQ (cmd, "reopen"))
914 r = do_reopen (cmd, argc, argv);
915 else if (STRCASEEQ (cmd, "sparse"))
916 r = do_sparse (cmd, argc, argv);
917 else if (STRCASEEQ (cmd, "time"))
918 r = do_time (cmd, argc, argv);
920 r = run_action (cmd, argc, argv);
922 /* Always flush stdout after every command, so that messages, results
923 * etc appear immediately.
925 if (fflush (stdout) == EOF) {
926 perror ("failed to flush standard output");
932 if (dup2 (stdout_saved_fd, 1) < 0) {
933 perror ("failed to dup2 standard output");
936 close (stdout_saved_fd);
937 if (waitpid (pid, NULL, 0) < 0) {
938 perror ("waiting for command to complete");
947 list_builtin_commands (void)
949 /* help and quit should appear at the top */
950 printf ("%-20s %s\n",
951 "help", _("display a list of commands or help on a command"));
952 printf ("%-20s %s\n",
953 "quit", _("quit guestfish"));
955 printf ("%-20s %s\n",
956 "alloc", _("allocate an image"));
957 printf ("%-20s %s\n",
958 "echo", _("display a line of text"));
959 printf ("%-20s %s\n",
960 "edit", _("edit a file in the image"));
961 printf ("%-20s %s\n",
962 "lcd", _("local change directory"));
963 printf ("%-20s %s\n",
964 "glob", _("expand wildcards in command"));
965 printf ("%-20s %s\n",
966 "more", _("view a file in the pager"));
967 printf ("%-20s %s\n",
968 "reopen", _("close and reopen libguestfs handle"));
969 printf ("%-20s %s\n",
970 "sparse", _("allocate a sparse image file"));
971 printf ("%-20s %s\n",
972 "time", _("measure time taken to run command"));
974 /* actions are printed after this (see list_commands) */
978 display_builtin_command (const char *cmd)
980 /* help for actions is auto-generated, see display_command */
982 if (STRCASEEQ (cmd, "alloc") ||
983 STRCASEEQ (cmd, "allocate")) {
984 printf (_("alloc - allocate an image\n"
985 " alloc <filename> <size>\n"
987 " This creates an empty (zeroed) file of the given size,\n"
988 " and then adds so it can be further examined.\n"
990 " For more advanced image creation, see qemu-img utility.\n"
992 " Size can be specified (where <nn> means a number):\n"
993 " <nn> number of kilobytes\n"
994 " eg: 1440 standard 3.5\" floppy\n"
995 " <nn>K or <nn>KB number of kilobytes\n"
996 " <nn>M or <nn>MB number of megabytes\n"
997 " <nn>G or <nn>GB number of gigabytes\n"
998 " <nn>T or <nn>TB number of terabytes\n"
999 " <nn>P or <nn>PB number of petabytes\n"
1000 " <nn>E or <nn>EB number of exabytes\n"
1001 " <nn>sects number of 512 byte sectors\n"));
1004 else if (STRCASEEQ (cmd, "echo")) {
1005 printf (_("echo - display a line of text\n"
1006 " echo [<params> ...]\n"
1008 " This echos the parameters to the terminal.\n"));
1011 else if (STRCASEEQ (cmd, "edit") ||
1012 STRCASEEQ (cmd, "vi") ||
1013 STRCASEEQ (cmd, "emacs")) {
1014 printf (_("edit - edit a file in the image\n"
1015 " edit <filename>\n"
1017 " This is used to edit a file.\n"
1019 " It is the equivalent of (and is implemented by)\n"
1020 " running \"cat\", editing locally, and then \"write-file\".\n"
1022 " Normally it uses $EDITOR, but if you use the aliases\n"
1023 " \"vi\" or \"emacs\" you will get those editors.\n"
1025 " NOTE: This will not work reliably for large files\n"
1026 " (> 2 MB) or binary files containing \\0 bytes.\n"));
1029 else if (STRCASEEQ (cmd, "lcd")) {
1030 printf (_("lcd - local change directory\n"
1031 " lcd <directory>\n"
1033 " Change guestfish's current directory. This command is\n"
1034 " useful if you want to download files to a particular\n"
1038 else if (STRCASEEQ (cmd, "glob")) {
1039 printf (_("glob - expand wildcards in command\n"
1040 " glob <command> [<args> ...]\n"
1042 " Glob runs <command> with wildcards expanded in any\n"
1043 " command args. Note that the command is run repeatedly\n"
1044 " once for each expanded argument.\n"));
1047 else if (STRCASEEQ (cmd, "help")) {
1048 printf (_("help - display a list of commands or help on a command\n"
1053 else if (STRCASEEQ (cmd, "more") ||
1054 STRCASEEQ (cmd, "less")) {
1055 printf (_("more - view a file in the pager\n"
1056 " more <filename>\n"
1058 " This is used to view a file in the pager.\n"
1060 " It is the equivalent of (and is implemented by)\n"
1061 " running \"cat\" and using the pager.\n"
1063 " Normally it uses $PAGER, but if you use the alias\n"
1064 " \"less\" then it always uses \"less\".\n"
1066 " NOTE: This will not work reliably for large files\n"
1067 " (> 2 MB) or binary files containing \\0 bytes.\n"));
1070 else if (STRCASEEQ (cmd, "quit") ||
1071 STRCASEEQ (cmd, "exit") ||
1072 STRCASEEQ (cmd, "q")) {
1073 printf (_("quit - quit guestfish\n"
1077 else if (STRCASEEQ (cmd, "reopen")) {
1078 printf (_("reopen - close and reopen the libguestfs handle\n"
1081 "Close and reopen the libguestfs handle. It is not necessary to use\n"
1082 "this normally, because the handle is closed properly when guestfish\n"
1083 "exits. However this is occasionally useful for testing.\n"));
1086 else if (STRCASEEQ (cmd, "sparse")) {
1087 printf (_("sparse - allocate a sparse image file\n"
1088 " sparse <filename> <size>\n"
1090 " This creates an empty sparse file of the given size,\n"
1091 " and then adds so it can be further examined.\n"
1093 " In all respects it works the same as the 'alloc'\n"
1094 " command, except that the image file is allocated\n"
1095 " sparsely, which means that disk blocks are not assigned\n"
1096 " to the file until they are needed. Sparse disk files\n"
1097 " only use space when written to, but they are slower\n"
1098 " and there is a danger you could run out of real disk\n"
1099 " space during a write operation.\n"
1101 " For more advanced image creation, see qemu-img utility.\n"
1103 " Size can be specified (where <nn> means a number):\n"
1104 " <nn> number of kilobytes\n"
1105 " eg: 1440 standard 3.5\" floppy\n"
1106 " <nn>K or <nn>KB number of kilobytes\n"
1107 " <nn>M or <nn>MB number of megabytes\n"
1108 " <nn>G or <nn>GB number of gigabytes\n"
1109 " <nn>T or <nn>TB number of terabytes\n"
1110 " <nn>P or <nn>PB number of petabytes\n"
1111 " <nn>E or <nn>EB number of exabytes\n"
1112 " <nn>sects number of 512 byte sectors\n"));
1115 else if (STRCASEEQ (cmd, "time")) {
1116 printf (_("time - measure time taken to run command\n"
1117 " time <command> [<args> ...]\n"
1119 " This runs <command> as usual, and prints the elapsed\n"
1120 " time afterwards.\n"));
1124 fprintf (stderr, _("%s: command not known, use -h to list all commands\n"),
1130 /* This is printed when the user types in an unknown command for the
1131 * first command issued. A common case is the user doing:
1132 * guestfish disk.img
1133 * expecting guestfish to open 'disk.img' (in fact, this tried to
1134 * run a command 'disk.img').
1137 extended_help_message (void)
1140 _("Did you mean to open a disk image? guestfish -a disk.img\n"
1141 "For a list of commands: guestfish -h\n"
1142 "For complete documentation: man guestfish\n"));
1146 free_strings (char **argv)
1150 for (argc = 0; argv[argc] != NULL; ++argc)
1156 count_strings (char *const *argv)
1160 for (c = 0; argv[c]; ++c)
1166 print_strings (char *const *argv)
1170 for (argc = 0; argv[argc] != NULL; ++argc)
1171 printf ("%s\n", argv[argc]);
1175 print_table (char *const *argv)
1179 for (i = 0; argv[i] != NULL; i += 2)
1180 printf ("%s: %s\n", argv[i], argv[i+1]);
1184 is_true (const char *str)
1187 STRCASENEQ (str, "0") &&
1188 STRCASENEQ (str, "f") &&
1189 STRCASENEQ (str, "false") &&
1190 STRCASENEQ (str, "n") &&
1191 STRCASENEQ (str, "no");
1194 /* Free strings from a non-NULL terminated char** */
1196 free_n_strings (char **str, size_t len)
1200 for (i = 0; i < len; i++) {
1207 parse_string_list (const char *str)
1210 size_t argv_len = 0;
1212 /* Current position pointer */
1213 const char *p = str;
1215 /* Token might be simple:
1218 * 'This is a single token'
1219 * or contain embedded single-quoted sections:
1220 * This' is a sing'l'e to'ken
1222 * The latter may seem over-complicated, but it's what a normal shell does.
1223 * Not doing it risks surprising somebody.
1225 * This outer loop is over complete tokens.
1231 /* Skip leading whitespace */
1232 p += strspn (p, " \t");
1236 /* This loop is over token 'fragments'. A token can be in multiple bits if
1237 * it contains single quotes. We also treat both sides of an escaped quote
1238 * as separate fragments because we can't just copy it: we have to remove
1241 while (*p && (!c_isblank (*p) || in_quote)) {
1242 const char *end = p;
1244 /* Check if the fragment starts with a quote */
1246 /* Toggle in_quote */
1247 in_quote = !in_quote;
1249 /* Skip the quote */
1253 /* If we're in a quote, look for an end quote */
1255 end += strcspn (end, "'");
1258 /* Otherwise, look for whitespace or a quote */
1260 end += strcspn (end, " \t'");
1263 /* Grow the token to accommodate the fragment */
1264 size_t tok_end = tok_len;
1266 char *tok_new = realloc (tok, tok_len + 1);
1267 if (NULL == tok_new) {
1269 free_n_strings (argv, argv_len);
1271 exit (EXIT_FAILURE);
1275 /* Check if we stopped on an escaped quote */
1276 if ('\'' == *end && end != p && *(end-1) == '\\') {
1277 /* Add everything before \' to the token */
1278 memcpy (&tok[tok_end], p, end - p - 1);
1281 tok[tok_len-1] = '\'';
1283 /* Already processed the quote */
1288 /* Add the whole fragment */
1289 memcpy (&tok[tok_end], p, end - p);
1295 /* We've reached the end of a token. We shouldn't still be in quotes. */
1297 fprintf (stderr, _("Runaway quote in string \"%s\"\n"), str);
1299 free_n_strings (argv, argv_len);
1304 /* Add this token if there is one. There might not be if there was
1305 * whitespace at the end of the input string */
1307 /* Add the NULL terminator */
1308 tok[tok_len] = '\0';
1310 /* Add the argument to the argument list */
1312 char **argv_new = realloc (argv, sizeof (*argv) * argv_len);
1313 if (NULL == argv_new) {
1315 free_n_strings (argv, argv_len-1);
1317 exit (EXIT_FAILURE);
1321 argv[argv_len-1] = tok;
1325 /* NULL terminate the argument list */
1327 char **argv_new = realloc (argv, sizeof (*argv) * argv_len);
1328 if (NULL == argv_new) {
1330 free_n_strings (argv, argv_len-1);
1331 exit (EXIT_FAILURE);
1335 argv[argv_len-1] = NULL;
1340 #ifdef HAVE_LIBREADLINE
1341 static char histfile[1024];
1342 static int nr_history_lines = 0;
1346 initialize_readline (void)
1348 #ifdef HAVE_LIBREADLINE
1351 home = getenv ("HOME");
1353 snprintf (histfile, sizeof histfile, "%s/.guestfish", home);
1355 (void) read_history (histfile);
1358 rl_readline_name = "guestfish";
1359 rl_attempted_completion_function = do_completion;
1361 /* Note that .inputrc (or /etc/inputrc) is not read until the first
1362 * call the readline(), which happens later. Therefore, these
1363 * provide default values which can be overridden by the user if
1366 (void) rl_variable_bind ("completion-ignore-case", "on");
1371 cleanup_readline (void)
1373 #ifdef HAVE_LIBREADLINE
1376 if (histfile[0] != '\0') {
1377 fd = open (histfile, O_WRONLY|O_CREAT, 0644);
1384 #ifdef HAVE_APPEND_HISTORY
1385 (void) append_history (nr_history_lines, histfile);
1387 (void) write_history (histfile);
1393 #ifdef HAVE_LIBREADLINE
1395 add_history_line (const char *line)
1403 xwrite (int fd, const void *v_buf, size_t len)
1406 const char *buf = v_buf;
1409 r = write (fd, buf, len);
1421 /* Resolve the special "win:..." form for Windows-specific paths.
1422 * This always returns a newly allocated string which is freed by the
1423 * caller function in "cmds.c".
1426 resolve_win_path (const char *path)
1431 if (STRCASENEQLEN (path, "win:", 4)) {
1432 ret = strdup (path);
1440 /* Drop drive letter, if it's "C:". */
1441 if (STRCASEEQLEN (path, "c:", 2))
1451 ret = strdup (path);
1457 /* Blindly convert any backslashes into forward slashes. Is this good? */
1458 for (i = 0; i < strlen (ret); ++i)
1462 char *t = guestfs_case_sensitive_path (g, ret);