fish: Allow '-' prefix on command line to override exit on error (RHBZ#578407).
[libguestfs.git] / fish / fish.c
index 2411f72..61a8405 100644 (file)
@@ -28,6 +28,7 @@
 #include <assert.h>
 #include <sys/types.h>
 #include <sys/wait.h>
+#include <locale.h>
 
 #ifdef HAVE_LIBREADLINE
 #include <readline/readline.h>
@@ -72,6 +73,7 @@ int echo_commands = 0;
 int remote_control_listen = 0;
 int remote_control = 0;
 int exit_on_error = 1;
+int command_num = 0;
 
 int
 launch (guestfs_h *_g)
@@ -766,6 +768,15 @@ cmdline (char *argv[], int optind, int argc)
     fprintf (stderr, _("%s: empty command on command line\n"), program_name);
     exit (EXIT_FAILURE);
   }
+
+  /* Allow -cmd on the command line to mean (temporarily) override
+   * the normal exit on error (RHBZ#578407).
+   */
+  if (cmd[0] == '-') {
+    exit_on_error = 0;
+    cmd++;
+  }
+
   params = &argv[optind];
 
   /* Search for end of command list or ":" ... */
@@ -773,10 +784,12 @@ cmdline (char *argv[], int optind, int argc)
     optind++;
 
   if (optind == argc) {
-    if (issue_command (cmd, params, NULL) == -1) exit (EXIT_FAILURE);
+    if (issue_command (cmd, params, NULL) == -1 && exit_on_error)
+        exit (EXIT_FAILURE);
   } else {
     argv[optind] = NULL;
-    if (issue_command (cmd, params, NULL) == -1) exit (EXIT_FAILURE);
+    if (issue_command (cmd, params, NULL) == -1 && exit_on_error)
+      exit (EXIT_FAILURE);
     cmdline (argv, optind+1, argc);
   }
 }
@@ -789,6 +802,9 @@ issue_command (const char *cmd, char *argv[], const char *pipecmd)
   int pid = 0;
   int i, r;
 
+  /* This counts the commands issued, starting at 1. */
+  command_num++;
+
   if (echo_commands) {
     printf ("%s", cmd);
     for (i = 0; argv[i] != NULL; ++i)
@@ -1072,6 +1088,21 @@ display_builtin_command (const char *cmd)
              cmd);
 }
 
+/* This is printed when the user types in an unknown command for the
+ * first command issued.  A common case is the user doing:
+ *   guestfish disk.img
+ * expecting guestfish to open 'disk.img' (in fact, this tried to
+ * run a command 'disk.img').
+ */
+void
+extended_help_message (void)
+{
+  fprintf (stderr,
+           _("Did you mean to open a disk image?  guestfish -a disk.img\n"
+             "For a list of commands:             guestfish -h\n"
+             "For complete documentation:         man guestfish\n"));
+}
+
 void
 free_strings (char **argv)
 {