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