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.
29 #include <sys/types.h>
32 #ifdef HAVE_LIBREADLINE
33 #include <readline/readline.h>
34 #include <readline/history.h>
55 static void add_drives (struct drv *drv);
56 static void mount_mps (struct mp *mp);
57 static void interactive (void);
58 static void shell_script (void);
59 static void script (int prompt);
60 static void cmdline (char *argv[], int optind, int argc);
61 static void initialize_readline (void);
62 static void cleanup_readline (void);
63 static void add_history_line (const char *);
65 /* Currently open libguestfs handle. */
71 int echo_commands = 0;
72 int remote_control_listen = 0;
73 int remote_control = 0;
74 int exit_on_error = 1;
77 launch (guestfs_h *_g)
81 if (guestfs_is_config (g)) {
82 if (guestfs_launch (g) == -1)
88 static void __attribute__((noreturn))
91 if (status != EXIT_SUCCESS)
92 fprintf (stderr, _("Try `%s --help' for more information.\n"),
96 _("%s: guest filesystem shell\n"
97 "%s lets you edit virtual machine filesystems\n"
98 "Copyright (C) 2009 Red Hat Inc.\n"
100 " %s [--options] cmd [: cmd : cmd ...]\n"
101 " %s -i libvirt-domain\n"
102 " %s -i disk-image(s)\n"
103 "or for interactive use:\n"
105 "or from a shell script:\n"
111 " -h|--cmd-help List available commands\n"
112 " -h|--cmd-help cmd Display detailed help on 'cmd'\n"
113 " -a|--add image Add image\n"
114 " -D|--no-dest-paths Don't tab-complete paths from guest fs\n"
115 " -f|--file file Read commands from file\n"
116 " -i|--inspector Run virt-inspector to get disk mountpoints\n"
117 " --listen Listen for remote commands\n"
118 " -m|--mount dev[:mnt] Mount dev on mnt (if omitted, /)\n"
119 " -n|--no-sync Don't autosync\n"
120 " --remote[=pid] Send commands to remote %s\n"
121 " -r|--ro Mount read-only\n"
122 " --selinux Enable SELinux support\n"
123 " -v|--verbose Verbose messages\n"
124 " -x Echo each command before executing it\n"
125 " -V|--version Display version and exit\n"
126 "For more information, see the manpage %s(1).\n"),
127 program_name, program_name, program_name,
128 program_name, program_name, program_name,
129 program_name, program_name, program_name);
135 main (int argc, char *argv[])
137 /* Set global program name that is not polluted with libtool artifacts. */
138 set_program_name (argv[0]);
140 atexit (close_stdout);
142 setlocale (LC_ALL, "");
143 bindtextdomain (PACKAGE, LOCALEBASEDIR);
144 textdomain (PACKAGE);
146 enum { HELP_OPTION = CHAR_MAX + 1 };
148 static const char *options = "a:Df:h::im:nrv?Vx";
149 static const struct option long_options[] = {
150 { "add", 1, 0, 'a' },
151 { "cmd-help", 2, 0, 'h' },
152 { "file", 1, 0, 'f' },
153 { "help", 0, 0, HELP_OPTION },
154 { "inspector", 0, 0, 'i' },
155 { "listen", 0, 0, 0 },
156 { "mount", 1, 0, 'm' },
157 { "no-dest-paths", 0, 0, 'D' },
158 { "no-sync", 0, 0, 'n' },
159 { "remote", 2, 0, 0 },
161 { "selinux", 0, 0, 0 },
162 { "verbose", 0, 0, 'v' },
163 { "version", 0, 0, 'V' },
166 struct drv *drvs = NULL;
168 struct mp *mps = NULL;
170 char *p, *file = NULL;
176 initialize_readline ();
178 memset (&sa, 0, sizeof sa);
179 sa.sa_handler = SIG_IGN;
180 sa.sa_flags = SA_RESTART;
181 sigaction (SIGPIPE, &sa, NULL);
183 /* guestfs_create is meant to be a lightweight operation, so
184 * it's OK to do it early here.
186 g = guestfs_create ();
188 fprintf (stderr, _("guestfs_create: failed to create handle\n"));
192 guestfs_set_autosync (g, 1);
194 /* If developing, add ./appliance to the path. Note that libtools
195 * interferes with this because uninstalled guestfish is a shell
196 * script that runs the real program with an absolute path. Detect
199 * BUT if LIBGUESTFS_PATH environment variable is already set by
200 * the user, then don't override it.
202 if (getenv ("LIBGUESTFS_PATH") == NULL &&
204 (argv[0][0] != '/' || strstr (argv[0], "/.libs/lt-") != NULL))
205 guestfs_set_path (g, "appliance:" GUESTFS_DEFAULT_PATH);
207 /* CAUTION: we are careful to modify argv[0] here, only after
208 * using it just above.
210 * getopt_long uses argv[0], so give it the sanitized name. Save a copy
211 * of the original, in case it's needed in virt-inspector mode, below.
213 char *real_argv0 = argv[0];
214 argv[0] = bad_cast (program_name);
217 c = getopt_long (argc, argv, options, long_options, &option_index);
221 case 0: /* options which are long only */
222 if (STREQ (long_options[option_index].name, "listen"))
223 remote_control_listen = 1;
224 else if (STREQ (long_options[option_index].name, "remote")) {
226 if (sscanf (optarg, "%d", &remote_control) != 1) {
227 fprintf (stderr, _("%s: --listen=PID: PID was not a number: %s\n"),
228 program_name, optarg);
232 p = getenv ("GUESTFISH_PID");
233 if (!p || sscanf (p, "%d", &remote_control) != 1) {
234 fprintf (stderr, _("%s: remote: $GUESTFISH_PID must be set"
235 " to the PID of the remote process\n"),
240 } else if (STREQ (long_options[option_index].name, "selinux")) {
241 guestfs_set_selinux (g, 1);
243 fprintf (stderr, _("%s: unknown long option: %s (%d)\n"),
244 program_name, long_options[option_index].name, option_index);
250 if (access (optarg, R_OK) != 0) {
254 drv = malloc (sizeof (struct drv));
259 drv->filename = optarg;
265 complete_dest_paths = 0;
270 fprintf (stderr, _("%s: only one -f parameter can be given\n"),
279 display_command (optarg);
280 else if (argv[optind] && argv[optind][0] != '-')
281 display_command (argv[optind++]);
291 mp = malloc (sizeof (struct mp));
296 p = strchr (optarg, ':');
299 mp->mountpoint = p+1;
301 mp->mountpoint = bad_cast ("/");
308 guestfs_set_autosync (g, 0);
317 guestfs_set_verbose (g, verbose);
321 printf ("%s %s\n", program_name, PACKAGE_VERSION);
329 usage (EXIT_SUCCESS);
332 usage (EXIT_FAILURE);
336 /* Inspector mode invalidates most of the other arguments. */
341 if (drvs || mps || remote_control_listen || remote_control ||
342 guestfs_get_selinux (g)) {
343 fprintf (stderr, _("%s: cannot use -i option with -a, -m,"
344 " --listen, --remote or --selinux\n"),
348 if (optind >= argc) {
350 _("%s: -i requires a libvirt domain or path(s) to disk image(s)\n"),
355 strcpy (cmd, "a=`virt-inspector");
356 while (optind < argc) {
357 if (strlen (cmd) + strlen (argv[optind]) + strlen (real_argv0) + 60
360 _("%s: virt-inspector command too long for fixed-size buffer\n"),
365 strcat (cmd, argv[optind]);
371 strcat (cmd, " --ro-fish");
373 strcat (cmd, " --fish");
375 sprintf (&cmd[strlen(cmd)], "` && %s $a", real_argv0);
377 if (guestfs_get_verbose (g))
379 if (!guestfs_get_autosync (g))
384 "%s -i: running virt-inspector command:\n%s\n", program_name, cmd);
391 exit (WEXITSTATUS (r));
394 /* If we've got drives to add, add them now. */
397 /* If we've got mountpoints, we must launch the guest and mount them. */
399 if (launch (g) == -1) exit (EXIT_FAILURE);
403 /* Remote control? */
404 if (remote_control_listen && remote_control) {
406 _("%s: cannot use --listen and --remote options at the same time\n"),
411 if (remote_control_listen) {
414 _("%s: extra parameters on the command line with --listen flag\n"),
420 _("%s: cannot use --listen and --file options at the same time\n"),
427 /* -f (file) parameter? */
430 if (open (file, O_RDONLY) == -1) {
436 /* Interactive, shell script, or command(s) on the command line? */
437 if (optind >= argc) {
444 cmdline (argv, optind, argc);
452 pod2text (const char *name, const char *shortdesc, const char *str)
456 fp = popen ("pod2text", "w");
458 /* pod2text failed, maybe not found, so let's just print the
459 * source instead, since that's better than doing nothing.
461 printf ("%s - %s\n\n%s\n", name, shortdesc, str);
464 fprintf (fp, "=head1 NAME\n\n%s - %s\n\n", name, shortdesc);
469 /* List is built in reverse order, so mount them in reverse order. */
471 mount_mps (struct mp *mp)
476 mount_mps (mp->next);
478 r = guestfs_mount (g, mp->device, mp->mountpoint);
480 r = guestfs_mount_ro (g, mp->device, mp->mountpoint);
487 add_drives (struct drv *drv)
492 add_drives (drv->next);
494 r = guestfs_add_drive (g, drv->filename);
496 r = guestfs_add_drive_ro (g, drv->filename);
514 #define FISH "><fs> "
516 static char *line_read = NULL;
521 #ifdef HAVE_LIBREADLINE
529 line_read = readline (prompt ? FISH : "");
531 if (line_read && *line_read)
532 add_history_line (line_read);
537 #endif /* HAVE_LIBREADLINE */
539 static char buf[8192];
542 if (prompt) printf (FISH);
543 line_read = fgets (buf, sizeof buf, stdin);
546 len = strlen (line_read);
547 if (len > 0 && buf[len-1] == '\n') buf[len-1] = '\0';
561 int global_exit_on_error = !prompt;
566 "Welcome to guestfish, the libguestfs filesystem interactive shell for\n"
567 "editing virtual machine filesystems.\n"
569 "Type: 'help' for help with commands\n"
570 " 'quit' to quit the shell\n"
576 exit_on_error = global_exit_on_error;
578 buf = rl_gets (prompt);
584 /* Skip any initial whitespace before the command. */
586 while (*buf && c_isspace (*buf))
591 /* If the next character is '#' then this is a comment. */
592 if (*buf == '#') continue;
594 /* If the next character is '!' then pass the whole lot to system(3). */
602 (WTERMSIG (r) == SIGINT || WTERMSIG (r) == SIGQUIT)) ||
603 WEXITSTATUS (r) != 0)
609 /* If the next character is '-' allow the command to fail without
610 * exiting on error (just for this one command though).
618 /* Get the command (cannot be quoted). */
619 len = strcspn (buf, " \t");
621 if (len == 0) continue;
625 if (buf[len] == '\0') {
632 p += strspn (p, " \t");
634 /* Get the parameters. */
635 while (*p && i < sizeof argv / sizeof argv[0]) {
638 /* Parameters which start with quotes or pipes are treated
639 * specially. Bare parameters are delimited by whitespace.
643 len = strcspn (p, "\"");
644 if (p[len] == '\0') {
645 fprintf (stderr, _("%s: unterminated double quote\n"), program_name);
646 if (exit_on_error) exit (EXIT_FAILURE);
649 if (p[len+1] && (p[len+1] != ' ' && p[len+1] != '\t')) {
651 _("%s: command arguments not separated by whitespace\n"),
653 if (exit_on_error) exit (EXIT_FAILURE);
657 pend = p[len+1] ? &p[len+2] : &p[len+1];
658 } else if (*p == '\'') {
660 len = strcspn (p, "'");
661 if (p[len] == '\0') {
662 fprintf (stderr, _("%s: unterminated single quote\n"), program_name);
663 if (exit_on_error) exit (EXIT_FAILURE);
666 if (p[len+1] && (p[len+1] != ' ' && p[len+1] != '\t')) {
668 _("%s: command arguments not separated by whitespace\n"),
670 if (exit_on_error) exit (EXIT_FAILURE);
674 pend = p[len+1] ? &p[len+2] : &p[len+1];
675 } else if (*p == '|') {
680 } else if (*p == '[') {
684 while (*pend && c != 0) {
685 if (*pend == '[') c++;
686 else if (*pend == ']') c--;
691 _("%s: unterminated \"[...]\" sequence\n"), program_name);
692 if (exit_on_error) exit (EXIT_FAILURE);
695 if (*pend && (*pend != ' ' && *pend != '\t')) {
697 _("%s: command arguments not separated by whitespace\n"),
699 if (exit_on_error) exit (EXIT_FAILURE);
704 } else if (*p != ' ' && *p != '\t') {
705 /* If the first character is a ~ then note that this parameter
706 * is a candidate for ~username expansion. NB this does not
707 * apply to quoted parameters.
709 tilde_candidate = *p == '~';
710 len = strcspn (p, " \t");
717 fprintf (stderr, _("%s: internal error parsing string at '%s'\n"),
722 if (!tilde_candidate)
725 argv[i] = try_tilde_expansion (p);
730 p += strspn (p, " \t");
733 if (i == sizeof argv / sizeof argv[0]) {
734 fprintf (stderr, _("%s: too many arguments\n"), program_name);
735 if (exit_on_error) exit (EXIT_FAILURE);
742 if (issue_command (cmd, argv, pipe) == -1) {
743 if (exit_on_error) exit (EXIT_FAILURE);
748 if (prompt) printf ("\n");
752 cmdline (char *argv[], int optind, int argc)
759 if (optind >= argc) return;
761 cmd = argv[optind++];
762 if (STREQ (cmd, ":")) {
763 fprintf (stderr, _("%s: empty command on command line\n"), program_name);
766 params = &argv[optind];
768 /* Search for end of command list or ":" ... */
769 while (optind < argc && STRNEQ (argv[optind], ":"))
772 if (optind == argc) {
773 if (issue_command (cmd, params, NULL) == -1) exit (EXIT_FAILURE);
776 if (issue_command (cmd, params, NULL) == -1) exit (EXIT_FAILURE);
777 cmdline (argv, optind+1, argc);
782 issue_command (const char *cmd, char *argv[], const char *pipecmd)
785 int stdout_saved_fd = -1;
791 for (i = 0; argv[i] != NULL; ++i)
792 printf (" %s", argv[i]);
796 /* For | ... commands. Annoyingly we can't use popen(3) here. */
800 if (fflush (stdout) == EOF) {
801 perror ("failed to flush standard output");
805 perror ("pipe failed");
814 if (pid == 0) { /* Child process. */
816 if (dup2 (fd[0], 0) < 0) {
817 perror ("dup2 of stdin failed");
821 r = system (pipecmd);
826 _exit (WEXITSTATUS (r));
829 if ((stdout_saved_fd = dup (1)) < 0) {
830 perror ("failed to dup stdout");
834 if (dup2 (fd[1], 1) < 0) {
835 perror ("failed to dup stdout");
836 close (stdout_saved_fd);
842 for (argc = 0; argv[argc] != NULL; ++argc)
845 /* If --remote was set, then send this command to a remote process. */
847 r = rc_remote (remote_control, cmd, argc, argv, exit_on_error);
849 /* Otherwise execute it locally. */
850 else if (STRCASEEQ (cmd, "help")) {
854 display_command (argv[0]);
857 else if (STRCASEEQ (cmd, "quit") ||
858 STRCASEEQ (cmd, "exit") ||
859 STRCASEEQ (cmd, "q")) {
863 else if (STRCASEEQ (cmd, "alloc") ||
864 STRCASEEQ (cmd, "allocate"))
865 r = do_alloc (cmd, argc, argv);
866 else if (STRCASEEQ (cmd, "echo"))
867 r = do_echo (cmd, argc, argv);
868 else if (STRCASEEQ (cmd, "edit") ||
869 STRCASEEQ (cmd, "vi") ||
870 STRCASEEQ (cmd, "emacs"))
871 r = do_edit (cmd, argc, argv);
872 else if (STRCASEEQ (cmd, "lcd"))
873 r = do_lcd (cmd, argc, argv);
874 else if (STRCASEEQ (cmd, "glob"))
875 r = do_glob (cmd, argc, argv);
876 else if (STRCASEEQ (cmd, "more") ||
877 STRCASEEQ (cmd, "less"))
878 r = do_more (cmd, argc, argv);
879 else if (STRCASEEQ (cmd, "reopen"))
880 r = do_reopen (cmd, argc, argv);
881 else if (STRCASEEQ (cmd, "sparse"))
882 r = do_sparse (cmd, argc, argv);
883 else if (STRCASEEQ (cmd, "time"))
884 r = do_time (cmd, argc, argv);
886 r = run_action (cmd, argc, argv);
888 /* Always flush stdout after every command, so that messages, results
889 * etc appear immediately.
891 if (fflush (stdout) == EOF) {
892 perror ("failed to flush standard output");
898 if (dup2 (stdout_saved_fd, 1) < 0) {
899 perror ("failed to dup2 standard output");
902 close (stdout_saved_fd);
903 if (waitpid (pid, NULL, 0) < 0) {
904 perror ("waiting for command to complete");
913 list_builtin_commands (void)
915 /* help and quit should appear at the top */
916 printf ("%-20s %s\n",
917 "help", _("display a list of commands or help on a command"));
918 printf ("%-20s %s\n",
919 "quit", _("quit guestfish"));
921 printf ("%-20s %s\n",
922 "alloc", _("allocate an image"));
923 printf ("%-20s %s\n",
924 "echo", _("display a line of text"));
925 printf ("%-20s %s\n",
926 "edit", _("edit a file in the image"));
927 printf ("%-20s %s\n",
928 "lcd", _("local change directory"));
929 printf ("%-20s %s\n",
930 "glob", _("expand wildcards in command"));
931 printf ("%-20s %s\n",
932 "more", _("view a file in the pager"));
933 printf ("%-20s %s\n",
934 "reopen", _("close and reopen libguestfs handle"));
935 printf ("%-20s %s\n",
936 "sparse", _("allocate a sparse image file"));
937 printf ("%-20s %s\n",
938 "time", _("measure time taken to run command"));
940 /* actions are printed after this (see list_commands) */
944 display_builtin_command (const char *cmd)
946 /* help for actions is auto-generated, see display_command */
948 if (STRCASEEQ (cmd, "alloc") ||
949 STRCASEEQ (cmd, "allocate"))
950 printf (_("alloc - allocate an image\n"
951 " alloc <filename> <size>\n"
953 " This creates an empty (zeroed) file of the given size,\n"
954 " and then adds so it can be further examined.\n"
956 " For more advanced image creation, see qemu-img utility.\n"
958 " Size can be specified (where <nn> means a number):\n"
959 " <nn> number of kilobytes\n"
960 " eg: 1440 standard 3.5\" floppy\n"
961 " <nn>K or <nn>KB number of kilobytes\n"
962 " <nn>M or <nn>MB number of megabytes\n"
963 " <nn>G or <nn>GB number of gigabytes\n"
964 " <nn>T or <nn>TB number of terabytes\n"
965 " <nn>P or <nn>PB number of petabytes\n"
966 " <nn>E or <nn>EB number of exabytes\n"
967 " <nn>sects number of 512 byte sectors\n"));
968 else if (STRCASEEQ (cmd, "echo"))
969 printf (_("echo - display a line of text\n"
970 " echo [<params> ...]\n"
972 " This echos the parameters to the terminal.\n"));
973 else if (STRCASEEQ (cmd, "edit") ||
974 STRCASEEQ (cmd, "vi") ||
975 STRCASEEQ (cmd, "emacs"))
976 printf (_("edit - edit a file in the image\n"
979 " This is used to edit a file.\n"
981 " It is the equivalent of (and is implemented by)\n"
982 " running \"cat\", editing locally, and then \"write-file\".\n"
984 " Normally it uses $EDITOR, but if you use the aliases\n"
985 " \"vi\" or \"emacs\" you will get those editors.\n"
987 " NOTE: This will not work reliably for large files\n"
988 " (> 2 MB) or binary files containing \\0 bytes.\n"));
989 else if (STRCASEEQ (cmd, "lcd"))
990 printf (_("lcd - local change directory\n"
993 " Change guestfish's current directory. This command is\n"
994 " useful if you want to download files to a particular\n"
996 else if (STRCASEEQ (cmd, "glob"))
997 printf (_("glob - expand wildcards in command\n"
998 " glob <command> [<args> ...]\n"
1000 " Glob runs <command> with wildcards expanded in any\n"
1001 " command args. Note that the command is run repeatedly\n"
1002 " once for each expanded argument.\n"));
1003 else if (STRCASEEQ (cmd, "help"))
1004 printf (_("help - display a list of commands or help on a command\n"
1007 else if (STRCASEEQ (cmd, "more") ||
1008 STRCASEEQ (cmd, "less"))
1009 printf (_("more - view a file in the pager\n"
1010 " more <filename>\n"
1012 " This is used to view a file in the pager.\n"
1014 " It is the equivalent of (and is implemented by)\n"
1015 " running \"cat\" and using the pager.\n"
1017 " Normally it uses $PAGER, but if you use the alias\n"
1018 " \"less\" then it always uses \"less\".\n"
1020 " NOTE: This will not work reliably for large files\n"
1021 " (> 2 MB) or binary files containing \\0 bytes.\n"));
1022 else if (STRCASEEQ (cmd, "quit") ||
1023 STRCASEEQ (cmd, "exit") ||
1024 STRCASEEQ (cmd, "q"))
1025 printf (_("quit - quit guestfish\n"
1027 else if (STRCASEEQ (cmd, "reopen"))
1028 printf (_("reopen - close and reopen the libguestfs handle\n"
1031 "Close and reopen the libguestfs handle. It is not necessary to use\n"
1032 "this normally, because the handle is closed properly when guestfish\n"
1033 "exits. However this is occasionally useful for testing.\n"));
1034 else if (STRCASEEQ (cmd, "sparse"))
1035 printf (_("sparse - allocate a sparse image file\n"
1036 " sparse <filename> <size>\n"
1038 " This creates an empty sparse file of the given size,\n"
1039 " and then adds so it can be further examined.\n"
1041 " In all respects it works the same as the 'alloc'\n"
1042 " command, except that the image file is allocated\n"
1043 " sparsely, which means that disk blocks are not assigned\n"
1044 " to the file until they are needed. Sparse disk files\n"
1045 " only use space when written to, but they are slower\n"
1046 " and there is a danger you could run out of real disk\n"
1047 " space during a write operation.\n"
1049 " For more advanced image creation, see qemu-img utility.\n"
1051 " Size can be specified (where <nn> means a number):\n"
1052 " <nn> number of kilobytes\n"
1053 " eg: 1440 standard 3.5\" floppy\n"
1054 " <nn>K or <nn>KB number of kilobytes\n"
1055 " <nn>M or <nn>MB number of megabytes\n"
1056 " <nn>G or <nn>GB number of gigabytes\n"
1057 " <nn>T or <nn>TB number of terabytes\n"
1058 " <nn>P or <nn>PB number of petabytes\n"
1059 " <nn>E or <nn>EB number of exabytes\n"
1060 " <nn>sects number of 512 byte sectors\n"));
1061 else if (STRCASEEQ (cmd, "time"))
1062 printf (_("time - measure time taken to run command\n"
1063 " time <command> [<args> ...]\n"
1065 " This runs <command> as usual, and prints the elapsed\n"
1066 " time afterwards.\n"));
1068 fprintf (stderr, _("%s: command not known, use -h to list all commands\n"),
1073 free_strings (char **argv)
1077 for (argc = 0; argv[argc] != NULL; ++argc)
1083 count_strings (char *const *argv)
1087 for (c = 0; argv[c]; ++c)
1093 print_strings (char *const *argv)
1097 for (argc = 0; argv[argc] != NULL; ++argc)
1098 printf ("%s\n", argv[argc]);
1102 print_table (char *const *argv)
1106 for (i = 0; argv[i] != NULL; i += 2)
1107 printf ("%s: %s\n", argv[i], argv[i+1]);
1111 is_true (const char *str)
1114 STRCASENEQ (str, "0") &&
1115 STRCASENEQ (str, "f") &&
1116 STRCASENEQ (str, "false") &&
1117 STRCASENEQ (str, "n") &&
1118 STRCASENEQ (str, "no");
1121 /* Free strings from a non-NULL terminated char** */
1123 free_n_strings (char **str, size_t len)
1127 for (i = 0; i < len; i++) {
1134 parse_string_list (const char *str)
1137 size_t argv_len = 0;
1139 /* Current position pointer */
1140 const char *p = str;
1142 /* Token might be simple:
1145 * 'This is a single token'
1146 * or contain embedded single-quoted sections:
1147 * This' is a sing'l'e to'ken
1149 * The latter may seem over-complicated, but it's what a normal shell does.
1150 * Not doing it risks surprising somebody.
1152 * This outer loop is over complete tokens.
1158 /* Skip leading whitespace */
1159 p += strspn (p, " \t");
1163 /* This loop is over token 'fragments'. A token can be in multiple bits if
1164 * it contains single quotes. We also treat both sides of an escaped quote
1165 * as separate fragments because we can't just copy it: we have to remove
1168 while (*p && (!c_isblank (*p) || in_quote)) {
1169 const char *end = p;
1171 /* Check if the fragment starts with a quote */
1173 /* Toggle in_quote */
1174 in_quote = !in_quote;
1176 /* Skip the quote */
1180 /* If we're in a quote, look for an end quote */
1182 end += strcspn (end, "'");
1185 /* Otherwise, look for whitespace or a quote */
1187 end += strcspn (end, " \t'");
1190 /* Grow the token to accommodate the fragment */
1191 size_t tok_end = tok_len;
1193 char *tok_new = realloc (tok, tok_len + 1);
1194 if (NULL == tok_new) {
1196 free_n_strings (argv, argv_len);
1198 exit (EXIT_FAILURE);
1202 /* Check if we stopped on an escaped quote */
1203 if ('\'' == *end && end != p && *(end-1) == '\\') {
1204 /* Add everything before \' to the token */
1205 memcpy (&tok[tok_end], p, end - p - 1);
1208 tok[tok_len-1] = '\'';
1210 /* Already processed the quote */
1215 /* Add the whole fragment */
1216 memcpy (&tok[tok_end], p, end - p);
1222 /* We've reached the end of a token. We shouldn't still be in quotes. */
1224 fprintf (stderr, _("Runaway quote in string \"%s\"\n"), str);
1226 free_n_strings (argv, argv_len);
1231 /* Add this token if there is one. There might not be if there was
1232 * whitespace at the end of the input string */
1234 /* Add the NULL terminator */
1235 tok[tok_len] = '\0';
1237 /* Add the argument to the argument list */
1239 char **argv_new = realloc (argv, sizeof (*argv) * argv_len);
1240 if (NULL == argv_new) {
1242 free_n_strings (argv, argv_len-1);
1244 exit (EXIT_FAILURE);
1248 argv[argv_len-1] = tok;
1252 /* NULL terminate the argument list */
1254 char **argv_new = realloc (argv, sizeof (*argv) * argv_len);
1255 if (NULL == argv_new) {
1257 free_n_strings (argv, argv_len-1);
1258 exit (EXIT_FAILURE);
1262 argv[argv_len-1] = NULL;
1267 #ifdef HAVE_LIBREADLINE
1268 static char histfile[1024];
1269 static int nr_history_lines = 0;
1273 initialize_readline (void)
1275 #ifdef HAVE_LIBREADLINE
1278 home = getenv ("HOME");
1280 snprintf (histfile, sizeof histfile, "%s/.guestfish", home);
1282 (void) read_history (histfile);
1285 rl_readline_name = "guestfish";
1286 rl_attempted_completion_function = do_completion;
1291 cleanup_readline (void)
1293 #ifdef HAVE_LIBREADLINE
1296 if (histfile[0] != '\0') {
1297 fd = open (histfile, O_WRONLY|O_CREAT, 0644);
1304 (void) append_history (nr_history_lines, histfile);
1310 add_history_line (const char *line)
1312 #ifdef HAVE_LIBREADLINE
1319 xwrite (int fd, const void *v_buf, size_t len)
1322 const char *buf = v_buf;
1325 r = write (fd, buf, len);
1337 /* Resolve the special "win:..." form for Windows-specific paths.
1338 * This always returns a newly allocated string which is freed by the
1339 * caller function in "cmds.c".
1342 resolve_win_path (const char *path)
1347 if (STRCASENEQLEN (path, "win:", 4)) {
1348 ret = strdup (path);
1356 /* Drop drive letter, if it's "C:". */
1357 if (STRCASEEQLEN (path, "c:", 2))
1367 ret = strdup (path);
1373 /* Blindly convert any backslashes into forward slashes. Is this good? */
1374 for (i = 0; i < strlen (ret); ++i)
1378 char *t = guestfs_case_sensitive_path (g, ret);