Back to index

php5  5.3.10
php_cli.c
Go to the documentation of this file.
00001 /*
00002    +----------------------------------------------------------------------+
00003    | PHP Version 5                                                        |
00004    +----------------------------------------------------------------------+
00005    | Copyright (c) 1997-2012 The PHP Group                                |
00006    +----------------------------------------------------------------------+
00007    | This source file is subject to version 3.01 of the PHP license,      |
00008    | that is bundled with this package in the file LICENSE, and is        |
00009    | available through the world-wide-web at the following url:           |
00010    | http://www.php.net/license/3_01.txt                                  |
00011    | If you did not receive a copy of the PHP license and are unable to   |
00012    | obtain it through the world-wide-web, please send a note to          |
00013    | license@php.net so we can mail you a copy immediately.               |
00014    +----------------------------------------------------------------------+
00015    | Author: Edin Kadribasic <edink@php.net>                              |
00016    |         Marcus Boerger <helly@php.net>                               |
00017    |         Johannes Schlueter <johannes@php.net>                        |
00018    |         Parts based on CGI SAPI Module by                            |
00019    |         Rasmus Lerdorf, Stig Bakken and Zeev Suraski                 |
00020    +----------------------------------------------------------------------+
00021 */
00022 
00023 /* $Id: php_cli.c 321634 2012-01-01 13:15:04Z felipe $ */
00024 
00025 #include "php.h"
00026 #include "php_globals.h"
00027 #include "php_variables.h"
00028 #include "zend_hash.h"
00029 #include "zend_modules.h"
00030 #include "zend_interfaces.h"
00031 
00032 #include "ext/reflection/php_reflection.h"
00033 
00034 #include "SAPI.h"
00035 
00036 #include <stdio.h>
00037 #include "php.h"
00038 #ifdef PHP_WIN32
00039 #include "win32/time.h"
00040 #include "win32/signal.h"
00041 #include <process.h>
00042 #endif
00043 #if HAVE_SYS_TIME_H
00044 #include <sys/time.h>
00045 #endif
00046 #if HAVE_UNISTD_H
00047 #include <unistd.h>
00048 #endif
00049 #if HAVE_SIGNAL_H
00050 #include <signal.h>
00051 #endif
00052 #if HAVE_SETLOCALE
00053 #include <locale.h>
00054 #endif
00055 #include "zend.h"
00056 #include "zend_extensions.h"
00057 #include "php_ini.h"
00058 #include "php_globals.h"
00059 #include "php_main.h"
00060 #include "fopen_wrappers.h"
00061 #include "ext/standard/php_standard.h"
00062 #ifdef PHP_WIN32
00063 #include <io.h>
00064 #include <fcntl.h>
00065 #include "win32/php_registry.h"
00066 #endif
00067 
00068 #if HAVE_SIGNAL_H
00069 #include <signal.h>
00070 #endif
00071 
00072 #ifdef __riscos__
00073 #include <unixlib/local.h>
00074 #endif
00075 
00076 #if (HAVE_LIBREADLINE || HAVE_LIBEDIT) && !defined(COMPILE_DL_READLINE)
00077 #if HAVE_LIBEDIT
00078 #include <editline/readline.h>
00079 #else
00080 #include <readline/readline.h>
00081 #include <readline/history.h>
00082 #endif
00083 #include "php_cli_readline.h"
00084 #endif /* HAVE_LIBREADLINE || HAVE_LIBEDIT */
00085 
00086 #include "zend_compile.h"
00087 #include "zend_execute.h"
00088 #include "zend_highlight.h"
00089 #include "zend_indent.h"
00090 #include "zend_exceptions.h"
00091 
00092 #include "php_getopt.h"
00093 
00094 #ifndef PHP_WIN32
00095 # define php_select(m, r, w, e, t) select(m, r, w, e, t)
00096 #else
00097 # include "win32/select.h"
00098 #endif
00099 
00100 PHPAPI extern char *php_ini_opened_path;
00101 PHPAPI extern char *php_ini_scanned_path;
00102 PHPAPI extern char *php_ini_scanned_files;
00103 
00104 #ifndef O_BINARY
00105 #define O_BINARY 0
00106 #endif
00107 
00108 #define PHP_MODE_STANDARD      1
00109 #define PHP_MODE_HIGHLIGHT     2
00110 #define PHP_MODE_INDENT        3
00111 #define PHP_MODE_LINT          4
00112 #define PHP_MODE_STRIP         5
00113 #define PHP_MODE_CLI_DIRECT    6
00114 #define PHP_MODE_PROCESS_STDIN 7
00115 #define PHP_MODE_REFLECTION_FUNCTION    8
00116 #define PHP_MODE_REFLECTION_CLASS       9
00117 #define PHP_MODE_REFLECTION_EXTENSION   10
00118 #define PHP_MODE_REFLECTION_EXT_INFO    11
00119 #define PHP_MODE_SHOW_INI_CONFIG        12
00120 
00121 const char HARDCODED_INI[] =
00122        "html_errors=0\n"
00123        "register_argc_argv=1\n"
00124        "implicit_flush=1\n"
00125        "output_buffering=0\n"
00126        "max_execution_time=0\n"
00127        "max_input_time=-1\n\0";
00128 
00129 static char *php_optarg = NULL;
00130 static int php_optind = 1;
00131 #if (HAVE_LIBREADLINE || HAVE_LIBEDIT) && !defined(COMPILE_DL_READLINE)
00132 static char php_last_char = '\0';
00133 #endif
00134 
00135 static const opt_struct OPTIONS[] = {
00136        {'a', 0, "interactive"},
00137        {'B', 1, "process-begin"},
00138        {'C', 0, "no-chdir"}, /* for compatibility with CGI (do not chdir to script directory) */
00139        {'c', 1, "php-ini"},
00140        {'d', 1, "define"},
00141        {'E', 1, "process-end"},
00142        {'e', 0, "profile-info"},
00143        {'F', 1, "process-file"},
00144        {'f', 1, "file"},
00145        {'h', 0, "help"},
00146        {'i', 0, "info"},
00147        {'l', 0, "syntax-check"},
00148        {'m', 0, "modules"},
00149        {'n', 0, "no-php-ini"},
00150        {'q', 0, "no-header"}, /* for compatibility with CGI (do not generate HTTP headers) */
00151        {'R', 1, "process-code"},
00152        {'H', 0, "hide-args"},
00153        {'r', 1, "run"},
00154        {'s', 0, "syntax-highlight"},
00155        {'s', 0, "syntax-highlighting"},
00156        {'w', 0, "strip"},
00157        {'?', 0, "usage"},/* help alias (both '?' and 'usage') */
00158        {'v', 0, "version"},
00159        {'z', 1, "zend-extension"},
00160        {10,  1, "rf"},
00161        {10,  1, "rfunction"},
00162        {11,  1, "rc"},
00163        {11,  1, "rclass"},
00164        {12,  1, "re"},
00165        {12,  1, "rextension"},
00166        {13,  1, "ri"},
00167        {13,  1, "rextinfo"},
00168        {14,  0, "ini"},
00169        {'-', 0, NULL} /* end of args */
00170 };
00171 
00172 static int print_module_info(zend_module_entry *module TSRMLS_DC) /* {{{ */
00173 {
00174        php_printf("%s\n", module->name);
00175        return ZEND_HASH_APPLY_KEEP;
00176 }
00177 /* }}} */
00178 
00179 static int module_name_cmp(const void *a, const void *b TSRMLS_DC) /* {{{ */
00180 {
00181        Bucket *f = *((Bucket **) a);
00182        Bucket *s = *((Bucket **) b);
00183 
00184        return strcasecmp(((zend_module_entry *)f->pData)->name,
00185                               ((zend_module_entry *)s->pData)->name);
00186 }
00187 /* }}} */
00188 
00189 static void print_modules(TSRMLS_D) /* {{{ */
00190 {
00191        HashTable sorted_registry;
00192        zend_module_entry tmp;
00193 
00194        zend_hash_init(&sorted_registry, 50, NULL, NULL, 1);
00195        zend_hash_copy(&sorted_registry, &module_registry, NULL, &tmp, sizeof(zend_module_entry));
00196        zend_hash_sort(&sorted_registry, zend_qsort, module_name_cmp, 0 TSRMLS_CC);
00197        zend_hash_apply(&sorted_registry, (apply_func_t) print_module_info TSRMLS_CC);
00198        zend_hash_destroy(&sorted_registry);
00199 }
00200 /* }}} */
00201 
00202 static int print_extension_info(zend_extension *ext, void *arg TSRMLS_DC) /* {{{ */
00203 {
00204        php_printf("%s\n", ext->name);
00205        return ZEND_HASH_APPLY_KEEP;
00206 }
00207 /* }}} */
00208 
00209 static int extension_name_cmp(const zend_llist_element **f, const zend_llist_element **s TSRMLS_DC) /* {{{ */
00210 {
00211        return strcmp(((zend_extension *)(*f)->data)->name,
00212                               ((zend_extension *)(*s)->data)->name);
00213 }
00214 /* }}} */
00215 
00216 static void print_extensions(TSRMLS_D) /* {{{ */
00217 {
00218        zend_llist sorted_exts;
00219 
00220        zend_llist_copy(&sorted_exts, &zend_extensions);
00221        sorted_exts.dtor = NULL;
00222        zend_llist_sort(&sorted_exts, extension_name_cmp TSRMLS_CC);
00223        zend_llist_apply(&sorted_exts, (llist_apply_func_t) print_extension_info TSRMLS_CC);
00224        zend_llist_destroy(&sorted_exts);
00225 }
00226 /* }}} */
00227 
00228 #ifndef STDOUT_FILENO
00229 #define STDOUT_FILENO 1
00230 #endif
00231 
00232 static inline int sapi_cli_select(int fd TSRMLS_DC)
00233 {
00234        fd_set wfd, dfd;
00235        struct timeval tv;
00236        int ret;
00237 
00238        FD_ZERO(&wfd);
00239        FD_ZERO(&dfd);
00240 
00241        PHP_SAFE_FD_SET(fd, &wfd);
00242 
00243        tv.tv_sec = FG(default_socket_timeout);
00244        tv.tv_usec = 0;
00245 
00246        ret = php_select(fd+1, &dfd, &wfd, &dfd, &tv);
00247 
00248        return ret != -1;
00249 }
00250 
00251 static inline size_t sapi_cli_single_write(const char *str, uint str_length TSRMLS_DC) /* {{{ */
00252 {
00253 #ifdef PHP_WRITE_STDOUT
00254        long ret;
00255 
00256        do {
00257               ret = write(STDOUT_FILENO, str, str_length);
00258        } while (ret <= 0 && errno == EAGAIN && sapi_cli_select(STDOUT_FILENO TSRMLS_CC));
00259 
00260        if (ret <= 0) {
00261               return 0;
00262        }
00263 
00264        return ret;
00265 #else
00266        size_t ret;
00267 
00268        ret = fwrite(str, 1, MIN(str_length, 16384), stdout);
00269        return ret;
00270 #endif
00271 }
00272 /* }}} */
00273 
00274 static int sapi_cli_ub_write(const char *str, uint str_length TSRMLS_DC) /* {{{ */
00275 {
00276        const char *ptr = str;
00277        uint remaining = str_length;
00278        size_t ret;
00279 
00280 #if (HAVE_LIBREADLINE || HAVE_LIBEDIT) && !defined(COMPILE_DL_READLINE)
00281        if (!str_length) {
00282               return 0;
00283        }
00284        php_last_char = str[str_length-1];
00285 #endif
00286 
00287        while (remaining > 0)
00288        {
00289               ret = sapi_cli_single_write(ptr, remaining TSRMLS_CC);
00290               if (!ret) {
00291 #ifndef PHP_CLI_WIN32_NO_CONSOLE
00292                      php_handle_aborted_connection();
00293 #endif
00294                      break;
00295               }
00296               ptr += ret;
00297               remaining -= ret;
00298        }
00299 
00300        return (ptr - str);
00301 }
00302 /* }}} */
00303 
00304 static void sapi_cli_flush(void *server_context) /* {{{ */
00305 {
00306        /* Ignore EBADF here, it's caused by the fact that STDIN/STDOUT/STDERR streams
00307         * are/could be closed before fflush() is called.
00308         */
00309        if (fflush(stdout)==EOF && errno!=EBADF) {
00310 #ifndef PHP_CLI_WIN32_NO_CONSOLE
00311               php_handle_aborted_connection();
00312 #endif
00313        }
00314 }
00315 /* }}} */
00316 
00317 static char *php_self = "";
00318 static char *script_filename = "";
00319 
00320 static void sapi_cli_register_variables(zval *track_vars_array TSRMLS_DC) /* {{{ */
00321 {
00322        unsigned int len;
00323        char   *docroot = "";
00324 
00325        /* In CGI mode, we consider the environment to be a part of the server
00326         * variables
00327         */
00328        php_import_environment_variables(track_vars_array TSRMLS_CC);
00329 
00330        /* Build the special-case PHP_SELF variable for the CLI version */
00331        len = strlen(php_self);
00332        if (sapi_module.input_filter(PARSE_SERVER, "PHP_SELF", &php_self, len, &len TSRMLS_CC)) {
00333               php_register_variable("PHP_SELF", php_self, track_vars_array TSRMLS_CC);
00334        }
00335        if (sapi_module.input_filter(PARSE_SERVER, "SCRIPT_NAME", &php_self, len, &len TSRMLS_CC)) {
00336               php_register_variable("SCRIPT_NAME", php_self, track_vars_array TSRMLS_CC);
00337        }
00338        /* filenames are empty for stdin */
00339        len = strlen(script_filename);
00340        if (sapi_module.input_filter(PARSE_SERVER, "SCRIPT_FILENAME", &script_filename, len, &len TSRMLS_CC)) {
00341               php_register_variable("SCRIPT_FILENAME", script_filename, track_vars_array TSRMLS_CC);
00342        }
00343        if (sapi_module.input_filter(PARSE_SERVER, "PATH_TRANSLATED", &script_filename, len, &len TSRMLS_CC)) {
00344               php_register_variable("PATH_TRANSLATED", script_filename, track_vars_array TSRMLS_CC);
00345        }
00346        /* just make it available */
00347        len = 0U;
00348        if (sapi_module.input_filter(PARSE_SERVER, "DOCUMENT_ROOT", &docroot, len, &len TSRMLS_CC)) {
00349               php_register_variable("DOCUMENT_ROOT", docroot, track_vars_array TSRMLS_CC);
00350        }
00351 }
00352 /* }}} */
00353 
00354 static void sapi_cli_log_message(char *message) /* {{{ */
00355 {
00356        fprintf(stderr, "%s\n", message);
00357 }
00358 /* }}} */
00359 
00360 static int sapi_cli_deactivate(TSRMLS_D) /* {{{ */
00361 {
00362        fflush(stdout);
00363        if(SG(request_info).argv0) {
00364               free(SG(request_info).argv0);
00365               SG(request_info).argv0 = NULL;
00366        }
00367        return SUCCESS;
00368 }
00369 /* }}} */
00370 
00371 static char* sapi_cli_read_cookies(TSRMLS_D) /* {{{ */
00372 {
00373        return NULL;
00374 }
00375 /* }}} */
00376 
00377 static int sapi_cli_header_handler(sapi_header_struct *h, sapi_header_op_enum op, sapi_headers_struct *s TSRMLS_DC) /* {{{ */
00378 {
00379        return 0;
00380 }
00381 /* }}} */
00382 
00383 static int sapi_cli_send_headers(sapi_headers_struct *sapi_headers TSRMLS_DC) /* {{{ */
00384 {
00385        /* We do nothing here, this function is needed to prevent that the fallback
00386         * header handling is called. */
00387        return SAPI_HEADER_SENT_SUCCESSFULLY;
00388 }
00389 /* }}} */
00390 
00391 static void sapi_cli_send_header(sapi_header_struct *sapi_header, void *server_context TSRMLS_DC) /* {{{ */
00392 {
00393 }
00394 /* }}} */
00395 
00396 static int php_cli_startup(sapi_module_struct *sapi_module) /* {{{ */
00397 {
00398        if (php_module_startup(sapi_module, NULL, 0)==FAILURE) {
00399               return FAILURE;
00400        }
00401        return SUCCESS;
00402 }
00403 /* }}} */
00404 
00405 /* {{{ sapi_cli_ini_defaults */
00406 
00407 /* overwriteable ini defaults must be set in sapi_cli_ini_defaults() */
00408 #define INI_DEFAULT(name,value)\
00409        Z_SET_REFCOUNT(tmp, 0);\
00410        Z_UNSET_ISREF(tmp);  \
00411        ZVAL_STRINGL(&tmp, zend_strndup(value, sizeof(value)-1), sizeof(value)-1, 0);\
00412        zend_hash_update(configuration_hash, name, sizeof(name), &tmp, sizeof(zval), NULL);\
00413 
00414 static void sapi_cli_ini_defaults(HashTable *configuration_hash)
00415 {
00416        zval tmp;
00417        INI_DEFAULT("report_zend_debug", "0");
00418        INI_DEFAULT("display_errors", "1");
00419 }
00420 /* }}} */
00421 
00422 /* {{{ sapi_module_struct cli_sapi_module
00423  */
00424 static sapi_module_struct cli_sapi_module = {
00425        "cli",                                           /* name */
00426        "Command Line Interface",          /* pretty name */
00427 
00428        php_cli_startup,                          /* startup */
00429        php_module_shutdown_wrapper,       /* shutdown */
00430 
00431        NULL,                                            /* activate */
00432        sapi_cli_deactivate,               /* deactivate */
00433 
00434        sapi_cli_ub_write,                 /* unbuffered write */
00435        sapi_cli_flush,                               /* flush */
00436        NULL,                                            /* get uid */
00437        NULL,                                            /* getenv */
00438 
00439        php_error,                                       /* error handler */
00440 
00441        sapi_cli_header_handler,           /* header handler */
00442        sapi_cli_send_headers,                    /* send headers handler */
00443        sapi_cli_send_header,                     /* send header handler */
00444 
00445        NULL,                                   /* read POST data */
00446        sapi_cli_read_cookies,          /* read Cookies */
00447 
00448        sapi_cli_register_variables,       /* register server variables */
00449        sapi_cli_log_message,                     /* Log message */
00450        NULL,                                            /* Get request time */
00451        NULL,                                            /* Child terminate */
00452 
00453        STANDARD_SAPI_MODULE_PROPERTIES
00454 };
00455 /* }}} */
00456 
00457 /* {{{ arginfo ext/standard/dl.c */
00458 ZEND_BEGIN_ARG_INFO(arginfo_dl, 0)
00459        ZEND_ARG_INFO(0, extension_filename)
00460 ZEND_END_ARG_INFO()
00461 /* }}} */
00462 
00463 static const zend_function_entry additional_functions[] = {
00464        ZEND_FE(dl, arginfo_dl)
00465        {NULL, NULL, NULL}
00466 };
00467 
00468 /* {{{ php_cli_usage
00469  */
00470 static void php_cli_usage(char *argv0)
00471 {
00472        char *prog;
00473 
00474        prog = strrchr(argv0, '/');
00475        if (prog) {
00476               prog++;
00477        } else {
00478               prog = "php";
00479        }
00480        
00481        php_printf( "Usage: %s [options] [-f] <file> [--] [args...]\n"
00482                    "       %s [options] -r <code> [--] [args...]\n"
00483                    "       %s [options] [-B <begin_code>] -R <code> [-E <end_code>] [--] [args...]\n"
00484                    "       %s [options] [-B <begin_code>] -F <file> [-E <end_code>] [--] [args...]\n"
00485                    "       %s [options] -- [args...]\n"
00486                    "       %s [options] -a\n"
00487                    "\n"
00488 #if (HAVE_LIBREADLINE || HAVE_LIBEDIT) && !defined(COMPILE_DL_READLINE)
00489                             "  -a               Run as interactive shell\n"
00490 #else
00491                             "  -a               Run interactively\n"
00492 #endif
00493                             "  -c <path>|<file> Look for php.ini file in this directory\n"
00494                             "  -n               No php.ini file will be used\n"
00495                             "  -d foo[=bar]     Define INI entry foo with value 'bar'\n"
00496                             "  -e               Generate extended information for debugger/profiler\n"
00497                             "  -f <file>        Parse and execute <file>.\n"
00498                             "  -h               This help\n"
00499                             "  -i               PHP information\n"
00500                             "  -l               Syntax check only (lint)\n"
00501                             "  -m               Show compiled in modules\n"
00502                             "  -r <code>        Run PHP <code> without using script tags <?..?>\n"
00503                             "  -B <begin_code>  Run PHP <begin_code> before processing input lines\n"
00504                             "  -R <code>        Run PHP <code> for every input line\n"
00505                             "  -F <file>        Parse and execute <file> for every input line\n"
00506                             "  -E <end_code>    Run PHP <end_code> after processing all input lines\n"
00507                             "  -H               Hide any passed arguments from external tools.\n"
00508                             "  -s               Output HTML syntax highlighted source.\n"
00509                             "  -v               Version number\n"
00510                             "  -w               Output source with stripped comments and whitespace.\n"
00511                             "  -z <file>        Load Zend extension <file>.\n"
00512                             "\n"
00513                             "  args...          Arguments passed to script. Use -- args when first argument\n"
00514                             "                   starts with - or script is read from stdin\n"
00515                             "\n"
00516                             "  --ini            Show configuration file names\n"
00517                             "\n"
00518                             "  --rf <name>      Show information about function <name>.\n"
00519                             "  --rc <name>      Show information about class <name>.\n"
00520                             "  --re <name>      Show information about extension <name>.\n"
00521                             "  --ri <name>      Show configuration for extension <name>.\n"
00522                             "\n"
00523                             , prog, prog, prog, prog, prog, prog);
00524 }
00525 /* }}} */
00526 
00527 static php_stream *s_in_process = NULL;
00528 
00529 static void cli_register_file_handles(TSRMLS_D) /* {{{ */
00530 {
00531        zval *zin, *zout, *zerr;
00532        php_stream *s_in, *s_out, *s_err;
00533        php_stream_context *sc_in=NULL, *sc_out=NULL, *sc_err=NULL;
00534        zend_constant ic, oc, ec;
00535        
00536        MAKE_STD_ZVAL(zin);
00537        MAKE_STD_ZVAL(zout);
00538        MAKE_STD_ZVAL(zerr);
00539 
00540        s_in  = php_stream_open_wrapper_ex("php://stdin",  "rb", 0, NULL, sc_in);
00541        s_out = php_stream_open_wrapper_ex("php://stdout", "wb", 0, NULL, sc_out);
00542        s_err = php_stream_open_wrapper_ex("php://stderr", "wb", 0, NULL, sc_err);
00543 
00544        if (s_in==NULL || s_out==NULL || s_err==NULL) {
00545               FREE_ZVAL(zin);
00546               FREE_ZVAL(zout);
00547               FREE_ZVAL(zerr);
00548               if (s_in) php_stream_close(s_in);
00549               if (s_out) php_stream_close(s_out);
00550               if (s_err) php_stream_close(s_err);
00551               return;
00552        }
00553        
00554 #if PHP_DEBUG
00555        /* do not close stdout and stderr */
00556        s_out->flags |= PHP_STREAM_FLAG_NO_CLOSE;
00557        s_err->flags |= PHP_STREAM_FLAG_NO_CLOSE;
00558 #endif
00559 
00560        s_in_process = s_in;
00561 
00562        php_stream_to_zval(s_in,  zin);
00563        php_stream_to_zval(s_out, zout);
00564        php_stream_to_zval(s_err, zerr);
00565        
00566        ic.value = *zin;
00567        ic.flags = CONST_CS;
00568        ic.name = zend_strndup(ZEND_STRL("STDIN"));
00569        ic.name_len = sizeof("STDIN");
00570        ic.module_number = 0;
00571        zend_register_constant(&ic TSRMLS_CC);
00572 
00573        oc.value = *zout;
00574        oc.flags = CONST_CS;
00575        oc.name = zend_strndup(ZEND_STRL("STDOUT"));
00576        oc.name_len = sizeof("STDOUT");
00577        oc.module_number = 0;
00578        zend_register_constant(&oc TSRMLS_CC);
00579 
00580        ec.value = *zerr;
00581        ec.flags = CONST_CS;
00582        ec.name = zend_strndup(ZEND_STRL("STDERR"));
00583        ec.name_len = sizeof("STDERR");
00584        ec.module_number = 0;
00585        zend_register_constant(&ec TSRMLS_CC);
00586 
00587        FREE_ZVAL(zin);
00588        FREE_ZVAL(zout);
00589        FREE_ZVAL(zerr);
00590 }
00591 /* }}} */
00592 
00593 static const char *param_mode_conflict = "Either execute direct code, process stdin or use a file.\n";
00594 
00595 /* {{{ cli_seek_file_begin
00596  */
00597 static int cli_seek_file_begin(zend_file_handle *file_handle, char *script_file, int *lineno TSRMLS_DC)
00598 {
00599        int c;
00600 
00601        *lineno = 1;
00602 
00603        file_handle->type = ZEND_HANDLE_FP;
00604        file_handle->opened_path = NULL;
00605        file_handle->free_filename = 0;
00606        if (!(file_handle->handle.fp = VCWD_FOPEN(script_file, "rb"))) {
00607               php_printf("Could not open input file: %s\n", script_file);
00608               return FAILURE;
00609        }
00610        file_handle->filename = script_file;
00611 
00612        /* #!php support */
00613        c = fgetc(file_handle->handle.fp);
00614        if (c == '#' && (c = fgetc(file_handle->handle.fp)) == '!') {
00615               while (c != '\n' && c != '\r' && c != EOF) {
00616                      c = fgetc(file_handle->handle.fp); /* skip to end of line */
00617               }
00618               /* handle situations where line is terminated by \r\n */
00619               if (c == '\r') {
00620                      if (fgetc(file_handle->handle.fp) != '\n') {
00621                             long pos = ftell(file_handle->handle.fp);
00622                             fseek(file_handle->handle.fp, pos - 1, SEEK_SET);
00623                      }
00624               }
00625               *lineno = 2;
00626        } else {
00627               rewind(file_handle->handle.fp);
00628        }
00629 
00630        return SUCCESS;
00631 }
00632 /* }}} */
00633 
00634 /* {{{ main
00635  */
00636 #ifdef PHP_CLI_WIN32_NO_CONSOLE
00637 int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)
00638 #else
00639 int main(int argc, char *argv[])
00640 #endif
00641 {
00642        volatile int exit_status = SUCCESS;
00643        int c;
00644        zend_file_handle file_handle;
00645 /* temporary locals */
00646        int behavior=PHP_MODE_STANDARD;
00647        char *reflection_what = NULL;
00648        int orig_optind=php_optind;
00649        char *orig_optarg=php_optarg;
00650        char *arg_free=NULL, **arg_excp=&arg_free;
00651        char *script_file=NULL;
00652        int interactive=0;
00653        volatile int module_started = 0;
00654        volatile int request_started = 0;
00655        int lineno = 0;
00656        char *exec_direct=NULL, *exec_run=NULL, *exec_begin=NULL, *exec_end=NULL;
00657        const char *param_error=NULL;
00658        int hide_argv = 0;
00659 /* end of temporary locals */
00660 #ifdef ZTS
00661        void ***tsrm_ls;
00662 #endif
00663 #ifdef PHP_CLI_WIN32_NO_CONSOLE
00664        int argc = __argc;
00665        char **argv = __argv;
00666 #endif
00667        int ini_entries_len = 0;
00668 
00669 #if defined(PHP_WIN32) && defined(_DEBUG) && defined(PHP_WIN32_DEBUG_HEAP)
00670        {
00671               int tmp_flag;
00672               _CrtSetReportMode(_CRT_WARN, _CRTDBG_MODE_FILE);
00673               _CrtSetReportFile(_CRT_WARN, _CRTDBG_FILE_STDERR);
00674               _CrtSetReportMode(_CRT_ERROR, _CRTDBG_MODE_FILE);
00675               _CrtSetReportFile(_CRT_ERROR, _CRTDBG_FILE_STDERR);
00676               _CrtSetReportMode(_CRT_ASSERT, _CRTDBG_MODE_FILE);
00677               _CrtSetReportFile(_CRT_ASSERT, _CRTDBG_FILE_STDERR);
00678               tmp_flag = _CrtSetDbgFlag(_CRTDBG_REPORT_FLAG);
00679               tmp_flag |= _CRTDBG_DELAY_FREE_MEM_DF;
00680               tmp_flag |= _CRTDBG_LEAK_CHECK_DF;
00681 
00682               _CrtSetDbgFlag(tmp_flag);
00683        }
00684 #endif
00685 
00686 #ifdef HAVE_SIGNAL_H
00687 #if defined(SIGPIPE) && defined(SIG_IGN)
00688        signal(SIGPIPE, SIG_IGN); /* ignore SIGPIPE in standalone mode so
00689                                                         that sockets created via fsockopen()
00690                                                         don't kill PHP if the remote site
00691                                                         closes it.  in apache|apxs mode apache
00692                                                         does that for us!  thies@thieso.net
00693                                                         20000419 */
00694 #endif
00695 #endif
00696 
00697 
00698 #ifdef ZTS
00699        tsrm_startup(1, 1, 0, NULL);
00700        tsrm_ls = ts_resource(0);
00701 #endif
00702 
00703        cli_sapi_module.ini_defaults = sapi_cli_ini_defaults;
00704        cli_sapi_module.php_ini_path_override = NULL;
00705        cli_sapi_module.phpinfo_as_text = 1;
00706        sapi_startup(&cli_sapi_module);
00707 
00708 #ifdef PHP_WIN32
00709        _fmode = _O_BINARY;                /*sets default for file streams to binary */
00710        setmode(_fileno(stdin), O_BINARY);        /* make the stdio mode be binary */
00711        setmode(_fileno(stdout), O_BINARY);              /* make the stdio mode be binary */
00712        setmode(_fileno(stderr), O_BINARY);              /* make the stdio mode be binary */
00713 #endif
00714 
00715        ini_entries_len = sizeof(HARDCODED_INI)-2;
00716        cli_sapi_module.ini_entries = malloc(sizeof(HARDCODED_INI));
00717        memcpy(cli_sapi_module.ini_entries, HARDCODED_INI, sizeof(HARDCODED_INI));
00718 
00719        while ((c = php_getopt(argc, argv, OPTIONS, &php_optarg, &php_optind, 0, 2))!=-1) {
00720               switch (c) {
00721                      case 'c':
00722                             if (cli_sapi_module.php_ini_path_override) {
00723                                    free(cli_sapi_module.php_ini_path_override);
00724                             }
00725                             cli_sapi_module.php_ini_path_override = strdup(php_optarg);
00726                             break;
00727                      case 'n':
00728                             cli_sapi_module.php_ini_ignore = 1;
00729                             break;
00730                      case 'd': {
00731                             /* define ini entries on command line */
00732                             int len = strlen(php_optarg);
00733                             char *val;
00734 
00735                             if ((val = strchr(php_optarg, '='))) {
00736                                    val++;
00737                                    if (!isalnum(*val) && *val != '"' && *val != '\'' && *val != '\0') {
00738                                           cli_sapi_module.ini_entries = realloc(cli_sapi_module.ini_entries, ini_entries_len + len + sizeof("\"\"\n\0"));
00739                                           memcpy(cli_sapi_module.ini_entries + ini_entries_len, php_optarg, (val - php_optarg));
00740                                           ini_entries_len += (val - php_optarg);
00741                                           memcpy(cli_sapi_module.ini_entries + ini_entries_len, "\"", 1);
00742                                           ini_entries_len++;
00743                                           memcpy(cli_sapi_module.ini_entries + ini_entries_len, val, len - (val - php_optarg));
00744                                           ini_entries_len += len - (val - php_optarg);
00745                                           memcpy(cli_sapi_module.ini_entries + ini_entries_len, "\"\n\0", sizeof("\"\n\0"));
00746                                           ini_entries_len += sizeof("\n\0\"") - 2;
00747                                    } else {
00748                                           cli_sapi_module.ini_entries = realloc(cli_sapi_module.ini_entries, ini_entries_len + len + sizeof("\n\0"));
00749                                           memcpy(cli_sapi_module.ini_entries + ini_entries_len, php_optarg, len);
00750                                           memcpy(cli_sapi_module.ini_entries + ini_entries_len + len, "\n\0", sizeof("\n\0"));
00751                                           ini_entries_len += len + sizeof("\n\0") - 2;
00752                                    }
00753                             } else {
00754                                    cli_sapi_module.ini_entries = realloc(cli_sapi_module.ini_entries, ini_entries_len + len + sizeof("=1\n\0"));
00755                                    memcpy(cli_sapi_module.ini_entries + ini_entries_len, php_optarg, len);
00756                                    memcpy(cli_sapi_module.ini_entries + ini_entries_len + len, "=1\n\0", sizeof("=1\n\0"));
00757                                    ini_entries_len += len + sizeof("=1\n\0") - 2;
00758                             }
00759                             break;
00760                      }
00761               }
00762        }
00763        php_optind = orig_optind;
00764        php_optarg = orig_optarg;
00765 
00766        cli_sapi_module.executable_location = argv[0];
00767        cli_sapi_module.additional_functions = additional_functions;
00768 
00769        /* startup after we get the above ini override se we get things right */
00770        if (cli_sapi_module.startup(&cli_sapi_module)==FAILURE) {
00771               /* there is no way to see if we must call zend_ini_deactivate()
00772                * since we cannot check if EG(ini_directives) has been initialised
00773                * because the executor's constructor does not set initialize it.
00774                * Apart from that there seems no need for zend_ini_deactivate() yet.
00775                * So we goto out_err.*/
00776               exit_status = 1;
00777               goto out_err;
00778        }
00779        module_started = 1;
00780 
00781        zend_first_try {
00782               CG(in_compilation) = 0; /* not initialized but needed for several options */
00783               EG(uninitialized_zval_ptr) = NULL;
00784 
00785               while ((c = php_getopt(argc, argv, OPTIONS, &php_optarg, &php_optind, 0, 2)) != -1) {
00786                      switch (c) {
00787 
00788                      case 'h': /* help & quit */
00789                      case '?':
00790                             if (php_request_startup(TSRMLS_C)==FAILURE) {
00791                                    goto err;
00792                             }
00793                             request_started = 1;
00794                             php_cli_usage(argv[0]);
00795                             php_end_ob_buffers(1 TSRMLS_CC);
00796                             exit_status = (c == '?' && argc > 1 && !strchr(argv[1],  c));
00797                             goto out;
00798 
00799                      case 'i': /* php info & quit */
00800                             if (php_request_startup(TSRMLS_C)==FAILURE) {
00801                                    goto err;
00802                             }
00803                             request_started = 1;
00804                             php_print_info(0xFFFFFFFF TSRMLS_CC);
00805                             php_end_ob_buffers(1 TSRMLS_CC);
00806                             exit_status=0;
00807                             goto out;
00808 
00809                      case 'm': /* list compiled in modules */
00810                             if (php_request_startup(TSRMLS_C)==FAILURE) {
00811                                    goto err;
00812                             }
00813                             request_started = 1;
00814                             php_printf("[PHP Modules]\n");
00815                             print_modules(TSRMLS_C);
00816                             php_printf("\n[Zend Modules]\n");
00817                             print_extensions(TSRMLS_C);
00818                             php_printf("\n");
00819                             php_end_ob_buffers(1 TSRMLS_CC);
00820                             exit_status=0;
00821                             goto out;
00822 
00823                      case 'v': /* show php version & quit */
00824                             if (php_request_startup(TSRMLS_C) == FAILURE) {
00825                                    goto err;
00826                             }
00827 
00828                             request_started = 1;
00829                             php_printf("PHP %s (%s) (built: %s %s) %s\nCopyright (c) 1997-2012 The PHP Group\n%s",
00830                                    PHP_VERSION, sapi_module.name, __DATE__, __TIME__,
00831 #if ZEND_DEBUG && defined(HAVE_GCOV)
00832                                    "(DEBUG GCOV)",
00833 #elif ZEND_DEBUG
00834                                    "(DEBUG)",
00835 #elif defined(HAVE_GCOV)
00836                                    "(GCOV)",
00837 #else
00838                                    "",
00839 #endif
00840                                    get_zend_version()
00841                             );
00842                             php_end_ob_buffers(1 TSRMLS_CC);
00843                             exit_status=0;
00844                             goto out;
00845 
00846                      default:
00847                             break;
00848                      }
00849               }
00850 
00851               /* Set some CLI defaults */
00852               SG(options) |= SAPI_OPTION_NO_CHDIR;
00853 
00854               php_optind = orig_optind;
00855               php_optarg = orig_optarg;
00856               while ((c = php_getopt(argc, argv, OPTIONS, &php_optarg, &php_optind, 0, 2)) != -1) {
00857                      switch (c) {
00858 
00859                      case 'a':     /* interactive mode */
00860                             if (!interactive) {
00861                                    if (behavior != PHP_MODE_STANDARD) {
00862                                           param_error = param_mode_conflict;
00863                                           break;
00864                                    }
00865 
00866                                    interactive=1;
00867                             }
00868                             break;
00869 
00870                      case 'C': /* don't chdir to the script directory */
00871                             /* This is default so NOP */
00872                             break;
00873 
00874                      case 'e': /* enable extended info output */
00875                             CG(compiler_options) |= ZEND_COMPILE_EXTENDED_INFO;
00876                             break;
00877 
00878                      case 'F':
00879                             if (behavior == PHP_MODE_PROCESS_STDIN) {
00880                                    if (exec_run || script_file) {
00881                                           param_error = "You can use -R or -F only once.\n";
00882                                           break;
00883                                    }
00884                             } else if (behavior != PHP_MODE_STANDARD) {
00885                                    param_error = param_mode_conflict;
00886                                    break;
00887                             }
00888                             behavior=PHP_MODE_PROCESS_STDIN;
00889                             script_file = php_optarg;
00890                             break;
00891 
00892                      case 'f': /* parse file */
00893                             if (behavior == PHP_MODE_CLI_DIRECT || behavior == PHP_MODE_PROCESS_STDIN) {
00894                                    param_error = param_mode_conflict;
00895                                    break;
00896                             } else if (script_file) {
00897                                    param_error = "You can use -f only once.\n";
00898                                    break;
00899                             }
00900                             script_file = php_optarg;
00901                             break;
00902 
00903                      case 'l': /* syntax check mode */
00904                             if (behavior != PHP_MODE_STANDARD) {
00905                                    break;
00906                             }
00907                             behavior=PHP_MODE_LINT;
00908                             break;
00909 
00910 #if 0 /* not yet operational, see also below ... */
00911                      case '': /* generate indented source mode*/
00912                             if (behavior == PHP_MODE_CLI_DIRECT || behavior == PHP_MODE_PROCESS_STDIN) {
00913                                    param_error = "Source indenting only works for files.\n";
00914                                    break;
00915                             }
00916                             behavior=PHP_MODE_INDENT;
00917                             break;
00918 #endif
00919 
00920                      case 'q': /* do not generate HTTP headers */
00921                             /* This is default so NOP */
00922                             break;
00923 
00924                      case 'r': /* run code from command line */
00925                             if (behavior == PHP_MODE_CLI_DIRECT) {
00926                                    if (exec_direct || script_file) {
00927                                           param_error = "You can use -r only once.\n";
00928                                           break;
00929                                    }
00930                             } else if (behavior != PHP_MODE_STANDARD || interactive) {
00931                                    param_error = param_mode_conflict;
00932                                    break;
00933                             }
00934                             behavior=PHP_MODE_CLI_DIRECT;
00935                             exec_direct=php_optarg;
00936                             break;
00937                      
00938                      case 'R':
00939                             if (behavior == PHP_MODE_PROCESS_STDIN) {
00940                                    if (exec_run || script_file) {
00941                                           param_error = "You can use -R or -F only once.\n";
00942                                           break;
00943                                    }
00944                             } else if (behavior != PHP_MODE_STANDARD) {
00945                                    param_error = param_mode_conflict;
00946                                    break;
00947                             }
00948                             behavior=PHP_MODE_PROCESS_STDIN;
00949                             exec_run=php_optarg;
00950                             break;
00951 
00952                      case 'B':
00953                             if (behavior == PHP_MODE_PROCESS_STDIN) {
00954                                    if (exec_begin) {
00955                                           param_error = "You can use -B only once.\n";
00956                                           break;
00957                                    }
00958                             } else if (behavior != PHP_MODE_STANDARD || interactive) {
00959                                    param_error = param_mode_conflict;
00960                                    break;
00961                             }
00962                             behavior=PHP_MODE_PROCESS_STDIN;
00963                             exec_begin=php_optarg;
00964                             break;
00965 
00966                      case 'E':
00967                             if (behavior == PHP_MODE_PROCESS_STDIN) {
00968                                    if (exec_end) {
00969                                           param_error = "You can use -E only once.\n";
00970                                           break;
00971                                    }
00972                             } else if (behavior != PHP_MODE_STANDARD || interactive) {
00973                                    param_error = param_mode_conflict;
00974                                    break;
00975                             }
00976                             behavior=PHP_MODE_PROCESS_STDIN;
00977                             exec_end=php_optarg;
00978                             break;
00979 
00980                      case 's': /* generate highlighted HTML from source */
00981                             if (behavior == PHP_MODE_CLI_DIRECT || behavior == PHP_MODE_PROCESS_STDIN) {
00982                                    param_error = "Source highlighting only works for files.\n";
00983                                    break;
00984                             }
00985                             behavior=PHP_MODE_HIGHLIGHT;
00986                             break;
00987 
00988                      case 'w':
00989                             if (behavior == PHP_MODE_CLI_DIRECT || behavior == PHP_MODE_PROCESS_STDIN) {
00990                                    param_error = "Source stripping only works for files.\n";
00991                                    break;
00992                             }
00993                             behavior=PHP_MODE_STRIP;
00994                             break;
00995 
00996                      case 'z': /* load extension file */
00997                             zend_load_extension(php_optarg);
00998                             break;
00999                      case 'H':
01000                             hide_argv = 1;
01001                             break;
01002                      case 10:
01003                             behavior=PHP_MODE_REFLECTION_FUNCTION;
01004                             reflection_what = php_optarg;
01005                             break;
01006                      case 11:
01007                             behavior=PHP_MODE_REFLECTION_CLASS;
01008                             reflection_what = php_optarg;
01009                             break;
01010                      case 12:
01011                             behavior=PHP_MODE_REFLECTION_EXTENSION;
01012                             reflection_what = php_optarg;
01013                             break;
01014                      case 13:
01015                             behavior=PHP_MODE_REFLECTION_EXT_INFO;
01016                             reflection_what = php_optarg;
01017                             break;
01018                      case 14:
01019                             behavior = PHP_MODE_SHOW_INI_CONFIG;
01020                             break;
01021                      default:
01022                             break;
01023                      }
01024               }
01025 
01026               if (param_error) {
01027                      PUTS(param_error);
01028                      exit_status=1;
01029                      goto err;
01030               }
01031 
01032               if (interactive) {
01033 #if (HAVE_LIBREADLINE || HAVE_LIBEDIT) && !defined(COMPILE_DL_READLINE)
01034                      printf("Interactive shell\n\n");
01035 #else
01036                      printf("Interactive mode enabled\n\n");
01037 #endif
01038                      fflush(stdout);
01039               }
01040 
01041               CG(interactive) = interactive;
01042 
01043               /* only set script_file if not set already and not in direct mode and not at end of parameter list */
01044               if (argc > php_optind 
01045                 && !script_file 
01046                 && behavior!=PHP_MODE_CLI_DIRECT 
01047                 && behavior!=PHP_MODE_PROCESS_STDIN 
01048                 && strcmp(argv[php_optind-1],"--")) 
01049               {
01050                      script_file=argv[php_optind];
01051                      php_optind++;
01052               }
01053               if (script_file) {
01054                      if (cli_seek_file_begin(&file_handle, script_file, &lineno TSRMLS_CC) != SUCCESS) {
01055                             goto err;
01056                      }
01057                      script_filename = script_file;
01058               } else {
01059                      /* We could handle PHP_MODE_PROCESS_STDIN in a different manner  */
01060                      /* here but this would make things only more complicated. And it */
01061                      /* is consitent with the way -R works where the stdin file handle*/
01062                      /* is also accessible. */
01063                      file_handle.filename = "-";
01064                      file_handle.handle.fp = stdin;
01065               }
01066               file_handle.type = ZEND_HANDLE_FP;
01067               file_handle.opened_path = NULL;
01068               file_handle.free_filename = 0;
01069               php_self = file_handle.filename;
01070 
01071               /* before registering argv to module exchange the *new* argv[0] */
01072               /* we can achieve this without allocating more memory */
01073               SG(request_info).argc=argc-php_optind+1;
01074               arg_excp = argv+php_optind-1;
01075               arg_free = argv[php_optind-1];
01076               SG(request_info).path_translated = file_handle.filename;
01077               argv[php_optind-1] = file_handle.filename;
01078               SG(request_info).argv=argv+php_optind-1;
01079 
01080               if (php_request_startup(TSRMLS_C)==FAILURE) {
01081                      *arg_excp = arg_free;
01082                      fclose(file_handle.handle.fp);
01083                      PUTS("Could not startup.\n");
01084                      goto err;
01085               }
01086               request_started = 1;
01087               CG(start_lineno) = lineno;
01088               *arg_excp = arg_free; /* reconstuct argv */
01089 
01090               if (hide_argv) {
01091                      int i;
01092                      for (i = 1; i < argc; i++) {
01093                             memset(argv[i], 0, strlen(argv[i]));
01094                      }
01095               }
01096 
01097               zend_is_auto_global("_SERVER", sizeof("_SERVER")-1 TSRMLS_CC);
01098 
01099               PG(during_request_startup) = 0;
01100               switch (behavior) {
01101               case PHP_MODE_STANDARD:
01102                      if (strcmp(file_handle.filename, "-")) {
01103                             cli_register_file_handles(TSRMLS_C);
01104                      }
01105 
01106 #if (HAVE_LIBREADLINE || HAVE_LIBEDIT) && !defined(COMPILE_DL_READLINE)
01107                      if (interactive) {
01108                             char *line;
01109                             size_t size = 4096, pos = 0, len;
01110                             char *code = emalloc(size);
01111                             char *prompt = "php > ";
01112                             char *history_file;
01113 
01114                             if (PG(auto_prepend_file) && PG(auto_prepend_file)[0]) {
01115                                    zend_file_handle *prepend_file_p;
01116                                    zend_file_handle prepend_file = {0};
01117 
01118                                    prepend_file.filename = PG(auto_prepend_file);
01119                                    prepend_file.opened_path = NULL;
01120                                    prepend_file.free_filename = 0;
01121                                    prepend_file.type = ZEND_HANDLE_FILENAME;
01122                                    prepend_file_p = &prepend_file;
01123 
01124                                    zend_execute_scripts(ZEND_REQUIRE TSRMLS_CC, NULL, 1, prepend_file_p);
01125                             }
01126 
01127                             history_file = tilde_expand("~/.php_history");
01128                             rl_attempted_completion_function = cli_code_completion;
01129                             rl_special_prefixes = "$";
01130                             read_history(history_file);
01131 
01132                             EG(exit_status) = 0;
01133                             while ((line = readline(prompt)) != NULL) {
01134                                    if (strcmp(line, "exit") == 0 || strcmp(line, "quit") == 0) {
01135                                           free(line);
01136                                           break;
01137                                    }
01138 
01139                                    if (!pos && !*line) {
01140                                           free(line);
01141                                           continue;
01142                                    }
01143 
01144                                    len = strlen(line);
01145                                    if (pos + len + 2 > size) {
01146                                           size = pos + len + 2;
01147                                           code = erealloc(code, size);
01148                                    }
01149                                    memcpy(&code[pos], line, len);
01150                                    pos += len;
01151                                    code[pos] = '\n';
01152                                    code[++pos] = '\0';
01153 
01154                                    if (*line) {
01155                                           add_history(line);
01156                                    }
01157 
01158                                    free(line);
01159 
01160                                    if (!cli_is_valid_code(code, pos, &prompt TSRMLS_CC)) {
01161                                           continue;
01162                                    }
01163 
01164                                    zend_eval_stringl(code, pos, NULL, "php shell code" TSRMLS_CC);
01165                                    pos = 0;
01166                                    
01167                                    if (php_last_char != '\0' && php_last_char != '\n') {
01168                                           sapi_cli_single_write("\n", 1 TSRMLS_CC);
01169                                    }
01170 
01171                                    if (EG(exception)) {
01172                                           zend_exception_error(EG(exception), E_WARNING TSRMLS_CC);
01173                                    }
01174 
01175                                    php_last_char = '\0';
01176                             }
01177                             write_history(history_file);
01178                             free(history_file);
01179                             efree(code);
01180                             exit_status = EG(exit_status);
01181                             break;
01182                      }
01183 #endif /* HAVE_LIBREADLINE || HAVE_LIBEDIT */
01184                      php_execute_script(&file_handle TSRMLS_CC);
01185                      exit_status = EG(exit_status);
01186                      break;
01187               case PHP_MODE_LINT:
01188                      exit_status = php_lint_script(&file_handle TSRMLS_CC);
01189                      if (exit_status==SUCCESS) {
01190                             zend_printf("No syntax errors detected in %s\n", file_handle.filename);
01191                      } else {
01192                             zend_printf("Errors parsing %s\n", file_handle.filename);
01193                      }
01194                      break;
01195               case PHP_MODE_STRIP:
01196                      if (open_file_for_scanning(&file_handle TSRMLS_CC)==SUCCESS) {
01197                             zend_strip(TSRMLS_C);
01198                      }
01199                      goto out;
01200                      break;
01201               case PHP_MODE_HIGHLIGHT:
01202                      {
01203                             zend_syntax_highlighter_ini syntax_highlighter_ini;
01204 
01205                             if (open_file_for_scanning(&file_handle TSRMLS_CC)==SUCCESS) {
01206                                    php_get_highlight_struct(&syntax_highlighter_ini);
01207                                    zend_highlight(&syntax_highlighter_ini TSRMLS_CC);
01208                             }
01209                             goto out;
01210                      }
01211                      break;
01212 #if 0
01213                      /* Zeev might want to do something with this one day */
01214               case PHP_MODE_INDENT:
01215                      open_file_for_scanning(&file_handle TSRMLS_CC);
01216                      zend_indent();
01217                      zend_file_handle_dtor(file_handle.handle TSRMLS_CC);
01218                      goto out;
01219                      break;
01220 #endif
01221               case PHP_MODE_CLI_DIRECT:
01222                      cli_register_file_handles(TSRMLS_C);
01223                      if (zend_eval_string_ex(exec_direct, NULL, "Command line code", 1 TSRMLS_CC) == FAILURE) {
01224                             exit_status=254;
01225                      }
01226                      break;
01227                      
01228               case PHP_MODE_PROCESS_STDIN:
01229                      {
01230                             char *input;
01231                             size_t len, index = 0;
01232                             zval *argn, *argi;
01233 
01234                             cli_register_file_handles(TSRMLS_C);
01235        
01236                             if (exec_begin && zend_eval_string_ex(exec_begin, NULL, "Command line begin code", 1 TSRMLS_CC) == FAILURE) {
01237                                    exit_status=254;
01238                             }
01239                             ALLOC_ZVAL(argi);
01240                             Z_TYPE_P(argi) = IS_LONG;
01241                             Z_LVAL_P(argi) = index;
01242                             INIT_PZVAL(argi);
01243                             zend_hash_update(&EG(symbol_table), "argi", sizeof("argi"), &argi, sizeof(zval *), NULL);
01244                             while (exit_status == SUCCESS && (input=php_stream_gets(s_in_process, NULL, 0)) != NULL) {
01245                                    len = strlen(input);
01246                                    while (len-- && (input[len]=='\n' || input[len]=='\r')) {
01247                                           input[len] = '\0';
01248                                    }
01249                                    ALLOC_ZVAL(argn);
01250                                    Z_TYPE_P(argn) = IS_STRING;
01251                                    Z_STRLEN_P(argn) = ++len;
01252                                    Z_STRVAL_P(argn) = estrndup(input, len);
01253                                    INIT_PZVAL(argn);
01254                                    zend_hash_update(&EG(symbol_table), "argn", sizeof("argn"), &argn, sizeof(zval *), NULL);
01255                                    Z_LVAL_P(argi) = ++index;
01256                                    if (exec_run) {
01257                                           if (zend_eval_string_ex(exec_run, NULL, "Command line run code", 1 TSRMLS_CC) == FAILURE) {
01258                                                  exit_status=254;
01259                                           }
01260                                    } else {
01261                                           if (script_file) {
01262                                                  if (cli_seek_file_begin(&file_handle, script_file, &lineno TSRMLS_CC) != SUCCESS) {
01263                                                         exit_status = 1;
01264                                                  } else {
01265                                                         CG(start_lineno) = lineno;
01266                                                         php_execute_script(&file_handle TSRMLS_CC);
01267                                                         exit_status = EG(exit_status);
01268                                                  }
01269                                           }
01270                                    }
01271                                    efree(input);
01272                             }
01273                             if (exec_end && zend_eval_string_ex(exec_end, NULL, "Command line end code", 1 TSRMLS_CC) == FAILURE) {
01274                                    exit_status=254;
01275                             }
01276 
01277                             break;
01278                      }
01279                      case PHP_MODE_REFLECTION_FUNCTION:
01280                      case PHP_MODE_REFLECTION_CLASS:
01281                      case PHP_MODE_REFLECTION_EXTENSION:
01282                             {
01283                                    zend_class_entry *pce = NULL;
01284                                    zval *arg, *ref;
01285                                    zend_execute_data execute_data;
01286 
01287                                    switch (behavior) {
01288                                           default:
01289                                                  break;
01290                                           case PHP_MODE_REFLECTION_FUNCTION:
01291                                                  if (strstr(reflection_what, "::")) {
01292                                                         pce = reflection_method_ptr;
01293                                                  } else {
01294                                                         pce = reflection_function_ptr;
01295                                                  }
01296                                                  break;
01297                                           case PHP_MODE_REFLECTION_CLASS:
01298                                                  pce = reflection_class_ptr;
01299                                                  break;
01300                                           case PHP_MODE_REFLECTION_EXTENSION:
01301                                                  pce = reflection_extension_ptr;
01302                                                  break;
01303                                    }
01304                                    
01305                                    MAKE_STD_ZVAL(arg);
01306                                    ZVAL_STRING(arg, reflection_what, 1);
01307                                    ALLOC_ZVAL(ref);
01308                                    object_init_ex(ref, pce);
01309                                    INIT_PZVAL(ref);
01310 
01311                                    memset(&execute_data, 0, sizeof(zend_execute_data));
01312                                    EG(current_execute_data) = &execute_data;
01313                                    EX(function_state).function = pce->constructor;
01314                                    zend_call_method_with_1_params(&ref, pce, &pce->constructor, "__construct", NULL, arg);
01315 
01316                                    if (EG(exception)) {
01317                                           zval *msg = zend_read_property(zend_exception_get_default(TSRMLS_C), EG(exception), "message", sizeof("message")-1, 0 TSRMLS_CC);
01318                                           zend_printf("Exception: %s\n", Z_STRVAL_P(msg));
01319                                           zval_ptr_dtor(&EG(exception));
01320                                           EG(exception) = NULL;
01321                                    } else {
01322                                           zend_call_method_with_1_params(NULL, reflection_ptr, NULL, "export", NULL, ref);
01323                                    }
01324                                    zval_ptr_dtor(&ref);
01325                                    zval_ptr_dtor(&arg);
01326 
01327                                    break;
01328                             }
01329                      case PHP_MODE_REFLECTION_EXT_INFO:
01330                             {
01331                                    int len = strlen(reflection_what);
01332                                    char *lcname = zend_str_tolower_dup(reflection_what, len);
01333                                    zend_module_entry *module;
01334 
01335                                    if (zend_hash_find(&module_registry, lcname, len+1, (void**)&module) == FAILURE) {
01336                                           if (!strcmp(reflection_what, "main")) {
01337                                                  display_ini_entries(NULL);
01338                                           } else {
01339                                                  zend_printf("Extension '%s' not present.\n", reflection_what);
01340                                                  exit_status = 1;
01341                                           }
01342                                    } else {
01343                                           php_info_print_module(module TSRMLS_CC);
01344                                    }
01345                                    
01346                                    efree(lcname);
01347                                    break;
01348                             }
01349                      case PHP_MODE_SHOW_INI_CONFIG:
01350                             {
01351                                    zend_printf("Configuration File (php.ini) Path: %s\n", PHP_CONFIG_FILE_PATH);
01352                                    zend_printf("Loaded Configuration File:         %s\n", php_ini_opened_path ? php_ini_opened_path : "(none)");
01353                                    zend_printf("Scan for additional .ini files in: %s\n", php_ini_scanned_path ? php_ini_scanned_path : "(none)");
01354                                    zend_printf("Additional .ini files parsed:      %s\n", php_ini_scanned_files ? php_ini_scanned_files : "(none)");
01355                                    break;
01356                             }
01357               }
01358 
01359        } zend_end_try();
01360 
01361 out:
01362        if (request_started) {
01363               php_request_shutdown((void *) 0);
01364        }
01365        if (exit_status == 0) {
01366               exit_status = EG(exit_status);
01367        }
01368 out_err:      
01369        if (cli_sapi_module.php_ini_path_override) {
01370               free(cli_sapi_module.php_ini_path_override);
01371        }
01372        if (cli_sapi_module.ini_entries) {
01373               free(cli_sapi_module.ini_entries);
01374        }
01375 
01376        if (module_started) {
01377               php_module_shutdown(TSRMLS_C);
01378        }
01379        sapi_shutdown();
01380 #ifdef ZTS
01381        tsrm_shutdown();
01382 #endif
01383 
01384        exit(exit_status);
01385 
01386 err:
01387        sapi_deactivate(TSRMLS_C);
01388        zend_ini_deactivate(TSRMLS_C);
01389        exit_status = 1;
01390        goto out_err;
01391 }
01392 /* }}} */
01393 
01394 /*
01395  * Local variables:
01396  * tab-width: 4
01397  * c-basic-offset: 4
01398  * End:
01399  * vim600: sw=4 ts=4 fdm=marker
01400  * vim<600: sw=4 ts=4
01401  */