1 %{
2 /*
3 +----------------------------------------------------------------------+
4 | Zend Engine |
5 +----------------------------------------------------------------------+
6 | Copyright (c) 1998-2015 Zend Technologies Ltd. (http://www.zend.com) |
7 +----------------------------------------------------------------------+
8 | This source file is subject to version 2.00 of the Zend license, |
9 | that is bundled with this package in the file LICENSE, and is |
10 | available through the world-wide-web at the following url: |
11 | http://www.zend.com/license/2_00.txt. |
12 | If you did not receive a copy of the Zend license and are unable to |
13 | obtain it through the world-wide-web, please send a note to |
14 | license@zend.com so we can mail you a copy immediately. |
15 +----------------------------------------------------------------------+
16 | Authors: Zeev Suraski <zeev@zend.com> |
17 | Jani Taskinen <jani@php.net> |
18 +----------------------------------------------------------------------+
19 */
20
21 /* $Id$ */
22
23 #define DEBUG_CFG_PARSER 0
24
25 #include "zend.h"
26 #include "zend_API.h"
27 #include "zend_ini.h"
28 #include "zend_constants.h"
29 #include "zend_ini_scanner.h"
30 #include "zend_extensions.h"
31
32 #ifdef PHP_WIN32
33 #include "win32/syslog.h"
34 #endif
35
36 #define YYERROR_VERBOSE
37 #define YYSTYPE zval
38
39 #ifdef ZTS
40 #define YYPARSE_PARAM tsrm_ls
41 #define YYLEX_PARAM tsrm_ls
42 int ini_parse(void *arg);
43 #else
44 int ini_parse(void);
45 #endif
46
47 #define ZEND_INI_PARSER_CB (CG(ini_parser_param))->ini_parser_cb
48 #define ZEND_INI_PARSER_ARG (CG(ini_parser_param))->arg
49
50 /* {{{ zend_ini_do_op()
51 */
zend_ini_do_op(char type,zval * result,zval * op1,zval * op2)52 static void zend_ini_do_op(char type, zval *result, zval *op1, zval *op2)
53 {
54 int i_result;
55 int i_op1, i_op2;
56 char str_result[MAX_LENGTH_OF_LONG];
57
58 i_op1 = atoi(Z_STRVAL_P(op1));
59 free(Z_STRVAL_P(op1));
60 if (op2) {
61 i_op2 = atoi(Z_STRVAL_P(op2));
62 free(Z_STRVAL_P(op2));
63 } else {
64 i_op2 = 0;
65 }
66
67 switch (type) {
68 case '|':
69 i_result = i_op1 | i_op2;
70 break;
71 case '&':
72 i_result = i_op1 & i_op2;
73 break;
74 case '^':
75 i_result = i_op1 ^ i_op2;
76 break;
77 case '~':
78 i_result = ~i_op1;
79 break;
80 case '!':
81 i_result = !i_op1;
82 break;
83 default:
84 i_result = 0;
85 break;
86 }
87
88 Z_STRLEN_P(result) = zend_sprintf(str_result, "%d", i_result);
89 Z_STRVAL_P(result) = (char *) malloc(Z_STRLEN_P(result)+1);
90 memcpy(Z_STRVAL_P(result), str_result, Z_STRLEN_P(result));
91 Z_STRVAL_P(result)[Z_STRLEN_P(result)] = 0;
92 Z_TYPE_P(result) = IS_STRING;
93 }
94 /* }}} */
95
96 /* {{{ zend_ini_init_string()
97 */
zend_ini_init_string(zval * result)98 static void zend_ini_init_string(zval *result)
99 {
100 Z_STRVAL_P(result) = malloc(1);
101 Z_STRVAL_P(result)[0] = 0;
102 Z_STRLEN_P(result) = 0;
103 Z_TYPE_P(result) = IS_STRING;
104 }
105 /* }}} */
106
107 /* {{{ zend_ini_add_string()
108 */
zend_ini_add_string(zval * result,zval * op1,zval * op2)109 static void zend_ini_add_string(zval *result, zval *op1, zval *op2)
110 {
111 int length = Z_STRLEN_P(op1) + Z_STRLEN_P(op2);
112
113 Z_STRVAL_P(result) = (char *) realloc(Z_STRVAL_P(op1), length+1);
114 memcpy(Z_STRVAL_P(result)+Z_STRLEN_P(op1), Z_STRVAL_P(op2), Z_STRLEN_P(op2));
115 Z_STRVAL_P(result)[length] = 0;
116 Z_STRLEN_P(result) = length;
117 Z_TYPE_P(result) = IS_STRING;
118 }
119 /* }}} */
120
121 /* {{{ zend_ini_get_constant()
122 */
zend_ini_get_constant(zval * result,zval * name TSRMLS_DC)123 static void zend_ini_get_constant(zval *result, zval *name TSRMLS_DC)
124 {
125 zval z_constant;
126
127 /* If name contains ':' it is not a constant. Bug #26893. */
128 if (!memchr(Z_STRVAL_P(name), ':', Z_STRLEN_P(name))
129 && zend_get_constant(Z_STRVAL_P(name), Z_STRLEN_P(name), &z_constant TSRMLS_CC)) {
130 /* z_constant is emalloc()'d */
131 convert_to_string(&z_constant);
132 Z_STRVAL_P(result) = zend_strndup(Z_STRVAL(z_constant), Z_STRLEN(z_constant));
133 Z_STRLEN_P(result) = Z_STRLEN(z_constant);
134 Z_TYPE_P(result) = Z_TYPE(z_constant);
135 zval_dtor(&z_constant);
136 free(Z_STRVAL_P(name));
137 } else {
138 *result = *name;
139 }
140 }
141 /* }}} */
142
143 /* {{{ zend_ini_get_var()
144 */
zend_ini_get_var(zval * result,zval * name TSRMLS_DC)145 static void zend_ini_get_var(zval *result, zval *name TSRMLS_DC)
146 {
147 zval curval;
148 char *envvar;
149
150 /* Fetch configuration option value */
151 if (zend_get_configuration_directive(Z_STRVAL_P(name), Z_STRLEN_P(name)+1, &curval) == SUCCESS) {
152 Z_STRVAL_P(result) = zend_strndup(Z_STRVAL(curval), Z_STRLEN(curval));
153 Z_STRLEN_P(result) = Z_STRLEN(curval);
154 /* ..or if not found, try ENV */
155 } else if ((envvar = zend_getenv(Z_STRVAL_P(name), Z_STRLEN_P(name) TSRMLS_CC)) != NULL ||
156 (envvar = getenv(Z_STRVAL_P(name))) != NULL) {
157 Z_STRVAL_P(result) = strdup(envvar);
158 Z_STRLEN_P(result) = strlen(envvar);
159 } else {
160 zend_ini_init_string(result);
161 }
162 }
163 /* }}} */
164
165 /* {{{ ini_error()
166 */
ini_error(char * msg)167 static void ini_error(char *msg)
168 {
169 char *error_buf;
170 int error_buf_len;
171 char *currently_parsed_filename;
172 TSRMLS_FETCH();
173
174 currently_parsed_filename = zend_ini_scanner_get_filename(TSRMLS_C);
175 if (currently_parsed_filename) {
176 error_buf_len = 128 + strlen(msg) + strlen(currently_parsed_filename); /* should be more than enough */
177 error_buf = (char *) emalloc(error_buf_len);
178
179 sprintf(error_buf, "%s in %s on line %d\n", msg, currently_parsed_filename, zend_ini_scanner_get_lineno(TSRMLS_C));
180 } else {
181 error_buf = estrdup("Invalid configuration directive\n");
182 }
183
184 if (CG(ini_parser_unbuffered_errors)) {
185 #ifdef PHP_WIN32
186 syslog(LOG_ALERT, "PHP: %s (%s)", error_buf, GetCommandLine());
187 #endif
188 fprintf(stderr, "PHP: %s", error_buf);
189 } else {
190 zend_error(E_WARNING, "%s", error_buf);
191 }
192 efree(error_buf);
193 }
194 /* }}} */
195
196 /* {{{ zend_parse_ini_file()
197 */
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)198 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)
199 {
200 int retval;
201 zend_ini_parser_param ini_parser_param;
202
203 ini_parser_param.ini_parser_cb = ini_parser_cb;
204 ini_parser_param.arg = arg;
205 CG(ini_parser_param) = &ini_parser_param;
206
207 if (zend_ini_open_file_for_scanning(fh, scanner_mode TSRMLS_CC) == FAILURE) {
208 return FAILURE;
209 }
210
211 CG(ini_parser_unbuffered_errors) = unbuffered_errors;
212 retval = ini_parse(TSRMLS_C);
213 zend_file_handle_dtor(fh TSRMLS_CC);
214
215 shutdown_ini_scanner(TSRMLS_C);
216
217 if (retval == 0) {
218 return SUCCESS;
219 } else {
220 return FAILURE;
221 }
222 }
223 /* }}} */
224
225 /* {{{ zend_parse_ini_string()
226 */
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)227 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)
228 {
229 int retval;
230 zend_ini_parser_param ini_parser_param;
231
232 ini_parser_param.ini_parser_cb = ini_parser_cb;
233 ini_parser_param.arg = arg;
234 CG(ini_parser_param) = &ini_parser_param;
235
236 if (zend_ini_prepare_string_for_scanning(str, scanner_mode TSRMLS_CC) == FAILURE) {
237 return FAILURE;
238 }
239
240 CG(ini_parser_unbuffered_errors) = unbuffered_errors;
241 retval = ini_parse(TSRMLS_C);
242
243 shutdown_ini_scanner(TSRMLS_C);
244
245 if (retval == 0) {
246 return SUCCESS;
247 } else {
248 return FAILURE;
249 }
250 }
251 /* }}} */
252
253 %}
254
255 %expect 0
256 %pure_parser
257
258 %token TC_SECTION
259 %token TC_RAW
260 %token TC_CONSTANT
261 %token TC_NUMBER
262 %token TC_STRING
263 %token TC_WHITESPACE
264 %token TC_LABEL
265 %token TC_OFFSET
266 %token TC_DOLLAR_CURLY
267 %token TC_VARNAME
268 %token TC_QUOTED_STRING
269 %token BOOL_TRUE
270 %token BOOL_FALSE
271 %token END_OF_LINE
272 %token '=' ':' ',' '.' '"' '\'' '^' '+' '-' '/' '*' '%' '$' '~' '<' '>' '?' '@' '{' '}'
273 %left '|' '&' '^'
274 %right '~' '!'
275
276 %%
277
278 statement_list:
279 statement_list statement
280 | /* empty */
281 ;
282
283 statement:
284 TC_SECTION section_string_or_value ']' {
285 #if DEBUG_CFG_PARSER
286 printf("SECTION: [%s]\n", Z_STRVAL($2));
287 #endif
288 ZEND_INI_PARSER_CB(&$2, NULL, NULL, ZEND_INI_PARSER_SECTION, ZEND_INI_PARSER_ARG TSRMLS_CC);
289 free(Z_STRVAL($2));
290 }
291 | TC_LABEL '=' string_or_value {
292 #if DEBUG_CFG_PARSER
293 printf("NORMAL: '%s' = '%s'\n", Z_STRVAL($1), Z_STRVAL($3));
294 #endif
295 ZEND_INI_PARSER_CB(&$1, &$3, NULL, ZEND_INI_PARSER_ENTRY, ZEND_INI_PARSER_ARG TSRMLS_CC);
296 free(Z_STRVAL($1));
297 free(Z_STRVAL($3));
298 }
299 | TC_OFFSET option_offset ']' '=' string_or_value {
300 #if DEBUG_CFG_PARSER
301 printf("OFFSET: '%s'[%s] = '%s'\n", Z_STRVAL($1), Z_STRVAL($2), Z_STRVAL($5));
302 #endif
303 ZEND_INI_PARSER_CB(&$1, &$5, &$2, ZEND_INI_PARSER_POP_ENTRY, ZEND_INI_PARSER_ARG TSRMLS_CC);
304 free(Z_STRVAL($1));
305 free(Z_STRVAL($2));
306 free(Z_STRVAL($5));
307 }
308 | TC_LABEL { ZEND_INI_PARSER_CB(&$1, NULL, NULL, ZEND_INI_PARSER_ENTRY, ZEND_INI_PARSER_ARG TSRMLS_CC); free(Z_STRVAL($1)); }
309 | END_OF_LINE
310 ;
311
312 section_string_or_value:
313 var_string_list_section { $$ = $1; }
314 | /* empty */ { zend_ini_init_string(&$$); }
315 ;
316
317 string_or_value:
318 expr { $$ = $1; }
319 | BOOL_TRUE { $$ = $1; }
320 | BOOL_FALSE { $$ = $1; }
321 | END_OF_LINE { zend_ini_init_string(&$$); }
322 ;
323
324 option_offset:
325 var_string_list { $$ = $1; }
326 | /* empty */ { zend_ini_init_string(&$$); }
327 ;
328
329 encapsed_list:
330 encapsed_list cfg_var_ref { zend_ini_add_string(&$$, &$1, &$2); free(Z_STRVAL($2)); }
331 | encapsed_list TC_QUOTED_STRING { zend_ini_add_string(&$$, &$1, &$2); free(Z_STRVAL($2)); }
332 | /* empty */ { zend_ini_init_string(&$$); }
333 ;
334
335 var_string_list_section:
336 cfg_var_ref { $$ = $1; }
337 | constant_literal { $$ = $1; }
338 | '"' encapsed_list '"' { $$ = $2; }
339 | var_string_list_section cfg_var_ref { zend_ini_add_string(&$$, &$1, &$2); free(Z_STRVAL($2)); }
340 | var_string_list_section constant_literal { zend_ini_add_string(&$$, &$1, &$2); free(Z_STRVAL($2)); }
341 | var_string_list_section '"' encapsed_list '"' { zend_ini_add_string(&$$, &$1, &$3); free(Z_STRVAL($3)); }
342 ;
343
344 var_string_list:
345 cfg_var_ref { $$ = $1; }
346 | constant_string { $$ = $1; }
347 | '"' encapsed_list '"' { $$ = $2; }
348 | var_string_list cfg_var_ref { zend_ini_add_string(&$$, &$1, &$2); free(Z_STRVAL($2)); }
349 | var_string_list constant_string { zend_ini_add_string(&$$, &$1, &$2); free(Z_STRVAL($2)); }
350 | var_string_list '"' encapsed_list '"' { zend_ini_add_string(&$$, &$1, &$3); free(Z_STRVAL($3)); }
351 ;
352
353 expr:
354 var_string_list { $$ = $1; }
355 | expr '|' expr { zend_ini_do_op('|', &$$, &$1, &$3); }
356 | expr '&' expr { zend_ini_do_op('&', &$$, &$1, &$3); }
357 | expr '^' expr { zend_ini_do_op('^', &$$, &$1, &$3); }
358 | '~' expr { zend_ini_do_op('~', &$$, &$2, NULL); }
359 | '!' expr { zend_ini_do_op('!', &$$, &$2, NULL); }
360 | '(' expr ')' { $$ = $2; }
361 ;
362
363 cfg_var_ref:
364 TC_DOLLAR_CURLY TC_VARNAME '}' { zend_ini_get_var(&$$, &$2 TSRMLS_CC); free(Z_STRVAL($2)); }
365 ;
366
367 constant_literal:
368 TC_CONSTANT { $$ = $1; }
369 | TC_RAW { $$ = $1; /*printf("TC_RAW: '%s'\n", Z_STRVAL($1));*/ }
370 | TC_NUMBER { $$ = $1; /*printf("TC_NUMBER: '%s'\n", Z_STRVAL($1));*/ }
371 | TC_STRING { $$ = $1; /*printf("TC_STRING: '%s'\n", Z_STRVAL($1));*/ }
372 | TC_WHITESPACE { $$ = $1; /*printf("TC_WHITESPACE: '%s'\n", Z_STRVAL($1));*/ }
373 ;
374
375 constant_string:
376 TC_CONSTANT { zend_ini_get_constant(&$$, &$1 TSRMLS_CC); }
377 | TC_RAW { $$ = $1; /*printf("TC_RAW: '%s'\n", Z_STRVAL($1));*/ }
378 | TC_NUMBER { $$ = $1; /*printf("TC_NUMBER: '%s'\n", Z_STRVAL($1));*/ }
379 | TC_STRING { $$ = $1; /*printf("TC_STRING: '%s'\n", Z_STRVAL($1));*/ }
380 | TC_WHITESPACE { $$ = $1; /*printf("TC_WHITESPACE: '%s'\n", Z_STRVAL($1));*/ }
381 ;
382
383 /*
384 * Local variables:
385 * tab-width: 4
386 * c-basic-offset: 4
387 * indent-tabs-mode: t
388 * End:
389 */
390