zend.c revision b4892511
1573b4602SAndi Gutmans/*
2573b4602SAndi Gutmans   +----------------------------------------------------------------------+
3573b4602SAndi Gutmans   | Zend Engine                                                          |
4573b4602SAndi Gutmans   +----------------------------------------------------------------------+
54223aa4dSSebastian Bergmann   | Copyright (c) 1998-2007 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"
31573b4602SAndi Gutmans
327a87fcbbSZeev Suraski#ifdef ZTS
33b4892511SJani Taskinen# define GLOBAL_FUNCTION_TABLE		global_function_table
34b4892511SJani Taskinen# define GLOBAL_CLASS_TABLE			global_class_table
35b4892511SJani Taskinen# define GLOBAL_CONSTANTS_TABLE		global_constants_table
36b4892511SJani Taskinen# define GLOBAL_AUTO_GLOBALS_TABLE	global_auto_globals_table
377a87fcbbSZeev Suraski#else
38b4892511SJani Taskinen# define GLOBAL_FUNCTION_TABLE		CG(function_table)
39b4892511SJani Taskinen# define GLOBAL_CLASS_TABLE			CG(class_table)
40b4892511SJani Taskinen# define GLOBAL_AUTO_GLOBALS_TABLE	CG(auto_globals)
41b4892511SJani Taskinen# define GLOBAL_CONSTANTS_TABLE		EG(zend_constants)
427a87fcbbSZeev Suraski#endif
437a87fcbbSZeev Suraski
44f34b9d41SZeev Suraski#if defined(ZEND_WIN32) && ZEND_DEBUG
452ffe85ddSZeev SuraskiBOOL WINAPI IsDebuggerPresent(VOID);
462ffe85ddSZeev Suraski#endif
472ffe85ddSZeev Suraski
48573b4602SAndi Gutmans/* true multithread-shared globals */
4992dd5e61SStanislav MalyshevZEND_API zend_class_entry *zend_standard_class_def = NULL;
50573b4602SAndi GutmansZEND_API int (*zend_printf)(const char *format, ...);
515dba4774SZeev SuraskiZEND_API zend_write_func_t zend_write;
525463dd5bSAndi GutmansZEND_API FILE *(*zend_fopen)(const char *filename, char **opened_path);
53e52aac94SWez FurlongZEND_API int (*zend_stream_open_function)(const char *filename, zend_file_handle *handle TSRMLS_DC);
5493536507SStig S. BakkenZEND_API void (*zend_block_interruptions)(void);
5593536507SStig S. BakkenZEND_API void (*zend_unblock_interruptions)(void);
56f1a2ee55SAndi GutmansZEND_API void (*zend_ticks_function)(int ticks);
57536221beSStanislav MalyshevZEND_API void (*zend_error_cb)(int type, const char *error_filename, const uint error_lineno, const char *format, va_list args);
58d45fd8a1SMarcus Boergerint (*zend_vspprintf)(char **pbuf, size_t max_len, const char *format, va_list ap);
5985a36e2bSZeev SuraskiZEND_API char *(*zend_getenv)(char *name, size_t name_len TSRMLS_DC);
60536221beSStanislav Malyshev
61bd115087SZeev Suraskivoid (*zend_on_timeout)(int seconds TSRMLS_DC);
62bd115087SZeev Suraski
6356251a72SZeev Suraskistatic void (*zend_message_dispatcher_p)(long message, void *data);
64903018efSZeev Suraskistatic int (*zend_get_configuration_directive_p)(char *name, uint name_length, zval *contents);
65d035af4aSZeev Suraski
66b4892511SJani Taskinenstatic ZEND_INI_MH(OnUpdateErrorReporting) /* {{{ */
674be862b0SZeev Suraski{
684be862b0SZeev Suraski	if (!new_value) {
69c0df4502SAndi Gutmans		EG(error_reporting) = E_ALL & ~E_NOTICE & ~E_STRICT;
704be862b0SZeev Suraski	} else {
714be862b0SZeev Suraski		EG(error_reporting) = atoi(new_value);
724be862b0SZeev Suraski	}
734be862b0SZeev Suraski	return SUCCESS;
744be862b0SZeev Suraski}
75b4892511SJani Taskinen/* }}} */
764be862b0SZeev Suraski
774be862b0SZeev SuraskiZEND_INI_BEGIN()
78eb6fd52eSZeev Suraski	ZEND_INI_ENTRY("error_reporting",				NULL,		ZEND_INI_ALL,		OnUpdateErrorReporting)
7951d49585SZeev Suraski	STD_ZEND_INI_BOOLEAN("zend.ze1_compatibility_mode",	"0",	ZEND_INI_ALL,		OnUpdateBool,	ze1_compatibility_mode,	zend_executor_globals,	executor_globals)
80a8c6b992SDmitry Stogov#ifdef ZEND_MULTIBYTE
81a8c6b992SDmitry Stogov	STD_ZEND_INI_BOOLEAN("detect_unicode", "1", ZEND_INI_ALL, OnUpdateBool, detect_unicode, zend_compiler_globals, compiler_globals)
82a8c6b992SDmitry Stogov#endif
834be862b0SZeev SuraskiZEND_INI_END()
844be862b0SZeev Suraski
854be862b0SZeev Suraski
867a87fcbbSZeev Suraski#ifdef ZTS
87e06a1ed2SZeev SuraskiZEND_API int compiler_globals_id;
88e06a1ed2SZeev SuraskiZEND_API int executor_globals_id;
89b4892511SJani Taskinenstatic HashTable *global_function_table = NULL;
90b4892511SJani Taskinenstatic HashTable *global_class_table = NULL;
91b4892511SJani Taskinenstatic HashTable *global_constants_table = NULL;
92b4892511SJani Taskinenstatic HashTable *global_auto_globals_table = NULL;
9321698c12SDmitry Stogovstatic HashTable *global_persistent_list = NULL;
947a87fcbbSZeev Suraski#endif
95573b4602SAndi Gutmans
96e801a99aSZeev SuraskiZEND_API zend_utility_values zend_uv;
97573b4602SAndi Gutmans
985ba5f00fSZeev SuraskiZEND_API zval zval_used_for_init; /* True global variable */
99213d0e2fSAndi Gutmans
100573b4602SAndi Gutmans/* version information */
101573b4602SAndi Gutmansstatic char *zend_version_info;
102573b4602SAndi Gutmansstatic uint zend_version_info_length;
1034223aa4dSSebastian Bergmann#define ZEND_CORE_VERSION_INFO	"Zend Engine v" ZEND_VERSION ", Copyright (c) 1998-2007 Zend Technologies\n"
10480bab9d9SAndrei Zmievski#define PRINT_ZVAL_INDENT 4
105573b4602SAndi Gutmans
106b4892511SJani Taskinenstatic void print_hash(HashTable *ht, int indent, zend_bool is_object TSRMLS_DC) /* {{{ */
107573b4602SAndi Gutmans{
108573b4602SAndi Gutmans	zval **tmp;
109573b4602SAndi Gutmans	char *string_key;
11063873abaSStanislav Malyshev	HashPosition iterator;
1114757104eSZeev Suraski	ulong num_key;
1124757104eSZeev Suraski	uint str_len;
113573b4602SAndi Gutmans	int i;
114573b4602SAndi Gutmans
115b4892511SJani Taskinen	for (i = 0; i < indent; i++) {
116b4892511SJani Taskinen		ZEND_PUTS(" ");
117573b4602SAndi Gutmans	}
118b4892511SJani Taskinen	ZEND_PUTS("(\n");
11980bab9d9SAndrei Zmievski	indent += PRINT_ZVAL_INDENT;
12063873abaSStanislav Malyshev	zend_hash_internal_pointer_reset_ex(ht, &iterator);
12163873abaSStanislav Malyshev	while (zend_hash_get_current_data_ex(ht, (void **) &tmp, &iterator) == SUCCESS) {
122b4892511SJani Taskinen		for (i = 0; i < indent; i++) {
123b4892511SJani Taskinen			ZEND_PUTS(" ");
124573b4602SAndi Gutmans		}
125b4892511SJani Taskinen		ZEND_PUTS("[");
12636eaad25SZeev Suraski		switch (zend_hash_get_current_key_ex(ht, &string_key, &str_len, &num_key, 0, &iterator)) {
127573b4602SAndi Gutmans			case HASH_KEY_IS_STRING:
128d329ce93SZeev Suraski				if (is_object) {
129d329ce93SZeev Suraski					char *prop_name, *class_name;
130b4892511SJani Taskinen					int mangled = zend_unmangle_property_name(string_key, str_len - 1, &class_name, &prop_name);
131d329ce93SZeev Suraski
132978dd0fdSMarcus Boerger					if (class_name && mangled == SUCCESS) {
133d329ce93SZeev Suraski						if (class_name[0]=='*') {
134b4892511SJani Taskinen							zend_printf("%s:protected", prop_name);
135d329ce93SZeev Suraski						} else {
136b4892511SJani Taskinen							zend_printf("%s:%s:private", prop_name, class_name);
137d329ce93SZeev Suraski						}
138b4892511SJani Taskinen					} else {
139b4892511SJani Taskinen						zend_printf("%s", prop_name);
140d329ce93SZeev Suraski					}
141d329ce93SZeev Suraski				} else {
142b4892511SJani Taskinen					zend_printf("%s", string_key);
143d329ce93SZeev Suraski				}
144573b4602SAndi Gutmans				break;
145573b4602SAndi Gutmans			case HASH_KEY_IS_LONG:
146b4892511SJani Taskinen				zend_printf("%ld", num_key);
147573b4602SAndi Gutmans				break;
148573b4602SAndi Gutmans		}
149b4892511SJani Taskinen		ZEND_PUTS("] => ");
150b4892511SJani Taskinen		zend_print_zval_r(*tmp, indent+PRINT_ZVAL_INDENT TSRMLS_CC);
151b4892511SJani Taskinen		ZEND_PUTS("\n");
15263873abaSStanislav Malyshev		zend_hash_move_forward_ex(ht, &iterator);
153573b4602SAndi Gutmans	}
15480bab9d9SAndrei Zmievski	indent -= PRINT_ZVAL_INDENT;
155b4892511SJani Taskinen	for (i = 0; i < indent; i++) {
156b4892511SJani Taskinen		ZEND_PUTS(" ");
157573b4602SAndi Gutmans	}
158b4892511SJani Taskinen	ZEND_PUTS(")\n");
159573b4602SAndi Gutmans}
160b4892511SJani Taskinen/* }}} */
161573b4602SAndi Gutmans
162b4892511SJani Taskinenstatic void print_flat_hash(HashTable *ht TSRMLS_DC) /* {{{ */
16327de54b0SAndi Gutmans{
164b4892511SJani Taskinen	zval **tmp;
165b4892511SJani Taskinen	char *string_key;
166b4892511SJani Taskinen	HashPosition iterator;
167b4892511SJani Taskinen	ulong num_key;
168b4892511SJani Taskinen	uint str_len;
169b4892511SJani Taskinen	int i = 0;
170b4892511SJani Taskinen
171b4892511SJani Taskinen	zend_hash_internal_pointer_reset_ex(ht, &iterator);
172b4892511SJani Taskinen	while (zend_hash_get_current_data_ex(ht, (void **) &tmp, &iterator) == SUCCESS) {
173b4892511SJani Taskinen		if (i++ > 0) {
174b4892511SJani Taskinen			ZEND_PUTS(",");
175b4892511SJani Taskinen		}
176b4892511SJani Taskinen		ZEND_PUTS("[");
177b4892511SJani Taskinen		switch (zend_hash_get_current_key_ex(ht, &string_key, &str_len, &num_key, 0, &iterator)) {
178b4892511SJani Taskinen			case HASH_KEY_IS_STRING:
179b4892511SJani Taskinen				ZEND_PUTS(string_key);
180b4892511SJani Taskinen				break;
181b4892511SJani Taskinen			case HASH_KEY_IS_LONG:
182b4892511SJani Taskinen				zend_printf("%ld", num_key);
183b4892511SJani Taskinen				break;
184b4892511SJani Taskinen		}
185b4892511SJani Taskinen		ZEND_PUTS("] => ");
186b4892511SJani Taskinen		zend_print_flat_zval_r(*tmp TSRMLS_CC);
187b4892511SJani Taskinen		zend_hash_move_forward_ex(ht, &iterator);
18827de54b0SAndi Gutmans	}
18927de54b0SAndi Gutmans}
190b4892511SJani Taskinen/* }}} */
1917a87fcbbSZeev Suraski
192b4892511SJani TaskinenZEND_API void zend_make_printable_zval(zval *expr, zval *expr_copy, int *use_copy) /* {{{ */
193573b4602SAndi Gutmans{
194b4892511SJani Taskinen	if (Z_TYPE_P(expr)==IS_STRING) {
195da9faa2cSZeev Suraski		*use_copy = 0;
196da9faa2cSZeev Suraski		return;
197da9faa2cSZeev Suraski	}
198b4892511SJani Taskinen	switch (Z_TYPE_P(expr)) {
199bdefd5daSAndi Gutmans		case IS_NULL:
200b4892511SJani Taskinen			Z_STRLEN_P(expr_copy) = 0;
201b4892511SJani Taskinen			Z_STRVAL_P(expr_copy) = STR_EMPTY_ALLOC();
2028c4ab98eSThies C. Arntzen			break;
203da9faa2cSZeev Suraski		case IS_BOOL:
204b4892511SJani Taskinen			if (Z_LVAL_P(expr)) {
205b4892511SJani Taskinen				Z_STRLEN_P(expr_copy) = 1;
206b4892511SJani Taskinen				Z_STRVAL_P(expr_copy) = estrndup("1", 1);
20791d7ba76SAndi Gutmans			} else {
208b4892511SJani Taskinen				Z_STRLEN_P(expr_copy) = 0;
209b4892511SJani Taskinen				Z_STRVAL_P(expr_copy) = STR_EMPTY_ALLOC();
21091d7ba76SAndi Gutmans			}
211da9faa2cSZeev Suraski			break;
212da9faa2cSZeev Suraski		case IS_RESOURCE:
213b4892511SJani Taskinen			Z_STRVAL_P(expr_copy) = (char *) emalloc(sizeof("Resource id #") - 1 + MAX_LENGTH_OF_LONG);
214b4892511SJani Taskinen			Z_STRLEN_P(expr_copy) = sprintf(Z_STRVAL_P(expr_copy), "Resource id #%ld", Z_LVAL_P(expr));
215da9faa2cSZeev Suraski			break;
216da9faa2cSZeev Suraski		case IS_ARRAY:
217b4892511SJani Taskinen			Z_STRLEN_P(expr_copy) = sizeof("Array") - 1;
218b4892511SJani Taskinen			Z_STRVAL_P(expr_copy) = estrndup("Array", Z_STRLEN_P(expr_copy));
219da9faa2cSZeev Suraski			break;
2201b282b35SAndi Gutmans		case IS_OBJECT:
2217901a221SAndi Gutmans			{
222ae2bfb78SSterling Hughes				TSRMLS_FETCH();
223637a4042SMarcus Boerger
224b4892511SJani Taskinen				if (Z_OBJ_HANDLER_P(expr, cast_object) && Z_OBJ_HANDLER_P(expr, cast_object)(expr, expr_copy, IS_STRING TSRMLS_CC) == SUCCESS) {
225848c5c76SMarcus Boerger					break;
226848c5c76SMarcus Boerger				}
22788de69b9SAndi Gutmans				/* Standard PHP objects */
228b3c3c6deSMarcus Boerger				if (Z_OBJ_HT_P(expr) == &std_object_handlers || !Z_OBJ_HANDLER_P(expr, cast_object)) {
229637a4042SMarcus Boerger					if (zend_std_cast_object_tostring(expr, expr_copy, IS_STRING TSRMLS_CC) == SUCCESS) {
230d4f64f88SMarcus Boerger						break;
231d4f64f88SMarcus Boerger					}
232b3c3c6deSMarcus Boerger				}
233b3c3c6deSMarcus Boerger				if (!Z_OBJ_HANDLER_P(expr, cast_object) && Z_OBJ_HANDLER_P(expr, get)) {
234b3c3c6deSMarcus Boerger					zval *z = Z_OBJ_HANDLER_P(expr, get)(expr TSRMLS_CC);
2357addae50SMarcus Boerger
2364b4d634cSYiduo (David) Wang					Z_ADDREF_P(z);
237b4892511SJani Taskinen					if (Z_TYPE_P(z) != IS_OBJECT) {
238b3c3c6deSMarcus Boerger						zend_make_printable_zval(z, expr_copy, use_copy);
239b3c3c6deSMarcus Boerger						if (*use_copy) {
240b3c3c6deSMarcus Boerger							zval_ptr_dtor(&z);
241b3c3c6deSMarcus Boerger						} else {
242b3c3c6deSMarcus Boerger							ZVAL_ZVAL(expr_copy, z, 0, 1);
243b3c3c6deSMarcus Boerger							*use_copy = 1;
2443b8b43a9SStanislav Malyshev						}
245b3c3c6deSMarcus Boerger						return;
2463b8b43a9SStanislav Malyshev					}
247b3c3c6deSMarcus Boerger					zval_ptr_dtor(&z);
24871ba4427SMarcus Boerger				}
249d76c5161SMarcus Boerger				zend_error(EG(exception) ? E_ERROR : E_RECOVERABLE_ERROR, "Object of class %s could not be converted to string", Z_OBJCE_P(expr)->name);
250b4892511SJani Taskinen				Z_STRLEN_P(expr_copy) = 0;
251b4892511SJani Taskinen				Z_STRVAL_P(expr_copy) = STR_EMPTY_ALLOC();
252ae2bfb78SSterling Hughes			}
253da9faa2cSZeev Suraski			break;
254e842ef9fSIlia Alshanetsky		case IS_DOUBLE:
255e842ef9fSIlia Alshanetsky			*expr_copy = *expr;
256e842ef9fSIlia Alshanetsky			zval_copy_ctor(expr_copy);
257e842ef9fSIlia Alshanetsky			zend_locale_sprintf_double(expr_copy ZEND_FILE_LINE_CC);
258e842ef9fSIlia Alshanetsky			break;
259da9faa2cSZeev Suraski		default:
260da9faa2cSZeev Suraski			*expr_copy = *expr;
261da9faa2cSZeev Suraski			zval_copy_ctor(expr_copy);
262da9faa2cSZeev Suraski			convert_to_string(expr_copy);
263da9faa2cSZeev Suraski			break;
264da9faa2cSZeev Suraski	}
265b4892511SJani Taskinen	Z_TYPE_P(expr_copy) = IS_STRING;
266da9faa2cSZeev Suraski	*use_copy = 1;
267da9faa2cSZeev Suraski}
268b4892511SJani Taskinen/* }}} */
269da9faa2cSZeev Suraski
270b4892511SJani TaskinenZEND_API int zend_print_zval(zval *expr, int indent) /* {{{ */
271da9faa2cSZeev Suraski{
2725dba4774SZeev Suraski	return zend_print_zval_ex(zend_write, expr, indent);
2735dba4774SZeev Suraski}
274b4892511SJani Taskinen/* }}} */
2755dba4774SZeev Suraski
276b4892511SJani TaskinenZEND_API int zend_print_zval_ex(zend_write_func_t write_func, zval *expr, int indent) /* {{{ */
2775dba4774SZeev Suraski{
278da9faa2cSZeev Suraski	zval expr_copy;
279da9faa2cSZeev Suraski	int use_copy;
280da9faa2cSZeev Suraski
281da9faa2cSZeev Suraski	zend_make_printable_zval(expr, &expr_copy, &use_copy);
282da9faa2cSZeev Suraski	if (use_copy) {
283573b4602SAndi Gutmans		expr = &expr_copy;
284573b4602SAndi Gutmans	}
285b4892511SJani Taskinen	if (Z_STRLEN_P(expr) == 0) { /* optimize away empty strings */
286da9faa2cSZeev Suraski		if (use_copy) {
287da9faa2cSZeev Suraski			zval_dtor(expr);
288da9faa2cSZeev Suraski		}
289573b4602SAndi Gutmans		return 0;
290573b4602SAndi Gutmans	}
291b4892511SJani Taskinen	write_func(Z_STRVAL_P(expr), Z_STRLEN_P(expr));
292da9faa2cSZeev Suraski	if (use_copy) {
293573b4602SAndi Gutmans		zval_dtor(expr);
294573b4602SAndi Gutmans	}
295b4892511SJani Taskinen	return Z_STRLEN_P(expr);
296573b4602SAndi Gutmans}
297b4892511SJani Taskinen/* }}} */
298573b4602SAndi Gutmans
299b4892511SJani TaskinenZEND_API void zend_print_flat_zval_r(zval *expr TSRMLS_DC) /* {{{ */
30027de54b0SAndi Gutmans{
301b4892511SJani Taskinen	switch (Z_TYPE_P(expr)) {
302b4892511SJani Taskinen		case IS_ARRAY:
303b4892511SJani Taskinen			ZEND_PUTS("Array (");
304b4892511SJani Taskinen			if (++Z_ARRVAL_P(expr)->nApplyCount>1) {
305b4892511SJani Taskinen				ZEND_PUTS(" *RECURSION*");
306b4892511SJani Taskinen				Z_ARRVAL_P(expr)->nApplyCount--;
307b4892511SJani Taskinen				return;
308b4892511SJani Taskinen			}
309b4892511SJani Taskinen			print_flat_hash(Z_ARRVAL_P(expr) TSRMLS_CC);
310b4892511SJani Taskinen			ZEND_PUTS(")");
311b4892511SJani Taskinen			Z_ARRVAL_P(expr)->nApplyCount--;
312b4892511SJani Taskinen			break;
313b4892511SJani Taskinen		case IS_OBJECT:
314b4892511SJani Taskinen		{
3159bba373aSStanislav Malyshev			HashTable *properties = NULL;
3169bba373aSStanislav Malyshev			char *class_name = NULL;
3179bba373aSStanislav Malyshev			zend_uint clen;
3188ce1211aSMarcus Boerger
319f30f3590SStanislav Malyshev			if (Z_OBJ_HANDLER_P(expr, get_class_name)) {
3209bba373aSStanislav Malyshev				Z_OBJ_HANDLER_P(expr, get_class_name)(expr, &class_name, &clen, 0 TSRMLS_CC);
3219bba373aSStanislav Malyshev			}
322b4892511SJani Taskinen			if (class_name) {
323b4892511SJani Taskinen				zend_printf("%s Object (", class_name);
324b4892511SJani Taskinen			} else {
325b4892511SJani Taskinen				zend_printf("%s Object (", "Unknown Class");
326b4892511SJani Taskinen			}
327288daccaSAndi Gutmans			if (class_name) {
328ad01495aSStanislav Malyshev				efree(class_name);
3299bba373aSStanislav Malyshev			}
330f30f3590SStanislav Malyshev			if (Z_OBJ_HANDLER_P(expr, get_properties)) {
3319bba373aSStanislav Malyshev				properties = Z_OBJPROP_P(expr);
3329bba373aSStanislav Malyshev			}
333f30f3590SStanislav Malyshev			if (properties) {
3349bba373aSStanislav Malyshev				if (++properties->nApplyCount>1) {
3359bba373aSStanislav Malyshev					ZEND_PUTS(" *RECURSION*");
3369bba373aSStanislav Malyshev					properties->nApplyCount--;
3379bba373aSStanislav Malyshev					return;
3389bba373aSStanislav Malyshev				}
33924db7f18SSebastian Bergmann				print_flat_hash(properties TSRMLS_CC);
3409bba373aSStanislav Malyshev				properties->nApplyCount--;
3419bba373aSStanislav Malyshev			}
3429bba373aSStanislav Malyshev			ZEND_PUTS(")");
3439bba373aSStanislav Malyshev			break;
344b4892511SJani Taskinen		}
345b4892511SJani Taskinen		default:
346b4892511SJani Taskinen			zend_print_variable(expr);
347b4892511SJani Taskinen			break;
348b4892511SJani Taskinen	}
34927de54b0SAndi Gutmans}
350b4892511SJani Taskinen/* }}} */
351573b4602SAndi Gutmans
352b4892511SJani TaskinenZEND_API void zend_print_zval_r(zval *expr, int indent TSRMLS_DC) /* {{{ */
3535dba4774SZeev Suraski{
35424db7f18SSebastian Bergmann	zend_print_zval_r_ex(zend_write, expr, indent TSRMLS_CC);
3555dba4774SZeev Suraski}
356b4892511SJani Taskinen/* }}} */
3575dba4774SZeev Suraski
358b4892511SJani TaskinenZEND_API void zend_print_zval_r_ex(zend_write_func_t write_func, zval *expr, int indent TSRMLS_DC) /* {{{ */
359573b4602SAndi Gutmans{
360b4892511SJani Taskinen	switch (Z_TYPE_P(expr)) {
361573b4602SAndi Gutmans		case IS_ARRAY:
362b4892511SJani Taskinen			ZEND_PUTS("Array\n");
363b4892511SJani Taskinen			if (++Z_ARRVAL_P(expr)->nApplyCount>1) {
364b4892511SJani Taskinen				ZEND_PUTS(" *RECURSION*");
365b4892511SJani Taskinen				Z_ARRVAL_P(expr)->nApplyCount--;
3666053f2a1SZeev Suraski				return;
3676053f2a1SZeev Suraski			}
368b4892511SJani Taskinen			print_hash(Z_ARRVAL_P(expr), indent, 0 TSRMLS_CC);
369b4892511SJani Taskinen			Z_ARRVAL_P(expr)->nApplyCount--;
370573b4602SAndi Gutmans			break;
371573b4602SAndi Gutmans		case IS_OBJECT:
372c259cb7cSAndi Gutmans			{
3738ce1211aSMarcus Boerger				HashTable *properties;
3749bba373aSStanislav Malyshev				char *class_name = NULL;
3759bba373aSStanislav Malyshev				zend_uint clen;
3768ce1211aSMarcus Boerger				int is_temp;
3778ce1211aSMarcus Boerger
378f30f3590SStanislav Malyshev				if (Z_OBJ_HANDLER_P(expr, get_class_name)) {
3799bba373aSStanislav Malyshev					Z_OBJ_HANDLER_P(expr, get_class_name)(expr, &class_name, &clen, 0 TSRMLS_CC);
3809bba373aSStanislav Malyshev				}
381f1fb77d1SIlia Alshanetsky				if (class_name) {
382b4892511SJani Taskinen					zend_printf("%s Object\n", class_name);
383f1fb77d1SIlia Alshanetsky				} else {
384b4892511SJani Taskinen					zend_printf("%s Object\n", "Unknown class");
385f1fb77d1SIlia Alshanetsky				}
386288daccaSAndi Gutmans				if (class_name) {
387ad01495aSStanislav Malyshev					efree(class_name);
3889bba373aSStanislav Malyshev				}
3898ce1211aSMarcus Boerger				if ((properties = Z_OBJDEBUG_P(expr, is_temp)) == NULL) {
3908ce1211aSMarcus Boerger					break;
3919bba373aSStanislav Malyshev				}
3928ce1211aSMarcus Boerger				if (++properties->nApplyCount>1) {
393b4892511SJani Taskinen					ZEND_PUTS(" *RECURSION*");
3949bba373aSStanislav Malyshev					properties->nApplyCount--;
3958ce1211aSMarcus Boerger					return;
3968ce1211aSMarcus Boerger				}
397b4892511SJani Taskinen				print_hash(properties, indent, 1 TSRMLS_CC);
3988ce1211aSMarcus Boerger				properties->nApplyCount--;
3998ce1211aSMarcus Boerger				if (is_temp) {
4008ce1211aSMarcus Boerger					zend_hash_destroy(properties);
4018ce1211aSMarcus Boerger					efree(properties);
402c259cb7cSAndi Gutmans				}
403c259cb7cSAndi Gutmans				break;
4046053f2a1SZeev Suraski			}
405573b4602SAndi Gutmans		default:
406b4892511SJani Taskinen			zend_print_variable(expr);
407573b4602SAndi Gutmans			break;
408573b4602SAndi Gutmans	}
409573b4602SAndi Gutmans}
410b4892511SJani Taskinen/* }}} */
411573b4602SAndi Gutmans
412b4892511SJani Taskinenstatic FILE *zend_fopen_wrapper(const char *filename, char **opened_path) /* {{{ */
413573b4602SAndi Gutmans{
4145463dd5bSAndi Gutmans	if (opened_path) {
41527de5001SAndi Gutmans		*opened_path = estrdup(filename);
4165463dd5bSAndi Gutmans	}
417f311ab16SAndi Gutmans	return fopen(filename, "rb");
418573b4602SAndi Gutmans}
419b4892511SJani Taskinen/* }}} */
420573b4602SAndi Gutmans
421b4892511SJani Taskinenstatic void register_standard_class(TSRMLS_D) /* {{{ */
422573b4602SAndi Gutmans{
4231bf1b64cSMarcus Boerger	zend_standard_class_def = calloc(1, sizeof(zend_class_entry));
424424e98f4SZeev Suraski
42592dd5e61SStanislav Malyshev	zend_standard_class_def->type = ZEND_INTERNAL_CLASS;
426c17c7a2bSZeev Suraski	zend_standard_class_def->name_length = sizeof("stdClass") - 1;
427372e0877SZeev Suraski	zend_standard_class_def->name = zend_strndup("stdClass", zend_standard_class_def->name_length);
4286d300bafSSebastian Bergmann	zend_initialize_class_data(zend_standard_class_def, 1 TSRMLS_CC);
4292de45becSAndi Gutmans
43062421b52SMarcus Boerger	zend_hash_add(CG(class_table), "stdclass", sizeof("stdclass"), &zend_standard_class_def, sizeof(zend_class_entry *), NULL);
431573b4602SAndi Gutmans}
432b4892511SJani Taskinen/* }}} */
433573b4602SAndi Gutmans
4342f586e96SDmitry Stogov#ifdef ZTS
435b4892511SJani Taskinenstatic zend_bool asp_tags_default		= 0;
436b4892511SJani Taskinenstatic zend_bool short_tags_default		= 1;
437b4892511SJani Taskinenstatic zend_bool ct_pass_ref_default	= 1;
438b4892511SJani Taskinenstatic zend_bool extended_info_default	= 0;
4392f586e96SDmitry Stogov#else
440b4892511SJani Taskinen# define asp_tags_default		0
441b4892511SJani Taskinen# define short_tags_default		1
442b4892511SJani Taskinen# define ct_pass_ref_default	1
443b4892511SJani Taskinen# define extended_info_default	0
4442f586e96SDmitry Stogov#endif
4452f586e96SDmitry Stogov
446b4892511SJani Taskinenstatic void zend_set_default_compile_time_values(TSRMLS_D) /* {{{ */
4478b794bb1SZeev Suraski{
4488b794bb1SZeev Suraski	/* default compile-time values */
4492f586e96SDmitry Stogov	CG(asp_tags) = asp_tags_default;
4502f586e96SDmitry Stogov	CG(short_tags) = short_tags_default;
4512f586e96SDmitry Stogov	CG(allow_call_time_pass_reference) = ct_pass_ref_default;
4522f586e96SDmitry Stogov	CG(extended_info) = extended_info_default;
4538b794bb1SZeev Suraski}
454b4892511SJani Taskinen/* }}} */
4558b794bb1SZeev Suraski
4567a87fcbbSZeev Suraski#ifdef ZTS
457b4892511SJani Taskinenstatic void compiler_globals_ctor(zend_compiler_globals *compiler_globals TSRMLS_DC) /* {{{ */
4587a87fcbbSZeev Suraski{
4597c4a0838SZeev Suraski	zend_function tmp_func;
460334ddd12SAndi Gutmans	zend_class_entry *tmp_class;
4617c4a0838SZeev Suraski
4627f4909d9SThies C. Arntzen	compiler_globals->compiled_filename = NULL;
4637f4909d9SThies C. Arntzen
464f7f5a5eaSStanislav Malyshev	compiler_globals->function_table = (HashTable *) malloc(sizeof(HashTable));
465b4892511SJani Taskinen	zend_hash_init_ex(compiler_globals->function_table, 100, NULL, ZEND_FUNCTION_DTOR, 1, 0);
466b4892511SJani Taskinen	zend_hash_copy(compiler_globals->function_table, global_function_table, NULL, &tmp_func, sizeof(zend_function));
4677a87fcbbSZeev Suraski
468b4892511SJani Taskinen	compiler_globals->class_table = (HashTable *) malloc(sizeof(HashTable));
469b4892511SJani Taskinen	zend_hash_init_ex(compiler_globals->class_table, 10, NULL, ZEND_CLASS_DTOR, 1, 0);
470b4892511SJani Taskinen	zend_hash_copy(compiler_globals->class_table, global_class_table, (copy_ctor_func_t) zend_class_add_ref, &tmp_class, sizeof(zend_class_entry *));
4719cf1a98dSThies C. Arntzen
472b4f3b9d3SZeev Suraski	zend_set_default_compile_time_values(TSRMLS_C);
47385b4df53SZeev Suraski
47485b4df53SZeev Suraski	CG(interactive) = 0;
4759d11db12SAndi Gutmans
4769d11db12SAndi Gutmans	compiler_globals->auto_globals = (HashTable *) malloc(sizeof(HashTable));
4779d11db12SAndi Gutmans	zend_hash_init_ex(compiler_globals->auto_globals, 8, NULL, NULL, 1, 0);
478f7b78004SZeev Suraski	zend_hash_copy(compiler_globals->auto_globals, global_auto_globals_table, NULL, NULL, sizeof(zend_auto_global) /* empty element */);
4798768ab94SDmitry Stogov
4808768ab94SDmitry Stogov	compiler_globals->last_static_member = zend_hash_num_elements(compiler_globals->class_table);
4818768ab94SDmitry Stogov	if (compiler_globals->last_static_member) {
4828768ab94SDmitry Stogov		compiler_globals->static_members = (HashTable**)calloc(compiler_globals->last_static_member, sizeof(HashTable*));
4838768ab94SDmitry Stogov	} else {
4848768ab94SDmitry Stogov		compiler_globals->static_members = NULL;
4858768ab94SDmitry Stogov	}
4867a87fcbbSZeev Suraski}
487b4892511SJani Taskinen/* }}} */
4887a87fcbbSZeev Suraski
489b4892511SJani Taskinenstatic void compiler_globals_dtor(zend_compiler_globals *compiler_globals TSRMLS_DC) /* {{{ */
4907a87fcbbSZeev Suraski{
491ce98c73fSAndi Gutmans	if (compiler_globals->function_table != GLOBAL_FUNCTION_TABLE) {
49274a9ed7bSZeev Suraski		zend_hash_destroy(compiler_globals->function_table);
4938fb5ea30SMarcus Boerger		free(compiler_globals->function_table);
49474a9ed7bSZeev Suraski	}
495ce98c73fSAndi Gutmans	if (compiler_globals->class_table != GLOBAL_CLASS_TABLE) {
49674a9ed7bSZeev Suraski		zend_hash_destroy(compiler_globals->class_table);
4978fb5ea30SMarcus Boerger		free(compiler_globals->class_table);
49874a9ed7bSZeev Suraski	}
4998fb5ea30SMarcus Boerger	if (compiler_globals->auto_globals != GLOBAL_AUTO_GLOBALS_TABLE) {
5009d11db12SAndi Gutmans		zend_hash_destroy(compiler_globals->auto_globals);
5019d11db12SAndi Gutmans		free(compiler_globals->auto_globals);
5029d11db12SAndi Gutmans	}
5038768ab94SDmitry Stogov	if (compiler_globals->static_members) {
5048768ab94SDmitry Stogov		free(compiler_globals->static_members);
5058768ab94SDmitry Stogov	}
5068768ab94SDmitry Stogov	compiler_globals->last_static_member = 0;
5077a87fcbbSZeev Suraski}
508b4892511SJani Taskinen/* }}} */
509a9f9ae79SZeev Suraski
510b4892511SJani Taskinenstatic void executor_globals_ctor(zend_executor_globals *executor_globals TSRMLS_DC) /* {{{ */
511a9f9ae79SZeev Suraski{
5125fcae67eSZeev Suraski	zend_startup_constants(TSRMLS_C);
5135fcae67eSZeev Suraski	zend_copy_constants(EG(zend_constants), GLOBAL_CONSTANTS_TABLE);
5142c254ba7SZeev Suraski	zend_init_rsrc_plist(TSRMLS_C);
515b4892511SJani Taskinen	EG(lambda_count) = 0;
5161f23e507SZeev Suraski	EG(user_error_handler) = NULL;
51704788f95SStig Bakken	EG(user_exception_handler) = NULL;
518ab30c8ccSZeev Suraski	EG(in_execution) = 0;
51955cf45abSDmitry Stogov	EG(in_autoload) = NULL;
520e6054d28SDerick Rethans	EG(current_execute_data) = NULL;
52152303217SMarcus Boerger	EG(current_module) = NULL;
5229d576212SAntony Dovgal	EG(exit_status) = 0;
523d6ee9d48SAntony Dovgal	EG(active) = 0;
524a9f9ae79SZeev Suraski}
525b4892511SJani Taskinen/* }}} */
526a9f9ae79SZeev Suraski
527b4892511SJani Taskinenstatic void executor_globals_dtor(zend_executor_globals *executor_globals TSRMLS_DC) /* {{{ */
528a9f9ae79SZeev Suraski{
5292c254ba7SZeev Suraski	zend_ini_shutdown(TSRMLS_C);
53021698c12SDmitry Stogov	if (&executor_globals->persistent_list != global_persistent_list) {
53121698c12SDmitry Stogov		zend_destroy_rsrc_list(&executor_globals->persistent_list TSRMLS_CC);
53221698c12SDmitry Stogov	}
53321698c12SDmitry Stogov	if (executor_globals->zend_constants != GLOBAL_CONSTANTS_TABLE) {
53421698c12SDmitry Stogov		zend_hash_destroy(executor_globals->zend_constants);
53521698c12SDmitry Stogov		free(executor_globals->zend_constants);
53621698c12SDmitry Stogov	}
537a9f9ae79SZeev Suraski}
538b4892511SJani Taskinen/* }}} */
539a9f9ae79SZeev Suraski
540b4892511SJani Taskinenstatic void zend_new_thread_end_handler(THREAD_T thread_id TSRMLS_DC) /* {{{ */
541609d58a4SZeev Suraski{
542a84a2eaeSDmitry Stogov	if (zend_copy_ini_directives(TSRMLS_C) == SUCCESS) {
543a84a2eaeSDmitry Stogov		zend_ini_refresh_caches(ZEND_INI_STAGE_STARTUP TSRMLS_CC);
544a84a2eaeSDmitry Stogov	}
545609d58a4SZeev Suraski}
546b4892511SJani Taskinen/* }}} */
54766d9314bSZeev Suraski#endif
54866d9314bSZeev Suraski
549d46f8078SJon Parise#if defined(__FreeBSD__) || defined(__DragonFly__)
550d46f8078SJon Parise/* FreeBSD and DragonFly floating point precision fix */
5515928970dSAndi Gutmans#include <floatingpoint.h>
5525928970dSAndi Gutmans#endif
5537a87fcbbSZeev Suraski
554b4892511SJani Taskinenstatic void scanner_globals_ctor(zend_scanner_globals *scanner_globals_p TSRMLS_DC) /* {{{ */
55554d53055SZeev Suraski{
556609d58a4SZeev Suraski	scanner_globals_p->c_buf_p = (char *) 0;
557609d58a4SZeev Suraski	scanner_globals_p->init = 1;
558609d58a4SZeev Suraski	scanner_globals_p->start = 0;
559609d58a4SZeev Suraski	scanner_globals_p->current_buffer = NULL;
560483fc4c7SZeev Suraski	scanner_globals_p->yy_in = NULL;
561483fc4c7SZeev Suraski	scanner_globals_p->yy_out = NULL;
56217352812SZeev Suraski	scanner_globals_p->_yy_more_flag = 0;
56317352812SZeev Suraski	scanner_globals_p->_yy_more_len = 0;
5640bb780b3SZeev Suraski	scanner_globals_p->yy_start_stack_ptr = 0;
5650bb780b3SZeev Suraski	scanner_globals_p->yy_start_stack_depth = 0;
5660bb780b3SZeev Suraski	scanner_globals_p->yy_start_stack = 0;
56754d53055SZeev Suraski}
568b4892511SJani Taskinen/* }}} */
56954d53055SZeev Suraski
570226c56feSJani Taskinenvoid zend_init_opcodes_handlers(void);
57154d53055SZeev Suraski
572b4892511SJani Taskinenint zend_startup(zend_utility_functions *utility_functions, char **extensions, int start_builtin_functions) /* {{{ */
573573b4602SAndi Gutmans{
574a9f9ae79SZeev Suraski#ifdef ZTS
5757c4a0838SZeev Suraski	zend_compiler_globals *compiler_globals;
576a9f9ae79SZeev Suraski	zend_executor_globals *executor_globals;
577609d58a4SZeev Suraski	extern ZEND_API ts_rsrc_id ini_scanner_globals_id;
578609d58a4SZeev Suraski	extern ZEND_API ts_rsrc_id language_scanner_globals_id;
579609d58a4SZeev Suraski#else
580609d58a4SZeev Suraski	extern zend_scanner_globals ini_scanner_globals;
581609d58a4SZeev Suraski	extern zend_scanner_globals language_scanner_globals;
582609d58a4SZeev Suraski#endif
583ca7875abSAntony Dovgal	TSRMLS_FETCH();
584a9f9ae79SZeev Suraski
58529ed52ffSDmitry Stogov	start_memory_manager(TSRMLS_C);
586a9f9ae79SZeev Suraski
587d46f8078SJon Parise#if defined(__FreeBSD__) || defined(__DragonFly__)
588d46f8078SJon Parise	/* FreeBSD and DragonFly floating point precision fix */
589c3340584SSascha Schumann	fpsetmask(0);
5905928970dSAndi Gutmans#endif
591309827ecSZeev Suraski
5924d44a5b7SAntony Dovgal	zend_startup_strtod();
593309827ecSZeev Suraski	zend_startup_extensions_mechanism();
594309827ecSZeev Suraski
595573b4602SAndi Gutmans	/* Set up utility functions and values */
59667f69743SZeev Suraski	zend_error_cb = utility_functions->error_function;
597573b4602SAndi Gutmans	zend_printf = utility_functions->printf_function;
5985dba4774SZeev Suraski	zend_write = (zend_write_func_t) utility_functions->write_function;
599573b4602SAndi Gutmans	zend_fopen = utility_functions->fopen_function;
600573b4602SAndi Gutmans	if (!zend_fopen) {
601573b4602SAndi Gutmans		zend_fopen = zend_fopen_wrapper;
602573b4602SAndi Gutmans	}
603e52aac94SWez Furlong	zend_stream_open_function = utility_functions->stream_open_function;
60456251a72SZeev Suraski	zend_message_dispatcher_p = utility_functions->message_handler;
605573b4602SAndi Gutmans	zend_block_interruptions = utility_functions->block_interruptions;
606573b4602SAndi Gutmans	zend_unblock_interruptions = utility_functions->unblock_interruptions;
607903018efSZeev Suraski	zend_get_configuration_directive_p = utility_functions->get_configuration_directive;
608fafbf6d8SZeev Suraski	zend_ticks_function = utility_functions->ticks_function;
609bd115087SZeev Suraski	zend_on_timeout = utility_functions->on_timeout;
610d45fd8a1SMarcus Boerger	zend_vspprintf = utility_functions->vspprintf_function;
61185a36e2bSZeev Suraski	zend_getenv = utility_functions->getenv_function;
612d035af4aSZeev Suraski
613c06692e9SZeev Suraski	zend_compile_file = compile_file;
6147bbf3532SStefan Esser	zend_compile_string = compile_string;
615573b4602SAndi Gutmans	zend_execute = execute;
6162c44fab3SDerick Rethans	zend_execute_internal = NULL;
617979da661SAndi Gutmans	zend_throw_exception_hook = NULL;
618573b4602SAndi Gutmans
619cf36abb2SAndi Gutmans	zend_init_opcodes_handlers();
620cf36abb2SAndi Gutmans
621573b4602SAndi Gutmans	/* set up version */
622573b4602SAndi Gutmans	zend_version_info = strdup(ZEND_CORE_VERSION_INFO);
623b4892511SJani Taskinen	zend_version_info_length = sizeof(ZEND_CORE_VERSION_INFO) - 1;
624b4892511SJani Taskinen
625b4892511SJani Taskinen	GLOBAL_FUNCTION_TABLE = (HashTable *) malloc(sizeof(HashTable));
626b4892511SJani Taskinen	GLOBAL_CLASS_TABLE = (HashTable *) malloc(sizeof(HashTable));
627b4892511SJani Taskinen	GLOBAL_AUTO_GLOBALS_TABLE = (HashTable *) malloc(sizeof(HashTable));
628b4892511SJani Taskinen	GLOBAL_CONSTANTS_TABLE = (HashTable *) malloc(sizeof(HashTable));
629573b4602SAndi Gutmans
6301263932aSZeev Suraski	zend_hash_init_ex(GLOBAL_FUNCTION_TABLE, 100, NULL, ZEND_FUNCTION_DTOR, 1, 0);
6311263932aSZeev Suraski	zend_hash_init_ex(GLOBAL_CLASS_TABLE, 10, NULL, ZEND_CLASS_DTOR, 1, 0);
632b4892511SJani Taskinen	zend_hash_init_ex(GLOBAL_AUTO_GLOBALS_TABLE, 8, NULL, (dtor_func_t) zend_auto_global_dtor, 1, 0);
633b4892511SJani Taskinen	zend_hash_init_ex(GLOBAL_CONSTANTS_TABLE, 20, NULL, ZEND_CONSTANT_DTOR, 1, 0);
6342eabb14dSAndi Gutmans
6351263932aSZeev Suraski	zend_hash_init_ex(&module_registry, 50, NULL, ZEND_MODULE_DTOR, 1, 0);
6364cb9a483SZeev Suraski	zend_init_rsrc_list_dtors();
637573b4602SAndi Gutmans
638213d0e2fSAndi Gutmans	/* This zval can be used to initialize allocate zval's to an uninit'ed value */
6394b4d634cSYiduo (David) Wang	Z_UNSET_ISREF(zval_used_for_init);
6404b4d634cSYiduo (David) Wang	Z_SET_REFCOUNT(zval_used_for_init, 1);
641b4892511SJani Taskinen	Z_TYPE(zval_used_for_init) = IS_NULL;
642213d0e2fSAndi Gutmans
6437a87fcbbSZeev Suraski#ifdef ZTS
6442c254ba7SZeev Suraski	ts_allocate_id(&compiler_globals_id, sizeof(zend_compiler_globals), (ts_allocate_ctor) compiler_globals_ctor, (ts_allocate_dtor) compiler_globals_dtor);
6452c254ba7SZeev Suraski	ts_allocate_id(&executor_globals_id, sizeof(zend_executor_globals), (ts_allocate_ctor) executor_globals_ctor, (ts_allocate_dtor) executor_globals_dtor);
646609d58a4SZeev Suraski	ts_allocate_id(&language_scanner_globals_id, sizeof(zend_scanner_globals), (ts_allocate_ctor) scanner_globals_ctor, NULL);
647609d58a4SZeev Suraski	ts_allocate_id(&ini_scanner_globals_id, sizeof(zend_scanner_globals), (ts_allocate_ctor) scanner_globals_ctor, NULL);
6487c4a0838SZeev Suraski	compiler_globals = ts_resource(compiler_globals_id);
649a9f9ae79SZeev Suraski	executor_globals = ts_resource(executor_globals_id);
6502c254ba7SZeev Suraski	tsrm_ls = ts_resource_ex(0, NULL);
6515fcae67eSZeev Suraski
6528fb5ea30SMarcus Boerger	compiler_globals_dtor(compiler_globals TSRMLS_CC);
6532a8426a6SMarcus Boerger	compiler_globals->in_compilation = 0;
6548fb5ea30SMarcus Boerger	compiler_globals->function_table = (HashTable *) malloc(sizeof(HashTable));
6558fb5ea30SMarcus Boerger	compiler_globals->class_table = (HashTable *) malloc(sizeof(HashTable));
6568fb5ea30SMarcus Boerger
657f4b832d2SAndi Gutmans	*compiler_globals->function_table = *GLOBAL_FUNCTION_TABLE;
658f4b832d2SAndi Gutmans	*compiler_globals->class_table = *GLOBAL_CLASS_TABLE;
6599d11db12SAndi Gutmans	compiler_globals->auto_globals = GLOBAL_AUTO_GLOBALS_TABLE;
6605fcae67eSZeev Suraski
6615fcae67eSZeev Suraski	zend_hash_destroy(executor_globals->zend_constants);
6625fcae67eSZeev Suraski	*executor_globals->zend_constants = *GLOBAL_CONSTANTS_TABLE;
663762158aaSAndi Gutmans#else
664609d58a4SZeev Suraski	scanner_globals_ctor(&ini_scanner_globals TSRMLS_CC);
665609d58a4SZeev Suraski	scanner_globals_ctor(&language_scanner_globals TSRMLS_CC);
666b4f3b9d3SZeev Suraski	zend_set_default_compile_time_values(TSRMLS_C);
6671f23e507SZeev Suraski	EG(user_error_handler) = NULL;
66804788f95SStig Bakken	EG(user_exception_handler) = NULL;
6697a87fcbbSZeev Suraski#endif
6706d300bafSSebastian Bergmann	register_standard_class(TSRMLS_C);
6712c254ba7SZeev Suraski	zend_register_standard_constants(TSRMLS_C);
672b4892511SJani Taskinen	zend_register_auto_global("GLOBALS", sizeof("GLOBALS") - 1, NULL TSRMLS_CC);
6737a87fcbbSZeev Suraski
674bfbe8618SZeev Suraski#ifndef ZTS
6752c254ba7SZeev Suraski	zend_init_rsrc_plist(TSRMLS_C);
676bfbe8618SZeev Suraski#endif
677a9f9ae79SZeev Suraski
67813e74131SAndi Gutmans	if (start_builtin_functions) {
679b5770382SZeev Suraski		zend_startup_builtin_functions(TSRMLS_C);
68013e74131SAndi Gutmans	}
68113d840bcSZeev Suraski
6822c254ba7SZeev Suraski	zend_ini_startup(TSRMLS_C);
68354d53055SZeev Suraski
68454d53055SZeev Suraski#ifdef ZTS
68554d53055SZeev Suraski	tsrm_set_new_thread_end_handler(zend_new_thread_end_handler);
68654d53055SZeev Suraski#endif
68754d53055SZeev Suraski
688573b4602SAndi Gutmans	return SUCCESS;
689573b4602SAndi Gutmans}
690b4892511SJani Taskinen/* }}} */
691573b4602SAndi Gutmans
692b4892511SJani Taskinenvoid zend_register_standard_ini_entries(TSRMLS_D) /* {{{ */
6934be862b0SZeev Suraski{
6944be862b0SZeev Suraski	int module_number = 0;
6954be862b0SZeev Suraski
6964be862b0SZeev Suraski	REGISTER_INI_ENTRIES();
6974be862b0SZeev Suraski}
698b4892511SJani Taskinen/* }}} */
6994be862b0SZeev Suraski
700711be83dSZeev Suraski/* Unlink the global (r/o) copies of the class, function and constant tables,
701711be83dSZeev Suraski * and use a fresh r/w copy for the startup thread
702711be83dSZeev Suraski */
703b4892511SJani Taskinenvoid zend_post_startup(TSRMLS_D) /* {{{ */
704711be83dSZeev Suraski{
705b4892511SJani Taskinen#ifdef ZTS
706711be83dSZeev Suraski	zend_compiler_globals *compiler_globals = ts_resource(compiler_globals_id);
7075fcae67eSZeev Suraski	zend_executor_globals *executor_globals = ts_resource(executor_globals_id);
708711be83dSZeev Suraski
7095fcae67eSZeev Suraski	*GLOBAL_FUNCTION_TABLE = *compiler_globals->function_table;
7105fcae67eSZeev Suraski	*GLOBAL_CLASS_TABLE = *compiler_globals->class_table;
7115fcae67eSZeev Suraski	*GLOBAL_CONSTANTS_TABLE = *executor_globals->zend_constants;
7122f586e96SDmitry Stogov
7132f586e96SDmitry Stogov	asp_tags_default = CG(asp_tags);
7142f586e96SDmitry Stogov	short_tags_default = CG(short_tags);
7152f586e96SDmitry Stogov	ct_pass_ref_default = CG(allow_call_time_pass_reference);
7162f586e96SDmitry Stogov	extended_info_default = CG(extended_info);
7178ce1211aSMarcus Boerger
718b817dab2SZeev Suraski	zend_destroy_rsrc_list(&EG(persistent_list) TSRMLS_CC);
7198fb5ea30SMarcus Boerger	free(compiler_globals->function_table);
7208fb5ea30SMarcus Boerger	free(compiler_globals->class_table);
721711be83dSZeev Suraski	compiler_globals_ctor(compiler_globals, tsrm_ls);
7228fb5ea30SMarcus Boerger	free(EG(zend_constants));
7235fcae67eSZeev Suraski	executor_globals_ctor(executor_globals, tsrm_ls);
72421698c12SDmitry Stogov	global_persistent_list = &EG(persistent_list);
725a84a2eaeSDmitry Stogov	zend_copy_ini_directives(TSRMLS_C);
726711be83dSZeev Suraski#endif
727b4892511SJani Taskinen}
728b4892511SJani Taskinen/* }}} */
729711be83dSZeev Suraski
730b4892511SJani Taskinenvoid zend_shutdown(TSRMLS_D) /* {{{ */
731573b4602SAndi Gutmans{
73284eb48f0SZeev Suraski#ifdef ZEND_WIN32
73384eb48f0SZeev Suraski	zend_shutdown_timeout_thread();
73484eb48f0SZeev Suraski#endif
7359e1beb4fSDmitry Stogov	zend_destroy_rsrc_list(&EG(persistent_list) TSRMLS_CC);
736d4acdd34SDerick Rethans	zend_hash_graceful_reverse_destroy(&module_registry);
7373458373eSAndi Gutmans
7387a87fcbbSZeev Suraski	zend_hash_destroy(GLOBAL_FUNCTION_TABLE);
73976b9acc1SAndi Gutmans	zend_hash_destroy(GLOBAL_CLASS_TABLE);
7403458373eSAndi Gutmans
7419d11db12SAndi Gutmans	zend_hash_destroy(GLOBAL_AUTO_GLOBALS_TABLE);
7429d11db12SAndi Gutmans	free(GLOBAL_AUTO_GLOBALS_TABLE);
7432da8b014Sfoobar
744d76cf1daSZeev Suraski	zend_shutdown_extensions(TSRMLS_C);
745573b4602SAndi Gutmans	free(zend_version_info);
74648b33032SSterling Hughes
747a2513618SMarcus Boerger	free(GLOBAL_FUNCTION_TABLE);
748a2513618SMarcus Boerger	free(GLOBAL_CLASS_TABLE);
74921698c12SDmitry Stogov
75048b33032SSterling Hughes	zend_hash_destroy(GLOBAL_CONSTANTS_TABLE);
751a54c7dcbSMarcus Boerger	free(GLOBAL_CONSTANTS_TABLE);
7524d44a5b7SAntony Dovgal	zend_shutdown_strtod();
753b4892511SJani Taskinen
75421698c12SDmitry Stogov#ifdef ZTS
7558fb5ea30SMarcus Boerger	GLOBAL_FUNCTION_TABLE = NULL;
7568fb5ea30SMarcus Boerger	GLOBAL_CLASS_TABLE = NULL;
7578fb5ea30SMarcus Boerger	GLOBAL_AUTO_GLOBALS_TABLE = NULL;
75821698c12SDmitry Stogov	GLOBAL_CONSTANTS_TABLE = NULL;
759a17d2f37SSebastian Bergmann#endif
7604c283fffSIlia Alshanetsky	zend_destroy_rsrc_list_dtors();
761573b4602SAndi Gutmans}
762b4892511SJani Taskinen/* }}} */
763573b4602SAndi Gutmans
764b4892511SJani Taskinenvoid zend_set_utility_values(zend_utility_values *utility_values) /* {{{ */
7657656c77cSZeev Suraski{
7667656c77cSZeev Suraski	zend_uv = *utility_values;
7679baad804SZeev Suraski	zend_uv.import_use_extension_length = strlen(zend_uv.import_use_extension);
7687656c77cSZeev Suraski}
769b4892511SJani Taskinen/* }}} */
7707656c77cSZeev Suraski
771573b4602SAndi Gutmans/* this should be compatible with the standard zenderror */
772b4892511SJani Taskinenvoid zenderror(char *error) /* {{{ */
773573b4602SAndi Gutmans{
77410def9a8SIlia Alshanetsky	zend_error(E_PARSE, "%s", error);
775573b4602SAndi Gutmans}
776b4892511SJani Taskinen/* }}} */
777573b4602SAndi Gutmans
778b0bfa458SZeev SuraskiBEGIN_EXTERN_C()
779b4892511SJani TaskinenZEND_API void _zend_bailout(char *filename, uint lineno) /* {{{ */
780573b4602SAndi Gutmans{
7812c254ba7SZeev Suraski	TSRMLS_FETCH();
782573b4602SAndi Gutmans
78314f6b914SDmitry Stogov	if (!EG(bailout)) {
784ef4fe54fSZeev Suraski		zend_output_debug_string(1, "%s(%d) : Bailed out without a bailout address!", filename, lineno);
785931ebe08SZeev Suraski		exit(-1);
786931ebe08SZeev Suraski	}
7876bc6dacfSZeev Suraski	CG(unclean_shutdown) = 1;
788b23d83cfSZeev Suraski	CG(in_compilation) = EG(in_execution) = 0;
789bdc35c9fSThies C. Arntzen	EG(current_execute_data) = NULL;
79014f6b914SDmitry Stogov	longjmp(*EG(bailout), FAILURE);
791573b4602SAndi Gutmans}
792b4892511SJani Taskinen/* }}} */
793b0bfa458SZeev SuraskiEND_EXTERN_C()
794573b4602SAndi Gutmans
795b4892511SJani Taskinenvoid zend_append_version_info(zend_extension *extension) /* {{{ */
796573b4602SAndi Gutmans{
797573b4602SAndi Gutmans	char *new_info;
798573b4602SAndi Gutmans	uint new_info_length;
799573b4602SAndi Gutmans
800573b4602SAndi Gutmans	new_info_length = sizeof("    with  v,  by \n")
801573b4602SAndi Gutmans						+ strlen(extension->name)
802573b4602SAndi Gutmans						+ strlen(extension->version)
803573b4602SAndi Gutmans						+ strlen(extension->copyright)
804573b4602SAndi Gutmans						+ strlen(extension->author);
805573b4602SAndi Gutmans
806b4892511SJani Taskinen	new_info = (char *) malloc(new_info_length + 1);
807573b4602SAndi Gutmans
808573b4602SAndi Gutmans	sprintf(new_info, "    with %s v%s, %s, by %s\n", extension->name, extension->version, extension->copyright, extension->author);
809573b4602SAndi Gutmans
810b4892511SJani Taskinen	zend_version_info = (char *) realloc(zend_version_info, zend_version_info_length+new_info_length + 1);
811573b4602SAndi Gutmans	strcat(zend_version_info, new_info);
812573b4602SAndi Gutmans	zend_version_info_length += new_info_length;
813573b4602SAndi Gutmans	free(new_info);
814573b4602SAndi Gutmans}
815b4892511SJani Taskinen/* }}} */
816573b4602SAndi Gutmans
817b4892511SJani TaskinenZEND_API char *get_zend_version(void) /* {{{ */
818573b4602SAndi Gutmans{
819573b4602SAndi Gutmans	return zend_version_info;
820573b4602SAndi Gutmans}
821b4892511SJani Taskinen/* }}} */
822ed06a70fSZeev Suraski
823b4892511SJani Taskinenvoid zend_activate(TSRMLS_D) /* {{{ */
824ed06a70fSZeev Suraski{
825b4f3b9d3SZeev Suraski	init_compiler(TSRMLS_C);
826b4f3b9d3SZeev Suraski	init_executor(TSRMLS_C);
827b4f3b9d3SZeev Suraski	startup_scanner(TSRMLS_C);
828ed06a70fSZeev Suraski}
829b4892511SJani Taskinen/* }}} */
830ed06a70fSZeev Suraski
831b4892511SJani Taskinenvoid zend_activate_modules(TSRMLS_D) /* {{{ */
832dc0e6632SZeev Suraski{
833d76cf1daSZeev Suraski	zend_hash_apply(&module_registry, (apply_func_t) module_registry_request_startup TSRMLS_CC);
834dc0e6632SZeev Suraski}
835b4892511SJani Taskinen/* }}} */
836dc0e6632SZeev Suraski
837b4892511SJani Taskinenvoid zend_deactivate_modules(TSRMLS_D) /* {{{ */
838a8b0d960SAndi Gutmans{
839a8b0d960SAndi Gutmans	EG(opline_ptr) = NULL; /* we're no longer executing anything */
840a8b0d960SAndi Gutmans
841931ebe08SZeev Suraski	zend_try {
842d76cf1daSZeev Suraski		zend_hash_apply(&module_registry, (apply_func_t) module_registry_cleanup TSRMLS_CC);
843931ebe08SZeev Suraski	} zend_end_try();
844a8b0d960SAndi Gutmans}
845b4892511SJani Taskinen/* }}} */
846dc0e6632SZeev Suraski
847b4892511SJani Taskinenvoid zend_call_destructors(TSRMLS_D) /* {{{ */
8480cd2da03SMarcus Boerger{
8490cd2da03SMarcus Boerger	zend_try {
8500cd2da03SMarcus Boerger		shutdown_destructors(TSRMLS_C);
8510cd2da03SMarcus Boerger	} zend_end_try();
8520cd2da03SMarcus Boerger}
853b4892511SJani Taskinen/* }}} */
8540cd2da03SMarcus Boerger
855b4892511SJani Taskinenvoid zend_deactivate(TSRMLS_D) /* {{{ */
856ed06a70fSZeev Suraski{
857372f71ebSThies C. Arntzen	/* we're no longer executing anything */
858424e98f4SZeev Suraski	EG(opline_ptr) = NULL;
859372f71ebSThies C. Arntzen	EG(active_symbol_table) = NULL;
860f4c9d96fSThies C. Arntzen
861931ebe08SZeev Suraski	zend_try {
862b4f3b9d3SZeev Suraski		shutdown_scanner(TSRMLS_C);
863931ebe08SZeev Suraski	} zend_end_try();
864931ebe08SZeev Suraski
8658084d278SZeev Suraski	/* shutdown_executor() takes care of its own bailout handling */
8662c254ba7SZeev Suraski	shutdown_executor(TSRMLS_C);
867931ebe08SZeev Suraski
868931ebe08SZeev Suraski	zend_try {
869b4f3b9d3SZeev Suraski		shutdown_compiler(TSRMLS_C);
870931ebe08SZeev Suraski	} zend_end_try();
871931ebe08SZeev Suraski
87259a46c34SDmitry Stogov	zend_destroy_rsrc_list(&EG(regular_list) TSRMLS_CC);
87359a46c34SDmitry Stogov
874931ebe08SZeev Suraski	zend_try {
8752c254ba7SZeev Suraski		zend_ini_deactivate(TSRMLS_C);
876931ebe08SZeev Suraski	} zend_end_try();
877ed06a70fSZeev Suraski}
878b4892511SJani Taskinen/* }}} */
87956251a72SZeev Suraski
880b4892511SJani Taskinenstatic int exec_done_cb(zend_module_entry *module TSRMLS_DC) /* {{{ */
8812fd4ffceSZeev Suraski{
8826535933fSDerick Rethans	if (module->post_deactivate_func) {
8836535933fSDerick Rethans		module->post_deactivate_func();
8842fd4ffceSZeev Suraski	}
8852fd4ffceSZeev Suraski	return 0;
8862fd4ffceSZeev Suraski}
887b4892511SJani Taskinen/* }}} */
8882fd4ffceSZeev Suraski
889b4892511SJani Taskinenvoid zend_post_deactivate_modules(TSRMLS_D) /* {{{ */
8902fd4ffceSZeev Suraski{
8912fd4ffceSZeev Suraski	zend_hash_apply(&module_registry, (apply_func_t) exec_done_cb TSRMLS_CC);
892c31c9843SDmitry Stogov	zend_hash_reverse_apply(&module_registry, (apply_func_t) module_registry_unload_temp TSRMLS_CC);
8932fd4ffceSZeev Suraski}
894b4892511SJani Taskinen/* }}} */
8952fd4ffceSZeev Suraski
896fc173b6aSSam RubyBEGIN_EXTERN_C()
897b4892511SJani TaskinenZEND_API void zend_message_dispatcher(long message, void *data) /* {{{ */
89856251a72SZeev Suraski{
89956251a72SZeev Suraski	if (zend_message_dispatcher_p) {
90056251a72SZeev Suraski		zend_message_dispatcher_p(message, data);
90156251a72SZeev Suraski	}
90256251a72SZeev Suraski}
903b4892511SJani Taskinen/* }}} */
904fc173b6aSSam RubyEND_EXTERN_C()
90556251a72SZeev Suraski
906b4892511SJani TaskinenZEND_API int zend_get_configuration_directive(char *name, uint name_length, zval *contents) /* {{{ */
90756251a72SZeev Suraski{
908903018efSZeev Suraski	if (zend_get_configuration_directive_p) {
909903018efSZeev Suraski		return zend_get_configuration_directive_p(name, name_length, contents);
91056251a72SZeev Suraski	} else {
91156251a72SZeev Suraski		return FAILURE;
91256251a72SZeev Suraski	}
91356251a72SZeev Suraski}
914b4892511SJani Taskinen/* }}} */
91567f69743SZeev Suraski
916b4892511SJani TaskinenZEND_API void zend_error(int type, const char *format, ...) /* {{{ */
91767f69743SZeev Suraski{
91867f69743SZeev Suraski	va_list args;
9199e9c193eSStanislav Malyshev	va_list usr_copy;
9203cda6a75SZeev Suraski	zval ***params;
9213cda6a75SZeev Suraski	zval *retval;
922f34b9d41SZeev Suraski	zval *z_error_type, *z_error_message, *z_error_filename, *z_error_lineno, *z_context;
923b80b8381SZeev Suraski	char *error_filename;
924b80b8381SZeev Suraski	uint error_lineno;
925a31dc47aSZeev Suraski	zval *orig_user_error_handler;
9262e1a2438SDmitry Stogov	zend_bool in_compilation;
9272e1a2438SDmitry Stogov	zend_class_entry *saved_class_entry;
9282c254ba7SZeev Suraski	TSRMLS_FETCH();
92967f69743SZeev Suraski
930b80b8381SZeev Suraski	/* Obtain relevant filename and lineno */
931b80b8381SZeev Suraski	switch (type) {
932b80b8381SZeev Suraski		case E_CORE_ERROR:
933b80b8381SZeev Suraski		case E_CORE_WARNING:
934b80b8381SZeev Suraski			error_filename = NULL;
935b80b8381SZeev Suraski			error_lineno = 0;
936b80b8381SZeev Suraski			break;
937b80b8381SZeev Suraski		case E_PARSE:
938b80b8381SZeev Suraski		case E_COMPILE_ERROR:
939b80b8381SZeev Suraski		case E_COMPILE_WARNING:
940b80b8381SZeev Suraski		case E_ERROR:
941b80b8381SZeev Suraski		case E_NOTICE:
942c0df4502SAndi Gutmans		case E_STRICT:
943b80b8381SZeev Suraski		case E_WARNING:
944b80b8381SZeev Suraski		case E_USER_ERROR:
945b80b8381SZeev Suraski		case E_USER_WARNING:
946b80b8381SZeev Suraski		case E_USER_NOTICE:
9478a03fc5eSAntony Dovgal		case E_RECOVERABLE_ERROR:
9484187439cSZeev Suraski			if (zend_is_compiling(TSRMLS_C)) {
949b4f3b9d3SZeev Suraski				error_filename = zend_get_compiled_filename(TSRMLS_C);
950b4f3b9d3SZeev Suraski				error_lineno = zend_get_compiled_lineno(TSRMLS_C);
9514187439cSZeev Suraski			} else if (zend_is_executing(TSRMLS_C)) {
9522c254ba7SZeev Suraski				error_filename = zend_get_executed_filename(TSRMLS_C);
9532c254ba7SZeev Suraski				error_lineno = zend_get_executed_lineno(TSRMLS_C);
954b80b8381SZeev Suraski			} else {
955b80b8381SZeev Suraski				error_filename = NULL;
956b80b8381SZeev Suraski				error_lineno = 0;
957b80b8381SZeev Suraski			}
958b80b8381SZeev Suraski			break;
959b80b8381SZeev Suraski		default:
960b80b8381SZeev Suraski			error_filename = NULL;
961b80b8381SZeev Suraski			error_lineno = 0;
962b80b8381SZeev Suraski			break;
963b80b8381SZeev Suraski	}
964b80b8381SZeev Suraski	if (!error_filename) {
965b80b8381SZeev Suraski		error_filename = "Unknown";
966b80b8381SZeev Suraski	}
96767f69743SZeev Suraski
968b80b8381SZeev Suraski	va_start(args, format);
9698a8c3eafSZeev Suraski
97067f69743SZeev Suraski	/* if we don't have a user defined error handler */
97132007b19SZeev Suraski	if (!EG(user_error_handler)
97232007b19SZeev Suraski		|| !(EG(user_error_handler_error_reporting) & type)) {
973b80b8381SZeev Suraski		zend_error_cb(type, error_filename, error_lineno, format, args);
974b80b8381SZeev Suraski	} else switch (type) {
97567f69743SZeev Suraski		case E_ERROR:
97667f69743SZeev Suraski		case E_PARSE:
97767f69743SZeev Suraski		case E_CORE_ERROR:
97867f69743SZeev Suraski		case E_CORE_WARNING:
97967f69743SZeev Suraski		case E_COMPILE_ERROR:
98067f69743SZeev Suraski		case E_COMPILE_WARNING:
981b80b8381SZeev Suraski			/* The error may not be safe to handle in user-space */
982b80b8381SZeev Suraski			zend_error_cb(type, error_filename, error_lineno, format, args);
983b80b8381SZeev Suraski			break;
984b80b8381SZeev Suraski		default:
985b80b8381SZeev Suraski			/* Handle the error in user space */
986f34b9d41SZeev Suraski			ALLOC_INIT_ZVAL(z_error_message);
987f34b9d41SZeev Suraski			ALLOC_INIT_ZVAL(z_error_type);
988a31dc47aSZeev Suraski			ALLOC_INIT_ZVAL(z_error_filename);
989a31dc47aSZeev Suraski			ALLOC_INIT_ZVAL(z_error_lineno);
9905dac0007SIlia Alshanetsky			ALLOC_INIT_ZVAL(z_context);
991d15ad460SMarcus Boerger
992990aa745Sfoobar/* va_copy() is __va_copy() in old gcc versions.
993990aa745Sfoobar * According to the autoconf manual, using
994990aa745Sfoobar * memcpy(&dst, &src, sizeof(va_list))
995990aa745Sfoobar * gives maximum portability. */
996990aa745Sfoobar#ifndef va_copy
997990aa745Sfoobar# ifdef __va_copy
998990aa745Sfoobar#  define va_copy(dest, src)	__va_copy((dest), (src))
999