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

Generated by: LCOV version 1.10

Generated at Sun, 01 Mar 2015 23:22:41 +0000 (3 days ago)

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