2 * Copyright (C) 2009-2010 Red Hat Inc.
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 (* Please read generator/README first. *)
26 open Generator_docstrings
27 open Generator_api_versions
28 open Generator_optgroups
29 open Generator_actions
30 open Generator_structs
34 type optarg_proto = Dots | VA | Argv
36 (* Generate a C function prototype. *)
37 let rec generate_prototype ?(extern = true) ?(static = false)
39 ?(single_line = false) ?(indent = "") ?(newline = false)
41 ?(prefix = "") ?(suffix = "")
43 ?(optarg_proto = Dots)
44 name (ret, args, optargs) =
46 if extern then pr "extern ";
47 if static then pr "static ";
53 if single_line then pr " " else pr "\n%s" indent
56 if single_line then pr " " else pr "\n%s" indent
57 | RConstString _ | RConstOptString _ ->
59 if not single_line then pr "\n%s" indent
60 | RString _ | RBufferOut _ ->
62 if not single_line then pr "\n%s" indent
63 | RStringList _ | RHashtable _ ->
65 if not single_line then pr "\n%s" indent
67 if not in_daemon then pr "struct guestfs_%s *" typ
68 else pr "guestfs_int_%s *" typ;
69 if not single_line then pr "\n%s" indent
70 | RStructList (_, typ) ->
71 if not in_daemon then pr "struct guestfs_%s_list *" typ
72 else pr "guestfs_int_%s_list *" typ;
73 if not single_line then pr "\n%s" indent
75 let is_RBufferOut = match ret with RBufferOut _ -> true | _ -> false in
76 pr "%s%s%s (" prefix name suffix;
77 if handle = None && args = [] && optargs = [] && not is_RBufferOut then
80 let comma = ref false in
83 | Some handle -> pr "guestfs_h *%s" handle; comma := true
87 if single_line then pr ", "
89 let namelen = String.length prefix + String.length name +
90 String.length suffix + 2 in
91 pr ",\n%s%s" indent (spaces namelen)
99 | Device n | Dev_or_Path n
104 pr "const char *%s" n
105 | StringList n | DeviceList n ->
107 pr "char *const *%s" n
108 | Bool n -> next (); pr "int %s" n
109 | Int n -> next (); pr "int %s" n
110 | Int64 n -> next (); pr "int64_t %s" n
113 if not in_daemon then (next (); pr "const char *%s" n)
116 pr "const char *%s" n;
118 pr "size_t %s_size" n
120 if is_RBufferOut then (next (); pr "size_t *size_r");
121 if optargs <> [] then (
123 match optarg_proto with
125 | VA -> pr "va_list args"
126 | Argv -> pr "const struct guestfs_%s_argv *optargs" name
130 if semicolon then pr ";";
131 if newline then pr "\n"
133 (* Generate C call arguments, eg "(handle, foo, bar)" *)
134 and generate_c_call_args ?handle (ret, args, optargs) =
136 let comma = ref false in
138 if !comma then pr ", ";
143 | Some handle -> pr "%s" handle; comma := true
152 pr "%s" (name_of_argt arg)
154 (* For RBufferOut calls, add implicit &size parameter. *)
161 (* For calls with optional arguments, add implicit optargs parameter. *)
162 if optargs <> [] then (
168 (* Generate the pod documentation for the C API. *)
169 and generate_actions_pod () =
171 fun (shortname, (ret, args, optargs as style), _, flags, _, _, longdesc) ->
172 if not (List.mem NotInDocs flags) then (
173 let name = "guestfs_" ^ shortname in
174 pr "=head2 %s\n\n" name;
175 generate_prototype ~extern:false ~indent:" " ~handle:"g" name style;
178 let uc_shortname = String.uppercase shortname in
179 if optargs <> [] then (
180 pr "You may supply a list of optional arguments to this call.\n";
181 pr "Use zero or more of the following pairs of parameters,\n";
182 pr "and terminate the list with C<-1> on its own.\n";
183 pr "See L</CALLS WITH OPTIONAL ARGUMENTS>.\n\n";
186 let n = name_of_argt argt in
187 let uc_n = String.uppercase n in
188 pr " GUESTFS_%s_%s, " uc_shortname uc_n;
190 | Bool n -> pr "int %s,\n" n
191 | Int n -> pr "int %s,\n" n
192 | Int64 n -> pr "int64_t %s,\n" n
193 | String n -> pr "const char *%s,\n" n
199 pr "%s\n\n" longdesc;
200 let ret, args, optargs = style in
203 pr "This function returns 0 on success or -1 on error.\n\n"
205 pr "On error this function returns -1.\n\n"
207 pr "On error this function returns -1.\n\n"
209 pr "This function returns a C truth value on success or -1 on error.\n\n"
211 pr "This function returns a string, or NULL on error.
212 The string is owned by the guest handle and must I<not> be freed.\n\n"
213 | RConstOptString _ ->
214 pr "This function returns a string which may be NULL.
215 There is no way to return an error from this function.
216 The string is owned by the guest handle and must I<not> be freed.\n\n"
218 pr "This function returns a string, or NULL on error.
219 I<The caller must free the returned string after use>.\n\n"
221 pr "This function returns a NULL-terminated array of strings
222 (like L<environ(3)>), or NULL if there was an error.
223 I<The caller must free the strings and the array after use>.\n\n"
224 | RStruct (_, typ) ->
225 pr "This function returns a C<struct guestfs_%s *>,
226 or NULL if there was an error.
227 I<The caller must call C<guestfs_free_%s> after use>.\n\n" typ typ
228 | RStructList (_, typ) ->
229 pr "This function returns a C<struct guestfs_%s_list *>
230 (see E<lt>guestfs-structs.hE<gt>),
231 or NULL if there was an error.
232 I<The caller must call C<guestfs_free_%s_list> after use>.\n\n" typ typ
234 pr "This function returns a NULL-terminated array of
235 strings, or NULL if there was an error.
236 The array of strings will always have length C<2n+1>, where
237 C<n> keys and values alternate, followed by the trailing NULL entry.
238 I<The caller must free the strings and the array after use>.\n\n"
240 pr "This function returns a buffer, or NULL on error.
241 The size of the returned buffer is written to C<*size_r>.
242 I<The caller must free the returned buffer after use>.\n\n"
244 if List.mem Progress flags then
245 pr "%s\n\n" progress_message;
246 if List.mem ProtocolLimitWarning flags then
247 pr "%s\n\n" protocol_limit_warning;
248 if List.mem DangerWillRobinson flags then
249 pr "%s\n\n" danger_will_robinson;
250 if List.exists (function Key _ -> true | _ -> false) (args@optargs) then
251 pr "This function takes a key or passphrase parameter which
252 could contain sensitive material. Read the section
253 L</KEYS AND PASSPHRASES> for more information.\n\n";
254 (match deprecation_notice flags with
256 | Some txt -> pr "%s\n\n" txt
258 let version = lookup_api_version name in
259 pr "(Added in %s)\n\n" version;
261 (* Handling of optional argument variants. *)
262 if optargs <> [] then (
263 pr "=head2 %s_va\n\n" name;
264 generate_prototype ~extern:false ~indent:" " ~handle:"g"
265 ~prefix:"guestfs_" ~suffix:"_va" ~optarg_proto:VA
268 pr "This is the \"va_list variant\" of L</%s>.\n\n" name;
269 pr "See L</CALLS WITH OPTIONAL ARGUMENTS>.\n\n";
270 pr "=head2 %s_argv\n\n" name;
271 generate_prototype ~extern:false ~indent:" " ~handle:"g"
272 ~prefix:"guestfs_" ~suffix:"_argv" ~optarg_proto:Argv
275 pr "This is the \"argv variant\" of L</%s>.\n\n" name;
276 pr "See L</CALLS WITH OPTIONAL ARGUMENTS>.\n\n";
279 ) all_functions_sorted
281 and generate_structs_pod () =
282 (* Structs documentation. *)
285 pr "=head2 guestfs_%s\n" typ;
287 pr " struct guestfs_%s {\n" typ;
290 | name, FChar -> pr " char %s;\n" name
291 | name, FUInt32 -> pr " uint32_t %s;\n" name
292 | name, FInt32 -> pr " int32_t %s;\n" name
293 | name, (FUInt64|FBytes) -> pr " uint64_t %s;\n" name
294 | name, FInt64 -> pr " int64_t %s;\n" name
295 | name, FString -> pr " char *%s;\n" name
297 pr " /* The next two fields describe a byte array. */\n";
298 pr " uint32_t %s_len;\n" name;
299 pr " char *%s;\n" name
301 pr " /* The next field is NOT nul-terminated, be careful when printing it: */\n";
302 pr " char %s[32];\n" name
303 | name, FOptPercent ->
304 pr " /* The next field is [0..100] or -1 meaning 'not present': */\n";
305 pr " float %s;\n" name
309 pr " struct guestfs_%s_list {\n" typ;
310 pr " uint32_t len; /* Number of elements in list. */\n";
311 pr " struct guestfs_%s *val; /* Elements. */\n" typ;
314 pr " void guestfs_free_%s (struct guestfs_free_%s *);\n" typ typ;
315 pr " void guestfs_free_%s_list (struct guestfs_free_%s_list *);\n"
320 and generate_availability_pod () =
321 (* Availability documentation. *)
325 fun (group, functions) ->
326 pr "=item B<%s>\n" group;
328 pr "The following functions:\n";
329 List.iter (pr "L</guestfs_%s>\n") functions;
335 (* Generate the guestfs-structs.h file. *)
336 and generate_structs_h () =
337 generate_header CStyle LGPLv2plus;
339 (* This is a public exported header file containing various
340 * structures. The structures are carefully written to have
341 * exactly the same in-memory format as the XDR structures that
342 * we use on the wire to the daemon. The reason for creating
343 * copies of these structures here is just so we don't have to
344 * export the whole of guestfs_protocol.h (which includes much
345 * unrelated and XDR-dependent stuff that we don't want to be
346 * public, or required by clients).
348 * To reiterate, we will pass these structures to and from the
349 * client with a simple assignment or memcpy, so the format
350 * must be identical to what rpcgen / the RFC defines.
353 (* Public structures. *)
356 pr "struct guestfs_%s {\n" typ;
359 | name, FChar -> pr " char %s;\n" name
360 | name, FString -> pr " char *%s;\n" name
362 pr " uint32_t %s_len;\n" name;
363 pr " char *%s;\n" name
364 | name, FUUID -> pr " char %s[32]; /* this is NOT nul-terminated, be careful when printing */\n" name
365 | name, FUInt32 -> pr " uint32_t %s;\n" name
366 | name, FInt32 -> pr " int32_t %s;\n" name
367 | name, (FUInt64|FBytes) -> pr " uint64_t %s;\n" name
368 | name, FInt64 -> pr " int64_t %s;\n" name
369 | name, FOptPercent -> pr " float %s; /* [0..100] or -1 */\n" name
373 pr "struct guestfs_%s_list {\n" typ;
374 pr " uint32_t len;\n";
375 pr " struct guestfs_%s *val;\n" typ;
378 pr "extern void guestfs_free_%s (struct guestfs_%s *);\n" typ typ;
379 pr "extern void guestfs_free_%s_list (struct guestfs_%s_list *);\n" typ typ;
383 (* Generate the guestfs-actions.h file. *)
384 and generate_actions_h () =
385 generate_header CStyle LGPLv2plus;
387 fun (shortname, (ret, args, optargs as style), _, flags, _, _, _) ->
389 List.exists (function DeprecatedBy _ -> true | _ -> false) flags in
391 String.length shortname >= 5 && String.sub shortname 0 5 = "test0" in
393 String.length shortname >= 5 && String.sub shortname 0 5 = "debug" in
394 if not deprecated && not test0 && not debug then
395 pr "#define LIBGUESTFS_HAVE_%s 1\n" (String.uppercase shortname);
397 generate_prototype ~single_line:true ~newline:true ~handle:"g"
398 ~prefix:"guestfs_" shortname style;
400 if optargs <> [] then (
401 generate_prototype ~single_line:true ~newline:true ~handle:"g"
402 ~prefix:"guestfs_" ~suffix:"_va" ~optarg_proto:VA
405 pr "struct guestfs_%s_argv {\n" shortname;
406 pr " uint64_t bitmask;\n";
412 | Int n -> "int64_t "
414 | String n -> "const char *"
415 | _ -> assert false (* checked in generator_checks *) in
416 let uc_shortname = String.uppercase shortname in
417 let n = name_of_argt argt in
418 let uc_n = String.uppercase n in
419 pr "#define GUESTFS_%s_%s %d\n" uc_shortname uc_n i;
420 pr "#define GUESTFS_%s_%s_BITMASK (UINT64_C(1)<<%d)\n" uc_shortname uc_n i;
421 pr "/* The field below is only valid in this struct if the\n";
422 pr " * GUESTFS_%s_%s_BITMASK bit is set\n" uc_shortname uc_n;
423 pr " * in the bitmask above, otherwise the contents are ignored.\n";
425 pr " %s%s;\n" c_type n
429 generate_prototype ~single_line:true ~newline:true ~handle:"g"
430 ~prefix:"guestfs_" ~suffix:"_argv" ~optarg_proto:Argv
433 ) all_functions_sorted
435 (* Generate the guestfs-internal-actions.h file. *)
436 and generate_internal_actions_h () =
437 generate_header CStyle LGPLv2plus;
439 fun (shortname, style, _, _, _, _, _) ->
440 generate_prototype ~single_line:true ~newline:true ~handle:"g"
441 ~prefix:"guestfs__" ~optarg_proto:Argv
443 ) non_daemon_functions
445 (* Generate the client-side dispatch stubs. *)
446 and generate_client_actions () =
447 generate_header CStyle LGPLv2plus;
454 #include <inttypes.h>
456 #include \"guestfs.h\"
457 #include \"guestfs-internal.h\"
458 #include \"guestfs-internal-actions.h\"
459 #include \"guestfs_protocol.h\"
460 #include \"errnostring.h\"
462 /* Check the return message from a call for validity. */
464 check_reply_header (guestfs_h *g,
465 const struct guestfs_message_header *hdr,
466 unsigned int proc_nr, unsigned int serial)
468 if (hdr->prog != GUESTFS_PROGRAM) {
469 error (g, \"wrong program (%%d/%%d)\", hdr->prog, GUESTFS_PROGRAM);
472 if (hdr->vers != GUESTFS_PROTOCOL_VERSION) {
473 error (g, \"wrong protocol version (%%d/%%d)\",
474 hdr->vers, GUESTFS_PROTOCOL_VERSION);
477 if (hdr->direction != GUESTFS_DIRECTION_REPLY) {
478 error (g, \"unexpected message direction (%%d/%%d)\",
479 hdr->direction, GUESTFS_DIRECTION_REPLY);
482 if (hdr->proc != proc_nr) {
483 error (g, \"unexpected procedure number (%%d/%%d)\", hdr->proc, proc_nr);
486 if (hdr->serial != serial) {
487 error (g, \"unexpected serial (%%d/%%d)\", hdr->serial, serial);
494 /* Check we are in the right state to run a high-level action. */
496 check_state (guestfs_h *g, const char *caller)
498 if (!guestfs__is_ready (g)) {
499 if (guestfs__is_config (g) || guestfs__is_launching (g))
500 error (g, \"%%s: call launch before using this function\\n(in guestfish, don't forget to use the 'run' command)\",
503 error (g, \"%%s called from the wrong state, %%d != READY\",
504 caller, guestfs__get_state (g));
512 let error_code_of = function
513 | RErr | RInt _ | RInt64 _ | RBool _ -> "-1"
514 | RConstString _ | RConstOptString _
515 | RString _ | RStringList _
516 | RStruct _ | RStructList _
517 | RHashtable _ | RBufferOut _ -> "NULL"
520 (* Generate code to check String-like parameters are not passed in
521 * as NULL (returning an error if they are).
523 let check_null_strings shortname (ret, args, optargs) =
524 let pr_newline = ref false in
527 (* parameters which should not be NULL *)
538 pr " if (%s == NULL) {\n" n;
539 pr " error (g, \"%%s: %%s: parameter cannot be NULL\",\n";
540 pr " \"%s\", \"%s\");\n" shortname n;
541 pr " return %s;\n" (error_code_of ret);
554 (* For optional arguments. *)
558 pr " if ((optargs->bitmask & GUESTFS_%s_%s_BITMASK) &&\n"
559 (String.uppercase shortname) (String.uppercase n);
560 pr " optargs->%s == NULL) {\n" n;
561 pr " error (g, \"%%s: %%s: optional parameter cannot be NULL\",\n";
562 pr " \"%s\", \"%s\");\n" shortname n;
563 pr " return %s;\n" (error_code_of ret);
568 | Bool _ | Int _ | Int64 _ -> ()
573 if !pr_newline then pr "\n";
576 (* Generate code to reject optargs we don't know about. *)
577 let reject_unknown_optargs shortname = function
580 let len = List.length optargs in
581 let mask = Int64.lognot (Int64.pred (Int64.shift_left 1L len)) in
582 pr " if (optargs->bitmask & UINT64_C(0x%Lx)) {\n" mask;
583 pr " error (g, \"%%s: unknown option in guestfs_%%s_argv->bitmask (this can happen if a program is compiled against a newer version of libguestfs, then dynamically linked to an older version)\",\n";
584 pr " \"%s\", \"%s\");\n" shortname shortname;
585 pr " return %s;\n" (error_code_of ret);
590 (* Generate code to generate guestfish call traces. *)
591 let trace_call shortname (ret, args, optargs) =
592 pr " if (guestfs__get_trace (g)) {\n";
595 List.exists (function
596 | StringList _ | DeviceList _ -> true
597 | _ -> false) args in
603 pr " fprintf (stderr, \"%s\");\n" shortname;
605 (* Required arguments. *)
608 | String n (* strings *)
614 (* guestfish doesn't support string escaping, so neither do we *)
615 pr " fprintf (stderr, \" \\\"%%s\\\"\", %s);\n" n
617 (* don't print keys *)
618 pr " fprintf (stderr, \" \\\"***\\\"\");\n"
619 | OptString n -> (* string option *)
620 pr " if (%s) fprintf (stderr, \" \\\"%%s\\\"\", %s);\n" n n;
621 pr " else fprintf (stderr, \" null\");\n"
623 | DeviceList n -> (* string list *)
624 pr " fputc (' ', stderr);\n";
625 pr " fputc ('\"', stderr);\n";
626 pr " for (i = 0; %s[i]; ++i) {\n" n;
627 pr " if (i > 0) fputc (' ', stderr);\n";
628 pr " fputs (%s[i], stderr);\n" n;
630 pr " fputc ('\"', stderr);\n";
631 | Bool n -> (* boolean *)
632 pr " fputs (%s ? \" true\" : \" false\", stderr);\n" n
634 pr " fprintf (stderr, \" %%d\", %s);\n" n
636 pr " fprintf (stderr, \" %%\" PRIi64, %s);\n" n
637 | BufferIn n -> (* RHBZ#646822 *)
638 pr " fputc (' ', stderr);\n";
639 pr " guestfs___print_BufferIn (stderr, %s, %s_size);\n" n n
642 (* Optional arguments. *)
645 let n = name_of_argt argt in
646 let uc_shortname = String.uppercase shortname in
647 let uc_n = String.uppercase n in
648 pr " if (optargs->bitmask & GUESTFS_%s_%s_BITMASK)\n"
652 pr " fprintf (stderr, \" \\\"%%s:%%s\\\"\", \"%s\", optargs->%s);\n" n n
654 pr " fprintf (stderr, \" \\\"%%s:%%s\\\"\", \"%s\", optargs->%s ? \"true\" : \"false\");\n" n n
656 pr " fprintf (stderr, \" \\\"%%s:%%d\\\"\", \"%s\", optargs->%s);\n" n n
658 pr " fprintf (stderr, \" \\\"%%s:%%\" PRIi64 \"\\\"\", \"%s\", optargs->%s);\n" n n
663 pr " fputc ('\\n', stderr);\n";
668 (* For non-daemon functions, generate a wrapper around each function. *)
670 fun (shortname, (_, _, optargs as style), _, _, _, _, _) ->
672 generate_prototype ~extern:false ~semicolon:false ~newline:true
673 ~handle:"g" ~prefix:"guestfs_"
676 generate_prototype ~extern:false ~semicolon:false ~newline:true
677 ~handle:"g" ~prefix:"guestfs_" ~suffix:"_argv" ~optarg_proto:Argv
680 check_null_strings shortname style;
681 reject_unknown_optargs shortname style;
682 trace_call shortname style;
683 pr " return guestfs__%s " shortname;
684 generate_c_call_args ~handle:"g" style;
688 ) non_daemon_functions;
690 (* Client-side stubs for each function. *)
692 fun (shortname, (ret, args, optargs as style), _, _, _, _, _) ->
693 if optargs <> [] then
694 failwithf "optargs not yet implemented for daemon functions";
696 let name = "guestfs_" ^ shortname in
697 let error_code = error_code_of ret in
699 (* Generate the action stub. *)
701 generate_prototype ~extern:false ~semicolon:false ~newline:true
702 ~handle:"g" name style
704 generate_prototype ~extern:false ~semicolon:false ~newline:true
705 ~handle:"g" ~suffix:"_argv" ~optarg_proto:Argv name style;
711 | _ -> pr " struct %s_args args;\n" name
714 pr " guestfs_message_header hdr;\n";
715 pr " guestfs_message_error err;\n";
719 | RConstString _ | RConstOptString _ ->
720 failwithf "RConstString|RConstOptString cannot be used by daemon functions"
722 | RBool _ | RString _ | RStringList _
723 | RStruct _ | RStructList _
724 | RHashtable _ | RBufferOut _ ->
725 pr " struct %s_ret ret;\n" name;
731 check_null_strings shortname style;
732 reject_unknown_optargs shortname style;
733 trace_call shortname style;
734 pr " if (check_state (g, \"%s\") == -1) return %s;\n"
735 shortname error_code;
736 pr " guestfs___set_busy (g);\n";
739 (* Send the main header and arguments. *)
742 pr " serial = guestfs___send (g, GUESTFS_PROC_%s, NULL, NULL);\n"
743 (String.uppercase shortname)
747 | Pathname n | Device n | Dev_or_Path n | String n | Key n ->
748 pr " args.%s = (char *) %s;\n" n n
750 pr " args.%s = %s ? (char **) &%s : NULL;\n" n n n
751 | StringList n | DeviceList n ->
752 pr " args.%s.%s_val = (char **) %s;\n" n n n;
753 pr " for (args.%s.%s_len = 0; %s[args.%s.%s_len]; args.%s.%s_len++) ;\n" n n n n n n n;
755 pr " args.%s = %s;\n" n n
757 pr " args.%s = %s;\n" n n
759 pr " args.%s = %s;\n" n n
760 | FileIn _ | FileOut _ -> ()
762 pr " /* Just catch grossly large sizes. XDR encoding will make this precise. */\n";
763 pr " if (%s_size >= GUESTFS_MESSAGE_MAX) {\n" n;
764 pr " error (g, \"%%s: size of input buffer too large\", \"%s\");\n"
766 pr " guestfs___end_busy (g);\n";
767 pr " return %s;\n" error_code;
769 pr " args.%s.%s_val = (char *) %s;\n" n n n;
770 pr " args.%s.%s_len = %s_size;\n" n n n
772 pr " serial = guestfs___send (g, GUESTFS_PROC_%s,\n"
773 (String.uppercase shortname);
774 pr " (xdrproc_t) xdr_%s_args, (char *) &args);\n"
777 pr " if (serial == -1) {\n";
778 pr " guestfs___end_busy (g);\n";
779 pr " return %s;\n" error_code;
783 (* Send any additional files (FileIn) requested. *)
784 let need_read_reply_label = ref false in
788 pr " r = guestfs___send_file (g, %s);\n" n;
789 pr " if (r == -1) {\n";
790 pr " guestfs___end_busy (g);\n";
791 pr " return %s;\n" error_code;
793 pr " if (r == -2) /* daemon cancelled */\n";
794 pr " goto read_reply;\n";
795 need_read_reply_label := true;
800 (* Wait for the reply from the remote end. *)
801 if !need_read_reply_label then pr " read_reply:\n";
802 pr " memset (&hdr, 0, sizeof hdr);\n";
803 pr " memset (&err, 0, sizeof err);\n";
804 if has_ret then pr " memset (&ret, 0, sizeof ret);\n";
806 pr " r = guestfs___recv (g, \"%s\", &hdr, &err,\n " shortname;
810 pr "(xdrproc_t) xdr_guestfs_%s_ret, (char *) &ret" shortname;
813 pr " if (r == -1) {\n";
814 pr " guestfs___end_busy (g);\n";
815 pr " return %s;\n" error_code;
819 pr " if (check_reply_header (g, &hdr, GUESTFS_PROC_%s, serial) == -1) {\n"
820 (String.uppercase shortname);
821 pr " guestfs___end_busy (g);\n";
822 pr " return %s;\n" error_code;
826 pr " if (hdr.status == GUESTFS_STATUS_ERROR) {\n";
827 pr " int errnum = 0;\n";
828 pr " if (err.errno_string[0] != '\\0')\n";
829 pr " errnum = guestfs___string_to_errno (err.errno_string);\n";
830 pr " if (errnum <= 0)\n";
831 pr " error (g, \"%%s: %%s\", \"%s\", err.error_message);\n"
834 pr " guestfs_error_errno (g, errnum, \"%%s: %%s\", \"%s\",\n"
836 pr " err.error_message);\n";
837 pr " free (err.error_message);\n";
838 pr " free (err.errno_string);\n";
839 pr " guestfs___end_busy (g);\n";
840 pr " return %s;\n" error_code;
844 (* Expecting to receive further files (FileOut)? *)
848 pr " if (guestfs___recv_file (g, %s) == -1) {\n" n;
849 pr " guestfs___end_busy (g);\n";
850 pr " return %s;\n" error_code;
856 pr " guestfs___end_busy (g);\n";
859 | RErr -> pr " return 0;\n"
860 | RInt n | RInt64 n | RBool n ->
861 pr " return ret.%s;\n" n
862 | RConstString _ | RConstOptString _ ->
863 failwithf "RConstString|RConstOptString cannot be used by daemon functions"
865 pr " return ret.%s; /* caller will free */\n" n
866 | RStringList n | RHashtable n ->
867 pr " /* caller will free this, but we need to add a NULL entry */\n";
868 pr " ret.%s.%s_val =\n" n n;
869 pr " safe_realloc (g, ret.%s.%s_val,\n" n n;
870 pr " sizeof (char *) * (ret.%s.%s_len + 1));\n"
872 pr " ret.%s.%s_val[ret.%s.%s_len] = NULL;\n" n n n n;
873 pr " return ret.%s.%s_val;\n" n n
875 pr " /* caller will free this */\n";
876 pr " return safe_memdup (g, &ret.%s, sizeof (ret.%s));\n" n n
877 | RStructList (n, _) ->
878 pr " /* caller will free this */\n";
879 pr " return safe_memdup (g, &ret.%s, sizeof (ret.%s));\n" n n
881 pr " /* RBufferOut is tricky: If the buffer is zero-length, then\n";
882 pr " * _val might be NULL here. To make the API saner for\n";
883 pr " * callers, we turn this case into a unique pointer (using\n";
884 pr " * malloc(1)).\n";
886 pr " if (ret.%s.%s_len > 0) {\n" n n;
887 pr " *size_r = ret.%s.%s_len;\n" n n;
888 pr " return ret.%s.%s_val; /* caller will free */\n" n n;
890 pr " free (ret.%s.%s_val);\n" n n;
891 pr " char *p = safe_malloc (g, 1);\n";
892 pr " *size_r = ret.%s.%s_len;\n" n n;
900 (* Functions to free structures. *)
901 pr "/* Structure-freeing functions. These rely on the fact that the\n";
902 pr " * structure format is identical to the XDR format. See note in\n";
903 pr " * generator.ml.\n";
910 pr "guestfs_free_%s (struct guestfs_%s *x)\n" typ typ;
912 pr " xdr_free ((xdrproc_t) xdr_guestfs_int_%s, (char *) x);\n" typ;
918 pr "guestfs_free_%s_list (struct guestfs_%s_list *x)\n" typ typ;
920 pr " xdr_free ((xdrproc_t) xdr_guestfs_int_%s_list, (char *) x);\n" typ;
927 (* Functions which have optional arguments have two generated variants. *)
930 | shortname, (ret, args, (_::_ as optargs) as style), _, _, _, _, _ ->
931 let uc_shortname = String.uppercase shortname in
933 (* Get the name of the last regular argument. *)
937 | args -> name_of_argt (List.hd (List.rev args)) in
939 let rerrcode, rtype =
941 | RErr | RInt _ | RBool _ -> "-1", "int "
942 | RInt64 _ -> "-1", "int64_t "
943 | RConstString _ | RConstOptString _ -> "NULL", "const char *"
944 | RString _ | RBufferOut _ -> "NULL", "char *"
945 | RStringList _ | RHashtable _ -> "NULL", "char **"
946 | RStruct (_, typ) -> "NULL", sprintf "struct guestfs_%s *" typ
947 | RStructList (_, typ) ->
948 "NULL", sprintf "struct guestfs_%s_list *" typ in
950 (* The regular variable args function, just calls the _va variant. *)
951 generate_prototype ~extern:false ~semicolon:false ~newline:true
952 ~handle:"g" ~prefix:"guestfs_" shortname style;
954 pr " va_list optargs;\n";
956 pr " va_start (optargs, %s);\n" last_arg;
957 pr " %sr = guestfs_%s_va " rtype shortname;
958 generate_c_call_args ~handle:"g" style;
960 pr " va_end (optargs);\n";
965 generate_prototype ~extern:false ~semicolon:false ~newline:true
966 ~handle:"g" ~prefix:"guestfs_" ~suffix:"_va" ~optarg_proto:VA
969 pr " struct guestfs_%s_argv optargs_s;\n" shortname;
970 pr " struct guestfs_%s_argv *optargs = &optargs_s;\n" shortname;
973 pr " optargs_s.bitmask = 0;\n";
975 pr " while ((i = va_arg (args, int)) >= 0) {\n";
976 pr " switch (i) {\n";
980 let n = name_of_argt argt in
981 let uc_n = String.uppercase n in
982 pr " case GUESTFS_%s_%s:\n" uc_shortname uc_n;
983 pr " optargs_s.%s = va_arg (args, " n;
985 | Bool _ | Int _ -> pr "int"
986 | Int64 _ -> pr "int64_t"
987 | String _ -> pr "const char *"
995 pr " error (g, \"%%s: unknown option %%d (this can happen if a program is compiled against a newer version of libguestfs, then dynamically linked to an older version)\",\n";
996 pr " \"%s\", i);\n" shortname;
997 pr " return %s;\n" rerrcode;
1000 pr " uint64_t i_mask = UINT64_C(1) << i;\n";
1001 pr " if (optargs_s.bitmask & i_mask) {\n";
1002 pr " error (g, \"%%s: same optional argument specified more than once\",\n";
1003 pr " \"%s\");\n" shortname;
1004 pr " return %s;\n" rerrcode;
1006 pr " optargs_s.bitmask |= i_mask;\n";
1009 pr " return guestfs_%s_argv " shortname;
1010 generate_c_call_args ~handle:"g" style;
1014 ) all_functions_sorted
1016 (* Generate the linker script which controls the visibility of
1017 * symbols in the public ABI and ensures no other symbols get
1018 * exported accidentally.
1020 and generate_linker_script () =
1021 generate_header HashStyle GPLv2plus;
1026 "guestfs_get_error_handler";
1027 "guestfs_get_out_of_memory_handler";
1028 "guestfs_get_private";
1029 "guestfs_last_errno";
1030 "guestfs_last_error";
1031 "guestfs_set_close_callback";
1032 "guestfs_set_error_handler";
1033 "guestfs_set_launch_done_callback";
1034 "guestfs_set_log_message_callback";
1035 "guestfs_set_out_of_memory_handler";
1036 "guestfs_set_private";
1037 "guestfs_set_progress_callback";
1038 "guestfs_set_subprocess_quit_callback";
1040 (* Unofficial parts of the API: the bindings code use these
1041 * functions, so it is useful to export them.
1043 "guestfs_safe_calloc";
1044 "guestfs_safe_malloc";
1045 "guestfs_safe_strdup";
1046 "guestfs_safe_memdup";
1053 | name, (_, _, []), _, _, _, _, _ -> ["guestfs_" ^ name]
1054 | name, (_, _, _), _, _, _, _, _ ->
1056 "guestfs_" ^ name ^ "_va";
1057 "guestfs_" ^ name ^ "_argv"]
1062 List.map (fun (typ, _) ->
1063 ["guestfs_free_" ^ typ; "guestfs_free_" ^ typ ^ "_list"])
1066 let globals = List.sort compare (globals @ functions @ structs) in
1070 List.iter (pr " %s;\n") globals;
1077 and generate_max_proc_nr () =
1078 pr "%d\n" max_proc_nr