From 8506ee1bfe569364c552118d69d82516b6a17a12 Mon Sep 17 00:00:00 2001 From: foobar Date: Sat, 31 May 2003 01:37:45 +0000 Subject: [PATCH] - Re-added the FD lexer patch. # Marcus is going to add the long-opts patches back in after this. --- NEWS | 4 +- ext/standard/basic_functions.c | 2 +- ext/standard/browscap.c | 2 +- main/main.c | 14 +- main/php_ini.c | 3 +- main/php_streams.h | 3 + main/streams.c | 39 +++ sapi/aolserver/aolserver.c | 2 +- sapi/apache/mod_php4.c | 2 +- sapi/apache/sapi_apache.c | 2 +- sapi/apache2filter/sapi_apache2.c | 2 +- sapi/apache2handler/sapi_apache2.c | 2 +- sapi/caudium/caudium.c | 2 +- sapi/cgi/cgi_main.c | 91 +++--- sapi/cgi/getopt.c | 293 ++++++++++--------- sapi/cgi/php_getopt.h | 31 +- sapi/cli/README | 1 - sapi/cli/getopt.c | 293 ++++++++++--------- sapi/cli/php_cli.c | 443 +++++++++-------------------- sapi/cli/php_getopt.h | 16 +- sapi/isapi/php4isapi.c | 2 +- sapi/phttpd/phttpd.c | 2 +- sapi/pi3web/pi3web_sapi.c | 2 +- sapi/roxen/roxen.c | 2 +- sapi/servlet/servlet.c | 2 +- sapi/thttpd/thttpd.c | 2 +- sapi/tux/php_tux.c | 2 +- sapi/webjames/webjames.c | 2 +- 28 files changed, 563 insertions(+), 700 deletions(-) diff --git a/NEWS b/NEWS index 0ba1d0d765..4268beaca5 100644 --- a/NEWS +++ b/NEWS @@ -1,6 +1,7 @@ PHP 4 NEWS ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||| ?? Jul 2003, Version 4.3.3 +- Improved the engine to use POSIX/socket IO where feasible. (Sascha) - Improved NSAPI SAPI module (Uwe Schindler) . php4_init (magnus.conf): new parameter to set alternate path to php.ini. (php_ini="/path/to/php.ini") @@ -9,9 +10,6 @@ PHP 4 NEWS (See sapi/nsapi/nsapi-readme.txt for more information) . Added support for virtual() . Synced $_SERVER variables to be similar to Apache variables -- Added long options into CLI & CGI (e.g. --version). (Marcus) -- Added new command line parameters -B, -F, -R and -E which allow to process - stdin line by line (See 'php -h' or 'man php' for more). (Marcus) - Added DBA handler 'inifile' to support ini files. (Marcus) - Added a "DEBUG" note to 'php -v' output when --enable-debug is used. (Derick) - Fixed ext/yaz to not log unless yaz.log_file is set. (Adam Dickmeiss) diff --git a/ext/standard/basic_functions.c b/ext/standard/basic_functions.c index 7686bd3370..141922ad98 100644 --- a/ext/standard/basic_functions.c +++ b/ext/standard/basic_functions.c @@ -2840,7 +2840,7 @@ static void php_ini_parser_cb_with_sections(zval *arg1, zval *arg2, int callback PHP_FUNCTION(parse_ini_file) { zval **filename, **process_sections; - zend_file_handle fh; + zend_file_handle fh = {0}; zend_ini_parser_cb_t ini_parser_cb; switch (ARG_COUNT(ht)) { diff --git a/ext/standard/browscap.c b/ext/standard/browscap.c index abf63f4c8c..ca14e7a736 100644 --- a/ext/standard/browscap.c +++ b/ext/standard/browscap.c @@ -150,7 +150,7 @@ PHP_MINIT_FUNCTION(browscap) char *browscap = INI_STR("browscap"); if (browscap) { - zend_file_handle fh; + zend_file_handle fh = {0}; if (zend_hash_init(&browser_hash, 0, NULL, (dtor_func_t) browscap_entry_dtor, 1)==FAILURE) { return FAILURE; diff --git a/main/main.c b/main/main.c index 9aa567b07c..27484151c9 100644 --- a/main/main.c +++ b/main/main.c @@ -763,6 +763,17 @@ static FILE *php_fopen_wrapper_for_zend(const char *filename, char **opened_path } /* }}} */ + +/* {{{ php_open_wrapper_for_zend + */ +static zend_bool php_open_wrapper_for_zend(const char *filename, struct _zend_file_handle *fh) +{ + TSRMLS_FETCH(); + + return php_stream_open_wrapper_as_file_handle((char *)filename, "rb", ENFORCE_SAFE_MODE|USE_PATH|IGNORE_URL_WIN|REPORT_ERRORS|STREAM_OPEN_FOR_INCLUDE, fh); +} +/* }}} */ + /* {{{ php_get_configuration_directive_for_zend */ static int php_get_configuration_directive_for_zend(char *name, uint name_length, zval *contents) @@ -1096,6 +1107,7 @@ int php_module_startup(sapi_module_struct *sf, zend_module_entry *additional_mod zuf.printf_function = php_printf; zuf.write_function = php_body_write_wrapper; zuf.fopen_function = php_fopen_wrapper_for_zend; + zuf.open_function = php_open_wrapper_for_zend; zuf.message_handler = php_message_handler_for_zend; zuf.block_interruptions = sapi_module.block_interruptions; zuf.unblock_interruptions = sapi_module.unblock_interruptions; @@ -1591,7 +1603,7 @@ PHPAPI int php_handle_special_queries(TSRMLS_D) PHPAPI int php_execute_script(zend_file_handle *primary_file TSRMLS_DC) { zend_file_handle *prepend_file_p, *append_file_p; - zend_file_handle prepend_file, append_file; + zend_file_handle prepend_file = {0}, append_file = {0}; #if HAVE_BROKEN_GETCWD int old_cwd_fd = -1; #else diff --git a/main/php_ini.c b/main/php_ini.c index 2be3459577..c30818965b 100644 --- a/main/php_ini.c +++ b/main/php_ini.c @@ -236,7 +236,7 @@ int php_init_config() int safe_mode_state; char *open_basedir; int free_ini_search_path=0; - zend_file_handle fh; + zend_file_handle fh = {0}; struct stat sb; char ini_file[MAXPATHLEN]; char *p; @@ -347,7 +347,6 @@ int php_init_config() PG(safe_mode) = 0; PG(open_basedir) = NULL; - memset(&fh, 0, sizeof(fh)); /* Check if php_ini_path_override is a file */ if (!sapi_module.php_ini_ignore) { if (sapi_module.php_ini_path_override && sapi_module.php_ini_path_override[0]) { diff --git a/main/php_streams.h b/main/php_streams.h index 9af2679cf2..83e8c8b71c 100755 --- a/main/php_streams.h +++ b/main/php_streams.h @@ -554,6 +554,9 @@ PHPAPI FILE * _php_stream_open_wrapper_as_file(char * path, char * mode, int opt #define php_stream_open_wrapper_as_file(path, mode, options, opened_path) _php_stream_open_wrapper_as_file((path), (mode), (options), (opened_path) STREAMS_CC TSRMLS_CC) +PHPAPI zend_bool _php_stream_open_wrapper_as_file_handle(char * path, char * mode, int options, zend_file_handle * STREAMS_DC TSRMLS_DC); +#define php_stream_open_wrapper_as_file_handle(path, mode, options, fh) _php_stream_open_wrapper_as_file_handle((path), (mode), (options), (fh) STREAMS_CC TSRMLS_CC) + /* for user-space streams */ PHPAPI extern php_stream_ops php_stream_userspace_ops; PHPAPI extern php_stream_ops php_stream_userspace_dir_ops; diff --git a/main/streams.c b/main/streams.c index 4af3c21f8b..3d3c42cee2 100755 --- a/main/streams.c +++ b/main/streams.c @@ -2728,6 +2728,45 @@ PHPAPI FILE * _php_stream_open_wrapper_as_file(char *path, char *mode, int optio +/* {{{ php_stream_open_wrapper_as_file_handle */ +PHPAPI zend_bool _php_stream_open_wrapper_as_file_handle(char *path, char *mode, int options, zend_file_handle *fh STREAMS_DC TSRMLS_DC) +{ + php_stream *stream = NULL; + int is_sock = 0; + + stream = php_stream_open_wrapper_rel(path, mode, options|STREAM_WILL_CAST, &fh->opened_path); + + if (stream == NULL) + return FAILURE; + + if ((options & STREAM_OPEN_FOR_INCLUDE) + && php_stream_is(stream, PHP_STREAM_IS_SOCKET)) { + is_sock = 1; + } + + if (php_stream_can_cast(stream, PHP_STREAM_AS_FD) == SUCCESS && + php_stream_cast(stream, PHP_STREAM_AS_FD | PHP_STREAM_CAST_TRY_HARD + | PHP_STREAM_CAST_RELEASE, (void **) &fh->handle.fd, + REPORT_ERRORS) == SUCCESS) { + if (is_sock) { + fh->type = ZEND_HANDLE_SOCKET_FD; + } else { + fh->type = ZEND_HANDLE_FD; + } + } else if (php_stream_cast(stream, PHP_STREAM_AS_STDIO + |PHP_STREAM_CAST_TRY_HARD | PHP_STREAM_CAST_RELEASE, + (void**) &fh->handle.fp, REPORT_ERRORS) == SUCCESS) { + fh->type = ZEND_HANDLE_FP; + } else { + php_stream_close(stream); + if (fh->opened_path) + efree(fh->opened_path); + fh->opened_path = NULL; + return FAILURE; + } + return SUCCESS; +} +/* }}} */ /* {{{ php_stream_make_seekable */ PHPAPI int _php_stream_make_seekable(php_stream *origstream, php_stream **newstream, int flags STREAMS_DC TSRMLS_DC) diff --git a/sapi/aolserver/aolserver.c b/sapi/aolserver/aolserver.c index 665dd4190c..ea2f2ab593 100644 --- a/sapi/aolserver/aolserver.c +++ b/sapi/aolserver/aolserver.c @@ -404,7 +404,7 @@ static sapi_module_struct aolserver_sapi_module = { static int php_ns_module_main(TSRMLS_D) { - zend_file_handle file_handle; + zend_file_handle file_handle = {0}; file_handle.type = ZEND_HANDLE_FILENAME; file_handle.filename = SG(request_info).path_translated; diff --git a/sapi/apache/mod_php4.c b/sapi/apache/mod_php4.c index d5f59b21d2..a0cbee47fd 100644 --- a/sapi/apache/mod_php4.c +++ b/sapi/apache/mod_php4.c @@ -534,7 +534,7 @@ static int send_php(request_rec *r, int display_source_mode, char *filename) TSRMLS_FETCH(); if (AP(in_request)) { - zend_file_handle fh; + zend_file_handle fh = {0}; fh.filename = r->filename; fh.opened_path = NULL; diff --git a/sapi/apache/sapi_apache.c b/sapi/apache/sapi_apache.c index 8b651de012..3075f25d8f 100644 --- a/sapi/apache/sapi_apache.c +++ b/sapi/apache/sapi_apache.c @@ -28,7 +28,7 @@ int apache_php_module_main(request_rec *r, int display_source_mode TSRMLS_DC) { int retval = OK; - zend_file_handle file_handle; + zend_file_handle file_handle = {0}; if (php_request_startup(TSRMLS_C) == FAILURE) { return FAILURE; diff --git a/sapi/apache2filter/sapi_apache2.c b/sapi/apache2filter/sapi_apache2.c index 5e603edee9..f201983d54 100644 --- a/sapi/apache2filter/sapi_apache2.c +++ b/sapi/apache2filter/sapi_apache2.c @@ -452,7 +452,7 @@ static int php_output_filter(ap_filter_t *f, apr_bucket_brigade *bb) } for (b = APR_BRIGADE_FIRST(bb); b != APR_BRIGADE_SENTINEL(bb); b = APR_BUCKET_NEXT(b)) { - zend_file_handle zfd; + zend_file_handle zfd = {0}; if (!ctx->request_processed && APR_BUCKET_IS_FILE(b)) { const char *path; diff --git a/sapi/apache2handler/sapi_apache2.c b/sapi/apache2handler/sapi_apache2.c index 91399b351f..81ea318130 100644 --- a/sapi/apache2handler/sapi_apache2.c +++ b/sapi/apache2handler/sapi_apache2.c @@ -514,7 +514,7 @@ static int php_handler(request_rec *r) php_get_highlight_struct(&syntax_highlighter_ini); highlight_file((char *)r->filename, &syntax_highlighter_ini TSRMLS_CC); } else { - zend_file_handle zfd; + zend_file_handle zfd = {0}; zfd.type = ZEND_HANDLE_FILENAME; zfd.filename = (char *) r->filename; diff --git a/sapi/caudium/caudium.c b/sapi/caudium/caudium.c index 6b61060c88..1914430638 100644 --- a/sapi/caudium/caudium.c +++ b/sapi/caudium/caudium.c @@ -564,7 +564,7 @@ static sapi_module_struct caudium_sapi_module = { static void php_caudium_module_main(php_caudium_request *ureq) { int res; - zend_file_handle file_handle; + zend_file_handle file_handle = {0}; #ifndef USE_PIKE_LEVEL_THREADS struct thread_state *state; extern struct program *thread_id_prog; diff --git a/sapi/cgi/cgi_main.c b/sapi/cgi/cgi_main.c index 2173952ff8..9b4ef08bc9 100644 --- a/sapi/cgi/cgi_main.c +++ b/sapi/cgi/cgi_main.c @@ -117,31 +117,8 @@ static pid_t pgroup; #define PHP_MODE_LINT 4 #define PHP_MODE_STRIP 5 -static char *optarg = NULL; -static int optind = 1; - -static const opt_struct OPTIONS[] = { - {'a', 0, "interactive"}, - {'C', 0, "no-chdir"}, - {'c', 1, "php-ini"}, - {'d', 1, "define"}, - {'e', 0, "profile-info"}, - {'f', 1, "file"}, - {'g', 1, "global"}, - {'h', 0, "help"}, - {'i', 0, "info"}, - {'l', 0, "syntax-check"}, - {'m', 0, "modules"}, - {'n', 0, "no-php-ini"}, - {'q', 0, "no-header"}, - {'s', 0, "syntax-highlight"}, - {'s', 0, "syntax-highlighting"}, - {'w', 0, "strip"}, - {'?', 0, "usage"},/* help alias (both '?' and 'usage') */ - {'v', 0, "version"}, - {'z', 1, "zend-extension"}, - {'-', 0, NULL} /* end of args */ -}; +extern char *ap_php_optarg; +extern int ap_php_optind; #if ENABLE_PATHINFO_CHECK /* true global. this is retreived once only, even for fastcgi */ @@ -161,6 +138,8 @@ long fix_pathinfo=1; #define TRANSLATE_SLASHES(path) #endif +#define OPTSTRING "ab:Cc:d:ef:g:hilmnqsw?vz:" + static int print_module_info(zend_module_entry *module, void *arg TSRMLS_DC) { php_printf("%s\n", module->name); @@ -912,14 +891,14 @@ int main(int argc, char *argv[]) { int exit_status = SUCCESS; int cgi = 0, c, i, len; - zend_file_handle file_handle; + zend_file_handle file_handle = {0}; int retval = FAILURE; char *s; /* temporary locals */ int behavior=PHP_MODE_STANDARD; int no_headers=0; - int orig_optind=optind; - char *orig_optarg=optarg; + int orig_optind=ap_php_optind; + char *orig_optarg=ap_php_optarg; char *script_file=NULL; zend_llist global_vars; int interactive=0; @@ -995,10 +974,10 @@ int main(int argc, char *argv[]) /* allow ini override for fastcgi */ #endif ) { - while ((c=php_getopt(argc, argv, OPTIONS, &optarg, &optind, 0))!=-1) { + while ((c=ap_php_getopt(argc, argv, OPTSTRING))!=-1) { switch (c) { case 'c': - cgi_sapi_module.php_ini_path_override = strdup(optarg); + cgi_sapi_module.php_ini_path_override = strdup(ap_php_optarg); break; case 'n': cgi_sapi_module.php_ini_ignore = 1; @@ -1009,15 +988,15 @@ int main(int argc, char *argv[]) server by accepting a bindpath parameter. */ case 'b': if (!fastcgi) { - bindpath = strdup(optarg); + bindpath = strdup(ap_php_optarg); } break; #endif } } - optind = orig_optind; - optarg = orig_optarg; + ap_php_optind = orig_optind; + ap_php_optarg = orig_optarg; } #ifdef ZTS @@ -1211,9 +1190,8 @@ consult the installation file that came with this distribution, or visit \n\ && !fastcgi #endif ) { - while ((c=php_getopt(argc, argv, OPTIONS, &optarg, &optind, 1))!=-1) { + while ((c=ap_php_getopt(argc, argv, OPTSTRING))!=-1) { switch (c) { - case 'h': case '?': no_headers = 1; php_output_startup(); @@ -1225,8 +1203,8 @@ consult the installation file that came with this distribution, or visit \n\ break; } } - optind = orig_optind; - optarg = orig_optarg; + ap_php_optind = orig_optind; + ap_php_optarg = orig_optarg; } #if PHP_FASTCGI @@ -1276,7 +1254,7 @@ consult the installation file that came with this distribution, or visit \n\ exit(1); } - while ((c = php_getopt(argc, argv, OPTIONS, &optarg, &optind, 0)) != -1) { + while ((c = ap_php_getopt(argc, argv, OPTSTRING)) != -1) { switch (c) { case 'a': /* interactive mode */ @@ -1288,7 +1266,7 @@ consult the installation file that came with this distribution, or visit \n\ SG(options) |= SAPI_OPTION_NO_CHDIR; break; case 'd': /* define ini entries on command line */ - define_command_line_ini_entry(optarg); + define_command_line_ini_entry(ap_php_optarg); break; case 'e': /* enable extended info output */ @@ -1296,21 +1274,32 @@ consult the installation file that came with this distribution, or visit \n\ break; case 'f': /* parse file */ - script_file = estrdup(optarg); + script_file = estrdup(ap_php_optarg); no_headers = 1; /* arguments after the file are considered script args */ - SG(request_info).argc = argc - (optind-1); - SG(request_info).argv = &argv[optind-1]; + SG(request_info).argc = argc - (ap_php_optind-1); + SG(request_info).argv = &argv[ap_php_optind-1]; break; case 'g': /* define global variables on command line */ { - char *arg = estrdup(optarg); + char *arg = estrdup(ap_php_optarg); zend_llist_add_element(&global_vars, &arg); } break; + case 'h': /* help & quit */ + case '?': + no_headers = 1; + php_output_startup(); + php_output_activate(TSRMLS_C); + SG(headers_sent) = 1; + php_cgi_usage(argv[0]); + php_end_ob_buffers(1 TSRMLS_CC); + exit(1); + break; + case 'i': /* php info & quit */ if (php_request_startup(TSRMLS_C) == FAILURE) { php_module_shutdown(TSRMLS_C); @@ -1381,7 +1370,7 @@ consult the installation file that came with this distribution, or visit \n\ break; case 'z': /* load extension file */ - zend_load_extension(optarg); + zend_load_extension(ap_php_optarg); break; default: @@ -1399,12 +1388,12 @@ consult the installation file that came with this distribution, or visit \n\ SG(request_info).no_headers = 1; } - if (!SG(request_info).path_translated && argc > optind) { + if (!SG(request_info).path_translated && argc > ap_php_optind) { /* arguments after the file are considered script args */ - SG(request_info).argc = argc - optind; - SG(request_info).argv = &argv[optind]; + SG(request_info).argc = argc - ap_php_optind; + SG(request_info).argv = &argv[ap_php_optind]; /* file is on command line, but not in -f opt */ - SG(request_info).path_translated = estrdup(argv[optind++]); + SG(request_info).path_translated = estrdup(argv[ap_php_optind++]); } /* all remaining arguments are part of the query string @@ -1416,15 +1405,15 @@ consult the installation file that came with this distribution, or visit \n\ test.php "v1=test&v2=hello world!" test.php v1=test "v2=hello world!" */ - if (!SG(request_info).query_string && argc > optind) { + if (!SG(request_info).query_string && argc > ap_php_optind) { len = 0; - for (i = optind; i < argc; i++) { + for (i = ap_php_optind; i < argc; i++) { len += strlen(argv[i]) + 1; } s = malloc(len + 1); /* leak - but only for command line version, so ok */ *s = '\0'; /* we are pretending it came from the environment */ - for (i = optind, len = 0; i < argc; i++) { + for (i = ap_php_optind, len = 0; i < argc; i++) { strcat(s, argv[i]); if (i < (argc - 1)) { strcat(s, "&"); diff --git a/sapi/cgi/getopt.c b/sapi/cgi/getopt.c index e048e95058..a26ca3c431 100644 --- a/sapi/cgi/getopt.c +++ b/sapi/cgi/getopt.c @@ -1,20 +1,4 @@ -/* - +----------------------------------------------------------------------+ - | PHP Version 4 | - +----------------------------------------------------------------------+ - | Copyright (c) 1997-2003 The PHP Group | - +----------------------------------------------------------------------+ - | This source file is subject to version 2.02 of the PHP license, | - | that is bundled with this package in the file LICENSE, and is | - | available at through the world-wide-web at | - | http://www.php.net/license/2_02.txt. | - | If you did not receive a copy of the PHP license and are unable to | - | obtain it through the world-wide-web, please send a note to | - | license@php.net so we can mail you a copy immediately. | - +----------------------------------------------------------------------+ - | Author: Marcus Boerger | - +----------------------------------------------------------------------+ -*/ +/* Borrowed from Apache NT Port */ #include #include @@ -26,129 +10,166 @@ #define OPTERRARG (3) -static int php_opt_error(int argc, char * const *argv, int oint, int optchr, int err, int show_err) +char *ap_php_optarg; +int ap_php_optind = 1; +static int ap_php_opterr = 1; +static int ap_php_optopt; + +static int +ap_php_optiserr(int argc, char * const *argv, int oint, const char *optstr, + int optchr, int err) { - if (show_err) - { - fprintf(stderr, "Error in argument %d, char %d: ", oint, optchr+1); - switch(err) - { - case OPTERRCOLON: - fprintf(stderr, ": in flags\n"); - break; - case OPTERRNF: - fprintf(stderr, "option not found %c\n", argv[oint][optchr]); - break; - case OPTERRARG: - fprintf(stderr, "no argument for option %c\n", argv[oint][optchr]); - break; - default: - fprintf(stderr, "unknown\n"); - break; - } - } - return('?'); + if (ap_php_opterr) + { + fprintf(stderr, "Error in argument %d, char %d: ", oint, optchr+1); + switch(err) + { + case OPTERRCOLON: + fprintf(stderr, ": in flags\n"); + break; + case OPTERRNF: + fprintf(stderr, "option not found %c\n", argv[oint][optchr]); + break; + case OPTERRARG: + fprintf(stderr, "no argument for option %c\n", argv[oint][optchr]); + break; + default: + fprintf(stderr, "unknown\n"); + break; + } + } + ap_php_optopt = argv[oint][optchr]; + return('?'); } - -int php_getopt(int argc, char* const *argv, const opt_struct opts[], char **optarg, int *optind, int show_err) + +int ap_php_getopt(int argc, char* const *argv, const char *optstr) { - static int optchr = 0; - static int dash = 0; /* have already seen the - */ - int arg_start = 2; + static int optchr = 0; + static int dash = 0; /* have already seen the - */ + + char *cp; - int opts_idx = -1; + if (ap_php_optind >= argc) + return(EOF); + if (!dash && (argv[ap_php_optind][0] != '-')) + return(EOF); + if (!dash && (argv[ap_php_optind][0] == '-') && !argv[ap_php_optind][1]) + { + /* + * use to specify stdin. Need to let pgm process this and + * the following args + */ + return(EOF); + } + if ((argv[ap_php_optind][0] == '-') && (argv[ap_php_optind][1] == '-')) + { + /* -- indicates end of args */ + ap_php_optind++; + return(EOF); + } + if (!dash) + { + assert((argv[ap_php_optind][0] == '-') && argv[ap_php_optind][1]); + dash = 1; + optchr = 1; + } - if (*optind >= argc) { - return(EOF); - } - if (!dash) { - if ((argv[*optind][0] != '-')) { - return(EOF); - } else { - if (!argv[*optind][1]) - { - /* - * use to specify stdin. Need to let pgm process this and - * the following args - */ - return(EOF); - } - } - } - if ((argv[*optind][0] == '-') && (argv[*optind][1] == '-')) { - /* '--' indicates end of args if not followed by a known long option name */ - while (1) { - opts_idx++; - if (opts[opts_idx].opt_char == '-') { - (*optind)++; - return(EOF); - } else if (opts[opts_idx].opt_name && !strcmp(&argv[*optind][2], opts[opts_idx].opt_name)) { - break; - } - } - optchr = 0; - dash = 1; - arg_start = 2 + strlen(opts[opts_idx].opt_name); - } - if (!dash) { - dash = 1; - optchr = 1; - } + /* Check if the guy tries to do a -: kind of flag */ + assert(dash); + if (argv[ap_php_optind][optchr] == ':') + { + dash = 0; + ap_php_optind++; + return(ap_php_optiserr(argc, argv, ap_php_optind-1, optstr, optchr, OPTERRCOLON)); + } + if (!(cp = strchr(optstr, argv[ap_php_optind][optchr]))) + { + int errind = ap_php_optind; + int errchr = optchr; - /* Check if the guy tries to do a -: kind of flag */ - if (argv[*optind][optchr] == ':') { - dash = 0; - (*optind)++; - return (php_opt_error(argc, argv, *optind-1, optchr, OPTERRCOLON, show_err)); - } - if (opts_idx < 0) { - while (1) { - opts_idx++; - if (opts[opts_idx].opt_char == '-') { - int errind = *optind; - int errchr = optchr; - - if (!argv[*optind][optchr+1]) { - dash = 0; - (*optind)++; - } else { - optchr++; - } - return(php_opt_error(argc, argv, errind, errchr, OPTERRNF, show_err)); - } else if (argv[*optind][optchr] == opts[opts_idx].opt_char) { - break; - } - } - } - if (opts[opts_idx].need_param) { - /* Check for cases where the value of the argument - is in the form - or in the form - */ - dash = 0; - if(!argv[*optind][arg_start]) { - (*optind)++; - if (*optind == argc) { - return(php_opt_error(argc, argv, *optind-1, optchr, OPTERRARG, show_err)); - } - *optarg = argv[(*optind)++]; - } else { - *optarg = &argv[*optind][arg_start]; - (*optind)++; - } - return opts[opts_idx].opt_char; - } else { - if (arg_start == 2) { - if (!argv[*optind][optchr+1]) - { - dash = 0; - (*optind)++; - } else { - optchr++; - } - } else { - (*optind)++; - } - return opts[opts_idx].opt_char; - } - assert(0); - return(0); /* never reached */ + if (!argv[ap_php_optind][optchr+1]) + { + dash = 0; + ap_php_optind++; + } + else + optchr++; + return(ap_php_optiserr(argc, argv, errind, optstr, errchr, OPTERRNF)); + } + if (cp[1] == ':') + { + /* Check for cases where the value of the argument + is in the form - or in the form - */ + dash = 0; + if(!argv[ap_php_optind][2]) { + ap_php_optind++; + if (ap_php_optind == argc) + return(ap_php_optiserr(argc, argv, ap_php_optind-1, optstr, optchr, OPTERRARG)); + ap_php_optarg = argv[ap_php_optind++]; + } + else + { + ap_php_optarg = &argv[ap_php_optind][2]; + ap_php_optind++; + } + return(*cp); + } + else + { + if (!argv[ap_php_optind][optchr+1]) + { + dash = 0; + ap_php_optind++; + } + else + optchr++; + return(*cp); + } + assert(0); + return(0); /* never reached */ } + +#ifdef TESTGETOPT +int + main (int argc, char **argv) + { + int c; + extern char *ap_php_optarg; + extern int ap_php_optind; + int aflg = 0; + int bflg = 0; + int errflg = 0; + char *ofile = NULL; + + while ((c = ap_php_getopt(argc, argv, "abo:")) != EOF) + switch (c) { + case 'a': + if (bflg) + errflg++; + else + aflg++; + break; + case 'b': + if (aflg) + errflg++; + else + bflg++; + break; + case 'o': + ofile = ap_php_optarg; + (void)printf("ofile = %s\n", ofile); + break; + case '?': + errflg++; + } + if (errflg) { + (void)fprintf(stderr, + "usage: cmd [-a|-b] [-o ] files...\n"); + exit (2); + } + for ( ; ap_php_optind < argc; ap_php_optind++) + (void)printf("%s\n", argv[ap_php_optind]); + return 0; + } + +#endif /* TESTGETOPT */ diff --git a/sapi/cgi/php_getopt.h b/sapi/cgi/php_getopt.h index 1e2355ff63..40da432b59 100644 --- a/sapi/cgi/php_getopt.h +++ b/sapi/cgi/php_getopt.h @@ -1,30 +1,7 @@ -/* - +----------------------------------------------------------------------+ - | PHP Version 4 | - +----------------------------------------------------------------------+ - | Copyright (c) 1997-2003 The PHP Group | - +----------------------------------------------------------------------+ - | This source file is subject to version 2.02 of the PHP license, | - | that is bundled with this package in the file LICENSE, and is | - | available at through the world-wide-web at | - | http://www.php.net/license/2_02.txt. | - | If you did not receive a copy of the PHP license and are unable to | - | obtain it through the world-wide-web, please send a note to | - | license@php.net so we can mail you a copy immediately. | - +----------------------------------------------------------------------+ - | Author: Marcus Boerger | - +----------------------------------------------------------------------+ -*/ - +/* Borrowed from Apache NT Port */ #include "php.h" -/* Define structure for one recognized option (both single char and long name). - * If short_open is '-' this is the last option. - */ -typedef struct _opt_struct { - const char opt_char; - const int need_param; - const char * opt_name; -} opt_struct; +extern char *ap_php_optarg; +extern int ap_php_optind; -int php_getopt(int argc, char* const *argv, const opt_struct opts[], char **optarg, int *optind, int show_err); +int ap_php_getopt(int argc, char* const *argv, const char *optstr); diff --git a/sapi/cli/README b/sapi/cli/README index 9e519e9bd0..e6bf5cc6c6 100644 --- a/sapi/cli/README +++ b/sapi/cli/README @@ -16,5 +16,4 @@ The main differences between the two: * implicit_flush always on * -r option which allows execution of PHP code directly from the command line (e.g. php -r 'echo md5("test");' ) -* Other more sophisticated command line switches (see: man php) * max_execution_time is set to unlimited, overriding php.ini setting. diff --git a/sapi/cli/getopt.c b/sapi/cli/getopt.c index 01e532b7cb..f5874d577e 100644 --- a/sapi/cli/getopt.c +++ b/sapi/cli/getopt.c @@ -1,22 +1,4 @@ -/* - +----------------------------------------------------------------------+ - | PHP Version 4 | - +----------------------------------------------------------------------+ - | Copyright (c) 1997-2003 The PHP Group | - +----------------------------------------------------------------------+ - | This source file is subject to version 2.02 of the PHP license, | - | that is bundled with this package in the file LICENSE, and is | - | available at through the world-wide-web at | - | http://www.php.net/license/2_02.txt. | - | If you did not receive a copy of the PHP license and are unable to | - | obtain it through the world-wide-web, please send a note to | - | license@php.net so we can mail you a copy immediately. | - +----------------------------------------------------------------------+ - | Author: Marcus Boerger | - +----------------------------------------------------------------------+ -*/ - -/* $Id$ */ +/* Borrowed from Apache NT Port */ #include #include @@ -28,129 +10,164 @@ #define OPTERRARG (3) -static int php_opt_error(int argc, char * const *argv, int oint, int optchr, int err, int show_err) +char *ap_php_optarg; +int ap_php_optind = 1; +static int ap_php_opterr = 1; + +static int +ap_php_optiserr(int argc, char * const *argv, int oint, const char *optstr, + int optchr, int err) { - if (show_err) - { - fprintf(stderr, "Error in argument %d, char %d: ", oint, optchr+1); - switch(err) - { - case OPTERRCOLON: - fprintf(stderr, ": in flags\n"); - break; - case OPTERRNF: - fprintf(stderr, "option not found %c\n", argv[oint][optchr]); - break; - case OPTERRARG: - fprintf(stderr, "no argument for option %c\n", argv[oint][optchr]); - break; - default: - fprintf(stderr, "unknown\n"); - break; - } - } - return('?'); + if (ap_php_opterr) + { + fprintf(stderr, "Error in argument %d, char %d: ", oint, optchr+1); + switch(err) + { + case OPTERRCOLON: + fprintf(stderr, ": in flags\n"); + break; + case OPTERRNF: + fprintf(stderr, "option not found %c\n", argv[oint][optchr]); + break; + case OPTERRARG: + fprintf(stderr, "no argument for option %c\n", argv[oint][optchr]); + break; + default: + fprintf(stderr, "unknown\n"); + break; + } + } + return('?'); } - -int php_getopt(int argc, char* const *argv, const opt_struct opts[], char **optarg, int *optind, int show_err) + +int ap_php_getopt(int argc, char* const *argv, const char *optstr) { - static int optchr = 0; - static int dash = 0; /* have already seen the - */ - int arg_start = 2; + static int optchr = 0; + static int dash = 0; /* have already seen the - */ + + char *cp; - int opts_idx = -1; + if (ap_php_optind >= argc) + return(EOF); + if (!dash && (argv[ap_php_optind][0] != '-')) + return(EOF); + if (!dash && (argv[ap_php_optind][0] == '-') && !argv[ap_php_optind][1]) + { + /* + * use to specify stdin. Need to let pgm process this and + * the following args + */ + return(EOF); + } + if ((argv[ap_php_optind][0] == '-') && (argv[ap_php_optind][1] == '-')) + { + /* -- indicates end of args */ + ap_php_optind++; + return(EOF); + } + if (!dash) + { + assert((argv[ap_php_optind][0] == '-') && argv[ap_php_optind][1]); + dash = 1; + optchr = 1; + } - if (*optind >= argc) { - return(EOF); - } - if (!dash) { - if ((argv[*optind][0] != '-')) { - return(EOF); - } else { - if (!argv[*optind][1]) - { - /* - * use to specify stdin. Need to let pgm process this and - * the following args - */ - return(EOF); - } - } - } - if ((argv[*optind][0] == '-') && (argv[*optind][1] == '-')) { - /* '--' indicates end of args if not followed by a known long option name */ - while (1) { - opts_idx++; - if (opts[opts_idx].opt_char == '-') { - (*optind)++; - return(EOF); - } else if (opts[opts_idx].opt_name && !strcmp(&argv[*optind][2], opts[opts_idx].opt_name)) { - break; - } - } - optchr = 0; - dash = 1; - arg_start = 2 + strlen(opts[opts_idx].opt_name); - } - if (!dash) { - dash = 1; - optchr = 1; - } + /* Check if the guy tries to do a -: kind of flag */ + assert(dash); + if (argv[ap_php_optind][optchr] == ':') + { + dash = 0; + ap_php_optind++; + return(ap_php_optiserr(argc, argv, ap_php_optind-1, optstr, optchr, OPTERRCOLON)); + } + if (!(cp = strchr(optstr, argv[ap_php_optind][optchr]))) + { + int errind = ap_php_optind; + int errchr = optchr; - /* Check if the guy tries to do a -: kind of flag */ - if (argv[*optind][optchr] == ':') { - dash = 0; - (*optind)++; - return (php_opt_error(argc, argv, *optind-1, optchr, OPTERRCOLON, show_err)); - } - if (opts_idx < 0) { - while (1) { - opts_idx++; - if (opts[opts_idx].opt_char == '-') { - int errind = *optind; - int errchr = optchr; - - if (!argv[*optind][optchr+1]) { - dash = 0; - (*optind)++; - } else { - optchr++; - } - return(php_opt_error(argc, argv, errind, errchr, OPTERRNF, show_err)); - } else if (argv[*optind][optchr] == opts[opts_idx].opt_char) { - break; - } - } - } - if (opts[opts_idx].need_param) { - /* Check for cases where the value of the argument - is in the form - or in the form - */ - dash = 0; - if(!argv[*optind][arg_start]) { - (*optind)++; - if (*optind == argc) { - return(php_opt_error(argc, argv, *optind-1, optchr, OPTERRARG, show_err)); - } - *optarg = argv[(*optind)++]; - } else { - *optarg = &argv[*optind][arg_start]; - (*optind)++; - } - return opts[opts_idx].opt_char; - } else { - if (arg_start == 2) { - if (!argv[*optind][optchr+1]) - { - dash = 0; - (*optind)++; - } else { - optchr++; - } - } else { - (*optind)++; - } - return opts[opts_idx].opt_char; - } - assert(0); - return(0); /* never reached */ + if (!argv[ap_php_optind][optchr+1]) + { + dash = 0; + ap_php_optind++; + } + else + optchr++; + return(ap_php_optiserr(argc, argv, errind, optstr, errchr, OPTERRNF)); + } + if (cp[1] == ':') + { + /* Check for cases where the value of the argument + is in the form - or in the form - */ + dash = 0; + if(!argv[ap_php_optind][2]) { + ap_php_optind++; + if (ap_php_optind == argc) + return(ap_php_optiserr(argc, argv, ap_php_optind-1, optstr, optchr, OPTERRARG)); + ap_php_optarg = argv[ap_php_optind++]; + } + else + { + ap_php_optarg = &argv[ap_php_optind][2]; + ap_php_optind++; + } + return(*cp); + } + else + { + if (!argv[ap_php_optind][optchr+1]) + { + dash = 0; + ap_php_optind++; + } + else + optchr++; + return(*cp); + } + assert(0); + return(0); /* never reached */ } + +#ifdef TESTGETOPT +int + main (int argc, char **argv) + { + int c; + extern char *ap_php_optarg; + extern int ap_php_optind; + int aflg = 0; + int bflg = 0; + int errflg = 0; + char *ofile = NULL; + + while ((c = ap_php_getopt(argc, argv, "abo:")) != EOF) + switch (c) { + case 'a': + if (bflg) + errflg++; + else + aflg++; + break; + case 'b': + if (aflg) + errflg++; + else + bflg++; + break; + case 'o': + ofile = ap_php_optarg; + (void)printf("ofile = %s\n", ofile); + break; + case '?': + errflg++; + } + if (errflg) { + (void)fprintf(stderr, + "usage: cmd [-a|-b] [-o ] files...\n"); + exit (2); + } + for ( ; ap_php_optind < argc; ap_php_optind++) + (void)printf("%s\n", argv[ap_php_optind]); + return 0; + } + +#endif /* TESTGETOPT */ diff --git a/sapi/cli/php_cli.c b/sapi/cli/php_cli.c index 22f90f825e..8b16f7d364 100644 --- a/sapi/cli/php_cli.c +++ b/sapi/cli/php_cli.c @@ -89,39 +89,11 @@ #define PHP_MODE_LINT 4 #define PHP_MODE_STRIP 5 #define PHP_MODE_CLI_DIRECT 6 -#define PHP_MODE_PROCESS_STDIN 7 - -static char *optarg = NULL; -static int optind = 1; - -static const opt_struct OPTIONS[] = { - {'a', 0, "interactive"}, - {'B', 1, "process-begin"}, - {'C', 0, "no-chdir"}, /* for compatibility with CGI (do not chdir to script directory) */ - {'c', 1, "php-ini"}, - {'d', 1, "define"}, - {'E', 1, "process-end"}, - {'e', 0, "profile-info"}, - {'F', 1, "process-file"}, - {'f', 1, "file"}, - {'g', 1, "global"}, - {'h', 0, "help"}, - {'i', 0, "info"}, - {'l', 0, "syntax-check"}, - {'m', 0, "modules"}, - {'n', 0, "no-php-ini"}, - {'q', 0, "no-header"}, /* for compatibility with CGI (do not generate HTTP headers) */ - {'R', 1, "process-code"}, - {'H', 0, "hide-args"}, - {'r', 1, "run"}, - {'s', 0, "syntax-highlight"}, - {'s', 0, "syntax-highlighting"}, - {'w', 0, "strip"}, - {'?', 0, "usage"},/* help alias (both '?' and 'usage') */ - {'v', 0, "version"}, - {'z', 1, "zend-extension"}, - {'-', 0, NULL} /* end of args */ -}; + +extern char *ap_php_optarg; +extern int ap_php_optind; + +#define OPTSTRING "aCc:d:ef:g:hilmnqr:sw?vz:" static int print_module_info(zend_module_entry *module, void *arg TSRMLS_DC) { @@ -358,12 +330,9 @@ static void php_cli_usage(char *argv0) prog = "php"; } - php_printf( "Usage: %s [options] [-f] [--] [args...]\n" - " %s [options] -r [--] [args...]\n" - " %s [options] [-B ] -R [-E ] [--] [args...]\n" - " %s [options] [-B ] -F [-E ] [--] [args...]\n" - " %s [options] -- [args...]\n" - "\n" + php_printf( "Usage: %s [options] [-f] [args...]\n" + " %s [options] -r [args...]\n" + " %s [options] [-- args...]\n" " -a Run interactively\n" " -c | Look for php.ini file in this directory\n" " -n No php.ini file will be used\n" @@ -375,11 +344,6 @@ static void php_cli_usage(char *argv0) " -l Syntax check only (lint)\n" " -m Show compiled in modules\n" " -r Run PHP without using script tags \n" - " -B Run PHP before processing input lines\n" - " -R Run PHP for every input line\n" - " -F Parse and execute for every input line\n" - " -E Run PHP after processing all input lines\n" - " -H Hide any passed arguments from external tools.\n" " -s Display colour syntax highlighted source.\n" " -v Version number\n" " -w Display source with stripped comments and whitespace.\n" @@ -387,8 +351,7 @@ static void php_cli_usage(char *argv0) "\n" " args... Arguments passed to script. Use -- args when first argument \n" " starts with - or script is read from stdin\n" - "\n" - , prog, prog, prog, prog, prog); + , prog, prog, prog); } /* }}} */ @@ -423,9 +386,6 @@ static void php_register_command_line_global_vars(char **arg TSRMLS_DC) efree(*arg); } -static php_stream_context *sc_in_process = NULL; -static php_stream *s_in_process = NULL; - static void cli_register_file_handles(TSRMLS_D) { zval *zin, *zout, *zerr; @@ -445,9 +405,6 @@ static void cli_register_file_handles(TSRMLS_D) return; } - sc_in_process = sc_in; - s_in_process = s_in; - php_stream_to_zval(s_in, zin); php_stream_to_zval(s_out, zout); php_stream_to_zval(s_err, zerr); @@ -475,42 +432,6 @@ static void cli_register_file_handles(TSRMLS_D) FREE_ZVAL(zerr); } -static const char *param_mode_conflict = "Either execute direct code, process stdin or use a file.\n"; - -/* {{{ cli_seek_file_begin - */ -static int cli_seek_file_begin(zend_file_handle *file_handle, char *script_file, int *lineno TSRMLS_DC) -{ - int c; - - *lineno = 1; - - if (!(file_handle->handle.fp = VCWD_FOPEN(script_file, "rb"))) { - php_printf("Could not open input file: %s.\n", script_file); - return FAILURE; - } - file_handle->filename = script_file; - /* #!php support */ - c = fgetc(file_handle->handle.fp); - if (c == '#') { - while (c != 10 && c != 13) { - c = fgetc(file_handle->handle.fp); /* skip to end of line */ - } - /* handle situations where line is terminated by \r\n */ - if (c == 13) { - if (fgetc(file_handle->handle.fp) != 10) { - long pos = ftell(file_handle->handle.fp); - fseek(file_handle->handle.fp, pos - 1, SEEK_SET); - } - } - *lineno = -2; - } else { - rewind(file_handle->handle.fp); - } - return SUCCESS; -} -/* }}} */ - /* {{{ main */ int main(int argc, char *argv[]) @@ -520,18 +441,15 @@ int main(int argc, char *argv[]) zend_file_handle file_handle; /* temporary locals */ int behavior=PHP_MODE_STANDARD; - int orig_optind=optind; - char *orig_optarg=optarg; + int orig_optind=ap_php_optind; + char *orig_optarg=ap_php_optarg; char *arg_free=NULL, **arg_excp=&arg_free; char *script_file=NULL; zend_llist global_vars; - int interactive=0; + int interactive=0, is_hashbang=0; int module_started = 0; - int lineno = 0; - char *exec_direct=NULL, *exec_run=NULL, *exec_begin=NULL, *exec_end=NULL; - const char *param_error=NULL; - int scan_input = 0; - int hide_argv = 0; + char *exec_direct=NULL; + char *param_error=NULL; /* end of temporary locals */ #ifdef ZTS zend_compiler_globals *compiler_globals; @@ -570,18 +488,18 @@ int main(int argc, char *argv[]) #endif - while ((c = php_getopt(argc, argv, OPTIONS, &optarg, &optind, 0))!=-1) { + while ((c=ap_php_getopt(argc, argv, OPTSTRING))!=-1) { switch (c) { case 'c': - cli_sapi_module.php_ini_path_override = strdup(optarg); + cli_sapi_module.php_ini_path_override = strdup(ap_php_optarg); break; case 'n': cli_sapi_module.php_ini_ignore = 1; break; } } - optind = orig_optind; - optarg = orig_optarg; + ap_php_optind = orig_optind; + ap_php_optarg = orig_optarg; cli_sapi_module.executable_location = argv[0]; @@ -606,69 +524,21 @@ int main(int argc, char *argv[]) module_started = 1; zend_first_try { - zend_llist_init(&global_vars, sizeof(char *), NULL, 0); - - zend_uv.html_errors = 0; /* tell the engine we're in non-html mode */ - CG(in_compilation) = 0; /* not initialized but needed for several options */ - EG(uninitialized_zval_ptr) = NULL; - - if (cli_sapi_module.php_ini_path_override && cli_sapi_module.php_ini_ignore) { - PUTS("You cannot use both -n and -c switch. Use -h for help.\n"); - exit_status=1; - goto out_err; - } - - while ((c = php_getopt(argc, argv, OPTIONS, &optarg, &optind, 0)) != -1) { + while ((c=ap_php_getopt(argc, argv, OPTSTRING))!=-1) { switch (c) { - - case 'h': /* help & quit */ case '?': php_output_startup(); php_output_activate(TSRMLS_C); php_cli_usage(argv[0]); php_end_ob_buffers(1 TSRMLS_CC); - exit_status=1; - goto out_err; - - - case 'i': /* php info & quit */ - if (php_request_startup(TSRMLS_C)==FAILURE) { - goto err; - } - php_print_info(0xFFFFFFFF TSRMLS_CC); - php_end_ob_buffers(1 TSRMLS_CC); - exit_status=1; - goto out; - - case 'm': /* list compiled in modules */ - php_output_startup(); - php_output_activate(TSRMLS_C); - php_printf("[PHP Modules]\n"); - print_modules(TSRMLS_C); - php_printf("\n[Zend Modules]\n"); - print_extensions(TSRMLS_C); - php_printf("\n"); - php_end_ob_buffers(1 TSRMLS_CC); - exit_status=1; - goto out_err; - - case 'v': /* show php version & quit */ - if (php_request_startup(TSRMLS_C)==FAILURE) { - goto err; - } -#if ZEND_DEBUG - php_printf("PHP %s (%s) (built: %s %s) (DEBUG)\nCopyright (c) 1997-2003 The PHP Group\n%s", PHP_VERSION, sapi_module.name, __DATE__, __TIME__, get_zend_version()); -#else - php_printf("PHP %s (%s) (built: %s %s)\nCopyright (c) 1997-2003 The PHP Group\n%s", PHP_VERSION, sapi_module.name, __DATE__, __TIME__, get_zend_version()); -#endif - php_end_ob_buffers(1 TSRMLS_CC); - exit_status=1; - goto out; - - default: + exit(1); break; } } + ap_php_optind = orig_optind; + ap_php_optarg = orig_optarg; + + zend_llist_init(&global_vars, sizeof(char *), NULL, 0); /* Set some CLI defaults */ SG(options) |= SAPI_OPTION_NO_CHDIR; @@ -678,9 +548,17 @@ int main(int argc, char *argv[]) INI_HARDCODED("implicit_flush", "1"); INI_HARDCODED("max_execution_time", "0"); - optind = orig_optind; - optarg = orig_optarg; - while ((c = php_getopt(argc, argv, OPTIONS, &optarg, &optind, 0)) != -1) { + zend_uv.html_errors = 0; /* tell the engine we're in non-html mode */ + CG(in_compilation) = 0; /* not initialized but needed for several options */ + EG(uninitialized_zval_ptr) = NULL; + + if (cli_sapi_module.php_ini_path_override && cli_sapi_module.php_ini_ignore) { + PUTS("You cannot use both -n and -c switch. Use -h for help.\n"); + exit_status=1; + goto out_err; + } + + while ((c = ap_php_getopt(argc, argv, OPTSTRING)) != -1) { switch (c) { case 'a': /* interactive mode */ @@ -692,46 +570,48 @@ int main(int argc, char *argv[]) /* This is default so NOP */ break; case 'd': /* define ini entries on command line */ - define_command_line_ini_entry(optarg); + define_command_line_ini_entry(ap_php_optarg); break; case 'e': /* enable extended info output */ CG(extended_info) = 1; break; - case 'F': - if (behavior == PHP_MODE_PROCESS_STDIN) { - if (exec_run || script_file) { - param_error = "You can use -R or -F only once.\n"; - break; - } - } else if (behavior != PHP_MODE_STANDARD) { - param_error = param_mode_conflict; - break; - } - behavior=PHP_MODE_PROCESS_STDIN; - script_file = optarg; - break; - case 'f': /* parse file */ - if (behavior == PHP_MODE_CLI_DIRECT || behavior == PHP_MODE_PROCESS_STDIN) { - param_error = param_mode_conflict; - break; - } else if (script_file) { - param_error = "You can use -f only once.\n"; + if (behavior == PHP_MODE_CLI_DIRECT) { + param_error = "Either execute direct code or use a file.\n"; break; } - script_file = optarg; + script_file = ap_php_optarg; break; case 'g': /* define global variables on command line */ { - char *arg = estrdup(optarg); + char *arg = estrdup(ap_php_optarg); zend_llist_add_element(&global_vars, &arg); } break; + case 'h': /* help & quit */ + case '?': + php_output_startup(); + php_output_activate(TSRMLS_C); + php_cli_usage(argv[0]); + php_end_ob_buffers(1 TSRMLS_CC); + exit_status=1; + zend_ini_deactivate(TSRMLS_C); + goto out_err; + + case 'i': /* php info & quit */ + if (php_request_startup(TSRMLS_C)==FAILURE) { + goto err; + } + php_print_info(0xFFFFFFFF TSRMLS_CC); + php_end_ob_buffers(1 TSRMLS_CC); + exit_status=1; + goto out; + case 'l': /* syntax check mode */ if (behavior != PHP_MODE_STANDARD) { break; @@ -739,9 +619,22 @@ int main(int argc, char *argv[]) behavior=PHP_MODE_LINT; break; + case 'm': /* list compiled in modules */ + php_output_startup(); + php_output_activate(TSRMLS_C); + php_printf("[PHP Modules]\n"); + print_modules(TSRMLS_C); + php_printf("\n[Zend Modules]\n"); + print_extensions(TSRMLS_C); + php_printf("\n"); + php_end_ob_buffers(1 TSRMLS_CC); + exit_status=1; + zend_ini_deactivate(TSRMLS_C); + goto out_err; + #if 0 /* not yet operational, see also below ... */ case '': /* generate indented source mode*/ - if (behavior == PHP_MODE_CLI_DIRECT || behavior == PHP_MODE_PROCESS_STDIN) { + if (behavior == PHP_MODE_CLI_DIRECT) { param_error = "Source indenting only works for files.\n"; break; } @@ -754,72 +647,39 @@ int main(int argc, char *argv[]) break; case 'r': /* run code from command line */ - if (behavior == PHP_MODE_CLI_DIRECT) { - if (exec_direct || script_file) { - param_error = "You can use -r only once.\n"; - break; - } - } else if (behavior != PHP_MODE_STANDARD) { - param_error = param_mode_conflict; + if (behavior != PHP_MODE_STANDARD) { + param_error = "Either execute direct code or use a file.\n"; break; } behavior=PHP_MODE_CLI_DIRECT; - exec_direct=optarg; - break; - - case 'R': - if (behavior == PHP_MODE_PROCESS_STDIN) { - if (exec_run || script_file) { - param_error = "You can use -R or -F only once.\n"; - break; - } - } else if (behavior != PHP_MODE_STANDARD) { - param_error = param_mode_conflict; - break; - } - behavior=PHP_MODE_PROCESS_STDIN; - exec_run=optarg; - break; - - case 'B': - if (behavior == PHP_MODE_PROCESS_STDIN) { - if (exec_begin) { - param_error = "You can use -B only once.\n"; - break; - } - } else if (behavior != PHP_MODE_STANDARD) { - param_error = param_mode_conflict; - break; - } - behavior=PHP_MODE_PROCESS_STDIN; - exec_begin=optarg; - break; - - case 'E': - if (behavior == PHP_MODE_PROCESS_STDIN) { - if (exec_end) { - param_error = "You can use -E only once.\n"; - break; - } - } else if (behavior != PHP_MODE_STANDARD) { - param_error = param_mode_conflict; - break; - } - scan_input = 1; - behavior=PHP_MODE_PROCESS_STDIN; - exec_end=optarg; + exec_direct=ap_php_optarg; break; case 's': /* generate highlighted HTML from source */ - if (behavior == PHP_MODE_CLI_DIRECT || behavior == PHP_MODE_PROCESS_STDIN) { + if (behavior == PHP_MODE_CLI_DIRECT) { param_error = "Source highlighting only works for files.\n"; break; } behavior=PHP_MODE_HIGHLIGHT; break; + case 'v': /* show php version & quit */ + if (php_request_startup(TSRMLS_C)==FAILURE) { + goto err; + } + +#if ZEND_DEBUG + php_printf("PHP %s (%s) (built: %s %s) (DEBUG)\nCopyright (c) 1997-2003 The PHP Group\n%s", PHP_VERSION, sapi_module.name, __DATE__, __TIME__, get_zend_version()); +#else + php_printf("PHP %s (%s) (built: %s %s)\nCopyright (c) 1997-2003 The PHP Group\n%s", PHP_VERSION, sapi_module.name, __DATE__, __TIME__, get_zend_version()); +#endif + php_printf("PHP %s (%s) (built: %s %s)\nCopyright (c) 1997-2003 The PHP Group\n%s", PHP_VERSION, sapi_module.name, __DATE__, __TIME__, get_zend_version()); + php_end_ob_buffers(1 TSRMLS_CC); + exit_status=1; + goto out; + case 'w': - if (behavior == PHP_MODE_CLI_DIRECT || behavior == PHP_MODE_PROCESS_STDIN) { + if (behavior == PHP_MODE_CLI_DIRECT) { param_error = "Source stripping only works for files.\n"; break; } @@ -827,10 +687,7 @@ int main(int argc, char *argv[]) break; case 'z': /* load extension file */ - zend_load_extension(optarg); - break; - case 'H': - hide_argv = 1; + zend_load_extension(ap_php_optarg); break; default: @@ -847,25 +704,35 @@ int main(int argc, char *argv[]) CG(interactive) = interactive; /* only set script_file if not set already and not in direct mode and not at end of parameter list */ - if (argc > optind - && !script_file - && behavior!=PHP_MODE_CLI_DIRECT - && behavior!=PHP_MODE_PROCESS_STDIN - && strcmp(argv[optind-1],"--")) - { - script_file=argv[optind]; - optind++; + if (argc > ap_php_optind && !script_file && behavior!=PHP_MODE_CLI_DIRECT && strcmp(argv[ap_php_optind-1],"--")) { + script_file=argv[ap_php_optind]; + ap_php_optind++; } if (script_file) { - if (cli_seek_file_begin(&file_handle, script_file, &lineno TSRMLS_CC) != SUCCESS) { + if (!(file_handle.handle.fp = VCWD_FOPEN(script_file, "rb"))) { + php_printf("Could not open input file: %s.\n", script_file); goto err; } + file_handle.filename = script_file; script_filename = script_file; + /* #!php support */ + c = fgetc(file_handle.handle.fp); + if (c == '#') { + while (c != 10 && c != 13) { + c = fgetc(file_handle.handle.fp); /* skip to end of line */ + } + /* handle situations where line is terminated by \r\n */ + if (c == 13) { + if (fgetc(file_handle.handle.fp) != 10) { + long pos = ftell(file_handle.handle.fp); + fseek(file_handle.handle.fp, pos - 1, SEEK_SET); + } + } + is_hashbang = 1; + } else { + rewind(file_handle.handle.fp); + } } else { - /* We could handle PHP_MODE_PROCESS_STDIN in a different manner */ - /* here but this would make things only more complicated. And it */ - /* is consitent with the way -R works where the stdin file handle*/ - /* is also accessible. */ file_handle.filename = "-"; file_handle.handle.fp = stdin; } @@ -874,14 +741,14 @@ int main(int argc, char *argv[]) file_handle.free_filename = 0; php_self = file_handle.filename; - /* before registering argv to module exchange the *new* argv[0] */ + /* before registering argv to modulule exchange the *new* argv[0] */ /* we can achieve this without allocating more memory */ - SG(request_info).argc=argc-optind+1; - arg_excp = argv+optind-1; - arg_free = argv[optind-1]; + SG(request_info).argc=argc-ap_php_optind+1; + arg_excp = argv+ap_php_optind-1; + arg_free = argv[ap_php_optind-1]; SG(request_info).path_translated = file_handle.filename; - argv[optind-1] = file_handle.filename; - SG(request_info).argv=argv+optind-1; + argv[ap_php_optind-1] = file_handle.filename; + SG(request_info).argv=argv+ap_php_optind-1; if (php_request_startup(TSRMLS_C)==FAILURE) { *arg_excp = arg_free; @@ -890,16 +757,14 @@ int main(int argc, char *argv[]) PUTS("Could not startup.\n"); goto err; } - CG(zend_lineno) = lineno; - *arg_excp = arg_free; /* reconstuct argv */ - if (hide_argv) { - int i; - for (i = 1; i < argc; i++) { - memset(argv[i], 0, strlen(argv[i])); - } + /* Correct line numbers when #!php is used. This is reset in php_request_startup(). */ + if (is_hashbang) { + CG(zend_lineno) = -2; } + *arg_excp = arg_free; /* reconstuct argv */ + /* This actually destructs the elements of the list - ugly hack */ zend_llist_apply(&global_vars, (llist_apply_func_t) php_register_command_line_global_vars TSRMLS_CC); zend_llist_destroy(&global_vars); @@ -955,58 +820,6 @@ int main(int argc, char *argv[]) exit_status=254; } break; - - case PHP_MODE_PROCESS_STDIN: - { - char *input; - size_t len, index = 0; - pval *argn, *argi; - - cli_register_file_handles(TSRMLS_C); - - if (exec_begin && zend_eval_string(exec_begin, NULL, "Command line begin code" TSRMLS_CC) == FAILURE) { - exit_status=254; - } - ALLOC_ZVAL(argi); - Z_TYPE_P(argi) = IS_LONG; - Z_LVAL_P(argi) = index; - INIT_PZVAL(argi); - zend_hash_update(&EG(symbol_table), "argi", sizeof("argi"), &argi, sizeof(pval *), NULL); - while (exit_status == SUCCESS && (input=php_stream_gets(s_in_process, NULL, 0)) != NULL) { - len = strlen(input); - while (len-- && (input[len]=='\n' || input[len]=='\r')) { - input[len] = '\0'; - } - ALLOC_ZVAL(argn); - Z_TYPE_P(argn) = IS_STRING; - Z_STRLEN_P(argn) = ++len; - Z_STRVAL_P(argn) = estrndup(input, len); - INIT_PZVAL(argn); - zend_hash_update(&EG(symbol_table), "argn", sizeof("argn"), &argn, sizeof(pval *), NULL); - Z_LVAL_P(argi) = ++index; - if (exec_run) { - if (zend_eval_string(exec_run, NULL, "Command line run code" TSRMLS_CC) == FAILURE) { - exit_status=254; - } - } else { - if (script_file) { - if (cli_seek_file_begin(&file_handle, script_file, &lineno TSRMLS_CC) != SUCCESS) { - exit_status = 1; - } else { - CG(zend_lineno) = lineno; - php_execute_script(&file_handle TSRMLS_CC); - exit_status = EG(exit_status); - } - } - } - efree(input); - } - if (exec_end && zend_eval_string(exec_end, NULL, "Command line end code" TSRMLS_CC) == FAILURE) { - exit_status=254; - } - - break; - } } if (cli_sapi_module.php_ini_path_override) { diff --git a/sapi/cli/php_getopt.h b/sapi/cli/php_getopt.h index a01fc2ae85..2434ba7eb2 100644 --- a/sapi/cli/php_getopt.h +++ b/sapi/cli/php_getopt.h @@ -20,13 +20,9 @@ #include "php.h" -/* Define structure for one recognized option (both single char and long name). - * If short_open is '-' this is the last option. - */ -typedef struct _opt_struct { - const char opt_char; - const int need_param; - const char * opt_name; -} opt_struct; - -int php_getopt(int argc, char* const *argv, const opt_struct opts[], char **optarg, int *optind, int show_err); +extern char *ap_php_optarg; +extern int ap_php_optind; + +int ap_php_getopt(int argc, char* const *argv, const char *optstr); + + \ No newline at end of file diff --git a/sapi/isapi/php4isapi.c b/sapi/isapi/php4isapi.c index fc8f5d9667..88d95ebdaf 100644 --- a/sapi/isapi/php4isapi.c +++ b/sapi/isapi/php4isapi.c @@ -717,7 +717,7 @@ BOOL exceptionhandler(LPEXCEPTION_POINTERS *e, LPEXCEPTION_POINTERS ep) DWORD WINAPI HttpExtensionProc(LPEXTENSION_CONTROL_BLOCK lpECB) { - zend_file_handle file_handle; + zend_file_handle file_handle = {0}; zend_bool stack_overflown=0; #ifdef PHP_ENABLE_SEH LPEXCEPTION_POINTERS e; diff --git a/sapi/phttpd/phttpd.c b/sapi/phttpd/phttpd.c index 1928b6060e..0e662856bd 100644 --- a/sapi/phttpd/phttpd.c +++ b/sapi/phttpd/phttpd.c @@ -247,7 +247,7 @@ php_phttpd_request_dtor(TSRMLS_D TSRMLS_DC) int php_doit(TSRMLS_D TSRMLS_DC) { struct stat sb; - zend_file_handle file_handle; + zend_file_handle file_handle = {0}; struct httpinfo *hip = PHG(cip)->hip; TSRMLS_FETCH(); diff --git a/sapi/pi3web/pi3web_sapi.c b/sapi/pi3web/pi3web_sapi.c index 43f784d534..09bb892777 100644 --- a/sapi/pi3web/pi3web_sapi.c +++ b/sapi/pi3web/pi3web_sapi.c @@ -387,7 +387,7 @@ static sapi_module_struct pi3web_sapi_module = { DWORD PHP4_wrapper(LPCONTROL_BLOCK lpCB) { - zend_file_handle file_handle; + zend_file_handle file_handle = {0}; int iRet = PIAPI_COMPLETED; TSRMLS_FETCH(); diff --git a/sapi/roxen/roxen.c b/sapi/roxen/roxen.c index 855e1e90b3..d0e5501cce 100644 --- a/sapi/roxen/roxen.c +++ b/sapi/roxen/roxen.c @@ -575,7 +575,7 @@ static int php_roxen_module_main(TSRMLS_D) { int res, len; char *dir; - zend_file_handle file_handle; + zend_file_handle file_handle = {0}; #ifdef ROXEN_USE_ZTS GET_THIS(); #endif diff --git a/sapi/servlet/servlet.c b/sapi/servlet/servlet.c index c4fceba252..b3bcd3fe20 100644 --- a/sapi/servlet/servlet.c +++ b/sapi/servlet/servlet.c @@ -310,7 +310,7 @@ JNIEXPORT void JNICALL Java_net_php_servlet_send jstring contentType, jint contentLength, jstring authUser, jboolean display_source_mode) { - zend_file_handle file_handle; + zend_file_handle file_handle = {0}; int retval; #ifndef VIRTUAL_DIR char cwd[MAXPATHLEN]; diff --git a/sapi/thttpd/thttpd.c b/sapi/thttpd/thttpd.c index 48054c4394..344962921a 100644 --- a/sapi/thttpd/thttpd.c +++ b/sapi/thttpd/thttpd.c @@ -388,7 +388,7 @@ static sapi_module_struct thttpd_sapi_module = { static void thttpd_module_main(int show_source TSRMLS_DC) { - zend_file_handle file_handle; + zend_file_handle file_handle = {0}; if (php_request_startup(TSRMLS_C) == FAILURE) { return; diff --git a/sapi/tux/php_tux.c b/sapi/tux/php_tux.c index 7db243773c..093e3326cf 100644 --- a/sapi/tux/php_tux.c +++ b/sapi/tux/php_tux.c @@ -296,7 +296,7 @@ static sapi_module_struct tux_sapi_module = { static void tux_module_main(TSRMLS_D) { - zend_file_handle file_handle; + zend_file_handle file_handle = {0}; file_handle.type = ZEND_HANDLE_FILENAME; file_handle.filename = SG(request_info).path_translated; diff --git a/sapi/webjames/webjames.c b/sapi/webjames/webjames.c index 75a55b79c5..06e9adb59b 100644 --- a/sapi/webjames/webjames.c +++ b/sapi/webjames/webjames.c @@ -142,7 +142,7 @@ static void sapi_webjames_register_variables(zval *track_vars_array TSRMLS_DC) static void webjames_module_main(TSRMLS_D) { - zend_file_handle file_handle; + zend_file_handle file_handle = {0}; FILE *fp=NULL; char *path; -- 2.40.0