11 #include "php_scandir.h"
14 #include "validator.h"
16 #define psi_predef_count(s) (sizeof(psi_predef_ ##s## s)/sizeof(psi_predef ##s))
17 typedef struct psi_predef_type
{
19 const char *type_name
;
22 #define psi_predef_type_count() psi_predef_count(type)
23 static const psi_predef_types
[] = {
27 typedef struct psi_predef_const
{
29 const char *type_name
;
34 #define psi_predef_const_count() psi_predef_count(const)
35 static const psi_predef_consts
[] = {
39 PSI_Context
*PSI_ContextInit(PSI_Context
*C
, PSI_ContextOps
*ops
, PSI_ContextErrorFunc error
)
45 C
= malloc(sizeof(*C
));
47 memset(C
, 0, sizeof(*C
));
53 memset(&data
, 0, sizeof(data
));
54 for (i
= 0; i
< psi_predef_type_count(); ++i
) {
55 psi_predef_type
*pre
= &psi_predef_types
[i
];
56 decl_type
*type
= init_decl_type(pre
->type_tag
, pre
->type_name
);
57 decl_typedef
*def
= init_decl_typedef(pre
->alias
, type
);
59 data
.defs
= add_decl_typedef(data
.defs
, def
);
61 for (i
= 0; i
< psi_predef_const_count(); ++i
) {
62 psi_predef_const
*pre
= psi_predef_const
[i
];
63 impl_def_val
*val
= init_impl_def_val(pre
->val_type_tag
, pre
->val_text
);
64 const_type
*type
= init_const_type(pre
->type_tag
, pre
->type_name
);
65 constant
*constant
= init_constant(type
, pre
->name
, val
);
67 data
.consts
= add_constant(data
.consts
, constant
);
72 static int psi_select_dirent(const struct dirent
*entry
)
75 #define FNM_CASEFOLD 0
77 return 0 == fnmatch("*.psi", entry
->d_name
, FNM_CASEFOLD
);
81 void PSI_ContextBuild(PSI_Context
*C
, const char *path
)
84 struct dirent
**entries
= NULL
;
86 n
= php_scandir(path
, &entries
, psi_select_dirent
, alphasort
);
90 } else for (i
= 0; i
< n
; ++i
) {
95 if (MAXPATHLEN
<= slprintf(psi
, MAXPATHLEN
, "%s/%s", path
, entries
[i
]->d_name
)) {
96 C
->error(PSI_WARNING
, "Path to PSI file too long: %s/%s",
97 path
, entries
[i
]->d_name
);
99 if (!PSI_ParserInit(&P
, psi
, C
->error
, 0)) {
100 C
->error(PSI_WARNING
, "Failed to init PSI parser (%s): %s",
101 psi
, strerror(errno
));
105 while (-1 != PSI_ParserScan(&P
)) {
106 PSI_ParserParse(&P
, PSI_TokenAlloc(&P
));
108 PSI_ParserParse(&P
, NULL
);
110 if (!PSI_ValidatorInit(&V
, &P
)) {
111 C
->error(PSI_WARNING
, "Failed to init PSI validator");
116 if (PSI_ValidatorValidate(&V
)) {
117 zend_function_entry
*closures
;
119 closures
= PSI_ContextCompile(C
, (PSI_Data
*) &V
);
120 if (closures
&& SUCCESS
!= zend_register_functions(NULL
, closures
, NULL
, MODULE_PERSISTENT
)) {
121 C
->error(PSI_WARNING
, "Failed to register functions!");
124 PSI_ValidatorDtor(&V
);
127 for (i
= 0; i
< n
; ++i
) {
135 zend_function_entry
*PSI_ContextCompile(PSI_Context
*C
, PSI_Data
*D
)
137 size_t i
, count
= C
->count
++;
138 zend_function_entry
*zfe
;
143 zc
.flags
= CONST_PERSISTENT
|CONST_CS
;
144 zc
.module_number
= EG(current_module
)->module_number
;
146 for (i
= 0; i
< D
->consts
->count
; ++i
) {
147 constant
*c
= D
->consts
->list
[i
];
149 zc
.name
= zend_string_init(c
->name
+ (c
->name
[0] == '\\'), strlen(c
->name
) - (c
->name
[0] == '\\'), 1);
150 ZVAL_NEW_STR(&zc
.value
, zend_string_init(c
->val
->text
, strlen(c
->val
->text
), 1));
152 switch (c
->type
->type
) {
154 convert_to_boolean(&zc
.value
);
157 convert_to_long(&zc
.value
);
160 convert_to_double(&zc
.value
);
163 zend_register_constant(&zc
);
167 C
->data
= realloc(C
->data
, C
->count
* sizeof(*C
->data
));
168 PSI_DataExchange(&C
->data
[count
], D
);
170 zfe
= C
->ops
->compile(C
, &C
->data
[count
]);
172 C
->closures
= realloc(C
->closures
, C
->count
* sizeof(*C
->closures
));
173 C
->closures
[count
] = zfe
;
178 void PSI_ContextDtor(PSI_Context
*C
)
184 for (i
= 0; i
< C
->count
; ++i
) {
185 PSI_DataDtor(&C
->data
[i
]);
187 free(C
->closures
[i
]);
193 memset(C
, 0, sizeof(*C
));
196 void PSI_ContextFree(PSI_Context
**C
)