From 2751fe0354c0d603202881a91d52e1b27559042b Mon Sep 17 00:00:00 2001 From: Felipe Pena Date: Wed, 25 Jun 2008 16:22:13 +0000 Subject: [PATCH] - New parameter parsing API --- ext/odbc/birdstep.c | 122 ++++++++++++++++++++-------------------- ext/readline/readline.c | 96 +++++++++++++------------------ 2 files changed, 101 insertions(+), 117 deletions(-) diff --git a/ext/odbc/birdstep.c b/ext/odbc/birdstep.c index 828475226a2..6d12452da32 100644 --- a/ext/odbc/birdstep.c +++ b/ext/odbc/birdstep.c @@ -104,8 +104,8 @@ ZEND_GET_MODULE(birdstep) THREAD_LS birdstep_module php_birdstep_module; THREAD_LS static HENV henv; -#define PHP_GET_BIRDSTEP_RES_IDX(id) convert_to_long_ex(id); if (!(res = birdstep_find_result(list, Z_LVAL_PP(id)))) { php_error_docref(NULL TSRMLS_CC, E_WARNING, "Birdstep: Not result index (%ld)", Z_LVAL_PP(id)); RETURN_FALSE; } -#define PHP_BIRDSTEP_CHK_LNK(id) convert_to_long_ex(id); if (!(conn = birdstep_find_conn(list,Z_LVAL_PP(id)))) { php_error_docref(NULL TSRMLS_CC, E_WARNING, "Birdstep: Not connection index (%ld)", Z_LVAL_PP(id)); RETURN_FALSE; } +#define PHP_GET_BIRDSTEP_RES_IDX(id) if (!(res = birdstep_find_result(list, id))) { php_error_docref(NULL TSRMLS_CC, E_WARNING, "Birdstep: Not result index (%ld)", id); RETURN_FALSE; } +#define PHP_BIRDSTEP_CHK_LNK(id) if (!(conn = birdstep_find_conn(list, id))) { php_error_docref(NULL TSRMLS_CC, E_WARNING, "Birdstep: Not connection index (%ld)", id); RETURN_FALSE; } static void _close_birdstep_link(zend_rsrc_list_entry *rsrc TSRMLS_DC) @@ -231,36 +231,30 @@ static void birdstep_del_result(HashTable *list,int ind) */ PHP_FUNCTION(birdstep_connect) { - zval **serv,**user,**pass; - char *Serv = NULL; - char *User = NULL; - char *Pass = NULL; + char *serv, *user, *pass; + int serv_len, user_len, pass_len; RETCODE stat; HDBC hdbc; VConn *new; long ind; + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sss", &serv, &serv_len, &user, &user_len, &pass, &pass_len) == FAILURE) { + return; + } + if ( php_birdstep_module.max_links != -1 && php_birdstep_module.num_links == php_birdstep_module.max_links ) { php_error_docref(NULL TSRMLS_CC, E_WARNING, "Birdstep: Too many open connections (%d)",php_birdstep_module.num_links); RETURN_FALSE; } - if (ZEND_NUM_ARGS() != 3 || zend_get_parameters_ex(3, &serv, &user, &pass) == FAILURE ) { - WRONG_PARAM_COUNT; - } - convert_to_string_ex(serv); - convert_to_string_ex(user); - convert_to_string_ex(pass); - Serv = Z_STRVAL_PP(serv); - User = Z_STRVAL_PP(user); - Pass = Z_STRVAL_PP(pass); + stat = SQLAllocConnect(henv,&hdbc); if ( stat != SQL_SUCCESS ) { php_error_docref(NULL TSRMLS_CC, E_WARNING, "Birdstep: Could not allocate connection handle"); RETURN_FALSE; } - stat = SQLConnect(hdbc,Serv,SQL_NTS,User,SQL_NTS,Pass,SQL_NTS); + stat = SQLConnect(hdbc, serv, SQL_NTS, user, SQL_NTS, pass, SQL_NTS); if ( stat != SQL_SUCCESS && stat != SQL_SUCCESS_WITH_INFO ) { - php_error_docref(NULL TSRMLS_CC, E_WARNING, "Birdstep: Could not connect to server \"%s\" for %s",Serv,User); + php_error_docref(NULL TSRMLS_CC, E_WARNING, "Birdstep: Could not connect to server \"%s\" for %s", serv, user); SQLFreeConnect(hdbc); RETURN_FALSE; } @@ -275,17 +269,18 @@ PHP_FUNCTION(birdstep_connect) */ PHP_FUNCTION(birdstep_close) { - zval **id; + long id; VConn *conn; - if (ZEND_NUM_ARGS() != 1 || zend_get_parameters_ex(1, &id) == FAILURE) { - WRONG_PARAM_COUNT; + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &id) == FAILURE) { + return; } + PHP_BIRDSTEP_CHK_LNK(id); SQLDisconnect(conn->hdbc); SQLFreeConnect(conn->hdbc); - birdstep_del_conn(list,Z_LVAL_PP(id)); + birdstep_del_conn(list, id); php_birdstep_module.num_links--; RETURN_TRUE; } @@ -295,22 +290,20 @@ PHP_FUNCTION(birdstep_close) */ PHP_FUNCTION(birdstep_exec) { - zval **ind, **exec_str; - char *query = NULL; - int indx; + char *query; + long ind; + int query_len, indx; VConn *conn; Vresult *res; RETCODE stat; SWORD cols,i,colnamelen; SDWORD rows,coldesc; - if (ZEND_NUM_ARGS() != 2 || zend_get_parameters_ex(2, &ind, &exec_str) == FAILURE) { - WRONG_PARAM_COUNT; + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ls", &ind, &query, &query_len) == FAILURE) { + return; } - PHP_BIRDSTEP_CHK_LNK(ind); - convert_to_string_ex(exec_str); - query = Z_STRVAL_PP(exec_str); + PHP_BIRDSTEP_CHK_LNK(ind); res = (Vresult *)emalloc(sizeof(Vresult)); stat = SQLAllocStmt(conn->hdbc,&res->hstmt); @@ -378,15 +371,16 @@ PHP_FUNCTION(birdstep_exec) */ PHP_FUNCTION(birdstep_fetch) { - zval **ind; + long ind; Vresult *res; RETCODE stat; UDWORD row; UWORD RowStat[1]; - if ( ZEND_NUM_ARGS() != 1 || zend_get_parameters_ex(1, &ind) == FAILURE ) { - WRONG_PARAM_COUNT; + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &ind) == FAILURE) { + return; } + PHP_GET_BIRDSTEP_RES_IDX(ind); stat = SQLExtendedFetch(res->hstmt,SQL_FETCH_NEXT,1,&row,RowStat); @@ -406,11 +400,12 @@ PHP_FUNCTION(birdstep_fetch) } /* }}} */ -/* {{{ proto mixed birdstep_result(int index, int col) +/* {{{ proto mixed birdstep_result(int index, mixed col) */ PHP_FUNCTION(birdstep_result) { - zval **ind, **col; + zval **col; + long ind; Vresult *res; RETCODE stat; int i,sql_c_type; @@ -419,9 +414,10 @@ PHP_FUNCTION(birdstep_result) SWORD indx = -1; char *field = NULL; - if ( ZEND_NUM_ARGS() != 2 || zend_get_parameters_ex(2, &ind, &col) == FAILURE ) { - WRONG_PARAM_COUNT; + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "lZ", &ind, &col) == FAILURE) { + return; } + PHP_GET_BIRDSTEP_RES_IDX(ind); if ( Z_TYPE_PP(col) == IS_STRING ) { @@ -502,16 +498,17 @@ l1: */ PHP_FUNCTION(birdstep_freeresult) { - zval **ind; + long ind; Vresult *res; - if ( ZEND_NUM_ARGS() != 1 || zend_get_parameters_ex(1, &ind) == FAILURE ) { - WRONG_PARAM_COUNT; + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &ind) == FAILURE) { + return; } + PHP_GET_BIRDSTEP_RES_IDX(ind); SQLFreeStmt(res->hstmt,SQL_DROP); - birdstep_del_result(list,Z_LVAL_PP(ind)); + birdstep_del_result(list, ind); RETURN_TRUE; } /* }}} */ @@ -520,13 +517,14 @@ PHP_FUNCTION(birdstep_freeresult) */ PHP_FUNCTION(birdstep_autocommit) { - zval **id; + long id; RETCODE stat; VConn *conn; - if ( ZEND_NUM_ARGS() != 1 || zend_get_parameters_ex(1, &id) == FAILURE ) { - WRONG_PARAM_COUNT; + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &id) == FAILURE) { + return; } + PHP_BIRDSTEP_CHK_LNK(id); stat = SQLSetConnectOption(conn->hdbc,SQL_AUTOCOMMIT,SQL_AUTOCOMMIT_ON); @@ -542,13 +540,14 @@ PHP_FUNCTION(birdstep_autocommit) */ PHP_FUNCTION(birdstep_off_autocommit) { - zval **id; + long id; RETCODE stat; VConn *conn; - if ( ZEND_NUM_ARGS() != 1 || zend_get_parameters_ex(1, &id) == FAILURE ) { - WRONG_PARAM_COUNT; + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &id) == FAILURE) { + return; } + PHP_BIRDSTEP_CHK_LNK(id); stat = SQLSetConnectOption(conn->hdbc,SQL_AUTOCOMMIT,SQL_AUTOCOMMIT_OFF); @@ -564,13 +563,14 @@ PHP_FUNCTION(birdstep_off_autocommit) */ PHP_FUNCTION(birdstep_commit) { - zval **id; + long id; RETCODE stat; VConn *conn; - if ( ZEND_NUM_ARGS() != 1 || zend_get_parameters_ex(1, &id) == FAILURE ) { - WRONG_PARAM_COUNT; + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &id) == FAILURE) { + return; } + PHP_BIRDSTEP_CHK_LNK(id) stat = SQLTransact(NULL,conn->hdbc,SQL_COMMIT); @@ -586,13 +586,14 @@ PHP_FUNCTION(birdstep_commit) */ PHP_FUNCTION(birdstep_rollback) { - zval **id; + long id; RETCODE stat; VConn *conn; - if ( ZEND_NUM_ARGS() != 1 || zend_get_parameters_ex(1, &id) == FAILURE ) { - WRONG_PARAM_COUNT; + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &id) == FAILURE) { + return; } + PHP_BIRDSTEP_CHK_LNK(id); stat = SQLTransact(NULL,conn->hdbc,SQL_ROLLBACK); @@ -608,17 +609,17 @@ PHP_FUNCTION(birdstep_rollback) */ PHP_FUNCTION(birdstep_fieldname) { - zval **ind, **col; + long ind, col; Vresult *res; SWORD indx; - if ( ZEND_NUM_ARGS() != 2 || zend_get_parameters_ex(2, &ind, &col) == FAILURE ) { - WRONG_PARAM_COUNT; + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ll", &ind, &col) == FAILURE) { + return; } + PHP_GET_BIRDSTEP_RES_IDX(ind); - convert_to_long_ex(col); - indx = Z_LVAL_PP(col); + indx = col; if ( indx < 0 || indx >= res->numcols ) { php_error_docref(NULL TSRMLS_CC, E_WARNING, "Birdstep: Field index not in range"); RETURN_FALSE; @@ -631,12 +632,13 @@ PHP_FUNCTION(birdstep_fieldname) */ PHP_FUNCTION(birdstep_fieldnum) { - zval **ind; + long ind; Vresult *res; - if ( ZEND_NUM_ARGS() != 1 || zend_get_parameters_ex(1, &ind) == FAILURE ) { - WRONG_PARAM_COUNT; + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &ind) == FAILURE) { + return; } + PHP_GET_BIRDSTEP_RES_IDX(ind); RETURN_LONG(res->numcols); diff --git a/ext/readline/readline.c b/ext/readline/readline.c index 715f434db2e..b1b1812569e 100644 --- a/ext/readline/readline.c +++ b/ext/readline/readline.c @@ -154,21 +154,20 @@ PHP_FUNCTION(readline) /* }}} */ -/* {{{ proto mixed readline_info([string varname] [, string newvalue]) - Gets/sets various internal readline variables. */ - #define SAFE_STRING(s) ((s)?(char*)(s):"") +/* {{{ proto mixed readline_info([string varname [, string newvalue]]) + Gets/sets various internal readline variables. */ PHP_FUNCTION(readline_info) { - zval **what; + char *what; zval **value; - int oldval; + int what_len, oldval; char *oldstr; int ac = ZEND_NUM_ARGS(); - if (ac < 0 || ac > 2 || zend_get_parameters_ex(ac, &what, &value) == FAILURE) { - WRONG_PARAM_COUNT; + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|sZ", &what, &what_len, &value) == FAILURE) { + return; } if (ac == 0) { @@ -189,9 +188,7 @@ PHP_FUNCTION(readline_info) add_assoc_string(return_value,"library_version",(char *)SAFE_STRING(rl_library_version),1); add_assoc_string(return_value,"readline_name",(char *)SAFE_STRING(rl_readline_name),1); } else { - convert_to_string_ex(what); - - if (! strcasecmp(Z_STRVAL_PP(what),"line_buffer")) { + if (!strcasecmp(what,"line_buffer")) { oldstr = rl_line_buffer; if (ac == 2) { /* XXX if (rl_line_buffer) free(rl_line_buffer); */ @@ -199,34 +196,34 @@ PHP_FUNCTION(readline_info) rl_line_buffer = strdup(Z_STRVAL_PP(value)); } RETVAL_STRING(SAFE_STRING(oldstr),1); - } else if (! strcasecmp(Z_STRVAL_PP(what),"point")) { + } else if (!strcasecmp(what, "point")) { RETVAL_LONG(rl_point); - } else if (! strcasecmp(Z_STRVAL_PP(what),"end")) { + } else if (!strcasecmp(what, "end")) { RETVAL_LONG(rl_end); #ifdef HAVE_LIBREADLINE - } else if (! strcasecmp(Z_STRVAL_PP(what),"mark")) { + } else if (!strcasecmp(what, "mark")) { RETVAL_LONG(rl_mark); - } else if (! strcasecmp(Z_STRVAL_PP(what),"done")) { + } else if (!strcasecmp(what, "done")) { oldval = rl_done; if (ac == 2) { convert_to_long_ex(value); rl_done = Z_LVAL_PP(value); } RETVAL_LONG(oldval); - } else if (! strcasecmp(Z_STRVAL_PP(what),"pending_input")) { + } else if (!strcasecmp(what, "pending_input")) { oldval = rl_pending_input; if (ac == 2) { convert_to_string_ex(value); rl_pending_input = Z_STRVAL_PP(value)[0]; } RETVAL_LONG(oldval); - } else if (! strcasecmp(Z_STRVAL_PP(what),"prompt")) { + } else if (!strcasecmp(what, "prompt")) { RETVAL_STRING(SAFE_STRING(rl_prompt),1); - } else if (! strcasecmp(Z_STRVAL_PP(what),"terminal_name")) { + } else if (!strcasecmp(what, "terminal_name")) { RETVAL_STRING((char *)SAFE_STRING(rl_terminal_name),1); #endif #if HAVE_ERASE_EMPTY_LINE - } else if (! strcasecmp(Z_STRVAL_PP(what),"erase_empty_line")) { + } else if (!strcasecmp(what, "erase_empty_line")) { oldval = rl_erase_empty_line; if (ac == 2) { convert_to_long_ex(value); @@ -234,9 +231,9 @@ PHP_FUNCTION(readline_info) } RETVAL_LONG(oldval); #endif - } else if (! strcasecmp(Z_STRVAL_PP(what),"library_version")) { + } else if (!strcasecmp(what,"library_version")) { RETVAL_STRING((char *)SAFE_STRING(rl_library_version),1); - } else if (! strcasecmp(Z_STRVAL_PP(what),"readline_name")) { + } else if (!strcasecmp(what, "readline_name")) { oldstr = (char*)rl_readline_name; if (ac == 2) { /* XXX if (rl_readline_name) free(rl_readline_name); */ @@ -249,19 +246,18 @@ PHP_FUNCTION(readline_info) } /* }}} */ -/* {{{ proto bool readline_add_history([string prompt]) +/* {{{ proto bool readline_add_history(string prompt) Adds a line to the history */ PHP_FUNCTION(readline_add_history) { - zval **arg; - int ac = ZEND_NUM_ARGS(); + char *arg; + int arg_len; - if (ac != 1 || zend_get_parameters_ex(ac, &arg) == FAILURE) { - WRONG_PARAM_COUNT; + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &arg, &arg_len) == FAILURE) { + return; } - convert_to_string_ex(arg); - add_history(Z_STRVAL_PP(arg)); + add_history(arg); RETURN_TRUE; } @@ -271,10 +267,8 @@ PHP_FUNCTION(readline_add_history) Clears the history */ PHP_FUNCTION(readline_clear_history) { - int ac = ZEND_NUM_ARGS(); - - if (ac != 0) { - WRONG_PARAM_COUNT; + if (zend_parse_parameters_none() == FAILURE) { + return; } clear_history(); @@ -308,26 +302,19 @@ PHP_FUNCTION(readline_list_history) } #endif /* }}} */ -/* {{{ proto bool readline_read_history([string filename] [, int from] [,int to]) +/* {{{ proto bool readline_read_history([string filename]) Reads the history */ PHP_FUNCTION(readline_read_history) { - zval **arg; - char *filename = NULL; - int ac = ZEND_NUM_ARGS(); + char *arg = NULL; + int arg_len; - if (ac < 0 || ac > 1 || zend_get_parameters_ex(ac, &arg) == FAILURE) { - WRONG_PARAM_COUNT; + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|s", &arg, &arg_len) == FAILURE) { + return; } /* XXX from & to NYI */ - - if (ac == 1) { - convert_to_string_ex(arg); - filename = Z_STRVAL_PP(arg); - } - - if (read_history(filename)) { + if (read_history(arg)) { RETURN_FALSE; } else { RETURN_TRUE; @@ -339,20 +326,14 @@ PHP_FUNCTION(readline_read_history) Writes the history */ PHP_FUNCTION(readline_write_history) { - zval **arg; - char *filename = NULL; - int ac = ZEND_NUM_ARGS(); + char *arg = NULL; + int arg_len; - if (ac < 0 || ac > 1 || zend_get_parameters_ex(ac, &arg) == FAILURE) { - WRONG_PARAM_COUNT; + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|s", &arg, &arg_len) == FAILURE) { + return; } - if (ac == 1) { - convert_to_string_ex(arg); - filename = Z_STRVAL_PP(arg); - } - - if (write_history(filename)) { + if (write_history(arg)) { RETURN_FALSE; } else { RETURN_TRUE; @@ -368,7 +349,7 @@ static char *_readline_command_generator(const char *text, int state) HashTable *myht = Z_ARRVAL(_readline_array); zval **entry; - if (! state) { + if (!state) { zend_hash_internal_pointer_reset(myht); } @@ -458,8 +439,9 @@ PHP_FUNCTION(readline_completion_function) } zval_dtor(&name); - if (_readline_completion) + if (_readline_completion) { FREE_ZVAL(_readline_completion); + } MAKE_STD_ZVAL(_readline_completion); *_readline_completion = *arg;