7 #include "php_scandir.h"
10 #include "validator.h"
12 PSI_Context
*PSI_ContextInit(PSI_Context
*C
, PSI_ContextOps
*ops
, PSI_ContextErrorFunc error
)
15 C
= malloc(sizeof(*C
));
17 memset(C
, 0, sizeof(*C
));
26 static int psi_select_dirent(const struct dirent
*entry
)
29 #define FNM_CASEFOLD 0
31 return 0 == fnmatch("*.psi", entry
->d_name
, FNM_CASEFOLD
);
35 void PSI_ContextBuild(PSI_Context
*C
, const char *path
)
38 struct dirent
**entries
= NULL
;
40 n
= php_scandir(path
, &entries
, psi_select_dirent
, alphasort
);
44 } else for (i
= 0; i
< n
; ++i
) {
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
);
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
));
59 while (-1 != PSI_ParserScan(&P
)) {
60 PSI_ParserParse(&P
, PSI_TokenAlloc(&P
));
62 PSI_ParserParse(&P
, NULL
);
64 if (!PSI_ValidatorInit(&V
, &P
)) {
65 C
->error(PSI_WARNING
, "Failed to init PSI validator");
70 if (PSI_ValidatorValidate(&V
)) {
71 zend_function_entry
*closures
;
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!");
78 PSI_ValidatorDtor(&V
);
81 for (i
= 0; i
< n
; ++i
) {
89 zend_function_entry
*PSI_ContextCompile(PSI_Context
*C
, PSI_Data
*D
)
91 size_t i
, count
= C
->count
++;
92 zend_function_entry
*zfe
;
97 zc
.flags
= CONST_PERSISTENT
|CONST_CS
;
98 zc
.module_number
= EG(current_module
)->module_number
;
100 for (i
= 0; i
< D
->consts
->count
; ++i
) {
101 constant
*c
= D
->consts
->list
[i
];
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));
106 switch (c
->type
->type
) {
108 convert_to_boolean(&zc
.value
);
111 convert_to_long(&zc
.value
);
114 convert_to_double(&zc
.value
);
117 zend_register_constant(&zc
);
121 C
->data
= realloc(C
->data
, C
->count
* sizeof(*C
->data
));
122 PSI_DataExchange(&C
->data
[count
], D
);
124 zfe
= C
->ops
->compile(C
, &C
->data
[count
]);
126 C
->closures
= realloc(C
->closures
, C
->count
* sizeof(*C
->closures
));
127 C
->closures
[count
] = zfe
;
132 void PSI_ContextDtor(PSI_Context
*C
)
138 for (i
= 0; i
< C
->count
; ++i
) {
139 PSI_DataDtor(&C
->data
[i
]);
141 free(C
->closures
[i
]);
147 memset(C
, 0, sizeof(*C
));
150 void PSI_ContextFree(PSI_Context
**C
)