cpp: include psi.directory in cpp search path
[m6w6/ext-psi] / src / cpp.c
index e79a563185d8c0139c855c9a800a529067a4ca3b..29f8541afee2f04e3657e2e98bc08cf424651dc8 100644 (file)
--- a/src/cpp.c
+++ b/src/cpp.c
@@ -32,6 +32,8 @@
 #define PSI_CPP_PREDEF
 #include "php_psi_cpp.h"
 
+#include "php_psi.h"
+
 static void free_cpp_def(zval *p)
 {
        if (Z_TYPE_P(p) == IS_PTR) {
@@ -100,7 +102,8 @@ static bool psi_cpp_stage1(struct psi_cpp *cpp)
                struct psi_token *token = psi_cpp_tokiter_current(cpp);
 
                /* strip comments and attributes */
-               if (token->type == PSI_T_COMMENT || token->type == PSI_T_CPP_ATTRIBUTE) {
+               if (token->type == PSI_T_COMMENT
+                               || token->type == PSI_T_CPP_ATTRIBUTE) {
                        psi_cpp_tokiter_del_cur(cpp, true);
                        continue;
                }
@@ -125,6 +128,9 @@ static bool psi_cpp_stage1(struct psi_cpp *cpp)
                 */
 
                if (token->type == PSI_T_WHITESPACE) {
+                       if (name) {
+                               name = false;
+                       }
                        ws = true;
                        psi_cpp_tokiter_del_cur(cpp, true);
                        continue;
@@ -281,17 +287,20 @@ static bool psi_cpp_stage2(struct psi_cpp *cpp)
 
                                if (is_eol) {
                                        size_t processed = 0;
+                                       bool parsed = psi_parser_process(cpp->parser, parser_tokens, &processed);
 
-                                       if (!psi_parser_process(cpp->parser, parser_tokens, &processed)) {
-                                               psi_plist_free(parser_tokens);
-                                               return false;
-                                       }
+                                       /* EOL */
                                        psi_plist_pop(parser_tokens, NULL);
                                        psi_plist_clean(parser_tokens);
                                        do_cpp = false;
+
+                                       if (!parsed) {
+                                               psi_plist_free(parser_tokens);
+                                               return false;
+                                       }
                                } else {
                                        /* leave EOLs in the input stream, else we might end up
-                                        * with a hash not preceeded with a new line after include */
+                                        * with a hash not preceded with a new line after include */
                                        psi_cpp_tokiter_del_cur(cpp, false);
                                }
 
@@ -412,19 +421,25 @@ static inline bool try_include(struct psi_cpp *cpp, const char *path, bool *pars
 
 bool psi_cpp_include(struct psi_cpp *cpp, const char *file, unsigned flags)
 {
-       char path[PATH_MAX];
        bool parsed = false;
-       int p_len, f_len = strlen(file) - 2;
+       int f_len = strlen(file);
 
-       if (file[1] == '/') {
-               if (PATH_MAX > (p_len = snprintf(path, PATH_MAX, "%.*s", f_len, file + 1))) {
-                       if ((flags & PSI_CPP_INCLUDE_ONCE) && zend_hash_str_exists(&cpp->once, path, p_len)) {
-                               return true;
-                       }
-                       return try_include(cpp, path, &parsed) && parsed;
+       if (!(flags & PSI_CPP_INCLUDE_NEXT) || *file == '/') {
+               /* first try as is, full or relative path */
+               if ((flags & PSI_CPP_INCLUDE_ONCE) && zend_hash_str_exists(&cpp->once, file, f_len)) {
+                       return true;
                }
-       } else {
+               if (try_include(cpp, file, &parsed)) {
+                       /* found */
+                       return parsed;
+               }
+       }
+
+       /* look through search paths */
+       if (*file != '/') {
+               char path[PATH_MAX];
                const char *sep;
+               int p_len;
 
                if ((flags & PSI_CPP_INCLUDE_NEXT) && cpp->search) {
                        if ((sep = strchr(cpp->search, ':'))) {
@@ -436,7 +451,7 @@ bool psi_cpp_include(struct psi_cpp *cpp, const char *file, unsigned flags)
                }
 
                if (!(flags & PSI_CPP_INCLUDE_NEXT) || !cpp->search) {
-                       cpp->search = &psi_cpp_search[0];
+                       cpp->search = PSI_G(search_path);
                }
 
                do {
@@ -445,7 +460,7 @@ bool psi_cpp_include(struct psi_cpp *cpp, const char *file, unsigned flags)
                        sep = strchr(cpp->search, ':');
                        d_len = sep ? sep - cpp->search : strlen(cpp->search);
 
-                       if (PATH_MAX > (p_len = snprintf(path, PATH_MAX, "%.*s/%.*s", d_len, cpp->search, f_len, file + 1))) {
+                       if (PATH_MAX > (p_len = snprintf(path, PATH_MAX, "%.*s/%.*s", d_len, cpp->search, f_len, file))) {
                                if ((flags & PSI_CPP_INCLUDE_ONCE) && zend_hash_str_exists(&cpp->once, path, p_len)) {
                                        return true;
                                }
@@ -453,7 +468,10 @@ bool psi_cpp_include(struct psi_cpp *cpp, const char *file, unsigned flags)
                                        break;
                                }
                        }
-                       cpp->search = sep + 1;
+
+                       if (sep) {
+                               cpp->search = sep + 1;
+                       }
                } while (sep);
        }