OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*******************************************************************************/
-#include "php_psi_stdinc.h"
+#ifdef HAVE_CONFIG_H
+# include "config.h"
+#else
+# include "php_config.h"
+#endif
+
+#include <stdbool.h>
+#include <stdarg.h>
#include "php_psi.h"
#include "builtin.h"
#include "parser.h"
#include "cpp.h"
-#include <stdarg.h>
-
HashTable psi_builtins;
static bool has_include(struct psi_cpp *cpp, struct psi_token *target, struct psi_plist **args, struct psi_plist **res);
static bool has_include_next(struct psi_cpp *cpp, struct psi_token *target, struct psi_plist **args, struct psi_plist **res);
+static bool has_feature(struct psi_cpp *cpp, struct psi_token *target, struct psi_plist **args, struct psi_plist **res);
static bool builtin_constant_p(struct psi_cpp *cpp, struct psi_token *target, struct psi_plist **args, struct psi_plist **res);
+static bool signed__(struct psi_cpp *cpp, struct psi_token *target, struct psi_plist **args, struct psi_plist **res);
+static bool BASE_FILE__(struct psi_cpp *cpp, struct psi_token *target, struct psi_plist **args, struct psi_plist **res);
static bool COUNTER__(struct psi_cpp *cpp, struct psi_token *target, struct psi_plist **args, struct psi_plist **res);
+static bool DATE__(struct psi_cpp *cpp, struct psi_token *target, struct psi_plist **args, struct psi_plist **res);
+static bool FILE__(struct psi_cpp *cpp, struct psi_token *target, struct psi_plist **args, struct psi_plist **res);
+static bool INCLUDE_LEVEL__(struct psi_cpp *cpp, struct psi_token *target, struct psi_plist **args, struct psi_plist **res);
+static bool LINE__(struct psi_cpp *cpp, struct psi_token *target, struct psi_plist **args, struct psi_plist **res);
+static bool TIME__(struct psi_cpp *cpp, struct psi_token *target, struct psi_plist **args, struct psi_plist **res);
+static bool TIMESTAMP__(struct psi_cpp *cpp, struct psi_token *target, struct psi_plist **args, struct psi_plist **res);
static inline struct psi_plist *builtin_sig(token_t typ, ...)
{
}
}
+PHP_MINIT_FUNCTION(psi_builtin);
PHP_MINIT_FUNCTION(psi_builtin)
{
#define PSI_BUILTIN(builtin, ...) do { \
zend_hash_init(&psi_builtins, 0, NULL, free_builtin, 1);
PSI_BUILTIN(has_include, PSI_T_CPP_HEADER);
PSI_BUILTIN(has_include_next, PSI_T_CPP_HEADER);
+ PSI_BUILTIN(has_feature, PSI_T_NAME);
PSI_BUILTIN(builtin_constant_p, PSI_T_NAME);
+
+ PSI_BUILTIN(signed__, -1);
+
+ PSI_BUILTIN(BASE_FILE__, -1);
PSI_BUILTIN(COUNTER__, -1);
+ PSI_BUILTIN(DATE__, -1);
+ PSI_BUILTIN(FILE__, -1);
+ PSI_BUILTIN(INCLUDE_LEVEL__, -1);
+ PSI_BUILTIN(LINE__, -1);
+ PSI_BUILTIN(TIME__, -1);
+ PSI_BUILTIN(TIMESTAMP__, -1);
return SUCCESS;
}
+PHP_MSHUTDOWN_FUNCTION(psi_builtin);
PHP_MSHUTDOWN_FUNCTION(psi_builtin)
{
zend_hash_destroy(&psi_builtins);
return false;
}
+static bool has_feature(struct psi_cpp *cpp, struct psi_token *target,
+ struct psi_plist **args, struct psi_plist **res_ptr)
+{
+ return false;
+}
+
static bool builtin_constant_p(struct psi_cpp *cpp, struct psi_token *target,
struct psi_plist **args, struct psi_plist **res_ptr)
{
return false;
}
+#define NEW_TOKEN(typ, str, len) \
+ psi_token_init((typ), (str), (len), (target)->col, (target)->line, (target)->file)
+
+#define ADD_TOKEN(tok) do { \
+ struct psi_token *tok__ = tok; \
+ if (!*res) { \
+ *res = psi_plist_init((psi_plist_dtor) psi_token_free); \
+ } \
+ *res = psi_plist_add(*res, &tok__); \
+} while (0)
+
+#define ADD_QUOTED_STRING(buf, len) do { \
+ ADD_TOKEN(NEW_TOKEN(PSI_T_QUOTED_STRING, buf, len)); \
+} while(0)
+
+#define ADD_QUOTED_ZSTRING(zs) do { \
+ zend_string *zs_ = zs; \
+ ADD_QUOTED_STRING(zs_->val, zs_->len); \
+} while (0)
+
+#define ADD_UNSIGNED_NUMBER(u) do { \
+ char buf[0x20]; \
+ unsigned u_ = u; \
+ size_t len = snprintf(buf, sizeof(buf), "%u", u_); \
+ struct psi_token *tok_ = NEW_TOKEN(PSI_T_NUMBER, buf, len); \
+ tok_->flags |= PSI_NUMBER_INT | PSI_NUMBER_U; \
+ ADD_TOKEN(tok_); \
+} while (0)
+
+static bool signed__(struct psi_cpp *cpp, struct psi_token *target,
+ struct psi_plist **args, struct psi_plist **res)
+{
+ ADD_TOKEN(NEW_TOKEN(PSI_T_SIGNED, "signed", 8));
+ return true;
+}
+
+static bool BASE_FILE__(struct psi_cpp *cpp, struct psi_token *target,
+ struct psi_plist **args, struct psi_plist **res)
+{
+ ADD_QUOTED_ZSTRING(cpp->parser->input->file);
+ return true;
+}
+
static bool COUNTER__(struct psi_cpp *cpp, struct psi_token *target,
struct psi_plist **args, struct psi_plist **res)
{
- struct psi_token *tok;
- char buf[0x20];
- size_t len = sprintf(buf, "%u", cpp->counter++);
+ ADD_UNSIGNED_NUMBER(cpp->counter++);
+ return true;
+}
+
+static bool DATE__(struct psi_cpp *cpp, struct psi_token *target,
+ struct psi_plist **args, struct psi_plist **res)
+{
+ char buf[12];
+ struct tm *tp;
+ time_t t = time(NULL);
+#if HAVE_LOCALTIME_R
+ struct tm tm = {0};
+ tp = localtime_r(&t, &tm);
+#else
+ tp = localtime(&t);
+#endif
+
+ strftime(buf, sizeof(buf), "%b %e %Y", tp);
+ ADD_QUOTED_STRING(buf, 11);
+ return true;
+}
+
+static bool FILE__(struct psi_cpp *cpp, struct psi_token *target,
+ struct psi_plist **args, struct psi_plist **res)
+{
+ ADD_QUOTED_ZSTRING(target->file);
+ return true;
+}
+
+static bool INCLUDE_LEVEL__(struct psi_cpp *cpp, struct psi_token *target,
+ struct psi_plist **args, struct psi_plist **res)
+{
+ ADD_UNSIGNED_NUMBER(cpp->include_level);
+ return true;
+}
- tok = psi_token_init(PSI_T_NUMBER, buf, len, target->col, target->line, target->file);
- *res = psi_plist_init((psi_plist_dtor) psi_token_free);
- *res = psi_plist_add(*res, &tok);
+static bool LINE__(struct psi_cpp *cpp, struct psi_token *target,
+ struct psi_plist **args, struct psi_plist **res)
+{
+ ADD_UNSIGNED_NUMBER(target->line);
+ return true;
+}
+
+static bool TIME__(struct psi_cpp *cpp, struct psi_token *target,
+ struct psi_plist **args, struct psi_plist **res)
+{
+ char buf[9];
+ struct tm *tp;
+ time_t t = time(NULL);
+#if HAVE_LOCALTIME_R
+ struct tm tm = {0};
+ tp = localtime_r(&t, &tm);
+#else
+ tp = localtime(&t);
+#endif
+ strftime(buf, sizeof(buf), "%H:%M:%S", tp);
+ ADD_QUOTED_STRING(buf, 8);
return true;
}
+
+static bool TIMESTAMP__(struct psi_cpp *cpp, struct psi_token *target,
+ struct psi_plist **args, struct psi_plist **res)
+{
+ char *str;
+#ifdef HAVE_CTIME_R
+ char buf[26];
+ str = ctime_r(&cpp->parser->input->lmod, buf);
+#else
+ str = ctime(&cpp->parser->input->lmod);
+#endif
+
+ /* kill EOL */
+ ADD_QUOTED_STRING(str, 24);
+ return true;
+}
+
+
+#ifdef __APPLE__
+#include <libkern/OSByteOrder.h>
+# define bswap_16(u) _OSSwapInt16(u)
+# define bswap_32(u) _OSSwapInt32(u)
+# define bswap_64(u) _OSSwapInt64(u)
+#elif defined(__FreeBSD__)
+# include <sys/endian.h>
+# define bswap_16(u) bswap16(u)
+# define bswap_32(u) bswap32(u)
+# define bswap_64(u) bswap64(u)
+#elif defined(__OpenBSD__)
+# include <sys/types.h>
+# define bswap_16(u) swap16(u)
+# define bswap_32(u) swap32(u)
+# define bswap_64(u) swap64(u)
+#elif defined(__NetBSD__)
+# include <sys/types.h>
+# include <machine/bswap.h>
+# define bswap_16(u) bswap16(u)
+# define bswap_32(u) bswap32(u)
+# define bswap_64(u) bswap64(u)
+#else
+# include <byteswap.h>
+#endif
+
+uint16_t psi_swap16(uint16_t u)
+{
+ return bswap_16(u);
+}
+
+uint32_t psi_swap32(uint32_t u)
+{
+ return bswap_32(u);
+}
+
+uint64_t psi_swap64(uint64_t u)
+{
+ return bswap_64(u);
+}