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: 473 569 83.1 %
Date: 2014-04-16 Functions: 20 25 80.0 %
Legend: Lines: hit not hit

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

Generated by: LCOV version 1.10

Generated at Wed, 16 Apr 2014 12:48:03 +0000 (8 days ago)

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