LTP GCOV extension - code coverage report
Current view: directory - sapi/cli - php_cli.c
Test: PHP Code Coverage
Date: 2007-04-10 Instrumented lines: 497
Code covered: 38.2 % Executed lines: 190
Legend: not executed executed

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

Generated by: LTP GCOV extension version 1.5