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 #define YYSTYPE zval
36
37 int ini_parse(void);
38
39 #define ZEND_INI_PARSER_CB (CG(ini_parser_param))->ini_parser_cb
40 #define ZEND_INI_PARSER_ARG (CG(ini_parser_param))->arg
41
42 #ifdef _MSC_VER
43 #define YYMALLOC malloc
44 #define YYFREE free
45 #endif
46
47 #define ZEND_SYSTEM_INI CG(ini_parser_unbuffered_errors)
48
get_int_val(zval * op)49 static int get_int_val(zval *op) {
50 switch (Z_TYPE_P(op)) {
51 case IS_LONG:
52 return Z_LVAL_P(op);
53 case IS_DOUBLE:
54 return (int)Z_DVAL_P(op);
55 case IS_STRING:
56 {
57 int val = atoi(Z_STRVAL_P(op));
58 zend_string_free(Z_STR_P(op));
59 return val;
60 }
61 EMPTY_SWITCH_DEFAULT_CASE()
62 }
63 }
64
65 /* {{{ zend_ini_do_op()
66 */
zend_ini_do_op(char type,zval * result,zval * op1,zval * op2)67 static void zend_ini_do_op(char type, zval *result, zval *op1, zval *op2)
68 {
69 int i_result;
70 int i_op1, i_op2;
71 int str_len;
72 char str_result[MAX_LENGTH_OF_LONG+1];
73
74 i_op1 = get_int_val(op1);
75 i_op2 = op2 ? get_int_val(op2) : 0;
76
77 switch (type) {
78 case '|':
79 i_result = i_op1 | i_op2;
80 break;
81 case '&':
82 i_result = i_op1 & i_op2;
83 break;
84 case '^':
85 i_result = i_op1 ^ i_op2;
86 break;
87 case '~':
88 i_result = ~i_op1;
89 break;
90 case '!':
91 i_result = !i_op1;
92 break;
93 default:
94 i_result = 0;
95 break;
96 }
97
98 str_len = sprintf(str_result, "%d", i_result);
99 ZVAL_NEW_STR(result, zend_string_init(str_result, str_len, ZEND_SYSTEM_INI));
100 }
101 /* }}} */
102
103 /* {{{ zend_ini_init_string()
104 */
zend_ini_init_string(zval * result)105 static void zend_ini_init_string(zval *result)
106 {
107 if (ZEND_SYSTEM_INI) {
108 ZVAL_EMPTY_PSTRING(result);
109 } else {
110 ZVAL_EMPTY_STRING(result);
111 }
112 }
113 /* }}} */
114
115 /* {{{ zend_ini_add_string()
116 */
zend_ini_add_string(zval * result,zval * op1,zval * op2)117 static void zend_ini_add_string(zval *result, zval *op1, zval *op2)
118 {
119 int length, op1_len;
120
121 if (Z_TYPE_P(op1) != IS_STRING) {
122 /* ZEND_ASSERT(!Z_REFCOUNTED_P(op1)); */
123 if (ZEND_SYSTEM_INI) {
124 zend_string *tmp_str;
125 zend_string *str = zval_get_tmp_string(op1, &tmp_str);
126 ZVAL_PSTRINGL(op1, ZSTR_VAL(str), ZSTR_LEN(str));
127 zend_tmp_string_release(tmp_str);
128 } else {
129 ZVAL_STR(op1, zval_get_string_func(op1));
130 }
131 }
132 op1_len = (int)Z_STRLEN_P(op1);
133
134 if (Z_TYPE_P(op2) != IS_STRING) {
135 convert_to_string(op2);
136 }
137 length = op1_len + (int)Z_STRLEN_P(op2);
138
139 ZVAL_NEW_STR(result, zend_string_extend(Z_STR_P(op1), length, ZEND_SYSTEM_INI));
140 memcpy(Z_STRVAL_P(result) + op1_len, Z_STRVAL_P(op2), Z_STRLEN_P(op2) + 1);
141 }
142 /* }}} */
143
144 /* {{{ zend_ini_get_constant()
145 */
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()
173 */
zend_ini_get_var(zval * result,zval * name)174 static void zend_ini_get_var(zval *result, zval *name)
175 {
176 zval *curval;
177 char *envvar;
178
179 /* Fetch configuration option value */
180 if ((curval = zend_get_configuration_directive(Z_STR_P(name))) != NULL) {
181 ZVAL_NEW_STR(result, zend_string_init(Z_STRVAL_P(curval), Z_STRLEN_P(curval), ZEND_SYSTEM_INI));
182 /* ..or if not found, try ENV */
183 } else if ((envvar = zend_getenv(Z_STRVAL_P(name), Z_STRLEN_P(name))) != NULL ||
184 (envvar = getenv(Z_STRVAL_P(name))) != NULL) {
185 ZVAL_NEW_STR(result, zend_string_init(envvar, strlen(envvar), ZEND_SYSTEM_INI));
186 } else {
187 zend_ini_init_string(result);
188 }
189 }
190 /* }}} */
191
192 /* {{{ ini_error()
193 */
ini_error(const char * msg)194 static ZEND_COLD void ini_error(const char *msg)
195 {
196 char *error_buf;
197 int error_buf_len;
198 char *currently_parsed_filename;
199
200 currently_parsed_filename = zend_ini_scanner_get_filename();
201 if (currently_parsed_filename) {
202 error_buf_len = 128 + (int)strlen(msg) + (int)strlen(currently_parsed_filename); /* should be more than enough */
203 error_buf = (char *) emalloc(error_buf_len);
204
205 sprintf(error_buf, "%s in %s on line %d\n", msg, currently_parsed_filename, zend_ini_scanner_get_lineno());
206 } else {
207 error_buf = estrdup("Invalid configuration directive\n");
208 }
209
210 if (CG(ini_parser_unbuffered_errors)) {
211 #ifdef ZEND_WIN32
212 syslog(LOG_ALERT, "PHP: %s (%s)", error_buf, GetCommandLine());
213 #endif
214 fprintf(stderr, "PHP: %s", error_buf);
215 } else {
216 zend_error(E_WARNING, "%s", error_buf);
217 }
218 efree(error_buf);
219 }
220 /* }}} */
221
222 /* {{{ zend_parse_ini_file()
223 */
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)224 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)
225 {
226 int retval;
227 zend_ini_parser_param ini_parser_param;
228
229 ini_parser_param.ini_parser_cb = ini_parser_cb;
230 ini_parser_param.arg = arg;
231 CG(ini_parser_param) = &ini_parser_param;
232
233 if (zend_ini_open_file_for_scanning(fh, scanner_mode) == FAILURE) {
234 return FAILURE;
235 }
236
237 CG(ini_parser_unbuffered_errors) = unbuffered_errors;
238 retval = ini_parse();
239 zend_file_handle_dtor(fh);
240
241 shutdown_ini_scanner();
242
243 if (retval == 0) {
244 return SUCCESS;
245 } else {
246 return FAILURE;
247 }
248 }
249 /* }}} */
250
251 /* {{{ zend_parse_ini_string()
252 */
zend_parse_ini_string(char * str,zend_bool unbuffered_errors,int scanner_mode,zend_ini_parser_cb_t ini_parser_cb,void * arg)253 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)
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()
280 */
zval_ini_dtor(zval * zv)281 static void zval_ini_dtor(zval *zv)
282 {
283 if (Z_TYPE_P(zv) == IS_STRING) {
284 zend_string_release(Z_STR_P(zv));
285 }
286 }
287 /* }}} */
288
289 %}
290
291 %expect 0
292 %define api.pure full
293 %define parse.error verbose
294
295 %token END 0 "end of file"
296 %token TC_SECTION
297 %token TC_RAW
298 %token TC_CONSTANT
299 %token TC_NUMBER
300 %token TC_STRING
301 %token TC_WHITESPACE
302 %token TC_LABEL
303 %token TC_OFFSET
304 %token TC_DOLLAR_CURLY
305 %token TC_VARNAME
306 %token TC_QUOTED_STRING
307 %token BOOL_TRUE
308 %token BOOL_FALSE
309 %token NULL_NULL
310 %token END_OF_LINE
311 %token '=' ':' ',' '.' '"' '\'' '^' '+' '-' '/' '*' '%' '$' '~' '<' '>' '?' '@' '{' '}'
312 %left '|' '&' '^'
313 %right '~' '!'
314
315 %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
316
317 %%
318
319 statement_list:
320 statement_list statement
321 | /* empty */
322 ;
323
324 statement:
325 TC_SECTION section_string_or_value ']' {
326 #if DEBUG_CFG_PARSER
327 printf("SECTION: [%s]\n", Z_STRVAL($2));
328 #endif
329 ZEND_INI_PARSER_CB(&$2, NULL, NULL, ZEND_INI_PARSER_SECTION, ZEND_INI_PARSER_ARG);
330 zend_string_release(Z_STR($2));
331 }
332 | TC_LABEL '=' string_or_value {
333 #if DEBUG_CFG_PARSER
334 printf("NORMAL: '%s' = '%s'\n", Z_STRVAL($1), Z_STRVAL($3));
335 #endif
336 ZEND_INI_PARSER_CB(&$1, &$3, NULL, ZEND_INI_PARSER_ENTRY, ZEND_INI_PARSER_ARG);
337 zend_string_release(Z_STR($1));
338 zval_ini_dtor(&$3);
339 }
340 | TC_OFFSET option_offset ']' '=' string_or_value {
341 #if DEBUG_CFG_PARSER
342 printf("OFFSET: '%s'[%s] = '%s'\n", Z_STRVAL($1), Z_STRVAL($2), Z_STRVAL($5));
343 #endif
344 ZEND_INI_PARSER_CB(&$1, &$5, &$2, ZEND_INI_PARSER_POP_ENTRY, ZEND_INI_PARSER_ARG);
345 zend_string_release(Z_STR($1));
346 zval_ini_dtor(&$2);
347 zval_ini_dtor(&$5);
348 }
349 | TC_LABEL { ZEND_INI_PARSER_CB(&$1, NULL, NULL, ZEND_INI_PARSER_ENTRY, ZEND_INI_PARSER_ARG); zend_string_release(Z_STR($1)); }
350 | END_OF_LINE
351 ;
352
353 section_string_or_value:
354 var_string_list_section { $$ = $1; }
355 | /* empty */ { zend_ini_init_string(&$$); }
356 ;
357
358 string_or_value:
359 expr { $$ = $1; }
360 | BOOL_TRUE { $$ = $1; }
361 | BOOL_FALSE { $$ = $1; }
362 | NULL_NULL { $$ = $1; }
363 | END_OF_LINE { zend_ini_init_string(&$$); }
364 ;
365
366 option_offset:
367 var_string_list { $$ = $1; }
368 | /* empty */ { zend_ini_init_string(&$$); }
369 ;
370
371 encapsed_list:
372 encapsed_list cfg_var_ref { zend_ini_add_string(&$$, &$1, &$2); zend_string_free(Z_STR($2)); }
373 | encapsed_list TC_QUOTED_STRING { zend_ini_add_string(&$$, &$1, &$2); zend_string_free(Z_STR($2)); }
374 | /* empty */ { zend_ini_init_string(&$$); }
375 ;
376
377 var_string_list_section:
378 cfg_var_ref { $$ = $1; }
379 | constant_literal { $$ = $1; }
380 | '"' encapsed_list '"' { $$ = $2; }
381 | var_string_list_section cfg_var_ref { zend_ini_add_string(&$$, &$1, &$2); zend_string_free(Z_STR($2)); }
382 | var_string_list_section constant_literal { zend_ini_add_string(&$$, &$1, &$2); zend_string_free(Z_STR($2)); }
383 | var_string_list_section '"' encapsed_list '"' { zend_ini_add_string(&$$, &$1, &$3); zend_string_free(Z_STR($3)); }
384 ;
385
386 var_string_list:
387 cfg_var_ref { $$ = $1; }
388 | constant_string { $$ = $1; }
389 | '"' encapsed_list '"' { $$ = $2; }
390 | var_string_list cfg_var_ref { zend_ini_add_string(&$$, &$1, &$2); zend_string_free(Z_STR($2)); }
391 | var_string_list constant_string { zend_ini_add_string(&$$, &$1, &$2); zend_string_free(Z_STR($2)); }
392 | var_string_list '"' encapsed_list '"' { zend_ini_add_string(&$$, &$1, &$3); zend_string_free(Z_STR($3)); }
393 ;
394
395 expr:
396 var_string_list { $$ = $1; }
397 | expr '|' expr { zend_ini_do_op('|', &$$, &$1, &$3); }
398 | expr '&' expr { zend_ini_do_op('&', &$$, &$1, &$3); }
399 | expr '^' expr { zend_ini_do_op('^', &$$, &$1, &$3); }
400 | '~' expr { zend_ini_do_op('~', &$$, &$2, NULL); }
401 | '!' expr { zend_ini_do_op('!', &$$, &$2, NULL); }
402 | '(' expr ')' { $$ = $2; }
403 ;
404
405 cfg_var_ref:
406 TC_DOLLAR_CURLY TC_VARNAME '}' { zend_ini_get_var(&$$, &$2); zend_string_free(Z_STR($2)); }
407 ;
408
409 constant_literal:
410 TC_CONSTANT { $$ = $1; }
411 | TC_RAW { $$ = $1; /*printf("TC_RAW: '%s'\n", Z_STRVAL($1));*/ }
412 | TC_NUMBER { $$ = $1; /*printf("TC_NUMBER: '%s'\n", Z_STRVAL($1));*/ }
413 | TC_STRING { $$ = $1; /*printf("TC_STRING: '%s'\n", Z_STRVAL($1));*/ }
414 | TC_WHITESPACE { $$ = $1; /*printf("TC_WHITESPACE: '%s'\n", Z_STRVAL($1));*/ }
415 ;
416
417 constant_string:
418 TC_CONSTANT { zend_ini_get_constant(&$$, &$1); }
419 | TC_RAW { $$ = $1; /*printf("TC_RAW: '%s'\n", Z_STRVAL($1));*/ }
420 | TC_NUMBER { $$ = $1; /*printf("TC_NUMBER: '%s'\n", Z_STRVAL($1));*/ }
421 | TC_STRING { $$ = $1; /*printf("TC_STRING: '%s'\n", Z_STRVAL($1));*/ }
422 | TC_WHITESPACE { $$ = $1; /*printf("TC_WHITESPACE: '%s'\n", Z_STRVAL($1));*/ }
423 ;
424