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"
36 #include <Zend/zend_smart_str.h>
40 struct psi_parser
*psi_parser_init(struct psi_parser
*P
, psi_error_cb error
,
44 P
= pemalloc(sizeof(*P
), 1);
46 memset(P
, 0, sizeof(*P
));
48 psi_data_ctor_with_dtors(PSI_DATA(P
), error
, flags
);
50 P
->preproc
= psi_cpp_init(P
);
55 struct psi_parser_input
*psi_parser_open_file(struct psi_parser
*P
,
56 const char *filename
, bool report_errors
)
60 struct psi_parser_input
*fb
;
62 if (stat(filename
, &sb
)) {
64 P
->error(PSI_DATA(P
), NULL
, PSI_WARNING
,
65 "Could not stat '%s': %s",
66 filename
, strerror(errno
));
71 if (!(fb
= pemalloc(sizeof(*fb
) + sb
.st_size
+ psi_parser_maxfill(), 1))) {
73 P
->error(PSI_DATA(P
), NULL
, PSI_WARNING
,
74 "Could not allocate %zu bytes for reading '%s': %s",
75 sb
.st_size
+ psi_parser_maxfill(), filename
, strerror(errno
));
80 if (!(fp
= fopen(filename
, "r"))) {
83 P
->error(PSI_DATA(P
), NULL
, PSI_WARNING
,
84 "Could not open '%s' for reading: %s",
85 filename
, strerror(errno
));
90 if (sb
.st_size
!= fread(fb
->buffer
, 1, sb
.st_size
, fp
)) {
94 P
->error(PSI_DATA(P
), NULL
, PSI_WARNING
,
95 "Could not read %zu bytes from '%s': %s",
96 sb
.st_size
+ psi_parser_maxfill(), filename
, strerror(errno
));
102 memset(fb
->buffer
+ sb
.st_size
, 0, psi_parser_maxfill());
104 fb
->length
= sb
.st_size
;
105 fb
->file
= psi_string_init_interned(filename
, strlen(filename
), 1);
110 struct psi_parser_input
*psi_parser_open_string(struct psi_parser
*P
,
111 const char *string
, size_t length
)
113 struct psi_parser_input
*sb
;
115 if (!(sb
= pemalloc(sizeof(*sb
) + length
+ psi_parser_maxfill(), 1))) {
116 P
->error(PSI_DATA(P
), NULL
, PSI_WARNING
,
117 "Could not allocate %zu bytes: %s",
118 length
+ psi_parser_maxfill(), strerror(errno
));
122 memcpy(sb
->buffer
, string
, length
);
123 memset(sb
->buffer
+ length
, 0, psi_parser_maxfill());
126 sb
->file
= psi_string_init_interned("<stdin>", strlen("<stdin>"), 1);
131 struct psi_plist
*psi_parser_preprocess(struct psi_parser
*P
,
132 struct psi_plist
**tokens
)
134 if (psi_cpp_process(P
->preproc
, tokens
)) {
140 bool psi_parser_process(struct psi_parser
*P
, struct psi_plist
*tokens
,
143 if (psi_plist_count(tokens
)) {
144 return 0 == psi_parser_proc_parse(P
, tokens
, processed
);
149 static inline zend_string
*macro_to_constant(struct psi_parser
*parser
,
150 zend_string
*name
, struct psi_validate_scope
*scope
)
155 struct psi_token
*tok
;
160 smart_str_appendl_ex(&str
, ZEND_STRL("const psi\\"), 1);
161 smart_str_append_ex(&str
, name
, 1);
162 smart_str_appendl_ex(&str
, ZEND_STRL(" = "), 1);
166 smart_str_append_printf(&str
, "const psi\\%s = ", name
->val
);
168 if (scope
->macro
->exp
) {
171 struct psi_dump dump
= {{.hn
= &astr
},
172 .fun
= (psi_dump_cb
) asprintf
};
174 struct psi_dump dump
= {{.hn
= &str
},
175 .fun
= (psi_dump_cb
) smart_str_append_printf
};
178 psi_num_exp_dump(&dump
, scope
->macro
->exp
);
181 smart_str_appends_ex(&str
, astr
, 1);
184 } else while (psi_plist_get(scope
->macro
->tokens
, i
++, &tok
)) {
185 if (tok
->type
== PSI_T_QUOTED_STRING
) {
186 smart_str_appendc_ex(&str
, '"', persistent
);
188 smart_str_append_ex(&str
, tok
->text
, persistent
);
189 if (tok
->type
== PSI_T_QUOTED_STRING
) {
190 smart_str_appendc_ex(&str
, '"', persistent
);
192 smart_str_appendc_ex(&str
, ' ', persistent
);
194 smart_str_appendl_ex(&str
, ";\n", 2, persistent
);
195 return smart_str_extract(&str
);
198 void psi_parser_postprocess(struct psi_parser
*P
)
202 struct psi_validate_scope scope
= {0};
204 psi_validate_scope_ctor(&scope
);
205 scope
.cpp
= P
->preproc
;
208 P
->flags
|= PSI_SILENT
;
210 ZEND_HASH_FOREACH_STR_KEY_PTR(&P
->preproc
->defs
, name
, scope
.macro
)
213 size_t processed
= 0;
214 struct psi_plist
*scanned
, *preproc
;
215 struct psi_parser_input
*I
;
218 if (scope
.macro
->sig
) {
220 } else if (!scope
.macro
->exp
) {
221 struct psi_token
*tok
;
222 if (psi_plist_count(scope
.macro
->tokens
) != 1) {
224 } else if (!psi_plist_get(scope
.macro
->tokens
, 0, &tok
)) {
226 } else if (tok
->type
!= PSI_T_QUOTED_STRING
) {
229 } else if (!psi_num_exp_validate(PSI_DATA(P
), scope
.macro
->exp
, &scope
)) {
233 cnst
= macro_to_constant(P
, name
, &scope
);
237 //fprintf(stderr, "PARSE: %s", ZSTR_VAL(cnst));
238 I
= psi_parser_open_string(P
, ZSTR_VAL(cnst
), ZSTR_LEN(cnst
));
239 zend_string_release(cnst
);
241 if (!(scanned
= psi_parser_scan(P
, I
))) {
242 psi_parser_input_free(&I
);
245 psi_parser_input_free(&I
);
247 if (!(preproc
= psi_parser_preprocess(P
, &scanned
))) {
248 psi_plist_free(scanned
);
252 parsed
= psi_parser_process(P
, preproc
, &processed
);
253 PSI_DEBUG_PRINT(PSI_DATA(P
), "PSI: processed=%zu success=%d\n",
254 processed
, (int) parsed
);
255 psi_plist_free(preproc
);
257 ZEND_HASH_FOREACH_END();
261 psi_validate_scope_dtor(&scope
);
264 bool psi_parser_parse(struct psi_parser
*P
, struct psi_parser_input
*I
)
266 struct psi_plist
*scanned
, *preproc
;
267 size_t processed
= 0;
269 if (!(scanned
= psi_parser_scan(P
, I
))) {
273 if (!(preproc
= psi_parser_preprocess(P
, &scanned
))) {
274 psi_plist_free(scanned
);
278 if (!psi_parser_process(P
, preproc
, &processed
)) {
279 psi_plist_free(preproc
);
283 psi_parser_postprocess(P
);
285 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
)