zend.c revision bda96e3c
1573b4602SAndi Gutmans/*
2573b4602SAndi Gutmans   +----------------------------------------------------------------------+
3573b4602SAndi Gutmans   | Zend Engine                                                          |
4573b4602SAndi Gutmans   +----------------------------------------------------------------------+
5c081ce62SXinchen Hui   | Copyright (c) 1998-2014 Zend Technologies Ltd. (http://www.zend.com) |
6573b4602SAndi Gutmans   +----------------------------------------------------------------------+
7d863d52aSSebastian Bergmann   | This source file is subject to version 2.00 of the Zend license,     |
8b5b11177SZeev Suraski   | that is bundled with this package in the file LICENSE, and is        |
9f68c7ff2SJames Cox   | available through the world-wide-web at the following url:           |
10d863d52aSSebastian Bergmann   | http://www.zend.com/license/2_00.txt.                                |
11b5b11177SZeev Suraski   | If you did not receive a copy of the Zend license and are unable to  |
12b5b11177SZeev Suraski   | obtain it through the world-wide-web, please send a note to          |
13b5b11177SZeev Suraski   | license@zend.com so we can mail you a copy immediately.              |
14573b4602SAndi Gutmans   +----------------------------------------------------------------------+
15573b4602SAndi Gutmans   | Authors: Andi Gutmans <andi@zend.com>                                |
16573b4602SAndi Gutmans   |          Zeev Suraski <zeev@zend.com>                                |
17573b4602SAndi Gutmans   +----------------------------------------------------------------------+
18573b4602SAndi Gutmans*/
19573b4602SAndi Gutmans
20333406bdSfoobar/* $Id$ */
21a6043d3eSRasmus Lerdorf
22573b4602SAndi Gutmans#include "zend.h"
23573b4602SAndi Gutmans#include "zend_extensions.h"
247080fa5bSAndi Gutmans#include "zend_modules.h"
25573b4602SAndi Gutmans#include "zend_constants.h"
26573b4602SAndi Gutmans#include "zend_list.h"
27da9faa2cSZeev Suraski#include "zend_API.h"
2800ed3badSZeev Suraski#include "zend_exceptions.h"
2913d840bcSZeev Suraski#include "zend_builtin_functions.h"
3054d53055SZeev Suraski#include "zend_ini.h"
31fcaa1c6dSDmitry Stogov#include "zend_vm.h"
3208b9fdc8SDavid Soria Parra#include "zend_dtrace.h"
33cf6ab0e9SAnatol Belski#include "zend_virtual_cwd.h"
34573b4602SAndi Gutmans
357a87fcbbSZeev Suraski#ifdef ZTS
36b4892511SJani Taskinen# define GLOBAL_FUNCTION_TABLE		global_function_table
37b4892511SJani Taskinen# define GLOBAL_CLASS_TABLE			global_class_table
38b4892511SJani Taskinen# define GLOBAL_CONSTANTS_TABLE		global_constants_table
39b4892511SJani Taskinen# define GLOBAL_AUTO_GLOBALS_TABLE	global_auto_globals_table
407a87fcbbSZeev Suraski#else
41b4892511SJani Taskinen# define GLOBAL_FUNCTION_TABLE		CG(function_table)
42b4892511SJani Taskinen# define GLOBAL_CLASS_TABLE			CG(class_table)
43b4892511SJani Taskinen# define GLOBAL_AUTO_GLOBALS_TABLE	CG(auto_globals)
44b4892511SJani Taskinen# define GLOBAL_CONSTANTS_TABLE		EG(zend_constants)
457a87fcbbSZeev Suraski#endif
467a87fcbbSZeev Suraski
47573b4602SAndi Gutmans/* true multithread-shared globals */
4892dd5e61SStanislav MalyshevZEND_API zend_class_entry *zend_standard_class_def = NULL;
49573b4602SAndi GutmansZEND_API int (*zend_printf)(const char *format, ...);
505dba4774SZeev SuraskiZEND_API zend_write_func_t zend_write;
5185ab4235SFelipe PenaZEND_API FILE *(*zend_fopen)(const char *filename, char **opened_path TSRMLS_DC);
52e52aac94SWez FurlongZEND_API int (*zend_stream_open_function)(const char *filename, zend_file_handle *handle TSRMLS_DC);
5393536507SStig S. BakkenZEND_API void (*zend_block_interruptions)(void);
5493536507SStig S. BakkenZEND_API void (*zend_unblock_interruptions)(void);
55aed4b086SKalle Sommer NielsenZEND_API void (*zend_ticks_function)(int ticks TSRMLS_DC);
56536221beSStanislav MalyshevZEND_API void (*zend_error_cb)(int type, const char *error_filename, const uint error_lineno, const char *format, va_list args);
57d45fd8a1SMarcus Boergerint (*zend_vspprintf)(char **pbuf, size_t max_len, const char *format, va_list ap);
5885a36e2bSZeev SuraskiZEND_API char *(*zend_getenv)(char *name, size_t name_len TSRMLS_DC);
59ab232458SDmitry StogovZEND_API char *(*zend_resolve_path)(const char *filename, int filename_len TSRMLS_DC);
60536221beSStanislav Malyshev
61bd115087SZeev Suraskivoid (*zend_on_timeout)(int seconds TSRMLS_DC);
62bd115087SZeev Suraski
634a25a774SDmitry Stogovstatic void (*zend_message_dispatcher_p)(long message, const void *data TSRMLS_DC);
64cf7384aaSFelipe Penastatic int (*zend_get_configuration_directive_p)(const char *name, uint name_length, zval *contents);
65d035af4aSZeev Suraski
66b4892511SJani Taskinenstatic ZEND_INI_MH(OnUpdateErrorReporting) /* {{{ */
674be862b0SZeev Suraski{
684be862b0SZeev Suraski	if (!new_value) {
69d3e50265SMarcus Boerger		EG(error_reporting) = E_ALL & ~E_NOTICE & ~E_STRICT & ~E_DEPRECATED;
704be862b0SZeev Suraski	} else {
714be862b0SZeev Suraski		EG(error_reporting) = atoi(new_value);
724be862b0SZeev Suraski	}
734be862b0SZeev Suraski	return SUCCESS;
744be862b0SZeev Suraski}
75b4892511SJani Taskinen/* }}} */
764be862b0SZeev Suraski
7771592cecSDmitry Stogovstatic ZEND_INI_MH(OnUpdateGCEnabled) /* {{{ */
7871592cecSDmitry Stogov{
7971592cecSDmitry Stogov	OnUpdateBool(entry, new_value, new_value_length, mh_arg1, mh_arg2, mh_arg3, stage TSRMLS_CC);
8071592cecSDmitry Stogov
8171592cecSDmitry Stogov	if (GC_G(gc_enabled)) {
8271592cecSDmitry Stogov		gc_init(TSRMLS_C);
8371592cecSDmitry Stogov	}
8471592cecSDmitry Stogov
8571592cecSDmitry Stogov	return SUCCESS;
8671592cecSDmitry Stogov}
8771592cecSDmitry Stogov/* }}} */
8871592cecSDmitry Stogov
89bbf3d43cSMoriyoshi Koizumistatic ZEND_INI_MH(OnUpdateScriptEncoding) /* {{{ */
90bbf3d43cSMoriyoshi Koizumi{
91bbf3d43cSMoriyoshi Koizumi	if (!CG(multibyte)) {
92bbf3d43cSMoriyoshi Koizumi		return FAILURE;
93bbf3d43cSMoriyoshi Koizumi	}
94bbf3d43cSMoriyoshi Koizumi	if (!zend_multibyte_get_functions(TSRMLS_C)) {
95bbf3d43cSMoriyoshi Koizumi		return SUCCESS;
96bbf3d43cSMoriyoshi Koizumi	}
97bbf3d43cSMoriyoshi Koizumi	return zend_multibyte_set_script_encoding_by_string(new_value, new_value_length TSRMLS_CC);
98bbf3d43cSMoriyoshi Koizumi}
99bbf3d43cSMoriyoshi Koizumi/* }}} */
100bbf3d43cSMoriyoshi Koizumi
101bbf3d43cSMoriyoshi Koizumi
1024be862b0SZeev SuraskiZEND_INI_BEGIN()
103eb6fd52eSZeev Suraski	ZEND_INI_ENTRY("error_reporting",				NULL,		ZEND_INI_ALL,		OnUpdateErrorReporting)
10471592cecSDmitry Stogov	STD_ZEND_INI_BOOLEAN("zend.enable_gc",				"1",	ZEND_INI_ALL,		OnUpdateGCEnabled,      gc_enabled,     zend_gc_globals,        gc_globals)
105ab93d8c6SDmitry Stogov 	STD_ZEND_INI_BOOLEAN("zend.multibyte", "0", ZEND_INI_PERDIR, OnUpdateBool, multibyte,      zend_compiler_globals, compiler_globals)
106bbf3d43cSMoriyoshi Koizumi 	ZEND_INI_ENTRY("zend.script_encoding",			NULL,		ZEND_INI_ALL,		OnUpdateScriptEncoding)
107bbf3d43cSMoriyoshi Koizumi 	STD_ZEND_INI_BOOLEAN("zend.detect_unicode",			"1",	ZEND_INI_ALL,		OnUpdateBool, detect_unicode, zend_compiler_globals, compiler_globals)
10834d93f0cSIlia Alshanetsky#ifdef ZEND_SIGNALS
10934d93f0cSIlia Alshanetsky	STD_ZEND_INI_BOOLEAN("zend.signal_check", "0", ZEND_INI_SYSTEM, OnUpdateBool, check, zend_signal_globals_t, zend_signal_globals)
11034d93f0cSIlia Alshanetsky#endif
1114be862b0SZeev SuraskiZEND_INI_END()
1124be862b0SZeev Suraski
1134be862b0SZeev Suraski
1147a87fcbbSZeev Suraski#ifdef ZTS
115e06a1ed2SZeev SuraskiZEND_API int compiler_globals_id;
116e06a1ed2SZeev SuraskiZEND_API int executor_globals_id;
117b4892511SJani Taskinenstatic HashTable *global_function_table = NULL;
118b4892511SJani Taskinenstatic HashTable *global_class_table = NULL;
119b4892511SJani Taskinenstatic HashTable *global_constants_table = NULL;
120b4892511SJani Taskinenstatic HashTable *global_auto_globals_table = NULL;
12121698c12SDmitry Stogovstatic HashTable *global_persistent_list = NULL;
1227a87fcbbSZeev Suraski#endif
123573b4602SAndi Gutmans
124e801a99aSZeev SuraskiZEND_API zend_utility_values zend_uv;
125573b4602SAndi Gutmans
126573b4602SAndi Gutmans/* version information */
127573b4602SAndi Gutmansstatic char *zend_version_info;
128573b4602SAndi Gutmansstatic uint zend_version_info_length;
129c081ce62SXinchen Hui#define ZEND_CORE_VERSION_INFO	"Zend Engine v" ZEND_VERSION ", Copyright (c) 1998-2014 Zend Technologies\n"
13080bab9d9SAndrei Zmievski#define PRINT_ZVAL_INDENT 4
131573b4602SAndi Gutmans
132d3d02fbcSDmitry Stogovstatic void print_hash(zend_write_func_t write_func, HashTable *ht, int indent, zend_bool is_object TSRMLS_DC) /* {{{ */
133573b4602SAndi Gutmans{
134f4cfaf36SDmitry Stogov	zval *tmp;
135f4cfaf36SDmitry Stogov	zend_string *string_key;
13663873abaSStanislav Malyshev	HashPosition iterator;
1374757104eSZeev Suraski	ulong num_key;
138573b4602SAndi Gutmans	int i;
139573b4602SAndi Gutmans
140b4892511SJani Taskinen	for (i = 0; i < indent; i++) {
141d3d02fbcSDmitry Stogov		ZEND_PUTS_EX(" ");
142573b4602SAndi Gutmans	}
143d3d02fbcSDmitry Stogov	ZEND_PUTS_EX("(\n");
14480bab9d9SAndrei Zmievski	indent += PRINT_ZVAL_INDENT;
1457652a977SDmitry Stogov	ZEND_HASH_FOREACH_KEY_VAL(ht, num_key, string_key, tmp) {
146887189caSDmitry Stogov		if (Z_TYPE_P(tmp) == IS_INDIRECT) {
147887189caSDmitry Stogov			tmp = Z_INDIRECT_P(tmp);
148887189caSDmitry Stogov			if (Z_TYPE_P(tmp) == IS_UNDEF) {
149887189caSDmitry Stogov				continue;
150887189caSDmitry Stogov			}
151887189caSDmitry Stogov		}
152b4892511SJani Taskinen		for (i = 0; i < indent; i++) {
153d3d02fbcSDmitry Stogov			ZEND_PUTS_EX(" ");
154573b4602SAndi Gutmans		}
155d3d02fbcSDmitry Stogov		ZEND_PUTS_EX("[");
1567652a977SDmitry Stogov		if (string_key) {
1577652a977SDmitry Stogov			if (is_object) {
1587652a977SDmitry Stogov				const char *prop_name, *class_name;
1597652a977SDmitry Stogov				int prop_len;
1607652a977SDmitry Stogov				int mangled = zend_unmangle_property_name_ex(string_key->val, string_key->len, &class_name, &prop_name, &prop_len);
1617652a977SDmitry Stogov
1627652a977SDmitry Stogov				ZEND_WRITE_EX(prop_name, prop_len);
1637652a977SDmitry Stogov				if (class_name && mangled == SUCCESS) {
1647652a977SDmitry Stogov					if (class_name[0]=='*') {
1657652a977SDmitry Stogov						ZEND_PUTS_EX(":protected");
1667652a977SDmitry Stogov					} else {
1677652a977SDmitry Stogov						ZEND_PUTS_EX(":");
1687652a977SDmitry Stogov						ZEND_PUTS_EX(class_name);
1697652a977SDmitry Stogov						ZEND_PUTS_EX(":private");
170d329ce93SZeev Suraski					}
171d3d02fbcSDmitry Stogov				}
1727652a977SDmitry Stogov			} else {
1737652a977SDmitry Stogov				ZEND_WRITE_EX(string_key->val, string_key->len);
1747652a977SDmitry Stogov			}
1757652a977SDmitry Stogov		} else {
1767652a977SDmitry Stogov			char key[25];
1777652a977SDmitry Stogov			snprintf(key, sizeof(key), "%ld", num_key);
1787652a977SDmitry Stogov			ZEND_PUTS_EX(key);
179573b4602SAndi Gutmans		}
180d3d02fbcSDmitry Stogov		ZEND_PUTS_EX("] => ");
181f4cfaf36SDmitry Stogov		zend_print_zval_r_ex(write_func, tmp, indent+PRINT_ZVAL_INDENT TSRMLS_CC);
182d3d02fbcSDmitry Stogov		ZEND_PUTS_EX("\n");
1837652a977SDmitry Stogov	} ZEND_HASH_FOREACH_END();
18480bab9d9SAndrei Zmievski	indent -= PRINT_ZVAL_INDENT;
185b4892511SJani Taskinen	for (i = 0; i < indent; i++) {
186d3d02fbcSDmitry Stogov		ZEND_PUTS_EX(" ");
187573b4602SAndi Gutmans	}
188d3d02fbcSDmitry Stogov	ZEND_PUTS_EX(")\n");
189573b4602SAndi Gutmans}
190b4892511SJani Taskinen/* }}} */
191573b4602SAndi Gutmans
192b4892511SJani Taskinenstatic void print_flat_hash(HashTable *ht TSRMLS_DC) /* {{{ */
19327de54b0SAndi Gutmans{
194f4cfaf36SDmitry Stogov	zval *tmp;
195f4cfaf36SDmitry Stogov	zend_string *string_key;
196b4892511SJani Taskinen	HashPosition iterator;
197b4892511SJani Taskinen	ulong num_key;
198b4892511SJani Taskinen	int i = 0;
199b4892511SJani Taskinen
2007652a977SDmitry Stogov	ZEND_HASH_FOREACH_KEY_VAL(ht, num_key, string_key, tmp) {
201b4892511SJani Taskinen		if (i++ > 0) {
202b4892511SJani Taskinen			ZEND_PUTS(",");
203b4892511SJani Taskinen		}
204b4892511SJani Taskinen		ZEND_PUTS("[");
2057652a977SDmitry Stogov		if (string_key) {
2067652a977SDmitry Stogov			ZEND_WRITE(string_key->val, string_key->len);
2077652a977SDmitry Stogov		} else {
2087652a977SDmitry Stogov			zend_printf("%ld", num_key);
209b4892511SJani Taskinen		}
210b4892511SJani Taskinen		ZEND_PUTS("] => ");
211f4cfaf36SDmitry Stogov		zend_print_flat_zval_r(tmp TSRMLS_CC);
2127652a977SDmitry Stogov	} ZEND_HASH_FOREACH_END();
21327de54b0SAndi Gutmans}
214b4892511SJani Taskinen/* }}} */
2157a87fcbbSZeev Suraski
216b4892511SJani TaskinenZEND_API void zend_make_printable_zval(zval *expr, zval *expr_copy, int *use_copy) /* {{{ */
217573b4602SAndi Gutmans{
218f3a6c30cSXinchen Hui	if (Z_TYPE_P(expr) == IS_STRING) {
219da9faa2cSZeev Suraski		*use_copy = 0;
220da9faa2cSZeev Suraski		return;
221da9faa2cSZeev Suraski	}
222f3a6c30cSXinchen Hui
223f3a6c30cSXinchen Huiagain:
224b4892511SJani Taskinen	switch (Z_TYPE_P(expr)) {
22520f2e598SNikita Popov		case IS_NULL: {
22620f2e598SNikita Popov			TSRMLS_FETCH();
22776cc99feSDmitry Stogov			ZVAL_EMPTY_STRING(expr_copy);
2288c4ab98eSThies C. Arntzen			break;
22920f2e598SNikita Popov		}
230da9faa2cSZeev Suraski		case IS_BOOL:
231b4892511SJani Taskinen			if (Z_LVAL_P(expr)) {
232050d7e38SDmitry Stogov				// TODO: use interned string ???
23376cc99feSDmitry Stogov				ZVAL_NEW_STR(expr_copy, STR_INIT("1", 1, 0));
23491d7ba76SAndi Gutmans			} else {
23520f2e598SNikita Popov				TSRMLS_FETCH();
23676cc99feSDmitry Stogov				ZVAL_EMPTY_STRING(expr_copy);
23791d7ba76SAndi Gutmans			}
238da9faa2cSZeev Suraski			break;
239f4cfaf36SDmitry Stogov		case IS_RESOURCE: {
240f4cfaf36SDmitry Stogov				char buf[sizeof("Resource id #") + MAX_LENGTH_OF_LONG];
241f4cfaf36SDmitry Stogov				int len;
242f4cfaf36SDmitry Stogov
243f4cfaf36SDmitry Stogov				len = snprintf(buf, sizeof(buf), "Resource id #%ld", Z_RES_HANDLE_P(expr));
24476cc99feSDmitry Stogov				ZVAL_NEW_STR(expr_copy, STR_INIT(buf, len, 0));
245f4cfaf36SDmitry Stogov			}
246da9faa2cSZeev Suraski			break;
247da9faa2cSZeev Suraski		case IS_ARRAY:
248d81ea16eSStanislav Malyshev			zend_error(E_NOTICE, "Array to string conversion");
249050d7e38SDmitry Stogov			// TODO: use interned string ???
25076cc99feSDmitry Stogov			ZVAL_NEW_STR(expr_copy, STR_INIT("Array", sizeof("Array") - 1, 0));
251da9faa2cSZeev Suraski			break;
2521b282b35SAndi Gutmans		case IS_OBJECT:
2537901a221SAndi Gutmans			{
254ae2bfb78SSterling Hughes				TSRMLS_FETCH();
255637a4042SMarcus Boerger
2567b307fb9SXinchen Hui				if (zend_std_cast_object_tostring(expr, expr_copy, IS_STRING TSRMLS_CC) == SUCCESS) {
2577b307fb9SXinchen Hui					break;
2587b307fb9SXinchen Hui				}
259750e4e10SDmitry Stogov				if (Z_OBJ_HANDLER_P(expr, cast_object)) {
260f4cfaf36SDmitry Stogov					zval val;
261750e4e10SDmitry Stogov
26217bf59f8SDmitry Stogov					ZVAL_DUP_DEREF(&val, expr);
263f4cfaf36SDmitry Stogov					if (Z_OBJ_HANDLER_P(expr, cast_object)(&val, expr_copy, IS_STRING TSRMLS_CC) == SUCCESS) {
264750e4e10SDmitry Stogov						zval_ptr_dtor(&val);
265750e4e10SDmitry Stogov						break;
266750e4e10SDmitry Stogov					}
267750e4e10SDmitry Stogov					zval_ptr_dtor(&val);
268848c5c76SMarcus Boerger				}
269b3c3c6deSMarcus Boerger				if (!Z_OBJ_HANDLER_P(expr, cast_object) && Z_OBJ_HANDLER_P(expr, get)) {
270a0690ff2SXinchen Hui					zval rv;
271a0690ff2SXinchen Hui					zval *z = Z_OBJ_HANDLER_P(expr, get)(expr, &rv TSRMLS_CC);
2727addae50SMarcus Boerger
2734b4d634cSYiduo (David) Wang					Z_ADDREF_P(z);
274b4892511SJani Taskinen					if (Z_TYPE_P(z) != IS_OBJECT) {
275b3c3c6deSMarcus Boerger						zend_make_printable_zval(z, expr_copy, use_copy);
276b3c3c6deSMarcus Boerger						if (*use_copy) {
277f4cfaf36SDmitry Stogov							zval_ptr_dtor(z);
278b3c3c6deSMarcus Boerger						} else {
279b3c3c6deSMarcus Boerger							ZVAL_ZVAL(expr_copy, z, 0, 1);
280b3c3c6deSMarcus Boerger							*use_copy = 1;
2813b8b43a9SStanislav Malyshev						}
282b3c3c6deSMarcus Boerger						return;
2833b8b43a9SStanislav Malyshev					}
284f4cfaf36SDmitry Stogov					zval_ptr_dtor(z);
28571ba4427SMarcus Boerger				}
286f4cfaf36SDmitry Stogov				zend_error(EG(exception) ? E_ERROR : E_RECOVERABLE_ERROR, "Object of class %s could not be converted to string", Z_OBJCE_P(expr)->name->val);
28776cc99feSDmitry Stogov				ZVAL_EMPTY_STRING(expr_copy);
288ae2bfb78SSterling Hughes			}
289da9faa2cSZeev Suraski			break;
290e842ef9fSIlia Alshanetsky		case IS_DOUBLE:
291f4cfaf36SDmitry Stogov			ZVAL_DUP(expr_copy, expr);
292e842ef9fSIlia Alshanetsky			zend_locale_sprintf_double(expr_copy ZEND_FILE_LINE_CC);
293e842ef9fSIlia Alshanetsky			break;
294f3a6c30cSXinchen Hui		case IS_REFERENCE:
295f3a6c30cSXinchen Hui			expr = Z_REFVAL_P(expr);
296f3a6c30cSXinchen Hui			if (Z_TYPE_P(expr) == IS_STRING) {
297f3a6c30cSXinchen Hui				ZVAL_STR(expr_copy, STR_COPY(Z_STR_P(expr)));
298f3a6c30cSXinchen Hui				*use_copy = 1;
299f3a6c30cSXinchen Hui				return;
300f3a6c30cSXinchen Hui			}
301f3a6c30cSXinchen Hui			goto again;
302f3a6c30cSXinchen Hui			break;
303da9faa2cSZeev Suraski		default:
304f4cfaf36SDmitry Stogov			ZVAL_DUP(expr_copy, expr);
305da9faa2cSZeev Suraski			convert_to_string(expr_copy);
306da9faa2cSZeev Suraski			break;
307da9faa2cSZeev Suraski	}
308da9faa2cSZeev Suraski	*use_copy = 1;
309da9faa2cSZeev Suraski}
310b4892511SJani Taskinen/* }}} */
311da9faa2cSZeev Suraski
312bda96e3cSNikita PopovZEND_API int zend_print_zval(zval *expr, int indent TSRMLS_DC) /* {{{ */
313da9faa2cSZeev Suraski{
314bda96e3cSNikita Popov	return zend_print_zval_ex(zend_write, expr, indent TSRMLS_CC);
3155dba4774SZeev Suraski}
316b4892511SJani Taskinen/* }}} */
3175dba4774SZeev Suraski
318bda96e3cSNikita PopovZEND_API int zend_print_zval_ex(zend_write_func_t write_func, zval *expr, int indent TSRMLS_DC) /* {{{ */
3195dba4774SZeev Suraski{
320bda96e3cSNikita Popov	zend_string *str = zval_get_string(expr TSRMLS_CC);
321bda96e3cSNikita Popov	int len = str->len;
322da9faa2cSZeev Suraski
323bda96e3cSNikita Popov	if (len != 0) {
324bda96e3cSNikita Popov		write_func(str->val, len);
325573b4602SAndi Gutmans	}
326bda96e3cSNikita Popov
327bda96e3cSNikita Popov	STR_RELEASE(str);
328bda96e3cSNikita Popov	return len;
329573b4602SAndi Gutmans}
330b4892511SJani Taskinen/* }}} */
331573b4602SAndi Gutmans
332b4892511SJani TaskinenZEND_API void zend_print_flat_zval_r(zval *expr TSRMLS_DC) /* {{{ */
33327de54b0SAndi Gutmans{
334b4892511SJani Taskinen	switch (Z_TYPE_P(expr)) {
335b4892511SJani Taskinen		case IS_ARRAY:
336b4892511SJani Taskinen			ZEND_PUTS("Array (");
337b4892511SJani Taskinen			if (++Z_ARRVAL_P(expr)->nApplyCount>1) {
338b4892511SJani Taskinen				ZEND_PUTS(" *RECURSION*");
339b4892511SJani Taskinen				Z_ARRVAL_P(expr)->nApplyCount--;
340b4892511SJani Taskinen				return;
341b4892511SJani Taskinen			}
342b4892511SJani Taskinen			print_flat_hash(Z_ARRVAL_P(expr) TSRMLS_CC);
343b4892511SJani Taskinen			ZEND_PUTS(")");
344b4892511SJani Taskinen			Z_ARRVAL_P(expr)->nApplyCount--;
345b4892511SJani Taskinen			break;
346b4892511SJani Taskinen		case IS_OBJECT:
347b4892511SJani Taskinen		{
3489bba373aSStanislav Malyshev			HashTable *properties = NULL;
349f4cfaf36SDmitry Stogov			zend_string *class_name = NULL;
3508ce1211aSMarcus Boerger
351f30f3590SStanislav Malyshev			if (Z_OBJ_HANDLER_P(expr, get_class_name)) {
352ea85451bSDmitry Stogov				class_name = Z_OBJ_HANDLER_P(expr, get_class_name)(Z_OBJ_P(expr), 0 TSRMLS_CC);
3539bba373aSStanislav Malyshev			}
354b4892511SJani Taskinen			if (class_name) {
355f4cfaf36SDmitry Stogov				zend_printf("%s Object (", class_name->val);
356b4892511SJani Taskinen			} else {
357b4892511SJani Taskinen				zend_printf("%s Object (", "Unknown Class");
358b4892511SJani Taskinen			}
359288daccaSAndi Gutmans			if (class_name) {
360d062fb33SDmitry Stogov				STR_RELEASE(class_name);
3619bba373aSStanislav Malyshev			}
362f30f3590SStanislav Malyshev			if (Z_OBJ_HANDLER_P(expr, get_properties)) {
3639bba373aSStanislav Malyshev				properties = Z_OBJPROP_P(expr);
3649bba373aSStanislav Malyshev			}
365f30f3590SStanislav Malyshev			if (properties) {
3669bba373aSStanislav Malyshev				if (++properties->nApplyCount>1) {
3679bba373aSStanislav Malyshev					ZEND_PUTS(" *RECURSION*");
3689bba373aSStanislav Malyshev					properties->nApplyCount--;
3699bba373aSStanislav Malyshev					return;
3709bba373aSStanislav Malyshev				}
37124db7f18SSebastian Bergmann				print_flat_hash(properties TSRMLS_CC);
3729bba373aSStanislav Malyshev				properties->nApplyCount--;
3739bba373aSStanislav Malyshev			}
3749bba373aSStanislav Malyshev			ZEND_PUTS(")");
3759bba373aSStanislav Malyshev			break;
376b4892511SJani Taskinen		}
377b4892511SJani Taskinen		default:
378bda96e3cSNikita Popov			zend_print_variable(expr TSRMLS_CC);
379b4892511SJani Taskinen			break;
380b4892511SJani Taskinen	}
38127de54b0SAndi Gutmans}
382b4892511SJani Taskinen/* }}} */
383573b4602SAndi Gutmans
384b4892511SJani TaskinenZEND_API void zend_print_zval_r(zval *expr, int indent TSRMLS_DC) /* {{{ */
3855dba4774SZeev Suraski{
38624db7f18SSebastian Bergmann	zend_print_zval_r_ex(zend_write, expr, indent TSRMLS_CC);
3875dba4774SZeev Suraski}
388b4892511SJani Taskinen/* }}} */
3895dba4774SZeev Suraski
390b4892511SJani TaskinenZEND_API void zend_print_zval_r_ex(zend_write_func_t write_func, zval *expr, int indent TSRMLS_DC) /* {{{ */
391573b4602SAndi Gutmans{
392c6cba554SDmitry Stogov	ZVAL_DEREF(expr);
393b4892511SJani Taskinen	switch (Z_TYPE_P(expr)) {
394573b4602SAndi Gutmans		case IS_ARRAY:
395d3d02fbcSDmitry Stogov			ZEND_PUTS_EX("Array\n");
396b4892511SJani Taskinen			if (++Z_ARRVAL_P(expr)->nApplyCount>1) {
397d3d02fbcSDmitry Stogov				ZEND_PUTS_EX(" *RECURSION*");
398b4892511SJani Taskinen				Z_ARRVAL_P(expr)->nApplyCount--;
3996053f2a1SZeev Suraski				return;
4006053f2a1SZeev Suraski			}
401d3d02fbcSDmitry Stogov			print_hash(write_func, Z_ARRVAL_P(expr), indent, 0 TSRMLS_CC);
402b4892511SJani Taskinen			Z_ARRVAL_P(expr)->nApplyCount--;
403573b4602SAndi Gutmans			break;
404573b4602SAndi Gutmans		case IS_OBJECT:
405c259cb7cSAndi Gutmans			{
4068ce1211aSMarcus Boerger				HashTable *properties;
407f4cfaf36SDmitry Stogov				zend_string *class_name = NULL;
4088ce1211aSMarcus Boerger				int is_temp;
4098ce1211aSMarcus Boerger
410f30f3590SStanislav Malyshev				if (Z_OBJ_HANDLER_P(expr, get_class_name)) {
411ea85451bSDmitry Stogov					class_name = Z_OBJ_HANDLER_P(expr, get_class_name)(Z_OBJ_P(expr), 0 TSRMLS_CC);
4129bba373aSStanislav Malyshev				}
413f1fb77d1SIlia Alshanetsky				if (class_name) {
414f4cfaf36SDmitry Stogov					ZEND_PUTS_EX(class_name->val);
415f1fb77d1SIlia Alshanetsky				} else {
416d3d02fbcSDmitry Stogov					ZEND_PUTS_EX("Unknown Class");
417f1fb77d1SIlia Alshanetsky				}
418d3d02fbcSDmitry Stogov				ZEND_PUTS_EX(" Object\n");
419288daccaSAndi Gutmans				if (class_name) {
420bdeaf2efSDmitry Stogov					STR_RELEASE(class_name);
4219bba373aSStanislav Malyshev				}
4228ce1211aSMarcus Boerger				if ((properties = Z_OBJDEBUG_P(expr, is_temp)) == NULL) {
4238ce1211aSMarcus Boerger					break;
4249bba373aSStanislav Malyshev				}
4258ce1211aSMarcus Boerger				if (++properties->nApplyCount>1) {
426d3d02fbcSDmitry Stogov					ZEND_PUTS_EX(" *RECURSION*");
4279bba373aSStanislav Malyshev					properties->nApplyCount--;
4288ce1211aSMarcus Boerger					return;
4298ce1211aSMarcus Boerger				}
430d3d02fbcSDmitry Stogov				print_hash(write_func, properties, indent, 1 TSRMLS_CC);
4318ce1211aSMarcus Boerger				properties->nApplyCount--;
4328ce1211aSMarcus Boerger				if (is_temp) {
4338ce1211aSMarcus Boerger					zend_hash_destroy(properties);
4348ce1211aSMarcus Boerger					efree(properties);
435c259cb7cSAndi Gutmans				}
436c259cb7cSAndi Gutmans				break;
4376053f2a1SZeev Suraski			}
438573b4602SAndi Gutmans		default:
439bda96e3cSNikita Popov			zend_print_zval_ex(write_func, expr, indent TSRMLS_CC);
440573b4602SAndi Gutmans			break;
441573b4602SAndi Gutmans	}
442573b4602SAndi Gutmans}
443b4892511SJani Taskinen/* }}} */
444573b4602SAndi Gutmans
44585ab4235SFelipe Penastatic FILE *zend_fopen_wrapper(const char *filename, char **opened_path TSRMLS_DC) /* {{{ */
446573b4602SAndi Gutmans{
4475463dd5bSAndi Gutmans	if (opened_path) {
44827de5001SAndi Gutmans		*opened_path = estrdup(filename);
4495463dd5bSAndi Gutmans	}
450f311ab16SAndi Gutmans	return fopen(filename, "rb");
451573b4602SAndi Gutmans}
452b4892511SJani Taskinen/* }}} */
453573b4602SAndi Gutmans
4542f586e96SDmitry Stogov#ifdef ZTS
4558c885b89SDmitry Stogovstatic zend_bool asp_tags_default		  = 0;
4568c885b89SDmitry Stogovstatic zend_bool short_tags_default		  = 1;
4578c885b89SDmitry Stogovstatic zend_uint compiler_options_default = ZEND_COMPILE_DEFAULT;
4582f586e96SDmitry Stogov#else
4598c885b89SDmitry Stogov# define asp_tags_default			0
4608c885b89SDmitry Stogov# define short_tags_default			1
4618c885b89SDmitry Stogov# define compiler_options_default	ZEND_COMPILE_DEFAULT
4622f586e96SDmitry Stogov#endif
4632f586e96SDmitry Stogov
464b4892511SJani Taskinenstatic void zend_set_default_compile_time_values(TSRMLS_D) /* {{{ */
4658b794bb1SZeev Suraski{
4668b794bb1SZeev Suraski	/* default compile-time values */
4672f586e96SDmitry Stogov	CG(asp_tags) = asp_tags_default;
4682f586e96SDmitry Stogov	CG(short_tags) = short_tags_default;
4698c885b89SDmitry Stogov	CG(compiler_options) = compiler_options_default;
4708b794bb1SZeev Suraski}
471b4892511SJani Taskinen/* }}} */
4728b794bb1SZeev Suraski
473fcaa1c6dSDmitry Stogovstatic void zend_init_exception_op(TSRMLS_D) /* {{{ */
474fcaa1c6dSDmitry Stogov{
475fcaa1c6dSDmitry Stogov	memset(EG(exception_op), 0, sizeof(EG(exception_op)));
476fcaa1c6dSDmitry Stogov	EG(exception_op)[0].opcode = ZEND_HANDLE_EXCEPTION;
47794dd8372SDmitry Stogov	EG(exception_op)[0].op1_type = IS_UNUSED;
47894dd8372SDmitry Stogov	EG(exception_op)[0].op2_type = IS_UNUSED;
47994dd8372SDmitry Stogov	EG(exception_op)[0].result_type = IS_UNUSED;
480fcaa1c6dSDmitry Stogov	ZEND_VM_SET_OPCODE_HANDLER(EG(exception_op));
481fcaa1c6dSDmitry Stogov	EG(exception_op)[1].opcode = ZEND_HANDLE_EXCEPTION;
48294dd8372SDmitry Stogov	EG(exception_op)[1].op1_type = IS_UNUSED;
48394dd8372SDmitry Stogov	EG(exception_op)[1].op2_type = IS_UNUSED;
48494dd8372SDmitry Stogov	EG(exception_op)[1].result_type = IS_UNUSED;
485fcaa1c6dSDmitry Stogov	ZEND_VM_SET_OPCODE_HANDLER(EG(exception_op)+1);
486fcaa1c6dSDmitry Stogov	EG(exception_op)[2].opcode = ZEND_HANDLE_EXCEPTION;
48794dd8372SDmitry Stogov	EG(exception_op)[2].op1_type = IS_UNUSED;
48894dd8372SDmitry Stogov	EG(exception_op)[2].op2_type = IS_UNUSED;
48994dd8372SDmitry Stogov	EG(exception_op)[2].result_type = IS_UNUSED;
490fcaa1c6dSDmitry Stogov	ZEND_VM_SET_OPCODE_HANDLER(EG(exception_op)+2);
491fcaa1c6dSDmitry Stogov}
492fcaa1c6dSDmitry Stogov/* }}} */
493fcaa1c6dSDmitry Stogov
4947a87fcbbSZeev Suraski#ifdef ZTS
4953ce96eabSNikita Popovstatic void function_copy_ctor(zval *zv)
4963ce96eabSNikita Popov{
4973ce96eabSNikita Popov	zend_function *old_func = Z_FUNC_P(zv);
4983ce96eabSNikita Popov	Z_FUNC_P(zv) = pemalloc(sizeof(zend_internal_function), 1);
4993ce96eabSNikita Popov	memcpy(Z_FUNC_P(zv), old_func, sizeof(zend_internal_function));
5003ce96eabSNikita Popov	function_add_ref(Z_FUNC_P(zv));
5013ce96eabSNikita Popov}
5023ce96eabSNikita Popov
503b4892511SJani Taskinenstatic void compiler_globals_ctor(zend_compiler_globals *compiler_globals TSRMLS_DC) /* {{{ */
5047a87fcbbSZeev Suraski{
5057f4909d9SThies C. Arntzen	compiler_globals->compiled_filename = NULL;
5067f4909d9SThies C. Arntzen
507f7f5a5eaSStanislav Malyshev	compiler_globals->function_table = (HashTable *) malloc(sizeof(HashTable));
508b4892511SJani Taskinen	zend_hash_init_ex(compiler_globals->function_table, 100, NULL, ZEND_FUNCTION_DTOR, 1, 0);
5093ce96eabSNikita Popov	zend_hash_copy(compiler_globals->function_table, global_function_table, function_copy_ctor);
5107a87fcbbSZeev Suraski
511b4892511SJani Taskinen	compiler_globals->class_table = (HashTable *) malloc(sizeof(HashTable));
512b4892511SJani Taskinen	zend_hash_init_ex(compiler_globals->class_table, 10, NULL, ZEND_CLASS_DTOR, 1, 0);
5132b9b9afaSDmitry Stogov	zend_hash_copy(compiler_globals->class_table, global_class_table, zend_class_add_ref);
5149cf1a98dSThies C. Arntzen
515b4f3b9d3SZeev Suraski	zend_set_default_compile_time_values(TSRMLS_C);
51685b4df53SZeev Suraski
51785b4df53SZeev Suraski	CG(interactive) = 0;
5189d11db12SAndi Gutmans
5199d11db12SAndi Gutmans	compiler_globals->auto_globals = (HashTable *) malloc(sizeof(HashTable));
5209d11db12SAndi Gutmans	zend_hash_init_ex(compiler_globals->auto_globals, 8, NULL, NULL, 1, 0);
52120f2e598SNikita Popov	zend_hash_copy(compiler_globals->auto_globals, global_auto_globals_table, NULL /* empty element */);
5228768ab94SDmitry Stogov
5238768ab94SDmitry Stogov	compiler_globals->last_static_member = zend_hash_num_elements(compiler_globals->class_table);
5248768ab94SDmitry Stogov	if (compiler_globals->last_static_member) {
525f23e8576SDmitry Stogov		compiler_globals->static_members_table = calloc(compiler_globals->last_static_member, sizeof(zval**));
5268768ab94SDmitry Stogov	} else {
527f23e8576SDmitry Stogov		compiler_globals->static_members_table = NULL;
5288768ab94SDmitry Stogov	}
529ae0e5a0cSAntony Dovgal	compiler_globals->script_encoding_list = NULL;
5307a87fcbbSZeev Suraski}
531b4892511SJani Taskinen/* }}} */
5327a87fcbbSZeev Suraski
533b4892511SJani Taskinenstatic void compiler_globals_dtor(zend_compiler_globals *compiler_globals TSRMLS_DC) /* {{{ */
5347a87fcbbSZeev Suraski{
535ce98c73fSAndi Gutmans	if (compiler_globals->function_table != GLOBAL_FUNCTION_TABLE) {
53674a9ed7bSZeev Suraski		zend_hash_destroy(compiler_globals->function_table);
5378fb5ea30SMarcus Boerger		free(compiler_globals->function_table);
53874a9ed7bSZeev Suraski	}
539ce98c73fSAndi Gutmans	if (compiler_globals->class_table != GLOBAL_CLASS_TABLE) {
54074a9ed7bSZeev Suraski		zend_hash_destroy(compiler_globals->class_table);
5418fb5ea30SMarcus Boerger		free(compiler_globals->class_table);
54274a9ed7bSZeev Suraski	}
5438fb5ea30SMarcus Boerger	if (compiler_globals->auto_globals != GLOBAL_AUTO_GLOBALS_TABLE) {
5449d11db12SAndi Gutmans		zend_hash_destroy(compiler_globals->auto_globals);
5459d11db12SAndi Gutmans		free(compiler_globals->auto_globals);
5469d11db12SAndi Gutmans	}
547f23e8576SDmitry Stogov	if (compiler_globals->static_members_table) {
548f23e8576SDmitry Stogov		free(compiler_globals->static_members_table);
5498768ab94SDmitry Stogov	}
550bbf3d43cSMoriyoshi Koizumi	if (compiler_globals->script_encoding_list) {
551bbf3d43cSMoriyoshi Koizumi		pefree(compiler_globals->script_encoding_list, 1);
552bbf3d43cSMoriyoshi Koizumi	}
5538768ab94SDmitry Stogov	compiler_globals->last_static_member = 0;
5547a87fcbbSZeev Suraski}
555b4892511SJani Taskinen/* }}} */
556a9f9ae79SZeev Suraski
557b4892511SJani Taskinenstatic void executor_globals_ctor(zend_executor_globals *executor_globals TSRMLS_DC) /* {{{ */
558a9f9ae79SZeev Suraski{
5595fcae67eSZeev Suraski	zend_startup_constants(TSRMLS_C);
5605fcae67eSZeev Suraski	zend_copy_constants(EG(zend_constants), GLOBAL_CONSTANTS_TABLE);
5612c254ba7SZeev Suraski	zend_init_rsrc_plist(TSRMLS_C);
562fcaa1c6dSDmitry Stogov	zend_init_exception_op(TSRMLS_C);
563b4892511SJani Taskinen	EG(lambda_count) = 0;
56420f2e598SNikita Popov	ZVAL_UNDEF(&EG(user_error_handler));
56520f2e598SNikita Popov	ZVAL_UNDEF(&EG(user_exception_handler));
566ab30c8ccSZeev Suraski	EG(in_execution) = 0;
56755cf45abSDmitry Stogov	EG(in_autoload) = NULL;
568e6054d28SDerick Rethans	EG(current_execute_data) = NULL;
56952303217SMarcus Boerger	EG(current_module) = NULL;
5709d576212SAntony Dovgal	EG(exit_status) = 0;
571ee4f5ac1SFelipe Pena#if XPFPA_HAVE_CW
572ee4f5ac1SFelipe Pena	EG(saved_fpu_cw) = 0;
573ee4f5ac1SFelipe Pena#endif
57425710a40SFelipe Pena	EG(saved_fpu_cw_ptr) = NULL;
575d6ee9d48SAntony Dovgal	EG(active) = 0;
576a9f9ae79SZeev Suraski}
577b4892511SJani Taskinen/* }}} */
578a9f9ae79SZeev Suraski
579b4892511SJani Taskinenstatic void executor_globals_dtor(zend_executor_globals *executor_globals TSRMLS_DC) /* {{{ */
580a9f9ae79SZeev Suraski{
5812c254ba7SZeev Suraski	zend_ini_shutdown(TSRMLS_C);
58221698c12SDmitry Stogov	if (&executor_globals->persistent_list != global_persistent_list) {
58321698c12SDmitry Stogov		zend_destroy_rsrc_list(&executor_globals->persistent_list TSRMLS_CC);
58421698c12SDmitry Stogov	}
58521698c12SDmitry Stogov	if (executor_globals->zend_constants != GLOBAL_CONSTANTS_TABLE) {
58621698c12SDmitry Stogov		zend_hash_destroy(executor_globals->zend_constants);
58721698c12SDmitry Stogov		free(executor_globals->zend_constants);
58821698c12SDmitry Stogov	}
589a9f9ae79SZeev Suraski}
590b4892511SJani Taskinen/* }}} */
591a9f9ae79SZeev Suraski
592b4892511SJani Taskinenstatic void zend_new_thread_end_handler(THREAD_T thread_id TSRMLS_DC) /* {{{ */
593609d58a4SZeev Suraski{
594a84a2eaeSDmitry Stogov	if (zend_copy_ini_directives(TSRMLS_C) == SUCCESS) {
595a84a2eaeSDmitry Stogov		zend_ini_refresh_caches(ZEND_INI_STAGE_STARTUP TSRMLS_CC);
596a84a2eaeSDmitry Stogov	}
597609d58a4SZeev Suraski}
598b4892511SJani Taskinen/* }}} */
59966d9314bSZeev Suraski#endif
60066d9314bSZeev Suraski
601d46f8078SJon Parise#if defined(__FreeBSD__) || defined(__DragonFly__)
602d46f8078SJon Parise/* FreeBSD and DragonFly floating point precision fix */
6035928970dSAndi Gutmans#include <floatingpoint.h>
6045928970dSAndi Gutmans#endif
6057a87fcbbSZeev Suraski
606af316021SMarcus Boergerstatic void ini_scanner_globals_ctor(zend_ini_scanner_globals *scanner_globals_p TSRMLS_DC) /* {{{ */
607af316021SMarcus Boerger{
608af316021SMarcus Boerger	memset(scanner_globals_p, 0, sizeof(*scanner_globals_p));
609af316021SMarcus Boerger}
610af316021SMarcus Boerger/* }}} */
611af316021SMarcus Boerger
612af316021SMarcus Boergerstatic void php_scanner_globals_ctor(zend_php_scanner_globals *scanner_globals_p TSRMLS_DC) /* {{{ */
61354d53055SZeev Suraski{
614af316021SMarcus Boerger	memset(scanner_globals_p, 0, sizeof(*scanner_globals_p));
61554d53055SZeev Suraski}
616b4892511SJani Taskinen/* }}} */
61754d53055SZeev Suraski
618226c56feSJani Taskinenvoid zend_init_opcodes_handlers(void);
61954d53055SZeev Suraski
6202b9b9afaSDmitry Stogovstatic void module_destructor_zval(zval *zv) /* {{{ */
6212b9b9afaSDmitry Stogov{
6222b9b9afaSDmitry Stogov	zend_module_entry *module = (zend_module_entry*)Z_PTR_P(zv);
6232b9b9afaSDmitry Stogov
6242b9b9afaSDmitry Stogov	module_destructor(module);
6252b9b9afaSDmitry Stogov	free(module);
6262b9b9afaSDmitry Stogov}
6272b9b9afaSDmitry Stogov/* }}} */
6282b9b9afaSDmitry Stogov
6292b9b9afaSDmitry Stogovstatic void auto_global_dtor(zval *zv) /* {{{ */
6302b9b9afaSDmitry Stogov{
6312b9b9afaSDmitry Stogov	free(Z_PTR_P(zv));
6322b9b9afaSDmitry Stogov}
6332b9b9afaSDmitry Stogov/* }}} */
6342b9b9afaSDmitry Stogov
635f4cfaf36SDmitry Stogovstatic zend_bool php_auto_globals_create_globals(zend_string *name TSRMLS_DC) /* {{{ */
636f0c8366aSDmitry Stogov{
637f4cfaf36SDmitry Stogov	zval globals;
638f4cfaf36SDmitry Stogov
639f4cfaf36SDmitry Stogov	ZVAL_ARR(&globals, &EG(symbol_table));
640f4cfaf36SDmitry Stogov	ZVAL_NEW_REF(&globals, &globals);
641f4cfaf36SDmitry Stogov	zend_hash_update(&EG(symbol_table).ht, name, &globals);
642f0c8366aSDmitry Stogov	return 0;
643f0c8366aSDmitry Stogov}
644f0c8366aSDmitry Stogov/* }}} */
645f0c8366aSDmitry Stogov
6462faea53dSMarcus Boergerint zend_startup(zend_utility_functions *utility_functions, char **extensions TSRMLS_DC) /* {{{ */
647573b4602SAndi Gutmans{
648a9f9ae79SZeev Suraski#ifdef ZTS
6497c4a0838SZeev Suraski	zend_compiler_globals *compiler_globals;
650a9f9ae79SZeev Suraski	zend_executor_globals *executor_globals;
651609d58a4SZeev Suraski	extern ZEND_API ts_rsrc_id ini_scanner_globals_id;
652609d58a4SZeev Suraski	extern ZEND_API ts_rsrc_id language_scanner_globals_id;
653609d58a4SZeev Suraski#else
654af316021SMarcus Boerger	extern zend_ini_scanner_globals ini_scanner_globals;
655af316021SMarcus Boerger	extern zend_php_scanner_globals language_scanner_globals;
656609d58a4SZeev Suraski#endif
657a9f9ae79SZeev Suraski
65829ed52ffSDmitry Stogov	start_memory_manager(TSRMLS_C);
659a9f9ae79SZeev Suraski
660cf6ab0e9SAnatol Belski	virtual_cwd_startup(); /* Could use shutdown to free the main cwd but it would just slow it down for CGI */
661cf6ab0e9SAnatol Belski
662d46f8078SJon Parise#if defined(__FreeBSD__) || defined(__DragonFly__)
663d46f8078SJon Parise	/* FreeBSD and DragonFly floating point precision fix */
664c3340584SSascha Schumann	fpsetmask(0);
6655928970dSAndi Gutmans#endif
666309827ecSZeev Suraski
6674d44a5b7SAntony Dovgal	zend_startup_strtod();
668309827ecSZeev Suraski	zend_startup_extensions_mechanism();
669309827ecSZeev Suraski
670573b4602SAndi Gutmans	/* Set up utility functions and values */
67167f69743SZeev Suraski	zend_error_cb = utility_functions->error_function;
672573b4602SAndi Gutmans	zend_printf = utility_functions->printf_function;
6735dba4774SZeev Suraski	zend_write = (zend_write_func_t) utility_functions->write_function;
674573b4602SAndi Gutmans	zend_fopen = utility_functions->fopen_function;
675573b4602SAndi Gutmans	if (!zend_fopen) {
676573b4602SAndi Gutmans		zend_fopen = zend_fopen_wrapper;
677573b4602SAndi Gutmans	}
678e52aac94SWez Furlong	zend_stream_open_function = utility_functions->stream_open_function;
67956251a72SZeev Suraski	zend_message_dispatcher_p = utility_functions->message_handler;
68034d93f0cSIlia Alshanetsky#ifndef ZEND_SIGNALS
681573b4602SAndi Gutmans	zend_block_interruptions = utility_functions->block_interruptions;
682573b4602SAndi Gutmans	zend_unblock_interruptions = utility_functions->unblock_interruptions;
68334d93f0cSIlia Alshanetsky#endif
684903018efSZeev Suraski	zend_get_configuration_directive_p = utility_functions->get_configuration_directive;
685fafbf6d8SZeev Suraski	zend_ticks_function = utility_functions->ticks_function;
686bd115087SZeev Suraski	zend_on_timeout = utility_functions->on_timeout;
687d45fd8a1SMarcus Boerger	zend_vspprintf = utility_functions->vspprintf_function;
68885a36e2bSZeev Suraski	zend_getenv = utility_functions->getenv_function;
689ab232458SDmitry Stogov	zend_resolve_path = utility_functions->resolve_path_function;
690d035af4aSZeev Suraski
69108b9fdc8SDavid Soria Parra#if HAVE_DTRACE
69208b9fdc8SDavid Soria Parra/* build with dtrace support */
69308b9fdc8SDavid Soria Parra	zend_compile_file = dtrace_compile_file;
69470f83f35SDmitry Stogov	zend_execute_ex = dtrace_execute_ex;
69508b9fdc8SDavid Soria Parra	zend_execute_internal = dtrace_execute_internal;
69608b9fdc8SDavid Soria Parra#else
697c06692e9SZeev Suraski	zend_compile_file = compile_file;
69870f83f35SDmitry Stogov	zend_execute_ex = execute_ex;
6992c44fab3SDerick Rethans	zend_execute_internal = NULL;
70008b9fdc8SDavid Soria Parra#endif /* HAVE_SYS_SDT_H */
70108b9fdc8SDavid Soria Parra	zend_compile_string = compile_string;
702979da661SAndi Gutmans	zend_throw_exception_hook = NULL;
703573b4602SAndi Gutmans
704cf36abb2SAndi Gutmans	zend_init_opcodes_handlers();
705cf36abb2SAndi Gutmans
706573b4602SAndi Gutmans	/* set up version */
707573b4602SAndi Gutmans	zend_version_info = strdup(ZEND_CORE_VERSION_INFO);
708b4892511SJani Taskinen	zend_version_info_length = sizeof(ZEND_CORE_VERSION_INFO) - 1;
709b4892511SJani Taskinen
710b4892511SJani Taskinen	GLOBAL_FUNCTION_TABLE = (HashTable *) malloc(sizeof(HashTable));
711b4892511SJani Taskinen	GLOBAL_CLASS_TABLE = (HashTable *) malloc(sizeof(HashTable));
712b4892511SJani Taskinen	GLOBAL_AUTO_GLOBALS_TABLE = (HashTable *) malloc(sizeof(HashTable));
713b4892511SJani Taskinen	GLOBAL_CONSTANTS_TABLE = (HashTable *) malloc(sizeof(HashTable));
714573b4602SAndi Gutmans
7151263932aSZeev Suraski	zend_hash_init_ex(GLOBAL_FUNCTION_TABLE, 100, NULL, ZEND_FUNCTION_DTOR, 1, 0);
7161263932aSZeev Suraski	zend_hash_init_ex(GLOBAL_CLASS_TABLE, 10, NULL, ZEND_CLASS_DTOR, 1, 0);
7172b9b9afaSDmitry Stogov	zend_hash_init_ex(GLOBAL_AUTO_GLOBALS_TABLE, 8, NULL, auto_global_dtor, 1, 0);
718b4892511SJani Taskinen	zend_hash_init_ex(GLOBAL_CONSTANTS_TABLE, 20, NULL, ZEND_CONSTANT_DTOR, 1, 0);
7192eabb14dSAndi Gutmans
7202b9b9afaSDmitry Stogov	zend_hash_init_ex(&module_registry, 50, NULL, module_destructor_zval, 1, 0);
7214cb9a483SZeev Suraski	zend_init_rsrc_list_dtors();
722573b4602SAndi Gutmans
7237a87fcbbSZeev Suraski#ifdef ZTS
7242c254ba7SZeev Suraski	ts_allocate_id(&compiler_globals_id, sizeof(zend_compiler_globals), (ts_allocate_ctor) compiler_globals_ctor, (ts_allocate_dtor) compiler_globals_dtor);
7252c254ba7SZeev Suraski	ts_allocate_id(&executor_globals_id, sizeof(zend_executor_globals), (ts_allocate_ctor) executor_globals_ctor, (ts_allocate_dtor) executor_globals_dtor);
726af316021SMarcus Boerger	ts_allocate_id(&language_scanner_globals_id, sizeof(zend_php_scanner_globals), (ts_allocate_ctor) php_scanner_globals_ctor, NULL);
727af316021SMarcus Boerger	ts_allocate_id(&ini_scanner_globals_id, sizeof(zend_ini_scanner_globals), (ts_allocate_ctor) ini_scanner_globals_ctor, NULL);
7287c4a0838SZeev Suraski	compiler_globals = ts_resource(compiler_globals_id);
729a9f9ae79SZeev Suraski	executor_globals = ts_resource(executor_globals_id);
7305fcae67eSZeev Suraski
7318fb5ea30SMarcus Boerger	compiler_globals_dtor(compiler_globals TSRMLS_CC);
7322a8426a6SMarcus Boerger	compiler_globals->in_compilation = 0;
7338fb5ea30SMarcus Boerger	compiler_globals->function_table = (HashTable *) malloc(sizeof(HashTable));
7348fb5ea30SMarcus Boerger	compiler_globals->class_table = (HashTable *) malloc(sizeof(HashTable));
7358fb5ea30SMarcus Boerger
736f4b832d2SAndi Gutmans	*compiler_globals->function_table = *GLOBAL_FUNCTION_TABLE;
737f4b832d2SAndi Gutmans	*compiler_globals->class_table = *GLOBAL_CLASS_TABLE;
7389d11db12SAndi Gutmans	compiler_globals->auto_globals = GLOBAL_AUTO_GLOBALS_TABLE;
7395fcae67eSZeev Suraski
7405fcae67eSZeev Suraski	zend_hash_destroy(executor_globals->zend_constants);
7415fcae67eSZeev Suraski	*executor_globals->zend_constants = *GLOBAL_CONSTANTS_TABLE;
742762158aaSAndi Gutmans#else
743af316021SMarcus Boerger	ini_scanner_globals_ctor(&ini_scanner_globals TSRMLS_CC);
744af316021SMarcus Boerger	php_scanner_globals_ctor(&language_scanner_globals TSRMLS_CC);
745b4f3b9d3SZeev Suraski	zend_set_default_compile_time_values(TSRMLS_C);
746f4cfaf36SDmitry Stogov	ZVAL_UNDEF(&EG(user_error_handler));
747f4cfaf36SDmitry Stogov	ZVAL_UNDEF(&EG(user_exception_handler));
7487a87fcbbSZeev Suraski#endif
749826cc0dcSMarcus Boerger
750dd5c478bSDmitry Stogov	zend_interned_strings_init(TSRMLS_C);
7512faea53dSMarcus Boerger	zend_startup_builtin_functions(TSRMLS_C);
7522c254ba7SZeev Suraski	zend_register_standard_constants(TSRMLS_C);
7532b9b9afaSDmitry Stogov	zend_register_auto_global(STR_INIT("GLOBALS", sizeof("GLOBALS") - 1, 1), 1, php_auto_globals_create_globals TSRMLS_CC);
7547a87fcbbSZeev Suraski
755bfbe8618SZeev Suraski#ifndef ZTS
7562c254ba7SZeev Suraski	zend_init_rsrc_plist(TSRMLS_C);
757fcaa1c6dSDmitry Stogov	zend_init_exception_op(TSRMLS_C);
758bfbe8618SZeev Suraski#endif
759a9f9ae79SZeev Suraski
7602c254ba7SZeev Suraski	zend_ini_startup(TSRMLS_C);
76154d53055SZeev Suraski
76254d53055SZeev Suraski#ifdef ZTS
76354d53055SZeev Suraski	tsrm_set_new_thread_end_handler(zend_new_thread_end_handler);
76454d53055SZeev Suraski#endif
76554d53055SZeev Suraski
766573b4602SAndi Gutmans	return SUCCESS;
767573b4602SAndi Gutmans}
768b4892511SJani Taskinen/* }}} */
769573b4602SAndi Gutmans
770b4892511SJani Taskinenvoid zend_register_standard_ini_entries(TSRMLS_D) /* {{{ */
7714be862b0SZeev Suraski{
7724be862b0SZeev Suraski	int module_number = 0;
7734be862b0SZeev Suraski
7744be862b0SZeev Suraski	REGISTER_INI_ENTRIES();
7754be862b0SZeev Suraski}
776b4892511SJani Taskinen/* }}} */
7774be862b0SZeev Suraski
778711be83dSZeev Suraski/* Unlink the global (r/o) copies of the class, function and constant tables,
779711be83dSZeev Suraski * and use a fresh r/w copy for the startup thread
780711be83dSZeev Suraski */
781b4892511SJani Taskinenvoid zend_post_startup(TSRMLS_D) /* {{{ */
782711be83dSZeev Suraski{
783b4892511SJani Taskinen#ifdef ZTS
78472f19e9aSXinchen Hui	zend_encoding **script_encoding_list;
78572f19e9aSXinchen Hui
786711be83dSZeev Suraski	zend_compiler_globals *compiler_globals = ts_resource(compiler_globals_id);
7875fcae67eSZeev Suraski	zend_executor_globals *executor_globals = ts_resource(executor_globals_id);
788711be83dSZeev Suraski
7895fcae67eSZeev Suraski	*GLOBAL_FUNCTION_TABLE = *compiler_globals->function_table;
7905fcae67eSZeev Suraski	*GLOBAL_CLASS_TABLE = *compiler_globals->class_table;
7915fcae67eSZeev Suraski	*GLOBAL_CONSTANTS_TABLE = *executor_globals->zend_constants;
7922f586e96SDmitry Stogov
7932f586e96SDmitry Stogov	asp_tags_default = CG(asp_tags);
7942f586e96SDmitry Stogov	short_tags_default = CG(short_tags);
7958c885b89SDmitry Stogov	compiler_options_default = CG(compiler_options);
7968ce1211aSMarcus Boerger
797b817dab2SZeev Suraski	zend_destroy_rsrc_list(&EG(persistent_list) TSRMLS_CC);
7988fb5ea30SMarcus Boerger	free(compiler_globals->function_table);
7998fb5ea30SMarcus Boerger	free(compiler_globals->class_table);
80072f19e9aSXinchen Hui	if ((script_encoding_list = (zend_encoding **)compiler_globals->script_encoding_list)) {
80172f19e9aSXinchen Hui		compiler_globals_ctor(compiler_globals, tsrm_ls);
80272f19e9aSXinchen Hui		compiler_globals->script_encoding_list = (const zend_encoding **)script_encoding_list;
80372f19e9aSXinchen Hui	} else {
80472f19e9aSXinchen Hui		compiler_globals_ctor(compiler_globals, tsrm_ls);
80572f19e9aSXinchen Hui	}
8068fb5ea30SMarcus Boerger	free(EG(zend_constants));
807cf6ab0e9SAnatol Belski
808cf6ab0e9SAnatol Belski	virtual_cwd_deactivate(TSRMLS_C);
809cf6ab0e9SAnatol Belski
8105fcae67eSZeev Suraski	executor_globals_ctor(executor_globals, tsrm_ls);
81121698c12SDmitry Stogov	global_persistent_list = &EG(persistent_list);
812a84a2eaeSDmitry Stogov	zend_copy_ini_directives(TSRMLS_C);
813cf6ab0e9SAnatol Belski#else
814cf6ab0e9SAnatol Belski	virtual_cwd_deactivate(TSRMLS_C);
815711be83dSZeev Suraski#endif
816b4892511SJani Taskinen}
817b4892511SJani Taskinen/* }}} */
818711be83dSZeev Suraski
819b4892511SJani Taskinenvoid zend_shutdown(TSRMLS_D) /* {{{ */
820573b4602SAndi Gutmans{
82134d93f0cSIlia Alshanetsky#ifdef ZEND_SIGNALS
82234d93f0cSIlia Alshanetsky	zend_signal_shutdown(TSRMLS_C);
82334d93f0cSIlia Alshanetsky#endif
82484eb48f0SZeev Suraski#ifdef ZEND_WIN32
82584eb48f0SZeev Suraski	zend_shutdown_timeout_thread();
82684eb48f0SZeev Suraski#endif
8279e1beb4fSDmitry Stogov	zend_destroy_rsrc_list(&EG(persistent_list) TSRMLS_CC);
8281a1178a6SDmitry Stogov	zend_destroy_modules();
8293458373eSAndi Gutmans
830cf6ab0e9SAnatol Belski 	virtual_cwd_deactivate(TSRMLS_C);
831cf6ab0e9SAnatol Belski 	virtual_cwd_shutdown();
832cf6ab0e9SAnatol Belski
8337a87fcbbSZeev Suraski	zend_hash_destroy(GLOBAL_FUNCTION_TABLE);
83476b9acc1SAndi Gutmans	zend_hash_destroy(GLOBAL_CLASS_TABLE);
8353458373eSAndi Gutmans
8369d11db12SAndi Gutmans	zend_hash_destroy(GLOBAL_AUTO_GLOBALS_TABLE);
8379d11db12SAndi Gutmans	free(GLOBAL_AUTO_GLOBALS_TABLE);
8382da8b014Sfoobar
839d76cf1daSZeev Suraski	zend_shutdown_extensions(TSRMLS_C);
840573b4602SAndi Gutmans	free(zend_version_info);
84148b33032SSterling Hughes
842a2513618SMarcus Boerger	free(GLOBAL_FUNCTION_TABLE);
843a2513618SMarcus Boerger	free(GLOBAL_CLASS_TABLE);
84421698c12SDmitry Stogov
84548b33032SSterling Hughes	zend_hash_destroy(GLOBAL_CONSTANTS_TABLE);
846a54c7dcbSMarcus Boerger	free(GLOBAL_CONSTANTS_TABLE);
8474d44a5b7SAntony Dovgal	zend_shutdown_strtod();
848b4892511SJani Taskinen
84921698c12SDmitry Stogov#ifdef ZTS
8508fb5ea30SMarcus Boerger	GLOBAL_FUNCTION_TABLE = NULL;
8518fb5ea30SMarcus Boerger	GLOBAL_CLASS_TABLE = NULL;
8528fb5ea30SMarcus Boerger	GLOBAL_AUTO_GLOBALS_TABLE = NULL;
85321698c12SDmitry Stogov	GLOBAL_CONSTANTS_TABLE = NULL;
854a17d2f37SSebastian Bergmann#endif
8554c283fffSIlia Alshanetsky	zend_destroy_rsrc_list_dtors();
856dd5c478bSDmitry Stogov
857dd5c478bSDmitry Stogov	zend_interned_strings_dtor(TSRMLS_C);
858573b4602SAndi Gutmans}
859b4892511SJani Taskinen/* }}} */
860573b4602SAndi Gutmans
861b4892511SJani Taskinenvoid zend_set_utility_values(zend_utility_values *utility_values) /* {{{ */
8627656c77cSZeev Suraski{
8637656c77cSZeev Suraski	zend_uv = *utility_values;
8649baad804SZeev Suraski	zend_uv.import_use_extension_length = strlen(zend_uv.import_use_extension);
8657656c77cSZeev Suraski}
866b4892511SJani Taskinen/* }}} */
8677656c77cSZeev Suraski
868573b4602SAndi Gutmans/* this should be compatible with the standard zenderror */
869cf7384aaSFelipe Penavoid zenderror(const char *error) /* {{{ */
870573b4602SAndi Gutmans{
87110def9a8SIlia Alshanetsky	zend_error(E_PARSE, "%s", error);
872573b4602SAndi Gutmans}
873b4892511SJani Taskinen/* }}} */
874573b4602SAndi Gutmans
875b0bfa458SZeev SuraskiBEGIN_EXTERN_C()
876b4892511SJani TaskinenZEND_API void _zend_bailout(char *filename, uint lineno) /* {{{ */
877573b4602SAndi Gutmans{
8782c254ba7SZeev Suraski	TSRMLS_FETCH();
879573b4602SAndi Gutmans
88014f6b914SDmitry Stogov	if (!EG(bailout)) {
881ef4fe54fSZeev Suraski		zend_output_debug_string(1, "%s(%d) : Bailed out without a bailout address!", filename, lineno);
882931ebe08SZeev Suraski		exit(-1);
883931ebe08SZeev Suraski	}
8846bc6dacfSZeev Suraski	CG(unclean_shutdown) = 1;
88508b3d76cSFelipe Pena	CG(active_class_entry) = NULL;
886b23d83cfSZeev Suraski	CG(in_compilation) = EG(in_execution) = 0;
887bdc35c9fSThies C. Arntzen	EG(current_execute_data) = NULL;
88810afe5d9SRasmus Lerdorf	LONGJMP(*EG(bailout), FAILURE);
889573b4602SAndi Gutmans}
890b4892511SJani Taskinen/* }}} */
891b0bfa458SZeev SuraskiEND_EXTERN_C()
892573b4602SAndi Gutmans
893cf7384aaSFelipe Penavoid zend_append_version_info(const zend_extension *extension) /* {{{ */
894573b4602SAndi Gutmans{
895573b4602SAndi Gutmans	char *new_info;
896573b4602SAndi Gutmans	uint new_info_length;
897573b4602SAndi Gutmans
898ed7b3e70SPierrick Charron	new_info_length = sizeof("    with  v, , by \n")
899573b4602SAndi Gutmans						+ strlen(extension->name)
900573b4602SAndi Gutmans						+ strlen(extension->version)
901573b4602SAndi Gutmans						+ strlen(extension->copyright)
902573b4602SAndi Gutmans						+ strlen(extension->author);
903573b4602SAndi Gutmans
904b4892511SJani Taskinen	new_info = (char *) malloc(new_info_length + 1);
905573b4602SAndi Gutmans
906ed7b3e70SPierrick Charron	snprintf(new_info, new_info_length, "    with %s v%s, %s, by %s\n", extension->name, extension->version, extension->copyright, extension->author);
907573b4602SAndi Gutmans
908b4892511SJani Taskinen	zend_version_info = (char *) realloc(zend_version_info, zend_version_info_length+new_info_length + 1);
909ed7b3e70SPierrick Charron	strncat(zend_version_info, new_info, new_info_length);
910573b4602SAndi Gutmans	zend_version_info_length += new_info_length;
911573b4602SAndi Gutmans	free(new_info);
912573b4602SAndi Gutmans}
913b4892511SJani Taskinen/* }}} */
914573b4602SAndi Gutmans
915b4892511SJani TaskinenZEND_API char *get_zend_version(void) /* {{{ */
916573b4602SAndi Gutmans{
917573b4602SAndi Gutmans	return zend_version_info;
918573b4602SAndi Gutmans}
919b4892511SJani Taskinen/* }}} */
920ed06a70fSZeev Suraski
921f046189eSkrakjoeZEND_API void zend_activate(TSRMLS_D) /* {{{ */
922ed06a70fSZeev Suraski{
923699f07baSAnatol Belski#ifdef ZTS
9249df78bf9SAnatol Belski	virtual_cwd_activate(TSRMLS_C);
925699f07baSAnatol Belski#endif
92671592cecSDmitry Stogov	gc_reset(TSRMLS_C);
927b4f3b9d3SZeev Suraski	init_compiler(TSRMLS_C);
928b4f3b9d3SZeev Suraski	init_executor(TSRMLS_C);
929b4f3b9d3SZeev Suraski	startup_scanner(TSRMLS_C);
930ed06a70fSZeev Suraski}
931b4892511SJani Taskinen/* }}} */
932ed06a70fSZeev Suraski
933b4892511SJani Taskinenvoid zend_call_destructors(TSRMLS_D) /* {{{ */
9340cd2da03SMarcus Boerger{
9350cd2da03SMarcus Boerger	zend_try {
9360cd2da03SMarcus Boerger		shutdown_destructors(TSRMLS_C);
9370cd2da03SMarcus Boerger	} zend_end_try();
9380cd2da03SMarcus Boerger}
939b4892511SJani Taskinen/* }}} */
9400cd2da03SMarcus Boerger
941f046189eSkrakjoeZEND_API void zend_deactivate(TSRMLS_D) /* {{{ */
942ed06a70fSZeev Suraski{
943372f71ebSThies C. Arntzen	/* we're no longer executing anything */
944424e98f4SZeev Suraski	EG(opline_ptr) = NULL;
945372f71ebSThies C. Arntzen	EG(active_symbol_table) = NULL;
946f4c9d96fSThies C. Arntzen
947931ebe08SZeev Suraski	zend_try {
948b4f3b9d3SZeev Suraski		shutdown_scanner(TSRMLS_C);
949931ebe08SZeev Suraski	} zend_end_try();
950931ebe08SZeev Suraski
9518084d278SZeev Suraski	/* shutdown_executor() takes care of its own bailout handling */
9522c254ba7SZeev Suraski	shutdown_executor(TSRMLS_C);
953931ebe08SZeev Suraski
954931ebe08SZeev Suraski	zend_try {
955b4f3b9d3SZeev Suraski		shutdown_compiler(TSRMLS_C);
956931ebe08SZeev Suraski	} zend_end_try();
957931ebe08SZeev Suraski
95859a46c34SDmitry Stogov	zend_destroy_rsrc_list(&EG(regular_list) TSRMLS_CC);
95959a46c34SDmitry Stogov
96071592cecSDmitry Stogov#ifdef ZEND_DEBUG
96171592cecSDmitry Stogov	if (GC_G(gc_enabled) && !CG(unclean_shutdown)) {
96271592cecSDmitry Stogov		gc_collect_cycles(TSRMLS_C);
96371592cecSDmitry Stogov	}
96471592cecSDmitry Stogov#endif
96571592cecSDmitry Stogov
9663bd291e1SDerick Rethans#if GC_BENCH
96713d75ce6SDerick Rethans	fprintf(stderr, "GC Statistics\n");
96813d75ce6SDerick Rethans	fprintf(stderr, "-------------\n");
96913d75ce6SDerick Rethans	fprintf(stderr, "Runs:               %d\n", GC_G(gc_runs));
97013d75ce6SDerick Rethans	fprintf(stderr, "Collected:          %d\n", GC_G(collected));
97113d75ce6SDerick Rethans	fprintf(stderr, "Root buffer length: %d\n", GC_G(root_buf_length));
97213d75ce6SDerick Rethans	fprintf(stderr, "Root buffer peak:   %d\n\n", GC_G(root_buf_peak));
97313d75ce6SDerick Rethans	fprintf(stderr, "      Possible            Remove from  Marked\n");
97413d75ce6SDerick Rethans	fprintf(stderr, "        Root    Buffered     buffer     grey\n");
97513d75ce6SDerick Rethans	fprintf(stderr, "      --------  --------  -----------  ------\n");
97613d75ce6SDerick Rethans	fprintf(stderr, "ZVAL  %8d  %8d  %9d  %8d\n", GC_G(zval_possible_root), GC_G(zval_buffered), GC_G(zval_remove_from_buffer), GC_G(zval_marked_grey));
97713d75ce6SDerick Rethans	fprintf(stderr, "ZOBJ  %8d  %8d  %9d  %8d\n", GC_G(zobj_possible_root), GC_G(zobj_buffered), GC_G(zobj_remove_from_buffer), GC_G(zobj_marked_grey));
9783bd291e1SDerick Rethans#endif
9793bd291e1SDerick Rethans
980931ebe08SZeev Suraski	zend_try {
9812c254ba7SZeev Suraski		zend_ini_deactivate(TSRMLS_C);
982931ebe08SZeev Suraski	} zend_end_try();
983ed06a70fSZeev Suraski}
984b4892511SJani Taskinen/* }}} */
98556251a72SZeev Suraski
986fc173b6aSSam RubyBEGIN_EXTERN_C()
9874a25a774SDmitry StogovZEND_API void zend_message_dispatcher(long message, const void *data TSRMLS_DC) /* {{{ */
98856251a72SZeev Suraski{
98956251a72SZeev Suraski	if (zend_message_dispatcher_p) {
99085ab4235SFelipe Pena		zend_message_dispatcher_p(message, data TSRMLS_CC);
99156251a72SZeev Suraski	}
99256251a72SZeev Suraski}
993b4892511SJani Taskinen/* }}} */
994fc173b6aSSam RubyEND_EXTERN_C()
99556251a72SZeev Suraski
996cf7384aaSFelipe PenaZEND_API int zend_get_configuration_directive(const char *name, uint name_length, zval *contents) /* {{{ */
99756251a72SZeev Suraski{
998903018efSZeev Suraski	if (zend_get_configuration_directive_p) {
999903018efSZeev Suraski		return zend_get_configuration_directive_p(name, name_length, contents);
100056251a72SZeev Suraski	} else {
100156251a72SZeev Suraski		return FAILURE;
100256251a72SZeev Suraski	}
100356251a72SZeev Suraski}
1004b4892511SJani Taskinen/* }}} */
100567f69743SZeev Suraski
1006a550566cSDmitry Stogov#define SAVE_STACK(stack) do { \
1007a550566cSDmitry Stogov		if (CG(stack).top) { \
1008a550566cSDmitry Stogov			memcpy(&stack, &CG(stack), sizeof(zend_stack)); \
1009a550566cSDmitry Stogov			CG(stack).top = CG(stack).max = 0; \
1010a550566cSDmitry Stogov			CG(stack).elements = NULL; \
1011a550566cSDmitry Stogov		} else { \
1012a550566cSDmitry Stogov			stack.top = 0; \
1013a550566cSDmitry Stogov		} \
1014a550566cSDmitry Stogov	} while (0)
1015a550566cSDmitry Stogov
1016a550566cSDmitry Stogov#define RESTORE_STACK(stack) do { \
1017a550566cSDmitry Stogov		if (stack.top) { \
1018a550566cSDmitry Stogov			zend_stack_destroy(&CG(stack)); \
1019a550566cSDmitry Stogov			memcpy(&CG(stack), &stack, sizeof(zend_stack)); \
1020a550566cSDmitry Stogov		} \
1021a550566cSDmitry Stogov	} while (0)
1022a550566cSDmitry Stogov
1023b4892511SJani TaskinenZEND_API void zend_error(int type, const char *format, ...) /* {{{ */
102467f69743SZeev Suraski{
1025f4cfaf36SDmitry Stogov	char *str;
1026f4cfaf36SDmitry Stogov	int len;
102767f69743SZeev Suraski	va_list args;
10289e9c193eSStanislav Malyshev	va_list usr_copy;
1029f4cfaf36SDmitry Stogov	zval params[5];
1030f4cfaf36SDmitry Stogov	zval retval;
10314a25a774SDmitry Stogov	const char *error_filename;
1032b80b8381SZeev Suraski	uint error_lineno;
1033f4cfaf36SDmitry Stogov	zval orig_user_error_handler;
10342e1a2438SDmitry Stogov	zend_bool in_compilation;
10352e1a2438SDmitry Stogov	zend_class_entry *saved_class_entry;
1036a550566cSDmitry Stogov	zend_stack bp_stack;
1037a550566cSDmitry Stogov	zend_stack function_call_stack;
1038a550566cSDmitry Stogov	zend_stack switch_cond_stack;
1039a550566cSDmitry Stogov	zend_stack foreach_copy_stack;
1040a550566cSDmitry Stogov	zend_stack object_stack;
1041a550566cSDmitry Stogov	zend_stack declare_stack;
1042a550566cSDmitry Stogov	zend_stack list_stack;
1043a550566cSDmitry Stogov	zend_stack context_stack;
10442c254ba7SZeev Suraski	TSRMLS_FETCH();
104567f69743SZeev Suraski
1046b29dc146SDmitry Stogov	/* Report about uncaught exception in case of fatal errors */
1047b29dc146SDmitry Stogov	if (EG(exception)) {
1048b29dc146SDmitry Stogov		switch (type) {
1049b29dc146SDmitry Stogov			case E_CORE_ERROR:
1050b29dc146SDmitry Stogov			case E_ERROR:
1051b29dc146SDmitry Stogov			case E_RECOVERABLE_ERROR:
1052b29dc146SDmitry Stogov			case E_PARSE:
1053b29dc146SDmitry Stogov			case E_COMPILE_ERROR:
1054b29dc146SDmitry Stogov			case E_USER_ERROR:
1055b29dc146SDmitry Stogov				if (zend_is_executing(TSRMLS_C)) {
1056b29dc146SDmitry Stogov					error_lineno = zend_get_executed_lineno(TSRMLS_C);
1057b29dc146SDmitry Stogov				}
1058b29dc146SDmitry Stogov				zend_exception_error(EG(exception), E_WARNING TSRMLS_CC);
1059b29dc146SDmitry Stogov				EG(exception) = NULL;
1060b29dc146SDmitry Stogov				if (zend_is_executing(TSRMLS_C) && EG(opline_ptr)) {
1061b29dc146SDmitry Stogov					active_opline->lineno = error_lineno;
1062b29dc146SDmitry Stogov				}
1063b29dc146SDmitry Stogov				break;
1064b29dc146SDmitry Stogov			default:
1065b29dc146SDmitry Stogov				break;
1066b29dc146SDmitry Stogov		}
1067b29dc146SDmitry Stogov	}
1068b29dc146SDmitry Stogov
1069b80b8381SZeev Suraski	/* Obtain relevant filename and lineno */
1070b80b8381SZeev Suraski	switch (type) {
1071b80b8381SZeev Suraski		case E_CORE_ERROR:
1072