1 %{
2 /*
3 +----------------------------------------------------------------------+
4 | Zend Engine |
5 +----------------------------------------------------------------------+
6 | Copyright (c) 1998-2018 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 ZEND_WIN32
33 #include "win32/syslog.h"
34 #endif
35
36 #define YYERROR_VERBOSE
37 #define YYSTYPE zval
38
39 int ini_parse(void);
40
41 #define ZEND_INI_PARSER_CB (CG(ini_parser_param))->ini_parser_cb
42 #define ZEND_INI_PARSER_ARG (CG(ini_parser_param))->arg
43
44 #ifdef _MSC_VER
45 #define YYMALLOC malloc
46 #define YYFREE free
47 #endif
48
49 #define ZEND_SYSTEM_INI CG(ini_parser_unbuffered_errors)
50
get_int_val(zval * op)51 static int get_int_val(zval *op) {
52 switch (Z_TYPE_P(op)) {
53 case IS_LONG:
54 return Z_LVAL_P(op);
55 case IS_DOUBLE:
56 return (int)Z_DVAL_P(op);
57 case IS_STRING:
58 {
59 int val = atoi(Z_STRVAL_P(op));
60 zend_string_free(Z_STR_P(op));
61 return val;
62 }
63 EMPTY_SWITCH_DEFAULT_CASE()
64 }
65 }
66
67 /* {{{ zend_ini_do_op()
68 */
zend_ini_do_op(char type,zval * result,zval * op1,zval * op2)69 static void zend_ini_do_op(char type, zval *result, zval *op1, zval *op2)
70 {
71 int i_result;
72 int i_op1, i_op2;
73 int str_len;
74 char str_result[MAX_LENGTH_OF_LONG+1];
75
76 i_op1 = get_int_val(op1);
77 i_op2 = op2 ? get_int_val(op2) : 0;
78
79 switch (type) {
80 case '|':
81 i_result = i_op1 | i_op2;
82 break;
83 case '&':
84 i_result = i_op1 & i_op2;
85 break;
86 case '^':
87 i_result = i_op1 ^ i_op2;
88 break;
89 case '~':
90 i_result = ~i_op1;
91 break;
92 case '!':
93 i_result = !i_op1;
94 break;
95 default:
96 i_result = 0;
97 break;
98 }
99
100 str_len = zend_sprintf(str_result, "%d", i_result);
101 ZVAL_NEW_STR(result, zend_string_init(str_result, str_len, ZEND_SYSTEM_INI));
102 }
103 /* }}} */
104
105 /* {{{ zend_ini_init_string()
106 */
zend_ini_init_string(zval * result)107 static void zend_ini_init_string(zval *result)
108 {
109 if (ZEND_SYSTEM_INI) {
110 ZVAL_EMPTY_PSTRING(result);
111 } else {
112 ZVAL_EMPTY_STRING(result);
113 }
114 }
115 /* }}} */
116
117 /* {{{ zend_ini_add_string()
118 */
zend_ini_add_string(zval * result,zval * op1,zval * op2)119 static void zend_ini_add_string(zval *result, zval *op1, zval *op2)
120 {
121 int length, op1_len;
122
123 if (Z_TYPE_P(op1) != IS_STRING) {
124 zend_string *str = zval_get_string(op1);
125 /* ZEND_ASSERT(!Z_REFCOUNTED_P(op1)); */
126 if (ZEND_SYSTEM_INI) {
127 ZVAL_PSTRINGL(op1, ZSTR_VAL(str), ZSTR_LEN(str));
128 zend_string_release(str);
129 } else {
130 ZVAL_STR(op1, str);
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()
146 */
zend_ini_get_constant(zval * result,zval * name)147 static void zend_ini_get_constant(zval *result, zval *name)
148 {
149 zval *c, tmp;
150
151 /* If name contains ':' it is not a constant. Bug #26893. */
152 if (!memchr(Z_STRVAL_P(name), ':', Z_STRLEN_P(name))
153 && (c = zend_get_constant(Z_STR_P(name))) != 0) {
154 if (Z_TYPE_P(c) != IS_STRING) {
155 ZVAL_DUP(&tmp, c);
156 if (Z_OPT_CONSTANT(tmp)) {
157 zval_update_constant_ex(&tmp, NULL);
158 }
159 convert_to_string(&tmp);
160 c = &tmp;
161 }
162 ZVAL_NEW_STR(result, zend_string_init(Z_STRVAL_P(c), Z_STRLEN_P(c), ZEND_SYSTEM_INI));
163 if (c == &tmp) {
164 zend_string_release(Z_STR(tmp));
165 }
166 zend_string_free(Z_STR_P(name));
167 } else {
168 *result = *name;
169 }
170 }
171 /* }}} */
172
173 /* {{{ zend_ini_get_var()
174 */
zend_ini_get_var(zval * result,zval * name)175 static void zend_ini_get_var(zval *result, zval *name)
176 {
177 zval *curval;
178 char *envvar;
179
180 /* Fetch configuration option value */
181 if ((curval = zend_get_configuration_directive(Z_STR_P(name))) != NULL) {
182 ZVAL_NEW_STR(result, zend_string_init(Z_STRVAL_P(curval), Z_STRLEN_P(curval), ZEND_SYSTEM_INI));
183 /* ..or if not found, try ENV */
184 } else if ((envvar = zend_getenv(Z_STRVAL_P(name), Z_STRLEN_P(name))) != NULL ||
185 (envvar = getenv(Z_STRVAL_P(name))) != NULL) {
186 ZVAL_NEW_STR(result, zend_string_init(envvar, strlen(envvar), ZEND_SYSTEM_INI));
187 } else {
188 zend_ini_init_string(result);
189 }
190 }
191 /* }}} */
192
193 /* {{{ ini_error()
194 */
ini_error(const char * msg)195 static ZEND_COLD void ini_error(const char *msg)
196 {
197 char *error_buf;
198 int error_buf_len;
199 char *currently_parsed_filename;
200
201 currently_parsed_filename = zend_ini_scanner_get_filename();
202 if (currently_parsed_filename) {
203 error_buf_len = 128 + (int)strlen(msg) + (int)strlen(currently_parsed_filename); /* should be more than enough */
204 error_buf = (char *) emalloc(error_buf_len);
205
206 sprintf(error_buf, "%s in %s on line %d\n", msg, currently_parsed_filename, zend_ini_scanner_get_lineno());
207 } else {
208 error_buf = estrdup("Invalid configuration directive\n");
209 }
210
211 if (CG(ini_parser_unbuffered_errors)) {
212 #ifdef ZEND_WIN32
213 syslog(LOG_ALERT, "PHP: %s (%s)", error_buf, GetCommandLine());
214 #endif
215 fprintf(stderr, "PHP: %s", error_buf);
216 } else {
217 zend_error(E_WARNING, "%s", error_buf);
218 }
219 efree(error_buf);
220 }
221 /* }}} */
222
223 /* {{{ zend_parse_ini_file()
224 */
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)225 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)
226 {
227 int retval;
228 zend_ini_parser_param ini_parser_param;
229
230 ini_parser_param.ini_parser_cb = ini_parser_cb;
231 ini_parser_param.arg = arg;
232 CG(ini_parser_param) = &ini_parser_param;
233
234 if (zend_ini_open_file_for_scanning(fh, scanner_mode) == FAILURE) {
235 return FAILURE;
236 }
237
238 CG(ini_parser_unbuffered_errors) = unbuffered_errors;
239 retval = ini_parse();
240 zend_file_handle_dtor(fh);
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()
253 */
zend_parse_ini_string(char * str,zend_bool unbuffered_errors,int scanner_mode,zend_ini_parser_cb_t ini_parser_cb,void * arg)254 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)
255 {
256 int retval;
257 zend_ini_parser_param ini_parser_param;
258
259 ini_parser_param.ini_parser_cb = ini_parser_cb;
260 ini_parser_param.arg = arg;
261 CG(ini_parser_param) = &ini_parser_param;
262
263 if (zend_ini_prepare_string_for_scanning(str, scanner_mode) == FAILURE) {
264 return FAILURE;
265 }
266
267 CG(ini_parser_unbuffered_errors) = unbuffered_errors;
268 retval = ini_parse();
269
270 shutdown_ini_scanner();
271
272 if (retval == 0) {
273 return SUCCESS;
274 } else {
275 return FAILURE;
276 }
277 }
278 /* }}} */
279
280 %}
281
282 %expect 0
283 %pure-parser
284
285 %token TC_SECTION
286 %token TC_RAW
287 %token TC_CONSTANT
288 %token TC_NUMBER
289 %token TC_STRING
290 %token TC_WHITESPACE
291 %token TC_LABEL
292 %token TC_OFFSET
293 %token TC_DOLLAR_CURLY
294 %token TC_VARNAME
295 %token TC_QUOTED_STRING
296 %token BOOL_TRUE
297 %token BOOL_FALSE
298 %token NULL_NULL
299 %token END_OF_LINE
300 %token '=' ':' ',' '.' '"' '\'' '^' '+' '-' '/' '*' '%' '$' '~' '<' '>' '?' '@' '{' '}'
301 %left '|' '&' '^'
302 %right '~' '!'
303
304 %destructor { zval_ptr_dtor(&$$); } TC_RAW TC_CONSTANT TC_NUMBER TC_STRING TC_WHITESPACE TC_LABEL TC_OFFSET TC_VARNAME BOOL_TRUE BOOL_FALSE NULL_NULL
305
306 %%
307
308 statement_list:
309 statement_list statement
310 | /* empty */
311 ;
312
313 statement:
314 TC_SECTION section_string_or_value ']' {
315 #if DEBUG_CFG_PARSER
316 printf("SECTION: [%s]\n", Z_STRVAL($2));
317 #endif
318 ZEND_INI_PARSER_CB(&$2, NULL, NULL, ZEND_INI_PARSER_SECTION, ZEND_INI_PARSER_ARG);
319 zend_string_release(Z_STR($2));
320 }
321 | TC_LABEL '=' string_or_value {
322 #if DEBUG_CFG_PARSER
323 printf("NORMAL: '%s' = '%s'\n", Z_STRVAL($1), Z_STRVAL($3));
324 #endif
325 ZEND_INI_PARSER_CB(&$1, &$3, NULL, ZEND_INI_PARSER_ENTRY, ZEND_INI_PARSER_ARG);
326 zend_string_release(Z_STR($1));
327 zval_ptr_dtor(&$3);
328 }
329 | TC_OFFSET option_offset ']' '=' string_or_value {
330 #if DEBUG_CFG_PARSER
331 printf("OFFSET: '%s'[%s] = '%s'\n", Z_STRVAL($1), Z_STRVAL($2), Z_STRVAL($5));
332 #endif
333 ZEND_INI_PARSER_CB(&$1, &$5, &$2, ZEND_INI_PARSER_POP_ENTRY, ZEND_INI_PARSER_ARG);
334 zend_string_release(Z_STR($1));
335 if (Z_TYPE($2) == IS_STRING) {
336 zend_string_release(Z_STR($2));
337 } else {
338 zval_dtor(&$2);
339 }
340 zval_ptr_dtor(&$5);
341 }
342 | TC_LABEL { ZEND_INI_PARSER_CB(&$1, NULL, NULL, ZEND_INI_PARSER_ENTRY, ZEND_INI_PARSER_ARG); zend_string_release(Z_STR($1)); }
343 | END_OF_LINE
344 ;
345
346 section_string_or_value:
347 var_string_list_section { $$ = $1; }
348 | /* empty */ { zend_ini_init_string(&$$); }
349 ;
350
351 string_or_value:
352 expr { $$ = $1; }
353 | BOOL_TRUE { $$ = $1; }
354 | BOOL_FALSE { $$ = $1; }
355 | NULL_NULL { $$ = $1; }
356 | END_OF_LINE { zend_ini_init_string(&$$); }
357 ;
358
359 option_offset:
360 var_string_list { $$ = $1; }
361 | /* empty */ { zend_ini_init_string(&$$); }
362 ;
363
364 encapsed_list:
365 encapsed_list cfg_var_ref { zend_ini_add_string(&$$, &$1, &$2); zend_string_free(Z_STR($2)); }
366 | encapsed_list TC_QUOTED_STRING { zend_ini_add_string(&$$, &$1, &$2); zend_string_free(Z_STR($2)); }
367 | /* empty */ { zend_ini_init_string(&$$); }
368 ;
369
370 var_string_list_section:
371 cfg_var_ref { $$ = $1; }
372 | constant_literal { $$ = $1; }
373 | '"' encapsed_list '"' { $$ = $2; }
374 | var_string_list_section cfg_var_ref { zend_ini_add_string(&$$, &$1, &$2); zend_string_free(Z_STR($2)); }
375 | var_string_list_section constant_literal { zend_ini_add_string(&$$, &$1, &$2); zend_string_free(Z_STR($2)); }
376 | var_string_list_section '"' encapsed_list '"' { zend_ini_add_string(&$$, &$1, &$3); zend_string_free(Z_STR($3)); }
377 ;
378
379 var_string_list:
380 cfg_var_ref { $$ = $1; }
381 | constant_string { $$ = $1; }
382 | '"' encapsed_list '"' { $$ = $2; }
383 | var_string_list cfg_var_ref { zend_ini_add_string(&$$, &$1, &$2); zend_string_free(Z_STR($2)); }
384 | var_string_list constant_string { zend_ini_add_string(&$$, &$1, &$2); zend_string_free(Z_STR($2)); }
385 | var_string_list '"' encapsed_list '"' { zend_ini_add_string(&$$, &$1, &$3); zend_string_free(Z_STR($3)); }
386 ;
387
388 expr:
389 var_string_list { $$ = $1; }
390 | expr '|' expr { zend_ini_do_op('|', &$$, &$1, &$3); }
391 | expr '&' expr { zend_ini_do_op('&', &$$, &$1, &$3); }
392 | expr '^' expr { zend_ini_do_op('^', &$$, &$1, &$3); }
393 | '~' expr { zend_ini_do_op('~', &$$, &$2, NULL); }
394 | '!' expr { zend_ini_do_op('!', &$$, &$2, NULL); }
395 | '(' expr ')' { $$ = $2; }
396 ;
397
398 cfg_var_ref:
399 TC_DOLLAR_CURLY TC_VARNAME '}' { zend_ini_get_var(&$$, &$2); zend_string_free(Z_STR($2)); }
400 ;
401
402 constant_literal:
403 TC_CONSTANT { $$ = $1; }
404 | TC_RAW { $$ = $1; /*printf("TC_RAW: '%s'\n", Z_STRVAL($1));*/ }
405 | TC_NUMBER { $$ = $1; /*printf("TC_NUMBER: '%s'\n", Z_STRVAL($1));*/ }
406 | TC_STRING { $$ = $1; /*printf("TC_STRING: '%s'\n", Z_STRVAL($1));*/ }
407 | TC_WHITESPACE { $$ = $1; /*printf("TC_WHITESPACE: '%s'\n", Z_STRVAL($1));*/ }
408 ;
409
410 constant_string:
411 TC_CONSTANT { zend_ini_get_constant(&$$, &$1); }
412 | TC_RAW { $$ = $1; /*printf("TC_RAW: '%s'\n", Z_STRVAL($1));*/ }
413 | TC_NUMBER { $$ = $1; /*printf("TC_NUMBER: '%s'\n", Z_STRVAL($1));*/ }
414 | TC_STRING { $$ = $1; /*printf("TC_STRING: '%s'\n", Z_STRVAL($1));*/ }
415 | TC_WHITESPACE { $$ = $1; /*printf("TC_WHITESPACE: '%s'\n", Z_STRVAL($1));*/ }
416 ;
417
418 /*
419 * Local variables:
420 * tab-width: 4
421 * c-basic-offset: 4
422 * indent-tabs-mode: t
423 * End:
424 * vim600: sw=4 ts=4 fdm=marker
425 * vim<600: sw=4 ts=4
426 */
427