File:  [ELWIX - Embedded LightWeight unIX -] / embedaddon / php / sapi / cli / php_cli.c
Revision 1.1.1.4 (vendor branch): download - view: text, annotated - select for diffs - revision graph
Mon Oct 14 08:02:45 2013 UTC (10 years, 9 months ago) by misho
Branches: php, MAIN
CVS tags: v5_4_29p0, v5_4_20p0, v5_4_20, HEAD
v 5.4.20

    1: /*
    2:    +----------------------------------------------------------------------+
    3:    | PHP Version 5                                                        |
    4:    +----------------------------------------------------------------------+
    5:    | Copyright (c) 1997-2013 The PHP Group                                |
    6:    +----------------------------------------------------------------------+
    7:    | This source file is subject to version 3.01 of the PHP license,      |
    8:    | that is bundled with this package in the file LICENSE, and is        |
    9:    | available through the world-wide-web at the following url:           |
   10:    | http://www.php.net/license/3_01.txt                                  |
   11:    | If you did not receive a copy of the PHP license and are unable to   |
   12:    | obtain it through the world-wide-web, please send a note to          |
   13:    | license@php.net so we can mail you a copy immediately.               |
   14:    +----------------------------------------------------------------------+
   15:    | Author: Edin Kadribasic <edink@php.net>                              |
   16:    |         Marcus Boerger <helly@php.net>                               |
   17:    |         Johannes Schlueter <johannes@php.net>                        |
   18:    |         Parts based on CGI SAPI Module by                            |
   19:    |         Rasmus Lerdorf, Stig Bakken and Zeev Suraski                 |
   20:    +----------------------------------------------------------------------+
   21: */
   22: 
   23: /* $Id: php_cli.c,v 1.1.1.4 2013/10/14 08:02:45 misho Exp $ */
   24: 
   25: #include "php.h"
   26: #include "php_globals.h"
   27: #include "php_variables.h"
   28: #include "zend_hash.h"
   29: #include "zend_modules.h"
   30: #include "zend_interfaces.h"
   31: 
   32: #include "ext/reflection/php_reflection.h"
   33: 
   34: #include "SAPI.h"
   35: 
   36: #include <stdio.h>
   37: #include "php.h"
   38: #ifdef PHP_WIN32
   39: #include "win32/time.h"
   40: #include "win32/signal.h"
   41: #include <process.h>
   42: #endif
   43: #if HAVE_SYS_TIME_H
   44: #include <sys/time.h>
   45: #endif
   46: #if HAVE_UNISTD_H
   47: #include <unistd.h>
   48: #endif
   49: #if HAVE_SIGNAL_H
   50: #include <signal.h>
   51: #endif
   52: #if HAVE_SETLOCALE
   53: #include <locale.h>
   54: #endif
   55: #include "zend.h"
   56: #include "zend_extensions.h"
   57: #include "php_ini.h"
   58: #include "php_globals.h"
   59: #include "php_main.h"
   60: #include "fopen_wrappers.h"
   61: #include "ext/standard/php_standard.h"
   62: #include "cli.h"
   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: 
   85: #ifndef PHP_CLI_WIN32_NO_CONSOLE
   86: #include "php_cli_server.h"
   87: #endif
   88: 
   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
  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: }
  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: 
  132: const opt_struct OPTIONS[] = {
  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"},
  153: 	{'S', 1, "server"},
  154: 	{'t', 1, "docroot"},
  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"},
  165: 	{13,  1, "rz"},
  166: 	{13,  1, "rzendextension"},
  167: 	{14,  1, "ri"},
  168: 	{14,  1, "rextinfo"},
  169: 	{15,  0, "ini"},
  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: 
  252: PHP_CLI_API size_t sapi_cli_single_write(const char *str, uint str_length TSRMLS_DC) /* {{{ */
  253: {
  254: #ifdef PHP_WRITE_STDOUT
  255: 	long ret;
  256: #else
  257: 	size_t ret;
  258: #endif
  259: 
  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
  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: 	}
  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: 	}
  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: 
  370: static void sapi_cli_log_message(char *message TSRMLS_DC) /* {{{ */
  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 */
  468: 	
  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: 	
  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] -S <addr>:<port> [-t docroot]\n"
  502: 				"   %s [options] -- [args...]\n"
  503: 				"   %s [options] -a\n"
  504: 				"\n"
  505: #if (HAVE_LIBREADLINE || HAVE_LIBEDIT) && !defined(COMPILE_DL_READLINE)
  506: 				"  -a               Run as interactive shell\n"
  507: #else
  508: 				"  -a               Run interactively\n"
  509: #endif
  510: 				"  -c <path>|<file> Look for php.ini file in this directory\n"
  511: 				"  -n               No php.ini file will be used\n"
  512: 				"  -d foo[=bar]     Define INI entry foo with value 'bar'\n"
  513: 				"  -e               Generate extended information for debugger/profiler\n"
  514: 				"  -f <file>        Parse and execute <file>.\n"
  515: 				"  -h               This help\n"
  516: 				"  -i               PHP information\n"
  517: 				"  -l               Syntax check only (lint)\n"
  518: 				"  -m               Show compiled in modules\n"
  519: 				"  -r <code>        Run PHP <code> without using script tags <?..?>\n"
  520: 				"  -B <begin_code>  Run PHP <begin_code> before processing input lines\n"
  521: 				"  -R <code>        Run PHP <code> for every input line\n"
  522: 				"  -F <file>        Parse and execute <file> for every input line\n"
  523: 				"  -E <end_code>    Run PHP <end_code> after processing all input lines\n"
  524: 				"  -H               Hide any passed arguments from external tools.\n"
  525: 				"  -S <addr>:<port> Run with built-in web server.\n"
  526: 				"  -t <docroot>     Specify document root <docroot> for built-in web server.\n"
  527: 				"  -s               Output HTML syntax highlighted source.\n"
  528: 				"  -v               Version number\n"
  529: 				"  -w               Output source with stripped comments and whitespace.\n"
  530: 				"  -z <file>        Load Zend extension <file>.\n"
  531: 				"\n"
  532: 				"  args...          Arguments passed to script. Use -- args when first argument\n"
  533: 				"                   starts with - or script is read from stdin\n"
  534: 				"\n"
  535: 				"  --ini            Show configuration file names\n"
  536: 				"\n"
  537: 				"  --rf <name>      Show information about function <name>.\n"
  538: 				"  --rc <name>      Show information about class <name>.\n"
  539: 				"  --re <name>      Show information about extension <name>.\n"
  540: 				"  --rz <name>      Show information about Zend extension <name>.\n"
  541: 				"  --ri <name>      Show configuration for extension <name>.\n"
  542: 				"\n"
  543: 				, prog, prog, prog, prog, prog, prog, prog);
  544: }
  545: /* }}} */
  546: 
  547: static php_stream *s_in_process = NULL;
  548: 
  549: static void cli_register_file_handles(TSRMLS_D) /* {{{ */
  550: {
  551: 	zval *zin, *zout, *zerr;
  552: 	php_stream *s_in, *s_out, *s_err;
  553: 	php_stream_context *sc_in=NULL, *sc_out=NULL, *sc_err=NULL;
  554: 	zend_constant ic, oc, ec;
  555: 	
  556: 	MAKE_STD_ZVAL(zin);
  557: 	MAKE_STD_ZVAL(zout);
  558: 	MAKE_STD_ZVAL(zerr);
  559: 
  560: 	s_in  = php_stream_open_wrapper_ex("php://stdin",  "rb", 0, NULL, sc_in);
  561: 	s_out = php_stream_open_wrapper_ex("php://stdout", "wb", 0, NULL, sc_out);
  562: 	s_err = php_stream_open_wrapper_ex("php://stderr", "wb", 0, NULL, sc_err);
  563: 
  564: 	if (s_in==NULL || s_out==NULL || s_err==NULL) {
  565: 		FREE_ZVAL(zin);
  566: 		FREE_ZVAL(zout);
  567: 		FREE_ZVAL(zerr);
  568: 		if (s_in) php_stream_close(s_in);
  569: 		if (s_out) php_stream_close(s_out);
  570: 		if (s_err) php_stream_close(s_err);
  571: 		return;
  572: 	}
  573: 	
  574: #if PHP_DEBUG
  575: 	/* do not close stdout and stderr */
  576: 	s_out->flags |= PHP_STREAM_FLAG_NO_CLOSE;
  577: 	s_err->flags |= PHP_STREAM_FLAG_NO_CLOSE;
  578: #endif
  579: 
  580: 	s_in_process = s_in;
  581: 
  582: 	php_stream_to_zval(s_in,  zin);
  583: 	php_stream_to_zval(s_out, zout);
  584: 	php_stream_to_zval(s_err, zerr);
  585: 	
  586: 	ic.value = *zin;
  587: 	ic.flags = CONST_CS;
  588: 	ic.name = zend_strndup(ZEND_STRL("STDIN"));
  589: 	ic.name_len = sizeof("STDIN");
  590: 	ic.module_number = 0;
  591: 	zend_register_constant(&ic TSRMLS_CC);
  592: 
  593: 	oc.value = *zout;
  594: 	oc.flags = CONST_CS;
  595: 	oc.name = zend_strndup(ZEND_STRL("STDOUT"));
  596: 	oc.name_len = sizeof("STDOUT");
  597: 	oc.module_number = 0;
  598: 	zend_register_constant(&oc TSRMLS_CC);
  599: 
  600: 	ec.value = *zerr;
  601: 	ec.flags = CONST_CS;
  602: 	ec.name = zend_strndup(ZEND_STRL("STDERR"));
  603: 	ec.name_len = sizeof("STDERR");
  604: 	ec.module_number = 0;
  605: 	zend_register_constant(&ec TSRMLS_CC);
  606: 
  607: 	FREE_ZVAL(zin);
  608: 	FREE_ZVAL(zout);
  609: 	FREE_ZVAL(zerr);
  610: }
  611: /* }}} */
  612: 
  613: static const char *param_mode_conflict = "Either execute direct code, process stdin or use a file.\n";
  614: 
  615: /* {{{ cli_seek_file_begin
  616:  */
  617: static int cli_seek_file_begin(zend_file_handle *file_handle, char *script_file, int *lineno TSRMLS_DC)
  618: {
  619: 	int c;
  620: 
  621: 	*lineno = 1;
  622: 
  623: 	file_handle->type = ZEND_HANDLE_FP;
  624: 	file_handle->opened_path = NULL;
  625: 	file_handle->free_filename = 0;
  626: 	if (!(file_handle->handle.fp = VCWD_FOPEN(script_file, "rb"))) {
  627: 		php_printf("Could not open input file: %s\n", script_file);
  628: 		return FAILURE;
  629: 	}
  630: 	file_handle->filename = script_file;
  631: 
  632: 	/* #!php support */
  633: 	c = fgetc(file_handle->handle.fp);
  634: 	if (c == '#' && (c = fgetc(file_handle->handle.fp)) == '!') {
  635: 		while (c != '\n' && c != '\r' && c != EOF) {
  636: 			c = fgetc(file_handle->handle.fp);	/* skip to end of line */
  637: 		}
  638: 		/* handle situations where line is terminated by \r\n */
  639: 		if (c == '\r') {
  640: 			if (fgetc(file_handle->handle.fp) != '\n') {
  641: 				long pos = ftell(file_handle->handle.fp);
  642: 				fseek(file_handle->handle.fp, pos - 1, SEEK_SET);
  643: 			}
  644: 		}
  645: 		*lineno = 2;
  646: 	} else {
  647: 		rewind(file_handle->handle.fp);
  648: 	}
  649: 
  650: 	return SUCCESS;
  651: }
  652: /* }}} */
  653: 
  654: static int do_cli(int argc, char **argv TSRMLS_DC) /* {{{ */
  655: {
  656: 	int c;
  657: 	zend_file_handle file_handle;
  658: 	int behavior = PHP_MODE_STANDARD;
  659: 	char *reflection_what = NULL;
  660: 	volatile int request_started = 0;
  661: 	volatile int exit_status = 0;
  662: 	char *php_optarg = NULL, *orig_optarg = NULL;
  663: 	int php_optind = 1, orig_optind = 1;
  664: 	char *exec_direct=NULL, *exec_run=NULL, *exec_begin=NULL, *exec_end=NULL;
  665: 	char *arg_free=NULL, **arg_excp=&arg_free;
  666: 	char *script_file=NULL, *translated_path = NULL;
  667: 	int interactive=0;
  668: 	int lineno = 0;
  669: 	const char *param_error=NULL;
  670: 	int hide_argv = 0;
  671: 
  672: 	zend_try {
  673: 	
  674: 		CG(in_compilation) = 0; /* not initialized but needed for several options */
  675: 		EG(uninitialized_zval_ptr) = NULL;
  676: 
  677: 		while ((c = php_getopt(argc, argv, OPTIONS, &php_optarg, &php_optind, 0, 2)) != -1) {
  678: 			switch (c) {
  679: 
  680: 			case 'i': /* php info & quit */
  681: 				if (php_request_startup(TSRMLS_C)==FAILURE) {
  682: 					goto err;
  683: 				}
  684: 				request_started = 1;
  685: 				php_print_info(0xFFFFFFFF TSRMLS_CC);
  686: 				php_output_end_all(TSRMLS_C);
  687: 				exit_status = (c == '?' && argc > 1 && !strchr(argv[1],  c));
  688: 				goto out;
  689: 
  690: 			case 'v': /* show php version & quit */
  691: 				php_printf("PHP %s (%s) (built: %s %s) %s\nCopyright (c) 1997-2013 The PHP Group\n%s",
  692: 					PHP_VERSION, cli_sapi_module.name, __DATE__, __TIME__,
  693: #if ZEND_DEBUG && defined(HAVE_GCOV)
  694: 					"(DEBUG GCOV)",
  695: #elif ZEND_DEBUG
  696: 					"(DEBUG)",
  697: #elif defined(HAVE_GCOV)
  698: 					"(GCOV)",
  699: #else
  700: 					"",
  701: #endif
  702: 					get_zend_version()
  703: 				);
  704: 				sapi_deactivate(TSRMLS_C);
  705: 				goto out;
  706: 
  707: 			case 'm': /* list compiled in modules */
  708: 				if (php_request_startup(TSRMLS_C)==FAILURE) {
  709: 					goto err;
  710: 				}
  711: 				request_started = 1;
  712: 				php_printf("[PHP Modules]\n");
  713: 				print_modules(TSRMLS_C);
  714: 				php_printf("\n[Zend Modules]\n");
  715: 				print_extensions(TSRMLS_C);
  716: 				php_printf("\n");
  717: 				php_output_end_all(TSRMLS_C);
  718: 				exit_status=0;
  719: 				goto out;
  720: 
  721: 			default:
  722: 				break;
  723: 			}
  724: 		}
  725: 
  726: 		/* Set some CLI defaults */
  727: 		SG(options) |= SAPI_OPTION_NO_CHDIR;
  728: 
  729: 		php_optind = orig_optind;
  730: 		php_optarg = orig_optarg;
  731: 		while ((c = php_getopt(argc, argv, OPTIONS, &php_optarg, &php_optind, 0, 2)) != -1) {
  732: 			switch (c) {
  733: 
  734: 			case 'a':	/* interactive mode */
  735: 				if (!interactive) {
  736: 					if (behavior != PHP_MODE_STANDARD) {
  737: 						param_error = param_mode_conflict;
  738: 						break;
  739: 					}
  740: 
  741: 					interactive=1;
  742: 				}
  743: 				break;
  744: 
  745: 			case 'C': /* don't chdir to the script directory */
  746: 				/* This is default so NOP */
  747: 				break;
  748: 
  749: 			case 'F':
  750: 				if (behavior == PHP_MODE_PROCESS_STDIN) {
  751: 					if (exec_run || script_file) {
  752: 						param_error = "You can use -R or -F only once.\n";
  753: 						break;
  754: 					}
  755: 				} else if (behavior != PHP_MODE_STANDARD) {
  756: 					param_error = param_mode_conflict;
  757: 					break;
  758: 				}
  759: 				behavior=PHP_MODE_PROCESS_STDIN;
  760: 				script_file = php_optarg;
  761: 				break;
  762: 
  763: 			case 'f': /* parse file */
  764: 				if (behavior == PHP_MODE_CLI_DIRECT || behavior == PHP_MODE_PROCESS_STDIN) {
  765: 					param_error = param_mode_conflict;
  766: 					break;
  767: 				} else if (script_file) {
  768: 					param_error = "You can use -f only once.\n";
  769: 					break;
  770: 				}
  771: 				script_file = php_optarg;
  772: 				break;
  773: 
  774: 			case 'l': /* syntax check mode */
  775: 				if (behavior != PHP_MODE_STANDARD) {
  776: 					break;
  777: 				}
  778: 				behavior=PHP_MODE_LINT;
  779: 				break;
  780: 
  781: #if 0 /* not yet operational, see also below ... */
  782: 			case '': /* generate indented source mode*/
  783: 				if (behavior == PHP_MODE_CLI_DIRECT || behavior == PHP_MODE_PROCESS_STDIN) {
  784: 					param_error = "Source indenting only works for files.\n";
  785: 					break;
  786: 				}
  787: 				behavior=PHP_MODE_INDENT;
  788: 				break;
  789: #endif
  790: 
  791: 			case 'q': /* do not generate HTTP headers */
  792: 				/* This is default so NOP */
  793: 				break;
  794: 
  795: 			case 'r': /* run code from command line */
  796: 				if (behavior == PHP_MODE_CLI_DIRECT) {
  797: 					if (exec_direct || script_file) {
  798: 						param_error = "You can use -r only once.\n";
  799: 						break;
  800: 					}
  801: 				} else if (behavior != PHP_MODE_STANDARD || interactive) {
  802: 					param_error = param_mode_conflict;
  803: 					break;
  804: 				}
  805: 				behavior=PHP_MODE_CLI_DIRECT;
  806: 				exec_direct=php_optarg;
  807: 				break;
  808: 			
  809: 			case 'R':
  810: 				if (behavior == PHP_MODE_PROCESS_STDIN) {
  811: 					if (exec_run || script_file) {
  812: 						param_error = "You can use -R or -F only once.\n";
  813: 						break;
  814: 					}
  815: 				} else if (behavior != PHP_MODE_STANDARD) {
  816: 					param_error = param_mode_conflict;
  817: 					break;
  818: 				}
  819: 				behavior=PHP_MODE_PROCESS_STDIN;
  820: 				exec_run=php_optarg;
  821: 				break;
  822: 
  823: 			case 'B':
  824: 				if (behavior == PHP_MODE_PROCESS_STDIN) {
  825: 					if (exec_begin) {
  826: 						param_error = "You can use -B only once.\n";
  827: 						break;
  828: 					}
  829: 				} else if (behavior != PHP_MODE_STANDARD || interactive) {
  830: 					param_error = param_mode_conflict;
  831: 					break;
  832: 				}
  833: 				behavior=PHP_MODE_PROCESS_STDIN;
  834: 				exec_begin=php_optarg;
  835: 				break;
  836: 
  837: 			case 'E':
  838: 				if (behavior == PHP_MODE_PROCESS_STDIN) {
  839: 					if (exec_end) {
  840: 						param_error = "You can use -E only once.\n";
  841: 						break;
  842: 					}
  843: 				} else if (behavior != PHP_MODE_STANDARD || interactive) {
  844: 					param_error = param_mode_conflict;
  845: 					break;
  846: 				}
  847: 				behavior=PHP_MODE_PROCESS_STDIN;
  848: 				exec_end=php_optarg;
  849: 				break;
  850: 
  851: 			case 's': /* generate highlighted HTML from source */
  852: 				if (behavior == PHP_MODE_CLI_DIRECT || behavior == PHP_MODE_PROCESS_STDIN) {
  853: 					param_error = "Source highlighting only works for files.\n";
  854: 					break;
  855: 				}
  856: 				behavior=PHP_MODE_HIGHLIGHT;
  857: 				break;
  858: 
  859: 			case 'w':
  860: 				if (behavior == PHP_MODE_CLI_DIRECT || behavior == PHP_MODE_PROCESS_STDIN) {
  861: 					param_error = "Source stripping only works for files.\n";
  862: 					break;
  863: 				}
  864: 				behavior=PHP_MODE_STRIP;
  865: 				break;
  866: 
  867: 			case 'z': /* load extension file */
  868: 				zend_load_extension(php_optarg);
  869: 				break;
  870: 			case 'H':
  871: 				hide_argv = 1;
  872: 				break;
  873: 			case 10:
  874: 				behavior=PHP_MODE_REFLECTION_FUNCTION;
  875: 				reflection_what = php_optarg;
  876: 				break;
  877: 			case 11:
  878: 				behavior=PHP_MODE_REFLECTION_CLASS;
  879: 				reflection_what = php_optarg;
  880: 				break;
  881: 			case 12:
  882: 				behavior=PHP_MODE_REFLECTION_EXTENSION;
  883: 				reflection_what = php_optarg;
  884: 				break;
  885: 			case 13:
  886: 				behavior=PHP_MODE_REFLECTION_ZEND_EXTENSION;
  887: 				reflection_what = php_optarg;
  888: 				break;
  889: 			case 14:
  890: 				behavior=PHP_MODE_REFLECTION_EXT_INFO;
  891: 				reflection_what = php_optarg;
  892: 				break;
  893: 			case 15:
  894: 				behavior = PHP_MODE_SHOW_INI_CONFIG;
  895: 				break;
  896: 			default:
  897: 				break;
  898: 			}
  899: 		}
  900: 
  901: 		if (param_error) {
  902: 			PUTS(param_error);
  903: 			exit_status=1;
  904: 			goto err;
  905: 		}
  906: 
  907: 		if (interactive) {
  908: #if (HAVE_LIBREADLINE || HAVE_LIBEDIT) && !defined(COMPILE_DL_READLINE)
  909: 			printf("Interactive shell\n\n");
  910: #else
  911: 			printf("Interactive mode enabled\n\n");
  912: #endif
  913: 			fflush(stdout);
  914: 		}
  915: 
  916: 		CG(interactive) = interactive;
  917: 
  918: 		/* only set script_file if not set already and not in direct mode and not at end of parameter list */
  919: 		if (argc > php_optind 
  920: 		  && !script_file 
  921: 		  && behavior!=PHP_MODE_CLI_DIRECT 
  922: 		  && behavior!=PHP_MODE_PROCESS_STDIN 
  923: 		  && strcmp(argv[php_optind-1],"--")) 
  924: 		{
  925: 			script_file=argv[php_optind];
  926: 			php_optind++;
  927: 		}
  928: 		if (script_file) {
  929: 			if (cli_seek_file_begin(&file_handle, script_file, &lineno TSRMLS_CC) != SUCCESS) {
  930: 				goto err;
  931: 			} else {
  932: 				char real_path[MAXPATHLEN];
  933: 				if (VCWD_REALPATH(script_file, real_path)) {
  934: 					translated_path = strdup(real_path);
  935: 				}
  936: 				script_filename = script_file;
  937: 			}
  938: 		} else {
  939: 			/* We could handle PHP_MODE_PROCESS_STDIN in a different manner  */
  940: 			/* here but this would make things only more complicated. And it */
  941: 			/* is consitent with the way -R works where the stdin file handle*/
  942: 			/* is also accessible. */
  943: 			file_handle.filename = "-";
  944: 			file_handle.handle.fp = stdin;
  945: 		}
  946: 		file_handle.type = ZEND_HANDLE_FP;
  947: 		file_handle.opened_path = NULL;
  948: 		file_handle.free_filename = 0;
  949: 		php_self = (char*)file_handle.filename;
  950: 
  951: 		/* before registering argv to module exchange the *new* argv[0] */
  952: 		/* we can achieve this without allocating more memory */
  953: 		SG(request_info).argc=argc-php_optind+1;
  954: 		arg_excp = argv+php_optind-1;
  955: 		arg_free = argv[php_optind-1];
  956: 		SG(request_info).path_translated = translated_path? translated_path: (char*)file_handle.filename;
  957: 		argv[php_optind-1] = (char*)file_handle.filename;
  958: 		SG(request_info).argv=argv+php_optind-1;
  959: 
  960: 		if (php_request_startup(TSRMLS_C)==FAILURE) {
  961: 			*arg_excp = arg_free;
  962: 			fclose(file_handle.handle.fp);
  963: 			PUTS("Could not startup.\n");
  964: 			goto err;
  965: 		}
  966: 		request_started = 1;
  967: 		CG(start_lineno) = lineno;
  968: 		*arg_excp = arg_free; /* reconstuct argv */
  969: 
  970: 		if (hide_argv) {
  971: 			int i;
  972: 			for (i = 1; i < argc; i++) {
  973: 				memset(argv[i], 0, strlen(argv[i]));
  974: 			}
  975: 		}
  976: 
  977: 		zend_is_auto_global("_SERVER", sizeof("_SERVER")-1 TSRMLS_CC);
  978: 
  979: 		PG(during_request_startup) = 0;
  980: 		switch (behavior) {
  981: 		case PHP_MODE_STANDARD:
  982: 			if (strcmp(file_handle.filename, "-")) {
  983: 				cli_register_file_handles(TSRMLS_C);
  984: 			}
  985: 
  986: 			if (interactive && cli_shell_callbacks.cli_shell_run) {
  987: 				exit_status = cli_shell_callbacks.cli_shell_run(TSRMLS_C);
  988: 			} else {
  989: 				php_execute_script(&file_handle TSRMLS_CC);
  990: 				exit_status = EG(exit_status);
  991: 			}
  992: 			break;
  993: 		case PHP_MODE_LINT:
  994: 			exit_status = php_lint_script(&file_handle TSRMLS_CC);
  995: 			if (exit_status==SUCCESS) {
  996: 				zend_printf("No syntax errors detected in %s\n", file_handle.filename);
  997: 			} else {
  998: 				zend_printf("Errors parsing %s\n", file_handle.filename);
  999: 			}
 1000: 			break;
 1001: 		case PHP_MODE_STRIP:
 1002: 			if (open_file_for_scanning(&file_handle TSRMLS_CC)==SUCCESS) {
 1003: 				zend_strip(TSRMLS_C);
 1004: 			}
 1005: 			goto out;
 1006: 			break;
 1007: 		case PHP_MODE_HIGHLIGHT:
 1008: 			{
 1009: 				zend_syntax_highlighter_ini syntax_highlighter_ini;
 1010: 
 1011: 				if (open_file_for_scanning(&file_handle TSRMLS_CC)==SUCCESS) {
 1012: 					php_get_highlight_struct(&syntax_highlighter_ini);
 1013: 					zend_highlight(&syntax_highlighter_ini TSRMLS_CC);
 1014: 				}
 1015: 				goto out;
 1016: 			}
 1017: 			break;
 1018: #if 0
 1019: 			/* Zeev might want to do something with this one day */
 1020: 		case PHP_MODE_INDENT:
 1021: 			open_file_for_scanning(&file_handle TSRMLS_CC);
 1022: 			zend_indent();
 1023: 			zend_file_handle_dtor(file_handle.handle TSRMLS_CC);
 1024: 			goto out;
 1025: 			break;
 1026: #endif
 1027: 		case PHP_MODE_CLI_DIRECT:
 1028: 			cli_register_file_handles(TSRMLS_C);
 1029: 			if (zend_eval_string_ex(exec_direct, NULL, "Command line code", 1 TSRMLS_CC) == FAILURE) {
 1030: 				exit_status=254;
 1031: 			}
 1032: 			break;
 1033: 			
 1034: 		case PHP_MODE_PROCESS_STDIN:
 1035: 			{
 1036: 				char *input;
 1037: 				size_t len, index = 0;
 1038: 				zval *argn, *argi;
 1039: 
 1040: 				cli_register_file_handles(TSRMLS_C);
 1041: 
 1042: 				if (exec_begin && zend_eval_string_ex(exec_begin, NULL, "Command line begin code", 1 TSRMLS_CC) == FAILURE) {
 1043: 					exit_status=254;
 1044: 				}
 1045: 				ALLOC_ZVAL(argi);
 1046: 				Z_TYPE_P(argi) = IS_LONG;
 1047: 				Z_LVAL_P(argi) = index;
 1048: 				INIT_PZVAL(argi);
 1049: 				zend_hash_update(&EG(symbol_table), "argi", sizeof("argi"), &argi, sizeof(zval *), NULL);
 1050: 				while (exit_status == SUCCESS && (input=php_stream_gets(s_in_process, NULL, 0)) != NULL) {
 1051: 					len = strlen(input);
 1052: 					while (len-- && (input[len]=='\n' || input[len]=='\r')) {
 1053: 						input[len] = '\0';
 1054: 					}
 1055: 					ALLOC_ZVAL(argn);
 1056: 					Z_TYPE_P(argn) = IS_STRING;
 1057: 					Z_STRLEN_P(argn) = ++len;
 1058: 					Z_STRVAL_P(argn) = estrndup(input, len);
 1059: 					INIT_PZVAL(argn);
 1060: 					zend_hash_update(&EG(symbol_table), "argn", sizeof("argn"), &argn, sizeof(zval *), NULL);
 1061: 					Z_LVAL_P(argi) = ++index;
 1062: 					if (exec_run) {
 1063: 						if (zend_eval_string_ex(exec_run, NULL, "Command line run code", 1 TSRMLS_CC) == FAILURE) {
 1064: 							exit_status=254;
 1065: 						}
 1066: 					} else {
 1067: 						if (script_file) {
 1068: 							if (cli_seek_file_begin(&file_handle, script_file, &lineno TSRMLS_CC) != SUCCESS) {
 1069: 								exit_status = 1;
 1070: 							} else {
 1071: 								CG(start_lineno) = lineno;
 1072: 								php_execute_script(&file_handle TSRMLS_CC);
 1073: 								exit_status = EG(exit_status);
 1074: 							}
 1075: 						}
 1076: 					}
 1077: 					efree(input);
 1078: 				}
 1079: 				if (exec_end && zend_eval_string_ex(exec_end, NULL, "Command line end code", 1 TSRMLS_CC) == FAILURE) {
 1080: 					exit_status=254;
 1081: 				}
 1082: 
 1083: 				break;
 1084: 			}
 1085: 			case PHP_MODE_REFLECTION_FUNCTION:
 1086: 			case PHP_MODE_REFLECTION_CLASS:
 1087: 			case PHP_MODE_REFLECTION_EXTENSION:
 1088: 			case PHP_MODE_REFLECTION_ZEND_EXTENSION:
 1089: 				{
 1090: 					zend_class_entry *pce = NULL;
 1091: 					zval *arg, *ref;
 1092: 					zend_execute_data execute_data;
 1093: 
 1094: 					switch (behavior) {
 1095: 						default:
 1096: 							break;
 1097: 						case PHP_MODE_REFLECTION_FUNCTION:
 1098: 							if (strstr(reflection_what, "::")) {
 1099: 								pce = reflection_method_ptr;
 1100: 							} else {
 1101: 								pce = reflection_function_ptr;
 1102: 							}
 1103: 							break;
 1104: 						case PHP_MODE_REFLECTION_CLASS:
 1105: 							pce = reflection_class_ptr;
 1106: 							break;
 1107: 						case PHP_MODE_REFLECTION_EXTENSION:
 1108: 							pce = reflection_extension_ptr;
 1109: 							break;
 1110: 						case PHP_MODE_REFLECTION_ZEND_EXTENSION:
 1111: 							pce = reflection_zend_extension_ptr;
 1112: 							break;
 1113: 					}
 1114: 					
 1115: 					MAKE_STD_ZVAL(arg);
 1116: 					ZVAL_STRING(arg, reflection_what, 1);
 1117: 					ALLOC_ZVAL(ref);
 1118: 					object_init_ex(ref, pce);
 1119: 					INIT_PZVAL(ref);
 1120: 
 1121: 					memset(&execute_data, 0, sizeof(zend_execute_data));
 1122: 					EG(current_execute_data) = &execute_data;
 1123: 					EX(function_state).function = pce->constructor;
 1124: 					zend_call_method_with_1_params(&ref, pce, &pce->constructor, "__construct", NULL, arg);
 1125: 
 1126: 					if (EG(exception)) {
 1127: 						zval *msg = zend_read_property(zend_exception_get_default(TSRMLS_C), EG(exception), "message", sizeof("message")-1, 0 TSRMLS_CC);
 1128: 						zend_printf("Exception: %s\n", Z_STRVAL_P(msg));
 1129: 						zval_ptr_dtor(&EG(exception));
 1130: 						EG(exception) = NULL;
 1131: 					} else {
 1132: 						zend_call_method_with_1_params(NULL, reflection_ptr, NULL, "export", NULL, ref);
 1133: 					}
 1134: 					zval_ptr_dtor(&ref);
 1135: 					zval_ptr_dtor(&arg);
 1136: 
 1137: 					break;
 1138: 				}
 1139: 			case PHP_MODE_REFLECTION_EXT_INFO:
 1140: 				{
 1141: 					int len = strlen(reflection_what);
 1142: 					char *lcname = zend_str_tolower_dup(reflection_what, len);
 1143: 					zend_module_entry *module;
 1144: 
 1145: 					if (zend_hash_find(&module_registry, lcname, len+1, (void**)&module) == FAILURE) {
 1146: 						if (!strcmp(reflection_what, "main")) {
 1147: 							display_ini_entries(NULL);
 1148: 						} else {
 1149: 							zend_printf("Extension '%s' not present.\n", reflection_what);
 1150: 							exit_status = 1;
 1151: 						}
 1152: 					} else {
 1153: 						php_info_print_module(module TSRMLS_CC);
 1154: 					}
 1155: 					
 1156: 					efree(lcname);
 1157: 					break;
 1158: 				}
 1159: 			case PHP_MODE_SHOW_INI_CONFIG:
 1160: 				{
 1161: 					zend_printf("Configuration File (php.ini) Path: %s\n", PHP_CONFIG_FILE_PATH);
 1162: 					zend_printf("Loaded Configuration File:         %s\n", php_ini_opened_path ? php_ini_opened_path : "(none)");
 1163: 					zend_printf("Scan for additional .ini files in: %s\n", php_ini_scanned_path  ? php_ini_scanned_path : "(none)");
 1164: 					zend_printf("Additional .ini files parsed:      %s\n", php_ini_scanned_files ? php_ini_scanned_files : "(none)");
 1165: 					break;
 1166: 				}
 1167: 		}
 1168: 	} zend_end_try();
 1169: 
 1170: out:
 1171: 	if (request_started) {
 1172: 		php_request_shutdown((void *) 0);
 1173: 	}
 1174: 	if (translated_path) {
 1175: 		free(translated_path);
 1176: 	}
 1177: 	if (exit_status == 0) {
 1178: 		exit_status = EG(exit_status);
 1179: 	}
 1180: 	return exit_status;
 1181: err:
 1182: 	sapi_deactivate(TSRMLS_C);
 1183: 	zend_ini_deactivate(TSRMLS_C);
 1184: 	exit_status = 1;
 1185: 	goto out;
 1186: }
 1187: /* }}} */
 1188: 
 1189: /* {{{ main
 1190:  */
 1191: #ifdef PHP_CLI_WIN32_NO_CONSOLE
 1192: int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)
 1193: #else
 1194: int main(int argc, char *argv[])
 1195: #endif
 1196: {
 1197: #ifdef ZTS
 1198: 	void ***tsrm_ls;
 1199: #endif
 1200: #ifdef PHP_CLI_WIN32_NO_CONSOLE
 1201: 	int argc = __argc;
 1202: 	char **argv = __argv;
 1203: #endif
 1204: 	int c;
 1205: 	int exit_status = SUCCESS;
 1206: 	int module_started = 0, sapi_started = 0;
 1207: 	char *php_optarg = NULL;
 1208: 	int php_optind = 1, use_extended_info = 0;
 1209: 	char *ini_path_override = NULL;
 1210: 	char *ini_entries = NULL;
 1211: 	int ini_entries_len = 0;
 1212: 	int ini_ignore = 0;
 1213: 	sapi_module_struct *sapi_module = &cli_sapi_module;
 1214: 
 1215: 	cli_sapi_module.additional_functions = additional_functions;
 1216: 
 1217: #if defined(PHP_WIN32) && defined(_DEBUG) && defined(PHP_WIN32_DEBUG_HEAP)
 1218: 	{
 1219: 		int tmp_flag;
 1220: 		_CrtSetReportMode(_CRT_WARN, _CRTDBG_MODE_FILE);
 1221: 		_CrtSetReportFile(_CRT_WARN, _CRTDBG_FILE_STDERR);
 1222: 		_CrtSetReportMode(_CRT_ERROR, _CRTDBG_MODE_FILE);
 1223: 		_CrtSetReportFile(_CRT_ERROR, _CRTDBG_FILE_STDERR);
 1224: 		_CrtSetReportMode(_CRT_ASSERT, _CRTDBG_MODE_FILE);
 1225: 		_CrtSetReportFile(_CRT_ASSERT, _CRTDBG_FILE_STDERR);
 1226: 		tmp_flag = _CrtSetDbgFlag(_CRTDBG_REPORT_FLAG);
 1227: 		tmp_flag |= _CRTDBG_DELAY_FREE_MEM_DF;
 1228: 		tmp_flag |= _CRTDBG_LEAK_CHECK_DF;
 1229: 
 1230: 		_CrtSetDbgFlag(tmp_flag);
 1231: 	}
 1232: #endif
 1233: 
 1234: #ifdef HAVE_SIGNAL_H
 1235: #if defined(SIGPIPE) && defined(SIG_IGN)
 1236: 	signal(SIGPIPE, SIG_IGN); /* ignore SIGPIPE in standalone mode so
 1237: 								that sockets created via fsockopen()
 1238: 								don't kill PHP if the remote site
 1239: 								closes it.  in apache|apxs mode apache
 1240: 								does that for us!  thies@thieso.net
 1241: 								20000419 */
 1242: #endif
 1243: #endif
 1244: 
 1245: 
 1246: #ifdef ZTS
 1247: 	tsrm_startup(1, 1, 0, NULL);
 1248: 	tsrm_ls = ts_resource(0);
 1249: #endif
 1250: 
 1251: #ifdef PHP_WIN32
 1252: 	_fmode = _O_BINARY;			/*sets default for file streams to binary */
 1253: 	setmode(_fileno(stdin), O_BINARY);		/* make the stdio mode be binary */
 1254: 	setmode(_fileno(stdout), O_BINARY);		/* make the stdio mode be binary */
 1255: 	setmode(_fileno(stderr), O_BINARY);		/* make the stdio mode be binary */
 1256: #endif
 1257: 
 1258: 	while ((c = php_getopt(argc, argv, OPTIONS, &php_optarg, &php_optind, 0, 2))!=-1) {
 1259: 		switch (c) {
 1260: 			case 'c':
 1261: 				if (ini_path_override) {
 1262: 					free(ini_path_override);
 1263: 				}
 1264:  				ini_path_override = strdup(php_optarg);
 1265: 				break;
 1266: 			case 'n':
 1267: 				ini_ignore = 1;
 1268: 				break;
 1269: 			case 'd': {
 1270: 				/* define ini entries on command line */
 1271: 				int len = strlen(php_optarg);
 1272: 				char *val;
 1273: 
 1274: 				if ((val = strchr(php_optarg, '='))) {
 1275: 					val++;
 1276: 					if (!isalnum(*val) && *val != '"' && *val != '\'' && *val != '\0') {
 1277: 						ini_entries = realloc(ini_entries, ini_entries_len + len + sizeof("\"\"\n\0"));
 1278: 						memcpy(ini_entries + ini_entries_len, php_optarg, (val - php_optarg));
 1279: 						ini_entries_len += (val - php_optarg);
 1280: 						memcpy(ini_entries + ini_entries_len, "\"", 1);
 1281: 						ini_entries_len++;
 1282: 						memcpy(ini_entries + ini_entries_len, val, len - (val - php_optarg));
 1283: 						ini_entries_len += len - (val - php_optarg);
 1284: 						memcpy(ini_entries + ini_entries_len, "\"\n\0", sizeof("\"\n\0"));
 1285: 						ini_entries_len += sizeof("\n\0\"") - 2;
 1286: 					} else {
 1287: 						ini_entries = realloc(ini_entries, ini_entries_len + len + sizeof("\n\0"));
 1288: 						memcpy(ini_entries + ini_entries_len, php_optarg, len);
 1289: 						memcpy(ini_entries + ini_entries_len + len, "\n\0", sizeof("\n\0"));
 1290: 						ini_entries_len += len + sizeof("\n\0") - 2;
 1291: 					}
 1292: 				} else {
 1293: 					ini_entries = realloc(ini_entries, ini_entries_len + len + sizeof("=1\n\0"));
 1294: 					memcpy(ini_entries + ini_entries_len, php_optarg, len);
 1295: 					memcpy(ini_entries + ini_entries_len + len, "=1\n\0", sizeof("=1\n\0"));
 1296: 					ini_entries_len += len + sizeof("=1\n\0") - 2;
 1297: 				}
 1298: 				break;
 1299: 			}
 1300: #ifndef PHP_CLI_WIN32_NO_CONSOLE
 1301: 			case 'S':
 1302: 				sapi_module = &cli_server_sapi_module;
 1303: 				break;
 1304: #endif
 1305: 			case 'h': /* help & quit */
 1306: 			case '?':
 1307: 				php_cli_usage(argv[0]);
 1308: 				goto out;
 1309: 			case 'i': case 'v': case 'm':
 1310: 				sapi_module = &cli_sapi_module;
 1311: 				goto exit_loop;
 1312: 			case 'e': /* enable extended info output */
 1313: 				use_extended_info = 1;
 1314: 				break;
 1315: 		}
 1316: 	}
 1317: exit_loop:
 1318: 
 1319: 	sapi_module->ini_defaults = sapi_cli_ini_defaults;
 1320: 	sapi_module->php_ini_path_override = ini_path_override;
 1321: 	sapi_module->phpinfo_as_text = 1;
 1322: 	sapi_module->php_ini_ignore_cwd = 1;
 1323: 	sapi_startup(sapi_module);
 1324: 	sapi_started = 1;
 1325: 
 1326: 	sapi_module->php_ini_ignore = ini_ignore;
 1327: 
 1328: 	sapi_module->executable_location = argv[0];
 1329: 
 1330: 	if (sapi_module == &cli_sapi_module) {
 1331: 		if (ini_entries) {
 1332: 			ini_entries = realloc(ini_entries, ini_entries_len + sizeof(HARDCODED_INI));
 1333: 			memmove(ini_entries + sizeof(HARDCODED_INI) - 2, ini_entries, ini_entries_len + 1);
 1334: 			memcpy(ini_entries, HARDCODED_INI, sizeof(HARDCODED_INI) - 2);
 1335: 		} else {
 1336: 			ini_entries = malloc(sizeof(HARDCODED_INI));
 1337: 			memcpy(ini_entries, HARDCODED_INI, sizeof(HARDCODED_INI));
 1338: 		}
 1339: 		ini_entries_len += sizeof(HARDCODED_INI) - 2;
 1340: 	}
 1341: 
 1342: 	sapi_module->ini_entries = ini_entries;
 1343: 
 1344: 	/* startup after we get the above ini override se we get things right */
 1345: 	if (sapi_module->startup(sapi_module) == FAILURE) {
 1346: 		/* there is no way to see if we must call zend_ini_deactivate()
 1347: 		 * since we cannot check if EG(ini_directives) has been initialised
 1348: 		 * because the executor's constructor does not set initialize it.
 1349: 		 * Apart from that there seems no need for zend_ini_deactivate() yet.
 1350: 		 * So we goto out_err.*/
 1351: 		exit_status = 1;
 1352: 		goto out;
 1353: 	}
 1354: 	module_started = 1;
 1355: 	
 1356: 	/* -e option */
 1357: 	if (use_extended_info) {
 1358: 		CG(compiler_options) |= ZEND_COMPILE_EXTENDED_INFO;
 1359: 	}
 1360: 
 1361: 	zend_first_try {
 1362: #ifndef PHP_CLI_WIN32_NO_CONSOLE
 1363: 		if (sapi_module == &cli_sapi_module) {
 1364: #endif
 1365: 			exit_status = do_cli(argc, argv TSRMLS_CC);
 1366: #ifndef PHP_CLI_WIN32_NO_CONSOLE
 1367: 		} else {
 1368: 			exit_status = do_cli_server(argc, argv TSRMLS_CC);
 1369: 		}
 1370: #endif
 1371: 	} zend_end_try();
 1372: out:
 1373: 	if (ini_path_override) {
 1374: 		free(ini_path_override);
 1375: 	}
 1376: 	if (ini_entries) {
 1377: 		free(ini_entries);
 1378: 	}
 1379: 	if (module_started) {
 1380: 		php_module_shutdown(TSRMLS_C);
 1381: 	}
 1382: 	if (sapi_started) {
 1383: 		sapi_shutdown();
 1384: 	}
 1385: #ifdef ZTS
 1386: 	tsrm_shutdown();
 1387: #endif
 1388: 
 1389: 	exit(exit_status);
 1390: }
 1391: /* }}} */
 1392: 
 1393: /*
 1394:  * Local variables:
 1395:  * tab-width: 4
 1396:  * c-basic-offset: 4
 1397:  * End:
 1398:  * vim600: sw=4 ts=4 fdm=marker
 1399:  * vim<600: sw=4 ts=4
 1400:  */

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