Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:15106 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 26728 invoked by uid 1010); 20 Feb 2005 01:39:22 -0000 Delivered-To: ezmlm-scan-internals@lists.php.net Delivered-To: ezmlm-internals@lists.php.net Received: (qmail 19423 invoked from network); 20 Feb 2005 01:37:37 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 20 Feb 2005 01:37:37 -0000 X-Host-Fingerprint: 213.186.50.69 blanchette.skouat.net Linux 2.4/2.6 Received: from ([213.186.50.69:47280] helo=blanchette.skouat.net) by pb1.pair.com (ecelerity HEAD (r4059)) with SMTP id F1/29-19278-BB847124 for ; Sat, 19 Feb 2005 09:10:03 -0500 Received: from gabriel ([::ffff:213.223.77.121]) (AUTH: LOGIN frederic.lecointre@burnweb.net) by blanchette.skouat.net with esmtp; Sat, 19 Feb 2005 15:09:54 +0100 id 00028168.421748B2.00004607 Message-ID: <005401c5168c$eb0b0e50$0201a8c0@gabriel> "=?UTF-8?Q?Fr=C3=A9d=C3=A9ric_LECOINTRE?=" To: internals@lists.php.net References: <005801c5156b$ae0711b0$0201a8c0@gabriel> <855185390.20050218092558@marcus-boerger.de> <008701c515a7$4fec38d0$0201a8c0@gabriel> <20050218121817.30870.qmail@lists.php.net> Date: Sat, 19 Feb 2005 15:11:33 +0100 Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="=_blanchette-17927-1108822195-0001-2" X-Priority: 3 X-MSMail-Priority: Normal X-Mailer: Microsoft Outlook Express 6.00.2800.1106 X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1106 Subject: Re: [PHP-DEV] function proposal - php_check_syntax_string From: bogus@news.php.net (Unknown Sender) --=_blanchette-17927-1108822195-0001-2 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 7bit Hello all, With Val Khokhlov's help: sapi/cli/php -r "php_check_syntax_string('class foo {}'); var_dump(class_exists('foo'));" bool(false) thanks :-) The original purpose is to valid small code like $foo->bar($foo->bar($blah,$foo,44,"foo",$foo[0].bar)) and avoid regexp ( good example in Smarty_Compiler.class.php#144-164 with limitations) fred ----- Original Message ----- From: "Johannes Schlueter" To: Sent: Friday, February 18, 2005 1:17 PM Subject: Re: [PHP-DEV] function proposal - php_check_syntax_string | Hi Fred, | | this patch doesn't just check wether the string has the right syntax but | also compiles it and registers it's functions and classes in the relevant | tables. | | $ sapi/cli/php -r "php_check_syntax_string('class foo {}'); | var_dump(class_exists('foo'));" | bool(true) | | johannes | --=_blanchette-17927-1108822195-0001-2 Content-Type: text/plain; name="basic_functions.h.diff.txt"; charset=iso-8859-1 Content-Transfer-Encoding: quoted-printable Content-Disposition: attachment; filename="basic_functions.h.diff.txt" --- ext/standard/basic_functions.h.old 2004-03-27 01:50:39.000000000 = +0100=0A= +++ ext/standard/basic_functions.h 2005-02-18 03:15:00.000000000 +0100=0A= @@ -83,6 +83,7 @@=0A= PHP_FUNCTION(highlight_string);=0A= PHP_FUNCTION(php_strip_whitespace);=0A= PHP_FUNCTION(php_check_syntax);=0A= +PHP_FUNCTION(php_check_syntax_string);=0A= ZEND_API void php_get_highlight_struct(zend_syntax_highlighter_ini = *syntax_highlighter_ini);=0A= =0A= PHP_FUNCTION(ini_get);=0A= --=_blanchette-17927-1108822195-0001-2 Content-Type: text/plain; name="basic_functions.c.diff.txt"; charset=iso-8859-1 Content-Transfer-Encoding: quoted-printable Content-Disposition: attachment; filename="basic_functions.c.diff.txt" --- ext/standard/basic_functions.c.old 2004-11-16 00:16:20.000000000 = +0100=0A= +++ ext/standard/basic_functions.c 2005-02-19 14:29:44.011407448 +0100=0A= @@ -33,7 +33,7 @@=0A= #include "ext/session/php_session.h"=0A= #include "zend_operators.h"=0A= #include "ext/standard/dns.h"=0A= -#include "ext/standard/php_uuencode.h"=0A= +#include "ext/standard/php_uuencode.h" =0A= #ifdef PHP_WIN32=0A= #include "win32/php_win32_globals.h"=0A= @@ -481,7 +481,8 @@=0A= PHP_FE(highlight_string, NULL)=0A= PHP_FE(php_strip_whitespace, NULL)=0A= PHP_FE(php_check_syntax, second_arg_force_ref)=0A= -=0A= + PHP_FE(php_check_syntax_string, second_arg_force_ref) + =0A= PHP_FE(ini_get, NULL)=0A= PHP_FE(ini_get_all, NULL)=0A= PHP_FE(ini_set, NULL)=0A= @@ -2366,6 +2367,90 @@=0A= return;=0A= }=0A= /* }}} */=0A= +/* {{{ proto bool php_check_syntax_string(string string [, = &$error_message]) + Check the syntax of the specified string. */ +PHP_FUNCTION(php_check_syntax_string) +{ + zval *zcode, *zerrors =3D NULL; + zend_op_array *ops; + zend_uchar original_handle_op_arrays; + int n_old_func =3D zend_hash_num_elements(CG(function_table)); + int n_old_class =3D zend_hash_num_elements(CG(class_table)); + int old_errors =3D PG(display_errors); + int log_errors =3D PG(log_errors); + int i =3D 0; + zend_function *zf; + zend_class_entry *zc; + HashPosition pos; + =20 + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z|z", = &zcode, &zerrors) =3D=3D FAILURE){ + RETURN_FALSE; + } + =20 + convert_to_string(zcode);=20 + =20 + PG(log_errors) =3D PG(display_errors) =3D 0; + =20 + original_handle_op_arrays =3D CG(handle_op_arrays); + ops =3D compile_string(zcode, "php_check_syntax_string" = TSRMLS_CC); + CG(handle_op_arrays) =3D original_handle_op_arrays; + + if (ops) { + =20 + #ifdef ZEND_ENGINE_2 + destroy_op_array(ops TSRMLS_CC); + #else + destroy_op_array(ops); + #endif + efree(ops); + =20 + /* clean-up created functions */ + i =3D 0; + zend_hash_internal_pointer_reset_ex(CG(function_table), = &pos); + while (zend_hash_get_current_data_ex(CG(function_table), = (void **)&zf, &pos) =3D=3D SUCCESS) + { + i++; + if (i > n_old_func && zf->type =3D=3D = ZEND_USER_FUNCTION) { + zend_hash_del(CG(function_table), pos->arKey, = pos->nKeyLength); + } + zend_hash_move_forward_ex(CG(function_table), &pos); + } + =20 + /* clean-up created classes */ + i =3D 0; + zend_hash_internal_pointer_reset_ex(CG(class_table), &pos); + while (zend_hash_get_current_data_ex(CG(class_table), (void = **)&zc, &pos) =3D=3D SUCCESS) + { + #ifdef ZEND_ENGINE_2 + zc =3D *((zend_class_entry**)zc); + #endif + i++; + if (i > n_old_class && zc->type =3D=3D ZEND_USER_CLASS) = { + zend_hash_del(CG(class_table), pos->arKey, = pos->nKeyLength); + } + =20 + zend_hash_move_forward_ex(CG(class_table), &pos); + } + =20 + RETVAL_TRUE; + =20 + } else { + if (zerrors) { + char *error_str; +=20 + zval_dtor(zerrors); + spprintf(&error_str, 0, "%s on line %d", = PG(last_error_message), PG(last_error_lineno)); + ZVAL_STRING(zerrors, error_str, 0); + } + RETVAL_FALSE; + } +=20 + PG(display_errors) =3D old_errors; + PG(log_errors) =3D log_errors; +=20 + return; + } + /* }}} */ =0A= /* {{{ proto bool highlight_string(string string [, bool return] )=0A= Syntax highlight a string or optionally return it */=0A= --=_blanchette-17927-1108822195-0001-2--