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>
33 #ifdef HAVE_LIBREADLINE
34 #include <readline/readline.h>
35 #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)
84 if (guestfs_wait_ready (g) == -1)
90 static void __attribute__((noreturn))
93 if (status != EXIT_SUCCESS)
94 fprintf (stderr, _("Try `%s --help' for more information.\n"),
98 _("%s: guest filesystem shell\n"
99 "%s lets you edit virtual machine filesystems\n"
100 "Copyright (C) 2009 Red Hat Inc.\n"
102 " %s [--options] cmd [: cmd : cmd ...]\n"
103 " %s -i libvirt-domain\n"
104 " %s -i disk-image(s)\n"
105 "or for interactive use:\n"
107 "or from a shell script:\n"
113 " -h|--cmd-help List available commands\n"
114 " -h|--cmd-help cmd Display detailed help on 'cmd'\n"
115 " -a|--add image Add image\n"
116 " -D|--no-dest-paths Don't tab-complete paths from guest fs\n"
117 " -f|--file file Read commands from file\n"
118 " -i|--inspector Run virt-inspector to get disk mountpoints\n"
119 " --listen Listen for remote commands\n"
120 " -m|--mount dev[:mnt] Mount dev on mnt (if omitted, /)\n"
121 " -n|--no-sync Don't autosync\n"
122 " --remote[=pid] Send commands to remote %s\n"
123 " -r|--ro Mount read-only\n"
124 " --selinux Enable SELinux support\n"
125 " -v|--verbose Verbose messages\n"
126 " -x Echo each command before executing it\n"
127 " -V|--version Display version and exit\n"
128 "For more information, see the manpage %s(1).\n"),
129 program_name, program_name, program_name,
130 program_name, program_name, program_name,
131 program_name, program_name, program_name);
137 main (int argc, char *argv[])
139 /* Set global program name that is not polluted with libtool artifacts. */
140 set_program_name (argv[0]);
142 atexit (close_stdout);
144 enum { HELP_OPTION = CHAR_MAX + 1 };
146 static const char *options = "a:Df:h::im:nrv?Vx";
147 static const struct option long_options[] = {
148 { "add", 1, 0, 'a' },
149 { "cmd-help", 2, 0, 'h' },
150 { "file", 1, 0, 'f' },
151 { "help", 0, 0, HELP_OPTION },
152 { "inspector", 0, 0, 'i' },
153 { "listen", 0, 0, 0 },
154 { "mount", 1, 0, 'm' },
155 { "no-dest-paths", 0, 0, 'D' },
156 { "no-sync", 0, 0, 'n' },
157 { "remote", 2, 0, 0 },
159 { "selinux", 0, 0, 0 },
160 { "verbose", 0, 0, 'v' },
161 { "version", 0, 0, 'V' },
164 struct drv *drvs = NULL;
166 struct mp *mps = NULL;
168 char *p, *file = NULL;
174 initialize_readline ();
176 memset (&sa, 0, sizeof sa);
177 sa.sa_handler = SIG_IGN;
178 sa.sa_flags = SA_RESTART;
179 sigaction (SIGPIPE, &sa, NULL);
181 /* guestfs_create is meant to be a lightweight operation, so
182 * it's OK to do it early here.
184 g = guestfs_create ();
186 fprintf (stderr, _("guestfs_create: failed to create handle\n"));
190 guestfs_set_autosync (g, 1);
192 /* If developing, add ./appliance to the path. Note that libtools
193 * interferes with this because uninstalled guestfish is a shell
194 * script that runs the real program with an absolute path. Detect
197 * BUT if LIBGUESTFS_PATH environment variable is already set by
198 * the user, then don't override it.
200 if (getenv ("LIBGUESTFS_PATH") == NULL &&
202 (argv[0][0] != '/' || strstr (argv[0], "/.libs/lt-") != NULL))
203 guestfs_set_path (g, "appliance:" GUESTFS_DEFAULT_PATH);
205 /* CAUTION: we are careful to modify argv[0] here, only after
206 * using it just above.
208 * getopt_long uses argv[0], so give it the sanitized name. Save a copy
209 * of the original, in case it's needed in virt-inspector mode, below.
211 char *real_argv0 = argv[0];
212 argv[0] = bad_cast (program_name);
215 c = getopt_long (argc, argv, options, long_options, &option_index);
219 case 0: /* options which are long only */
220 if (strcmp (long_options[option_index].name, "listen") == 0)
221 remote_control_listen = 1;
222 else if (strcmp (long_options[option_index].name, "remote") == 0) {
224 if (sscanf (optarg, "%d", &remote_control) != 1) {
225 fprintf (stderr, _("%s: --listen=PID: PID was not a number: %s\n"),
226 program_name, optarg);
230 p = getenv ("GUESTFISH_PID");
231 if (!p || sscanf (p, "%d", &remote_control) != 1) {
232 fprintf (stderr, _("%s: remote: $GUESTFISH_PID must be set"
233 " to the PID of the remote process\n"),
238 } else if (strcmp (long_options[option_index].name, "selinux") == 0) {
239 guestfs_set_selinux (g, 1);
241 fprintf (stderr, _("%s: unknown long option: %s (%d)\n"),
242 program_name, long_options[option_index].name, option_index);
248 if (access (optarg, R_OK) != 0) {
252 drv = malloc (sizeof (struct drv));
257 drv->filename = optarg;
263 complete_dest_paths = 0;
268 fprintf (stderr, _("%s: only one -f parameter can be given\n"),
277 display_command (optarg);
278 else if (argv[optind] && argv[optind][0] != '-')
279 display_command (argv[optind++]);
289 mp = malloc (sizeof (struct mp));
294 p = strchr (optarg, ':');
297 mp->mountpoint = p+1;
299 mp->mountpoint = bad_cast ("/");
306 guestfs_set_autosync (g, 0);
315 guestfs_set_verbose (g, verbose);
319 printf ("%s %s\n", program_name, PACKAGE_VERSION);
334 /* Inspector mode invalidates most of the other arguments. */
339 if (drvs || mps || remote_control_listen || remote_control ||
340 guestfs_get_selinux (g)) {
341 fprintf (stderr, _("%s: cannot use -i option with -a, -m,"
342 " --listen, --remote or --selinux\n"),
346 if (optind >= argc) {
348 _("%s: -i requires a libvirt domain or path(s) to disk image(s)\n"),
353 strcpy (cmd, "a=`virt-inspector");
354 while (optind < argc) {
355 if (strlen (cmd) + strlen (argv[optind]) + strlen (real_argv0) + 60
358 _("%s: virt-inspector command too long for fixed-size buffer\n"),
363 strcat (cmd, argv[optind]);
369 strcat (cmd, " --ro-fish");
371 strcat (cmd, " --fish");
373 sprintf (&cmd[strlen(cmd)], "` && %s $a", real_argv0);
375 if (guestfs_get_verbose (g))
377 if (!guestfs_get_autosync (g))
382 "%s -i: running virt-inspector command:\n%s\n", program_name, cmd);
389 exit (WEXITSTATUS (r));
392 /* If we've got drives to add, add them now. */
395 /* If we've got mountpoints, we must launch the guest and mount them. */
397 if (launch (g) == -1) exit (1);
401 /* Remote control? */
402 if (remote_control_listen && remote_control) {
404 _("%s: cannot use --listen and --remote options at the same time\n"),
409 if (remote_control_listen) {
412 _("%s: extra parameters on the command line with --listen flag\n"),
418 _("%s: cannot use --listen and --file options at the same time\n"),
425 /* -f (file) parameter? */
428 if (open (file, O_RDONLY) == -1) {
434 /* Interactive, shell script, or command(s) on the command line? */
435 if (optind >= argc) {
442 cmdline (argv, optind, argc);
450 pod2text (const char *name, const char *shortdesc, const char *str)
454 fp = popen ("pod2text", "w");
456 /* pod2text failed, maybe not found, so let's just print the
457 * source instead, since that's better than doing nothing.
459 printf ("%s - %s\n\n%s\n", name, shortdesc, str);
462 fprintf (fp, "=head1 %s - %s\n\n", name, shortdesc);
467 /* List is built in reverse order, so mount them in reverse order. */
469 mount_mps (struct mp *mp)
474 mount_mps (mp->next);
476 r = guestfs_mount (g, mp->device, mp->mountpoint);
478 r = guestfs_mount_ro (g, mp->device, mp->mountpoint);
485 add_drives (struct drv *drv)
490 add_drives (drv->next);
492 r = guestfs_add_drive (g, drv->filename);
494 r = guestfs_add_drive_ro (g, drv->filename);
512 #define FISH "><fs> "
514 static char *line_read = NULL;
519 #ifdef HAVE_LIBREADLINE
527 line_read = readline (prompt ? FISH : "");
529 if (line_read && *line_read)
530 add_history_line (line_read);
535 #endif /* HAVE_LIBREADLINE */
537 static char buf[8192];
540 if (prompt) printf (FISH);
541 line_read = fgets (buf, sizeof buf, stdin);
544 len = strlen (line_read);
545 if (len > 0 && buf[len-1] == '\n') buf[len-1] = '\0';
559 int global_exit_on_error = !prompt;
564 "Welcome to guestfish, the libguestfs filesystem interactive shell for\n"
565 "editing virtual machine filesystems.\n"
567 "Type: 'help' for help with commands\n"
568 " 'quit' to quit the shell\n"
574 exit_on_error = global_exit_on_error;
576 buf = rl_gets (prompt);
582 /* Skip any initial whitespace before the command. */
584 while (*buf && isspace (*buf))
589 /* If the next character is '#' then this is a comment. */
590 if (*buf == '#') continue;
592 /* If the next character is '!' then pass the whole lot to system(3). */
600 (WTERMSIG (r) == SIGINT || WTERMSIG (r) == SIGQUIT)) ||
601 WEXITSTATUS (r) != 0)
607 /* If the next character is '-' allow the command to fail without
608 * exiting on error (just for this one command though).
616 /* Get the command (cannot be quoted). */
617 len = strcspn (buf, " \t");
619 if (len == 0) continue;
623 if (buf[len] == '\0') {
630 p += strspn (p, " \t");
632 /* Get the parameters. */
633 while (*p && i < sizeof argv / sizeof argv[0]) {
636 /* Parameters which start with quotes or pipes are treated
637 * specially. Bare parameters are delimited by whitespace.
641 len = strcspn (p, "\"");
642 if (p[len] == '\0') {
643 fprintf (stderr, _("%s: unterminated double quote\n"), program_name);
644 if (exit_on_error) exit (1);
647 if (p[len+1] && (p[len+1] != ' ' && p[len+1] != '\t')) {
649 _("%s: command arguments not separated by whitespace\n"),
651 if (exit_on_error) exit (1);
655 pend = p[len+1] ? &p[len+2] : &p[len+1];
656 } else if (*p == '\'') {
658 len = strcspn (p, "'");
659 if (p[len] == '\0') {
660 fprintf (stderr, _("%s: unterminated single quote\n"), program_name);
661 if (exit_on_error) exit (1);
664 if (p[len+1] && (p[len+1] != ' ' && p[len+1] != '\t')) {
666 _("%s: command arguments not separated by whitespace\n"),
668 if (exit_on_error) exit (1);
672 pend = p[len+1] ? &p[len+2] : &p[len+1];
673 } else if (*p == '|') {
678 } else if (*p == '[') {
682 while (*pend && c != 0) {
683 if (*pend == '[') c++;
684 else if (*pend == ']') c--;
689 _("%s: unterminated \"[...]\" sequence\n"), program_name);
690 if (exit_on_error) exit (1);
693 if (*pend && (*pend != ' ' && *pend != '\t')) {
695 _("%s: command arguments not separated by whitespace\n"),
697 if (exit_on_error) exit (1);
702 } else if (*p != ' ' && *p != '\t') {
703 /* If the first character is a ~ then note that this parameter
704 * is a candidate for ~username expansion. NB this does not
705 * apply to quoted parameters.
707 tilde_candidate = *p == '~';
708 len = strcspn (p, " \t");
715 fprintf (stderr, _("%s: internal error parsing string at '%s'\n"),
720 if (!tilde_candidate)
723 argv[i] = try_tilde_expansion (p);
728 p += strspn (p, " \t");
731 if (i == sizeof argv / sizeof argv[0]) {
732 fprintf (stderr, _("%s: too many arguments\n"), program_name);
733 if (exit_on_error) exit (1);
740 if (issue_command (cmd, argv, pipe) == -1) {
741 if (exit_on_error) exit (1);
746 if (prompt) printf ("\n");
750 cmdline (char *argv[], int optind, int argc)
757 if (optind >= argc) return;
759 cmd = argv[optind++];
760 if (strcmp (cmd, ":") == 0) {
761 fprintf (stderr, _("%s: empty command on command line\n"), program_name);
764 params = &argv[optind];
766 /* Search for end of command list or ":" ... */
767 while (optind < argc && strcmp (argv[optind], ":") != 0)
770 if (optind == argc) {
771 if (issue_command (cmd, params, NULL) == -1) exit (1);
774 if (issue_command (cmd, params, NULL) == -1) exit (1);
775 cmdline (argv, optind+1, argc);
780 issue_command (const char *cmd, char *argv[], const char *pipecmd)
783 int stdout_saved_fd = -1;
789 for (i = 0; argv[i] != NULL; ++i)
790 printf (" %s", argv[i]);
794 /* For | ... commands. Annoyingly we can't use popen(3) here. */
798 if (fflush (stdout) == EOF) {
799 perror ("failed to flush standard output");
803 perror ("pipe failed");
812 if (pid == 0) { /* Child process. */
814 if (dup2 (fd[0], 0) < 0) {
815 perror ("dup2 of stdin failed");
819 r = system (pipecmd);
824 _exit (WEXITSTATUS (r));
827 if ((stdout_saved_fd = dup (1)) < 0) {
828 perror ("failed to dup stdout");
832 if (dup2 (fd[1], 1) < 0) {
833 perror ("failed to dup stdout");
834 close (stdout_saved_fd);
840 for (argc = 0; argv[argc] != NULL; ++argc)
843 /* If --remote was set, then send this command to a remote process. */
845 r = rc_remote (remote_control, cmd, argc, argv, exit_on_error);
847 /* Otherwise execute it locally. */
848 else if (strcasecmp (cmd, "help") == 0) {
852 display_command (argv[0]);
855 else if (strcasecmp (cmd, "quit") == 0 ||
856 strcasecmp (cmd, "exit") == 0 ||
857 strcasecmp (cmd, "q") == 0) {
861 else if (strcasecmp (cmd, "alloc") == 0 ||
862 strcasecmp (cmd, "allocate") == 0)
863 r = do_alloc (cmd, argc, argv);
864 else if (strcasecmp (cmd, "echo") == 0)
865 r = do_echo (cmd, argc, argv);
866 else if (strcasecmp (cmd, "edit") == 0 ||
867 strcasecmp (cmd, "vi") == 0 ||
868 strcasecmp (cmd, "emacs") == 0)
869 r = do_edit (cmd, argc, argv);
870 else if (strcasecmp (cmd, "lcd") == 0)
871 r = do_lcd (cmd, argc, argv);
872 else if (strcasecmp (cmd, "glob") == 0)
873 r = do_glob (cmd, argc, argv);
874 else if (strcasecmp (cmd, "more") == 0 ||
875 strcasecmp (cmd, "less") == 0)
876 r = do_more (cmd, argc, argv);
877 else if (strcasecmp (cmd, "reopen") == 0)
878 r = do_reopen (cmd, argc, argv);
879 else if (strcasecmp (cmd, "time") == 0)
880 r = do_time (cmd, argc, argv);
882 r = run_action (cmd, argc, argv);
884 /* Always flush stdout after every command, so that messages, results
885 * etc appear immediately.
887 if (fflush (stdout) == EOF) {
888 perror ("failed to flush standard output");
894 if (dup2 (stdout_saved_fd, 1) < 0) {
895 perror ("failed to dup2 standard output");
898 close (stdout_saved_fd);
899 if (waitpid (pid, NULL, 0) < 0) {
900 perror ("waiting for command to complete");
909 list_builtin_commands (void)
911 /* help and quit should appear at the top */
912 printf ("%-20s %s\n",
913 "help", _("display a list of commands or help on a command"));
914 printf ("%-20s %s\n",
915 "quit", _("quit guestfish"));
917 printf ("%-20s %s\n",
918 "alloc", _("allocate an image"));
919 printf ("%-20s %s\n",
920 "echo", _("display a line of text"));
921 printf ("%-20s %s\n",
922 "edit", _("edit a file in the image"));
923 printf ("%-20s %s\n",
924 "lcd", _("local change directory"));
925 printf ("%-20s %s\n",
926 "glob", _("expand wildcards in command"));
927 printf ("%-20s %s\n",
928 "more", _("view a file in the pager"));
929 printf ("%-20s %s\n",
930 "reopen", _("close and reopen libguestfs handle"));
931 printf ("%-20s %s\n",
932 "time", _("measure time taken to run command"));
934 /* actions are printed after this (see list_commands) */
938 display_builtin_command (const char *cmd)
940 /* help for actions is auto-generated, see display_command */
942 if (strcasecmp (cmd, "alloc") == 0 ||
943 strcasecmp (cmd, "allocate") == 0)
944 printf (_("alloc - allocate an image\n"
945 " alloc <filename> <size>\n"
947 " This creates an empty (zeroed) file of the given size,\n"
948 " and then adds so it can be further examined.\n"
950 " For more advanced image creation, see qemu-img utility.\n"
952 " Size can be specified (where <nn> means a number):\n"
953 " <nn> number of kilobytes\n"
954 " eg: 1440 standard 3.5\" floppy\n"
955 " <nn>K or <nn>KB number of kilobytes\n"
956 " <nn>M or <nn>MB number of megabytes\n"
957 " <nn>G or <nn>GB number of gigabytes\n"
958 " <nn>sects number of 512 byte sectors\n"));
959 else if (strcasecmp (cmd, "echo") == 0)
960 printf (_("echo - display a line of text\n"
961 " echo [<params> ...]\n"
963 " This echos the parameters to the terminal.\n"));
964 else if (strcasecmp (cmd, "edit") == 0 ||
965 strcasecmp (cmd, "vi") == 0 ||
966 strcasecmp (cmd, "emacs") == 0)
967 printf (_("edit - edit a file in the image\n"
970 " This is used to edit a file.\n"
972 " It is the equivalent of (and is implemented by)\n"
973 " running \"cat\", editing locally, and then \"write-file\".\n"
975 " Normally it uses $EDITOR, but if you use the aliases\n"
976 " \"vi\" or \"emacs\" you will get those editors.\n"
978 " NOTE: This will not work reliably for large files\n"
979 " (> 2 MB) or binary files containing \\0 bytes.\n"));
980 else if (strcasecmp (cmd, "lcd") == 0)
981 printf (_("lcd - local change directory\n"
984 " Change guestfish's current directory. This command is\n"
985 " useful if you want to download files to a particular\n"
987 else if (strcasecmp (cmd, "glob") == 0)
988 printf (_("glob - expand wildcards in command\n"
989 " glob <command> [<args> ...]\n"
991 " Glob runs <command> with wildcards expanded in any\n"
992 " command args. Note that the command is run repeatedly\n"
993 " once for each expanded argument.\n"));
994 else if (strcasecmp (cmd, "help") == 0)
995 printf (_("help - display a list of commands or help on a command\n"
998 else if (strcasecmp (cmd, "more") == 0 ||
999 strcasecmp (cmd, "less") == 0)
1000 printf (_("more - view a file in the pager\n"
1001 " more <filename>\n"
1003 " This is used to view a file in the pager.\n"
1005 " It is the equivalent of (and is implemented by)\n"
1006 " running \"cat\" and using the pager.\n"
1008 " Normally it uses $PAGER, but if you use the alias\n"
1009 " \"less\" then it always uses \"less\".\n"
1011 " NOTE: This will not work reliably for large files\n"
1012 " (> 2 MB) or binary files containing \\0 bytes.\n"));
1013 else if (strcasecmp (cmd, "quit") == 0 ||
1014 strcasecmp (cmd, "exit") == 0 ||
1015 strcasecmp (cmd, "q") == 0)
1016 printf (_("quit - quit guestfish\n"
1018 else if (strcasecmp (cmd, "reopen") == 0)
1019 printf (_("reopen - close and reopen the libguestfs handle\n"
1022 "Close and reopen the libguestfs handle. It is not necessary to use\n"
1023 "this normally, because the handle is closed properly when guestfish\n"
1024 "exits. However this is occasionally useful for testing.\n"));
1025 else if (strcasecmp (cmd, "time") == 0)
1026 printf (_("time - measure time taken to run command\n"
1027 " time <command> [<args> ...]\n"
1029 " This runs <command> as usual, and prints the elapsed\n"
1030 " time afterwards.\n"));
1032 fprintf (stderr, _("%s: command not known, use -h to list all commands\n"),
1037 free_strings (char **argv)
1041 for (argc = 0; argv[argc] != NULL; ++argc)
1047 count_strings (char *const *argv)
1051 for (c = 0; argv[c]; ++c)
1057 print_strings (char *const *argv)
1061 for (argc = 0; argv[argc] != NULL; ++argc)
1062 printf ("%s\n", argv[argc]);
1066 print_table (char *const *argv)
1070 for (i = 0; argv[i] != NULL; i += 2)
1071 printf ("%s: %s\n", argv[i], argv[i+1]);
1075 is_true (const char *str)
1078 strcasecmp (str, "0") != 0 &&
1079 strcasecmp (str, "f") != 0 &&
1080 strcasecmp (str, "false") != 0 &&
1081 strcasecmp (str, "n") != 0 &&
1082 strcasecmp (str, "no") != 0;
1085 /* Free strings from a non-NULL terminated char** */
1087 free_n_strings (char **str, size_t len)
1091 for (i = 0; i < len; i++) {
1098 parse_string_list (const char *str)
1101 size_t argv_len = 0;
1103 /* Current position pointer */
1104 const char *p = str;
1106 /* Token might be simple:
1109 * 'This is a single token'
1110 * or contain embedded single-quoted sections:
1111 * This' is a sing'l'e to'ken
1113 * The latter may seem over-complicated, but it's what a normal shell does.
1114 * Not doing it risks surprising somebody.
1116 * This outer loop is over complete tokens.
1122 /* Skip leading whitespace */
1123 p += strspn (p, " \t");
1127 /* This loop is over token 'fragments'. A token can be in multiple bits if
1128 * it contains single quotes. We also treat both sides of an escaped quote
1129 * as separate fragments because we can't just copy it: we have to remove
1132 while (*p && (!isblank (*p) || in_quote)) {
1133 const char *end = p;
1135 /* Check if the fragment starts with a quote */
1137 /* Toggle in_quote */
1138 in_quote = !in_quote;
1140 /* Skip the quote */
1144 /* If we're in a quote, look for an end quote */
1146 end += strcspn (end, "'");
1149 /* Otherwise, look for whitespace or a quote */
1151 end += strcspn (end, " \t'");
1154 /* Grow the token to accommodate the fragment */
1155 size_t tok_end = tok_len;
1157 char *tok_new = realloc (tok, tok_len + 1);
1158 if (NULL == tok_new) {
1160 free_n_strings (argv, argv_len);
1166 /* Check if we stopped on an escaped quote */
1167 if ('\'' == *end && end != p && *(end-1) == '\\') {
1168 /* Add everything before \' to the token */
1169 memcpy (&tok[tok_end], p, end - p - 1);
1172 tok[tok_len-1] = '\'';
1174 /* Already processed the quote */
1179 /* Add the whole fragment */
1180 memcpy (&tok[tok_end], p, end - p);
1186 /* We've reached the end of a token. We shouldn't still be in quotes. */
1188 fprintf (stderr, _("Runaway quote in string \"%s\"\n"), str);
1190 free_n_strings (argv, argv_len);
1195 /* Add this token if there is one. There might not be if there was
1196 * whitespace at the end of the input string */
1198 /* Add the NULL terminator */
1199 tok[tok_len] = '\0';
1201 /* Add the argument to the argument list */
1203 char **argv_new = realloc (argv, sizeof (*argv) * argv_len);
1204 if (NULL == argv_new) {
1206 free_n_strings (argv, argv_len-1);
1212 argv[argv_len-1] = tok;
1216 /* NULL terminate the argument list */
1218 char **argv_new = realloc (argv, sizeof (*argv) * argv_len);
1219 if (NULL == argv_new) {
1221 free_n_strings (argv, argv_len-1);
1226 argv[argv_len-1] = NULL;
1231 #ifdef HAVE_LIBREADLINE
1232 static char histfile[1024];
1233 static int nr_history_lines = 0;
1237 initialize_readline (void)
1239 #ifdef HAVE_LIBREADLINE
1242 home = getenv ("HOME");
1244 snprintf (histfile, sizeof histfile, "%s/.guestfish", home);
1246 (void) read_history (histfile);
1249 rl_readline_name = "guestfish";
1250 rl_attempted_completion_function = do_completion;
1255 cleanup_readline (void)
1257 #ifdef HAVE_LIBREADLINE
1260 if (histfile[0] != '\0') {
1261 fd = open (histfile, O_WRONLY|O_CREAT, 0644);
1268 (void) append_history (nr_history_lines, histfile);
1274 add_history_line (const char *line)
1276 #ifdef HAVE_LIBREADLINE
1283 xwrite (int fd, const void *v_buf, size_t len)
1286 const char *buf = v_buf;
1289 r = write (fd, buf, len);