GNU Readline

Introduction

The readline functions implement an interface
to the GNU Readline library. These are functions that provide
editable command lines. An example being the way Bash allows you
to use the arrow keys to insert characters or scroll through
command history. Because of the interactive nature of this
library, it will be of little use for writing Web applications,
but may be useful when writing scripts used from a
command line.

you can also use these functions with the libedit library, a non-gpl
replacement for the readline library. the libedit library is bsd
licensed and available for download from
» http://www.thrysoee.dk/editline/.

Installation

To use these functions you must compile the CGI or CLI version of PHP
with readline support. You need to configure PHP
--with-readline[=DIR].
In order you want to use the libedit readline replacement, configure PHP
--with-libedit[=DIR].

Code Examples / Notes » ref.readline

14-apr-2002 03:17

[Ed. note: you can use fopen("php://stdin", "w") to achieve the same thing, works on both Windows and Unix)]
I wanted to get console input in a PHP script running on windows, so I made a little hack, which is so simple, it is clearly public domain. What I did was write a C++ program to get a line, then output it. Then all that is needed is to exec() that program and capture the output - readline() for windows. The C++ source is as follows:
#include <iostream.h>
#include <string>
void main()
{
string input;
cin >> input;
cout << input;
}
It works wonderfully for my purposes, since I love the PHP language and want to have console input.
Justin Henck

To get all arguments passed to a batch file in one variable
rather than using %1 %2 %3 etc;
:LOOP
if "%1" == "" goto DONE
set args=%args% %1
shift
goto LOOP
:DONE
@c:\\php\\cli\\php.exe script.php %args%
set args=

Readline only reads the window size on startup or on SIGWINCH. This means if the window is resized when not in a readline() call, the next call will have odd behavior due to confusion about the window size.
The work-around is to force Readline to re-read the window size by sending it SIGWINCH. This is accomplished using the async interface, which installs the signal handler but returns control to PHP.
The following function is a drop-in replacement for readline(), but re-reads the window size every time:
<?
function xreadline($prompt)
{
global $xreadline, $xreadline_line;
$code = '$GLOBALS["xreadline"] = false;' .
'$GLOBALS["xreadline_line"] = $line;' .
'readline_callback_handler_remove();';
$cb = create_function('$line', $code);
readline_callback_handler_install($prompt, $cb);
$signal = defined("SIGWINCH") ? SIGWINCH : 28;
posix_kill(posix_getpid(), $signal);
$xreadline = true;
while ($xreadline)
readline_callback_read_char();
return is_null($xreadline_line) ? false : $xreadline_line;
}
?>