0fb98164ea1629cdd94be97380a8834acdb547a1
[m6w6/ext-psi] / src / data.c
1 /*******************************************************************************
2 Copyright (c) 2016, Michael Wallner <mike@php.net>.
3 All rights reserved.
4
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions are met:
7
8 * Redistributions of source code must retain the above copyright notice,
9 this list of conditions and the following disclaimer.
10 * Redistributions in binary form must reproduce the above copyright
11 notice, this list of conditions and the following disclaimer in the
12 documentation and/or other materials provided with the distribution.
13
14 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
15 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
17 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
18 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
20 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
21 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
22 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 *******************************************************************************/
25
26 #include "php_psi_stdinc.h"
27 #include "data.h"
28
29 #include "php_globals.h"
30 #include "php_network.h"
31
32 #include <dlfcn.h>
33 #include <ctype.h>
34
35 static void psi_data_ctor_internal(struct psi_data *data,
36 psi_error_cb error, unsigned flags)
37 {
38 data->error = error;
39 data->flags = flags;
40
41 if (data->flags & PSI_DEBUG) {
42 char *debug = getenv("PSI_DEBUG");
43
44 if (debug) {
45 int fd = -1;
46 char *addr = strstr(debug, "://");
47
48 if (addr) {
49 addr += 3;
50 }
51 if (addr && *addr) {
52 struct sockaddr_storage sa = {0};
53 socklen_t ss = 0;
54 int rc = php_network_parse_network_address_with_port(addr,
55 strlen(addr), (struct sockaddr *) &sa, &ss);
56
57 if (SUCCESS == rc) {
58 int styp = strncmp(debug, "udp:", 4)
59 ? SOCK_STREAM
60 : SOCK_DGRAM;
61 int sfam = sa.ss_family == AF_INET6
62 ? ((struct sockaddr_in6 *) &sa)->sin6_family
63 : ((struct sockaddr_in *) &sa)->sin_family;
64
65 fd = socket(sfam, styp, 0);
66
67 if (fd > 0 && 0 != connect(fd, (struct sockaddr *) &sa, ss)) {
68 perror(debug);
69 close(fd);
70 fd = -1;
71 }
72 }
73 } else if (!strcmp(debug, "stdout")) {
74 fd = STDOUT_FILENO;
75 } else if (!strcmp(debug, "stderr")) {
76 fd = STDERR_FILENO;
77 } else if (!(fd = atoi(debug))) {
78 fd = open(debug, O_WRONLY|O_APPEND|O_CREAT|O_CLOEXEC, 0664);
79 }
80
81 if (fd > 0) {
82 data->debug_fd = fd;
83 } else {
84 data->debug_fd = STDERR_FILENO;
85 }
86 }
87 }
88 }
89
90 struct psi_data *psi_data_ctor_with_dtors(struct psi_data *data,
91 psi_error_cb error, unsigned flags)
92 {
93 if (!data) {
94 data = pecalloc(1, sizeof(*data), 1);
95 }
96
97 psi_data_ctor_internal(data, error, flags);
98
99 if (!data->file.libnames) {
100 data->file.libnames = psi_plist_init((psi_plist_dtor) psi_names_free);
101 }
102 if (!data->file.dlopened) {
103 data->file.dlopened = psi_plist_init((psi_plist_dtor) psi_libs_free);
104 }
105
106 if (!data->consts) {
107 data->consts = psi_plist_init((psi_plist_dtor) psi_const_free);
108 }
109 if (!data->types) {
110 data->types = psi_plist_init((psi_plist_dtor) psi_decl_arg_free);
111 }
112 if (!data->structs) {
113 data->structs = psi_plist_init((psi_plist_dtor) psi_decl_struct_free);
114 }
115 if (!data->unions) {
116 data->unions = psi_plist_init((psi_plist_dtor) psi_decl_union_free);
117 }
118 if (!data->enums) {
119 data->enums = psi_plist_init((psi_plist_dtor) psi_decl_enum_free);
120 }
121 if (!data->decls) {
122 data->decls = psi_plist_init((psi_plist_dtor) psi_decl_free);
123 }
124 if (!data->vars) {
125 data->vars = psi_plist_init((psi_plist_dtor) psi_decl_extvar_free);
126 }
127 if (!data->impls) {
128 data->impls = psi_plist_init((psi_plist_dtor) psi_impl_free);
129 }
130 return data;
131 }
132
133 struct psi_data *psi_data_ctor(struct psi_data *data, psi_error_cb error,
134 unsigned flags)
135 {
136 if (!data) {
137 data = pecalloc(1, sizeof(*data), 1);
138 }
139
140 psi_data_ctor_internal(data, error, flags);
141
142 if (!data->file.libnames) {
143 data->file.libnames = psi_plist_init(NULL);
144 }
145 if (!data->file.dlopened) {
146 data->file.dlopened = psi_plist_init(NULL);
147 }
148
149 if (!data->consts) {
150 data->consts = psi_plist_init(NULL);
151 }
152 if (!data->types) {
153 data->types = psi_plist_init(NULL);
154 }
155 if (!data->structs) {
156 data->structs = psi_plist_init(NULL);
157 }
158 if (!data->unions) {
159 data->unions = psi_plist_init(NULL);
160 }
161 if (!data->enums) {
162 data->enums = psi_plist_init(NULL);
163 }
164 if (!data->decls) {
165 data->decls = psi_plist_init(NULL);
166 }
167 if (!data->vars) {
168 data->vars = psi_plist_init(NULL);
169 }
170 if (!data->impls) {
171 data->impls = psi_plist_init(NULL);
172 }
173 return data;
174 }
175
176 struct psi_data *psi_data_exchange(struct psi_data *dest, struct psi_data *src)
177 {
178 if (!dest) {
179 dest = pemalloc(sizeof(*dest), 1);
180 }
181 *dest = *src;
182 memset(src, 0, sizeof(*src));
183 return dest;
184 }
185
186 void psi_data_dtor(struct psi_data *data)
187 {
188 if (data->debug_fd) {
189 close(data->debug_fd);
190 }
191 if (data->consts) {
192 psi_plist_free(data->consts);
193 }
194 if (data->types) {
195 psi_plist_free(data->types);
196 }
197 if (data->structs) {
198 psi_plist_free(data->structs);
199 }
200 if (data->unions) {
201 psi_plist_free(data->unions);
202 }
203 if (data->enums) {
204 psi_plist_free(data->enums);
205 }
206 if (data->decls) {
207 psi_plist_free(data->decls);
208 }
209 if (data->vars) {
210 psi_plist_free(data->vars);
211 }
212 if (data->impls) {
213 psi_plist_free(data->impls);
214 }
215
216 psi_decl_file_dtor(&data->file);
217 }
218
219 void psi_data_dump(struct psi_dump *dump, struct psi_data *D)
220 {
221 size_t i = 0;
222 char *libname;
223
224 if (D->file.filename) {
225 PSI_DUMP(dump, "// filename=%s (%u errors)\n", D->file.filename->val, D->errors);
226 }
227 while (psi_plist_get(D->file.libnames, i++, &libname)) {
228 PSI_DUMP(dump, "lib \"%s\";\n", libname);
229 }
230 if (psi_plist_count(D->types)) {
231 size_t i = 0;
232 struct psi_decl_arg *def;
233
234 while (psi_plist_get(D->types, i++, &def)) {
235 PSI_DUMP(dump, "typedef ");
236 psi_decl_arg_dump(dump, def, 0);
237 PSI_DUMP(dump, ";\n");
238 }
239 PSI_DUMP(dump, "\n");
240 }
241 if (psi_plist_count(D->unions)) {
242 size_t i = 0;
243 struct psi_decl_union *unn;
244
245 while (psi_plist_get(D->unions, i++, &unn)) {
246 if (!psi_decl_type_is_anon(unn->name, "union")) {
247 psi_decl_union_dump(fd, unn);
248 PSI_DUMP(dump, "\n");
249 }
250 }
251 PSI_DUMP(dump, "\n");
252 }
253 if (psi_plist_count(D->structs)) {
254 size_t i = 0;
255 struct psi_decl_struct *strct;
256
257 while (psi_plist_get(D->structs, i++, &strct)) {
258 if (!psi_decl_type_is_anon(strct->name, "struct")) {
259 psi_decl_struct_dump(dump, strct);
260 PSI_DUMP(dump, "\n");
261 }
262 }
263 PSI_DUMP(dump, "\n");
264 }
265 if (psi_plist_count(D->enums)) {
266 size_t i = 0;
267 struct psi_decl_enum *enm;
268
269 while (psi_plist_get(D->enums, i++, &enm)) {
270 if (!psi_decl_type_is_anon(enm->name, "enum")) {
271 psi_decl_enum_dump(dump, enm, 0);
272 PSI_DUMP(dump, "\n");
273 }
274 }
275 PSI_DUMP(dump, "\n");
276 }
277 if (psi_plist_count(D->consts)) {
278 size_t i = 0;
279 struct psi_const *c;
280
281 while (psi_plist_get(D->consts, i++, &c)) {
282 psi_const_dump(dump, c);
283 PSI_DUMP(dump, "\n");
284 }
285 PSI_DUMP(dump, "\n");
286 }
287 if (psi_plist_count(D->decls)) {
288 size_t i = 0;
289 struct psi_decl *decl;
290
291 while (psi_plist_get(D->decls, i++, &decl)) {
292 if (decl->extvar) {
293 PSI_DUMP(dump, "/* extvar accessor\n");
294 }
295 psi_decl_dump(dump, decl);
296 PSI_DUMP(dump, "\n");
297 if (decl->extvar) {
298 PSI_DUMP(dump, " extvar accessor */\n");
299 }
300 }
301 PSI_DUMP(dump, "\n");
302 }
303 if (psi_plist_count(D->vars)) {
304 size_t i = 0;
305 struct psi_decl_extvar *evar;
306
307 while (psi_plist_get(D->vars, i++, &evar)) {
308 psi_decl_extvar_dump(dump, evar);
309 }
310 PSI_DUMP(dump, "\n");
311 }
312 if (psi_plist_count(D->impls)) {
313 size_t i = 0;
314 struct psi_impl *impl;
315
316 while (psi_plist_get(D->impls, i++, &impl)) {
317 psi_impl_dump(dump, impl);
318 PSI_DUMP(dump, "\n");
319 }
320 PSI_DUMP(dump, "\n");
321 }
322 }