1 /*******************************************************************************
2 Copyright (c) 2017, 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"
40 #define PSI_CPP_SEARCH
41 #define PSI_CPP_PREDEF
42 #include "php_psi_predef.h"
44 HashTable psi_cpp_defaults
;
46 PHP_MINIT_FUNCTION(psi_cpp
);
47 PHP_MINIT_FUNCTION(psi_cpp
)
49 struct psi_parser parser
;
50 struct psi_parser_input
*predef
;
52 PSI_G(search_path
) = pemalloc(strlen(PSI_G(directory
)) + strlen(psi_cpp_search
) + 1 + 1, 1);
53 sprintf(PSI_G(search_path
), "%s:%s", PSI_G(directory
), psi_cpp_search
);
55 if (!psi_parser_init(&parser
, psi_error_wrapper
, PSI_SILENT
)) {
59 if (!(predef
= psi_parser_open_string(&parser
, psi_cpp_predef
, sizeof(psi_cpp_predef
) - 1))) {
60 psi_parser_dtor(&parser
);
64 if (!psi_parser_parse(&parser
, predef
)) {
65 psi_parser_input_free(&predef
);
66 psi_parser_dtor(&parser
);
69 psi_parser_input_free(&predef
);
71 zend_hash_init(&psi_cpp_defaults
, 0, NULL
, NULL
, 1);
72 zend_hash_copy(&psi_cpp_defaults
, &parser
.preproc
->defs
, NULL
);
74 psi_parser_dtor(&parser
);
79 PHP_MSHUTDOWN_FUNCTION(psi_cpp
);
80 PHP_MSHUTDOWN_FUNCTION(psi_cpp
)
82 struct psi_cpp_macro_decl
*macro
;
84 ZEND_HASH_FOREACH_PTR(&psi_cpp_defaults
, macro
)
86 psi_cpp_macro_decl_free(¯o
);
88 ZEND_HASH_FOREACH_END();
90 zend_hash_destroy(&psi_cpp_defaults
);
95 static void free_cpp_def(zval
*p
)
97 if (Z_TYPE_P(p
) == IS_PTR
) {
98 struct psi_cpp_macro_decl
*macro
= Z_PTR_P(p
);
100 if (!zend_hash_exists(&psi_cpp_defaults
, macro
->token
->text
)) {
101 psi_cpp_macro_decl_free(¯o
);
106 struct psi_cpp
*psi_cpp_init(struct psi_parser
*P
)
108 struct psi_cpp
*cpp
= pecalloc(1, sizeof(*cpp
), 1);
111 zend_hash_init(&cpp
->once
, 0, NULL
, NULL
, 1);
112 zend_hash_init(&cpp
->defs
, 0, NULL
, free_cpp_def
, 1);
113 zend_hash_copy(&cpp
->defs
, &psi_cpp_defaults
, NULL
);
114 zend_hash_init(&cpp
->expanding
, 0, NULL
, NULL
, 1);
119 static char *include_flavor
[] = {
125 void psi_cpp_free(struct psi_cpp
**cpp_ptr
)
128 struct psi_cpp
*cpp
= *cpp_ptr
;
131 zend_hash_destroy(&cpp
->defs
);
132 zend_hash_destroy(&cpp
->once
);
133 zend_hash_destroy(&cpp
->expanding
);
138 static bool psi_cpp_stage1(struct psi_cpp
*cpp
)
140 bool name
= false, define
= false, hash
= false, eol
= true, esc
= false, ws
= false;
142 PSI_DEBUG_PRINT(cpp
->parser
, "PSI: CPP %s\n", "stage1");
144 psi_cpp_tokiter_reset(cpp
);
145 while (psi_cpp_tokiter_valid(cpp
)) {
146 struct psi_token
*token
= psi_cpp_tokiter_current(cpp
);
148 /* strip comments and attributes */
149 if (token
->type
== PSI_T_COMMENT
150 || token
->type
== PSI_T_CPP_ATTRIBUTE
) {
151 psi_cpp_tokiter_del_cur(cpp
, true);
155 /* line continuations */
156 if (token
->type
== PSI_T_EOL
) {
158 psi_cpp_tokiter_del_prev(cpp
, true);
159 psi_cpp_tokiter_del_cur(cpp
, true);
163 } else if (token
->type
== PSI_T_BSLASH
) {
169 /* this whole turf is needed to distinct between:
170 * #define foo (1,2,3)
174 if (token
->type
== PSI_T_WHITESPACE
) {
179 psi_cpp_tokiter_del_cur(cpp
, true);
183 switch (token
->type
) {
209 /* mask special token for parser */
210 struct psi_token
*no_ws
= psi_token_copy(token
);
212 no_ws
->type
= PSI_T_NO_WHITESPACE
;
213 zend_string_release(no_ws
->text
);
214 no_ws
->text
= psi_string_init_interned("\xA0", 1, 1);
215 psi_cpp_tokiter_add(cpp
, no_ws
);
221 name
= define
= hash
= eol
= false;
226 psi_cpp_tokiter_add_cur(cpp
);
227 psi_cpp_tokiter_next(cpp
);
233 static bool psi_cpp_stage2(struct psi_cpp
*cpp
)
235 bool is_eol
= true, do_expansion
= true, skip_paren
= false, skip_all
= false;
237 PSI_DEBUG_PRINT(cpp
->parser
, "PSI: CPP %s\n", "stage2");
239 psi_cpp_tokiter_reset(cpp
);
240 while (psi_cpp_tokiter_valid(cpp
)) {
241 struct psi_token
*current
= psi_cpp_tokiter_current(cpp
);
243 if (current
->type
== PSI_T_HASH
) {
248 } else if (current
->type
== PSI_T_EOL
) {
250 PSI_DEBUG_PRINT(cpp
->parser
, "PSI: CPP do_expansion=true, PSI_T_EOL\n");
256 psi_cpp_tokiter_del_cur(cpp
, true);
263 switch (current
->type
) {
266 PSI_DEBUG_PRINT(cpp
->parser
, "PSI: CPP do_expansion=false, PSI_T_DEFINE, skip_all\n");
268 do_expansion
= false;
278 PSI_DEBUG_PRINT(cpp
->parser
, "PSI: CPP do_expansion=false, PSI_T_{IF{,N},UN}DEF\n");
280 do_expansion
= false;
290 PSI_DEBUG_PRINT(cpp
->parser
, "PSI: CPP do_expansion=true, PSI_T_LPAREN, !skip_all, !skip_paren\n");
298 do_expansion
= !skip_all
;
300 PSI_DEBUG_PRINT(cpp
->parser
, "PSI: CPP do_expansion=%s, <- !skip_all\n", do_expansion
?"true":"false");
309 psi_debug_lock(PSI_DATA(cpp
->parser
));
310 PSI_DEBUG_PRINT(cpp
->parser
, "PSI: CPP skip ");
311 PSI_DEBUG_DUMP(cpp
->parser
, psi_token_dump
, current
);
312 psi_debug_unlock(PSI_DATA(cpp
->parser
));
314 psi_cpp_tokiter_del_cur(cpp
, true);
319 if (do_expansion
&& psi_cpp_defined(cpp
, current
)) {
320 bool expanded
= false;
322 if (psi_cpp_tokiter_expand(cpp
)) {
330 psi_cpp_tokiter_add_cur(cpp
);
332 if (cpp
->do_cpp
&& is_eol
) {
333 size_t processed
= 0;
338 parsed
= psi_parser_process(cpp
->parser
, cpp
->tokens
.exec
, &processed
);
340 /* leave EOLs in the input stream, else we might end up
341 * with a hash not preceded with a new line after include */
342 psi_plist_pop(cpp
->tokens
.exec
, NULL
);
343 psi_plist_clean(cpp
->tokens
.exec
);
346 psi_plist_free(cpp
->tokens
.exec
);
350 #if PSI_CPP_DEBUG > 1
351 PSI_DEBUG_DUMP(cpp
->parser
, psi_cpp_tokiter_dump
, cpp
);
355 psi_cpp_tokiter_next(cpp
);
358 psi_plist_free(cpp
->tokens
.exec
);
363 bool psi_cpp_process(struct psi_cpp
*cpp
, struct psi_plist
**tokens
,
364 struct psi_token
*expanding
)
367 struct psi_cpp temp
= *cpp
;
369 cpp
->tokens
.iter
= *tokens
;
370 cpp
->tokens
.next
= NULL
;
371 cpp
->tokens
.exec
= NULL
;
374 zend_hash_add_empty_element(&cpp
->expanding
, expanding
->text
);
376 if (psi_cpp_stage1(cpp
) && psi_cpp_stage2(cpp
)) {
380 zend_hash_del(&cpp
->expanding
, expanding
->text
);
383 if (cpp
->tokens
.next
) {
384 free(cpp
->tokens
.iter
);
385 cpp
->tokens
.iter
= cpp
->tokens
.next
;
386 cpp
->tokens
.next
= NULL
;
389 *tokens
= cpp
->tokens
.iter
;
391 if (temp
.tokens
.iter
) {
392 cpp
->tokens
.iter
= temp
.tokens
.iter
;
393 cpp
->tokens
.next
= temp
.tokens
.next
;
394 cpp
->tokens
.exec
= temp
.tokens
.exec
;
396 cpp
->index
= temp
.index
;
397 cpp
->skip
= temp
.skip
;
398 cpp
->level
= temp
.level
;
399 cpp
->seen
= temp
.seen
;
400 cpp
->do_cpp
= temp
.do_cpp
;
405 bool psi_cpp_defined(struct psi_cpp
*cpp
, struct psi_token
*tok
)
407 bool defined
= false;
409 if (tok
->type
== PSI_T_NAME
) {
410 if (psi_builtin_exists(tok
->text
)) {
412 } else if (!zend_hash_exists(&cpp
->expanding
, tok
->text
)) {
413 struct psi_macro_decl
*macro
= zend_hash_find_ptr(&cpp
->defs
, tok
->text
);
420 psi_debug_lock(PSI_DATA(cpp
->parser
));
421 PSI_DEBUG_PRINT(cpp
->parser
, "PSI: CPP defined -> %s ", defined
? "true" : "false");
423 struct psi_cpp_macro_decl
*macro
= zend_hash_find_ptr(&cpp
->defs
, tok
->text
);
425 PSI_DEBUG_PRINT(cpp
->parser
, " @ %s:%u ", macro
->token
->file
->val
, macro
->token
->line
);
430 PSI_DEBUG_PRINT(cpp
->parser
, " expanding=");
431 ZEND_HASH_FOREACH_STR_KEY(&cpp
->expanding
, key
)
433 PSI_DEBUG_PRINT(cpp
->parser
, "%s,", key
->val
);
435 ZEND_HASH_FOREACH_END();
436 PSI_DEBUG_PRINT(cpp
->parser
, "\t");
438 PSI_DEBUG_DUMP(cpp
->parser
, psi_token_dump
, tok
);
439 psi_debug_unlock(PSI_DATA(cpp
->parser
));
446 void psi_cpp_define(struct psi_cpp
*cpp
, struct psi_cpp_macro_decl
*decl
)
448 struct psi_cpp_macro_decl
*old
= zend_hash_find_ptr(&cpp
->defs
, decl
->token
->text
);
450 if (old
&& !psi_cpp_macro_decl_equal(old
, decl
)) {
451 cpp
->parser
->error(PSI_DATA(cpp
->parser
), decl
->token
, PSI_WARNING
,
452 "'%s' redefined", decl
->token
->text
->val
);
453 cpp
->parser
->error(PSI_DATA(cpp
->parser
), old
->token
, PSI_WARNING
,
454 "'%s' previously defined", old
->token
->text
->val
);
457 psi_debug_lock(PSI_DATA(cpp
->parser
));
459 PSI_DEBUG_PRINT(cpp
->parser
, "PSI: CPP MACRO num_exp -> ");
461 PSI_DEBUG_PRINT(cpp
->parser
, "PSI: CPP MACRO decl -> ");
463 PSI_DEBUG_DUMP(cpp
->parser
, psi_cpp_macro_decl_dump
, decl
);
464 PSI_DEBUG_PRINT(cpp
->parser
, "\n");
465 psi_debug_unlock(PSI_DATA(cpp
->parser
));
467 zend_hash_update_ptr(&cpp
->defs
, decl
->token
->text
, decl
);
470 bool psi_cpp_undef(struct psi_cpp
*cpp
, struct psi_token
*tok
)
472 return SUCCESS
== zend_hash_del(&cpp
->defs
, tok
->text
);
475 bool psi_cpp_if(struct psi_cpp
*cpp
, struct psi_cpp_exp
*exp
)
477 struct psi_validate_scope scope
= {0};
480 if (!psi_num_exp_validate(PSI_DATA(cpp
->parser
), exp
->data
.num
, &scope
)) {
483 if (!psi_num_exp_get_long(exp
->data
.num
, NULL
, cpp
)) {
489 bool psi_cpp_include(struct psi_cpp
*cpp
, const struct psi_token
*file
, unsigned flags
)
493 struct psi_plist
*tokens
;
494 struct psi_parser_input
*include
;
496 if (!psi_cpp_has_include(cpp
, file
, flags
, path
)) {
500 if (flags
& PSI_CPP_INCLUDE_ONCE
) {
501 if (zend_hash_str_exists(&cpp
->once
, path
, strlen(path
))) {
506 PSI_DEBUG_PRINT(cpp
->parser
, "PSI: CPP %s opening %s\n",
507 include_flavor
[flags
], path
);
509 include
= psi_parser_open_file(cpp
->parser
, path
, false);
514 zend_hash_str_add_empty_element(&cpp
->once
, path
, strlen(path
));
516 PSI_DEBUG_PRINT(cpp
->parser
, "PSI: CPP include scanning %s\n", path
);
518 tokens
= psi_parser_scan(cpp
->parser
, include
);
519 psi_parser_input_free(&include
);
525 parsed
= psi_cpp_process(cpp
, &tokens
, NULL
);
527 psi_plist_free(tokens
);
531 psi_cpp_tokiter_add_range(cpp
, psi_plist_count(tokens
), psi_plist_eles(tokens
));
539 # define eaccess access
541 bool psi_cpp_has_include(struct psi_cpp
*cpp
, const struct psi_token
*file
, unsigned flags
, char *path
)
549 if (file
->type
== PSI_T_QUOTED_STRING
&& (!(flags
& PSI_CPP_INCLUDE_NEXT
) || file
->text
->val
[0] == '/')) {
550 /* first try as is, full or relative path */
551 if (file
->text
->val
[0] == '/') {
552 path
= file
->text
->val
;
557 strncpy(path
, file
->file
->val
, PATH_MAX
);
562 assert(len
+ file
->text
->len
+ 1 < PATH_MAX
);
564 memmove(path
, dir
, len
);
566 memcpy(&(path
)[len
+ 1], file
->text
->val
, file
->text
->len
+ 1);
569 PSI_DEBUG_PRINT(cpp
->parser
, "PSI: CPP %s trying %s\n",
570 include_flavor
[flags
], path
);
571 if (0 == eaccess(path
, R_OK
)) {
576 /* look through search paths */
577 if (file
->text
->val
[0] != '/') {
581 if ((flags
& PSI_CPP_INCLUDE_NEXT
) && cpp
->search
) {
582 if ((sep
= strchr(cpp
->search
, ':'))) {
583 cpp
->search
= sep
+ 1;
585 /* point to end of string */
586 cpp
->search
+= strlen(cpp
->search
);
590 if (!(flags
& PSI_CPP_INCLUDE_NEXT
)) {
591 cpp
->search
= PSI_G(search_path
);
597 sep
= strchr(cpp
->search
, ':');
598 d_len
= sep
? sep
- cpp
->search
: strlen(cpp
->search
);
600 if (PATH_MAX
> (p_len
= snprintf(path
, PATH_MAX
, "%.*s/%.*s", d_len
, cpp
->search
, (int) file
->text
->len
, file
->text
->val
))) {
601 PSI_DEBUG_PRINT(cpp
->parser
, "PSI: CPP %s trying %s\n",
602 include_flavor
[flags
], path
);
603 if (0 == eaccess(path
, R_OK
)) {
609 cpp
->search
= sep
+ 1;