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) ?(indent = "") ?(newline = false)
40 ?(prefix = "") ?(suffix = "")
42 ?(optarg_proto = Dots)
43 name (ret, args, optargs) =
45 if extern then pr "extern ";
46 if static then pr "static ";
52 if single_line then pr " " else pr "\n%s" indent
55 if single_line then pr " " else pr "\n%s" indent
56 | RConstString _ | RConstOptString _ ->
58 if not single_line then pr "\n%s" indent
59 | RString _ | RBufferOut _ ->
61 if not single_line then pr "\n%s" indent
62 | RStringList _ | RHashtable _ ->
64 if not single_line then pr "\n%s" indent
66 if not in_daemon then pr "struct guestfs_%s *" typ
67 else pr "guestfs_int_%s *" typ;
68 if not single_line then pr "\n%s" indent
69 | RStructList (_, typ) ->
70 if not in_daemon then pr "struct guestfs_%s_list *" typ
71 else pr "guestfs_int_%s_list *" typ;
72 if not single_line then pr "\n%s" indent
74 let is_RBufferOut = match ret with RBufferOut _ -> true | _ -> false in
75 pr "%s%s%s (" prefix name suffix;
76 if handle = None && args = [] && optargs = [] && not is_RBufferOut then
79 let comma = ref false in
82 | Some handle -> pr "guestfs_h *%s" handle; comma := true
86 if single_line then pr ", "
88 let namelen = String.length prefix + String.length name +
89 String.length suffix + 2 in
90 pr ",\n%s%s" indent (spaces namelen)
98 | Device n | Dev_or_Path n
103 pr "const char *%s" n
104 | StringList n | DeviceList n ->
106 pr "char *const *%s" n
107 | Bool n -> next (); pr "int %s" n
108 | Int n -> next (); pr "int %s" n
109 | Int64 n -> next (); pr "int64_t %s" n
112 if not in_daemon then (next (); pr "const char *%s" n)
115 pr "const char *%s" n;
117 pr "size_t %s_size" n
119 if is_RBufferOut then (next (); pr "size_t *size_r");
120 if optargs <> [] then (
122 match optarg_proto with
124 | VA -> pr "va_list args"
125 | Argv -> pr "const struct guestfs_%s_argv *optargs" name
129 if semicolon then pr ";";
130 if newline then pr "\n"
132 (* Generate C call arguments, eg "(handle, foo, bar)" *)
133 and generate_c_call_args ?handle (ret, args, optargs) =
135 let comma = ref false in
137 if !comma then pr ", ";
142 | Some handle -> pr "%s" handle; comma := true
151 pr "%s" (name_of_argt arg)
153 (* For RBufferOut calls, add implicit &size parameter. *)
160 (* For calls with optional arguments, add implicit optargs parameter. *)
161 if optargs <> [] then (
167 (* Generate the pod documentation for the C API. *)
168 and generate_actions_pod () =
170 fun (shortname, (ret, args, optargs as style), _, flags, _, _, longdesc) ->
171 if not (List.mem NotInDocs flags) then (
172 let name = "guestfs_" ^ shortname in
173 pr "=head2 %s\n\n" name;
174 generate_prototype ~extern:false ~indent:" " ~handle:"g" name style;
177 let uc_shortname = String.uppercase shortname in
178 if optargs <> [] then (
179 pr "You may supply a list of optional arguments to this call.\n";
180 pr "Use zero or more of the following pairs of parameters,\n";
181 pr "and terminate the list with C<-1> on its own.\n";
182 pr "See L</CALLS WITH OPTIONAL ARGUMENTS>.\n\n";
185 let n = name_of_argt argt in
186 let uc_n = String.uppercase n in
187 pr " GUESTFS_%s_%s, " uc_shortname uc_n;
189 | Bool n -> pr "int %s,\n" n
190 | Int n -> pr "int %s,\n" n
191 | Int64 n -> pr "int64_t %s,\n" n
192 | String n -> pr "const char *%s,\n" n
198 pr "%s\n\n" longdesc;
199 let ret, args, optargs = style in
202 pr "This function returns 0 on success or -1 on error.\n\n"
204 pr "On error this function returns -1.\n\n"
206 pr "On error this function returns -1.\n\n"
208 pr "This function returns a C truth value on success or -1 on error.\n\n"
210 pr "This function returns a string, or NULL on error.
211 The string is owned by the guest handle and must I<not> be freed.\n\n"
212 | RConstOptString _ ->
213 pr "This function returns a string which may be NULL.
214 There is no way to return an error from this function.
215 The string is owned by the guest handle and must I<not> be freed.\n\n"
217 pr "This function returns a string, or NULL on error.
218 I<The caller must free the returned string after use>.\n\n"
220 pr "This function returns a NULL-terminated array of strings
221 (like L<environ(3)>), or NULL if there was an error.
222 I<The caller must free the strings and the array after use>.\n\n"
223 | RStruct (_, typ) ->
224 pr "This function returns a C<struct guestfs_%s *>,
225 or NULL if there was an error.
226 I<The caller must call C<guestfs_free_%s> after use>.\n\n" typ typ
227 | RStructList (_, typ) ->
228 pr "This function returns a C<struct guestfs_%s_list *>
229 (see E<lt>guestfs-structs.hE<gt>),
230 or NULL if there was an error.
231 I<The caller must call C<guestfs_free_%s_list> after use>.\n\n" typ typ
233 pr "This function returns a NULL-terminated array of
234 strings, or NULL if there was an error.
235 The array of strings will always have length C<2n+1>, where
236 C<n> keys and values alternate, followed by the trailing NULL entry.
237 I<The caller must free the strings and the array after use>.\n\n"
239 pr "This function returns a buffer, or NULL on error.
240 The size of the returned buffer is written to C<*size_r>.
241 I<The caller must free the returned buffer after use>.\n\n"
243 if List.mem Progress flags then
244 pr "%s\n\n" progress_message;
245 if List.mem ProtocolLimitWarning flags then
246 pr "%s\n\n" protocol_limit_warning;
247 if List.mem DangerWillRobinson flags then
248 pr "%s\n\n" danger_will_robinson;
249 if List.exists (function Key _ -> true | _ -> false) (args@optargs) then
250 pr "This function takes a key or passphrase parameter which
251 could contain sensitive material. Read the section
252 L</KEYS AND PASSPHRASES> for more information.\n\n";
253 (match deprecation_notice flags with
255 | Some txt -> pr "%s\n\n" txt
258 (* Handling of optional argument variants. *)
259 if optargs <> [] then (
260 pr "=head2 %s_va\n\n" name;
261 generate_prototype ~extern:false ~indent:" " ~handle:"g"
262 ~prefix:"guestfs_" ~suffix:"_va" ~optarg_proto:VA
265 pr "This is the \"va_list variant\" of L</%s>.\n\n" name;
266 pr "See L</CALLS WITH OPTIONAL ARGUMENTS>.\n\n";
267 pr "=head2 %s_argv\n\n" name;
268 generate_prototype ~extern:false ~indent:" " ~handle:"g"
269 ~prefix:"guestfs_" ~suffix:"_argv" ~optarg_proto:Argv
272 pr "This is the \"argv variant\" of L</%s>.\n\n" name;
273 pr "See L</CALLS WITH OPTIONAL ARGUMENTS>.\n\n";
276 ) all_functions_sorted
278 and generate_structs_pod () =
279 (* Structs documentation. *)
282 pr "=head2 guestfs_%s\n" typ;
284 pr " struct guestfs_%s {\n" typ;
287 | name, FChar -> pr " char %s;\n" name
288 | name, FUInt32 -> pr " uint32_t %s;\n" name
289 | name, FInt32 -> pr " int32_t %s;\n" name
290 | name, (FUInt64|FBytes) -> pr " uint64_t %s;\n" name
291 | name, FInt64 -> pr " int64_t %s;\n" name
292 | name, FString -> pr " char *%s;\n" name
294 pr " /* The next two fields describe a byte array. */\n";
295 pr " uint32_t %s_len;\n" name;
296 pr " char *%s;\n" name
298 pr " /* The next field is NOT nul-terminated, be careful when printing it: */\n";
299 pr " char %s[32];\n" name
300 | name, FOptPercent ->
301 pr " /* The next field is [0..100] or -1 meaning 'not present': */\n";
302 pr " float %s;\n" name
306 pr " struct guestfs_%s_list {\n" typ;
307 pr " uint32_t len; /* Number of elements in list. */\n";
308 pr " struct guestfs_%s *val; /* Elements. */\n" typ;
311 pr " void guestfs_free_%s (struct guestfs_free_%s *);\n" typ typ;
312 pr " void guestfs_free_%s_list (struct guestfs_free_%s_list *);\n"
317 and generate_availability_pod () =
318 (* Availability documentation. *)
322 fun (group, functions) ->
323 pr "=item B<%s>\n" group;
325 pr "The following functions:\n";
326 List.iter (pr "L</guestfs_%s>\n") functions;
332 (* Generate the guestfs-structs.h file. *)
333 and generate_structs_h () =
334 generate_header CStyle LGPLv2plus;
336 (* This is a public exported header file containing various
337 * structures. The structures are carefully written to have
338 * exactly the same in-memory format as the XDR structures that
339 * we use on the wire to the daemon. The reason for creating
340 * copies of these structures here is just so we don't have to
341 * export the whole of guestfs_protocol.h (which includes much
342 * unrelated and XDR-dependent stuff that we don't want to be
343 * public, or required by clients).
345 * To reiterate, we will pass these structures to and from the
346 * client with a simple assignment or memcpy, so the format
347 * must be identical to what rpcgen / the RFC defines.
350 (* Public structures. *)
353 pr "struct guestfs_%s {\n" typ;
356 | name, FChar -> pr " char %s;\n" name
357 | name, FString -> pr " char *%s;\n" name
359 pr " uint32_t %s_len;\n" name;
360 pr " char *%s;\n" name
361 | name, FUUID -> pr " char %s[32]; /* this is NOT nul-terminated, be careful when printing */\n" name
362 | name, FUInt32 -> pr " uint32_t %s;\n" name
363 | name, FInt32 -> pr " int32_t %s;\n" name
364 | name, (FUInt64|FBytes) -> pr " uint64_t %s;\n" name
365 | name, FInt64 -> pr " int64_t %s;\n" name
366 | name, FOptPercent -> pr " float %s; /* [0..100] or -1 */\n" name
370 pr "struct guestfs_%s_list {\n" typ;
371 pr " uint32_t len;\n";
372 pr " struct guestfs_%s *val;\n" typ;
375 pr "extern void guestfs_free_%s (struct guestfs_%s *);\n" typ typ;
376 pr "extern void guestfs_free_%s_list (struct guestfs_%s_list *);\n" typ typ;
380 (* Generate the guestfs-actions.h file. *)
381 and generate_actions_h () =
382 generate_header CStyle LGPLv2plus;
384 fun (shortname, (ret, args, optargs as style), _, flags, _, _, _) ->
386 List.exists (function DeprecatedBy _ -> true | _ -> false) flags in
388 String.length shortname >= 5 && String.sub shortname 0 5 = "test0" in
390 String.length shortname >= 5 && String.sub shortname 0 5 = "debug" in
391 if not deprecated && not test0 && not debug then
392 pr "#define LIBGUESTFS_HAVE_%s 1\n" (String.uppercase shortname);
394 generate_prototype ~single_line:true ~newline:true ~handle:"g"
395 ~prefix:"guestfs_" shortname style;
397 if optargs <> [] then (
398 generate_prototype ~single_line:true ~newline:true ~handle:"g"
399 ~prefix:"guestfs_" ~suffix:"_va" ~optarg_proto:VA
402 pr "struct guestfs_%s_argv {\n" shortname;
403 pr " uint64_t bitmask;\n";
409 | Int n -> "int64_t "
411 | String n -> "const char *"
412 | _ -> assert false (* checked in generator_checks *) in
413 let uc_shortname = String.uppercase shortname in
414 let n = name_of_argt argt in
415 let uc_n = String.uppercase n in
416 pr "#define GUESTFS_%s_%s %d\n" uc_shortname uc_n i;
417 pr "#define GUESTFS_%s_%s_BITMASK (UINT64_C(1)<<%d)\n" uc_shortname uc_n i;
418 pr "/* The field below is only valid in this struct if the\n";
419 pr " * GUESTFS_%s_%s_BITMASK bit is set\n" uc_shortname uc_n;
420 pr " * in the bitmask above, otherwise the contents are ignored.\n";
422 pr " %s%s;\n" c_type n
426 generate_prototype ~single_line:true ~newline:true ~handle:"g"
427 ~prefix:"guestfs_" ~suffix:"_argv" ~optarg_proto:Argv
430 ) all_functions_sorted
432 (* Generate the guestfs-internal-actions.h file. *)
433 and generate_internal_actions_h () =
434 generate_header CStyle LGPLv2plus;
436 fun (shortname, style, _, _, _, _, _) ->
437 generate_prototype ~single_line:true ~newline:true ~handle:"g"
438 ~prefix:"guestfs__" ~optarg_proto:Argv
440 ) non_daemon_functions
442 (* Generate the client-side dispatch stubs. *)
443 and generate_client_actions () =
444 generate_header CStyle LGPLv2plus;
451 #include <inttypes.h>
453 #include \"guestfs.h\"
454 #include \"guestfs-internal.h\"
455 #include \"guestfs-internal-actions.h\"
456 #include \"guestfs_protocol.h\"
458 /* Check the return message from a call for validity. */
460 check_reply_header (guestfs_h *g,
461 const struct guestfs_message_header *hdr,
462 unsigned int proc_nr, unsigned int serial)
464 if (hdr->prog != GUESTFS_PROGRAM) {
465 error (g, \"wrong program (%%d/%%d)\", hdr->prog, GUESTFS_PROGRAM);
468 if (hdr->vers != GUESTFS_PROTOCOL_VERSION) {
469 error (g, \"wrong protocol version (%%d/%%d)\",
470 hdr->vers, GUESTFS_PROTOCOL_VERSION);
473 if (hdr->direction != GUESTFS_DIRECTION_REPLY) {
474 error (g, \"unexpected message direction (%%d/%%d)\",
475 hdr->direction, GUESTFS_DIRECTION_REPLY);
478 if (hdr->proc != proc_nr) {
479 error (g, \"unexpected procedure number (%%d/%%d)\", hdr->proc, proc_nr);
482 if (hdr->serial != serial) {
483 error (g, \"unexpected serial (%%d/%%d)\", hdr->serial, serial);
490 /* Check we are in the right state to run a high-level action. */
492 check_state (guestfs_h *g, const char *caller)
494 if (!guestfs__is_ready (g)) {
495 if (guestfs__is_config (g) || guestfs__is_launching (g))
496 error (g, \"%%s: call launch before using this function\\n(in guestfish, don't forget to use the 'run' command)\",
499 error (g, \"%%s called from the wrong state, %%d != READY\",
500 caller, guestfs__get_state (g));
508 let error_code_of = function
509 | RErr | RInt _ | RInt64 _ | RBool _ -> "-1"
510 | RConstString _ | RConstOptString _
511 | RString _ | RStringList _
512 | RStruct _ | RStructList _
513 | RHashtable _ | RBufferOut _ -> "NULL"
516 (* Generate code to check String-like parameters are not passed in
517 * as NULL (returning an error if they are).
519 let check_null_strings shortname (ret, args, optargs) =
520 let pr_newline = ref false in
523 (* parameters which should not be NULL *)
534 pr " if (%s == NULL) {\n" n;
535 pr " error (g, \"%%s: %%s: parameter cannot be NULL\",\n";
536 pr " \"%s\", \"%s\");\n" shortname n;
537 pr " return %s;\n" (error_code_of ret);
550 (* For optional arguments. *)
554 pr " if ((optargs->bitmask & GUESTFS_%s_%s_BITMASK) &&\n"
555 (String.uppercase shortname) (String.uppercase n);
556 pr " optargs->%s == NULL) {\n" n;
557 pr " error (g, \"%%s: %%s: optional parameter cannot be NULL\",\n";
558 pr " \"%s\", \"%s\");\n" shortname n;
559 pr " return %s;\n" (error_code_of ret);
564 | Bool _ | Int _ | Int64 _ -> ()
569 if !pr_newline then pr "\n";
572 (* Generate code to reject optargs we don't know about. *)
573 let reject_unknown_optargs shortname = function
576 let len = List.length optargs in
577 let mask = Int64.lognot (Int64.pred (Int64.shift_left 1L len)) in
578 pr " if (optargs->bitmask & UINT64_C(0x%Lx)) {\n" mask;
579 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";
580 pr " \"%s\", \"%s\");\n" shortname shortname;
581 pr " return %s;\n" (error_code_of ret);
586 (* Generate code to generate guestfish call traces. *)
587 let trace_call shortname (ret, args, optargs) =
588 pr " if (guestfs__get_trace (g)) {\n";
591 List.exists (function
592 | StringList _ | DeviceList _ -> true
593 | _ -> false) args in
599 pr " fprintf (stderr, \"%s\");\n" shortname;
601 (* Required arguments. *)
604 | String n (* strings *)
610 (* guestfish doesn't support string escaping, so neither do we *)
611 pr " fprintf (stderr, \" \\\"%%s\\\"\", %s);\n" n
613 (* don't print keys *)
614 pr " fprintf (stderr, \" \\\"***\\\"\");\n"
615 | OptString n -> (* string option *)
616 pr " if (%s) fprintf (stderr, \" \\\"%%s\\\"\", %s);\n" n n;
617 pr " else fprintf (stderr, \" null\");\n"
619 | DeviceList n -> (* string list *)
620 pr " fputc (' ', stderr);\n";
621 pr " fputc ('\"', stderr);\n";
622 pr " for (i = 0; %s[i]; ++i) {\n" n;
623 pr " if (i > 0) fputc (' ', stderr);\n";
624 pr " fputs (%s[i], stderr);\n" n;
626 pr " fputc ('\"', stderr);\n";
627 | Bool n -> (* boolean *)
628 pr " fputs (%s ? \" true\" : \" false\", stderr);\n" n
630 pr " fprintf (stderr, \" %%d\", %s);\n" n
632 pr " fprintf (stderr, \" %%\" PRIi64, %s);\n" n
633 | BufferIn n -> (* RHBZ#646822 *)
634 pr " fputc (' ', stderr);\n";
635 pr " guestfs___print_BufferIn (stderr, %s, %s_size);\n" n n
638 (* Optional arguments. *)
641 let n = name_of_argt argt in
642 let uc_shortname = String.uppercase shortname in
643 let uc_n = String.uppercase n in
644 pr " if (optargs->bitmask & GUESTFS_%s_%s_BITMASK)\n"
648 pr " fprintf (stderr, \" \\\"%%s:%%s\\\"\", \"%s\", optargs->%s);\n" n n
650 pr " fprintf (stderr, \" \\\"%%s:%%s\\\"\", \"%s\", optargs->%s ? \"true\" : \"false\");\n" n n
652 pr " fprintf (stderr, \" \\\"%%s:%%d\\\"\", \"%s\", optargs->%s);\n" n n
654 pr " fprintf (stderr, \" \\\"%%s:%%\" PRIi64 \"\\\"\", \"%s\", optargs->%s);\n" n n
659 pr " fputc ('\\n', stderr);\n";
664 (* For non-daemon functions, generate a wrapper around each function. *)
666 fun (shortname, (_, _, optargs as style), _, _, _, _, _) ->
668 generate_prototype ~extern:false ~semicolon:false ~newline:true
669 ~handle:"g" ~prefix:"guestfs_"
672 generate_prototype ~extern:false ~semicolon:false ~newline:true
673 ~handle:"g" ~prefix:"guestfs_" ~suffix:"_argv" ~optarg_proto:Argv
676 check_null_strings shortname style;
677 reject_unknown_optargs shortname style;
678 trace_call shortname style;
679 pr " return guestfs__%s " shortname;
680 generate_c_call_args ~handle:"g" style;
684 ) non_daemon_functions;
686 (* Client-side stubs for each function. *)
688 fun (shortname, (ret, args, optargs as style), _, _, _, _, _) ->
689 if optargs <> [] then
690 failwithf "optargs not yet implemented for daemon functions";
692 let name = "guestfs_" ^ shortname in
693 let error_code = error_code_of ret in
695 (* Generate the action stub. *)
697 generate_prototype ~extern:false ~semicolon:false ~newline:true
698 ~handle:"g" name style
700 generate_prototype ~extern:false ~semicolon:false ~newline:true
701 ~handle:"g" ~suffix:"_argv" ~optarg_proto:Argv name style;
707 | _ -> pr " struct %s_args args;\n" name
710 pr " guestfs_message_header hdr;\n";
711 pr " guestfs_message_error err;\n";
715 | RConstString _ | RConstOptString _ ->
716 failwithf "RConstString|RConstOptString cannot be used by daemon functions"
718 | RBool _ | RString _ | RStringList _
719 | RStruct _ | RStructList _
720 | RHashtable _ | RBufferOut _ ->
721 pr " struct %s_ret ret;\n" name;
727 check_null_strings shortname style;
728 reject_unknown_optargs shortname style;
729 trace_call shortname style;
730 pr " if (check_state (g, \"%s\") == -1) return %s;\n"
731 shortname error_code;
732 pr " guestfs___set_busy (g);\n";
735 (* Send the main header and arguments. *)
738 pr " serial = guestfs___send (g, GUESTFS_PROC_%s, NULL, NULL);\n"
739 (String.uppercase shortname)
743 | Pathname n | Device n | Dev_or_Path n | String n | Key n ->
744 pr " args.%s = (char *) %s;\n" n n
746 pr " args.%s = %s ? (char **) &%s : NULL;\n" n n n
747 | StringList n | DeviceList n ->
748 pr " args.%s.%s_val = (char **) %s;\n" n n n;
749 pr " for (args.%s.%s_len = 0; %s[args.%s.%s_len]; args.%s.%s_len++) ;\n" n n n n n n n;
751 pr " args.%s = %s;\n" n n
753 pr " args.%s = %s;\n" n n
755 pr " args.%s = %s;\n" n n
756 | FileIn _ | FileOut _ -> ()
758 pr " /* Just catch grossly large sizes. XDR encoding will make this precise. */\n";
759 pr " if (%s_size >= GUESTFS_MESSAGE_MAX) {\n" n;
760 pr " error (g, \"%%s: size of input buffer too large\", \"%s\");\n"
762 pr " guestfs___end_busy (g);\n";
763 pr " return %s;\n" error_code;
765 pr " args.%s.%s_val = (char *) %s;\n" n n n;
766 pr " args.%s.%s_len = %s_size;\n" n n n
768 pr " serial = guestfs___send (g, GUESTFS_PROC_%s,\n"
769 (String.uppercase shortname);
770 pr " (xdrproc_t) xdr_%s_args, (char *) &args);\n"
773 pr " if (serial == -1) {\n";
774 pr " guestfs___end_busy (g);\n";
775 pr " return %s;\n" error_code;
779 (* Send any additional files (FileIn) requested. *)
780 let need_read_reply_label = ref false in
784 pr " r = guestfs___send_file (g, %s);\n" n;
785 pr " if (r == -1) {\n";
786 pr " guestfs___end_busy (g);\n";
787 pr " return %s;\n" error_code;
789 pr " if (r == -2) /* daemon cancelled */\n";
790 pr " goto read_reply;\n";
791 need_read_reply_label := true;
796 (* Wait for the reply from the remote end. *)
797 if !need_read_reply_label then pr " read_reply:\n";
798 pr " memset (&hdr, 0, sizeof hdr);\n";
799 pr " memset (&err, 0, sizeof err);\n";
800 if has_ret then pr " memset (&ret, 0, sizeof ret);\n";
802 pr " r = guestfs___recv (g, \"%s\", &hdr, &err,\n " shortname;
806 pr "(xdrproc_t) xdr_guestfs_%s_ret, (char *) &ret" shortname;
809 pr " if (r == -1) {\n";
810 pr " guestfs___end_busy (g);\n";
811 pr " return %s;\n" error_code;
815 pr " if (check_reply_header (g, &hdr, GUESTFS_PROC_%s, serial) == -1) {\n"
816 (String.uppercase shortname);
817 pr " guestfs___end_busy (g);\n";
818 pr " return %s;\n" error_code;
822 pr " if (hdr.status == GUESTFS_STATUS_ERROR) {\n";
823 pr " error (g, \"%%s: %%s\", \"%s\", err.error_message);\n" shortname;
824 pr " free (err.error_message);\n";
825 pr " guestfs___end_busy (g);\n";
826 pr " return %s;\n" error_code;
830 (* Expecting to receive further files (FileOut)? *)
834 pr " if (guestfs___recv_file (g, %s) == -1) {\n" n;
835 pr " guestfs___end_busy (g);\n";
836 pr " return %s;\n" error_code;
842 pr " guestfs___end_busy (g);\n";
845 | RErr -> pr " return 0;\n"
846 | RInt n | RInt64 n | RBool n ->
847 pr " return ret.%s;\n" n
848 | RConstString _ | RConstOptString _ ->
849 failwithf "RConstString|RConstOptString cannot be used by daemon functions"
851 pr " return ret.%s; /* caller will free */\n" n
852 | RStringList n | RHashtable n ->
853 pr " /* caller will free this, but we need to add a NULL entry */\n";
854 pr " ret.%s.%s_val =\n" n n;
855 pr " safe_realloc (g, ret.%s.%s_val,\n" n n;
856 pr " sizeof (char *) * (ret.%s.%s_len + 1));\n"
858 pr " ret.%s.%s_val[ret.%s.%s_len] = NULL;\n" n n n n;
859 pr " return ret.%s.%s_val;\n" n n
861 pr " /* caller will free this */\n";
862 pr " return safe_memdup (g, &ret.%s, sizeof (ret.%s));\n" n n
863 | RStructList (n, _) ->
864 pr " /* caller will free this */\n";
865 pr " return safe_memdup (g, &ret.%s, sizeof (ret.%s));\n" n n
867 pr " /* RBufferOut is tricky: If the buffer is zero-length, then\n";
868 pr " * _val might be NULL here. To make the API saner for\n";
869 pr " * callers, we turn this case into a unique pointer (using\n";
870 pr " * malloc(1)).\n";
872 pr " if (ret.%s.%s_len > 0) {\n" n n;
873 pr " *size_r = ret.%s.%s_len;\n" n n;
874 pr " return ret.%s.%s_val; /* caller will free */\n" n n;
876 pr " free (ret.%s.%s_val);\n" n n;
877 pr " char *p = safe_malloc (g, 1);\n";
878 pr " *size_r = ret.%s.%s_len;\n" n n;
886 (* Functions to free structures. *)
887 pr "/* Structure-freeing functions. These rely on the fact that the\n";
888 pr " * structure format is identical to the XDR format. See note in\n";
889 pr " * generator.ml.\n";
896 pr "guestfs_free_%s (struct guestfs_%s *x)\n" typ typ;
898 pr " xdr_free ((xdrproc_t) xdr_guestfs_int_%s, (char *) x);\n" typ;
904 pr "guestfs_free_%s_list (struct guestfs_%s_list *x)\n" typ typ;
906 pr " xdr_free ((xdrproc_t) xdr_guestfs_int_%s_list, (char *) x);\n" typ;
913 (* Functions which have optional arguments have two generated variants. *)
916 | shortname, (ret, args, (_::_ as optargs) as style), _, _, _, _, _ ->
917 let uc_shortname = String.uppercase shortname in
919 (* Get the name of the last regular argument. *)
923 | args -> name_of_argt (List.hd (List.rev args)) in
925 let rerrcode, rtype =
927 | RErr | RInt _ | RBool _ -> "-1", "int "
928 | RInt64 _ -> "-1", "int64_t "
929 | RConstString _ | RConstOptString _ -> "NULL", "const char *"
930 | RString _ | RBufferOut _ -> "NULL", "char *"
931 | RStringList _ | RHashtable _ -> "NULL", "char **"
932 | RStruct (_, typ) -> "NULL", sprintf "struct guestfs_%s *" typ
933 | RStructList (_, typ) ->
934 "NULL", sprintf "struct guestfs_%s_list *" typ in
936 (* The regular variable args function, just calls the _va variant. *)
937 generate_prototype ~extern:false ~semicolon:false ~newline:true
938 ~handle:"g" ~prefix:"guestfs_" shortname style;
940 pr " va_list optargs;\n";
942 pr " va_start (optargs, %s);\n" last_arg;
943 pr " %sr = guestfs_%s_va " rtype shortname;
944 generate_c_call_args ~handle:"g" style;
946 pr " va_end (optargs);\n";
951 generate_prototype ~extern:false ~semicolon:false ~newline:true
952 ~handle:"g" ~prefix:"guestfs_" ~suffix:"_va" ~optarg_proto:VA
955 pr " struct guestfs_%s_argv optargs_s;\n" shortname;
956 pr " struct guestfs_%s_argv *optargs = &optargs_s;\n" shortname;
959 pr " optargs_s.bitmask = 0;\n";
961 pr " while ((i = va_arg (args, int)) >= 0) {\n";
962 pr " switch (i) {\n";
966 let n = name_of_argt argt in
967 let uc_n = String.uppercase n in
968 pr " case GUESTFS_%s_%s:\n" uc_shortname uc_n;
969 pr " optargs_s.%s = va_arg (args, " n;
971 | Bool _ | Int _ -> pr "int"
972 | Int64 _ -> pr "int64_t"
973 | String _ -> pr "const char *"
981 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";
982 pr " \"%s\", i);\n" shortname;
983 pr " return %s;\n" rerrcode;
986 pr " uint64_t i_mask = UINT64_C(1) << i;\n";
987 pr " if (optargs_s.bitmask & i_mask) {\n";
988 pr " error (g, \"%%s: same optional argument specified more than once\",\n";
989 pr " \"%s\");\n" shortname;
990 pr " return %s;\n" rerrcode;
992 pr " optargs_s.bitmask |= i_mask;\n";
995 pr " return guestfs_%s_argv " shortname;
996 generate_c_call_args ~handle:"g" style;
1000 ) all_functions_sorted
1002 (* Generate the linker script which controls the visibility of
1003 * symbols in the public ABI and ensures no other symbols get
1004 * exported accidentally.
1006 and generate_linker_script () =
1007 generate_header HashStyle GPLv2plus;
1012 "guestfs_get_error_handler";
1013 "guestfs_get_out_of_memory_handler";
1014 "guestfs_get_private";
1015 "guestfs_last_error";
1016 "guestfs_set_close_callback";
1017 "guestfs_set_error_handler";
1018 "guestfs_set_launch_done_callback";
1019 "guestfs_set_log_message_callback";
1020 "guestfs_set_out_of_memory_handler";
1021 "guestfs_set_private";
1022 "guestfs_set_progress_callback";
1023 "guestfs_set_subprocess_quit_callback";
1025 (* Unofficial parts of the API: the bindings code use these
1026 * functions, so it is useful to export them.
1028 "guestfs_safe_calloc";
1029 "guestfs_safe_malloc";
1030 "guestfs_safe_strdup";
1031 "guestfs_safe_memdup";
1038 | name, (_, _, []), _, _, _, _, _ -> ["guestfs_" ^ name]
1039 | name, (_, _, _), _, _, _, _, _ ->
1041 "guestfs_" ^ name ^ "_va";
1042 "guestfs_" ^ name ^ "_argv"]
1047 List.map (fun (typ, _) ->
1048 ["guestfs_free_" ^ typ; "guestfs_free_" ^ typ ^ "_list"])
1051 let globals = List.sort compare (globals @ functions @ structs) in
1055 List.iter (pr " %s;\n") globals;
1062 and generate_max_proc_nr () =
1063 pr "%d\n" max_proc_nr