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

Generated by: LCOV version 1.10

Generated at Wed, 02 Sep 2015 17:19:25 +0000 (38 hours ago)

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