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

Generated by: LCOV version 1.10

Generated at Sat, 13 Sep 2014 16:24:40 +0000 (6 days ago)

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