Skip to content

Commit

Permalink
Implement simple stream support in the ZE scanners.
Browse files Browse the repository at this point in the history
  • Loading branch information
wez committed Feb 18, 2003
1 parent 500dfcf commit e52aac9
Show file tree
Hide file tree
Showing 16 changed files with 274 additions and 99 deletions.
2 changes: 1 addition & 1 deletion Zend/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ libZend_la_SOURCES=\
zend_variables.c zend.c zend_API.c zend_extensions.c zend_hash.c \
zend_list.c zend_indent.c zend_builtin_functions.c zend_sprintf.c \
zend_ini.c zend_qsort.c zend_objects.c zend_object_handlers.c \
zend_object_API.c
zend_object_API.c zend_ts_hash.c zend_stream.c

libZend_la_LDFLAGS =
libZend_la_LIBADD = @ZEND_EXTRA_LIBS@
Expand Down
42 changes: 26 additions & 16 deletions Zend/flex.skl
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

/* Scanner skeleton version:
* $Header$
* vim:ft=lex:
*/

#define FLEX_SCANNER
Expand Down Expand Up @@ -154,7 +155,7 @@ typedef unsigned int yy_size_t;
struct yy_buffer_state
{
%-
FILE *yy_input_file;
struct _zend_file_handle *yy_input_file;
%+
istream* yy_input_file;
%*
Expand Down Expand Up @@ -256,13 +257,13 @@ static int yy_n_chars; /* number of characters read into yy_ch_buf */
/* 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 )

Expand Down Expand Up @@ -434,6 +435,18 @@ YY_MALLOC_DECL
/* Copy whatever the last rule matched to the standard output. */
/* Zend file handle reading */
#ifndef ECHO
#define ECHO /* There is no output */
#endif
#define YY_INPUT(buf, result, max_size) \
if ( ((result = zend_stream_read(yyin, buf, max_size TSRMLS_CC)) == 0) \
&& zend_stream_ferror( yyin ) ) \
YY_FATAL_ERROR( "input in flex scanner failed" );
#ifndef ECHO
%- Standard (non-C++) definition
/* This used to be an fputs(), but since the string might contain NUL's,
Expand Down Expand Up @@ -524,6 +537,7 @@ YY_DECL
if ( ! yy_start )
yy_start = 1; /* first start state */
#if 0
if ( ! SCNG(yy_in) )
%-
SCNG(yy_in) = stdin;
Expand All @@ -537,6 +551,7 @@ YY_DECL
%+
SCNG(yy_out) = &cout;
%*
#endif
if ( ! yy_current_buffer )
yy_current_buffer =
Expand Down Expand Up @@ -1111,10 +1126,10 @@ int yyFlexLexer::yyinput(TSRMLS_D)

%-
#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 )
Expand Down Expand Up @@ -1187,10 +1202,10 @@ void yyFlexLexer::yy_load_buffer_state()

%-
#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;
Expand Down Expand Up @@ -1251,18 +1266,13 @@ void yyFlexLexer::yy_delete_buffer( YY_BUFFER_STATE b TSRMLS_DC )


%-
#ifndef YY_ALWAYS_INTERACTIVE
#ifndef YY_NEVER_INTERACTIVE
ZEND_DLIMPORT 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
Expand All @@ -1286,7 +1296,7 @@ void yyFlexLexer::yy_init_buffer( YY_BUFFER_STATE b, istream* file TSRMLS_DC )
#if YY_NEVER_INTERACTIVE
b->yy_is_interactive = 0;
#else
b->yy_is_interactive = file ? (isatty( fileno(file) ) > 0) : 0;
b->yy_is_interactive = file->handle.stream.interactive;
#endif
#endif
%+
Expand Down
2 changes: 2 additions & 0 deletions Zend/zend.c
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ ZEND_API zend_class_entry *zend_standard_class_def = NULL;
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 int (*zend_stream_open_function)(const char *filename, zend_file_handle *handle TSRMLS_DC);
ZEND_API void (*zend_block_interruptions)(void);
ZEND_API void (*zend_unblock_interruptions)(void);
ZEND_API void (*zend_ticks_function)(int ticks);
Expand Down Expand Up @@ -534,6 +535,7 @@ int zend_startup(zend_utility_functions *utility_functions, char **extensions, i
if (!zend_fopen) {
zend_fopen = zend_fopen_wrapper;
}
zend_stream_open_function = utility_functions->stream_open_function;
zend_message_dispatcher_p = utility_functions->message_handler;
zend_block_interruptions = utility_functions->block_interruptions;
zend_unblock_interruptions = utility_functions->unblock_interruptions;
Expand Down
3 changes: 3 additions & 0 deletions Zend/zend.h
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,7 @@ struct _zend_class_entry {

typedef struct _zend_class_entry zend_namespace; /* namespace is the same as class */

#include "zend_stream.h"
typedef struct _zend_utility_functions {
void (*error_function)(int type, const char *error_filename, const uint error_lineno, const char *format, va_list args);
int (*printf_function)(const char *format, ...);
Expand All @@ -353,6 +354,7 @@ typedef struct _zend_utility_functions {
int (*get_configuration_directive)(char *name, uint name_length, zval *contents);
void (*ticks_function)(int ticks);
void (*on_timeout)(int seconds TSRMLS_DC);
int (*stream_open_function)(const char *filename, zend_file_handle *handle TSRMLS_DC);
} zend_utility_functions;


Expand Down Expand Up @@ -485,6 +487,7 @@ extern ZEND_API void (*zend_unblock_interruptions)(void);
extern ZEND_API void (*zend_ticks_function)(int ticks);
extern ZEND_API void (*zend_error_cb)(int type, const char *error_filename, const uint error_lineno, const char *format, va_list args);
extern void (*zend_on_timeout)(int seconds TSRMLS_DC);
extern ZEND_API int (*zend_stream_open_function)(const char *filename, zend_file_handle *handle TSRMLS_DC);


ZEND_API void zend_error(int type, const char *format, ...);
Expand Down
13 changes: 1 addition & 12 deletions Zend/zend_compile.h
Original file line number Diff line number Diff line change
Expand Up @@ -198,18 +198,6 @@ typedef struct _list_llist_element {
znode value;
} list_llist_element;


typedef struct _zend_file_handle {
zend_uchar type;
char *filename;
char *opened_path;
union {
int fd;
FILE *fp;
} handle;
zend_bool free_filename;
} zend_file_handle;

union _temp_variable;

struct _zend_execute_data {
Expand Down Expand Up @@ -726,6 +714,7 @@ int zendlex(znode *zendlval TSRMLS_DC);
#define ZEND_HANDLE_FP 2
#define ZEND_HANDLE_STDIOSTREAM 3
#define ZEND_HANDLE_FSTREAM 4
#define ZEND_HANDLE_STREAM 5

#define ZEND_FETCH_STANDARD 0
#define ZEND_FETCH_ADD_LOCK 1
Expand Down
20 changes: 5 additions & 15 deletions Zend/zend_execute.c
Original file line number Diff line number Diff line change
Expand Up @@ -3338,28 +3338,21 @@ int zend_include_or_eval_handler(ZEND_OPCODE_HANDLER_ARGS)
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;

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 (SUCCESS == zend_stream_open(inc_filename->value.str.val, &file_handle TSRMLS_CC)) {

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);
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) {
CG(active_namespace) = EG(active_namespace);
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 {
Expand All @@ -3369,9 +3362,6 @@ int zend_include_or_eval_handler(ZEND_OPCODE_HANDLER_ARGS)
zend_message_dispatcher(ZMSG_FAILED_REQUIRE_FOPEN, file_handle.filename);
}
}
if (opened_path) {
efree(opened_path);
}
break;
}
break;
Expand Down
4 changes: 2 additions & 2 deletions Zend/zend_globals.h
Original file line number Diff line number Diff line change
Expand Up @@ -254,8 +254,8 @@ struct _zend_alloc_globals {
};

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;
Expand Down
22 changes: 4 additions & 18 deletions Zend/zend_ini_scanner.l
Original file line number Diff line number Diff line change
Expand Up @@ -71,22 +71,12 @@ char *zend_ini_scanner_get_filename(TSRMLS_D)

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;
break;
default:
return FAILURE;
if (FAILURE == zend_stream_fixup(fh TSRMLS_CC)) {
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;
Expand All @@ -95,11 +85,7 @@ int zend_ini_open_file_for_scanning(zend_file_handle *fh TSRMLS_DC)

void zend_ini_close_file(zend_file_handle *fh TSRMLS_DC)
{
switch (fh->type) {
case ZEND_HANDLE_FP:
fclose(fh->handle.fp);
break;
}
zend_stream_close(fh);
}

%}
Expand Down
2 changes: 1 addition & 1 deletion Zend/zend_language_scanner.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
typedef struct _zend_lex_state {
YY_BUFFER_STATE buffer_state;
int state;
FILE *in;
zend_file_handle *in;
uint lineno;
char *filename;
} zend_lex_state;
Expand Down
33 changes: 15 additions & 18 deletions Zend/zend_language_scanner.l
Original file line number Diff line number Diff line change
Expand Up @@ -171,10 +171,15 @@ BEGIN_EXTERN_C()

ZEND_API void zend_file_handle_dtor(zend_file_handle *fh)
{
TSRMLS_FETCH();

switch (fh->type) {
case ZEND_HANDLE_FP:
fclose(fh->handle.fp);
break;
case ZEND_HANDLE_STREAM:
fh->handle.stream.closer(fh->handle.stream.handle TSRMLS_CC);
break;
case ZEND_HANDLE_FILENAME:
/* We're only supposed to get here when destructing the used_files hash,
* which doesn't really contain open files, but references to their names/paths
Expand All @@ -183,9 +188,11 @@ ZEND_API void zend_file_handle_dtor(zend_file_handle *fh)
}
if (fh->opened_path) {
efree(fh->opened_path);
fh->opened_path = NULL;
}
if (fh->free_filename && fh->filename) {
efree(fh->filename);
fh->filename = NULL;
}
}

Expand All @@ -199,6 +206,9 @@ int zend_compare_file_handles(zend_file_handle *fh1, zend_file_handle *fh2)
case ZEND_HANDLE_FP:
return fh1->handle.fp==fh2->handle.fp;
break;
case ZEND_HANDLE_STREAM:
return fh1->handle.stream.handle == fh2->handle.stream.handle;
break;
}
return 0;
}
Expand All @@ -214,27 +224,14 @@ ZEND_API int open_file_for_scanning(zend_file_handle *file_handle TSRMLS_DC)
{
char *file_path=NULL;

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");
break;
case ZEND_HANDLE_FP:
file_handle->handle.fp = file_handle->handle.fp;
break;
}
if (!file_handle->handle.fp) {
if (FAILURE == zend_stream_fixup(file_handle TSRMLS_CC)) {
return FAILURE;
}

file_handle->type = ZEND_HANDLE_FP;
if (file_handle->handle.fp != stdin) {
zend_llist_add_element(&CG(open_files), file_handle);
}
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;
yy_switch_to_buffer(yy_create_buffer(SCNG(yy_in), YY_BUF_SIZE TSRMLS_CC) TSRMLS_CC);
BEGIN(INITIAL);

Expand Down Expand Up @@ -339,7 +336,7 @@ zend_op_array *compile_filename(int type, zval *filename TSRMLS_DC)


retval = zend_compile_file(&file_handle, type TSRMLS_CC);
if (retval && file_handle.handle.fp) {
if (retval && file_handle.handle.stream.handle) {
int dummy = 1;

if (!file_handle.opened_path) {
Expand Down
Loading

0 comments on commit e52aac9

Please sign in to comment.