Commit Graph

759 Commits

Author SHA1 Message Date
christos a3c16b5549 Provide more compatibility with readline headers; now python-3.6.5 works
when changing 'readline' -> 'edit' in setup.py.
Revert previous conditional setting of unbuffered.
2018-06-09 17:41:55 +00:00
christos 01f10711d4 fix uninitialized 2018-05-04 20:38:26 +00:00
abhinav 2afc179c8f Handle filename autocompletion when the cursor is at a backslash or quote character
For example, handle following case:
	$ touch 'foo bar'
	$ ls foo\<TAB> --> $ ls foo\ bar

Also add test cases for this.

Thanks to Christos for review
2018-05-04 16:39:14 +00:00
abhinav b36e4c8fbf Add '*' and '[' to the list of characters which need escaping during autocompletion 2018-05-02 08:45:03 +00:00
christos 7767204c93 PR/53058: Nikhil Benesch: use correctly typed variables (wchar_t vs wint_t)
as parameters.
2018-02-26 17:36:14 +00:00
christos ff1a7c8cc8 Only FLUSH if we are ending libedit; DRAIN if we suspend for readline.
This allows pasting multiline buffers (Gerry Swislow)
2018-01-01 22:32:46 +00:00
uwe ec59efb2b5 The order in which the arguments to a function are evaluated is
undefined, so don't use va_arg() twice.

PR lib/52849
2017-12-23 18:25:03 +00:00
christos 47abce8f3f For applications that don't issue their own prompt (like python)
don't set unbuffered unless they've already printed the prompt.
This avoids printing the prompt before the application has a chance
to process the input line.
From sjg@
2017-12-08 16:56:23 +00:00
abhinav 3c9fe72fbc Add NULL check after doing memory allocation at a couple of places
ok christos@
2017-10-27 18:16:09 +00:00
abhinav 92a9b5c3ba Add copyright and rcs header to the test program.
Also, remove a test case which was not supposed to be there.
(While that test case works with the changes I committed, testing that
 test case with the test program is not possible in its current form. I'm
 working on that.)
2017-10-15 19:17:30 +00:00
abhinav fa61555626 Add support for escaping special characters when doing filename completion.
For instance if the file name is "foo bar":
$ ls foo<TAB>
should get autocompleted to:
$ ls foo\ bar

Works for similar other characters too, which need escaping.

Also, add an accompanying test program to ensure the escaping is correct
in various scenarios (within quotes, without quotes, with other special characeters)

Thanks to Christos for reviews, help and feedback.
2017-10-15 18:59:00 +00:00
abhinav cf2b6f694e Fix typo in comment 2017-10-11 06:49:03 +00:00
kre 5312bfd48d PR lib/52547 - read_history (readline.c) should now sets history_length.
Patch from Yen Chi Hsuan in the PR, extracted from Apple's version of
readline.c, then modified by me to be consistent about what the return
value really is.
2017-09-17 08:10:08 +00:00
abhinav 7a99333ffa Fix indentation (convert spaces to tab) 2017-09-16 20:40:34 +00:00
christos b2ad969a13 For readline emulation, don't reset the tty to "sane" (cooked) mode if we
did not start this way. Also set and reset the tty on entry and exit from
readline() since this is what readline does.
2017-09-05 18:07:59 +00:00
wiz 3e39477b0a Remove trailing whitespace; convert Xr readline to Nm readline, since
we won't get a readline man page in base.
2017-09-02 06:48:10 +00:00
christos 27916d7c22 PR/51517: Jay West: Tty settings not restored on exit
PR/51518: Jay West: prompt is interleaved with client output

Both these issues are caused by rl_restore_handler not DTRT; fix
it so that it kills the internal libedit state completely. This is
inefficient, but it works.

Also fix:
1. add append_history()/H_NSAVE_FP
2. call the rl_startup_hook before printing the first prompt as documented.
   callint it from rl_initialize breaks python, because the callback ends
   up being invoked before the readline module is installed, and we end up
   dereferencing a NULL pointer.
3. add el_resize_terminal.

With those changes, s/lreadline/ledit/g in python works.
2017-09-01 10:19:10 +00:00
wiz 01869ca4d2 Remove workaround for ancient HTML generation code. 2017-07-03 21:28:48 +00:00
kre e3112e6cea Allow wide characters (properly encoded as byte strings according to LC_CTYPE)
to be (perhaps part of) the "invisible" characters in a prompt, or the
required prompt character which follows the literal sequence (this character
must be one with a printing column width >= 1).  The literal indicator
character (which is just a marker, and not printed anywhere) (the PSlit
parameter in sh(1)) can also be a wide char (passed to libedit as a wchar_t,
encoded as that by sh(1) or other applications that support this.)

Note: this has currently only been tested with everything ascii (C locale).
2017-06-30 20:26:52 +00:00
kre 2977f4314f Fix an obvious, but almost invisible typo (avoid some core dumps). 2017-06-29 02:54:40 +00:00
christos f51c99da13 fix comment 2017-06-27 23:29:12 +00:00
christos 9abf018dda add literal escape sequence support, patterned after the tcsh ones. 2017-06-27 23:25:13 +00:00
christos bec29a0714 remove unused variable 2017-06-27 23:24:19 +00:00
christos 3d09ba7b3f - add literal sequence handling. 2017-06-27 23:23:48 +00:00
christos 06d596a8e9 - handle literal escape sequence printing.
- factor out common code in allocation and freeing of the display.
2017-06-27 23:23:09 +00:00
christos 0a321818f6 mention the limitation of the literal sequence delimiter. 2017-06-27 23:22:20 +00:00
kre 00f36e26a6 Include EDITRC in doc. 2017-06-27 01:22:58 +00:00
kre 4fa1b5993a Make the default editrc file be $EDITRC (from env) if set, falling back
to $HOME/.editrc otherwise.    Better support for this in sh coming.
2017-06-27 00:47:37 +00:00
christos c74ab69c28 Add DragonFly. 2017-05-22 19:16:25 +00:00
abhinav 3a89c5769a When doing filename autocompletion, append a trailing slash at the end of directory
names. We already do this when there is only one completion option but
in case of of multiple completion options, it wasn't being done.

ok christos@
2017-04-21 05:38:03 +00:00
abhinav c626d04131 Add missing argument for H_SET.
ok christos@
2017-04-10 15:02:40 +00:00
christos 3bea32ca4f one extra char for NUL. 2017-03-05 19:23:58 +00:00
christos bd242e1e3e Grow the buffer for event search if there was not enough space.
From Gerry Swislow
2017-03-05 17:30:38 +00:00
christos 1ec9eafabc Make sure we take into account history_base when computing negative history
offsets. (Gerry Swinslow)
2017-01-09 03:09:05 +00:00
christos 4937de3b47 Make sure that argv is NULL terminated since functions like tty_stty rely
on it to be so (Gerry Swinslow)
2017-01-09 02:54:18 +00:00
christos 3d8a0ad9b3 PR/51706: Amir Plivatsky: Fix memory leak 2016-12-11 15:47:06 +00:00
christos f09a36d8a1 Change the way the built-in history works; some programs enter history
with the trailing newline, others don't so don't make any assumptions
about it when printing. Also print the correct event number (generated),
separate the event number from the event with a tab, and visually encode
the string (don't encode tabs and spaces though).
2016-11-07 15:30:18 +00:00
abhinav 0551ffcf66 Fix file name auto completion in one specific case.
For example if you do
$mkdir -p /tmp/dir1/dir2

Then:
$ls /tmp/di <TAB> auto completes to
$ls /tmp/dir1/

Hitting <TAB> again auto completes to
$ls /tmp/dir1/dir2

Whereas it should auto complete to
$ls /tmp/dir1/dir2/


Essentially, in cases like above where you have to hit <TAB> twice to get
to the match and there is only one match (because only one file/sub-directory) then
auto complete doesn't work correctly. It doesn't append a trailing slash (in case
of directory) or a space (in case of a file) to the match name.

I have tested file name completion in sh(1) and symbol completion in gdb after
this change.
2016-10-31 17:46:32 +00:00
christos 931e76f255 export rl_done 2016-10-28 18:32:35 +00:00
christos 1a4590725b pass the stream to the getc function 2016-10-28 18:32:26 +00:00
mbalmer fc37f15ca1 fix typo 2016-09-01 13:23:44 +00:00
christos 78abb1b97e more compatible with readline history functions. 2016-08-24 13:10:59 +00:00
christos d764cbd394 Fix previous to better match readline behavior (Ingo Schwarze) 2016-06-02 21:40:51 +00:00
christos 956883ecd7 From Ingo Schwarze:
In libedit, the only way how H_ENTER can fail is memory exhaustion,
too, and of course it is handled gracefully, returning -1 from
history().  So of course, we will continue to handle it gracefully
in add_history() as well, but we are free to decide what to do with
the library state in this case because GNU just dies...

I think the most reasonable course of action is to simply not change
the library state in any way when add_history() fails due to memory
exhaustion, but just return.

If H_ENTER does not fail, we know that the history now contains at
least one entry, so there is no need any longer to check the H_GETSIZE
return value.  And we can of course always set current_history_valid.

While testing these changes, i noticed three problems so closely
related that i'd like to fix them in the same diff.

 1. libedit has the wrong prototype for add_history().
    GNU readline-6.3 defines it as void add_history(const char *).
    Of course, that is very stupid - no way to report problems to
    the caller!  But the whole point of a compatibility mode is
    being compatible, so we should ultimately change this.
    Of course, changing the prototype of a public symbol requires
    a libedit major bump.  I don't want to do that casually.
    Rather, i will take a note and change the prototype the next
    time we need a libedit major bump for more important reasons.
    For now, let's just always return 0.

 2. While *implicitely* pushing an old entry off the history
    increments history_base in GNU readline, testing reveals that
    *explicitly* deleting one does not.  Again, this is not
    documented, but it applies to both remove_history() and
    stifle_history().  So delete history_base manipulation
    from stifle_history(), which also allows to simplify the
    code and delete two automatic variables.

 3. GNU readline add_history(NULL) crashes with a segfault.
    There is nothing wrong with having a public interface
    behave that way.  Many standard interfaces do, including
    strlen(3).  Such crashes can even be useful to catch
    buggy application programs.
    In libedit/readline.c rev. 1.104, Christos made add_history()
    silently ignore this coding error, according to the commit
    message to hide a bug in nslookup(1).  That change was never
    merged to OpenBSD.  I strongly disagree with this change.
    If nslookup(1) is still broken, that program needs to be
    fixed instead.  In any case, delete the bogus check; hiding
    bugs is dangerous.
2016-06-02 15:11:18 +00:00
christos 94b4dd721e remove the right history entry (Ingo Schwarze) 2016-05-31 19:25:17 +00:00
christos d8509bf6e2 abstract read code to a single function (Ingo Schwarze) 2016-05-25 13:01:11 +00:00
christos fa7ab2f97a el_map.alt can't be NULL here (Ingo Schwarze) 2016-05-24 19:31:27 +00:00
christos 6eacc2dbd2 remove debug read (Ingo Schwarze) 2016-05-24 17:42:54 +00:00
christos 0a374fd7e5 From Ingo Schwarze:
Reduce obfuscation of errno handling. There is only one purpose
non-local errno handling is needed for:  Inside el_wgets(), several
functions call down indirectly to el_wgetc(), many of them via the
dispatch table.  When el_wgetc() fails, it does properly report
failure, but then various cleanup is done which may clobber errno.
But when returning due to failure, el_wgets() wants to have errno
set to the reason of the original read failure, not to the reason
of some subsequent failure of some cleanup operation.  So el_wgetc()
needs to save errno, and if it's non-zero, el_wgets() needs to
restore it on failure.

This core logic is currently obscured by the fact that el_errno
is set and inspected at some additional places where it isn't needed.
Besides, since el_wgetc() and and el_wgets() are both in read.c,
el_errno does not need to be in struct editline, it can and should
be local to read.c in struct el_read_t.

Let's look at what can be simplified.

 1. keymacro_get() abuses el_errno instead of having a proper
    error return code.  Adding that error return code is easy
    because node_trav() already detects the condition and an
    adequate code is already defined.  Returning it, testing
    for it in read_getcmd(), and returning with error from there
    removes the need to inspect el_errno from el_wgets() after
    calling read_getcmd().
    Note that resetting lastchar and cursor and clearing buffer[0]
    is irrelevant.  The code returns from el_wgets() right afterwards.
    Outside el_wgets(), these variables are no longer relevant.
    When el_wgets() is called the next time, it will call ch_reset()
    anyway, resetting the two pointers.  And as long as lastchar
    points to the beginning of the buffer, the contents of the
    buffer won't be used for anything.

 2. read_getcmd() doesn't need to set el_errno again after el_wgetc()
    failure since el_wgetc() already did so.  While here, remove
    the silly "if EOF or error" comments from the el_wgetc()
    return value tests.  It's a public interface documented in a
    manual, so people working on the implementation can obviously
    be expected to know how it works.  It's a case of

      count++;  /* Increment count. */

 3. In the two code paths of el_wgets() that lead up to "goto noedit",
    there is no need to save the errno because nothing that might
    change it happens before returning.

For clarity, since el_wgets() is the function restoring the errno,
also move initializing it to the same function.

Finally, note that restoring errno when the saved value is zero is
wrong.  No library code is ever allowed to clear a previously set
value of errno.  Only application programs are allowed to do that,
and even they usually don't need to do so, except when using certain
ill-designed interfaces like strtol(3).

I tested that the behaviour remains sane in the following cases,
all during execution of el_wgets(3) and with a signal handler
for USR1 installed without SA_RESTART.

 * Enter some text and maybe move around a bit.
   Then send a USR1 signal.
   The signal gets processed, then read_char() resumes reading.
   Send another USR1 signal.
   Now el_wgets() sets errno=EINTR and returns -1.

 * Press Ctrl-V to activate ed-quoted-insert.
   Then send a USR1 signal.
   The signal gets processed, then read_char() resumes reading.
   Send another USR1 signal.
   ed_quoted_insert() returns ed_end_of_file(), i.e. CC_EOF,
   and el_wgets() returns 0.

 * Press a key starting a keyboard macro.
   Then send a USR1 signal.
   The signal gets processed, then read_char() resumes reading.
   Send another USR1 signal.
   Now el_wgets() sets errno=EINTR and returns -1.

 * Press : to enter builtin command mode.
   Start typing a command.
   Then send a USR1 signal.
   The signal gets processed, then read_char() resumes reading.
   Send another USR1 signal.
   Now c_gets() returns -1, ed_command() beeps and returns CC_REFRESH,
   and el_wgets() resumes operation as it should.

I also tested with "el_set(el, EL_EDITMODE, 0)", and it returns
the right value and sets errno correctly.
2016-05-24 15:00:45 +00:00
christos 16467be6f9 documentation improvements (Ingo Schwarze) 2016-05-22 23:54:20 +00:00