1 /* guestfish - the filesystem interactive shell
2 * Copyright (C) 2009-2010 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>
35 #ifdef HAVE_LIBREADLINE
36 #include <readline/readline.h>
37 #include <readline/history.h>
47 /* List of drives added via -a, -d or -N options. */
50 enum { drv_a, drv_d, drv_N } type;
53 char *filename; /* disk filename */
56 char *guest; /* guest name */
59 char *filename; /* disk filename (testX.img) */
60 prep_data *data; /* prepared type */
61 char *device; /* device inside the appliance */
72 static char add_drives (struct drv *drv, char next_drive);
73 static void prepare_drives (struct drv *drv);
74 static void mount_mps (struct mp *mp);
75 static int launch (void);
76 static void interactive (void);
77 static void shell_script (void);
78 static void script (int prompt);
79 static void cmdline (char *argv[], int optind, int argc);
80 static void initialize_readline (void);
81 static void cleanup_readline (void);
82 #ifdef HAVE_LIBREADLINE
83 static void add_history_line (const char *);
86 /* Currently open libguestfs handle. */
92 int remote_control_listen = 0;
93 int remote_control = 0;
94 int exit_on_error = 1;
96 int keys_from_stdin = 0;
97 const char *libvirt_uri = NULL;
100 static void __attribute__((noreturn))
103 if (status != EXIT_SUCCESS)
104 fprintf (stderr, _("Try `%s --help' for more information.\n"),
108 _("%s: guest filesystem shell\n"
109 "%s lets you edit virtual machine filesystems\n"
110 "Copyright (C) 2009 Red Hat Inc.\n"
112 " %s [--options] cmd [: cmd : cmd ...]\n"
113 " %s -i libvirt-domain\n"
114 " %s -i disk-image(s)\n"
115 "or for interactive use:\n"
117 "or from a shell script:\n"
123 " -h|--cmd-help List available commands\n"
124 " -h|--cmd-help cmd Display detailed help on 'cmd'\n"
125 " -a|--add image Add image\n"
126 " -c|--connect uri Specify libvirt URI for -d option\n"
127 " -d|--domain guest Add disks from libvirt guest\n"
128 " -D|--no-dest-paths Don't tab-complete paths from guest fs\n"
129 " -f|--file file Read commands from file\n"
130 " -i|--inspector Automatically mount filesystems\n"
131 " --keys-from-stdin Read passphrases from stdin\n"
132 " --listen Listen for remote commands\n"
133 " -m|--mount dev[:mnt] Mount dev on mnt (if omitted, /)\n"
134 " -n|--no-sync Don't autosync\n"
135 " -N|--new type Create prepared disk (test1.img, ...)\n"
136 " --remote[=pid] Send commands to remote %s\n"
137 " -r|--ro Mount read-only\n"
138 " --selinux Enable SELinux support\n"
139 " -v|--verbose Verbose messages\n"
140 " -x Echo each command before executing it\n"
141 " -V|--version Display version and exit\n"
142 "For more information, see the manpage %s(1).\n"),
143 program_name, program_name, program_name,
144 program_name, program_name, program_name,
145 program_name, program_name, program_name);
151 main (int argc, char *argv[])
153 /* Set global program name that is not polluted with libtool artifacts. */
154 set_program_name (argv[0]);
156 atexit (close_stdout);
158 setlocale (LC_ALL, "");
159 bindtextdomain (PACKAGE, LOCALEBASEDIR);
160 textdomain (PACKAGE);
162 enum { HELP_OPTION = CHAR_MAX + 1 };
164 static const char *options = "a:c:d:Df:h::im:nN:rv?Vx";
165 static const struct option long_options[] = {
166 { "add", 1, 0, 'a' },
167 { "cmd-help", 2, 0, 'h' },
168 { "connect", 1, 0, 'c' },
169 { "domain", 1, 0, 'd' },
170 { "file", 1, 0, 'f' },
171 { "help", 0, 0, HELP_OPTION },
172 { "inspector", 0, 0, 'i' },
173 { "keys-from-stdin", 0, 0, 0 },
174 { "listen", 0, 0, 0 },
175 { "mount", 1, 0, 'm' },
176 { "new", 1, 0, 'N' },
177 { "no-dest-paths", 0, 0, 'D' },
178 { "no-sync", 0, 0, 'n' },
179 { "remote", 2, 0, 0 },
181 { "selinux", 0, 0, 0 },
182 { "verbose", 0, 0, 'v' },
183 { "version", 0, 0, 'V' },
186 struct drv *drvs = NULL;
188 struct mp *mps = NULL;
190 char *p, *file = NULL;
194 int next_prepared_drive = 1;
196 initialize_readline ();
198 memset (&sa, 0, sizeof sa);
199 sa.sa_handler = SIG_IGN;
200 sa.sa_flags = SA_RESTART;
201 sigaction (SIGPIPE, &sa, NULL);
203 /* guestfs_create is meant to be a lightweight operation, so
204 * it's OK to do it early here.
206 g = guestfs_create ();
208 fprintf (stderr, _("guestfs_create: failed to create handle\n"));
212 guestfs_set_autosync (g, 1);
214 /* If developing, add ./appliance to the path. Note that libtools
215 * interferes with this because uninstalled guestfish is a shell
216 * script that runs the real program with an absolute path. Detect
219 * BUT if LIBGUESTFS_PATH environment variable is already set by
220 * the user, then don't override it.
222 if (getenv ("LIBGUESTFS_PATH") == NULL &&
224 (argv[0][0] != '/' || strstr (argv[0], "/.libs/lt-") != NULL))
225 guestfs_set_path (g, "appliance:" GUESTFS_DEFAULT_PATH);
227 /* CAUTION: we are careful to modify argv[0] here, only after
228 * using it just above.
230 * getopt_long uses argv[0], so give it the sanitized name. Save a copy
231 * of the original, in case it's needed below.
233 char *real_argv0 = argv[0];
234 argv[0] = bad_cast (program_name);
237 c = getopt_long (argc, argv, options, long_options, &option_index);
241 case 0: /* options which are long only */
242 if (STREQ (long_options[option_index].name, "listen"))
243 remote_control_listen = 1;
244 else if (STREQ (long_options[option_index].name, "remote")) {
246 if (sscanf (optarg, "%d", &remote_control) != 1) {
247 fprintf (stderr, _("%s: --listen=PID: PID was not a number: %s\n"),
248 program_name, optarg);
252 p = getenv ("GUESTFISH_PID");
253 if (!p || sscanf (p, "%d", &remote_control) != 1) {
254 fprintf (stderr, _("%s: remote: $GUESTFISH_PID must be set"
255 " to the PID of the remote process\n"),
260 } else if (STREQ (long_options[option_index].name, "selinux")) {
261 guestfs_set_selinux (g, 1);
262 } else if (STREQ (long_options[option_index].name, "keys-from-stdin")) {
265 fprintf (stderr, _("%s: unknown long option: %s (%d)\n"),
266 program_name, long_options[option_index].name, option_index);
272 if (access (optarg, R_OK) != 0) {
276 drv = malloc (sizeof (struct drv));
282 drv->a.filename = optarg;
288 libvirt_uri = optarg;
292 drv = malloc (sizeof (struct drv));
298 drv->d.guest = optarg;
304 if (STRCASEEQ (optarg, "list")) {
305 list_prepared_drives ();
308 drv = malloc (sizeof (struct drv));
314 if (asprintf (&drv->N.filename, "test%d.img",
315 next_prepared_drive++) == -1) {
319 drv->N.data = create_prepared_file (optarg, drv->N.filename);
320 drv->N.device = NULL; /* filled in by add_drives */
326 complete_dest_paths = 0;
331 fprintf (stderr, _("%s: only one -f parameter can be given\n"),
342 r = display_command (optarg);
343 else if (argv[optind] && argv[optind][0] != '-')
344 r = display_command (argv[optind++]);
348 exit (r == 0 ? EXIT_SUCCESS : EXIT_FAILURE);
356 mp = malloc (sizeof (struct mp));
361 p = strchr (optarg, ':');
364 mp->mountpoint = p+1;
366 mp->mountpoint = bad_cast ("/");
373 guestfs_set_autosync (g, 0);
382 guestfs_set_verbose (g, verbose);
386 struct guestfs_version *v = guestfs_version (g);
387 printf ("%s %"PRIi64".%"PRIi64".%"PRIi64"%s\n", program_name,
388 v->major, v->minor, v->release, v->extra);
393 guestfs_set_trace (g, 1);
397 usage (EXIT_SUCCESS);
400 usage (EXIT_FAILURE);
404 /* Old-style -i syntax? Since -a/-d/-N and -i was disallowed
405 * previously, if we have -i without any drives but with something
406 * on the command line, it must be old-style syntax.
408 if (inspector && drvs == NULL && optind < argc) {
409 while (optind < argc) {
410 if (strchr (argv[optind], '/') ||
411 access (argv[optind], F_OK) == 0) { /* simulate -a option */
412 drv = malloc (sizeof (struct drv));
418 drv->a.filename = argv[optind];
421 } else { /* simulate -d option */
422 drv = malloc (sizeof (struct drv));
428 drv->d.guest = argv[optind];
437 /* If we've got drives to add, add them now. */
438 add_drives (drvs, 'a');
440 /* If we've got mountpoints or prepared drives or -i option, we must
441 * launch the guest and mount them.
443 if (next_prepared_drive > 1 || mps != NULL || inspector) {
444 /* RHBZ#612178: If --listen flag is given, then we will fork into
445 * the background in rc_listen(). However you can't do this while
446 * holding a libguestfs handle open because the recovery process
447 * will think the main program has died and kill qemu. Therefore
448 * don't use the recovery process for this case. (A better
449 * solution would be to call launch () etc after the fork, but
450 * that greatly complicates the code here).
452 if (remote_control_listen)
453 guestfs_set_recovery_proc (g, 0);
455 if (launch () == -1) exit (EXIT_FAILURE);
460 prepare_drives (drvs);
464 /* Remote control? */
465 if (remote_control_listen && remote_control) {
467 _("%s: cannot use --listen and --remote options at the same time\n"),
472 if (remote_control_listen) {
475 _("%s: extra parameters on the command line with --listen flag\n"),
481 _("%s: cannot use --listen and --file options at the same time\n"),
488 /* -f (file) parameter? */
491 if (open (file, O_RDONLY) == -1) {
497 /* Interactive, shell script, or command(s) on the command line? */
498 if (optind >= argc) {
505 cmdline (argv, optind, argc);
513 pod2text (const char *name, const char *shortdesc, const char *str)
517 fp = popen ("pod2text", "w");
519 /* pod2text failed, maybe not found, so let's just print the
520 * source instead, since that's better than doing nothing.
522 printf ("%s - %s\n\n%s\n", name, shortdesc, str);
525 fprintf (fp, "=head1 NAME\n\n%s - %s\n\n", name, shortdesc);
530 /* List is built in reverse order, so mount them in reverse order. */
532 mount_mps (struct mp *mp)
537 mount_mps (mp->next);
539 /* Don't use guestfs_mount here because that will default to mount
540 * options -o sync,noatime. For more information, see guestfs(3)
541 * section "LIBGUESTFS GOTCHAS".
543 const char *options = read_only ? "ro" : "";
544 r = guestfs_mount_options (g, options, mp->device, mp->mountpoint);
551 add_drives (struct drv *drv, char next_drive)
555 if (next_drive > 'z') {
557 _("guestfish: too many drives added on the command line\n"));
562 next_drive = add_drives (drv->next, next_drive);
567 r = guestfs_add_drive (g, drv->a.filename);
569 r = guestfs_add_drive_ro (g, drv->a.filename);
577 r = add_libvirt_drives (drv->d.guest);
585 /* -N option is not affected by --ro */
586 r = guestfs_add_drive (g, drv->N.filename);
590 if (asprintf (&drv->N.device, "/dev/sd%c", next_drive) == -1) {
598 default: /* keep GCC happy */
607 prepare_drives (struct drv *drv)
610 prepare_drives (drv->next);
611 if (drv->type == drv_N)
612 prepare_drive (drv->N.filename, drv->N.data, drv->N.device);
619 if (guestfs_is_config (g)) {
620 if (guestfs_launch (g) == -1)
638 #define FISH "><fs> "
640 static char *line_read = NULL;
645 #ifdef HAVE_LIBREADLINE
653 line_read = readline (prompt ? FISH : "");
655 if (line_read && *line_read)
656 add_history_line (line_read);
661 #endif /* HAVE_LIBREADLINE */
663 static char buf[8192];
666 if (prompt) printf (FISH);
667 line_read = fgets (buf, sizeof buf, stdin);
670 len = strlen (line_read);
671 if (len > 0 && buf[len-1] == '\n') buf[len-1] = '\0';
685 int global_exit_on_error = !prompt;
690 "Welcome to guestfish, the libguestfs filesystem interactive shell for\n"
691 "editing virtual machine filesystems.\n"
693 "Type: 'help' for a list of commands\n"
694 " 'man' to read the manual\n"
695 " 'quit' to quit the shell\n"
699 print_inspect_prompt ();
707 exit_on_error = global_exit_on_error;
709 buf = rl_gets (prompt);
715 /* Skip any initial whitespace before the command. */
717 while (*buf && c_isspace (*buf))
722 /* If the next character is '#' then this is a comment. */
723 if (*buf == '#') continue;
725 /* If the next character is '!' then pass the whole lot to system(3). */
733 (WTERMSIG (r) == SIGINT || WTERMSIG (r) == SIGQUIT)) ||
734 WEXITSTATUS (r) != 0)
740 /* If the next character is '-' allow the command to fail without
741 * exiting on error (just for this one command though).
749 /* Get the command (cannot be quoted). */
750 len = strcspn (buf, " \t");
752 if (len == 0) continue;
756 if (buf[len] == '\0') {
763 p += strspn (p, " \t");
765 /* Get the parameters. */
766 while (*p && i < sizeof argv / sizeof argv[0]) {
769 /* Parameters which start with quotes or pipes are treated
770 * specially. Bare parameters are delimited by whitespace.
774 len = strcspn (p, "\"");
775 if (p[len] == '\0') {
776 fprintf (stderr, _("%s: unterminated double quote\n"), program_name);
777 if (exit_on_error) exit (EXIT_FAILURE);
780 if (p[len+1] && (p[len+1] != ' ' && p[len+1] != '\t')) {
782 _("%s: command arguments not separated by whitespace\n"),
784 if (exit_on_error) exit (EXIT_FAILURE);
788 pend = p[len+1] ? &p[len+2] : &p[len+1];
789 } else if (*p == '\'') {
791 len = strcspn (p, "'");
792 if (p[len] == '\0') {
793 fprintf (stderr, _("%s: unterminated single quote\n"), program_name);
794 if (exit_on_error) exit (EXIT_FAILURE);
797 if (p[len+1] && (p[len+1] != ' ' && p[len+1] != '\t')) {
799 _("%s: command arguments not separated by whitespace\n"),
801 if (exit_on_error) exit (EXIT_FAILURE);
805 pend = p[len+1] ? &p[len+2] : &p[len+1];
806 } else if (*p == '|') {
811 } else if (*p == '[') {
815 while (*pend && c != 0) {
816 if (*pend == '[') c++;
817 else if (*pend == ']') c--;
822 _("%s: unterminated \"[...]\" sequence\n"), program_name);
823 if (exit_on_error) exit (EXIT_FAILURE);
826 if (*pend && (*pend != ' ' && *pend != '\t')) {
828 _("%s: command arguments not separated by whitespace\n"),
830 if (exit_on_error) exit (EXIT_FAILURE);
835 } else if (*p != ' ' && *p != '\t') {
836 /* If the first character is a ~ then note that this parameter
837 * is a candidate for ~username expansion. NB this does not
838 * apply to quoted parameters.
840 tilde_candidate = *p == '~';
841 len = strcspn (p, " \t");
848 fprintf (stderr, _("%s: internal error parsing string at '%s'\n"),
853 if (!tilde_candidate)
856 argv[i] = try_tilde_expansion (p);
861 p += strspn (p, " \t");
864 if (i == sizeof argv / sizeof argv[0]) {
865 fprintf (stderr, _("%s: too many arguments\n"), program_name);
866 if (exit_on_error) exit (EXIT_FAILURE);
873 if (issue_command (cmd, argv, pipe) == -1) {
874 if (exit_on_error) exit (EXIT_FAILURE);
879 if (prompt) printf ("\n");
883 cmdline (char *argv[], int optind, int argc)
890 if (optind >= argc) return;
892 cmd = argv[optind++];
893 if (STREQ (cmd, ":")) {
894 fprintf (stderr, _("%s: empty command on command line\n"), program_name);
898 /* Allow -cmd on the command line to mean (temporarily) override
899 * the normal exit on error (RHBZ#578407).
906 params = &argv[optind];
908 /* Search for end of command list or ":" ... */
909 while (optind < argc && STRNEQ (argv[optind], ":"))
912 if (optind == argc) {
913 if (issue_command (cmd, params, NULL) == -1 && exit_on_error)
917 if (issue_command (cmd, params, NULL) == -1 && exit_on_error)
919 cmdline (argv, optind+1, argc);
924 issue_command (const char *cmd, char *argv[], const char *pipecmd)
927 int stdout_saved_fd = -1;
931 /* This counts the commands issued, starting at 1. */
934 /* For | ... commands. Annoyingly we can't use popen(3) here. */
938 if (fflush (stdout) == EOF) {
939 perror ("failed to flush standard output");
943 perror ("pipe failed");
952 if (pid == 0) { /* Child process. */
954 if (dup2 (fd[0], 0) < 0) {
955 perror ("dup2 of stdin failed");
959 r = system (pipecmd);
964 _exit (WEXITSTATUS (r));
967 if ((stdout_saved_fd = dup (1)) < 0) {
968 perror ("failed to dup stdout");
972 if (dup2 (fd[1], 1) < 0) {
973 perror ("failed to dup stdout");
974 close (stdout_saved_fd);
980 for (argc = 0; argv[argc] != NULL; ++argc)
983 /* If --remote was set, then send this command to a remote process. */
985 r = rc_remote (remote_control, cmd, argc, argv, exit_on_error);
987 /* Otherwise execute it locally. */
988 else if (STRCASEEQ (cmd, "help")) {
993 r = display_command (argv[0]);
995 else if (STRCASEEQ (cmd, "quit") ||
996 STRCASEEQ (cmd, "exit") ||
997 STRCASEEQ (cmd, "q")) {
1001 else if (STRCASEEQ (cmd, "alloc") ||
1002 STRCASEEQ (cmd, "allocate"))
1003 r = do_alloc (cmd, argc, argv);
1004 else if (STRCASEEQ (cmd, "echo"))
1005 r = do_echo (cmd, argc, argv);
1006 else if (STRCASEEQ (cmd, "edit") ||
1007 STRCASEEQ (cmd, "vi") ||
1008 STRCASEEQ (cmd, "emacs"))
1009 r = do_edit (cmd, argc, argv);
1010 else if (STRCASEEQ (cmd, "lcd"))
1011 r = do_lcd (cmd, argc, argv);
1012 else if (STRCASEEQ (cmd, "glob"))
1013 r = do_glob (cmd, argc, argv);
1014 else if (STRCASEEQ (cmd, "man") ||
1015 STRCASEEQ (cmd, "manual"))
1016 r = do_man (cmd, argc, argv);
1017 else if (STRCASEEQ (cmd, "more") ||
1018 STRCASEEQ (cmd, "less"))
1019 r = do_more (cmd, argc, argv);
1020 else if (STRCASEEQ (cmd, "reopen"))
1021 r = do_reopen (cmd, argc, argv);
1022 else if (STRCASEEQ (cmd, "sparse"))
1023 r = do_sparse (cmd, argc, argv);
1024 else if (STRCASEEQ (cmd, "supported"))
1025 r = do_supported (cmd, argc, argv);
1026 else if (STRCASEEQ (cmd, "time"))
1027 r = do_time (cmd, argc, argv);
1029 r = run_action (cmd, argc, argv);
1031 /* Always flush stdout after every command, so that messages, results
1032 * etc appear immediately.
1034 if (fflush (stdout) == EOF) {
1035 perror ("failed to flush standard output");
1041 if (dup2 (stdout_saved_fd, 1) < 0) {
1042 perror ("failed to dup2 standard output");
1045 close (stdout_saved_fd);
1046 if (waitpid (pid, NULL, 0) < 0) {
1047 perror ("waiting for command to complete");
1056 list_builtin_commands (void)
1058 /* help, man and quit should appear at the top */
1059 printf ("%-20s %s\n",
1060 "help", _("display a list of commands or help on a command"));
1061 printf ("%-20s %s\n",
1062 "man", _("read the manual"));
1063 printf ("%-20s %s\n",
1064 "quit", _("quit guestfish"));
1066 printf ("%-20s %s\n",
1067 "alloc", _("allocate an image"));
1068 printf ("%-20s %s\n",
1069 "echo", _("display a line of text"));
1070 printf ("%-20s %s\n",
1071 "edit", _("edit a file in the image"));
1072 printf ("%-20s %s\n",
1073 "lcd", _("local change directory"));
1074 printf ("%-20s %s\n",
1075 "glob", _("expand wildcards in command"));
1076 printf ("%-20s %s\n",
1077 "more", _("view a file in the pager"));
1078 printf ("%-20s %s\n",
1079 "reopen", _("close and reopen libguestfs handle"));
1080 printf ("%-20s %s\n",
1081 "sparse", _("allocate a sparse image file"));
1082 printf ("%-20s %s\n",
1083 "supported", _("list supported groups of commands"));
1084 printf ("%-20s %s\n",
1085 "time", _("measure time taken to run command"));
1087 /* actions are printed after this (see list_commands) */
1091 display_builtin_command (const char *cmd)
1093 /* help for actions is auto-generated, see display_command */
1095 if (STRCASEEQ (cmd, "alloc") ||
1096 STRCASEEQ (cmd, "allocate")) {
1097 printf (_("alloc - allocate an image\n"
1098 " alloc <filename> <size>\n"
1100 " This creates an empty (zeroed) file of the given size,\n"
1101 " and then adds so it can be further examined.\n"
1103 " For more advanced image creation, see qemu-img utility.\n"
1105 " Size can be specified using standard suffixes, eg. '1M'.\n"
1109 else if (STRCASEEQ (cmd, "echo")) {
1110 printf (_("echo - display a line of text\n"
1111 " echo [<params> ...]\n"
1113 " This echos the parameters to the terminal.\n"));
1116 else if (STRCASEEQ (cmd, "edit") ||
1117 STRCASEEQ (cmd, "vi") ||
1118 STRCASEEQ (cmd, "emacs")) {
1119 printf (_("edit - edit a file in the image\n"
1120 " edit <filename>\n"
1122 " This is used to edit a file.\n"
1124 " It is the equivalent of (and is implemented by)\n"
1125 " running \"cat\", editing locally, and then \"write\".\n"
1127 " Normally it uses $EDITOR, but if you use the aliases\n"
1128 " \"vi\" or \"emacs\" you will get those editors.\n"
1130 " NOTE: This will not work reliably for large files\n"
1131 " (> 2 MB) or binary files containing \\0 bytes.\n"));
1134 else if (STRCASEEQ (cmd, "lcd")) {
1135 printf (_("lcd - local change directory\n"
1136 " lcd <directory>\n"
1138 " Change guestfish's current directory. This command is\n"
1139 " useful if you want to download files to a particular\n"
1143 else if (STRCASEEQ (cmd, "glob")) {
1144 printf (_("glob - expand wildcards in command\n"
1145 " glob <command> [<args> ...]\n"
1147 " Glob runs <command> with wildcards expanded in any\n"
1148 " command args. Note that the command is run repeatedly\n"
1149 " once for each expanded argument.\n"));
1152 else if (STRCASEEQ (cmd, "man") ||
1153 STRCASEEQ (cmd, "manual")) {
1154 printf (_("man - read the manual\n"
1157 " Opens the manual page for guestfish.\n"));
1160 else if (STRCASEEQ (cmd, "help")) {
1161 printf (_("help - display a list of commands or help on a command\n"
1166 else if (STRCASEEQ (cmd, "more") ||
1167 STRCASEEQ (cmd, "less")) {
1168 printf (_("more - view a file in the pager\n"
1169 " more <filename>\n"
1171 " This is used to view a file in the pager.\n"
1173 " It is the equivalent of (and is implemented by)\n"
1174 " running \"cat\" and using the pager.\n"
1176 " Normally it uses $PAGER, but if you use the alias\n"
1177 " \"less\" then it always uses \"less\".\n"
1179 " NOTE: This will not work reliably for large files\n"
1180 " (> 2 MB) or binary files containing \\0 bytes.\n"));
1183 else if (STRCASEEQ (cmd, "quit") ||
1184 STRCASEEQ (cmd, "exit") ||
1185 STRCASEEQ (cmd, "q")) {
1186 printf (_("quit - quit guestfish\n"
1190 else if (STRCASEEQ (cmd, "reopen")) {
1191 printf (_("reopen - close and reopen the libguestfs handle\n"
1194 "Close and reopen the libguestfs handle. It is not necessary to use\n"
1195 "this normally, because the handle is closed properly when guestfish\n"
1196 "exits. However this is occasionally useful for testing.\n"));
1199 else if (STRCASEEQ (cmd, "sparse")) {
1200 printf (_("sparse - allocate a sparse image file\n"
1201 " sparse <filename> <size>\n"
1203 " This creates an empty sparse file of the given size,\n"
1204 " and then adds so it can be further examined.\n"
1206 " In all respects it works the same as the 'alloc'\n"
1207 " command, except that the image file is allocated\n"
1208 " sparsely, which means that disk blocks are not assigned\n"
1209 " to the file until they are needed. Sparse disk files\n"
1210 " only use space when written to, but they are slower\n"
1211 " and there is a danger you could run out of real disk\n"
1212 " space during a write operation.\n"
1214 " For more advanced image creation, see qemu-img utility.\n"
1216 " Size can be specified using standard suffixes, eg. '1M'.\n"
1220 else if (STRCASEEQ (cmd, "supported")) {
1221 printf (_("supported - list supported groups of commands\n"
1224 " This command returns a list of the optional groups\n"
1225 " known to the daemon, and indicates which ones are\n"
1226 " supported by this build of the libguestfs appliance.\n"
1228 " See also guestfs(3) section AVAILABILITY.\n"
1232 else if (STRCASEEQ (cmd, "time")) {
1233 printf (_("time - measure time taken to run command\n"
1234 " time <command> [<args> ...]\n"
1236 " This runs <command> as usual, and prints the elapsed\n"
1237 " time afterwards.\n"));
1241 fprintf (stderr, _("%s: command not known, use -h to list all commands\n"),
1247 /* This is printed when the user types in an unknown command for the
1248 * first command issued. A common case is the user doing:
1249 * guestfish disk.img
1250 * expecting guestfish to open 'disk.img' (in fact, this tried to
1251 * run a command 'disk.img').
1254 extended_help_message (void)
1257 _("Did you mean to open a disk image? guestfish -a disk.img\n"
1258 "For a list of commands: guestfish -h\n"
1259 "For complete documentation: man guestfish\n"));
1263 free_strings (char **argv)
1267 for (argc = 0; argv[argc] != NULL; ++argc)
1273 count_strings (char *const *argv)
1277 for (c = 0; argv[c]; ++c)
1283 print_strings (char *const *argv)
1287 for (argc = 0; argv[argc] != NULL; ++argc)
1288 printf ("%s\n", argv[argc]);
1292 print_table (char *const *argv)
1296 for (i = 0; argv[i] != NULL; i += 2)
1297 printf ("%s: %s\n", argv[i], argv[i+1]);
1301 is_true (const char *str)
1304 STRCASENEQ (str, "0") &&
1305 STRCASENEQ (str, "f") &&
1306 STRCASENEQ (str, "false") &&
1307 STRCASENEQ (str, "n") &&
1308 STRCASENEQ (str, "no");
1311 /* Free strings from a non-NULL terminated char** */
1313 free_n_strings (char **str, size_t len)
1317 for (i = 0; i < len; i++) {
1324 parse_string_list (const char *str)
1327 size_t argv_len = 0;
1329 /* Current position pointer */
1330 const char *p = str;
1332 /* Token might be simple:
1335 * 'This is a single token'
1336 * or contain embedded single-quoted sections:
1337 * This' is a sing'l'e to'ken
1339 * The latter may seem over-complicated, but it's what a normal shell does.
1340 * Not doing it risks surprising somebody.
1342 * This outer loop is over complete tokens.
1348 /* Skip leading whitespace */
1349 p += strspn (p, " \t");
1353 /* This loop is over token 'fragments'. A token can be in multiple bits if
1354 * it contains single quotes. We also treat both sides of an escaped quote
1355 * as separate fragments because we can't just copy it: we have to remove
1358 while (*p && (!c_isblank (*p) || in_quote)) {
1359 const char *end = p;
1361 /* Check if the fragment starts with a quote */
1363 /* Toggle in_quote */
1364 in_quote = !in_quote;
1366 /* Skip the quote */
1370 /* If we're in a quote, look for an end quote */
1372 end += strcspn (end, "'");
1375 /* Otherwise, look for whitespace or a quote */
1377 end += strcspn (end, " \t'");
1380 /* Grow the token to accommodate the fragment */
1381 size_t tok_end = tok_len;
1383 char *tok_new = realloc (tok, tok_len + 1);
1384 if (NULL == tok_new) {
1386 free_n_strings (argv, argv_len);
1388 exit (EXIT_FAILURE);
1392 /* Check if we stopped on an escaped quote */
1393 if ('\'' == *end && end != p && *(end-1) == '\\') {
1394 /* Add everything before \' to the token */
1395 memcpy (&tok[tok_end], p, end - p - 1);
1398 tok[tok_len-1] = '\'';
1400 /* Already processed the quote */
1405 /* Add the whole fragment */
1406 memcpy (&tok[tok_end], p, end - p);
1412 /* We've reached the end of a token. We shouldn't still be in quotes. */
1414 fprintf (stderr, _("Runaway quote in string \"%s\"\n"), str);
1416 free_n_strings (argv, argv_len);
1421 /* Add this token if there is one. There might not be if there was
1422 * whitespace at the end of the input string */
1424 /* Add the NULL terminator */
1425 tok[tok_len] = '\0';
1427 /* Add the argument to the argument list */
1429 char **argv_new = realloc (argv, sizeof (*argv) * argv_len);
1430 if (NULL == argv_new) {
1432 free_n_strings (argv, argv_len-1);
1434 exit (EXIT_FAILURE);
1438 argv[argv_len-1] = tok;
1442 /* NULL terminate the argument list */
1444 char **argv_new = realloc (argv, sizeof (*argv) * argv_len);
1445 if (NULL == argv_new) {
1447 free_n_strings (argv, argv_len-1);
1448 exit (EXIT_FAILURE);
1452 argv[argv_len-1] = NULL;
1457 #ifdef HAVE_LIBREADLINE
1458 static char histfile[1024];
1459 static int nr_history_lines = 0;
1463 initialize_readline (void)
1465 #ifdef HAVE_LIBREADLINE
1468 home = getenv ("HOME");
1470 snprintf (histfile, sizeof histfile, "%s/.guestfish", home);
1472 (void) read_history (histfile);
1475 rl_readline_name = "guestfish";
1476 rl_attempted_completion_function = do_completion;
1478 /* Note that .inputrc (or /etc/inputrc) is not read until the first
1479 * call the readline(), which happens later. Therefore, these
1480 * provide default values which can be overridden by the user if
1483 (void) rl_variable_bind ("completion-ignore-case", "on");
1488 cleanup_readline (void)
1490 #ifdef HAVE_LIBREADLINE
1493 if (histfile[0] != '\0') {
1494 fd = open (histfile, O_WRONLY|O_CREAT, 0644);
1501 #ifdef HAVE_APPEND_HISTORY
1502 (void) append_history (nr_history_lines, histfile);
1504 (void) write_history (histfile);
1510 #ifdef HAVE_LIBREADLINE
1512 add_history_line (const char *line)
1520 xwrite (int fd, const void *v_buf, size_t len)
1523 const char *buf = v_buf;
1526 r = write (fd, buf, len);
1538 /* Resolve the special "win:..." form for Windows-specific paths.
1539 * This always returns a newly allocated string which is freed by the
1540 * caller function in "cmds.c".
1543 resolve_win_path (const char *path)
1548 if (STRCASENEQLEN (path, "win:", 4)) {
1549 ret = strdup (path);
1557 /* Drop drive letter, if it's "C:". */
1558 if (STRCASEEQLEN (path, "c:", 2))
1568 ret = strdup (path);
1574 /* Blindly convert any backslashes into forward slashes. Is this good? */
1575 for (i = 0; i < strlen (ret); ++i)
1579 char *t = guestfs_case_sensitive_path (g, ret);
1586 /* Resolve the special FileIn paths ("-" or "-<<END" or filename).
1587 * The caller (cmds.c) will call free_file_in after the command has
1588 * run which should clean up resources.
1590 static char *file_in_heredoc (const char *endmarker);
1591 static char *file_in_tmpfile = NULL;
1594 file_in (const char *arg)
1598 if (STREQ (arg, "-")) {
1599 ret = strdup ("/dev/stdin");
1605 else if (STRPREFIX (arg, "-<<")) {
1606 const char *endmarker = &arg[3];
1607 if (*endmarker == '\0') {
1608 fprintf (stderr, "%s: missing end marker in -<< expression\n",
1612 ret = file_in_heredoc (endmarker);
1628 file_in_heredoc (const char *endmarker)
1630 static const char template[] = "/tmp/heredocXXXXXX";
1631 file_in_tmpfile = strdup (template);
1632 if (file_in_tmpfile == NULL) {
1637 int fd = mkstemp (file_in_tmpfile);
1643 size_t markerlen = strlen (endmarker);
1645 char buffer[BUFSIZ];
1646 int write_error = 0;
1647 while (fgets (buffer, sizeof buffer, stdin) != NULL) {
1648 /* Look for "END"<EOF> or "END\n" in input. */
1649 size_t blen = strlen (buffer);
1650 if (STREQLEN (buffer, endmarker, markerlen) &&
1651 (blen == markerlen ||
1652 (blen == markerlen+1 && buffer[markerlen] == '\n')))
1655 if (xwrite (fd, buffer, blen) == -1) {
1656 if (!write_error) perror ("write");
1658 /* continue reading up to the end marker */
1662 /* Reached EOF of stdin without finding the end marker, which
1663 * is likely to be an error.
1665 fprintf (stderr, "%s: end of input reached without finding '%s'\n",
1666 program_name, endmarker);
1675 if (close (fd) == -1) {
1680 return file_in_tmpfile;
1683 unlink (file_in_tmpfile);
1686 free (file_in_tmpfile);
1687 file_in_tmpfile = NULL;
1692 free_file_in (char *s)
1694 if (file_in_tmpfile) {
1695 if (unlink (file_in_tmpfile) == -1)
1696 perror (file_in_tmpfile);
1697 file_in_tmpfile = NULL;
1700 /* Free the device or file name which was strdup'd in file_in().
1701 * Note it's not immediately clear, but for -<< heredocs,
1702 * s == file_in_tmpfile, so this frees up that buffer.
1707 /* Resolve the special FileOut paths ("-" or filename).
1708 * The caller (cmds.c) will call free (str) after the command has run.
1711 file_out (const char *arg)
1715 if (STREQ (arg, "-"))
1716 ret = strdup ("/dev/stdout");
1727 /* Read a passphrase ('Key') from /dev/tty with echo off.
1728 * The caller (cmds.c) will call free on the string afterwards.
1729 * Based on the code in cryptsetup file lib/utils.c.
1732 read_key (const char *param)
1735 struct termios orig, temp;
1738 /* Read and write to /dev/tty if available. */
1739 if (keys_from_stdin ||
1740 (infp = outfp = fopen ("/dev/tty", "w+")) == NULL) {
1745 /* Print the prompt and set no echo. */
1746 int tty = isatty (fileno (infp));
1749 fprintf (outfp, _("Enter key or passphrase (\"%s\"): "), param);
1751 if (tcgetattr (fileno (infp), &orig) == -1) {
1752 perror ("tcgetattr");
1755 memcpy (&temp, &orig, sizeof temp);
1756 temp.c_lflag &= ~ECHO;
1758 tcsetattr (fileno (infp), TCSAFLUSH, &temp);
1764 len = getline (&ret, &n, infp);
1771 /* Remove the terminating \n if there is one. */
1772 if (len > 0 && ret[len-1] == '\n')
1776 /* Restore echo, close file descriptor. */
1779 tcsetattr (fileno (infp), TCSAFLUSH, &orig);
1783 fclose (infp); /* outfp == infp, so this is closed also */