1 %require "3.0"
2 %{
3 /*
4 +----------------------------------------------------------------------+
5 | Zend Engine |
6 +----------------------------------------------------------------------+
7 | Copyright (c) Zend Technologies Ltd. (http://www.zend.com) |
8 +----------------------------------------------------------------------+
9 | This source file is subject to version 2.00 of the Zend license, |
10 | that is bundled with this package in the file LICENSE, and is |
11 | available through the world-wide-web at the following url: |
12 | http://www.zend.com/license/2_00.txt. |
13 | If you did not receive a copy of the Zend license and are unable to |
14 | obtain it through the world-wide-web, please send a note to |
15 | license@zend.com so we can mail you a copy immediately. |
16 +----------------------------------------------------------------------+
17 | Authors: Zeev Suraski <zeev@php.net> |
18 | Jani Taskinen <jani@php.net> |
19 +----------------------------------------------------------------------+
20 */
21
22 #define DEBUG_CFG_PARSER 0
23
24 #include "zend.h"
25 #include "zend_API.h"
26 #include "zend_ini.h"
27 #include "zend_constants.h"
28 #include "zend_ini_scanner.h"
29 #include "zend_extensions.h"
30
31 #ifdef ZEND_WIN32
32 #include "win32/syslog.h"
33 #endif
34
35 static int ini_parse(void);
36
37 #define ZEND_INI_PARSER_CB (CG(ini_parser_param))->ini_parser_cb
38 #define ZEND_INI_PARSER_ARG (CG(ini_parser_param))->arg
39
40 #ifdef _MSC_VER
41 #define YYMALLOC malloc
42 #define YYFREE free
43 #endif
44
45 #define ZEND_SYSTEM_INI CG(ini_parser_unbuffered_errors)
46 #define INI_ZVAL_IS_NUMBER 1
47
get_int_val(zval * op)48 static int get_int_val(zval *op) {
49 switch (Z_TYPE_P(op)) {
50 case IS_LONG:
51 return Z_LVAL_P(op);
52 case IS_DOUBLE:
53 return (int)Z_DVAL_P(op);
54 case IS_STRING:
55 {
56 int val = atoi(Z_STRVAL_P(op));
57 zend_string_free(Z_STR_P(op));
58 return val;
59 }
60 EMPTY_SWITCH_DEFAULT_CASE()
61 }
62 }
63
64 /* {{{ zend_ini_do_op() */
zend_ini_do_op(char type,zval * result,zval * op1,zval * op2)65 static void zend_ini_do_op(char type, zval *result, zval *op1, zval *op2)
66 {
67 int i_result;
68 int i_op1, i_op2;
69 int str_len;
70 char str_result[MAX_LENGTH_OF_LONG+1];
71
72 i_op1 = get_int_val(op1);
73 i_op2 = op2 ? get_int_val(op2) : 0;
74
75 switch (type) {
76 case '|':
77 i_result = i_op1 | i_op2;
78 break;
79 case '&':
80 i_result = i_op1 & i_op2;
81 break;
82 case '^':
83 i_result = i_op1 ^ i_op2;
84 break;
85 case '~':
86 i_result = ~i_op1;
87 break;
88 case '!':
89 i_result = !i_op1;
90 break;
91 default:
92 i_result = 0;
93 break;
94 }
95
96 if (INI_SCNG(scanner_mode) != ZEND_INI_SCANNER_TYPED) {
97 str_len = sprintf(str_result, "%d", i_result);
98 ZVAL_NEW_STR(result, zend_string_init(str_result, str_len, ZEND_SYSTEM_INI));
99 } else {
100 ZVAL_LONG(result, i_result);
101 }
102 }
103 /* }}} */
104
105 /* {{{ zend_ini_init_string() */
zend_ini_init_string(zval * result)106 static void zend_ini_init_string(zval *result)
107 {
108 if (ZEND_SYSTEM_INI) {
109 ZVAL_EMPTY_PSTRING(result);
110 } else {
111 ZVAL_EMPTY_STRING(result);
112 }
113 Z_EXTRA_P(result) = 0;
114 }
115 /* }}} */
116
117 /* {{{ zend_ini_add_string() */
zend_ini_add_string(zval * result,zval * op1,zval * op2)118 static void zend_ini_add_string(zval *result, zval *op1, zval *op2)
119 {
120 int length, op1_len;
121
122 if (Z_TYPE_P(op1) != IS_STRING) {
123 /* ZEND_ASSERT(!Z_REFCOUNTED_P(op1)); */
124 if (ZEND_SYSTEM_INI) {
125 zend_string *tmp_str;
126 zend_string *str = zval_get_tmp_string(op1, &tmp_str);
127 ZVAL_PSTRINGL(op1, ZSTR_VAL(str), ZSTR_LEN(str));
128 zend_tmp_string_release(tmp_str);
129 } else {
130 ZVAL_STR(op1, zval_get_string_func(op1));
131 }
132 }
133 op1_len = (int)Z_STRLEN_P(op1);
134
135 if (Z_TYPE_P(op2) != IS_STRING) {
136 convert_to_string(op2);
137 }
138 length = op1_len + (int)Z_STRLEN_P(op2);
139
140 ZVAL_NEW_STR(result, zend_string_extend(Z_STR_P(op1), length, ZEND_SYSTEM_INI));
141 memcpy(Z_STRVAL_P(result) + op1_len, Z_STRVAL_P(op2), Z_STRLEN_P(op2) + 1);
142 }
143 /* }}} */
144
145 /* {{{ zend_ini_get_constant() */
zend_ini_get_constant(zval * result,zval * name)146 static void zend_ini_get_constant(zval *result, zval *name)
147 {
148 zval *c, tmp;
149
150 /* If name contains ':' it is not a constant. Bug #26893. */
151 if (!memchr(Z_STRVAL_P(name), ':', Z_STRLEN_P(name))
152 && (c = zend_get_constant(Z_STR_P(name))) != 0) {
153 if (Z_TYPE_P(c) != IS_STRING) {
154 ZVAL_COPY_OR_DUP(&tmp, c);
155 if (Z_OPT_CONSTANT(tmp)) {
156 zval_update_constant_ex(&tmp, NULL);
157 }
158 convert_to_string(&tmp);
159 c = &tmp;
160 }
161 ZVAL_NEW_STR(result, zend_string_init(Z_STRVAL_P(c), Z_STRLEN_P(c), ZEND_SYSTEM_INI));
162 if (c == &tmp) {
163 zend_string_release(Z_STR(tmp));
164 }
165 zend_string_free(Z_STR_P(name));
166 } else {
167 *result = *name;
168 }
169 }
170 /* }}} */
171
172 /* {{{ zend_ini_get_var() */
zend_ini_get_var(zval * result,zval * name,zval * fallback)173 static void zend_ini_get_var(zval *result, zval *name, zval *fallback)
174 {
175 zval *curval;
176 char *envvar;
177
178 /* Fetch configuration option value */
179 if ((curval = zend_get_configuration_directive(Z_STR_P(name))) != NULL) {
180 ZVAL_NEW_STR(result, zend_string_init(Z_STRVAL_P(curval), Z_STRLEN_P(curval), ZEND_SYSTEM_INI));
181 /* ..or if not found, try ENV */
182 } else if ((envvar = zend_getenv(Z_STRVAL_P(name), Z_STRLEN_P(name))) != NULL) {
183 ZVAL_NEW_STR(result, zend_string_init(envvar, strlen(envvar), ZEND_SYSTEM_INI));
184 efree(envvar);
185 } else if ((envvar = getenv(Z_STRVAL_P(name))) != NULL) {
186 ZVAL_NEW_STR(result, zend_string_init(envvar, strlen(envvar), ZEND_SYSTEM_INI));
187 /* ..or if not defined, try fallback value */
188 } else if (fallback) {
189 ZVAL_NEW_STR(result, zend_string_init(Z_STRVAL_P(fallback), strlen(Z_STRVAL_P(fallback)), ZEND_SYSTEM_INI));
190 } else {
191 zend_ini_init_string(result);
192 }
193
194 }
195 /* }}} */
196
197 /* {{{ ini_error() */
ini_error(const char * msg)198 static ZEND_COLD void ini_error(const char *msg)
199 {
200 char *error_buf;
201 int error_buf_len;
202
203 const char *const currently_parsed_filename = zend_ini_scanner_get_filename();
204 if (currently_parsed_filename) {
205 error_buf_len = 128 + (int)strlen(msg) + (int)strlen(currently_parsed_filename); /* should be more than enough */
206 error_buf = (char *) emalloc(error_buf_len);
207
208 sprintf(error_buf, "%s in %s on line %d\n", msg, currently_parsed_filename, zend_ini_scanner_get_lineno());
209 } else {
210 error_buf = estrdup("Invalid configuration directive\n");
211 }
212
213 if (CG(ini_parser_unbuffered_errors)) {
214 #ifdef ZEND_WIN32
215 syslog(LOG_ALERT, "PHP: %s (%s)", error_buf, GetCommandLine());
216 #endif
217 fprintf(stderr, "PHP: %s", error_buf);
218 } else {
219 zend_error(E_WARNING, "%s", error_buf);
220 }
221 efree(error_buf);
222 }
223 /* }}} */
224
225 /* {{{ zend_parse_ini_file() */
zend_parse_ini_file(zend_file_handle * fh,bool unbuffered_errors,int scanner_mode,zend_ini_parser_cb_t ini_parser_cb,void * arg)226 ZEND_API zend_result zend_parse_ini_file(zend_file_handle *fh, bool unbuffered_errors, int scanner_mode, zend_ini_parser_cb_t ini_parser_cb, void *arg)
227 {
228 int retval;
229 zend_ini_parser_param ini_parser_param;
230
231 ini_parser_param.ini_parser_cb = ini_parser_cb;
232 ini_parser_param.arg = arg;
233 CG(ini_parser_param) = &ini_parser_param;
234
235 if (zend_ini_open_file_for_scanning(fh, scanner_mode) == FAILURE) {
236 return FAILURE;
237 }
238
239 CG(ini_parser_unbuffered_errors) = unbuffered_errors;
240 retval = ini_parse();
241
242 shutdown_ini_scanner();
243
244 if (retval == 0) {
245 return SUCCESS;
246 } else {
247 return FAILURE;
248 }
249 }
250 /* }}} */
251
252 /* {{{ zend_parse_ini_string() */
zend_parse_ini_string(const char * str,bool unbuffered_errors,int scanner_mode,zend_ini_parser_cb_t ini_parser_cb,void * arg)253 ZEND_API zend_result zend_parse_ini_string(const char *str, bool unbuffered_errors, int scanner_mode, zend_ini_parser_cb_t ini_parser_cb, void *arg)
254 {
255 int retval;
256 zend_ini_parser_param ini_parser_param;
257
258 ini_parser_param.ini_parser_cb = ini_parser_cb;
259 ini_parser_param.arg = arg;
260 CG(ini_parser_param) = &ini_parser_param;
261
262 if (zend_ini_prepare_string_for_scanning(str, scanner_mode) == FAILURE) {
263 return FAILURE;
264 }
265
266 CG(ini_parser_unbuffered_errors) = unbuffered_errors;
267 retval = ini_parse();
268
269 shutdown_ini_scanner();
270
271 if (retval == 0) {
272 return SUCCESS;
273 } else {
274 return FAILURE;
275 }
276 }
277 /* }}} */
278
279 /* {{{ zval_ini_dtor() */
zval_ini_dtor(zval * zv)280 static void zval_ini_dtor(zval *zv)
281 {
282 if (Z_TYPE_P(zv) == IS_STRING) {
283 if (ZEND_SYSTEM_INI) {
284 GC_MAKE_PERSISTENT_LOCAL(Z_STR_P(zv));
285 }
286 zend_string_release(Z_STR_P(zv));
287 }
288 }
289 /* }}} */
290
convert_to_number(zval * retval,const char * str,const int str_len)291 static inline zend_result convert_to_number(zval *retval, const char *str, const int str_len)
292 {
293 uint8_t type;
294 int overflow;
295 zend_long lval;
296 double dval;
297
298 if ((type = is_numeric_string_ex(str, str_len, &lval, &dval, 0, &overflow, NULL)) != 0) {
299 if (type == IS_LONG) {
300 ZVAL_LONG(retval, lval);
301 return SUCCESS;
302 } else if (type == IS_DOUBLE && !overflow) {
303 ZVAL_DOUBLE(retval, dval);
304 return SUCCESS;
305 }
306 }
307
308 return FAILURE;
309 }
310
normalize_value(zval * zv)311 static void normalize_value(zval *zv)
312 {
313 if (INI_SCNG(scanner_mode) != ZEND_INI_SCANNER_TYPED) {
314 return;
315 }
316
317 ZEND_ASSERT(Z_EXTRA_P(zv) == 0 || Z_EXTRA_P(zv) == INI_ZVAL_IS_NUMBER);
318 if (Z_EXTRA_P(zv) == INI_ZVAL_IS_NUMBER && Z_TYPE_P(zv) == IS_STRING) {
319 zval number_rv;
320 if (convert_to_number(&number_rv, Z_STRVAL_P(zv), Z_STRLEN_P(zv)) == SUCCESS) {
321 zval_ptr_dtor(zv);
322 ZVAL_COPY_VALUE(zv, &number_rv);
323 }
324 }
325 }
326
327 %}
328
329 %expect 0
330 %define api.prefix {ini_}
331 %define api.pure full
332 %define api.value.type {zval}
333 %define parse.error verbose
334
335 %token END 0 "end of file"
336 %token TC_SECTION
337 %token TC_RAW
338 %token TC_CONSTANT
339 %token TC_NUMBER
340 %token TC_STRING
341 %token TC_WHITESPACE
342 %token TC_LABEL
343 %token TC_OFFSET
344 %token TC_DOLLAR_CURLY
345 %token TC_VARNAME
346 %token TC_QUOTED_STRING
347 %token TC_FALLBACK
348 %token BOOL_TRUE
349 %token BOOL_FALSE
350 %token NULL_NULL
351 %token END_OF_LINE
352 %token '=' ':' ',' '.' '"' '\'' '^' '+' '-' '/' '*' '%' '$' '~' '<' '>' '?' '@' '{' '}'
353 %left '|' '&' '^'
354 %precedence '~' '!'
355
356 %destructor { zval_ini_dtor(&$$); } TC_RAW TC_CONSTANT TC_NUMBER TC_STRING TC_WHITESPACE TC_LABEL TC_OFFSET TC_VARNAME BOOL_TRUE BOOL_FALSE NULL_NULL cfg_var_ref constant_literal constant_string encapsed_list expr option_offset section_string_or_value string_or_value var_string_list var_string_list_section
357
358 %%
359
360 statement_list:
361 statement_list statement
362 | %empty { (void) ini_nerrs; }
363 ;
364
365 statement:
366 TC_SECTION section_string_or_value ']' {
367 #if DEBUG_CFG_PARSER
368 printf("SECTION: [%s]\n", Z_STRVAL($2));
369 #endif
370 ZEND_INI_PARSER_CB(&$2, NULL, NULL, ZEND_INI_PARSER_SECTION, ZEND_INI_PARSER_ARG);
371 zend_string_release(Z_STR($2));
372 }
373 | TC_LABEL '=' string_or_value {
374 #if DEBUG_CFG_PARSER
375 printf("NORMAL: '%s' = '%s'\n", Z_STRVAL($1), Z_STRVAL($3));
376 #endif
377 ZEND_INI_PARSER_CB(&$1, &$3, NULL, ZEND_INI_PARSER_ENTRY, ZEND_INI_PARSER_ARG);
378 if (ZEND_SYSTEM_INI) {
379 GC_MAKE_PERSISTENT_LOCAL(Z_STR($1));
380 }
381 zend_string_release(Z_STR($1));
382 zval_ini_dtor(&$3);
383 }
384 | TC_OFFSET option_offset ']' '=' string_or_value {
385 #if DEBUG_CFG_PARSER
386 printf("OFFSET: '%s'[%s] = '%s'\n", Z_STRVAL($1), Z_STRVAL($2), Z_STRVAL($5));
387 #endif
388 ZEND_INI_PARSER_CB(&$1, &$5, &$2, ZEND_INI_PARSER_POP_ENTRY, ZEND_INI_PARSER_ARG);
389 zend_string_release(Z_STR($1));
390 zval_ini_dtor(&$2);
391 zval_ini_dtor(&$5);
392 }
393 | TC_LABEL { ZEND_INI_PARSER_CB(&$1, NULL, NULL, ZEND_INI_PARSER_ENTRY, ZEND_INI_PARSER_ARG); zend_string_release(Z_STR($1)); }
394 | END_OF_LINE
395 ;
396
397 section_string_or_value:
398 var_string_list_section { $$ = $1; }
399 | %empty { zend_ini_init_string(&$$); }
400 ;
401
402 string_or_value:
403 expr { $$ = $1; normalize_value(&$$); }
404 | BOOL_TRUE { $$ = $1; }
405 | BOOL_FALSE { $$ = $1; }
406 | NULL_NULL { $$ = $1; }
407 | END_OF_LINE { zend_ini_init_string(&$$); }
408 ;
409
410 option_offset:
411 var_string_list { $$ = $1; }
412 | %empty { zend_ini_init_string(&$$); }
413 ;
414
415 encapsed_list:
416 encapsed_list cfg_var_ref { zend_ini_add_string(&$$, &$1, &$2); zend_string_free(Z_STR($2)); }
417 | encapsed_list TC_QUOTED_STRING { zend_ini_add_string(&$$, &$1, &$2); zend_string_free(Z_STR($2)); }
418 | %empty { zend_ini_init_string(&$$); }
419 ;
420
421 var_string_list_section:
422 cfg_var_ref { $$ = $1; }
423 | constant_literal { $$ = $1; }
424 | '"' encapsed_list '"' { $$ = $2; }
425 | var_string_list_section cfg_var_ref { zend_ini_add_string(&$$, &$1, &$2); zend_string_free(Z_STR($2)); }
426 | var_string_list_section constant_literal { zend_ini_add_string(&$$, &$1, &$2); zend_string_free(Z_STR($2)); }
427 | var_string_list_section '"' encapsed_list '"' { zend_ini_add_string(&$$, &$1, &$3); zend_string_free(Z_STR($3)); }
428 ;
429
430 var_string_list:
431 cfg_var_ref { $$ = $1; }
432 | constant_string { $$ = $1; }
433 | '"' encapsed_list '"' { $$ = $2; }
434 | var_string_list cfg_var_ref { zend_ini_add_string(&$$, &$1, &$2); zend_string_free(Z_STR($2)); }
435 | var_string_list constant_string { zend_ini_add_string(&$$, &$1, &$2); zend_string_free(Z_STR($2)); }
436 | var_string_list '"' encapsed_list '"' { zend_ini_add_string(&$$, &$1, &$3); zend_string_free(Z_STR($3)); }
437 ;
438
439 expr:
440 var_string_list { $$ = $1; }
441 | expr '|' expr { zend_ini_do_op('|', &$$, &$1, &$3); }
442 | expr '&' expr { zend_ini_do_op('&', &$$, &$1, &$3); }
443 | expr '^' expr { zend_ini_do_op('^', &$$, &$1, &$3); }
444 | '~' expr { zend_ini_do_op('~', &$$, &$2, NULL); }
445 | '!' expr { zend_ini_do_op('!', &$$, &$2, NULL); }
446 | '(' expr ')' { $$ = $2; }
447 ;
448
449 cfg_var_ref:
450 TC_DOLLAR_CURLY TC_VARNAME '}' { zend_ini_get_var(&$$, &$2, NULL); zend_string_free(Z_STR($2)); }
451 | TC_DOLLAR_CURLY TC_VARNAME TC_FALLBACK fallback '}' { zend_ini_get_var(&$$, &$2, &$4); zend_string_free(Z_STR($2)); zend_string_free(Z_STR($4)); }
452 ;
453
454
455 fallback:
456 var_string_list { $$ = $1; }
457 | %empty { zend_ini_init_string(&$$); }
458 ;
459
460 constant_literal:
461 TC_CONSTANT { $$ = $1; }
462 | TC_RAW { $$ = $1; /*printf("TC_RAW: '%s'\n", Z_STRVAL($1));*/ }
463 | TC_NUMBER { $$ = $1; /*printf("TC_NUMBER: '%s'\n", Z_STRVAL($1));*/ }
464 | TC_STRING { $$ = $1; /*printf("TC_STRING: '%s'\n", Z_STRVAL($1));*/ }
465 | TC_WHITESPACE { $$ = $1; /*printf("TC_WHITESPACE: '%s'\n", Z_STRVAL($1));*/ }
466 ;
467
468 constant_string:
469 TC_CONSTANT { zend_ini_get_constant(&$$, &$1); }
470 | TC_RAW { $$ = $1; /*printf("TC_RAW: '%s'\n", Z_STRVAL($1));*/ }
471 | TC_NUMBER {
472 $$ = $1;
473 Z_EXTRA($$) = INI_ZVAL_IS_NUMBER;
474 /*printf("TC_NUMBER: '%s'\n", Z_STRVAL($1));*/
475 }
476 | TC_STRING { $$ = $1; /*printf("TC_STRING: '%s'\n", Z_STRVAL($1));*/ }
477 | TC_WHITESPACE { $$ = $1; /*printf("TC_WHITESPACE: '%s'\n", Z_STRVAL($1));*/ }
478 ;
479