]> granicus.if.org Git - php/commitdiff
standardize the callback support to allow array($obj, 'method') callbacks.
authorWez Furlong <wez@php.net>
Fri, 17 Sep 2004 11:26:43 +0000 (11:26 +0000)
committerWez Furlong <wez@php.net>
Fri, 17 Sep 2004 11:26:43 +0000 (11:26 +0000)
Add a couple of utility functions.

ext/readline/readline.c

index 3084b8abd7e8ba6335aeb335a791934d23f925e6..3e3e1a961dd6278928590c007ffa40be05ca417b 100644 (file)
@@ -49,12 +49,14 @@ PHP_FUNCTION(readline_completion_function);
 PHP_FUNCTION(readline_callback_handler_install);
 PHP_FUNCTION(readline_callback_read_char);
 PHP_FUNCTION(readline_callback_handler_remove);
+PHP_FUNCTION(readline_redisplay);
+PHP_FUNCTION(readline_on_new_line);
 
 static zval *_prepped_callback = NULL;
 
 #endif
 
-static char *_readline_completion = NULL;
+static zval *_readline_completion = NULL;
 static zval _readline_array;
 
 PHP_MINIT_FUNCTION(readline);
@@ -78,6 +80,8 @@ static zend_function_entry php_readline_functions[] = {
        PHP_FE(readline_callback_handler_install, NULL)
        PHP_FE(readline_callback_read_char,                     NULL)
        PHP_FE(readline_callback_handler_remove,        NULL)
+       PHP_FE(readline_redisplay, NULL)
+       PHP_FE(readline_on_new_line, NULL)
 #endif
        {NULL, NULL, NULL}
 };
@@ -108,7 +112,7 @@ PHP_MINIT_FUNCTION(readline)
 PHP_RSHUTDOWN_FUNCTION(readline)
 {
        if (_readline_completion) 
-               efree(_readline_completion);
+               FREE_ZVAL(_readline_completion);
 #if HAVE_RL_CALLBACK_READ_CHAR
        if (_prepped_callback) {
                rl_callback_handler_remove();
@@ -121,23 +125,20 @@ PHP_RSHUTDOWN_FUNCTION(readline)
 }
 
 /* }}} */
+
 /* {{{ proto string readline([string prompt]) 
    Reads a line */
 PHP_FUNCTION(readline)
 {
+       char *prompt = NULL;
+       int prompt_len;
        char *result;
-       pval **arg;
-       int ac = ZEND_NUM_ARGS();
-
-       if (ac < 0 || ac > 1 || zend_get_parameters_ex(ac, &arg) == FAILURE) {
-               WRONG_PARAM_COUNT;
-       }
 
-       if (ac == 1) {
-               convert_to_string_ex(arg);
+       if (FAILURE == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s!", &prompt, &prompt_len)) {
+               RETURN_FALSE;
        }
 
-       result = readline(ac?Z_STRVAL_PP(arg):NULL);
+       result = readline(prompt);
 
        if (! result) {
                RETURN_FALSE;
@@ -148,10 +149,11 @@ PHP_FUNCTION(readline)
 }
 
 /* }}} */
+
 /* {{{ proto mixed readline_info([string varname] [, string newvalue]) 
    Gets/sets various internal readline variables. */
 
-#define SAFE_STRING(s) ((s)?(s):"")
+#define SAFE_STRING(s) ((s)?(char*)(s):"")
 
 PHP_FUNCTION(readline_info)
 {
@@ -231,7 +233,7 @@ PHP_FUNCTION(readline_info)
                } else if (! strcasecmp(Z_STRVAL_PP(what),"library_version")) {
                        RETVAL_STRING(SAFE_STRING(rl_library_version),1);
                } else if (! strcasecmp(Z_STRVAL_PP(what),"readline_name")) {
-                       oldstr = rl_readline_name;
+                       oldstr = (char*)rl_readline_name;
                        if (ac == 2) {
                                /* XXX if (rl_readline_name) free(rl_readline_name); */
                                convert_to_string_ex(value);
@@ -381,12 +383,17 @@ static char *_readline_command_generator(char *text,int state)
 static zval *_readline_string_zval(const char *str)
 {
        zval *ret;
-       int len = strlen(str);
+       int len;
+       
        MAKE_STD_ZVAL(ret);
+       
+       if (str) {
+               len = strlen(str);
+               ZVAL_STRINGL(ret, (char*)str, len, 1);
+       } else {
+               ZVAL_NULL(ret);
+       }
 
-       Z_TYPE_P(ret) = IS_STRING;
-       Z_STRLEN_P(ret) = len;
-       Z_STRVAL_P(ret) = estrndup(str, len);
        return ret;
 }
 
@@ -400,25 +407,24 @@ static zval *_readline_long_zval(long l)
        return ret;
 }
 
-static char **_readline_completion_cb(char *text, int start, int end)
+static char **_readline_completion_cb(const char *text, int start, int end)
 { 
-       zval *params[4];
+       zval *params[3];
        int i;
        char **matches = NULL;
        TSRMLS_FETCH();
 
-       params[0]=_readline_string_zval(_readline_completion);
-       params[1]=_readline_string_zval(text);
-       params[2]=_readline_long_zval(start);
-       params[3]=_readline_long_zval(end);
+       params[0]=_readline_string_zval(text);
+       params[1]=_readline_long_zval(start);
+       params[2]=_readline_long_zval(end);
 
-       if (call_user_function(CG(function_table), NULL, params[0], &_readline_array, 3, params+1 TSRMLS_CC) == SUCCESS) {
+       if (call_user_function(CG(function_table), NULL, _readline_completion, &_readline_array, 3, params TSRMLS_CC) == SUCCESS) {
                if (Z_TYPE(_readline_array) == IS_ARRAY) {
                        matches = completion_matches(text,_readline_command_generator);
                }
        }
        
-       for (i = 0; i < 4; i++) {
+       for (i = 0; i < 3; i++) {
                zval_ptr_dtor(&params[i]);
        }
        zval_dtor(&_readline_array);
@@ -428,22 +434,26 @@ static char **_readline_completion_cb(char *text, int start, int end)
 
 PHP_FUNCTION(readline_completion_function)
 {
-       pval **arg;
-       int ac = ZEND_NUM_ARGS();
+       zval *arg = NULL;
 
-       if (ac < 0 || ac > 1 || zend_get_parameters_ex(ac, &arg) == FAILURE) {
-               WRONG_PARAM_COUNT;
+       if (FAILURE == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &arg)) {
+               RETURN_FALSE;
        }
 
-       if (ac == 1) {
-               convert_to_string_ex(arg);
+       char *name = NULL;
+       if (!zend_is_callable(arg, 0, &name)) {
+               php_error_docref(NULL TSRMLS_CC, E_WARNING, "%s is not callable", name);
+               RETURN_FALSE;
+       }
 
-               if (_readline_completion)
-                       efree(_readline_completion);
+       if (_readline_completion)
+               FREE_ZVAL(_readline_completion);
 
-               _readline_completion = estrdup(Z_STRVAL_PP(arg));
-               rl_attempted_completion_function = _readline_completion_cb;
-       }
+       MAKE_STD_ZVAL(_readline_completion);
+       *_readline_completion = *arg;
+       zval_copy_ctor(_readline_completion);
+
+       rl_attempted_completion_function = _readline_completion_cb;
 
        RETURN_TRUE;
 }
@@ -524,6 +534,23 @@ PHP_FUNCTION(readline_callback_handler_remove)
        RETURN_FALSE;
 }
 /* }}} */
+
+/* {{{ proto void readline_redisplay(void)
+   Ask readline to redraw the display */
+PHP_FUNCTION(readline_redisplay)
+{
+       rl_redisplay();
+}
+/* }}} */
+
+/* {{{ proto void readline_on_new_line(void)
+   Inform readline that the cursor has moved to a new line */
+PHP_FUNCTION(readline_on_new_line)
+{
+       rl_on_new_line();
+}
+/* }}} */
+
 #endif