PHP  
 PHP: Test and Code Coverage Analysis
downloads | QA | documentation | faq | getting help | mailing lists | reporting bugs | php.net sites | links | my php.net 
 

LTP GCOV extension - code coverage report
Current view: directory - var/php_gcov/PHP_5_2/sapi/cli - php_cli.c
Test: PHP Code Coverage
Date: 2009-11-19 Instrumented lines: 515
Code covered: 73.2 % Executed lines: 377
Legend: not executed executed

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

Generated by: LTP GCOV extension version 1.5

Generated at Thu, 19 Nov 2009 08:20:29 +0000 (5 days ago)

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