Command line, help.
[libguestfs.git] / src / generator.ml
index f3e53cd..75dcc79 100755 (executable)
@@ -53,7 +53,13 @@ names can be used.
 The rules are the same as for L<mount(2)>:  A filesystem must
 first be mounted on C</> before others can be mounted.  Other
 filesystems can only be mounted on directories which already
-exist.");
+exist.
+
+The mounted filesystem is writable, if we have sufficient permissions
+on the underlying device.
+
+The filesystem options C<sync> and C<noatime> are set with this
+call, in order to improve reliability.");
 
   ("sync", (Err, P0), 2,
    "Sync disks, writes are flushed through to the disk image",
@@ -68,7 +74,7 @@ calling C<guestfs_close>.");
    "Update file timestamps or create a new file",
    "\
 Touch acts like the L<touch(1)> command.  It can be used to
-update the filesystems on a file, or, if the file does not exist,
+update the timestamps on a file, or, if the file does not exist,
 to create a new zero-length file.");
 ]
 
@@ -81,6 +87,11 @@ let iter_args f = function
   | P1 arg1 -> f arg1
   | P2 (arg1, arg2) -> f arg1; f arg2
 
+let map_args f = function
+  | P0 -> []
+  | P1 arg1 -> [f arg1]
+  | P2 (arg1, arg2) -> [f arg1; f arg2]
+
 type comment_style = CStyle | HashStyle | OCamlStyle
 type license = GPLv2 | LGPLv2
 
@@ -243,12 +254,12 @@ and generate_client_actions () =
 
       (* Generate the return value struct. *)
       pr "struct %s_rv {\n" shortname;
-      pr "  int err_code;      /* 0 OK or -1 error */\n";
-      pr "  int serial;        /* serial number of reply */\n";
-      pr "  char err_str[GUESTFS_ERROR_LEN]; /* error from daemon */\n";
+      pr "  int cb_done;  /* flag to indicate callback was called */\n";
+      pr "  struct guestfs_message_header hdr;\n";
+      pr "  struct guestfs_message_error err;\n";
       (match style with
        | (Err, _) -> ()
-    (* | _ -> pr "  struct %s_ret ret;\n" name; REMEMBER TO MEMSET *)
+    (* | _ -> pr "  struct %s_ret ret;\n" name; *)
       );
       pr "};\n\n";
 
@@ -257,8 +268,25 @@ and generate_client_actions () =
       pr "{\n";
       pr "  struct %s_rv *rv = (struct %s_rv *) data;\n" shortname shortname;
       pr "\n";
-      pr "  /* XXX */ rv->err_code = 0;\n";
-      pr "  /* XXX rv->serial = ?; */\n";
+      pr "  if (!xdr_guestfs_message_header (xdr, &rv->hdr)) {\n";
+      pr "    error (g, \"%s: failed to parse reply header\");\n" name;
+      pr "    return;\n";
+      pr "  }\n";
+      pr "  if (rv->hdr.status == GUESTFS_STATUS_ERROR) {\n";
+      pr "    if (!xdr_guestfs_message_error (xdr, &rv->err)) {\n";
+      pr "      error (g, \"%s: failed to parse reply error\");\n" name;
+      pr "      return;\n";
+      pr "    }\n";
+      pr "    goto done;\n";
+      pr "  }\n";
+
+      (match style with
+       | (Err, _) -> ()
+    (* |  _ -> pr "  if (!xdr_%s_ret (&xdr, &rv->ret)) ..." *)
+      );
+
+      pr " done:\n";
+      pr "  rv->cb_done = 1;\n";
       pr "  main_loop.main_loop_quit (g);\n";
       pr "}\n\n";
 
@@ -286,13 +314,15 @@ and generate_client_actions () =
       pr "      g->state);\n";
       pr "    return %s;\n" error_code;
       pr "  }\n";
+      pr "\n";
+      pr "  memset (&rv, 0, sizeof rv);\n";
+      pr "\n";
 
       (match style with
        | (_, P0) ->
           pr "  serial = dispatch (g, GUESTFS_PROC_%s, NULL, NULL);\n"
             (String.uppercase shortname)
        | (_, args) ->
-          pr "\n";
           iter_args (
             function
             | String name -> pr "  args.%s = (char *) %s;\n" name name
@@ -306,23 +336,28 @@ and generate_client_actions () =
       pr "    return %s;\n" error_code;
       pr "\n";
 
-      pr "  rv.err_code = 42;\n";
+      pr "  rv.cb_done = 0;\n";
       pr "  g->reply_cb_internal = %s_cb;\n" shortname;
       pr "  g->reply_cb_internal_data = &rv;\n";
       pr "  main_loop.main_loop_run (g);\n";
       pr "  g->reply_cb_internal = NULL;\n";
       pr "  g->reply_cb_internal_data = NULL;\n";
-      pr "  if (rv.err_code == 42) { /* callback wasn't called */\n";
+      pr "  if (!rv.cb_done) {\n";
       pr "    error (g, \"%s failed, see earlier error messages\");\n" name;
       pr "    return %s;\n" error_code;
       pr "  }\n";
-      pr "  else if (rv.err_code == -1) { /* error from remote end */\n";
-      pr "    error (g, \"%%s\", rv.err_str);\n";
+      pr "\n";
+
+      pr "  if (check_reply_header (g, &rv.hdr, GUESTFS_PROC_%s, serial) == -1)\n"
+       (String.uppercase shortname);
       pr "    return %s;\n" error_code;
-      pr "  }\n";
       pr "\n";
 
-      pr "  /* XXX check serial number agrees */\n\n";
+      pr "  if (rv.hdr.status == GUESTFS_STATUS_ERROR) {\n";
+      pr "    error (g, \"%%s\", rv.err.error);\n";
+      pr "    return %s;\n" error_code;
+      pr "  }\n";
+      pr "\n";
 
       (match style with
        | (Err, _) -> pr "  return 0;\n"
@@ -416,7 +451,75 @@ and generate_daemon_actions () =
   pr "    default:\n";
   pr "      reply_with_error (\"dispatch_incoming_message: unknown procedure number %%d\", proc_nr);\n";
   pr "  }\n";
+  pr "}\n"
+
+and generate_fish_cmds () =
+  generate_header CStyle GPLv2;
+
+  pr "#include <stdio.h>\n";
+  pr "#include <stdlib.h>\n";
+  pr "#include <string.h>\n";
+  pr "\n";
+  pr "#include \"fish.h\"\n";
+  pr "\n";
+
+  (* list_commands function, which implements guestfish -h *)
+  pr "void list_commands (void)\n";
+  pr "{\n";
+  pr "  printf (\"    %%-16s     %%s\\n\", \"Command\", \"Description\");\n";
+  pr "  list_builtin_commands ();\n";
+  List.iter (
+    fun (name, _, _, shortdesc, _) ->
+      pr "  printf (\"%%-20s %%s\\n\", \"%s\", \"%s\");\n"
+       name shortdesc
+  ) functions;
+  pr "  printf (\"Use -h <cmd> to show detailed help for a command.\\n\");\n";
   pr "}\n";
+  pr "\n";
+
+  (* display_command function, which implements guestfish -h cmd *)
+  pr "void display_command (const char *cmd)\n";
+  pr "{\n";
+  List.iter (
+    fun (name, style, _, shortdesc, longdesc) ->
+      let synopsis =
+       match style with
+       | (Err, P0) -> name
+       | (Err, args) ->
+           sprintf "%s <%s>"
+             name (
+               String.concat "> <" (
+                 map_args (function
+                           | String n -> n) args
+               )
+             ) in
+
+      pr "  if (strcasecmp (cmd, \"%s\") == 0)\n" name;
+      pr "    pod2text (\"%s - %s\", %S);\n"
+       name shortdesc
+       (" " ^ synopsis ^ "\n\n" ^ longdesc);
+      pr "  else\n"
+  ) functions;
+  pr "    display_builtin_command (cmd);\n";
+  pr "}\n";
+  pr "\n";
+
+  (* run_action function *)
+  pr "int run_action (const char *cmd, int argc, char *argv[])\n";
+  pr "{\n";
+  List.iter (
+    fun (name, style, _, _, _) ->
+      pr "  if (strcasecmp (cmd, \"%s\") == 0)\n" name;
+      pr "    printf (\"running %s ...\\n\");\n" name;
+      pr "  else\n";
+  ) functions;
+  pr "    {\n";
+  pr "      fprintf (stderr, \"%%s: unknown command\\n\", cmd);\n";
+  pr "      return -1;\n";
+  pr "    }\n";
+  pr "  return 0;\n";
+  pr "}\n";
+  pr "\n"
 
 (* Generate a C function prototype. *)
 and generate_prototype ?(extern = true) ?(static = false) ?(semicolon = true)
@@ -497,6 +600,10 @@ let () =
   generate_daemon_actions ();
   close ();
 
+  let close = output_to "fish/cmds.c" in
+  generate_fish_cmds ();
+  close ();
+
   let close = output_to "guestfs-actions.pod" in
   generate_pod ();
   close ()