fish: help command return error for non-existent commands (RHBZ#597145).
[libguestfs.git] / fish / fish.c
index a630cdc..470c625 100644 (file)
@@ -65,7 +65,9 @@ static void script (int prompt);
 static void cmdline (char *argv[], int optind, int argc);
 static void initialize_readline (void);
 static void cleanup_readline (void);
+#ifdef HAVE_LIBREADLINE
 static void add_history_line (const char *);
+#endif
 static void print_shell_quote (FILE *stream, const char *str);
 
 /* Currently open libguestfs handle. */
@@ -302,14 +304,18 @@ main (int argc, char *argv[])
       file = optarg;
       break;
 
-    case 'h':
+    case 'h': {
+      int r = 0;
+
       if (optarg)
-        display_command (optarg);
+        r = display_command (optarg);
       else if (argv[optind] && argv[optind][0] != '-')
-        display_command (argv[optind++]);
+        r = display_command (argv[optind++]);
       else
         list_commands ();
-      exit (EXIT_SUCCESS);
+
+      exit (r == 0 ? EXIT_SUCCESS : EXIT_FAILURE);
+    }
 
     case 'i':
       inspector = 1;
@@ -955,11 +961,11 @@ issue_command (const char *cmd, char *argv[], const char *pipecmd)
 
   /* Otherwise execute it locally. */
   else if (STRCASEEQ (cmd, "help")) {
-    if (argc == 0)
+    if (argc == 0) {
       list_commands ();
-    else
-      display_command (argv[0]);
-    r = 0;
+      r = 0;
+    } else
+      r = display_command (argv[0]);
   }
   else if (STRCASEEQ (cmd, "quit") ||
            STRCASEEQ (cmd, "exit") ||
@@ -990,6 +996,8 @@ issue_command (const char *cmd, char *argv[], const char *pipecmd)
     r = do_reopen (cmd, argc, argv);
   else if (STRCASEEQ (cmd, "sparse"))
     r = do_sparse (cmd, argc, argv);
+  else if (STRCASEEQ (cmd, "supported"))
+    r = do_supported (cmd, argc, argv);
   else if (STRCASEEQ (cmd, "time"))
     r = do_time (cmd, argc, argv);
   else
@@ -1047,18 +1055,20 @@ list_builtin_commands (void)
   printf ("%-20s %s\n",
           "sparse", _("allocate a sparse image file"));
   printf ("%-20s %s\n",
+          "supported", _("list supported groups of commands"));
+  printf ("%-20s %s\n",
           "time", _("measure time taken to run command"));
 
   /* actions are printed after this (see list_commands) */
 }
 
-void
+int
 display_builtin_command (const char *cmd)
 {
   /* help for actions is auto-generated, see display_command */
 
   if (STRCASEEQ (cmd, "alloc") ||
-      STRCASEEQ (cmd, "allocate"))
+      STRCASEEQ (cmd, "allocate")) {
     printf (_("alloc - allocate an image\n"
               "     alloc <filename> <size>\n"
               "\n"
@@ -1067,63 +1077,69 @@ display_builtin_command (const char *cmd)
               "\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>T or <nn>TB  number of terabytes\n"
-              "    <nn>P or <nn>PB  number of petabytes\n"
-              "    <nn>E or <nn>EB  number of exabytes\n"
-              "    <nn>sects        number of 512 byte sectors\n"));
-  else if (STRCASEEQ (cmd, "echo"))
+              "    Size can be specified using standard suffixes, eg. '1M'.\n"
+              ));
+    return 0;
+  }
+  else if (STRCASEEQ (cmd, "echo")) {
     printf (_("echo - display a line of text\n"
               "     echo [<params> ...]\n"
               "\n"
               "    This echos the parameters to the terminal.\n"));
+    return 0;
+  }
   else if (STRCASEEQ (cmd, "edit") ||
            STRCASEEQ (cmd, "vi") ||
-           STRCASEEQ (cmd, "emacs"))
+           STRCASEEQ (cmd, "emacs")) {
     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"
+              "    running \"cat\", editing locally, and then \"write\".\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 (STRCASEEQ (cmd, "lcd"))
+    return 0;
+  }
+  else if (STRCASEEQ (cmd, "lcd")) {
     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 (STRCASEEQ (cmd, "glob"))
+    return 0;
+  }
+  else if (STRCASEEQ (cmd, "glob")) {
     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"));
+    return 0;
+  }
   else if (STRCASEEQ (cmd, "man") ||
-           STRCASEEQ (cmd, "manual"))
+           STRCASEEQ (cmd, "manual")) {
     printf (_("man - read the manual\n"
               "    man\n"
               "\n"
               "    Opens the manual page for guestfish.\n"));
-  else if (STRCASEEQ (cmd, "help"))
+    return 0;
+  }
+  else if (STRCASEEQ (cmd, "help")) {
     printf (_("help - display a list of commands or help on a command\n"
               "     help cmd\n"
               "     help\n"));
+    return 0;
+  }
   else if (STRCASEEQ (cmd, "more") ||
-           STRCASEEQ (cmd, "less"))
+           STRCASEEQ (cmd, "less")) {
     printf (_("more - view a file in the pager\n"
               "     more <filename>\n"
               "\n"
@@ -1137,19 +1153,25 @@ display_builtin_command (const char *cmd)
               "\n"
               "    NOTE: This will not work reliably for large files\n"
               "    (> 2 MB) or binary files containing \\0 bytes.\n"));
+    return 0;
+  }
   else if (STRCASEEQ (cmd, "quit") ||
            STRCASEEQ (cmd, "exit") ||
-           STRCASEEQ (cmd, "q"))
+           STRCASEEQ (cmd, "q")) {
     printf (_("quit - quit guestfish\n"
               "     quit\n"));
-  else if (STRCASEEQ (cmd, "reopen"))
+    return 0;
+  }
+  else if (STRCASEEQ (cmd, "reopen")) {
     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 (STRCASEEQ (cmd, "sparse"))
+    return 0;
+  }
+  else if (STRCASEEQ (cmd, "sparse")) {
     printf (_("sparse - allocate a sparse image file\n"
               "     sparse <filename> <size>\n"
               "\n"
@@ -1166,25 +1188,35 @@ display_builtin_command (const char *cmd)
               "\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>T or <nn>TB  number of terabytes\n"
-              "    <nn>P or <nn>PB  number of petabytes\n"
-              "    <nn>E or <nn>EB  number of exabytes\n"
-              "    <nn>sects        number of 512 byte sectors\n"));
-  else if (STRCASEEQ (cmd, "time"))
+              "    Size can be specified using standard suffixes, eg. '1M'.\n"
+              ));
+    return 0;
+  }
+  else if (STRCASEEQ (cmd, "supported")) {
+    printf (_("supported - list supported groups of commands\n"
+              "     supported\n"
+              "\n"
+              "    This command returns a list of the optional groups\n"
+              "    known to the daemon, and indicates which ones are\n"
+              "    supported by this build of the libguestfs appliance.\n"
+              "\n"
+              "    See also guestfs(3) section AVAILABILITY.\n"
+              ));
+    return 0;
+  }
+  else if (STRCASEEQ (cmd, "time")) {
     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
+    return 0;
+  }
+  else {
     fprintf (stderr, _("%s: command not known, use -h to list all commands\n"),
              cmd);
+    return -1;
+  }
 }
 
 /* This is printed when the user types in an unknown command for the
@@ -1417,6 +1449,13 @@ initialize_readline (void)
 
   rl_readline_name = "guestfish";
   rl_attempted_completion_function = do_completion;
+
+  /* Note that .inputrc (or /etc/inputrc) is not read until the first
+   * call the readline(), which happens later.  Therefore, these
+   * provide default values which can be overridden by the user if
+   * they wish.
+   */
+  (void) rl_variable_bind ("completion-ignore-case", "on");
 #endif
 }
 
@@ -1443,14 +1482,14 @@ cleanup_readline (void)
 #endif
 }
 
+#ifdef HAVE_LIBREADLINE
 static void
 add_history_line (const char *line)
 {
-#ifdef HAVE_LIBREADLINE
   add_history (line);
   nr_history_lines++;
-#endif
 }
+#endif
 
 int
 xwrite (int fd, const void *v_buf, size_t len)