# php interactive shell without readline

- Thread: https://externals.io/message/62034
- Original archive: https://news-web.php.net/php.internals/62034
- Started by Ferenc Kovacs on 2012-08-04T19:49:03+00:00
- Messages: 9
- Reply by email to internals@lists.php.net
- More for agents: search with https://externals.io/search.md?q={query} · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

---

## #62034 — Ferenc Kovacs — 2012-08-04T19:49:03+00:00

URL: https://externals.io/message/62034#62034

Hi,

As most of you know, the current php interactive shell is pretty much
useless without compiling php --with-readline, but for the most users this
the first impression what they experience (eg. using the php interactive
shell without readline support).
There is an alternative readline replacement called linenoise (
https://github.com/antirez/linenoise/) written by the author of Redis, and
I would like to know, what do you think about using that for the
interactive shell if php is built without readline support.
It is really small/compact and it is under the two clause BSD license so I
think it would be a good candidate for bundling it in php.
What do you think about it? Is it something worth looking into?

-- 
Ferenc Kovács
@Tyr43l - http://tyrael.hu

---

## #62035 — Andrew Faulds — 2012-08-04T19:50:30+00:00

URL: https://externals.io/message/62034#62035 · In reply to: #62034

Well, I saw it, and I say for a ~600 line library, why not?

Heck, we could maybe reimplement the readline extension with it.


> Hi,
>
> As most of you know, the current php interactive shell is pretty much
> useless without compiling php --with-readline, but for the most users this
> the first impression what they experience (eg. using the php interactive
> shell without readline support).
> There is an alternative readline replacement called linenoise (
> https://github.com/antirez/linenoise/) written by the author of Redis, and
> I would like to know, what do you think about using that for the
> interactive shell if php is built without readline support.
> It is really small/compact and it is under the two clause BSD license so I
> think it would be a good candidate for bundling it in php.
> What do you think about it? Is it something worth looking into?
>

-- 
Andrew Faulds
http://ajf.me/

---

## #62046 — Ivan Enderlin @ Hoa — 2012-08-05T09:50:25+00:00

URL: https://externals.io/message/62034#62046 · In reply to: #62035

Hello,

This project is interesting but it is also possible to do it only with PHP.
As a proof of concept, I would like to share some links with you. First 
one: 
http://hg.hoa-project.net/Central/file/tip/Library/Console/Readline/Readline.php, 
which is highly hackable: we can add mappings (esc, ctrl, abc…), history 
(when pressing key up and key down) and it works as an infinite 
automaton based on the STATE_* constants. And thanks to PHP and its 
static:: keyword, we can overload states, e.g.: 
http://hg.hoa-project.net/Central/file/tip/Library/Console/Readline/Password.php. 
Hop, password is hidden :-).
This library has some limitations: it does not support multibytes 
(Unicode) and has an unlimited history (which can cause memory errors), 
but it is a (advanced-)POC. It works on Unix-based system, actually 
STTY-based shells, and has a pretty simple fallback for DOS. It does not 
require PHP to be compiled --with-readline, it uses stty options 
instead. And finally, it uses around 850 lines of codes (with comments + 
licences).

Best regards :-).


> Well, I saw it, and I say for a ~600 line library, why not?
>
> Heck, we could maybe reimplement the readline extension with it.
>
> 
> > […]
>

-- 
Ivan Enderlin
Developer of Hoa
http://hoa.42/ or http://hoa-project.net/

PhD. student at DISC/Femto-ST (Vesontio) and INRIA (Cassis)
http://disc.univ-fcomte.fr/ and http://www.inria.fr/

Member of HTML and WebApps Working Group of W3C
http://w3.org/

---

## #62052 — johannes@schlueters.de — 2012-08-05T22:18:46+00:00

URL: https://externals.io/message/62034#62052 · In reply to: #62034

> Hi,
> 
> As most of you know, the current php interactive shell is pretty much
> useless without compiling php --with-readline, but for the most users this
> the first impression what they experience (eg. using the php interactive
> shell without readline support).

Note that for 5.4 I refactored the code so a shared readline extension
is good enough by moving all shell related parts to ext/readline. This
should reduce the pain for distribution package users where typically
everything is shared.

> There is an alternative readline replacement called linenoise (
> https://github.com/antirez/linenoise/) written by the author of Redis, and
> I would like to know, what do you think about using that for the
> interactive shell if php is built without readline support.
> It is really small/compact and it is under the two clause BSD license so I
> think it would be a good candidate for bundling it in php.
> What do you think about it? Is it something worth looking into?

There are two ways to do that: Either move the shell code back to
sapi/cli or making ext/readline play nice with linenoise.

When using the first approach make sure that the user can still pick
readline, as readline has features linenoise (currently) hasn't, like
searching using Ctrl+r or configuration of the key combinations. I'd
also like if readline can be built shared but still overwrite the
built-in default.

Doing the second approach has the benefit that the lib can be exported
to userland in the same run. While then there's little benefit as it all
still depends on an extension ...

(Actually there would be a third possibility - writing a linenoise
extension which uses sapi/cli's hook but that requires duplicating most
of the shell code and makes things more complicated, let#s ignore
this ;-) )

What's your plan there?

johannes

---

## #62054 — Ferenc Kovacs — 2012-08-05T23:21:25+00:00

URL: https://externals.io/message/62034#62054 · In reply to: #62052

On Mon, Aug 6, 2012 at 12:18 AM, Johannes Schlüter
<johannes@schlueters.de>wrote:

> 
> > […]
> this
> > […]
>
> Note that for 5.4 I refactored the code so a shared readline extension
> is good enough by moving all shell related parts to ext/readline. This
> should reduce the pain for distribution package users where typically
> everything is shared.
>

for the record that would be https://bugs.php.net/bug.php?id=53878 and
http://git.php.net/?p=php-src.git;a=commit;h=6c734a6b4c9ecf90162cf53fcf5f89864ccabbde
 right?

>
> > […]
> and
> > […]
> I
> > […]
>
> There are two ways to do that: Either move the shell code back to
> sapi/cli or making ext/readline play nice with linenoise.
>

I have to grasp the code a little bit better to answer that, but from a
quick look I think that it was an improvement, that the readline stuff was
moved out from there, so I think that we should keep that.
The linenoise would be always available and I think it would be only a
couple of lines, so I think that it would be possible to put it into
php_cli.c but as you mentioned we should make it such a way, that even the
dynamically loaded readline has to be able to override it.


>
> When using the first approach make sure that the user can still pick
> readline, as readline has features linenoise (currently) hasn't, like
> searching using Ctrl+r or configuration of the key combinations.


agree, linenoise can't replace readline, so in general I think that we
should only use it, if readline isn't available.


> I'd
> also like if readline can be built shared but still overwrite the
> built-in default.
>

agree


>
> Doing the second approach has the benefit that the lib can be exported
> to userland in the same run. While then there's little benefit as it all
> still depends on an extension ...
>
> (Actually there would be a third possibility - writing a linenoise
> extension which uses sapi/cli's hook but that requires duplicating most
> of the shell code and makes things more complicated, let#s ignore
> this ;-) )
>
> What's your plan there?
>
>
to tell you the truth I'm just throwing around ideas and waiting for
somebody with better understanding the actual implementation to help me
out. ;)

-- 
Ferenc Kovács
@Tyr43l - http://tyrael.hu

---

## #62055 — Andrew Faulds — 2012-08-05T23:22:47+00:00

URL: https://externals.io/message/62034#62055 · In reply to: #62054

> On Mon, Aug 6, 2012 at 12:18 AM, Johannes Schlüter
> <johannes@schlueters.de>wrote:
>
> > […]
> for the record that would be https://bugs.php.net/bug.php?id=53878 and
> http://git.php.net/?p=php-src.git;a=commit;h=6c734a6b4c9ecf90162cf53fcf5f89864ccabbde
>   right?
>
> > […]
> I have to grasp the code a little bit better to answer that, but from a
> quick look I think that it was an improvement, that the readline stuff was
> moved out from there, so I think that we should keep that.
> The linenoise would be always available and I think it would be only a
> couple of lines, so I think that it would be possible to put it into
> php_cli.c but as you mentioned we should make it such a way, that even the
> dynamically loaded readline has to be able to override it.
>
>
> > […]
>
> agree, linenoise can't replace readline, so in general I think that we
> should only use it, if readline isn't available.
>
>
> > […]
> agree
>
>
> > […]
> to tell you the truth I'm just throwing around ideas and waiting for
> somebody with better understanding the actual implementation to help me
> out. ;)
>
Well if linenoise() isn't that different, you could use #ifdefs to make 
it use the correct lib at compile-time, no?

-- 
Andrew Faulds
http://ajf.me/

---

## #62057 — Ferenc Kovacs — 2012-08-05T23:37:53+00:00

URL: https://externals.io/message/62034#62057 · In reply to: #62055

> 
>
> > […]
> it use the correct lib at compile-time, no?
>
>
you mean putting the #ifdefs in ext/readline/readline_cli.c ?
yeah that would be easy, but that would be both hackish and doesn't solve
anything, as that ext won't be loaded if readline isn't present.
this is why Johannes said, that we have to either move some code back to
sapi/cli/php_cli.c or integrate linenoise in such a way, that dynamically
loaded extensions could still replace/override it(else we would lose
Johannes work in 5.4 which made it possible to dynamically load readline).
Johannes, please correct me if I'm factually wrong here.

-- 
Ferenc Kovács
@Tyr43l - http://tyrael.hu

---

## #62056 — johannes@schlueters.de — 2012-08-05T23:37:57+00:00

URL: https://externals.io/message/62034#62056 · In reply to: #62055

> > […]

I wonder which is the actual problem to solve? ;-)

Probably a better solution is to simply drop the -a mode without
readline and show a nice error message instead. Unix/Linux users should
have either libedit or readline around. For Windows this is still might
not be a complete solutions, last time I tried I failed with providing
inofficial readline builds (don't remember the problem) but maybe the
win folks can provide libedit based builds (we can't provide readline
based builds due to licensing)

> Well if linenoise() isn't that different, you could use #ifdefs to make 
> it use the correct lib at compile-time, no?

That won't solve the issue that the extension might not be there (unless
you undo the refactoring in 5.4 which means there's no way to override
using readline anymore, as mentioned in my previous mail)

johannes

---

## #62058 — Andrew Faulds — 2012-08-05T23:38:58+00:00

URL: https://externals.io/message/62034#62058 · In reply to: #62057

>
>
> On Mon, Aug 6, 2012 at 1:22 AM, Andrew Faulds <ajf@ajf.me 
> <mailto:ajf@ajf.me>> wrote:
>
>     
>
>         On Mon, Aug 6, 2012 at 12:18 AM, Johannes Schlüter
>         <johannes@schlueters.de <mailto:johannes@schlueters.de>>wrote:
>
>             
>
>                 Hi,
>
>                 As most of you know, the current php interactive shell
>                 is pretty much
>                 useless without compiling php --with-readline, but for
>                 the most users
>
>             this
>
>                 the first impression what they experience (eg. using
>                 the php interactive
>                 shell without readline support).
>
>             Note that for 5.4 I refactored the code so a shared
>             readline extension
>             is good enough by moving all shell related parts to
>             ext/readline. This
>             should reduce the pain for distribution package users
>             where typically
>             everything is shared.
>
>         for the record that would be
>         https://bugs.php.net/bug.php?id=53878 and
>         http://git.php.net/?p=php-src.git;a=commit;h=6c734a6b4c9ecf90162cf53fcf5f89864ccabbde
>           right?
>
>                 There is an alternative readline replacement called
>                 linenoise (
>                 https://github.com/antirez/linenoise/) written by the
>                 author of Redis,
>
>             and
>
>                 I would like to know, what do you think about using
>                 that for the
>                 interactive shell if php is built without readline
>                 support.
>                 It is really small/compact and it is under the two
>                 clause BSD license so
>
>             I
>
>                 think it would be a good candidate for bundling it in php.
>                 What do you think about it? Is it something worth
>                 looking into?
>
>             There are two ways to do that: Either move the shell code
>             back to
>             sapi/cli or making ext/readline play nice with linenoise.
>
>         I have to grasp the code a little bit better to answer that,
>         but from a
>         quick look I think that it was an improvement, that the
>         readline stuff was
>         moved out from there, so I think that we should keep that.
>         The linenoise would be always available and I think it would
>         be only a
>         couple of lines, so I think that it would be possible to put
>         it into
>         php_cli.c but as you mentioned we should make it such a way,
>         that even the
>         dynamically loaded readline has to be able to override it.
>
>
>             When using the first approach make sure that the user can
>             still pick
>             readline, as readline has features linenoise (currently)
>             hasn't, like
>             searching using Ctrl+r or configuration of the key
>             combinations.
>
>
>         agree, linenoise can't replace readline, so in general I think
>         that we
>         should only use it, if readline isn't available.
>
>
>             I'd
>             also like if readline can be built shared but still
>             overwrite the
>             built-in default.
>
>         agree
>
>
>             Doing the second approach has the benefit that the lib can
>             be exported
>             to userland in the same run. While then there's little
>             benefit as it all
>             still depends on an extension ...
>
>             (Actually there would be a third possibility - writing a
>             linenoise
>             extension which uses sapi/cli's hook but that requires
>             duplicating most
>             of the shell code and makes things more complicated, let#s
>             ignore
>             this ;-) )
>
>             What's your plan there?
>
>
>         to tell you the truth I'm just throwing around ideas and
>         waiting for
>         somebody with better understanding the actual implementation
>         to help me
>         out. ;)
>
>     Well if linenoise() isn't that different, you could use #ifdefs to
>     make it use the correct lib at compile-time, no?
>
>
> you mean putting the #ifdefs in ext/readline/readline_cli.c ?
No, I mean moving code bact to sapi/cli/php_cli.c and using either 
readline or linenoise there as appropriate.
> yeah that would be easy, but that would be both hackish and doesn't 
> solve anything, as that ext won't be loaded if readline isn't present.
> this is why Johannes said, that we have to either move some code back 
> to sapi/cli/php_cli.c or integrate linenoise in such a way, that 
> dynamically loaded extensions could still replace/override it(else we 
> would lose Johannes work in 5.4 which made it possible to dynamically 
> load readline).
> Johannes, please correct me if I'm factually wrong here.
>
> -- 
> Ferenc Kovács
> @Tyr43l - http://tyrael.hu


-- 
Andrew Faulds
http://ajf.me/
