Debian Codegraph @

contrib/php/genders.c

raw
1 /*****************************************************************************\
2 * $Id: genders.c,v 1.5 2010-02-02 00:04:34 chu11 Exp $
3 *****************************************************************************
4 * Copyright (C) 2007-2019 Lawrence Livermore National Security, LLC.
5 * Copyright (C) 2001-2007 The Regents of the University of California.
6 * Produced at Lawrence Livermore National Laboratory (cf, DISCLAIMER).
7 * Written by Jim Garlick <[email protected]> and Albert Chu <[email protected]>.
8 * UCRL-CODE-2003-004.
9 *
10 * This file is part of Genders, a cluster configuration database.
11 * For details, see <http://www.llnl.gov/linux/genders/>.
12 *
13 * Genders is free software; you can redistribute it and/or modify it under
14 * the terms of the GNU General Public License as published by the Free
15 * Software Foundation; either version 2 of the License, or (at your option)
16 * any later version.
17 *
18 * Genders is distributed in the hope that it will be useful, but WITHOUT ANY
19 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
20 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
21 * details.
22 *
23 * You should have received a copy of the GNU General Public License along
24 * with Genders. If not, see <http://www.gnu.org/licenses/>.
25 \*****************************************************************************/
26
27 #ifdef HAVE_CONFIG_H
28 #include "config.h"
29 #endif
30
31 #include "php.h"
32 #include "php_ini.h"
33 #include "ext/standard/info.h"
34 #include "php_genders.h"
35
36 #include <genders.h>
37
38 static int le_genders;
39
40 zend_function_entry genders_functions[] = {
41 PHP_FE(genders_getnumattrs, NULL)
42 PHP_FE(genders_getattr, NULL)
43 PHP_FE(genders_getattr_all, NULL)
44 PHP_FE(genders_getnodes, NULL)
45 {NULL, NULL, NULL}
46 };
47
48
49 zend_module_entry genders_module_entry = {
50 STANDARD_MODULE_HEADER,
51 "genders",
52 genders_functions,
53 PHP_MINIT(genders),
54 PHP_MSHUTDOWN(genders),
55 PHP_RINIT(genders),
56 PHP_RSHUTDOWN(genders),
57 PHP_MINFO(genders),
58 NO_VERSION_YET,
59 STANDARD_MODULE_PROPERTIES
60 };
61
62
63 #ifdef COMPILE_DL_GENDERS
64 ZEND_GET_MODULE(genders)
65 #endif
66
67 PHP_MINIT_FUNCTION(genders)
68 {
69 return SUCCESS;
70 }
71
72 PHP_MSHUTDOWN_FUNCTION(genders)
73 {
74 return SUCCESS;
75 }
76
77 PHP_RINIT_FUNCTION(genders)
78 {
79 return SUCCESS;
80 }
81
82 PHP_RSHUTDOWN_FUNCTION(genders)
83 {
84 return SUCCESS;
85 }
86
87 PHP_MINFO_FUNCTION(genders)
88 {
89 php_info_print_table_start();
90 php_info_print_table_header(2, "genders support", "enabled");
91 php_info_print_table_end();
92 }
93
94 /* {{{ proto int genders_getnumattrs (string file) */
95 PHP_FUNCTION(genders_getnumattrs)
96 {
97 genders_t ghandle;
98 unsigned long retval;
99 zval *file;
100
101 if (zend_get_parameters(ht, 1, &file) == SUCCESS)
102 {
103 ghandle = genders_handle_create();
104 genders_load_data(ghandle, file->value.str.val);
105
106 retval = genders_getnumattrs(ghandle);
107
108 if(retval < 0)
109 {
110 php_error(E_WARNING, genders_strerror(genders_errnum(ghandle)));
111 RETURN_FALSE;
112 }
113
114 RETVAL_LONG(retval);
115 genders_handle_destroy(ghandle);
116 }
117 else
118 {
119 WRONG_PARAM_COUNT;
120 }
121 return;
122 }
123
124 /* {{{ proto string array genders_getattr (string file, string node, bool ret_type) */
125 /* if ret_type is 0, return the attribute list. Otherwise, return the value list. */
126 PHP_FUNCTION(genders_getattr)
127 {
128 genders_t ghandle;
129 zval *file;
130 zval *node;
131 zval *ret_type;
132 char **attrlist;
133 char **vallist;
134 int len, num, k;
135
136 if ( ZEND_NUM_ARGS() == 3 && zend_get_parameters(ht, 3, &file, &node, &ret_type) == SUCCESS)
137 {
138 ghandle = genders_handle_create();
139 genders_load_data(ghandle, file->value.str.val);
140
141 len = genders_attrlist_create(ghandle, &attrlist);
142 genders_vallist_create(ghandle, &vallist);
143
144 num = genders_getattr(ghandle, attrlist, vallist, len, node->value.str.val);
145 if(num < 0)
146 {
147 if(genders_errnum(ghandle) == GENDERS_ERR_NOTFOUND)
148 {
149 RETURN_FALSE;
150 }
151 else
152 {
153 php_error(E_WARNING, genders_strerror(genders_errnum(ghandle)));
154 RETURN_FALSE;
155 }
156 }
157
158 array_init(return_value);
159
160 if(ret_type->value.lval)
161 {
162 for(k=0;k<num;k++)
163 {
164 add_next_index_string(return_value, vallist[k], 1);
165 }
166 }
167 else
168 {
169 for(k=0;k<num;k++)
170 {
171 add_next_index_string(return_value, attrlist[k], 1);
172 }
173 }
174
175 genders_attrlist_destroy(ghandle, attrlist);
176 genders_vallist_destroy(ghandle, vallist);
177
178 genders_handle_destroy(ghandle);
179 }
180 else
181 {
182 WRONG_PARAM_COUNT;
183 }
184 return;
185 }
186
187 /* {{{ proto string array genders_getnodes (string file, string attr, string val) */
188 PHP_FUNCTION(genders_getnodes)
189 {
190 genders_t ghandle;
191 zval *file;
192 zval *attr;
193 zval *val;
194 char **nodelist;
195 int len, num, k;
196
197 if ( ZEND_NUM_ARGS() == 3 && zend_get_parameters(ht, 3, &file, &attr, &val) == SUCCESS)
198 {
199 ghandle = genders_handle_create();
200 genders_load_data(ghandle, file->value.str.val);
201
202 len = genders_nodelist_create(ghandle, &nodelist);
203
204 num = genders_getnodes(ghandle, nodelist, len, attr->value.str.val, val->value.str.val);
205 if(num < 0)
206 {
207 php_error(E_WARNING, genders_strerror(genders_errnum(ghandle)));
208 RETURN_FALSE;
209 }
210
211 array_init(return_value);
212
213 for(k=0;k<num;k++)
214 {
215 add_next_index_string(return_value, nodelist[k], 1);
216 }
217
218 genders_nodelist_destroy(ghandle, nodelist);
219 genders_handle_destroy(ghandle);
220 }
221 else
222 {
223 WRONG_PARAM_COUNT;
224 }
225 return;
226 }
227
228 /* {{{ proto string array genders_getattr_all (string file) */
229 PHP_FUNCTION(genders_getattr_all)
230 {
231 genders_t ghandle;
232 zval *file;
233 char **attrlist;
234 int len, num, k;
235
236 if ( ZEND_NUM_ARGS() == 1 && zend_get_parameters(ht, 1, &file) == SUCCESS)
237 {
238 ghandle = genders_handle_create();
239 genders_load_data(ghandle, file->value.str.val);
240
241 len = genders_attrlist_create(ghandle, &attrlist);
242
243 num = genders_getattr_all(ghandle, attrlist, len);
244 if(num < 0)
245 {
246 php_error(E_WARNING, genders_strerror(genders_errnum(ghandle)));
247 RETURN_FALSE;
248 }
249
250 array_init(return_value);
251
252 for(k=0;k<num;k++)
253 {
254 add_next_index_string(return_value, attrlist[k], 1);
255 }
256
257 genders_attrlist_destroy(ghandle, attrlist);
258 genders_handle_destroy(ghandle);
259 }
260 else
261 {
262 WRONG_PARAM_COUNT;
263 }
264 return;
265 }