File:  [ELWIX - Embedded LightWeight unIX -] / embedaddon / php / sapi / cli / php_cli.c
Revision 1.1.1.3 (vendor branch): download - view: text, annotated - select for diffs - revision graph
Mon Jul 22 01:32:13 2013 UTC (11 years ago) by misho
Branches: php, MAIN
CVS tags: v5_4_17, HEAD
5.4.17

    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.3 2013/07/22 01:32:13 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] -- [args...]\n"
  502: 				"   %s [options] -a\n"
  503: 				"\n"
  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"
  524: 				"  -S <addr>:<port> Run with built-in web server.\n"
  525: 				"  -t <docroot>     Specify document root <docroot> for built-in web server.\n"
  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"
  539: 				"  --rz <name>      Show information about Zend extension <name>.\n"
  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: 
  653: static int do_cli(int argc, char **argv TSRMLS_DC) /* {{{ */
  654: {
  655: 	int c;
  656: 	zend_file_handle file_handle;
  657: 	int behavior = PHP_MODE_STANDARD;
  658: 	char *reflection_what = NULL;
  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;
  664: 	char *arg_free=NULL, **arg_excp=&arg_free;
  665: 	char *script_file=NULL, *translated_path = NULL;
  666: 	int interactive=0;
  667: 	int lineno = 0;
  668: 	const char *param_error=NULL;
  669: 	int hide_argv = 0;
  670: 
  671: 	zend_try {
  672: 	
  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);
  685: 				php_output_end_all(TSRMLS_C);
  686: 				exit_status = (c == '?' && argc > 1 && !strchr(argv[1],  c));
  687: 				goto out;
  688: 
  689: 			case 'v': /* show php version & quit */
  690: 				php_printf("PHP %s (%s) (built: %s %s) %s\nCopyright (c) 1997-2013 The PHP Group\n%s",
  691: 					PHP_VERSION, cli_sapi_module.name, __DATE__, __TIME__,
  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: 				);
  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);
  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:
  885: 				behavior=PHP_MODE_REFLECTION_ZEND_EXTENSION;
  886: 				reflection_what = php_optarg;
  887: 				break;
  888: 			case 14:
  889: 				behavior=PHP_MODE_REFLECTION_EXT_INFO;
  890: 				reflection_what = php_optarg;
  891: 				break;
  892: 			case 15:
  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: 			} else {
  931: 				char real_path[MAXPATHLEN];
  932: 				if (VCWD_REALPATH(script_file, real_path)) {
  933: 					translated_path = strdup(real_path);
  934: 				}
  935: 				script_filename = script_file;
  936: 			}
  937: 		} else {
  938: 			/* We could handle PHP_MODE_PROCESS_STDIN in a different manner  */
  939: 			/* here but this would make things only more complicated. And it */
  940: 			/* is consitent with the way -R works where the stdin file handle*/
  941: 			/* is also accessible. */
  942: 			file_handle.filename = "-";
  943: 			file_handle.handle.fp = stdin;
  944: 		}
  945: 		file_handle.type = ZEND_HANDLE_FP;
  946: 		file_handle.opened_path = NULL;
  947: 		file_handle.free_filename = 0;
  948: 		php_self = (char*)file_handle.filename;
  949: 
  950: 		/* before registering argv to module exchange the *new* argv[0] */
  951: 		/* we can achieve this without allocating more memory */
  952: 		SG(request_info).argc=argc-php_optind+1;
  953: 		arg_excp = argv+php_optind-1;
  954: 		arg_free = argv[php_optind-1];
  955: 		SG(request_info).path_translated = translated_path? translated_path: (char*)file_handle.filename;
  956: 		argv[php_optind-1] = (char*)file_handle.filename;
  957: 		SG(request_info).argv=argv+php_optind-1;
  958: 
  959: 		if (php_request_startup(TSRMLS_C)==FAILURE) {
  960: 			*arg_excp = arg_free;
  961: 			fclose(file_handle.handle.fp);
  962: 			PUTS("Could not startup.\n");
  963: 			goto err;
  964: 		}
  965: 		request_started = 1;
  966: 		CG(start_lineno) = lineno;
  967: 		*arg_excp = arg_free; /* reconstuct argv */
  968: 
  969: 		if (hide_argv) {
  970: 			int i;
  971: 			for (i = 1; i < argc; i++) {
  972: 				memset(argv[i], 0, strlen(argv[i]));
  973: 			}
  974: 		}
  975: 
  976: 		zend_is_auto_global("_SERVER", sizeof("_SERVER")-1 TSRMLS_CC);
  977: 
  978: 		PG(during_request_startup) = 0;
  979: 		switch (behavior) {
  980: 		case PHP_MODE_STANDARD:
  981: 			if (strcmp(file_handle.filename, "-")) {
  982: 				cli_register_file_handles(TSRMLS_C);
  983: 			}
  984: 
  985: 			if (interactive && cli_shell_callbacks.cli_shell_run) {
  986: 				exit_status = cli_shell_callbacks.cli_shell_run(TSRMLS_C);
  987: 			} else {
  988: 				php_execute_script(&file_handle TSRMLS_CC);
  989: 				exit_status = EG(exit_status);
  990: 			}
  991: 			break;
  992: 		case PHP_MODE_LINT:
  993: 			exit_status = php_lint_script(&file_handle TSRMLS_CC);
  994: 			if (exit_status==SUCCESS) {
  995: 				zend_printf("No syntax errors detected in %s\n", file_handle.filename);
  996: 			} else {
  997: 				zend_printf("Errors parsing %s\n", file_handle.filename);
  998: 			}
  999: 			break;
 1000: 		case PHP_MODE_STRIP:
 1001: 			if (open_file_for_scanning(&file_handle TSRMLS_CC)==SUCCESS) {
 1002: 				zend_strip(TSRMLS_C);
 1003: 			}
 1004: 			goto out;
 1005: 			break;
 1006: 		case PHP_MODE_HIGHLIGHT:
 1007: 			{
 1008: 				zend_syntax_highlighter_ini syntax_highlighter_ini;
 1009: 
 1010: 				if (open_file_for_scanning(&file_handle TSRMLS_CC)==SUCCESS) {
 1011: 					php_get_highlight_struct(&syntax_highlighter_ini);
 1012: 					zend_highlight(&syntax_highlighter_ini TSRMLS_CC);
 1013: 				}
 1014: 				goto out;
 1015: 			}
 1016: 			break;
 1017: #if 0
 1018: 			/* Zeev might want to do something with this one day */
 1019: 		case PHP_MODE_INDENT:
 1020: 			open_file_for_scanning(&file_handle TSRMLS_CC);
 1021: 			zend_indent();
 1022: 			zend_file_handle_dtor(file_handle.handle TSRMLS_CC);
 1023: 			goto out;
 1024: 			break;
 1025: #endif
 1026: 		case PHP_MODE_CLI_DIRECT:
 1027: 			cli_register_file_handles(TSRMLS_C);
 1028: 			if (zend_eval_string_ex(exec_direct, NULL, "Command line code", 1 TSRMLS_CC) == FAILURE) {
 1029: 				exit_status=254;
 1030: 			}
 1031: 			break;
 1032: 			
 1033: 		case PHP_MODE_PROCESS_STDIN:
 1034: 			{
 1035: 				char *input;
 1036: 				size_t len, index = 0;
 1037: 				zval *argn, *argi;
 1038: 
 1039: 				cli_register_file_handles(TSRMLS_C);
 1040: 
 1041: 				if (exec_begin && zend_eval_string_ex(exec_begin, NULL, "Command line begin code", 1 TSRMLS_CC) == FAILURE) {
 1042: 					exit_status=254;
 1043: 				}
 1044: 				ALLOC_ZVAL(argi);
 1045: 				Z_TYPE_P(argi) = IS_LONG;
 1046: 				Z_LVAL_P(argi) = index;
 1047: 				INIT_PZVAL(argi);
 1048: 				zend_hash_update(&EG(symbol_table), "argi", sizeof("argi"), &argi, sizeof(zval *), NULL);
 1049: 				while (exit_status == SUCCESS && (input=php_stream_gets(s_in_process, NULL, 0)) != NULL) {
 1050: 					len = strlen(input);
 1051: 					while (len-- && (input[len]=='\n' || input[len]=='\r')) {
 1052: 						input[len] = '\0';
 1053: 					}
 1054: 					ALLOC_ZVAL(argn);
 1055: 					Z_TYPE_P(argn) = IS_STRING;
 1056: 					Z_STRLEN_P(argn) = ++len;
 1057: 					Z_STRVAL_P(argn) = estrndup(input, len);
 1058: 					INIT_PZVAL(argn);
 1059: 					zend_hash_update(&EG(symbol_table), "argn", sizeof("argn"), &argn, sizeof(zval *), NULL);
 1060: 					Z_LVAL_P(argi) = ++index;
 1061: 					if (exec_run) {
 1062: 						if (zend_eval_string_ex(exec_run, NULL, "Command line run code", 1 TSRMLS_CC) == FAILURE) {
 1063: 							exit_status=254;
 1064: 						}
 1065: 					} else {
 1066: 						if (script_file) {
 1067: 							if (cli_seek_file_begin(&file_handle, script_file, &lineno TSRMLS_CC) != SUCCESS) {
 1068: 								exit_status = 1;
 1069: 							} else {
 1070: 								CG(start_lineno) = lineno;
 1071: 								php_execute_script(&file_handle TSRMLS_CC);
 1072: 								exit_status = EG(exit_status);
 1073: 							}
 1074: 						}
 1075: 					}
 1076: 					efree(input);
 1077: 				}
 1078: 				if (exec_end && zend_eval_string_ex(exec_end, NULL, "Command line end code", 1 TSRMLS_CC) == FAILURE) {
 1079: 					exit_status=254;
 1080: 				}
 1081: 
 1082: 				break;
 1083: 			}
 1084: 			case PHP_MODE_REFLECTION_FUNCTION:
 1085: 			case PHP_MODE_REFLECTION_CLASS:
 1086: 			case PHP_MODE_REFLECTION_EXTENSION:
 1087: 			case PHP_MODE_REFLECTION_ZEND_EXTENSION:
 1088: 				{
 1089: 					zend_class_entry *pce = NULL;
 1090: 					zval *arg, *ref;
 1091: 					zend_execute_data execute_data;
 1092: 
 1093: 					switch (behavior) {
 1094: 						default:
 1095: 							break;
 1096: 						case PHP_MODE_REFLECTION_FUNCTION:
 1097: 							if (strstr(reflection_what, "::")) {
 1098: 								pce = reflection_method_ptr;
 1099: 							} else {
 1100: 								pce = reflection_function_ptr;
 1101: 							}
 1102: 							break;
 1103: 						case PHP_MODE_REFLECTION_CLASS:
 1104: 							pce = reflection_class_ptr;
 1105: 							break;
 1106: 						case PHP_MODE_REFLECTION_EXTENSION:
 1107: 							pce = reflection_extension_ptr;
 1108: 							break;
 1109: 						case PHP_MODE_REFLECTION_ZEND_EXTENSION:
 1110: 							pce = reflection_zend_extension_ptr;
 1111: 							break;
 1112: 					}
 1113: 					
 1114: 					MAKE_STD_ZVAL(arg);
 1115: 					ZVAL_STRING(arg, reflection_what, 1);
 1116: 					ALLOC_ZVAL(ref);
 1117: 					object_init_ex(ref, pce);
 1118: 					INIT_PZVAL(ref);
 1119: 
 1120: 					memset(&execute_data, 0, sizeof(zend_execute_data));
 1121: 					EG(current_execute_data) = &execute_data;
 1122: 					EX(function_state).function = pce->constructor;
 1123: 					zend_call_method_with_1_params(&ref, pce, &pce->constructor, "__construct", NULL, arg);
 1124: 
 1125: 					if (EG(exception)) {
 1126: 						zval *msg = zend_read_property(zend_exception_get_default(TSRMLS_C), EG(exception), "message", sizeof("message")-1, 0 TSRMLS_CC);
 1127: 						zend_printf("Exception: %s\n", Z_STRVAL_P(msg));
 1128: 						zval_ptr_dtor(&EG(exception));
 1129: 						EG(exception) = NULL;
 1130: 					} else {
 1131: 						zend_call_method_with_1_params(NULL, reflection_ptr, NULL, "export", NULL, ref);
 1132: 					}
 1133: 					zval_ptr_dtor(&ref);
 1134: 					zval_ptr_dtor(&arg);
 1135: 
 1136: 					break;
 1137: 				}
 1138: 			case PHP_MODE_REFLECTION_EXT_INFO:
 1139: 				{
 1140: 					int len = strlen(reflection_what);
 1141: 					char *lcname = zend_str_tolower_dup(reflection_what, len);
 1142: 					zend_module_entry *module;
 1143: 
 1144: 					if (zend_hash_find(&module_registry, lcname, len+1, (void**)&module) == FAILURE) {
 1145: 						if (!strcmp(reflection_what, "main")) {
 1146: 							display_ini_entries(NULL);
 1147: 						} else {
 1148: 							zend_printf("Extension '%s' not present.\n", reflection_what);
 1149: 							exit_status = 1;
 1150: 						}
 1151: 					} else {
 1152: 						php_info_print_module(module TSRMLS_CC);
 1153: 					}
 1154: 					
 1155: 					efree(lcname);
 1156: 					break;
 1157: 				}
 1158: 			case PHP_MODE_SHOW_INI_CONFIG:
 1159: 				{
 1160: 					zend_printf("Configuration File (php.ini) Path: %s\n", PHP_CONFIG_FILE_PATH);
 1161: 					zend_printf("Loaded Configuration File:         %s\n", php_ini_opened_path ? php_ini_opened_path : "(none)");
 1162: 					zend_printf("Scan for additional .ini files in: %s\n", php_ini_scanned_path  ? php_ini_scanned_path : "(none)");
 1163: 					zend_printf("Additional .ini files parsed:      %s\n", php_ini_scanned_files ? php_ini_scanned_files : "(none)");
 1164: 					break;
 1165: 				}
 1166: 		}
 1167: 	} zend_end_try();
 1168: 
 1169: out:
 1170: 	if (request_started) {
 1171: 		php_request_shutdown((void *) 0);
 1172: 	}
 1173: 	if (translated_path) {
 1174: 		free(translated_path);
 1175: 	}
 1176: 	if (exit_status == 0) {
 1177: 		exit_status = EG(exit_status);
 1178: 	}
 1179: 	return exit_status;
 1180: err:
 1181: 	sapi_deactivate(TSRMLS_C);
 1182: 	zend_ini_deactivate(TSRMLS_C);
 1183: 	exit_status = 1;
 1184: 	goto out;
 1185: }
 1186: /* }}} */
 1187: 
 1188: /* {{{ main
 1189:  */
 1190: #ifdef PHP_CLI_WIN32_NO_CONSOLE
 1191: int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)
 1192: #else
 1193: int main(int argc, char *argv[])
 1194: #endif
 1195: {
 1196: #ifdef ZTS
 1197: 	void ***tsrm_ls;
 1198: #endif
 1199: #ifdef PHP_CLI_WIN32_NO_CONSOLE
 1200: 	int argc = __argc;
 1201: 	char **argv = __argv;
 1202: #endif
 1203: 	int c;
 1204: 	int exit_status = SUCCESS;
 1205: 	int module_started = 0, sapi_started = 0;
 1206: 	char *php_optarg = NULL;
 1207: 	int php_optind = 1, use_extended_info = 0;
 1208: 	char *ini_path_override = NULL;
 1209: 	char *ini_entries = NULL;
 1210: 	int ini_entries_len = 0;
 1211: 	int ini_ignore = 0;
 1212: 	sapi_module_struct *sapi_module = &cli_sapi_module;
 1213: 
 1214: 	cli_sapi_module.additional_functions = additional_functions;
 1215: 
 1216: #if defined(PHP_WIN32) && defined(_DEBUG) && defined(PHP_WIN32_DEBUG_HEAP)
 1217: 	{
 1218: 		int tmp_flag;
 1219: 		_CrtSetReportMode(_CRT_WARN, _CRTDBG_MODE_FILE);
 1220: 		_CrtSetReportFile(_CRT_WARN, _CRTDBG_FILE_STDERR);
 1221: 		_CrtSetReportMode(_CRT_ERROR, _CRTDBG_MODE_FILE);
 1222: 		_CrtSetReportFile(_CRT_ERROR, _CRTDBG_FILE_STDERR);
 1223: 		_CrtSetReportMode(_CRT_ASSERT, _CRTDBG_MODE_FILE);
 1224: 		_CrtSetReportFile(_CRT_ASSERT, _CRTDBG_FILE_STDERR);
 1225: 		tmp_flag = _CrtSetDbgFlag(_CRTDBG_REPORT_FLAG);
 1226: 		tmp_flag |= _CRTDBG_DELAY_FREE_MEM_DF;
 1227: 		tmp_flag |= _CRTDBG_LEAK_CHECK_DF;
 1228: 
 1229: 		_CrtSetDbgFlag(tmp_flag);
 1230: 	}
 1231: #endif
 1232: 
 1233: #ifdef HAVE_SIGNAL_H
 1234: #if defined(SIGPIPE) && defined(SIG_IGN)
 1235: 	signal(SIGPIPE, SIG_IGN); /* ignore SIGPIPE in standalone mode so
 1236: 								that sockets created via fsockopen()
 1237: 								don't kill PHP if the remote site
 1238: 								closes it.  in apache|apxs mode apache
 1239: 								does that for us!  thies@thieso.net
 1240: 								20000419 */
 1241: #endif
 1242: #endif
 1243: 
 1244: 
 1245: #ifdef ZTS
 1246: 	tsrm_startup(1, 1, 0, NULL);
 1247: 	tsrm_ls = ts_resource(0);
 1248: #endif
 1249: 
 1250: #ifdef PHP_WIN32
 1251: 	_fmode = _O_BINARY;			/*sets default for file streams to binary */
 1252: 	setmode(_fileno(stdin), O_BINARY);		/* make the stdio mode be binary */
 1253: 	setmode(_fileno(stdout), O_BINARY);		/* make the stdio mode be binary */
 1254: 	setmode(_fileno(stderr), O_BINARY);		/* make the stdio mode be binary */
 1255: #endif
 1256: 
 1257: 	while ((c = php_getopt(argc, argv, OPTIONS, &php_optarg, &php_optind, 0, 2))!=-1) {
 1258: 		switch (c) {
 1259: 			case 'c':
 1260: 				if (ini_path_override) {
 1261: 					free(ini_path_override);
 1262: 				}
 1263:  				ini_path_override = strdup(php_optarg);
 1264: 				break;
 1265: 			case 'n':
 1266: 				ini_ignore = 1;
 1267: 				break;
 1268: 			case 'd': {
 1269: 				/* define ini entries on command line */
 1270: 				int len = strlen(php_optarg);
 1271: 				char *val;
 1272: 
 1273: 				if ((val = strchr(php_optarg, '='))) {
 1274: 					val++;
 1275: 					if (!isalnum(*val) && *val != '"' && *val != '\'' && *val != '\0') {
 1276: 						ini_entries = realloc(ini_entries, ini_entries_len + len + sizeof("\"\"\n\0"));
 1277: 						memcpy(ini_entries + ini_entries_len, php_optarg, (val - php_optarg));
 1278: 						ini_entries_len += (val - php_optarg);
 1279: 						memcpy(ini_entries + ini_entries_len, "\"", 1);
 1280: 						ini_entries_len++;
 1281: 						memcpy(ini_entries + ini_entries_len, val, len - (val - php_optarg));
 1282: 						ini_entries_len += len - (val - php_optarg);
 1283: 						memcpy(ini_entries + ini_entries_len, "\"\n\0", sizeof("\"\n\0"));
 1284: 						ini_entries_len += sizeof("\n\0\"") - 2;
 1285: 					} else {
 1286: 						ini_entries = realloc(ini_entries, ini_entries_len + len + sizeof("\n\0"));
 1287: 						memcpy(ini_entries + ini_entries_len, php_optarg, len);
 1288: 						memcpy(ini_entries + ini_entries_len + len, "\n\0", sizeof("\n\0"));
 1289: 						ini_entries_len += len + sizeof("\n\0") - 2;
 1290: 					}
 1291: 				} else {
 1292: 					ini_entries = realloc(ini_entries, ini_entries_len + len + sizeof("=1\n\0"));
 1293: 					memcpy(ini_entries + ini_entries_len, php_optarg, len);
 1294: 					memcpy(ini_entries + ini_entries_len + len, "=1\n\0", sizeof("=1\n\0"));
 1295: 					ini_entries_len += len + sizeof("=1\n\0") - 2;
 1296: 				}
 1297: 				break;
 1298: 			}
 1299: #ifndef PHP_CLI_WIN32_NO_CONSOLE
 1300: 			case 'S':
 1301: 				sapi_module = &cli_server_sapi_module;
 1302: 				break;
 1303: #endif
 1304: 			case 'h': /* help & quit */
 1305: 			case '?':
 1306: 				php_cli_usage(argv[0]);
 1307: 				goto out;
 1308: 			case 'i': case 'v': case 'm':
 1309: 				sapi_module = &cli_sapi_module;
 1310: 				goto exit_loop;
 1311: 			case 'e': /* enable extended info output */
 1312: 				use_extended_info = 1;
 1313: 				break;
 1314: 		}
 1315: 	}
 1316: exit_loop:
 1317: 
 1318: 	sapi_module->ini_defaults = sapi_cli_ini_defaults;
 1319: 	sapi_module->php_ini_path_override = ini_path_override;
 1320: 	sapi_module->phpinfo_as_text = 1;
 1321: 	sapi_module->php_ini_ignore_cwd = 1;
 1322: 	sapi_startup(sapi_module);
 1323: 	sapi_started = 1;
 1324: 
 1325: 	sapi_module->php_ini_ignore = ini_ignore;
 1326: 
 1327: 	sapi_module->executable_location = argv[0];
 1328: 
 1329: 	if (sapi_module == &cli_sapi_module) {
 1330: 		if (ini_entries) {
 1331: 			ini_entries = realloc(ini_entries, ini_entries_len + sizeof(HARDCODED_INI));
 1332: 			memmove(ini_entries + sizeof(HARDCODED_INI) - 2, ini_entries, ini_entries_len + 1);
 1333: 			memcpy(ini_entries, HARDCODED_INI, sizeof(HARDCODED_INI) - 2);
 1334: 		} else {
 1335: 			ini_entries = malloc(sizeof(HARDCODED_INI));
 1336: 			memcpy(ini_entries, HARDCODED_INI, sizeof(HARDCODED_INI));
 1337: 		}
 1338: 		ini_entries_len += sizeof(HARDCODED_INI) - 2;
 1339: 	}
 1340: 
 1341: 	sapi_module->ini_entries = ini_entries;
 1342: 
 1343: 	/* startup after we get the above ini override se we get things right */
 1344: 	if (sapi_module->startup(sapi_module) == FAILURE) {
 1345: 		/* there is no way to see if we must call zend_ini_deactivate()
 1346: 		 * since we cannot check if EG(ini_directives) has been initialised
 1347: 		 * because the executor's constructor does not set initialize it.
 1348: 		 * Apart from that there seems no need for zend_ini_deactivate() yet.
 1349: 		 * So we goto out_err.*/
 1350: 		exit_status = 1;
 1351: 		goto out;
 1352: 	}
 1353: 	module_started = 1;
 1354: 	
 1355: 	/* -e option */
 1356: 	if (use_extended_info) {
 1357: 		CG(compiler_options) |= ZEND_COMPILE_EXTENDED_INFO;
 1358: 	}
 1359: 
 1360: 	zend_first_try {
 1361: #ifndef PHP_CLI_WIN32_NO_CONSOLE
 1362: 		if (sapi_module == &cli_sapi_module) {
 1363: #endif
 1364: 			exit_status = do_cli(argc, argv TSRMLS_CC);
 1365: #ifndef PHP_CLI_WIN32_NO_CONSOLE
 1366: 		} else {
 1367: 			exit_status = do_cli_server(argc, argv TSRMLS_CC);
 1368: 		}
 1369: #endif
 1370: 	} zend_end_try();
 1371: out:
 1372: 	if (ini_path_override) {
 1373: 		free(ini_path_override);
 1374: 	}
 1375: 	if (ini_entries) {
 1376: 		free(ini_entries);
 1377: 	}
 1378: 	if (module_started) {
 1379: 		php_module_shutdown(TSRMLS_C);
 1380: 	}
 1381: 	if (sapi_started) {
 1382: 		sapi_shutdown();
 1383: 	}
 1384: #ifdef ZTS
 1385: 	tsrm_shutdown();
 1386: #endif
 1387: 
 1388: 	exit(exit_status);
 1389: }
 1390: /* }}} */
 1391: 
 1392: /*
 1393:  * Local variables:
 1394:  * tab-width: 4
 1395:  * c-basic-offset: 4
 1396:  * End:
 1397:  * vim600: sw=4 ts=4 fdm=marker
 1398:  * vim<600: sw=4 ts=4
 1399:  */

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