zend.c revision 54d53055
1573b4602SAndi Gutmans/*
2573b4602SAndi Gutmans   +----------------------------------------------------------------------+
3573b4602SAndi Gutmans   | Zend Engine                                                          |
4573b4602SAndi Gutmans   +----------------------------------------------------------------------+
55e55e47fSZeev Suraski   | Copyright (c) 1998-2000 Zend Technologies Ltd. (http://www.zend.com) |
6573b4602SAndi Gutmans   +----------------------------------------------------------------------+
75e55e47fSZeev Suraski   | This source file is subject to version 0.92 of the Zend license,     |
8b5b11177SZeev Suraski   | that is bundled with this package in the file LICENSE, and is        |
9b5b11177SZeev Suraski   | available at through the world-wide-web at                           |
105e55e47fSZeev Suraski   | http://www.zend.com/license/0_92.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
20a6043d3eSRasmus Lerdorf
21573b4602SAndi Gutmans#include "zend.h"
22573b4602SAndi Gutmans#include "zend_extensions.h"
23573b4602SAndi Gutmans#include "modules.h"
24573b4602SAndi Gutmans#include "zend_constants.h"
25573b4602SAndi Gutmans#include "zend_list.h"
26da9faa2cSZeev Suraski#include "zend_API.h"
2713d840bcSZeev Suraski#include "zend_builtin_functions.h"
2854d53055SZeev Suraski#include "zend_ini.h"
29573b4602SAndi Gutmans
307a87fcbbSZeev Suraski#ifdef ZTS
317a87fcbbSZeev Suraski#	define GLOBAL_FUNCTION_TABLE	global_function_table
327a87fcbbSZeev Suraski#	define GLOBAL_CLASS_TABLE		global_class_table
33fec59d3bSAndi Gutmans#	define GLOBAL_CONSTANTS_TABLE	global_constants_table
347a87fcbbSZeev Suraski#else
357a87fcbbSZeev Suraski#	define GLOBAL_FUNCTION_TABLE	CG(function_table)
367a87fcbbSZeev Suraski#	define GLOBAL_CLASS_TABLE		CG(class_table)
37fec59d3bSAndi Gutmans#	define GLOBAL_CONSTANTS_TABLE	CG(zend_constants)
387a87fcbbSZeev Suraski#endif
397a87fcbbSZeev Suraski
40f34b9d41SZeev Suraski#if defined(ZEND_WIN32) && ZEND_DEBUG
412ffe85ddSZeev SuraskiBOOL WINAPI IsDebuggerPresent(VOID);
422ffe85ddSZeev Suraski#endif
432ffe85ddSZeev Suraski
44573b4602SAndi Gutmans/* true multithread-shared globals */
459108abc2SZeev SuraskiZEND_API zend_class_entry zend_standard_class_def;
46573b4602SAndi GutmansZEND_API int (*zend_printf)(const char *format, ...);
475dba4774SZeev SuraskiZEND_API zend_write_func_t zend_write;
485463dd5bSAndi GutmansZEND_API FILE *(*zend_fopen)(const char *filename, char **opened_path);
4993536507SStig S. BakkenZEND_API void (*zend_block_interruptions)(void);
5093536507SStig S. BakkenZEND_API void (*zend_unblock_interruptions)(void);
51f1a2ee55SAndi GutmansZEND_API void (*zend_ticks_function)(int ticks);
52536221beSStanislav MalyshevZEND_API void (*zend_error_cb)(int type, const char *error_filename, const uint error_lineno, const char *format, va_list args);
53536221beSStanislav Malyshev
5456251a72SZeev Suraskistatic void (*zend_message_dispatcher_p)(long message, void *data);
5556251a72SZeev Suraskistatic int (*zend_get_ini_entry_p)(char *name, uint name_length, zval *contents);
56d035af4aSZeev Suraski
5767f69743SZeev Suraski
587a87fcbbSZeev Suraski#ifdef ZTS
59e06a1ed2SZeev SuraskiZEND_API int compiler_globals_id;
60e06a1ed2SZeev SuraskiZEND_API int executor_globals_id;
615864b57eSZeev SuraskiZEND_API int alloc_globals_id;
627a87fcbbSZeev SuraskiHashTable *global_function_table;
637a87fcbbSZeev SuraskiHashTable *global_class_table;
64fec59d3bSAndi GutmansHashTable *global_constants_table;
657a87fcbbSZeev Suraski#endif
66573b4602SAndi Gutmans
67573b4602SAndi Gutmanszend_utility_values zend_uv;
68573b4602SAndi Gutmans
695ba5f00fSZeev SuraskiZEND_API zval zval_used_for_init; /* True global variable */
70213d0e2fSAndi Gutmans
71573b4602SAndi Gutmans/* version information */
72573b4602SAndi Gutmansstatic char *zend_version_info;
73573b4602SAndi Gutmansstatic uint zend_version_info_length;
740302ee59SZeev Suraski#define ZEND_CORE_VERSION_INFO	"Zend Engine v" ZEND_VERSION ", Copyright (c) 1998-2000 Zend Technologies\n"
75573b4602SAndi Gutmans
76573b4602SAndi Gutmans
7780bab9d9SAndrei Zmievski#define PRINT_ZVAL_INDENT 4
78573b4602SAndi Gutmans
79573b4602SAndi Gutmansstatic void print_hash(HashTable *ht, int indent)
80573b4602SAndi Gutmans{
81573b4602SAndi Gutmans	zval **tmp;
82573b4602SAndi Gutmans	char *string_key;
8363873abaSStanislav Malyshev	HashPosition iterator;
8463873abaSStanislav Malyshev	unsigned long num_key, str_len;
85573b4602SAndi Gutmans	int i;
86573b4602SAndi Gutmans
87573b4602SAndi Gutmans	for (i=0; i<indent; i++) {
88573b4602SAndi Gutmans		ZEND_PUTS(" ");
89573b4602SAndi Gutmans	}
90573b4602SAndi Gutmans	ZEND_PUTS("(\n");
9180bab9d9SAndrei Zmievski	indent += PRINT_ZVAL_INDENT;
9263873abaSStanislav Malyshev	zend_hash_internal_pointer_reset_ex(ht, &iterator);
9363873abaSStanislav Malyshev	while (zend_hash_get_current_data_ex(ht, (void **) &tmp, &iterator) == SUCCESS) {
94573b4602SAndi Gutmans		for (i=0; i<indent; i++) {
95573b4602SAndi Gutmans			ZEND_PUTS(" ");
96573b4602SAndi Gutmans		}
97573b4602SAndi Gutmans		ZEND_PUTS("[");
9836eaad25SZeev Suraski		switch (zend_hash_get_current_key_ex(ht, &string_key, &str_len, &num_key, 0, &iterator)) {
99573b4602SAndi Gutmans			case HASH_KEY_IS_STRING:
100573b4602SAndi Gutmans				ZEND_PUTS(string_key);
101573b4602SAndi Gutmans				break;
102573b4602SAndi Gutmans			case HASH_KEY_IS_LONG:
103573b4602SAndi Gutmans				zend_printf("%ld",num_key);
104573b4602SAndi Gutmans				break;
105573b4602SAndi Gutmans		}
106573b4602SAndi Gutmans		ZEND_PUTS("] => ");
10780bab9d9SAndrei Zmievski		zend_print_zval_r(*tmp, indent+PRINT_ZVAL_INDENT);
108573b4602SAndi Gutmans		ZEND_PUTS("\n");
10963873abaSStanislav Malyshev		zend_hash_move_forward_ex(ht, &iterator);
110573b4602SAndi Gutmans	}
11180bab9d9SAndrei Zmievski	indent -= PRINT_ZVAL_INDENT;
112573b4602SAndi Gutmans	for (i=0; i<indent; i++) {
113573b4602SAndi Gutmans		ZEND_PUTS(" ");
114573b4602SAndi Gutmans	}
115573b4602SAndi Gutmans	ZEND_PUTS(")\n");
116573b4602SAndi Gutmans}
117573b4602SAndi Gutmans
1187a87fcbbSZeev Suraski
119da9faa2cSZeev SuraskiZEND_API void zend_make_printable_zval(zval *expr, zval *expr_copy, int *use_copy)
120573b4602SAndi Gutmans{
121da9faa2cSZeev Suraski	if (expr->type==IS_STRING) {
122da9faa2cSZeev Suraski		*use_copy = 0;
123da9faa2cSZeev Suraski		return;
124da9faa2cSZeev Suraski	}
125da9faa2cSZeev Suraski	switch (expr->type) {
126bdefd5daSAndi Gutmans		case IS_NULL:
127f2d703e9SZeev Suraski			expr_copy->value.str.len = 0;
128f2d703e9SZeev Suraski			expr_copy->value.str.val = empty_string;
1298c4ab98eSThies C. Arntzen			break;
130da9faa2cSZeev Suraski		case IS_BOOL:
13191d7ba76SAndi Gutmans			if (expr->value.lval) {
13291d7ba76SAndi Gutmans				expr_copy->value.str.len = 1;
13391d7ba76SAndi Gutmans				expr_copy->value.str.val = estrndup("1", 1);
13491d7ba76SAndi Gutmans			} else {
13591d7ba76SAndi Gutmans				expr_copy->value.str.len = 0;
13691d7ba76SAndi Gutmans				expr_copy->value.str.val = empty_string;
13791d7ba76SAndi Gutmans			}
138da9faa2cSZeev Suraski			break;
139da9faa2cSZeev Suraski		case IS_RESOURCE:
140da9faa2cSZeev Suraski			expr_copy->value.str.val = (char *) emalloc(sizeof("Resource id #")-1 + MAX_LENGTH_OF_LONG);
141da9faa2cSZeev Suraski			expr_copy->value.str.len = sprintf(expr_copy->value.str.val, "Resource id #%ld", expr->value.lval);
142da9faa2cSZeev Suraski			break;
143da9faa2cSZeev Suraski		case IS_ARRAY:
144da9faa2cSZeev Suraski			expr_copy->value.str.len = sizeof("Array")-1;
145da9faa2cSZeev Suraski			expr_copy->value.str.val = estrndup("Array", expr_copy->value.str.len);
146da9faa2cSZeev Suraski			break;
1471b282b35SAndi Gutmans		case IS_OBJECT:
1481b282b35SAndi Gutmans			expr_copy->value.str.len = sizeof("Object")-1;
1491b282b35SAndi Gutmans			expr_copy->value.str.val = estrndup("Object", expr_copy->value.str.len);
150da9faa2cSZeev Suraski			break;
151da9faa2cSZeev Suraski		default:
152da9faa2cSZeev Suraski			*expr_copy = *expr;
153da9faa2cSZeev Suraski			zval_copy_ctor(expr_copy);
154da9faa2cSZeev Suraski			convert_to_string(expr_copy);
155da9faa2cSZeev Suraski			break;
156da9faa2cSZeev Suraski	}
157da9faa2cSZeev Suraski	expr_copy->type = IS_STRING;
158da9faa2cSZeev Suraski	*use_copy = 1;
159da9faa2cSZeev Suraski}
160da9faa2cSZeev Suraski
161da9faa2cSZeev Suraski
162da9faa2cSZeev SuraskiZEND_API int zend_print_zval(zval *expr, int indent)
163da9faa2cSZeev Suraski{
1645dba4774SZeev Suraski	return zend_print_zval_ex(zend_write, expr, indent);
1655dba4774SZeev Suraski}
1665dba4774SZeev Suraski
1675dba4774SZeev Suraski
1685dba4774SZeev SuraskiZEND_API int zend_print_zval_ex(zend_write_func_t write_func, zval *expr, int indent)
1695dba4774SZeev Suraski{
170da9faa2cSZeev Suraski	zval expr_copy;
171da9faa2cSZeev Suraski	int use_copy;
172da9faa2cSZeev Suraski
173da9faa2cSZeev Suraski	zend_make_printable_zval(expr, &expr_copy, &use_copy);
174da9faa2cSZeev Suraski	if (use_copy) {
175573b4602SAndi Gutmans		expr = &expr_copy;
176573b4602SAndi Gutmans	}
177573b4602SAndi Gutmans	if (expr->value.str.len==0) { /* optimize away empty strings */
178da9faa2cSZeev Suraski		if (use_copy) {
179da9faa2cSZeev Suraski			zval_dtor(expr);
180da9faa2cSZeev Suraski		}
181573b4602SAndi Gutmans		return 0;
182573b4602SAndi Gutmans	}
1835dba4774SZeev Suraski	write_func(expr->value.str.val,expr->value.str.len);
184da9faa2cSZeev Suraski	if (use_copy) {
185573b4602SAndi Gutmans		zval_dtor(expr);
186573b4602SAndi Gutmans	}
187573b4602SAndi Gutmans	return expr->value.str.len;
188573b4602SAndi Gutmans}
189573b4602SAndi Gutmans
190573b4602SAndi Gutmans
1915dba4774SZeev SuraskiZEND_API void zend_print_zval_r(zval *expr, int indent)
1925dba4774SZeev Suraski{
1935dba4774SZeev Suraski	zend_print_zval_r_ex(zend_write, expr, indent);
1945dba4774SZeev Suraski}
1955dba4774SZeev Suraski
1965dba4774SZeev Suraski
1975dba4774SZeev SuraskiZEND_API void zend_print_zval_r_ex(zend_write_func_t write_func, zval *expr, int indent)
198573b4602SAndi Gutmans{
199573b4602SAndi Gutmans	switch(expr->type) {
200573b4602SAndi Gutmans		case IS_ARRAY:
201573b4602SAndi Gutmans			ZEND_PUTS("Array\n");
2026053f2a1SZeev Suraski			if (++expr->value.ht->nApplyCount>1) {
2036053f2a1SZeev Suraski				ZEND_PUTS(" *RECURSION*");
2046053f2a1SZeev Suraski				expr->value.ht->nApplyCount=0;
2056053f2a1SZeev Suraski				return;
2066053f2a1SZeev Suraski			}
207573b4602SAndi Gutmans			print_hash(expr->value.ht,indent);
2086053f2a1SZeev Suraski			expr->value.ht->nApplyCount--;
209573b4602SAndi Gutmans			break;
210573b4602SAndi Gutmans		case IS_OBJECT:
2116053f2a1SZeev Suraski			if (++expr->value.obj.properties->nApplyCount>1) {
2126053f2a1SZeev Suraski				ZEND_PUTS(" *RECURSION*");
2136053f2a1SZeev Suraski				expr->value.obj.properties->nApplyCount=0;
2146053f2a1SZeev Suraski				return;
2156053f2a1SZeev Suraski			}
216573b4602SAndi Gutmans			zend_printf("%s Object\n", expr->value.obj.ce->name);
217573b4602SAndi Gutmans			print_hash(expr->value.obj.properties, indent);
2186053f2a1SZeev Suraski			expr->value.obj.properties->nApplyCount--;
219573b4602SAndi Gutmans			break;
220573b4602SAndi Gutmans		default:
221573b4602SAndi Gutmans			zend_print_variable(expr);
222573b4602SAndi Gutmans			break;
223573b4602SAndi Gutmans	}
224573b4602SAndi Gutmans}
225573b4602SAndi Gutmans
226573b4602SAndi Gutmans
2275463dd5bSAndi Gutmansstatic FILE *zend_fopen_wrapper(const char *filename, char **opened_path)
228573b4602SAndi Gutmans{
2295463dd5bSAndi Gutmans	if (opened_path) {
23027de5001SAndi Gutmans		*opened_path = estrdup(filename);
2315463dd5bSAndi Gutmans	}
232f311ab16SAndi Gutmans	return fopen(filename, "rb");
233573b4602SAndi Gutmans}
234573b4602SAndi Gutmans
235573b4602SAndi Gutmans
23693536507SStig S. Bakkenstatic void register_standard_class(void)
237573b4602SAndi Gutmans{
2389108abc2SZeev Suraski	zend_standard_class_def.type = ZEND_INTERNAL_CLASS;
2399108abc2SZeev Suraski	zend_standard_class_def.name_length = sizeof("stdClass") - 1;
2409108abc2SZeev Suraski	zend_standard_class_def.name = zend_strndup("stdClass", zend_standard_class_def.name_length);
2419108abc2SZeev Suraski	zend_standard_class_def.parent = NULL;
2421263932aSZeev Suraski	zend_hash_init_ex(&zend_standard_class_def.default_properties, 0, NULL, ZVAL_PTR_DTOR, 1, 0);
2431263932aSZeev Suraski	zend_hash_init_ex(&zend_standard_class_def.function_table, 0, NULL, ZEND_FUNCTION_DTOR, 1, 0);
2449108abc2SZeev Suraski	zend_standard_class_def.handle_function_call = NULL;
2459108abc2SZeev Suraski	zend_standard_class_def.handle_property_get = NULL;
2469108abc2SZeev Suraski	zend_standard_class_def.handle_property_set = NULL;
2479108abc2SZeev Suraski	zend_standard_class_def.refcount = (int *) malloc(sizeof(int));
2489108abc2SZeev Suraski	*zend_standard_class_def.refcount = 1;
2495f1793e2SZeev Suraski	zend_hash_add(GLOBAL_CLASS_TABLE, "stdclass", sizeof("stdclass"), &zend_standard_class_def, sizeof(zend_class_entry), NULL);
250573b4602SAndi Gutmans}
251573b4602SAndi Gutmans
252573b4602SAndi Gutmans
2538b794bb1SZeev Suraskistatic void zend_set_default_compile_time_values(CLS_D)
2548b794bb1SZeev Suraski{
2558b794bb1SZeev Suraski	/* default compile-time values */
2568b794bb1SZeev Suraski	CG(asp_tags) = 0;
2578b794bb1SZeev Suraski	CG(short_tags) = 1;
2588b794bb1SZeev Suraski	CG(allow_call_time_pass_reference) = 1;
2598b794bb1SZeev Suraski	CG(extended_info) = 0;
2608b794bb1SZeev Suraski}
2618b794bb1SZeev Suraski
2628b794bb1SZeev Suraski
2637a87fcbbSZeev Suraski#ifdef ZTS
2647a87fcbbSZeev Suraskistatic void compiler_globals_ctor(zend_compiler_globals *compiler_globals)
2657a87fcbbSZeev Suraski{
2667c4a0838SZeev Suraski	zend_function tmp_func;
2677c4a0838SZeev Suraski	zend_class_entry tmp_class;
2687c4a0838SZeev Suraski
2697f4909d9SThies C. Arntzen	compiler_globals->compiled_filename = NULL;
2707f4909d9SThies C. Arntzen
2717a87fcbbSZeev Suraski	compiler_globals->function_table = (HashTable *) malloc(sizeof(HashTable));
2721263932aSZeev Suraski	zend_hash_init_ex(compiler_globals->function_table, 100, NULL, ZEND_FUNCTION_DTOR, 1, 0);
2737c4a0838SZeev Suraski	zend_hash_copy(compiler_globals->function_table, global_function_table, NULL, &tmp_func, sizeof(zend_function));
2747a87fcbbSZeev Suraski
2757a87fcbbSZeev Suraski	compiler_globals->class_table = (HashTable *) malloc(sizeof(HashTable));
2761263932aSZeev Suraski	zend_hash_init_ex(compiler_globals->class_table, 10, NULL, ZEND_CLASS_DTOR, 1, 0);
2779f51f154SZeev Suraski	zend_hash_copy(compiler_globals->class_table, global_class_table, (copy_ctor_func_t) zend_class_add_ref, &tmp_class, sizeof(zend_class_entry));
2789cf1a98dSThies C. Arntzen
2798b794bb1SZeev Suraski	zend_set_default_compile_time_values(CLS_C);
2807a87fcbbSZeev Suraski}
2817a87fcbbSZeev Suraski
2827a87fcbbSZeev Suraski
2837a87fcbbSZeev Suraskistatic void compiler_globals_dtor(zend_compiler_globals *compiler_globals)
2847a87fcbbSZeev Suraski{
28574a9ed7bSZeev Suraski	if (compiler_globals->function_table != global_function_table) {
28674a9ed7bSZeev Suraski		zend_hash_destroy(compiler_globals->function_table);
28774a9ed7bSZeev Suraski		free(compiler_globals->function_table);
28874a9ed7bSZeev Suraski	}
28974a9ed7bSZeev Suraski	if (compiler_globals->class_table != global_class_table) {
29074a9ed7bSZeev Suraski		zend_hash_destroy(compiler_globals->class_table);
29174a9ed7bSZeev Suraski		free(compiler_globals->class_table);
29274a9ed7bSZeev Suraski	}
2937a87fcbbSZeev Suraski}
294a9f9ae79SZeev Suraski
295a9f9ae79SZeev Suraski
296a9f9ae79SZeev Suraskistatic void executor_globals_ctor(zend_executor_globals *executor_globals)
297a9f9ae79SZeev Suraski{
298fec59d3bSAndi Gutmans	if (global_constants_table) {
2996313238cSZeev Suraski		zend_startup_constants(ELS_C);
300fec59d3bSAndi Gutmans		zend_copy_constants(executor_globals->zend_constants, global_constants_table);
301fec59d3bSAndi Gutmans	}
3024cb9a483SZeev Suraski	zend_init_rsrc_plist(ELS_C);
3030b7a9ceaSZeev Suraski	EG(lambda_count)=0;
304a9f9ae79SZeev Suraski}
305a9f9ae79SZeev Suraski
306a9f9ae79SZeev Suraski
307a9f9ae79SZeev Suraskistatic void executor_globals_dtor(zend_executor_globals *executor_globals)
308a9f9ae79SZeev Suraski{
309a9f9ae79SZeev Suraski	zend_shutdown_constants(ELS_C);
3104cb9a483SZeev Suraski	zend_destroy_rsrc_plist(ELS_C);
31154d53055SZeev Suraski	zend_ini_shutdown(ELS_C);
312a9f9ae79SZeev Suraski}
313a9f9ae79SZeev Suraski
314a9f9ae79SZeev Suraski
3157c4a0838SZeev Suraskistatic void alloc_globals_ctor(zend_alloc_globals *alloc_globals)
3167c4a0838SZeev Suraski{
31799947a25SAndi Gutmans	start_memory_manager(ALS_C);
3187c4a0838SZeev Suraski}
3197c4a0838SZeev Suraski
3207a87fcbbSZeev Suraski#endif
3217a87fcbbSZeev Suraski
3221f5ee72eSAndi Gutmans#ifdef __FreeBSD__
3235928970dSAndi Gutmans/* FreeBSD floating point precision fix */
3245928970dSAndi Gutmans#include <floatingpoint.h>
3255928970dSAndi Gutmans#endif
3267a87fcbbSZeev Suraski
32754d53055SZeev Suraski#ifdef ZTS
32854d53055SZeev Suraskistatic void zend_new_thread_end_handler(THREAD_T thread_id)
32954d53055SZeev Suraski{
33054d53055SZeev Suraski	ELS_FETCH();
33154d53055SZeev Suraski
33254d53055SZeev Suraski	zend_copy_ini_directives(ELS_C);
33354d53055SZeev Suraski	zend_ini_refresh_caches(ZEND_INI_STAGE_STARTUP ELS_CC);
33454d53055SZeev Suraski}
33554d53055SZeev Suraski#endif
33654d53055SZeev Suraski
33754d53055SZeev Suraski
33813e74131SAndi Gutmansint zend_startup(zend_utility_functions *utility_functions, char **extensions, int start_builtin_functions)
339573b4602SAndi Gutmans{
340a9f9ae79SZeev Suraski#ifdef ZTS
3417c4a0838SZeev Suraski	zend_compiler_globals *compiler_globals;
342a9f9ae79SZeev Suraski	zend_executor_globals *executor_globals;
343a9f9ae79SZeev Suraski
34470df2d74SSascha Schumann	alloc_globals_id = ts_allocate_id(sizeof(zend_alloc_globals), (ts_allocate_ctor) alloc_globals_ctor, NULL);
34599947a25SAndi Gutmans#else
34699947a25SAndi Gutmans	start_memory_manager(ALS_C);
347a9f9ae79SZeev Suraski#endif
348a9f9ae79SZeev Suraski
3491f5ee72eSAndi Gutmans#ifdef __FreeBSD__
350c3340584SSascha Schumann	/* FreeBSD floating point precision fix */
351c3340584SSascha Schumann	fpsetmask(0);
3525928970dSAndi Gutmans#endif
353309827ecSZeev Suraski
354309827ecSZeev Suraski	zend_startup_extensions_mechanism();
355309827ecSZeev Suraski
356573b4602SAndi Gutmans	/* Set up utility functions and values */
35767f69743SZeev Suraski	zend_error_cb = utility_functions->error_function;
358573b4602SAndi Gutmans	zend_printf = utility_functions->printf_function;
3595dba4774SZeev Suraski	zend_write = (zend_write_func_t) utility_functions->write_function;
360573b4602SAndi Gutmans	zend_fopen = utility_functions->fopen_function;
361573b4602SAndi Gutmans	if (!zend_fopen) {
362573b4602SAndi Gutmans		zend_fopen = zend_fopen_wrapper;
363573b4602SAndi Gutmans	}
36456251a72SZeev Suraski	zend_message_dispatcher_p = utility_functions->message_handler;
365573b4602SAndi Gutmans	zend_block_interruptions = utility_functions->block_interruptions;
366573b4602SAndi Gutmans	zend_unblock_interruptions = utility_functions->unblock_interruptions;
36756251a72SZeev Suraski	zend_get_ini_entry_p = utility_functions->get_ini_entry;
368fafbf6d8SZeev Suraski	zend_ticks_function = utility_functions->ticks_function;
369d035af4aSZeev Suraski
370c06692e9SZeev Suraski	zend_compile_file = compile_file;
371573b4602SAndi Gutmans	zend_execute = execute;
372573b4602SAndi Gutmans
373573b4602SAndi Gutmans	/* set up version */
374573b4602SAndi Gutmans	zend_version_info = strdup(ZEND_CORE_VERSION_INFO);
375573b4602SAndi Gutmans	zend_version_info_length = sizeof(ZEND_CORE_VERSION_INFO)-1;
376573b4602SAndi Gutmans
3777a87fcbbSZeev Suraski	GLOBAL_FUNCTION_TABLE = (HashTable *) malloc(sizeof(HashTable));
3787a87fcbbSZeev Suraski	GLOBAL_CLASS_TABLE = (HashTable *) malloc(sizeof(HashTable));
3791263932aSZeev Suraski	zend_hash_init_ex(GLOBAL_FUNCTION_TABLE, 100, NULL, ZEND_FUNCTION_DTOR, 1, 0);
3801263932aSZeev Suraski	zend_hash_init_ex(GLOBAL_CLASS_TABLE, 10, NULL, ZEND_CLASS_DTOR, 1, 0);
381573b4602SAndi Gutmans	register_standard_class();
3821263932aSZeev Suraski	zend_hash_init_ex(&module_registry, 50, NULL, ZEND_MODULE_DTOR, 1, 0);
3834cb9a483SZeev Suraski	zend_init_rsrc_list_dtors();
384573b4602SAndi Gutmans
385213d0e2fSAndi Gutmans	/* This zval can be used to initialize allocate zval's to an uninit'ed value */
386213d0e2fSAndi Gutmans	zval_used_for_init.is_ref = 0;
387213d0e2fSAndi Gutmans	zval_used_for_init.refcount = 1;
388bdefd5daSAndi Gutmans	zval_used_for_init.type = IS_NULL;
389213d0e2fSAndi Gutmans
3907a87fcbbSZeev Suraski#ifdef ZTS
391fec59d3bSAndi Gutmans	global_constants_table = NULL;
3927a87fcbbSZeev Suraski	compiler_globals_id = ts_allocate_id(sizeof(zend_compiler_globals), (void (*)(void *)) compiler_globals_ctor, (void (*)(void *)) compiler_globals_dtor);
393a9f9ae79SZeev Suraski	executor_globals_id = ts_allocate_id(sizeof(zend_executor_globals), (void (*)(void *)) executor_globals_ctor, (void (*)(void *)) executor_globals_dtor);
3947c4a0838SZeev Suraski	compiler_globals = ts_resource(compiler_globals_id);
395a9f9ae79SZeev Suraski	executor_globals = ts_resource(executor_globals_id);
39674a9ed7bSZeev Suraski	compiler_globals_dtor(compiler_globals);
3977c4a0838SZeev Suraski	compiler_globals->function_table = GLOBAL_FUNCTION_TABLE;
3987c4a0838SZeev Suraski	compiler_globals->class_table = GLOBAL_CLASS_TABLE;
3996313238cSZeev Suraski	zend_startup_constants(executor_globals);
400762158aaSAndi Gutmans	GLOBAL_CONSTANTS_TABLE = EG(zend_constants);
401762158aaSAndi Gutmans#else
4026313238cSZeev Suraski	zend_startup_constants();
4038b794bb1SZeev Suraski	zend_set_default_compile_time_values(CLS_C);
4047a87fcbbSZeev Suraski#endif
405762158aaSAndi Gutmans	zend_register_standard_constants(ELS_C);
4067a87fcbbSZeev Suraski
407bfbe8618SZeev Suraski#ifndef ZTS
4084cb9a483SZeev Suraski	zend_init_rsrc_plist(ELS_C);
409bfbe8618SZeev Suraski#endif
410a9f9ae79SZeev Suraski
41113e74131SAndi Gutmans	if (start_builtin_functions) {
41213e74131SAndi Gutmans		zend_startup_builtin_functions();
41313e74131SAndi Gutmans	}
41413d840bcSZeev Suraski
41554d53055SZeev Suraski	zend_ini_startup(ELS_C);
41654d53055SZeev Suraski
41754d53055SZeev Suraski#ifdef ZTS
41854d53055SZeev Suraski	tsrm_set_new_thread_end_handler(zend_new_thread_end_handler);
41954d53055SZeev Suraski#endif
42054d53055SZeev Suraski
421573b4602SAndi Gutmans	return SUCCESS;
422573b4602SAndi Gutmans}
423573b4602SAndi Gutmans
424573b4602SAndi Gutmans
425573b4602SAndi Gutmansvoid zend_shutdown()
426573b4602SAndi Gutmans{
42784eb48f0SZeev Suraski#ifdef ZEND_WIN32
42884eb48f0SZeev Suraski	zend_shutdown_timeout_thread();
42984eb48f0SZeev Suraski#endif
430bfbe8618SZeev Suraski#ifndef ZTS
431603d5ff5SZeev Suraski	zend_destroy_rsrc_plist();
432bfbe8618SZeev Suraski#endif
4334cb9a483SZeev Suraski	zend_destroy_rsrc_list_dtors();
434573b4602SAndi Gutmans	zend_hash_destroy(&module_registry);
4357a87fcbbSZeev Suraski	zend_hash_destroy(GLOBAL_FUNCTION_TABLE);
4367a87fcbbSZeev Suraski	free(GLOBAL_FUNCTION_TABLE);
4377a87fcbbSZeev Suraski	zend_hash_destroy(GLOBAL_CLASS_TABLE);
4387a87fcbbSZeev Suraski	free(GLOBAL_CLASS_TABLE);
439342c6e0bSZeev Suraski	zend_shutdown_extensions();
440573b4602SAndi Gutmans	free(zend_version_info);
4417a87fcbbSZeev Suraski#ifndef ZTS
442ee08b81aSZeev Suraski	zend_shutdown_constants();
4437a87fcbbSZeev Suraski#endif
444573b4602SAndi Gutmans}
445573b4602SAndi Gutmans
446573b4602SAndi Gutmans
4477656c77cSZeev Suraskivoid zend_set_utility_values(zend_utility_values *utility_values)
4487656c77cSZeev Suraski{
4497656c77cSZeev Suraski	zend_uv = *utility_values;
4509baad804SZeev Suraski	zend_uv.import_use_extension_length = strlen(zend_uv.import_use_extension);
4517656c77cSZeev Suraski}
4527656c77cSZeev Suraski
4537656c77cSZeev Suraski
454573b4602SAndi Gutmans/* this should be compatible with the standard zenderror */
455573b4602SAndi Gutmansvoid zenderror(char *error)
456573b4602SAndi Gutmans{
457573b4602SAndi Gutmans	zend_error(E_PARSE, error);
458573b4602SAndi Gutmans}
459573b4602SAndi Gutmans
460573b4602SAndi Gutmans
461b0bfa458SZeev SuraskiBEGIN_EXTERN_C()
462573b4602SAndi GutmansZEND_API void zend_bailout()
463573b4602SAndi Gutmans{
4646bc6dacfSZeev Suraski	CLS_FETCH();
465573b4602SAndi Gutmans	ELS_FETCH();
466573b4602SAndi Gutmans
4676bc6dacfSZeev Suraski	CG(unclean_shutdown) = 1;
468573b4602SAndi Gutmans	longjmp(EG(bailout), FAILURE);
469573b4602SAndi Gutmans}
470b0bfa458SZeev SuraskiEND_EXTERN_C()
471573b4602SAndi Gutmans
472573b4602SAndi Gutmans
473573b4602SAndi Gutmansvoid zend_append_version_info(zend_extension *extension)
474573b4602SAndi Gutmans{
475573b4602SAndi Gutmans	char *new_info;
476573b4602SAndi Gutmans	uint new_info_length;
477573b4602SAndi Gutmans
478573b4602SAndi Gutmans	new_info_length = sizeof("    with  v,  by \n")
479573b4602SAndi Gutmans						+ strlen(extension->name)
480573b4602SAndi Gutmans						+ strlen(extension->version)
481573b4602SAndi Gutmans						+ strlen(extension->copyright)
482573b4602SAndi Gutmans						+ strlen(extension->author);
483573b4602SAndi Gutmans
484573b4602SAndi Gutmans	new_info = (char *) malloc(new_info_length+1);
485573b4602SAndi Gutmans
486573b4602SAndi Gutmans	sprintf(new_info, "    with %s v%s, %s, by %s\n", extension->name, extension->version, extension->copyright, extension->author);
487573b4602SAndi Gutmans
488573b4602SAndi Gutmans	zend_version_info = (char *) realloc(zend_version_info, zend_version_info_length+new_info_length+1);
489573b4602SAndi Gutmans	strcat(zend_version_info, new_info);
490573b4602SAndi Gutmans	zend_version_info_length += new_info_length;
491573b4602SAndi Gutmans	free(new_info);
492573b4602SAndi Gutmans}
493573b4602SAndi Gutmans
494573b4602SAndi Gutmans
495573b4602SAndi GutmansZEND_API char *get_zend_version()
496573b4602SAndi Gutmans{
497573b4602SAndi Gutmans	return zend_version_info;
498573b4602SAndi Gutmans}
499ed06a70fSZeev Suraski
500ed06a70fSZeev Suraski
501ed06a70fSZeev Suraskivoid zend_activate(CLS_D ELS_DC)
502ed06a70fSZeev Suraski{
503ed06a70fSZeev Suraski	init_compiler(CLS_C ELS_CC);
504ed06a70fSZeev Suraski	init_executor(CLS_C ELS_CC);
505ed06a70fSZeev Suraski	startup_scanner(CLS_C);
506ed06a70fSZeev Suraski}
507ed06a70fSZeev Suraski
508dc0e6632SZeev Suraski
509dc0e6632SZeev Suraskivoid zend_activate_modules()
510dc0e6632SZeev Suraski{
511dc0e6632SZeev Suraski	zend_hash_apply(&module_registry, (int (*)(void *)) module_registry_request_startup);
512dc0e6632SZeev Suraski}
513dc0e6632SZeev Suraski
514a8b0d960SAndi Gutmansvoid zend_deactivate_modules()
515a8b0d960SAndi Gutmans{
516c5cf13ebSSam Ruby	ELS_FETCH();
517a8b0d960SAndi Gutmans	EG(opline_ptr) = NULL; /* we're no longer executing anything */
518a8b0d960SAndi Gutmans
51999af3ed8SZeev Suraski	if (setjmp(EG(bailout))==0) {
52099af3ed8SZeev Suraski		zend_hash_apply(&module_registry, (int (*)(void *)) module_registry_cleanup);
52199af3ed8SZeev Suraski	}
522a8b0d960SAndi Gutmans}
523dc0e6632SZeev Suraski
524ed06a70fSZeev Suraskivoid zend_deactivate(CLS_D ELS_DC)
525ed06a70fSZeev Suraski{
526372f71ebSThies C. Arntzen	/* we're no longer executing anything */
527372f71ebSThies C. Arntzen	EG(opline_ptr) = NULL;
528372f71ebSThies C. Arntzen	EG(active_symbol_table) = NULL;
529f4c9d96fSThies C. Arntzen
53099af3ed8SZeev Suraski	if (setjmp(EG(bailout))==0) {
53199af3ed8SZeev Suraski		shutdown_scanner(CLS_C);
53299af3ed8SZeev Suraski	}
53399af3ed8SZeev Suraski	if (setjmp(EG(bailout))==0) {
53499af3ed8SZeev Suraski		shutdown_executor(ELS_C);
53599af3ed8SZeev Suraski	}
53699af3ed8SZeev Suraski	if (setjmp(EG(bailout))==0) {
53799af3ed8SZeev Suraski		shutdown_compiler(CLS_C);
53899af3ed8SZeev Suraski	}
53954d53055SZeev Suraski	if (setjmp(EG(bailout))==0) {
54054d53055SZeev Suraski		zend_ini_deactivate(ELS_C);
54154d53055SZeev Suraski	}
542ed06a70fSZeev Suraski}
54356251a72SZeev Suraski
54456251a72SZeev Suraski
545fc173b6aSSam RubyBEGIN_EXTERN_C()
54656251a72SZeev SuraskiZEND_API void zend_message_dispatcher(long message, void *data)
54756251a72SZeev Suraski{
54856251a72SZeev Suraski	if (zend_message_dispatcher_p) {
54956251a72SZeev Suraski		zend_message_dispatcher_p(message, data);
55056251a72SZeev Suraski	}
55156251a72SZeev Suraski}
552fc173b6aSSam RubyEND_EXTERN_C()
55356251a72SZeev Suraski
55456251a72SZeev Suraski
55556251a72SZeev SuraskiZEND_API int zend_get_ini_entry(char *name, uint name_length, zval *contents)
55656251a72SZeev Suraski{
55756251a72SZeev Suraski	if (zend_get_ini_entry_p) {
55856251a72SZeev Suraski		return zend_get_ini_entry_p(name, name_length, contents);
55956251a72SZeev Suraski	} else {
56056251a72SZeev Suraski		return FAILURE;
56156251a72SZeev Suraski	}
56256251a72SZeev Suraski}
56367f69743SZeev Suraski
56467f69743SZeev Suraski
56567f69743SZeev Suraski#define ZEND_ERROR_BUFFER_SIZE 1024
56667f69743SZeev Suraski
56767f69743SZeev SuraskiZEND_API void zend_error(int type, const char *format, ...)
56867f69743SZeev Suraski{
56967f69743SZeev Suraski	va_list args;
5703cda6a75SZeev Suraski	zval ***params;
5713cda6a75SZeev Suraski	zval *retval;
572f34b9d41SZeev Suraski	zval *z_error_type, *z_error_message, *z_error_filename, *z_error_lineno, *z_context;
573b80b8381SZeev Suraski	char *error_filename;
574b80b8381SZeev Suraski	uint error_lineno;
575a31dc47aSZeev Suraski	zval *orig_user_error_handler;
57667f69743SZeev Suraski	ELS_FETCH();
57767f69743SZeev Suraski	CLS_FETCH();
57867f69743SZeev Suraski
579b80b8381SZeev Suraski	/* Obtain relevant filename and lineno */
580b80b8381SZeev Suraski	switch (type) {
581b80b8381SZeev Suraski		case E_CORE_ERROR:
582b80b8381SZeev Suraski		case E_CORE_WARNING:
583b80b8381SZeev Suraski			error_filename = NULL;
584b80b8381SZeev Suraski			error_lineno = 0;
585b80b8381SZeev Suraski			break;
586b80b8381SZeev Suraski		case E_PARSE:
587b80b8381SZeev Suraski		case E_COMPILE_ERROR:
588b80b8381SZeev Suraski		case E_COMPILE_WARNING:
589b80b8381SZeev Suraski		case E_ERROR:
590b80b8381SZeev Suraski		case E_NOTICE:
591b80b8381SZeev Suraski		case E_WARNING:
592b80b8381SZeev Suraski		case E_USER_ERROR:
593b80b8381SZeev Suraski		case E_USER_WARNING:
594b80b8381SZeev Suraski		case E_USER_NOTICE:
595b80b8381SZeev Suraski			if (zend_is_compiling()) {
596b80b8381SZeev Suraski				error_filename = zend_get_compiled_filename(CLS_C);
597b80b8381SZeev Suraski				error_lineno = zend_get_compiled_lineno(CLS_C);
598b80b8381SZeev Suraski			} else if (zend_is_executing()) {
599b80b8381SZeev Suraski				error_filename = zend_get_executed_filename(ELS_C);
600b80b8381SZeev Suraski				error_lineno = zend_get_executed_lineno(ELS_C);
601b80b8381SZeev Suraski			} else {
602b80b8381SZeev Suraski				error_filename = NULL;
603b80b8381SZeev Suraski				error_lineno = 0;
604b80b8381SZeev Suraski			}
605b80b8381SZeev Suraski			break;
606b80b8381SZeev Suraski		default:
607b80b8381SZeev Suraski			error_filename = NULL;
608b80b8381SZeev Suraski			error_lineno = 0;
609b80b8381SZeev Suraski			break;
610b80b8381SZeev Suraski	}
611b80b8381SZeev Suraski	if (!error_filename) {
612b80b8381SZeev Suraski		error_filename = "Unknown";
613b80b8381SZeev Suraski	}
61467f69743SZeev Suraski
61567f69743SZeev Suraski
616b80b8381SZeev Suraski	va_start(args, format);
6178a8c3eafSZeev Suraski
61867f69743SZeev Suraski	/* if we don't have a user defined error handler */
61967f69743SZeev Suraski	if (!EG(user_error_handler)) {
620b80b8381SZeev Suraski		zend_error_cb(type, error_filename, error_lineno, format, args);
621b80b8381SZeev Suraski	} else switch (type) {
62267f69743SZeev Suraski		case E_ERROR:
62367f69743SZeev Suraski		case E_PARSE:
62467f69743SZeev Suraski		case E_CORE_ERROR:
62567f69743SZeev Suraski		case E_CORE_WARNING:
62667f69743SZeev Suraski		case E_COMPILE_ERROR:
62767f69743SZeev Suraski		case E_COMPILE_WARNING:
628b80b8381SZeev Suraski			/* The error may not be safe to handle in user-space */
629b80b8381SZeev Suraski			zend_error_cb(type, error_filename, error_lineno, format, args);
630b80b8381SZeev Suraski			break;
631b80b8381SZeev Suraski		default:
632b80b8381SZeev Suraski			/* Handle the error in user space */
633f34b9d41SZeev Suraski			ALLOC_INIT_ZVAL(z_error_message);
634f34b9d41SZeev Suraski			ALLOC_INIT_ZVAL(z_error_type);
635a31dc47aSZeev Suraski			ALLOC_INIT_ZVAL(z_error_filename);
636a31dc47aSZeev Suraski			ALLOC_INIT_ZVAL(z_error_lineno);
637f34b9d41SZeev Suraski			ALLOC_INIT_ZVAL(z_context);
638f34b9d41SZeev Suraski			z_error_message->value.str.val = (char *) emalloc(ZEND_ERROR_BUFFER_SIZE);
63967f69743SZeev Suraski
6408a8c3eafSZeev Suraski#ifdef HAVE_VSNPRINTF
641f34b9d41SZeev Suraski			z_error_message->value.str.len = vsnprintf(z_error_message->value.str.val, ZEND_ERROR_BUFFER_SIZE, format, args);
6424acff8f3SZeev Suraski			if (z_error_message->value.str.len > ZEND_ERROR_BUFFER_SIZE-1) {
643d10336ecSStanislav Malyshev				z_error_message->value.str.len = ZEND_ERROR_BUFFER_SIZE-1;
644d10336ecSStanislav Malyshev			}
6458a8c3eafSZeev Suraski#else
6464acff8f3SZeev Suraski			strncpy(z_error_message->value.str.val, format, ZEND_ERROR_BUFFER_SIZE);
6478a8c3eafSZeev Suraski			/* This is risky... */
6484acff8f3SZeev Suraski			/* z_error_message->value.str.len = vsprintf(z_error_message->value.str.val, format, args); */
6498a8c3eafSZeev Suraski#endif
650f34b9d41SZeev Suraski			z_error_message->type = IS_STRING;
65167f69743SZeev Suraski
652f34b9d41SZeev Suraski			z_error_type->value.lval = type;
653f34b9d41SZeev Suraski			z_error_type->type = IS_LONG;
65467f69743SZeev Suraski
655a31dc47aSZeev Suraski			if (error_filename) {
656a31dc47aSZeev Suraski				z_error_filename->value.str.len = strlen(error_filename);
657a31dc47aSZeev Suraski				z_error_filename->value.str.val = estrndup(error_filename, z_error_filename->value.str.len);
658a31dc47aSZeev Suraski				z_error_filename->type = IS_STRING;
659a31dc47aSZeev Suraski			}
660a31dc47aSZeev Suraski
661a31dc47aSZeev Suraski			z_error_lineno->value.lval = error_lineno;
662a31dc47aSZeev Suraski			z_error_lineno->type = IS_LONG;
663a31dc47aSZeev Suraski
664f34b9d41SZeev Suraski			z_context->value.ht = EG(active_symbol_table);
665f34b9d41SZeev Suraski			z_context->type = IS_ARRAY;
666f34b9d41SZeev Suraski			ZVAL_ADDREF(z_context);  /* we don't want this one to be freed */
667f34b9d41SZeev Suraski
668f34b9d41SZeev Suraski			params = (zval ***) emalloc(sizeof(zval **)*5);
669f34b9d41SZeev Suraski			params[0] = &z_error_type;
670f34b9d41SZeev Suraski			params[1] = &z_error_message;
671a31dc47aSZeev Suraski			params[2] = &z_error_filename;
672a31dc47aSZeev Suraski			params[3] = &z_error_lineno;
673f34b9d41SZeev Suraski			params[4] = &z_context;
67467f69743SZeev Suraski
675a31dc47aSZeev Suraski			orig_user_error_handler = EG(user_error_handler);
676a31dc47aSZeev Suraski			EG(user_error_handler) = NULL;
677f34b9d41SZeev Suraski			if (call_user_function_ex(CG(function_table), NULL, orig_user_error_handler, &retval, 5, params, 1, NULL)==SUCCESS) {
6783cda6a75SZeev Suraski				zval_ptr_dtor(&retval);
679b80b8381SZeev Suraski			} else {
680b80b8381SZeev Suraski				/* The user error handler failed, use built-in error handler */
681b80b8381SZeev Suraski				zend_error_cb(type, error_filename, error_lineno, format, args);
682b80b8381SZeev Suraski			}
683a31dc47aSZeev Suraski			EG(user_error_handler) = orig_user_error_handler;
684a31dc47aSZeev Suraski
685b80b8381SZeev Suraski			efree(params);
686f34b9d41SZeev Suraski			zval_ptr_dtor(&z_error_message);
687f34b9d41SZeev Suraski			zval_ptr_dtor(&z_error_type);
688a31dc47aSZeev Suraski			zval_ptr_dtor(&z_error_filename);
689a31dc47aSZeev Suraski			zval_ptr_dtor(&z_error_lineno);
690f34b9d41SZeev Suraski			if (ZVAL_REFCOUNT(z_context)==2) {
691f34b9d41SZeev Suraski				FREE_ZVAL(z_context);
692f34b9d41SZeev Suraski			}
693b80b8381SZeev Suraski			break;
69467f69743SZeev Suraski	}
695b80b8381SZeev Suraski
696b80b8381SZeev Suraski	va_end(args);
69767f69743SZeev Suraski}
69867f69743SZeev Suraski
6990b7a9ceaSZeev Suraski
7000b7a9ceaSZeev SuraskiZEND_API void zend_output_debug_string(zend_bool trigger_break, char *format, ...)
7010b7a9ceaSZeev Suraski{
7020b7a9ceaSZeev Suraski#if ZEND_DEBUG
7030b7a9ceaSZeev Suraski	va_list args;
7040b7a9ceaSZeev Suraski
7050b7a9ceaSZeev Suraski	va_start(args, format);
70601d478baSAndi Gutmans#	ifdef ZEND_WIN32
7070b7a9ceaSZeev Suraski	{
7080b7a9ceaSZeev Suraski		char output_buf[1024];
7090b7a9ceaSZeev Suraski
7100b7a9ceaSZeev Suraski		vsnprintf(output_buf, 1024, format, args);
7110b7a9ceaSZeev Suraski		OutputDebugString(output_buf);
7120b7a9ceaSZeev Suraski		OutputDebugString("\n");
7130b7a9ceaSZeev Suraski		if (trigger_break && IsDebuggerPresent()) {
7140b7a9ceaSZeev Suraski			DebugBreak();
7150b7a9ceaSZeev Suraski		}
7160b7a9ceaSZeev Suraski	}
7170b7a9ceaSZeev Suraski#	else
7180b7a9ceaSZeev Suraski	vfprintf(stderr, format, args);
7190b7a9ceaSZeev Suraski	fprintf(stderr, "\n");
7200b7a9ceaSZeev Suraski#	endif
7210b7a9ceaSZeev Suraski	va_end(args);
7220b7a9ceaSZeev Suraski#endif
7230b7a9ceaSZeev Suraski}
724c06692e9SZeev Suraski
725c06692e9SZeev Suraski
726c06692e9SZeev SuraskiZEND_API int zend_execute_scripts(int type CLS_DC ELS_DC, int file_count, ...)
727c06692e9SZeev Suraski{
728c06692e9SZeev Suraski	va_list files;
729c06692e9SZeev Suraski	int i;
730c06692e9SZeev Suraski	zend_file_handle *file_handle;
731c06692e9SZeev Suraski
732c06692e9SZeev Suraski	va_start(files, file_count);
733c06692e9SZeev Suraski	for (i=0; i<file_count; i++) {
734c06692e9SZeev Suraski		file_handle = va_arg(files, zend_file_handle *);
735c06692e9SZeev Suraski		if (!file_handle) {
736c06692e9SZeev Suraski			continue;
737c06692e9SZeev Suraski		}
7387795aca4SZeev Suraski		EG(active_op_array) = zend_compile_file(file_handle, ZEND_INCLUDE CLS_CC);
7395090b1e8SStanislav Malyshev		zend_destroy_file_handle(file_handle CLS_CC);
740c06692e9SZeev Suraski		if (EG(active_op_array)) {
741c06692e9SZeev Suraski			zend_execute(EG(active_op_array) ELS_CC);
742c06692e9SZeev Suraski			zval_ptr_dtor(EG(return_value_ptr_ptr));
743c06692e9SZeev Suraski			EG(return_value_ptr_ptr) = &EG(global_return_value_ptr);
744c06692e9SZeev Suraski			EG(global_return_value_ptr) = NULL;
745c06692e9SZeev Suraski			destroy_op_array(EG(active_op_array));
746c06692e9SZeev Suraski			efree(EG(active_op_array));
747c06692e9SZeev Suraski		} else if (type==ZEND_REQUIRE) {
748c06692e9SZeev Suraski			va_end(files);
749c06692e9SZeev Suraski			return FAILURE;
750c06692e9SZeev Suraski		}
751c06692e9SZeev Suraski	}
752c06692e9SZeev Suraski	va_end(files);
753c06692e9SZeev Suraski
754c06692e9SZeev Suraski	return SUCCESS;
755c06692e9SZeev Suraski}
756c06692e9SZeev Suraski
7575286b397SZeev Suraski#define COMPILED_STRING_DESCRIPTION_FORMAT "%s(%d) : %s"
7585286b397SZeev Suraski
7595286b397SZeev SuraskiZEND_API char *zend_make_compiled_string_description(char *name)
7605286b397SZeev Suraski{
7615286b397SZeev Suraski	char *cur_filename;
7625286b397SZeev Suraski	int cur_lineno;
7635286b397SZeev Suraski	char *compiled_string_description;
7645286b397SZeev Suraski	CLS_FETCH();
7655286b397SZeev Suraski	ELS_FETCH();
7665286b397SZeev Suraski
7675286b397SZeev Suraski	if (zend_is_compiling()) {
7685286b397SZeev Suraski		cur_filename = zend_get_compiled_filename(CLS_C);
7695286b397SZeev Suraski		cur_lineno = zend_get_compiled_lineno(CLS_C);
7705286b397SZeev Suraski	} else if (zend_is_executing()) {
7715286b397SZeev Suraski		cur_filename = zend_get_executed_filename(ELS_C);
7725286b397SZeev Suraski		cur_lineno = zend_get_executed_lineno(ELS_C);
7735286b397SZeev Suraski	} else {
7745286b397SZeev Suraski		cur_filename = "Unknown";
7755286b397SZeev Suraski		cur_lineno = 0;
7765286b397SZeev Suraski	}
7775286b397SZeev Suraski
7785286b397SZeev Suraski	compiled_string_description = emalloc(sizeof(COMPILED_STRING_DESCRIPTION_FORMAT)+strlen(name)+strlen(cur_filename)+MAX_LENGTH_OF_LONG);
7795286b397SZeev Suraski	sprintf(compiled_string_description, COMPILED_STRING_DESCRIPTION_FORMAT, cur_filename, cur_lineno, name);
7805286b397SZeev Suraski	return compiled_string_description;
7815286b397SZeev Suraski}
7825286b397SZeev Suraski
783