flush
[m6w6/ext-psi] / src / context.c
1 #include <sys/param.h>
2 #include <dirent.h>
3 #include <fnmatch.h>
4 #include <errno.h>
5
6 #include "php.h"
7 #include "php_scandir.h"
8 #include "context.h"
9 #include "parser.h"
10 #include "validator.h"
11
12 PSI_Context *PSI_ContextInit(PSI_Context *C, PSI_ContextOps *ops, PSI_ContextErrorFunc error)
13 {
14 if (!C) {
15 C = malloc(sizeof(*C));
16 }
17 memset(C, 0, sizeof(*C));
18
19 C->error = error;
20 C->ops = ops;
21 ops->init(C);
22
23 return C;
24 }
25
26 static int psi_select_dirent(const struct dirent *entry)
27 {
28 #ifndef FNM_CASEFOLD
29 #define FNM_CASEFOLD 0
30 #endif
31 return 0 == fnmatch("*.psi", entry->d_name, FNM_CASEFOLD);
32 }
33
34
35 void PSI_ContextBuild(PSI_Context *C, const char *path)
36 {
37 int i, n;
38 struct dirent **entries = NULL;
39
40 n = php_scandir(path, &entries, psi_select_dirent, alphasort);
41
42 if (n < 0) {
43 return;
44 } else for (i = 0; i < n; ++i) {
45 char psi[MAXPATHLEN];
46 PSI_Parser P;
47 PSI_Validator V;
48
49 if (MAXPATHLEN <= slprintf(psi, MAXPATHLEN, "%s/%s", path, entries[i]->d_name)) {
50 C->error(PSI_WARNING, "Path to PSI file too long: %s/%s",
51 path, entries[i]->d_name);
52 }
53 if (!PSI_ParserInit(&P, psi, C->error, 0)) {
54 C->error(PSI_WARNING, "Failed to init PSI parser (%s): %s",
55 psi, strerror(errno));
56 continue;
57 }
58
59 while (-1 != PSI_ParserScan(&P)) {
60 PSI_ParserParse(&P, PSI_TokenAlloc(&P));
61 };
62 PSI_ParserParse(&P, NULL);
63
64 if (!PSI_ValidatorInit(&V, &P)) {
65 C->error(PSI_WARNING, "Failed to init PSI validator");
66 break;
67 }
68 PSI_ParserDtor(&P);
69
70 if (PSI_ValidatorValidate(&V)) {
71 zend_function_entry *closures;
72
73 closures = PSI_ContextCompile(C, (PSI_Data *) &V);
74 if (closures && SUCCESS != zend_register_functions(NULL, closures, NULL, MODULE_PERSISTENT)) {
75 C->error(PSI_WARNING, "Failed to register functions!");
76 }
77 }
78 PSI_ValidatorDtor(&V);
79 }
80 if (entries) {
81 for (i = 0; i < n; ++i) {
82 free(entries[i]);
83 }
84 free(entries);
85 }
86
87 }
88
89 zend_function_entry *PSI_ContextCompile(PSI_Context *C, PSI_Data *D)
90 {
91 size_t i, count = C->count++;
92 zend_function_entry *zfe;
93
94 if (D->consts) {
95 zend_constant zc;
96
97 zc.flags = CONST_PERSISTENT|CONST_CS;
98 zc.module_number = EG(current_module)->module_number;
99
100 for (i = 0; i < D->consts->count; ++i) {
101 constant *c = D->consts->list[i];
102
103 zc.name = zend_string_init(c->name + (c->name[0] == '\\'), strlen(c->name) - (c->name[0] == '\\'), 1);
104 ZVAL_NEW_STR(&zc.value, zend_string_init(c->val->text, strlen(c->val->text), 1));
105
106 switch (c->type->type) {
107 case PSI_T_BOOL:
108 convert_to_boolean(&zc.value);
109 break;
110 case PSI_T_INT:
111 convert_to_long(&zc.value);
112 break;
113 case PSI_T_FLOAT:
114 convert_to_double(&zc.value);
115 break;
116 }
117 zend_register_constant(&zc);
118 }
119 }
120
121 C->data = realloc(C->data, C->count * sizeof(*C->data));
122 PSI_DataExchange(&C->data[count], D);
123
124 zfe = C->ops->compile(C, &C->data[count]);
125
126 C->closures = realloc(C->closures, C->count * sizeof(*C->closures));
127 C->closures[count] = zfe;
128
129 return zfe;
130 }
131
132 void PSI_ContextDtor(PSI_Context *C)
133 {
134 size_t i;
135
136 C->ops->dtor(C);
137
138 for (i = 0; i < C->count; ++i) {
139 PSI_DataDtor(&C->data[i]);
140 if (C->closures[i]){
141 free(C->closures[i]);
142 }
143 }
144 free(C->data);
145 free(C->closures);
146
147 memset(C, 0, sizeof(*C));
148 }
149
150 void PSI_ContextFree(PSI_Context **C)
151 {
152 if (*C) {
153 PSI_ContextDtor(*C);
154 free(*C);
155 *C = NULL;
156 }
157 }
158