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 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
get_int_val(zval * op)47 static int get_int_val(zval *op) {
48 switch (Z_TYPE_P(op)) {
49 case IS_LONG:
50 return Z_LVAL_P(op);
51 case IS_DOUBLE:
52 return (int)Z_DVAL_P(op);
53 case IS_STRING:
54 {
55 int val = atoi(Z_STRVAL_P(op));
56 zend_string_free(Z_STR_P(op));
57 return val;
58 }
59 EMPTY_SWITCH_DEFAULT_CASE()
60 }
61 }
62
63 /* {{{ zend_ini_do_op() */
zend_ini_do_op(char type,zval * result,zval * op1,zval * op2)64 static void zend_ini_do_op(char type, zval *result, zval *op1, zval *op2)
65 {
66 int i_result;
67 int i_op1, i_op2;
68 int str_len;
69 char str_result[MAX_LENGTH_OF_LONG+1];
70
71 i_op1 = get_int_val(op1);
72 i_op2 = op2 ? get_int_val(op2) : 0;
73
74 switch (type) {
75 case '|':
76 i_result = i_op1 | i_op2;
77 break;
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;
86 break;
87 case '!':
88 i_result = !i_op1;
89 break;
90 default:
91 i_result = 0;
92 break;
93 }
94
95 str_len = sprintf(str_result, "%d", i_result);
96 ZVAL_NEW_STR(result, zend_string_init(str_result, str_len, ZEND_SYSTEM_INI));
97 }
98 /* }}} */
99
100 /* {{{ zend_ini_init_string() */
zend_ini_init_string(zval * result)101 static void zend_ini_init_string(zval *result)
102 {
103 if (ZEND_SYSTEM_INI) {
104 ZVAL_EMPTY_PSTRING(result);
105 } else {
106 ZVAL_EMPTY_STRING(result);
107 }
108 }
109 /* }}} */
110
111 /* {{{ zend_ini_add_string() */
zend_ini_add_string(zval * result,zval * op1,zval * op2)112 static void zend_ini_add_string(zval *result, zval *op1, zval *op2)
113 {
114 int length, op1_len;
115
116 if (Z_TYPE_P(op1) != IS_STRING) {
117 /* ZEND_ASSERT(!Z_REFCOUNTED_P(op1)); */
118 if (ZEND_SYSTEM_INI) {
119 zend_string *tmp_str;
120 zend_string *str = zval_get_tmp_string(op1, &tmp_str);
121 ZVAL_PSTRINGL(op1, ZSTR_VAL(str), ZSTR_LEN(str));
122 zend_tmp_string_release(tmp_str);
123 } else {
124 ZVAL_STR(op1, zval_get_string_func(op1));
125 }
126 }
127 op1_len = (int)Z_STRLEN_P(op1);
128
129 if (Z_TYPE_P(op2) != IS_STRING) {
130 convert_to_string(op2);
131 }
132 length = op1_len + (int)Z_STRLEN_P(op2);
133
134 ZVAL_NEW_STR(result, zend_string_extend(Z_STR_P(op1), length, ZEND_SYSTEM_INI));
135 memcpy(Z_STRVAL_P(result) + op1_len, Z_STRVAL_P(op2), Z_STRLEN_P(op2) + 1);
136 }
137 /* }}} */
138
139 /* {{{ zend_ini_get_constant() */
zend_ini_get_constant(zval * result,zval * name)140 static void zend_ini_get_constant(zval *result, zval *name)
141 {
142 zval *c, tmp;
143
144 /* If name contains ':' it is not a constant. Bug #26893. */
145 if (!memchr(Z_STRVAL_P(name), ':', Z_STRLEN_P(name))
146 && (c = zend_get_constant(Z_STR_P(name))) != 0) {
147 if (Z_TYPE_P(c) != IS_STRING) {
148 ZVAL_COPY_OR_DUP(&tmp, c);
149 if (Z_OPT_CONSTANT(tmp)) {
150 zval_update_constant_ex(&tmp, NULL);
151 }
152 convert_to_string(&tmp);
153 c = &tmp;
154 }
155 ZVAL_NEW_STR(result, zend_string_init(Z_STRVAL_P(c), Z_STRLEN_P(c), ZEND_SYSTEM_INI));
156 if (c == &tmp) {
157 zend_string_release(Z_STR(tmp));
158 }
159 zend_string_free(Z_STR_P(name));
160 } else {
161 *result = *name;
162 }
163 }
164 /* }}} */
165
166 /* {{{ zend_ini_get_var() */
zend_ini_get_var(zval * result,zval * name)167 static void zend_ini_get_var(zval *result, zval *name)
168 {
169 zval *curval;
170 char *envvar;
171
172 /* Fetch configuration option value */
173 if ((curval = zend_get_configuration_directive(Z_STR_P(name))) != NULL) {
174 ZVAL_NEW_STR(result, zend_string_init(Z_STRVAL_P(curval), Z_STRLEN_P(curval), ZEND_SYSTEM_INI));
175 /* ..or if not found, try ENV */
176 } else if ((envvar = zend_getenv(Z_STRVAL_P(name), Z_STRLEN_P(name))) != NULL ||
177 (envvar = getenv(Z_STRVAL_P(name))) != NULL) {
178 ZVAL_NEW_STR(result, zend_string_init(envvar, strlen(envvar), ZEND_SYSTEM_INI));
179 } else {
180 zend_ini_init_string(result);
181 }
182 }
183 /* }}} */
184
185 /* {{{ ini_error() */
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() */
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)215 ZEND_API zend_result 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 {
217 int retval;
218 zend_ini_parser_param ini_parser_param;
219
220 ini_parser_param.ini_parser_cb = ini_parser_cb;
221 ini_parser_param.arg = arg;
222 CG(ini_parser_param) = &ini_parser_param;
223
224 if (zend_ini_open_file_for_scanning(fh, scanner_mode) == FAILURE) {
225 return FAILURE;
226 }
227
228 CG(ini_parser_unbuffered_errors) = unbuffered_errors;
229 retval = ini_parse();
230 zend_file_handle_dtor(fh);
231
232 shutdown_ini_scanner();
233
234 if (retval == 0) {
235 return SUCCESS;
236 } else {
237 return FAILURE;
238 }
239 }
240 /* }}} */
241
242 /* {{{ zend_parse_ini_string() */
zend_parse_ini_string(char * str,zend_bool unbuffered_errors,int scanner_mode,zend_ini_parser_cb_t ini_parser_cb,void * arg)243 ZEND_API zend_result zend_parse_ini_string(char *str, zend_bool unbuffered_errors, int scanner_mode, zend_ini_parser_cb_t ini_parser_cb, void *arg)
244 {
245 int retval;
246 zend_ini_parser_param ini_parser_param;
247
248 ini_parser_param.ini_parser_cb = ini_parser_cb;
249 ini_parser_param.arg = arg;
250 CG(ini_parser_param) = &ini_parser_param;
251
252 if (zend_ini_prepare_string_for_scanning(str, scanner_mode) == FAILURE) {
253 return FAILURE;
254 }
255
256 CG(ini_parser_unbuffered_errors) = unbuffered_errors;
257 retval = ini_parse();
258
259 shutdown_ini_scanner();
260
261 if (retval == 0) {
262 return SUCCESS;
263 } else {
264 return FAILURE;
265 }
266 }
267 /* }}} */
268
269 /* {{{ zval_ini_dtor() */
zval_ini_dtor(zval * zv)270 static void zval_ini_dtor(zval *zv)
271 {
272 if (Z_TYPE_P(zv) == IS_STRING) {
273 zend_string_release(Z_STR_P(zv));
274 }
275 }
276 /* }}} */
277
278 %}
279
280 %expect 0
281 %define api.prefix {ini_}
282 %define api.pure full
283 %define api.value.type {zval}
284 %define parse.error verbose
285
286 %token END 0 "end of file"
287 %token TC_SECTION
288 %token TC_RAW
289 %token TC_CONSTANT
290 %token TC_NUMBER
291 %token TC_STRING
292 %token TC_WHITESPACE
293 %token TC_LABEL
294 %token TC_OFFSET
295 %token TC_DOLLAR_CURLY
296 %token TC_VARNAME
297 %token TC_QUOTED_STRING
298 %token BOOL_TRUE
299 %token BOOL_FALSE
300 %token NULL_NULL
301 %token END_OF_LINE
302 %token '=' ':' ',' '.' '"' '\'' '^' '+' '-' '/' '*' '%' '$' '~' '<' '>' '?' '@' '{' '}'
303 %left '|' '&' '^'
304 %precedence '~' '!'
305
306 %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
307
308 %%
309
310 statement_list:
311 statement_list statement
312 | %empty
313 ;
314
315 statement:
316 TC_SECTION section_string_or_value ']' {
317 #if DEBUG_CFG_PARSER
318 printf("SECTION: [%s]\n", Z_STRVAL($2));
319 #endif
320 ZEND_INI_PARSER_CB(&$2, NULL, NULL, ZEND_INI_PARSER_SECTION, ZEND_INI_PARSER_ARG);
321 zend_string_release(Z_STR($2));
322 }
323 | TC_LABEL '=' string_or_value {
324 #if DEBUG_CFG_PARSER
325 printf("NORMAL: '%s' = '%s'\n", Z_STRVAL($1), Z_STRVAL($3));
326 #endif
327 ZEND_INI_PARSER_CB(&$1, &$3, NULL, ZEND_INI_PARSER_ENTRY, ZEND_INI_PARSER_ARG);
328 zend_string_release(Z_STR($1));
329 zval_ini_dtor(&$3);
330 }
331 | TC_OFFSET option_offset ']' '=' string_or_value {
332 #if DEBUG_CFG_PARSER
333 printf("OFFSET: '%s'[%s] = '%s'\n", Z_STRVAL($1), Z_STRVAL($2), Z_STRVAL($5));
334 #endif
335 ZEND_INI_PARSER_CB(&$1, &$5, &$2, ZEND_INI_PARSER_POP_ENTRY, ZEND_INI_PARSER_ARG);
336 zend_string_release(Z_STR($1));
337 zval_ini_dtor(&$2);
338 zval_ini_dtor(&$5);
339 }
340 | TC_LABEL { ZEND_INI_PARSER_CB(&$1, NULL, NULL, ZEND_INI_PARSER_ENTRY, ZEND_INI_PARSER_ARG); zend_string_release(Z_STR($1)); }
341 | END_OF_LINE
342 ;
343
344 section_string_or_value:
345 var_string_list_section { $$ = $1; }
346 | %empty { zend_ini_init_string(&$$); }
347 ;
348
349 string_or_value:
350 expr { $$ = $1; }
351 | BOOL_TRUE { $$ = $1; }
352 | BOOL_FALSE { $$ = $1; }
353 | NULL_NULL { $$ = $1; }
354 | END_OF_LINE { zend_ini_init_string(&$$); }
355 ;
356
357 option_offset:
358 var_string_list { $$ = $1; }
359 | %empty { zend_ini_init_string(&$$); }
360 ;
361
362 encapsed_list:
363 encapsed_list cfg_var_ref { zend_ini_add_string(&$$, &$1, &$2); zend_string_free(Z_STR($2)); }
364 | encapsed_list TC_QUOTED_STRING { zend_ini_add_string(&$$, &$1, &$2); zend_string_free(Z_STR($2)); }
365 | %empty { zend_ini_init_string(&$$); }
366 ;
367
368 var_string_list_section:
369 cfg_var_ref { $$ = $1; }
370 | constant_literal { $$ = $1; }
371 | '"' encapsed_list '"' { $$ = $2; }
372 | var_string_list_section cfg_var_ref { zend_ini_add_string(&$$, &$1, &$2); zend_string_free(Z_STR($2)); }
373 | var_string_list_section constant_literal { zend_ini_add_string(&$$, &$1, &$2); zend_string_free(Z_STR($2)); }
374 | var_string_list_section '"' encapsed_list '"' { zend_ini_add_string(&$$, &$1, &$3); zend_string_free(Z_STR($3)); }
375 ;
376
377 var_string_list:
378 cfg_var_ref { $$ = $1; }
379 | constant_string { $$ = $1; }
380 | '"' encapsed_list '"' { $$ = $2; }
381 | var_string_list cfg_var_ref { zend_ini_add_string(&$$, &$1, &$2); zend_string_free(Z_STR($2)); }
382 | var_string_list constant_string { zend_ini_add_string(&$$, &$1, &$2); zend_string_free(Z_STR($2)); }
383 | var_string_list '"' encapsed_list '"' { zend_ini_add_string(&$$, &$1, &$3); zend_string_free(Z_STR($3)); }
384 ;
385
386 expr:
387 var_string_list { $$ = $1; }
388 | expr '|' expr { zend_ini_do_op('|', &$$, &$1, &$3); }
389 | expr '&' expr { zend_ini_do_op('&', &$$, &$1, &$3); }
390 | expr '^' expr { zend_ini_do_op('^', &$$, &$1, &$3); }
391 | '~' expr { zend_ini_do_op('~', &$$, &$2, NULL); }
392 | '!' expr { zend_ini_do_op('!', &$$, &$2, NULL); }
393 | '(' expr ')' { $$ = $2; }
394 ;
395
396 cfg_var_ref:
397 TC_DOLLAR_CURLY TC_VARNAME '}' { zend_ini_get_var(&$$, &$2); zend_string_free(Z_STR($2)); }
398 ;
399
400 constant_literal:
401 TC_CONSTANT { $$ = $1; }
402 | TC_RAW { $$ = $1; /*printf("TC_RAW: '%s'\n", Z_STRVAL($1));*/ }
403 | TC_NUMBER { $$ = $1; /*printf("TC_NUMBER: '%s'\n", Z_STRVAL($1));*/ }
404 | TC_STRING { $$ = $1; /*printf("TC_STRING: '%s'\n", Z_STRVAL($1));*/ }
405 | TC_WHITESPACE { $$ = $1; /*printf("TC_WHITESPACE: '%s'\n", Z_STRVAL($1));*/ }
406 ;
407
408 constant_string:
409 TC_CONSTANT { zend_ini_get_constant(&$$, &$1); }
410 | TC_RAW { $$ = $1; /*printf("TC_RAW: '%s'\n", Z_STRVAL($1));*/ }
411 | TC_NUMBER { $$ = $1; /*printf("TC_NUMBER: '%s'\n", Z_STRVAL($1));*/ }
412 | TC_STRING { $$ = $1; /*printf("TC_STRING: '%s'\n", Z_STRVAL($1));*/ }
413 | TC_WHITESPACE { $$ = $1; /*printf("TC_WHITESPACE: '%s'\n", Z_STRVAL($1));*/ }
414 ;
415