]> granicus.if.org Git - php/commitdiff
- Re-added the FD lexer patch.
authorfoobar <sniper@php.net>
Sat, 31 May 2003 01:37:45 +0000 (01:37 +0000)
committerfoobar <sniper@php.net>
Sat, 31 May 2003 01:37:45 +0000 (01:37 +0000)
# Marcus is going to add the long-opts patches back in after this.

28 files changed:
NEWS
ext/standard/basic_functions.c
ext/standard/browscap.c
main/main.c
main/php_ini.c
main/php_streams.h
main/streams.c
sapi/aolserver/aolserver.c
sapi/apache/mod_php4.c
sapi/apache/sapi_apache.c
sapi/apache2filter/sapi_apache2.c
sapi/apache2handler/sapi_apache2.c
sapi/caudium/caudium.c
sapi/cgi/cgi_main.c
sapi/cgi/getopt.c
sapi/cgi/php_getopt.h
sapi/cli/README
sapi/cli/getopt.c
sapi/cli/php_cli.c
sapi/cli/php_getopt.h
sapi/isapi/php4isapi.c
sapi/phttpd/phttpd.c
sapi/pi3web/pi3web_sapi.c
sapi/roxen/roxen.c
sapi/servlet/servlet.c
sapi/thttpd/thttpd.c
sapi/tux/php_tux.c
sapi/webjames/webjames.c

diff --git a/NEWS b/NEWS
index 0ba1d0d7657c2c7121024c61660bf942b78bf1a6..4268beaca5a26cab2fcad5dea9af069e2db98934 100644 (file)
--- 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)
index 7686bd3370c03208e6b54c103f7b516c3c701420..141922ad9879e795e6c015fc001d64bc9f3b58af 100644 (file)
@@ -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)) {
index abf63f4c8c9a2f030a5ace40fe5196dade62e748..ca14e7a7368630ee014dcda5ffd256534c4c9e03 100644 (file)
@@ -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;
index 9aa567b07cf786cf7af9ec5234433b4c935da45c..27484151c9ec1e63b5a170325bdc33f05750a435 100644 (file)
@@ -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
index 2be34595776fd9d1bfa1deb9482e01474b11b9e5..c30818965b6b95b682b7356dd92640e5ea1dab21 100644 (file)
@@ -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]) {
index 9af2679cf238a404ba360cb89817b662f9175e66..83e8c8b71c03a6d11e4fdea1fd16915be48e78fc 100755 (executable)
@@ -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;
index 4af3c21f8be94f1cb051473e3c130342dd3aa2f6..3d3c42cee253bdaa65662a0efb4b01800251d619 100755 (executable)
@@ -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)
index 665dd4190c15972e9258cdc6557c00a76436d196..ea2f2ab593468adcb433325cdb3f0cba7e9af727 100644 (file)
@@ -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;
index d5f59b21d2aab5b952224cdd414a3b0bd9aa2355..a0cbee47fd9b2637e8b6d1402c075b3f68b2b3d7 100644 (file)
@@ -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;
index 8b651de012788f47a563bb35179efd8d6911a1ec..3075f25d8fe2dd41473a97350e3cdd0760d325c9 100644 (file)
@@ -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;
index 5e603edee93529742c0046ea20d7cba2103d4489..f201983d543e0c71ac30c4b2b780c2c8e69f03ef 100644 (file)
@@ -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;
index 91399b351fe819df2e07540589aa8f60066564e3..81ea318130fd628942d887f840cf67ec97ab5c85 100644 (file)
@@ -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;
index 6b61060c88e1f2a2692698a6bdd10a2c4930ae91..1914430638e375366859c5d5b65eb602535aa748 100644 (file)
@@ -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;
index 2173952ff8457c0ee3679ce43aac16032b6fa0de..9b4ef08bc9b03bd25ff3397890e4ca2ebca23912 100644 (file)
@@ -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, "&");
index e048e950582815e131d3d42b3dafcaea247aa4f5..a26ca3c43178b5ef2925c1be8a3b9d2dc37baa2f 100644 (file)
@@ -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 <helly@php.net>                               |
-   +----------------------------------------------------------------------+
-*/
+/* Borrowed from Apache NT Port */
 
 #include <stdio.h>
 #include <string.h>
 #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 -<arg> <val> or in the form -<arg><val> */
-               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 -<arg> <val> or in the form -<arg><val> */
+        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 <filename>] files...\n");
+           exit (2);
+      }
+      for ( ; ap_php_optind < argc; ap_php_optind++)
+           (void)printf("%s\n", argv[ap_php_optind]);
+      return 0;
+ }
+
+#endif /* TESTGETOPT */
index 1e2355ff63b804943260c35fd952989743f06c83..40da432b596da6740c1baad4fc46da7564d41c67 100644 (file)
@@ -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 <helly@php.net>                               |
-   +----------------------------------------------------------------------+
-*/
-
+/* 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);
index 9e519e9bd011e5916e2a2988ebfcef572ae65de8..e6bf5cc6c60509a16bf83304a7f186ecdca0db1a 100644 (file)
@@ -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.
index 01e532b7cb74f9070c55ebbd2bed73e073b75827..f5874d577e7feda0ba1c308a54eef14fc2c0c866 100644 (file)
@@ -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 <helly@php.net>                               |
-   +----------------------------------------------------------------------+
-*/
-
-/* $Id$ */
+/* Borrowed from Apache NT Port */
 
 #include <stdio.h>
 #include <string.h>
 #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 -<arg> <val> or in the form -<arg><val> */
-               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 -<arg> <val> or in the form -<arg><val> */
+        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 <filename>] files...\n");
+           exit (2);
+      }
+      for ( ; ap_php_optind < argc; ap_php_optind++)
+           (void)printf("%s\n", argv[ap_php_optind]);
+      return 0;
+ }
+
+#endif /* TESTGETOPT */
index 22f90f825ea1541a9ea9ab39d4622b5de74db67e..8b16f7d364833531baf57f1956c57d3347e7f1fb 100644 (file)
 #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] <file> [--] [args...]\n"
-                   "       %s [options] -r <code> [--] [args...]\n"
-                   "       %s [options] [-B <begin_code>] -R <code> [-E <end_code>] [--] [args...]\n"
-                   "       %s [options] [-B <begin_code>] -F <file> [-E <end_code>] [--] [args...]\n"
-                   "       %s [options] -- [args...]\n"
-                   "\n"
+       php_printf( "Usage: %s [options] [-f] <file> [args...]\n"
+                   "       %s [options] -r <code> [args...]\n"
+                   "       %s [options] [-- args...]\n"
                                "  -a               Run interactively\n"
                                "  -c <path>|<file> 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 <code>        Run PHP <code> without using script tags <?..?>\n"
-                               "  -B <begin_code>  Run PHP <begin_code> before processing input lines\n"
-                               "  -R <code>        Run PHP <code> for every input line\n"
-                               "  -F <file>        Parse and execute <file> for every input line\n"
-                               "  -E <end_code>    Run PHP <end_code> 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) {
index a01fc2ae85e0208637c96d907011fda7802ce17e..2434ba7eb24b560e2e106bb00f01a2333b539bc2 100644 (file)
 
 #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
index fc8f5d96677a4de3c2a0d642eb36a42c2208b504..88d95ebdafbe9d60fc3ab40e98aa47f8d392e2cd 100644 (file)
@@ -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;
index 1928b6060e5cb173279e8628a8a694debdd9365a..0e662856bdc04127aed52b05b1af28502c52ce0d 100644 (file)
@@ -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();
 
index 43f784d5340cf372d3824f45159af1174008aba1..09bb892777f1b1fe9c08c5473ffc25ffd066709c 100644 (file)
@@ -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();
 
index 855e1e90b3e3109a70860abbe340a3ab86e99c8f..d0e5501cce417375e5573fe616c9b966859ee4ca 100644 (file)
@@ -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
index c4fceba25243295d11cbf6b93991286ffe336e02..b3bcd3fe208fb5c9b2b57388e64733d0e1a1e4dc 100644 (file)
@@ -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];
index 48054c4394999df86c31207fd68ce9d07ec8c45d..344962921a60e86f0db9c741a39bd29db2b28489 100644 (file)
@@ -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;
index 7db243773c7408408d2b19ee4048666260d2095a..093e3326cf1019ca1d69cf82778ce49a9f675d91 100644 (file)
@@ -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;
index 75a55b79c59aaf652b446bbb5a1915c2b0ecd917..06e9adb59b9721c6add6ddd931073438a68945a6 100644 (file)
@@ -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;