sniper          Fri May 30 21:37:46 2003 EDT

  Modified files:              (Branch: PHP_4_3)
    /php4       NEWS 
    /Zend       flex.skl zend.c zend.h zend_compile.h zend_execute.c 
                zend_globals.h zend_ini_scanner.h zend_ini_scanner.l 
                zend_language_scanner.h zend_language_scanner.l 
    /php4/ext/standard  basic_functions.c browscap.c 
    /php4/main  main.c php_ini.c php_streams.h streams.c 
    /php4/sapi/aolserver        aolserver.c 
    /php4/sapi/apache   mod_php4.c sapi_apache.c 
    /php4/sapi/apache2filter    sapi_apache2.c 
    /php4/sapi/apache2handler   sapi_apache2.c 
    /php4/sapi/caudium  caudium.c 
    /php4/sapi/cgi      cgi_main.c config9.m4 getopt.c php_getopt.h 
    /php4/sapi/cli      README getopt.c php_cli.c php_getopt.h 
    /php4/sapi/isapi    php4isapi.c 
    /php4/sapi/phttpd   phttpd.c 
    /php4/sapi/pi3web   pi3web_sapi.c 
    /php4/sapi/roxen    roxen.c 
    /php4/sapi/servlet  servlet.c 
    /php4/sapi/thttpd   thttpd.c 
    /php4/sapi/tux      php_tux.c 
    /php4/sapi/webjames webjames.c 
  Log:
  - Re-added the FD lexer patch.
  # Marcus is going to add the long-opts patches back in after this.
  
  
Index: php4/NEWS
diff -u php4/NEWS:1.1247.2.228 php4/NEWS:1.1247.2.229
--- php4/NEWS:1.1247.2.228      Fri May 30 20:34:14 2003
+++ php4/NEWS   Fri May 30 21:37:43 2003
@@ -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 @@
     (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: Zend/flex.skl
diff -u Zend/flex.skl:1.27.4.3 Zend/flex.skl:1.27.4.4
--- Zend/flex.skl:1.27.4.3      Wed May 21 03:53:20 2003
+++ Zend/flex.skl       Fri May 30 21:37:43 2003
@@ -1,7 +1,7 @@
 /* A Lexical scanner generated by flex */
 
 /* Scanner skeleton version:
- * $Header: /usr/repository/Zend/flex.skl,v 1.27.4.3 2003/05/21 07:53:20 zeev Exp $
+ * $Header: /usr/repository/Zend/flex.skl,v 1.27.4.4 2003/05/31 01:37:43 sniper Exp $
  */
 
 #define FLEX_SCANNER
@@ -9,6 +9,22 @@
 #define YY_FLEX_MINOR_VERSION 5
 
 %-
+
+/* for HAVE_* */
+#include "zend.h"
+
+/* for recv */
+#ifdef HAVE_SYS_TYPES_H
+# include <sys/types.h>
+#endif
+#ifdef HAVE_SYS_SOCKET_H
+# include <sys/socket.h> 
+#endif
+
+/* for read */
+#ifdef HAVE_UNISTD_H
+# include <unistd.h>
+#endif
 #include <stdio.h>
 %*
 
@@ -154,7 +170,7 @@
 struct yy_buffer_state
        {
 %-
-       FILE *yy_input_file;
+       struct _zend_file_handle *yy_input_file;
 %+
        istream* yy_input_file;
 %*
@@ -178,13 +194,6 @@
         */
        int yy_is_our_buffer;
 
-       /* Whether this is an "interactive" input source; if so, and
-        * if we're using stdio for input, then we want to use getc()
-        * instead of fread(), to make sure we stop fetching input after
-        * each newline.
-        */
-       int yy_is_interactive;
-
        /* Whether we're considered to be at the beginning of a line.
         * If so, '^' rules will be active on the next match, otherwise
         * not.
@@ -238,31 +247,19 @@
 #define yy_init SCNG(init)
 #define yy_start SCNG(start)
 
-#ifdef ZTS
-#define TSRMLS_D       void ***tsrm_ls
-#define TSRMLS_DC      , TSRMLS_D
-#define TSRMLS_C       tsrm_ls
-#define TSRMLS_CC      , TSRMLS_C
-#else
-#define TSRMLS_D
-#define TSRMLS_DC
-#define TSRMLS_C
-#define TSRMLS_CC
-#endif
-
 /* Flag which is used to allow yywrap()'s to do buffer switches
  * instead of setting up a fresh yyin.  A bit of a hack ...
  */
 /* static int yy_did_buffer_switch_on_eof; */
 #define yy_did_buffer_switch_on_eof SCNG(_yy_did_buffer_switch_on_eof)
 
-void yyrestart YY_PROTO(( FILE *input_file TSRMLS_DC ));
+void yyrestart YY_PROTO(( struct _zend_file_handle  *input_file TSRMLS_DC ));
 
 void yy_switch_to_buffer YY_PROTO(( YY_BUFFER_STATE new_buffer TSRMLS_DC ));
 void yy_load_buffer_state YY_PROTO(( TSRMLS_D ));
-YY_BUFFER_STATE yy_create_buffer YY_PROTO(( FILE *file, int size TSRMLS_DC ));
+YY_BUFFER_STATE yy_create_buffer YY_PROTO(( struct _zend_file_handle *file, int size 
TSRMLS_DC ));
 void yy_delete_buffer YY_PROTO(( YY_BUFFER_STATE b TSRMLS_DC ));
-void yy_init_buffer YY_PROTO(( YY_BUFFER_STATE b, FILE *file TSRMLS_DC ));
+void yy_init_buffer YY_PROTO(( YY_BUFFER_STATE b, struct _zend_file_handle *file 
TSRMLS_DC ));
 void yy_flush_buffer YY_PROTO(( YY_BUFFER_STATE b TSRMLS_DC ));
 #define YY_FLUSH_BUFFER yy_flush_buffer( yy_current_buffer TSRMLS_CC )
 
@@ -282,7 +279,6 @@
        { \
        if ( ! yy_current_buffer ) \
                yy_current_buffer = yy_create_buffer( SCNG(yy_in), YY_BUF_SIZE 
TSRMLS_CC ); \
-       yy_current_buffer->yy_is_interactive = is_interactive; \
        }
 
 #define yy_set_bol(at_bol) \
@@ -322,11 +318,6 @@
 static void yy_fatal_error YY_PROTO(( yyconst char msg[] ));
 %*
 
-#undef TSRMLS_D
-#undef TSRMLS_DC
-#undef TSRMLS_C
-#undef TSRMLS_CC
-
 /* Done after the current pattern has been matched and before the
  * corresponding action - sets up yytext.
  */
@@ -435,19 +426,25 @@
 /* Copy whatever the last rule matched to the standard output. */
 
 #ifndef ECHO
-%- Standard (non-C++) definition
-/* This used to be an fputs(), but since the string might contain NUL's,
- * we now use fwrite().
- */
-#define ECHO (void) fwrite( yytext, yyleng, 1, SCNG(yy_out) )
-%+ C++ definition
-#define ECHO LexerOutput( yytext, yyleng )
-%*
+#define ECHO
 #endif
 
 /* Gets input and stuffs it into "buf".  number of characters read, or YY_NULL,
  * is returned in "result".
  */
+
+#define YY_INPUT(buf,result,max_size) do {                             \
+       result = yyin->type == ZEND_HANDLE_FD ?                         \
+                       read(yyin->handle.fd, buf, max_size)            \
+                       : yyin->type == ZEND_HANDLE_FP ?                        \
+                       fread(buf, 1, max_size, yyin->handle.fp)        \
+                       : yyin->type == ZEND_HANDLE_SOCKET_FD ?         \
+                       recv(yyin->handle.fd, buf, max_size, 0)         \
+                       : -1;                                                          
                 \
+       if (result < 0)                                                                
         \
+               YY_FATAL_ERROR("input in flex scanner failed"); \
+} while (0)
+
 #ifndef YY_INPUT
 #define YY_INPUT(buf,result,max_size) \
 %% fread()/read() definition of YY_INPUT goes here unless we're doing C++
@@ -524,6 +521,7 @@
                if ( ! yy_start )
                        yy_start = 1;   /* first start state */
 
+#if 0
                if ( ! SCNG(yy_in) )
 %-
                        SCNG(yy_in) = stdin;
@@ -537,6 +535,7 @@
 %+
                        SCNG(yy_out) = &cout;
 %*
+#endif
 
                if ( ! yy_current_buffer )
                        yy_current_buffer =
@@ -1111,10 +1110,10 @@
 
 %-
 #ifdef YY_USE_PROTOS
-void yyrestart( FILE *input_file TSRMLS_DC )
+void yyrestart( struct _zend_file_handle *input_file TSRMLS_DC )
 #else
 void yyrestart( input_file TSRMLS_CC )
-FILE *input_file;
+struct _zend_file_handle *input_file;
 #endif
 %+
 void yyFlexLexer::yyrestart( istream* input_file TSRMLS_DC )
@@ -1187,10 +1186,10 @@
 
 %-
 #ifdef YY_USE_PROTOS
-YY_BUFFER_STATE yy_create_buffer( FILE *file, int size TSRMLS_DC )
+YY_BUFFER_STATE yy_create_buffer( struct _zend_file_handle *file, int size TSRMLS_DC )
 #else
 YY_BUFFER_STATE yy_create_buffer( file, size TSRMLS_CC )
-FILE *file;
+struct _zend_file_handle *file;
 int size;
 #ifdef ZTS
 void ***tsrm_ls;
@@ -1253,23 +1252,21 @@
 %-
 #ifndef YY_ALWAYS_INTERACTIVE
 #ifndef YY_NEVER_INTERACTIVE
-extern int isatty YY_PROTO(( int ));
 #endif
 #endif
 
 #ifdef YY_USE_PROTOS
-void yy_init_buffer( YY_BUFFER_STATE b, FILE *file TSRMLS_DC )
+void yy_init_buffer( YY_BUFFER_STATE b, struct _zend_file_handle *file TSRMLS_DC )
 #else
 void yy_init_buffer( b, file TSRMLS_CC )
 YY_BUFFER_STATE b;
-FILE *file;
+struct _zend_file_handle *file;
 #ifdef ZTS
 void ***tsrm_ls;
 #endif
 #endif
 
 %+
-extern "C" int isatty YY_PROTO(( int ));
 void yyFlexLexer::yy_init_buffer( YY_BUFFER_STATE b, istream* file TSRMLS_DC )
 %*
 
@@ -1280,17 +1277,7 @@
        b->yy_fill_buffer = 1;
 
 %-
-#if YY_ALWAYS_INTERACTIVE
-       b->yy_is_interactive = 1;
-#else
-#if YY_NEVER_INTERACTIVE
-       b->yy_is_interactive = 0;
-#else
-       b->yy_is_interactive = file ? (isatty( fileno(file) ) > 0) : 0;
-#endif
-#endif
 %+
-       b->yy_is_interactive = (file == (istream *) &cin) ? 1 : 0;
 %*
        }
 
@@ -1363,7 +1350,6 @@
        b->yy_is_our_buffer = 0;
        b->yy_input_file = 0;
        b->yy_n_chars = b->yy_buf_size;
-       b->yy_is_interactive = 0;
        b->yy_at_bol = 1;
        b->yy_fill_buffer = 0;
        b->yy_buffer_status = YY_BUFFER_NEW;
Index: Zend/zend.c
diff -u Zend/zend.c:1.162.2.5 Zend/zend.c:1.162.2.6
--- Zend/zend.c:1.162.2.5       Wed May 21 03:53:20 2003
+++ Zend/zend.c Fri May 30 21:37:43 2003
@@ -48,6 +48,7 @@
 ZEND_API int (*zend_printf)(const char *format, ...);
 ZEND_API zend_write_func_t zend_write;
 ZEND_API FILE *(*zend_fopen)(const char *filename, char **opened_path);
+ZEND_API zend_bool (*zend_open)(const char *filename, zend_file_handle *);
 ZEND_API void (*zend_block_interruptions)(void);
 ZEND_API void (*zend_unblock_interruptions)(void);
 ZEND_API void (*zend_ticks_function)(int ticks);
@@ -255,6 +256,18 @@
 }
 
 
+static zend_bool zend_open_wrapper(const char *filename, zend_file_handle *fh)
+{
+       fh->handle.fp = zend_fopen(filename, &fh->opened_path);
+
+       if (fh->handle.fp) {
+               fh->type = ZEND_HANDLE_FP;
+               return SUCCESS;
+       }
+       return FAILURE;
+}
+
+
 static FILE *zend_fopen_wrapper(const char *filename, char **opened_path)
 {
        if (opened_path) {
@@ -432,6 +445,10 @@
        zend_fopen = utility_functions->fopen_function;
        if (!zend_fopen) {
                zend_fopen = zend_fopen_wrapper;
+       }
+       zend_open = utility_functions->open_function;
+       if (!zend_open) {
+               zend_open = zend_open_wrapper;
        }
        zend_message_dispatcher_p = utility_functions->message_handler;
        zend_block_interruptions = utility_functions->block_interruptions;
Index: Zend/zend.h
diff -u Zend/zend.h:1.164.2.7 Zend/zend.h:1.164.2.8
--- Zend/zend.h:1.164.2.7       Wed May 21 03:53:20 2003
+++ Zend/zend.h Fri May 30 21:37:43 2003
@@ -17,7 +17,7 @@
    +----------------------------------------------------------------------+
 */
 
-/* $Id: zend.h,v 1.164.2.7 2003/05/21 07:53:20 zeev Exp $ */
+/* $Id: zend.h,v 1.164.2.8 2003/05/31 01:37:43 sniper Exp $ */
 
 #ifndef ZEND_H
 #define ZEND_H
@@ -279,6 +279,7 @@
        int (*handle_property_set)(zend_property_reference *property_reference, zval 
*value);
 };
 
+struct _zend_file_handle;
 
 
 typedef struct _zend_utility_functions {
@@ -292,6 +293,7 @@
        int (*get_configuration_directive)(char *name, uint name_length, zval 
*contents);
        void (*ticks_function)(int ticks);
        void (*on_timeout)(int seconds TSRMLS_DC);
+       zend_bool (*open_function)(const char *filename, struct _zend_file_handle *);
 } zend_utility_functions;
 
                
@@ -417,6 +419,7 @@
 extern ZEND_API int (*zend_printf)(const char *format, ...);
 extern ZEND_API zend_write_func_t zend_write;
 extern ZEND_API FILE *(*zend_fopen)(const char *filename, char **opened_path);
+extern ZEND_API zend_bool (*zend_open)(const char *filename, struct _zend_file_handle 
*fh);
 extern ZEND_API void (*zend_block_interruptions)(void);
 extern ZEND_API void (*zend_unblock_interruptions)(void);
 extern ZEND_API void (*zend_ticks_function)(int ticks);
Index: Zend/zend_compile.h
diff -u Zend/zend_compile.h:1.144.4.4 Zend/zend_compile.h:1.144.4.5
--- Zend/zend_compile.h:1.144.4.4       Wed May 21 03:53:20 2003
+++ Zend/zend_compile.h Fri May 30 21:37:43 2003
@@ -175,7 +175,7 @@
        zend_bool free_filename;
 } zend_file_handle;
 
-
+#define ZEND_IS_VALID_FILE_HANDLE(hn)   ((((hn)->type == ZEND_HANDLE_FD || (hn)->type 
== ZEND_HANDLE_SOCKET_FD) && (hn)->handle.fd >= 0) || ((hn)->type == ZEND_HANDLE_FP && 
(hn)->handle.fp != NULL))
 
 #define IS_CONST       (1<<0)
 #define IS_TMP_VAR     (1<<1)
@@ -568,6 +568,7 @@
 #define ZEND_HANDLE_FP                         2
 #define ZEND_HANDLE_STDIOSTREAM                3
 #define ZEND_HANDLE_FSTREAM                    4
+#define ZEND_HANDLE_SOCKET_FD          5
 
 #define ZEND_DECLARE_CLASS                             1
 #define ZEND_DECLARE_FUNCTION                  2
Index: Zend/zend_execute.c
diff -u Zend/zend_execute.c:1.316.2.10 Zend/zend_execute.c:1.316.2.11
--- Zend/zend_execute.c:1.316.2.10      Wed May 21 03:53:20 2003
+++ Zend/zend_execute.c Fri May 30 21:37:43 2003
@@ -2101,38 +2101,32 @@
                                        switch (EX(opline)->op2.u.constant.value.lval) 
{
                                                case ZEND_INCLUDE_ONCE:
                                                case ZEND_REQUIRE_ONCE: {
-                                                               char *opened_path=NULL;
                                                                int dummy = 1;
-                                                               zend_file_handle 
file_handle;
+                                                               zend_file_handle 
file_handle = {0};
 
-                                                               file_handle.handle.fp 
= zend_fopen(inc_filename->value.str.val, &opened_path);
-                                                               file_handle.type = 
ZEND_HANDLE_FP;
-                                                               file_handle.filename = 
inc_filename->value.str.val;
-                                                               
file_handle.opened_path = opened_path;
-                                                               
file_handle.free_filename = 0;
+                                                               if 
(zend_open(inc_filename->value.str.val, &file_handle) == SUCCESS 
+                                                                               && 
ZEND_IS_VALID_FILE_HANDLE(&file_handle)) {
 
-                                                               if 
(file_handle.handle.fp) {
-                                                                       if( 
!opened_path ) {
-                                                                               
opened_path = file_handle.opened_path = estrndup(inc_filename->value.str.val, 
inc_filename->value.str.len);
+                                                                       
file_handle.filename = inc_filename->value.str.val;
+                                                                       
file_handle.free_filename = 0;
+
+                                                                       if( 
!file_handle.opened_path ) {
+                                                                               
file_handle.opened_path = estrndup(inc_filename->value.str.val, 
inc_filename->value.str.len);
                                                                        }       
                                                                
-                                                                       if 
(zend_hash_add(&EG(included_files), opened_path, strlen(opened_path)+1, (void 
*)&dummy, sizeof(int), NULL)==SUCCESS) {
+                                                                       if 
(zend_hash_add(&EG(included_files), file_handle.opened_path, 
strlen(file_handle.opened_path)+1, (void *)&dummy, sizeof(int), NULL)==SUCCESS) {
                                                                                
new_op_array = zend_compile_file(&file_handle, 
(EX(opline)->op2.u.constant.value.lval==ZEND_INCLUDE_ONCE?ZEND_INCLUDE:ZEND_REQUIRE) 
TSRMLS_CC);
                                                                                
zend_destroy_file_handle(&file_handle TSRMLS_CC);
-                                                                               
opened_path = NULL; /* zend_destroy_file_handle() already frees it */
                                                                        } else {
-                                                                               
fclose(file_handle.handle.fp);
+                                                                               
zend_file_handle_dtor(&file_handle);
                                                                                
failure_retval=1;
                                                                        }
                                                                } else {
                                                                        if 
(EX(opline)->op2.u.constant.value.lval==ZEND_INCLUDE_ONCE) {
-                                                                               
zend_message_dispatcher(ZMSG_FAILED_INCLUDE_FOPEN, file_handle.filename);
+                                                                               
zend_message_dispatcher(ZMSG_FAILED_INCLUDE_FOPEN, inc_filename->value.str.val);
                                                                        } else {
-                                                                               
zend_message_dispatcher(ZMSG_FAILED_REQUIRE_FOPEN, file_handle.filename);
+                                                                               
zend_message_dispatcher(ZMSG_FAILED_REQUIRE_FOPEN, inc_filename->value.str.val);
                                                                        }
-                                                               }
-                                                               if (opened_path) {
-                                                                       
efree(opened_path);
                                                                }
                                                                break;
                                                        }
Index: Zend/zend_globals.h
diff -u Zend/zend_globals.h:1.93.2.3 Zend/zend_globals.h:1.93.2.4
--- Zend/zend_globals.h:1.93.2.3        Wed May 21 03:53:20 2003
+++ Zend/zend_globals.h Fri May 30 21:37:43 2003
@@ -238,8 +238,8 @@
 };
 
 struct _zend_scanner_globals {
-       FILE *yy_in;
-       FILE *yy_out;
+       zend_file_handle *yy_in;
+       zend_file_handle *yy_out;
        int yy_leng;
        char *yy_text;
        struct yy_buffer_state *current_buffer;
Index: Zend/zend_ini_scanner.h
diff -u Zend/zend_ini_scanner.h:1.10.8.2 Zend/zend_ini_scanner.h:1.10.8.3
--- Zend/zend_ini_scanner.h:1.10.8.2    Wed May 21 03:53:21 2003
+++ Zend/zend_ini_scanner.h     Fri May 30 21:37:43 2003
@@ -5,7 +5,6 @@
 int zend_ini_scanner_get_lineno(TSRMLS_D);
 char *zend_ini_scanner_get_filename(TSRMLS_D);
 int zend_ini_open_file_for_scanning(zend_file_handle *fh TSRMLS_DC);
-void zend_ini_close_file(zend_file_handle *fh TSRMLS_DC);
 int ini_lex(zval *ini_lval TSRMLS_DC);
 END_EXTERN_C()
 
Index: Zend/zend_ini_scanner.l
diff -u Zend/zend_ini_scanner.l:1.26.2.4 Zend/zend_ini_scanner.l:1.26.2.5
--- Zend/zend_ini_scanner.l:1.26.2.4    Wed May 21 03:53:21 2003
+++ Zend/zend_ini_scanner.l     Fri May 30 21:37:43 2003
@@ -33,6 +33,10 @@
 #include "zend_ini_parser.h"
 #include "zend_ini_scanner.h"
 
+#ifdef HAVE_UNISTD_H
+# include <unistd.h>
+#endif
+
 #undef YYSTYPE
 #define YYSTYPE zval
 
@@ -69,36 +73,41 @@
 
 int zend_ini_open_file_for_scanning(zend_file_handle *fh TSRMLS_DC)
 {
-       FILE *fp;
-
        switch (fh->type) {
-               case ZEND_HANDLE_FP:
-                       fp = fh->handle.fp;
-                       break;
                case ZEND_HANDLE_FILENAME:
-                       fp = zend_fopen(fh->filename, NULL);
                        fh->type = ZEND_HANDLE_FP;
+                       fh->handle.fp = zend_fopen(fh->filename, NULL);
                        break;
-               default:
-                       return FAILURE;
        }
 
+       if (!ZEND_IS_VALID_FILE_HANDLE(fh))
+               return FAILURE;
+
        init_ini_scanner(TSRMLS_C);
-       yyin = fp;
+       yyin = fh;
        yy_switch_to_buffer(yy_create_buffer(yyin, YY_BUF_SIZE TSRMLS_CC) TSRMLS_CC);
        ini_filename = fh->filename;
        return SUCCESS;
 }
 
-
 void zend_ini_close_file(zend_file_handle *fh TSRMLS_DC)
 {
-       switch (fh->type) {
-               case ZEND_HANDLE_FP:
-                       fclose(fh->handle.fp);
-                       break;
-       }
+    switch (fh->type) {
+        case ZEND_HANDLE_SOCKET_FD:
+#ifdef ZEND_WIN32
+            closesocket(fh->handle.fd);
+            break;
+#endif
+        /* fall-through */
+        case ZEND_HANDLE_FD:
+            close(fh->handle.fd);
+            break;
+        case ZEND_HANDLE_FP:
+            fclose(fh->handle.fp);
+            break;
+    }
 }
+
 
 %}
 
Index: Zend/zend_language_scanner.h
diff -u Zend/zend_language_scanner.h:1.10.4.3 Zend/zend_language_scanner.h:1.10.4.4
--- Zend/zend_language_scanner.h:1.10.4.3       Wed May 21 03:53:21 2003
+++ Zend/zend_language_scanner.h        Fri May 30 21:37:43 2003
@@ -24,7 +24,7 @@
 typedef struct _zend_lex_state {
        YY_BUFFER_STATE buffer_state;
        int state;
-       FILE *in;
+       zend_file_handle *in;
        uint lineno;
        char *filename;
 #ifdef ZEND_MULTIBYTE
Index: Zend/zend_language_scanner.l
diff -u Zend/zend_language_scanner.l:1.54.2.19 Zend/zend_language_scanner.l:1.54.2.20
--- Zend/zend_language_scanner.l:1.54.2.19      Wed May 21 03:58:49 2003
+++ Zend/zend_language_scanner.l        Fri May 30 21:37:43 2003
@@ -74,6 +74,11 @@
 #endif
 
 
+#ifndef STDIN_FILENO
+# define STDIN_FILENO 0
+#endif
+
+
 /* Globals Macros */
 #define SCNG   LANG_SCNG
 #ifdef ZTS
@@ -198,6 +203,15 @@
 ZEND_API void zend_file_handle_dtor(zend_file_handle *fh)
 {
        switch (fh->type) {
+               case ZEND_HANDLE_SOCKET_FD:
+#ifdef ZEND_WIN32
+                       closesocket(fh->handle.fd);
+                       break;
+#endif
+               /* fall-through */ 
+               case ZEND_HANDLE_FD:
+                       close(fh->handle.fd);
+                       break;
                case ZEND_HANDLE_FP:
                        fclose(fh->handle.fp);
                        break;
@@ -209,9 +223,11 @@
        }
        if (fh->opened_path) {
                efree(fh->opened_path);
+               fh->opened_path = NULL;
        }
        if (fh->free_filename && fh->filename) {
                efree(fh->filename);
+               fh->filename = NULL;
        }
 }
 
@@ -225,6 +241,10 @@
                case ZEND_HANDLE_FP:
                        return fh1->handle.fp==fh2->handle.fp;
                        break;
+               case ZEND_HANDLE_SOCKET_FD:
+               case ZEND_HANDLE_FD:
+                       return fh1->handle.fd==fh2->handle.fd;
+                       break;
        }
        return 0;
 }
@@ -239,28 +259,28 @@
 ZEND_API int open_file_for_scanning(zend_file_handle *file_handle TSRMLS_DC)
 {
        char *file_path=NULL;
+       int do_add = 1;
 
        switch (file_handle->type) {
                case ZEND_HANDLE_FILENAME:
-                       file_handle->handle.fp = zend_fopen(file_handle->filename, 
&file_handle->opened_path);
-                       break;
-               case ZEND_HANDLE_FD:
-                       file_handle->handle.fp = fdopen(file_handle->handle.fd, "r");
+                       if (zend_open(file_handle->filename, file_handle) != SUCCESS)
+                               return FAILURE;
                        break;
-               case ZEND_HANDLE_FP:
-                       file_handle->handle.fp = file_handle->handle.fp;
-                       break;
-       }
-       if (!file_handle->handle.fp) {
-               return FAILURE;
        }
 
-       file_handle->type = ZEND_HANDLE_FP;     
-       if (file_handle->handle.fp != stdin) {
+       if (!ZEND_IS_VALID_FILE_HANDLE(file_handle))
+                       return FAILURE;
+       
+       if (file_handle->type == ZEND_HANDLE_FP && file_handle->handle.fp == stdin)
+               do_add = 0;
+       else if (file_handle->type == ZEND_HANDLE_FD && file_handle->handle.fd == 
STDIN_FILENO)
+               do_add = 0;
+       
+       if (do_add) {
                zend_llist_add_element(&CG(open_files), file_handle);
        }
        /* Reset the scanner for scanning the new file */
-       SCNG(yy_in) = file_handle->handle.fp;
+       SCNG(yy_in) = file_handle;
 #ifdef ZEND_MULTIBYTE
        if (zend_read_file(TSRMLS_C) != 0) {
                return FAILURE;
@@ -391,7 +411,7 @@
 
 zend_op_array *compile_filename(int type, zval *filename TSRMLS_DC)
 {
-       zend_file_handle file_handle;
+       zend_file_handle file_handle = {0};
        zval tmp;
        zend_op_array *retval;
        char *opened_path = NULL;
@@ -407,9 +427,8 @@
        file_handle.type = ZEND_HANDLE_FILENAME;
        file_handle.opened_path = NULL;
 
-
        retval = zend_compile_file(&file_handle, type TSRMLS_CC);
-       if (retval && file_handle.handle.fp) {
+       if (retval && ZEND_IS_VALID_FILE_HANDLE(&file_handle)) {
                int dummy = 1;
        
                if (!file_handle.opened_path) {
@@ -544,7 +563,7 @@
 int highlight_file(char *filename, zend_syntax_highlighter_ini 
*syntax_highlighter_ini TSRMLS_DC)
 {
        zend_lex_state original_lex_state;
-       zend_file_handle file_handle;
+       zend_file_handle file_handle = {0};
 
        file_handle.type = ZEND_HANDLE_FILENAME;
        file_handle.filename = filename;
@@ -666,27 +685,41 @@
        SCNG(code) = NULL;
        SCNG(code_size) = 0;
 
-       do {
-               n = fread(buf, 1, 8192, SCNG(yy_in));
-               if (n) {
-                       SCNG(code_size) += n;
-                       if (SCNG(code)) {
-                               SCNG(code) = (char*)erealloc(SCNG(code), 
SCNG(code_size)+1);
-                               if (!SCNG(code)) {
-                                       return -1;
-                               }
-                       } else {
-                               SCNG(code) = (char*)emalloc(SCNG(code_size)+1);
-                               if (!SCNG(code)) {
-                                       return -1;
-                               }
+       for (;;) {
+               switch (SCNG(yy_in)->type) {
+                       case ZEND_HANDLE_FD:
+                               n = read(SCNG(yy_in)->handle.fd, buf, sizeof(buf));
+                               break;
+                       case ZEND_HANDLE_FP:
+                               n = fread(buf, sizeof(buf[0]), sizeof(buf) / 
sizeof(buf[0]), SCNG(yy_in)->handle.fp);
+                               break;
+                       case ZEND_HANDLE_SOCKET_FD:
+                               n =     recv(SCNG(yy_in)->handle.fd, buf, sizeof(buf), 
0);
+               }
+
+               if (n <= 0) {
+                       break;
+               }
+
+               SCNG(code_size) += n;
+               if (SCNG(code)) {
+                       SCNG(code) = (char*)erealloc(SCNG(code), SCNG(code_size)+1);
+                       if (!SCNG(code)) {
+                               return -1;
+                       }
+               } else {
+                       SCNG(code) = (char*)emalloc(SCNG(code_size)+1);
+                       if (!SCNG(code)) {
+                               return -1;
                        }
-                       memcpy(SCNG(code)+SCNG(code_size)-n, buf, n);
                }
-       } while(n);
-       if (ferror(SCNG(yy_in))) {
+               memcpy(SCNG(code)+SCNG(code_size)-n, buf, n);
+       }
+
+       if (n < 0) {
                return -1;
        }
+
        if (!SCNG(code)) {
                SCNG(code) = emalloc(SCNG(code_size)+1);
        }
Index: php4/ext/standard/basic_functions.c
diff -u php4/ext/standard/basic_functions.c:1.543.2.17 
php4/ext/standard/basic_functions.c:1.543.2.18
--- php4/ext/standard/basic_functions.c:1.543.2.17      Wed May 21 05:34:12 2003
+++ php4/ext/standard/basic_functions.c Fri May 30 21:37:43 2003
@@ -17,7 +17,7 @@
    +----------------------------------------------------------------------+
  */
 
-/* $Id: basic_functions.c,v 1.543.2.17 2003/05/21 09:34:12 zeev Exp $ */
+/* $Id: basic_functions.c,v 1.543.2.18 2003/05/31 01:37:43 sniper Exp $ */
 
 #include "php.h"
 #include "php_streams.h"
@@ -2840,7 +2840,7 @@
 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: php4/ext/standard/browscap.c
diff -u php4/ext/standard/browscap.c:1.60.2.11 php4/ext/standard/browscap.c:1.60.2.12
--- php4/ext/standard/browscap.c:1.60.2.11      Tue May 27 13:24:09 2003
+++ php4/ext/standard/browscap.c        Fri May 30 21:37:43 2003
@@ -16,7 +16,7 @@
    +----------------------------------------------------------------------+
  */
 
-/* $Id: browscap.c,v 1.60.2.11 2003/05/27 17:24:09 sniper Exp $ */
+/* $Id: browscap.c,v 1.60.2.12 2003/05/31 01:37:43 sniper Exp $ */
 
 #include "php.h"
 #include "php_regex.h"
@@ -150,7 +150,7 @@
        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: php4/main/main.c
diff -u php4/main/main.c:1.512.2.37 php4/main/main.c:1.512.2.38
--- php4/main/main.c:1.512.2.37 Wed May 21 18:54:38 2003
+++ php4/main/main.c    Fri May 30 21:37:43 2003
@@ -18,7 +18,7 @@
    +----------------------------------------------------------------------+
 */
 
-/* $Id: main.c,v 1.512.2.37 2003/05/21 22:54:38 iliaa Exp $ */
+/* $Id: main.c,v 1.512.2.38 2003/05/31 01:37:43 sniper Exp $ */
 
 /* {{{ includes
  */
@@ -763,6 +763,17 @@
 }
 /* }}} */
 
+
+/* {{{ 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 @@
        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_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: php4/main/php_ini.c
diff -u php4/main/php_ini.c:1.106.2.11 php4/main/php_ini.c:1.106.2.12
--- php4/main/php_ini.c:1.106.2.11      Wed May 21 05:34:13 2003
+++ php4/main/php_ini.c Fri May 30 21:37:44 2003
@@ -16,7 +16,7 @@
    +----------------------------------------------------------------------+
  */
 
-/* $Id: php_ini.c,v 1.106.2.11 2003/05/21 09:34:13 zeev Exp $ */
+/* $Id: php_ini.c,v 1.106.2.12 2003/05/31 01:37:44 sniper Exp $ */
 
 /* Check CWD for php.ini */
 #define INI_CHECK_CWD
@@ -236,7 +236,7 @@
        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 @@
        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: php4/main/php_streams.h
diff -u php4/main/php_streams.h:1.61.2.13 php4/main/php_streams.h:1.61.2.14
--- php4/main/php_streams.h:1.61.2.13   Wed May 21 06:19:03 2003
+++ php4/main/php_streams.h     Fri May 30 21:37:44 2003
@@ -16,7 +16,7 @@
    +----------------------------------------------------------------------+
  */
 
-/* $Id: php_streams.h,v 1.61.2.13 2003/05/21 10:19:03 zeev Exp $ */
+/* $Id: php_streams.h,v 1.61.2.14 2003/05/31 01:37:44 sniper Exp $ */
 
 #ifndef PHP_STREAMS_H
 #define PHP_STREAMS_H
@@ -553,6 +553,9 @@
 PHPAPI FILE * _php_stream_open_wrapper_as_file(char * path, char * mode, int options, 
char **opened_path STREAMS_DC TSRMLS_DC);
 #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;
Index: php4/main/streams.c
diff -u php4/main/streams.c:1.125.2.67 php4/main/streams.c:1.125.2.68
--- php4/main/streams.c:1.125.2.67      Wed May 28 06:15:00 2003
+++ php4/main/streams.c Fri May 30 21:37:44 2003
@@ -20,7 +20,7 @@
    +----------------------------------------------------------------------+
  */
 
-/* $Id: streams.c,v 1.125.2.67 2003/05/28 10:15:00 wez Exp $ */
+/* $Id: streams.c,v 1.125.2.68 2003/05/31 01:37:44 sniper Exp $ */
 
 #define _GNU_SOURCE
 #include "php.h"
@@ -2728,6 +2728,45 @@
 
 
 
+/* {{{ 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: php4/sapi/aolserver/aolserver.c
diff -u php4/sapi/aolserver/aolserver.c:1.72.2.3 
php4/sapi/aolserver/aolserver.c:1.72.2.4
--- php4/sapi/aolserver/aolserver.c:1.72.2.3    Wed May 21 05:34:13 2003
+++ php4/sapi/aolserver/aolserver.c     Fri May 30 21:37:44 2003
@@ -22,7 +22,7 @@
  * - CGI/1.1 conformance
  */
 
-/* $Id: aolserver.c,v 1.72.2.3 2003/05/21 09:34:13 zeev Exp $ */
+/* $Id: aolserver.c,v 1.72.2.4 2003/05/31 01:37:44 sniper Exp $ */
 
 /* conflict between PHP and AOLserver headers */
 #define Debug php_Debug
@@ -205,7 +205,7 @@
        int i;
        
        php_info_print_table_start();
-       php_info_print_table_row(2, "SAPI module version", "$Id: aolserver.c,v 
1.72.2.3 2003/05/21 09:34:13 zeev Exp $");
+       php_info_print_table_row(2, "SAPI module version", "$Id: aolserver.c,v 
1.72.2.4 2003/05/31 01:37:44 sniper Exp $");
        php_info_print_table_row(2, "Build date", Ns_InfoBuildDate());
        php_info_print_table_row(2, "Config file path", Ns_InfoConfigFile());
        php_info_print_table_row(2, "Error Log path", Ns_InfoErrorLog());
@@ -404,7 +404,7 @@
 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: php4/sapi/apache/mod_php4.c
diff -u php4/sapi/apache/mod_php4.c:1.146.2.10 php4/sapi/apache/mod_php4.c:1.146.2.11
--- php4/sapi/apache/mod_php4.c:1.146.2.10      Wed May 21 05:34:13 2003
+++ php4/sapi/apache/mod_php4.c Fri May 30 21:37:44 2003
@@ -17,7 +17,7 @@
    | PHP 4.0 patches by Zeev Suraski <[EMAIL PROTECTED]>                      |
    +----------------------------------------------------------------------+
  */
-/* $Id: mod_php4.c,v 1.146.2.10 2003/05/21 09:34:13 zeev Exp $ */
+/* $Id: mod_php4.c,v 1.146.2.11 2003/05/31 01:37:44 sniper Exp $ */
 
 #include "php_apache_http.h"
 #include "http_conf_globals.h"
@@ -534,7 +534,7 @@
        TSRMLS_FETCH();
 
        if (AP(in_request)) {
-               zend_file_handle fh;
+               zend_file_handle fh = {0};
 
                fh.filename = r->filename;
                fh.opened_path = NULL;
Index: php4/sapi/apache/sapi_apache.c
diff -u php4/sapi/apache/sapi_apache.c:1.40.4.4 php4/sapi/apache/sapi_apache.c:1.40.4.5
--- php4/sapi/apache/sapi_apache.c:1.40.4.4     Wed May 21 05:34:13 2003
+++ php4/sapi/apache/sapi_apache.c      Fri May 30 21:37:44 2003
@@ -19,7 +19,7 @@
    | Stig Bakken <[EMAIL PROTECTED]>                                            |
    +----------------------------------------------------------------------+
  */
-/* $Id: sapi_apache.c,v 1.40.4.4 2003/05/21 09:34:13 zeev Exp $ */
+/* $Id: sapi_apache.c,v 1.40.4.5 2003/05/31 01:37:44 sniper Exp $ */
 
 #include "php_apache_http.h"
 
@@ -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: php4/sapi/apache2filter/sapi_apache2.c
diff -u php4/sapi/apache2filter/sapi_apache2.c:1.91.2.15 
php4/sapi/apache2filter/sapi_apache2.c:1.91.2.16
--- php4/sapi/apache2filter/sapi_apache2.c:1.91.2.15    Thu May 22 22:42:22 2003
+++ php4/sapi/apache2filter/sapi_apache2.c      Fri May 30 21:37:44 2003
@@ -18,7 +18,7 @@
    +----------------------------------------------------------------------+
  */
 
-/* $Id: sapi_apache2.c,v 1.91.2.15 2003/05/23 02:42:22 iliaa Exp $ */
+/* $Id: sapi_apache2.c,v 1.91.2.16 2003/05/31 01:37:44 sniper Exp $ */
 
 #include <fcntl.h>
 
@@ -452,7 +452,7 @@
        }
 
        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: php4/sapi/apache2handler/sapi_apache2.c
diff -u php4/sapi/apache2handler/sapi_apache2.c:1.1.2.15 
php4/sapi/apache2handler/sapi_apache2.c:1.1.2.16
--- php4/sapi/apache2handler/sapi_apache2.c:1.1.2.15    Thu May 22 22:42:22 2003
+++ php4/sapi/apache2handler/sapi_apache2.c     Fri May 30 21:37:44 2003
@@ -18,7 +18,7 @@
    +----------------------------------------------------------------------+
  */
 
-/* $Id: sapi_apache2.c,v 1.1.2.15 2003/05/23 02:42:22 iliaa Exp $ */
+/* $Id: sapi_apache2.c,v 1.1.2.16 2003/05/31 01:37:44 sniper Exp $ */
 
 #include <fcntl.h>
 
@@ -514,7 +514,7 @@
                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: php4/sapi/caudium/caudium.c
diff -u php4/sapi/caudium/caudium.c:1.28.2.3 php4/sapi/caudium/caudium.c:1.28.2.4
--- php4/sapi/caudium/caudium.c:1.28.2.3        Wed May 21 05:34:14 2003
+++ php4/sapi/caudium/caudium.c Fri May 30 21:37:44 2003
@@ -17,7 +17,7 @@
    +----------------------------------------------------------------------+
  */
 
-/* $Id: caudium.c,v 1.28.2.3 2003/05/21 09:34:14 zeev Exp $ */
+/* $Id: caudium.c,v 1.28.2.4 2003/05/31 01:37:44 sniper Exp $ */
 
 #include "php.h"
 #ifdef HAVE_CAUDIUM
@@ -444,7 +444,7 @@
 {
   /*  char buf[512]; */
   php_info_print_table_start();
-  php_info_print_table_row(2, "SAPI module version", "$Id: caudium.c,v 1.28.2.3 
2003/05/21 09:34:14 zeev Exp $");
+  php_info_print_table_row(2, "SAPI module version", "$Id: caudium.c,v 1.28.2.4 
2003/05/31 01:37:44 sniper Exp $");
   /*  php_info_print_table_row(2, "Build date", Ns_InfoBuildDate());
       php_info_print_table_row(2, "Config file path", Ns_InfoConfigFile());
       php_info_print_table_row(2, "Error Log path", Ns_InfoErrorLog());
@@ -564,7 +564,7 @@
 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: php4/sapi/cgi/cgi_main.c
diff -u php4/sapi/cgi/cgi_main.c:1.190.2.34 php4/sapi/cgi/cgi_main.c:1.190.2.35
--- php4/sapi/cgi/cgi_main.c:1.190.2.34 Fri May 30 13:03:10 2003
+++ php4/sapi/cgi/cgi_main.c    Fri May 30 21:37:44 2003
@@ -20,7 +20,7 @@
    +----------------------------------------------------------------------+
 */
 
-/* $Id: cgi_main.c,v 1.190.2.34 2003/05/30 17:03:10 derick Exp $ */
+/* $Id: cgi_main.c,v 1.190.2.35 2003/05/31 01:37:44 sniper Exp $ */
 
 #include "php.h"
 #include "php_globals.h"
@@ -117,31 +117,8 @@
 #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 @@
 #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 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 @@
                /* 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 @@
                                   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 @@
                        && !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 @@
                                                break;
                                }
                        }
-                       optind = orig_optind;
-                       optarg = orig_optarg;
+                       ap_php_optind = orig_optind;
+                       ap_php_optarg = orig_optarg;
                }
 
 #if PHP_FASTCGI
@@ -1276,7 +1254,7 @@
                                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 @@
                                                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 @@
                                                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 @@
                                                break;
 
                                case 'z': /* load extension file */
-                                               zend_load_extension(optarg);
+                                               zend_load_extension(ap_php_optarg);
                                                break;
 
                                        default:
@@ -1399,12 +1388,12 @@
                                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 @@
                           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: php4/sapi/cgi/config9.m4
diff -u php4/sapi/cgi/config9.m4:1.1.2.5 php4/sapi/cgi/config9.m4:1.1.2.6
--- php4/sapi/cgi/config9.m4:1.1.2.5    Thu May 29 10:55:07 2003
+++ php4/sapi/cgi/config9.m4    Fri May 30 21:37:44 2003
@@ -1,5 +1,5 @@
 dnl
-dnl $Id: config9.m4,v 1.1.2.5 2003/05/29 14:55:07 sas Exp $
+dnl $Id: config9.m4,v 1.1.2.6 2003/05/31 01:37:44 sniper Exp $
 dnl
 
 AC_ARG_ENABLE(cgi,
Index: php4/sapi/cgi/getopt.c
diff -u php4/sapi/cgi/getopt.c:1.5.8.1 php4/sapi/cgi/getopt.c:1.5.8.2
--- php4/sapi/cgi/getopt.c:1.5.8.1      Thu May 29 11:41:52 2003
+++ php4/sapi/cgi/getopt.c      Fri May 30 21:37:44 2003
@@ -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          |
-   | [EMAIL PROTECTED] so we can mail you a copy immediately.               |
-   +----------------------------------------------------------------------+
-   | Author: Marcus Boerger <[EMAIL PROTECTED]>                               |
-   +----------------------------------------------------------------------+
-*/
+/* Borrowed from Apache NT Port */
 
 #include <stdio.h>
 #include <string.h>
@@ -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;
-
-       int opts_idx = -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 */
-       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 */
+    static int optchr = 0;
+    static int dash = 0; /* have already seen the - */
+
+    char *cp;
+
+    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;
+    }
+
+    /* 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;
+
+        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: php4/sapi/cgi/php_getopt.h
diff -u php4/sapi/cgi/php_getopt.h:1.3.14.1 php4/sapi/cgi/php_getopt.h:1.3.14.2
--- php4/sapi/cgi/php_getopt.h:1.3.14.1 Thu May 29 11:41:52 2003
+++ php4/sapi/cgi/php_getopt.h  Fri May 30 21:37:44 2003
@@ -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          |
-   | [EMAIL PROTECTED] so we can mail you a copy immediately.               |
-   +----------------------------------------------------------------------+
-   | Author: Marcus Boerger <[EMAIL PROTECTED]>                               |
-   +----------------------------------------------------------------------+
-*/
-
+/* 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: php4/sapi/cli/README
diff -u php4/sapi/cli/README:1.4.4.1 php4/sapi/cli/README:1.4.4.2
--- php4/sapi/cli/README:1.4.4.1        Thu May 29 11:28:01 2003
+++ php4/sapi/cli/README        Fri May 30 21:37:45 2003
@@ -16,5 +16,4 @@
 * 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: php4/sapi/cli/getopt.c
diff -u php4/sapi/cli/getopt.c:1.3.4.1 php4/sapi/cli/getopt.c:1.3.4.2
--- php4/sapi/cli/getopt.c:1.3.4.1      Thu May 29 11:28:01 2003
+++ php4/sapi/cli/getopt.c      Fri May 30 21:37:45 2003
@@ -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          |
-   | [EMAIL PROTECTED] so we can mail you a copy immediately.               |
-   +----------------------------------------------------------------------+
-   | Author: Marcus Boerger <[EMAIL PROTECTED]>                               |
-   +----------------------------------------------------------------------+
-*/
-
-/* $Id: getopt.c,v 1.3.4.1 2003/05/29 15:28:01 helly Exp $ */
+/* Borrowed from Apache NT Port */
 
 #include <stdio.h>
 #include <string.h>
@@ -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;
-
-       int opts_idx = -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 */
-       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 */
+    static int optchr = 0;
+    static int dash = 0; /* have already seen the - */
+
+    char *cp;
+
+    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;
+    }
+
+    /* 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;
+
+        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: php4/sapi/cli/php_cli.c
diff -u php4/sapi/cli/php_cli.c:1.51.2.22 php4/sapi/cli/php_cli.c:1.51.2.23
--- php4/sapi/cli/php_cli.c:1.51.2.22   Fri May 30 13:03:10 2003
+++ php4/sapi/cli/php_cli.c     Fri May 30 21:37:45 2003
@@ -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;
+extern char *ap_php_optarg;
+extern int ap_php_optind;
 
-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 */
-};
+#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 @@
                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 @@
                                "  -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 @@
                                "\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 @@
        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 @@
                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 @@
        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 @@
        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 @@
 #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 @@
        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 @@
                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 @@
                                /* 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 @@
                                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 @@
                                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 @@
                                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 @@
                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 @@
                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 @@
                        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 @@
                                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: php4/sapi/cli/php_getopt.h
diff -u php4/sapi/cli/php_getopt.h:1.1.8.1 php4/sapi/cli/php_getopt.h:1.1.8.2
--- php4/sapi/cli/php_getopt.h:1.1.8.1  Thu May 29 11:28:01 2003
+++ php4/sapi/cli/php_getopt.h  Fri May 30 21:37:45 2003
@@ -16,17 +16,13 @@
    +----------------------------------------------------------------------+
 */
 
-/* $Id: php_getopt.h,v 1.1.8.1 2003/05/29 15:28:01 helly Exp $ */
+/* $Id: php_getopt.h,v 1.1.8.2 2003/05/31 01:37:45 sniper Exp $ */
 
 #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: php4/sapi/isapi/php4isapi.c
diff -u php4/sapi/isapi/php4isapi.c:1.92.2.6 php4/sapi/isapi/php4isapi.c:1.92.2.7
--- php4/sapi/isapi/php4isapi.c:1.92.2.6        Wed May 21 05:34:15 2003
+++ php4/sapi/isapi/php4isapi.c Fri May 30 21:37:45 2003
@@ -717,7 +717,7 @@
 
 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: php4/sapi/phttpd/phttpd.c
diff -u php4/sapi/phttpd/phttpd.c:1.35.2.3 php4/sapi/phttpd/phttpd.c:1.35.2.4
--- php4/sapi/phttpd/phttpd.c:1.35.2.3  Wed May 21 05:34:16 2003
+++ php4/sapi/phttpd/phttpd.c   Fri May 30 21:37:45 2003
@@ -247,7 +247,7 @@
 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: php4/sapi/pi3web/pi3web_sapi.c
diff -u php4/sapi/pi3web/pi3web_sapi.c:1.46.2.4 php4/sapi/pi3web/pi3web_sapi.c:1.46.2.5
--- php4/sapi/pi3web/pi3web_sapi.c:1.46.2.4     Wed May 21 05:34:16 2003
+++ php4/sapi/pi3web/pi3web_sapi.c      Fri May 30 21:37:45 2003
@@ -21,7 +21,7 @@
    +----------------------------------------------------------------------+
  */
 
-/* $Id: pi3web_sapi.c,v 1.46.2.4 2003/05/21 09:34:16 zeev Exp $ */
+/* $Id: pi3web_sapi.c,v 1.46.2.5 2003/05/31 01:37:45 sniper Exp $ */
 
 #include "pi3web_sapi.h"
 #include "php.h"
@@ -78,7 +78,7 @@
        PUTS("<table border=0 cellpadding=3 cellspacing=1 width=600 align=center>\n");
        PUTS("<tr><th colspan=2 bgcolor=\"" PHP_HEADER_COLOR "\">Pi3Web Server 
Information</th></tr>\n");
        php_info_print_table_header(2, "Information Field", "Value");
-       php_info_print_table_row(2, "Pi3Web SAPI module version", "$Id: 
pi3web_sapi.c,v 1.46.2.4 2003/05/21 09:34:16 zeev Exp $");
+       php_info_print_table_row(2, "Pi3Web SAPI module version", "$Id: 
pi3web_sapi.c,v 1.46.2.5 2003/05/31 01:37:45 sniper Exp $");
        php_info_print_table_row(2, "Server Name Stamp", HTTPCore_getServerStamp());
        snprintf(variable_buf, 511, "%d", HTTPCore_debugEnabled());
        php_info_print_table_row(2, "Debug Enabled", variable_buf);
@@ -387,7 +387,7 @@
 
 DWORD PHP4_wrapper(LPCONTROL_BLOCK lpCB)
 {
-       zend_file_handle file_handle;
+       zend_file_handle file_handle = {0};
        int iRet = PIAPI_COMPLETED;
        TSRMLS_FETCH();
 
Index: php4/sapi/roxen/roxen.c
diff -u php4/sapi/roxen/roxen.c:1.53.2.4 php4/sapi/roxen/roxen.c:1.53.2.5
--- php4/sapi/roxen/roxen.c:1.53.2.4    Wed May 21 05:34:16 2003
+++ php4/sapi/roxen/roxen.c     Fri May 30 21:37:45 2003
@@ -17,7 +17,7 @@
    +----------------------------------------------------------------------+
  */
 
-/* $Id: roxen.c,v 1.53.2.4 2003/05/21 09:34:16 zeev Exp $ */
+/* $Id: roxen.c,v 1.53.2.5 2003/05/31 01:37:45 sniper Exp $ */
 
 #include "php.h"
 #ifdef HAVE_ROXEN
@@ -438,7 +438,7 @@
 {
   /*  char buf[512]; */
   php_info_print_table_start();
-  php_info_print_table_row(2, "SAPI module version", "$Id: roxen.c,v 1.53.2.4 
2003/05/21 09:34:16 zeev Exp $");
+  php_info_print_table_row(2, "SAPI module version", "$Id: roxen.c,v 1.53.2.5 
2003/05/31 01:37:45 sniper Exp $");
   /*  php_info_print_table_row(2, "Build date", Ns_InfoBuildDate());
       php_info_print_table_row(2, "Config file path", Ns_InfoConfigFile());
       php_info_print_table_row(2, "Error Log path", Ns_InfoErrorLog());
@@ -575,7 +575,7 @@
 {
   int res, len;
   char *dir;
-  zend_file_handle file_handle;
+  zend_file_handle file_handle = {0};
 #ifdef ROXEN_USE_ZTS
   GET_THIS();
 #endif
Index: php4/sapi/servlet/servlet.c
diff -u php4/sapi/servlet/servlet.c:1.65.2.5 php4/sapi/servlet/servlet.c:1.65.2.6
--- php4/sapi/servlet/servlet.c:1.65.2.5        Wed May 21 05:34:16 2003
+++ php4/sapi/servlet/servlet.c Fri May 30 21:37:45 2003
@@ -310,7 +310,7 @@
         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: php4/sapi/thttpd/thttpd.c
diff -u php4/sapi/thttpd/thttpd.c:1.77.2.11 php4/sapi/thttpd/thttpd.c:1.77.2.12
--- php4/sapi/thttpd/thttpd.c:1.77.2.11 Wed May 21 05:34:16 2003
+++ php4/sapi/thttpd/thttpd.c   Fri May 30 21:37:45 2003
@@ -16,7 +16,7 @@
    +----------------------------------------------------------------------+
 */
 
-/* $Id: thttpd.c,v 1.77.2.11 2003/05/21 09:34:16 zeev Exp $ */
+/* $Id: thttpd.c,v 1.77.2.12 2003/05/31 01:37:45 sniper Exp $ */
 
 #include "php.h"
 #include "SAPI.h"
@@ -388,7 +388,7 @@
 
 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: php4/sapi/tux/php_tux.c
diff -u php4/sapi/tux/php_tux.c:1.18.2.4 php4/sapi/tux/php_tux.c:1.18.2.5
--- php4/sapi/tux/php_tux.c:1.18.2.4    Wed May 21 05:34:17 2003
+++ php4/sapi/tux/php_tux.c     Fri May 30 21:37:45 2003
@@ -296,7 +296,7 @@
 
 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: php4/sapi/webjames/webjames.c
diff -u php4/sapi/webjames/webjames.c:1.5.2.3 php4/sapi/webjames/webjames.c:1.5.2.4
--- php4/sapi/webjames/webjames.c:1.5.2.3       Wed May 21 05:34:17 2003
+++ php4/sapi/webjames/webjames.c       Fri May 30 21:37:45 2003
@@ -142,7 +142,7 @@
 
 static void webjames_module_main(TSRMLS_D)
 {
-       zend_file_handle file_handle;
+       zend_file_handle file_handle = {0};
        FILE *fp=NULL;
        char *path;
 

-- 
PHP CVS Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to