python: Pass $PYTHON environment variable to tests.
[libguestfs.git] / generator / generator_fish.ml
index f59c520..6d6a571 100644 (file)
@@ -48,16 +48,21 @@ let generate_fish_cmds () =
 
   pr "#include <config.h>\n";
   pr "\n";
+  pr "/* It is safe to call deprecated functions from this file. */\n";
+  pr "#undef GUESTFS_WARN_DEPRECATED\n";
+  pr "\n";
   pr "#include <stdio.h>\n";
   pr "#include <stdlib.h>\n";
   pr "#include <string.h>\n";
   pr "#include <inttypes.h>\n";
   pr "\n";
-  pr "#include <guestfs.h>\n";
   pr "#include \"c-ctype.h\"\n";
   pr "#include \"full-write.h\"\n";
   pr "#include \"xstrtol.h\"\n";
+  pr "\n";
+  pr "#include <guestfs.h>\n";
   pr "#include \"fish.h\"\n";
+  pr "#include \"fish-cmds.h\"\n";
   pr "#include \"options.h\"\n";
   pr "#include \"cmds_gperf.h\"\n";
   pr "\n";
@@ -76,11 +81,7 @@ let generate_fish_cmds () =
   (* List of command_entry structs. *)
   List.iter (
     fun (name, _, _, flags, _, shortdesc, longdesc) ->
-      pr "struct command_entry %s_cmd_entry = {\n" name;
-
       let name2 = replace_char name '_' '-' in
-      pr "  .name = \"%s\",\n" name2;
-
       let aliases =
         filter_map (function FishAlias n -> Some n | _ -> None) flags in
       let describe_alias =
@@ -89,10 +90,16 @@ let generate_fish_cmds () =
             (String.concat " or " (List.map (fun s -> "'" ^ s ^ "'") aliases))
         else "" in
 
-      pr "  .shortdesc = \"%s\",\n" shortdesc;
-      pr "  .podbody = %S,\n"
-        ("=head1 DESCRIPTION\n\n" ^ longdesc ^ describe_alias);
+      let pod =
+        sprintf "%s - %s\n\n=head1 DESCRIPTION\n\n%s\n\n%s"
+          name2 shortdesc longdesc describe_alias in
+      let text =
+        String.concat "\n" (pod2text ~trim:false ~discard:false "NAME" pod)
+        ^ "\n" in
 
+      pr "struct command_entry %s_cmd_entry = {\n" name;
+      pr "  .name = \"%s\",\n" name2;
+      pr "  .help = \"%s\",\n" (c_quote text);
       pr "  .run = run_%s\n" name;
       pr "};\n";
       pr "\n";
@@ -100,11 +107,7 @@ let generate_fish_cmds () =
 
   List.iter (
     fun (name, (_, args, optargs), _, flags, _, shortdesc, longdesc) ->
-      pr "struct command_entry %s_cmd_entry = {\n" name;
-
       let name2 = replace_char name '_' '-' in
-      pr "  .name = \"%s\",\n" name2;
-
       let aliases =
         filter_map (function FishAlias n -> Some n | _ -> None) flags in
 
@@ -133,16 +136,6 @@ Guestfish will prompt for these separately."
             ("\n\n" ^ protocol_limit_warning)
           else "" in
 
-      (* For DangerWillRobinson commands, we should probably have
-       * guestfish prompt before allowing you to use them (especially
-       * in interactive mode). XXX
-       *)
-      let warnings =
-        warnings ^
-          if List.mem DangerWillRobinson flags then
-            ("\n\n" ^ danger_will_robinson)
-          else "" in
-
       let warnings =
         warnings ^
           match deprecation_notice flags with
@@ -155,12 +148,16 @@ Guestfish will prompt for these separately."
             (String.concat " or " (List.map (fun s -> "'" ^ s ^ "'") aliases))
         else "" in
 
-      pr "  .shortdesc = \"%s\",\n" shortdesc;
-      pr "  .podbody = %S,\n"
-        ("=head1 SYNOPSIS\n\n " ^ synopsis ^ "\n\n" ^
-         "=head1 DESCRIPTION\n\n" ^
-         longdesc ^ warnings ^ describe_alias);
+      let pod =
+        sprintf "%s - %s\n\n=head1 SYNOPSIS\n\n %s\n\n=head1 DESCRIPTION\n\n%s%s%s"
+          name2 shortdesc synopsis longdesc warnings describe_alias in
+      let text =
+        String.concat "\n" (pod2text ~trim:false ~discard:false "NAME" pod)
+        ^ "\n" in
 
+      pr "struct command_entry %s_cmd_entry = {\n" name;
+      pr "  .name = \"%s\",\n" name2;
+      pr "  .help = \"%s\",\n" (c_quote text);
       pr "  .run = run_%s\n" name;
       pr "};\n";
       pr "\n";
@@ -189,7 +186,7 @@ Guestfish will prompt for these separately."
   pr "\n";
   pr "  ct = lookup_fish_command (cmd, strlen (cmd));\n";
   pr "  if (ct) {\n";
-  pr "    pod2text (ct->entry->name, ct->entry->shortdesc, ct->entry->podbody);\n";
+  pr "    fputs (ct->entry->help, stdout);\n";
   pr "    return 0;\n";
   pr "  }\n";
   pr "  else\n";
@@ -391,7 +388,7 @@ Guestfish will prompt for these separately."
             pr "  %s = argv[i++];\n" name
         | Pathname name
         | Dev_or_Path name ->
-            pr "  %s = resolve_win_path (argv[i++]);\n" name;
+            pr "  %s = win_prefix (argv[i++]); /* process \"win:\" prefix */\n" name;
             pr "  if (%s == NULL) return -1;\n" name
         | OptString name ->
             pr "  %s = STRNEQ (argv[i], \"\") ? argv[i] : NULL;\n" name;
@@ -411,6 +408,8 @@ Guestfish will prompt for these separately."
             pr "  if (%s == NULL) return -1;\n" name
         | Key name ->
             pr "  %s = read_key (\"%s\");\n" name name;
+            pr "  if (keys_from_stdin)\n";
+            pr "    input_lineno++;\n";
             pr "  if (%s == NULL) return -1;\n" name
         | Bool name ->
             pr "  %s = is_true (argv[i++]) ? 1 : 0;\n" name
@@ -457,10 +456,12 @@ Guestfish will prompt for these separately."
                      "The Int type in the generator is a signed 31 bit int." in
                    Some (min, max, comment) in
                  let expr = sprintf "&argv[i][%d]" (len+1) in
-                 parse_integer expr "xstrtoll" "long long" "int" range name
+                 parse_integer expr "xstrtoll" "long long" "int" range
+                   (sprintf "optargs_s.%s" n)
              | Int64 n ->
                  let expr = sprintf "&argv[i][%d]" (len+1) in
-                 parse_integer expr "xstrtoll" "long long" "int64_t" None name
+                 parse_integer expr "xstrtoll" "long long" "int64_t" None
+                   (sprintf "optargs_s.%s" n)
              | String n ->
                  pr "      optargs_s.%s = &argv[i][%d];\n" n (len+1);
              | _ -> assert false
@@ -612,6 +613,22 @@ Guestfish will prompt for these separately."
   pr "  }\n";
   pr "}\n"
 
+and generate_fish_cmds_h () =
+  generate_header CStyle GPLv2plus;
+
+  pr "#ifndef FISH_CMDS_H\n";
+  pr "#define FISH_CMDS_H\n";
+  pr "\n";
+
+  List.iter (
+    fun (shortname, _, _, _, _, _, _) ->
+      pr "extern int run_%s (const char *cmd, size_t argc, char *argv[]);\n"
+        shortname
+  ) fish_commands;
+
+  pr "\n";
+  pr "#endif /* FISH_CMDS_H */\n"
+
 (* gperf code to do fast lookups of commands. *)
 and generate_fish_cmds_gperf () =
   generate_header CStyle GPLv2plus;
@@ -838,9 +855,6 @@ Guestfish will prompt for these separately.\n\n";
       if List.mem ProtocolLimitWarning flags then
         pr "%s\n\n" protocol_limit_warning;
 
-      if List.mem DangerWillRobinson flags then
-        pr "%s\n\n" danger_will_robinson;
-
       match deprecation_notice flags with
       | None -> ()
       | Some txt -> pr "%s\n\n" txt
@@ -909,6 +923,8 @@ and generate_fish_prep_options_c () =
   generate_header CStyle GPLv2plus;
 
   pr "\
+#include <stdio.h>
+
 #include \"fish.h\"
 #include \"prepopts.h\"