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 ZVAL_NEW_STR(result, zend_string_init(envvar, strlen(envvar), ZEND_SYSTEM_INI));
178 efree(envvar);
179 } else if ((envvar = getenv(Z_STRVAL_P(name))) != NULL) {
180 ZVAL_NEW_STR(result, zend_string_init(envvar, strlen(envvar), ZEND_SYSTEM_INI));
181 } else {
182 zend_ini_init_string(result);
183 }
184 }
185 /* }}} */
186
187 /* {{{ ini_error() */
ini_error(const char * msg)188 static ZEND_COLD void ini_error(const char *msg)
189 {
190 char *error_buf;
191 int error_buf_len;
192 char *currently_parsed_filename;
193
194 currently_parsed_filename = zend_ini_scanner_get_filename();
195 if (currently_parsed_filename) {
196 error_buf_len = 128 + (int)strlen(msg) + (int)strlen(currently_parsed_filename); /* should be more than enough */
197 error_buf = (char *) emalloc(error_buf_len);
198
199 sprintf(error_buf, "%s in %s on line %d\n", msg, currently_parsed_filename, zend_ini_scanner_get_lineno());
200 } else {
201 error_buf = estrdup("Invalid configuration directive\n");
202 }
203
204 if (CG(ini_parser_unbuffered_errors)) {
205 #ifdef ZEND_WIN32
206 syslog(LOG_ALERT, "PHP: %s (%s)", error_buf, GetCommandLine());
207 #endif
208 fprintf(stderr, "PHP: %s", error_buf);
209 } else {
210 zend_error(E_WARNING, "%s", error_buf);
211 }
212 efree(error_buf);
213 }
214 /* }}} */
215
216 /* {{{ 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)217 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)
218 {
219 int retval;
220 zend_ini_parser_param ini_parser_param;
221
222 ini_parser_param.ini_parser_cb = ini_parser_cb;
223 ini_parser_param.arg = arg;
224 CG(ini_parser_param) = &ini_parser_param;
225
226 if (zend_ini_open_file_for_scanning(fh, scanner_mode) == FAILURE) {
227 return FAILURE;
228 }
229
230 CG(ini_parser_unbuffered_errors) = unbuffered_errors;
231 retval = ini_parse();
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() */
zend_parse_ini_string(char * str,bool unbuffered_errors,int scanner_mode,zend_ini_parser_cb_t ini_parser_cb,void * arg)244 ZEND_API zend_result zend_parse_ini_string(char *str, bool unbuffered_errors, int scanner_mode, zend_ini_parser_cb_t ini_parser_cb, void *arg)
245 {
246 int retval;
247 zend_ini_parser_param ini_parser_param;
248
249 ini_parser_param.ini_parser_cb = ini_parser_cb;
250 ini_parser_param.arg = arg;
251 CG(ini_parser_param) = &ini_parser_param;
252
253 if (zend_ini_prepare_string_for_scanning(str, scanner_mode) == FAILURE) {
254 return FAILURE;
255 }
256
257 CG(ini_parser_unbuffered_errors) = unbuffered_errors;
258 retval = ini_parse();
259
260 shutdown_ini_scanner();
261
262 if (retval == 0) {
263 return SUCCESS;
264 } else {
265 return FAILURE;
266 }
267 }
268 /* }}} */
269
270 /* {{{ zval_ini_dtor() */
zval_ini_dtor(zval * zv)271 static void zval_ini_dtor(zval *zv)
272 {
273 if (Z_TYPE_P(zv) == IS_STRING) {
274 if (ZEND_SYSTEM_INI) {
275 GC_MAKE_PERSISTENT_LOCAL(Z_STR_P(zv));
276 }
277 zend_string_release(Z_STR_P(zv));
278 }
279 }
280 /* }}} */
281
282 %}
283
284 %expect 0
285 %define api.prefix {ini_}
286 %define api.pure full
287 %define api.value.type {zval}
288 %define parse.error verbose
289
290 %token END 0 "end of file"
291 %token TC_SECTION
292 %token TC_RAW
293 %token TC_CONSTANT
294 %token TC_NUMBER
295 %token TC_STRING
296 %token TC_WHITESPACE
297 %token TC_LABEL
298 %token TC_OFFSET
299 %token TC_DOLLAR_CURLY
300 %token TC_VARNAME
301 %token TC_QUOTED_STRING
302 %token BOOL_TRUE
303 %token BOOL_FALSE
304 %token NULL_NULL
305 %token END_OF_LINE
306 %token '=' ':' ',' '.' '"' '\'' '^' '+' '-' '/' '*' '%' '$' '~' '<' '>' '?' '@' '{' '}'
307 %left '|' '&' '^'
308 %precedence '~' '!'
309
310 %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
311
312 %%
313
314 statement_list:
315 statement_list statement
316 | %empty { (void) ini_nerrs; }
317 ;
318
319 statement:
320 TC_SECTION section_string_or_value ']' {
321 #if DEBUG_CFG_PARSER
322 printf("SECTION: [%s]\n", Z_STRVAL($2));
323 #endif
324 ZEND_INI_PARSER_CB(&$2, NULL, NULL, ZEND_INI_PARSER_SECTION, ZEND_INI_PARSER_ARG);
325 zend_string_release(Z_STR($2));
326 }
327 | TC_LABEL '=' string_or_value {
328 #if DEBUG_CFG_PARSER
329 printf("NORMAL: '%s' = '%s'\n", Z_STRVAL($1), Z_STRVAL($3));
330 #endif
331 ZEND_INI_PARSER_CB(&$1, &$3, NULL, ZEND_INI_PARSER_ENTRY, ZEND_INI_PARSER_ARG);
332 if (ZEND_SYSTEM_INI) {
333 GC_MAKE_PERSISTENT_LOCAL(Z_STR($1));
334 }
335 zend_string_release(Z_STR($1));
336 zval_ini_dtor(&$3);
337 }
338 | TC_OFFSET option_offset ']' '=' string_or_value {
339 #if DEBUG_CFG_PARSER
340 printf("OFFSET: '%s'[%s] = '%s'\n", Z_STRVAL($1), Z_STRVAL($2), Z_STRVAL($5));
341 #endif
342 ZEND_INI_PARSER_CB(&$1, &$5, &$2, ZEND_INI_PARSER_POP_ENTRY, ZEND_INI_PARSER_ARG);
343 zend_string_release(Z_STR($1));
344 zval_ini_dtor(&$2);
345 zval_ini_dtor(&$5);
346 }
347 | TC_LABEL { ZEND_INI_PARSER_CB(&$1, NULL, NULL, ZEND_INI_PARSER_ENTRY, ZEND_INI_PARSER_ARG); zend_string_release(Z_STR($1)); }
348 | END_OF_LINE
349 ;
350
351 section_string_or_value:
352 var_string_list_section { $$ = $1; }
353 | %empty { zend_ini_init_string(&$$); }
354 ;
355
356 string_or_value:
357 expr { $$ = $1; }
358 | BOOL_TRUE { $$ = $1; }
359 | BOOL_FALSE { $$ = $1; }
360 | NULL_NULL { $$ = $1; }
361 | END_OF_LINE { zend_ini_init_string(&$$); }
362 ;
363
364 option_offset:
365 var_string_list { $$ = $1; }
366 | %empty { zend_ini_init_string(&$$); }
367 ;
368
369 encapsed_list:
370 encapsed_list cfg_var_ref { zend_ini_add_string(&$$, &$1, &$2); zend_string_free(Z_STR($2)); }
371 | encapsed_list TC_QUOTED_STRING { zend_ini_add_string(&$$, &$1, &$2); zend_string_free(Z_STR($2)); }
372 | %empty { zend_ini_init_string(&$$); }
373 ;
374
375 var_string_list_section:
376 cfg_var_ref { $$ = $1; }
377 | constant_literal { $$ = $1; }
378 | '"' encapsed_list '"' { $$ = $2; }
379 | var_string_list_section cfg_var_ref { zend_ini_add_string(&$$, &$1, &$2); zend_string_free(Z_STR($2)); }
380 | var_string_list_section constant_literal { zend_ini_add_string(&$$, &$1, &$2); zend_string_free(Z_STR($2)); }
381 | var_string_list_section '"' encapsed_list '"' { zend_ini_add_string(&$$, &$1, &$3); zend_string_free(Z_STR($3)); }
382 ;
383
384 var_string_list:
385 cfg_var_ref { $$ = $1; }
386 | constant_string { $$ = $1; }
387 | '"' encapsed_list '"' { $$ = $2; }
388 | var_string_list cfg_var_ref { zend_ini_add_string(&$$, &$1, &$2); zend_string_free(Z_STR($2)); }
389 | var_string_list constant_string { zend_ini_add_string(&$$, &$1, &$2); zend_string_free(Z_STR($2)); }
390 | var_string_list '"' encapsed_list '"' { zend_ini_add_string(&$$, &$1, &$3); zend_string_free(Z_STR($3)); }
391 ;
392
393 expr:
394 var_string_list { $$ = $1; }
395 | expr '|' expr { zend_ini_do_op('|', &$$, &$1, &$3); }
396 | expr '&' expr { zend_ini_do_op('&', &$$, &$1, &$3); }
397 | expr '^' expr { zend_ini_do_op('^', &$$, &$1, &$3); }
398 | '~' expr { zend_ini_do_op('~', &$$, &$2, NULL); }
399 | '!' expr { zend_ini_do_op('!', &$$, &$2, NULL); }
400 | '(' expr ')' { $$ = $2; }
401 ;
402
403 cfg_var_ref:
404 TC_DOLLAR_CURLY TC_VARNAME '}' { zend_ini_get_var(&$$, &$2); zend_string_free(Z_STR($2)); }
405 ;
406
407 constant_literal:
408 TC_CONSTANT { $$ = $1; }
409 | TC_RAW { $$ = $1; /*printf("TC_RAW: '%s'\n", Z_STRVAL($1));*/ }
410 | TC_NUMBER { $$ = $1; /*printf("TC_NUMBER: '%s'\n", Z_STRVAL($1));*/ }
411 | TC_STRING { $$ = $1; /*printf("TC_STRING: '%s'\n", Z_STRVAL($1));*/ }
412 | TC_WHITESPACE { $$ = $1; /*printf("TC_WHITESPACE: '%s'\n", Z_STRVAL($1));*/ }
413 ;
414
415 constant_string:
416 TC_CONSTANT { zend_ini_get_constant(&$$, &$1); }
417 | TC_RAW { $$ = $1; /*printf("TC_RAW: '%s'\n", Z_STRVAL($1));*/ }
418 | TC_NUMBER { $$ = $1; /*printf("TC_NUMBER: '%s'\n", Z_STRVAL($1));*/ }
419 | TC_STRING { $$ = $1; /*printf("TC_STRING: '%s'\n", Z_STRVAL($1));*/ }
420 | TC_WHITESPACE { $$ = $1; /*printf("TC_WHITESPACE: '%s'\n", Z_STRVAL($1));*/ }
421 ;
422