Code

fix for #301: plug memory leak in lua bindings -- bmayland @ leoninedev.com
[rrdtool-all.git] / contrib / php3 / php3_rrdtool.c
1 /*
2  *
3  * php3_rrdtool.c
4  *
5  *      PHP interface to RRD Tool.
6  *
7  *
8  *       Joey Miller, <joeym@inficad.com> 
9  *          SkyLynx / Inficad Communications
10  *          2/12/2000
11  *
12  *
13  * See README, INSTALL, and USAGE files for more details.
14  *
15  */
17 #include "dl/phpdl.h"
18 #include "rrd_tool.h"
19 #include "php3_rrdtool.h"
22 /* {{{ proto string rrd_error(void)
23    Get the error message set by the last rrd tool function call */
25 void php3_rrd_error(INTERNAL_FUNCTION_PARAMETERS)
26 {
27     char *msg;
29     if ( rrd_test_error() )
30     {
31         msg = rrd_get_error();        
33         RETVAL_STRING(msg, 1);
34         rrd_clear_error();
35     }
36     else
37         return;
38 }
39 /* }}} */
43 /* {{{ proto void rrd_clear_error(void)
44    Clear the error set by the last rrd tool function call */
46 void php3_rrd_clear_error(INTERNAL_FUNCTION_PARAMETERS)
47 {
48     if ( rrd_test_error() )
49         rrd_clear_error();
51     return;
52 }
53 /* }}} */
57 /* {{{ proto int rrd_update(string file, string opt) 
58    Update an RRD file with values specified */
60 void php3_rrd_update(INTERNAL_FUNCTION_PARAMETERS)
61 {
62     pval *file, *opt;
63     char **argv;
65     if ( rrd_test_error() )
66         rrd_clear_error();
68     if ( ARG_COUNT(ht) == 2 && 
69          getParameters(ht, 2, &file, &opt) == SUCCESS )
70     {
71         convert_to_string(file);
72         convert_to_string(opt);
74         argv = (char **) emalloc(4 * sizeof(char *));
76         argv[0] = "dummy";
77         argv[1] = estrdup("update");
78         argv[2] = estrdup(file->value.str.val);
79         argv[3] = estrdup(opt->value.str.val);
81         optind = 0; opterr = 0;
82         if ( rrd_update(3, &argv[1]) != -1 )
83         {
84             RETVAL_TRUE;
85         }
86         else
87         {
88             RETVAL_FALSE;
89         }
90         efree(argv[1]); efree(argv[2]); efree(argv[3]);
91         efree(argv);
92     }
93     else
94     {
95         WRONG_PARAM_COUNT;
96     }
97     return;
98 }
99 /* }}} */
103 /* {{{ proto int rrd_last(string file)
104    Gets last update time of an RRD file */
106 void php3_rrd_last(INTERNAL_FUNCTION_PARAMETERS)
108     pval *file;
109     unsigned long retval;
111     char **argv = (char **) emalloc(3 * sizeof(char *));
112     
113     if ( rrd_test_error() )
114         rrd_clear_error();
115     
116     if (getParameters(ht, 1, &file) == SUCCESS)
117     {
118         convert_to_string(file);
120         argv[0] = "dummy";
121         argv[1] = estrdup("last");
122         argv[2] = estrdup(file->value.str.val);
124         optind = 0; opterr = 0;
125         retval = rrd_last(2, &argv[1]);
127         efree(argv[1]);  efree(argv[2]);
128         efree(argv);
129         RETVAL_LONG(retval);
130     }
131     else
132     {
133         WRONG_PARAM_COUNT;
134     }
135     return;
137 /* }}} */
140 /* {{{ proto int rrd_create(string file, array args_arr, int argc)
141    Create an RRD file with the options passed (passed via array) */ 
143 void php3_rrd_create(INTERNAL_FUNCTION_PARAMETERS)
145     pval *file, *args_arr, *p_argc;
146     pval *entry;
147     char **argv;
148     int argc, i;
150     if ( rrd_test_error() )
151         rrd_clear_error();
153     if ( ARG_COUNT(ht) == 3 && getParameters(ht, 3, &file, &args_arr, &p_argc) == SUCCESS )
154     {
155         if ( args_arr->type != IS_ARRAY )
156         { 
157             php3_error(E_WARNING, "2nd Variable passed to rrd_create is not an array!\n");
158             RETURN_FALSE;
159         }
161         convert_to_long(p_argc);
162         convert_to_string(file);
164         argc = p_argc->value.lval + 3;
165         argv = (char **) emalloc(argc * sizeof(char *));
167         argv[0] = "dummy";
168         argv[1] = estrdup("create");
169         argv[2] = estrdup(file->value.str.val);
171         for (i = 3; i < argc; i++) 
172         {
173             if ( _php3_hash_get_current_data(args_arr->value.ht, (void **) &entry) == FAILURE )
174                 continue;
176             if ( entry->type != IS_STRING )
177                 convert_to_string(entry);
179             argv[i] = estrdup(entry->value.str.val);
181             if ( i < argc )
182                 _php3_hash_move_forward(args_arr->value.ht);
183         }
184   
185         optind = 0;  opterr = 0;
187         if ( rrd_create(argc-1, &argv[1]) != -1 )
188         {
189             RETVAL_TRUE;
190         }
191         else
192         {
193             RETVAL_FALSE;
194         }
195         for (i = 1; i < argc; i++)
196             efree(argv[i]);
198         efree(argv);
199     }
200     else
201     {
202         WRONG_PARAM_COUNT;
203     }
204     return;
206 /* }}} */
210 /* {{{ proto mixed rrd_graph(string file, array args_arr, int argc)
211    Creates a graph based on options passed via an array */
213 void php3_rrd_graph(INTERNAL_FUNCTION_PARAMETERS)
215     pval *file, *args_arr, *p_argc;
216     pval *entry, p_calcpr;
217     int i, xsize, ysize, argc;
218     char **argv, **calcpr;
219     
220     if ( rrd_test_error() )
221         rrd_clear_error();
222     
223     if ( ARG_COUNT(ht) == 3 && 
224          getParameters(ht, 3, &file, &args_arr, &p_argc) == SUCCESS)
225     {
226         if ( args_arr->type != IS_ARRAY )
227         { 
228             php3_error(E_WARNING, "2nd Variable passed to rrd_graph is not an array!\n");
229             RETURN_FALSE;
230         }
231         
232         convert_to_long(p_argc);
233         convert_to_string(file);
235         argc = p_argc->value.lval + 3;
236         argv = (char **) emalloc(argc * sizeof(char *));
237  
238         argv[0] = "dummy";
239         argv[1] = estrdup("graph");
240         argv[2] = estrdup(file->value.str.val);
242         for (i = 3; i < argc; i++) 
243         {
244             if ( _php3_hash_get_current_data(args_arr->value.ht, (void **) &entry) == FAILURE 
245                  || entry->type != IS_STRING )
246             {  
247                 continue;
248             }
249             argv[i] = estrdup(entry->value.str.val);
251             if ( i < argc )
252                 _php3_hash_move_forward(args_arr->value.ht);
253         }
254    
255         optind = 0; opterr = 0; 
256         if ( rrd_graph(argc-1, &argv[1], &calcpr, &xsize, &ysize) != -1 )
257         {
258             array_init(return_value);
259             add_assoc_long(return_value, "xsize", xsize);
260             add_assoc_long(return_value, "ysize", ysize);
262             array_init(&p_calcpr);
263     
264             if (calcpr)
265             {
266                 for (i = 0; calcpr[i]; i++)
267                 {
268                     add_next_index_string(&p_calcpr, calcpr[i], 1);
269                     free(calcpr[i]);
270                 }
271                 free(calcpr);
272             }
273             _php3_hash_update(return_value->value.ht, "calcpr", sizeof("calcpr"), 
274                               &p_calcpr, sizeof(pval), NULL);
275         }
276         else
277         {
278             RETVAL_FALSE;
279         }
280         for (i = 1; i < argc; i++)
281             efree(argv[i]);
283         efree(argv);
284     }
285     else
286     { 
287         WRONG_PARAM_COUNT;
288     }
289     return;
291 /* }}} */
295 /* {{{ proto mixed rrd_fetch(string file, array args_arr, int p_argc)
296    Fetch info from an RRD file */
298 void php3_rrd_fetch(INTERNAL_FUNCTION_PARAMETERS)
300     pval *file, *args_arr, *p_argc;
301     pval *entry;
302     pval *p_start, *p_end, *p_step, *p_ds_cnt, p_ds_namv, p_data;
303     int i, argc;
304     time_t start, end;
305     unsigned long step, ds_cnt;
306     char **argv, **ds_namv; 
307     rrd_value_t *data, *datap;
308     
309     if ( rrd_test_error() )
310         rrd_clear_error();
311     
312     if ( ARG_COUNT(ht) == 3 && 
313          getParameters(ht, 3, &file, &args_arr, &p_argc) == SUCCESS)
314     {
315         if ( args_arr->type != IS_ARRAY )
316         { 
317             php3_error(E_WARNING, "2nd Variable passed to rrd_fetch is not an array!\n");
318             RETURN_FALSE;
319         }
320         
321         convert_to_long(p_argc);
322         convert_to_string(file);
324         argc = p_argc->value.lval + 3;
325         argv = (char **) emalloc(argc * sizeof(char *));
326  
327         argv[0] = "dummy";
328         argv[1] = estrdup("fetch");
329         argv[2] = estrdup(file->value.str.val);
331         for (i = 3; i < argc; i++) 
332         {
333             if ( _php3_hash_get_current_data(args_arr->value.ht, (void **) &entry) == FAILURE 
334                  || entry->type != IS_STRING )
335             {  
336                 continue;
337             }
338             argv[i] = estrdup(entry->value.str.val);
340             if ( i < argc )
341                 _php3_hash_move_forward(args_arr->value.ht);
342         }
343   
344         optind = 0; opterr = 0; 
346         if ( rrd_fetch(argc-1, &argv[1], &start,&end,&step,&ds_cnt,&ds_namv,&data) != -1 )
347         {
348             array_init(return_value);
349             add_assoc_long(return_value, "start", start);
350             add_assoc_long(return_value, "end", end);
351             add_assoc_long(return_value, "step", step);
352             add_assoc_long(return_value, "ds_cnt", ds_cnt);
354             array_init(&p_ds_namv);
355             array_init(&p_data);
356    
357             if (ds_namv)
358             {
359                 for (i = 0; i < ds_cnt; i++)
360                 {
361                     add_next_index_string(&p_ds_namv, ds_namv[i], 1);
362                     free(ds_namv[i]);
363                 }
364                 free(ds_namv);
365             }
367             if (data)
368             {
369                 datap = data;
370  
371                 for (i = start; i <= end; i += step)
372                     add_next_index_double(&p_data, *(datap++));
373  
374                 free(data);
375             }
377             _php3_hash_update(return_value->value.ht, "ds_namv", sizeof("ds_namv"), 
378                               &p_ds_namv, sizeof(pval), NULL);
379             _php3_hash_update(return_value->value.ht, "data", sizeof("data"), 
380                               &p_data, sizeof(pval), NULL);
381         }
382         else
383         {
384             RETVAL_FALSE;
385         }
386         for (i = 1; i < argc; i++)
387             efree(argv[i]);
389         efree(argv);
390     }
391     else
392     { 
393         WRONG_PARAM_COUNT;
394     }
395     return;
397 /* }}} */
400 function_entry rrdtool_functions[] = {
401         {"rrd_error", php3_rrd_error, NULL},
402         {"rrd_clear_error", php3_rrd_clear_error, NULL},
403         {"rrd_graph", php3_rrd_graph, NULL},
404         {"rrd_last", php3_rrd_last, NULL},
405         {"rrd_fetch", php3_rrd_fetch, NULL},
406         {"rrd_update", php3_rrd_update, NULL},
407         {"rrd_create", php3_rrd_create, NULL},
408         {NULL, NULL, NULL}
409 };
412 php3_module_entry rrdtool_module_entry = {
413         "RRDTool", rrdtool_functions, NULL, NULL, NULL, NULL, NULL, 0, 0, 0, NULL
414 };
416 php3_module_entry *get_module(void) { return &rrdtool_module_entry; }