generator: Missing newline character.
[libguestfs.git] / generator / generator_c.ml
1 (* libguestfs
2  * Copyright (C) 2009-2010 Red Hat Inc.
3  *
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.
8  *
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.
13  *
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
17  *)
18
19 (* Please read generator/README first. *)
20
21 open Printf
22
23 open Generator_types
24 open Generator_utils
25 open Generator_pr
26 open Generator_docstrings
27 open Generator_optgroups
28 open Generator_actions
29 open Generator_structs
30
31 (* Generate C API. *)
32
33 (* Generate a C function prototype. *)
34 let rec generate_prototype ?(extern = true) ?(static = false)
35     ?(semicolon = true)
36     ?(single_line = false) ?(newline = false) ?(in_daemon = false)
37     ?(prefix = "")
38     ?handle name style =
39   if extern then pr "extern ";
40   if static then pr "static ";
41   (match fst style with
42    | RErr -> pr "int "
43    | RInt _ -> pr "int "
44    | RInt64 _ -> pr "int64_t "
45    | RBool _ -> pr "int "
46    | RConstString _ | RConstOptString _ -> pr "const char *"
47    | RString _ | RBufferOut _ -> pr "char *"
48    | RStringList _ | RHashtable _ -> pr "char **"
49    | RStruct (_, typ) ->
50        if not in_daemon then pr "struct guestfs_%s *" typ
51        else pr "guestfs_int_%s *" typ
52    | RStructList (_, typ) ->
53        if not in_daemon then pr "struct guestfs_%s_list *" typ
54        else pr "guestfs_int_%s_list *" typ
55   );
56   let is_RBufferOut = match fst style with RBufferOut _ -> true | _ -> false in
57   pr "%s%s (" prefix name;
58   if handle = None && List.length (snd style) = 0 && not is_RBufferOut then
59     pr "void"
60   else (
61     let comma = ref false in
62     (match handle with
63      | None -> ()
64      | Some handle -> pr "guestfs_h *%s" handle; comma := true
65     );
66     let next () =
67       if !comma then (
68         if single_line then pr ", " else pr ",\n\t\t"
69       );
70       comma := true
71     in
72     List.iter (
73       function
74       | Pathname n
75       | Device n | Dev_or_Path n
76       | String n
77       | OptString n
78       | Key n ->
79           next ();
80           pr "const char *%s" n
81       | StringList n | DeviceList n ->
82           next ();
83           pr "char *const *%s" n
84       | Bool n -> next (); pr "int %s" n
85       | Int n -> next (); pr "int %s" n
86       | Int64 n -> next (); pr "int64_t %s" n
87       | FileIn n
88       | FileOut n ->
89           if not in_daemon then (next (); pr "const char *%s" n)
90       | BufferIn n ->
91           next ();
92           pr "const char *%s" n;
93           next ();
94           pr "size_t %s_size" n
95     ) (snd style);
96     if is_RBufferOut then (next (); pr "size_t *size_r");
97   );
98   pr ")";
99   if semicolon then pr ";";
100   if newline then pr "\n"
101
102 (* Generate C call arguments, eg "(handle, foo, bar)" *)
103 and generate_c_call_args ?handle style =
104   pr "(";
105   let comma = ref false in
106   let next () =
107     if !comma then pr ", ";
108     comma := true
109   in
110   (match handle with
111    | None -> ()
112    | Some handle -> pr "%s" handle; comma := true
113   );
114   List.iter (
115     function
116     | BufferIn n ->
117         next ();
118         pr "%s, %s_size" n n
119     | arg ->
120         next ();
121         pr "%s" (name_of_argt arg)
122   ) (snd style);
123   (* For RBufferOut calls, add implicit &size parameter. *)
124   (match fst style with
125    | RBufferOut _ ->
126        next ();
127        pr "&size"
128    | _ -> ()
129   );
130   pr ")"
131
132 (* Generate the pod documentation for the C API. *)
133 and generate_actions_pod () =
134   List.iter (
135     fun (shortname, style, _, flags, _, _, longdesc) ->
136       if not (List.mem NotInDocs flags) then (
137         let name = "guestfs_" ^ shortname in
138         pr "=head2 %s\n\n" name;
139         pr " ";
140         generate_prototype ~extern:false ~handle:"g" name style;
141         pr "\n\n";
142         pr "%s\n\n" longdesc;
143         (match fst style with
144          | RErr ->
145              pr "This function returns 0 on success or -1 on error.\n\n"
146          | RInt _ ->
147              pr "On error this function returns -1.\n\n"
148          | RInt64 _ ->
149              pr "On error this function returns -1.\n\n"
150          | RBool _ ->
151              pr "This function returns a C truth value on success or -1 on error.\n\n"
152          | RConstString _ ->
153              pr "This function returns a string, or NULL on error.
154 The string is owned by the guest handle and must I<not> be freed.\n\n"
155          | RConstOptString _ ->
156              pr "This function returns a string which may be NULL.
157 There is no way to return an error from this function.
158 The string is owned by the guest handle and must I<not> be freed.\n\n"
159          | RString _ ->
160              pr "This function returns a string, or NULL on error.
161 I<The caller must free the returned string after use>.\n\n"
162          | RStringList _ ->
163              pr "This function returns a NULL-terminated array of strings
164 (like L<environ(3)>), or NULL if there was an error.
165 I<The caller must free the strings and the array after use>.\n\n"
166          | RStruct (_, typ) ->
167              pr "This function returns a C<struct guestfs_%s *>,
168 or NULL if there was an error.
169 I<The caller must call C<guestfs_free_%s> after use>.\n\n" typ typ
170          | RStructList (_, typ) ->
171              pr "This function returns a C<struct guestfs_%s_list *>
172 (see E<lt>guestfs-structs.hE<gt>),
173 or NULL if there was an error.
174 I<The caller must call C<guestfs_free_%s_list> after use>.\n\n" typ typ
175          | RHashtable _ ->
176              pr "This function returns a NULL-terminated array of
177 strings, or NULL if there was an error.
178 The array of strings will always have length C<2n+1>, where
179 C<n> keys and values alternate, followed by the trailing NULL entry.
180 I<The caller must free the strings and the array after use>.\n\n"
181          | RBufferOut _ ->
182              pr "This function returns a buffer, or NULL on error.
183 The size of the returned buffer is written to C<*size_r>.
184 I<The caller must free the returned buffer after use>.\n\n"
185         );
186         if List.mem Progress flags then
187           pr "%s\n\n" progress_message;
188         if List.mem ProtocolLimitWarning flags then
189           pr "%s\n\n" protocol_limit_warning;
190         if List.mem DangerWillRobinson flags then
191           pr "%s\n\n" danger_will_robinson;
192         if List.exists (function Key _ -> true | _ -> false) (snd style) then
193           pr "This function takes a key or passphrase parameter which
194 could contain sensitive material.  Read the section
195 L</KEYS AND PASSPHRASES> for more information.\n\n";
196         match deprecation_notice flags with
197         | None -> ()
198         | Some txt -> pr "%s\n\n" txt
199       )
200   ) all_functions_sorted
201
202 and generate_structs_pod () =
203   (* Structs documentation. *)
204   List.iter (
205     fun (typ, cols) ->
206       pr "=head2 guestfs_%s\n" typ;
207       pr "\n";
208       pr " struct guestfs_%s {\n" typ;
209       List.iter (
210         function
211         | name, FChar -> pr "   char %s;\n" name
212         | name, FUInt32 -> pr "   uint32_t %s;\n" name
213         | name, FInt32 -> pr "   int32_t %s;\n" name
214         | name, (FUInt64|FBytes) -> pr "   uint64_t %s;\n" name
215         | name, FInt64 -> pr "   int64_t %s;\n" name
216         | name, FString -> pr "   char *%s;\n" name
217         | name, FBuffer ->
218             pr "   /* The next two fields describe a byte array. */\n";
219             pr "   uint32_t %s_len;\n" name;
220             pr "   char *%s;\n" name
221         | name, FUUID ->
222             pr "   /* The next field is NOT nul-terminated, be careful when printing it: */\n";
223             pr "   char %s[32];\n" name
224         | name, FOptPercent ->
225             pr "   /* The next field is [0..100] or -1 meaning 'not present': */\n";
226             pr "   float %s;\n" name
227       ) cols;
228       pr " };\n";
229       pr " \n";
230       pr " struct guestfs_%s_list {\n" typ;
231       pr "   uint32_t len; /* Number of elements in list. */\n";
232       pr "   struct guestfs_%s *val; /* Elements. */\n" typ;
233       pr " };\n";
234       pr " \n";
235       pr " void guestfs_free_%s (struct guestfs_free_%s *);\n" typ typ;
236       pr " void guestfs_free_%s_list (struct guestfs_free_%s_list *);\n"
237         typ typ;
238       pr "\n"
239   ) structs
240
241 and generate_availability_pod () =
242   (* Availability documentation. *)
243   pr "=over 4\n";
244   pr "\n";
245   List.iter (
246     fun (group, functions) ->
247       pr "=item B<%s>\n" group;
248       pr "\n";
249       pr "The following functions:\n";
250       List.iter (pr "L</guestfs_%s>\n") functions;
251       pr "\n"
252   ) optgroups;
253   pr "=back\n";
254   pr "\n"
255
256 (* Generate the guestfs-structs.h file. *)
257 and generate_structs_h () =
258   generate_header CStyle LGPLv2plus;
259
260   (* This is a public exported header file containing various
261    * structures.  The structures are carefully written to have
262    * exactly the same in-memory format as the XDR structures that
263    * we use on the wire to the daemon.  The reason for creating
264    * copies of these structures here is just so we don't have to
265    * export the whole of guestfs_protocol.h (which includes much
266    * unrelated and XDR-dependent stuff that we don't want to be
267    * public, or required by clients).
268    *
269    * To reiterate, we will pass these structures to and from the
270    * client with a simple assignment or memcpy, so the format
271    * must be identical to what rpcgen / the RFC defines.
272    *)
273
274   (* Public structures. *)
275   List.iter (
276     fun (typ, cols) ->
277       pr "struct guestfs_%s {\n" typ;
278       List.iter (
279         function
280         | name, FChar -> pr "  char %s;\n" name
281         | name, FString -> pr "  char *%s;\n" name
282         | name, FBuffer ->
283             pr "  uint32_t %s_len;\n" name;
284             pr "  char *%s;\n" name
285         | name, FUUID -> pr "  char %s[32]; /* this is NOT nul-terminated, be careful when printing */\n" name
286         | name, FUInt32 -> pr "  uint32_t %s;\n" name
287         | name, FInt32 -> pr "  int32_t %s;\n" name
288         | name, (FUInt64|FBytes) -> pr "  uint64_t %s;\n" name
289         | name, FInt64 -> pr "  int64_t %s;\n" name
290         | name, FOptPercent -> pr "  float %s; /* [0..100] or -1 */\n" name
291       ) cols;
292       pr "};\n";
293       pr "\n";
294       pr "struct guestfs_%s_list {\n" typ;
295       pr "  uint32_t len;\n";
296       pr "  struct guestfs_%s *val;\n" typ;
297       pr "};\n";
298       pr "\n";
299       pr "extern void guestfs_free_%s (struct guestfs_%s *);\n" typ typ;
300       pr "extern void guestfs_free_%s_list (struct guestfs_%s_list *);\n" typ typ;
301       pr "\n"
302   ) structs
303
304 (* Generate the guestfs-actions.h file. *)
305 and generate_actions_h () =
306   generate_header CStyle LGPLv2plus;
307   List.iter (
308     fun (shortname, style, _, flags, _, _, _) ->
309       let name = "guestfs_" ^ shortname in
310
311       let deprecated =
312         List.exists (function DeprecatedBy _ -> true | _ -> false) flags in
313       let test0 =
314         String.length shortname >= 5 && String.sub shortname 0 5 = "test0" in
315       let debug =
316         String.length shortname >= 5 && String.sub shortname 0 5 = "debug" in
317       if not deprecated && not test0 && not debug then
318         pr "#define LIBGUESTFS_HAVE_%s 1\n" (String.uppercase shortname);
319
320       generate_prototype ~single_line:true ~newline:true ~handle:"g"
321         name style
322   ) all_functions_sorted
323
324 (* Generate the guestfs-internal-actions.h file. *)
325 and generate_internal_actions_h () =
326   generate_header CStyle LGPLv2plus;
327   List.iter (
328     fun (shortname, style, _, _, _, _, _) ->
329       let name = "guestfs__" ^ shortname in
330       generate_prototype ~single_line:true ~newline:true ~handle:"g"
331         name style
332   ) non_daemon_functions
333
334 (* Generate the client-side dispatch stubs. *)
335 and generate_client_actions () =
336   generate_header CStyle LGPLv2plus;
337
338   pr "\
339 #include <stdio.h>
340 #include <stdlib.h>
341 #include <stdint.h>
342 #include <string.h>
343 #include <inttypes.h>
344
345 #include \"guestfs.h\"
346 #include \"guestfs-internal.h\"
347 #include \"guestfs-internal-actions.h\"
348 #include \"guestfs_protocol.h\"
349
350 /* Check the return message from a call for validity. */
351 static int
352 check_reply_header (guestfs_h *g,
353                     const struct guestfs_message_header *hdr,
354                     unsigned int proc_nr, unsigned int serial)
355 {
356   if (hdr->prog != GUESTFS_PROGRAM) {
357     error (g, \"wrong program (%%d/%%d)\", hdr->prog, GUESTFS_PROGRAM);
358     return -1;
359   }
360   if (hdr->vers != GUESTFS_PROTOCOL_VERSION) {
361     error (g, \"wrong protocol version (%%d/%%d)\",
362            hdr->vers, GUESTFS_PROTOCOL_VERSION);
363     return -1;
364   }
365   if (hdr->direction != GUESTFS_DIRECTION_REPLY) {
366     error (g, \"unexpected message direction (%%d/%%d)\",
367            hdr->direction, GUESTFS_DIRECTION_REPLY);
368     return -1;
369   }
370   if (hdr->proc != proc_nr) {
371     error (g, \"unexpected procedure number (%%d/%%d)\", hdr->proc, proc_nr);
372     return -1;
373   }
374   if (hdr->serial != serial) {
375     error (g, \"unexpected serial (%%d/%%d)\", hdr->serial, serial);
376     return -1;
377   }
378
379   return 0;
380 }
381
382 /* Check we are in the right state to run a high-level action. */
383 static int
384 check_state (guestfs_h *g, const char *caller)
385 {
386   if (!guestfs__is_ready (g)) {
387     if (guestfs__is_config (g) || guestfs__is_launching (g))
388       error (g, \"%%s: call launch before using this function\\n(in guestfish, don't forget to use the 'run' command)\",
389         caller);
390     else
391       error (g, \"%%s called from the wrong state, %%d != READY\",
392         caller, guestfs__get_state (g));
393     return -1;
394   }
395   return 0;
396 }
397
398 ";
399
400   let error_code_of = function
401     | RErr | RInt _ | RInt64 _ | RBool _ -> "-1"
402     | RConstString _ | RConstOptString _
403     | RString _ | RStringList _
404     | RStruct _ | RStructList _
405     | RHashtable _ | RBufferOut _ -> "NULL"
406   in
407
408   (* Generate code to check String-like parameters are not passed in
409    * as NULL (returning an error if they are).
410    *)
411   let check_null_strings shortname style =
412     let pr_newline = ref false in
413     List.iter (
414       function
415       (* parameters which should not be NULL *)
416       | String n
417       | Device n
418       | Pathname n
419       | Dev_or_Path n
420       | FileIn n
421       | FileOut n
422       | BufferIn n
423       | StringList n
424       | DeviceList n
425       | Key n ->
426           pr "  if (%s == NULL) {\n" n;
427           pr "    error (g, \"%%s: %%s: parameter cannot be NULL\",\n";
428           pr "           \"%s\", \"%s\");\n" shortname n;
429           pr "    return %s;\n" (error_code_of (fst style));
430           pr "  }\n";
431           pr_newline := true
432
433       (* can be NULL *)
434       | OptString _
435
436       (* not applicable *)
437       | Bool _
438       | Int _
439       | Int64 _ -> ()
440     ) (snd style);
441
442     if !pr_newline then pr "\n";
443   in
444
445   (* Generate code to generate guestfish call traces. *)
446   let trace_call shortname style =
447     pr "  if (guestfs__get_trace (g)) {\n";
448
449     let needs_i =
450       List.exists (function
451                    | StringList _ | DeviceList _ -> true
452                    | _ -> false) (snd style) in
453     if needs_i then (
454       pr "    size_t i;\n";
455       pr "\n"
456     );
457
458     pr "    fprintf (stderr, \"%s\");\n" shortname;
459     List.iter (
460       function
461       | String n                        (* strings *)
462       | Device n
463       | Pathname n
464       | Dev_or_Path n
465       | FileIn n
466       | FileOut n
467       | BufferIn n
468       | Key n ->
469           (* guestfish doesn't support string escaping, so neither do we *)
470           pr "    fprintf (stderr, \" \\\"%%s\\\"\", %s);\n" n
471       | OptString n ->                  (* string option *)
472           pr "    if (%s) fprintf (stderr, \" \\\"%%s\\\"\", %s);\n" n n;
473           pr "    else fprintf (stderr, \" null\");\n"
474       | StringList n
475       | DeviceList n ->                 (* string list *)
476           pr "    fputc (' ', stderr);\n";
477           pr "    fputc ('\"', stderr);\n";
478           pr "    for (i = 0; %s[i]; ++i) {\n" n;
479           pr "      if (i > 0) fputc (' ', stderr);\n";
480           pr "      fputs (%s[i], stderr);\n" n;
481           pr "    }\n";
482           pr "    fputc ('\"', stderr);\n";
483       | Bool n ->                       (* boolean *)
484           pr "    fputs (%s ? \" true\" : \" false\", stderr);\n" n
485       | Int n ->                        (* int *)
486           pr "    fprintf (stderr, \" %%d\", %s);\n" n
487       | Int64 n ->
488           pr "    fprintf (stderr, \" %%\" PRIi64, %s);\n" n
489     ) (snd style);
490     pr "    fputc ('\\n', stderr);\n";
491     pr "  }\n";
492     pr "\n";
493   in
494
495   (* For non-daemon functions, generate a wrapper around each function. *)
496   List.iter (
497     fun (shortname, style, _, _, _, _, _) ->
498       let name = "guestfs_" ^ shortname in
499
500       generate_prototype ~extern:false ~semicolon:false ~newline:true
501         ~handle:"g" name style;
502       pr "{\n";
503       check_null_strings shortname style;
504       trace_call shortname style;
505       pr "  return guestfs__%s " shortname;
506       generate_c_call_args ~handle:"g" style;
507       pr ";\n";
508       pr "}\n";
509       pr "\n"
510   ) non_daemon_functions;
511
512   (* Client-side stubs for each function. *)
513   List.iter (
514     fun (shortname, style, _, _, _, _, _) ->
515       let name = "guestfs_" ^ shortname in
516       let error_code = error_code_of (fst style) in
517
518       (* Generate the action stub. *)
519       generate_prototype ~extern:false ~semicolon:false ~newline:true
520         ~handle:"g" name style;
521
522       pr "{\n";
523
524       (match snd style with
525        | [] -> ()
526        | _ -> pr "  struct %s_args args;\n" name
527       );
528
529       pr "  guestfs_message_header hdr;\n";
530       pr "  guestfs_message_error err;\n";
531       let has_ret =
532         match fst style with
533         | RErr -> false
534         | RConstString _ | RConstOptString _ ->
535             failwithf "RConstString|RConstOptString cannot be used by daemon functions"
536         | RInt _ | RInt64 _
537         | RBool _ | RString _ | RStringList _
538         | RStruct _ | RStructList _
539         | RHashtable _ | RBufferOut _ ->
540             pr "  struct %s_ret ret;\n" name;
541             true in
542
543       pr "  int serial;\n";
544       pr "  int r;\n";
545       pr "\n";
546       check_null_strings shortname style;
547       trace_call shortname style;
548       pr "  if (check_state (g, \"%s\") == -1) return %s;\n"
549         shortname error_code;
550       pr "  guestfs___set_busy (g);\n";
551       pr "\n";
552
553       (* Send the main header and arguments. *)
554       (match snd style with
555        | [] ->
556            pr "  serial = guestfs___send (g, GUESTFS_PROC_%s, NULL, NULL);\n"
557              (String.uppercase shortname)
558        | args ->
559            List.iter (
560              function
561              | Pathname n | Device n | Dev_or_Path n | String n | Key n ->
562                  pr "  args.%s = (char *) %s;\n" n n
563              | OptString n ->
564                  pr "  args.%s = %s ? (char **) &%s : NULL;\n" n n n
565              | StringList n | DeviceList n ->
566                  pr "  args.%s.%s_val = (char **) %s;\n" n n n;
567                  pr "  for (args.%s.%s_len = 0; %s[args.%s.%s_len]; args.%s.%s_len++) ;\n" n n n n n n n;
568              | Bool n ->
569                  pr "  args.%s = %s;\n" n n
570              | Int n ->
571                  pr "  args.%s = %s;\n" n n
572              | Int64 n ->
573                  pr "  args.%s = %s;\n" n n
574              | FileIn _ | FileOut _ -> ()
575              | BufferIn n ->
576                  pr "  /* Just catch grossly large sizes. XDR encoding will make this precise. */\n";
577                  pr "  if (%s_size >= GUESTFS_MESSAGE_MAX) {\n" n;
578                  pr "    error (g, \"%%s: size of input buffer too large\", \"%s\");\n"
579                    shortname;
580                  pr "    guestfs___end_busy (g);\n";
581                  pr "    return %s;\n" error_code;
582                  pr "  }\n";
583                  pr "  args.%s.%s_val = (char *) %s;\n" n n n;
584                  pr "  args.%s.%s_len = %s_size;\n" n n n
585            ) args;
586            pr "  serial = guestfs___send (g, GUESTFS_PROC_%s,\n"
587              (String.uppercase shortname);
588            pr "        (xdrproc_t) xdr_%s_args, (char *) &args);\n"
589              name;
590       );
591       pr "  if (serial == -1) {\n";
592       pr "    guestfs___end_busy (g);\n";
593       pr "    return %s;\n" error_code;
594       pr "  }\n";
595       pr "\n";
596
597       (* Send any additional files (FileIn) requested. *)
598       let need_read_reply_label = ref false in
599       List.iter (
600         function
601         | FileIn n ->
602             pr "  r = guestfs___send_file (g, %s);\n" n;
603             pr "  if (r == -1) {\n";
604             pr "    guestfs___end_busy (g);\n";
605             pr "    return %s;\n" error_code;
606             pr "  }\n";
607             pr "  if (r == -2) /* daemon cancelled */\n";
608             pr "    goto read_reply;\n";
609             need_read_reply_label := true;
610             pr "\n";
611         | _ -> ()
612       ) (snd style);
613
614       (* Wait for the reply from the remote end. *)
615       if !need_read_reply_label then pr " read_reply:\n";
616       pr "  memset (&hdr, 0, sizeof hdr);\n";
617       pr "  memset (&err, 0, sizeof err);\n";
618       if has_ret then pr "  memset (&ret, 0, sizeof ret);\n";
619       pr "\n";
620       pr "  r = guestfs___recv (g, \"%s\", &hdr, &err,\n        " shortname;
621       if not has_ret then
622         pr "NULL, NULL"
623       else
624         pr "(xdrproc_t) xdr_guestfs_%s_ret, (char *) &ret" shortname;
625       pr ");\n";
626
627       pr "  if (r == -1) {\n";
628       pr "    guestfs___end_busy (g);\n";
629       pr "    return %s;\n" error_code;
630       pr "  }\n";
631       pr "\n";
632
633       pr "  if (check_reply_header (g, &hdr, GUESTFS_PROC_%s, serial) == -1) {\n"
634         (String.uppercase shortname);
635       pr "    guestfs___end_busy (g);\n";
636       pr "    return %s;\n" error_code;
637       pr "  }\n";
638       pr "\n";
639
640       pr "  if (hdr.status == GUESTFS_STATUS_ERROR) {\n";
641       pr "    error (g, \"%%s: %%s\", \"%s\", err.error_message);\n" shortname;
642       pr "    free (err.error_message);\n";
643       pr "    guestfs___end_busy (g);\n";
644       pr "    return %s;\n" error_code;
645       pr "  }\n";
646       pr "\n";
647
648       (* Expecting to receive further files (FileOut)? *)
649       List.iter (
650         function
651         | FileOut n ->
652             pr "  if (guestfs___recv_file (g, %s) == -1) {\n" n;
653             pr "    guestfs___end_busy (g);\n";
654             pr "    return %s;\n" error_code;
655             pr "  }\n";
656             pr "\n";
657         | _ -> ()
658       ) (snd style);
659
660       pr "  guestfs___end_busy (g);\n";
661
662       (match fst style with
663        | RErr -> pr "  return 0;\n"
664        | RInt n | RInt64 n | RBool n ->
665            pr "  return ret.%s;\n" n
666        | RConstString _ | RConstOptString _ ->
667            failwithf "RConstString|RConstOptString cannot be used by daemon functions"
668        | RString n ->
669            pr "  return ret.%s; /* caller will free */\n" n
670        | RStringList n | RHashtable n ->
671            pr "  /* caller will free this, but we need to add a NULL entry */\n";
672            pr "  ret.%s.%s_val =\n" n n;
673            pr "    safe_realloc (g, ret.%s.%s_val,\n" n n;
674            pr "                  sizeof (char *) * (ret.%s.%s_len + 1));\n"
675              n n;
676            pr "  ret.%s.%s_val[ret.%s.%s_len] = NULL;\n" n n n n;
677            pr "  return ret.%s.%s_val;\n" n n
678        | RStruct (n, _) ->
679            pr "  /* caller will free this */\n";
680            pr "  return safe_memdup (g, &ret.%s, sizeof (ret.%s));\n" n n
681        | RStructList (n, _) ->
682            pr "  /* caller will free this */\n";
683            pr "  return safe_memdup (g, &ret.%s, sizeof (ret.%s));\n" n n
684        | RBufferOut n ->
685            pr "  /* RBufferOut is tricky: If the buffer is zero-length, then\n";
686            pr "   * _val might be NULL here.  To make the API saner for\n";
687            pr "   * callers, we turn this case into a unique pointer (using\n";
688            pr "   * malloc(1)).\n";
689            pr "   */\n";
690            pr "  if (ret.%s.%s_len > 0) {\n" n n;
691            pr "    *size_r = ret.%s.%s_len;\n" n n;
692            pr "    return ret.%s.%s_val; /* caller will free */\n" n n;
693            pr "  } else {\n";
694            pr "    free (ret.%s.%s_val);\n" n n;
695            pr "    char *p = safe_malloc (g, 1);\n";
696            pr "    *size_r = ret.%s.%s_len;\n" n n;
697            pr "    return p;\n";
698            pr "  }\n";
699       );
700
701       pr "}\n\n"
702   ) daemon_functions;
703
704   (* Functions to free structures. *)
705   pr "/* Structure-freeing functions.  These rely on the fact that the\n";
706   pr " * structure format is identical to the XDR format.  See note in\n";
707   pr " * generator.ml.\n";
708   pr " */\n";
709   pr "\n";
710
711   List.iter (
712     fun (typ, _) ->
713       pr "void\n";
714       pr "guestfs_free_%s (struct guestfs_%s *x)\n" typ typ;
715       pr "{\n";
716       pr "  xdr_free ((xdrproc_t) xdr_guestfs_int_%s, (char *) x);\n" typ;
717       pr "  free (x);\n";
718       pr "}\n";
719       pr "\n";
720
721       pr "void\n";
722       pr "guestfs_free_%s_list (struct guestfs_%s_list *x)\n" typ typ;
723       pr "{\n";
724       pr "  xdr_free ((xdrproc_t) xdr_guestfs_int_%s_list, (char *) x);\n" typ;
725       pr "  free (x);\n";
726       pr "}\n";
727       pr "\n";
728
729   ) structs;
730
731 (* Generate the linker script which controls the visibility of
732  * symbols in the public ABI and ensures no other symbols get
733  * exported accidentally.
734  *)
735 and generate_linker_script () =
736   generate_header HashStyle GPLv2plus;
737
738   let globals = [
739     "guestfs_create";
740     "guestfs_close";
741     "guestfs_get_error_handler";
742     "guestfs_get_out_of_memory_handler";
743     "guestfs_get_private";
744     "guestfs_last_error";
745     "guestfs_set_close_callback";
746     "guestfs_set_error_handler";
747     "guestfs_set_launch_done_callback";
748     "guestfs_set_log_message_callback";
749     "guestfs_set_out_of_memory_handler";
750     "guestfs_set_private";
751     "guestfs_set_progress_callback";
752     "guestfs_set_subprocess_quit_callback";
753
754     (* Unofficial parts of the API: the bindings code use these
755      * functions, so it is useful to export them.
756      *)
757     "guestfs_safe_calloc";
758     "guestfs_safe_malloc";
759     "guestfs_safe_strdup";
760     "guestfs_safe_memdup";
761     "guestfs_tmpdir";
762   ] in
763   let functions =
764     List.map (fun (name, _, _, _, _, _, _) -> "guestfs_" ^ name)
765       all_functions in
766   let structs =
767     List.concat (
768       List.map (fun (typ, _) ->
769                   ["guestfs_free_" ^ typ; "guestfs_free_" ^ typ ^ "_list"])
770         structs
771     ) in
772   let globals = List.sort compare (globals @ functions @ structs) in
773
774   pr "{\n";
775   pr "    global:\n";
776   List.iter (pr "        %s;\n") globals;
777   pr "\n";
778
779   pr "    local:\n";
780   pr "        *;\n";
781   pr "};\n"
782
783 and generate_max_proc_nr () =
784   pr "%d\n" max_proc_nr