1/*
2   +----------------------------------------------------------------------+
3   | Zend Engine                                                          |
4   +----------------------------------------------------------------------+
5   | Copyright (c) 1998-2015 Zend Technologies Ltd. (http://www.zend.com) |
6   +----------------------------------------------------------------------+
7   | This source file is subject to version 2.00 of the Zend license,     |
8   | that is bundled with this package in the file LICENSE, and is        |
9   | available through the world-wide-web at the following url:           |
10   | http://www.zend.com/license/2_00.txt.                                |
11   | If you did not receive a copy of the Zend license and are unable to  |
12   | obtain it through the world-wide-web, please send a note to          |
13   | license@zend.com so we can mail you a copy immediately.              |
14   +----------------------------------------------------------------------+
15   | Authors: Andi Gutmans <andi@zend.com>                                |
16   |          Zeev Suraski <zeev@zend.com>                                |
17   +----------------------------------------------------------------------+
18*/
19
20/* $Id$ */
21
22#include "zend.h"
23#include "zend_extensions.h"
24#include "zend_modules.h"
25#include "zend_constants.h"
26#include "zend_list.h"
27#include "zend_API.h"
28#include "zend_exceptions.h"
29#include "zend_builtin_functions.h"
30#include "zend_ini.h"
31#include "zend_vm.h"
32#include "zend_dtrace.h"
33#include "zend_virtual_cwd.h"
34
35#ifdef ZTS
36# define GLOBAL_FUNCTION_TABLE      global_function_table
37# define GLOBAL_CLASS_TABLE         global_class_table
38# define GLOBAL_CONSTANTS_TABLE     global_constants_table
39# define GLOBAL_AUTO_GLOBALS_TABLE  global_auto_globals_table
40#else
41# define GLOBAL_FUNCTION_TABLE      CG(function_table)
42# define GLOBAL_CLASS_TABLE         CG(class_table)
43# define GLOBAL_AUTO_GLOBALS_TABLE  CG(auto_globals)
44# define GLOBAL_CONSTANTS_TABLE     EG(zend_constants)
45#endif
46
47/* true multithread-shared globals */
48ZEND_API zend_class_entry *zend_standard_class_def = NULL;
49ZEND_API size_t (*zend_printf)(const char *format, ...);
50ZEND_API zend_write_func_t zend_write;
51ZEND_API FILE *(*zend_fopen)(const char *filename, char **opened_path);
52ZEND_API int (*zend_stream_open_function)(const char *filename, zend_file_handle *handle);
53ZEND_API void (*zend_block_interruptions)(void);
54ZEND_API void (*zend_unblock_interruptions)(void);
55ZEND_API void (*zend_ticks_function)(int ticks);
56ZEND_API void (*zend_error_cb)(int type, const char *error_filename, const uint error_lineno, const char *format, va_list args);
57size_t (*zend_vspprintf)(char **pbuf, size_t max_len, const char *format, va_list ap);
58zend_string *(*zend_vstrpprintf)(size_t max_len, const char *format, va_list ap);
59ZEND_API char *(*zend_getenv)(char *name, size_t name_len);
60ZEND_API char *(*zend_resolve_path)(const char *filename, int filename_len);
61
62void (*zend_on_timeout)(int seconds);
63
64static void (*zend_message_dispatcher_p)(zend_long message, const void *data);
65static zval *(*zend_get_configuration_directive_p)(zend_string *name);
66
67static ZEND_INI_MH(OnUpdateErrorReporting) /* {{{ */
68{
69    if (!new_value) {
70        EG(error_reporting) = E_ALL & ~E_NOTICE & ~E_STRICT & ~E_DEPRECATED;
71    } else {
72        EG(error_reporting) = atoi(new_value->val);
73    }
74    return SUCCESS;
75}
76/* }}} */
77
78static ZEND_INI_MH(OnUpdateGCEnabled) /* {{{ */
79{
80    OnUpdateBool(entry, new_value, mh_arg1, mh_arg2, mh_arg3, stage);
81
82    if (GC_G(gc_enabled)) {
83        gc_init();
84    }
85
86    return SUCCESS;
87}
88/* }}} */
89
90static ZEND_INI_MH(OnUpdateScriptEncoding) /* {{{ */
91{
92    if (!CG(multibyte)) {
93        return FAILURE;
94    }
95    if (!zend_multibyte_get_functions()) {
96        return SUCCESS;
97    }
98    return zend_multibyte_set_script_encoding_by_string(new_value ? new_value->val : NULL, new_value ? new_value->len : 0);
99}
100/* }}} */
101
102
103ZEND_INI_BEGIN()
104    ZEND_INI_ENTRY("error_reporting",               NULL,       ZEND_INI_ALL,       OnUpdateErrorReporting)
105    STD_ZEND_INI_BOOLEAN("zend.enable_gc",              "1",    ZEND_INI_ALL,       OnUpdateGCEnabled,      gc_enabled,     zend_gc_globals,        gc_globals)
106    STD_ZEND_INI_BOOLEAN("zend.multibyte", "0", ZEND_INI_PERDIR, OnUpdateBool, multibyte,      zend_compiler_globals, compiler_globals)
107    ZEND_INI_ENTRY("zend.script_encoding",          NULL,       ZEND_INI_ALL,       OnUpdateScriptEncoding)
108    STD_ZEND_INI_BOOLEAN("zend.detect_unicode",         "1",    ZEND_INI_ALL,       OnUpdateBool, detect_unicode, zend_compiler_globals, compiler_globals)
109#ifdef ZEND_SIGNALS
110    STD_ZEND_INI_BOOLEAN("zend.signal_check", "0", ZEND_INI_SYSTEM, OnUpdateBool, check, zend_signal_globals_t, zend_signal_globals)
111#endif
112ZEND_INI_END()
113
114
115#ifdef ZTS
116ZEND_API int compiler_globals_id;
117ZEND_API int executor_globals_id;
118static HashTable *global_function_table = NULL;
119static HashTable *global_class_table = NULL;
120static HashTable *global_constants_table = NULL;
121static HashTable *global_auto_globals_table = NULL;
122static HashTable *global_persistent_list = NULL;
123ZEND_TSRMLS_CACHE_DEFINE;
124#endif
125
126ZEND_API zend_utility_values zend_uv;
127
128/* version information */
129static char *zend_version_info;
130static uint zend_version_info_length;
131#define ZEND_CORE_VERSION_INFO  "Zend Engine v" ZEND_VERSION ", Copyright (c) 1998-2015 Zend Technologies\n"
132#define PRINT_ZVAL_INDENT 4
133
134static void print_hash(zend_write_func_t write_func, HashTable *ht, int indent, zend_bool is_object) /* {{{ */
135{
136    zval *tmp;
137    zend_string *string_key;
138    zend_ulong num_key;
139    int i;
140
141    for (i = 0; i < indent; i++) {
142        ZEND_PUTS_EX(" ");
143    }
144    ZEND_PUTS_EX("(\n");
145    indent += PRINT_ZVAL_INDENT;
146    ZEND_HASH_FOREACH_KEY_VAL(ht, num_key, string_key, tmp) {
147        if (Z_TYPE_P(tmp) == IS_INDIRECT) {
148            tmp = Z_INDIRECT_P(tmp);
149            if (Z_TYPE_P(tmp) == IS_UNDEF) {
150                continue;
151            }
152        }
153        for (i = 0; i < indent; i++) {
154            ZEND_PUTS_EX(" ");
155        }
156        ZEND_PUTS_EX("[");
157        if (string_key) {
158            if (is_object) {
159                const char *prop_name, *class_name;
160                size_t prop_len;
161                int mangled = zend_unmangle_property_name_ex(string_key, &class_name, &prop_name, &prop_len);
162
163                ZEND_WRITE_EX(prop_name, prop_len);
164                if (class_name && mangled == SUCCESS) {
165                    if (class_name[0]=='*') {
166                        ZEND_PUTS_EX(":protected");
167                    } else {
168                        ZEND_PUTS_EX(":");
169                        ZEND_PUTS_EX(class_name);
170                        ZEND_PUTS_EX(":private");
171                    }
172                }
173            } else {
174                ZEND_WRITE_EX(string_key->val, string_key->len);
175            }
176        } else {
177            char key[25];
178            snprintf(key, sizeof(key), ZEND_LONG_FMT, num_key);
179            ZEND_PUTS_EX(key);
180        }
181        ZEND_PUTS_EX("] => ");
182        zend_print_zval_r_ex(write_func, tmp, indent+PRINT_ZVAL_INDENT);
183        ZEND_PUTS_EX("\n");
184    } ZEND_HASH_FOREACH_END();
185    indent -= PRINT_ZVAL_INDENT;
186    for (i = 0; i < indent; i++) {
187        ZEND_PUTS_EX(" ");
188    }
189    ZEND_PUTS_EX(")\n");
190}
191/* }}} */
192
193static void print_flat_hash(HashTable *ht) /* {{{ */
194{
195    zval *tmp;
196    zend_string *string_key;
197    zend_ulong num_key;
198    int i = 0;
199
200    ZEND_HASH_FOREACH_KEY_VAL_IND(ht, num_key, string_key, tmp) {
201        if (i++ > 0) {
202            ZEND_PUTS(",");
203        }
204        ZEND_PUTS("[");
205        if (string_key) {
206            ZEND_WRITE(string_key->val, string_key->len);
207        } else {
208            zend_printf(ZEND_ULONG_FMT, num_key);
209        }
210        ZEND_PUTS("] => ");
211        zend_print_flat_zval_r(tmp);
212    } ZEND_HASH_FOREACH_END();
213}
214/* }}} */
215
216ZEND_API int zend_make_printable_zval(zval *expr, zval *expr_copy) /* {{{ */
217{
218    if (Z_TYPE_P(expr) == IS_STRING) {
219        return 0;
220    } else {
221        ZVAL_STR(expr_copy, _zval_get_string_func(expr));
222        return 1;
223    }
224}
225/* }}} */
226
227ZEND_API size_t zend_print_zval(zval *expr, int indent) /* {{{ */
228{
229    return zend_print_zval_ex(zend_write, expr, indent);
230}
231/* }}} */
232
233ZEND_API size_t zend_print_zval_ex(zend_write_func_t write_func, zval *expr, int indent) /* {{{ */
234{
235    zend_string *str = zval_get_string(expr);
236    size_t len = str->len;
237
238    if (len != 0) {
239        write_func(str->val, len);
240    }
241
242    zend_string_release(str);
243    return len;
244}
245/* }}} */
246
247ZEND_API void zend_print_flat_zval_r(zval *expr) /* {{{ */
248{
249    switch (Z_TYPE_P(expr)) {
250        case IS_ARRAY:
251            ZEND_PUTS("Array (");
252            if (ZEND_HASH_APPLY_PROTECTION(Z_ARRVAL_P(expr)) &&
253                ++Z_ARRVAL_P(expr)->u.v.nApplyCount>1) {
254                ZEND_PUTS(" *RECURSION*");
255                Z_ARRVAL_P(expr)->u.v.nApplyCount--;
256                return;
257            }
258            print_flat_hash(Z_ARRVAL_P(expr));
259            ZEND_PUTS(")");
260            if (ZEND_HASH_APPLY_PROTECTION(Z_ARRVAL_P(expr))) {
261                Z_ARRVAL_P(expr)->u.v.nApplyCount--;
262            }
263            break;
264        case IS_OBJECT:
265        {
266            HashTable *properties = NULL;
267            zend_string *class_name = Z_OBJ_HANDLER_P(expr, get_class_name)(Z_OBJ_P(expr));
268            zend_printf("%s Object (", class_name->val);
269            zend_string_release(class_name);
270
271            if (Z_OBJ_HANDLER_P(expr, get_properties)) {
272                properties = Z_OBJPROP_P(expr);
273            }
274            if (properties) {
275                if (++properties->u.v.nApplyCount>1) {
276                    ZEND_PUTS(" *RECURSION*");
277                    properties->u.v.nApplyCount--;
278                    return;
279                }
280                print_flat_hash(properties);
281                properties->u.v.nApplyCount--;
282            }
283            ZEND_PUTS(")");
284            break;
285        }
286        default:
287            zend_print_variable(expr);
288            break;
289    }
290}
291/* }}} */
292
293ZEND_API void zend_print_zval_r(zval *expr, int indent) /* {{{ */
294{
295    zend_print_zval_r_ex(zend_write, expr, indent);
296}
297/* }}} */
298
299ZEND_API void zend_print_zval_r_ex(zend_write_func_t write_func, zval *expr, int indent) /* {{{ */
300{
301    ZVAL_DEREF(expr);
302    switch (Z_TYPE_P(expr)) {
303        case IS_ARRAY:
304            ZEND_PUTS_EX("Array\n");
305            if (ZEND_HASH_APPLY_PROTECTION(Z_ARRVAL_P(expr)) &&
306                ++Z_ARRVAL_P(expr)->u.v.nApplyCount>1) {
307                ZEND_PUTS_EX(" *RECURSION*");
308                Z_ARRVAL_P(expr)->u.v.nApplyCount--;
309                return;
310            }
311            print_hash(write_func, Z_ARRVAL_P(expr), indent, 0);
312            if (ZEND_HASH_APPLY_PROTECTION(Z_ARRVAL_P(expr))) {
313                Z_ARRVAL_P(expr)->u.v.nApplyCount--;
314            }
315            break;
316        case IS_OBJECT:
317            {
318                HashTable *properties;
319                int is_temp;
320
321                zend_string *class_name = Z_OBJ_HANDLER_P(expr, get_class_name)(Z_OBJ_P(expr));
322                ZEND_PUTS_EX(class_name->val);
323                zend_string_release(class_name);
324
325                ZEND_PUTS_EX(" Object\n");
326                if ((properties = Z_OBJDEBUG_P(expr, is_temp)) == NULL) {
327                    break;
328                }
329                if (++properties->u.v.nApplyCount>1) {
330                    ZEND_PUTS_EX(" *RECURSION*");
331                    properties->u.v.nApplyCount--;
332                    return;
333                }
334                print_hash(write_func, properties, indent, 1);
335                properties->u.v.nApplyCount--;
336                if (is_temp) {
337                    zend_hash_destroy(properties);
338                    FREE_HASHTABLE(properties);
339                }
340                break;
341            }
342        default:
343            zend_print_zval_ex(write_func, expr, indent);
344            break;
345    }
346}
347/* }}} */
348
349static FILE *zend_fopen_wrapper(const char *filename, char **opened_path) /* {{{ */
350{
351    if (opened_path) {
352        *opened_path = estrdup(filename);
353    }
354    return fopen(filename, "rb");
355}
356/* }}} */
357
358#ifdef ZTS
359static zend_bool short_tags_default      = 1;
360static uint32_t compiler_options_default = ZEND_COMPILE_DEFAULT;
361#else
362# define short_tags_default         1
363# define compiler_options_default   ZEND_COMPILE_DEFAULT
364#endif
365
366static void zend_set_default_compile_time_values(void) /* {{{ */
367{
368    /* default compile-time values */
369    CG(short_tags) = short_tags_default;
370    CG(compiler_options) = compiler_options_default;
371}
372/* }}} */
373
374static void zend_init_exception_op(void) /* {{{ */
375{
376    memset(EG(exception_op), 0, sizeof(EG(exception_op)));
377    EG(exception_op)[0].opcode = ZEND_HANDLE_EXCEPTION;
378    EG(exception_op)[0].op1_type = IS_UNUSED;
379    EG(exception_op)[0].op2_type = IS_UNUSED;
380    EG(exception_op)[0].result_type = IS_UNUSED;
381    ZEND_VM_SET_OPCODE_HANDLER(EG(exception_op));
382    EG(exception_op)[1].opcode = ZEND_HANDLE_EXCEPTION;
383    EG(exception_op)[1].op1_type = IS_UNUSED;
384    EG(exception_op)[1].op2_type = IS_UNUSED;
385    EG(exception_op)[1].result_type = IS_UNUSED;
386    ZEND_VM_SET_OPCODE_HANDLER(EG(exception_op)+1);
387    EG(exception_op)[2].opcode = ZEND_HANDLE_EXCEPTION;
388    EG(exception_op)[2].op1_type = IS_UNUSED;
389    EG(exception_op)[2].op2_type = IS_UNUSED;
390    EG(exception_op)[2].result_type = IS_UNUSED;
391    ZEND_VM_SET_OPCODE_HANDLER(EG(exception_op)+2);
392}
393/* }}} */
394
395#ifdef ZTS
396static void function_copy_ctor(zval *zv)
397{
398    zend_function *old_func = Z_FUNC_P(zv);
399    Z_FUNC_P(zv) = pemalloc(sizeof(zend_internal_function), 1);
400    memcpy(Z_FUNC_P(zv), old_func, sizeof(zend_internal_function));
401    function_add_ref(Z_FUNC_P(zv));
402}
403
404static void compiler_globals_ctor(zend_compiler_globals *compiler_globals) /* {{{ */
405{
406    compiler_globals->compiled_filename = NULL;
407
408    compiler_globals->function_table = (HashTable *) malloc(sizeof(HashTable));
409    zend_hash_init_ex(compiler_globals->function_table, 1024, NULL, ZEND_FUNCTION_DTOR, 1, 0);
410    zend_hash_copy(compiler_globals->function_table, global_function_table, function_copy_ctor);
411
412    compiler_globals->class_table = (HashTable *) malloc(sizeof(HashTable));
413    zend_hash_init_ex(compiler_globals->class_table, 64, NULL, ZEND_CLASS_DTOR, 1, 0);
414    zend_hash_copy(compiler_globals->class_table, global_class_table, zend_class_add_ref);
415
416    zend_set_default_compile_time_values();
417
418    compiler_globals->auto_globals = (HashTable *) malloc(sizeof(HashTable));
419    zend_hash_init_ex(compiler_globals->auto_globals, 8, NULL, NULL, 1, 0);
420    zend_hash_copy(compiler_globals->auto_globals, global_auto_globals_table, NULL /* empty element */);
421
422    compiler_globals->last_static_member = zend_hash_num_elements(compiler_globals->class_table);
423    if (compiler_globals->last_static_member) {
424        compiler_globals->static_members_table = calloc(compiler_globals->last_static_member, sizeof(zval*));
425    } else {
426        compiler_globals->static_members_table = NULL;
427    }
428    compiler_globals->script_encoding_list = NULL;
429
430#ifdef ZTS
431    zend_interned_empty_string_init(&compiler_globals->empty_string);
432
433    memset(compiler_globals->one_char_string, 0, sizeof(compiler_globals->one_char_string));
434#endif
435}
436/* }}} */
437
438static void compiler_globals_dtor(zend_compiler_globals *compiler_globals) /* {{{ */
439{
440    if (compiler_globals->function_table != GLOBAL_FUNCTION_TABLE) {
441        zend_hash_destroy(compiler_globals->function_table);
442        free(compiler_globals->function_table);
443    }
444    if (compiler_globals->class_table != GLOBAL_CLASS_TABLE) {
445        zend_hash_destroy(compiler_globals->class_table);
446        free(compiler_globals->class_table);
447    }
448    if (compiler_globals->auto_globals != GLOBAL_AUTO_GLOBALS_TABLE) {
449        zend_hash_destroy(compiler_globals->auto_globals);
450        free(compiler_globals->auto_globals);
451    }
452    if (compiler_globals->static_members_table) {
453        free(compiler_globals->static_members_table);
454    }
455    if (compiler_globals->script_encoding_list) {
456        pefree((char*)compiler_globals->script_encoding_list, 1);
457    }
458    compiler_globals->last_static_member = 0;
459
460#ifdef ZTS
461    zend_interned_empty_string_free(&compiler_globals->empty_string);
462#endif
463}
464/* }}} */
465
466static void executor_globals_ctor(zend_executor_globals *executor_globals) /* {{{ */
467{
468    ZEND_TSRMLS_CACHE_UPDATE;
469    zend_startup_constants();
470    zend_copy_constants(EG(zend_constants), GLOBAL_CONSTANTS_TABLE);
471    zend_init_rsrc_plist();
472    zend_init_exception_op();
473    EG(lambda_count) = 0;
474    ZVAL_UNDEF(&EG(user_error_handler));
475    ZVAL_UNDEF(&EG(user_exception_handler));
476    EG(in_autoload) = NULL;
477    EG(current_execute_data) = NULL;
478    EG(current_module) = NULL;
479    EG(exit_status) = 0;
480#if XPFPA_HAVE_CW
481    EG(saved_fpu_cw) = 0;
482#endif
483    EG(saved_fpu_cw_ptr) = NULL;
484    EG(active) = 0;
485}
486/* }}} */
487
488static void executor_globals_dtor(zend_executor_globals *executor_globals) /* {{{ */
489{
490#ifdef ZTS
491    zend_ini_dtor(executor_globals->ini_directives);
492#else
493    zend_ini_shutdown();
494#endif
495    if (&executor_globals->persistent_list != global_persistent_list) {
496        zend_destroy_rsrc_list(&executor_globals->persistent_list);
497    }
498    if (executor_globals->zend_constants != GLOBAL_CONSTANTS_TABLE) {
499        zend_hash_destroy(executor_globals->zend_constants);
500        free(executor_globals->zend_constants);
501    }
502}
503/* }}} */
504
505static void zend_new_thread_end_handler(THREAD_T thread_id) /* {{{ */
506{
507    if (zend_copy_ini_directives() == SUCCESS) {
508        zend_ini_refresh_caches(ZEND_INI_STAGE_STARTUP);
509    }
510}
511/* }}} */
512#endif
513
514#if defined(__FreeBSD__) || defined(__DragonFly__)
515/* FreeBSD and DragonFly floating point precision fix */
516#include <floatingpoint.h>
517#endif
518
519static void ini_scanner_globals_ctor(zend_ini_scanner_globals *scanner_globals_p) /* {{{ */
520{
521    memset(scanner_globals_p, 0, sizeof(*scanner_globals_p));
522}
523/* }}} */
524
525static void php_scanner_globals_ctor(zend_php_scanner_globals *scanner_globals_p) /* {{{ */
526{
527    memset(scanner_globals_p, 0, sizeof(*scanner_globals_p));
528}
529/* }}} */
530
531void zend_init_opcodes_handlers(void);
532
533static void module_destructor_zval(zval *zv) /* {{{ */
534{
535    zend_module_entry *module = (zend_module_entry*)Z_PTR_P(zv);
536
537    module_destructor(module);
538    free(module);
539}
540/* }}} */
541
542static void auto_global_dtor(zval *zv) /* {{{ */
543{
544    free(Z_PTR_P(zv));
545}
546/* }}} */
547
548static zend_bool php_auto_globals_create_globals(zend_string *name) /* {{{ */
549{
550    zval globals;
551
552    ZVAL_ARR(&globals, &EG(symbol_table));
553    ZVAL_NEW_REF(&globals, &globals);
554    zend_hash_update(&EG(symbol_table).ht, name, &globals);
555    return 0;
556}
557/* }}} */
558
559int zend_startup(zend_utility_functions *utility_functions, char **extensions) /* {{{ */
560{
561#ifdef ZTS
562    zend_compiler_globals *compiler_globals;
563    zend_executor_globals *executor_globals;
564    extern ZEND_API ts_rsrc_id ini_scanner_globals_id;
565    extern ZEND_API ts_rsrc_id language_scanner_globals_id;
566    ZEND_TSRMLS_CACHE_UPDATE;
567#else
568    extern zend_ini_scanner_globals ini_scanner_globals;
569    extern zend_php_scanner_globals language_scanner_globals;
570#endif
571
572    start_memory_manager();
573
574    virtual_cwd_startup(); /* Could use shutdown to free the main cwd but it would just slow it down for CGI */
575
576#if defined(__FreeBSD__) || defined(__DragonFly__)
577    /* FreeBSD and DragonFly floating point precision fix */
578    fpsetmask(0);
579#endif
580
581    zend_startup_strtod();
582    zend_startup_extensions_mechanism();
583
584    /* Set up utility functions and values */
585    zend_error_cb = utility_functions->error_function;
586    zend_printf = utility_functions->printf_function;
587    zend_write = (zend_write_func_t) utility_functions->write_function;
588    zend_fopen = utility_functions->fopen_function;
589    if (!zend_fopen) {
590        zend_fopen = zend_fopen_wrapper;
591    }
592    zend_stream_open_function = utility_functions->stream_open_function;
593    zend_message_dispatcher_p = utility_functions->message_handler;
594#ifndef ZEND_SIGNALS
595    zend_block_interruptions = utility_functions->block_interruptions;
596    zend_unblock_interruptions = utility_functions->unblock_interruptions;
597#endif
598    zend_get_configuration_directive_p = utility_functions->get_configuration_directive;
599    zend_ticks_function = utility_functions->ticks_function;
600    zend_on_timeout = utility_functions->on_timeout;
601    zend_vspprintf = utility_functions->vspprintf_function;
602    zend_vstrpprintf = utility_functions->vstrpprintf_function;
603    zend_getenv = utility_functions->getenv_function;
604    zend_resolve_path = utility_functions->resolve_path_function;
605
606#if HAVE_DTRACE
607/* build with dtrace support */
608    zend_compile_file = dtrace_compile_file;
609    zend_execute_ex = dtrace_execute_ex;
610    zend_execute_internal = dtrace_execute_internal;
611#else
612    zend_compile_file = compile_file;
613    zend_execute_ex = execute_ex;
614    zend_execute_internal = NULL;
615#endif /* HAVE_SYS_SDT_H */
616    zend_compile_string = compile_string;
617    zend_throw_exception_hook = NULL;
618
619    /* Set up the default garbage collection implementation. */
620    gc_collect_cycles = zend_gc_collect_cycles;
621
622    zend_init_opcodes_handlers();
623
624    /* set up version */
625    zend_version_info = strdup(ZEND_CORE_VERSION_INFO);
626    zend_version_info_length = sizeof(ZEND_CORE_VERSION_INFO) - 1;
627
628    GLOBAL_FUNCTION_TABLE = (HashTable *) malloc(sizeof(HashTable));
629    GLOBAL_CLASS_TABLE = (HashTable *) malloc(sizeof(HashTable));
630    GLOBAL_AUTO_GLOBALS_TABLE = (HashTable *) malloc(sizeof(HashTable));
631    GLOBAL_CONSTANTS_TABLE = (HashTable *) malloc(sizeof(HashTable));
632
633    zend_hash_init_ex(GLOBAL_FUNCTION_TABLE, 1024, NULL, ZEND_FUNCTION_DTOR, 1, 0);
634    zend_hash_init_ex(GLOBAL_CLASS_TABLE, 64, NULL, ZEND_CLASS_DTOR, 1, 0);
635    zend_hash_init_ex(GLOBAL_AUTO_GLOBALS_TABLE, 8, NULL, auto_global_dtor, 1, 0);
636    zend_hash_init_ex(GLOBAL_CONSTANTS_TABLE, 128, NULL, ZEND_CONSTANT_DTOR, 1, 0);
637
638    zend_hash_init_ex(&module_registry, 32, NULL, module_destructor_zval, 1, 0);
639    zend_init_rsrc_list_dtors();
640
641#ifdef ZTS
642    ts_allocate_id(&compiler_globals_id, sizeof(zend_compiler_globals), (ts_allocate_ctor) compiler_globals_ctor, (ts_allocate_dtor) compiler_globals_dtor);
643    ts_allocate_id(&executor_globals_id, sizeof(zend_executor_globals), (ts_allocate_ctor) executor_globals_ctor, (ts_allocate_dtor) executor_globals_dtor);
644    ts_allocate_id(&language_scanner_globals_id, sizeof(zend_php_scanner_globals), (ts_allocate_ctor) php_scanner_globals_ctor, NULL);
645    ts_allocate_id(&ini_scanner_globals_id, sizeof(zend_ini_scanner_globals), (ts_allocate_ctor) ini_scanner_globals_ctor, NULL);
646    compiler_globals = ts_resource(compiler_globals_id);
647    executor_globals = ts_resource(executor_globals_id);
648
649    compiler_globals_dtor(compiler_globals);
650    compiler_globals->in_compilation = 0;
651    compiler_globals->function_table = (HashTable *) malloc(sizeof(HashTable));
652    compiler_globals->class_table = (HashTable *) malloc(sizeof(HashTable));
653
654    *compiler_globals->function_table = *GLOBAL_FUNCTION_TABLE;
655    *compiler_globals->class_table = *GLOBAL_CLASS_TABLE;
656    compiler_globals->auto_globals = GLOBAL_AUTO_GLOBALS_TABLE;
657
658    zend_hash_destroy(executor_globals->zend_constants);
659    *executor_globals->zend_constants = *GLOBAL_CONSTANTS_TABLE;
660#else
661    ini_scanner_globals_ctor(&ini_scanner_globals);
662    php_scanner_globals_ctor(&language_scanner_globals);
663    zend_set_default_compile_time_values();
664    ZVAL_UNDEF(&EG(user_error_handler));
665    ZVAL_UNDEF(&EG(user_exception_handler));
666#endif
667
668    zend_interned_strings_init();
669    zend_startup_builtin_functions();
670    zend_register_standard_constants();
671    zend_register_auto_global(zend_string_init("GLOBALS", sizeof("GLOBALS") - 1, 1), 1, php_auto_globals_create_globals);
672
673#ifndef ZTS
674    zend_init_rsrc_plist();
675    zend_init_exception_op();
676#endif
677
678    zend_ini_startup();
679
680#ifdef ZTS
681    tsrm_set_new_thread_end_handler(zend_new_thread_end_handler);
682#endif
683
684    return SUCCESS;
685}
686/* }}} */
687
688void zend_register_standard_ini_entries(void) /* {{{ */
689{
690    int module_number = 0;
691
692    REGISTER_INI_ENTRIES();
693}
694/* }}} */
695
696/* Unlink the global (r/o) copies of the class, function and constant tables,
697 * and use a fresh r/w copy for the startup thread
698 */
699void zend_post_startup(void) /* {{{ */
700{
701#ifdef ZTS
702    zend_encoding **script_encoding_list;
703
704    zend_compiler_globals *compiler_globals = ts_resource(compiler_globals_id);
705    zend_executor_globals *executor_globals = ts_resource(executor_globals_id);
706
707    *GLOBAL_FUNCTION_TABLE = *compiler_globals->function_table;
708    *GLOBAL_CLASS_TABLE = *compiler_globals->class_table;
709    *GLOBAL_CONSTANTS_TABLE = *executor_globals->zend_constants;
710
711    short_tags_default = CG(short_tags);
712    compiler_options_default = CG(compiler_options);
713
714    zend_destroy_rsrc_list(&EG(persistent_list));
715    free(compiler_globals->function_table);
716    free(compiler_globals->class_table);
717    if ((script_encoding_list = (zend_encoding **)compiler_globals->script_encoding_list)) {
718        compiler_globals_ctor(compiler_globals);
719        compiler_globals->script_encoding_list = (const zend_encoding **)script_encoding_list;
720    } else {
721        compiler_globals_ctor(compiler_globals);
722    }
723    free(EG(zend_constants));
724
725    virtual_cwd_deactivate();
726
727    executor_globals_ctor(executor_globals);
728    global_persistent_list = &EG(persistent_list);
729    zend_copy_ini_directives();
730#else
731    virtual_cwd_deactivate();
732#endif
733}
734/* }}} */
735
736void zend_shutdown(void) /* {{{ */
737{
738#ifdef ZEND_SIGNALS
739    zend_signal_shutdown();
740#endif
741    zend_destroy_rsrc_list(&EG(persistent_list));
742    if (EG(active))
743    {
744        /*
745         * The order of destruction is important here.
746         * See bugs #65463 and 66036.
747         */
748        zend_function *func;
749        zend_class_entry *ce;
750
751        ZEND_HASH_REVERSE_FOREACH_PTR(GLOBAL_FUNCTION_TABLE, func) {
752            if (func->type == ZEND_USER_FUNCTION) {
753                zend_cleanup_op_array_data((zend_op_array *) func);
754            }
755        } ZEND_HASH_FOREACH_END();
756        ZEND_HASH_REVERSE_FOREACH_PTR(GLOBAL_CLASS_TABLE, ce) {
757            if (ce->type == ZEND_USER_CLASS) {
758                zend_cleanup_user_class_data(ce);
759            } else {
760                break;
761            }
762        } ZEND_HASH_FOREACH_END();
763        zend_cleanup_internal_classes();
764        zend_hash_reverse_apply(GLOBAL_FUNCTION_TABLE, (apply_func_t) clean_non_persistent_function_full);
765        zend_hash_reverse_apply(GLOBAL_CLASS_TABLE, (apply_func_t) clean_non_persistent_class_full);
766    }
767    zend_destroy_modules();
768
769    virtual_cwd_deactivate();
770    virtual_cwd_shutdown();
771
772    zend_hash_destroy(GLOBAL_FUNCTION_TABLE);
773    zend_hash_destroy(GLOBAL_CLASS_TABLE);
774
775    zend_hash_destroy(GLOBAL_AUTO_GLOBALS_TABLE);
776    free(GLOBAL_AUTO_GLOBALS_TABLE);
777
778    zend_shutdown_extensions();
779    free(zend_version_info);
780
781    free(GLOBAL_FUNCTION_TABLE);
782    free(GLOBAL_CLASS_TABLE);
783
784    zend_hash_destroy(GLOBAL_CONSTANTS_TABLE);
785    free(GLOBAL_CONSTANTS_TABLE);
786    zend_shutdown_strtod();
787
788#ifdef ZTS
789    GLOBAL_FUNCTION_TABLE = NULL;
790    GLOBAL_CLASS_TABLE = NULL;
791    GLOBAL_AUTO_GLOBALS_TABLE = NULL;
792    GLOBAL_CONSTANTS_TABLE = NULL;
793#endif
794    zend_destroy_rsrc_list_dtors();
795
796    zend_interned_strings_dtor();
797}
798/* }}} */
799
800void zend_set_utility_values(zend_utility_values *utility_values) /* {{{ */
801{
802    zend_uv = *utility_values;
803    zend_uv.import_use_extension_length = (uint)strlen(zend_uv.import_use_extension);
804}
805/* }}} */
806
807/* this should be compatible with the standard zenderror */
808void zenderror(const char *error) /* {{{ */
809{
810    zend_error(E_PARSE, "%s", error);
811}
812/* }}} */
813
814BEGIN_EXTERN_C()
815ZEND_API void _zend_bailout(char *filename, uint lineno) /* {{{ */
816{
817
818    if (!EG(bailout)) {
819        zend_output_debug_string(1, "%s(%d) : Bailed out without a bailout address!", filename, lineno);
820        exit(-1);
821    }
822    CG(unclean_shutdown) = 1;
823    CG(active_class_entry) = NULL;
824    CG(in_compilation) = 0;
825    EG(current_execute_data) = NULL;
826    LONGJMP(*EG(bailout), FAILURE);
827}
828/* }}} */
829END_EXTERN_C()
830
831ZEND_API void zend_append_version_info(const zend_extension *extension) /* {{{ */
832{
833    char *new_info;
834    uint new_info_length;
835
836    new_info_length = (uint)(sizeof("    with  v, , by \n")
837                        + strlen(extension->name)
838                        + strlen(extension->version)
839                        + strlen(extension->copyright)
840                        + strlen(extension->author));
841
842    new_info = (char *) malloc(new_info_length + 1);
843
844    snprintf(new_info, new_info_length, "    with %s v%s, %s, by %s\n", extension->name, extension->version, extension->copyright, extension->author);
845
846    zend_version_info = (char *) realloc(zend_version_info, zend_version_info_length+new_info_length + 1);
847    strncat(zend_version_info, new_info, new_info_length);
848    zend_version_info_length += new_info_length;
849    free(new_info);
850}
851/* }}} */
852
853ZEND_API char *get_zend_version(void) /* {{{ */
854{
855    return zend_version_info;
856}
857/* }}} */
858
859ZEND_API void zend_activate(void) /* {{{ */
860{
861#ifdef ZTS
862    virtual_cwd_activate();
863#endif
864    gc_reset();
865    init_compiler();
866    init_executor();
867    startup_scanner();
868}
869/* }}} */
870
871void zend_call_destructors(void) /* {{{ */
872{
873    zend_try {
874        shutdown_destructors();
875    } zend_end_try();
876}
877/* }}} */
878
879ZEND_API void zend_deactivate(void) /* {{{ */
880{
881    /* we're no longer executing anything */
882    EG(current_execute_data) = NULL;
883
884    zend_try {
885        shutdown_scanner();
886    } zend_end_try();
887
888    /* shutdown_executor() takes care of its own bailout handling */
889    shutdown_executor();
890
891    zend_try {
892        shutdown_compiler();
893    } zend_end_try();
894
895    zend_destroy_rsrc_list(&EG(regular_list));
896
897#if ZEND_DEBUG
898    if (GC_G(gc_enabled) && !CG(unclean_shutdown)) {
899        gc_collect_cycles();
900    }
901#endif
902
903#if GC_BENCH
904    fprintf(stderr, "GC Statistics\n");
905    fprintf(stderr, "-------------\n");
906    fprintf(stderr, "Runs:               %d\n", GC_G(gc_runs));
907    fprintf(stderr, "Collected:          %d\n", GC_G(collected));
908    fprintf(stderr, "Root buffer length: %d\n", GC_G(root_buf_length));
909    fprintf(stderr, "Root buffer peak:   %d\n\n", GC_G(root_buf_peak));
910    fprintf(stderr, "      Possible            Remove from  Marked\n");
911    fprintf(stderr, "        Root    Buffered     buffer     grey\n");
912    fprintf(stderr, "      --------  --------  -----------  ------\n");
913    fprintf(stderr, "ZVAL  %8d  %8d  %9d  %8d\n", GC_G(zval_possible_root), GC_G(zval_buffered), GC_G(zval_remove_from_buffer), GC_G(zval_marked_grey));
914    fprintf(stderr, "ZOBJ  %8d  %8d  %9d  %8d\n", GC_G(zobj_possible_root), GC_G(zobj_buffered), GC_G(zobj_remove_from_buffer), GC_G(zobj_marked_grey));
915#endif
916
917    zend_try {
918        zend_ini_deactivate();
919    } zend_end_try();
920}
921/* }}} */
922
923BEGIN_EXTERN_C()
924ZEND_API void zend_message_dispatcher(zend_long message, const void *data) /* {{{ */
925{
926    if (zend_message_dispatcher_p) {
927        zend_message_dispatcher_p(message, data);
928    }
929}
930/* }}} */
931END_EXTERN_C()
932
933ZEND_API zval *zend_get_configuration_directive(zend_string *name) /* {{{ */
934{
935    if (zend_get_configuration_directive_p) {
936        return zend_get_configuration_directive_p(name);
937    } else {
938        return NULL;
939    }
940}
941/* }}} */
942
943#define SAVE_STACK(stack) do { \
944        if (CG(stack).top) { \
945            memcpy(&stack, &CG(stack), sizeof(zend_stack)); \
946            CG(stack).top = CG(stack).max = 0; \
947            CG(stack).elements = NULL; \
948        } else { \
949            stack.top = 0; \
950        } \
951    } while (0)
952
953#define RESTORE_STACK(stack) do { \
954        if (stack.top) { \
955            zend_stack_destroy(&CG(stack)); \
956            memcpy(&CG(stack), &stack, sizeof(zend_stack)); \
957        } \
958    } while (0)
959
960#if !defined(ZEND_WIN32) && !defined(DARWIN)
961ZEND_API void zend_error(int type, const char *format, ...) /* {{{ */
962#else
963static void zend_error_va_list(int type, const char *format, va_list args)
964#endif
965{
966    char *str;
967    int len;
968#if !defined(ZEND_WIN32) && !defined(DARWIN)
969    va_list args;
970#endif
971    va_list usr_copy;
972    zval params[5];
973    zval retval;
974    const char *error_filename;
975    uint error_lineno = 0;
976    zval orig_user_error_handler;
977    zend_bool in_compilation;
978    zend_class_entry *saved_class_entry;
979    zend_stack loop_var_stack;
980    zend_stack delayed_oplines_stack;
981    zend_stack context_stack;
982    zend_array *symbol_table;
983
984    /* Report about uncaught exception in case of fatal errors */
985    if (EG(exception)) {
986        zend_execute_data *ex;
987        const zend_op *opline;
988
989        switch (type) {
990            case E_CORE_ERROR:
991            case E_ERROR:
992            case E_RECOVERABLE_ERROR:
993            case E_PARSE:
994            case E_COMPILE_ERROR:
995            case E_USER_ERROR:
996                ex = EG(current_execute_data);
997                opline = NULL;
998                while (ex && (!ex->func || !ZEND_USER_CODE(ex->func->type))) {
999                    ex = ex->prev_execute_data;
1000                }
1001                if (ex && ex->opline->opcode == ZEND_HANDLE_EXCEPTION &&
1002                    EG(opline_before_exception)) {
1003                    opline = EG(opline_before_exception);
1004                }
1005                zend_exception_error(EG(exception), E_WARNING);
1006                EG(exception) = NULL;
1007                if (opline) {
1008                    ex->opline = opline;
1009                }
1010                break;
1011            default:
1012                break;
1013        }
1014    }
1015
1016    /* Obtain relevant filename and lineno */
1017    switch (type) {
1018        case E_CORE_ERROR:
1019        case E_CORE_WARNING:
1020            error_filename = NULL;
1021            error_lineno = 0;
1022            break;
1023        case E_PARSE:
1024        case E_COMPILE_ERROR:
1025        case E_COMPILE_WARNING:
1026        case E_ERROR:
1027        case E_NOTICE:
1028        case E_STRICT:
1029        case E_DEPRECATED:
1030        case E_WARNING:
1031        case E_USER_ERROR:
1032        case E_USER_WARNING:
1033        case E_USER_NOTICE:
1034        case E_USER_DEPRECATED:
1035        case E_RECOVERABLE_ERROR:
1036            if (zend_is_compiling()) {
1037                error_filename = zend_get_compiled_filename()->val;
1038                error_lineno = zend_get_compiled_lineno();
1039            } else if (zend_is_executing()) {
1040                error_filename = zend_get_executed_filename();
1041                if (error_filename[0] == '[') { /* [no active file] */
1042                    error_filename = NULL;
1043                    error_lineno = 0;
1044                } else {
1045                    error_lineno = zend_get_executed_lineno();
1046                }
1047            } else {
1048                error_filename = NULL;
1049                error_lineno = 0;
1050            }
1051            break;
1052        default:
1053            error_filename = NULL;
1054            error_lineno = 0;
1055            break;
1056    }
1057    if (!error_filename) {
1058        error_filename = "Unknown";
1059    }
1060
1061#ifdef HAVE_DTRACE
1062    if(DTRACE_ERROR_ENABLED()) {
1063        char *dtrace_error_buffer;
1064        va_start(args, format);
1065        zend_vspprintf(&dtrace_error_buffer, 0, format, args);
1066        DTRACE_ERROR(dtrace_error_buffer, (char *)error_filename, error_lineno);
1067        efree(dtrace_error_buffer);
1068        va_end(args);
1069    }
1070#endif /* HAVE_DTRACE */
1071
1072#if !defined(ZEND_WIN32) && !defined(DARWIN)
1073    va_start(args, format);
1074#endif
1075
1076    /* if we don't have a user defined error handler */
1077    if (Z_TYPE(EG(user_error_handler)) == IS_UNDEF
1078        || !(EG(user_error_handler_error_reporting) & type)
1079        || EG(error_handling) != EH_NORMAL) {
1080        zend_error_cb(type, error_filename, error_lineno, format, args);
1081    } else switch (type) {
1082        case E_ERROR:
1083        case E_PARSE:
1084        case E_CORE_ERROR:
1085        case E_CORE_WARNING:
1086        case E_COMPILE_ERROR:
1087        case E_COMPILE_WARNING:
1088            /* The error may not be safe to handle in user-space */
1089            zend_error_cb(type, error_filename, error_lineno, format, args);
1090            break;
1091        default:
1092            /* Handle the error in user space */
1093/* va_copy() is __va_copy() in old gcc versions.
1094 * According to the autoconf manual, using
1095 * memcpy(&dst, &src, sizeof(va_list))
1096 * gives maximum portability. */
1097#ifndef va_copy
1098# ifdef __va_copy
1099#  define va_copy(dest, src)    __va_copy((dest), (src))
1100# else
1101#  define va_copy(dest, src)    memcpy(&(dest), &(src), sizeof(va_list))
1102# endif
1103#endif
1104            va_copy(usr_copy, args);
1105            len = (int)zend_vspprintf(&str, 0, format, usr_copy);
1106            ZVAL_NEW_STR(&params[1], zend_string_init(str, len, 0));
1107            efree(str);
1108#ifdef va_copy
1109            va_end(usr_copy);
1110#endif
1111
1112            ZVAL_LONG(&params[0], type);
1113
1114            if (error_filename) {
1115                ZVAL_STRING(&params[2], error_filename);
1116            } else {
1117                ZVAL_NULL(&params[2]);
1118            }
1119
1120            ZVAL_LONG(&params[3], error_lineno);
1121
1122            symbol_table = zend_rebuild_symbol_table();
1123
1124            /* during shutdown the symbol table table can be still null */
1125            if (!symbol_table) {
1126                ZVAL_NULL(&params[4]);
1127            } else {
1128                ZVAL_NEW_ARR(&params[4]);
1129                zend_array_dup(Z_ARRVAL(params[4]), &symbol_table->ht);
1130            }
1131
1132            ZVAL_COPY_VALUE(&orig_user_error_handler, &EG(user_error_handler));
1133            ZVAL_UNDEF(&EG(user_error_handler));
1134
1135            /* User error handler may include() additinal PHP files.
1136             * If an error was generated during comilation PHP will compile
1137             * such scripts recursivly, but some CG() variables may be
1138             * inconsistent. */
1139
1140            in_compilation = CG(in_compilation);
1141            if (in_compilation) {
1142                saved_class_entry = CG(active_class_entry);
1143                CG(active_class_entry) = NULL;
1144                SAVE_STACK(loop_var_stack);
1145                SAVE_STACK(delayed_oplines_stack);
1146                SAVE_STACK(context_stack);
1147                CG(in_compilation) = 0;
1148            }
1149
1150            ZVAL_UNDEF(&retval);
1151            if (call_user_function_ex(CG(function_table), NULL, &orig_user_error_handler, &retval, 5, params, 1, NULL) == SUCCESS) {
1152                if (Z_TYPE(retval) != IS_UNDEF) {
1153                    if (Z_TYPE(retval) == IS_FALSE) {
1154                        zend_error_cb(type, error_filename, error_lineno, format, args);
1155                    }
1156                    zval_ptr_dtor(&retval);
1157                }
1158            } else if (!EG(exception)) {
1159                /* The user error handler failed, use built-in error handler */
1160                zend_error_cb(type, error_filename, error_lineno, format, args);
1161            }
1162
1163            if (in_compilation) {
1164                CG(active_class_entry) = saved_class_entry;
1165                RESTORE_STACK(loop_var_stack);
1166                RESTORE_STACK(delayed_oplines_stack);
1167                RESTORE_STACK(context_stack);
1168                CG(in_compilation) = 1;
1169            }
1170
1171            zval_ptr_dtor(&params[4]);
1172            zval_ptr_dtor(&params[3]);
1173            zval_ptr_dtor(&params[2]);
1174            zval_ptr_dtor(&params[1]);
1175            zval_ptr_dtor(&params[0]);
1176
1177            if (Z_TYPE(EG(user_error_handler)) == IS_UNDEF) {
1178                ZVAL_COPY_VALUE(&EG(user_error_handler), &orig_user_error_handler);
1179            } else {
1180                zval_ptr_dtor(&orig_user_error_handler);
1181            }
1182            break;
1183    }
1184
1185#if !defined(ZEND_WIN32) && !defined(DARWIN)
1186    va_end(args);
1187#endif
1188
1189    if (type == E_PARSE) {
1190        /* eval() errors do not affect exit_status */
1191        if (!(EG(current_execute_data) &&
1192            EG(current_execute_data)->func &&
1193            ZEND_USER_CODE(EG(current_execute_data)->func->type) &&
1194            EG(current_execute_data)->opline->opcode == ZEND_INCLUDE_OR_EVAL &&
1195            EG(current_execute_data)->opline->extended_value == ZEND_EVAL)) {
1196            EG(exit_status) = 255;
1197        }
1198    }
1199}
1200/* }}} */
1201
1202#if (defined(__GNUC__) && __GNUC__ >= 3 && !defined(__INTEL_COMPILER) && !defined(DARWIN) && !defined(__hpux) && !defined(_AIX) && !defined(__osf__))
1203void zend_error_noreturn(int type, const char *format, ...) __attribute__ ((alias("zend_error"),noreturn));
1204#elif defined(ZEND_WIN32) || defined(DARWIN)
1205ZEND_API void zend_error(int type, const char *format, ...) /* {{{ */
1206{
1207    va_list va;
1208
1209    va_start(va, format);
1210    zend_error_va_list(type, format, va);
1211    va_end(va);
1212}
1213
1214ZEND_API ZEND_NORETURN void zend_error_noreturn(int type, const char *format, ...)
1215{
1216    va_list va;
1217
1218    va_start(va, format);
1219    zend_error_va_list(type, format, va);
1220    va_end(va);
1221}
1222/* }}} */
1223#endif
1224
1225ZEND_API void zend_output_debug_string(zend_bool trigger_break, const char *format, ...) /* {{{ */
1226{
1227#if ZEND_DEBUG
1228    va_list args;
1229
1230    va_start(args, format);
1231#   ifdef ZEND_WIN32
1232    {
1233        char output_buf[1024];
1234
1235        vsnprintf(output_buf, 1024, format, args);
1236        OutputDebugString(output_buf);
1237        OutputDebugString("\n");
1238        if (trigger_break && IsDebuggerPresent()) {
1239            DebugBreak();
1240        }
1241    }
1242#   else
1243    vfprintf(stderr, format, args);
1244    fprintf(stderr, "\n");
1245#   endif
1246    va_end(args);
1247#endif
1248}
1249/* }}} */
1250
1251ZEND_API int zend_execute_scripts(int type, zval *retval, int file_count, ...) /* {{{ */
1252{
1253    va_list files;
1254    int i;
1255    zend_file_handle *file_handle;
1256    zend_op_array *op_array;
1257
1258    va_start(files, file_count);
1259    for (i = 0; i < file_count; i++) {
1260        file_handle = va_arg(files, zend_file_handle *);
1261        if (!file_handle) {
1262            continue;
1263        }
1264
1265        op_array = zend_compile_file(file_handle, type);
1266        if (file_handle->opened_path) {
1267            zend_hash_str_add_empty_element(&EG(included_files), file_handle->opened_path, strlen(file_handle->opened_path));
1268        }
1269        zend_destroy_file_handle(file_handle);
1270        if (op_array) {
1271            zend_execute(op_array, retval);
1272            zend_exception_restore();
1273            if (EG(exception)) {
1274                if (Z_TYPE(EG(user_exception_handler)) != IS_UNDEF) {
1275                    zval orig_user_exception_handler;
1276                    zval params[1], retval2;
1277                    zend_object *old_exception;
1278                    old_exception = EG(exception);
1279                    EG(exception) = NULL;
1280                    ZVAL_OBJ(&params[0], old_exception);
1281                    ZVAL_COPY_VALUE(&orig_user_exception_handler, &EG(user_exception_handler));
1282                    ZVAL_UNDEF(&retval2);
1283                    if (call_user_function_ex(CG(function_table), NULL, &orig_user_exception_handler, &retval2, 1, params, 1, NULL) == SUCCESS) {
1284                        zval_ptr_dtor(&retval2);
1285                        if (EG(exception)) {
1286                            OBJ_RELEASE(EG(exception));
1287                            EG(exception) = NULL;
1288                        }
1289                        OBJ_RELEASE(old_exception);
1290                    } else {
1291                        EG(exception) = old_exception;
1292                        zend_exception_error(EG(exception), E_ERROR);
1293                    }
1294                } else {
1295                    zend_exception_error(EG(exception), E_ERROR);
1296                }
1297            }
1298            destroy_op_array(op_array);
1299            efree_size(op_array, sizeof(zend_op_array));
1300        } else if (type==ZEND_REQUIRE) {
1301            va_end(files);
1302            return FAILURE;
1303        }
1304    }
1305    va_end(files);
1306
1307    return SUCCESS;
1308}
1309/* }}} */
1310
1311#define COMPILED_STRING_DESCRIPTION_FORMAT "%s(%d) : %s"
1312
1313ZEND_API char *zend_make_compiled_string_description(const char *name) /* {{{ */
1314{
1315    const char *cur_filename;
1316    int cur_lineno;
1317    char *compiled_string_description;
1318
1319    if (zend_is_compiling()) {
1320        cur_filename = zend_get_compiled_filename()->val;
1321        cur_lineno = zend_get_compiled_lineno();
1322    } else if (zend_is_executing()) {
1323        cur_filename = zend_get_executed_filename();
1324        cur_lineno = zend_get_executed_lineno();
1325    } else {
1326        cur_filename = "Unknown";
1327        cur_lineno = 0;
1328    }
1329
1330    zend_spprintf(&compiled_string_description, 0, COMPILED_STRING_DESCRIPTION_FORMAT, cur_filename, cur_lineno, name);
1331    return compiled_string_description;
1332}
1333/* }}} */
1334
1335void free_estring(char **str_p) /* {{{ */
1336{
1337    efree(*str_p);
1338}
1339/* }}} */
1340
1341void free_string_zval(zval *zv) /* {{{ */
1342{
1343    zend_string *str = Z_PTR_P(zv);
1344    zend_string_release(str);
1345}
1346/* }}} */
1347
1348/*
1349 * Local variables:
1350 * tab-width: 4
1351 * c-basic-offset: 4
1352 * indent-tabs-mode: t
1353 * End:
1354 */
1355