ocaml: Document g#close () method for objects.
[libguestfs.git] / generator / generator_php.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 open Generator_c
31
32 let rec generate_php_h () =
33   generate_header CStyle LGPLv2plus;
34
35   pr "\
36 #ifndef PHP_GUESTFS_PHP_H
37 #define PHP_GUESTFS_PHP_H 1
38
39 #ifdef ZTS
40 #include \"TSRM.h\"
41 #endif
42
43 #define PHP_GUESTFS_PHP_EXTNAME \"guestfs_php\"
44 #define PHP_GUESTFS_PHP_VERSION \"1.0\"
45
46 PHP_MINIT_FUNCTION (guestfs_php);
47
48 #define PHP_GUESTFS_HANDLE_RES_NAME \"guestfs_h\"
49
50 PHP_FUNCTION (guestfs_create);
51 PHP_FUNCTION (guestfs_last_error);
52 ";
53
54   List.iter (
55     fun (shortname, style, _, _, _, _, _) ->
56       pr "PHP_FUNCTION (guestfs_%s);\n" shortname
57   ) all_functions_sorted;
58
59   pr "\
60
61 extern zend_module_entry guestfs_php_module_entry;
62 #define phpext_guestfs_php_ptr &guestfs_php_module_entry
63
64 #endif /* PHP_GUESTFS_PHP_H */
65 "
66
67 and generate_php_c () =
68   generate_header CStyle LGPLv2plus;
69
70   pr "\
71 /* NOTE: Be very careful with all macros in PHP header files.  The
72  * morons who wrote them aren't good at making them safe for inclusion
73  * in arbitrary places in C code, eg. not using 'do ... while(0)'
74  * or parenthesizing any of the arguments.
75  */
76
77 /* NOTE (2): Some parts of the API can't be used on 32 bit platforms.
78  * Any 64 bit numbers will be truncated.  There's no easy way around
79  * this in PHP.
80  */
81
82 #include <config.h>
83
84 #include <stdio.h>
85 #include <stdlib.h>
86
87 #include <php.h>
88 #include <php_guestfs_php.h>
89
90 #include \"guestfs.h\"
91
92 static int res_guestfs_h;
93
94 static void
95 guestfs_php_handle_dtor (zend_rsrc_list_entry *rsrc TSRMLS_DC)
96 {
97   guestfs_h *g = (guestfs_h *) rsrc->ptr;
98   if (g != NULL)
99     guestfs_close (g);
100 }
101
102 PHP_MINIT_FUNCTION (guestfs_php)
103 {
104   res_guestfs_h =
105     zend_register_list_destructors_ex (guestfs_php_handle_dtor,
106     NULL, PHP_GUESTFS_HANDLE_RES_NAME, module_number);
107 }
108
109 static function_entry guestfs_php_functions[] = {
110   PHP_FE (guestfs_create, NULL)
111   PHP_FE (guestfs_last_error, NULL)
112 ";
113
114   List.iter (
115     fun (shortname, style, _, _, _, _, _) ->
116       pr "  PHP_FE (guestfs_%s, NULL)\n" shortname
117   ) all_functions_sorted;
118
119   pr "  { NULL, NULL, NULL }
120 };
121
122 zend_module_entry guestfs_php_module_entry = {
123 #if ZEND_MODULE_API_NO >= 20010901
124   STANDARD_MODULE_HEADER,
125 #endif
126   PHP_GUESTFS_PHP_EXTNAME,
127   guestfs_php_functions,
128   PHP_MINIT (guestfs_php),
129   NULL,
130   NULL,
131   NULL,
132   NULL,
133 #if ZEND_MODULE_API_NO >= 20010901
134   PHP_GUESTFS_PHP_VERSION,
135 #endif
136   STANDARD_MODULE_PROPERTIES
137 };
138
139 #ifdef COMPILE_DL_GUESTFS_PHP
140 ZEND_GET_MODULE (guestfs_php)
141 #endif
142
143 PHP_FUNCTION (guestfs_create)
144 {
145   guestfs_h *g = guestfs_create ();
146   if (g == NULL) {
147     RETURN_FALSE;
148   }
149
150   guestfs_set_error_handler (g, NULL, NULL);
151
152   ZEND_REGISTER_RESOURCE (return_value, g, res_guestfs_h);
153 }
154
155 PHP_FUNCTION (guestfs_last_error)
156 {
157   zval *z_g;
158   guestfs_h *g;
159
160   if (zend_parse_parameters (ZEND_NUM_ARGS() TSRMLS_CC, \"r\",
161                              &z_g) == FAILURE) {
162     RETURN_FALSE;
163   }
164
165   ZEND_FETCH_RESOURCE (g, guestfs_h *, &z_g, -1, PHP_GUESTFS_HANDLE_RES_NAME,
166                        res_guestfs_h);
167   if (g == NULL) {
168     RETURN_FALSE;
169   }
170
171   const char *err = guestfs_last_error (g);
172   if (err) {
173     RETURN_STRING (err, 1);
174   } else {
175     RETURN_NULL ();
176   }
177 }
178
179 ";
180
181   (* Now generate the PHP bindings for each action. *)
182   List.iter (
183     fun (shortname, style, _, _, _, _, _) ->
184       pr "PHP_FUNCTION (guestfs_%s)\n" shortname;
185       pr "{\n";
186       pr "  zval *z_g;\n";
187       pr "  guestfs_h *g;\n";
188
189       List.iter (
190         function
191         | String n | Device n | Pathname n | Dev_or_Path n
192         | FileIn n | FileOut n | Key n
193         | OptString n
194         | BufferIn n ->
195             pr "  char *%s;\n" n;
196             pr "  int %s_size;\n" n
197         | StringList n
198         | DeviceList n ->
199             pr "  zval *z_%s;\n" n;
200             pr "  char **%s;\n" n;
201         | Bool n ->
202             pr "  zend_bool %s;\n" n
203         | Int n | Int64 n ->
204             pr "  long %s;\n" n
205         ) (snd style);
206
207       pr "\n";
208
209       (* Parse the parameters. *)
210       let param_string = String.concat "" (
211         List.map (
212           function
213           | String n | Device n | Pathname n | Dev_or_Path n
214           | FileIn n | FileOut n | BufferIn n | Key n -> "s"
215           | OptString n -> "s!"
216           | StringList n | DeviceList n -> "a"
217           | Bool n -> "b"
218           | Int n | Int64 n -> "l"
219         ) (snd style)
220       ) in
221
222       pr "  if (zend_parse_parameters (ZEND_NUM_ARGS() TSRMLS_CC, \"r%s\",\n"
223         param_string;
224       pr "        &z_g";
225       List.iter (
226         function
227         | String n | Device n | Pathname n | Dev_or_Path n
228         | FileIn n | FileOut n | BufferIn n | Key n
229         | OptString n ->
230             pr ", &%s, &%s_size" n n
231         | StringList n | DeviceList n ->
232             pr ", &z_%s" n
233         | Bool n ->
234             pr ", &%s" n
235         | Int n | Int64 n ->
236             pr ", &%s" n
237       ) (snd style);
238       pr ") == FAILURE) {\n";
239       pr "    RETURN_FALSE;\n";
240       pr "  }\n";
241       pr "\n";
242       pr "  ZEND_FETCH_RESOURCE (g, guestfs_h *, &z_g, -1, PHP_GUESTFS_HANDLE_RES_NAME,\n";
243       pr "                       res_guestfs_h);\n";
244       pr "  if (g == NULL) {\n";
245       pr "    RETURN_FALSE;\n";
246       pr "  }\n";
247       pr "\n";
248
249       List.iter (
250         function
251         | String n | Device n | Pathname n | Dev_or_Path n
252         | FileIn n | FileOut n | Key n
253         | OptString n ->
254             (* Just need to check the string doesn't contain any ASCII
255              * NUL characters, which won't be supported by the C API.
256              *)
257             pr "  if (strlen (%s) != %s_size) {\n" n n;
258             pr "    fprintf (stderr, \"libguestfs: %s: parameter '%s' contains embedded ASCII NUL.\\n\");\n" shortname n;
259             pr "    RETURN_FALSE;\n";
260             pr "  }\n";
261             pr "\n"
262         | BufferIn n -> ()
263         | StringList n
264         | DeviceList n ->
265             (* Convert array to list of strings.
266              * http://marc.info/?l=pecl-dev&m=112205192100631&w=2
267              *)
268             pr "  {\n";
269             pr "    HashTable *a;\n";
270             pr "    int n;\n";
271             pr "    HashPosition p;\n";
272             pr "    zval **d;\n";
273             pr "    size_t c = 0;\n";
274             pr "\n";
275             pr "    a = Z_ARRVAL_P (z_%s);\n" n;
276             pr "    n = zend_hash_num_elements (a);\n";
277             pr "    %s = safe_emalloc (n + 1, sizeof (char *), 0);\n" n;
278             pr "    for (zend_hash_internal_pointer_reset_ex (a, &p);\n";
279             pr "         zend_hash_get_current_data_ex (a, (void **) &d, &p) == SUCCESS;\n";
280             pr "         zend_hash_move_forward_ex (a, &p)) {\n";
281             pr "      zval t = **d;\n";
282             pr "      zval_copy_ctor (&t);\n";
283             pr "      convert_to_string (&t);\n";
284             pr "      %s[c] = Z_STRVAL (t);\n" n;
285             pr "      c++;\n";
286             pr "    }\n";
287             pr "    %s[c] = NULL;\n" n;
288             pr "  }\n";
289             pr "\n"
290         | Bool n | Int n | Int64 n -> ()
291         ) (snd style);
292
293       (* Return value. *)
294       let error_code =
295         match fst style with
296         | RErr -> pr "  int r;\n"; "-1"
297         | RBool _
298         | RInt _ -> pr "  int r;\n"; "-1"
299         | RInt64 _ -> pr "  int64_t r;\n"; "-1"
300         | RConstString _ -> pr "  const char *r;\n"; "NULL"
301         | RConstOptString _ -> pr "  const char *r;\n"; "NULL"
302         | RString _ ->
303             pr "  char *r;\n"; "NULL"
304         | RStringList _ ->
305             pr "  char **r;\n"; "NULL"
306         | RStruct (_, typ) ->
307             pr "  struct guestfs_%s *r;\n" typ; "NULL"
308         | RStructList (_, typ) ->
309             pr "  struct guestfs_%s_list *r;\n" typ; "NULL"
310         | RHashtable _ ->
311             pr "  char **r;\n"; "NULL"
312         | RBufferOut _ ->
313             pr "  char *r;\n";
314             pr "  size_t size;\n";
315             "NULL" in
316
317       (* Call the function. *)
318       pr "  r = guestfs_%s " shortname;
319       generate_c_call_args ~handle:"g" style;
320       pr ";\n";
321       pr "\n";
322
323       (* Free up parameters. *)
324       List.iter (
325         function
326         | String n | Device n | Pathname n | Dev_or_Path n
327         | FileIn n | FileOut n | Key n
328         | OptString n -> ()
329         | BufferIn n -> ()
330         | StringList n
331         | DeviceList n ->
332             pr "  {\n";
333             pr "    size_t c = 0;\n";
334             pr "\n";
335             pr "    for (c = 0; %s[c] != NULL; ++c)\n" n;
336             pr "      efree (%s[c]);\n" n;
337             pr "    efree (%s);\n" n;
338             pr "  }\n";
339             pr "\n"
340         | Bool n | Int n | Int64 n -> ()
341         ) (snd style);
342
343       (* Check for errors. *)
344       pr "  if (r == %s) {\n" error_code;
345       pr "    RETURN_FALSE;\n";
346       pr "  }\n";
347       pr "\n";
348
349       (* Convert the return value. *)
350       (match fst style with
351        | RErr ->
352            pr "  RETURN_TRUE;\n"
353        | RBool _ ->
354            pr "  RETURN_BOOL (r);\n"
355        | RInt _ ->
356            pr "  RETURN_LONG (r);\n"
357        | RInt64 _ ->
358            pr "  RETURN_LONG (r);\n"
359        | RConstString _ ->
360            pr "  RETURN_STRING (r, 1);\n"
361        | RConstOptString _ ->
362            pr "  if (r) { RETURN_STRING (r, 1); }\n";
363            pr "  else { RETURN_NULL (); }\n"
364        | RString _ ->
365            pr "  char *r_copy = estrdup (r);\n";
366            pr "  free (r);\n";
367            pr "  RETURN_STRING (r_copy, 0);\n"
368        | RBufferOut _ ->
369            pr "  char *r_copy = estrndup (r, size);\n";
370            pr "  free (r);\n";
371            pr "  RETURN_STRING (r_copy, 0);\n"
372        | RStringList _ ->
373            pr "  size_t c = 0;\n";
374            pr "  array_init (return_value);\n";
375            pr "  for (c = 0; r[c] != NULL; ++c) {\n";
376            pr "    add_next_index_string (return_value, r[c], 1);\n";
377            pr "    free (r[c]);\n";
378            pr "  }\n";
379            pr "  free (r);\n";
380        | RHashtable _ ->
381            pr "  size_t c = 0;\n";
382            pr "  array_init (return_value);\n";
383            pr "  for (c = 0; r[c] != NULL; c += 2) {\n";
384            pr "    add_assoc_string (return_value, r[c], r[c+1], 1);\n";
385            pr "    free (r[c]);\n";
386            pr "    free (r[c+1]);\n";
387            pr "  }\n";
388            pr "  free (r);\n";
389        | RStruct (_, typ) ->
390            let cols = cols_of_struct typ in
391            generate_php_struct_code typ cols
392        | RStructList (_, typ) ->
393            let cols = cols_of_struct typ in
394            generate_php_struct_list_code typ cols
395       );
396
397       pr "}\n";
398       pr "\n"
399   ) all_functions_sorted
400
401 and generate_php_struct_code typ cols =
402   pr "  array_init (return_value);\n";
403   List.iter (
404     function
405     | name, FString ->
406         pr "  add_assoc_string (return_value, \"%s\", r->%s, 1);\n" name name
407     | name, FBuffer ->
408         pr "  add_assoc_stringl (return_value, \"%s\", r->%s, r->%s_len, 1);\n"
409           name name name
410     | name, FUUID ->
411         pr "  add_assoc_stringl (return_value, \"%s\", r->%s, 32, 1);\n"
412           name name
413     | name, (FBytes|FUInt64|FInt64|FInt32|FUInt32) ->
414         pr "  add_assoc_long (return_value, \"%s\", r->%s);\n"
415           name name
416     | name, FChar ->
417         pr "  add_assoc_stringl (return_value, \"%s\", &r->%s, 1, 1);\n"
418           name name
419     | name, FOptPercent ->
420         pr "  add_assoc_double (return_value, \"%s\", r->%s);\n"
421           name name
422   ) cols;
423   pr "  guestfs_free_%s (r);\n" typ
424
425 and generate_php_struct_list_code typ cols =
426   pr "  array_init (return_value);\n";
427   pr "  size_t c = 0;\n";
428   pr "  for (c = 0; c < r->len; ++c) {\n";
429   pr "    zval *z_elem;\n";
430   pr "    ALLOC_INIT_ZVAL (z_elem);\n";
431   pr "    array_init (z_elem);\n";
432   List.iter (
433     function
434     | name, FString ->
435         pr "    add_assoc_string (z_elem, \"%s\", r->val[c].%s, 1);\n"
436           name name
437     | name, FBuffer ->
438         pr "    add_assoc_stringl (z_elem, \"%s\", r->val[c].%s, r->val[c].%s_len, 1);\n"
439           name name name
440     | name, FUUID ->
441         pr "    add_assoc_stringl (z_elem, \"%s\", r->val[c].%s, 32, 1);\n"
442           name name
443     | name, (FBytes|FUInt64|FInt64|FInt32|FUInt32) ->
444         pr "    add_assoc_long (z_elem, \"%s\", r->val[c].%s);\n"
445           name name
446     | name, FChar ->
447         pr "    add_assoc_stringl (z_elem, \"%s\", &r->val[c].%s, 1, 1);\n"
448           name name
449     | name, FOptPercent ->
450         pr "    add_assoc_double (z_elem, \"%s\", r->val[c].%s);\n"
451           name name
452   ) cols;
453   pr "    add_next_index_zval (return_value, z_elem);\n";
454   pr "  }\n";
455   pr "  guestfs_free_%s_list (r);\n" typ