Annotation of embedaddon/php/ext/standard/assert.c, revision 1.1.1.1
1.1 misho 1: /*
2: +----------------------------------------------------------------------+
3: | PHP Version 5 |
4: +----------------------------------------------------------------------+
5: | Copyright (c) 1997-2012 The PHP Group |
6: +----------------------------------------------------------------------+
7: | This source file is subject to version 3.01 of the PHP license, |
8: | that is bundled with this package in the file LICENSE, and is |
9: | available through the world-wide-web at the following url: |
10: | http://www.php.net/license/3_01.txt |
11: | If you did not receive a copy of the PHP license and are unable to |
12: | obtain it through the world-wide-web, please send a note to |
13: | license@php.net so we can mail you a copy immediately. |
14: +----------------------------------------------------------------------+
15: | Author: Thies C. Arntzen <thies@thieso.net> |
16: +----------------------------------------------------------------------+
17: */
18:
19: /* $Id: assert.c 321634 2012-01-01 13:15:04Z felipe $ */
20:
21: /* {{{ includes */
22: #include "php.h"
23: #include "php_assert.h"
24: #include "php_ini.h"
25: /* }}} */
26:
27: ZEND_BEGIN_MODULE_GLOBALS(assert)
28: long active;
29: long bail;
30: long warning;
31: long quiet_eval;
32: zval *callback;
33: char *cb;
34: ZEND_END_MODULE_GLOBALS(assert)
35:
36: ZEND_DECLARE_MODULE_GLOBALS(assert)
37:
38: #ifdef ZTS
39: #define ASSERTG(v) TSRMG(assert_globals_id, zend_assert_globals *, v)
40: #else
41: #define ASSERTG(v) (assert_globals.v)
42: #endif
43:
44: #define SAFE_STRING(s) ((s)?(s):"")
45:
46: enum {
47: ASSERT_ACTIVE=1,
48: ASSERT_CALLBACK,
49: ASSERT_BAIL,
50: ASSERT_WARNING,
51: ASSERT_QUIET_EVAL
52: };
53:
54: static PHP_INI_MH(OnChangeCallback) /* {{{ */
55: {
56: if (EG(in_execution)) {
57: if (ASSERTG(callback)) {
58: zval_ptr_dtor(&ASSERTG(callback));
59: ASSERTG(callback) = NULL;
60: }
61: if (new_value && (ASSERTG(callback) || new_value_length)) {
62: MAKE_STD_ZVAL(ASSERTG(callback));
63: ZVAL_STRINGL(ASSERTG(callback), new_value, new_value_length, 1);
64: }
65: } else {
66: if (ASSERTG(cb)) {
67: pefree(ASSERTG(cb), 1);
68: }
69: if (new_value && new_value_length) {
70: ASSERTG(cb) = pemalloc(new_value_length + 1, 1);
71: memcpy(ASSERTG(cb), new_value, new_value_length);
72: ASSERTG(cb)[new_value_length] = '\0';
73: } else {
74: ASSERTG(cb) = NULL;
75: }
76: }
77: return SUCCESS;
78: }
79: /* }}} */
80:
81: PHP_INI_BEGIN()
82: STD_PHP_INI_ENTRY("assert.active", "1", PHP_INI_ALL, OnUpdateLong, active, zend_assert_globals, assert_globals)
83: STD_PHP_INI_ENTRY("assert.bail", "0", PHP_INI_ALL, OnUpdateLong, bail, zend_assert_globals, assert_globals)
84: STD_PHP_INI_ENTRY("assert.warning", "1", PHP_INI_ALL, OnUpdateLong, warning, zend_assert_globals, assert_globals)
85: PHP_INI_ENTRY("assert.callback", NULL, PHP_INI_ALL, OnChangeCallback)
86: STD_PHP_INI_ENTRY("assert.quiet_eval", "0", PHP_INI_ALL, OnUpdateLong, quiet_eval, zend_assert_globals, assert_globals)
87: PHP_INI_END()
88:
89: static void php_assert_init_globals(zend_assert_globals *assert_globals_p TSRMLS_DC) /* {{{ */
90: {
91: assert_globals_p->callback = NULL;
92: assert_globals_p->cb = NULL;
93: }
94: /* }}} */
95:
96: PHP_MINIT_FUNCTION(assert) /* {{{ */
97: {
98: ZEND_INIT_MODULE_GLOBALS(assert, php_assert_init_globals, NULL);
99:
100: REGISTER_INI_ENTRIES();
101:
102: REGISTER_LONG_CONSTANT("ASSERT_ACTIVE", ASSERT_ACTIVE, CONST_CS|CONST_PERSISTENT);
103: REGISTER_LONG_CONSTANT("ASSERT_CALLBACK", ASSERT_CALLBACK, CONST_CS|CONST_PERSISTENT);
104: REGISTER_LONG_CONSTANT("ASSERT_BAIL", ASSERT_BAIL, CONST_CS|CONST_PERSISTENT);
105: REGISTER_LONG_CONSTANT("ASSERT_WARNING", ASSERT_WARNING, CONST_CS|CONST_PERSISTENT);
106: REGISTER_LONG_CONSTANT("ASSERT_QUIET_EVAL", ASSERT_QUIET_EVAL, CONST_CS|CONST_PERSISTENT);
107:
108: return SUCCESS;
109: }
110: /* }}} */
111:
112: PHP_MSHUTDOWN_FUNCTION(assert) /* {{{ */
113: {
114: if (ASSERTG(cb)) {
115: pefree(ASSERTG(cb), 1);
116: ASSERTG(cb) = NULL;
117: }
118: return SUCCESS;
119: }
120: /* }}} */
121:
122: PHP_RSHUTDOWN_FUNCTION(assert) /* {{{ */
123: {
124: if (ASSERTG(callback)) {
125: zval_ptr_dtor(&ASSERTG(callback));
126: ASSERTG(callback) = NULL;
127: }
128:
129: return SUCCESS;
130: }
131: /* }}} */
132:
133: PHP_MINFO_FUNCTION(assert) /* {{{ */
134: {
135: DISPLAY_INI_ENTRIES();
136: }
137: /* }}} */
138:
139: /* {{{ proto int assert(string|bool assertion)
140: Checks if assertion is false */
141: PHP_FUNCTION(assert)
142: {
143: zval **assertion;
144: int val;
145: char *myeval = NULL;
146: char *compiled_string_description;
147:
148: if (! ASSERTG(active)) {
149: RETURN_TRUE;
150: }
151:
152: if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "Z", &assertion) == FAILURE) {
153: return;
154: }
155:
156: if (Z_TYPE_PP(assertion) == IS_STRING) {
157: zval retval;
158: int old_error_reporting = 0; /* shut up gcc! */
159:
160: myeval = Z_STRVAL_PP(assertion);
161:
162: if (ASSERTG(quiet_eval)) {
163: old_error_reporting = EG(error_reporting);
164: EG(error_reporting) = 0;
165: }
166:
167: compiled_string_description = zend_make_compiled_string_description("assert code" TSRMLS_CC);
168: if (zend_eval_stringl(myeval, Z_STRLEN_PP(assertion), &retval, compiled_string_description TSRMLS_CC) == FAILURE) {
169: efree(compiled_string_description);
170: php_error_docref(NULL TSRMLS_CC, E_RECOVERABLE_ERROR, "Failure evaluating code: %s%s", PHP_EOL, myeval);
171: if (ASSERTG(bail)) {
172: zend_bailout();
173: }
174: RETURN_FALSE;
175: }
176: efree(compiled_string_description);
177:
178: if (ASSERTG(quiet_eval)) {
179: EG(error_reporting) = old_error_reporting;
180: }
181:
182: convert_to_boolean(&retval);
183: val = Z_LVAL(retval);
184: } else {
185: convert_to_boolean_ex(assertion);
186: val = Z_LVAL_PP(assertion);
187: }
188:
189: if (val) {
190: RETURN_TRUE;
191: }
192:
193: if (!ASSERTG(callback) && ASSERTG(cb)) {
194: MAKE_STD_ZVAL(ASSERTG(callback));
195: ZVAL_STRING(ASSERTG(callback), ASSERTG(cb), 1);
196: }
197:
198: if (ASSERTG(callback)) {
199: zval *args[3];
200: zval *retval;
201: int i;
202: uint lineno = zend_get_executed_lineno(TSRMLS_C);
203: char *filename = zend_get_executed_filename(TSRMLS_C);
204:
205: MAKE_STD_ZVAL(args[0]);
206: MAKE_STD_ZVAL(args[1]);
207: MAKE_STD_ZVAL(args[2]);
208:
209: ZVAL_STRING(args[0], SAFE_STRING(filename), 1);
210: ZVAL_LONG (args[1], lineno);
211: ZVAL_STRING(args[2], SAFE_STRING(myeval), 1);
212:
213: MAKE_STD_ZVAL(retval);
214: ZVAL_FALSE(retval);
215:
216: /* XXX do we want to check for error here? */
217: call_user_function(CG(function_table), NULL, ASSERTG(callback), retval, 3, args TSRMLS_CC);
218:
219: for (i = 0; i <= 2; i++) {
220: zval_ptr_dtor(&(args[i]));
221: }
222: zval_ptr_dtor(&retval);
223: }
224:
225: if (ASSERTG(warning)) {
226: if (myeval) {
227: php_error_docref(NULL TSRMLS_CC, E_WARNING, "Assertion \"%s\" failed", myeval);
228: } else {
229: php_error_docref(NULL TSRMLS_CC, E_WARNING, "Assertion failed");
230: }
231: }
232:
233: if (ASSERTG(bail)) {
234: zend_bailout();
235: }
236: }
237: /* }}} */
238:
239: /* {{{ proto mixed assert_options(int what [, mixed value])
240: Set/get the various assert flags */
241: PHP_FUNCTION(assert_options)
242: {
243: zval **value = NULL;
244: long what;
245: int oldint;
246: int ac = ZEND_NUM_ARGS();
247:
248: if (zend_parse_parameters(ac TSRMLS_CC, "l|Z", &what, &value) == FAILURE) {
249: return;
250: }
251:
252: switch (what) {
253: case ASSERT_ACTIVE:
254: oldint = ASSERTG(active);
255: if (ac == 2) {
256: convert_to_string_ex(value);
257: zend_alter_ini_entry_ex("assert.active", sizeof("assert.active"), Z_STRVAL_PP(value), Z_STRLEN_PP(value), PHP_INI_USER, PHP_INI_STAGE_RUNTIME, 0 TSRMLS_CC);
258: }
259: RETURN_LONG(oldint);
260: break;
261:
262: case ASSERT_BAIL:
263: oldint = ASSERTG(bail);
264: if (ac == 2) {
265: convert_to_string_ex(value);
266: zend_alter_ini_entry_ex("assert.bail", sizeof("assert.bail"), Z_STRVAL_PP(value), Z_STRLEN_PP(value), PHP_INI_USER, PHP_INI_STAGE_RUNTIME, 0 TSRMLS_CC);
267: }
268: RETURN_LONG(oldint);
269: break;
270:
271: case ASSERT_QUIET_EVAL:
272: oldint = ASSERTG(quiet_eval);
273: if (ac == 2) {
274: convert_to_string_ex(value);
275: zend_alter_ini_entry_ex("assert.quiet_eval", sizeof("assert.quiet_eval"), Z_STRVAL_PP(value), Z_STRLEN_PP(value), PHP_INI_USER, PHP_INI_STAGE_RUNTIME, 0 TSRMLS_CC);
276: }
277: RETURN_LONG(oldint);
278: break;
279:
280: case ASSERT_WARNING:
281: oldint = ASSERTG(warning);
282: if (ac == 2) {
283: convert_to_string_ex(value);
284: zend_alter_ini_entry_ex("assert.warning", sizeof("assert.warning"), Z_STRVAL_PP(value), Z_STRLEN_PP(value), PHP_INI_USER, PHP_INI_STAGE_RUNTIME, 0 TSRMLS_CC);
285: }
286: RETURN_LONG(oldint);
287: break;
288:
289: case ASSERT_CALLBACK:
290: if (ASSERTG(callback) != NULL) {
291: RETVAL_ZVAL(ASSERTG(callback), 1, 0);
292: } else if (ASSERTG(cb)) {
293: RETVAL_STRING(ASSERTG(cb), 1);
294: } else {
295: RETVAL_NULL();
296: }
297: if (ac == 2) {
298: if (ASSERTG(callback)) {
299: zval_ptr_dtor(&ASSERTG(callback));
300: }
301: ASSERTG(callback) = *value;
302: zval_add_ref(value);
303: }
304: return;
305: break;
306:
307: default:
308: php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unknown value %ld", what);
309: break;
310: }
311:
312: RETURN_FALSE;
313: }
314: /* }}} */
315:
316: /*
317: * Local variables:
318: * tab-width: 4
319: * c-basic-offset: 4
320: * End:
321: * vim600: sw=4 ts=4 fdm=marker
322: * vim<600: sw=4 ts=4
323: */
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>