PHP  
 PHP: Test and Code Coverage Analysis
downloads | QA | documentation | faq | getting help | mailing lists | reporting bugs | php.net sites | links | my php.net 
 

LCOV - code coverage report
Current view: top level - sapi/cli - php_cli.c (source / functions) Hit Total Coverage
Test: PHP Code Coverage Lines: 449 543 82.7 %
Date: 2016-09-27 Functions: 20 25 80.0 %
Legend: Lines: hit not hit

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

Generated by: LCOV version 1.10

Generated at Tue, 27 Sep 2016 10:26:13 +0000 (47 hours ago)

Copyright © 2005-2016 The PHP Group
All rights reserved.