Version 1.11.13.
[libguestfs.git] / generator / generator_c.ml
index 7aa472b..2e9607b 100644 (file)
@@ -135,7 +135,8 @@ let rec generate_prototype ?(extern = true) ?(static = false)
   if newline then pr "\n"
 
 (* Generate C call arguments, eg "(handle, foo, bar)" *)
-and generate_c_call_args ?handle (ret, args, optargs) =
+and generate_c_call_args ?handle ?(implicit_size_ptr = "&size")
+    (ret, args, optargs) =
   pr "(";
   let comma = ref false in
   let next () =
@@ -155,11 +156,11 @@ and generate_c_call_args ?handle (ret, args, optargs) =
         next ();
         pr "%s" (name_of_argt arg)
   ) args;
-  (* For RBufferOut calls, add implicit &size parameter. *)
+  (* For RBufferOut calls, add implicit size pointer parameter. *)
   (match ret with
    | RBufferOut _ ->
        next ();
-       pr "&size"
+       pr "%s" implicit_size_ptr
    | _ -> ()
   );
   (* For calls with optional arguments, add implicit optargs parameter. *)
@@ -556,6 +557,16 @@ extern void *guestfs_next_private (guestfs_h *g, const char **key_rtn);
       if deprecated = None && not test0 && not debug then
         pr "#define LIBGUESTFS_HAVE_%s 1\n" (String.uppercase shortname);
 
+      if optargs <> [] then (
+        iteri (
+          fun i argt ->
+            let uc_shortname = String.uppercase shortname in
+            let n = name_of_argt argt in
+            let uc_n = String.uppercase n in
+            pr "#define GUESTFS_%s_%s %d\n" uc_shortname uc_n i;
+        ) optargs;
+      );
+
       generate_prototype ~single_line:true ~semicolon:false
         ~handle:"g" ~prefix:"guestfs_" shortname style;
       (match deprecated with
@@ -568,6 +579,7 @@ extern void *guestfs_next_private (guestfs_h *g, const char **key_rtn);
           ~prefix:"guestfs_" ~suffix:"_va" ~optarg_proto:VA
           shortname style;
 
+        pr "\n";
         pr "struct guestfs_%s_argv {\n" shortname;
         pr "  uint64_t bitmask;\n";
         iteri (
@@ -582,15 +594,16 @@ extern void *guestfs_next_private (guestfs_h *g, const char **key_rtn);
             let uc_shortname = String.uppercase shortname in
             let n = name_of_argt argt in
             let uc_n = String.uppercase n in
-            pr "#define GUESTFS_%s_%s %d\n" uc_shortname uc_n i;
-            pr "#define GUESTFS_%s_%s_BITMASK (UINT64_C(1)<<%d)\n" uc_shortname uc_n i;
-            pr "/* The field below is only valid in this struct if the\n";
-            pr " * GUESTFS_%s_%s_BITMASK bit is set\n" uc_shortname uc_n;
-            pr " * in the bitmask above, otherwise the contents are ignored.\n";
-            pr " */\n";
+            pr "\n";
+            pr "# define GUESTFS_%s_%s_BITMASK (UINT64_C(1)<<%d)\n" uc_shortname uc_n i;
+            pr "  /* The field below is only valid in this struct if the\n";
+            pr "   * GUESTFS_%s_%s_BITMASK bit is set\n" uc_shortname uc_n;
+            pr "   * in the bitmask above.  If not, the field is ignored.\n";
+            pr "   */\n";
             pr "  %s%s;\n" c_type n
         ) optargs;
         pr "};\n";
+        pr "\n";
 
         generate_prototype ~single_line:true ~newline:true ~handle:"g"
           ~prefix:"guestfs_" ~suffix:"_argv" ~optarg_proto:Argv
@@ -1013,7 +1026,7 @@ trace_send_line (guestfs_h *g)
       reject_unknown_optargs shortname style;
       trace_call shortname style;
       pr "  r = guestfs__%s " shortname;
-      generate_c_call_args ~handle:"g" style;
+      generate_c_call_args ~handle:"g" ~implicit_size_ptr:"size_r" style;
       pr ";\n";
       pr "\n";
       (match errcode_of_ret ret with
@@ -1359,9 +1372,12 @@ trace_send_line (guestfs_h *g)
 
         (* Get the name of the last regular argument. *)
         let last_arg =
-          match args with
-          | [] -> "g"
-          | args -> name_of_argt (List.hd (List.rev args)) in
+          match ret with
+          | RBufferOut _ -> "size_r"
+          | _ ->
+              match args with
+              | [] -> "g"
+              | args -> name_of_argt (List.hd (List.rev args)) in
 
         let rtype =
           match ret with
@@ -1382,7 +1398,7 @@ trace_send_line (guestfs_h *g)
         pr "\n";
         pr "  va_start (optargs, %s);\n" last_arg;
         pr "  %sr = guestfs_%s_va " rtype shortname;
-        generate_c_call_args ~handle:"g" style;
+        generate_c_call_args ~handle:"g" ~implicit_size_ptr:"size_r" style;
         pr ";\n";
         pr "  va_end (optargs);\n";
         pr "\n";
@@ -1439,7 +1455,7 @@ trace_send_line (guestfs_h *g)
         pr "  }\n";
         pr "\n";
         pr "  return guestfs_%s_argv " shortname;
-        generate_c_call_args ~handle:"g" style;
+        generate_c_call_args ~handle:"g" ~implicit_size_ptr:"size_r" style;
         pr ";\n";
         pr "}\n\n"
     | _ -> ()