1/*
2   +----------------------------------------------------------------------+
3   | Zend Engine                                                          |
4   +----------------------------------------------------------------------+
5   | Copyright (c) 1998-2014 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->val, new_value->len);
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-2014 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    zend_init_opcodes_handlers();
620
621    /* set up version */
622    zend_version_info = strdup(ZEND_CORE_VERSION_INFO);
623    zend_version_info_length = sizeof(ZEND_CORE_VERSION_INFO) - 1;
624
625    GLOBAL_FUNCTION_TABLE = (HashTable *) malloc(sizeof(HashTable));
626    GLOBAL_CLASS_TABLE = (HashTable *) malloc(sizeof(HashTable));
627    GLOBAL_AUTO_GLOBALS_TABLE = (HashTable *) malloc(sizeof(HashTable));
628    GLOBAL_CONSTANTS_TABLE = (HashTable *) malloc(sizeof(HashTable));
629
630    zend_hash_init_ex(GLOBAL_FUNCTION_TABLE, 1024, NULL, ZEND_FUNCTION_DTOR, 1, 0);
631    zend_hash_init_ex(GLOBAL_CLASS_TABLE, 64, NULL, ZEND_CLASS_DTOR, 1, 0);
632    zend_hash_init_ex(GLOBAL_AUTO_GLOBALS_TABLE, 8, NULL, auto_global_dtor, 1, 0);
633    zend_hash_init_ex(GLOBAL_CONSTANTS_TABLE, 128, NULL, ZEND_CONSTANT_DTOR, 1, 0);
634
635    zend_hash_init_ex(&module_registry, 32, NULL, module_destructor_zval, 1, 0);
636    zend_init_rsrc_list_dtors();
637
638#ifdef ZTS
639    ts_allocate_id(&compiler_globals_id, sizeof(zend_compiler_globals), (ts_allocate_ctor) compiler_globals_ctor, (ts_allocate_dtor) compiler_globals_dtor);
640    ts_allocate_id(&executor_globals_id, sizeof(zend_executor_globals), (ts_allocate_ctor) executor_globals_ctor, (ts_allocate_dtor) executor_globals_dtor);
641    ts_allocate_id(&language_scanner_globals_id, sizeof(zend_php_scanner_globals), (ts_allocate_ctor) php_scanner_globals_ctor, NULL);
642    ts_allocate_id(&ini_scanner_globals_id, sizeof(zend_ini_scanner_globals), (ts_allocate_ctor) ini_scanner_globals_ctor, NULL);
643    compiler_globals = ts_resource(compiler_globals_id);
644    executor_globals = ts_resource(executor_globals_id);
645
646    compiler_globals_dtor(compiler_globals);
647    compiler_globals->in_compilation = 0;
648    compiler_globals->function_table = (HashTable *) malloc(sizeof(HashTable));
649    compiler_globals->class_table = (HashTable *) malloc(sizeof(HashTable));
650
651    *compiler_globals->function_table = *GLOBAL_FUNCTION_TABLE;
652    *compiler_globals->class_table = *GLOBAL_CLASS_TABLE;
653    compiler_globals->auto_globals = GLOBAL_AUTO_GLOBALS_TABLE;
654
655    zend_hash_destroy(executor_globals->zend_constants);
656    *executor_globals->zend_constants = *GLOBAL_CONSTANTS_TABLE;
657#else
658    ini_scanner_globals_ctor(&ini_scanner_globals);
659    php_scanner_globals_ctor(&language_scanner_globals);
660    zend_set_default_compile_time_values();
661    ZVAL_UNDEF(&EG(user_error_handler));
662    ZVAL_UNDEF(&EG(user_exception_handler));
663#endif
664
665    zend_interned_strings_init();
666    zend_startup_builtin_functions();
667    zend_register_standard_constants();
668    zend_register_auto_global(zend_string_init("GLOBALS", sizeof("GLOBALS") - 1, 1), 1, php_auto_globals_create_globals);
669
670#ifndef ZTS
671    zend_init_rsrc_plist();
672    zend_init_exception_op();
673#endif
674
675    zend_ini_startup();
676
677#ifdef ZTS
678    tsrm_set_new_thread_end_handler(zend_new_thread_end_handler);
679#endif
680
681    return SUCCESS;
682}
683/* }}} */
684
685void zend_register_standard_ini_entries(void) /* {{{ */
686{
687    int module_number = 0;
688
689    REGISTER_INI_ENTRIES();
690}
691/* }}} */
692
693/* Unlink the global (r/o) copies of the class, function and constant tables,
694 * and use a fresh r/w copy for the startup thread
695 */
696void zend_post_startup(void) /* {{{ */
697{
698#ifdef ZTS
699    zend_encoding **script_encoding_list;
700
701    zend_compiler_globals *compiler_globals = ts_resource(compiler_globals_id);
702    zend_executor_globals *executor_globals = ts_resource(executor_globals_id);
703
704    *GLOBAL_FUNCTION_TABLE = *compiler_globals->function_table;
705    *GLOBAL_CLASS_TABLE = *compiler_globals->class_table;
706    *GLOBAL_CONSTANTS_TABLE = *executor_globals->zend_constants;
707
708    short_tags_default = CG(short_tags);
709    compiler_options_default = CG(compiler_options);
710
711    zend_destroy_rsrc_list(&EG(persistent_list));
712    free(compiler_globals->function_table);
713    free(compiler_globals->class_table);
714    if ((script_encoding_list = (zend_encoding **)compiler_globals->script_encoding_list)) {
715        compiler_globals_ctor(compiler_globals);
716        compiler_globals->script_encoding_list = (const zend_encoding **)script_encoding_list;
717    } else {
718        compiler_globals_ctor(compiler_globals);
719    }
720    free(EG(zend_constants));
721
722    virtual_cwd_deactivate();
723
724    executor_globals_ctor(executor_globals);
725    global_persistent_list = &EG(persistent_list);
726    zend_copy_ini_directives();
727#else
728    virtual_cwd_deactivate();
729#endif
730}
731/* }}} */
732
733void zend_shutdown(void) /* {{{ */
734{
735#ifdef ZEND_SIGNALS
736    zend_signal_shutdown();
737#endif
738    zend_destroy_rsrc_list(&EG(persistent_list));
739    if (EG(active))
740    {
741        /*
742         * The order of destruction is important here.
743         * See bugs #65463 and 66036.
744         */
745        zend_function *func;
746        zend_class_entry *ce;
747
748        ZEND_HASH_REVERSE_FOREACH_PTR(GLOBAL_FUNCTION_TABLE, func) {
749            if (func->type == ZEND_USER_FUNCTION) {
750                zend_cleanup_op_array_data((zend_op_array *) func);
751            }
752        } ZEND_HASH_FOREACH_END();
753        ZEND_HASH_REVERSE_FOREACH_PTR(GLOBAL_CLASS_TABLE, ce) {
754            if (ce->type == ZEND_USER_CLASS) {
755                zend_cleanup_user_class_data(ce);
756            } else {
757                break;
758            }
759        } ZEND_HASH_FOREACH_END();
760        zend_cleanup_internal_classes();
761        zend_hash_reverse_apply(GLOBAL_FUNCTION_TABLE, (apply_func_t) clean_non_persistent_function_full);
762        zend_hash_reverse_apply(GLOBAL_CLASS_TABLE, (apply_func_t) clean_non_persistent_class_full);
763    }
764    zend_destroy_modules();
765
766    virtual_cwd_deactivate();
767    virtual_cwd_shutdown();
768
769    zend_hash_destroy(GLOBAL_FUNCTION_TABLE);
770    zend_hash_destroy(GLOBAL_CLASS_TABLE);
771
772    zend_hash_destroy(GLOBAL_AUTO_GLOBALS_TABLE);
773    free(GLOBAL_AUTO_GLOBALS_TABLE);
774
775    zend_shutdown_extensions();
776    free(zend_version_info);
777
778    free(GLOBAL_FUNCTION_TABLE);
779    free(GLOBAL_CLASS_TABLE);
780
781    zend_hash_destroy(GLOBAL_CONSTANTS_TABLE);
782    free(GLOBAL_CONSTANTS_TABLE);
783    zend_shutdown_strtod();
784
785#ifdef ZTS
786    GLOBAL_FUNCTION_TABLE = NULL;
787    GLOBAL_CLASS_TABLE = NULL;
788    GLOBAL_AUTO_GLOBALS_TABLE = NULL;
789    GLOBAL_CONSTANTS_TABLE = NULL;
790#endif
791    zend_destroy_rsrc_list_dtors();
792
793    zend_interned_strings_dtor();
794}
795/* }}} */
796
797void zend_set_utility_values(zend_utility_values *utility_values) /* {{{ */
798{
799    zend_uv = *utility_values;
800    zend_uv.import_use_extension_length = (uint)strlen(zend_uv.import_use_extension);
801}
802/* }}} */
803
804/* this should be compatible with the standard zenderror */
805void zenderror(const char *error) /* {{{ */
806{
807    zend_error(E_PARSE, "%s", error);
808}
809/* }}} */
810
811BEGIN_EXTERN_C()
812ZEND_API void _zend_bailout(char *filename, uint lineno) /* {{{ */
813{
814
815    if (!EG(bailout)) {
816        zend_output_debug_string(1, "%s(%d) : Bailed out without a bailout address!", filename, lineno);
817        exit(-1);
818    }
819    CG(unclean_shutdown) = 1;
820    CG(active_class_entry) = NULL;
821    CG(in_compilation) = 0;
822    EG(current_execute_data) = NULL;
823    LONGJMP(*EG(bailout), FAILURE);
824}
825/* }}} */
826END_EXTERN_C()
827
828ZEND_API void zend_append_version_info(const zend_extension *extension) /* {{{ */
829{
830    char *new_info;
831    uint new_info_length;
832
833    new_info_length = (uint)(sizeof("    with  v, , by \n")
834                        + strlen(extension->name)
835                        + strlen(extension->version)
836                        + strlen(extension->copyright)
837                        + strlen(extension->author));
838
839    new_info = (char *) malloc(new_info_length + 1);
840
841    snprintf(new_info, new_info_length, "    with %s v%s, %s, by %s\n", extension->name, extension->version, extension->copyright, extension->author);
842
843    zend_version_info = (char *) realloc(zend_version_info, zend_version_info_length+new_info_length + 1);
844    strncat(zend_version_info, new_info, new_info_length);
845    zend_version_info_length += new_info_length;
846    free(new_info);
847}
848/* }}} */
849
850ZEND_API char *get_zend_version(void) /* {{{ */
851{
852    return zend_version_info;
853}
854/* }}} */
855
856ZEND_API void zend_activate(void) /* {{{ */
857{
858#ifdef ZTS
859    virtual_cwd_activate();
860#endif
861    gc_reset();
862    init_compiler();
863    init_executor();
864    startup_scanner();
865}
866/* }}} */
867
868void zend_call_destructors(void) /* {{{ */
869{
870    zend_try {
871        shutdown_destructors();
872    } zend_end_try();
873}
874/* }}} */
875
876ZEND_API void zend_deactivate(void) /* {{{ */
877{
878    /* we're no longer executing anything */
879    EG(current_execute_data) = NULL;
880
881    zend_try {
882        shutdown_scanner();
883    } zend_end_try();
884
885    /* shutdown_executor() takes care of its own bailout handling */
886    shutdown_executor();
887
888    zend_try {
889        shutdown_compiler();
890    } zend_end_try();
891
892    zend_destroy_rsrc_list(&EG(regular_list));
893
894#if ZEND_DEBUG
895    if (GC_G(gc_enabled) && !CG(unclean_shutdown)) {
896        gc_collect_cycles();
897    }
898#endif
899
900#if GC_BENCH
901    fprintf(stderr, "GC Statistics\n");
902    fprintf(stderr, "-------------\n");
903    fprintf(stderr, "Runs:               %d\n", GC_G(gc_runs));
904    fprintf(stderr, "Collected:          %d\n", GC_G(collected));
905    fprintf(stderr, "Root buffer length: %d\n", GC_G(root_buf_length));
906    fprintf(stderr, "Root buffer peak:   %d\n\n", GC_G(root_buf_peak));
907    fprintf(stderr, "      Possible            Remove from  Marked\n");
908    fprintf(stderr, "        Root    Buffered     buffer     grey\n");
909    fprintf(stderr, "      --------  --------  -----------  ------\n");
910    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));
911    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));
912#endif
913
914    zend_try {
915        zend_ini_deactivate();
916    } zend_end_try();
917}
918/* }}} */
919
920BEGIN_EXTERN_C()
921ZEND_API void zend_message_dispatcher(zend_long message, const void *data) /* {{{ */
922{
923    if (zend_message_dispatcher_p) {
924        zend_message_dispatcher_p(message, data);
925    }
926}
927/* }}} */
928END_EXTERN_C()
929
930ZEND_API zval *zend_get_configuration_directive(zend_string *name) /* {{{ */
931{
932    if (zend_get_configuration_directive_p) {
933        return zend_get_configuration_directive_p(name);
934    } else {
935        return NULL;
936    }
937}
938/* }}} */
939
940#define SAVE_STACK(stack) do { \
941        if (CG(stack).top) { \
942            memcpy(&stack, &CG(stack), sizeof(zend_stack)); \
943            CG(stack).top = CG(stack).max = 0; \
944            CG(stack).elements = NULL; \
945        } else { \
946            stack.top = 0; \
947        } \
948    } while (0)
949
950#define RESTORE_STACK(stack) do { \
951        if (stack.top) { \
952            zend_stack_destroy(&CG(stack)); \
953            memcpy(&CG(stack), &stack, sizeof(zend_stack)); \
954        } \
955    } while (0)
956
957#if !defined(ZEND_WIN32) && !defined(DARWIN)
958ZEND_API void zend_error(int type, const char *format, ...) /* {{{ */
959#else
960static void zend_error_va_list(int type, const char *format, va_list args)
961#endif
962{
963    char *str;
964    int len;
965#if !defined(ZEND_WIN32) && !defined(DARWIN)
966    va_list args;
967#endif
968    va_list usr_copy;
969    zval params[5];
970    zval retval;
971    const char *error_filename;
972    uint error_lineno = 0;
973    zval orig_user_error_handler;
974    zend_bool in_compilation;
975    zend_class_entry *saved_class_entry;
976    zend_stack loop_var_stack;
977    zend_stack delayed_oplines_stack;
978    zend_stack context_stack;
979    zend_array *symbol_table;
980
981    /* Report about uncaught exception in case of fatal errors */
982    if (EG(exception)) {
983        zend_execute_data *ex;
984        const zend_op *opline;
985
986        switch (type) {
987            case E_CORE_ERROR:
988            case E_ERROR:
989            case E_RECOVERABLE_ERROR:
990            case E_PARSE:
991            case E_COMPILE_ERROR:
992            case E_USER_ERROR:
993                ex = EG(current_execute_data);
994                opline = NULL;
995                while (ex && (!ex->func || !ZEND_USER_CODE(ex->func->type))) {
996                    ex = ex->prev_execute_data;
997                }
998                if (ex && ex->opline->opcode == ZEND_HANDLE_EXCEPTION &&
999                    EG(opline_before_exception)) {
1000                    opline = EG(opline_before_exception);
1001                }
1002                zend_exception_error(EG(exception), E_WARNING);
1003                EG(exception) = NULL;
1004                if (opline) {
1005                    ex->opline = opline;
1006                }
1007                break;
1008            default:
1009                break;
1010        }
1011    }
1012
1013    /* Obtain relevant filename and lineno */
1014    switch (type) {
1015        case E_CORE_ERROR:
1016        case E_CORE_WARNING:
1017            error_filename = NULL;
1018            error_lineno = 0;
1019            break;
1020        case E_PARSE:
1021        case E_COMPILE_ERROR:
1022        case E_COMPILE_WARNING:
1023        case E_ERROR:
1024        case E_NOTICE:
1025        case E_STRICT:
1026        case E_DEPRECATED:
1027        case E_WARNING:
1028        case E_USER_ERROR:
1029        case E_USER_WARNING:
1030        case E_USER_NOTICE:
1031        case E_USER_DEPRECATED:
1032        case E_RECOVERABLE_ERROR:
1033            if (zend_is_compiling()) {
1034                error_filename = zend_get_compiled_filename()->val;
1035                error_lineno = zend_get_compiled_lineno();
1036            } else if (zend_is_executing()) {
1037                error_filename = zend_get_executed_filename();
1038                if (error_filename[0] == '[') { /* [no active file] */
1039                    error_filename = NULL;
1040                    error_lineno = 0;
1041                } else {
1042                    error_lineno = zend_get_executed_lineno();
1043                }
1044            } else {
1045                error_filename = NULL;
1046                error_lineno = 0;
1047            }
1048            break;
1049        default:
1050            error_filename = NULL;
1051            error_lineno = 0;
1052            break;
1053    }
1054    if (!error_filename) {
1055        error_filename = "Unknown";
1056    }
1057
1058#ifdef HAVE_DTRACE
1059    if(DTRACE_ERROR_ENABLED()) {
1060        char *dtrace_error_buffer;
1061        va_start(args, format);
1062        zend_vspprintf(&dtrace_error_buffer, 0, format, args);
1063        DTRACE_ERROR(dtrace_error_buffer, (char *)error_filename, error_lineno);
1064        efree(dtrace_error_buffer);
1065        va_end(args);
1066    }
1067#endif /* HAVE_DTRACE */
1068
1069#if !defined(ZEND_WIN32) && !defined(DARWIN)
1070    va_start(args, format);
1071#endif
1072
1073    /* if we don't have a user defined error handler */
1074    if (Z_TYPE(EG(user_error_handler)) == IS_UNDEF
1075        || !(EG(user_error_handler_error_reporting) & type)
1076        || EG(error_handling) != EH_NORMAL) {
1077        zend_error_cb(type, error_filename, error_lineno, format, args);
1078    } else switch (type) {
1079        case E_ERROR:
1080        case E_PARSE:
1081        case E_CORE_ERROR:
1082        case E_CORE_WARNING:
1083        case E_COMPILE_ERROR:
1084        case E_COMPILE_WARNING:
1085            /* The error may not be safe to handle in user-space */
1086            zend_error_cb(type, error_filename, error_lineno, format, args);
1087            break;
1088        default:
1089            /* Handle the error in user space */
1090/* va_copy() is __va_copy() in old gcc versions.
1091 * According to the autoconf manual, using
1092 * memcpy(&dst, &src, sizeof(va_list))
1093 * gives maximum portability. */
1094#ifndef va_copy
1095# ifdef __va_copy
1096#  define va_copy(dest, src)    __va_copy((dest), (src))
1097# else
1098#  define va_copy(dest, src)    memcpy(&(dest), &(src), sizeof(va_list))
1099# endif
1100#endif
1101            va_copy(usr_copy, args);
1102            len = (int)zend_vspprintf(&str, 0, format, usr_copy);
1103            ZVAL_NEW_STR(&params[1], zend_string_init(str, len, 0));
1104            efree(str);
1105#ifdef va_copy
1106            va_end(usr_copy);
1107#endif
1108
1109            ZVAL_LONG(&params[0], type);
1110
1111            if (error_filename) {
1112                ZVAL_STRING(&params[2], error_filename);
1113            } else {
1114                ZVAL_NULL(&params[2]);
1115            }
1116
1117            ZVAL_LONG(&params[3], error_lineno);
1118
1119            symbol_table = zend_rebuild_symbol_table();
1120
1121            /* during shutdown the symbol table table can be still null */
1122            if (!symbol_table) {
1123                ZVAL_NULL(&params[4]);
1124            } else {
1125                ZVAL_NEW_ARR(&params[4]);
1126                zend_array_dup(Z_ARRVAL(params[4]), &symbol_table->ht);
1127            }
1128
1129            ZVAL_COPY_VALUE(&orig_user_error_handler, &EG(user_error_handler));
1130            ZVAL_UNDEF(&EG(user_error_handler));
1131
1132            /* User error handler may include() additinal PHP files.
1133             * If an error was generated during comilation PHP will compile
1134             * such scripts recursivly, but some CG() variables may be
1135             * inconsistent. */
1136
1137            in_compilation = CG(in_compilation);
1138            if (in_compilation) {
1139                saved_class_entry = CG(active_class_entry);
1140                CG(active_class_entry) = NULL;
1141                SAVE_STACK(loop_var_stack);
1142                SAVE_STACK(delayed_oplines_stack);
1143                SAVE_STACK(context_stack);
1144                CG(in_compilation) = 0;
1145            }
1146
1147            ZVAL_UNDEF(&retval);
1148            if (call_user_function_ex(CG(function_table), NULL, &orig_user_error_handler, &retval, 5, params, 1, NULL) == SUCCESS) {
1149                if (Z_TYPE(retval) != IS_UNDEF) {
1150                    if (Z_TYPE(retval) == IS_FALSE) {
1151                        zend_error_cb(type, error_filename, error_lineno, format, args);
1152                    }
1153                    zval_ptr_dtor(&retval);
1154                }
1155            } else if (!EG(exception)) {
1156                /* The user error handler failed, use built-in error handler */
1157                zend_error_cb(type, error_filename, error_lineno, format, args);
1158            }
1159
1160            if (in_compilation) {
1161                CG(active_class_entry) = saved_class_entry;
1162                RESTORE_STACK(loop_var_stack);
1163                RESTORE_STACK(delayed_oplines_stack);
1164                RESTORE_STACK(context_stack);
1165                CG(in_compilation) = 1;
1166            }
1167
1168            zval_ptr_dtor(&params[4]);
1169            zval_ptr_dtor(&params[3]);
1170            zval_ptr_dtor(&params[2]);
1171            zval_ptr_dtor(&params[1]);
1172            zval_ptr_dtor(&params[0]);
1173
1174            if (Z_TYPE(EG(user_error_handler)) == IS_UNDEF) {
1175                ZVAL_COPY_VALUE(&EG(user_error_handler), &orig_user_error_handler);
1176            } else {
1177                zval_ptr_dtor(&orig_user_error_handler);
1178            }
1179            break;
1180    }
1181
1182#if !defined(ZEND_WIN32) && !defined(DARWIN)
1183    va_end(args);
1184#endif
1185
1186    if (type == E_PARSE) {
1187        /* eval() errors do not affect exit_status */
1188        if (!(EG(current_execute_data) &&
1189            EG(current_execute_data)->func &&
1190            ZEND_USER_CODE(EG(current_execute_data)->func->type) &&
1191            EG(current_execute_data)->opline->opcode == ZEND_INCLUDE_OR_EVAL &&
1192            EG(current_execute_data)->opline->extended_value == ZEND_EVAL)) {
1193            EG(exit_status) = 255;
1194        }
1195    }
1196}
1197/* }}} */
1198
1199#if (defined(__GNUC__) && __GNUC__ >= 3 && !defined(__INTEL_COMPILER) && !defined(DARWIN) && !defined(__hpux) && !defined(_AIX) && !defined(__osf__))
1200void zend_error_noreturn(int type, const char *format, ...) __attribute__ ((alias("zend_error"),noreturn));
1201#elif defined(ZEND_WIN32) || defined(DARWIN)
1202ZEND_API void zend_error(int type, const char *format, ...) /* {{{ */
1203{
1204    va_list va;
1205
1206    va_start(va, format);
1207    zend_error_va_list(type, format, va);
1208    va_end(va);
1209}
1210
1211ZEND_API ZEND_NORETURN void zend_error_noreturn(int type, const char *format, ...)
1212{
1213    va_list va;
1214
1215    va_start(va, format);
1216    zend_error_va_list(type, format, va);
1217    va_end(va);
1218}
1219/* }}} */
1220#endif
1221
1222ZEND_API void zend_output_debug_string(zend_bool trigger_break, const char *format, ...) /* {{{ */
1223{
1224#if ZEND_DEBUG
1225    va_list args;
1226
1227    va_start(args, format);
1228#   ifdef ZEND_WIN32
1229    {
1230        char output_buf[1024];
1231
1232        vsnprintf(output_buf, 1024, format, args);
1233        OutputDebugString(output_buf);
1234        OutputDebugString("\n");
1235        if (trigger_break && IsDebuggerPresent()) {
1236            DebugBreak();
1237        }
1238    }
1239#   else
1240    vfprintf(stderr, format, args);
1241    fprintf(stderr, "\n");
1242#   endif
1243    va_end(args);
1244#endif
1245}
1246/* }}} */
1247
1248ZEND_API int zend_execute_scripts(int type, zval *retval, int file_count, ...) /* {{{ */
1249{
1250    va_list files;
1251    int i;
1252    zend_file_handle *file_handle;
1253    zend_op_array *op_array;
1254
1255    va_start(files, file_count);
1256    for (i = 0; i < file_count; i++) {
1257        file_handle = va_arg(files, zend_file_handle *);
1258        if (!file_handle) {
1259            continue;
1260        }
1261
1262        op_array = zend_compile_file(file_handle, type);
1263        if (file_handle->opened_path) {
1264            zend_hash_str_add_empty_element(&EG(included_files), file_handle->opened_path, strlen(file_handle->opened_path));
1265        }
1266        zend_destroy_file_handle(file_handle);
1267        if (op_array) {
1268            zend_execute(op_array, retval);
1269            zend_exception_restore();
1270            if (EG(exception)) {
1271                if (Z_TYPE(EG(user_exception_handler)) != IS_UNDEF) {
1272                    zval orig_user_exception_handler;
1273                    zval params[1], retval2;
1274                    zend_object *old_exception;
1275                    old_exception = EG(exception);
1276                    EG(exception) = NULL;
1277                    ZVAL_OBJ(&params[0], old_exception);
1278                    ZVAL_COPY_VALUE(&orig_user_exception_handler, &EG(user_exception_handler));
1279                    ZVAL_UNDEF(&retval2);
1280                    if (call_user_function_ex(CG(function_table), NULL, &orig_user_exception_handler, &retval2, 1, params, 1, NULL) == SUCCESS) {
1281                        zval_ptr_dtor(&retval2);
1282                        if (EG(exception)) {
1283                            OBJ_RELEASE(EG(exception));
1284                            EG(exception) = NULL;
1285                        }
1286                        OBJ_RELEASE(old_exception);
1287                    } else {
1288                        EG(exception) = old_exception;
1289                        zend_exception_error(EG(exception), E_ERROR);
1290                    }
1291                } else {
1292                    zend_exception_error(EG(exception), E_ERROR);
1293                }
1294            }
1295            destroy_op_array(op_array);
1296            efree_size(op_array, sizeof(zend_op_array));
1297        } else if (type==ZEND_REQUIRE) {
1298            va_end(files);
1299            return FAILURE;
1300        }
1301    }
1302    va_end(files);
1303
1304    return SUCCESS;
1305}
1306/* }}} */
1307
1308#define COMPILED_STRING_DESCRIPTION_FORMAT "%s(%d) : %s"
1309
1310ZEND_API char *zend_make_compiled_string_description(const char *name) /* {{{ */
1311{
1312    const char *cur_filename;
1313    int cur_lineno;
1314    char *compiled_string_description;
1315
1316    if (zend_is_compiling()) {
1317        cur_filename = zend_get_compiled_filename()->val;
1318        cur_lineno = zend_get_compiled_lineno();
1319    } else if (zend_is_executing()) {
1320        cur_filename = zend_get_executed_filename();
1321        cur_lineno = zend_get_executed_lineno();
1322    } else {
1323        cur_filename = "Unknown";
1324        cur_lineno = 0;
1325    }
1326
1327    zend_spprintf(&compiled_string_description, 0, COMPILED_STRING_DESCRIPTION_FORMAT, cur_filename, cur_lineno, name);
1328    return compiled_string_description;
1329}
1330/* }}} */
1331
1332void free_estring(char **str_p) /* {{{ */
1333{
1334    efree(*str_p);
1335}
1336/* }}} */
1337
1338void free_string_zval(zval *zv) /* {{{ */
1339{
1340    zend_string *str = Z_PTR_P(zv);
1341    zend_string_release(str);
1342}
1343/* }}} */
1344
1345/*
1346 * Local variables:
1347 * tab-width: 4
1348 * c-basic-offset: 4
1349 * indent-tabs-mode: t
1350 * End:
1351 */
1352