Annotation of embedaddon/php/sapi/cli/php_cli.c, revision 1.1

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

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>