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

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

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