]> granicus.if.org Git - php/commitdiff
Added php/embed toolkit for embedding PHP engine into C/C++ applications.
authorEdin Kadribasic <edink@php.net>
Sun, 29 Sep 2002 02:45:25 +0000 (02:45 +0000)
committerEdin Kadribasic <edink@php.net>
Sun, 29 Sep 2002 02:45:25 +0000 (02:45 +0000)
See my post to php-dev on the subject.

Makefile.global
configure.in
main/php_embed.c [new file with mode: 0644]
main/php_embed.h [new file with mode: 0644]
win32/phpembed.dsp [new file with mode: 0644]

index 6541da6fe84fc7f498933bd4ec773a9ef1ffc928..94462271179dc8e2852abbc1efff5a6833cf2298 100644 (file)
@@ -22,8 +22,15 @@ php: $(PHP_GLOBAL_OBJS) $(PHP_SAPI_OBJS)
 sapi/cli/php: $(PHP_GLOBAL_OBJS) $(PHP_CLI_OBJS)
        $(BUILD_CLI)
 
-install: $(install_targets) 
+libphp.la: $(PHP_LIB_OBJS)
+       $(LIBTOOL) --mode=link $(CC) $(COMMON_FLAGS) $(CFLAGS_CLEAN) $(EXTRA_CFLAGS) $(EXTRA_LDFLAGS) $(LDFLAGS) $(PHP_RPATHS) $(PHP_LIB_OBJS) $(EXTRA_LIBS) $(ZEND_EXTRA_LIBS) -o $@
+
+libphp.so: $(PHP_LIB_OBJS)
+       $(LIBTOOL) --mode=link $(CC) -shared -export-dynamic $(COMMON_FLAGS) $(CFLAGS_CLEAN) $(EXTRA_CFLAGS) $(EXTRA_LDFLAGS) $(LDFLAGS) $(PHP_RPATHS) $(PHP_LIB_OBJS) $(EXTRA_LIBS) $(ZEND_EXTRA_LIBS) -o $@ && cp $@ .libs
 
+libs: libphp.la libphp.so
+
+install: $(install_targets) 
 
 install-cli: sapi/cli/php
        @echo "Installing PHP CLI binary:        $(INSTALL_ROOT)$(bindir)/"
@@ -62,7 +69,7 @@ test: sapi/cli/php
                        $(top_builddir)/sapi/cli/php -c php.ini-dist $(top_srcdir)/run-tests.php $(TESTS)
 
 clean:
-       find . -name \*.lo -o -name \*.o -o -name \*.la -o -name \*.a| xargs rm -f
+       find . -name \*.lo -o -name \*.o -o -name \*.la -o -name \*.a -o -name \*.so| xargs rm -f
        find . -name .libs -a -type d|xargs rm -rf
        rm -f libphp4.la php sapi/cli/php modules/* libs/*
 
index 2111454c3f7c5753838722e107abd19c012416ae..4bc97e4d1acefa122d0e810c259ad4ecb6531b68 100644 (file)
@@ -1078,6 +1078,12 @@ fi
 
 PHP_ADD_SOURCES_X(Zend, zend_execute.c,,PHP_GLOBAL_OBJS,,$flag)
 
+
+PHP_ADD_SOURCES_X(main, php_embed.c,, PHP_LIB_OBJS)
+PHP_LIB_OBJS="$PHP_GLOBAL_OBJS $PHP_LIB_OBJS main/internal_functions_cli.lo"
+PHP_SUBST(PHP_LIB_OBJS)
+
+
 PHP_ADD_BUILD_DIR(main)
 PHP_ADD_BUILD_DIR(regex)
 PHP_ADD_BUILD_DIR(sapi/$PHP_SAPI sapi/cli)
diff --git a/main/php_embed.c b/main/php_embed.c
new file mode 100644 (file)
index 0000000..bbaa56c
--- /dev/null
@@ -0,0 +1,228 @@
+/*
+   +----------------------------------------------------------------------+
+   | PHP Version 4                                                        |
+   +----------------------------------------------------------------------+
+   | Copyright (c) 1997-2002 The PHP Group                                |
+   +----------------------------------------------------------------------+
+   | This source file is subject to version 2.02 of the PHP license,      |
+   | that is bundled with this package in the file LICENSE, and is        |
+   | available at through the world-wide-web at                           |
+   | http://www.php.net/license/2_02.txt.                                 |
+   | If you did not receive a copy of the PHP license and are unable to   |
+   | obtain it through the world-wide-web, please send a note to          |
+   | license@php.net so we can mail you a copy immediately.               |
+   +----------------------------------------------------------------------+
+   | Author: Edin Kadrinasic <edink@php.net>                              |
+   +----------------------------------------------------------------------+
+*/
+/* $Id$ */
+
+#include "php_embed.h"
+
+#ifdef PHP_WIN32
+#include <io.h>
+#include <fcntl.h>
+#endif
+
+static char* php_embed_read_cookies(TSRMLS_D)
+{
+       return NULL;
+}
+
+static int php_embed_deactivate(TSRMLS_D)
+{
+       fflush(stdout);
+       return SUCCESS;
+}
+
+static inline size_t php_embed_single_write(const char *str, uint str_length)
+{
+#ifdef PHP_WRITE_STDOUT
+       long ret;
+
+       ret = write(STDOUT_FILENO, str, str_length);
+       if (ret <= 0) return 0;
+       return ret;
+#else
+       size_t ret;
+
+       ret = fwrite(str, 1, MIN(str_length, 16384), stdout);
+       return ret;
+#endif
+}
+
+
+static int php_embed_ub_write(const char *str, uint str_length TSRMLS_DC)
+{
+       const char *ptr = str;
+       uint remaining = str_length;
+       size_t ret;
+
+       while (remaining > 0) {
+               ret = php_embed_single_write(ptr, remaining);
+               if (!ret) {
+                       php_handle_aborted_connection();
+               }
+               ptr += ret;
+               remaining -= ret;
+       }
+
+       return str_length;
+}
+
+static void php_embed_flush(void *server_context)
+{
+       if (fflush(stdout)==EOF) {
+               php_handle_aborted_connection();
+       }
+}
+
+static void php_embed_send_header(sapi_header_struct *sapi_header, void *server_context TSRMLS_DC)
+{
+}
+
+static void php_embed_log_message(char *message)
+{
+       fprintf (stderr, "%s\n", message);
+}
+
+static void php_embed_register_variables(zval *track_vars_array TSRMLS_DC)
+{
+       php_import_environment_variables(track_vars_array TSRMLS_CC);
+}
+
+static int php_embed_startup(sapi_module_struct *sapi_module)
+{
+       if (php_module_startup(sapi_module, NULL, 0)==FAILURE) {
+               return FAILURE;
+       }
+       return SUCCESS;
+}
+
+static sapi_module_struct php_embed_module = {
+       "embedded",                    /* name */
+       "PHP Embedded Library",        /* pretty name */
+       
+       php_embed_startup,              /* startup */
+       php_module_shutdown_wrapper,   /* shutdown */
+  
+       NULL,                          /* activate */
+       php_embed_deactivate,           /* deactivate */
+  
+       php_embed_ub_write,             /* unbuffered write */
+       php_embed_flush,                /* flush */
+       NULL,                          /* get uid */
+       NULL,                          /* getenv */
+  
+       php_error,                     /* error handler */
+  
+       NULL,                          /* header handler */
+       NULL,                          /* send headers handler */
+       php_embed_send_header,          /* send header handler */
+       
+       NULL,                          /* read POST data */
+       php_embed_read_cookies,         /* read Cookies */
+  
+       php_embed_register_variables,   /* register server variables */
+       php_embed_log_message,          /* Log message */
+  
+       NULL,                          /* Block interruptions */
+       NULL,                          /* Unblock interruptions */
+
+       STANDARD_SAPI_MODULE_PROPERTIES
+};
+/* }}} */
+
+int php_embed_init(int argc, char **argv PTSRMLS_DC)
+{
+       zend_llist global_vars;
+#ifdef ZTS
+       zend_compiler_globals *compiler_globals;
+       zend_executor_globals *executor_globals;
+       php_core_globals *core_globals;
+       sapi_globals_struct *sapi_globals;
+       void ***tsrm_ls;
+#endif
+
+#ifdef HAVE_SIGNAL_H
+#if defined(SIGPIPE) && defined(SIG_IGN)
+       signal(SIGPIPE, SIG_IGN); /* ignore SIGPIPE in standalone mode so
+                                                                that sockets created via fsockopen()
+                                                                don't kill PHP if the remote site
+                                                                closes it.  in apache|apxs mode apache
+                                                                does that for us!  thies@thieso.net
+                                                                20000419 */
+#endif
+#endif
+
+#ifdef PHP_WIN32
+  _fmode = _O_BINARY;                  /*sets default for file streams to binary */
+  setmode(_fileno(stdin), O_BINARY);           /* make the stdio mode be binary */
+  setmode(_fileno(stdout), O_BINARY);          /* make the stdio mode be binary */
+  setmode(_fileno(stderr), O_BINARY);          /* make the stdio mode be binary */
+#endif
+
+#ifdef ZTS
+  tsrm_startup(1, 1, 0, NULL);
+#endif
+
+#ifdef ZTS
+  compiler_globals = ts_resource(compiler_globals_id);
+  executor_globals = ts_resource(executor_globals_id);
+  core_globals = ts_resource(core_globals_id);
+  sapi_globals = ts_resource(sapi_globals_id);
+  tsrm_ls = ts_resource(0);
+  *ptsrm_ls = tsrm_ls;
+#endif
+
+  sapi_startup(&php_embed_module);
+
+  if (php_module_startup(&php_embed_module, NULL, 0)==FAILURE) {
+         return FAILURE;
+  }
+  if (argv) {
+       php_embed_module.executable_location = argv[0];
+  }
+
+  zend_llist_init(&global_vars, sizeof(char *), NULL, 0);  
+
+  /* Set some Embedded PHP defaults */
+  SG(options) |= SAPI_OPTION_NO_CHDIR;
+  zend_alter_ini_entry("register_argc_argv", 19, "1", 1, PHP_INI_SYSTEM, PHP_INI_STAGE_ACTIVATE);
+  zend_alter_ini_entry("html_errors", 12, "0", 1, PHP_INI_SYSTEM, PHP_INI_STAGE_ACTIVATE);
+  zend_alter_ini_entry("implicit_flush", 15, "1", 1, PHP_INI_SYSTEM, PHP_INI_STAGE_ACTIVATE);
+  zend_alter_ini_entry("max_execution_time", 19, "0", 1, PHP_INI_SYSTEM, PHP_INI_STAGE_ACTIVATE);
+
+  SG(request_info).argc=argc;
+  SG(request_info).argv=argv;
+
+  if (php_request_startup(TSRMLS_C)==FAILURE) {
+         php_module_shutdown(TSRMLS_C);
+         return FAILURE;
+  }
+  
+  SG(headers_sent) = 1;
+  SG(request_info).no_headers = 1;
+  php_register_variable("PHP_SELF", "-", NULL TSRMLS_CC);
+
+  return SUCCESS;
+}
+
+void php_embed_shutdown(TSRMLS_D)
+{
+       php_request_shutdown((void *) 0);
+       php_module_shutdown(TSRMLS_C);
+#ifdef ZTS
+    tsrm_shutdown();
+#endif
+}
+
+/*
+ * Local variables:
+ * tab-width: 4
+ * c-basic-offset: 4
+ * End:
+ * vim600: sw=4 ts=4 fdm=marker
+ * vim<600: sw=4 ts=4
+ */
diff --git a/main/php_embed.h b/main/php_embed.h
new file mode 100644 (file)
index 0000000..740bc93
--- /dev/null
@@ -0,0 +1,60 @@
+/*
+   +----------------------------------------------------------------------+
+   | PHP Version 4                                                        |
+   +----------------------------------------------------------------------+
+   | Copyright (c) 1997-2002 The PHP Group                                |
+   +----------------------------------------------------------------------+
+   | This source file is subject to version 2.02 of the PHP license,      |
+   | that is bundled with this package in the file LICENSE, and is        |
+   | available at through the world-wide-web at                           |
+   | http://www.php.net/license/2_02.txt.                                 |
+   | If you did not receive a copy of the PHP license and are unable to   |
+   | obtain it through the world-wide-web, please send a note to          |
+   | license@php.net so we can mail you a copy immediately.               |
+   +----------------------------------------------------------------------+
+   | Author: Edin Kadrinasic <edink@php.net>                              |
+   +----------------------------------------------------------------------+
+*/
+/* $Id$ */
+
+#ifndef _PHP_EMBED_H_
+#define _PHP_EMBED_H_
+
+#include <main/php.h>
+#include <main/SAPI.h>
+#include <main/php_main.h>
+#include <main/php_variables.h>
+#include <main/php_ini.h>
+#include <zend_ini.h>
+
+#ifdef ZTS
+#define PTSRMLS_D        void ****ptsrm_ls
+#define PTSRMLS_DC       , PTSRMLS_D
+#define PTSRMLS_C        &tsrm_ls
+#define PTSRMLS_CC       , PTSRMLS_C
+#else
+#define PTSRMLS_D
+#define PTSRMLS_DC
+#define PTSRMLS_C
+#define PTSRMLS_CC
+#endif
+
+#define PHP_EMBED_START_BLOCK(x,y) { \
+    void ***tsrm_ls; \
+    php_embed_init(x, y PTSRMLS_CC); \
+    zend_first_try {
+
+#define PHP_EMBED_END_BLOCK() \
+  } zend_catch { \
+    /* int exit_status = EG(exit_status); */ \
+  } zend_end_try(); \
+  php_embed_shutdown(TSRMLS_C); \
+}
+
+BEGIN_EXTERN_C() 
+int php_embed_init(int argc, char **argv PTSRMLS_DC);
+void php_embed_shutdown(TSRMLS_D);
+END_EXTERN_C()
+
+
+#endif /* _PHP_EMBED_H_ */
diff --git a/win32/phpembed.dsp b/win32/phpembed.dsp
new file mode 100644 (file)
index 0000000..77575d7
--- /dev/null
@@ -0,0 +1,100 @@
+# Microsoft Developer Studio Project File - Name="phpembed" - Package Owner=<4>\r
+# Microsoft Developer Studio Generated Build File, Format Version 6.00\r
+# ** DO NOT EDIT **\r
+\r
+# TARGTYPE "Win32 (x86) Static Library" 0x0104\r
+\r
+CFG=phpembed - Win32 Debug_TS\r
+!MESSAGE This is not a valid makefile. To build this project using NMAKE,\r
+!MESSAGE use the Export Makefile command and run\r
+!MESSAGE \r
+!MESSAGE NMAKE /f "phpembed.mak".\r
+!MESSAGE \r
+!MESSAGE You can specify a configuration when running NMAKE\r
+!MESSAGE by defining the macro CFG on the command line. For example:\r
+!MESSAGE \r
+!MESSAGE NMAKE /f "phpembed.mak" CFG="phpembed - Win32 Debug_TS"\r
+!MESSAGE \r
+!MESSAGE Possible choices for configuration are:\r
+!MESSAGE \r
+!MESSAGE "phpembed - Win32 Debug_TS" (based on "Win32 (x86) Static Library")\r
+!MESSAGE "phpembed - Win32 Release_TS" (based on "Win32 (x86) Static Library")\r
+!MESSAGE \r
+\r
+# Begin Project\r
+# PROP AllowPerConfigDependencies 0\r
+# PROP Scc_ProjName ""\r
+# PROP Scc_LocalPath ""\r
+CPP=cl.exe\r
+RSC=rc.exe\r
+\r
+!IF  "$(CFG)" == "phpembed - Win32 Debug_TS"\r
+\r
+# PROP BASE Use_MFC 0\r
+# PROP BASE Use_Debug_Libraries 1\r
+# PROP BASE Output_Dir "Debug_TS"\r
+# PROP BASE Intermediate_Dir "Debug_TS"\r
+# PROP BASE Target_Dir ""\r
+# PROP Use_MFC 0\r
+# PROP Use_Debug_Libraries 1\r
+# PROP Output_Dir "Debug_TS"\r
+# PROP Intermediate_Dir "Debug_TS"\r
+# PROP Target_Dir ""\r
+# ADD BASE CPP /nologo /W3 /Gm /GX /ZI /Od /D "WIN32" /D "_DEBUG" /D "_MBCS" /D "_LIB" /YX /FD /GZ /c\r
+# ADD CPP /nologo /MDd /W3 /Gm /GX /ZI /Od /I ".." /I "..\main" /I "..\Zend" /I "..\TSRM" /D "_DEBUG" /D "WIN32" /D "_MBCS" /D "_LIB" /D "ZTS" /D "ZEND_WIN32" /D "PHP_WIN32" /D ZEND_DEBUG=1 /YX /FD /GZ /c\r
+# ADD BASE RSC /l 0x406 /d "_DEBUG"\r
+# ADD RSC /l 0x406 /d "_DEBUG"\r
+BSC32=bscmake.exe\r
+# ADD BASE BSC32 /nologo\r
+# ADD BSC32 /nologo\r
+LIB32=link.exe -lib\r
+# ADD BASE LIB32 /nologo\r
+# ADD LIB32 /nologo /out:"..\Debug_TS\phpembed.lib"\r
+\r
+!ELSEIF  "$(CFG)" == "phpembed - Win32 Release_TS"\r
+\r
+# PROP BASE Use_MFC 0\r
+# PROP BASE Use_Debug_Libraries 0\r
+# PROP BASE Output_Dir "Release_TS"\r
+# PROP BASE Intermediate_Dir "Release_TS"\r
+# PROP BASE Target_Dir ""\r
+# PROP Use_MFC 0\r
+# PROP Use_Debug_Libraries 0\r
+# PROP Output_Dir "Release_TS"\r
+# PROP Intermediate_Dir "Release_TS"\r
+# PROP Target_Dir ""\r
+# ADD BASE CPP /nologo /W3 /GX /O2 /D "WIN32" /D "NDEBUG" /D "_MBCS" /D "_LIB" /YX /FD /c\r
+# ADD CPP /nologo /MD /W3 /GX /O2 /I ".." /I "..\main" /I "..\Zend" /I "..\TSRM" /D "NDEBUG" /D "WIN32" /D "_MBCS" /D "_LIB" /D "ZTS" /D "ZEND_WIN32" /D "PHP_WIN32" /D ZEND_DEBUG=0 /YX /FD /c\r
+# ADD BASE RSC /l 0x406 /d "NDEBUG"\r
+# ADD RSC /l 0x406 /d "NDEBUG"\r
+BSC32=bscmake.exe\r
+# ADD BASE BSC32 /nologo\r
+# ADD BSC32 /nologo\r
+LIB32=link.exe -lib\r
+# ADD BASE LIB32 /nologo\r
+# ADD LIB32 /nologo /out:"..\Release_TS\phpembed.lib"\r
+\r
+!ENDIF \r
+\r
+# Begin Target\r
+\r
+# Name "phpembed - Win32 Debug_TS"\r
+# Name "phpembed - Win32 Release_TS"\r
+# Begin Group "Source Files"\r
+\r
+# PROP Default_Filter "cpp;c;cxx;rc;def;r;odl;idl;hpj;bat"\r
+# Begin Source File\r
+\r
+SOURCE=..\main\php_embed.c\r
+# End Source File\r
+# End Group\r
+# Begin Group "Header Files"\r
+\r
+# PROP Default_Filter "h;hpp;hxx;hm;inl"\r
+# Begin Source File\r
+\r
+SOURCE=..\main\php_embed.h\r
+# End Source File\r
+# End Group\r
+# End Target\r
+# End Project\r