File: erlang.ml

package info (click to toggle)
libguestfs 1%3A1.54.1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 98,892 kB
  • sloc: ansic: 379,443; ml: 38,771; sh: 10,329; java: 9,631; cs: 6,377; haskell: 5,729; makefile: 5,178; python: 3,821; perl: 2,467; erlang: 2,461; ruby: 349; xml: 275; pascal: 257; javascript: 157; cpp: 10
file content (566 lines) | stat: -rw-r--r-- 17,869 bytes parent folder | download | duplicates (3)
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
(* libguestfs
 * Copyright (C) 2011 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 Std_utils
open Types
open Utils
open Pr
open Docstrings
open Optgroups
open Actions
open Structs
open C
open Events

let generate_header = generate_header ~inputs:["generator/erlang.ml"]

let rec generate_erlang_erl () =
  generate_header ErlangStyle LGPLv2plus;

  pr "-module(guestfs).\n";
  pr "\n";
  pr "-export([create/0, create/1, close/1, init/1]).\n";
  pr "\n";

  (* Export the public actions. *)
  List.iter (
    fun { name; style = _, args, optargs; non_c_aliases = aliases } ->
      let nr_args = List.length args in
      let export name =
        if optargs = [] then
          pr "-export([%s/%d]).\n" name (nr_args+1)
        else
          pr "-export([%s/%d, %s/%d]).\n" name (nr_args+1) name (nr_args+2)
      in
      export name;
      List.iter export aliases
  ) (actions |> external_functions |> sort);

  pr "\n";

  pr "\
create() ->
  create(\"erl-guestfs\").

create(ExtProg) ->
  G = spawn(?MODULE, init, [ExtProg]),
  {ok, G}.

close(G) ->
  G ! close,
  ok.

call_port(G, Args) ->
  G ! {call, self(), Args},
  receive
    {guestfs, Result} ->
      Result
  end.

init(ExtProg) ->
  process_flag(trap_exit, true),
  Port = open_port({spawn, ExtProg}, [{packet, 4}, binary]),
  loop(Port).
loop(Port) ->
  receive
    {call, Caller, Args} ->
      Port ! { self(), {command, term_to_binary(Args)}},
      receive
        {Port, {data, Result}} ->
          Caller ! { guestfs, binary_to_term(Result)}
      end,
      loop(Port);
    close ->
      port_close(Port),
      exit(normal);
    { 'EXIT', Port, _ } ->
      exit(port_terminated)
  end.

";

  (* These bindings just marshal the parameters and call the back-end
   * process which dispatches them to the port.
   *)
  List.iter (
    fun { name; style = _, args, optargs; non_c_aliases = aliases } ->
      pr "%s(G" name;
      List.iter (
        fun arg ->
          pr ", %s" (String.capitalize_ascii (name_of_argt arg))
      ) args;
      if optargs <> [] then
        pr ", Optargs";
      pr ") ->\n";

      pr "  call_port(G, {%s" name;
      List.iter (
        fun arg ->
          pr ", %s" (String.capitalize_ascii (name_of_argt arg))
      ) args;
      if optargs <> [] then
        pr ", Optargs";
      pr "}).\n";

      (* For functions with optional arguments, make a variant that
       * has no optarg array, which just calls the function above with
       * an empty list as the final arg.
       *)
      if optargs <> [] then (
        pr "%s(G" name;
        List.iter (
          fun arg ->
            pr ", %s" (String.capitalize_ascii (name_of_argt arg))
        ) args;
        pr ") ->\n";

        pr "  %s(G" name;
        List.iter (
          fun arg ->
            pr ", %s" (String.capitalize_ascii (name_of_argt arg))
        ) args;
        pr ", []";
        pr ").\n"
      );

      (* Aliases. *)
      List.iter (
        fun alias ->
          pr "%s(G" alias;
          List.iter (
            fun arg ->
              pr ", %s" (String.capitalize_ascii (name_of_argt arg))
          ) args;
          if optargs <> [] then
            pr ", Optargs";
          pr ") ->\n";

          pr "  %s(G" name;
          List.iter (
            fun arg ->
              pr ", %s" (String.capitalize_ascii (name_of_argt arg))
          ) args;
          if optargs <> [] then
            pr ", Optargs";
          pr ").\n";

          if optargs <> [] then (
            pr "%s(G" alias;
            List.iter (
              fun arg ->
                pr ", %s" (String.capitalize_ascii (name_of_argt arg))
            ) args;
            pr ") ->\n";

            pr "  %s(G" name;
            List.iter (
              fun arg ->
                pr ", %s" (String.capitalize_ascii (name_of_argt arg))
            ) args;
            pr ").\n"
          )
      ) aliases;

      pr "\n"
  ) (actions |> external_functions |> sort)

and generate_erlang_actions_h () =
  generate_header CStyle GPLv2plus;

  pr "\
#ifndef GUESTFS_ERLANG_ACTIONS_H_
#define GUESTFS_ERLANG_ACTIONS_H_

extern guestfs_h *g;

extern int dispatch (ei_x_buff *retbuff, const char *buff, int *index);
extern int make_error (ei_x_buff *retbuff, const char *funname);
extern int unknown_optarg (ei_x_buff *retbuff, const char *funname, const char *optargname);
extern int unknown_function (ei_x_buff *retbuff, const char *fun);
extern int make_string_list (ei_x_buff *buff, char **r);
extern int make_table (ei_x_buff *buff, char **r);
extern int make_bool (ei_x_buff *buff, int r);
extern int atom_equals (const char *atom, const char *name);
extern int decode_string_list (const char *buff, int *index, char ***res);
extern int decode_string (const char *buff, int *index, char **res);
extern int decode_binary (const char *buff, int *index, char **res, size_t *size);
extern int decode_bool (const char *buff, int *index, int *res);
extern int decode_int (const char *buff, int *index, int *res);
extern int decode_int64 (const char *buff, int *index, int64_t *res);

";

  let emit_copy_list_decl typ =
    pr "int make_%s_list (ei_x_buff *buff, const struct guestfs_%s_list *%ss);\n"
       typ typ typ;
  in
  List.iter (
    fun { s_name = typ; s_cols = cols } ->
      pr "int make_%s (ei_x_buff *buff, const struct guestfs_%s *%s);\n" typ typ typ;
  ) external_structs;

  List.iter (
    function
    | typ, (RStructListOnly | RStructAndList) ->
        emit_copy_list_decl typ
    | typ, _ -> () (* empty *)
  ) (rstructs_used_by (actions |> external_functions));

  pr "\n";

  List.iter (
    fun { name } ->
      pr "int run_%s (ei_x_buff *retbuff, const char *buff, int *index);\n" name
  ) (actions |> external_functions |> sort);

  pr "\n";
  pr "#endif /* GUESTFS_ERLANG_ACTIONS_H_ */\n"

and generate_erlang_structs () =
  generate_header CStyle GPLv2plus;

  pr "\
#include <config.h>

#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <errno.h>

#include <ei.h>

#include \"guestfs.h\"
#include \"guestfs-utils.h\"

#include \"actions.h\"
";

  (* Struct copy functions. *)
  let emit_copy_list_function typ =
    pr "\n";
    pr "int\n";
    pr "make_%s_list (ei_x_buff *buff, const struct guestfs_%s_list *%ss)\n" typ typ typ;
    pr "{\n";
    pr "  size_t len = %ss->len;\n" typ;
    pr "  size_t i;\n";
    pr "\n";
    pr "  if (ei_x_encode_list_header (buff, len) != 0) return -1;\n";
    pr "  for (i = 0; i < len; ++i) {\n";
    pr "    if (make_%s (buff, &%ss->val[i]) != 0) return -1;\n" typ typ;
    pr "  }\n";
    pr "  if (len > 0)\n";
    pr "    if (ei_x_encode_empty_list (buff) != 0) return -1;\n";
    pr "\n";
    pr "  return 0;\n";
    pr "}\n";
  in

  List.iter (
    fun { s_name = typ; s_cols = cols } ->
      pr "\n";
      pr "int\n";
      pr "make_%s (ei_x_buff *buff, const struct guestfs_%s *%s)\n" typ typ typ;
      pr "{\n";
      pr "  if (ei_x_encode_list_header (buff, %d) !=0) return -1;\n" (List.length cols);
      pr "\n";
      List.iteri (
        fun i col ->
          (match col with
           | name, FString ->
               pr "  if (ei_x_encode_string (buff, %s->%s) != 0) return -1;\n" typ name
           | name, FBuffer ->
               pr "  if (ei_x_encode_string_len (buff, %s->%s, %s->%s_len) != 0) return -1;\n"
                 typ name typ name
           | name, FUUID ->
               pr "  if (ei_x_encode_string_len (buff, %s->%s, 32) != 0) return -1;\n" typ name
           | name, (FBytes|FInt64|FUInt64) ->
               pr "  if (ei_x_encode_longlong (buff, %s->%s) != 0) return -1;\n" typ name
           | name, (FInt32|FUInt32) ->
               pr "  if (ei_x_encode_long (buff, %s->%s) != 0) return -1;\n" typ name
           | name, FOptPercent ->
               pr "  if (%s->%s >= 0) {\n" typ name;
               pr "    if (ei_x_encode_double (buff, %s->%s) != 0) return -1;\n" typ name;
               pr "  } else {\n";
               pr "    if (ei_x_encode_atom (buff, \"undefined\") != 0) return -1;\n";
               pr "  }\n"
           | name, FChar ->
               pr "  if (ei_x_encode_char (buff, %s->%s) != 0) return -1;\n" typ name
          );
      ) cols;
      if cols <> [] then (
        pr "\n";
        pr "  if (ei_x_encode_empty_list (buff) != 0) return -1;\n"
      );
      pr "\n";
      pr "  return 0;\n";
      pr "}\n";
  ) external_structs;

  (* Emit a copy_TYPE_list function definition only if that function is used. *)
  List.iter (
    function
    | typ, (RStructListOnly | RStructAndList) ->
        (* generate the function for typ *)
        emit_copy_list_function typ
    | typ, _ -> () (* empty *)
  ) (rstructs_used_by (actions |> external_functions));

and generate_erlang_actions actions () =
  generate_header CStyle GPLv2plus;

  pr "\
#include <config.h>

/* It is safe to call deprecated functions from this file. */
#define GUESTFS_NO_WARN_DEPRECATED
#undef GUESTFS_NO_DEPRECATED

#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <errno.h>

#include <ei.h>

#include \"guestfs.h\"
#include \"guestfs-utils.h\"

#include \"actions.h\"
";

  (* The wrapper functions. *)
  List.iter (
    fun { name; style = (ret, args, optargs as style);
          c_function; c_optarg_prefix } ->
      pr "\n";
      pr "int\n";
      pr "run_%s (ei_x_buff *retbuff, const char *buff, int *idx)\n" name;
      pr "{\n";

      List.iteri (
        fun i ->
          function
          | String (_, n) ->
            pr "  CLEANUP_FREE char *%s;\n" n;
            pr "  if (decode_string (buff, idx, &%s) != 0) return -1;\n" n
          | OptString n ->
            pr "  CLEANUP_FREE char *%s;\n" n;
            pr "  char %s_opt[MAXATOMLEN];\n" n;
            pr "  if (ei_decode_atom(buff, idx, %s_opt) == 0) {\n" n;
            pr "    if (atom_equals (%s_opt, \"undefined\"))\n" n;
            pr "      %s = NULL;\n" n;
            pr "    else\n";
            pr "      %s = %s_opt;\n" n n;
            pr "  } else {\n";
            pr "    if (decode_string (buff, idx, &%s) != 0) return -1;\n" n;
            pr "  }\n"
          | BufferIn n ->
            pr "  CLEANUP_FREE char *%s;\n" n;
            pr "  size_t %s_size;\n" n;
            pr "  if (decode_binary (buff, idx, &%s, &%s_size) != 0) return -1;\n" n n
          | StringList (_, n) ->
            pr "  CLEANUP_FREE_STRING_LIST char **%s;\n" n;
            pr "  if (decode_string_list (buff, idx, &%s) != 0) return -1;\n" n
          | Bool n ->
            pr "  int %s;\n" n;
            pr "  if (decode_bool (buff, idx, &%s) != 0) return -1;\n" n
          | Int n ->
            pr "  int %s;\n" n;
            pr "  if (decode_int (buff, idx, &%s) != 0) return -1;\n" n
          | Int64 n ->
            pr "  int64_t %s;\n" n;
            pr "  if (decode_int64 (buff, idx, &%s) != 0) return -1;\n" n
          | Pointer (t, n) ->
            pr "  void * /* %s */ %s = POINTER_NOT_IMPLEMENTED (\"%s\");\n" t n t
      ) args;

      (* Optional arguments. *)
      if optargs <> [] then (
        pr "\n";
        pr "  struct %s optargs_s = { .bitmask = 0 };\n" c_function;
        pr "  struct %s *optargs = &optargs_s;\n" c_function;
        pr "  int optargsize;\n";
        pr "  if (ei_decode_list_header (buff, idx, &optargsize) != 0) return -1;\n";
        pr "  for (int i = 0; i < optargsize; i++) {\n";
        pr "    int hd;\n";
        pr "    if (ei_decode_tuple_header (buff, idx, &hd) != 0) return -1;\n";
        pr "    char hd_name[MAXATOMLEN];\n";
        pr "    if (ei_decode_atom (buff, idx, hd_name) != 0) return -1;\n";
        pr "\n";
        List.iter (
          fun argt ->
            let n = name_of_optargt argt in
            let uc_n = String.uppercase_ascii n in
            pr "    if (atom_equals (hd_name, \"%s\")) {\n" n;
            pr "      optargs_s.bitmask |= %s_%s_BITMASK;\n"
              c_optarg_prefix uc_n;
            pr "      ";
            (match argt with
             | OBool _ -> pr "if (decode_bool (buff, idx, &optargs_s.%s) != 0) return -1;" n
             | OInt _ -> pr "if (decode_int (buff, idx, &optargs_s.%s) != 0) return -1" n
             | OInt64 _ -> pr "if (decode_int64 (buff, idx, &optargs_s.%s) != 0) return -1" n
             | OString _ -> pr "if (decode_string (buff, idx, (char **) &optargs_s.%s) != 0) return -1" n
             | OStringList n -> pr "if (decode_string_list (buff, idx, (char ***) &optargs_s.%s) != 0) return -1" n
            );
            pr ";\n";
            pr "    }\n";
            pr "    else\n";
        ) optargs;
        pr "      return unknown_optarg (retbuff, \"%s\", hd_name);\n" name;
        pr "  }\n";
        pr "  if (optargsize > 0 && buff[*idx] == ERL_NIL_EXT)\n";
        pr "    (*idx)++;\n";
        pr "\n";
      );

      (match ret with
       | RErr -> pr "  int r;\n"
       | RInt _ -> pr "  int r;\n"
       | RInt64 _ -> pr "  int64_t r;\n"
       | RBool _ -> pr "  int r;\n"
       | RConstString _ | RConstOptString _ ->
           pr "  const char *r;\n"
       | RString _ -> pr "  char *r;\n"
       | RStringList _ ->
           pr "  char **r;\n"
       | RStruct (_, typ) ->
           pr "  struct guestfs_%s *r;\n" typ
       | RStructList (_, typ) ->
           pr "  struct guestfs_%s_list *r;\n" typ
       | RHashtable _ ->
           pr "  char **r;\n"
       | RBufferOut _ ->
           pr "  char *r;\n";
           pr "  size_t size;\n"
      );
      pr "\n";

      pr "  r = %s " c_function;
      generate_c_call_args ~handle:"g" style;
      pr ";\n";

      (* Free strings if we copied them above. *)
      List.iter (
        function
        | OBool _ | OInt _ | OInt64 _ -> ()
        | OString n ->
            let uc_n = String.uppercase_ascii n in
            pr "  if ((optargs_s.bitmask & %s_%s_BITMASK))\n"
              c_optarg_prefix uc_n;
            pr "    free ((char *) optargs_s.%s);\n" n
        | OStringList n ->
            let uc_n = String.uppercase_ascii n in
            pr "  if ((optargs_s.bitmask & %s_%s_BITMASK))\n"
              c_optarg_prefix uc_n;
            pr "    guestfs_int_free_string_list ((char **) optargs_s.%s);\n" n
      ) optargs;

      (match errcode_of_ret ret with
       | `CannotReturnError -> ()
       | `ErrorIsMinusOne ->
           pr "  if (r == -1)\n";
           pr "    return make_error (retbuff, \"%s\");\n" name;
       | `ErrorIsNULL ->
           pr "  if (r == NULL)\n";
           pr "    return make_error (retbuff, \"%s\");\n" name;
      );
      pr "\n";

      (match ret with
       | RErr -> pr "  if (ei_x_encode_atom (retbuff, \"ok\") != 0) return -1;\n"
       | RInt _ -> pr "  if (ei_x_encode_long (retbuff, r) != 0) return -1;\n"
       | RInt64 _ -> pr "  if (ei_x_encode_longlong (retbuff, r) != 0) return -1;\n"
       | RBool _ -> pr "  if (make_bool (retbuff, r) != 0) return -1;\n"
       | RConstString _ -> pr "  if (ei_x_encode_string (retbuff, r) != 0) return -1;\n"
       | RConstOptString _ ->
           pr "  if (r) {\n";
           pr "    if (ei_x_encode_string (retbuff, r) != 0) return -1;\n";
           pr "  } else {\n";
           pr "    if (ei_x_encode_atom (retbuff, \"undefined\") != 0) return -1;\n";
           pr "  }\n"
       | RString _ ->
           pr "  if (ei_x_encode_string (retbuff, r) != 0) return -1;\n";
           pr "  free (r);\n";
       | RStringList _ ->
           pr "  if (make_string_list (retbuff, r) != 0) return -1;\n";
           pr "  guestfs_int_free_string_list (r);\n"
       | RStruct (_, typ) ->
           pr "  if (make_%s (retbuff, r) != 0) return -1;\n" typ;
           pr "  guestfs_free_%s (r);\n" typ
       | RStructList (_, typ) ->
           pr "  if (make_%s_list (retbuff, r) != 0) return -1;\n" typ;
           pr "  guestfs_free_%s_list (r);\n" typ
       | RHashtable _ ->
           pr "  if (make_table (retbuff, r) != 0) return -1;\n";
           pr "  guestfs_int_free_string_list (r);\n"
       | RBufferOut _ ->
           pr "  if (ei_x_encode_binary (retbuff, r, size) != 0) return -1;\n";
           pr "  free (r);\n";
      );

      pr "  return 0;\n";
      pr "}\n";
  ) (actions |> external_functions |> sort);

and generate_erlang_dispatch () =
  generate_header CStyle GPLv2plus;

  pr "\
#include <config.h>

#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <errno.h>

#include <ei.h>

#include \"guestfs.h\"
#include \"guestfs-utils.h\"

#include \"actions.h\"

int
dispatch (ei_x_buff *retbuff, const char *buff, int *index)
{
  int arity;
  char fun[MAXATOMLEN];

  if (ei_decode_tuple_header (buff, index, &arity) != 0) return -1;
  if (ei_decode_atom (buff, index, fun) != 0) return -1;

  /* XXX We should use gperf here. */
  ";

  List.iter (
    fun { name; style = ret, args, optargs } ->
      pr "if (atom_equals (fun, \"%s\"))\n" name;
      pr "    return run_%s (retbuff, buff, index);\n" name;
      pr "  else ";
  ) (actions |> external_functions |> sort);

  pr "return unknown_function (retbuff, fun);
}
";