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>
33 #ifdef HAVE_LIBREADLINE
34 #include <readline/readline.h>
35 #include <readline/history.h>
56 static void add_drives (struct drv *drv);
57 static void mount_mps (struct mp *mp);
58 static void interactive (void);
59 static void shell_script (void);
60 static void script (int prompt);
61 static void cmdline (char *argv[], int optind, int argc);
62 static void initialize_readline (void);
63 static void cleanup_readline (void);
64 static void add_history_line (const char *);
66 /* Currently open libguestfs handle. */
72 int echo_commands = 0;
73 int remote_control_listen = 0;
74 int remote_control = 0;
75 int exit_on_error = 1;
78 launch (guestfs_h *_g)
82 if (guestfs_is_config (g)) {
83 if (guestfs_launch (g) == -1)
89 static void __attribute__((noreturn))
92 if (status != EXIT_SUCCESS)
93 fprintf (stderr, _("Try `%s --help' for more information.\n"),
97 _("%s: guest filesystem shell\n"
98 "%s lets you edit virtual machine filesystems\n"
99 "Copyright (C) 2009 Red Hat Inc.\n"
101 " %s [--options] cmd [: cmd : cmd ...]\n"
102 " %s -i libvirt-domain\n"
103 " %s -i disk-image(s)\n"
104 "or for interactive use:\n"
106 "or from a shell script:\n"
112 " -h|--cmd-help List available commands\n"
113 " -h|--cmd-help cmd Display detailed help on 'cmd'\n"
114 " -a|--add image Add image\n"
115 " -D|--no-dest-paths Don't tab-complete paths from guest fs\n"
116 " -f|--file file Read commands from file\n"
117 " -i|--inspector Run virt-inspector to get disk mountpoints\n"
118 " --listen Listen for remote commands\n"
119 " -m|--mount dev[:mnt] Mount dev on mnt (if omitted, /)\n"
120 " -n|--no-sync Don't autosync\n"
121 " --remote[=pid] Send commands to remote %s\n"
122 " -r|--ro Mount read-only\n"
123 " --selinux Enable SELinux support\n"
124 " -v|--verbose Verbose messages\n"
125 " -x Echo each command before executing it\n"
126 " -V|--version Display version and exit\n"
127 "For more information, see the manpage %s(1).\n"),
128 program_name, program_name, program_name,
129 program_name, program_name, program_name,
130 program_name, program_name, program_name);
136 main (int argc, char *argv[])
138 /* Set global program name that is not polluted with libtool artifacts. */
139 set_program_name (argv[0]);
141 atexit (close_stdout);
143 setlocale (LC_ALL, "");
144 bindtextdomain (PACKAGE, LOCALEBASEDIR);
145 textdomain (PACKAGE);
147 enum { HELP_OPTION = CHAR_MAX + 1 };
149 static const char *options = "a:Df:h::im:nrv?Vx";
150 static const struct option long_options[] = {
151 { "add", 1, 0, 'a' },
152 { "cmd-help", 2, 0, 'h' },
153 { "file", 1, 0, 'f' },
154 { "help", 0, 0, HELP_OPTION },
155 { "inspector", 0, 0, 'i' },
156 { "listen", 0, 0, 0 },
157 { "mount", 1, 0, 'm' },
158 { "no-dest-paths", 0, 0, 'D' },
159 { "no-sync", 0, 0, 'n' },
160 { "remote", 2, 0, 0 },
162 { "selinux", 0, 0, 0 },
163 { "verbose", 0, 0, 'v' },
164 { "version", 0, 0, 'V' },
167 struct drv *drvs = NULL;
169 struct mp *mps = NULL;
171 char *p, *file = NULL;
177 initialize_readline ();
179 memset (&sa, 0, sizeof sa);
180 sa.sa_handler = SIG_IGN;
181 sa.sa_flags = SA_RESTART;
182 sigaction (SIGPIPE, &sa, NULL);
184 /* guestfs_create is meant to be a lightweight operation, so
185 * it's OK to do it early here.
187 g = guestfs_create ();
189 fprintf (stderr, _("guestfs_create: failed to create handle\n"));
193 guestfs_set_autosync (g, 1);
195 /* If developing, add ./appliance to the path. Note that libtools
196 * interferes with this because uninstalled guestfish is a shell
197 * script that runs the real program with an absolute path. Detect
200 * BUT if LIBGUESTFS_PATH environment variable is already set by
201 * the user, then don't override it.
203 if (getenv ("LIBGUESTFS_PATH") == NULL &&
205 (argv[0][0] != '/' || strstr (argv[0], "/.libs/lt-") != NULL))
206 guestfs_set_path (g, "appliance:" GUESTFS_DEFAULT_PATH);
208 /* CAUTION: we are careful to modify argv[0] here, only after
209 * using it just above.
211 * getopt_long uses argv[0], so give it the sanitized name. Save a copy
212 * of the original, in case it's needed in virt-inspector mode, below.
214 char *real_argv0 = argv[0];
215 argv[0] = bad_cast (program_name);
218 c = getopt_long (argc, argv, options, long_options, &option_index);
222 case 0: /* options which are long only */
223 if (STREQ (long_options[option_index].name, "listen"))
224 remote_control_listen = 1;
225 else if (STREQ (long_options[option_index].name, "remote")) {
227 if (sscanf (optarg, "%d", &remote_control) != 1) {
228 fprintf (stderr, _("%s: --listen=PID: PID was not a number: %s\n"),
229 program_name, optarg);
233 p = getenv ("GUESTFISH_PID");
234 if (!p || sscanf (p, "%d", &remote_control) != 1) {
235 fprintf (stderr, _("%s: remote: $GUESTFISH_PID must be set"
236 " to the PID of the remote process\n"),
241 } else if (STREQ (long_options[option_index].name, "selinux")) {
242 guestfs_set_selinux (g, 1);
244 fprintf (stderr, _("%s: unknown long option: %s (%d)\n"),
245 program_name, long_options[option_index].name, option_index);
251 if (access (optarg, R_OK) != 0) {
255 drv = malloc (sizeof (struct drv));
260 drv->filename = optarg;
266 complete_dest_paths = 0;
271 fprintf (stderr, _("%s: only one -f parameter can be given\n"),
280 display_command (optarg);
281 else if (argv[optind] && argv[optind][0] != '-')
282 display_command (argv[optind++]);
292 mp = malloc (sizeof (struct mp));
297 p = strchr (optarg, ':');
300 mp->mountpoint = p+1;
302 mp->mountpoint = bad_cast ("/");
309 guestfs_set_autosync (g, 0);
318 guestfs_set_verbose (g, verbose);
322 printf ("%s %s\n", program_name, PACKAGE_VERSION);
330 usage (EXIT_SUCCESS);
333 usage (EXIT_FAILURE);
337 /* Inspector mode invalidates most of the other arguments. */
342 if (drvs || mps || remote_control_listen || remote_control ||
343 guestfs_get_selinux (g)) {
344 fprintf (stderr, _("%s: cannot use -i option with -a, -m,"
345 " --listen, --remote or --selinux\n"),
349 if (optind >= argc) {
351 _("%s: -i requires a libvirt domain or path(s) to disk image(s)\n"),
356 strcpy (cmd, "a=`virt-inspector");
357 while (optind < argc) {
358 if (strlen (cmd) + strlen (argv[optind]) + strlen (real_argv0) + 60
361 _("%s: virt-inspector command too long for fixed-size buffer\n"),
366 strcat (cmd, argv[optind]);
372 strcat (cmd, " --ro-fish");
374 strcat (cmd, " --fish");
376 sprintf (&cmd[strlen(cmd)], "` && %s $a", real_argv0);
378 if (guestfs_get_verbose (g))
380 if (!guestfs_get_autosync (g))
385 "%s -i: running virt-inspector command:\n%s\n", program_name, cmd);
392 exit (WEXITSTATUS (r));
395 /* If we've got drives to add, add them now. */
398 /* If we've got mountpoints, we must launch the guest and mount them. */
400 if (launch (g) == -1) exit (EXIT_FAILURE);
404 /* Remote control? */
405 if (remote_control_listen && remote_control) {
407 _("%s: cannot use --listen and --remote options at the same time\n"),
412 if (remote_control_listen) {
415 _("%s: extra parameters on the command line with --listen flag\n"),
421 _("%s: cannot use --listen and --file options at the same time\n"),
428 /* -f (file) parameter? */
431 if (open (file, O_RDONLY) == -1) {
437 /* Interactive, shell script, or command(s) on the command line? */
438 if (optind >= argc) {
445 cmdline (argv, optind, argc);
453 pod2text (const char *name, const char *shortdesc, const char *str)
457 fp = popen ("pod2text", "w");
459 /* pod2text failed, maybe not found, so let's just print the
460 * source instead, since that's better than doing nothing.
462 printf ("%s - %s\n\n%s\n", name, shortdesc, str);
465 fprintf (fp, "=head1 NAME\n\n%s - %s\n\n", name, shortdesc);
470 /* List is built in reverse order, so mount them in reverse order. */
472 mount_mps (struct mp *mp)
477 mount_mps (mp->next);
479 /* Don't use guestfs_mount here because that will default to mount
480 * options -o sync,noatime. For more information, see guestfs(3)
481 * section "LIBGUESTFS GOTCHAS".
483 const char *options = read_only ? "ro" : "";
484 r = guestfs_mount_options (g, options, mp->device, mp->mountpoint);
491 add_drives (struct drv *drv)
496 add_drives (drv->next);
498 r = guestfs_add_drive (g, drv->filename);
500 r = guestfs_add_drive_ro (g, drv->filename);
518 #define FISH "><fs> "
520 static char *line_read = NULL;
525 #ifdef HAVE_LIBREADLINE
533 line_read = readline (prompt ? FISH : "");
535 if (line_read && *line_read)
536 add_history_line (line_read);
541 #endif /* HAVE_LIBREADLINE */
543 static char buf[8192];
546 if (prompt) printf (FISH);
547 line_read = fgets (buf, sizeof buf, stdin);
550 len = strlen (line_read);
551 if (len > 0 && buf[len-1] == '\n') buf[len-1] = '\0';
565 int global_exit_on_error = !prompt;
570 "Welcome to guestfish, the libguestfs filesystem interactive shell for\n"
571 "editing virtual machine filesystems.\n"
573 "Type: 'help' for help with commands\n"
574 " 'quit' to quit the shell\n"
580 exit_on_error = global_exit_on_error;
582 buf = rl_gets (prompt);
588 /* Skip any initial whitespace before the command. */
590 while (*buf && c_isspace (*buf))
595 /* If the next character is '#' then this is a comment. */
596 if (*buf == '#') continue;
598 /* If the next character is '!' then pass the whole lot to system(3). */
606 (WTERMSIG (r) == SIGINT || WTERMSIG (r) == SIGQUIT)) ||
607 WEXITSTATUS (r) != 0)
613 /* If the next character is '-' allow the command to fail without
614 * exiting on error (just for this one command though).
622 /* Get the command (cannot be quoted). */
623 len = strcspn (buf, " \t");
625 if (len == 0) continue;
629 if (buf[len] == '\0') {
636 p += strspn (p, " \t");
638 /* Get the parameters. */
639 while (*p && i < sizeof argv / sizeof argv[0]) {
642 /* Parameters which start with quotes or pipes are treated
643 * specially. Bare parameters are delimited by whitespace.
647 len = strcspn (p, "\"");
648 if (p[len] == '\0') {
649 fprintf (stderr, _("%s: unterminated double quote\n"), program_name);
650 if (exit_on_error) exit (EXIT_FAILURE);
653 if (p[len+1] && (p[len+1] != ' ' && p[len+1] != '\t')) {
655 _("%s: command arguments not separated by whitespace\n"),
657 if (exit_on_error) exit (EXIT_FAILURE);
661 pend = p[len+1] ? &p[len+2] : &p[len+1];
662 } else if (*p == '\'') {
664 len = strcspn (p, "'");
665 if (p[len] == '\0') {
666 fprintf (stderr, _("%s: unterminated single quote\n"), program_name);
667 if (exit_on_error) exit (EXIT_FAILURE);
670 if (p[len+1] && (p[len+1] != ' ' && p[len+1] != '\t')) {
672 _("%s: command arguments not separated by whitespace\n"),
674 if (exit_on_error) exit (EXIT_FAILURE);
678 pend = p[len+1] ? &p[len+2] : &p[len+1];
679 } else if (*p == '|') {
684 } else if (*p == '[') {
688 while (*pend && c != 0) {
689 if (*pend == '[') c++;
690 else if (*pend == ']') c--;
695 _("%s: unterminated \"[...]\" sequence\n"), program_name);
696 if (exit_on_error) exit (EXIT_FAILURE);
699 if (*pend && (*pend != ' ' && *pend != '\t')) {
701 _("%s: command arguments not separated by whitespace\n"),
703 if (exit_on_error) exit (EXIT_FAILURE);
708 } else if (*p != ' ' && *p != '\t') {
709 /* If the first character is a ~ then note that this parameter
710 * is a candidate for ~username expansion. NB this does not
711 * apply to quoted parameters.
713 tilde_candidate = *p == '~';
714 len = strcspn (p, " \t");
721 fprintf (stderr, _("%s: internal error parsing string at '%s'\n"),
726 if (!tilde_candidate)
729 argv[i] = try_tilde_expansion (p);
734 p += strspn (p, " \t");
737 if (i == sizeof argv / sizeof argv[0]) {
738 fprintf (stderr, _("%s: too many arguments\n"), program_name);
739 if (exit_on_error) exit (EXIT_FAILURE);
746 if (issue_command (cmd, argv, pipe) == -1) {
747 if (exit_on_error) exit (EXIT_FAILURE);
752 if (prompt) printf ("\n");
756 cmdline (char *argv[], int optind, int argc)
763 if (optind >= argc) return;
765 cmd = argv[optind++];
766 if (STREQ (cmd, ":")) {
767 fprintf (stderr, _("%s: empty command on command line\n"), program_name);
770 params = &argv[optind];
772 /* Search for end of command list or ":" ... */
773 while (optind < argc && STRNEQ (argv[optind], ":"))
776 if (optind == argc) {
777 if (issue_command (cmd, params, NULL) == -1) exit (EXIT_FAILURE);
780 if (issue_command (cmd, params, NULL) == -1) exit (EXIT_FAILURE);
781 cmdline (argv, optind+1, argc);
786 issue_command (const char *cmd, char *argv[], const char *pipecmd)
789 int stdout_saved_fd = -1;
795 for (i = 0; argv[i] != NULL; ++i)
796 printf (" %s", argv[i]);
800 /* For | ... commands. Annoyingly we can't use popen(3) here. */
804 if (fflush (stdout) == EOF) {
805 perror ("failed to flush standard output");
809 perror ("pipe failed");
818 if (pid == 0) { /* Child process. */
820 if (dup2 (fd[0], 0) < 0) {
821 perror ("dup2 of stdin failed");
825 r = system (pipecmd);
830 _exit (WEXITSTATUS (r));
833 if ((stdout_saved_fd = dup (1)) < 0) {
834 perror ("failed to dup stdout");
838 if (dup2 (fd[1], 1) < 0) {
839 perror ("failed to dup stdout");
840 close (stdout_saved_fd);
846 for (argc = 0; argv[argc] != NULL; ++argc)
849 /* If --remote was set, then send this command to a remote process. */
851 r = rc_remote (remote_control, cmd, argc, argv, exit_on_error);
853 /* Otherwise execute it locally. */
854 else if (STRCASEEQ (cmd, "help")) {
858 display_command (argv[0]);
861 else if (STRCASEEQ (cmd, "quit") ||
862 STRCASEEQ (cmd, "exit") ||
863 STRCASEEQ (cmd, "q")) {
867 else if (STRCASEEQ (cmd, "alloc") ||
868 STRCASEEQ (cmd, "allocate"))
869 r = do_alloc (cmd, argc, argv);
870 else if (STRCASEEQ (cmd, "echo"))
871 r = do_echo (cmd, argc, argv);
872 else if (STRCASEEQ (cmd, "edit") ||
873 STRCASEEQ (cmd, "vi") ||
874 STRCASEEQ (cmd, "emacs"))
875 r = do_edit (cmd, argc, argv);
876 else if (STRCASEEQ (cmd, "lcd"))
877 r = do_lcd (cmd, argc, argv);
878 else if (STRCASEEQ (cmd, "glob"))
879 r = do_glob (cmd, argc, argv);
880 else if (STRCASEEQ (cmd, "more") ||
881 STRCASEEQ (cmd, "less"))
882 r = do_more (cmd, argc, argv);
883 else if (STRCASEEQ (cmd, "reopen"))
884 r = do_reopen (cmd, argc, argv);
885 else if (STRCASEEQ (cmd, "sparse"))
886 r = do_sparse (cmd, argc, argv);
887 else if (STRCASEEQ (cmd, "time"))
888 r = do_time (cmd, argc, argv);
890 r = run_action (cmd, argc, argv);
892 /* Always flush stdout after every command, so that messages, results
893 * etc appear immediately.
895 if (fflush (stdout) == EOF) {
896 perror ("failed to flush standard output");
902 if (dup2 (stdout_saved_fd, 1) < 0) {
903 perror ("failed to dup2 standard output");
906 close (stdout_saved_fd);
907 if (waitpid (pid, NULL, 0) < 0) {
908 perror ("waiting for command to complete");
917 list_builtin_commands (void)
919 /* help and quit should appear at the top */
920 printf ("%-20s %s\n",
921 "help", _("display a list of commands or help on a command"));
922 printf ("%-20s %s\n",
923 "quit", _("quit guestfish"));
925 printf ("%-20s %s\n",
926 "alloc", _("allocate an image"));
927 printf ("%-20s %s\n",
928 "echo", _("display a line of text"));
929 printf ("%-20s %s\n",
930 "edit", _("edit a file in the image"));
931 printf ("%-20s %s\n",
932 "lcd", _("local change directory"));
933 printf ("%-20s %s\n",
934 "glob", _("expand wildcards in command"));
935 printf ("%-20s %s\n",
936 "more", _("view a file in the pager"));
937 printf ("%-20s %s\n",
938 "reopen", _("close and reopen libguestfs handle"));
939 printf ("%-20s %s\n",
940 "sparse", _("allocate a sparse image file"));
941 printf ("%-20s %s\n",
942 "time", _("measure time taken to run command"));
944 /* actions are printed after this (see list_commands) */
948 display_builtin_command (const char *cmd)
950 /* help for actions is auto-generated, see display_command */
952 if (STRCASEEQ (cmd, "alloc") ||
953 STRCASEEQ (cmd, "allocate"))
954 printf (_("alloc - allocate an image\n"
955 " alloc <filename> <size>\n"
957 " This creates an empty (zeroed) file of the given size,\n"
958 " and then adds so it can be further examined.\n"
960 " For more advanced image creation, see qemu-img utility.\n"
962 " Size can be specified (where <nn> means a number):\n"
963 " <nn> number of kilobytes\n"
964 " eg: 1440 standard 3.5\" floppy\n"
965 " <nn>K or <nn>KB number of kilobytes\n"
966 " <nn>M or <nn>MB number of megabytes\n"
967 " <nn>G or <nn>GB number of gigabytes\n"
968 " <nn>T or <nn>TB number of terabytes\n"
969 " <nn>P or <nn>PB number of petabytes\n"
970 " <nn>E or <nn>EB number of exabytes\n"
971 " <nn>sects number of 512 byte sectors\n"));
972 else if (STRCASEEQ (cmd, "echo"))
973 printf (_("echo - display a line of text\n"
974 " echo [<params> ...]\n"
976 " This echos the parameters to the terminal.\n"));
977 else if (STRCASEEQ (cmd, "edit") ||
978 STRCASEEQ (cmd, "vi") ||
979 STRCASEEQ (cmd, "emacs"))
980 printf (_("edit - edit a file in the image\n"
983 " This is used to edit a file.\n"
985 " It is the equivalent of (and is implemented by)\n"
986 " running \"cat\", editing locally, and then \"write-file\".\n"
988 " Normally it uses $EDITOR, but if you use the aliases\n"
989 " \"vi\" or \"emacs\" you will get those editors.\n"
991 " NOTE: This will not work reliably for large files\n"
992 " (> 2 MB) or binary files containing \\0 bytes.\n"));
993 else if (STRCASEEQ (cmd, "lcd"))
994 printf (_("lcd - local change directory\n"
997 " Change guestfish's current directory. This command is\n"
998 " useful if you want to download files to a particular\n"
1000 else if (STRCASEEQ (cmd, "glob"))
1001 printf (_("glob - expand wildcards in command\n"
1002 " glob <command> [<args> ...]\n"
1004 " Glob runs <command> with wildcards expanded in any\n"
1005 " command args. Note that the command is run repeatedly\n"
1006 " once for each expanded argument.\n"));
1007 else if (STRCASEEQ (cmd, "help"))
1008 printf (_("help - display a list of commands or help on a command\n"
1011 else if (STRCASEEQ (cmd, "more") ||
1012 STRCASEEQ (cmd, "less"))
1013 printf (_("more - view a file in the pager\n"
1014 " more <filename>\n"
1016 " This is used to view a file in the pager.\n"
1018 " It is the equivalent of (and is implemented by)\n"
1019 " running \"cat\" and using the pager.\n"
1021 " Normally it uses $PAGER, but if you use the alias\n"
1022 " \"less\" then it always uses \"less\".\n"
1024 " NOTE: This will not work reliably for large files\n"
1025 " (> 2 MB) or binary files containing \\0 bytes.\n"));
1026 else if (STRCASEEQ (cmd, "quit") ||
1027 STRCASEEQ (cmd, "exit") ||
1028 STRCASEEQ (cmd, "q"))
1029 printf (_("quit - quit guestfish\n"
1031 else if (STRCASEEQ (cmd, "reopen"))
1032 printf (_("reopen - close and reopen the libguestfs handle\n"
1035 "Close and reopen the libguestfs handle. It is not necessary to use\n"
1036 "this normally, because the handle is closed properly when guestfish\n"
1037 "exits. However this is occasionally useful for testing.\n"));
1038 else if (STRCASEEQ (cmd, "sparse"))
1039 printf (_("sparse - allocate a sparse image file\n"
1040 " sparse <filename> <size>\n"
1042 " This creates an empty sparse file of the given size,\n"
1043 " and then adds so it can be further examined.\n"
1045 " In all respects it works the same as the 'alloc'\n"
1046 " command, except that the image file is allocated\n"
1047 " sparsely, which means that disk blocks are not assigned\n"
1048 " to the file until they are needed. Sparse disk files\n"
1049 " only use space when written to, but they are slower\n"
1050 " and there is a danger you could run out of real disk\n"
1051 " space during a write operation.\n"
1053 " For more advanced image creation, see qemu-img utility.\n"
1055 " Size can be specified (where <nn> means a number):\n"
1056 " <nn> number of kilobytes\n"
1057 " eg: 1440 standard 3.5\" floppy\n"
1058 " <nn>K or <nn>KB number of kilobytes\n"
1059 " <nn>M or <nn>MB number of megabytes\n"
1060 " <nn>G or <nn>GB number of gigabytes\n"
1061 " <nn>T or <nn>TB number of terabytes\n"
1062 " <nn>P or <nn>PB number of petabytes\n"
1063 " <nn>E or <nn>EB number of exabytes\n"
1064 " <nn>sects number of 512 byte sectors\n"));
1065 else if (STRCASEEQ (cmd, "time"))
1066 printf (_("time - measure time taken to run command\n"
1067 " time <command> [<args> ...]\n"
1069 " This runs <command> as usual, and prints the elapsed\n"
1070 " time afterwards.\n"));
1072 fprintf (stderr, _("%s: command not known, use -h to list all commands\n"),
1077 free_strings (char **argv)
1081 for (argc = 0; argv[argc] != NULL; ++argc)
1087 count_strings (char *const *argv)
1091 for (c = 0; argv[c]; ++c)
1097 print_strings (char *const *argv)
1101 for (argc = 0; argv[argc] != NULL; ++argc)
1102 printf ("%s\n", argv[argc]);
1106 print_table (char *const *argv)
1110 for (i = 0; argv[i] != NULL; i += 2)
1111 printf ("%s: %s\n", argv[i], argv[i+1]);
1115 is_true (const char *str)
1118 STRCASENEQ (str, "0") &&
1119 STRCASENEQ (str, "f") &&
1120 STRCASENEQ (str, "false") &&
1121 STRCASENEQ (str, "n") &&
1122 STRCASENEQ (str, "no");
1125 /* Free strings from a non-NULL terminated char** */
1127 free_n_strings (char **str, size_t len)
1131 for (i = 0; i < len; i++) {
1138 parse_string_list (const char *str)
1141 size_t argv_len = 0;
1143 /* Current position pointer */
1144 const char *p = str;
1146 /* Token might be simple:
1149 * 'This is a single token'
1150 * or contain embedded single-quoted sections:
1151 * This' is a sing'l'e to'ken
1153 * The latter may seem over-complicated, but it's what a normal shell does.
1154 * Not doing it risks surprising somebody.
1156 * This outer loop is over complete tokens.
1162 /* Skip leading whitespace */
1163 p += strspn (p, " \t");
1167 /* This loop is over token 'fragments'. A token can be in multiple bits if
1168 * it contains single quotes. We also treat both sides of an escaped quote
1169 * as separate fragments because we can't just copy it: we have to remove
1172 while (*p && (!c_isblank (*p) || in_quote)) {
1173 const char *end = p;
1175 /* Check if the fragment starts with a quote */
1177 /* Toggle in_quote */
1178 in_quote = !in_quote;
1180 /* Skip the quote */
1184 /* If we're in a quote, look for an end quote */
1186 end += strcspn (end, "'");
1189 /* Otherwise, look for whitespace or a quote */
1191 end += strcspn (end, " \t'");
1194 /* Grow the token to accommodate the fragment */
1195 size_t tok_end = tok_len;
1197 char *tok_new = realloc (tok, tok_len + 1);
1198 if (NULL == tok_new) {
1200 free_n_strings (argv, argv_len);
1202 exit (EXIT_FAILURE);
1206 /* Check if we stopped on an escaped quote */
1207 if ('\'' == *end && end != p && *(end-1) == '\\') {
1208 /* Add everything before \' to the token */
1209 memcpy (&tok[tok_end], p, end - p - 1);
1212 tok[tok_len-1] = '\'';
1214 /* Already processed the quote */
1219 /* Add the whole fragment */
1220 memcpy (&tok[tok_end], p, end - p);
1226 /* We've reached the end of a token. We shouldn't still be in quotes. */
1228 fprintf (stderr, _("Runaway quote in string \"%s\"\n"), str);
1230 free_n_strings (argv, argv_len);
1235 /* Add this token if there is one. There might not be if there was
1236 * whitespace at the end of the input string */
1238 /* Add the NULL terminator */
1239 tok[tok_len] = '\0';
1241 /* Add the argument to the argument list */
1243 char **argv_new = realloc (argv, sizeof (*argv) * argv_len);
1244 if (NULL == argv_new) {
1246 free_n_strings (argv, argv_len-1);
1248 exit (EXIT_FAILURE);
1252 argv[argv_len-1] = tok;
1256 /* NULL terminate the argument list */
1258 char **argv_new = realloc (argv, sizeof (*argv) * argv_len);
1259 if (NULL == argv_new) {
1261 free_n_strings (argv, argv_len-1);
1262 exit (EXIT_FAILURE);
1266 argv[argv_len-1] = NULL;
1271 #ifdef HAVE_LIBREADLINE
1272 static char histfile[1024];
1273 static int nr_history_lines = 0;
1277 initialize_readline (void)
1279 #ifdef HAVE_LIBREADLINE
1282 home = getenv ("HOME");
1284 snprintf (histfile, sizeof histfile, "%s/.guestfish", home);
1286 (void) read_history (histfile);
1289 rl_readline_name = "guestfish";
1290 rl_attempted_completion_function = do_completion;
1295 cleanup_readline (void)
1297 #ifdef HAVE_LIBREADLINE
1300 if (histfile[0] != '\0') {
1301 fd = open (histfile, O_WRONLY|O_CREAT, 0644);
1308 #ifdef HAVE_APPEND_HISTORY
1309 (void) append_history (nr_history_lines, histfile);
1311 (void) write_history (histfile);
1318 add_history_line (const char *line)
1320 #ifdef HAVE_LIBREADLINE
1327 xwrite (int fd, const void *v_buf, size_t len)
1330 const char *buf = v_buf;
1333 r = write (fd, buf, len);
1345 /* Resolve the special "win:..." form for Windows-specific paths.
1346 * This always returns a newly allocated string which is freed by the
1347 * caller function in "cmds.c".
1350 resolve_win_path (const char *path)
1355 if (STRCASENEQLEN (path, "win:", 4)) {
1356 ret = strdup (path);
1364 /* Drop drive letter, if it's "C:". */
1365 if (STRCASEEQLEN (path, "c:", 2))
1375 ret = strdup (path);
1381 /* Blindly convert any backslashes into forward slashes. Is this good? */
1382 for (i = 0; i < strlen (ret); ++i)
1386 char *t = guestfs_case_sensitive_path (g, ret);