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