+int
+issue_command (const char *cmd, char *argv[], const char *pipecmd)
+{
+ int argc;
+ int stdout_saved_fd = -1;
+ int pid = 0;
+ int i, r;
+
+ if (echo_commands) {
+ printf ("%s", cmd);
+ for (i = 0; argv[i] != NULL; ++i)
+ printf (" %s", argv[i]);
+ printf ("\n");
+ }
+
+ /* For | ... commands. Annoyingly we can't use popen(3) here. */
+ if (pipecmd) {
+ int fd[2];
+
+ fflush (stdout);
+ pipe (fd);
+ pid = fork ();
+ if (pid == -1) {
+ perror ("fork");
+ return -1;
+ }
+
+ if (pid == 0) { /* Child process. */
+ close (fd[1]);
+ dup2 (fd[0], 0);
+
+ r = system (pipecmd);
+ if (r == -1) {
+ perror (pipecmd);
+ _exit (1);
+ }
+ _exit (WEXITSTATUS (r));
+ }
+
+ stdout_saved_fd = dup (1);
+ close (fd[0]);
+ dup2 (fd[1], 1);
+ close (fd[1]);
+ }
+
+ for (argc = 0; argv[argc] != NULL; ++argc)
+ ;
+
+ /* If --remote was set, then send this command to a remote process. */
+ if (remote_control)
+ r = rc_remote (remote_control, cmd, argc, argv, exit_on_error);
+
+ /* Otherwise execute it locally. */
+ else if (strcasecmp (cmd, "help") == 0) {
+ if (argc == 0)
+ list_commands ();
+ else
+ display_command (argv[0]);
+ r = 0;
+ }
+ else if (strcasecmp (cmd, "quit") == 0 ||
+ strcasecmp (cmd, "exit") == 0 ||
+ strcasecmp (cmd, "q") == 0) {
+ quit = 1;
+ r = 0;
+ }
+ else if (strcasecmp (cmd, "alloc") == 0 ||
+ strcasecmp (cmd, "allocate") == 0)
+ r = do_alloc (cmd, argc, argv);
+ else if (strcasecmp (cmd, "echo") == 0)
+ r = do_echo (cmd, argc, argv);
+ else if (strcasecmp (cmd, "edit") == 0 ||
+ strcasecmp (cmd, "vi") == 0 ||
+ strcasecmp (cmd, "emacs") == 0)
+ r = do_edit (cmd, argc, argv);
+ else if (strcasecmp (cmd, "lcd") == 0)
+ r = do_lcd (cmd, argc, argv);
+ else if (strcasecmp (cmd, "glob") == 0)
+ r = do_glob (cmd, argc, argv);
+ else if (strcasecmp (cmd, "more") == 0 ||
+ strcasecmp (cmd, "less") == 0)
+ r = do_more (cmd, argc, argv);
+ else if (strcasecmp (cmd, "reopen") == 0)
+ r = do_reopen (cmd, argc, argv);
+ else if (strcasecmp (cmd, "time") == 0)
+ r = do_time (cmd, argc, argv);
+ else
+ r = run_action (cmd, argc, argv);
+
+ /* Always flush stdout after every command, so that messages, results
+ * etc appear immediately.
+ */
+ fflush (stdout);
+
+ if (pipecmd) {
+ close (1);
+ dup2 (stdout_saved_fd, 1);
+ close (stdout_saved_fd);
+ waitpid (pid, NULL, 0);
+ }
+
+ return r;
+}
+
+void
+list_builtin_commands (void)
+{
+ /* help and quit should appear at the top */
+ printf ("%-20s %s\n",
+ "help", _("display a list of commands or help on a command"));
+ printf ("%-20s %s\n",
+ "quit", _("quit guestfish"));
+
+ printf ("%-20s %s\n",
+ "alloc", _("allocate an image"));
+ printf ("%-20s %s\n",
+ "echo", _("display a line of text"));
+ printf ("%-20s %s\n",
+ "edit", _("edit a file in the image"));
+ printf ("%-20s %s\n",
+ "lcd", _("local change directory"));
+ printf ("%-20s %s\n",
+ "glob", _("expand wildcards in command"));
+ printf ("%-20s %s\n",
+ "more", _("view a file in the pager"));
+ printf ("%-20s %s\n",
+ "reopen", _("close and reopen libguestfs handle"));
+ printf ("%-20s %s\n",
+ "time", _("measure time taken to run command"));
+
+ /* actions are printed after this (see list_commands) */
+}
+
+void
+display_builtin_command (const char *cmd)
+{
+ /* help for actions is auto-generated, see display_command */
+
+ if (strcasecmp (cmd, "alloc") == 0 ||
+ strcasecmp (cmd, "allocate") == 0)
+ printf (_("alloc - allocate an image\n"
+ " alloc <filename> <size>\n"
+ "\n"
+ " This creates an empty (zeroed) file of the given size,\n"
+ " and then adds so it can be further examined.\n"
+ "\n"
+ " For more advanced image creation, see qemu-img utility.\n"
+ "\n"
+ " Size can be specified (where <nn> means a number):\n"
+ " <nn> number of kilobytes\n"
+ " eg: 1440 standard 3.5\" floppy\n"
+ " <nn>K or <nn>KB number of kilobytes\n"
+ " <nn>M or <nn>MB number of megabytes\n"
+ " <nn>G or <nn>GB number of gigabytes\n"
+ " <nn>sects number of 512 byte sectors\n"));
+ else if (strcasecmp (cmd, "echo") == 0)
+ printf (_("echo - display a line of text\n"
+ " echo [<params> ...]\n"
+ "\n"
+ " This echos the parameters to the terminal.\n"));
+ else if (strcasecmp (cmd, "edit") == 0 ||
+ strcasecmp (cmd, "vi") == 0 ||
+ strcasecmp (cmd, "emacs") == 0)
+ printf (_("edit - edit a file in the image\n"
+ " edit <filename>\n"
+ "\n"
+ " This is used to edit a file.\n"
+ "\n"
+ " It is the equivalent of (and is implemented by)\n"
+ " running \"cat\", editing locally, and then \"write-file\".\n"
+ "\n"
+ " Normally it uses $EDITOR, but if you use the aliases\n"
+ " \"vi\" or \"emacs\" you will get those editors.\n"
+ "\n"
+ " NOTE: This will not work reliably for large files\n"
+ " (> 2 MB) or binary files containing \\0 bytes.\n"));
+ else if (strcasecmp (cmd, "lcd") == 0)
+ printf (_("lcd - local change directory\n"
+ " lcd <directory>\n"
+ "\n"
+ " Change guestfish's current directory. This command is\n"
+ " useful if you want to download files to a particular\n"
+ " place.\n"));
+ else if (strcasecmp (cmd, "glob") == 0)
+ printf (_("glob - expand wildcards in command\n"
+ " glob <command> [<args> ...]\n"
+ "\n"
+ " Glob runs <command> with wildcards expanded in any\n"
+ " command args. Note that the command is run repeatedly\n"
+ " once for each expanded argument.\n"));
+ else if (strcasecmp (cmd, "help") == 0)
+ printf (_("help - display a list of commands or help on a command\n"
+ " help cmd\n"
+ " help\n"));
+ else if (strcasecmp (cmd, "more") == 0 ||
+ strcasecmp (cmd, "less") == 0)
+ printf (_("more - view a file in the pager\n"
+ " more <filename>\n"
+ "\n"
+ " This is used to view a file in the pager.\n"
+ "\n"
+ " It is the equivalent of (and is implemented by)\n"
+ " running \"cat\" and using the pager.\n"
+ "\n"
+ " Normally it uses $PAGER, but if you use the alias\n"
+ " \"less\" then it always uses \"less\".\n"
+ "\n"
+ " NOTE: This will not work reliably for large files\n"
+ " (> 2 MB) or binary files containing \\0 bytes.\n"));
+ else if (strcasecmp (cmd, "quit") == 0 ||
+ strcasecmp (cmd, "exit") == 0 ||
+ strcasecmp (cmd, "q") == 0)
+ printf (_("quit - quit guestfish\n"
+ " quit\n"));
+ else if (strcasecmp (cmd, "reopen") == 0)
+ printf (_("reopen - close and reopen the libguestfs handle\n"
+ " reopen\n"
+ "\n"
+ "Close and reopen the libguestfs handle. It is not necessary to use\n"
+ "this normally, because the handle is closed properly when guestfish\n"
+ "exits. However this is occasionally useful for testing.\n"));
+ else if (strcasecmp (cmd, "time") == 0)
+ printf (_("time - measure time taken to run command\n"
+ " time <command> [<args> ...]\n"
+ "\n"
+ " This runs <command> as usual, and prints the elapsed\n"
+ " time afterwards.\n"));
+ else
+ fprintf (stderr, _("%s: command not known, use -h to list all commands\n"),
+ cmd);
+}
+
+void
+free_strings (char **argv)
+{
+ int argc;
+
+ for (argc = 0; argv[argc] != NULL; ++argc)
+ free (argv[argc]);
+ free (argv);
+}
+
+int
+count_strings (char * const * const argv)
+{
+ int c;
+
+ for (c = 0; argv[c]; ++c)
+ ;
+ return c;
+}
+
+void
+print_strings (char * const * const argv)
+{
+ int argc;
+
+ for (argc = 0; argv[argc] != NULL; ++argc)
+ printf ("%s\n", argv[argc]);
+}
+
+void
+print_table (char * const * const argv)