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

Generated by: LCOV version 1.10

Generated at Sat, 27 Jun 2015 09:41:25 +0000 (4 days ago)

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