1/*
2   +----------------------------------------------------------------------+
3   | PHP Version 7                                                        |
4   +----------------------------------------------------------------------+
5   | Copyright (c) 1997-2015 The PHP Group                                |
6   +----------------------------------------------------------------------+
7   | This source file is subject to version 3.01 of the PHP 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.php.net/license/3_01.txt                                  |
11   | If you did not receive a copy of the PHP license and are unable to   |
12   | obtain it through the world-wide-web, please send a note to          |
13   | license@php.net so we can mail you a copy immediately.               |
14   +----------------------------------------------------------------------+
15   | Author: Edin Kadribasic <edink@php.net>                              |
16   |         Marcus Boerger <helly@php.net>                               |
17   |         Johannes Schlueter <johannes@php.net>                        |
18   |         Parts based on CGI SAPI Module by                            |
19   |         Rasmus Lerdorf, Stig Bakken and Zeev Suraski                 |
20   +----------------------------------------------------------------------+
21*/
22
23/* $Id$ */
24
25#include "php.h"
26#include "php_globals.h"
27#include "php_variables.h"
28#include "zend_hash.h"
29#include "zend_modules.h"
30#include "zend_interfaces.h"
31
32#include "ext/reflection/php_reflection.h"
33
34#include "SAPI.h"
35
36#include <stdio.h>
37#include "php.h"
38#ifdef PHP_WIN32
39#include "win32/time.h"
40#include "win32/signal.h"
41#include <process.h>
42#endif
43#if HAVE_SYS_TIME_H
44#include <sys/time.h>
45#endif
46#if HAVE_UNISTD_H
47#include <unistd.h>
48#endif
49#if HAVE_SIGNAL_H
50#include <signal.h>
51#endif
52#if HAVE_SETLOCALE
53#include <locale.h>
54#endif
55#include "zend.h"
56#include "zend_extensions.h"
57#include "php_ini.h"
58#include "php_globals.h"
59#include "php_main.h"
60#include "fopen_wrappers.h"
61#include "ext/standard/php_standard.h"
62#include "cli.h"
63#ifdef PHP_WIN32
64#include <io.h>
65#include <fcntl.h>
66#include "win32/php_registry.h"
67#endif
68
69#if HAVE_SIGNAL_H
70#include <signal.h>
71#endif
72
73#ifdef __riscos__
74#include <unixlib/local.h>
75#endif
76
77#include "zend_compile.h"
78#include "zend_execute.h"
79#include "zend_highlight.h"
80#include "zend_exceptions.h"
81
82#include "php_getopt.h"
83
84#ifndef PHP_CLI_WIN32_NO_CONSOLE
85#include "php_cli_server.h"
86#endif
87
88#include "ps_title.h"
89#include "php_cli_process_title.h"
90
91#ifndef PHP_WIN32
92# define php_select(m, r, w, e, t)  select(m, r, w, e, t)
93#else
94# include "win32/select.h"
95#endif
96
97PHPAPI extern char *php_ini_opened_path;
98PHPAPI extern char *php_ini_scanned_path;
99PHPAPI extern char *php_ini_scanned_files;
100
101#if defined(PHP_WIN32) && defined(ZTS)
102ZEND_TSRMLS_CACHE_DEFINE();
103#endif
104
105#ifndef O_BINARY
106#define O_BINARY 0
107#endif
108
109#define PHP_MODE_STANDARD      1
110#define PHP_MODE_HIGHLIGHT     2
111#define PHP_MODE_LINT          4
112#define PHP_MODE_STRIP         5
113#define PHP_MODE_CLI_DIRECT    6
114#define PHP_MODE_PROCESS_STDIN 7
115#define PHP_MODE_REFLECTION_FUNCTION    8
116#define PHP_MODE_REFLECTION_CLASS       9
117#define PHP_MODE_REFLECTION_EXTENSION   10
118#define PHP_MODE_REFLECTION_EXT_INFO    11
119#define PHP_MODE_REFLECTION_ZEND_EXTENSION 12
120#define PHP_MODE_SHOW_INI_CONFIG        13
121
122cli_shell_callbacks_t cli_shell_callbacks = { NULL, NULL, NULL };
123PHP_CLI_API cli_shell_callbacks_t *php_cli_get_shell_callbacks()
124{
125    return &cli_shell_callbacks;
126}
127
128const char HARDCODED_INI[] =
129    "html_errors=0\n"
130    "register_argc_argv=1\n"
131    "implicit_flush=1\n"
132    "output_buffering=0\n"
133    "max_execution_time=0\n"
134    "max_input_time=-1\n\0";
135
136
137const opt_struct OPTIONS[] = {
138    {'a', 0, "interactive"},
139    {'B', 1, "process-begin"},
140    {'C', 0, "no-chdir"}, /* for compatibility with CGI (do not chdir to script directory) */
141    {'c', 1, "php-ini"},
142    {'d', 1, "define"},
143    {'E', 1, "process-end"},
144    {'e', 0, "profile-info"},
145    {'F', 1, "process-file"},
146    {'f', 1, "file"},
147    {'h', 0, "help"},
148    {'i', 0, "info"},
149    {'l', 0, "syntax-check"},
150    {'m', 0, "modules"},
151    {'n', 0, "no-php-ini"},
152    {'q', 0, "no-header"}, /* for compatibility with CGI (do not generate HTTP headers) */
153    {'R', 1, "process-code"},
154    {'H', 0, "hide-args"},
155    {'r', 1, "run"},
156    {'s', 0, "syntax-highlight"},
157    {'s', 0, "syntax-highlighting"},
158    {'S', 1, "server"},
159    {'t', 1, "docroot"},
160    {'w', 0, "strip"},
161    {'?', 0, "usage"},/* help alias (both '?' and 'usage') */
162    {'v', 0, "version"},
163    {'z', 1, "zend-extension"},
164    {10,  1, "rf"},
165    {10,  1, "rfunction"},
166    {11,  1, "rc"},
167    {11,  1, "rclass"},
168    {12,  1, "re"},
169    {12,  1, "rextension"},
170    {13,  1, "rz"},
171    {13,  1, "rzendextension"},
172    {14,  1, "ri"},
173    {14,  1, "rextinfo"},
174    {15,  0, "ini"},
175    {'-', 0, NULL} /* end of args */
176};
177
178static int print_module_info(zval *element) /* {{{ */
179{
180    zend_module_entry *module = (zend_module_entry*)Z_PTR_P(element);
181    php_printf("%s\n", module->name);
182    return ZEND_HASH_APPLY_KEEP;
183}
184/* }}} */
185
186static int module_name_cmp(const void *a, const void *b) /* {{{ */
187{
188    Bucket *f = (Bucket *) a;
189    Bucket *s = (Bucket *) b;
190
191    return strcasecmp(((zend_module_entry *)Z_PTR(f->val))->name,
192                  ((zend_module_entry *)Z_PTR(s->val))->name);
193}
194/* }}} */
195
196static void print_modules(void) /* {{{ */
197{
198    HashTable sorted_registry;
199
200    zend_hash_init(&sorted_registry, 50, NULL, NULL, 0);
201    zend_hash_copy(&sorted_registry, &module_registry, NULL);
202    zend_hash_sort(&sorted_registry, module_name_cmp, 0);
203    zend_hash_apply(&sorted_registry, print_module_info);
204    zend_hash_destroy(&sorted_registry);
205}
206/* }}} */
207
208static int print_extension_info(zend_extension *ext, void *arg) /* {{{ */
209{
210    php_printf("%s\n", ext->name);
211    return ZEND_HASH_APPLY_KEEP;
212}
213/* }}} */
214
215static int extension_name_cmp(const zend_llist_element **f, const zend_llist_element **s) /* {{{ */
216{
217    return strcmp(((zend_extension *)(*f)->data)->name,
218                  ((zend_extension *)(*s)->data)->name);
219}
220/* }}} */
221
222static void print_extensions(void) /* {{{ */
223{
224    zend_llist sorted_exts;
225
226    zend_llist_copy(&sorted_exts, &zend_extensions);
227    sorted_exts.dtor = NULL;
228    zend_llist_sort(&sorted_exts, extension_name_cmp);
229    zend_llist_apply(&sorted_exts, (llist_apply_func_t) print_extension_info);
230    zend_llist_destroy(&sorted_exts);
231}
232/* }}} */
233
234#ifndef STDOUT_FILENO
235#define STDOUT_FILENO 1
236#endif
237
238static inline int sapi_cli_select(int fd)
239{
240    fd_set wfd, dfd;
241    struct timeval tv;
242    int ret;
243
244    FD_ZERO(&wfd);
245    FD_ZERO(&dfd);
246
247    PHP_SAFE_FD_SET(fd, &wfd);
248
249    tv.tv_sec = (long)FG(default_socket_timeout);
250    tv.tv_usec = 0;
251
252    ret = php_select(fd+1, &dfd, &wfd, &dfd, &tv);
253
254    return ret != -1;
255}
256
257PHP_CLI_API size_t sapi_cli_single_write(const char *str, size_t str_length) /* {{{ */
258{
259#ifdef PHP_WRITE_STDOUT
260    zend_long ret;
261#else
262    size_t ret;
263#endif
264
265    if (cli_shell_callbacks.cli_shell_write) {
266        size_t shell_wrote;
267        shell_wrote = cli_shell_callbacks.cli_shell_write(str, str_length);
268        if (shell_wrote > -1) {
269            return shell_wrote;
270        }
271    }
272
273#ifdef PHP_WRITE_STDOUT
274    do {
275        ret = write(STDOUT_FILENO, str, str_length);
276    } while (ret <= 0 && errno == EAGAIN && sapi_cli_select(STDOUT_FILENO));
277
278    if (ret <= 0) {
279        return 0;
280    }
281
282    return ret;
283#else
284    ret = fwrite(str, 1, MIN(str_length, 16384), stdout);
285    return ret;
286#endif
287}
288/* }}} */
289
290static size_t sapi_cli_ub_write(const char *str, size_t str_length) /* {{{ */
291{
292    const char *ptr = str;
293    size_t remaining = str_length;
294    size_t ret;
295
296    if (!str_length) {
297        return 0;
298    }
299
300    if (cli_shell_callbacks.cli_shell_ub_write) {
301        size_t ub_wrote;
302        ub_wrote = cli_shell_callbacks.cli_shell_ub_write(str, str_length);
303        if (ub_wrote > -1) {
304            return ub_wrote;
305        }
306    }
307
308    while (remaining > 0)
309    {
310        ret = sapi_cli_single_write(ptr, remaining);
311        if (!ret) {
312#ifndef PHP_CLI_WIN32_NO_CONSOLE
313            php_handle_aborted_connection();
314#endif
315            break;
316        }
317        ptr += ret;
318        remaining -= ret;
319    }
320
321    return (ptr - str);
322}
323/* }}} */
324
325static void sapi_cli_flush(void *server_context) /* {{{ */
326{
327    /* Ignore EBADF here, it's caused by the fact that STDIN/STDOUT/STDERR streams
328     * are/could be closed before fflush() is called.
329     */
330    if (fflush(stdout)==EOF && errno!=EBADF) {
331#ifndef PHP_CLI_WIN32_NO_CONSOLE
332        php_handle_aborted_connection();
333#endif
334    }
335}
336/* }}} */
337
338static char *php_self = "";
339static char *script_filename = "";
340
341static void sapi_cli_register_variables(zval *track_vars_array) /* {{{ */
342{
343    size_t len;
344    char   *docroot = "";
345
346    /* In CGI mode, we consider the environment to be a part of the server
347     * variables
348     */
349    php_import_environment_variables(track_vars_array);
350
351    /* Build the special-case PHP_SELF variable for the CLI version */
352    len = strlen(php_self);
353    if (sapi_module.input_filter(PARSE_SERVER, "PHP_SELF", &php_self, len, &len)) {
354        php_register_variable("PHP_SELF", php_self, track_vars_array);
355    }
356    if (sapi_module.input_filter(PARSE_SERVER, "SCRIPT_NAME", &php_self, len, &len)) {
357        php_register_variable("SCRIPT_NAME", php_self, track_vars_array);
358    }
359    /* filenames are empty for stdin */
360    len = strlen(script_filename);
361    if (sapi_module.input_filter(PARSE_SERVER, "SCRIPT_FILENAME", &script_filename, len, &len)) {
362        php_register_variable("SCRIPT_FILENAME", script_filename, track_vars_array);
363    }
364    if (sapi_module.input_filter(PARSE_SERVER, "PATH_TRANSLATED", &script_filename, len, &len)) {
365        php_register_variable("PATH_TRANSLATED", script_filename, track_vars_array);
366    }
367    /* just make it available */
368    len = 0U;
369    if (sapi_module.input_filter(PARSE_SERVER, "DOCUMENT_ROOT", &docroot, len, &len)) {
370        php_register_variable("DOCUMENT_ROOT", docroot, track_vars_array);
371    }
372}
373/* }}} */
374
375static void sapi_cli_log_message(char *message) /* {{{ */
376{
377    fprintf(stderr, "%s\n", message);
378}
379/* }}} */
380
381static int sapi_cli_deactivate(void) /* {{{ */
382{
383    fflush(stdout);
384    if(SG(request_info).argv0) {
385        free(SG(request_info).argv0);
386        SG(request_info).argv0 = NULL;
387    }
388    return SUCCESS;
389}
390/* }}} */
391
392static char* sapi_cli_read_cookies(void) /* {{{ */
393{
394    return NULL;
395}
396/* }}} */
397
398static int sapi_cli_header_handler(sapi_header_struct *h, sapi_header_op_enum op, sapi_headers_struct *s) /* {{{ */
399{
400    return 0;
401}
402/* }}} */
403
404static int sapi_cli_send_headers(sapi_headers_struct *sapi_headers) /* {{{ */
405{
406    /* We do nothing here, this function is needed to prevent that the fallback
407     * header handling is called. */
408    return SAPI_HEADER_SENT_SUCCESSFULLY;
409}
410/* }}} */
411
412static void sapi_cli_send_header(sapi_header_struct *sapi_header, void *server_context) /* {{{ */
413{
414}
415/* }}} */
416
417static int php_cli_startup(sapi_module_struct *sapi_module) /* {{{ */
418{
419    if (php_module_startup(sapi_module, NULL, 0)==FAILURE) {
420        return FAILURE;
421    }
422    return SUCCESS;
423}
424/* }}} */
425
426/* {{{ sapi_cli_ini_defaults */
427
428/* overwriteable ini defaults must be set in sapi_cli_ini_defaults() */
429#define INI_DEFAULT(name,value)\
430    ZVAL_NEW_STR(&tmp, zend_string_init(value, sizeof(value)-1, 1));\
431    zend_hash_str_update(configuration_hash, name, sizeof(name)-1, &tmp);\
432
433static void sapi_cli_ini_defaults(HashTable *configuration_hash)
434{
435    zval tmp;
436    INI_DEFAULT("report_zend_debug", "0");
437    INI_DEFAULT("display_errors", "1");
438}
439/* }}} */
440
441/* {{{ sapi_module_struct cli_sapi_module
442 */
443static sapi_module_struct cli_sapi_module = {
444    "cli",                          /* name */
445    "Command Line Interface",       /* pretty name */
446
447    php_cli_startup,                /* startup */
448    php_module_shutdown_wrapper,    /* shutdown */
449
450    NULL,                           /* activate */
451    sapi_cli_deactivate,            /* deactivate */
452
453    sapi_cli_ub_write,              /* unbuffered write */
454    sapi_cli_flush,                 /* flush */
455    NULL,                           /* get uid */
456    NULL,                           /* getenv */
457
458    php_error,                      /* error handler */
459
460    sapi_cli_header_handler,        /* header handler */
461    sapi_cli_send_headers,          /* send headers handler */
462    sapi_cli_send_header,           /* send header handler */
463
464    NULL,                           /* read POST data */
465    sapi_cli_read_cookies,          /* read Cookies */
466
467    sapi_cli_register_variables,    /* register server variables */
468    sapi_cli_log_message,           /* Log message */
469    NULL,                           /* Get request time */
470    NULL,                           /* Child terminate */
471
472    STANDARD_SAPI_MODULE_PROPERTIES
473};
474/* }}} */
475
476/* {{{ arginfo ext/standard/dl.c */
477ZEND_BEGIN_ARG_INFO(arginfo_dl, 0)
478    ZEND_ARG_INFO(0, extension_filename)
479ZEND_END_ARG_INFO()
480/* }}} */
481
482static const zend_function_entry additional_functions[] = {
483    ZEND_FE(dl, arginfo_dl)
484    PHP_FE(cli_set_process_title,        arginfo_cli_set_process_title)
485    PHP_FE(cli_get_process_title,        arginfo_cli_get_process_title)
486    {NULL, NULL, NULL}
487};
488
489/* {{{ php_cli_usage
490 */
491static void php_cli_usage(char *argv0)
492{
493    char *prog;
494
495    prog = strrchr(argv0, '/');
496    if (prog) {
497        prog++;
498    } else {
499        prog = "php";
500    }
501
502    printf( "Usage: %s [options] [-f] <file> [--] [args...]\n"
503                "   %s [options] -r <code> [--] [args...]\n"
504                "   %s [options] [-B <begin_code>] -R <code> [-E <end_code>] [--] [args...]\n"
505                "   %s [options] [-B <begin_code>] -F <file> [-E <end_code>] [--] [args...]\n"
506                "   %s [options] -S <addr>:<port> [-t docroot]\n"
507                "   %s [options] -- [args...]\n"
508                "   %s [options] -a\n"
509                "\n"
510#if (HAVE_LIBREADLINE || HAVE_LIBEDIT) && !defined(COMPILE_DL_READLINE)
511                "  -a               Run as interactive shell\n"
512#else
513                "  -a               Run interactively\n"
514#endif
515                "  -c <path>|<file> Look for php.ini file in this directory\n"
516                "  -n               No php.ini file will be used\n"
517                "  -d foo[=bar]     Define INI entry foo with value 'bar'\n"
518                "  -e               Generate extended information for debugger/profiler\n"
519                "  -f <file>        Parse and execute <file>.\n"
520                "  -h               This help\n"
521                "  -i               PHP information\n"
522                "  -l               Syntax check only (lint)\n"
523                "  -m               Show compiled in modules\n"
524                "  -r <code>        Run PHP <code> without using script tags <?..?>\n"
525                "  -B <begin_code>  Run PHP <begin_code> before processing input lines\n"
526                "  -R <code>        Run PHP <code> for every input line\n"
527                "  -F <file>        Parse and execute <file> for every input line\n"
528                "  -E <end_code>    Run PHP <end_code> after processing all input lines\n"
529                "  -H               Hide any passed arguments from external tools.\n"
530                "  -S <addr>:<port> Run with built-in web server.\n"
531                "  -t <docroot>     Specify document root <docroot> for built-in web server.\n"
532                "  -s               Output HTML syntax highlighted source.\n"
533                "  -v               Version number\n"
534                "  -w               Output source with stripped comments and whitespace.\n"
535                "  -z <file>        Load Zend extension <file>.\n"
536                "\n"
537                "  args...          Arguments passed to script. Use -- args when first argument\n"
538                "                   starts with - or script is read from stdin\n"
539                "\n"
540                "  --ini            Show configuration file names\n"
541                "\n"
542                "  --rf <name>      Show information about function <name>.\n"
543                "  --rc <name>      Show information about class <name>.\n"
544                "  --re <name>      Show information about extension <name>.\n"
545                "  --rz <name>      Show information about Zend extension <name>.\n"
546                "  --ri <name>      Show configuration for extension <name>.\n"
547                "\n"
548                , prog, prog, prog, prog, prog, prog, prog);
549}
550/* }}} */
551
552static php_stream *s_in_process = NULL;
553
554static void cli_register_file_handles(void) /* {{{ */
555{
556    zval zin, zout, zerr;
557    php_stream *s_in, *s_out, *s_err;
558    php_stream_context *sc_in=NULL, *sc_out=NULL, *sc_err=NULL;
559    zend_constant ic, oc, ec;
560
561    s_in  = php_stream_open_wrapper_ex("php://stdin",  "rb", 0, NULL, sc_in);
562    s_out = php_stream_open_wrapper_ex("php://stdout", "wb", 0, NULL, sc_out);
563    s_err = php_stream_open_wrapper_ex("php://stderr", "wb", 0, NULL, sc_err);
564
565    if (s_in==NULL || s_out==NULL || s_err==NULL) {
566        if (s_in) php_stream_close(s_in);
567        if (s_out) php_stream_close(s_out);
568        if (s_err) php_stream_close(s_err);
569        return;
570    }
571
572#if PHP_DEBUG
573    /* do not close stdout and stderr */
574    s_out->flags |= PHP_STREAM_FLAG_NO_CLOSE;
575    s_err->flags |= PHP_STREAM_FLAG_NO_CLOSE;
576#endif
577
578    s_in_process = s_in;
579
580    php_stream_to_zval(s_in,  &zin);
581    php_stream_to_zval(s_out, &zout);
582    php_stream_to_zval(s_err, &zerr);
583
584    ZVAL_COPY_VALUE(&ic.value, &zin);
585    ic.flags = CONST_CS;
586    ic.name = zend_string_init("STDIN", sizeof("STDIN")-1, 1);
587    ic.module_number = 0;
588    zend_register_constant(&ic);
589
590    ZVAL_COPY_VALUE(&oc.value, &zout);
591    oc.flags = CONST_CS;
592    oc.name = zend_string_init("STDOUT", sizeof("STDOUT")-1, 1);
593    oc.module_number = 0;
594    zend_register_constant(&oc);
595
596    ZVAL_COPY_VALUE(&ec.value, &zerr);
597    ec.flags = CONST_CS;
598    ec.name = zend_string_init("STDERR", sizeof("STDERR")-1, 1);
599    ec.module_number = 0;
600    zend_register_constant(&ec);
601}
602/* }}} */
603
604static const char *param_mode_conflict = "Either execute direct code, process stdin or use a file.\n";
605
606/* {{{ cli_seek_file_begin
607 */
608static int cli_seek_file_begin(zend_file_handle *file_handle, char *script_file, int *lineno)
609{
610    int c;
611
612    *lineno = 1;
613
614    file_handle->type = ZEND_HANDLE_FP;
615    file_handle->opened_path = NULL;
616    file_handle->free_filename = 0;
617    if (!(file_handle->handle.fp = VCWD_FOPEN(script_file, "rb"))) {
618        php_printf("Could not open input file: %s\n", script_file);
619        return FAILURE;
620    }
621    file_handle->filename = script_file;
622
623    /* #!php support */
624    c = fgetc(file_handle->handle.fp);
625    if (c == '#' && (c = fgetc(file_handle->handle.fp)) == '!') {
626        while (c != '\n' && c != '\r' && c != EOF) {
627            c = fgetc(file_handle->handle.fp);  /* skip to end of line */
628        }
629        /* handle situations where line is terminated by \r\n */
630        if (c == '\r') {
631            if (fgetc(file_handle->handle.fp) != '\n') {
632                zend_long pos = zend_ftell(file_handle->handle.fp);
633                zend_fseek(file_handle->handle.fp, pos - 1, SEEK_SET);
634            }
635        }
636        *lineno = 2;
637    } else {
638        rewind(file_handle->handle.fp);
639    }
640
641    return SUCCESS;
642}
643/* }}} */
644
645static int do_cli(int argc, char **argv) /* {{{ */
646{
647    int c;
648    zend_file_handle file_handle;
649    int behavior = PHP_MODE_STANDARD;
650    char *reflection_what = NULL;
651    volatile int request_started = 0;
652    volatile int exit_status = 0;
653    char *php_optarg = NULL, *orig_optarg = NULL;
654    int php_optind = 1, orig_optind = 1;
655    char *exec_direct=NULL, *exec_run=NULL, *exec_begin=NULL, *exec_end=NULL;
656    char *arg_free=NULL, **arg_excp=&arg_free;
657    char *script_file=NULL, *translated_path = NULL;
658    int interactive=0;
659    int lineno = 0;
660    const char *param_error=NULL;
661    int hide_argv = 0;
662
663    zend_try {
664
665        CG(in_compilation) = 0; /* not initialized but needed for several options */
666
667        while ((c = php_getopt(argc, argv, OPTIONS, &php_optarg, &php_optind, 0, 2)) != -1) {
668            switch (c) {
669
670            case 'i': /* php info & quit */
671                if (php_request_startup()==FAILURE) {
672                    goto err;
673                }
674                request_started = 1;
675                php_print_info(0xFFFFFFFF);
676                php_output_end_all();
677                exit_status = (c == '?' && argc > 1 && !strchr(argv[1],  c));
678                goto out;
679
680            case 'v': /* show php version & quit */
681                php_printf("PHP %s (%s) (built: %s %s) %s\nCopyright (c) 1997-2015 The PHP Group\n%s",
682                    PHP_VERSION, cli_sapi_module.name, __DATE__, __TIME__,
683#if ZEND_DEBUG && defined(HAVE_GCOV)
684                    "(DEBUG GCOV)",
685#elif ZEND_DEBUG
686                    "(DEBUG)",
687#elif defined(HAVE_GCOV)
688                    "(GCOV)",
689#else
690                    "",
691#endif
692                    get_zend_version()
693                );
694                sapi_deactivate();
695                goto out;
696
697            case 'm': /* list compiled in modules */
698                if (php_request_startup()==FAILURE) {
699                    goto err;
700                }
701                request_started = 1;
702                php_printf("[PHP Modules]\n");
703                print_modules();
704                php_printf("\n[Zend Modules]\n");
705                print_extensions();
706                php_printf("\n");
707                php_output_end_all();
708                exit_status=0;
709                goto out;
710
711            default:
712                break;
713            }
714        }
715
716        /* Set some CLI defaults */
717        SG(options) |= SAPI_OPTION_NO_CHDIR;
718
719        php_optind = orig_optind;
720        php_optarg = orig_optarg;
721        while ((c = php_getopt(argc, argv, OPTIONS, &php_optarg, &php_optind, 0, 2)) != -1) {
722            switch (c) {
723
724            case 'a':   /* interactive mode */
725                if (!interactive) {
726                    if (behavior != PHP_MODE_STANDARD) {
727                        param_error = param_mode_conflict;
728                        break;
729                    }
730
731                    interactive=1;
732                }
733                break;
734
735            case 'C': /* don't chdir to the script directory */
736                /* This is default so NOP */
737                break;
738
739            case 'F':
740                if (behavior == PHP_MODE_PROCESS_STDIN) {
741                    if (exec_run || script_file) {
742                        param_error = "You can use -R or -F only once.\n";
743                        break;
744                    }
745                } else if (behavior != PHP_MODE_STANDARD) {
746                    param_error = param_mode_conflict;
747                    break;
748                }
749                behavior=PHP_MODE_PROCESS_STDIN;
750                script_file = php_optarg;
751                break;
752
753            case 'f': /* parse file */
754                if (behavior == PHP_MODE_CLI_DIRECT || behavior == PHP_MODE_PROCESS_STDIN) {
755                    param_error = param_mode_conflict;
756                    break;
757                } else if (script_file) {
758                    param_error = "You can use -f only once.\n";
759                    break;
760                }
761                script_file = php_optarg;
762                break;
763
764            case 'l': /* syntax check mode */
765                if (behavior != PHP_MODE_STANDARD) {
766                    break;
767                }
768                behavior=PHP_MODE_LINT;
769                break;
770
771            case 'q': /* do not generate HTTP headers */
772                /* This is default so NOP */
773                break;
774
775            case 'r': /* run code from command line */
776                if (behavior == PHP_MODE_CLI_DIRECT) {
777                    if (exec_direct || script_file) {
778                        param_error = "You can use -r only once.\n";
779                        break;
780                    }
781                } else if (behavior != PHP_MODE_STANDARD || interactive) {
782                    param_error = param_mode_conflict;
783                    break;
784                }
785                behavior=PHP_MODE_CLI_DIRECT;
786                exec_direct=php_optarg;
787                break;
788
789            case 'R':
790                if (behavior == PHP_MODE_PROCESS_STDIN) {
791                    if (exec_run || script_file) {
792                        param_error = "You can use -R or -F only once.\n";
793                        break;
794                    }
795                } else if (behavior != PHP_MODE_STANDARD) {
796                    param_error = param_mode_conflict;
797                    break;
798                }
799                behavior=PHP_MODE_PROCESS_STDIN;
800                exec_run=php_optarg;
801                break;
802
803            case 'B':
804                if (behavior == PHP_MODE_PROCESS_STDIN) {
805                    if (exec_begin) {
806                        param_error = "You can use -B only once.\n";
807                        break;
808                    }
809                } else if (behavior != PHP_MODE_STANDARD || interactive) {
810                    param_error = param_mode_conflict;
811                    break;
812                }
813                behavior=PHP_MODE_PROCESS_STDIN;
814                exec_begin=php_optarg;
815                break;
816
817            case 'E':
818                if (behavior == PHP_MODE_PROCESS_STDIN) {
819                    if (exec_end) {
820                        param_error = "You can use -E only once.\n";
821                        break;
822                    }
823                } else if (behavior != PHP_MODE_STANDARD || interactive) {
824                    param_error = param_mode_conflict;
825                    break;
826                }
827                behavior=PHP_MODE_PROCESS_STDIN;
828                exec_end=php_optarg;
829                break;
830
831            case 's': /* generate highlighted HTML from source */
832                if (behavior == PHP_MODE_CLI_DIRECT || behavior == PHP_MODE_PROCESS_STDIN) {
833                    param_error = "Source highlighting only works for files.\n";
834                    break;
835                }
836                behavior=PHP_MODE_HIGHLIGHT;
837                break;
838
839            case 'w':
840                if (behavior == PHP_MODE_CLI_DIRECT || behavior == PHP_MODE_PROCESS_STDIN) {
841                    param_error = "Source stripping only works for files.\n";
842                    break;
843                }
844                behavior=PHP_MODE_STRIP;
845                break;
846
847            case 'z': /* load extension file */
848                zend_load_extension(php_optarg);
849                break;
850            case 'H':
851                hide_argv = 1;
852                break;
853            case 10:
854                behavior=PHP_MODE_REFLECTION_FUNCTION;
855                reflection_what = php_optarg;
856                break;
857            case 11:
858                behavior=PHP_MODE_REFLECTION_CLASS;
859                reflection_what = php_optarg;
860                break;
861            case 12:
862                behavior=PHP_MODE_REFLECTION_EXTENSION;
863                reflection_what = php_optarg;
864                break;
865            case 13:
866                behavior=PHP_MODE_REFLECTION_ZEND_EXTENSION;
867                reflection_what = php_optarg;
868                break;
869            case 14:
870                behavior=PHP_MODE_REFLECTION_EXT_INFO;
871                reflection_what = php_optarg;
872                break;
873            case 15:
874                behavior = PHP_MODE_SHOW_INI_CONFIG;
875                break;
876            default:
877                break;
878            }
879        }
880
881        if (param_error) {
882            PUTS(param_error);
883            exit_status=1;
884            goto err;
885        }
886
887        if (interactive) {
888#if (HAVE_LIBREADLINE || HAVE_LIBEDIT) && !defined(COMPILE_DL_READLINE)
889            printf("Interactive shell\n\n");
890#else
891            printf("Interactive mode enabled\n\n");
892#endif
893            fflush(stdout);
894        }
895
896        /* only set script_file if not set already and not in direct mode and not at end of parameter list */
897        if (argc > php_optind
898          && !script_file
899          && behavior!=PHP_MODE_CLI_DIRECT
900          && behavior!=PHP_MODE_PROCESS_STDIN
901          && strcmp(argv[php_optind-1],"--"))
902        {
903            script_file=argv[php_optind];
904            php_optind++;
905        }
906        if (script_file) {
907            if (cli_seek_file_begin(&file_handle, script_file, &lineno) != SUCCESS) {
908                goto err;
909            } else {
910                char real_path[MAXPATHLEN];
911                if (VCWD_REALPATH(script_file, real_path)) {
912                    translated_path = strdup(real_path);
913                }
914                script_filename = script_file;
915            }
916        } else {
917            /* We could handle PHP_MODE_PROCESS_STDIN in a different manner  */
918            /* here but this would make things only more complicated. And it */
919            /* is consitent with the way -R works where the stdin file handle*/
920            /* is also accessible. */
921            file_handle.filename = "-";
922            file_handle.handle.fp = stdin;
923        }
924        file_handle.type = ZEND_HANDLE_FP;
925        file_handle.opened_path = NULL;
926        file_handle.free_filename = 0;
927        php_self = (char*)file_handle.filename;
928
929        /* before registering argv to module exchange the *new* argv[0] */
930        /* we can achieve this without allocating more memory */
931        SG(request_info).argc=argc-php_optind+1;
932        arg_excp = argv+php_optind-1;
933        arg_free = argv[php_optind-1];
934        SG(request_info).path_translated = translated_path? translated_path: (char*)file_handle.filename;
935        argv[php_optind-1] = (char*)file_handle.filename;
936        SG(request_info).argv=argv+php_optind-1;
937
938        if (php_request_startup()==FAILURE) {
939            *arg_excp = arg_free;
940            fclose(file_handle.handle.fp);
941            PUTS("Could not startup.\n");
942            goto err;
943        }
944        request_started = 1;
945        CG(start_lineno) = lineno;
946        *arg_excp = arg_free; /* reconstuct argv */
947
948        if (hide_argv) {
949            int i;
950            for (i = 1; i < argc; i++) {
951                memset(argv[i], 0, strlen(argv[i]));
952            }
953        }
954
955        zend_is_auto_global_str(ZEND_STRL("_SERVER"));
956
957        PG(during_request_startup) = 0;
958        switch (behavior) {
959        case PHP_MODE_STANDARD:
960            if (strcmp(file_handle.filename, "-")) {
961                cli_register_file_handles();
962            }
963
964            if (interactive && cli_shell_callbacks.cli_shell_run) {
965                exit_status = cli_shell_callbacks.cli_shell_run();
966            } else {
967                php_execute_script(&file_handle);
968                exit_status = EG(exit_status);
969            }
970            break;
971        case PHP_MODE_LINT:
972            exit_status = php_lint_script(&file_handle);
973            if (exit_status==SUCCESS) {
974                zend_printf("No syntax errors detected in %s\n", file_handle.filename);
975            } else {
976                zend_printf("Errors parsing %s\n", file_handle.filename);
977            }
978            break;
979        case PHP_MODE_STRIP:
980            if (open_file_for_scanning(&file_handle)==SUCCESS) {
981                zend_strip();
982            }
983            goto out;
984            break;
985        case PHP_MODE_HIGHLIGHT:
986            {
987                zend_syntax_highlighter_ini syntax_highlighter_ini;
988
989                if (open_file_for_scanning(&file_handle)==SUCCESS) {
990                    php_get_highlight_struct(&syntax_highlighter_ini);
991                    zend_highlight(&syntax_highlighter_ini);
992                }
993                goto out;
994            }
995            break;
996        case PHP_MODE_CLI_DIRECT:
997            cli_register_file_handles();
998            if (zend_eval_string_ex(exec_direct, NULL, "Command line code", 1) == FAILURE) {
999                exit_status=254;
1000            }
1001            break;
1002
1003        case PHP_MODE_PROCESS_STDIN:
1004            {
1005                char *input;
1006                size_t len, index = 0;
1007                zval argn, argi;
1008
1009                cli_register_file_handles();
1010
1011                if (exec_begin && zend_eval_string_ex(exec_begin, NULL, "Command line begin code", 1) == FAILURE) {
1012                    exit_status=254;
1013                }
1014                ZVAL_LONG(&argi, index);
1015                zend_hash_str_update(&EG(symbol_table), "argi", sizeof("argi")-1, &argi);
1016                while (exit_status == SUCCESS && (input=php_stream_gets(s_in_process, NULL, 0)) != NULL) {
1017                    len = strlen(input);
1018                    while (len > 0 && len-- && (input[len]=='\n' || input[len]=='\r')) {
1019                        input[len] = '\0';
1020                    }
1021                    ZVAL_STRINGL(&argn, input, len);
1022                    zend_hash_str_update(&EG(symbol_table), "argn", sizeof("argn")-1, &argn);
1023                    Z_LVAL(argi) = ++index;
1024                    if (exec_run) {
1025                        if (zend_eval_string_ex(exec_run, NULL, "Command line run code", 1) == FAILURE) {
1026                            exit_status=254;
1027                        }
1028                    } else {
1029                        if (script_file) {
1030                            if (cli_seek_file_begin(&file_handle, script_file, &lineno) != SUCCESS) {
1031                                exit_status = 1;
1032                            } else {
1033                                CG(start_lineno) = lineno;
1034                                php_execute_script(&file_handle);
1035                                exit_status = EG(exit_status);
1036                            }
1037                        }
1038                    }
1039                    efree(input);
1040                }
1041                if (exec_end && zend_eval_string_ex(exec_end, NULL, "Command line end code", 1) == FAILURE) {
1042                    exit_status=254;
1043                }
1044
1045                break;
1046            }
1047
1048            case PHP_MODE_REFLECTION_FUNCTION:
1049            case PHP_MODE_REFLECTION_CLASS:
1050            case PHP_MODE_REFLECTION_EXTENSION:
1051            case PHP_MODE_REFLECTION_ZEND_EXTENSION:
1052                {
1053                    zend_class_entry *pce = NULL;
1054                    zval arg, ref;
1055                    zend_execute_data execute_data;
1056
1057                    switch (behavior) {
1058                        default:
1059                            break;
1060                        case PHP_MODE_REFLECTION_FUNCTION:
1061                            if (strstr(reflection_what, "::")) {
1062                                pce = reflection_method_ptr;
1063                            } else {
1064                                pce = reflection_function_ptr;
1065                            }
1066                            break;
1067                        case PHP_MODE_REFLECTION_CLASS:
1068                            pce = reflection_class_ptr;
1069                            break;
1070                        case PHP_MODE_REFLECTION_EXTENSION:
1071                            pce = reflection_extension_ptr;
1072                            break;
1073                        case PHP_MODE_REFLECTION_ZEND_EXTENSION:
1074                            pce = reflection_zend_extension_ptr;
1075                            break;
1076                    }
1077
1078                    ZVAL_STRING(&arg, reflection_what);
1079                    object_init_ex(&ref, pce);
1080
1081                    memset(&execute_data, 0, sizeof(zend_execute_data));
1082                    EG(current_execute_data) = &execute_data;
1083                    zend_call_method_with_1_params(&ref, pce, &pce->constructor, "__construct", NULL, &arg);
1084
1085                    if (EG(exception)) {
1086                        zval tmp, *msg, rv;
1087
1088                        ZVAL_OBJ(&tmp, EG(exception));
1089                        msg = zend_read_property(zend_exception_get_default(), &tmp, "message", sizeof("message")-1, 0, &rv);
1090                        zend_printf("Exception: %s\n", Z_STRVAL_P(msg));
1091                        zval_ptr_dtor(&tmp);
1092                        EG(exception) = NULL;
1093                    } else {
1094                        zend_call_method_with_1_params(NULL, reflection_ptr, NULL, "export", NULL, &ref);
1095                    }
1096                    zval_ptr_dtor(&ref);
1097                    zval_ptr_dtor(&arg);
1098
1099                    break;
1100                }
1101            case PHP_MODE_REFLECTION_EXT_INFO:
1102                {
1103                    int len = (int)strlen(reflection_what);
1104                    char *lcname = zend_str_tolower_dup(reflection_what, len);
1105                    zend_module_entry *module;
1106
1107                    if ((module = zend_hash_str_find_ptr(&module_registry, lcname, len)) == NULL) {
1108                        if (!strcmp(reflection_what, "main")) {
1109                            display_ini_entries(NULL);
1110                        } else {
1111                            zend_printf("Extension '%s' not present.\n", reflection_what);
1112                            exit_status = 1;
1113                        }
1114                    } else {
1115                        php_info_print_module(module);
1116                    }
1117
1118                    efree(lcname);
1119                    break;
1120                }
1121
1122            case PHP_MODE_SHOW_INI_CONFIG:
1123                {
1124                    zend_printf("Configuration File (php.ini) Path: %s\n", PHP_CONFIG_FILE_PATH);
1125                    zend_printf("Loaded Configuration File:         %s\n", php_ini_opened_path ? php_ini_opened_path : "(none)");
1126                    zend_printf("Scan for additional .ini files in: %s\n", php_ini_scanned_path  ? php_ini_scanned_path : "(none)");
1127                    zend_printf("Additional .ini files parsed:      %s\n", php_ini_scanned_files ? php_ini_scanned_files : "(none)");
1128                    break;
1129                }
1130        }
1131    } zend_end_try();
1132
1133out:
1134    if (request_started) {
1135        php_request_shutdown((void *) 0);
1136    }
1137    if (translated_path) {
1138        free(translated_path);
1139    }
1140    if (exit_status == 0) {
1141        exit_status = EG(exit_status);
1142    }
1143    return exit_status;
1144err:
1145    sapi_deactivate();
1146    zend_ini_deactivate();
1147    exit_status = 1;
1148    goto out;
1149}
1150/* }}} */
1151
1152/* {{{ main
1153 */
1154#ifdef PHP_CLI_WIN32_NO_CONSOLE
1155int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)
1156#else
1157int main(int argc, char *argv[])
1158#endif
1159{
1160#ifdef PHP_CLI_WIN32_NO_CONSOLE
1161    int argc = __argc;
1162    char **argv = __argv;
1163#endif
1164
1165    int c;
1166    int exit_status = SUCCESS;
1167    int module_started = 0, sapi_started = 0;
1168    char *php_optarg = NULL;
1169    int php_optind = 1, use_extended_info = 0;
1170    char *ini_path_override = NULL;
1171    char *ini_entries = NULL;
1172    int ini_entries_len = 0;
1173    int ini_ignore = 0;
1174    sapi_module_struct *sapi_module = &cli_sapi_module;
1175
1176    /*
1177     * Do not move this initialization. It needs to happen before argv is used
1178     * in any way.
1179     */
1180    argv = save_ps_args(argc, argv);
1181
1182    cli_sapi_module.additional_functions = additional_functions;
1183
1184#if defined(PHP_WIN32) && defined(_DEBUG) && defined(PHP_WIN32_DEBUG_HEAP)
1185    {
1186        int tmp_flag;
1187        _CrtSetReportMode(_CRT_WARN, _CRTDBG_MODE_FILE);
1188        _CrtSetReportFile(_CRT_WARN, _CRTDBG_FILE_STDERR);
1189        _CrtSetReportMode(_CRT_ERROR, _CRTDBG_MODE_FILE);
1190        _CrtSetReportFile(_CRT_ERROR, _CRTDBG_FILE_STDERR);
1191        _CrtSetReportMode(_CRT_ASSERT, _CRTDBG_MODE_FILE);
1192        _CrtSetReportFile(_CRT_ASSERT, _CRTDBG_FILE_STDERR);
1193        tmp_flag = _CrtSetDbgFlag(_CRTDBG_REPORT_FLAG);
1194        tmp_flag |= _CRTDBG_DELAY_FREE_MEM_DF;
1195        tmp_flag |= _CRTDBG_LEAK_CHECK_DF;
1196
1197        _CrtSetDbgFlag(tmp_flag);
1198    }
1199#endif
1200
1201#ifdef HAVE_SIGNAL_H
1202#if defined(SIGPIPE) && defined(SIG_IGN)
1203    signal(SIGPIPE, SIG_IGN); /* ignore SIGPIPE in standalone mode so
1204                                that sockets created via fsockopen()
1205                                don't kill PHP if the remote site
1206                                closes it.  in apache|apxs mode apache
1207                                does that for us!  thies@thieso.net
1208                                20000419 */
1209#endif
1210#endif
1211
1212
1213#ifdef ZTS
1214    tsrm_startup(1, 1, 0, NULL);
1215    (void)ts_resource(0);
1216    ZEND_TSRMLS_CACHE_UPDATE();
1217#endif
1218
1219#ifdef PHP_WIN32
1220    _fmode = _O_BINARY;         /*sets default for file streams to binary */
1221    setmode(_fileno(stdin), O_BINARY);      /* make the stdio mode be binary */
1222    setmode(_fileno(stdout), O_BINARY);     /* make the stdio mode be binary */
1223    setmode(_fileno(stderr), O_BINARY);     /* make the stdio mode be binary */
1224#endif
1225
1226    while ((c = php_getopt(argc, argv, OPTIONS, &php_optarg, &php_optind, 0, 2))!=-1) {
1227        switch (c) {
1228            case 'c':
1229                if (ini_path_override) {
1230                    free(ini_path_override);
1231                }
1232                ini_path_override = strdup(php_optarg);
1233                break;
1234            case 'n':
1235                ini_ignore = 1;
1236                break;
1237            case 'd': {
1238                /* define ini entries on command line */
1239                int len = (int)strlen(php_optarg);
1240                char *val;
1241
1242                if ((val = strchr(php_optarg, '='))) {
1243                    val++;
1244                    if (!isalnum(*val) && *val != '"' && *val != '\'' && *val != '\0') {
1245                        ini_entries = realloc(ini_entries, ini_entries_len + len + sizeof("\"\"\n\0"));
1246                        memcpy(ini_entries + ini_entries_len, php_optarg, (val - php_optarg));
1247                        ini_entries_len += (int)(val - php_optarg);
1248                        memcpy(ini_entries + ini_entries_len, "\"", 1);
1249                        ini_entries_len++;
1250                        memcpy(ini_entries + ini_entries_len, val, len - (val - php_optarg));
1251                        ini_entries_len += len - (int)(val - php_optarg);
1252                        memcpy(ini_entries + ini_entries_len, "\"\n\0", sizeof("\"\n\0"));
1253                        ini_entries_len += sizeof("\n\0\"") - 2;
1254                    } else {
1255                        ini_entries = realloc(ini_entries, ini_entries_len + len + sizeof("\n\0"));
1256                        memcpy(ini_entries + ini_entries_len, php_optarg, len);
1257                        memcpy(ini_entries + ini_entries_len + len, "\n\0", sizeof("\n\0"));
1258                        ini_entries_len += len + sizeof("\n\0") - 2;
1259                    }
1260                } else {
1261                    ini_entries = realloc(ini_entries, ini_entries_len + len + sizeof("=1\n\0"));
1262                    memcpy(ini_entries + ini_entries_len, php_optarg, len);
1263                    memcpy(ini_entries + ini_entries_len + len, "=1\n\0", sizeof("=1\n\0"));
1264                    ini_entries_len += len + sizeof("=1\n\0") - 2;
1265                }
1266                break;
1267            }
1268#ifndef PHP_CLI_WIN32_NO_CONSOLE
1269            case 'S':
1270                sapi_module = &cli_server_sapi_module;
1271                cli_server_sapi_module.additional_functions = server_additional_functions;
1272                break;
1273#endif
1274            case 'h': /* help & quit */
1275            case '?':
1276                php_cli_usage(argv[0]);
1277                goto out;
1278            case 'i': case 'v': case 'm':
1279                sapi_module = &cli_sapi_module;
1280                goto exit_loop;
1281            case 'e': /* enable extended info output */
1282                use_extended_info = 1;
1283                break;
1284        }
1285    }
1286exit_loop:
1287
1288    sapi_module->ini_defaults = sapi_cli_ini_defaults;
1289    sapi_module->php_ini_path_override = ini_path_override;
1290    sapi_module->phpinfo_as_text = 1;
1291    sapi_module->php_ini_ignore_cwd = 1;
1292    sapi_startup(sapi_module);
1293    sapi_started = 1;
1294
1295    sapi_module->php_ini_ignore = ini_ignore;
1296
1297    sapi_module->executable_location = argv[0];
1298
1299    if (sapi_module == &cli_sapi_module) {
1300        if (ini_entries) {
1301            ini_entries = realloc(ini_entries, ini_entries_len + sizeof(HARDCODED_INI));
1302            memmove(ini_entries + sizeof(HARDCODED_INI) - 2, ini_entries, ini_entries_len + 1);
1303            memcpy(ini_entries, HARDCODED_INI, sizeof(HARDCODED_INI) - 2);
1304        } else {
1305            ini_entries = malloc(sizeof(HARDCODED_INI));
1306            memcpy(ini_entries, HARDCODED_INI, sizeof(HARDCODED_INI));
1307        }
1308        ini_entries_len += sizeof(HARDCODED_INI) - 2;
1309    }
1310
1311    sapi_module->ini_entries = ini_entries;
1312
1313    /* startup after we get the above ini override se we get things right */
1314    if (sapi_module->startup(sapi_module) == FAILURE) {
1315        /* there is no way to see if we must call zend_ini_deactivate()
1316         * since we cannot check if EG(ini_directives) has been initialised
1317         * because the executor's constructor does not set initialize it.
1318         * Apart from that there seems no need for zend_ini_deactivate() yet.
1319         * So we goto out_err.*/
1320        exit_status = 1;
1321        goto out;
1322    }
1323    module_started = 1;
1324
1325    /* -e option */
1326    if (use_extended_info) {
1327        CG(compiler_options) |= ZEND_COMPILE_EXTENDED_INFO;
1328    }
1329
1330    zend_first_try {
1331#ifndef PHP_CLI_WIN32_NO_CONSOLE
1332        if (sapi_module == &cli_sapi_module) {
1333#endif
1334            exit_status = do_cli(argc, argv);
1335#ifndef PHP_CLI_WIN32_NO_CONSOLE
1336        } else {
1337            exit_status = do_cli_server(argc, argv);
1338        }
1339#endif
1340    } zend_end_try();
1341out:
1342    if (ini_path_override) {
1343        free(ini_path_override);
1344    }
1345    if (ini_entries) {
1346        free(ini_entries);
1347    }
1348    if (module_started) {
1349        php_module_shutdown();
1350    }
1351    if (sapi_started) {
1352        sapi_shutdown();
1353    }
1354#ifdef ZTS
1355    tsrm_shutdown();
1356#endif
1357
1358    /*
1359     * Do not move this de-initialization. It needs to happen right before
1360     * exiting.
1361     */
1362    cleanup_ps_args(argv);
1363    exit(exit_status);
1364}
1365/* }}} */
1366
1367/*
1368 * Local variables:
1369 * tab-width: 4
1370 * c-basic-offset: 4
1371 * End:
1372 * vim600: sw=4 ts=4 fdm=marker
1373 * vim<600: sw=4 ts=4
1374 */
1375