1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
|
(* libguestfs
* Copyright (C) 2009-2010 Red Hat Inc.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*)
(* Please read generator/README first. *)
open Printf
open Generator_types
open Generator_utils
open Generator_pr
open Generator_docstrings
open Generator_api_versions
open Generator_optgroups
open Generator_actions
open Generator_structs
(* Generate C API. *)
type optarg_proto = Dots | VA | Argv
(* Generate a C function prototype. *)
let rec generate_prototype ?(extern = true) ?(static = false)
?(semicolon = true)
?(single_line = false) ?(indent = "") ?(newline = false)
?(in_daemon = false)
?(prefix = "") ?(suffix = "")
?handle
?(optarg_proto = Dots)
name (ret, args, optargs) =
pr "%s" indent;
if extern then pr "extern ";
if static then pr "static ";
(match ret with
| RErr
| RInt _
| RBool _ ->
pr "int";
if single_line then pr " " else pr "\n%s" indent
| RInt64 _ ->
pr "int64_t";
if single_line then pr " " else pr "\n%s" indent
| RConstString _ | RConstOptString _ ->
pr "const char *";
if not single_line then pr "\n%s" indent
| RString _ | RBufferOut _ ->
pr "char *";
if not single_line then pr "\n%s" indent
| RStringList _ | RHashtable _ ->
pr "char **";
if not single_line then pr "\n%s" indent
| RStruct (_, typ) ->
if not in_daemon then pr "struct guestfs_%s *" typ
else pr "guestfs_int_%s *" typ;
if not single_line then pr "\n%s" indent
| RStructList (_, typ) ->
if not in_daemon then pr "struct guestfs_%s_list *" typ
else pr "guestfs_int_%s_list *" typ;
if not single_line then pr "\n%s" indent
);
let is_RBufferOut = match ret with RBufferOut _ -> true | _ -> false in
pr "%s%s%s (" prefix name suffix;
if handle = None && args = [] && optargs = [] && not is_RBufferOut then
pr "void"
else (
let comma = ref false in
(match handle with
| None -> ()
| Some handle -> pr "guestfs_h *%s" handle; comma := true
);
let next () =
if !comma then (
if single_line then pr ", "
else (
let namelen = String.length prefix + String.length name +
String.length suffix + 2 in
pr ",\n%s%s" indent (spaces namelen)
)
);
comma := true
in
List.iter (
function
| Pathname n
| Device n | Dev_or_Path n
| String n
| OptString n
| Key n ->
next ();
pr "const char *%s" n
| StringList n | DeviceList n ->
next ();
pr "char *const *%s" n
| Bool n -> next (); pr "int %s" n
| Int n -> next (); pr "int %s" n
| Int64 n -> next (); pr "int64_t %s" n
| FileIn n
| FileOut n ->
if not in_daemon then (next (); pr "const char *%s" n)
| BufferIn n ->
next ();
pr "const char *%s" n;
next ();
pr "size_t %s_size" n
| Pointer (t, n) ->
next ();
pr "%s %s" t n
) args;
if is_RBufferOut then (next (); pr "size_t *size_r");
if optargs <> [] then (
next ();
match optarg_proto with
| Dots -> pr "..."
| VA -> pr "va_list args"
| Argv -> pr "const struct guestfs_%s_argv *optargs" name
);
);
pr ")";
if semicolon then pr ";";
if newline then pr "\n"
(* Generate C call arguments, eg "(handle, foo, bar)" *)
and generate_c_call_args ?handle (ret, args, optargs) =
pr "(";
let comma = ref false in
let next () =
if !comma then pr ", ";
comma := true
in
(match handle with
| None -> ()
| Some handle -> pr "%s" handle; comma := true
);
List.iter (
function
| BufferIn n ->
next ();
pr "%s, %s_size" n n
| arg ->
next ();
pr "%s" (name_of_argt arg)
) args;
(* For RBufferOut calls, add implicit &size parameter. *)
(match ret with
| RBufferOut _ ->
next ();
pr "&size"
| _ -> ()
);
(* For calls with optional arguments, add implicit optargs parameter. *)
if optargs <> [] then (
next ();
pr "optargs"
);
pr ")"
(* Generate the pod documentation for the C API. *)
and generate_actions_pod () =
List.iter (
fun (shortname, (ret, args, optargs as style), _, flags, _, _, longdesc) ->
if not (List.mem NotInDocs flags) then (
let name = "guestfs_" ^ shortname in
pr "=head2 %s\n\n" name;
generate_prototype ~extern:false ~indent:" " ~handle:"g" name style;
pr "\n\n";
let uc_shortname = String.uppercase shortname in
if optargs <> [] then (
pr "You may supply a list of optional arguments to this call.\n";
pr "Use zero or more of the following pairs of parameters,\n";
pr "and terminate the list with C<-1> on its own.\n";
pr "See L</CALLS WITH OPTIONAL ARGUMENTS>.\n\n";
List.iter (
fun argt ->
let n = name_of_argt argt in
let uc_n = String.uppercase n in
pr " GUESTFS_%s_%s, " uc_shortname uc_n;
match argt with
| Bool n -> pr "int %s,\n" n
| Int n -> pr "int %s,\n" n
| Int64 n -> pr "int64_t %s,\n" n
| String n -> pr "const char *%s,\n" n
| _ -> assert false
) optargs;
pr "\n";
);
pr "%s\n\n" longdesc;
let ret, args, optargs = style in
(match ret with
| RErr ->
pr "This function returns 0 on success or -1 on error.\n\n"
| RInt _ ->
pr "On error this function returns -1.\n\n"
| RInt64 _ ->
pr "On error this function returns -1.\n\n"
| RBool _ ->
pr "This function returns a C truth value on success or -1 on error.\n\n"
| RConstString _ ->
pr "This function returns a string, or NULL on error.
The string is owned by the guest handle and must I<not> be freed.\n\n"
| RConstOptString _ ->
pr "This function returns a string which may be NULL.
There is no way to return an error from this function.
The string is owned by the guest handle and must I<not> be freed.\n\n"
| RString _ ->
pr "This function returns a string, or NULL on error.
I<The caller must free the returned string after use>.\n\n"
| RStringList _ ->
pr "This function returns a NULL-terminated array of strings
(like L<environ(3)>), or NULL if there was an error.
I<The caller must free the strings and the array after use>.\n\n"
| RStruct (_, typ) ->
pr "This function returns a C<struct guestfs_%s *>,
or NULL if there was an error.
I<The caller must call C<guestfs_free_%s> after use>.\n\n" typ typ
| RStructList (_, typ) ->
pr "This function returns a C<struct guestfs_%s_list *>,
or NULL if there was an error.
I<The caller must call C<guestfs_free_%s_list> after use>.\n\n" typ typ
| RHashtable _ ->
pr "This function returns a NULL-terminated array of
strings, or NULL if there was an error.
The array of strings will always have length C<2n+1>, where
C<n> keys and values alternate, followed by the trailing NULL entry.
I<The caller must free the strings and the array after use>.\n\n"
| RBufferOut _ ->
pr "This function returns a buffer, or NULL on error.
The size of the returned buffer is written to C<*size_r>.
I<The caller must free the returned buffer after use>.\n\n"
);
if List.mem Progress flags then
pr "%s\n\n" progress_message;
if List.mem ProtocolLimitWarning flags then
pr "%s\n\n" protocol_limit_warning;
if List.mem DangerWillRobinson flags then
pr "%s\n\n" danger_will_robinson;
if List.exists (function Key _ -> true | _ -> false) (args@optargs) then
pr "This function takes a key or passphrase parameter which
could contain sensitive material. Read the section
L</KEYS AND PASSPHRASES> for more information.\n\n";
(match deprecation_notice flags with
| None -> ()
| Some txt -> pr "%s\n\n" txt
);
(match lookup_api_version name with
| Some version -> pr "(Added in %s)\n\n" version
| None -> ()
);
(* Handling of optional argument variants. *)
if optargs <> [] then (
pr "=head2 %s_va\n\n" name;
generate_prototype ~extern:false ~indent:" " ~handle:"g"
~prefix:"guestfs_" ~suffix:"_va" ~optarg_proto:VA
shortname style;
pr "\n\n";
pr "This is the \"va_list variant\" of L</%s>.\n\n" name;
pr "See L</CALLS WITH OPTIONAL ARGUMENTS>.\n\n";
pr "=head2 %s_argv\n\n" name;
generate_prototype ~extern:false ~indent:" " ~handle:"g"
~prefix:"guestfs_" ~suffix:"_argv" ~optarg_proto:Argv
shortname style;
pr "\n\n";
pr "This is the \"argv variant\" of L</%s>.\n\n" name;
pr "See L</CALLS WITH OPTIONAL ARGUMENTS>.\n\n";
);
)
) all_functions_sorted
and generate_structs_pod () =
(* Structs documentation. *)
List.iter (
fun (typ, cols) ->
pr "=head2 guestfs_%s\n" typ;
pr "\n";
pr " struct guestfs_%s {\n" typ;
List.iter (
function
| name, FChar -> pr " char %s;\n" name
| name, FUInt32 -> pr " uint32_t %s;\n" name
| name, FInt32 -> pr " int32_t %s;\n" name
| name, (FUInt64|FBytes) -> pr " uint64_t %s;\n" name
| name, FInt64 -> pr " int64_t %s;\n" name
| name, FString -> pr " char *%s;\n" name
| name, FBuffer ->
pr " /* The next two fields describe a byte array. */\n";
pr " uint32_t %s_len;\n" name;
pr " char *%s;\n" name
| name, FUUID ->
pr " /* The next field is NOT nul-terminated, be careful when printing it: */\n";
pr " char %s[32];\n" name
| name, FOptPercent ->
pr " /* The next field is [0..100] or -1 meaning 'not present': */\n";
pr " float %s;\n" name
) cols;
pr " };\n";
pr " \n";
pr " struct guestfs_%s_list {\n" typ;
pr " uint32_t len; /* Number of elements in list. */\n";
pr " struct guestfs_%s *val; /* Elements. */\n" typ;
pr " };\n";
pr " \n";
pr " void guestfs_free_%s (struct guestfs_free_%s *);\n" typ typ;
pr " void guestfs_free_%s_list (struct guestfs_free_%s_list *);\n"
typ typ;
pr "\n"
) structs
and generate_availability_pod () =
(* Availability documentation. *)
pr "=over 4\n";
pr "\n";
List.iter (
fun (group, functions) ->
pr "=item B<%s>\n" group;
pr "\n";
pr "The following functions:\n";
List.iter (pr "L</guestfs_%s>\n") functions;
pr "\n"
) optgroups;
pr "=back\n";
pr "\n"
(* Generate the guestfs.h file. *)
and generate_guestfs_h () =
generate_header CStyle LGPLv2plus;
pr "\
/* ---------- IMPORTANT NOTE ----------
*
* All API documentation is in the manpage, 'guestfs(3)'.
* To read it, type: man 3 guestfs
* Or read it online here: http://libguestfs.org/guestfs.3.html
*
* Go and read it now, I'll be right here waiting for you
* when you come back.
*
* ------------------------------------
*/
#ifndef GUESTFS_H_
#define GUESTFS_H_
#ifdef __cplusplus
extern \"C\" {
#endif
#include <stddef.h>
#include <stdint.h>
#include <stdarg.h>
/* The handle. */
#ifndef GUESTFS_TYPEDEF_H
#define GUESTFS_TYPEDEF_H 1
typedef struct guestfs_h guestfs_h;
#endif
/* Connection management. */
extern guestfs_h *guestfs_create (void);
extern void guestfs_close (guestfs_h *g);
/* Error handling. */
extern const char *guestfs_last_error (guestfs_h *g);
#define LIBGUESTFS_HAVE_LAST_ERRNO 1
extern int guestfs_last_errno (guestfs_h *g);
#ifndef GUESTFS_TYPEDEF_ERROR_HANDLER_CB
#define GUESTFS_TYPEDEF_ERROR_HANDLER_CB 1
typedef void (*guestfs_error_handler_cb) (guestfs_h *g, void *opaque, const char *msg);
#endif
#ifndef GUESTFS_TYPEDEF_ABORT_CB
#define GUESTFS_TYPEDEF_ABORT_CB 1
typedef void (*guestfs_abort_cb) (void) __attribute__((__noreturn__));
#endif
extern void guestfs_set_error_handler (guestfs_h *g, guestfs_error_handler_cb cb, void *opaque);
extern guestfs_error_handler_cb guestfs_get_error_handler (guestfs_h *g, void **opaque_rtn);
extern void guestfs_set_out_of_memory_handler (guestfs_h *g, guestfs_abort_cb);
extern guestfs_abort_cb guestfs_get_out_of_memory_handler (guestfs_h *g);
/* Events. */
#ifndef GUESTFS_TYPEDEF_LOG_MESSAGE_CB
#define GUESTFS_TYPEDEF_LOG_MESSAGE_CB 1
typedef void (*guestfs_log_message_cb) (guestfs_h *g, void *opaque, char *buf, int len);
#endif
#ifndef GUESTFS_TYPEDEF_SUBPROCESS_QUIT_CB
#define GUESTFS_TYPEDEF_SUBPROCESS_QUIT_CB 1
typedef void (*guestfs_subprocess_quit_cb) (guestfs_h *g, void *opaque);
#endif
#ifndef GUESTFS_TYPEDEF_LAUNCH_DONE_CB
#define GUESTFS_TYPEDEF_LAUNCH_DONE_CB 1
typedef void (*guestfs_launch_done_cb) (guestfs_h *g, void *opaque);
#endif
#ifndef GUESTFS_TYPEDEF_CLOSE_CB
#define GUESTFS_TYPEDEF_CLOSE_CB 1
typedef void (*guestfs_close_cb) (guestfs_h *g, void *opaque);
#endif
#ifndef GUESTFS_TYPEDEF_PROGRESS_CB
#define GUESTFS_TYPEDEF_PROGRESS_CB 1
typedef void (*guestfs_progress_cb) (guestfs_h *g, void *opaque, int proc_nr, int serial, uint64_t position, uint64_t total);
#endif
extern void guestfs_set_log_message_callback (guestfs_h *g, guestfs_log_message_cb cb, void *opaque);
extern void guestfs_set_subprocess_quit_callback (guestfs_h *g, guestfs_subprocess_quit_cb cb, void *opaque);
extern void guestfs_set_launch_done_callback (guestfs_h *g, guestfs_launch_done_cb cb, void *opaque);
#define LIBGUESTFS_HAVE_SET_CLOSE_CALLBACK 1
extern void guestfs_set_close_callback (guestfs_h *g, guestfs_close_cb cb, void *opaque);
#define LIBGUESTFS_HAVE_SET_PROGRESS_CALLBACK 1
extern void guestfs_set_progress_callback (guestfs_h *g, guestfs_progress_cb cb, void *opaque);
/* Private data area. */
#define LIBGUESTFS_HAVE_SET_PRIVATE 1
extern void guestfs_set_private (guestfs_h *g, const char *key, void *data);
#define LIBGUESTFS_HAVE_GET_PRIVATE 1
extern void *guestfs_get_private (guestfs_h *g, const char *key);
/* Structures. */
";
(* The structures are carefully written to have exactly the same
* in-memory format as the XDR structures that we use on the wire to
* the daemon. The reason for creating copies of these structures
* here is just so we don't have to export the whole of
* guestfs_protocol.h (which includes much unrelated and
* XDR-dependent stuff that we don't want to be public, or required
* by clients).
*
* To reiterate, we will pass these structures to and from the client
* with a simple assignment or memcpy, so the format must be
* identical to what rpcgen / the RFC defines.
*)
(* Public structures. *)
List.iter (
fun (typ, cols) ->
pr "struct guestfs_%s {\n" typ;
List.iter (
function
| name, FChar -> pr " char %s;\n" name
| name, FString -> pr " char *%s;\n" name
| name, FBuffer ->
pr " uint32_t %s_len;\n" name;
pr " char *%s;\n" name
| name, FUUID -> pr " char %s[32]; /* this is NOT nul-terminated, be careful when printing */\n" name
| name, FUInt32 -> pr " uint32_t %s;\n" name
| name, FInt32 -> pr " int32_t %s;\n" name
| name, (FUInt64|FBytes) -> pr " uint64_t %s;\n" name
| name, FInt64 -> pr " int64_t %s;\n" name
| name, FOptPercent -> pr " float %s; /* [0..100] or -1 */\n" name
) cols;
pr "};\n";
pr "\n";
pr "struct guestfs_%s_list {\n" typ;
pr " uint32_t len;\n";
pr " struct guestfs_%s *val;\n" typ;
pr "};\n";
pr "\n";
pr "extern void guestfs_free_%s (struct guestfs_%s *);\n" typ typ;
pr "extern void guestfs_free_%s_list (struct guestfs_%s_list *);\n" typ typ;
pr "\n"
) structs;
pr "\
/* Actions. */
";
List.iter (
fun (shortname, (ret, args, optargs as style), _, flags, _, _, _) ->
let deprecated =
List.exists (function DeprecatedBy _ -> true | _ -> false) flags in
let test0 =
String.length shortname >= 5 && String.sub shortname 0 5 = "test0" in
let debug =
String.length shortname >= 5 && String.sub shortname 0 5 = "debug" in
if not deprecated && not test0 && not debug then
pr "#define LIBGUESTFS_HAVE_%s 1\n" (String.uppercase shortname);
generate_prototype ~single_line:true ~newline:true ~handle:"g"
~prefix:"guestfs_" shortname style;
if optargs <> [] then (
generate_prototype ~single_line:true ~newline:true ~handle:"g"
~prefix:"guestfs_" ~suffix:"_va" ~optarg_proto:VA
shortname style;
pr "struct guestfs_%s_argv {\n" shortname;
pr " uint64_t bitmask;\n";
iteri (
fun i argt ->
let c_type =
match argt with
| Bool n -> "int "
| Int n -> "int64_t "
| Int64 n -> "int "
| String n -> "const char *"
| _ -> assert false (* checked in generator_checks *) in
let uc_shortname = String.uppercase shortname in
let n = name_of_argt argt in
let uc_n = String.uppercase n in
pr "#define GUESTFS_%s_%s %d\n" uc_shortname uc_n i;
pr "#define GUESTFS_%s_%s_BITMASK (UINT64_C(1)<<%d)\n" uc_shortname uc_n i;
pr "/* The field below is only valid in this struct if the\n";
pr " * GUESTFS_%s_%s_BITMASK bit is set\n" uc_shortname uc_n;
pr " * in the bitmask above, otherwise the contents are ignored.\n";
pr " */\n";
pr " %s%s;\n" c_type n
) optargs;
pr "};\n";
generate_prototype ~single_line:true ~newline:true ~handle:"g"
~prefix:"guestfs_" ~suffix:"_argv" ~optarg_proto:Argv
shortname style;
);
) all_functions_sorted;
pr "\
/* Private functions.
*
* These are NOT part of the public, stable API, and can change at any
* time! We export them because they are used by some of the language
* bindings.
*/
extern void *guestfs_safe_malloc (guestfs_h *g, size_t nbytes);
extern void *guestfs_safe_calloc (guestfs_h *g, size_t n, size_t s);
extern const char *guestfs_tmpdir (void);
/* End of private functions. */
#ifdef __cplusplus
}
#endif
#endif /* GUESTFS_H_ */
"
(* Generate the guestfs-internal-actions.h file. *)
and generate_internal_actions_h () =
generate_header CStyle LGPLv2plus;
List.iter (
fun (shortname, style, _, _, _, _, _) ->
generate_prototype ~single_line:true ~newline:true ~handle:"g"
~prefix:"guestfs__" ~optarg_proto:Argv
shortname style
) non_daemon_functions
(* Generate the client-side dispatch stubs. *)
and generate_client_actions () =
generate_header CStyle LGPLv2plus;
pr "\
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <inttypes.h>
#include \"guestfs.h\"
#include \"guestfs-internal.h\"
#include \"guestfs-internal-actions.h\"
#include \"guestfs_protocol.h\"
#include \"errnostring.h\"
/* Check the return message from a call for validity. */
static int
check_reply_header (guestfs_h *g,
const struct guestfs_message_header *hdr,
unsigned int proc_nr, unsigned int serial)
{
if (hdr->prog != GUESTFS_PROGRAM) {
error (g, \"wrong program (%%d/%%d)\", hdr->prog, GUESTFS_PROGRAM);
return -1;
}
if (hdr->vers != GUESTFS_PROTOCOL_VERSION) {
error (g, \"wrong protocol version (%%d/%%d)\",
hdr->vers, GUESTFS_PROTOCOL_VERSION);
return -1;
}
if (hdr->direction != GUESTFS_DIRECTION_REPLY) {
error (g, \"unexpected message direction (%%d/%%d)\",
hdr->direction, GUESTFS_DIRECTION_REPLY);
return -1;
}
if (hdr->proc != proc_nr) {
error (g, \"unexpected procedure number (%%d/%%d)\", hdr->proc, proc_nr);
return -1;
}
if (hdr->serial != serial) {
error (g, \"unexpected serial (%%d/%%d)\", hdr->serial, serial);
return -1;
}
return 0;
}
/* Check we are in the right state to run a high-level action. */
static int
check_state (guestfs_h *g, const char *caller)
{
if (!guestfs__is_ready (g)) {
if (guestfs__is_config (g) || guestfs__is_launching (g))
error (g, \"%%s: call launch before using this function\\n(in guestfish, don't forget to use the 'run' command)\",
caller);
else
error (g, \"%%s called from the wrong state, %%d != READY\",
caller, guestfs__get_state (g));
return -1;
}
return 0;
}
";
let error_code_of = function
| RErr | RInt _ | RInt64 _ | RBool _ -> "-1"
| RConstString _ | RConstOptString _
| RString _ | RStringList _
| RStruct _ | RStructList _
| RHashtable _ | RBufferOut _ -> "NULL"
in
(* Generate code to check String-like parameters are not passed in
* as NULL (returning an error if they are).
*)
let check_null_strings shortname (ret, args, optargs) =
let pr_newline = ref false in
List.iter (
function
(* parameters which should not be NULL *)
| String n
| Device n
| Pathname n
| Dev_or_Path n
| FileIn n
| FileOut n
| BufferIn n
| StringList n
| DeviceList n
| Key n
| Pointer (_, n) ->
pr " if (%s == NULL) {\n" n;
pr " error (g, \"%%s: %%s: parameter cannot be NULL\",\n";
pr " \"%s\", \"%s\");\n" shortname n;
pr " return %s;\n" (error_code_of ret);
pr " }\n";
pr_newline := true
(* can be NULL *)
| OptString _
(* not applicable *)
| Bool _
| Int _
| Int64 _ -> ()
) args;
(* For optional arguments. *)
List.iter (
function
| String n ->
pr " if ((optargs->bitmask & GUESTFS_%s_%s_BITMASK) &&\n"
(String.uppercase shortname) (String.uppercase n);
pr " optargs->%s == NULL) {\n" n;
pr " error (g, \"%%s: %%s: optional parameter cannot be NULL\",\n";
pr " \"%s\", \"%s\");\n" shortname n;
pr " return %s;\n" (error_code_of ret);
pr " }\n";
pr_newline := true
(* not applicable *)
| Bool _ | Int _ | Int64 _ -> ()
| _ -> assert false
) optargs;
if !pr_newline then pr "\n";
in
(* Generate code to reject optargs we don't know about. *)
let reject_unknown_optargs shortname = function
| _, _, [] -> ()
| ret, _, optargs ->
let len = List.length optargs in
let mask = Int64.lognot (Int64.pred (Int64.shift_left 1L len)) in
pr " if (optargs->bitmask & UINT64_C(0x%Lx)) {\n" mask;
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";
pr " \"%s\", \"%s\");\n" shortname shortname;
pr " return %s;\n" (error_code_of ret);
pr " }\n";
pr "\n";
in
(* Generate code to generate guestfish call traces. *)
let trace_call shortname (ret, args, optargs) =
pr " if (trace_flag) {\n";
let needs_i =
List.exists (function
| StringList _ | DeviceList _ -> true
| _ -> false) args in
if needs_i then (
pr " size_t i;\n";
pr "\n"
);
pr " fprintf (stderr, \"%s\");\n" shortname;
(* Required arguments. *)
List.iter (
function
| String n (* strings *)
| Device n
| Pathname n
| Dev_or_Path n
| FileIn n
| FileOut n ->
(* guestfish doesn't support string escaping, so neither do we *)
pr " fprintf (stderr, \" \\\"%%s\\\"\", %s);\n" n
| Key n ->
(* don't print keys *)
pr " fprintf (stderr, \" \\\"***\\\"\");\n"
| OptString n -> (* string option *)
pr " if (%s) fprintf (stderr, \" \\\"%%s\\\"\", %s);\n" n n;
pr " else fprintf (stderr, \" null\");\n"
| StringList n
| DeviceList n -> (* string list *)
pr " fputc (' ', stderr);\n";
pr " fputc ('\"', stderr);\n";
pr " for (i = 0; %s[i]; ++i) {\n" n;
pr " if (i > 0) fputc (' ', stderr);\n";
pr " fputs (%s[i], stderr);\n" n;
pr " }\n";
pr " fputc ('\"', stderr);\n";
| Bool n -> (* boolean *)
pr " fputs (%s ? \" true\" : \" false\", stderr);\n" n
| Int n -> (* int *)
pr " fprintf (stderr, \" %%d\", %s);\n" n
| Int64 n ->
pr " fprintf (stderr, \" %%\" PRIi64, %s);\n" n
| BufferIn n -> (* RHBZ#646822 *)
pr " fputc (' ', stderr);\n";
pr " guestfs___print_BufferIn (stderr, %s, %s_size);\n" n n
| Pointer (t, n) ->
pr " fprintf (stderr, \" (%s)%%p\", %s);\n" t n
) args;
(* Optional arguments. *)
List.iter (
fun argt ->
let n = name_of_argt argt in
let uc_shortname = String.uppercase shortname in
let uc_n = String.uppercase n in
pr " if (optargs->bitmask & GUESTFS_%s_%s_BITMASK)\n"
uc_shortname uc_n;
(match argt with
| String n ->
pr " fprintf (stderr, \" \\\"%%s:%%s\\\"\", \"%s\", optargs->%s);\n" n n
| Bool n ->
pr " fprintf (stderr, \" \\\"%%s:%%s\\\"\", \"%s\", optargs->%s ? \"true\" : \"false\");\n" n n
| Int n ->
pr " fprintf (stderr, \" \\\"%%s:%%d\\\"\", \"%s\", optargs->%s);\n" n n
| Int64 n ->
pr " fprintf (stderr, \" \\\"%%s:%%\" PRIi64 \"\\\"\", \"%s\", optargs->%s);\n" n n
| _ -> assert false
);
) optargs;
pr " fputc ('\\n', stderr);\n";
pr " }\n";
pr "\n";
in
(* For non-daemon functions, generate a wrapper around each function. *)
List.iter (
fun (shortname, (_, _, optargs as style), _, _, _, _, _) ->
if optargs = [] then
generate_prototype ~extern:false ~semicolon:false ~newline:true
~handle:"g" ~prefix:"guestfs_"
shortname style
else
generate_prototype ~extern:false ~semicolon:false ~newline:true
~handle:"g" ~prefix:"guestfs_" ~suffix:"_argv" ~optarg_proto:Argv
shortname style;
pr "{\n";
pr " int trace_flag = g->trace;\n";
pr "\n";
check_null_strings shortname style;
reject_unknown_optargs shortname style;
trace_call shortname style;
pr " return guestfs__%s " shortname;
generate_c_call_args ~handle:"g" style;
pr ";\n";
pr "}\n";
pr "\n"
) non_daemon_functions;
(* Client-side stubs for each function. *)
List.iter (
fun (shortname, (ret, args, optargs as style), _, _, _, _, _) ->
if optargs <> [] then
failwithf "optargs not yet implemented for daemon functions";
let name = "guestfs_" ^ shortname in
let error_code = error_code_of ret in
(* Generate the action stub. *)
if optargs = [] then
generate_prototype ~extern:false ~semicolon:false ~newline:true
~handle:"g" name style
else
generate_prototype ~extern:false ~semicolon:false ~newline:true
~handle:"g" ~suffix:"_argv" ~optarg_proto:Argv name style;
pr "{\n";
(match args with
| [] -> ()
| _ -> pr " struct %s_args args;\n" name
);
pr " guestfs_message_header hdr;\n";
pr " guestfs_message_error err;\n";
let has_ret =
match ret with
| RErr -> false
| RConstString _ | RConstOptString _ ->
failwithf "RConstString|RConstOptString cannot be used by daemon functions"
| RInt _ | RInt64 _
| RBool _ | RString _ | RStringList _
| RStruct _ | RStructList _
| RHashtable _ | RBufferOut _ ->
pr " struct %s_ret ret;\n" name;
true in
pr " int serial;\n";
pr " int r;\n";
pr " int trace_flag = g->trace;\n";
pr "\n";
check_null_strings shortname style;
reject_unknown_optargs shortname style;
trace_call shortname style;
pr " if (check_state (g, \"%s\") == -1) return %s;\n"
shortname error_code;
pr " guestfs___set_busy (g);\n";
pr "\n";
(* Send the main header and arguments. *)
(match args with
| [] ->
pr " serial = guestfs___send (g, GUESTFS_PROC_%s, NULL, NULL);\n"
(String.uppercase shortname)
| args ->
List.iter (
function
| Pathname n | Device n | Dev_or_Path n | String n | Key n ->
pr " args.%s = (char *) %s;\n" n n
| OptString n ->
pr " args.%s = %s ? (char **) &%s : NULL;\n" n n n
| StringList n | DeviceList n ->
pr " args.%s.%s_val = (char **) %s;\n" n n n;
pr " for (args.%s.%s_len = 0; %s[args.%s.%s_len]; args.%s.%s_len++) ;\n" n n n n n n n;
| Bool n ->
pr " args.%s = %s;\n" n n
| Int n ->
pr " args.%s = %s;\n" n n
| Int64 n ->
pr " args.%s = %s;\n" n n
| FileIn _ | FileOut _ -> ()
| BufferIn n ->
pr " /* Just catch grossly large sizes. XDR encoding will make this precise. */\n";
pr " if (%s_size >= GUESTFS_MESSAGE_MAX) {\n" n;
pr " error (g, \"%%s: size of input buffer too large\", \"%s\");\n"
shortname;
pr " guestfs___end_busy (g);\n";
pr " return %s;\n" error_code;
pr " }\n";
pr " args.%s.%s_val = (char *) %s;\n" n n n;
pr " args.%s.%s_len = %s_size;\n" n n n
| Pointer _ -> assert false
) args;
pr " serial = guestfs___send (g, GUESTFS_PROC_%s,\n"
(String.uppercase shortname);
pr " (xdrproc_t) xdr_%s_args, (char *) &args);\n"
name;
);
pr " if (serial == -1) {\n";
pr " guestfs___end_busy (g);\n";
pr " return %s;\n" error_code;
pr " }\n";
pr "\n";
(* Send any additional files (FileIn) requested. *)
let need_read_reply_label = ref false in
List.iter (
function
| FileIn n ->
pr " r = guestfs___send_file (g, %s);\n" n;
pr " if (r == -1) {\n";
pr " guestfs___end_busy (g);\n";
pr " return %s;\n" error_code;
pr " }\n";
pr " if (r == -2) /* daemon cancelled */\n";
pr " goto read_reply;\n";
need_read_reply_label := true;
pr "\n";
| _ -> ()
) args;
(* Wait for the reply from the remote end. *)
if !need_read_reply_label then pr " read_reply:\n";
pr " memset (&hdr, 0, sizeof hdr);\n";
pr " memset (&err, 0, sizeof err);\n";
if has_ret then pr " memset (&ret, 0, sizeof ret);\n";
pr "\n";
pr " r = guestfs___recv (g, \"%s\", &hdr, &err,\n " shortname;
if not has_ret then
pr "NULL, NULL"
else
pr "(xdrproc_t) xdr_guestfs_%s_ret, (char *) &ret" shortname;
pr ");\n";
pr " if (r == -1) {\n";
pr " guestfs___end_busy (g);\n";
pr " return %s;\n" error_code;
pr " }\n";
pr "\n";
pr " if (check_reply_header (g, &hdr, GUESTFS_PROC_%s, serial) == -1) {\n"
(String.uppercase shortname);
pr " guestfs___end_busy (g);\n";
pr " return %s;\n" error_code;
pr " }\n";
pr "\n";
pr " if (hdr.status == GUESTFS_STATUS_ERROR) {\n";
pr " int errnum = 0;\n";
pr " if (err.errno_string[0] != '\\0')\n";
pr " errnum = guestfs___string_to_errno (err.errno_string);\n";
pr " if (errnum <= 0)\n";
pr " error (g, \"%%s: %%s\", \"%s\", err.error_message);\n"
shortname;
pr " else\n";
pr " guestfs_error_errno (g, errnum, \"%%s: %%s\", \"%s\",\n"
shortname;
pr " err.error_message);\n";
pr " free (err.error_message);\n";
pr " free (err.errno_string);\n";
pr " guestfs___end_busy (g);\n";
pr " return %s;\n" error_code;
pr " }\n";
pr "\n";
(* Expecting to receive further files (FileOut)? *)
List.iter (
function
| FileOut n ->
pr " if (guestfs___recv_file (g, %s) == -1) {\n" n;
pr " guestfs___end_busy (g);\n";
pr " return %s;\n" error_code;
pr " }\n";
pr "\n";
| _ -> ()
) args;
pr " guestfs___end_busy (g);\n";
(match ret with
| RErr -> pr " return 0;\n"
| RInt n | RInt64 n | RBool n ->
pr " return ret.%s;\n" n
| RConstString _ | RConstOptString _ ->
failwithf "RConstString|RConstOptString cannot be used by daemon functions"
| RString n ->
pr " return ret.%s; /* caller will free */\n" n
| RStringList n | RHashtable n ->
pr " /* caller will free this, but we need to add a NULL entry */\n";
pr " ret.%s.%s_val =\n" n n;
pr " safe_realloc (g, ret.%s.%s_val,\n" n n;
pr " sizeof (char *) * (ret.%s.%s_len + 1));\n"
n n;
pr " ret.%s.%s_val[ret.%s.%s_len] = NULL;\n" n n n n;
pr " return ret.%s.%s_val;\n" n n
| RStruct (n, _) ->
pr " /* caller will free this */\n";
pr " return safe_memdup (g, &ret.%s, sizeof (ret.%s));\n" n n
| RStructList (n, _) ->
pr " /* caller will free this */\n";
pr " return safe_memdup (g, &ret.%s, sizeof (ret.%s));\n" n n
| RBufferOut n ->
pr " /* RBufferOut is tricky: If the buffer is zero-length, then\n";
pr " * _val might be NULL here. To make the API saner for\n";
pr " * callers, we turn this case into a unique pointer (using\n";
pr " * malloc(1)).\n";
pr " */\n";
pr " if (ret.%s.%s_len > 0) {\n" n n;
pr " *size_r = ret.%s.%s_len;\n" n n;
pr " return ret.%s.%s_val; /* caller will free */\n" n n;
pr " } else {\n";
pr " free (ret.%s.%s_val);\n" n n;
pr " char *p = safe_malloc (g, 1);\n";
pr " *size_r = ret.%s.%s_len;\n" n n;
pr " return p;\n";
pr " }\n";
);
pr "}\n\n"
) daemon_functions;
(* Functions to free structures. *)
pr "/* Structure-freeing functions. These rely on the fact that the\n";
pr " * structure format is identical to the XDR format. See note in\n";
pr " * generator.ml.\n";
pr " */\n";
pr "\n";
List.iter (
fun (typ, _) ->
pr "void\n";
pr "guestfs_free_%s (struct guestfs_%s *x)\n" typ typ;
pr "{\n";
pr " xdr_free ((xdrproc_t) xdr_guestfs_int_%s, (char *) x);\n" typ;
pr " free (x);\n";
pr "}\n";
pr "\n";
pr "void\n";
pr "guestfs_free_%s_list (struct guestfs_%s_list *x)\n" typ typ;
pr "{\n";
pr " xdr_free ((xdrproc_t) xdr_guestfs_int_%s_list, (char *) x);\n" typ;
pr " free (x);\n";
pr "}\n";
pr "\n";
) structs;
(* Functions which have optional arguments have two generated variants. *)
List.iter (
function
| shortname, (ret, args, (_::_ as optargs) as style), _, _, _, _, _ ->
let uc_shortname = String.uppercase shortname in
(* Get the name of the last regular argument. *)
let last_arg =
match args with
| [] -> "g"
| args -> name_of_argt (List.hd (List.rev args)) in
let rerrcode, rtype =
match ret with
| RErr | RInt _ | RBool _ -> "-1", "int "
| RInt64 _ -> "-1", "int64_t "
| RConstString _ | RConstOptString _ -> "NULL", "const char *"
| RString _ | RBufferOut _ -> "NULL", "char *"
| RStringList _ | RHashtable _ -> "NULL", "char **"
| RStruct (_, typ) -> "NULL", sprintf "struct guestfs_%s *" typ
| RStructList (_, typ) ->
"NULL", sprintf "struct guestfs_%s_list *" typ in
(* The regular variable args function, just calls the _va variant. *)
generate_prototype ~extern:false ~semicolon:false ~newline:true
~handle:"g" ~prefix:"guestfs_" shortname style;
pr "{\n";
pr " va_list optargs;\n";
pr "\n";
pr " va_start (optargs, %s);\n" last_arg;
pr " %sr = guestfs_%s_va " rtype shortname;
generate_c_call_args ~handle:"g" style;
pr ";\n";
pr " va_end (optargs);\n";
pr "\n";
pr " return r;\n";
pr "}\n\n";
generate_prototype ~extern:false ~semicolon:false ~newline:true
~handle:"g" ~prefix:"guestfs_" ~suffix:"_va" ~optarg_proto:VA
shortname style;
pr "{\n";
pr " struct guestfs_%s_argv optargs_s;\n" shortname;
pr " struct guestfs_%s_argv *optargs = &optargs_s;\n" shortname;
pr " int i;\n";
pr "\n";
pr " optargs_s.bitmask = 0;\n";
pr "\n";
pr " while ((i = va_arg (args, int)) >= 0) {\n";
pr " switch (i) {\n";
List.iter (
fun argt ->
let n = name_of_argt argt in
let uc_n = String.uppercase n in
pr " case GUESTFS_%s_%s:\n" uc_shortname uc_n;
pr " optargs_s.%s = va_arg (args, " n;
(match argt with
| Bool _ | Int _ -> pr "int"
| Int64 _ -> pr "int64_t"
| String _ -> pr "const char *"
| _ -> assert false
);
pr ");\n";
pr " break;\n";
) optargs;
pr " default:\n";
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";
pr " \"%s\", i);\n" shortname;
pr " return %s;\n" rerrcode;
pr " }\n";
pr "\n";
pr " uint64_t i_mask = UINT64_C(1) << i;\n";
pr " if (optargs_s.bitmask & i_mask) {\n";
pr " error (g, \"%%s: same optional argument specified more than once\",\n";
pr " \"%s\");\n" shortname;
pr " return %s;\n" rerrcode;
pr " }\n";
pr " optargs_s.bitmask |= i_mask;\n";
pr " }\n";
pr "\n";
pr " return guestfs_%s_argv " shortname;
generate_c_call_args ~handle:"g" style;
pr ";\n";
pr "}\n\n"
| _ -> ()
) all_functions_sorted
(* Generate the linker script which controls the visibility of
* symbols in the public ABI and ensures no other symbols get
* exported accidentally.
*)
and generate_linker_script () =
generate_header HashStyle GPLv2plus;
let globals = [
"guestfs_create";
"guestfs_close";
"guestfs_get_error_handler";
"guestfs_get_out_of_memory_handler";
"guestfs_get_private";
"guestfs_last_errno";
"guestfs_last_error";
"guestfs_set_close_callback";
"guestfs_set_error_handler";
"guestfs_set_launch_done_callback";
"guestfs_set_log_message_callback";
"guestfs_set_out_of_memory_handler";
"guestfs_set_private";
"guestfs_set_progress_callback";
"guestfs_set_subprocess_quit_callback";
(* Unofficial parts of the API: the bindings code use these
* functions, so it is useful to export them.
*)
"guestfs_safe_calloc";
"guestfs_safe_malloc";
"guestfs_safe_strdup";
"guestfs_safe_memdup";
"guestfs_tmpdir";
] in
let functions =
List.flatten (
List.map (
function
| name, (_, _, []), _, _, _, _, _ -> ["guestfs_" ^ name]
| name, (_, _, _), _, _, _, _, _ ->
["guestfs_" ^ name;
"guestfs_" ^ name ^ "_va";
"guestfs_" ^ name ^ "_argv"]
) all_functions
) in
let structs =
List.concat (
List.map (fun (typ, _) ->
["guestfs_free_" ^ typ; "guestfs_free_" ^ typ ^ "_list"])
structs
) in
let globals = List.sort compare (globals @ functions @ structs) in
pr "{\n";
pr " global:\n";
List.iter (pr " %s;\n") globals;
pr "\n";
pr " local:\n";
pr " *;\n";
pr "};\n"
and generate_max_proc_nr () =
pr "%d\n" max_proc_nr
|