dump fixes
[m6w6/ext-psi] / src / data.h
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 #ifndef PSI_DATA_H
27 #define PSI_DATA_H
28
29 #include "types.h"
30 #include "error.h"
31 #include "plist.h"
32 #include "validate.h"
33
34 #include <stdarg.h>
35 #include <dlfcn.h>
36
37 #define PSI_DEBUG 0x1
38 #define PSI_SILENT 0x2
39
40 #ifndef RTLD_NEXT
41 # define RTLD_NEXT ((void *) -1l)
42 #endif
43 #ifndef RTLD_DEFAULT
44 # define RTLD_DEFAULT ((void *) 0)
45 #endif
46
47 static inline void *psi_dlsym(struct psi_plist *dllist, const char *name, const char *redir)
48 {
49 void *dl, *sym = NULL;
50 const char *test = redir ?: name;
51
52 again:
53 if (dllist) {
54 size_t i = 0;
55
56 while (!sym && psi_plist_get(dllist, i++, &dl)) {
57 sym = dlsym(dl, test);
58 }
59 }
60 if (!sym) {
61 sym = dlsym(RTLD_DEFAULT, test);
62 }
63 if (!sym && test == redir) {
64 test = name;
65 goto again;
66 }
67
68 return sym;
69 }
70
71 #define PSI_DEBUG_PRINT(ctx, ...) do { \
72 if ((ctx) && (PSI_DATA(ctx)->flags & PSI_DEBUG)) { \
73 dprintf(PSI_DATA(ctx)->debug_fd, __VA_ARGS__); \
74 } \
75 } while(0)
76 #define PSI_DEBUG_PRINTV(ctx, msg, argv) do { \
77 if ((ctx) && (PSI_DATA(ctx)->flags & PSI_DEBUG)) { \
78 vdprintf(PSI_DATA(ctx)->debug_fd, msg, argv); \
79 } \
80 } while(0)
81 #define PSI_DEBUG_DUMP(ctx, dump_func, ...) do { \
82 if ((ctx) && (PSI_DATA(ctx)->flags & PSI_DEBUG)) { \
83 struct psi_dump dump_ = {{ .fd = PSI_DATA(ctx)->debug_fd}, \
84 .fun = (psi_dump_cb) dprintf}; \
85 dump_func(&dump_, __VA_ARGS__); \
86 } \
87 } while (0)
88
89 union psi_dump_arg {
90 void *hn;
91 int fd;
92 };
93 typedef void (*psi_dump_cb)(union psi_dump_arg, const char *msg, ...);
94 struct psi_dump {
95 union psi_dump_arg ctx;
96 psi_dump_cb fun;
97 };
98 #define PSI_DUMP(dump, ...) (dump)->fun((dump)->ctx, __VA_ARGS__)
99
100 #define PSI_DATA(D) ((struct psi_data *) (D))
101
102 #define PSI_DATA_MEMBERS \
103 struct psi_decl_file file; \
104 struct psi_plist *consts; \
105 struct psi_plist *types; \
106 struct psi_plist *structs; \
107 struct psi_plist *unions; \
108 struct psi_plist *enums; \
109 struct psi_plist *decls; \
110 struct psi_plist *vars; \
111 struct psi_plist *impls; \
112 psi_error_cb error; \
113 char last_error[0x1000]; \
114 unsigned errors; \
115 unsigned flags; \
116 int debug_fd
117
118 struct psi_data {
119 PSI_DATA_MEMBERS;
120 };
121
122 struct psi_data *psi_data_ctor(struct psi_data *data, psi_error_cb error, unsigned flags);
123 struct psi_data *psi_data_ctor_with_dtors(struct psi_data *data, psi_error_cb error, unsigned flags);
124 struct psi_data *psi_data_exchange(struct psi_data *dest, struct psi_data *src);
125 void psi_data_dtor(struct psi_data *data);
126 void psi_data_dump(struct psi_dump *dump, struct psi_data *data);
127
128 #endif