YP/NIS functions reworked.

From: Date: Sat, 17 Mar 2001 20:06:57 +0000
Subject: YP/NIS functions reworked.
Groups: php.dev 
Request: Send a blank email to php-dev+get-49247@lists.php.net to get a copy of this message
Hello! I spent some time cleaning up and adding new functionality to the YP/NIS module. General changes: All functions now maintain a global 'errno' variable with the returnvalue of the last yp call. All functions now call php_error with a warning message on failure (this might me overkill). A bunch of YPERR_xxxx error code constants has been added. yp_first has been 'fixed' to return an array in the same style as yp_next as outlined in bug #8041. It is still backwards compatible with code that relies on the old 'broken' behaviour. New functions: void yp_all(string domain, string map, string callback) Traverse a map and call the supplied callback function on each entry in the map. The callback function should have the following declaration: int callback (int status, string key, string value) If status is 0 a new entry has been retrived. Otherwise it contains the error code describing the reason for failure. After the last entry in the map the callback will be called a final time with status set to YPERR_NOMORE. key and value contains the data retrived from the map. To continue traversing the map the return value sould be FALSE, else return TRUE. EXAMPLE: function callback ($status, $key, $value) { if (!$status) print "$key -> $value<br>\n"; else print yp_err_string ($status); return $status; } $domain = yp_get_default_domain (); yp_all ($domain, "passwd.byname", "callback"); array yp_cat(string domain, string map) Retrive the entire map as an associative array. Note! YP maps may contain entries with zero length keys, these are silently dropped. EXAMPLE: $domain = yp_get_default_domain (); $map = yp_cat ($domain, "passwd.byname");D while (list ($key, $value) = each ($map)) { print "$key -> $value<br>\n"; } int yp_errno() Return the error code from the last YP call or 0 if no error occured. string yp_err_string(int errorcode) Return the corresponding error message for the given error code. New constants: YPERR_ACCESS Access violation. YPERR_BADARGS The arguments to the function are bad. YPERR_BADDB The YP database is bad. YPERR_BUSY The database is busy. YPERR_DOMAIN Cannot bind to server on this domain. YPERR_KEY No such key in map. YPERR_MAP No such map in server's domain. YPERR_NODOM Local domain name not set. YPERR_NOMORE No more records in map database. YPERR_PMAP Cannot communicate with rpcbind. YPERR_RESRC Resource allocation failure. YPERR_RPC RPC failure; domain has been unbound. YPERR_YPBIND Cannot communicate with ypbind. YPERR_YPERR Internal YP server or client error. YPERR_YPSERV Cannot communicate with ypserv. YPERR_VERS YP version mismatch. I hope this will be usefull to more ppl than me... :) I'm not subscribed to the list, please CC your comments to me. Regards, Fredrik -- Do fish get thirsty? Fredrik Öhrn Chalmers University of Technology ohrn@chl.chalmers.se Sweden *** php_yp.h.orig Sat Mar 17 20:09:13 2001 --- php_yp.h Sat Mar 17 20:25:57 2001 *************** *** 13,18 **** --- 13,19 ---- | license@php.net so we can mail you a copy immediately. | +----------------------------------------------------------------------+ | Authors: Stephanie Wehner <_@r4k.net> | + | Fredrik Ohrn | +----------------------------------------------------------------------+ */ *************** *** 23,28 **** --- 24,35 ---- #if HAVE_YP + #ifdef PHP_WIN32 + #define PHP_YP_API __declspec(dllexport) + #else + #define PHP_YP_API + #endif + extern zend_module_entry yp_module_entry; #define yp_module_ptr &yp_module_entry *************** *** 33,39 **** --- 40,72 ---- PHP_FUNCTION(yp_match); PHP_FUNCTION(yp_first); PHP_FUNCTION(yp_next); + PHP_FUNCTION(yp_all); + PHP_FUNCTION(yp_cat); + PHP_FUNCTION(yp_errno); + PHP_FUNCTION(yp_err_string); + PHP_MINIT_FUNCTION(yp); + PHP_RINIT_FUNCTION(yp); PHP_MINFO_FUNCTION(yp); + + typedef struct { + int error; + } php_yp_globals; + + #ifdef ZTS + #define YPLS_D php_yp_globals *yp_globals + #define YPLS_DC , YPLS_D + #define YPLS_C yp_globals + #define YPLS_CC , YPLS_C + #define YP(v) (yp_globals->v) + #define YPLS_FETCH() php_yp_globals *yp_globals = ts_resource(yp_globals_id) + #else + #define YPLS_D + #define YPLS_DC + #define YPLS_C + #define YPLS_CC + #define YP(v) (yp_globals.v) + #define YPLS_FETCH() + #endif #else *** yp.c.orig Sat Mar 17 20:09:02 2001 --- yp.c Sat Mar 17 20:26:07 2001 *************** *** 13,18 **** --- 13,19 ---- | license@php.net so we can mail you a copy immediately. | +----------------------------------------------------------------------+ | Authors: Stephanie Wehner <_@r4k.net> | + | Fredrik Ohrn | +----------------------------------------------------------------------+ */ /* $Id: yp.c,v 1.14 2000/06/05 19:47:45 andi Exp $ */ *************** *** 26,31 **** --- 27,42 ---- #include <rpcsvc/ypclnt.h> + /* {{{ thread safety stuff */ + + #ifdef ZTS + int yp_globals_id; + #else + PHP_YP_API php_yp_globals yp_globals; + #endif + + /* }}} */ + function_entry yp_functions[] = { PHP_FE(yp_get_default_domain, NULL) PHP_FE(yp_order, NULL) *************** *** 33,47 **** PHP_FE(yp_match, NULL) PHP_FE(yp_first, NULL) PHP_FE(yp_next, NULL) {NULL, NULL, NULL} }; zend_module_entry yp_module_entry = { "yp", yp_functions, NULL, ! NULL, ! NULL, NULL, PHP_MINFO(yp), STANDARD_MODULE_PROPERTIES --- 44,62 ---- PHP_FE(yp_match, NULL) PHP_FE(yp_first, NULL) PHP_FE(yp_next, NULL) + PHP_FE(yp_all, NULL) + PHP_FE(yp_cat, NULL) + PHP_FE(yp_errno, NULL) + PHP_FE(yp_err_string, NULL) {NULL, NULL, NULL} }; zend_module_entry yp_module_entry = { "yp", yp_functions, + PHP_MINIT(yp), NULL, ! PHP_RINIT(yp), NULL, PHP_MINFO(yp), STANDARD_MODULE_PROPERTIES *************** *** 55,62 **** Returns the domain or false */ PHP_FUNCTION(yp_get_default_domain) { char *outdomain; ! if(yp_get_default_domain(&outdomain)) { RETURN_FALSE; } RETVAL_STRING(outdomain,1); --- 70,79 ---- Returns the domain or false */ PHP_FUNCTION(yp_get_default_domain) { char *outdomain; + YPLS_FETCH(); ! if(YP(error) = yp_get_default_domain(&outdomain)) { ! php_error(E_WARNING, yperr_string (YP(error))); RETURN_FALSE; } RETVAL_STRING(outdomain,1); *************** *** 71,79 **** #if SOLARIS_YP unsigned long outval; #else ! int outval; #endif if((ZEND_NUM_ARGS() != 2) || zend_get_parameters_ex(2,&domain,&map) == FAILURE) { WRONG_PARAM_COUNT; } --- 88,98 ---- #if SOLARIS_YP unsigned long outval; #else ! int outval; #endif + YPLS_FETCH(); + if((ZEND_NUM_ARGS() != 2) || zend_get_parameters_ex(2,&domain,&map) == FAILURE) { WRONG_PARAM_COUNT; } *************** *** 81,87 **** convert_to_string_ex(domain); convert_to_string_ex(map); ! if(yp_order((*domain)->value.str.val,(*map)->value.str.val,&outval)) { RETURN_FALSE; } --- 100,107 ---- convert_to_string_ex(domain); convert_to_string_ex(map); ! if(YP(error) = yp_order(Z_STRVAL_PP (domain), Z_STRVAL_PP (map), &outval)) { ! php_error(E_WARNING, yperr_string (YP(error))); RETURN_FALSE; } *************** *** 94,99 **** --- 114,120 ---- PHP_FUNCTION(yp_master) { pval **domain, **map; char *outname; + YPLS_FETCH(); if((ZEND_NUM_ARGS() != 2) || zend_get_parameters_ex(2,&domain,&map) == FAILURE) { WRONG_PARAM_COUNT; *************** *** 102,108 **** convert_to_string_ex(domain); convert_to_string_ex(map); ! if(yp_master((*domain)->value.str.val,(*map)->value.str.val,&outname)) { RETURN_FALSE; } --- 123,130 ---- convert_to_string_ex(domain); convert_to_string_ex(map); ! if(YP(error) = yp_master(Z_STRVAL_PP (domain), Z_STRVAL_PP (map), &outname)) { ! php_error(E_WARNING, yperr_string (YP(error))); RETURN_FALSE; } *************** *** 116,121 **** --- 138,144 ---- pval **domain, **map, **key; char *outval; int outvallen; + YPLS_FETCH(); if((ZEND_NUM_ARGS() != 3) || zend_get_parameters_ex(3,&domain,&map,&key) == FAILURE) { WRONG_PARAM_COUNT; *************** *** 125,131 **** convert_to_string_ex(map); convert_to_string_ex(key); ! if(yp_match((*domain)->value.str.val,(*map)->value.str.val,(*key)->value.str.val,(*key)->value.str.len,&outval,&outvallen)) { RETURN_FALSE; } --- 148,155 ---- convert_to_string_ex(map); convert_to_string_ex(key); ! if(YP(error) = yp_match(Z_STRVAL_PP (domain), Z_STRVAL_PP (map), Z_STRVAL_PP (key), Z_STRLEN_PP (key), &outval, &outvallen)) { ! php_error(E_WARNING, yperr_string (YP(error))); RETURN_FALSE; } *************** *** 134,144 **** /* }}} */ /* {{{ proto array yp_first(string domain, string map) ! Returns the first key as $var["key"] and the first line as $var["value"] */ PHP_FUNCTION(yp_first) { pval **domain, **map; char *outval, *outkey; int outvallen, outkeylen; if((ZEND_NUM_ARGS() != 2) || zend_get_parameters_ex(2,&domain,&map) == FAILURE) { WRONG_PARAM_COUNT; --- 158,169 ---- /* }}} */ /* {{{ proto array yp_first(string domain, string map) ! Returns the first key as array with $var[$key] and the the line as the value */ PHP_FUNCTION(yp_first) { pval **domain, **map; char *outval, *outkey; int outvallen, outkeylen; + YPLS_FETCH(); if((ZEND_NUM_ARGS() != 2) || zend_get_parameters_ex(2,&domain,&map) == FAILURE) { WRONG_PARAM_COUNT; *************** *** 147,156 **** convert_to_string_ex(domain); convert_to_string_ex(map); ! if(yp_first((*domain)->value.str.val,(*map)->value.str.val,&outkey,&outkeylen,&outval,&outvallen)) { RETURN_FALSE; } array_init(return_value); add_assoc_string(return_value,"key",outkey,1); add_assoc_string(return_value,"value",outval,1); } --- 172,183 ---- convert_to_string_ex(domain); convert_to_string_ex(map); ! if(YP(error) = yp_first(Z_STRVAL_PP (domain), Z_STRVAL_PP (map), &outkey, &outkeylen, &outval, &outvallen)) { ! php_error(E_WARNING, yperr_string (YP(error))); RETURN_FALSE; } array_init(return_value); + add_assoc_string(return_value,outkey,outval,1); add_assoc_string(return_value,"key",outkey,1); add_assoc_string(return_value,"value",outval,1); } *************** *** 162,167 **** --- 189,195 ---- pval **domain, **map, **key; char *outval, *outkey; int outvallen, outkeylen; + YPLS_FETCH(); if((ZEND_NUM_ARGS() != 3) || zend_get_parameters_ex(3,&domain,&map,&key) == FAILURE) { WRONG_PARAM_COUNT; *************** *** 171,177 **** convert_to_string_ex(map); convert_to_string_ex(key); ! if(yp_next((*domain)->value.str.val,(*map)->value.str.val,(*key)->value.str.val,(*key)->value.str.len,&outkey,&outkeylen,&outval,&outvallen)) { RETURN_FALSE; } --- 199,206 ---- convert_to_string_ex(map); convert_to_string_ex(key); ! if(YP(error) = yp_next(Z_STRVAL_PP (domain), Z_STRVAL_PP (map), Z_STRVAL_PP (key), Z_STRLEN_PP (key), &outkey, &outkeylen, &outval, &outvallen)) { ! php_error(E_WARNING, yperr_string (YP(error))); RETURN_FALSE; } *************** *** 180,188 **** } /* }}} */ PHP_MINFO_FUNCTION(yp) { ! php_info_print_table_start(); ! php_info_print_table_row(2, "YP Support", "enabled"); ! php_info_print_table_end(); } #endif /* HAVE_YP */ --- 209,398 ---- } /* }}} */ + static int php_foreach_all (int instatus, char *inkey, int inkeylen, char *inval, int invallen, char *indata) + { + int r; + + zval *status, *key, *value; + zval **args [3] = { &status, &key, &value }; + zval *retval; + + MAKE_STD_ZVAL (status); + ZVAL_LONG (status, ypprot_err (instatus)); + + MAKE_STD_ZVAL (key); + ZVAL_STRINGL (key, inkey, inkeylen, 1); + + MAKE_STD_ZVAL (value); + ZVAL_STRINGL (value, inval, invallen, 1); + + CLS_FETCH(); + + if(call_user_function_ex(CG(function_table), NULL, *((zval **)indata), &retval, 3, args, 0, NULL) != SUCCESS) + { + zend_error(E_ERROR, "Function call failed"); + return 1; + } + + convert_to_long_ex(&retval); + r = Z_LVAL_P (retval); + + zval_ptr_dtor(&retval); + + zval_dtor(status); + FREE_ZVAL(status); + + zval_dtor(key); + FREE_ZVAL(key); + + zval_dtor(value); + FREE_ZVAL(value); + + return r; + } + + /* {{{ proto void yp_all(string domain, string map, string callback) + Traverse the map and call a function on each entry */ + PHP_FUNCTION(yp_all) { + pval **domain, **map, **php_callback; + struct ypall_callback callback; + + if((ZEND_NUM_ARGS() != 3) || zend_get_parameters_ex(3,&domain,&map,&php_callback) == FAILURE) { + WRONG_PARAM_COUNT; + } + + convert_to_string_ex(domain); + convert_to_string_ex(map); + + callback.foreach = php_foreach_all; + callback.data = (char *) php_callback; + + yp_all(Z_STRVAL_PP(domain),Z_STRVAL_PP(map),&callback); + + RETURN_FALSE; + } + /* }}} */ + + static int php_foreach_cat (int instatus, char *inkey, int inkeylen, char *inval, int invallen, char *indata) + { + int err; + + err = ypprot_err (instatus); + + if (!err) + { + if (inkeylen) + add_assoc_string((zval *) indata,inkey,inval,1); + + return 0; + } + + if (err != YPERR_NOMORE) + { + YPLS_FETCH(); + YP(error) = err; + php_error(E_WARNING, yperr_string (err)); + } + + return 0; + } + + /* {{{ proto array yp_cat(string domain, string map) + Return an array containing the entire map */ + PHP_FUNCTION(yp_cat) { + pval **domain, **map; + struct ypall_callback callback; + + if((ZEND_NUM_ARGS() != 2) || zend_get_parameters_ex(2,&domain,&map) == FAILURE) { + WRONG_PARAM_COUNT; + } + + convert_to_string_ex(domain); + convert_to_string_ex(map); + + array_init(return_value); + + callback.foreach = php_foreach_cat; + callback.data = (char *) return_value; + + yp_all(Z_STRVAL_PP(domain),Z_STRVAL_PP(map),&callback); + } + /* }}} */ + + /* {{{ proto int yp_errno() + Returns the error code from the last call or 0 if no error occured */ + PHP_FUNCTION(yp_errno) { + YPLS_FETCH(); + + if((ZEND_NUM_ARGS() != 0)) { + WRONG_PARAM_COUNT; + } + + RETURN_LONG (YP(error)); + } + /* }}} */ + + /* {{{ proto string yp_err_string(int errorcode) + Returns the corresponding error string for the given error code */ + PHP_FUNCTION(yp_err_string) { + pval **error; + char *string; + + if((ZEND_NUM_ARGS() != 1) || zend_get_parameters_ex(1,&error) == FAILURE) { + WRONG_PARAM_COUNT; + } + + convert_to_long_ex(error); + + if((string = yperr_string(Z_LVAL_PP(error))) == NULL) { + RETURN_FALSE; + } + + RETVAL_STRING(string,1); + } + /* }}} */ + + static void php_yp_init_globals(YPLS_D) + { + YP(error) = 0; + } + + PHP_MINIT_FUNCTION(yp) + { + #ifdef ZTS + yp_globals_id = ts_allocate_id(sizeof(php_yp_globals), (ts_allocate_ctor) php_yp_init_globals, NULL); + #else + php_yp_init_globals(YPLS_C); + #endif + + REGISTER_LONG_CONSTANT("YPERR_BADARGS", YPERR_BADARGS, CONST_CS | CONST_PERSISTENT); + REGISTER_LONG_CONSTANT("YPERR_BADDB", YPERR_BADDB, CONST_CS | CONST_PERSISTENT); + REGISTER_LONG_CONSTANT("YPERR_BUSY", YPERR_BUSY, CONST_CS | CONST_PERSISTENT); + REGISTER_LONG_CONSTANT("YPERR_DOMAIN", YPERR_DOMAIN, CONST_CS | CONST_PERSISTENT); + REGISTER_LONG_CONSTANT("YPERR_KEY", YPERR_KEY, CONST_CS | CONST_PERSISTENT); + REGISTER_LONG_CONSTANT("YPERR_MAP", YPERR_MAP, CONST_CS | CONST_PERSISTENT); + REGISTER_LONG_CONSTANT("YPERR_NODOM", YPERR_NODOM, CONST_CS | CONST_PERSISTENT); + REGISTER_LONG_CONSTANT("YPERR_NOMORE", YPERR_NOMORE, CONST_CS | CONST_PERSISTENT); + REGISTER_LONG_CONSTANT("YPERR_PMAP", YPERR_PMAP, CONST_CS | CONST_PERSISTENT); + REGISTER_LONG_CONSTANT("YPERR_RESRC", YPERR_RESRC, CONST_CS | CONST_PERSISTENT); + REGISTER_LONG_CONSTANT("YPERR_RPC", YPERR_RPC, CONST_CS | CONST_PERSISTENT); + REGISTER_LONG_CONSTANT("YPERR_YPBIND", YPERR_YPBIND, CONST_CS | CONST_PERSISTENT); + REGISTER_LONG_CONSTANT("YPERR_YPERR", YPERR_YPERR, CONST_CS | CONST_PERSISTENT); + REGISTER_LONG_CONSTANT("YPERR_YPSERV", YPERR_YPSERV, CONST_CS | CONST_PERSISTENT); + REGISTER_LONG_CONSTANT("YPERR_VERS", YPERR_VERS, CONST_CS | CONST_PERSISTENT); + + return SUCCESS; + } + + PHP_RINIT_FUNCTION(yp) + { + YPLS_FETCH(); + YP(error) = 0; + } + PHP_MINFO_FUNCTION(yp) { ! php_info_print_table_start(); ! php_info_print_table_row(2, "YP Support", "enabled"); ! php_info_print_table_end(); } #endif /* HAVE_YP */

« previous php.dev (#49247) next »