1 : /*
2 : +----------------------------------------------------------------------+
3 : | PHP Version 5 |
4 : +----------------------------------------------------------------------+
5 : | Copyright (c) 1997-2007 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,v 1.60.2.3.2.6 2007/02/16 16:35:04 dmitry Exp $ */
20 :
21 : /* {{{ includes/startup/misc */
22 :
23 : #include "php.h"
24 : #include "php_assert.h"
25 : #include "php_ini.h"
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 220 : {
56 220 : if (EG(in_execution)) {
57 0 : if (ASSERTG(callback)) {
58 0 : zval_ptr_dtor(&ASSERTG(callback));
59 : }
60 0 : if (new_value && (ASSERTG(callback) || new_value_length)) {
61 0 : MAKE_STD_ZVAL(ASSERTG(callback));
62 0 : ZVAL_STRINGL(ASSERTG(callback), new_value, new_value_length, 1);
63 : }
64 : } else {
65 220 : if (ASSERTG(cb)) {
66 0 : pefree(ASSERTG(cb), 1);
67 : }
68 220 : if (new_value && new_value_length) {
69 0 : ASSERTG(cb) = pemalloc(new_value_length + 1, 1);
70 0 : memcpy(ASSERTG(cb), new_value, new_value_length);
71 0 : ASSERTG(cb)[new_value_length] = '\0';
72 : } else {
73 220 : ASSERTG(cb) = NULL;
74 : }
75 : }
76 220 : return SUCCESS;
77 : }
78 :
79 : PHP_INI_BEGIN()
80 : STD_PHP_INI_ENTRY("assert.active", "1", PHP_INI_ALL, OnUpdateLong, active, zend_assert_globals, assert_globals)
81 : STD_PHP_INI_ENTRY("assert.bail", "0", PHP_INI_ALL, OnUpdateLong, bail, zend_assert_globals, assert_globals)
82 : STD_PHP_INI_ENTRY("assert.warning", "1", PHP_INI_ALL, OnUpdateLong, warning, zend_assert_globals, assert_globals)
83 : PHP_INI_ENTRY ("assert.callback", NULL, PHP_INI_ALL, OnChangeCallback)
84 : STD_PHP_INI_ENTRY("assert.quiet_eval", "0", PHP_INI_ALL, OnUpdateLong, quiet_eval, zend_assert_globals, assert_globals)
85 : PHP_INI_END()
86 :
87 : static void php_assert_init_globals(zend_assert_globals *assert_globals_p TSRMLS_DC)
88 220 : {
89 220 : assert_globals_p->callback = NULL;
90 220 : assert_globals_p->cb = NULL;
91 220 : }
92 :
93 : PHP_MINIT_FUNCTION(assert)
94 220 : {
95 220 : ZEND_INIT_MODULE_GLOBALS(assert, php_assert_init_globals, NULL);
96 :
97 220 : REGISTER_INI_ENTRIES();
98 :
99 220 : REGISTER_LONG_CONSTANT("ASSERT_ACTIVE", ASSERT_ACTIVE, CONST_CS|CONST_PERSISTENT);
100 220 : REGISTER_LONG_CONSTANT("ASSERT_CALLBACK", ASSERT_CALLBACK, CONST_CS|CONST_PERSISTENT);
101 220 : REGISTER_LONG_CONSTANT("ASSERT_BAIL", ASSERT_BAIL, CONST_CS|CONST_PERSISTENT);
102 220 : REGISTER_LONG_CONSTANT("ASSERT_WARNING", ASSERT_WARNING, CONST_CS|CONST_PERSISTENT);
103 220 : REGISTER_LONG_CONSTANT("ASSERT_QUIET_EVAL", ASSERT_QUIET_EVAL, CONST_CS|CONST_PERSISTENT);
104 :
105 220 : return SUCCESS;
106 : }
107 :
108 : PHP_MSHUTDOWN_FUNCTION(assert)
109 219 : {
110 219 : if (ASSERTG(cb)) {
111 0 : pefree(ASSERTG(cb), 1);
112 0 : ASSERTG(cb) = NULL;
113 : }
114 219 : return SUCCESS;
115 : }
116 :
117 : PHP_RSHUTDOWN_FUNCTION(assert)
118 219 : {
119 219 : if (ASSERTG(callback)) {
120 0 : zval_ptr_dtor(&ASSERTG(callback));
121 0 : ASSERTG(callback) = NULL;
122 : }
123 :
124 219 : return SUCCESS;
125 : }
126 :
127 : PHP_MINFO_FUNCTION(assert)
128 0 : {
129 0 : DISPLAY_INI_ENTRIES();
130 0 : }
131 :
132 : /* }}} */
133 : /* {{{ internal functions */
134 : /* }}} */
135 : /* {{{ proto int assert(string|bool assertion)
136 : Checks if assertion is false */
137 :
138 : PHP_FUNCTION(assert)
139 0 : {
140 : zval **assertion;
141 : int val;
142 0 : char *myeval = NULL;
143 : char *compiled_string_description;
144 :
145 0 : if (! ASSERTG(active)) {
146 0 : RETURN_TRUE;
147 : }
148 :
149 0 : if (ZEND_NUM_ARGS() != 1 || zend_get_parameters_ex(1, &assertion) == FAILURE) {
150 0 : WRONG_PARAM_COUNT;
151 : }
152 :
153 0 : if (Z_TYPE_PP(assertion) == IS_STRING) {
154 : zval retval;
155 0 : int old_error_reporting = 0; /* shut up gcc! */
156 :
157 0 : myeval = Z_STRVAL_PP(assertion);
158 :
159 0 : if (ASSERTG(quiet_eval)) {
160 0 : old_error_reporting = EG(error_reporting);
161 0 : EG(error_reporting) = 0;
162 : }
163 :
164 0 : compiled_string_description = zend_make_compiled_string_description("assert code" TSRMLS_CC);
165 0 : if (zend_eval_string(myeval, &retval, compiled_string_description TSRMLS_CC) == FAILURE) {
166 0 : efree(compiled_string_description);
167 0 : php_error_docref(NULL TSRMLS_CC, E_RECOVERABLE_ERROR, "Failure evaluating code: %s%s", PHP_EOL, myeval);
168 0 : if (ASSERTG(bail)) {
169 0 : zend_bailout();
170 : }
171 0 : RETURN_FALSE;
172 : }
173 0 : efree(compiled_string_description);
174 :
175 0 : if (ASSERTG(quiet_eval)) {
176 0 : EG(error_reporting) = old_error_reporting;
177 : }
178 :
179 0 : convert_to_boolean(&retval);
180 0 : val = Z_LVAL(retval);
181 : } else {
182 0 : convert_to_boolean_ex(assertion);
183 0 : val = Z_LVAL_PP(assertion);
184 : }
185 :
186 0 : if (val) {
187 0 : RETURN_TRUE;
188 : }
189 :
190 0 : if (!ASSERTG(callback) && ASSERTG(cb)) {
191 0 : MAKE_STD_ZVAL(ASSERTG(callback));
192 0 : ZVAL_STRING(ASSERTG(callback), ASSERTG(cb), 1);
193 : }
194 :
195 0 : if (ASSERTG(callback)) {
196 : zval *args[3];
197 : zval *retval;
198 : int i;
199 0 : uint lineno = zend_get_executed_lineno(TSRMLS_C);
200 0 : char *filename = zend_get_executed_filename(TSRMLS_C);
201 :
202 0 : MAKE_STD_ZVAL(args[0]);
203 0 : MAKE_STD_ZVAL(args[1]);
204 0 : MAKE_STD_ZVAL(args[2]);
205 :
206 0 : ZVAL_STRING(args[0], SAFE_STRING(filename), 1);
207 0 : ZVAL_LONG (args[1], lineno);
208 0 : ZVAL_STRING(args[2], SAFE_STRING(myeval), 1);
209 :
210 0 : MAKE_STD_ZVAL(retval);
211 0 : ZVAL_FALSE(retval);
212 :
213 : /* XXX do we want to check for error here? */
214 0 : call_user_function(CG(function_table), NULL, ASSERTG(callback), retval, 3, args TSRMLS_CC);
215 :
216 0 : for (i = 0; i <= 2; i++) {
217 0 : zval_ptr_dtor(&(args[i]));
218 : }
219 0 : zval_ptr_dtor(&retval);
220 : }
221 :
222 0 : if (ASSERTG(warning)) {
223 0 : if (myeval) {
224 0 : php_error_docref(NULL TSRMLS_CC, E_WARNING, "Assertion \"%s\" failed", myeval);
225 : } else {
226 0 : php_error_docref(NULL TSRMLS_CC, E_WARNING, "Assertion failed");
227 : }
228 : }
229 :
230 0 : if (ASSERTG(bail)) {
231 0 : zend_bailout();
232 : }
233 : }
234 :
235 : /* }}} */
236 : /* {{{ proto mixed assert_options(int what [, mixed value])
237 : Set/get the various assert flags */
238 :
239 : PHP_FUNCTION(assert_options)
240 0 : {
241 : zval **what, **value;
242 : int oldint;
243 0 : int ac = ZEND_NUM_ARGS();
244 :
245 0 : if (ac < 1 || ac > 2 || zend_get_parameters_ex(ac, &what, &value) == FAILURE) {
246 0 : WRONG_PARAM_COUNT;
247 : }
248 :
249 0 : convert_to_long_ex(what);
250 :
251 0 : switch (Z_LVAL_PP(what)) {
252 : case ASSERT_ACTIVE:
253 0 : oldint = ASSERTG(active);
254 0 : if (ac == 2) {
255 0 : convert_to_long_ex(value);
256 0 : ASSERTG(active) = Z_LVAL_PP(value);
257 : }
258 0 : RETURN_LONG(oldint);
259 : break;
260 :
261 : case ASSERT_BAIL:
262 0 : oldint = ASSERTG(bail);
263 0 : if (ac == 2) {
264 0 : convert_to_long_ex(value);
265 0 : ASSERTG(bail) = Z_LVAL_PP(value);
266 : }
267 0 : RETURN_LONG(oldint);
268 : break;
269 :
270 : case ASSERT_QUIET_EVAL:
271 0 : oldint = ASSERTG(quiet_eval);
272 0 : if (ac == 2) {
273 0 : convert_to_long_ex(value);
274 0 : ASSERTG(quiet_eval) = Z_LVAL_PP(value);
275 : }
276 0 : RETURN_LONG(oldint);
277 : break;
278 :
279 : case ASSERT_WARNING:
280 0 : oldint = ASSERTG(warning);
281 0 : if (ac == 2) {
282 0 : convert_to_long_ex(value);
283 0 : ASSERTG(warning) = Z_LVAL_PP(value);
284 : }
285 0 : RETURN_LONG(oldint);
286 : break;
287 :
288 : case ASSERT_CALLBACK:
289 0 : if (ASSERTG(callback) != NULL) {
290 0 : RETVAL_ZVAL(ASSERTG(callback), 1, 0);
291 0 : } else if (ASSERTG(cb)) {
292 0 : RETVAL_STRING(ASSERTG(cb), 1);
293 : } else {
294 0 : RETVAL_NULL();
295 : }
296 0 : if (ac == 2) {
297 0 : if (ASSERTG(callback)) {
298 0 : zval_ptr_dtor(&ASSERTG(callback));
299 : }
300 0 : ASSERTG(callback) = *value;
301 0 : zval_add_ref(value);
302 : }
303 0 : return;
304 : break;
305 :
306 : default:
307 0 : php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unknown value %ld", Z_LVAL_PP(what));
308 : break;
309 : }
310 :
311 0 : RETURN_FALSE;
312 : }
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 : */
|