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_optgroups
28 open Generator_actions
29 open Generator_structs
33 type optarg_proto = Dots | VA | Argv
35 (* Generate a C function prototype. *)
36 let rec generate_prototype ?(extern = true) ?(static = false)
38 ?(single_line = false) ?(newline = false) ?(in_daemon = false)
39 ?(prefix = "") ?(suffix = "")
41 ?(optarg_proto = Dots)
42 name (ret, args, optargs) =
43 if extern then pr "extern ";
44 if static then pr "static ";
48 | RInt64 _ -> pr "int64_t "
49 | RBool _ -> pr "int "
50 | RConstString _ | RConstOptString _ -> pr "const char *"
51 | RString _ | RBufferOut _ -> pr "char *"
52 | RStringList _ | RHashtable _ -> pr "char **"
54 if not in_daemon then pr "struct guestfs_%s *" typ
55 else pr "guestfs_int_%s *" typ
56 | RStructList (_, typ) ->
57 if not in_daemon then pr "struct guestfs_%s_list *" typ
58 else pr "guestfs_int_%s_list *" typ
60 let is_RBufferOut = match ret with RBufferOut _ -> true | _ -> false in
61 pr "%s%s%s (" prefix name suffix;
62 if handle = None && args = [] && optargs = [] && not is_RBufferOut then
65 let comma = ref false in
68 | Some handle -> pr "guestfs_h *%s" handle; comma := true
72 if single_line then pr ", " else pr ",\n\t\t"
79 | Device n | Dev_or_Path n
85 | StringList n | DeviceList n ->
87 pr "char *const *%s" n
88 | Bool n -> next (); pr "int %s" n
89 | Int n -> next (); pr "int %s" n
90 | Int64 n -> next (); pr "int64_t %s" n
93 if not in_daemon then (next (); pr "const char *%s" n)
96 pr "const char *%s" n;
100 if is_RBufferOut then (next (); pr "size_t *size_r");
101 if optargs <> [] then (
103 match optarg_proto with
105 | VA -> pr "va_list args"
106 | Argv -> pr "const struct guestfs_%s_argv *optargs" name
110 if semicolon then pr ";";
111 if newline then pr "\n"
113 (* Generate C call arguments, eg "(handle, foo, bar)" *)
114 and generate_c_call_args ?handle (ret, args, optargs) =
116 let comma = ref false in
118 if !comma then pr ", ";
123 | Some handle -> pr "%s" handle; comma := true
132 pr "%s" (name_of_argt arg)
134 (* For RBufferOut calls, add implicit &size parameter. *)
141 (* For calls with optional arguments, add implicit optargs parameter. *)
142 if optargs <> [] then (
148 (* Generate the pod documentation for the C API. *)
149 and generate_actions_pod () =
151 fun (shortname, (ret, args, optargs as style), _, flags, _, _, longdesc) ->
152 if not (List.mem NotInDocs flags) then (
153 let name = "guestfs_" ^ shortname in
154 pr "=head2 %s\n\n" name;
156 generate_prototype ~extern:false ~handle:"g" name style;
159 let uc_shortname = String.uppercase shortname in
160 if optargs <> [] then (
161 pr "You may supply a list of optional arguments to this call.\n";
162 pr "Use zero or more of the following pairs of parameters,\n";
163 pr "and terminate the list with C<-1> on its own.\n";
164 pr "See L</CALLS WITH OPTIONAL ARGUMENTS>.\n\n";
167 let n = name_of_argt argt in
168 let uc_n = String.uppercase n in
169 pr " GUESTFS_%s_%s, " uc_shortname uc_n;
171 | Bool n -> pr "int %s,\n" n
172 | Int n -> pr "int %s,\n" n
173 | Int64 n -> pr "int64_t %s,\n" n
174 | String n -> pr "const char *%s,\n" n
180 pr "%s\n\n" longdesc;
181 let ret, args, optargs = style in
184 pr "This function returns 0 on success or -1 on error.\n\n"
186 pr "On error this function returns -1.\n\n"
188 pr "On error this function returns -1.\n\n"
190 pr "This function returns a C truth value on success or -1 on error.\n\n"
192 pr "This function returns a string, or NULL on error.
193 The string is owned by the guest handle and must I<not> be freed.\n\n"
194 | RConstOptString _ ->
195 pr "This function returns a string which may be NULL.
196 There is no way to return an error from this function.
197 The string is owned by the guest handle and must I<not> be freed.\n\n"
199 pr "This function returns a string, or NULL on error.
200 I<The caller must free the returned string after use>.\n\n"
202 pr "This function returns a NULL-terminated array of strings
203 (like L<environ(3)>), or NULL if there was an error.
204 I<The caller must free the strings and the array after use>.\n\n"
205 | RStruct (_, typ) ->
206 pr "This function returns a C<struct guestfs_%s *>,
207 or NULL if there was an error.
208 I<The caller must call C<guestfs_free_%s> after use>.\n\n" typ typ
209 | RStructList (_, typ) ->
210 pr "This function returns a C<struct guestfs_%s_list *>
211 (see E<lt>guestfs-structs.hE<gt>),
212 or NULL if there was an error.
213 I<The caller must call C<guestfs_free_%s_list> after use>.\n\n" typ typ
215 pr "This function returns a NULL-terminated array of
216 strings, or NULL if there was an error.
217 The array of strings will always have length C<2n+1>, where
218 C<n> keys and values alternate, followed by the trailing NULL entry.
219 I<The caller must free the strings and the array after use>.\n\n"
221 pr "This function returns a buffer, or NULL on error.
222 The size of the returned buffer is written to C<*size_r>.
223 I<The caller must free the returned buffer after use>.\n\n"
225 if List.mem Progress flags then
226 pr "%s\n\n" progress_message;
227 if List.mem ProtocolLimitWarning flags then
228 pr "%s\n\n" protocol_limit_warning;
229 if List.mem DangerWillRobinson flags then
230 pr "%s\n\n" danger_will_robinson;
231 if List.exists (function Key _ -> true | _ -> false) (args@optargs) then
232 pr "This function takes a key or passphrase parameter which
233 could contain sensitive material. Read the section
234 L</KEYS AND PASSPHRASES> for more information.\n\n";
235 (match deprecation_notice flags with
237 | Some txt -> pr "%s\n\n" txt
240 (* Handling of optional argument variants. *)
241 if optargs <> [] then (
242 pr "=head2 %s_va\n\n" name;
244 generate_prototype ~extern:false ~handle:"g"
245 ~prefix:"guestfs_" ~suffix:"_va" ~optarg_proto:VA
248 pr "This is the \"va_list variant\" of L</%s>.\n\n" name;
249 pr "See L</CALLS WITH OPTIONAL ARGUMENTS>.\n\n";
250 pr "=head2 %s_argv\n\n" name;
252 generate_prototype ~extern:false ~handle:"g"
253 ~prefix:"guestfs_" ~suffix:"_argv" ~optarg_proto:Argv
256 pr "This is the \"argv variant\" of L</%s>.\n\n" name;
257 pr "See L</CALLS WITH OPTIONAL ARGUMENTS>.\n\n";
260 ) all_functions_sorted
262 and generate_structs_pod () =
263 (* Structs documentation. *)
266 pr "=head2 guestfs_%s\n" typ;
268 pr " struct guestfs_%s {\n" typ;
271 | name, FChar -> pr " char %s;\n" name
272 | name, FUInt32 -> pr " uint32_t %s;\n" name
273 | name, FInt32 -> pr " int32_t %s;\n" name
274 | name, (FUInt64|FBytes) -> pr " uint64_t %s;\n" name
275 | name, FInt64 -> pr " int64_t %s;\n" name
276 | name, FString -> pr " char *%s;\n" name
278 pr " /* The next two fields describe a byte array. */\n";
279 pr " uint32_t %s_len;\n" name;
280 pr " char *%s;\n" name
282 pr " /* The next field is NOT nul-terminated, be careful when printing it: */\n";
283 pr " char %s[32];\n" name
284 | name, FOptPercent ->
285 pr " /* The next field is [0..100] or -1 meaning 'not present': */\n";
286 pr " float %s;\n" name
290 pr " struct guestfs_%s_list {\n" typ;
291 pr " uint32_t len; /* Number of elements in list. */\n";
292 pr " struct guestfs_%s *val; /* Elements. */\n" typ;
295 pr " void guestfs_free_%s (struct guestfs_free_%s *);\n" typ typ;
296 pr " void guestfs_free_%s_list (struct guestfs_free_%s_list *);\n"
301 and generate_availability_pod () =
302 (* Availability documentation. *)
306 fun (group, functions) ->
307 pr "=item B<%s>\n" group;
309 pr "The following functions:\n";
310 List.iter (pr "L</guestfs_%s>\n") functions;
316 (* Generate the guestfs-structs.h file. *)
317 and generate_structs_h () =
318 generate_header CStyle LGPLv2plus;
320 (* This is a public exported header file containing various
321 * structures. The structures are carefully written to have
322 * exactly the same in-memory format as the XDR structures that
323 * we use on the wire to the daemon. The reason for creating
324 * copies of these structures here is just so we don't have to
325 * export the whole of guestfs_protocol.h (which includes much
326 * unrelated and XDR-dependent stuff that we don't want to be
327 * public, or required by clients).
329 * To reiterate, we will pass these structures to and from the
330 * client with a simple assignment or memcpy, so the format
331 * must be identical to what rpcgen / the RFC defines.
334 (* Public structures. *)
337 pr "struct guestfs_%s {\n" typ;
340 | name, FChar -> pr " char %s;\n" name
341 | name, FString -> pr " char *%s;\n" name
343 pr " uint32_t %s_len;\n" name;
344 pr " char *%s;\n" name
345 | name, FUUID -> pr " char %s[32]; /* this is NOT nul-terminated, be careful when printing */\n" name
346 | name, FUInt32 -> pr " uint32_t %s;\n" name
347 | name, FInt32 -> pr " int32_t %s;\n" name
348 | name, (FUInt64|FBytes) -> pr " uint64_t %s;\n" name
349 | name, FInt64 -> pr " int64_t %s;\n" name
350 | name, FOptPercent -> pr " float %s; /* [0..100] or -1 */\n" name
354 pr "struct guestfs_%s_list {\n" typ;
355 pr " uint32_t len;\n";
356 pr " struct guestfs_%s *val;\n" typ;
359 pr "extern void guestfs_free_%s (struct guestfs_%s *);\n" typ typ;
360 pr "extern void guestfs_free_%s_list (struct guestfs_%s_list *);\n" typ typ;
364 (* Generate the guestfs-actions.h file. *)
365 and generate_actions_h () =
366 generate_header CStyle LGPLv2plus;
368 fun (shortname, (ret, args, optargs as style), _, flags, _, _, _) ->
370 List.exists (function DeprecatedBy _ -> true | _ -> false) flags in
372 String.length shortname >= 5 && String.sub shortname 0 5 = "test0" in
374 String.length shortname >= 5 && String.sub shortname 0 5 = "debug" in
375 if not deprecated && not test0 && not debug then
376 pr "#define LIBGUESTFS_HAVE_%s 1\n" (String.uppercase shortname);
378 generate_prototype ~single_line:true ~newline:true ~handle:"g"
379 ~prefix:"guestfs_" shortname style;
381 if optargs <> [] then (
382 generate_prototype ~single_line:true ~newline:true ~handle:"g"
383 ~prefix:"guestfs_" ~suffix:"_va" ~optarg_proto:VA
386 pr "struct guestfs_%s_argv {\n" shortname;
387 pr " uint64_t bitmask;\n";
393 | Int n -> "int64_t "
395 | String n -> "const char *"
396 | _ -> assert false (* checked in generator_checks *) in
397 let uc_shortname = String.uppercase shortname in
398 let n = name_of_argt argt in
399 let uc_n = String.uppercase n in
400 pr "#define GUESTFS_%s_%s %d\n" uc_shortname uc_n i;
401 pr "#define GUESTFS_%s_%s_BITMASK (UINT64_C(1)<<%d)\n" uc_shortname uc_n i;
402 pr "/* The field below is only valid in this struct if the\n";
403 pr " * GUESTFS_%s_%s_BITMASK bit is set\n" uc_shortname uc_n;
404 pr " * in the bitmask above, otherwise the contents are ignored.\n";
406 pr " %s%s;\n" c_type n
410 generate_prototype ~single_line:true ~newline:true ~handle:"g"
411 ~prefix:"guestfs_" ~suffix:"_argv" ~optarg_proto:Argv
414 ) all_functions_sorted
416 (* Generate the guestfs-internal-actions.h file. *)
417 and generate_internal_actions_h () =
418 generate_header CStyle LGPLv2plus;
420 fun (shortname, style, _, _, _, _, _) ->
421 generate_prototype ~single_line:true ~newline:true ~handle:"g"
422 ~prefix:"guestfs__" ~optarg_proto:Argv
424 ) non_daemon_functions
426 (* Generate the client-side dispatch stubs. *)
427 and generate_client_actions () =
428 generate_header CStyle LGPLv2plus;
435 #include <inttypes.h>
437 #include \"guestfs.h\"
438 #include \"guestfs-internal.h\"
439 #include \"guestfs-internal-actions.h\"
440 #include \"guestfs_protocol.h\"
442 /* Check the return message from a call for validity. */
444 check_reply_header (guestfs_h *g,
445 const struct guestfs_message_header *hdr,
446 unsigned int proc_nr, unsigned int serial)
448 if (hdr->prog != GUESTFS_PROGRAM) {
449 error (g, \"wrong program (%%d/%%d)\", hdr->prog, GUESTFS_PROGRAM);
452 if (hdr->vers != GUESTFS_PROTOCOL_VERSION) {
453 error (g, \"wrong protocol version (%%d/%%d)\",
454 hdr->vers, GUESTFS_PROTOCOL_VERSION);
457 if (hdr->direction != GUESTFS_DIRECTION_REPLY) {
458 error (g, \"unexpected message direction (%%d/%%d)\",
459 hdr->direction, GUESTFS_DIRECTION_REPLY);
462 if (hdr->proc != proc_nr) {
463 error (g, \"unexpected procedure number (%%d/%%d)\", hdr->proc, proc_nr);
466 if (hdr->serial != serial) {
467 error (g, \"unexpected serial (%%d/%%d)\", hdr->serial, serial);
474 /* Check we are in the right state to run a high-level action. */
476 check_state (guestfs_h *g, const char *caller)
478 if (!guestfs__is_ready (g)) {
479 if (guestfs__is_config (g) || guestfs__is_launching (g))
480 error (g, \"%%s: call launch before using this function\\n(in guestfish, don't forget to use the 'run' command)\",
483 error (g, \"%%s called from the wrong state, %%d != READY\",
484 caller, guestfs__get_state (g));
492 let error_code_of = function
493 | RErr | RInt _ | RInt64 _ | RBool _ -> "-1"
494 | RConstString _ | RConstOptString _
495 | RString _ | RStringList _
496 | RStruct _ | RStructList _
497 | RHashtable _ | RBufferOut _ -> "NULL"
500 (* Generate code to check String-like parameters are not passed in
501 * as NULL (returning an error if they are).
503 let check_null_strings shortname (ret, args, optargs) =
504 let pr_newline = ref false in
507 (* parameters which should not be NULL *)
518 pr " if (%s == NULL) {\n" n;
519 pr " error (g, \"%%s: %%s: parameter cannot be NULL\",\n";
520 pr " \"%s\", \"%s\");\n" shortname n;
521 pr " return %s;\n" (error_code_of ret);
534 (* For optional arguments. *)
538 pr " if ((optargs->bitmask & GUESTFS_%s_%s_BITMASK) &&\n"
539 (String.uppercase shortname) (String.uppercase n);
540 pr " optargs->%s == NULL) {\n" n;
541 pr " error (g, \"%%s: %%s: optional parameter cannot be NULL\",\n";
542 pr " \"%s\", \"%s\");\n" shortname n;
543 pr " return %s;\n" (error_code_of ret);
548 | Bool _ | Int _ | Int64 _ -> ()
553 if !pr_newline then pr "\n";
556 (* Generate code to reject optargs we don't know about. *)
557 let reject_unknown_optargs shortname = function
560 let len = List.length optargs in
561 let mask = Int64.lognot (Int64.pred (Int64.shift_left 1L len)) in
562 pr " if (optargs->bitmask & UINT64_C(0x%Lx)) {\n" mask;
563 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";
564 pr " \"%s\", \"%s\");\n" shortname shortname;
565 pr " return %s;\n" (error_code_of ret);
570 (* Generate code to generate guestfish call traces. *)
571 let trace_call shortname (ret, args, optargs) =
572 pr " if (guestfs__get_trace (g)) {\n";
575 List.exists (function
576 | StringList _ | DeviceList _ -> true
577 | _ -> false) args in
583 pr " fprintf (stderr, \"%s\");\n" shortname;
585 (* Required arguments. *)
588 | String n (* strings *)
596 (* guestfish doesn't support string escaping, so neither do we *)
597 pr " fprintf (stderr, \" \\\"%%s\\\"\", %s);\n" n
598 | OptString n -> (* string option *)
599 pr " if (%s) fprintf (stderr, \" \\\"%%s\\\"\", %s);\n" n n;
600 pr " else fprintf (stderr, \" null\");\n"
602 | DeviceList n -> (* string list *)
603 pr " fputc (' ', stderr);\n";
604 pr " fputc ('\"', stderr);\n";
605 pr " for (i = 0; %s[i]; ++i) {\n" n;
606 pr " if (i > 0) fputc (' ', stderr);\n";
607 pr " fputs (%s[i], stderr);\n" n;
609 pr " fputc ('\"', stderr);\n";
610 | Bool n -> (* boolean *)
611 pr " fputs (%s ? \" true\" : \" false\", stderr);\n" n
613 pr " fprintf (stderr, \" %%d\", %s);\n" n
615 pr " fprintf (stderr, \" %%\" PRIi64, %s);\n" n
618 (* Optional arguments. *)
621 let n = name_of_argt argt in
622 let uc_shortname = String.uppercase shortname in
623 let uc_n = String.uppercase n in
624 pr " if (optargs->bitmask & GUESTFS_%s_%s_BITMASK)\n"
628 pr " fprintf (stderr, \" \\\"%%s:%%s\\\"\", \"%s\", optargs->%s);\n" n n
630 pr " fprintf (stderr, \" \\\"%%s:%%s\\\"\", \"%s\", optargs->%s ? \"true\" : \"false\");\n" n n
632 pr " fprintf (stderr, \" \\\"%%s:%%d\\\"\", \"%s\", optargs->%s);\n" n n
634 pr " fprintf (stderr, \" \\\"%%s:%%\" PRIi64 \"\\\"\", \"%s\", optargs->%s);\n" n n
639 pr " fputc ('\\n', stderr);\n";
644 (* For non-daemon functions, generate a wrapper around each function. *)
646 fun (shortname, (_, _, optargs as style), _, _, _, _, _) ->
648 generate_prototype ~extern:false ~semicolon:false ~newline:true
649 ~handle:"g" ~prefix:"guestfs_"
652 generate_prototype ~extern:false ~semicolon:false ~newline:true
653 ~handle:"g" ~prefix:"guestfs_" ~suffix:"_argv" ~optarg_proto:Argv
656 check_null_strings shortname style;
657 reject_unknown_optargs shortname style;
658 trace_call shortname style;
659 pr " return guestfs__%s " shortname;
660 generate_c_call_args ~handle:"g" style;
664 ) non_daemon_functions;
666 (* Client-side stubs for each function. *)
668 fun (shortname, (ret, args, optargs as style), _, _, _, _, _) ->
669 if optargs <> [] then
670 failwithf "optargs not yet implemented for daemon functions";
672 let name = "guestfs_" ^ shortname in
673 let error_code = error_code_of ret in
675 (* Generate the action stub. *)
677 generate_prototype ~extern:false ~semicolon:false ~newline:true
678 ~handle:"g" name style
680 generate_prototype ~extern:false ~semicolon:false ~newline:true
681 ~handle:"g" ~suffix:"_argv" ~optarg_proto:Argv name style;
687 | _ -> pr " struct %s_args args;\n" name
690 pr " guestfs_message_header hdr;\n";
691 pr " guestfs_message_error err;\n";
695 | RConstString _ | RConstOptString _ ->
696 failwithf "RConstString|RConstOptString cannot be used by daemon functions"
698 | RBool _ | RString _ | RStringList _
699 | RStruct _ | RStructList _
700 | RHashtable _ | RBufferOut _ ->
701 pr " struct %s_ret ret;\n" name;
707 check_null_strings shortname style;
708 reject_unknown_optargs shortname style;
709 trace_call shortname style;
710 pr " if (check_state (g, \"%s\") == -1) return %s;\n"
711 shortname error_code;
712 pr " guestfs___set_busy (g);\n";
715 (* Send the main header and arguments. *)
718 pr " serial = guestfs___send (g, GUESTFS_PROC_%s, NULL, NULL);\n"
719 (String.uppercase shortname)
723 | Pathname n | Device n | Dev_or_Path n | String n | Key n ->
724 pr " args.%s = (char *) %s;\n" n n
726 pr " args.%s = %s ? (char **) &%s : NULL;\n" n n n
727 | StringList n | DeviceList n ->
728 pr " args.%s.%s_val = (char **) %s;\n" n n n;
729 pr " for (args.%s.%s_len = 0; %s[args.%s.%s_len]; args.%s.%s_len++) ;\n" n n n n n n n;
731 pr " args.%s = %s;\n" n n
733 pr " args.%s = %s;\n" n n
735 pr " args.%s = %s;\n" n n
736 | FileIn _ | FileOut _ -> ()
738 pr " /* Just catch grossly large sizes. XDR encoding will make this precise. */\n";
739 pr " if (%s_size >= GUESTFS_MESSAGE_MAX) {\n" n;
740 pr " error (g, \"%%s: size of input buffer too large\", \"%s\");\n"
742 pr " guestfs___end_busy (g);\n";
743 pr " return %s;\n" error_code;
745 pr " args.%s.%s_val = (char *) %s;\n" n n n;
746 pr " args.%s.%s_len = %s_size;\n" n n n
748 pr " serial = guestfs___send (g, GUESTFS_PROC_%s,\n"
749 (String.uppercase shortname);
750 pr " (xdrproc_t) xdr_%s_args, (char *) &args);\n"
753 pr " if (serial == -1) {\n";
754 pr " guestfs___end_busy (g);\n";
755 pr " return %s;\n" error_code;
759 (* Send any additional files (FileIn) requested. *)
760 let need_read_reply_label = ref false in
764 pr " r = guestfs___send_file (g, %s);\n" n;
765 pr " if (r == -1) {\n";
766 pr " guestfs___end_busy (g);\n";
767 pr " return %s;\n" error_code;
769 pr " if (r == -2) /* daemon cancelled */\n";
770 pr " goto read_reply;\n";
771 need_read_reply_label := true;
776 (* Wait for the reply from the remote end. *)
777 if !need_read_reply_label then pr " read_reply:\n";
778 pr " memset (&hdr, 0, sizeof hdr);\n";
779 pr " memset (&err, 0, sizeof err);\n";
780 if has_ret then pr " memset (&ret, 0, sizeof ret);\n";
782 pr " r = guestfs___recv (g, \"%s\", &hdr, &err,\n " shortname;
786 pr "(xdrproc_t) xdr_guestfs_%s_ret, (char *) &ret" shortname;
789 pr " if (r == -1) {\n";
790 pr " guestfs___end_busy (g);\n";
791 pr " return %s;\n" error_code;
795 pr " if (check_reply_header (g, &hdr, GUESTFS_PROC_%s, serial) == -1) {\n"
796 (String.uppercase shortname);
797 pr " guestfs___end_busy (g);\n";
798 pr " return %s;\n" error_code;
802 pr " if (hdr.status == GUESTFS_STATUS_ERROR) {\n";
803 pr " error (g, \"%%s: %%s\", \"%s\", err.error_message);\n" shortname;
804 pr " free (err.error_message);\n";
805 pr " guestfs___end_busy (g);\n";
806 pr " return %s;\n" error_code;
810 (* Expecting to receive further files (FileOut)? *)
814 pr " if (guestfs___recv_file (g, %s) == -1) {\n" n;
815 pr " guestfs___end_busy (g);\n";
816 pr " return %s;\n" error_code;
822 pr " guestfs___end_busy (g);\n";
825 | RErr -> pr " return 0;\n"
826 | RInt n | RInt64 n | RBool n ->
827 pr " return ret.%s;\n" n
828 | RConstString _ | RConstOptString _ ->
829 failwithf "RConstString|RConstOptString cannot be used by daemon functions"
831 pr " return ret.%s; /* caller will free */\n" n
832 | RStringList n | RHashtable n ->
833 pr " /* caller will free this, but we need to add a NULL entry */\n";
834 pr " ret.%s.%s_val =\n" n n;
835 pr " safe_realloc (g, ret.%s.%s_val,\n" n n;
836 pr " sizeof (char *) * (ret.%s.%s_len + 1));\n"
838 pr " ret.%s.%s_val[ret.%s.%s_len] = NULL;\n" n n n n;
839 pr " return ret.%s.%s_val;\n" n n
841 pr " /* caller will free this */\n";
842 pr " return safe_memdup (g, &ret.%s, sizeof (ret.%s));\n" n n
843 | RStructList (n, _) ->
844 pr " /* caller will free this */\n";
845 pr " return safe_memdup (g, &ret.%s, sizeof (ret.%s));\n" n n
847 pr " /* RBufferOut is tricky: If the buffer is zero-length, then\n";
848 pr " * _val might be NULL here. To make the API saner for\n";
849 pr " * callers, we turn this case into a unique pointer (using\n";
850 pr " * malloc(1)).\n";
852 pr " if (ret.%s.%s_len > 0) {\n" n n;
853 pr " *size_r = ret.%s.%s_len;\n" n n;
854 pr " return ret.%s.%s_val; /* caller will free */\n" n n;
856 pr " free (ret.%s.%s_val);\n" n n;
857 pr " char *p = safe_malloc (g, 1);\n";
858 pr " *size_r = ret.%s.%s_len;\n" n n;
866 (* Functions to free structures. *)
867 pr "/* Structure-freeing functions. These rely on the fact that the\n";
868 pr " * structure format is identical to the XDR format. See note in\n";
869 pr " * generator.ml.\n";
876 pr "guestfs_free_%s (struct guestfs_%s *x)\n" typ typ;
878 pr " xdr_free ((xdrproc_t) xdr_guestfs_int_%s, (char *) x);\n" typ;
884 pr "guestfs_free_%s_list (struct guestfs_%s_list *x)\n" typ typ;
886 pr " xdr_free ((xdrproc_t) xdr_guestfs_int_%s_list, (char *) x);\n" typ;
893 (* Functions which have optional arguments have two generated variants. *)
896 | shortname, (ret, args, (_::_ as optargs) as style), _, _, _, _, _ ->
897 let uc_shortname = String.uppercase shortname in
899 (* Get the name of the last regular argument. *)
903 | args -> name_of_argt (List.hd (List.rev args)) in
905 let rerrcode, rtype =
907 | RErr | RInt _ | RBool _ -> "-1", "int "
908 | RInt64 _ -> "-1", "int64_t "
909 | RConstString _ | RConstOptString _ -> "NULL", "const char *"
910 | RString _ | RBufferOut _ -> "NULL", "char *"
911 | RStringList _ | RHashtable _ -> "NULL", "char **"
912 | RStruct (_, typ) -> "NULL", sprintf "struct guestfs_%s *" typ
913 | RStructList (_, typ) ->
914 "NULL", sprintf "struct guestfs_%s_list *" typ in
916 (* The regular variable args function, just calls the _va variant. *)
917 generate_prototype ~extern:false ~semicolon:false ~newline:true
918 ~handle:"g" ~prefix:"guestfs_" shortname style;
920 pr " va_list optargs;\n";
922 pr " va_start (optargs, %s);\n" last_arg;
923 pr " %sr = guestfs_%s_va " rtype shortname;
924 generate_c_call_args ~handle:"g" style;
926 pr " va_end (optargs);\n";
931 generate_prototype ~extern:false ~semicolon:false ~newline:true
932 ~handle:"g" ~prefix:"guestfs_" ~suffix:"_va" ~optarg_proto:VA
935 pr " struct guestfs_%s_argv optargs_s;\n" shortname;
936 pr " struct guestfs_%s_argv *optargs = &optargs_s;\n" shortname;
939 pr " optargs_s.bitmask = 0;\n";
941 pr " while ((i = va_arg (args, int)) >= 0) {\n";
942 pr " switch (i) {\n";
946 let n = name_of_argt argt in
947 let uc_n = String.uppercase n in
948 pr " case GUESTFS_%s_%s:\n" uc_shortname uc_n;
949 pr " optargs_s.%s = va_arg (args, " n;
951 | Bool _ | Int _ -> pr "int"
952 | Int64 _ -> pr "int64_t"
953 | String _ -> pr "const char *"
961 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";
962 pr " \"%s\", i);\n" shortname;
963 pr " return %s;\n" rerrcode;
966 pr " uint64_t i_mask = UINT64_C(1) << i;\n";
967 pr " if (optargs_s.bitmask & i_mask) {\n";
968 pr " error (g, \"%%s: same optional argument specified more than once\",\n";
969 pr " \"%s\");\n" shortname;
970 pr " return %s;\n" rerrcode;
972 pr " optargs_s.bitmask |= i_mask;\n";
975 pr " return guestfs_%s_argv " shortname;
976 generate_c_call_args ~handle:"g" style;
980 ) all_functions_sorted
982 (* Generate the linker script which controls the visibility of
983 * symbols in the public ABI and ensures no other symbols get
984 * exported accidentally.
986 and generate_linker_script () =
987 generate_header HashStyle GPLv2plus;
992 "guestfs_get_error_handler";
993 "guestfs_get_out_of_memory_handler";
994 "guestfs_get_private";
995 "guestfs_last_error";
996 "guestfs_set_close_callback";
997 "guestfs_set_error_handler";
998 "guestfs_set_launch_done_callback";
999 "guestfs_set_log_message_callback";
1000 "guestfs_set_out_of_memory_handler";
1001 "guestfs_set_private";
1002 "guestfs_set_progress_callback";
1003 "guestfs_set_subprocess_quit_callback";
1005 (* Unofficial parts of the API: the bindings code use these
1006 * functions, so it is useful to export them.
1008 "guestfs_safe_calloc";
1009 "guestfs_safe_malloc";
1010 "guestfs_safe_strdup";
1011 "guestfs_safe_memdup";
1018 | name, (_, _, []), _, _, _, _, _ -> ["guestfs_" ^ name]
1019 | name, (_, _, _), _, _, _, _, _ ->
1021 "guestfs_" ^ name ^ "_va";
1022 "guestfs_" ^ name ^ "_argv"]
1027 List.map (fun (typ, _) ->
1028 ["guestfs_free_" ^ typ; "guestfs_free_" ^ typ ^ "_list"])
1031 let globals = List.sort compare (globals @ functions @ structs) in
1035 List.iter (pr " %s;\n") globals;
1042 and generate_max_proc_nr () =
1043 pr "%d\n" max_proc_nr