~ubuntu-branches/ubuntu/hardy/php5/hardy-updates

« back to all changes in this revision

Viewing changes to ext/standard/assert.c

  • Committer: Bazaar Package Importer
  • Author(s): Adam Conrad
  • Date: 2005-10-09 03:14:32 UTC
  • Revision ID: james.westby@ubuntu.com-20051009031432-kspik3lobxstafv9
Tags: upstream-5.0.5
ImportĀ upstreamĀ versionĀ 5.0.5

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
   +----------------------------------------------------------------------+
 
3
   | PHP Version 5                                                        |
 
4
   +----------------------------------------------------------------------+
 
5
   | Copyright (c) 1997-2004 The PHP Group                                |
 
6
   +----------------------------------------------------------------------+
 
7
   | This source file is subject to version 3.0 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_0.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.59 2004/01/08 08:17:30 andi 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
ZEND_END_MODULE_GLOBALS(assert)
 
34
 
 
35
ZEND_DECLARE_MODULE_GLOBALS(assert)
 
36
 
 
37
#ifdef ZTS
 
38
#define ASSERTG(v) TSRMG(assert_globals_id, zend_assert_globals *, v)
 
39
#else
 
40
#define ASSERTG(v) (assert_globals.v)
 
41
#endif
 
42
 
 
43
#define SAFE_STRING(s) ((s)?(s):"")
 
44
 
 
45
enum {
 
46
        ASSERT_ACTIVE=1,
 
47
        ASSERT_CALLBACK,
 
48
        ASSERT_BAIL,
 
49
        ASSERT_WARNING,
 
50
        ASSERT_QUIET_EVAL
 
51
};
 
52
 
 
53
static PHP_INI_MH(OnChangeCallback)
 
54
{
 
55
        if (ASSERTG(callback)) {
 
56
                zval_ptr_dtor(&ASSERTG(callback));
 
57
        }
 
58
 
 
59
        if (new_value && (ASSERTG(callback) || new_value_length)) {
 
60
                MAKE_STD_ZVAL(ASSERTG(callback));
 
61
                ZVAL_STRINGL(ASSERTG(callback), new_value, new_value_length, 1);
 
62
        }
 
63
 
 
64
        return SUCCESS;
 
65
}
 
66
 
 
67
PHP_INI_BEGIN()
 
68
         STD_PHP_INI_ENTRY("assert.active",         "1",        PHP_INI_ALL,    OnUpdateLong,           active,                         zend_assert_globals,            assert_globals)
 
69
         STD_PHP_INI_ENTRY("assert.bail",           "0",        PHP_INI_ALL,    OnUpdateLong,           bail,                           zend_assert_globals,            assert_globals)
 
70
         STD_PHP_INI_ENTRY("assert.warning",    "1",    PHP_INI_ALL,    OnUpdateLong,           warning,                        zend_assert_globals,            assert_globals)
 
71
         PHP_INI_ENTRY    ("assert.callback",   NULL,   PHP_INI_ALL,    OnChangeCallback)
 
72
         STD_PHP_INI_ENTRY("assert.quiet_eval", "0",    PHP_INI_ALL,    OnUpdateLong,           quiet_eval,                     zend_assert_globals,            assert_globals)
 
73
PHP_INI_END()
 
74
 
 
75
static void php_assert_init_globals(zend_assert_globals *assert_globals_p TSRMLS_DC)
 
76
{
 
77
        assert_globals_p->callback = NULL;
 
78
}
 
79
 
 
80
PHP_MINIT_FUNCTION(assert)
 
81
{
 
82
        ZEND_INIT_MODULE_GLOBALS(assert, php_assert_init_globals, NULL);
 
83
 
 
84
        REGISTER_INI_ENTRIES();
 
85
 
 
86
        REGISTER_LONG_CONSTANT("ASSERT_ACTIVE", ASSERT_ACTIVE, CONST_CS|CONST_PERSISTENT);
 
87
        REGISTER_LONG_CONSTANT("ASSERT_CALLBACK", ASSERT_CALLBACK, CONST_CS|CONST_PERSISTENT);
 
88
        REGISTER_LONG_CONSTANT("ASSERT_BAIL", ASSERT_BAIL, CONST_CS|CONST_PERSISTENT);
 
89
        REGISTER_LONG_CONSTANT("ASSERT_WARNING", ASSERT_WARNING, CONST_CS|CONST_PERSISTENT);
 
90
        REGISTER_LONG_CONSTANT("ASSERT_QUIET_EVAL", ASSERT_QUIET_EVAL, CONST_CS|CONST_PERSISTENT);
 
91
 
 
92
        return SUCCESS;
 
93
}
 
94
 
 
95
PHP_MSHUTDOWN_FUNCTION(assert)
 
96
{
 
97
        if (ASSERTG(callback)) {
 
98
                zval_ptr_dtor(&ASSERTG(callback));
 
99
                ASSERTG(callback) = NULL;
 
100
        }
 
101
        return SUCCESS;
 
102
}
 
103
 
 
104
PHP_RSHUTDOWN_FUNCTION(assert)
 
105
{
 
106
        if (ASSERTG(callback)) { 
 
107
                zval_ptr_dtor(&ASSERTG(callback));
 
108
                ASSERTG(callback) = NULL;
 
109
        }
 
110
 
 
111
        return SUCCESS;
 
112
}
 
113
 
 
114
PHP_MINFO_FUNCTION(assert)
 
115
{
 
116
        DISPLAY_INI_ENTRIES();
 
117
}
 
118
 
 
119
/* }}} */
 
120
/* {{{ internal functions */
 
121
/* }}} */
 
122
/* {{{ proto int assert(string|bool assertion)
 
123
   Checks if assertion is false */
 
124
 
 
125
PHP_FUNCTION(assert)
 
126
{
 
127
        zval **assertion;       
 
128
        int val;
 
129
        char *myeval = NULL;
 
130
        char *compiled_string_description;
 
131
        
 
132
        if (! ASSERTG(active)) {
 
133
                RETURN_TRUE;
 
134
        }
 
135
 
 
136
        if (ZEND_NUM_ARGS() != 1 || zend_get_parameters_ex(1, &assertion) == FAILURE) {
 
137
                WRONG_PARAM_COUNT;
 
138
        }
 
139
 
 
140
        if (Z_TYPE_PP(assertion) == IS_STRING) {
 
141
                zval retval;
 
142
                int old_error_reporting = 0; /* shut up gcc! */
 
143
 
 
144
                myeval = Z_STRVAL_PP(assertion);
 
145
 
 
146
                if (ASSERTG(quiet_eval)) {
 
147
                        old_error_reporting = EG(error_reporting);
 
148
                        EG(error_reporting) = 0;
 
149
                }
 
150
 
 
151
                compiled_string_description = zend_make_compiled_string_description("assert code" TSRMLS_CC);
 
152
                if (zend_eval_string(myeval, &retval, compiled_string_description TSRMLS_CC) == FAILURE) {
 
153
                        efree(compiled_string_description);
 
154
                        php_error_docref(NULL TSRMLS_CC, E_ERROR, "Failure evaluating code:\n%s", myeval);
 
155
                        /* php_error_docref() does not return in this case. */
 
156
                }
 
157
                efree(compiled_string_description);
 
158
 
 
159
                if (ASSERTG(quiet_eval)) {
 
160
                        EG(error_reporting) = old_error_reporting;
 
161
                }
 
162
 
 
163
                convert_to_boolean(&retval);
 
164
                val = Z_LVAL(retval);
 
165
        } else {
 
166
                convert_to_boolean_ex(assertion);
 
167
                val = Z_LVAL_PP(assertion);
 
168
        }
 
169
 
 
170
        if (val) {
 
171
                RETURN_TRUE;
 
172
        }
 
173
 
 
174
        if (ASSERTG(callback)) {
 
175
                zval *args[3];
 
176
                zval *retval;
 
177
                int i;
 
178
                uint lineno = zend_get_executed_lineno(TSRMLS_C);
 
179
                char *filename = zend_get_executed_filename(TSRMLS_C);
 
180
 
 
181
                MAKE_STD_ZVAL(args[0]);
 
182
                MAKE_STD_ZVAL(args[1]);
 
183
                MAKE_STD_ZVAL(args[2]);
 
184
 
 
185
                ZVAL_STRING(args[0], SAFE_STRING(filename), 1);
 
186
                ZVAL_LONG (args[1], lineno);
 
187
                ZVAL_STRING(args[2], SAFE_STRING(myeval), 1);
 
188
                
 
189
                MAKE_STD_ZVAL(retval);
 
190
                ZVAL_FALSE(retval);
 
191
 
 
192
                /* XXX do we want to check for error here? */
 
193
                call_user_function(CG(function_table), NULL, ASSERTG(callback), retval, 3, args TSRMLS_CC);
 
194
 
 
195
                for (i = 0; i <= 2; i++) {
 
196
                        zval_ptr_dtor(&(args[i]));
 
197
                }
 
198
                zval_ptr_dtor(&retval);
 
199
        }
 
200
 
 
201
        if (ASSERTG(warning)) {
 
202
                if (myeval) {
 
203
                        php_error_docref(NULL TSRMLS_CC, E_WARNING, "Assertion \"%s\" failed", myeval);
 
204
                } else {
 
205
                        php_error_docref(NULL TSRMLS_CC, E_WARNING, "Assertion failed");
 
206
                }
 
207
        }
 
208
 
 
209
        if (ASSERTG(bail)) {
 
210
                zend_bailout();
 
211
        }
 
212
}
 
213
 
 
214
/* }}} */
 
215
/* {{{ proto mixed assert_options(int what [, mixed value])
 
216
   Set/get the various assert flags */
 
217
 
 
218
PHP_FUNCTION(assert_options)
 
219
{
 
220
        pval **what, **value;
 
221
        int oldint;
 
222
        int ac = ZEND_NUM_ARGS();
 
223
        
 
224
        if (ac < 1 || ac > 2 || zend_get_parameters_ex(ac, &what, &value) == FAILURE) {
 
225
                WRONG_PARAM_COUNT;
 
226
        }
 
227
 
 
228
        convert_to_long_ex(what);
 
229
 
 
230
        switch (Z_LVAL_PP(what)) {
 
231
        case ASSERT_ACTIVE:
 
232
                oldint = ASSERTG(active);
 
233
                if (ac == 2) {
 
234
                        convert_to_long_ex(value);
 
235
                        ASSERTG(active) = Z_LVAL_PP(value);
 
236
                }
 
237
                RETURN_LONG(oldint);
 
238
                break;
 
239
 
 
240
        case ASSERT_BAIL:
 
241
                oldint = ASSERTG(bail);
 
242
                if (ac == 2) {
 
243
                        convert_to_long_ex(value);
 
244
                        ASSERTG(bail) = Z_LVAL_PP(value);
 
245
                }
 
246
                RETURN_LONG(oldint);
 
247
                break;
 
248
 
 
249
        case ASSERT_QUIET_EVAL:
 
250
                oldint = ASSERTG(quiet_eval);
 
251
                if (ac == 2) {
 
252
                        convert_to_long_ex(value);
 
253
                        ASSERTG(quiet_eval) = Z_LVAL_PP(value);
 
254
                }
 
255
                RETURN_LONG(oldint);
 
256
                break;
 
257
 
 
258
        case ASSERT_WARNING:
 
259
                oldint = ASSERTG(warning);
 
260
                if (ac == 2) {
 
261
                        convert_to_long_ex(value);
 
262
                        ASSERTG(warning) = Z_LVAL_PP(value);
 
263
                }
 
264
                RETURN_LONG(oldint);
 
265
                break;
 
266
 
 
267
        case ASSERT_CALLBACK:
 
268
                if (ac == 2) {
 
269
                        if (ASSERTG(callback)) {
 
270
                                zval_ptr_dtor(&ASSERTG(callback));
 
271
                        }
 
272
                        ASSERTG(callback) = *value;
 
273
                        zval_add_ref(value);
 
274
                }
 
275
                RETURN_TRUE;
 
276
                break;
 
277
 
 
278
        default:
 
279
                php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unknown value %ld", Z_LVAL_PP(what));
 
280
                break;
 
281
        }
 
282
 
 
283
        RETURN_FALSE;
 
284
}
 
285
 
 
286
/* }}} */
 
287
 
 
288
/*
 
289
 * Local variables:
 
290
 * tab-width: 4
 
291
 * c-basic-offset: 4
 
292
 * End:
 
293
 * vim600: sw=4 ts=4 fdm=marker
 
294
 * vim<600: sw=4 ts=4
 
295
 */