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