diff options
author | Jani Taskinen <jani@php.net> | 2007-09-28 02:05:10 +0000 |
---|---|---|
committer | Jani Taskinen <jani@php.net> | 2007-09-28 02:05:10 +0000 |
commit | 09b6f37f20b239dd021c21a1756629c4a64dc2da (patch) | |
tree | 635d42eb3591448faedff41f62ce0fff087ea467 /Zend | |
parent | 0d3bdf23d24ede05b07d613d030514861ab475e5 (diff) | |
download | php-git-09b6f37f20b239dd021c21a1756629c4a64dc2da.tar.gz |
MFH:
- Added ".htaccess" style user-defined php.ini files support for
CGI/FastCGI.
- Added support for special [PATH=/opt/httpd/www.example.com/] sections
in php.ini. All directives set in these sections will not be able to be
overridden in user-defined ini-files or during runtime in the specified
path.
- Improved php.ini handling:
. Added better error reporting for syntax errors in php.ini files
. Allowed "ini-variables" to be used almost everywhere ini php.ini files
. Allowed using alphanumeric/variable indexes in "array" ini options
. Fixed get_cfg_var() to be able to return "array" ini options
- Fixed bug #27372 (parse error loading browscap.ini at apache startup)
- Fixed bug #42069 (parse_ini_file() allows using some non-alpha numeric
characters)
Diffstat (limited to 'Zend')
-rw-r--r-- | Zend/zend_globals.h | 3 | ||||
-rw-r--r-- | Zend/zend_ini.h | 14 | ||||
-rw-r--r-- | Zend/zend_ini_parser.y | 209 | ||||
-rw-r--r-- | Zend/zend_ini_scanner.h | 9 | ||||
-rw-r--r-- | Zend/zend_ini_scanner.l | 480 |
5 files changed, 520 insertions, 195 deletions
diff --git a/Zend/zend_globals.h b/Zend/zend_globals.h index d2a7909d53..9351239499 100644 --- a/Zend/zend_globals.h +++ b/Zend/zend_globals.h @@ -265,6 +265,9 @@ struct _zend_scanner_globals { int yy_start_stack_depth; int *yy_start_stack; + /* For ini scanner. Modes are: ZEND_INI_SCANNER_NORMAL, ZEND_INI_SCANNER_RAW */ + int scanner_mode; + #ifdef ZEND_MULTIBYTE /* original (unfiltered) script */ char *script_org; diff --git a/Zend/zend_ini.h b/Zend/zend_ini.h index 2b7ac53e44..03c62a410e 100644 --- a/Zend/zend_ini.h +++ b/Zend/zend_ini.h @@ -164,7 +164,6 @@ END_EXTERN_C() #define INI_ORIG_STR(name) zend_ini_string((name), sizeof(name), 1) #define INI_ORIG_BOOL(name) ((zend_bool) INI_ORIG_INT(name)) - #define REGISTER_INI_ENTRIES() zend_register_ini_entries(ini_entries, module_number TSRMLS_CC) #define UNREGISTER_INI_ENTRIES() zend_unregister_ini_entries(module_number TSRMLS_CC) #define DISPLAY_INI_ENTRIES() display_ini_entries(zend_module) @@ -193,15 +192,16 @@ END_EXTERN_C() #define ZEND_INI_STAGE_HTACCESS (1<<5) /* INI parsing engine */ -typedef void (*zend_ini_parser_cb_t)(zval *arg1, zval *arg2, int callback_type, void *arg); +typedef void (*zend_ini_parser_cb_t)(zval *arg1, zval *arg2, zval *arg3, int callback_type, void *arg TSRMLS_DC); BEGIN_EXTERN_C() -ZEND_API int zend_parse_ini_file(zend_file_handle *fh, zend_bool unbuffered_errors, zend_ini_parser_cb_t ini_parser_cb, void *arg); -ZEND_API int zend_parse_ini_string(char *str, zend_bool unbuffered_errors, zend_ini_parser_cb_t ini_parser_cb, void *arg); +ZEND_API int zend_parse_ini_file(zend_file_handle *fh, zend_bool unbuffered_errors, int scanner_mode, zend_ini_parser_cb_t ini_parser_cb, void *arg TSRMLS_DC); +ZEND_API int zend_parse_ini_string(char *str, zend_bool unbuffered_errors, int scanner_mode, zend_ini_parser_cb_t ini_parser_cb, void *arg TSRMLS_DC); END_EXTERN_C() -#define ZEND_INI_PARSER_ENTRY 1 -#define ZEND_INI_PARSER_SECTION 2 -#define ZEND_INI_PARSER_POP_ENTRY 3 +/* INI entries */ +#define ZEND_INI_PARSER_ENTRY 1 /* Normal entry: foo = bar */ +#define ZEND_INI_PARSER_SECTION 2 /* Section: [foobar] */ +#define ZEND_INI_PARSER_POP_ENTRY 3 /* Offset entry: foo[] = bar */ typedef struct _zend_ini_parser_param { zend_ini_parser_cb_t ini_parser_cb; diff --git a/Zend/zend_ini_parser.y b/Zend/zend_ini_parser.y index d392c41e5a..cadb5cfd69 100644 --- a/Zend/zend_ini_parser.y +++ b/Zend/zend_ini_parser.y @@ -13,13 +13,15 @@ | obtain it through the world-wide-web, please send a note to | | license@zend.com so we can mail you a copy immediately. | +----------------------------------------------------------------------+ - | Author: Zeev Suraski <zeev@zend.com> | + | Authors: Zeev Suraski <zeev@zend.com> | + | Jani Taskinen <jani@php.net> | +----------------------------------------------------------------------+ */ /* $Id$ */ #define DEBUG_CFG_PARSER 0 + #include "zend.h" #include "zend_API.h" #include "zend_ini.h" @@ -27,32 +29,23 @@ #include "zend_ini_scanner.h" #include "zend_extensions.h" +#define YYERROR_VERBOSE #define YYSTYPE zval #ifdef ZTS #define YYPARSE_PARAM tsrm_ls #define YYLEX_PARAM tsrm_ls -#endif - -#define ZEND_INI_PARSER_CB (CG(ini_parser_param))->ini_parser_cb -#define ZEND_INI_PARSER_ARG (CG(ini_parser_param))->arg - -int ini_lex(zval *ini_lval TSRMLS_DC); -#ifdef ZTS int ini_parse(void *arg); #else int ini_parse(void); #endif -zval yylval; - -#ifndef ZTS -extern int ini_lex(zval *ini_lval TSRMLS_DC); -extern FILE *ini_in; -extern void init_cfg_scanner(void); -#endif +#define ZEND_INI_PARSER_CB (CG(ini_parser_param))->ini_parser_cb +#define ZEND_INI_PARSER_ARG (CG(ini_parser_param))->arg -void zend_ini_do_op(char type, zval *result, zval *op1, zval *op2) +/* {{{ zend_ini_do_op() +*/ +static void zend_ini_do_op(char type, zval *result, zval *op1, zval *op2) { int i_result; int i_op1, i_op2; @@ -91,16 +84,22 @@ void zend_ini_do_op(char type, zval *result, zval *op1, zval *op2) Z_STRVAL_P(result)[Z_STRLEN_P(result)] = 0; Z_TYPE_P(result) = IS_STRING; } +/* }}} */ -void zend_ini_init_string(zval *result) +/* {{{ zend_ini_init_string() +*/ +static void zend_ini_init_string(zval *result) { Z_STRVAL_P(result) = malloc(1); Z_STRVAL_P(result)[0] = 0; Z_STRLEN_P(result) = 0; Z_TYPE_P(result) = IS_STRING; } +/* }}} */ -void zend_ini_add_string(zval *result, zval *op1, zval *op2) +/* {{{ zend_ini_add_string() +*/ +static void zend_ini_add_string(zval *result, zval *op1, zval *op2) { int length = Z_STRLEN_P(op1) + Z_STRLEN_P(op2); @@ -110,12 +109,15 @@ void zend_ini_add_string(zval *result, zval *op1, zval *op2) Z_STRLEN_P(result) = length; Z_TYPE_P(result) = IS_STRING; } +/* }}} */ -void zend_ini_get_constant(zval *result, zval *name) +/* {{{ zend_ini_get_constant() +*/ +static void zend_ini_get_constant(zval *result, zval *name TSRMLS_DC) { zval z_constant; - TSRMLS_FETCH(); + /* If name contains ':' it is not a constant. Bug #26893. */ if (!memchr(Z_STRVAL_P(name), ':', Z_STRLEN_P(name)) && zend_get_constant(Z_STRVAL_P(name), Z_STRLEN_P(name), &z_constant TSRMLS_CC)) { /* z_constant is emalloc()'d */ @@ -129,16 +131,20 @@ void zend_ini_get_constant(zval *result, zval *name) *result = *name; } } +/* }}} */ -void zend_ini_get_var(zval *result, zval *name) +/* {{{ zend_ini_get_var() +*/ +static void zend_ini_get_var(zval *result, zval *name TSRMLS_DC) { zval curval; char *envvar; - TSRMLS_FETCH(); + /* Fetch configuration option value */ if (zend_get_configuration_directive(Z_STRVAL_P(name), Z_STRLEN_P(name)+1, &curval) == SUCCESS) { Z_STRVAL_P(result) = zend_strndup(Z_STRVAL(curval), Z_STRLEN(curval)); Z_STRLEN_P(result) = Z_STRLEN(curval); + /* ..or if not found, try ENV */ } else if ((envvar = zend_getenv(Z_STRVAL_P(name), Z_STRLEN_P(name) TSRMLS_CC)) != NULL || (envvar = getenv(Z_STRVAL_P(name))) != NULL) { Z_STRVAL_P(result) = strdup(envvar); @@ -147,9 +153,11 @@ void zend_ini_get_var(zval *result, zval *name) zend_ini_init_string(result); } } +/* }}} */ - -static void ini_error(char *str) +/* {{{ ini_error() +*/ +static void ini_error(char *msg) { char *error_buf; int error_buf_len; @@ -158,10 +166,10 @@ static void ini_error(char *str) currently_parsed_filename = zend_ini_scanner_get_filename(TSRMLS_C); if (currently_parsed_filename) { - error_buf_len = 128+strlen(currently_parsed_filename); /* should be more than enough */ + error_buf_len = 128 + strlen(msg) + strlen(currently_parsed_filename); /* should be more than enough */ error_buf = (char *) emalloc(error_buf_len); - sprintf(error_buf, "Error parsing %s on line %d\n", currently_parsed_filename, zend_ini_scanner_get_lineno(TSRMLS_C)); + sprintf(error_buf, "%s in %s on line %d\n", msg, currently_parsed_filename, zend_ini_scanner_get_lineno(TSRMLS_C)); } else { error_buf = estrdup("Invalid configuration directive\n"); } @@ -177,71 +185,87 @@ static void ini_error(char *str) } efree(error_buf); } +/* }}} */ - -ZEND_API int zend_parse_ini_file(zend_file_handle *fh, zend_bool unbuffered_errors, zend_ini_parser_cb_t ini_parser_cb, void *arg) +/* {{{ zend_parse_ini_file() +*/ +ZEND_API int zend_parse_ini_file(zend_file_handle *fh, zend_bool unbuffered_errors, int scanner_mode, zend_ini_parser_cb_t ini_parser_cb, void *arg TSRMLS_DC) { int retval; zend_ini_parser_param ini_parser_param; - TSRMLS_FETCH(); ini_parser_param.ini_parser_cb = ini_parser_cb; ini_parser_param.arg = arg; - CG(ini_parser_param) = &ini_parser_param; - if (zend_ini_open_file_for_scanning(fh TSRMLS_CC)==FAILURE) { + + if (zend_ini_open_file_for_scanning(fh, scanner_mode TSRMLS_CC) == FAILURE) { return FAILURE; } CG(ini_parser_unbuffered_errors) = unbuffered_errors; retval = ini_parse(TSRMLS_C); - zend_ini_close_file(fh TSRMLS_CC); - if (retval==0) { + shutdown_ini_scanner(TSRMLS_C); + + if (retval == 0) { return SUCCESS; } else { return FAILURE; } } +/* }}} */ - -ZEND_API int zend_parse_ini_string(char *str, zend_bool unbuffered_errors, zend_ini_parser_cb_t ini_parser_cb, void *arg) +/* {{{ zend_parse_ini_string() +*/ +ZEND_API int zend_parse_ini_string(char *str, zend_bool unbuffered_errors, int scanner_mode, zend_ini_parser_cb_t ini_parser_cb, void *arg TSRMLS_DC) { + int retval; zend_ini_parser_param ini_parser_param; - TSRMLS_FETCH(); ini_parser_param.ini_parser_cb = ini_parser_cb; ini_parser_param.arg = arg; - CG(ini_parser_param) = &ini_parser_param; - if (zend_ini_prepare_string_for_scanning(str TSRMLS_CC)==FAILURE) { + + if (zend_ini_prepare_string_for_scanning(str, scanner_mode TSRMLS_CC) == FAILURE) { return FAILURE; } CG(ini_parser_unbuffered_errors) = unbuffered_errors; + retval = ini_parse(TSRMLS_C); + + shutdown_ini_scanner(TSRMLS_C); - if (ini_parse(TSRMLS_C)) { + if (retval == 0) { return SUCCESS; } else { return FAILURE; } } - +/* }}} */ %} +%expect 1 %pure_parser + +%token TC_SECTION +%token TC_RAW +%token TC_NUMBER %token TC_STRING -%token TC_ENCAPSULATED_STRING -%token BRACK -%token SECTION -%token CFG_TRUE -%token CFG_FALSE +%token TC_OFFSET %token TC_DOLLAR_CURLY +%token TC_VARNAME +%token TC_QUOTED_STRING +%token BOOL_TRUE +%token BOOL_FALSE +%token END_OF_LINE +%token '=' ':' ',' '.' '"' '\'' '^' '+' '-' '/' '*' '%' '$' '~' '<' '>' '?' '@' %left '|' '&' %right '~' '!' +%destructor { free(Z_STRVAL($$)); } TC_RAW TC_NUMBER TC_STRING TC_OFFSET TC_VARNAME TC_QUOTED_STRING BOOL_TRUE BOOL_FALSE + %% statement_list: @@ -250,61 +274,94 @@ statement_list: ; statement: - TC_STRING '=' string_or_value { + TC_SECTION section_string_or_value ']' { #if DEBUG_CFG_PARSER - printf("'%s' = '%s'\n", Z_STRVAL($1), Z_STRVAL($3)); + printf("SECTION: [%s]\n", Z_STRVAL($2)); #endif - ZEND_INI_PARSER_CB(&$1, &$3, ZEND_INI_PARSER_ENTRY, ZEND_INI_PARSER_ARG); + ZEND_INI_PARSER_CB(&$2, NULL, NULL, ZEND_INI_PARSER_SECTION, ZEND_INI_PARSER_ARG TSRMLS_CC); + free(Z_STRVAL($2)); + } + | TC_STRING '=' string_or_value { +#if DEBUG_CFG_PARSER + printf("NORMAL: '%s' = '%s'\n", Z_STRVAL($1), Z_STRVAL($3)); +#endif + ZEND_INI_PARSER_CB(&$1, &$3, NULL, ZEND_INI_PARSER_ENTRY, ZEND_INI_PARSER_ARG TSRMLS_CC); free(Z_STRVAL($1)); free(Z_STRVAL($3)); } - | TC_STRING BRACK '=' string_or_value { + | TC_OFFSET option_offset ']' '=' string_or_value { #if DEBUG_CFG_PARSER - printf("'%s'[ ] = '%s'\n", Z_STRVAL($1), Z_STRVAL($4)); + printf("OFFSET: '%s'[%s] = '%s'\n", Z_STRVAL($1), Z_STRVAL($2), Z_STRVAL($5)); #endif - ZEND_INI_PARSER_CB(&$1, &$4, ZEND_INI_PARSER_POP_ENTRY, ZEND_INI_PARSER_ARG); + ZEND_INI_PARSER_CB(&$1, &$5, &$2, ZEND_INI_PARSER_POP_ENTRY, ZEND_INI_PARSER_ARG TSRMLS_CC); free(Z_STRVAL($1)); - free(Z_STRVAL($4)); + free(Z_STRVAL($2)); + free(Z_STRVAL($5)); } - | TC_STRING { ZEND_INI_PARSER_CB(&$1, NULL, ZEND_INI_PARSER_ENTRY, ZEND_INI_PARSER_ARG); free(Z_STRVAL($1)); } - | SECTION { ZEND_INI_PARSER_CB(&$1, NULL, ZEND_INI_PARSER_SECTION, ZEND_INI_PARSER_ARG); free(Z_STRVAL($1)); } - | '\n' + | TC_STRING { ZEND_INI_PARSER_CB(&$1, NULL, NULL, ZEND_INI_PARSER_ENTRY, ZEND_INI_PARSER_ARG TSRMLS_CC); free(Z_STRVAL($1)); } + | END_OF_LINE ; +section_string_or_value: + TC_RAW { $$ = $1; } + | section_var_list { $$ = $1; } + | '"' encapsed_list '"' { $$ = $2; } + | /* empty */ { zend_ini_init_string(&$$); } +; string_or_value: - expr { $$ = $1; } - | CFG_TRUE { $$ = $1; } - | CFG_FALSE { $$ = $1; } - | '\n' { zend_ini_init_string(&$$); } - | /* empty */ { zend_ini_init_string(&$$); } + expr { $$ = $1; } + | TC_RAW { $$ = $1; } + | TC_NUMBER { $$ = $1; } + | BOOL_TRUE { $$ = $1; } + | BOOL_FALSE { $$ = $1; } + | '"' encapsed_list '"' { $$ = $2; } + | END_OF_LINE { zend_ini_init_string(&$$); } ; +option_offset: + TC_NUMBER { $$ = $1; } + | TC_RAW { $$ = $1; } + | var_string_list { $$ = $1; } + | '"' encapsed_list '"' { $$ = $2; } + | /* empty */ { zend_ini_init_string(&$$); } +; -var_string_list: - cfg_var_ref { $$ = $1; } - | TC_ENCAPSULATED_STRING { $$ = $1; } - | constant_string { $$ = $1; } - | var_string_list cfg_var_ref { zend_ini_add_string(&$$, &$1, &$2); free($2.value.str.val); } - | var_string_list TC_ENCAPSULATED_STRING { zend_ini_add_string(&$$, &$1, &$2); free(Z_STRVAL($2)); } - | var_string_list constant_string { zend_ini_add_string(&$$, &$1, &$2); free($2.value.str.val); } +encapsed_list: + encapsed_list cfg_var_ref { zend_ini_add_string(&$$, &$1, &$2); free(Z_STRVAL($2)); } + | encapsed_list TC_QUOTED_STRING { zend_ini_add_string(&$$, &$1, &$2); free(Z_STRVAL($2)); } + | /* empty */ { zend_ini_init_string(&$$); } ; -cfg_var_ref: - TC_DOLLAR_CURLY TC_STRING '}' { zend_ini_get_var(&$$, &$2); free($2.value.str.val); } +section_var_list: + cfg_var_ref { $$ = $1; } + | TC_STRING { $$ = $1; } + | section_var_list cfg_var_ref { zend_ini_add_string(&$$, &$1, &$2); free(Z_STRVAL($2)); } + | section_var_list TC_STRING { zend_ini_add_string(&$$, &$1, &$2); free(Z_STRVAL($2)); } +; + +var_string_list: + cfg_var_ref { $$ = $1; } + | constant_string { $$ = $1; } + | var_string_list cfg_var_ref { zend_ini_add_string(&$$, &$1, &$2); free(Z_STRVAL($2)); } + | var_string_list constant_string { zend_ini_add_string(&$$, &$1, &$2); free(Z_STRVAL($2)); } ; expr: - var_string_list { $$ = $1; } - | expr '|' expr { zend_ini_do_op('|', &$$, &$1, &$3); } - | expr '&' expr { zend_ini_do_op('&', &$$, &$1, &$3); } - | '~' expr { zend_ini_do_op('~', &$$, &$2, NULL); } - | '!' expr { zend_ini_do_op('!', &$$, &$2, NULL); } - | '(' expr ')' { $$ = $2; } + var_string_list { $$ = $1; } + | expr '|' expr { zend_ini_do_op('|', &$$, &$1, &$3); } + | expr '&' expr { zend_ini_do_op('&', &$$, &$1, &$3); } + | '~' expr { zend_ini_do_op('~', &$$, &$2, NULL); } + | '!' expr { zend_ini_do_op('!', &$$, &$2, NULL); } + | '(' expr ')' { $$ = $2; } +; + +cfg_var_ref: + TC_DOLLAR_CURLY TC_VARNAME '}' { zend_ini_get_var(&$$, &$2 TSRMLS_CC); free(Z_STRVAL($2)); } ; constant_string: - TC_STRING { zend_ini_get_constant(&$$, &$1); } + TC_STRING { zend_ini_get_constant(&$$, &$1 TSRMLS_CC); } ; /* diff --git a/Zend/zend_ini_scanner.h b/Zend/zend_ini_scanner.h index 76ca771058..3ba6e120f5 100644 --- a/Zend/zend_ini_scanner.h +++ b/Zend/zend_ini_scanner.h @@ -22,13 +22,18 @@ #ifndef _ZEND_INI_SCANNER_H #define _ZEND_INI_SCANNER_H +/* Scanner modes */ +#define ZEND_INI_SCANNER_NORMAL 0 /* Normal mode. [DEFAULT] */ +#define ZEND_INI_SCANNER_RAW 1 /* Raw mode. Option values are not parsed */ + BEGIN_EXTERN_C() 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); -int zend_ini_prepare_string_for_scanning(char *str TSRMLS_DC); +int zend_ini_open_file_for_scanning(zend_file_handle *fh, int scanner_mode TSRMLS_DC); +int zend_ini_prepare_string_for_scanning(char *str, int scanner_mode TSRMLS_DC); void zend_ini_close_file(zend_file_handle *fh TSRMLS_DC); int ini_lex(zval *ini_lval TSRMLS_DC); +void shutdown_ini_scanner(TSRMLS_D); END_EXTERN_C() #endif /* _ZEND_INI_SCANNER_H */ diff --git a/Zend/zend_ini_scanner.l b/Zend/zend_ini_scanner.l index 3f91bb64a7..17d0f01c1c 100644 --- a/Zend/zend_ini_scanner.l +++ b/Zend/zend_ini_scanner.l @@ -13,21 +13,71 @@ | obtain it through the world-wide-web, please send a note to | | license@zend.com so we can mail you a copy immediately. | +----------------------------------------------------------------------+ - | Author: Zeev Suraski <zeev@zend.com> | + | Authors: Zeev Suraski <zeev@zend.com> | + | Jani Taskinen <jani@php.net> | +----------------------------------------------------------------------+ */ /* $Id$ */ +#define DEBUG_CFG_SCANNER 0 + #define yyleng SCNG(yy_leng) #define yytext SCNG(yy_text) #define yytext_ptr SCNG(yy_text) #define yyin SCNG(yy_in) #define yyout SCNG(yy_out) + +/* How it works (for the core ini directives): + * =========================================== + * + * 1. Scanner scans file for tokens and passes them to parser. + * 2. Parser parses the tokens and passes the name/value pairs to the callback + * function which stores them in the configuration hash table. + * 3. Later REGISTER_INI_ENTRIES() is called which triggers the actual + * registering of ini entries and uses zend_get_configuration_directive() + * to fetch the previously stored name/value pair from configuration hash table + * and registers the static ini entries which match the name to the value + * into EG(ini_directives) hash table. + * 4. PATH section entries are used per-request from down to top, each overriding + * previous if one exists. zend_alter_ini_entry() is called for each entry. + * Settings in PATH section are ZEND_INI_SYSTEM accessible and thus mimics the + * php_admin_* directives used within Apache httpd.conf when PHP is compiled as + * module for Apache. + * 5. User defined ini files (like .htaccess for apache) are parsed for each request and + * stored in separate hash defined by SAPI. + */ + +/* TODO: (ordered by importance :-) + * =============================================================================== + * + * - Separate constant lookup totally from plain strings (using CONSTANT pattern) + * - Add #if .. #else .. #endif and ==, !=, <, > , <=, >= operators + * - Add #include "some.ini" + * - Allow variables to refer to options also when using parse_ini_file() + * + */ + +/* These are not needed when yymore() is not used */ +/* #define yy_last_accepting_state SCNG(_yy_last_accepting_state) #define yy_last_accepting_cpos SCNG(_yy_last_accepting_cpos) #define yy_more_flag SCNG(_yy_more_flag) #define yy_more_len SCNG(_yy_more_len) +*/ + +%} + +%x ST_DOUBLE_QUOTES +%x ST_OFFSET +%x ST_RAW +%x ST_SECTION_RAW +%x ST_SECTION_VALUE +%x ST_VALUE +%x ST_VARNAME +%option stack + +%{ #include <errno.h> #include "zend.h" @@ -35,9 +85,6 @@ #include <zend_ini_parser.h> #include "zend_ini_scanner.h" -#undef YYSTYPE -#define YYSTYPE zval - #define YY_DECL int ini_lex(zval *ini_lval TSRMLS_DC) /* Globals Macros */ @@ -48,204 +95,417 @@ ZEND_API ts_rsrc_id ini_scanner_globals_id; ZEND_API zend_scanner_globals ini_scanner_globals; #endif -# define YY_INPUT(buf, result, max_size) \ - if ( ((result = zend_stream_read(yyin, buf, max_size TSRMLS_CC)) == 0) \ - && zend_stream_ferror( yyin TSRMLS_CC) ) \ - YY_FATAL_ERROR( "input in flex scanner failed" ); +/* Eat trailing whitespace + extra char */ +#define EAT_TRAILING_WHITESPACE_EX(ch) \ + while (yyleng > 0 && ( \ + (ch != 'X' && yytext[yyleng - 1] == ch) || \ + yytext[yyleng - 1] == '\n' || \ + yytext[yyleng - 1] == '\r' || \ + yytext[yyleng - 1] == '\t' || \ + yytext[yyleng - 1] == ' ') \ + ) { \ + yyleng--; \ + yytext[yyleng]=0; \ + } + +/* Eat trailing whitespace */ +#define EAT_TRAILING_WHITESPACE() EAT_TRAILING_WHITESPACE_EX('X') + +#define zend_ini_copy_value(retval, str, len) { \ + Z_STRVAL_P(retval) = zend_strndup(str, len); \ + Z_STRLEN_P(retval) = len; \ + Z_TYPE_P(retval) = IS_STRING; \ +} + +#define RETURN_TOKEN(type, str, len) { \ + zend_ini_copy_value(ini_lval, str, len); \ + return type; \ +} static char *ini_filename; -void init_ini_scanner(TSRMLS_D) +/* {{{ init_ini_scanner() +*/ +static void init_ini_scanner(TSRMLS_D) +{ + SCNG(lineno) = 1; + SCNG(scanner_mode) = ZEND_INI_SCANNER_NORMAL; + SCNG(yy_start_stack_ptr) = 0; + SCNG(yy_start_stack_depth) = 0; + SCNG(current_buffer) = NULL; +} +/* }}} */ + +/* {{{ shutdown_ini_scanner() +*/ +void shutdown_ini_scanner(TSRMLS_D) { - SCNG(lineno)=1; + if (SCNG(yy_start_stack)) { + yy_flex_free(SCNG(yy_start_stack)); + SCNG(yy_start_stack) = NULL; + } + yy_delete_buffer(SCNG(current_buffer) TSRMLS_CC); + if (ini_filename) { + free(ini_filename); + } } +/* }}} */ +/* {{{ zend_ini_scanner_get_lineno() +*/ int zend_ini_scanner_get_lineno(TSRMLS_D) { return SCNG(lineno); } +/* }}} */ +/* {{{ zend_ini_scanner_get_filename() +*/ char *zend_ini_scanner_get_filename(TSRMLS_D) { return ini_filename; } +/* }}} */ -int zend_ini_open_file_for_scanning(zend_file_handle *fh TSRMLS_DC) +/* {{{ zend_ini_open_file_for_scanning() +*/ +int zend_ini_open_file_for_scanning(zend_file_handle *fh, int scanner_mode TSRMLS_DC) { if (FAILURE == zend_stream_fixup(fh TSRMLS_CC)) { return FAILURE; } init_ini_scanner(TSRMLS_C); + SCNG(scanner_mode) = scanner_mode; yyin = fh; yy_switch_to_buffer(yy_create_buffer(yyin, YY_BUF_SIZE TSRMLS_CC) TSRMLS_CC); - ini_filename = fh->filename; + ini_filename = zend_strndup(fh->filename, strlen(fh->filename)); return SUCCESS; } +/* }}} */ -int zend_ini_prepare_string_for_scanning(char *str TSRMLS_DC) +/* {{{ zend_ini_prepare_string_for_scanning() +*/ +int zend_ini_prepare_string_for_scanning(char *str, int scanner_mode TSRMLS_DC) { int len = strlen(str); + init_ini_scanner(TSRMLS_C); + SCNG(scanner_mode) = scanner_mode; yyin = NULL; yy_scan_buffer(str, len + 2 TSRMLS_CC); ini_filename = NULL; return SUCCESS; } +/* }}} */ +/* {{{ zend_ini_close_file() +*/ void zend_ini_close_file(zend_file_handle *fh TSRMLS_DC) { zend_stream_close(fh); } +/* }}} */ + +/* {{{ zend_ini_escape_string() + */ +static void zend_ini_escape_string(zval *lval, char *str, int len, char quote_type TSRMLS_DC) +{ + register char *s, *t; + char *end; + + zend_ini_copy_value(lval, str, len); + + /* convert escape sequences */ + s = t = Z_STRVAL_P(lval); + end = s + Z_STRLEN_P(lval); + + while (s < end) { + if (*s == '\\') { + s++; + if (s >= end) { + continue; + } + switch (*s) { + case 'n': + *t++ = '\n'; + Z_STRLEN_P(lval)--; + break; + case 'r': + *t++ = '\r'; + Z_STRLEN_P(lval)--; + break; + case 't': + *t++ = '\t'; + Z_STRLEN_P(lval)--; + break; + case '"': + if (*s != quote_type) { + *t++ = '\\'; + *t++ = *s; + break; + } + case '\\': + case '$': + *t++ = *s; + Z_STRLEN_P(lval)--; + break; + default: + *t++ = '\\'; + *t++ = *s; + break; + } + } else { + *t++ = *s; + } + + if (*s == '\n' || (*s == '\r' && (*(s+1) != '\n'))) { + SCNG(lineno)++; + } + s++; + } + *t = 0; +} +/* }}} */ %} -NEWLINE ("\r"|"\n"|"\r\n") +LNUM [0-9]+ +DNUM ([0-9]*[\.][0-9]+)|([0-9]+[\.][0-9]*) +NUMBER {LNUM}|{DNUM} +ANY_CHAR (.|[\n]) +NEWLINE ("\r"|"\n"|"\r\n") +TABS_AND_SPACES [ \t] +CONSTANT [a-zA-Z][a-zA-Z0-9_]* +LABEL [a-zA-Z0-9][a-zA-Z0-9._]* +TOKENS [:,.\[\]"'()|^&+-/*=%$!~<>?@] +OPERATORS [&|~()!] + +LITERAL_DOLLAR ("$"+([^a-zA-Z0-9$"'\\{]|("\\"{ANY_CHAR}))) +VALUE_CHARS ("{"*([^=\n\r;&|~()!$"'\\{]|("\\"{ANY_CHAR}))|{LITERAL_DOLLAR}) +RAW_VALUE_CHARS [^=\n\r;] +SINGLE_QUOTED_CHARS [^'] + +SECTION_VALUE_CHARS ("{"*([^\n\r;$"'\\{\]]|("\\"{ANY_CHAR}))|{LITERAL_DOLLAR}) +SECTION_RAW_CHARS [^\]\n\r] +/* Allow using ${foobar} inside quoted strings */ +DOUBLE_QUOTES_CHARS ("{"*([^$"\\{]|("\\"{ANY_CHAR}))|{LITERAL_DOLLAR}) + +/* " */ + +%option nounput %option noyywrap +%option noyylineno +%option noyy_top_state %option never-interactive %% -<INITIAL>[ ]*[\[][ ]*[\]][ ]* { - return BRACK; +<INITIAL>"[" { /* Section start */ + /* Enter section data lookup state */ + if (SCNG(scanner_mode) == ZEND_INI_SCANNER_RAW) { + yy_push_state(ST_SECTION_RAW TSRMLS_CC); + } else { + yy_push_state(ST_SECTION_VALUE TSRMLS_CC); + } + return TC_SECTION; +} + +<ST_VALUE,ST_SECTION_VALUE,ST_OFFSET>"'"{SINGLE_QUOTED_CHARS}+"'" { /* Raw string */ + /* Eat leading and trailing single quotes */ + if (yytext[0] == '\'' && yytext[yyleng - 1] == '\'') { + yytext++; + yyleng = yyleng - 2; + yytext[yyleng] = 0; + } + RETURN_TOKEN(TC_RAW, yytext, yyleng); } -<INITIAL>[ ]*("true"|"on"|"yes")[ ]* { - Z_STRVAL_P(ini_lval) = zend_strndup("1", 1); - Z_STRLEN_P(ini_lval) = 1; - Z_TYPE_P(ini_lval) = IS_STRING; - return CFG_TRUE; +<ST_SECTION_RAW,ST_SECTION_VALUE>"]"{TABS_AND_SPACES}*{NEWLINE}? { /* End of section */ + BEGIN(INITIAL); + SCNG(lineno)++; + return ']'; } +<INITIAL>{LABEL}"["{TABS_AND_SPACES}* { /* Start of option with offset */ + /* Eat trailing whitespace and [ */ + EAT_TRAILING_WHITESPACE_EX('['); -<INITIAL>[ ]*("false"|"off"|"no"|"none"|"null")[ ]* { - Z_STRVAL_P(ini_lval) = zend_strndup("", 0); - Z_STRLEN_P(ini_lval) = 0; - Z_TYPE_P(ini_lval) = IS_STRING; - return CFG_FALSE; + /* Enter offset lookup state */ + yy_push_state(ST_OFFSET TSRMLS_CC); + + RETURN_TOKEN(TC_OFFSET, yytext, yyleng); } -<INITIAL>[[][^\]\n]+[\]][ ]*{NEWLINE}? { - /* SECTION */ +<ST_OFFSET>{TABS_AND_SPACES}*"]" { /* End of section or an option offset */ + BEGIN(INITIAL); + return ']'; +} - /* eat trailing ] and spaces */ - while (yyleng>0 && (yytext[yyleng-1]=='\n' || yytext[yyleng-1]=='\r' || yytext[yyleng-1]==']' || yytext[yyleng-1]==' ')) { - yyleng--; - yytext[yyleng]=0; - } +<ST_DOUBLE_QUOTES,ST_SECTION_VALUE,ST_VALUE,ST_OFFSET>"${" { /* Variable start */ + yy_push_state(ST_VARNAME TSRMLS_CC); + return TC_DOLLAR_CURLY; +} - SCNG(lineno)++; +<ST_VARNAME>{LABEL} { /* Variable name */ + RETURN_TOKEN(TC_VARNAME, yytext, yyleng); +} - /* eat leading [ */ - yytext++; - yyleng--; +<ST_VARNAME>"}" { /* Variable end */ + yy_pop_state(TSRMLS_C); + return '}'; +} + +<INITIAL,ST_VALUE>("true"|"on"|"yes"){TABS_AND_SPACES}* { /* TRUE value (when used outside option value/offset this causes parse error!) */ + RETURN_TOKEN(BOOL_TRUE, "1", 1); +} - Z_STRVAL_P(ini_lval) = zend_strndup(yytext, yyleng); - Z_STRLEN_P(ini_lval) = yyleng; - Z_TYPE_P(ini_lval) = IS_STRING; - return SECTION; +<INITIAL,ST_VALUE>("false"|"off"|"no"|"none"|"null"){TABS_AND_SPACES}* { /* FALSE value (when used outside option value/offset this causes parse error!)*/ + RETURN_TOKEN(BOOL_FALSE, "", 0); } -<INITIAL>["][^"]*["] { - char *p = yytext; +<INITIAL,ST_OFFSET>{LABEL} { /* Get option name or option offset value */ + RETURN_TOKEN(TC_STRING, yytext, yyleng); +} - /* ENCAPSULATED TC_STRING */ +<INITIAL>{TABS_AND_SPACES}*[=]{TABS_AND_SPACES}* { /* Start option value */ + if (SCNG(scanner_mode) == ZEND_INI_SCANNER_RAW) { + yy_push_state(ST_RAW TSRMLS_CC); + } else { + yy_push_state(ST_VALUE TSRMLS_CC); + } + return '='; +} - while ((p = strpbrk(p, "\r\n"))) { - if (*p == '\r' && *(p + 1) == '\n') { - p++; - } - SCNG(lineno)++; - p++; +<ST_RAW>{RAW_VALUE_CHARS}+ { /* Raw value, only used when SCNG(scanner_mode) == ZEND_INI_SCANNER_RAW. */ + /* Eat leading and trailing double quotes */ + if (yytext[0] == '"' && yytext[yyleng - 1] == '"') { + yytext++; + yyleng = yyleng - 2; + yytext[yyleng] = 0; } + RETURN_TOKEN(TC_RAW, yytext, yyleng); +} - /* eat trailing " */ - yytext[yyleng-1]=0; +<ST_SECTION_RAW>{SECTION_RAW_CHARS}+ { /* Raw value, only used when SCNG(scanner_mode) == ZEND_INI_SCANNER_RAW. */ + RETURN_TOKEN(TC_RAW, yytext, yyleng); +} - /* eat leading " */ - yytext++; +<ST_VALUE,ST_RAW>{NEWLINE} { /* End of option value */ + BEGIN(INITIAL); + SCNG(lineno)++; + return END_OF_LINE; +} - Z_STRVAL_P(ini_lval) = zend_strndup(yytext, yyleng - 2); - Z_STRLEN_P(ini_lval) = yyleng - 2; - Z_TYPE_P(ini_lval) = IS_STRING; - return TC_ENCAPSULATED_STRING; +<ST_VALUE,ST_OFFSET>{CONSTANT} { /* Get constant option value */ + RETURN_TOKEN(TC_STRING, yytext, yyleng); } -<INITIAL>"${" { - return TC_DOLLAR_CURLY; +<ST_VALUE,ST_OFFSET>{NUMBER} { /* Get number option value as string */ + RETURN_TOKEN(TC_NUMBER, yytext, yyleng); } -<INITIAL>"}" { - Z_LVAL_P(ini_lval) = (long) yytext[0]; +<INITIAL>{TOKENS} { /* Disallow these chars outside option values */ return yytext[0]; } -<INITIAL>[&|~$(){}!] { +<ST_VALUE>{OPERATORS}{TABS_AND_SPACES}* { /* Boolean operators */ return yytext[0]; } -<INITIAL>[^=\n\r\t;|&$~(){}!"\[]+ { - /* STRING */ - register int i; +<ST_VALUE>[=] { /* Make = used in option value to trigger error */ + yyless(yyleng - 1); + BEGIN(INITIAL); + return END_OF_LINE; +} - /* eat trailing whitespace */ - for (i=yyleng-1; i>=0; i--) { - if (yytext[i]==' ' || yytext[i]=='\t') { - yytext[i]=0; - yyleng--; - } else { - break; - } - } - /* eat leading whitespace */ - while (yytext[0]) { - if (yytext[0]==' ' || yytext[0]=='\t') { - yytext++; - yyleng--; - } else { - break; - } - } - if (yyleng!=0) { - Z_STRVAL_P(ini_lval) = zend_strndup(yytext, yyleng); - Z_STRLEN_P(ini_lval) = yyleng; - Z_TYPE_P(ini_lval) = IS_STRING; - return TC_STRING; - } else { - /* whitespace */ - } +<ST_VALUE>{VALUE_CHARS}+ { /* Get everything else as option/offset value */ + /* Eat trailing tabs and spaces */ + EAT_TRAILING_WHITESPACE(); + RETURN_TOKEN(TC_STRING, yytext, yyleng); } -<INITIAL>[=\n] { - if (yytext[0] == '\n') { - SCNG(lineno)++; - } - return yytext[0]; +<ST_SECTION_VALUE,ST_OFFSET>{SECTION_VALUE_CHARS}+ { /* Get rest as section/offset value */ + RETURN_TOKEN(TC_STRING, yytext, yyleng); } -<INITIAL>{NEWLINE} { - SCNG(lineno)++; - return '\n'; +<ST_SECTION_VALUE,ST_VALUE,ST_OFFSET>["] { /* Double quoted '"' string start */ + yy_push_state(ST_DOUBLE_QUOTES TSRMLS_CC); + return '"'; } -<INITIAL>[;][^\r\n]*{NEWLINE}? { - /* comment */ - SCNG(lineno)++; - return '\n'; +<ST_DOUBLE_QUOTES>{DOUBLE_QUOTES_CHARS}+ { /* Escape double quoted string contents */ + zend_ini_escape_string(ini_lval, yytext, yyleng, '"' TSRMLS_CC); + return TC_QUOTED_STRING; +} + +<ST_DOUBLE_QUOTES>["] { /* Double quoted '"' string ends */ + yy_pop_state(TSRMLS_C); + return '"'; } -<INITIAL>[ \t] { +<INITIAL,ST_VALUE,ST_RAW,ST_OFFSET>{TABS_AND_SPACES} { /* eat whitespace */ } -<INITIAL>. { -#if DEBUG - php_error(E_NOTICE,"Unexpected character on line %d: '%s' (ASCII %d)\n", yylineno, yytext, yytext[0]); -#endif +<INITIAL>{NEWLINE} { + SCNG(lineno)++; + return END_OF_LINE; +} + +<INITIAL,ST_VALUE,ST_RAW>[;][^\r\n]*{NEWLINE} { /* Comment */ + BEGIN(INITIAL); + SCNG(lineno)++; + return END_OF_LINE; +} + +<ST_VALUE,ST_RAW><<EOF>> { /* End of option value (if EOF is reached before EOL */ + BEGIN(INITIAL); + return END_OF_LINE; } <<EOF>> { - yy_delete_buffer(YY_CURRENT_BUFFER TSRMLS_CC); +#if DEBUG_CFG_SCANNER + while (YYSTATE != INITIAL) { + switch (YYSTATE) { + case INITIAL: + break; + + case ST_DOUBLE_QUOTES: + fprintf(stderr, "ERROR: Unterminated ini option value double quotes\n"); + break; + + case ST_OFFSET: + fprintf(stderr, "ERROR: Unterminated ini option offset\n"); + break; + + case ST_RAW: + fprintf(stderr, "ERROR: Unterminated raw ini option value\n"); + break; + + case ST_SECTION_RAW: + fprintf(stderr, "ERROR: Unterminated raw ini section value\n"); + break; + + case ST_SECTION_VALUE: + fprintf(stderr, "ERROR: Unterminated ini section value\n"); + break; + + case ST_VALUE: + fprintf(stderr, "ERROR: Unterminated ini option value\n"); + break; + + case ST_VARNAME: + fprintf(stderr, "ERROR: Unterminated ini variable\n"); + break; + + default: + fprintf(stderr, "BUG: Unknown state (%d)\n", YYSTATE); + break; + } + yy_pop_state(TSRMLS_C); + } +#endif yyterminate(); } |