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 C
->error(PSI_WARNING
, "Failed to scan PSI directory '%s'", path
);
45 } else for (i
= 0; i
< n
; ++i
) {
50 if (MAXPATHLEN
<= slprintf(psi
, MAXPATHLEN
, "%s/%s", path
, entries
[i
]->d_name
)) {
51 C
->error(PSI_WARNING
, "Path to PSI file too long: %s/%s",
52 path
, entries
[i
]->d_name
);
54 if (!PSI_ParserInit(&P
, psi
, C
->error
, 0)) {
55 C
->error(PSI_WARNING
, "Failed to init PSI parser (%s): %s",
56 psi
, strerror(errno
));
60 while (-1 != PSI_ParserScan(&P
)) {
61 PSI_ParserParse(&P
, PSI_TokenAlloc(&P
));
63 PSI_ParserParse(&P
, NULL
);
65 if (!PSI_ValidatorInit(&V
, &P
)) {
66 C
->error(PSI_WARNING
, "Failed to init PSI validator");
71 if (PSI_ValidatorValidate(&V
)) {
72 zend_function_entry
*closures
;
77 zc
.flags
= CONST_PERSISTENT
|CONST_CS
;
78 zc
.module_number
= EG(current_module
)->module_number
;
80 for (i
= 0; i
< V
.consts
->count
; ++i
) {
81 constant
*c
= V
.consts
->list
[i
];
83 zc
.name
= zend_string_init(c
->name
+ (c
->name
[0] == '\\'), strlen(c
->name
) - (c
->name
[0] == '\\'), 1);
84 ZVAL_NEW_STR(&zc
.value
, zend_string_init(c
->val
->text
, strlen(c
->val
->text
), 1));
86 switch (c
->type
->type
) {
88 convert_to_boolean(&zc
.value
);
91 convert_to_long(&zc
.value
);
94 convert_to_double(&zc
.value
);
97 zend_register_constant(&zc
);
101 closures
= PSI_ContextCompile(C
, (PSI_Data
*) &V
);
102 if (closures
&& SUCCESS
!= zend_register_functions(NULL
, closures
, NULL
, MODULE_PERSISTENT
)) {
103 C
->error(PSI_WARNING
, "Failed to register functions!");
106 PSI_ValidatorDtor(&V
);
109 for (i
= 0; i
< n
; ++i
) {
117 zend_function_entry
*PSI_ContextCompile(PSI_Context
*C
, PSI_Data
*D
)
119 size_t count
= C
->count
++;
120 zend_function_entry
*zfe
;
122 C
->data
= realloc(C
->data
, C
->count
* sizeof(*C
->data
));
123 PSI_DataExchange(&C
->data
[count
], D
);
125 zfe
= C
->ops
->compile(C
, &C
->data
[count
]);
127 C
->closures
= realloc(C
->closures
, C
->count
* sizeof(*C
->closures
));
128 C
->closures
[count
] = zfe
;
133 void PSI_ContextDtor(PSI_Context
*C
)
139 for (i
= 0; i
< C
->count
; ++i
) {
140 PSI_DataDtor(&C
->data
[i
]);
142 free(C
->closures
[i
]->arg_info
);
143 free(C
->closures
[i
]);
149 memset(C
, 0, sizeof(*C
));
152 void PSI_ContextFree(PSI_Context
**C
)