1 /*******************************************************************************
2 Copyright (c) 2016, Michael Wallner <mike@php.net>.
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions are met:
8 * Redistributions of source code must retain the above copyright notice,
9 this list of conditions and the following disclaimer.
10 * Redistributions in binary form must reproduce the above copyright
11 notice, this list of conditions and the following disclaimer in the
12 documentation and/or other materials provided with the distribution.
14 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
15 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
17 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
18 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
20 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
21 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
22 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 *******************************************************************************/
29 # include "php_config.h"
37 #include <Zend/zend_smart_str.h>
41 struct psi_parser
*psi_parser_init(struct psi_parser
*P
, psi_error_cb error
,
45 P
= pemalloc(sizeof(*P
), 1);
47 memset(P
, 0, sizeof(*P
));
49 psi_data_ctor_with_dtors(PSI_DATA(P
), error
, flags
);
51 P
->preproc
= psi_cpp_init(P
);
56 struct psi_parser_input
*psi_parser_open_file(struct psi_parser
*P
,
57 const char *filename
, bool report_errors
)
61 struct psi_parser_input
*fb
;
63 if (stat(filename
, &sb
)) {
65 P
->error(PSI_DATA(P
), NULL
, PSI_WARNING
,
66 "Could not stat '%s': %s",
67 filename
, strerror(errno
));
72 if (!(fb
= pemalloc(sizeof(*fb
) + sb
.st_size
+ psi_parser_maxfill(), 1))) {
74 P
->error(PSI_DATA(P
), NULL
, PSI_WARNING
,
75 "Could not allocate %zu bytes for reading '%s': %s",
76 sb
.st_size
+ psi_parser_maxfill(), filename
, strerror(errno
));
81 if (!(fp
= fopen(filename
, "r"))) {
84 P
->error(PSI_DATA(P
), NULL
, PSI_WARNING
,
85 "Could not open '%s' for reading: %s",
86 filename
, strerror(errno
));
91 if (sb
.st_size
!= fread(fb
->buffer
, 1, sb
.st_size
, fp
)) {
95 P
->error(PSI_DATA(P
), NULL
, PSI_WARNING
,
96 "Could not read %zu bytes from '%s': %s",
97 sb
.st_size
+ psi_parser_maxfill(), filename
, strerror(errno
));
103 memset(fb
->buffer
+ sb
.st_size
, 0, psi_parser_maxfill());
105 fb
->lmod
= sb
.st_mtime
;
106 fb
->length
= sb
.st_size
;
107 fb
->file
= psi_string_init_interned(filename
, strlen(filename
), 1);
112 struct psi_parser_input
*psi_parser_open_string(struct psi_parser
*P
,
113 const char *string
, size_t length
)
115 struct psi_parser_input
*sb
;
117 if (!(sb
= pemalloc(sizeof(*sb
) + length
+ psi_parser_maxfill(), 1))) {
118 P
->error(PSI_DATA(P
), NULL
, PSI_WARNING
,
119 "Could not allocate %zu bytes: %s",
120 length
+ psi_parser_maxfill(), strerror(errno
));
124 memcpy(sb
->buffer
, string
, length
);
125 memset(sb
->buffer
+ length
, 0, psi_parser_maxfill());
127 sb
->lmod
= time(NULL
);
129 sb
->file
= psi_string_init_interned("<stdin>", strlen("<stdin>"), 1);
134 struct psi_plist
*psi_parser_preprocess(struct psi_parser
*P
,
135 struct psi_plist
**tokens
)
137 if (psi_cpp_process(P
->preproc
, tokens
, NULL
)) {
143 bool psi_parser_process(struct psi_parser
*P
, struct psi_plist
*tokens
,
146 if (psi_plist_count(tokens
)) {
147 return 0 == psi_parser_proc_parse(P
, tokens
, processed
);
152 static inline zend_string
*macro_to_constant(struct psi_parser
*parser
,
153 zend_string
*name
, struct psi_validate_scope
*scope
)
157 struct psi_token
*tok
;
158 #if PSI_THREADED_PARSER
164 psi_smart_str_printf(&str
, "const psi\\%s = ", name
->val
);
165 if (scope
->macro
->exp
) {
166 struct psi_dump dump
= {{.hn
= &str
},
167 .fun
= (psi_dump_cb
) psi_smart_str_printf
};
169 psi_num_exp_dump(&dump
, scope
->macro
->exp
);
171 } else while (psi_plist_get(scope
->macro
->tokens
, i
++, &tok
)) {
172 if (tok
->type
== PSI_T_QUOTED_STRING
) {
173 smart_str_appendc_ex(&str
, '"', persistent
);
175 smart_str_append_ex(&str
, tok
->text
, persistent
);
176 if (tok
->type
== PSI_T_QUOTED_STRING
) {
177 smart_str_appendc_ex(&str
, '"', persistent
);
179 smart_str_appendc_ex(&str
, ' ', persistent
);
181 smart_str_appendl_ex(&str
, ";\n", 2, persistent
);
182 return smart_str_extract(&str
);
185 void psi_parser_postprocess(struct psi_parser
*P
)
189 struct psi_validate_scope scope
= {0};
191 psi_validate_scope_ctor(&scope
);
192 scope
.cpp
= P
->preproc
;
195 P
->flags
|= PSI_SILENT
;
197 ZEND_HASH_FOREACH_STR_KEY_PTR(&P
->preproc
->defs
, name
, scope
.macro
)
200 size_t processed
= 0;
201 struct psi_plist
*scanned
, *preproc
;
202 struct psi_parser_input
*I
;
205 if (scope
.macro
->sig
) {
207 } else if (!scope
.macro
->exp
) {
208 struct psi_token
*tok
;
209 if (psi_plist_count(scope
.macro
->tokens
) != 1) {
211 } else if (!psi_plist_get(scope
.macro
->tokens
, 0, &tok
)) {
213 } else if (tok
->type
!= PSI_T_QUOTED_STRING
) {
216 } else if (!psi_num_exp_validate(PSI_DATA(P
), scope
.macro
->exp
, &scope
)) {
218 } else if (psi_plist_count(scope
.macro
->tokens
) == 1) {
219 struct psi_token
*tok
;
220 if (!psi_plist_get(scope
.macro
->tokens
, 0, &tok
)) {
222 } else if (zend_string_equals(name
, tok
->text
)) {
227 cnst
= macro_to_constant(P
, name
, &scope
);
232 I
= psi_parser_open_string(P
, ZSTR_VAL(cnst
), ZSTR_LEN(cnst
));
233 zend_string_release(cnst
);
235 if (!(scanned
= psi_parser_scan(P
, I
))) {
236 psi_parser_input_free(&I
);
239 psi_parser_input_free(&I
);
241 if (!(preproc
= psi_parser_preprocess(P
, &scanned
))) {
242 psi_plist_free(scanned
);
246 parsed
= psi_parser_process(P
, preproc
, &processed
);
247 PSI_DEBUG_PRINT(PSI_DATA(P
), "PSI: processed=%zu success=%d\n",
248 processed
, (int) parsed
);
249 psi_plist_free(preproc
);
251 ZEND_HASH_FOREACH_END();
255 psi_validate_scope_dtor(&scope
);
258 bool psi_parser_parse(struct psi_parser
*P
, struct psi_parser_input
*I
)
260 struct psi_plist
*scanned
, *preproc
;
261 size_t processed
= 0;
265 if (!(scanned
= psi_parser_scan(P
, I
))) {
270 if (!(preproc
= psi_parser_preprocess(P
, &scanned
))) {
272 psi_plist_free(scanned
);
276 if (!psi_parser_process(P
, preproc
, &processed
)) {
278 psi_plist_free(preproc
);
282 psi_parser_postprocess(P
);
284 psi_plist_free(preproc
);
289 void psi_parser_dtor(struct psi_parser
*P
)
291 psi_cpp_free(&P
->preproc
);
292 psi_data_dtor(PSI_DATA(P
));
294 memset(P
, 0, sizeof(*P
));
297 void psi_parser_free(struct psi_parser
**P
)