2 * Copyright (C) 2009-2011 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
123 if is_RBufferOut then (next (); pr "size_t *size_r");
124 if optargs <> [] then (
126 match optarg_proto with
128 | VA -> pr "va_list args"
129 | Argv -> pr "const struct guestfs_%s_argv *optargs" name
133 if semicolon then pr ";";
134 if newline then pr "\n"
136 (* Generate C call arguments, eg "(handle, foo, bar)" *)
137 and generate_c_call_args ?handle (ret, args, optargs) =
139 let comma = ref false in
141 if !comma then pr ", ";
146 | Some handle -> pr "%s" handle; comma := true
155 pr "%s" (name_of_argt arg)
157 (* For RBufferOut calls, add implicit &size parameter. *)
164 (* For calls with optional arguments, add implicit optargs parameter. *)
165 if optargs <> [] then (
171 (* Generate the pod documentation for the C API. *)
172 and generate_actions_pod () =
174 fun (shortname, (ret, args, optargs as style), _, flags, _, _, longdesc) ->
175 if not (List.mem NotInDocs flags) then (
176 let name = "guestfs_" ^ shortname in
177 pr "=head2 %s\n\n" name;
178 generate_prototype ~extern:false ~indent:" " ~handle:"g" name style;
181 let uc_shortname = String.uppercase shortname in
182 if optargs <> [] then (
183 pr "You may supply a list of optional arguments to this call.\n";
184 pr "Use zero or more of the following pairs of parameters,\n";
185 pr "and terminate the list with C<-1> on its own.\n";
186 pr "See L</CALLS WITH OPTIONAL ARGUMENTS>.\n\n";
189 let n = name_of_argt argt in
190 let uc_n = String.uppercase n in
191 pr " GUESTFS_%s_%s, " uc_shortname uc_n;
193 | Bool n -> pr "int %s,\n" n
194 | Int n -> pr "int %s,\n" n
195 | Int64 n -> pr "int64_t %s,\n" n
196 | String n -> pr "const char *%s,\n" n
202 pr "%s\n\n" longdesc;
203 let ret, args, optargs = style in
206 pr "This function returns 0 on success or -1 on error.\n\n"
208 pr "On error this function returns -1.\n\n"
210 pr "On error this function returns -1.\n\n"
212 pr "This function returns a C truth value on success or -1 on error.\n\n"
214 pr "This function returns a string, or NULL on error.
215 The string is owned by the guest handle and must I<not> be freed.\n\n"
216 | RConstOptString _ ->
217 pr "This function returns a string which may be NULL.
218 There is no way to return an error from this function.
219 The string is owned by the guest handle and must I<not> be freed.\n\n"
221 pr "This function returns a string, or NULL on error.
222 I<The caller must free the returned string after use>.\n\n"
224 pr "This function returns a NULL-terminated array of strings
225 (like L<environ(3)>), or NULL if there was an error.
226 I<The caller must free the strings and the array after use>.\n\n"
227 | RStruct (_, typ) ->
228 pr "This function returns a C<struct guestfs_%s *>,
229 or NULL if there was an error.
230 I<The caller must call C<guestfs_free_%s> after use>.\n\n" typ typ
231 | RStructList (_, typ) ->
232 pr "This function returns a C<struct guestfs_%s_list *>,
233 or NULL if there was an error.
234 I<The caller must call C<guestfs_free_%s_list> after use>.\n\n" typ typ
236 pr "This function returns a NULL-terminated array of
237 strings, or NULL if there was an error.
238 The array of strings will always have length C<2n+1>, where
239 C<n> keys and values alternate, followed by the trailing NULL entry.
240 I<The caller must free the strings and the array after use>.\n\n"
242 pr "This function returns a buffer, or NULL on error.
243 The size of the returned buffer is written to C<*size_r>.
244 I<The caller must free the returned buffer after use>.\n\n"
246 if List.mem Progress flags then
247 pr "%s\n\n" progress_message;
248 if List.mem ProtocolLimitWarning flags then
249 pr "%s\n\n" protocol_limit_warning;
250 if List.mem DangerWillRobinson flags then
251 pr "%s\n\n" danger_will_robinson;
252 if List.exists (function Key _ -> true | _ -> false) (args@optargs) then
253 pr "This function takes a key or passphrase parameter which
254 could contain sensitive material. Read the section
255 L</KEYS AND PASSPHRASES> for more information.\n\n";
256 (match deprecation_notice flags with
258 | Some txt -> pr "%s\n\n" txt
260 (match lookup_api_version name with
261 | Some version -> pr "(Added in %s)\n\n" version
265 (* Handling of optional argument variants. *)
266 if optargs <> [] then (
267 pr "=head2 %s_va\n\n" name;
268 generate_prototype ~extern:false ~indent:" " ~handle:"g"
269 ~prefix:"guestfs_" ~suffix:"_va" ~optarg_proto:VA
272 pr "This is the \"va_list variant\" of L</%s>.\n\n" name;
273 pr "See L</CALLS WITH OPTIONAL ARGUMENTS>.\n\n";
274 pr "=head2 %s_argv\n\n" name;
275 generate_prototype ~extern:false ~indent:" " ~handle:"g"
276 ~prefix:"guestfs_" ~suffix:"_argv" ~optarg_proto:Argv
279 pr "This is the \"argv variant\" of L</%s>.\n\n" name;
280 pr "See L</CALLS WITH OPTIONAL ARGUMENTS>.\n\n";
283 ) all_functions_sorted
285 and generate_structs_pod () =
286 (* Structs documentation. *)
289 pr "=head2 guestfs_%s\n" typ;
291 pr " struct guestfs_%s {\n" typ;
294 | name, FChar -> pr " char %s;\n" name
295 | name, FUInt32 -> pr " uint32_t %s;\n" name
296 | name, FInt32 -> pr " int32_t %s;\n" name
297 | name, (FUInt64|FBytes) -> pr " uint64_t %s;\n" name
298 | name, FInt64 -> pr " int64_t %s;\n" name
299 | name, FString -> pr " char *%s;\n" name
301 pr " /* The next two fields describe a byte array. */\n";
302 pr " uint32_t %s_len;\n" name;
303 pr " char *%s;\n" name
305 pr " /* The next field is NOT nul-terminated, be careful when printing it: */\n";
306 pr " char %s[32];\n" name
307 | name, FOptPercent ->
308 pr " /* The next field is [0..100] or -1 meaning 'not present': */\n";
309 pr " float %s;\n" name
313 pr " struct guestfs_%s_list {\n" typ;
314 pr " uint32_t len; /* Number of elements in list. */\n";
315 pr " struct guestfs_%s *val; /* Elements. */\n" typ;
318 pr " void guestfs_free_%s (struct guestfs_free_%s *);\n" typ typ;
319 pr " void guestfs_free_%s_list (struct guestfs_free_%s_list *);\n"
324 and generate_availability_pod () =
325 (* Availability documentation. *)
329 fun (group, functions) ->
330 pr "=item B<%s>\n" group;
332 pr "The following functions:\n";
333 List.iter (pr "L</guestfs_%s>\n") functions;
339 (* Generate the guestfs.h file. *)
340 and generate_guestfs_h () =
341 generate_header CStyle LGPLv2plus;
344 /* ---------- IMPORTANT NOTE ----------
346 * All API documentation is in the manpage, 'guestfs(3)'.
347 * To read it, type: man 3 guestfs
348 * Or read it online here: http://libguestfs.org/guestfs.3.html
350 * Go and read it now, I'll be right here waiting for you
351 * when you come back.
353 * ------------------------------------
368 #ifndef GUESTFS_TYPEDEF_H
369 #define GUESTFS_TYPEDEF_H 1
370 typedef struct guestfs_h guestfs_h;
373 /* Connection management. */
374 extern guestfs_h *guestfs_create (void);
375 extern void guestfs_close (guestfs_h *g);
377 /* Error handling. */
378 extern const char *guestfs_last_error (guestfs_h *g);
379 #define LIBGUESTFS_HAVE_LAST_ERRNO 1
380 extern int guestfs_last_errno (guestfs_h *g);
382 #ifndef GUESTFS_TYPEDEF_ERROR_HANDLER_CB
383 #define GUESTFS_TYPEDEF_ERROR_HANDLER_CB 1
384 typedef void (*guestfs_error_handler_cb) (guestfs_h *g, void *opaque, const char *msg);
387 #ifndef GUESTFS_TYPEDEF_ABORT_CB
388 #define GUESTFS_TYPEDEF_ABORT_CB 1
389 typedef void (*guestfs_abort_cb) (void) __attribute__((__noreturn__));
392 extern void guestfs_set_error_handler (guestfs_h *g, guestfs_error_handler_cb cb, void *opaque);
393 extern guestfs_error_handler_cb guestfs_get_error_handler (guestfs_h *g, void **opaque_rtn);
395 extern void guestfs_set_out_of_memory_handler (guestfs_h *g, guestfs_abort_cb);
396 extern guestfs_abort_cb guestfs_get_out_of_memory_handler (guestfs_h *g);
399 #ifndef GUESTFS_TYPEDEF_LOG_MESSAGE_CB
400 #define GUESTFS_TYPEDEF_LOG_MESSAGE_CB 1
401 typedef void (*guestfs_log_message_cb) (guestfs_h *g, void *opaque, char *buf, int len);
404 #ifndef GUESTFS_TYPEDEF_SUBPROCESS_QUIT_CB
405 #define GUESTFS_TYPEDEF_SUBPROCESS_QUIT_CB 1
406 typedef void (*guestfs_subprocess_quit_cb) (guestfs_h *g, void *opaque);
409 #ifndef GUESTFS_TYPEDEF_LAUNCH_DONE_CB
410 #define GUESTFS_TYPEDEF_LAUNCH_DONE_CB 1
411 typedef void (*guestfs_launch_done_cb) (guestfs_h *g, void *opaque);
414 #ifndef GUESTFS_TYPEDEF_CLOSE_CB
415 #define GUESTFS_TYPEDEF_CLOSE_CB 1
416 typedef void (*guestfs_close_cb) (guestfs_h *g, void *opaque);
419 #ifndef GUESTFS_TYPEDEF_PROGRESS_CB
420 #define GUESTFS_TYPEDEF_PROGRESS_CB 1
421 typedef void (*guestfs_progress_cb) (guestfs_h *g, void *opaque, int proc_nr, int serial, uint64_t position, uint64_t total);
424 extern void guestfs_set_log_message_callback (guestfs_h *g, guestfs_log_message_cb cb, void *opaque);
425 extern void guestfs_set_subprocess_quit_callback (guestfs_h *g, guestfs_subprocess_quit_cb cb, void *opaque);
426 extern void guestfs_set_launch_done_callback (guestfs_h *g, guestfs_launch_done_cb cb, void *opaque);
427 #define LIBGUESTFS_HAVE_SET_CLOSE_CALLBACK 1
428 extern void guestfs_set_close_callback (guestfs_h *g, guestfs_close_cb cb, void *opaque);
429 #define LIBGUESTFS_HAVE_SET_PROGRESS_CALLBACK 1
430 extern void guestfs_set_progress_callback (guestfs_h *g, guestfs_progress_cb cb, void *opaque);
432 /* Private data area. */
433 #define LIBGUESTFS_HAVE_SET_PRIVATE 1
434 extern void guestfs_set_private (guestfs_h *g, const char *key, void *data);
435 #define LIBGUESTFS_HAVE_GET_PRIVATE 1
436 extern void *guestfs_get_private (guestfs_h *g, const char *key);
441 (* The structures are carefully written to have exactly the same
442 * in-memory format as the XDR structures that we use on the wire to
443 * the daemon. The reason for creating copies of these structures
444 * here is just so we don't have to export the whole of
445 * guestfs_protocol.h (which includes much unrelated and
446 * XDR-dependent stuff that we don't want to be public, or required
449 * To reiterate, we will pass these structures to and from the client
450 * with a simple assignment or memcpy, so the format must be
451 * identical to what rpcgen / the RFC defines.
454 (* Public structures. *)
457 pr "struct guestfs_%s {\n" typ;
460 | name, FChar -> pr " char %s;\n" name
461 | name, FString -> pr " char *%s;\n" name
463 pr " uint32_t %s_len;\n" name;
464 pr " char *%s;\n" name
465 | name, FUUID -> pr " char %s[32]; /* this is NOT nul-terminated, be careful when printing */\n" name
466 | name, FUInt32 -> pr " uint32_t %s;\n" name
467 | name, FInt32 -> pr " int32_t %s;\n" name
468 | name, (FUInt64|FBytes) -> pr " uint64_t %s;\n" name
469 | name, FInt64 -> pr " int64_t %s;\n" name
470 | name, FOptPercent -> pr " float %s; /* [0..100] or -1 */\n" name
474 pr "struct guestfs_%s_list {\n" typ;
475 pr " uint32_t len;\n";
476 pr " struct guestfs_%s *val;\n" typ;
479 pr "extern void guestfs_free_%s (struct guestfs_%s *);\n" typ typ;
480 pr "extern void guestfs_free_%s_list (struct guestfs_%s_list *);\n" typ typ;
489 fun (shortname, (ret, args, optargs as style), _, flags, _, _, _) ->
491 List.exists (function DeprecatedBy _ -> true | _ -> false) flags in
493 String.length shortname >= 5 && String.sub shortname 0 5 = "test0" in
495 String.length shortname >= 5 && String.sub shortname 0 5 = "debug" in
496 if not deprecated && not test0 && not debug then
497 pr "#define LIBGUESTFS_HAVE_%s 1\n" (String.uppercase shortname);
499 generate_prototype ~single_line:true ~newline:true ~handle:"g"
500 ~prefix:"guestfs_" shortname style;
502 if optargs <> [] then (
503 generate_prototype ~single_line:true ~newline:true ~handle:"g"
504 ~prefix:"guestfs_" ~suffix:"_va" ~optarg_proto:VA
507 pr "struct guestfs_%s_argv {\n" shortname;
508 pr " uint64_t bitmask;\n";
515 | Int64 n -> "int64_t "
516 | String n -> "const char *"
517 | _ -> assert false (* checked in generator_checks *) in
518 let uc_shortname = String.uppercase shortname in
519 let n = name_of_argt argt in
520 let uc_n = String.uppercase n in
521 pr "#define GUESTFS_%s_%s %d\n" uc_shortname uc_n i;
522 pr "#define GUESTFS_%s_%s_BITMASK (UINT64_C(1)<<%d)\n" uc_shortname uc_n i;
523 pr "/* The field below is only valid in this struct if the\n";
524 pr " * GUESTFS_%s_%s_BITMASK bit is set\n" uc_shortname uc_n;
525 pr " * in the bitmask above, otherwise the contents are ignored.\n";
527 pr " %s%s;\n" c_type n
531 generate_prototype ~single_line:true ~newline:true ~handle:"g"
532 ~prefix:"guestfs_" ~suffix:"_argv" ~optarg_proto:Argv
535 ) all_functions_sorted;
539 /* Private functions.
541 * These are NOT part of the public, stable API, and can change at any
542 * time! We export them because they are used by some of the language
545 extern void *guestfs_safe_malloc (guestfs_h *g, size_t nbytes);
546 extern void *guestfs_safe_calloc (guestfs_h *g, size_t n, size_t s);
547 extern const char *guestfs_tmpdir (void);
548 #ifdef GUESTFS_PRIVATE_FOR_EACH_DISK
549 extern int guestfs___for_each_disk (guestfs_h *g, virDomainPtr dom, int (*)(guestfs_h *g, const char *filename, const char *format, void *data), void *data);
551 /* End of private functions. */
557 #endif /* GUESTFS_H_ */
560 (* Generate the guestfs-internal-actions.h file. *)
561 and generate_internal_actions_h () =
562 generate_header CStyle LGPLv2plus;
564 fun (shortname, style, _, _, _, _, _) ->
565 generate_prototype ~single_line:true ~newline:true ~handle:"g"
566 ~prefix:"guestfs__" ~optarg_proto:Argv
568 ) non_daemon_functions
570 (* Generate the client-side dispatch stubs. *)
571 and generate_client_actions () =
572 generate_header CStyle LGPLv2plus;
579 #include <inttypes.h>
581 #include <sys/types.h>
582 #include <sys/stat.h>
584 #include \"guestfs.h\"
585 #include \"guestfs-internal.h\"
586 #include \"guestfs-internal-actions.h\"
587 #include \"guestfs_protocol.h\"
588 #include \"errnostring.h\"
590 /* Check the return message from a call for validity. */
592 check_reply_header (guestfs_h *g,
593 const struct guestfs_message_header *hdr,
594 unsigned int proc_nr, unsigned int serial)
596 if (hdr->prog != GUESTFS_PROGRAM) {
597 error (g, \"wrong program (%%d/%%d)\", hdr->prog, GUESTFS_PROGRAM);
600 if (hdr->vers != GUESTFS_PROTOCOL_VERSION) {
601 error (g, \"wrong protocol version (%%d/%%d)\",
602 hdr->vers, GUESTFS_PROTOCOL_VERSION);
605 if (hdr->direction != GUESTFS_DIRECTION_REPLY) {
606 error (g, \"unexpected message direction (%%d/%%d)\",
607 hdr->direction, GUESTFS_DIRECTION_REPLY);
610 if (hdr->proc != proc_nr) {
611 error (g, \"unexpected procedure number (%%d/%%d)\", hdr->proc, proc_nr);
614 if (hdr->serial != serial) {
615 error (g, \"unexpected serial (%%d/%%d)\", hdr->serial, serial);
622 /* Check we are in the right state to run a high-level action. */
624 check_state (guestfs_h *g, const char *caller)
626 if (!guestfs__is_ready (g)) {
627 if (guestfs__is_config (g) || guestfs__is_launching (g))
628 error (g, \"%%s: call launch before using this function\\n(in guestfish, don't forget to use the 'run' command)\",
631 error (g, \"%%s called from the wrong state, %%d != READY\",
632 caller, guestfs__get_state (g));
640 let error_code_of = function
641 | RErr | RInt _ | RInt64 _ | RBool _ -> "-1"
642 | RConstString _ | RConstOptString _
643 | RString _ | RStringList _
644 | RStruct _ | RStructList _
645 | RHashtable _ | RBufferOut _ -> "NULL"
648 (* Generate code to check String-like parameters are not passed in
649 * as NULL (returning an error if they are).
651 let check_null_strings shortname (ret, args, optargs) =
652 let pr_newline = ref false in
655 (* parameters which should not be NULL *)
667 pr " if (%s == NULL) {\n" n;
668 pr " error (g, \"%%s: %%s: parameter cannot be NULL\",\n";
669 pr " \"%s\", \"%s\");\n" shortname n;
670 pr " return %s;\n" (error_code_of ret);
683 (* For optional arguments. *)
687 pr " if ((optargs->bitmask & GUESTFS_%s_%s_BITMASK) &&\n"
688 (String.uppercase shortname) (String.uppercase n);
689 pr " optargs->%s == NULL) {\n" n;
690 pr " error (g, \"%%s: %%s: optional parameter cannot be NULL\",\n";
691 pr " \"%s\", \"%s\");\n" shortname n;
692 pr " return %s;\n" (error_code_of ret);
697 | Bool _ | Int _ | Int64 _ -> ()
702 if !pr_newline then pr "\n";
705 (* Generate code to reject optargs we don't know about. *)
706 let reject_unknown_optargs shortname = function
709 let len = List.length optargs in
710 let mask = Int64.lognot (Int64.pred (Int64.shift_left 1L len)) in
711 pr " if (optargs->bitmask & UINT64_C(0x%Lx)) {\n" mask;
712 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";
713 pr " \"%s\", \"%s\");\n" shortname shortname;
714 pr " return %s;\n" (error_code_of ret);
719 (* Generate code to generate guestfish call traces. *)
720 let trace_call shortname (ret, args, optargs) =
721 pr " if (trace_flag) {\n";
724 List.exists (function
725 | StringList _ | DeviceList _ -> true
726 | _ -> false) args in
732 pr " fprintf (stderr, \"%%s: %%s: %%s\",\n";
733 pr " \"libguestfs\", \"trace\", \"%s\");\n" shortname;
735 (* Required arguments. *)
738 | String n (* strings *)
744 (* guestfish doesn't support string escaping, so neither do we *)
745 pr " fprintf (stderr, \" \\\"%%s\\\"\", %s);\n" n
747 (* don't print keys *)
748 pr " fprintf (stderr, \" \\\"***\\\"\");\n"
749 | OptString n -> (* string option *)
750 pr " if (%s) fprintf (stderr, \" \\\"%%s\\\"\", %s);\n" n n;
751 pr " else fprintf (stderr, \" null\");\n"
753 | DeviceList n -> (* string list *)
754 pr " fputc (' ', stderr);\n";
755 pr " fputc ('\"', stderr);\n";
756 pr " for (i = 0; %s[i]; ++i) {\n" n;
757 pr " if (i > 0) fputc (' ', stderr);\n";
758 pr " fputs (%s[i], stderr);\n" n;
760 pr " fputc ('\"', stderr);\n";
761 | Bool n -> (* boolean *)
762 pr " fputs (%s ? \" true\" : \" false\", stderr);\n" n
764 pr " fprintf (stderr, \" %%d\", %s);\n" n
766 pr " fprintf (stderr, \" %%\" PRIi64, %s);\n" n
767 | BufferIn n -> (* RHBZ#646822 *)
768 pr " fputc (' ', stderr);\n";
769 pr " guestfs___print_BufferIn (stderr, %s, %s_size);\n" n n
771 pr " fprintf (stderr, \" (%s)%%p\", %s);\n" t n
774 (* Optional arguments. *)
777 let n = name_of_argt argt in
778 let uc_shortname = String.uppercase shortname in
779 let uc_n = String.uppercase n in
780 pr " if (optargs->bitmask & GUESTFS_%s_%s_BITMASK)\n"
784 pr " fprintf (stderr, \" \\\"%%s:%%s\\\"\", \"%s\", optargs->%s);\n" n n
786 pr " fprintf (stderr, \" \\\"%%s:%%s\\\"\", \"%s\", optargs->%s ? \"true\" : \"false\");\n" n n
788 pr " fprintf (stderr, \" \\\"%%s:%%d\\\"\", \"%s\", optargs->%s);\n" n n
790 pr " fprintf (stderr, \" \\\"%%s:%%\" PRIi64 \"\\\"\", \"%s\", optargs->%s);\n" n n
795 pr " fputc ('\\n', stderr);\n";
800 let trace_return ?(indent = 2) shortname (ret, _, _) rv =
801 let indent = spaces indent in
803 pr "%sif (trace_flag) {\n" indent;
807 | RStringList _ | RHashtable _ -> true
810 pr "%s size_t i;\n" indent;
814 pr "%s fprintf (stderr, \"%%s: %%s: %%s = \",\n" indent;
815 pr "%s \"libguestfs\", \"trace\", \"%s\");\n"
819 | RErr | RInt _ | RBool _ ->
820 pr "%s fprintf (stderr, \"%%d\", %s);\n" indent rv
822 pr "%s fprintf (stderr, \"%%\" PRIi64, %s);\n" indent rv
823 | RConstString _ | RString _ ->
824 pr "%s fprintf (stderr, \"\\\"%%s\\\"\", %s);\n" indent rv
825 | RConstOptString _ ->
826 pr "%s fprintf (stderr, \"\\\"%%s\\\"\", %s != NULL ? %s : \"NULL\");\n"
829 pr "%s guestfs___print_BufferOut (stderr, %s, *size_r);\n" indent rv
830 | RStringList _ | RHashtable _ ->
831 pr "%s fputs (\"[\\\"\", stderr);\n" indent;
832 pr "%s for (i = 0; %s[i]; ++i) {\n" indent rv;
833 pr "%s if (i > 0) fputs (\"\\\", \\\"\", stderr);\n" indent;
834 pr "%s fputs (%s[i], stderr);\n" indent rv;
836 pr "%s fputs (\"\\\"]\", stderr);\n" indent;
837 | RStruct (_, typ) ->
838 (* XXX There is code generated for guestfish for printing
839 * these structures. We need to make it generally available
842 pr "%s fprintf (stderr, \"<struct guestfs_%s *>\");\n"
844 | RStructList (_, typ) ->
845 pr "%s fprintf (stderr, \"<struct guestfs_%s_list *>\");\n"
848 pr "%s fputc ('\\n', stderr);\n" indent;
853 let trace_return_error ?(indent = 2) shortname (ret, _, _) =
854 let indent = spaces indent in
856 pr "%sif (trace_flag)\n" indent;
858 pr "%s fprintf (stderr, \"%%s: %%s: %%s = %%s (error)\\n\",\n" indent;
859 pr "%s \"libguestfs\", \"trace\", \"%s\", "
863 | RErr | RInt _ | RBool _
866 | RConstString _ | RString _
869 | RStringList _ | RHashtable _
877 (* For non-daemon functions, generate a wrapper around each function. *)
879 fun (shortname, (ret, _, optargs as style), _, _, _, _, _) ->
881 generate_prototype ~extern:false ~semicolon:false ~newline:true
882 ~handle:"g" ~prefix:"guestfs_"
885 generate_prototype ~extern:false ~semicolon:false ~newline:true
886 ~handle:"g" ~prefix:"guestfs_" ~suffix:"_argv" ~optarg_proto:Argv
889 pr " int trace_flag = g->trace;\n";
891 | RErr | RInt _ | RBool _ ->
895 | RConstString _ | RConstOptString _ ->
896 pr " const char *r;\n"
897 | RString _ | RBufferOut _ ->
899 | RStringList _ | RHashtable _ ->
901 | RStruct (_, typ) ->
902 pr " struct guestfs_%s *r;\n" typ
903 | RStructList (_, typ) ->
904 pr " struct guestfs_%s_list *r;\n" typ
907 check_null_strings shortname style;
908 reject_unknown_optargs shortname style;
909 trace_call shortname style;
910 pr " r = guestfs__%s " shortname;
911 generate_c_call_args ~handle:"g" style;
913 trace_return shortname style "r";
917 ) non_daemon_functions;
919 (* Client-side stubs for each function. *)
921 fun (shortname, (ret, args, optargs as style), _, _, _, _, _) ->
922 let name = "guestfs_" ^ shortname in
923 let error_code = error_code_of ret in
925 (* Generate the action stub. *)
927 generate_prototype ~extern:false ~semicolon:false ~newline:true
928 ~handle:"g" ~prefix:"guestfs_" shortname style
930 generate_prototype ~extern:false ~semicolon:false ~newline:true
931 ~handle:"g" ~prefix:"guestfs_" ~suffix:"_argv"
932 ~optarg_proto:Argv shortname style;
938 | _ -> pr " struct %s_args args;\n" name
941 pr " guestfs_message_header hdr;\n";
942 pr " guestfs_message_error err;\n";
946 | RConstString _ | RConstOptString _ ->
947 failwithf "RConstString|RConstOptString cannot be used by daemon functions"
949 | RBool _ | RString _ | RStringList _
950 | RStruct _ | RStructList _
951 | RHashtable _ | RBufferOut _ ->
952 pr " struct %s_ret ret;\n" name;
957 pr " int trace_flag = g->trace;\n";
959 | RErr | RInt _ | RBool _ ->
962 pr " int64_t ret_v;\n"
963 | RConstString _ | RConstOptString _ ->
964 pr " const char *ret_v;\n"
965 | RString _ | RBufferOut _ ->
967 | RStringList _ | RHashtable _ ->
968 pr " char **ret_v;\n"
969 | RStruct (_, typ) ->
970 pr " struct guestfs_%s *ret_v;\n" typ
971 | RStructList (_, typ) ->
972 pr " struct guestfs_%s_list *ret_v;\n" typ
976 List.exists (function FileIn _ -> true | _ -> false) args in
978 pr " uint64_t progress_hint = 0;\n";
979 pr " struct stat progress_stat;\n";
981 pr " const uint64_t progress_hint = 0;\n";
984 check_null_strings shortname style;
985 reject_unknown_optargs shortname style;
986 trace_call shortname style;
988 (* Calculate the total size of all FileIn arguments to pass
989 * as a progress bar hint.
994 pr " if (stat (%s, &progress_stat) == 0 &&\n" n;
995 pr " S_ISREG (progress_stat.st_mode))\n";
996 pr " progress_hint += progress_stat.st_size;\n";
1001 (* Check we are in the right state for sending a request. *)
1002 pr " if (check_state (g, \"%s\") == -1) {\n" shortname;
1003 trace_return_error ~indent:4 shortname style;
1004 pr " return %s;\n" error_code;
1006 pr " guestfs___set_busy (g);\n";
1009 (* Send the main header and arguments. *)
1010 if args = [] && optargs = [] then (
1011 pr " serial = guestfs___send (g, GUESTFS_PROC_%s, progress_hint, 0,\n"
1012 (String.uppercase shortname);
1013 pr " NULL, NULL);\n"
1017 | Pathname n | Device n | Dev_or_Path n | String n | Key n ->
1018 pr " args.%s = (char *) %s;\n" n n
1020 pr " args.%s = %s ? (char **) &%s : NULL;\n" n n n
1021 | StringList n | DeviceList n ->
1022 pr " args.%s.%s_val = (char **) %s;\n" n n n;
1023 pr " for (args.%s.%s_len = 0; %s[args.%s.%s_len]; args.%s.%s_len++) ;\n" n n n n n n n;
1025 pr " args.%s = %s;\n" n n
1027 pr " args.%s = %s;\n" n n
1029 pr " args.%s = %s;\n" n n
1030 | FileIn _ | FileOut _ -> ()
1032 pr " /* Just catch grossly large sizes. XDR encoding will make this precise. */\n";
1033 pr " if (%s_size >= GUESTFS_MESSAGE_MAX) {\n" n;
1034 trace_return_error ~indent:4 shortname style;
1035 pr " error (g, \"%%s: size of input buffer too large\", \"%s\");\n"
1037 pr " guestfs___end_busy (g);\n";
1038 pr " return %s;\n" error_code;
1040 pr " args.%s.%s_val = (char *) %s;\n" n n n;
1041 pr " args.%s.%s_len = %s_size;\n" n n n
1042 | Pointer _ -> assert false
1047 let n = name_of_argt argt in
1048 let uc_shortname = String.uppercase shortname in
1049 let uc_n = String.uppercase n in
1050 pr " if ((optargs->bitmask & GUESTFS_%s_%s_BITMASK))\n"
1056 pr " args.%s = optargs->%s;\n" n n;
1058 pr " args.%s = 0;\n" n
1060 pr " args.%s = (char *) optargs->%s;\n" n n;
1062 pr " args.%s = (char *) \"\";\n" n
1067 pr " serial = guestfs___send (g, GUESTFS_PROC_%s,\n"
1068 (String.uppercase shortname);
1069 pr " progress_hint, %s,\n"
1070 (if optargs <> [] then "optargs->bitmask" else "0");
1071 pr " (xdrproc_t) xdr_%s_args, (char *) &args);\n"
1074 pr " if (serial == -1) {\n";
1075 pr " guestfs___end_busy (g);\n";
1076 trace_return_error ~indent:4 shortname style;
1077 pr " return %s;\n" error_code;
1081 (* Send any additional files (FileIn) requested. *)
1082 let need_read_reply_label = ref false in
1086 pr " r = guestfs___send_file (g, %s);\n" n;
1087 pr " if (r == -1) {\n";
1088 pr " guestfs___end_busy (g);\n";
1089 trace_return_error ~indent:4 shortname style;
1090 pr " return %s;\n" error_code;
1092 pr " if (r == -2) /* daemon cancelled */\n";
1093 pr " goto read_reply;\n";
1094 need_read_reply_label := true;
1099 (* Wait for the reply from the remote end. *)
1100 if !need_read_reply_label then pr " read_reply:\n";
1101 pr " memset (&hdr, 0, sizeof hdr);\n";
1102 pr " memset (&err, 0, sizeof err);\n";
1103 if has_ret then pr " memset (&ret, 0, sizeof ret);\n";
1105 pr " r = guestfs___recv (g, \"%s\", &hdr, &err,\n " shortname;
1109 pr "(xdrproc_t) xdr_guestfs_%s_ret, (char *) &ret" shortname;
1112 pr " if (r == -1) {\n";
1113 pr " guestfs___end_busy (g);\n";
1114 trace_return_error ~indent:4 shortname style;
1115 pr " return %s;\n" error_code;
1119 pr " if (check_reply_header (g, &hdr, GUESTFS_PROC_%s, serial) == -1) {\n"
1120 (String.uppercase shortname);
1121 pr " guestfs___end_busy (g);\n";
1122 trace_return_error ~indent:4 shortname style;
1123 pr " return %s;\n" error_code;
1127 pr " if (hdr.status == GUESTFS_STATUS_ERROR) {\n";
1128 trace_return_error ~indent:4 shortname style;
1129 pr " int errnum = 0;\n";
1130 pr " if (err.errno_string[0] != '\\0')\n";
1131 pr " errnum = guestfs___string_to_errno (err.errno_string);\n";
1132 pr " if (errnum <= 0)\n";
1133 pr " error (g, \"%%s: %%s\", \"%s\", err.error_message);\n"
1136 pr " guestfs_error_errno (g, errnum, \"%%s: %%s\", \"%s\",\n"
1138 pr " err.error_message);\n";
1139 pr " free (err.error_message);\n";
1140 pr " free (err.errno_string);\n";
1141 pr " guestfs___end_busy (g);\n";
1142 pr " return %s;\n" error_code;
1146 (* Expecting to receive further files (FileOut)? *)
1150 pr " if (guestfs___recv_file (g, %s) == -1) {\n" n;
1151 pr " guestfs___end_busy (g);\n";
1152 trace_return_error ~indent:4 shortname style;
1153 pr " return %s;\n" error_code;
1159 pr " guestfs___end_busy (g);\n";
1164 | RInt n | RInt64 n | RBool n ->
1165 pr " ret_v = ret.%s;\n" n
1166 | RConstString _ | RConstOptString _ ->
1167 failwithf "RConstString|RConstOptString cannot be used by daemon functions"
1169 pr " ret_v = ret.%s; /* caller will free */\n" n
1170 | RStringList n | RHashtable n ->
1171 pr " /* caller will free this, but we need to add a NULL entry */\n";
1172 pr " ret.%s.%s_val =\n" n n;
1173 pr " safe_realloc (g, ret.%s.%s_val,\n" n n;
1174 pr " sizeof (char *) * (ret.%s.%s_len + 1));\n"
1176 pr " ret.%s.%s_val[ret.%s.%s_len] = NULL;\n" n n n n;
1177 pr " ret_v = ret.%s.%s_val;\n" n n
1179 pr " /* caller will free this */\n";
1180 pr " ret_v = safe_memdup (g, &ret.%s, sizeof (ret.%s));\n" n n
1181 | RStructList (n, _) ->
1182 pr " /* caller will free this */\n";
1183 pr " ret_v = safe_memdup (g, &ret.%s, sizeof (ret.%s));\n" n n
1185 pr " /* RBufferOut is tricky: If the buffer is zero-length, then\n";
1186 pr " * _val might be NULL here. To make the API saner for\n";
1187 pr " * callers, we turn this case into a unique pointer (using\n";
1188 pr " * malloc(1)).\n";
1190 pr " if (ret.%s.%s_len > 0) {\n" n n;
1191 pr " *size_r = ret.%s.%s_len;\n" n n;
1192 pr " ret_v = ret.%s.%s_val; /* caller will free */\n" n n;
1194 pr " free (ret.%s.%s_val);\n" n n;
1195 pr " char *p = safe_malloc (g, 1);\n";
1196 pr " *size_r = ret.%s.%s_len;\n" n n;
1200 trace_return shortname style "ret_v";
1201 pr " return ret_v;\n";
1205 (* Functions to free structures. *)
1206 pr "/* Structure-freeing functions. These rely on the fact that the\n";
1207 pr " * structure format is identical to the XDR format. See note in\n";
1208 pr " * generator.ml.\n";
1215 pr "guestfs_free_%s (struct guestfs_%s *x)\n" typ typ;
1217 pr " xdr_free ((xdrproc_t) xdr_guestfs_int_%s, (char *) x);\n" typ;
1223 pr "guestfs_free_%s_list (struct guestfs_%s_list *x)\n" typ typ;
1225 pr " xdr_free ((xdrproc_t) xdr_guestfs_int_%s_list, (char *) x);\n" typ;
1232 (* Functions which have optional arguments have two generated variants. *)
1235 | shortname, (ret, args, (_::_ as optargs) as style), _, _, _, _, _ ->
1236 let uc_shortname = String.uppercase shortname in
1238 (* Get the name of the last regular argument. *)
1242 | args -> name_of_argt (List.hd (List.rev args)) in
1244 let rerrcode, rtype =
1246 | RErr | RInt _ | RBool _ -> "-1", "int "
1247 | RInt64 _ -> "-1", "int64_t "
1248 | RConstString _ | RConstOptString _ -> "NULL", "const char *"
1249 | RString _ | RBufferOut _ -> "NULL", "char *"
1250 | RStringList _ | RHashtable _ -> "NULL", "char **"
1251 | RStruct (_, typ) -> "NULL", sprintf "struct guestfs_%s *" typ
1252 | RStructList (_, typ) ->
1253 "NULL", sprintf "struct guestfs_%s_list *" typ in
1255 (* The regular variable args function, just calls the _va variant. *)
1256 generate_prototype ~extern:false ~semicolon:false ~newline:true
1257 ~handle:"g" ~prefix:"guestfs_" shortname style;
1259 pr " va_list optargs;\n";
1261 pr " va_start (optargs, %s);\n" last_arg;
1262 pr " %sr = guestfs_%s_va " rtype shortname;
1263 generate_c_call_args ~handle:"g" style;
1265 pr " va_end (optargs);\n";
1270 generate_prototype ~extern:false ~semicolon:false ~newline:true
1271 ~handle:"g" ~prefix:"guestfs_" ~suffix:"_va" ~optarg_proto:VA
1274 pr " struct guestfs_%s_argv optargs_s;\n" shortname;
1275 pr " struct guestfs_%s_argv *optargs = &optargs_s;\n" shortname;
1278 pr " optargs_s.bitmask = 0;\n";
1280 pr " while ((i = va_arg (args, int)) >= 0) {\n";
1281 pr " switch (i) {\n";
1285 let n = name_of_argt argt in
1286 let uc_n = String.uppercase n in
1287 pr " case GUESTFS_%s_%s:\n" uc_shortname uc_n;
1288 pr " optargs_s.%s = va_arg (args, " n;
1290 | Bool _ | Int _ -> pr "int"
1291 | Int64 _ -> pr "int64_t"
1292 | String _ -> pr "const char *"
1300 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";
1301 pr " \"%s\", i);\n" shortname;
1302 pr " return %s;\n" rerrcode;
1305 pr " uint64_t i_mask = UINT64_C(1) << i;\n";
1306 pr " if (optargs_s.bitmask & i_mask) {\n";
1307 pr " error (g, \"%%s: same optional argument specified more than once\",\n";
1308 pr " \"%s\");\n" shortname;
1309 pr " return %s;\n" rerrcode;
1311 pr " optargs_s.bitmask |= i_mask;\n";
1314 pr " return guestfs_%s_argv " shortname;
1315 generate_c_call_args ~handle:"g" style;
1319 ) all_functions_sorted
1321 (* Generate the linker script which controls the visibility of
1322 * symbols in the public ABI and ensures no other symbols get
1323 * exported accidentally.
1325 and generate_linker_script () =
1326 generate_header HashStyle GPLv2plus;
1331 "guestfs_get_error_handler";
1332 "guestfs_get_out_of_memory_handler";
1333 "guestfs_get_private";
1334 "guestfs_last_errno";
1335 "guestfs_last_error";
1336 "guestfs_set_close_callback";
1337 "guestfs_set_error_handler";
1338 "guestfs_set_launch_done_callback";
1339 "guestfs_set_log_message_callback";
1340 "guestfs_set_out_of_memory_handler";
1341 "guestfs_set_private";
1342 "guestfs_set_progress_callback";
1343 "guestfs_set_subprocess_quit_callback";
1345 (* Unofficial parts of the API: the bindings code use these
1346 * functions, so it is useful to export them.
1348 "guestfs_safe_calloc";
1349 "guestfs_safe_malloc";
1350 "guestfs_safe_strdup";
1351 "guestfs_safe_memdup";
1353 "guestfs___for_each_disk";
1359 | name, (_, _, []), _, _, _, _, _ -> ["guestfs_" ^ name]
1360 | name, (_, _, _), _, _, _, _, _ ->
1362 "guestfs_" ^ name ^ "_va";
1363 "guestfs_" ^ name ^ "_argv"]
1368 List.map (fun (typ, _) ->
1369 ["guestfs_free_" ^ typ; "guestfs_free_" ^ typ ^ "_list"])
1372 let globals = List.sort compare (globals @ functions @ structs) in
1376 List.iter (pr " %s;\n") globals;
1383 and generate_max_proc_nr () =
1384 pr "%d\n" max_proc_nr