7 #include "php_scandir.h"
10 PSI_Context
*PSI_ContextInit(PSI_Context
*C
, PSI_ContextOps
*ops
, PSI_ContextErrorFunc error
)
13 C
= malloc(sizeof(*C
));
15 memset(C
, 0, sizeof(*C
));
24 static int psi_select_dirent(const struct dirent
*entry
)
27 #define FNM_CASEFOLD 0
29 return 0 == fnmatch("*.psi", entry
->d_name
, FNM_CASEFOLD
);
33 void PSI_ContextBuild(PSI_Context
*C
, const char *path
)
36 struct dirent
**entries
= NULL
;
38 n
= php_scandir(path
, &entries
, psi_select_dirent
, alphasort
);
41 C
->error(PSI_WARNING
, "Failed to scan PSI directory '%s'", path
);
42 } else for (i
= 0; i
< n
; ++i
) {
47 if (MAXPATHLEN
<= slprintf(psi
, MAXPATHLEN
, "%s/%s", path
, entries
[i
]->d_name
)) {
48 C
->error(PSI_WARNING
, "Path to PSI file too long: %s/%s",
49 path
, entries
[i
]->d_name
);
51 if (!PSI_ParserInit(&P
, psi
, psi_error
, 0)) {
52 C
->error(PSI_WARNING
, "Failed to init PSI parser (%s): %s",
53 psi
, strerror(errno
));
57 while (-1 != PSI_ParserScan(&P
)) {
58 PSI_ParserParse(&P
, PSI_TokenAlloc(&P
));
60 PSI_ParserParse(&P
, NULL
);
62 if (!PSI_ValidatorInit(&V
, &P
)) {
63 C
->error(PSI_WARNING
, "Failed to init PSI validator");
68 if (PSI_ValidatorValidate(&V
)) {
69 zend_function_entry
*closures
= PSI_ContextCompile(C
, (PSI_Data
*) &V
);
71 if (closures
&& SUCCESS
!= zend_register_functions(NULL
, closures
, NULL
, MODULE_PERSISTENT
)) {
72 C
->error(PSI_WARNING
, "Failed to register functions!");
76 PSI_ValidatorDtor(&V
);
79 for (i
= 0; i
< n
; ++i
) {
87 zend_function_entry
*PSI_ContextCompile(PSI_Context
*C
, PSI_Data
*D
)
89 size_t count
= C
->count
++;
90 zend_function_entry
*zfe
;
92 C
->data
= realloc(C
->data
, C
->count
* sizeof(*C
->data
));
93 PSI_DataExchange(&C
->data
[count
], D
);
95 zfe
= C
->ops
->compile(C
, &C
->data
[count
]);
97 C
->closures
= realloc(C
->closures
, C
->count
* sizeof(*C
->closures
));
98 C
->closures
[count
] = zfe
;
103 void PSI_ContextDtor(PSI_Context
*C
)
109 for (i
= 0; i
< C
->count
; ++i
) {
110 PSI_DataDtor(&C
->data
[i
]);
114 memset(C
, 0, sizeof(*C
));
117 void PSI_ContextFree(PSI_Context
**C
)