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
35 type optarg_proto = Dots | VA | Argv
37 (* Generate a C function prototype. *)
38 let rec generate_prototype ?(extern = true) ?(static = false)
40 ?(single_line = false) ?(indent = "") ?(newline = false)
42 ?(prefix = "") ?(suffix = "")
44 ?(optarg_proto = Dots)
45 name (ret, args, optargs) =
47 if extern then pr "extern ";
48 if static then pr "static ";
54 if single_line then pr " " else pr "\n%s" indent
57 if single_line then pr " " else pr "\n%s" indent
58 | RConstString _ | RConstOptString _ ->
60 if not single_line then pr "\n%s" indent
61 | RString _ | RBufferOut _ ->
63 if not single_line then pr "\n%s" indent
64 | RStringList _ | RHashtable _ ->
66 if not single_line then pr "\n%s" indent
68 if not in_daemon then pr "struct guestfs_%s *" typ
69 else pr "guestfs_int_%s *" typ;
70 if not single_line then pr "\n%s" indent
71 | RStructList (_, typ) ->
72 if not in_daemon then pr "struct guestfs_%s_list *" typ
73 else pr "guestfs_int_%s_list *" typ;
74 if not single_line then pr "\n%s" indent
76 let is_RBufferOut = match ret with RBufferOut _ -> true | _ -> false in
77 pr "%s%s%s (" prefix name suffix;
78 if handle = None && args = [] && optargs = [] && not is_RBufferOut then
81 let comma = ref false in
84 | Some handle -> pr "guestfs_h *%s" handle; comma := true
88 if single_line then pr ", "
90 let namelen = String.length prefix + String.length name +
91 String.length suffix + 2 in
92 pr ",\n%s%s" indent (spaces namelen)
100 | Device n | Dev_or_Path n
105 pr "const char *%s" n
106 | StringList n | DeviceList n ->
108 pr "char *const *%s" n
109 | Bool n -> next (); pr "int %s" n
110 | Int n -> next (); pr "int %s" n
111 | Int64 n -> next (); pr "int64_t %s" n
114 if not in_daemon then (next (); pr "const char *%s" n)
117 pr "const char *%s" n;
119 pr "size_t %s_size" n
124 if is_RBufferOut then (next (); pr "size_t *size_r");
125 if optargs <> [] then (
127 match optarg_proto with
129 | VA -> pr "va_list args"
130 | Argv -> pr "const struct guestfs_%s_argv *optargs" name
134 if semicolon then pr ";";
135 if newline then pr "\n"
137 (* Generate C call arguments, eg "(handle, foo, bar)" *)
138 and generate_c_call_args ?handle (ret, args, optargs) =
140 let comma = ref false in
142 if !comma then pr ", ";
147 | Some handle -> pr "%s" handle; comma := true
156 pr "%s" (name_of_argt arg)
158 (* For RBufferOut calls, add implicit &size parameter. *)
165 (* For calls with optional arguments, add implicit optargs parameter. *)
166 if optargs <> [] then (
172 (* Generate the pod documentation for the C API. *)
173 and generate_actions_pod () =
175 fun (shortname, (ret, args, optargs as style), _, flags, _, _, longdesc) ->
176 if not (List.mem NotInDocs flags) then (
177 let name = "guestfs_" ^ shortname in
178 pr "=head2 %s\n\n" name;
179 generate_prototype ~extern:false ~indent:" " ~handle:"g" name style;
182 let uc_shortname = String.uppercase shortname in
183 if optargs <> [] then (
184 pr "You may supply a list of optional arguments to this call.\n";
185 pr "Use zero or more of the following pairs of parameters,\n";
186 pr "and terminate the list with C<-1> on its own.\n";
187 pr "See L</CALLS WITH OPTIONAL ARGUMENTS>.\n\n";
190 let n = name_of_argt argt in
191 let uc_n = String.uppercase n in
192 pr " GUESTFS_%s_%s, " uc_shortname uc_n;
194 | Bool n -> pr "int %s,\n" n
195 | Int n -> pr "int %s,\n" n
196 | Int64 n -> pr "int64_t %s,\n" n
197 | String n -> pr "const char *%s,\n" n
203 pr "%s\n\n" longdesc;
204 let ret, args, optargs = style in
207 pr "This function returns 0 on success or -1 on error.\n\n"
209 pr "On error this function returns -1.\n\n"
211 pr "On error this function returns -1.\n\n"
213 pr "This function returns a C truth value on success or -1 on error.\n\n"
215 pr "This function returns a string, or NULL on error.
216 The string is owned by the guest handle and must I<not> be freed.\n\n"
217 | RConstOptString _ ->
218 pr "This function returns a string which may be NULL.
219 There is no way to return an error from this function.
220 The string is owned by the guest handle and must I<not> be freed.\n\n"
222 pr "This function returns a string, or NULL on error.
223 I<The caller must free the returned string after use>.\n\n"
225 pr "This function returns a NULL-terminated array of strings
226 (like L<environ(3)>), or NULL if there was an error.
227 I<The caller must free the strings and the array after use>.\n\n"
228 | RStruct (_, typ) ->
229 pr "This function returns a C<struct guestfs_%s *>,
230 or NULL if there was an error.
231 I<The caller must call C<guestfs_free_%s> after use>.\n\n" typ typ
232 | RStructList (_, typ) ->
233 pr "This function returns a C<struct guestfs_%s_list *>,
234 or NULL if there was an error.
235 I<The caller must call C<guestfs_free_%s_list> after use>.\n\n" typ typ
237 pr "This function returns a NULL-terminated array of
238 strings, or NULL if there was an error.
239 The array of strings will always have length C<2n+1>, where
240 C<n> keys and values alternate, followed by the trailing NULL entry.
241 I<The caller must free the strings and the array after use>.\n\n"
243 pr "This function returns a buffer, or NULL on error.
244 The size of the returned buffer is written to C<*size_r>.
245 I<The caller must free the returned buffer after use>.\n\n"
247 if List.mem Progress flags then
248 pr "%s\n\n" progress_message;
249 if List.mem ProtocolLimitWarning flags then
250 pr "%s\n\n" protocol_limit_warning;
251 if List.mem DangerWillRobinson flags then
252 pr "%s\n\n" danger_will_robinson;
253 if List.exists (function Key _ -> true | _ -> false) (args@optargs) then
254 pr "This function takes a key or passphrase parameter which
255 could contain sensitive material. Read the section
256 L</KEYS AND PASSPHRASES> for more information.\n\n";
257 (match deprecation_notice flags with
259 | Some txt -> pr "%s\n\n" txt
261 (match lookup_api_version name with
262 | Some version -> pr "(Added in %s)\n\n" version
266 (* Handling of optional argument variants. *)
267 if optargs <> [] then (
268 pr "=head2 %s_va\n\n" name;
269 generate_prototype ~extern:false ~indent:" " ~handle:"g"
270 ~prefix:"guestfs_" ~suffix:"_va" ~optarg_proto:VA
273 pr "This is the \"va_list variant\" of L</%s>.\n\n" name;
274 pr "See L</CALLS WITH OPTIONAL ARGUMENTS>.\n\n";
275 pr "=head2 %s_argv\n\n" name;
276 generate_prototype ~extern:false ~indent:" " ~handle:"g"
277 ~prefix:"guestfs_" ~suffix:"_argv" ~optarg_proto:Argv
280 pr "This is the \"argv variant\" of L</%s>.\n\n" name;
281 pr "See L</CALLS WITH OPTIONAL ARGUMENTS>.\n\n";
284 ) all_functions_sorted
286 and generate_structs_pod () =
287 (* Structs documentation. *)
290 pr "=head2 guestfs_%s\n" typ;
292 pr " struct guestfs_%s {\n" typ;
295 | name, FChar -> pr " char %s;\n" name
296 | name, FUInt32 -> pr " uint32_t %s;\n" name
297 | name, FInt32 -> pr " int32_t %s;\n" name
298 | name, (FUInt64|FBytes) -> pr " uint64_t %s;\n" name
299 | name, FInt64 -> pr " int64_t %s;\n" name
300 | name, FString -> pr " char *%s;\n" name
302 pr " /* The next two fields describe a byte array. */\n";
303 pr " uint32_t %s_len;\n" name;
304 pr " char *%s;\n" name
306 pr " /* The next field is NOT nul-terminated, be careful when printing it: */\n";
307 pr " char %s[32];\n" name
308 | name, FOptPercent ->
309 pr " /* The next field is [0..100] or -1 meaning 'not present': */\n";
310 pr " float %s;\n" name
314 pr " struct guestfs_%s_list {\n" typ;
315 pr " uint32_t len; /* Number of elements in list. */\n";
316 pr " struct guestfs_%s *val; /* Elements. */\n" typ;
319 pr " void guestfs_free_%s (struct guestfs_free_%s *);\n" typ typ;
320 pr " void guestfs_free_%s_list (struct guestfs_free_%s_list *);\n"
325 and generate_availability_pod () =
326 (* Availability documentation. *)
330 fun (group, functions) ->
331 pr "=item B<%s>\n" group;
333 pr "The following functions:\n";
334 List.iter (pr "L</guestfs_%s>\n") functions;
340 (* Generate the guestfs.h file. *)
341 and generate_guestfs_h () =
342 generate_header CStyle LGPLv2plus;
345 /* ---------- IMPORTANT NOTE ----------
347 * All API documentation is in the manpage, 'guestfs(3)'.
348 * To read it, type: man 3 guestfs
349 * Or read it online here: http://libguestfs.org/guestfs.3.html
351 * Go and read it now, I'll be right here waiting for you
352 * when you come back.
354 * ------------------------------------
369 # define GUESTFS_GCC_VERSION \\
370 (__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + __GNUC_PATCHLEVEL__)
373 /* Define GUESTFS_WARN_DEPRECATED=1 to warn about deprecated API functions. */
374 #define GUESTFS_DEPRECATED_BY(s)
375 #if GUESTFS_WARN_DEPRECATED
376 # if defined(__GNUC__) && GUESTFS_GCC_VERSION >= 30100 /* gcc >= 3.1.0 */
377 # undef GUESTFS_DEPRECATED_BY
378 # define GUESTFS_DEPRECATED_BY(s) __attribute__((__deprecated__(\"change the program to use guestfs_\" s \" instead of this deprecated function\")))
380 #endif /* GUESTFS_WARN_DEPRECATED */
383 #ifndef GUESTFS_TYPEDEF_H
384 #define GUESTFS_TYPEDEF_H 1
385 typedef struct guestfs_h guestfs_h;
388 /* Connection management. */
389 extern guestfs_h *guestfs_create (void);
390 extern void guestfs_close (guestfs_h *g);
392 /* Error handling. */
393 extern const char *guestfs_last_error (guestfs_h *g);
394 #define LIBGUESTFS_HAVE_LAST_ERRNO 1
395 extern int guestfs_last_errno (guestfs_h *g);
397 #ifndef GUESTFS_TYPEDEF_ERROR_HANDLER_CB
398 #define GUESTFS_TYPEDEF_ERROR_HANDLER_CB 1
399 typedef void (*guestfs_error_handler_cb) (guestfs_h *g, void *opaque, const char *msg);
402 #ifndef GUESTFS_TYPEDEF_ABORT_CB
403 #define GUESTFS_TYPEDEF_ABORT_CB 1
404 typedef void (*guestfs_abort_cb) (void) __attribute__((__noreturn__));
407 extern void guestfs_set_error_handler (guestfs_h *g, guestfs_error_handler_cb cb, void *opaque);
408 extern guestfs_error_handler_cb guestfs_get_error_handler (guestfs_h *g, void **opaque_rtn);
410 extern void guestfs_set_out_of_memory_handler (guestfs_h *g, guestfs_abort_cb);
411 extern guestfs_abort_cb guestfs_get_out_of_memory_handler (guestfs_h *g);
417 fun (name, bitmask) ->
418 pr "#define GUESTFS_EVENT_%-16s 0x%04x\n"
419 (String.uppercase name) bitmask
421 pr "#define GUESTFS_EVENT_%-16s UINT64_MAX\n" "ALL";
425 #ifndef GUESTFS_TYPEDEF_EVENT_CALLBACK
426 #define GUESTFS_TYPEDEF_EVENT_CALLBACK 1
427 typedef void (*guestfs_event_callback) (
433 const char *buf, size_t buf_len,
434 const uint64_t *array, size_t array_len);
437 #define LIBGUESTFS_HAVE_SET_EVENT_CALLBACK 1
438 int guestfs_set_event_callback (guestfs_h *g,
439 guestfs_event_callback cb,
440 uint64_t event_bitmask,
443 #define LIBGUESTFS_HAVE_DELETE_EVENT_CALLBACK 1
444 void guestfs_delete_event_callback (guestfs_h *g, int event_handle);
446 /* Old-style event handling. */
447 #ifndef GUESTFS_TYPEDEF_LOG_MESSAGE_CB
448 #define GUESTFS_TYPEDEF_LOG_MESSAGE_CB 1
449 typedef void (*guestfs_log_message_cb) (guestfs_h *g, void *opaque, char *buf, int len);
452 #ifndef GUESTFS_TYPEDEF_SUBPROCESS_QUIT_CB
453 #define GUESTFS_TYPEDEF_SUBPROCESS_QUIT_CB 1
454 typedef void (*guestfs_subprocess_quit_cb) (guestfs_h *g, void *opaque);
457 #ifndef GUESTFS_TYPEDEF_LAUNCH_DONE_CB
458 #define GUESTFS_TYPEDEF_LAUNCH_DONE_CB 1
459 typedef void (*guestfs_launch_done_cb) (guestfs_h *g, void *opaque);
462 #ifndef GUESTFS_TYPEDEF_CLOSE_CB
463 #define GUESTFS_TYPEDEF_CLOSE_CB 1
464 typedef void (*guestfs_close_cb) (guestfs_h *g, void *opaque);
467 #ifndef GUESTFS_TYPEDEF_PROGRESS_CB
468 #define GUESTFS_TYPEDEF_PROGRESS_CB 1
469 typedef void (*guestfs_progress_cb) (guestfs_h *g, void *opaque, int proc_nr, int serial, uint64_t position, uint64_t total);
472 extern void guestfs_set_log_message_callback (guestfs_h *g, guestfs_log_message_cb cb, void *opaque)
473 GUESTFS_DEPRECATED_BY(\"set_event_callback\");
474 extern void guestfs_set_subprocess_quit_callback (guestfs_h *g, guestfs_subprocess_quit_cb cb, void *opaque)
475 GUESTFS_DEPRECATED_BY(\"set_event_callback\");
476 extern void guestfs_set_launch_done_callback (guestfs_h *g, guestfs_launch_done_cb cb, void *opaque)
477 GUESTFS_DEPRECATED_BY(\"set_event_callback\");
478 #define LIBGUESTFS_HAVE_SET_CLOSE_CALLBACK 1
479 extern void guestfs_set_close_callback (guestfs_h *g, guestfs_close_cb cb, void *opaque)
480 GUESTFS_DEPRECATED_BY(\"set_event_callback\");
481 #define LIBGUESTFS_HAVE_SET_PROGRESS_CALLBACK 1
482 extern void guestfs_set_progress_callback (guestfs_h *g, guestfs_progress_cb cb, void *opaque)
483 GUESTFS_DEPRECATED_BY(\"set_event_callback\");
485 /* Private data area. */
486 #define LIBGUESTFS_HAVE_SET_PRIVATE 1
487 extern void guestfs_set_private (guestfs_h *g, const char *key, void *data);
488 #define LIBGUESTFS_HAVE_GET_PRIVATE 1
489 extern void *guestfs_get_private (guestfs_h *g, const char *key);
490 #define LIBGUESTFS_HAVE_FIRST_PRIVATE 1
491 extern void *guestfs_first_private (guestfs_h *g, const char **key_rtn);
492 #define LIBGUESTFS_HAVE_NEXT_PRIVATE 1
493 extern void *guestfs_next_private (guestfs_h *g, const char **key_rtn);
498 (* The structures are carefully written to have exactly the same
499 * in-memory format as the XDR structures that we use on the wire to
500 * the daemon. The reason for creating copies of these structures
501 * here is just so we don't have to export the whole of
502 * guestfs_protocol.h (which includes much unrelated and
503 * XDR-dependent stuff that we don't want to be public, or required
506 * To reiterate, we will pass these structures to and from the client
507 * with a simple assignment or memcpy, so the format must be
508 * identical to what rpcgen / the RFC defines.
511 (* Public structures. *)
514 pr "struct guestfs_%s {\n" typ;
517 | name, FChar -> pr " char %s;\n" name
518 | name, FString -> pr " char *%s;\n" name
520 pr " uint32_t %s_len;\n" name;
521 pr " char *%s;\n" name
522 | name, FUUID -> pr " char %s[32]; /* this is NOT nul-terminated, be careful when printing */\n" name
523 | name, FUInt32 -> pr " uint32_t %s;\n" name
524 | name, FInt32 -> pr " int32_t %s;\n" name
525 | name, (FUInt64|FBytes) -> pr " uint64_t %s;\n" name
526 | name, FInt64 -> pr " int64_t %s;\n" name
527 | name, FOptPercent -> pr " float %s; /* [0..100] or -1 */\n" name
531 pr "struct guestfs_%s_list {\n" typ;
532 pr " uint32_t len;\n";
533 pr " struct guestfs_%s *val;\n" typ;
536 pr "extern void guestfs_free_%s (struct guestfs_%s *);\n" typ typ;
537 pr "extern void guestfs_free_%s_list (struct guestfs_%s_list *);\n" typ typ;
546 fun (shortname, (ret, args, optargs as style), _, flags, _, _, _) ->
549 Some (find_map (function DeprecatedBy fn -> Some fn | _ -> None)
551 with Not_found -> None in
553 String.length shortname >= 5 && String.sub shortname 0 5 = "test0" in
555 String.length shortname >= 5 && String.sub shortname 0 5 = "debug" in
556 if deprecated = None && not test0 && not debug then
557 pr "#define LIBGUESTFS_HAVE_%s 1\n" (String.uppercase shortname);
559 generate_prototype ~single_line:true ~semicolon:false
560 ~handle:"g" ~prefix:"guestfs_" shortname style;
561 (match deprecated with
562 | Some fn -> pr "\n GUESTFS_DEPRECATED_BY (%S);\n" fn
566 if optargs <> [] then (
567 generate_prototype ~single_line:true ~newline:true ~handle:"g"
568 ~prefix:"guestfs_" ~suffix:"_va" ~optarg_proto:VA
571 pr "struct guestfs_%s_argv {\n" shortname;
572 pr " uint64_t bitmask;\n";
579 | Int64 n -> "int64_t "
580 | String n -> "const char *"
581 | _ -> assert false (* checked in generator_checks *) in
582 let uc_shortname = String.uppercase shortname in
583 let n = name_of_argt argt in
584 let uc_n = String.uppercase n in
585 pr "#define GUESTFS_%s_%s %d\n" uc_shortname uc_n i;
586 pr "#define GUESTFS_%s_%s_BITMASK (UINT64_C(1)<<%d)\n" uc_shortname uc_n i;
587 pr "/* The field below is only valid in this struct if the\n";
588 pr " * GUESTFS_%s_%s_BITMASK bit is set\n" uc_shortname uc_n;
589 pr " * in the bitmask above, otherwise the contents are ignored.\n";
591 pr " %s%s;\n" c_type n
595 generate_prototype ~single_line:true ~newline:true ~handle:"g"
596 ~prefix:"guestfs_" ~suffix:"_argv" ~optarg_proto:Argv
601 ) all_functions_sorted;
605 /* Private functions.
607 * These are NOT part of the public, stable API, and can change at any
608 * time! We export them because they are used by some of the language
611 extern void *guestfs_safe_malloc (guestfs_h *g, size_t nbytes);
612 extern void *guestfs_safe_calloc (guestfs_h *g, size_t n, size_t s);
613 extern const char *guestfs_tmpdir (void);
614 #ifdef GUESTFS_PRIVATE_FOR_EACH_DISK
615 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);
617 /* End of private functions. */
623 #endif /* GUESTFS_H_ */
626 (* Generate the guestfs-internal-actions.h file. *)
627 and generate_internal_actions_h () =
628 generate_header CStyle LGPLv2plus;
630 fun (shortname, style, _, _, _, _, _) ->
631 generate_prototype ~single_line:true ~newline:true ~handle:"g"
632 ~prefix:"guestfs__" ~optarg_proto:Argv
634 ) non_daemon_functions
636 (* Generate the client-side dispatch stubs. *)
637 and generate_client_actions () =
638 generate_header CStyle LGPLv2plus;
645 #include <inttypes.h>
647 #include <sys/types.h>
648 #include <sys/stat.h>
651 #include \"guestfs.h\"
652 #include \"guestfs-internal.h\"
653 #include \"guestfs-internal-actions.h\"
654 #include \"guestfs_protocol.h\"
655 #include \"errnostring.h\"
657 /* Check the return message from a call for validity. */
659 check_reply_header (guestfs_h *g,
660 const struct guestfs_message_header *hdr,
661 unsigned int proc_nr, unsigned int serial)
663 if (hdr->prog != GUESTFS_PROGRAM) {
664 error (g, \"wrong program (%%d/%%d)\", hdr->prog, GUESTFS_PROGRAM);
667 if (hdr->vers != GUESTFS_PROTOCOL_VERSION) {
668 error (g, \"wrong protocol version (%%d/%%d)\",
669 hdr->vers, GUESTFS_PROTOCOL_VERSION);
672 if (hdr->direction != GUESTFS_DIRECTION_REPLY) {
673 error (g, \"unexpected message direction (%%d/%%d)\",
674 hdr->direction, GUESTFS_DIRECTION_REPLY);
677 if (hdr->proc != proc_nr) {
678 error (g, \"unexpected procedure number (%%d/%%d)\", hdr->proc, proc_nr);
681 if (hdr->serial != serial) {
682 error (g, \"unexpected serial (%%d/%%d)\", hdr->serial, serial);
689 /* Check we are in the right state to run a high-level action. */
691 check_state (guestfs_h *g, const char *caller)
693 if (!guestfs__is_ready (g)) {
694 if (guestfs__is_config (g) || guestfs__is_launching (g))
695 error (g, \"%%s: call launch before using this function\\n(in guestfish, don't forget to use the 'run' command)\",
698 error (g, \"%%s called from the wrong state, %%d != READY\",
699 caller, guestfs__get_state (g));
705 /* Convenience wrapper for tracing. */
707 trace_open (guestfs_h *g)
709 assert (g->trace_fp == NULL);
712 g->trace_fp = open_memstream (&g->trace_buf, &g->trace_len);
720 trace_send_line (guestfs_h *g)
726 fclose (g->trace_fp);
729 /* The callback might invoke other libguestfs calls, so keep
730 * a copy of the pointer to the buffer and length.
735 guestfs___call_callbacks_message (g, GUESTFS_EVENT_TRACE, buf, len);
743 (* Generate code to check String-like parameters are not passed in
744 * as NULL (returning an error if they are).
746 let check_null_strings shortname (ret, args, optargs) =
747 let pr_newline = ref false in
750 (* parameters which should not be NULL *)
762 pr " if (%s == NULL) {\n" n;
763 pr " error (g, \"%%s: %%s: parameter cannot be NULL\",\n";
764 pr " \"%s\", \"%s\");\n" shortname n;
766 match errcode_of_ret ret with
767 | `CannotReturnError ->
768 if shortname = "test0rconstoptstring" then (* XXX hack *)
772 "%s: RConstOptString function has invalid parameter '%s'"
774 | (`ErrorIsMinusOne |`ErrorIsNULL) as e -> e in
775 pr " return %s;\n" (string_of_errcode errcode);
788 (* For optional arguments. *)
792 pr " if ((optargs->bitmask & GUESTFS_%s_%s_BITMASK) &&\n"
793 (String.uppercase shortname) (String.uppercase n);
794 pr " optargs->%s == NULL) {\n" n;
795 pr " error (g, \"%%s: %%s: optional parameter cannot be NULL\",\n";
796 pr " \"%s\", \"%s\");\n" shortname n;
798 match errcode_of_ret ret with
799 | `CannotReturnError -> assert false
800 | (`ErrorIsMinusOne |`ErrorIsNULL) as e -> e in
801 pr " return %s;\n" (string_of_errcode errcode);
806 | Bool _ | Int _ | Int64 _ -> ()
811 if !pr_newline then pr "\n";
814 (* Generate code to reject optargs we don't know about. *)
815 let reject_unknown_optargs shortname = function
818 let len = List.length optargs in
819 let mask = Int64.lognot (Int64.pred (Int64.shift_left 1L len)) in
820 pr " if (optargs->bitmask & UINT64_C(0x%Lx)) {\n" mask;
821 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";
822 pr " \"%s\", \"%s\");\n" shortname shortname;
824 match errcode_of_ret ret with
825 | `CannotReturnError -> assert false
826 | (`ErrorIsMinusOne |`ErrorIsNULL) as e -> e in
827 pr " return %s;\n" (string_of_errcode errcode);
832 (* Generate code to generate guestfish call traces. *)
833 let trace_call shortname (ret, args, optargs) =
834 pr " if (trace_flag) {\n";
837 List.exists (function
838 | StringList _ | DeviceList _ -> true
839 | _ -> false) args in
845 pr " trace_fp = trace_open (g);\n";
847 pr " fprintf (trace_fp, \"%%s\", \"%s\");\n" shortname;
849 (* Required arguments. *)
852 | String n (* strings *)
858 (* guestfish doesn't support string escaping, so neither do we *)
859 pr " fprintf (trace_fp, \" \\\"%%s\\\"\", %s);\n" n
861 (* don't print keys *)
862 pr " fprintf (trace_fp, \" \\\"***\\\"\");\n"
863 | OptString n -> (* string option *)
864 pr " if (%s) fprintf (trace_fp, \" \\\"%%s\\\"\", %s);\n" n n;
865 pr " else fprintf (trace_fp, \" null\");\n"
867 | DeviceList n -> (* string list *)
868 pr " fputc (' ', trace_fp);\n";
869 pr " fputc ('\"', trace_fp);\n";
870 pr " for (i = 0; %s[i]; ++i) {\n" n;
871 pr " if (i > 0) fputc (' ', trace_fp);\n";
872 pr " fputs (%s[i], trace_fp);\n" n;
874 pr " fputc ('\"', trace_fp);\n";
875 | Bool n -> (* boolean *)
876 pr " fputs (%s ? \" true\" : \" false\", trace_fp);\n" n
878 pr " fprintf (trace_fp, \" %%d\", %s);\n" n
880 pr " fprintf (trace_fp, \" %%\" PRIi64, %s);\n" n
881 | BufferIn n -> (* RHBZ#646822 *)
882 pr " fputc (' ', trace_fp);\n";
883 pr " guestfs___print_BufferIn (trace_fp, %s, %s_size);\n" n n
885 pr " fprintf (trace_fp, \" (%s)%%p\", %s);\n" t n
888 (* Optional arguments. *)
891 let n = name_of_argt argt in
892 let uc_shortname = String.uppercase shortname in
893 let uc_n = String.uppercase n in
894 pr " if (optargs->bitmask & GUESTFS_%s_%s_BITMASK)\n"
898 pr " fprintf (trace_fp, \" \\\"%%s:%%s\\\"\", \"%s\", optargs->%s);\n" n n
900 pr " fprintf (trace_fp, \" \\\"%%s:%%s\\\"\", \"%s\", optargs->%s ? \"true\" : \"false\");\n" n n
902 pr " fprintf (trace_fp, \" \\\"%%s:%%d\\\"\", \"%s\", optargs->%s);\n" n n
904 pr " fprintf (trace_fp, \" \\\"%%s:%%\" PRIi64 \"\\\"\", \"%s\", optargs->%s);\n" n n
909 pr " trace_send_line (g);\n";
914 let trace_return ?(indent = 2) shortname (ret, _, _) rv =
915 let indent = spaces indent in
917 pr "%sif (trace_flag) {\n" indent;
921 | RStringList _ | RHashtable _ -> true
924 pr "%s size_t i;\n" indent;
928 pr "%s trace_fp = trace_open (g);\n" indent;
930 pr "%s fprintf (trace_fp, \"%%s = \", \"%s\");\n" indent shortname;
933 | RErr | RInt _ | RBool _ ->
934 pr "%s fprintf (trace_fp, \"%%d\", %s);\n" indent rv
936 pr "%s fprintf (trace_fp, \"%%\" PRIi64, %s);\n" indent rv
937 | RConstString _ | RString _ ->
938 pr "%s fprintf (trace_fp, \"\\\"%%s\\\"\", %s);\n" indent rv
939 | RConstOptString _ ->
940 pr "%s fprintf (trace_fp, \"\\\"%%s\\\"\", %s != NULL ? %s : \"NULL\");\n"
943 pr "%s guestfs___print_BufferOut (trace_fp, %s, *size_r);\n" indent rv
944 | RStringList _ | RHashtable _ ->
945 pr "%s fputs (\"[\\\"\", trace_fp);\n" indent;
946 pr "%s for (i = 0; %s[i]; ++i) {\n" indent rv;
947 pr "%s if (i > 0) fputs (\"\\\", \\\"\", trace_fp);\n" indent;
948 pr "%s fputs (%s[i], trace_fp);\n" indent rv;
950 pr "%s fputs (\"\\\"]\", trace_fp);\n" indent;
951 | RStruct (_, typ) ->
952 (* XXX There is code generated for guestfish for printing
953 * these structures. We need to make it generally available
956 pr "%s fprintf (trace_fp, \"<struct guestfs_%s *>\");\n"
958 | RStructList (_, typ) ->
959 pr "%s fprintf (trace_fp, \"<struct guestfs_%s_list *>\");\n"
962 pr "%s trace_send_line (g);\n" indent;
967 let trace_return_error ?(indent = 2) shortname (ret, _, _) errcode =
968 let indent = spaces indent in
970 pr "%sif (trace_flag)\n" indent;
972 pr "%s guestfs___trace (g, \"%%s = %%s (error)\",\n" indent;
973 pr "%s \"%s\", \"%s\");\n"
974 indent shortname (string_of_errcode errcode)
977 (* For non-daemon functions, generate a wrapper around each function. *)
979 fun (shortname, (ret, _, optargs as style), _, _, _, _, _) ->
981 generate_prototype ~extern:false ~semicolon:false ~newline:true
982 ~handle:"g" ~prefix:"guestfs_"
985 generate_prototype ~extern:false ~semicolon:false ~newline:true
986 ~handle:"g" ~prefix:"guestfs_" ~suffix:"_argv" ~optarg_proto:Argv
989 pr " int trace_flag = g->trace;\n";
990 pr " FILE *trace_fp;\n";
992 | RErr | RInt _ | RBool _ ->
997 pr " const char *r;\n"
998 | RConstOptString _ ->
999 pr " const char *r;\n"
1000 | RString _ | RBufferOut _ ->
1002 | RStringList _ | RHashtable _ ->
1004 | RStruct (_, typ) ->
1005 pr " struct guestfs_%s *r;\n" typ
1006 | RStructList (_, typ) ->
1007 pr " struct guestfs_%s_list *r;\n" typ
1010 check_null_strings shortname style;
1011 reject_unknown_optargs shortname style;
1012 trace_call shortname style;
1013 pr " r = guestfs__%s " shortname;
1014 generate_c_call_args ~handle:"g" style;
1017 (match errcode_of_ret ret with
1018 | (`ErrorIsMinusOne | `ErrorIsNULL) as errcode ->
1019 pr " if (r != %s) {\n" (string_of_errcode errcode);
1020 trace_return ~indent:4 shortname style "r";
1022 trace_return_error ~indent:4 shortname style errcode;
1024 | `CannotReturnError ->
1025 trace_return shortname style "r";
1031 ) non_daemon_functions;
1033 (* Client-side stubs for each function. *)
1035 fun (shortname, (ret, args, optargs as style), _, _, _, _, _) ->
1036 let name = "guestfs_" ^ shortname in
1038 match errcode_of_ret ret with
1039 | `CannotReturnError -> assert false
1040 | (`ErrorIsMinusOne | `ErrorIsNULL) as e -> e in
1042 (* Generate the action stub. *)
1043 if optargs = [] then
1044 generate_prototype ~extern:false ~semicolon:false ~newline:true
1045 ~handle:"g" ~prefix:"guestfs_" shortname style
1047 generate_prototype ~extern:false ~semicolon:false ~newline:true
1048 ~handle:"g" ~prefix:"guestfs_" ~suffix:"_argv"
1049 ~optarg_proto:Argv shortname style;
1055 | _ -> pr " struct %s_args args;\n" name
1058 pr " guestfs_message_header hdr;\n";
1059 pr " guestfs_message_error err;\n";
1063 | RConstString _ | RConstOptString _ ->
1064 failwithf "RConstString|RConstOptString cannot be used by daemon functions"
1066 | RBool _ | RString _ | RStringList _
1067 | RStruct _ | RStructList _
1068 | RHashtable _ | RBufferOut _ ->
1069 pr " struct %s_ret ret;\n" name;
1072 pr " int serial;\n";
1074 pr " int trace_flag = g->trace;\n";
1075 pr " FILE *trace_fp;\n";
1077 | RErr | RInt _ | RBool _ ->
1080 pr " int64_t ret_v;\n"
1081 | RConstString _ | RConstOptString _ ->
1082 pr " const char *ret_v;\n"
1083 | RString _ | RBufferOut _ ->
1084 pr " char *ret_v;\n"
1085 | RStringList _ | RHashtable _ ->
1086 pr " char **ret_v;\n"
1087 | RStruct (_, typ) ->
1088 pr " struct guestfs_%s *ret_v;\n" typ
1089 | RStructList (_, typ) ->
1090 pr " struct guestfs_%s_list *ret_v;\n" typ
1094 List.exists (function FileIn _ -> true | _ -> false) args in
1095 if has_filein then (
1096 pr " uint64_t progress_hint = 0;\n";
1097 pr " struct stat progress_stat;\n";
1099 pr " const uint64_t progress_hint = 0;\n";
1102 check_null_strings shortname style;
1103 reject_unknown_optargs shortname style;
1104 trace_call shortname style;
1106 (* Calculate the total size of all FileIn arguments to pass
1107 * as a progress bar hint.
1112 pr " if (stat (%s, &progress_stat) == 0 &&\n" n;
1113 pr " S_ISREG (progress_stat.st_mode))\n";
1114 pr " progress_hint += progress_stat.st_size;\n";
1119 (* Check we are in the right state for sending a request. *)
1120 pr " if (check_state (g, \"%s\") == -1) {\n" shortname;
1121 trace_return_error ~indent:4 shortname style errcode;
1122 pr " return %s;\n" (string_of_errcode errcode);
1124 pr " guestfs___set_busy (g);\n";
1127 (* Send the main header and arguments. *)
1128 if args = [] && optargs = [] then (
1129 pr " serial = guestfs___send (g, GUESTFS_PROC_%s, progress_hint, 0,\n"
1130 (String.uppercase shortname);
1131 pr " NULL, NULL);\n"
1135 | Pathname n | Device n | Dev_or_Path n | String n | Key n ->
1136 pr " args.%s = (char *) %s;\n" n n
1138 pr " args.%s = %s ? (char **) &%s : NULL;\n" n n n
1139 | StringList n | DeviceList n ->
1140 pr " args.%s.%s_val = (char **) %s;\n" n n n;
1141 pr " for (args.%s.%s_len = 0; %s[args.%s.%s_len]; args.%s.%s_len++) ;\n" n n n n n n n;
1143 pr " args.%s = %s;\n" n n
1145 pr " args.%s = %s;\n" n n
1147 pr " args.%s = %s;\n" n n
1148 | FileIn _ | FileOut _ -> ()
1150 pr " /* Just catch grossly large sizes. XDR encoding will make this precise. */\n";
1151 pr " if (%s_size >= GUESTFS_MESSAGE_MAX) {\n" n;
1152 trace_return_error ~indent:4 shortname style errcode;
1153 pr " error (g, \"%%s: size of input buffer too large\", \"%s\");\n"
1155 pr " guestfs___end_busy (g);\n";
1156 pr " return %s;\n" (string_of_errcode errcode);
1158 pr " args.%s.%s_val = (char *) %s;\n" n n n;
1159 pr " args.%s.%s_len = %s_size;\n" n n n
1160 | Pointer _ -> assert false
1165 let n = name_of_argt argt in
1166 let uc_shortname = String.uppercase shortname in
1167 let uc_n = String.uppercase n in
1168 pr " if ((optargs->bitmask & GUESTFS_%s_%s_BITMASK))\n"
1174 pr " args.%s = optargs->%s;\n" n n;
1176 pr " args.%s = 0;\n" n
1178 pr " args.%s = (char *) optargs->%s;\n" n n;
1180 pr " args.%s = (char *) \"\";\n" n
1185 pr " serial = guestfs___send (g, GUESTFS_PROC_%s,\n"
1186 (String.uppercase shortname);
1187 pr " progress_hint, %s,\n"
1188 (if optargs <> [] then "optargs->bitmask" else "0");
1189 pr " (xdrproc_t) xdr_%s_args, (char *) &args);\n"
1192 pr " if (serial == -1) {\n";
1193 pr " guestfs___end_busy (g);\n";
1194 trace_return_error ~indent:4 shortname style errcode;
1195 pr " return %s;\n" (string_of_errcode errcode);
1199 (* Send any additional files (FileIn) requested. *)
1200 let need_read_reply_label = ref false in
1204 pr " r = guestfs___send_file (g, %s);\n" n;
1205 pr " if (r == -1) {\n";
1206 pr " guestfs___end_busy (g);\n";
1207 trace_return_error ~indent:4 shortname style errcode;
1208 pr " /* daemon will send an error reply which we discard */\n";
1209 pr " guestfs___recv_discard (g, \"%s\");\n" shortname;
1210 pr " return %s;\n" (string_of_errcode errcode);
1212 pr " if (r == -2) /* daemon cancelled */\n";
1213 pr " goto read_reply;\n";
1214 need_read_reply_label := true;
1219 (* Wait for the reply from the remote end. *)
1220 if !need_read_reply_label then pr " read_reply:\n";
1221 pr " memset (&hdr, 0, sizeof hdr);\n";
1222 pr " memset (&err, 0, sizeof err);\n";
1223 if has_ret then pr " memset (&ret, 0, sizeof ret);\n";
1225 pr " r = guestfs___recv (g, \"%s\", &hdr, &err,\n " shortname;
1229 pr "(xdrproc_t) xdr_guestfs_%s_ret, (char *) &ret" shortname;
1232 pr " if (r == -1) {\n";
1233 pr " guestfs___end_busy (g);\n";
1234 trace_return_error ~indent:4 shortname style errcode;
1235 pr " return %s;\n" (string_of_errcode errcode);
1239 pr " if (check_reply_header (g, &hdr, GUESTFS_PROC_%s, serial) == -1) {\n"
1240 (String.uppercase shortname);
1241 pr " guestfs___end_busy (g);\n";
1242 trace_return_error ~indent:4 shortname style errcode;
1243 pr " return %s;\n" (string_of_errcode errcode);
1247 pr " if (hdr.status == GUESTFS_STATUS_ERROR) {\n";
1248 trace_return_error ~indent:4 shortname style errcode;
1249 pr " int errnum = 0;\n";
1250 pr " if (err.errno_string[0] != '\\0')\n";
1251 pr " errnum = guestfs___string_to_errno (err.errno_string);\n";
1252 pr " if (errnum <= 0)\n";
1253 pr " error (g, \"%%s: %%s\", \"%s\", err.error_message);\n"
1256 pr " guestfs_error_errno (g, errnum, \"%%s: %%s\", \"%s\",\n"
1258 pr " err.error_message);\n";
1259 pr " free (err.error_message);\n";
1260 pr " free (err.errno_string);\n";
1261 pr " guestfs___end_busy (g);\n";
1262 pr " return %s;\n" (string_of_errcode errcode);
1266 (* Expecting to receive further files (FileOut)? *)
1270 pr " if (guestfs___recv_file (g, %s) == -1) {\n" n;
1271 pr " guestfs___end_busy (g);\n";
1272 trace_return_error ~indent:4 shortname style errcode;
1273 pr " return %s;\n" (string_of_errcode errcode);
1279 pr " guestfs___end_busy (g);\n";
1284 | RInt n | RInt64 n | RBool n ->
1285 pr " ret_v = ret.%s;\n" n
1286 | RConstString _ | RConstOptString _ ->
1287 failwithf "RConstString|RConstOptString cannot be used by daemon functions"
1289 pr " ret_v = ret.%s; /* caller will free */\n" n
1290 | RStringList n | RHashtable n ->
1291 pr " /* caller will free this, but we need to add a NULL entry */\n";
1292 pr " ret.%s.%s_val =\n" n n;
1293 pr " safe_realloc (g, ret.%s.%s_val,\n" n n;
1294 pr " sizeof (char *) * (ret.%s.%s_len + 1));\n"
1296 pr " ret.%s.%s_val[ret.%s.%s_len] = NULL;\n" n n n n;
1297 pr " ret_v = ret.%s.%s_val;\n" n n
1299 pr " /* caller will free this */\n";
1300 pr " ret_v = safe_memdup (g, &ret.%s, sizeof (ret.%s));\n" n n
1301 | RStructList (n, _) ->
1302 pr " /* caller will free this */\n";
1303 pr " ret_v = safe_memdup (g, &ret.%s, sizeof (ret.%s));\n" n n
1305 pr " /* RBufferOut is tricky: If the buffer is zero-length, then\n";
1306 pr " * _val might be NULL here. To make the API saner for\n";
1307 pr " * callers, we turn this case into a unique pointer (using\n";
1308 pr " * malloc(1)).\n";
1310 pr " if (ret.%s.%s_len > 0) {\n" n n;
1311 pr " *size_r = ret.%s.%s_len;\n" n n;
1312 pr " ret_v = ret.%s.%s_val; /* caller will free */\n" n n;
1314 pr " free (ret.%s.%s_val);\n" n n;
1315 pr " char *p = safe_malloc (g, 1);\n";
1316 pr " *size_r = ret.%s.%s_len;\n" n n;
1320 trace_return shortname style "ret_v";
1321 pr " return ret_v;\n";
1325 (* Functions to free structures. *)
1326 pr "/* Structure-freeing functions. These rely on the fact that the\n";
1327 pr " * structure format is identical to the XDR format. See note in\n";
1328 pr " * generator.ml.\n";
1335 pr "guestfs_free_%s (struct guestfs_%s *x)\n" typ typ;
1337 pr " xdr_free ((xdrproc_t) xdr_guestfs_int_%s, (char *) x);\n" typ;
1343 pr "guestfs_free_%s_list (struct guestfs_%s_list *x)\n" typ typ;
1345 pr " xdr_free ((xdrproc_t) xdr_guestfs_int_%s_list, (char *) x);\n" typ;
1352 (* Functions which have optional arguments have two generated variants. *)
1355 | shortname, (ret, args, (_::_ as optargs) as style), _, _, _, _, _ ->
1356 let uc_shortname = String.uppercase shortname in
1358 (* Get the name of the last regular argument. *)
1362 | args -> name_of_argt (List.hd (List.rev args)) in
1366 | RErr | RInt _ | RBool _ -> "int "
1367 | RInt64 _ -> "int64_t "
1368 | RConstString _ | RConstOptString _ -> "const char *"
1369 | RString _ | RBufferOut _ -> "char *"
1370 | RStringList _ | RHashtable _ -> "char **"
1371 | RStruct (_, typ) -> sprintf "struct guestfs_%s *" typ
1372 | RStructList (_, typ) ->
1373 sprintf "struct guestfs_%s_list *" typ in
1375 (* The regular variable args function, just calls the _va variant. *)
1376 generate_prototype ~extern:false ~semicolon:false ~newline:true
1377 ~handle:"g" ~prefix:"guestfs_" shortname style;
1379 pr " va_list optargs;\n";
1381 pr " va_start (optargs, %s);\n" last_arg;
1382 pr " %sr = guestfs_%s_va " rtype shortname;
1383 generate_c_call_args ~handle:"g" style;
1385 pr " va_end (optargs);\n";
1390 generate_prototype ~extern:false ~semicolon:false ~newline:true
1391 ~handle:"g" ~prefix:"guestfs_" ~suffix:"_va" ~optarg_proto:VA
1394 pr " struct guestfs_%s_argv optargs_s;\n" shortname;
1395 pr " struct guestfs_%s_argv *optargs = &optargs_s;\n" shortname;
1398 pr " optargs_s.bitmask = 0;\n";
1400 pr " while ((i = va_arg (args, int)) >= 0) {\n";
1401 pr " switch (i) {\n";
1405 let n = name_of_argt argt in
1406 let uc_n = String.uppercase n in
1407 pr " case GUESTFS_%s_%s:\n" uc_shortname uc_n;
1408 pr " optargs_s.%s = va_arg (args, " n;
1410 | Bool _ | Int _ -> pr "int"
1411 | Int64 _ -> pr "int64_t"
1412 | String _ -> pr "const char *"
1420 match errcode_of_ret ret with
1421 | `CannotReturnError -> assert false
1422 | (`ErrorIsMinusOne | `ErrorIsNULL) as e -> e in
1425 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";
1426 pr " \"%s\", i);\n" shortname;
1427 pr " return %s;\n" (string_of_errcode errcode);
1430 pr " uint64_t i_mask = UINT64_C(1) << i;\n";
1431 pr " if (optargs_s.bitmask & i_mask) {\n";
1432 pr " error (g, \"%%s: same optional argument specified more than once\",\n";
1433 pr " \"%s\");\n" shortname;
1434 pr " return %s;\n" (string_of_errcode errcode);
1436 pr " optargs_s.bitmask |= i_mask;\n";
1439 pr " return guestfs_%s_argv " shortname;
1440 generate_c_call_args ~handle:"g" style;
1444 ) all_functions_sorted
1446 (* Generate the linker script which controls the visibility of
1447 * symbols in the public ABI and ensures no other symbols get
1448 * exported accidentally.
1450 and generate_linker_script () =
1451 generate_header HashStyle GPLv2plus;
1456 "guestfs_delete_event_callback";
1457 "guestfs_first_private";
1458 "guestfs_get_error_handler";
1459 "guestfs_get_out_of_memory_handler";
1460 "guestfs_get_private";
1461 "guestfs_last_errno";
1462 "guestfs_last_error";
1463 "guestfs_next_private";
1464 "guestfs_set_close_callback";
1465 "guestfs_set_error_handler";
1466 "guestfs_set_event_callback";
1467 "guestfs_set_launch_done_callback";
1468 "guestfs_set_log_message_callback";
1469 "guestfs_set_out_of_memory_handler";
1470 "guestfs_set_private";
1471 "guestfs_set_progress_callback";
1472 "guestfs_set_subprocess_quit_callback";
1474 (* Unofficial parts of the API: the bindings code use these
1475 * functions, so it is useful to export them.
1477 "guestfs_safe_calloc";
1478 "guestfs_safe_malloc";
1479 "guestfs_safe_strdup";
1480 "guestfs_safe_memdup";
1482 "guestfs___for_each_disk";
1488 | name, (_, _, []), _, _, _, _, _ -> ["guestfs_" ^ name]
1489 | name, (_, _, _), _, _, _, _, _ ->
1491 "guestfs_" ^ name ^ "_va";
1492 "guestfs_" ^ name ^ "_argv"]
1497 List.map (fun (typ, _) ->
1498 ["guestfs_free_" ^ typ; "guestfs_free_" ^ typ ^ "_list"])
1501 let globals = List.sort compare (globals @ functions @ structs) in
1505 List.iter (pr " %s;\n") globals;
1512 and generate_max_proc_nr () =
1513 pr "%d\n" max_proc_nr