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_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, 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
665    zend_try {
666
667        CG(in_compilation) = 0; /* not initialized but needed for several options */
668
669        while ((c = php_getopt(argc, argv, OPTIONS, &php_optarg, &php_optind, 0, 2)) != -1) {
670            switch (c) {
671
672            case 'i': /* php info & quit */
673                if (php_request_startup()==FAILURE) {
674                    goto err;
675                }
676                request_started = 1;
677                php_print_info(0xFFFFFFFF);
678                php_output_end_all();
679                exit_status = (c == '?' && argc > 1 && !strchr(argv[1],  c));
680                goto out;
681
682            case 'v': /* show php version & quit */
683                php_printf("PHP %s (%s) (built: %s %s) %s\nCopyright (c) 1997-2015 The PHP Group\n%s",
684                    PHP_VERSION, cli_sapi_module.name, __DATE__, __TIME__,
685#if ZEND_DEBUG && defined(HAVE_GCOV)
686                    "(DEBUG GCOV)",
687#elif ZEND_DEBUG
688                    "(DEBUG)",
689#elif defined(HAVE_GCOV)
690                    "(GCOV)",
691#else
692                    "",
693#endif
694                    get_zend_version()
695                );
696                sapi_deactivate();
697                goto out;
698
699            case 'm': /* list compiled in modules */
700                if (php_request_startup()==FAILURE) {
701                    goto err;
702                }
703                request_started = 1;
704                php_printf("[PHP Modules]\n");
705                print_modules();
706                php_printf("\n[Zend Modules]\n");
707                print_extensions();
708                php_printf("\n");
709                php_output_end_all();
710                exit_status=0;
711                goto out;
712
713            default:
714                break;
715            }
716        }
717
718        /* Set some CLI defaults */
719        SG(options) |= SAPI_OPTION_NO_CHDIR;
720
721        php_optind = orig_optind;
722        php_optarg = orig_optarg;
723        while ((c = php_getopt(argc, argv, OPTIONS, &php_optarg, &php_optind, 0, 2)) != -1) {
724            switch (c) {
725
726            case 'a':   /* interactive mode */
727                if (!interactive) {
728                    if (behavior != PHP_MODE_STANDARD) {
729                        param_error = param_mode_conflict;
730                        break;
731                    }
732
733                    interactive=1;
734                }
735                break;
736
737            case 'C': /* don't chdir to the script directory */
738                /* This is default so NOP */
739                break;
740
741            case 'F':
742                if (behavior == PHP_MODE_PROCESS_STDIN) {
743                    if (exec_run || script_file) {
744                        param_error = "You can use -R or -F only once.\n";
745                        break;
746                    }
747                } else if (behavior != PHP_MODE_STANDARD) {
748                    param_error = param_mode_conflict;
749                    break;
750                }
751                behavior=PHP_MODE_PROCESS_STDIN;
752                script_file = php_optarg;
753                break;
754
755            case 'f': /* parse file */
756                if (behavior == PHP_MODE_CLI_DIRECT || behavior == PHP_MODE_PROCESS_STDIN) {
757                    param_error = param_mode_conflict;
758                    break;
759                } else if (script_file) {
760                    param_error = "You can use -f only once.\n";
761                    break;
762                }
763                script_file = php_optarg;
764                break;
765
766            case 'l': /* syntax check mode */
767                if (behavior != PHP_MODE_STANDARD) {
768                    break;
769                }
770                behavior=PHP_MODE_LINT;
771                break;
772
773#if 0 /* not yet operational, see also below ... */
774            case '': /* generate indented source mode*/
775                if (behavior == PHP_MODE_CLI_DIRECT || behavior == PHP_MODE_PROCESS_STDIN) {
776                    param_error = "Source indenting only works for files.\n";
777                    break;
778                }
779                behavior=PHP_MODE_INDENT;
780                break;
781#endif
782
783            case 'q': /* do not generate HTTP headers */
784                /* This is default so NOP */
785                break;
786
787            case 'r': /* run code from command line */
788                if (behavior == PHP_MODE_CLI_DIRECT) {
789                    if (exec_direct || script_file) {
790                        param_error = "You can use -r only once.\n";
791                        break;
792                    }
793                } else if (behavior != PHP_MODE_STANDARD || interactive) {
794                    param_error = param_mode_conflict;
795                    break;
796                }
797                behavior=PHP_MODE_CLI_DIRECT;
798                exec_direct=php_optarg;
799                break;
800
801            case 'R':
802                if (behavior == PHP_MODE_PROCESS_STDIN) {
803                    if (exec_run || script_file) {
804                        param_error = "You can use -R or -F only once.\n";
805                        break;
806                    }
807                } else if (behavior != PHP_MODE_STANDARD) {
808                    param_error = param_mode_conflict;
809                    break;
810                }
811                behavior=PHP_MODE_PROCESS_STDIN;
812                exec_run=php_optarg;
813                break;
814
815            case 'B':
816                if (behavior == PHP_MODE_PROCESS_STDIN) {
817                    if (exec_begin) {
818                        param_error = "You can use -B only once.\n";
819                        break;
820                    }
821                } else if (behavior != PHP_MODE_STANDARD || interactive) {
822                    param_error = param_mode_conflict;
823                    break;
824                }
825                behavior=PHP_MODE_PROCESS_STDIN;
826                exec_begin=php_optarg;
827                break;
828
829            case 'E':
830                if (behavior == PHP_MODE_PROCESS_STDIN) {
831                    if (exec_end) {
832                        param_error = "You can use -E only once.\n";
833                        break;
834                    }
835                } else if (behavior != PHP_MODE_STANDARD || interactive) {
836                    param_error = param_mode_conflict;
837                    break;
838                }
839                behavior=PHP_MODE_PROCESS_STDIN;
840                exec_end=php_optarg;
841                break;
842
843            case 's': /* generate highlighted HTML from source */
844                if (behavior == PHP_MODE_CLI_DIRECT || behavior == PHP_MODE_PROCESS_STDIN) {
845                    param_error = "Source highlighting only works for files.\n";
846                    break;
847                }
848                behavior=PHP_MODE_HIGHLIGHT;
849                break;
850
851            case 'w':
852                if (behavior == PHP_MODE_CLI_DIRECT || behavior == PHP_MODE_PROCESS_STDIN) {
853                    param_error = "Source stripping only works for files.\n";
854                    break;
855                }
856                behavior=PHP_MODE_STRIP;
857                break;
858
859            case 'z': /* load extension file */
860                zend_load_extension(php_optarg);
861                break;
862            case 'H':
863                hide_argv = 1;
864                break;
865            case 10:
866                behavior=PHP_MODE_REFLECTION_FUNCTION;
867                reflection_what = php_optarg;
868                break;
869            case 11:
870                behavior=PHP_MODE_REFLECTION_CLASS;
871                reflection_what = php_optarg;
872                break;
873            case 12:
874                behavior=PHP_MODE_REFLECTION_EXTENSION;
875                reflection_what = php_optarg;
876                break;
877            case 13:
878                behavior=PHP_MODE_REFLECTION_ZEND_EXTENSION;
879                reflection_what = php_optarg;
880                break;
881            case 14:
882                behavior=PHP_MODE_REFLECTION_EXT_INFO;
883                reflection_what = php_optarg;
884                break;
885            case 15:
886                behavior = PHP_MODE_SHOW_INI_CONFIG;
887                break;
888            default:
889                break;
890            }
891        }
892
893        if (param_error) {
894            PUTS(param_error);
895            exit_status=1;
896            goto err;
897        }
898
899        if (interactive) {
900#if (HAVE_LIBREADLINE || HAVE_LIBEDIT) && !defined(COMPILE_DL_READLINE)
901            printf("Interactive shell\n\n");
902#else
903            printf("Interactive mode enabled\n\n");
904#endif
905            fflush(stdout);
906        }
907
908        /* only set script_file if not set already and not in direct mode and not at end of parameter list */
909        if (argc > php_optind
910          && !script_file
911          && behavior!=PHP_MODE_CLI_DIRECT
912          && behavior!=PHP_MODE_PROCESS_STDIN
913          && strcmp(argv[php_optind-1],"--"))
914        {
915            script_file=argv[php_optind];
916            php_optind++;
917        }
918        if (script_file) {
919            if (cli_seek_file_begin(&file_handle, script_file, &lineno) != SUCCESS) {
920                goto err;
921            } else {
922                char real_path[MAXPATHLEN];
923                if (VCWD_REALPATH(script_file, real_path)) {
924                    translated_path = strdup(real_path);
925                }
926                script_filename = script_file;
927            }
928        } else {
929            /* We could handle PHP_MODE_PROCESS_STDIN in a different manner  */
930            /* here but this would make things only more complicated. And it */
931            /* is consitent with the way -R works where the stdin file handle*/
932            /* is also accessible. */
933            file_handle.filename = "-";
934            file_handle.handle.fp = stdin;
935        }
936        file_handle.type = ZEND_HANDLE_FP;
937        file_handle.opened_path = NULL;
938        file_handle.free_filename = 0;
939        php_self = (char*)file_handle.filename;
940
941        /* before registering argv to module exchange the *new* argv[0] */
942        /* we can achieve this without allocating more memory */
943        SG(request_info).argc=argc-php_optind+1;
944        arg_excp = argv+php_optind-1;
945        arg_free = argv[php_optind-1];
946        SG(request_info).path_translated = translated_path? translated_path: (char*)file_handle.filename;
947        argv[php_optind-1] = (char*)file_handle.filename;
948        SG(request_info).argv=argv+php_optind-1;
949
950        if (php_request_startup()==FAILURE) {
951            *arg_excp = arg_free;
952            fclose(file_handle.handle.fp);
953            PUTS("Could not startup.\n");
954            goto err;
955        }
956        request_started = 1;
957        CG(start_lineno) = lineno;
958        *arg_excp = arg_free; /* reconstuct argv */
959
960        if (hide_argv) {
961            int i;
962            for (i = 1; i < argc; i++) {
963                memset(argv[i], 0, strlen(argv[i]));
964            }
965        }
966
967        zend_is_auto_global_str(ZEND_STRL("_SERVER"));
968
969        PG(during_request_startup) = 0;
970        switch (behavior) {
971        case PHP_MODE_STANDARD:
972            if (strcmp(file_handle.filename, "-")) {
973                cli_register_file_handles();
974            }
975
976            if (interactive && cli_shell_callbacks.cli_shell_run) {
977                exit_status = cli_shell_callbacks.cli_shell_run();
978            } else {
979                php_execute_script(&file_handle);
980                exit_status = EG(exit_status);
981            }
982            break;
983        case PHP_MODE_LINT:
984            exit_status = php_lint_script(&file_handle);
985            if (exit_status==SUCCESS) {
986                zend_printf("No syntax errors detected in %s\n", file_handle.filename);
987            } else {
988                zend_printf("Errors parsing %s\n", file_handle.filename);
989            }
990            break;
991        case PHP_MODE_STRIP:
992            if (open_file_for_scanning(&file_handle)==SUCCESS) {
993                zend_strip();
994            }
995            goto out;
996            break;
997        case PHP_MODE_HIGHLIGHT:
998            {
999                zend_syntax_highlighter_ini syntax_highlighter_ini;
1000
1001                if (open_file_for_scanning(&file_handle)==SUCCESS) {
1002                    php_get_highlight_struct(&syntax_highlighter_ini);
1003                    zend_highlight(&syntax_highlighter_ini);
1004                }
1005                goto out;
1006            }
1007            break;
1008#if 0
1009            /* Zeev might want to do something with this one day */
1010        case PHP_MODE_INDENT:
1011            open_file_for_scanning(&file_handle);
1012            zend_indent();
1013            zend_file_handle_dtor(file_handle.handle);
1014            goto out;
1015            break;
1016#endif
1017        case PHP_MODE_CLI_DIRECT:
1018            cli_register_file_handles();
1019            if (zend_eval_string_ex(exec_direct, NULL, "Command line code", 1) == FAILURE) {
1020                exit_status=254;
1021            }
1022            break;
1023
1024        case PHP_MODE_PROCESS_STDIN:
1025            {
1026                char *input;
1027                size_t len, index = 0;
1028                zval argn, argi;
1029
1030                cli_register_file_handles();
1031
1032                if (exec_begin && zend_eval_string_ex(exec_begin, NULL, "Command line begin code", 1) == FAILURE) {
1033                    exit_status=254;
1034                }
1035                ZVAL_LONG(&argi, index);
1036                zend_hash_str_update(&EG(symbol_table), "argi", sizeof("argi")-1, &argi);
1037                while (exit_status == SUCCESS && (input=php_stream_gets(s_in_process, NULL, 0)) != NULL) {
1038                    len = strlen(input);
1039                    while (len > 0 && len-- && (input[len]=='\n' || input[len]=='\r')) {
1040                        input[len] = '\0';
1041                    }
1042                    ZVAL_STRINGL(&argn, input, len);
1043                    zend_hash_str_update(&EG(symbol_table), "argn", sizeof("argn")-1, &argn);
1044                    Z_LVAL(argi) = ++index;
1045                    if (exec_run) {
1046                        if (zend_eval_string_ex(exec_run, NULL, "Command line run code", 1) == FAILURE) {
1047                            exit_status=254;
1048                        }
1049                    } else {
1050                        if (script_file) {
1051                            if (cli_seek_file_begin(&file_handle, script_file, &lineno) != SUCCESS) {
1052                                exit_status = 1;
1053                            } else {
1054                                CG(start_lineno) = lineno;
1055                                php_execute_script(&file_handle);
1056                                exit_status = EG(exit_status);
1057                            }
1058                        }
1059                    }
1060                    efree(input);
1061                }
1062                if (exec_end && zend_eval_string_ex(exec_end, NULL, "Command line end code", 1) == FAILURE) {
1063                    exit_status=254;
1064                }
1065
1066                break;
1067            }
1068
1069            case PHP_MODE_REFLECTION_FUNCTION:
1070            case PHP_MODE_REFLECTION_CLASS:
1071            case PHP_MODE_REFLECTION_EXTENSION:
1072            case PHP_MODE_REFLECTION_ZEND_EXTENSION:
1073                {
1074                    zend_class_entry *pce = NULL;
1075                    zval arg, ref;
1076                    zend_execute_data execute_data;
1077
1078                    switch (behavior) {
1079                        default:
1080                            break;
1081                        case PHP_MODE_REFLECTION_FUNCTION:
1082                            if (strstr(reflection_what, "::")) {
1083                                pce = reflection_method_ptr;
1084                            } else {
1085                                pce = reflection_function_ptr;
1086                            }
1087                            break;
1088                        case PHP_MODE_REFLECTION_CLASS:
1089                            pce = reflection_class_ptr;
1090                            break;
1091                        case PHP_MODE_REFLECTION_EXTENSION:
1092                            pce = reflection_extension_ptr;
1093                            break;
1094                        case PHP_MODE_REFLECTION_ZEND_EXTENSION:
1095                            pce = reflection_zend_extension_ptr;
1096                            break;
1097                    }
1098
1099                    ZVAL_STRING(&arg, reflection_what);
1100                    object_init_ex(&ref, pce);
1101
1102                    memset(&execute_data, 0, sizeof(zend_execute_data));
1103                    EG(current_execute_data) = &execute_data;
1104                    zend_call_method_with_1_params(&ref, pce, &pce->constructor, "__construct", NULL, &arg);
1105
1106                    if (EG(exception)) {
1107                        zval tmp, *msg, rv;
1108
1109                        ZVAL_OBJ(&tmp, EG(exception));
1110                        msg = zend_read_property(zend_exception_get_default(), &tmp, "message", sizeof("message")-1, 0, &rv);
1111                        zend_printf("Exception: %s\n", Z_STRVAL_P(msg));
1112                        zval_ptr_dtor(&tmp);
1113                        EG(exception) = NULL;
1114                    } else {
1115                        zend_call_method_with_1_params(NULL, reflection_ptr, NULL, "export", NULL, &ref);
1116                    }
1117                    zval_ptr_dtor(&ref);
1118                    zval_ptr_dtor(&arg);
1119
1120                    break;
1121                }
1122            case PHP_MODE_REFLECTION_EXT_INFO:
1123                {
1124                    int len = (int)strlen(reflection_what);
1125                    char *lcname = zend_str_tolower_dup(reflection_what, len);
1126                    zend_module_entry *module;
1127
1128                    if ((module = zend_hash_str_find_ptr(&module_registry, lcname, len)) == NULL) {
1129                        if (!strcmp(reflection_what, "main")) {
1130                            display_ini_entries(NULL);
1131                        } else {
1132                            zend_printf("Extension '%s' not present.\n", reflection_what);
1133                            exit_status = 1;
1134                        }
1135                    } else {
1136                        php_info_print_module(module);
1137                    }
1138
1139                    efree(lcname);
1140                    break;
1141                }
1142
1143            case PHP_MODE_SHOW_INI_CONFIG:
1144                {
1145                    zend_printf("Configuration File (php.ini) Path: %s\n", PHP_CONFIG_FILE_PATH);
1146                    zend_printf("Loaded Configuration File:         %s\n", php_ini_opened_path ? php_ini_opened_path : "(none)");
1147                    zend_printf("Scan for additional .ini files in: %s\n", php_ini_scanned_path  ? php_ini_scanned_path : "(none)");
1148                    zend_printf("Additional .ini files parsed:      %s\n", php_ini_scanned_files ? php_ini_scanned_files : "(none)");
1149                    break;
1150                }
1151        }
1152    } zend_end_try();
1153
1154out:
1155    if (request_started) {
1156        php_request_shutdown((void *) 0);
1157    }
1158    if (translated_path) {
1159        free(translated_path);
1160    }
1161    if (exit_status == 0) {
1162        exit_status = EG(exit_status);
1163    }
1164    return exit_status;
1165err:
1166    sapi_deactivate();
1167    zend_ini_deactivate();
1168    exit_status = 1;
1169    goto out;
1170}
1171/* }}} */
1172
1173/* {{{ main
1174 */
1175#ifdef PHP_CLI_WIN32_NO_CONSOLE
1176int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)
1177#else
1178int main(int argc, char *argv[])
1179#endif
1180{
1181#ifdef PHP_CLI_WIN32_NO_CONSOLE
1182    int argc = __argc;
1183    char **argv = __argv;
1184#endif
1185
1186    int c;
1187    int exit_status = SUCCESS;
1188    int module_started = 0, sapi_started = 0;
1189    char *php_optarg = NULL;
1190    int php_optind = 1, use_extended_info = 0;
1191    char *ini_path_override = NULL;
1192    char *ini_entries = NULL;
1193    int ini_entries_len = 0;
1194    int ini_ignore = 0;
1195    sapi_module_struct *sapi_module = &cli_sapi_module;
1196
1197    /*
1198     * Do not move this initialization. It needs to happen before argv is used
1199     * in any way.
1200     */
1201    argv = save_ps_args(argc, argv);
1202
1203    cli_sapi_module.additional_functions = additional_functions;
1204
1205#if defined(PHP_WIN32) && defined(_DEBUG) && defined(PHP_WIN32_DEBUG_HEAP)
1206    {
1207        int tmp_flag;
1208        _CrtSetReportMode(_CRT_WARN, _CRTDBG_MODE_FILE);
1209        _CrtSetReportFile(_CRT_WARN, _CRTDBG_FILE_STDERR);
1210        _CrtSetReportMode(_CRT_ERROR, _CRTDBG_MODE_FILE);
1211        _CrtSetReportFile(_CRT_ERROR, _CRTDBG_FILE_STDERR);
1212        _CrtSetReportMode(_CRT_ASSERT, _CRTDBG_MODE_FILE);
1213        _CrtSetReportFile(_CRT_ASSERT, _CRTDBG_FILE_STDERR);
1214        tmp_flag = _CrtSetDbgFlag(_CRTDBG_REPORT_FLAG);
1215        tmp_flag |= _CRTDBG_DELAY_FREE_MEM_DF;
1216        tmp_flag |= _CRTDBG_LEAK_CHECK_DF;
1217
1218        _CrtSetDbgFlag(tmp_flag);
1219    }
1220#endif
1221
1222#ifdef HAVE_SIGNAL_H
1223#if defined(SIGPIPE) && defined(SIG_IGN)
1224    signal(SIGPIPE, SIG_IGN); /* ignore SIGPIPE in standalone mode so
1225                                that sockets created via fsockopen()
1226                                don't kill PHP if the remote site
1227                                closes it.  in apache|apxs mode apache
1228                                does that for us!  thies@thieso.net
1229                                20000419 */
1230#endif
1231#endif
1232
1233
1234#ifdef ZTS
1235    tsrm_startup(1, 1, 0, NULL);
1236    (void)ts_resource(0);
1237    ZEND_TSRMLS_CACHE_UPDATE();
1238#endif
1239
1240#ifdef PHP_WIN32
1241    _fmode = _O_BINARY;         /*sets default for file streams to binary */
1242    setmode(_fileno(stdin), O_BINARY);      /* make the stdio mode be binary */
1243    setmode(_fileno(stdout), O_BINARY);     /* make the stdio mode be binary */
1244    setmode(_fileno(stderr), O_BINARY);     /* make the stdio mode be binary */
1245#endif
1246
1247    while ((c = php_getopt(argc, argv, OPTIONS, &php_optarg, &php_optind, 0, 2))!=-1) {
1248        switch (c) {
1249            case 'c':
1250                if (ini_path_override) {
1251                    free(ini_path_override);
1252                }
1253                ini_path_override = strdup(php_optarg);
1254                break;
1255            case 'n':
1256                ini_ignore = 1;
1257                break;
1258            case 'd': {
1259                /* define ini entries on command line */
1260                int len = (int)strlen(php_optarg);
1261                char *val;
1262
1263                if ((val = strchr(php_optarg, '='))) {
1264                    val++;
1265                    if (!isalnum(*val) && *val != '"' && *val != '\'' && *val != '\0') {
1266                        ini_entries = realloc(ini_entries, ini_entries_len + len + sizeof("\"\"\n\0"));
1267                        memcpy(ini_entries + ini_entries_len, php_optarg, (val - php_optarg));
1268                        ini_entries_len += (int)(val - php_optarg);
1269                        memcpy(ini_entries + ini_entries_len, "\"", 1);
1270                        ini_entries_len++;
1271                        memcpy(ini_entries + ini_entries_len, val, len - (val - php_optarg));
1272                        ini_entries_len += len - (int)(val - php_optarg);
1273                        memcpy(ini_entries + ini_entries_len, "\"\n\0", sizeof("\"\n\0"));
1274                        ini_entries_len += sizeof("\n\0\"") - 2;
1275                    } else {
1276                        ini_entries = realloc(ini_entries, ini_entries_len + len + sizeof("\n\0"));
1277                        memcpy(ini_entries + ini_entries_len, php_optarg, len);
1278                        memcpy(ini_entries + ini_entries_len + len, "\n\0", sizeof("\n\0"));
1279                        ini_entries_len += len + sizeof("\n\0") - 2;
1280                    }
1281                } else {
1282                    ini_entries = realloc(ini_entries, ini_entries_len + len + sizeof("=1\n\0"));
1283                    memcpy(ini_entries + ini_entries_len, php_optarg, len);
1284                    memcpy(ini_entries + ini_entries_len + len, "=1\n\0", sizeof("=1\n\0"));
1285                    ini_entries_len += len + sizeof("=1\n\0") - 2;
1286                }
1287                break;
1288            }
1289#ifndef PHP_CLI_WIN32_NO_CONSOLE
1290            case 'S':
1291                sapi_module = &cli_server_sapi_module;
1292                cli_server_sapi_module.additional_functions = server_additional_functions;
1293                break;
1294#endif
1295            case 'h': /* help & quit */
1296            case '?':
1297                php_cli_usage(argv[0]);
1298                goto out;
1299            case 'i': case 'v': case 'm':
1300                sapi_module = &cli_sapi_module;
1301                goto exit_loop;
1302            case 'e': /* enable extended info output */
1303                use_extended_info = 1;
1304                break;
1305        }
1306    }
1307exit_loop:
1308
1309    sapi_module->ini_defaults = sapi_cli_ini_defaults;
1310    sapi_module->php_ini_path_override = ini_path_override;
1311    sapi_module->phpinfo_as_text = 1;
1312    sapi_module->php_ini_ignore_cwd = 1;
1313    sapi_startup(sapi_module);
1314    sapi_started = 1;
1315
1316    sapi_module->php_ini_ignore = ini_ignore;
1317
1318    sapi_module->executable_location = argv[0];
1319
1320    if (sapi_module == &cli_sapi_module) {
1321        if (ini_entries) {
1322            ini_entries = realloc(ini_entries, ini_entries_len + sizeof(HARDCODED_INI));
1323            memmove(ini_entries + sizeof(HARDCODED_INI) - 2, ini_entries, ini_entries_len + 1);
1324            memcpy(ini_entries, HARDCODED_INI, sizeof(HARDCODED_INI) - 2);
1325        } else {
1326            ini_entries = malloc(sizeof(HARDCODED_INI));
1327            memcpy(ini_entries, HARDCODED_INI, sizeof(HARDCODED_INI));
1328        }
1329        ini_entries_len += sizeof(HARDCODED_INI) - 2;
1330    }
1331
1332    sapi_module->ini_entries = ini_entries;
1333
1334    /* startup after we get the above ini override se we get things right */
1335    if (sapi_module->startup(sapi_module) == FAILURE) {
1336        /* there is no way to see if we must call zend_ini_deactivate()
1337         * since we cannot check if EG(ini_directives) has been initialised
1338         * because the executor's constructor does not set initialize it.
1339         * Apart from that there seems no need for zend_ini_deactivate() yet.
1340         * So we goto out_err.*/
1341        exit_status = 1;
1342        goto out;
1343    }
1344    module_started = 1;
1345
1346    /* -e option */
1347    if (use_extended_info) {
1348        CG(compiler_options) |= ZEND_COMPILE_EXTENDED_INFO;
1349    }
1350
1351    zend_first_try {
1352#ifndef PHP_CLI_WIN32_NO_CONSOLE
1353        if (sapi_module == &cli_sapi_module) {
1354#endif
1355            exit_status = do_cli(argc, argv);
1356#ifndef PHP_CLI_WIN32_NO_CONSOLE
1357        } else {
1358            exit_status = do_cli_server(argc, argv);
1359        }
1360#endif
1361    } zend_end_try();
1362out:
1363    if (ini_path_override) {
1364        free(ini_path_override);
1365    }
1366    if (ini_entries) {
1367        free(ini_entries);
1368    }
1369    if (module_started) {
1370        php_module_shutdown();
1371    }
1372    if (sapi_started) {
1373        sapi_shutdown();
1374    }
1375#ifdef ZTS
1376    tsrm_shutdown();
1377#endif
1378
1379    /*
1380     * Do not move this de-initialization. It needs to happen right before
1381     * exiting.
1382     */
1383    cleanup_ps_args(argv);
1384    exit(exit_status);
1385}
1386/* }}} */
1387
1388/*
1389 * Local variables:
1390 * tab-width: 4
1391 * c-basic-offset: 4
1392 * End:
1393 * vim600: sw=4 ts=4 fdm=marker
1394 * vim<600: sw=4 ts=4
1395 */
1396