History log of /netbsd-current/lib/libedit/filecomplete.c
Revision (<<< Hide revision tags) (Show revision tags >>>) Date Author Comments
# 1.73 25-Apr-2023 christos

pass lint.


# 1.72 03-Feb-2023 christos

Add a entry point for the complete function for FreeBSD compatibility with sh.


Revision tags: netbsd-10-base
# 1.71 30-Oct-2022 christos

improvements in malloc/free handling.


# 1.70 12-Mar-2022 christos

Fix filename autocompletion for strings like a\)b

An escaped character should unconditionally be skipped together with the
character that does the escaping. For example, in "a\)b" only the ")b"
part was skipped but then the loop stopped at the "\" since it's one of
the characters listed in word_break. (Piotr P. Stefaniak)


# 1.69 26-Sep-2021 christos

- Completion should not add a quote at the end of the line to match an
already quoted quote. (Piotr Stefaniak)
- fix lint unconst warnings for strchr


Revision tags: cjep_sun2x-base1 cjep_sun2x-base cjep_staticlib_x-base1 cjep_staticlib_x-base
# 1.68 05-May-2021 christos

PR/56147: Miroslav Lichvar: Avoid memory leak if strdup fails.


# 1.67 28-Mar-2021 christos

Only unescape when we are quoting and don't add a space if we are quoting
(we already did) (Piotr Stefaniak)


# 1.66 28-Mar-2021 christos

Pass the unescaped filename the the append function so it has to do less work
(for example it can call stat(2) directly (Piotr Stefaniak)


# 1.65 27-Mar-2021 christos

Add fn_complete2() that controls the quoting of the returned match.
Before it was based on the heuristic that we were not supplied an
attempted_completion_function, which worked well because programs
that supplied that function were not shells and did not want/understand
shell quoting. Recently though Piotr Stefaniak wanted to enhance command
completion for the Bourne Shell and this could benefit quoting the returned
command. This function adds an extra flags argument that controls that quoting.


Revision tags: phil-wifi-20200421 phil-wifi-20200411 is-mlppp-base phil-wifi-20200406
# 1.64 05-Jan-2020 abhinav

PR lib/54510 - when user supplied completion function is there,
don't unescape the string to be completed.


# 1.63 05-Jan-2020 tih

Summary: Remove over-simplified extraneous test

The file name matching code in libedit tries to adjust to the presence
of explicit " or ' characters in the input line, but tries too hard.
Remove a conditional that goes overboard, and causes the completion
code to fail if a quoted string is seen before the filename to be
expanded, as in

grep 'foo' bar<TAB>

Before this change, the above would not expand any possible
completions, even if they existed, because it would choose to look for
files whose names started with " bar".


# 1.62 10-Dec-2019 christos

When 'attempted_completion_function' non-NULL, with a 'single_match'
match, the expected space is not being added. Problem observed with
"chronyc" and "sqlite3" tab completion. That functionality got
moved to escape_filename() for the !attempted_completion_function
case, but the non-NULL 'attempted_completion_function' case must
also be handled. (Lonnie Abelbeck)


Revision tags: phil-wifi-20191119
# 1.61 09-Oct-2019 christos

add +1 to strlcpy's (Patrick Welche)


# 1.60 08-Oct-2019 christos

remore error(1) comment


# 1.59 08-Oct-2019 christos

Change strncpy to either memcpy (when we know the len), or strlcpy (when
we used to NUL terminate explicitly.


# 1.58 08-Sep-2019 abhinav

PR lib/54510: Fix file completion inside quotes which broke in rev 1.53

While there also fix handling character appending in the file completions when
inside quotes. For example when inside a quote, if the completion is a directory then
append a '/' but don't close the quote. On the other hand when inside a quote if the
completion is a file name and it is the only match then we can close the quote.


Revision tags: netbsd-9-3-RELEASE netbsd-9-2-RELEASE netbsd-9-1-RELEASE netbsd-9-0-RELEASE netbsd-9-0-RC2 netbsd-9-0-RC1 netbsd-9-base
# 1.57 28-Jul-2019 christos

PR/54415: Ricky Zhou: libedit stats completions for non-file completions
Use the proper completion function and account for the character appended
by the function when computing the number of columns.


# 1.56 23-Jul-2019 christos

PR/54399: S�ren Tempel: Uninitialized memory access in libedit history.
Initialize the buffer using calloc. While here change all malloc(a * sizeof(b))
to calloc(a, sizeof(b)). XXX: should fix realloc similarly.


Revision tags: phil-wifi-20190609
# 1.55 20-Apr-2019 abhinav

PR lib/54131 - declare the loop variable outside the for loop


# 1.54 12-Apr-2019 christos

PR/54117: Paavo Helde: Fix memory overrun: Account for the closing quote
in memory allocation if quoted.


# 1.53 31-Mar-2019 abhinav

Perform quoting of filename completions when there are multiple matches as well

Quoting of special characters in filename completion was implemented for single match
case, this enables it for multiple matches as well. For example:

$ touch 'foo bar'
$ touch 'foo baz'
$ ls fo<TAB>
autocompletes to =>
$ ls foo\ ba
hitting <TAB> again shows:
foo bar foo baz

This required unescaping escape sequences generated during last completion
in order to find the word to complete.

While there, also update the test to include cases for multiple matches.

Reviewed by christos


# 1.52 24-Mar-2019 abhinav

Only quote the completion matches if we are doing filename completion

If the user supplies a value for the attempted_completion_function parameter
then we cannot be sure if the completion is for filename or something else, in such
a case don't attempt to quote the completion matches.

Reviewed by christos

This should address PR lib/54067


Revision tags: pgoyette-compat-20190127 pgoyette-compat-20190118 pgoyette-compat-1226 pgoyette-compat-1126 pgoyette-compat-1020 pgoyette-compat-0930 pgoyette-compat-0906 pgoyette-compat-0728 phil-wifi-base pgoyette-compat-0625 pgoyette-compat-0521
# 1.51 04-May-2018 christos

branches: 1.51.2;
fix uninitialized


# 1.50 04-May-2018 abhinav

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


# 1.49 02-May-2018 abhinav

Add '*' and '[' to the list of characters which need escaping during autocompletion


Revision tags: pgoyette-compat-0502 pgoyette-compat-0422 pgoyette-compat-0415 pgoyette-compat-0407 pgoyette-compat-0330 pgoyette-compat-0322 pgoyette-compat-0315 pgoyette-compat-base
# 1.48 27-Oct-2017 abhinav

branches: 1.48.2;
Add NULL check after doing memory allocation at a couple of places

ok christos@


# 1.47 15-Oct-2017 abhinav

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.


# 1.46 16-Sep-2017 abhinav

Fix indentation (convert spaces to tab)


Revision tags: netbsd-8-2-RELEASE netbsd-8-1-RELEASE netbsd-8-1-RC1 netbsd-8-0-RELEASE netbsd-8-0-RC2 netbsd-8-0-RC1 matt-nb8-mediatek-base perseant-stdc-iso10646-base netbsd-8-base prg-localcount2-base3 prg-localcount2-base2 prg-localcount2-base1 prg-localcount2-base pgoyette-localcount-20170426 bouyer-socketcan-base1
# 1.45 21-Apr-2017 abhinav

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@


Revision tags: pgoyette-localcount-20170320 bouyer-socketcan-base pgoyette-localcount-20170107 pgoyette-localcount-20161104
# 1.44 31-Oct-2016 abhinav

branches: 1.44.2;
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.


Revision tags: localcount-20160914 pgoyette-localcount-20160806 pgoyette-localcount-20160726 pgoyette-localcount-base
# 1.43 11-Apr-2016 christos

branches: 1.43.2;
Char -> wchar_t from Ingo Schwarze.


# 1.42 11-Apr-2016 christos

more macro WIDECHAR undoing from Ingo Schwarze.


# 1.41 09-Apr-2016 christos

Change some 0's to NULL's from Pedro Giffuni


# 1.40 17-Feb-2016 christos

whitespace and header sorting changes (Ingo Schwarze). No functional changes.


# 1.39 16-Feb-2016 christos

More header cleanups from Ingo Schwarze.


# 1.38 16-Feb-2016 christos

more include file cleanup (Ingo Schwarze)


# 1.37 16-Feb-2016 christos

From Ingo Scharze:
Let "el.h" include everything needed for struct editline,
and don't include that stuff multiple times. That also improves
consistency, also avoids circular inclusions, and also makes it
easier to follow what is going on, even though not quite as nice.
But it seems like the best we can do...


# 1.36 16-Feb-2016 christos

cleanup chartype.h includes (Ingo Schwarze)


# 1.35 16-Feb-2016 christos

cleanup inclusion of histedit.h (Ingo Schwarze)


# 1.34 18-Oct-2014 riz

callers's -> caller's


# 1.33 18-Oct-2014 snj

src is too big these days to tolerate superfluous apostrophes. It's
"its", people!


Revision tags: netbsd-7-base tls-earlyentropy-base tls-maxphys-base
# 1.32 05-Jun-2014 christos

branches: 1.32.2;
PR/48876: Dmitriy Grigoryev: Core dump in readline lib on attempted expansion
Make sure we have 2 matches before calling strcmp().


Revision tags: netbsd-6-0-6-RELEASE netbsd-6-1-5-RELEASE yamt-pagecache-base9 yamt-pagecache-tag8 netbsd-6-1-4-RELEASE netbsd-6-0-5-RELEASE riastradh-xf86-video-intel-2-7-1-pre-2-21-15 riastradh-drm2-base3 netbsd-6-1-3-RELEASE netbsd-6-0-4-RELEASE netbsd-6-1-2-RELEASE netbsd-6-0-3-RELEASE netbsd-6-1-1-RELEASE riastradh-drm2-base2 riastradh-drm2-base1 riastradh-drm2-base netbsd-6-0-2-RELEASE netbsd-6-1-RELEASE netbsd-6-1-RC4 netbsd-6-1-RC3 agc-symver-base netbsd-6-1-RC2 netbsd-6-1-RC1 yamt-pagecache-base8 netbsd-6-0-1-RELEASE yamt-pagecache-base7 matt-nb6-plus-nbase yamt-pagecache-base6 netbsd-6-0-RELEASE netbsd-6-0-RC2 matt-nb6-plus-base netbsd-6-0-RC1 yamt-pagecache-base5 yamt-pagecache-base4 netbsd-6-base yamt-pagecache-base3 yamt-pagecache-base2 yamt-pagecache-base
# 1.31 16-Sep-2011 plunky

branches: 1.31.8; 1.31.18;
NULL does not need a cast, here


# 1.30 16-Aug-2011 christos

re-enable -Wconversion


# 1.29 29-Jul-2011 christos

pass -Wconversion


# 1.28 29-Jul-2011 christos

- fix unused params
- unconditionalize vis.h


# 1.27 29-Jul-2011 christos

KNF return (\1); -> return \1;


# 1.26 28-Jul-2011 christos

kill ptr_t and ioctl_t, add * sizeof(*foo) to all allocations.


# 1.25 28-Jul-2011 christos

fix unused variable warnings on systems without _r functions


# 1.24 28-Jul-2011 christos

eliminate alloca for portability
portable getpw{nam,uid}


Revision tags: cherry-xenmp-base bouyer-quota2-nbase bouyer-quota2-base matt-mips64-premerge-20101231
# 1.23 06-Dec-2010 dholland

Improve previous to avoid changing the interface of an externally
exposed function. (But note that this function is neither documented
nor declared in any installed header file, and it probably should not
be externally exposed.) Related to PR 44183, closes PR 44186.


# 1.22 02-Dec-2010 dholland

add const, from PR 44183.


# 1.21 02-Dec-2010 dholland

Fix up bodgy code for printing completion matches; it used to sometimes
skip entries, print (null), run off the end of the array, or occasionally
receive SIGSEGV, and now will, hopefully at least, do none of that.

Based in part on the patch in PR 44183 from Sergio Acereda; I also
did some tidyup and fixed it to print top-to-bottom first like ls(1).


# 1.20 15-Nov-2010 christos

don't increment i twice in the loop. From Michael Byrnes


# 1.19 01-Jun-2010 christos

tidy up memory allocation and don't unnecessarily print "./" before names.


# 1.18 18-Jan-2010 christos

PR/42637: Joachim Kuebart: Shell tab completion crashes due to libedit stack
smashing


# 1.17 30-Dec-2009 christos

Wide character support (UTF-8) from Johny Mattsson; currently disabled.


# 1.16 28-Dec-2009 christos

improve on the listing display by printing only one character after the
filename not two, and no trailing blanks. I will revisit this when I write
the ls-F code.


Revision tags: matt-premerge-20091211 jym-xensuspend-nbase jym-xensuspend-base
# 1.15 16-Feb-2009 christos

fix sign compare issues.


# 1.14 15-Feb-2009 christos

pass lint on _LP64.


# 1.13 26-Jan-2009 apb

branches: 1.13.2;
Define HAVE_STRUCT_DIRENT_D_NAMLEN in config,h, and test it when
deciding whether to use entry->d_namlen or strlen(entry->d_name).
Addresses PR 40477 by Robert Millan.


# 1.12 11-Jan-2009 christos

- insert a space after the recognized string if it was an exact match
- initialize properly the string used for completion.
From Alex Bligh alex at alex dot org dot uk
- Make char constants consistent


Revision tags: netbsd-5-2-3-RELEASE netbsd-5-1-5-RELEASE netbsd-5-2-2-RELEASE netbsd-5-1-4-RELEASE netbsd-5-2-1-RELEASE netbsd-5-1-3-RELEASE netbsd-5-2-RELEASE netbsd-5-2-RC1 netbsd-5-1-2-RELEASE netbsd-5-1-1-RELEASE matt-nb5-mips64-premerge-20101231 matt-nb5-pq3-base netbsd-5-1-RELEASE netbsd-5-1-RC4 matt-nb5-mips64-k15 netbsd-5-1-RC3 netbsd-5-1-RC2 netbsd-5-1-RC1 netbsd-5-0-2-RELEASE matt-nb5-mips64-premerge-20091211 matt-nb5-mips64-u2-k2-k4-k7-k8-k9 matt-nb4-mips64-k7-u2a-k9b matt-nb5-mips64-u1-k1-k5 netbsd-5-0-1-RELEASE netbsd-5-0-RELEASE netbsd-5-0-RC4 netbsd-5-0-RC3 netbsd-5-0-RC2 netbsd-5-0-RC1 netbsd-5-base matt-mips64-base2 wrstuden-revivesa-base-3 wrstuden-revivesa-base-2 wrstuden-revivesa-base-1 yamt-pf42-base4 yamt-pf42-base3 hpcarm-cleanup-nbase yamt-pf42-base2 wrstuden-revivesa-base
# 1.11 29-Apr-2008 martin

Convert to new 2 clause license


Revision tags: netbsd-4-0-1-RELEASE wrstuden-fixsa-newbase yamt-pf42-baseX yamt-pf42-base keiichi-mipv6-base matt-armv6-nbase matt-armv6-prevmlocking wrstuden-fixsa-base-1 netbsd-4-0-RELEASE cube-autoconf-base netbsd-4-0-RC5 netbsd-4-0-RC4 netbsd-4-0-RC3 netbsd-4-0-RC2 netbsd-4-0-RC1 matt-armv6-base matt-mips64-base hpcarm-cleanup-base wrstuden-fixsa-base netbsd-4-base
# 1.10 09-Nov-2006 christos

branches: 1.10.16;
don't use alloca with ssp.


# 1.9 21-Aug-2006 christos

Change to a 3 clause copyright after permission of the holders.


Revision tags: abandoned-netbsd-4-base
# 1.8 03-Aug-2005 christos

Fix reversed test; from Gerry Swislow


# 1.7 11-Jun-2005 christos

PR/30500: Paul Shupak: Inconsistent definition of tilde_expand().
Provide a layer of indirection between the readline compatibility functions
and our internal implementation, so that we have the freedom to change the
function signature.


# 1.6 10-Jun-2005 christos

Bug reported from Martin Dietze:

The place to change the completion_append_character is
usually somewhere in the `rl_completion_entry_function'
callback which is where one usually can distinguish between
file- or dir-like entries to append a slash for dirs etc.

This does no longer work since `fn_complete()' takes the
`append_character' as argument before the callback is executed,
so that changes to the variable `rl_completion_append_character'
have in fact no effect for the current completion.

Fix by adding a function that returns the rl_completion_append_character,
when it gets passed in a filename in readline emulation.


# 1.5 18-May-2005 christos

Make completion_matches non-static since readline wants it.


# 1.4 12-May-2005 christos

PR/30215: Kouichirou Hiratsuka: /bin/sh dumps core with tabcomplete
Don't core-dump when trying to complete an empty line; instead assume ./


# 1.3 09-May-2005 dsl

Use getpwuid_r(getuid(), ...) to expand ~/....
Don't replace ~xyz with /home/xyz when expanding ~


# 1.2 07-May-2005 dsl

gdb directly calls filename_completion_function()


# 1.1 07-May-2005 dsl

Separate out the filename completion functions from the readline() code.
Pass in loads of parameters instead of relying on shed-loads of global
variables to modify the behaviour.
The filename completion code can now be enabled by code that uses el_gets().
(eg /bin/sh)


# 1.72 03-Feb-2023 christos

Add a entry point for the complete function for FreeBSD compatibility with sh.


Revision tags: netbsd-10-base
# 1.71 30-Oct-2022 christos

improvements in malloc/free handling.


# 1.70 12-Mar-2022 christos

Fix filename autocompletion for strings like a\)b

An escaped character should unconditionally be skipped together with the
character that does the escaping. For example, in "a\)b" only the ")b"
part was skipped but then the loop stopped at the "\" since it's one of
the characters listed in word_break. (Piotr P. Stefaniak)


# 1.69 26-Sep-2021 christos

- Completion should not add a quote at the end of the line to match an
already quoted quote. (Piotr Stefaniak)
- fix lint unconst warnings for strchr


Revision tags: cjep_sun2x-base1 cjep_sun2x-base cjep_staticlib_x-base1 cjep_staticlib_x-base
# 1.68 05-May-2021 christos

PR/56147: Miroslav Lichvar: Avoid memory leak if strdup fails.


# 1.67 28-Mar-2021 christos

Only unescape when we are quoting and don't add a space if we are quoting
(we already did) (Piotr Stefaniak)


# 1.66 28-Mar-2021 christos

Pass the unescaped filename the the append function so it has to do less work
(for example it can call stat(2) directly (Piotr Stefaniak)


# 1.65 27-Mar-2021 christos

Add fn_complete2() that controls the quoting of the returned match.
Before it was based on the heuristic that we were not supplied an
attempted_completion_function, which worked well because programs
that supplied that function were not shells and did not want/understand
shell quoting. Recently though Piotr Stefaniak wanted to enhance command
completion for the Bourne Shell and this could benefit quoting the returned
command. This function adds an extra flags argument that controls that quoting.


Revision tags: phil-wifi-20200421 phil-wifi-20200411 is-mlppp-base phil-wifi-20200406
# 1.64 05-Jan-2020 abhinav

PR lib/54510 - when user supplied completion function is there,
don't unescape the string to be completed.


# 1.63 05-Jan-2020 tih

Summary: Remove over-simplified extraneous test

The file name matching code in libedit tries to adjust to the presence
of explicit " or ' characters in the input line, but tries too hard.
Remove a conditional that goes overboard, and causes the completion
code to fail if a quoted string is seen before the filename to be
expanded, as in

grep 'foo' bar<TAB>

Before this change, the above would not expand any possible
completions, even if they existed, because it would choose to look for
files whose names started with " bar".


# 1.62 10-Dec-2019 christos

When 'attempted_completion_function' non-NULL, with a 'single_match'
match, the expected space is not being added. Problem observed with
"chronyc" and "sqlite3" tab completion. That functionality got
moved to escape_filename() for the !attempted_completion_function
case, but the non-NULL 'attempted_completion_function' case must
also be handled. (Lonnie Abelbeck)


Revision tags: phil-wifi-20191119
# 1.61 09-Oct-2019 christos

add +1 to strlcpy's (Patrick Welche)


# 1.60 08-Oct-2019 christos

remore error(1) comment


# 1.59 08-Oct-2019 christos

Change strncpy to either memcpy (when we know the len), or strlcpy (when
we used to NUL terminate explicitly.


# 1.58 08-Sep-2019 abhinav

PR lib/54510: Fix file completion inside quotes which broke in rev 1.53

While there also fix handling character appending in the file completions when
inside quotes. For example when inside a quote, if the completion is a directory then
append a '/' but don't close the quote. On the other hand when inside a quote if the
completion is a file name and it is the only match then we can close the quote.


Revision tags: netbsd-9-3-RELEASE netbsd-9-2-RELEASE netbsd-9-1-RELEASE netbsd-9-0-RELEASE netbsd-9-0-RC2 netbsd-9-0-RC1 netbsd-9-base
# 1.57 28-Jul-2019 christos

PR/54415: Ricky Zhou: libedit stats completions for non-file completions
Use the proper completion function and account for the character appended
by the function when computing the number of columns.


# 1.56 23-Jul-2019 christos

PR/54399: S�ren Tempel: Uninitialized memory access in libedit history.
Initialize the buffer using calloc. While here change all malloc(a * sizeof(b))
to calloc(a, sizeof(b)). XXX: should fix realloc similarly.


Revision tags: phil-wifi-20190609
# 1.55 20-Apr-2019 abhinav

PR lib/54131 - declare the loop variable outside the for loop


# 1.54 12-Apr-2019 christos

PR/54117: Paavo Helde: Fix memory overrun: Account for the closing quote
in memory allocation if quoted.


# 1.53 31-Mar-2019 abhinav

Perform quoting of filename completions when there are multiple matches as well

Quoting of special characters in filename completion was implemented for single match
case, this enables it for multiple matches as well. For example:

$ touch 'foo bar'
$ touch 'foo baz'
$ ls fo<TAB>
autocompletes to =>
$ ls foo\ ba
hitting <TAB> again shows:
foo bar foo baz

This required unescaping escape sequences generated during last completion
in order to find the word to complete.

While there, also update the test to include cases for multiple matches.

Reviewed by christos


# 1.52 24-Mar-2019 abhinav

Only quote the completion matches if we are doing filename completion

If the user supplies a value for the attempted_completion_function parameter
then we cannot be sure if the completion is for filename or something else, in such
a case don't attempt to quote the completion matches.

Reviewed by christos

This should address PR lib/54067


Revision tags: pgoyette-compat-20190127 pgoyette-compat-20190118 pgoyette-compat-1226 pgoyette-compat-1126 pgoyette-compat-1020 pgoyette-compat-0930 pgoyette-compat-0906 pgoyette-compat-0728 phil-wifi-base pgoyette-compat-0625 pgoyette-compat-0521
# 1.51 04-May-2018 christos

branches: 1.51.2;
fix uninitialized


# 1.50 04-May-2018 abhinav

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


# 1.49 02-May-2018 abhinav

Add '*' and '[' to the list of characters which need escaping during autocompletion


Revision tags: pgoyette-compat-0502 pgoyette-compat-0422 pgoyette-compat-0415 pgoyette-compat-0407 pgoyette-compat-0330 pgoyette-compat-0322 pgoyette-compat-0315 pgoyette-compat-base
# 1.48 27-Oct-2017 abhinav

branches: 1.48.2;
Add NULL check after doing memory allocation at a couple of places

ok christos@


# 1.47 15-Oct-2017 abhinav

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.


# 1.46 16-Sep-2017 abhinav

Fix indentation (convert spaces to tab)


Revision tags: netbsd-8-2-RELEASE netbsd-8-1-RELEASE netbsd-8-1-RC1 netbsd-8-0-RELEASE netbsd-8-0-RC2 netbsd-8-0-RC1 matt-nb8-mediatek-base perseant-stdc-iso10646-base netbsd-8-base prg-localcount2-base3 prg-localcount2-base2 prg-localcount2-base1 prg-localcount2-base pgoyette-localcount-20170426 bouyer-socketcan-base1
# 1.45 21-Apr-2017 abhinav

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@


Revision tags: pgoyette-localcount-20170320 bouyer-socketcan-base pgoyette-localcount-20170107 pgoyette-localcount-20161104
# 1.44 31-Oct-2016 abhinav

branches: 1.44.2;
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.


Revision tags: localcount-20160914 pgoyette-localcount-20160806 pgoyette-localcount-20160726 pgoyette-localcount-base
# 1.43 11-Apr-2016 christos

branches: 1.43.2;
Char -> wchar_t from Ingo Schwarze.


# 1.42 11-Apr-2016 christos

more macro WIDECHAR undoing from Ingo Schwarze.


# 1.41 09-Apr-2016 christos

Change some 0's to NULL's from Pedro Giffuni


# 1.40 17-Feb-2016 christos

whitespace and header sorting changes (Ingo Schwarze). No functional changes.


# 1.39 16-Feb-2016 christos

More header cleanups from Ingo Schwarze.


# 1.38 16-Feb-2016 christos

more include file cleanup (Ingo Schwarze)


# 1.37 16-Feb-2016 christos

From Ingo Scharze:
Let "el.h" include everything needed for struct editline,
and don't include that stuff multiple times. That also improves
consistency, also avoids circular inclusions, and also makes it
easier to follow what is going on, even though not quite as nice.
But it seems like the best we can do...


# 1.36 16-Feb-2016 christos

cleanup chartype.h includes (Ingo Schwarze)


# 1.35 16-Feb-2016 christos

cleanup inclusion of histedit.h (Ingo Schwarze)


# 1.34 18-Oct-2014 riz

callers's -> caller's


# 1.33 18-Oct-2014 snj

src is too big these days to tolerate superfluous apostrophes. It's
"its", people!


Revision tags: netbsd-7-base tls-earlyentropy-base tls-maxphys-base
# 1.32 05-Jun-2014 christos

branches: 1.32.2;
PR/48876: Dmitriy Grigoryev: Core dump in readline lib on attempted expansion
Make sure we have 2 matches before calling strcmp().


Revision tags: netbsd-6-0-6-RELEASE netbsd-6-1-5-RELEASE yamt-pagecache-base9 yamt-pagecache-tag8 netbsd-6-1-4-RELEASE netbsd-6-0-5-RELEASE riastradh-xf86-video-intel-2-7-1-pre-2-21-15 riastradh-drm2-base3 netbsd-6-1-3-RELEASE netbsd-6-0-4-RELEASE netbsd-6-1-2-RELEASE netbsd-6-0-3-RELEASE netbsd-6-1-1-RELEASE riastradh-drm2-base2 riastradh-drm2-base1 riastradh-drm2-base netbsd-6-0-2-RELEASE netbsd-6-1-RELEASE netbsd-6-1-RC4 netbsd-6-1-RC3 agc-symver-base netbsd-6-1-RC2 netbsd-6-1-RC1 yamt-pagecache-base8 netbsd-6-0-1-RELEASE yamt-pagecache-base7 matt-nb6-plus-nbase yamt-pagecache-base6 netbsd-6-0-RELEASE netbsd-6-0-RC2 matt-nb6-plus-base netbsd-6-0-RC1 yamt-pagecache-base5 yamt-pagecache-base4 netbsd-6-base yamt-pagecache-base3 yamt-pagecache-base2 yamt-pagecache-base
# 1.31 16-Sep-2011 plunky

branches: 1.31.8; 1.31.18;
NULL does not need a cast, here


# 1.30 16-Aug-2011 christos

re-enable -Wconversion


# 1.29 29-Jul-2011 christos

pass -Wconversion


# 1.28 29-Jul-2011 christos

- fix unused params
- unconditionalize vis.h


# 1.27 29-Jul-2011 christos

KNF return (\1); -> return \1;


# 1.26 28-Jul-2011 christos

kill ptr_t and ioctl_t, add * sizeof(*foo) to all allocations.


# 1.25 28-Jul-2011 christos

fix unused variable warnings on systems without _r functions


# 1.24 28-Jul-2011 christos

eliminate alloca for portability
portable getpw{nam,uid}


Revision tags: cherry-xenmp-base bouyer-quota2-nbase bouyer-quota2-base matt-mips64-premerge-20101231
# 1.23 06-Dec-2010 dholland

Improve previous to avoid changing the interface of an externally
exposed function. (But note that this function is neither documented
nor declared in any installed header file, and it probably should not
be externally exposed.) Related to PR 44183, closes PR 44186.


# 1.22 02-Dec-2010 dholland

add const, from PR 44183.


# 1.21 02-Dec-2010 dholland

Fix up bodgy code for printing completion matches; it used to sometimes
skip entries, print (null), run off the end of the array, or occasionally
receive SIGSEGV, and now will, hopefully at least, do none of that.

Based in part on the patch in PR 44183 from Sergio Acereda; I also
did some tidyup and fixed it to print top-to-bottom first like ls(1).


# 1.20 15-Nov-2010 christos

don't increment i twice in the loop. From Michael Byrnes


# 1.19 01-Jun-2010 christos

tidy up memory allocation and don't unnecessarily print "./" before names.


# 1.18 18-Jan-2010 christos

PR/42637: Joachim Kuebart: Shell tab completion crashes due to libedit stack
smashing


# 1.17 30-Dec-2009 christos

Wide character support (UTF-8) from Johny Mattsson; currently disabled.


# 1.16 28-Dec-2009 christos

improve on the listing display by printing only one character after the
filename not two, and no trailing blanks. I will revisit this when I write
the ls-F code.


Revision tags: matt-premerge-20091211 jym-xensuspend-nbase jym-xensuspend-base
# 1.15 16-Feb-2009 christos

fix sign compare issues.


# 1.14 15-Feb-2009 christos

pass lint on _LP64.


# 1.13 26-Jan-2009 apb

branches: 1.13.2;
Define HAVE_STRUCT_DIRENT_D_NAMLEN in config,h, and test it when
deciding whether to use entry->d_namlen or strlen(entry->d_name).
Addresses PR 40477 by Robert Millan.


# 1.12 11-Jan-2009 christos

- insert a space after the recognized string if it was an exact match
- initialize properly the string used for completion.
From Alex Bligh alex at alex dot org dot uk
- Make char constants consistent


Revision tags: netbsd-5-2-3-RELEASE netbsd-5-1-5-RELEASE netbsd-5-2-2-RELEASE netbsd-5-1-4-RELEASE netbsd-5-2-1-RELEASE netbsd-5-1-3-RELEASE netbsd-5-2-RELEASE netbsd-5-2-RC1 netbsd-5-1-2-RELEASE netbsd-5-1-1-RELEASE matt-nb5-mips64-premerge-20101231 matt-nb5-pq3-base netbsd-5-1-RELEASE netbsd-5-1-RC4 matt-nb5-mips64-k15 netbsd-5-1-RC3 netbsd-5-1-RC2 netbsd-5-1-RC1 netbsd-5-0-2-RELEASE matt-nb5-mips64-premerge-20091211 matt-nb5-mips64-u2-k2-k4-k7-k8-k9 matt-nb4-mips64-k7-u2a-k9b matt-nb5-mips64-u1-k1-k5 netbsd-5-0-1-RELEASE netbsd-5-0-RELEASE netbsd-5-0-RC4 netbsd-5-0-RC3 netbsd-5-0-RC2 netbsd-5-0-RC1 netbsd-5-base matt-mips64-base2 wrstuden-revivesa-base-3 wrstuden-revivesa-base-2 wrstuden-revivesa-base-1 yamt-pf42-base4 yamt-pf42-base3 hpcarm-cleanup-nbase yamt-pf42-base2 wrstuden-revivesa-base
# 1.11 29-Apr-2008 martin

Convert to new 2 clause license


Revision tags: netbsd-4-0-1-RELEASE wrstuden-fixsa-newbase yamt-pf42-baseX yamt-pf42-base keiichi-mipv6-base matt-armv6-nbase matt-armv6-prevmlocking wrstuden-fixsa-base-1 netbsd-4-0-RELEASE cube-autoconf-base netbsd-4-0-RC5 netbsd-4-0-RC4 netbsd-4-0-RC3 netbsd-4-0-RC2 netbsd-4-0-RC1 matt-armv6-base matt-mips64-base hpcarm-cleanup-base wrstuden-fixsa-base netbsd-4-base
# 1.10 09-Nov-2006 christos

branches: 1.10.16;
don't use alloca with ssp.


# 1.9 21-Aug-2006 christos

Change to a 3 clause copyright after permission of the holders.


Revision tags: abandoned-netbsd-4-base
# 1.8 03-Aug-2005 christos

Fix reversed test; from Gerry Swislow


# 1.7 11-Jun-2005 christos

PR/30500: Paul Shupak: Inconsistent definition of tilde_expand().
Provide a layer of indirection between the readline compatibility functions
and our internal implementation, so that we have the freedom to change the
function signature.


# 1.6 10-Jun-2005 christos

Bug reported from Martin Dietze:

The place to change the completion_append_character is
usually somewhere in the `rl_completion_entry_function'
callback which is where one usually can distinguish between
file- or dir-like entries to append a slash for dirs etc.

This does no longer work since `fn_complete()' takes the
`append_character' as argument before the callback is executed,
so that changes to the variable `rl_completion_append_character'
have in fact no effect for the current completion.

Fix by adding a function that returns the rl_completion_append_character,
when it gets passed in a filename in readline emulation.


# 1.5 18-May-2005 christos

Make completion_matches non-static since readline wants it.


# 1.4 12-May-2005 christos

PR/30215: Kouichirou Hiratsuka: /bin/sh dumps core with tabcomplete
Don't core-dump when trying to complete an empty line; instead assume ./


# 1.3 09-May-2005 dsl

Use getpwuid_r(getuid(), ...) to expand ~/....
Don't replace ~xyz with /home/xyz when expanding ~


# 1.2 07-May-2005 dsl

gdb directly calls filename_completion_function()


# 1.1 07-May-2005 dsl

Separate out the filename completion functions from the readline() code.
Pass in loads of parameters instead of relying on shed-loads of global
variables to modify the behaviour.
The filename completion code can now be enabled by code that uses el_gets().
(eg /bin/sh)


# 1.71 30-Oct-2022 christos

improvements in malloc/free handling.


# 1.70 12-Mar-2022 christos

Fix filename autocompletion for strings like a\)b

An escaped character should unconditionally be skipped together with the
character that does the escaping. For example, in "a\)b" only the ")b"
part was skipped but then the loop stopped at the "\" since it's one of
the characters listed in word_break. (Piotr P. Stefaniak)


# 1.69 26-Sep-2021 christos

- Completion should not add a quote at the end of the line to match an
already quoted quote. (Piotr Stefaniak)
- fix lint unconst warnings for strchr


Revision tags: cjep_sun2x-base1 cjep_sun2x-base cjep_staticlib_x-base1 cjep_staticlib_x-base
# 1.68 05-May-2021 christos

PR/56147: Miroslav Lichvar: Avoid memory leak if strdup fails.


# 1.67 28-Mar-2021 christos

Only unescape when we are quoting and don't add a space if we are quoting
(we already did) (Piotr Stefaniak)


# 1.66 28-Mar-2021 christos

Pass the unescaped filename the the append function so it has to do less work
(for example it can call stat(2) directly (Piotr Stefaniak)


# 1.65 27-Mar-2021 christos

Add fn_complete2() that controls the quoting of the returned match.
Before it was based on the heuristic that we were not supplied an
attempted_completion_function, which worked well because programs
that supplied that function were not shells and did not want/understand
shell quoting. Recently though Piotr Stefaniak wanted to enhance command
completion for the Bourne Shell and this could benefit quoting the returned
command. This function adds an extra flags argument that controls that quoting.


Revision tags: phil-wifi-20200421 phil-wifi-20200411 is-mlppp-base phil-wifi-20200406
# 1.64 05-Jan-2020 abhinav

PR lib/54510 - when user supplied completion function is there,
don't unescape the string to be completed.


# 1.63 05-Jan-2020 tih

Summary: Remove over-simplified extraneous test

The file name matching code in libedit tries to adjust to the presence
of explicit " or ' characters in the input line, but tries too hard.
Remove a conditional that goes overboard, and causes the completion
code to fail if a quoted string is seen before the filename to be
expanded, as in

grep 'foo' bar<TAB>

Before this change, the above would not expand any possible
completions, even if they existed, because it would choose to look for
files whose names started with " bar".


# 1.62 10-Dec-2019 christos

When 'attempted_completion_function' non-NULL, with a 'single_match'
match, the expected space is not being added. Problem observed with
"chronyc" and "sqlite3" tab completion. That functionality got
moved to escape_filename() for the !attempted_completion_function
case, but the non-NULL 'attempted_completion_function' case must
also be handled. (Lonnie Abelbeck)


Revision tags: phil-wifi-20191119
# 1.61 09-Oct-2019 christos

add +1 to strlcpy's (Patrick Welche)


# 1.60 08-Oct-2019 christos

remore error(1) comment


# 1.59 08-Oct-2019 christos

Change strncpy to either memcpy (when we know the len), or strlcpy (when
we used to NUL terminate explicitly.


# 1.58 08-Sep-2019 abhinav

PR lib/54510: Fix file completion inside quotes which broke in rev 1.53

While there also fix handling character appending in the file completions when
inside quotes. For example when inside a quote, if the completion is a directory then
append a '/' but don't close the quote. On the other hand when inside a quote if the
completion is a file name and it is the only match then we can close the quote.


Revision tags: netbsd-9-3-RELEASE netbsd-9-2-RELEASE netbsd-9-1-RELEASE netbsd-9-0-RELEASE netbsd-9-0-RC2 netbsd-9-0-RC1 netbsd-9-base
# 1.57 28-Jul-2019 christos

PR/54415: Ricky Zhou: libedit stats completions for non-file completions
Use the proper completion function and account for the character appended
by the function when computing the number of columns.


# 1.56 23-Jul-2019 christos

PR/54399: S�ren Tempel: Uninitialized memory access in libedit history.
Initialize the buffer using calloc. While here change all malloc(a * sizeof(b))
to calloc(a, sizeof(b)). XXX: should fix realloc similarly.


Revision tags: phil-wifi-20190609
# 1.55 20-Apr-2019 abhinav

PR lib/54131 - declare the loop variable outside the for loop


# 1.54 12-Apr-2019 christos

PR/54117: Paavo Helde: Fix memory overrun: Account for the closing quote
in memory allocation if quoted.


# 1.53 31-Mar-2019 abhinav

Perform quoting of filename completions when there are multiple matches as well

Quoting of special characters in filename completion was implemented for single match
case, this enables it for multiple matches as well. For example:

$ touch 'foo bar'
$ touch 'foo baz'
$ ls fo<TAB>
autocompletes to =>
$ ls foo\ ba
hitting <TAB> again shows:
foo bar foo baz

This required unescaping escape sequences generated during last completion
in order to find the word to complete.

While there, also update the test to include cases for multiple matches.

Reviewed by christos


# 1.52 24-Mar-2019 abhinav

Only quote the completion matches if we are doing filename completion

If the user supplies a value for the attempted_completion_function parameter
then we cannot be sure if the completion is for filename or something else, in such
a case don't attempt to quote the completion matches.

Reviewed by christos

This should address PR lib/54067


Revision tags: pgoyette-compat-20190127 pgoyette-compat-20190118 pgoyette-compat-1226 pgoyette-compat-1126 pgoyette-compat-1020 pgoyette-compat-0930 pgoyette-compat-0906 pgoyette-compat-0728 phil-wifi-base pgoyette-compat-0625 pgoyette-compat-0521
# 1.51 04-May-2018 christos

branches: 1.51.2;
fix uninitialized


# 1.50 04-May-2018 abhinav

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


# 1.49 02-May-2018 abhinav

Add '*' and '[' to the list of characters which need escaping during autocompletion


Revision tags: pgoyette-compat-0502 pgoyette-compat-0422 pgoyette-compat-0415 pgoyette-compat-0407 pgoyette-compat-0330 pgoyette-compat-0322 pgoyette-compat-0315 pgoyette-compat-base
# 1.48 27-Oct-2017 abhinav

branches: 1.48.2;
Add NULL check after doing memory allocation at a couple of places

ok christos@


# 1.47 15-Oct-2017 abhinav

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.


# 1.46 16-Sep-2017 abhinav

Fix indentation (convert spaces to tab)


Revision tags: netbsd-8-2-RELEASE netbsd-8-1-RELEASE netbsd-8-1-RC1 netbsd-8-0-RELEASE netbsd-8-0-RC2 netbsd-8-0-RC1 matt-nb8-mediatek-base perseant-stdc-iso10646-base netbsd-8-base prg-localcount2-base3 prg-localcount2-base2 prg-localcount2-base1 prg-localcount2-base pgoyette-localcount-20170426 bouyer-socketcan-base1
# 1.45 21-Apr-2017 abhinav

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@


Revision tags: pgoyette-localcount-20170320 bouyer-socketcan-base pgoyette-localcount-20170107 pgoyette-localcount-20161104
# 1.44 31-Oct-2016 abhinav

branches: 1.44.2;
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.


Revision tags: localcount-20160914 pgoyette-localcount-20160806 pgoyette-localcount-20160726 pgoyette-localcount-base
# 1.43 11-Apr-2016 christos

branches: 1.43.2;
Char -> wchar_t from Ingo Schwarze.


# 1.42 11-Apr-2016 christos

more macro WIDECHAR undoing from Ingo Schwarze.


# 1.41 09-Apr-2016 christos

Change some 0's to NULL's from Pedro Giffuni


# 1.40 17-Feb-2016 christos

whitespace and header sorting changes (Ingo Schwarze). No functional changes.


# 1.39 16-Feb-2016 christos

More header cleanups from Ingo Schwarze.


# 1.38 16-Feb-2016 christos

more include file cleanup (Ingo Schwarze)


# 1.37 16-Feb-2016 christos

From Ingo Scharze:
Let "el.h" include everything needed for struct editline,
and don't include that stuff multiple times. That also improves
consistency, also avoids circular inclusions, and also makes it
easier to follow what is going on, even though not quite as nice.
But it seems like the best we can do...


# 1.36 16-Feb-2016 christos

cleanup chartype.h includes (Ingo Schwarze)


# 1.35 16-Feb-2016 christos

cleanup inclusion of histedit.h (Ingo Schwarze)


# 1.34 18-Oct-2014 riz

callers's -> caller's


# 1.33 18-Oct-2014 snj

src is too big these days to tolerate superfluous apostrophes. It's
"its", people!


Revision tags: netbsd-7-base tls-earlyentropy-base tls-maxphys-base
# 1.32 05-Jun-2014 christos

branches: 1.32.2;
PR/48876: Dmitriy Grigoryev: Core dump in readline lib on attempted expansion
Make sure we have 2 matches before calling strcmp().


Revision tags: netbsd-6-0-6-RELEASE netbsd-6-1-5-RELEASE yamt-pagecache-base9 yamt-pagecache-tag8 netbsd-6-1-4-RELEASE netbsd-6-0-5-RELEASE riastradh-xf86-video-intel-2-7-1-pre-2-21-15 riastradh-drm2-base3 netbsd-6-1-3-RELEASE netbsd-6-0-4-RELEASE netbsd-6-1-2-RELEASE netbsd-6-0-3-RELEASE netbsd-6-1-1-RELEASE riastradh-drm2-base2 riastradh-drm2-base1 riastradh-drm2-base netbsd-6-0-2-RELEASE netbsd-6-1-RELEASE netbsd-6-1-RC4 netbsd-6-1-RC3 agc-symver-base netbsd-6-1-RC2 netbsd-6-1-RC1 yamt-pagecache-base8 netbsd-6-0-1-RELEASE yamt-pagecache-base7 matt-nb6-plus-nbase yamt-pagecache-base6 netbsd-6-0-RELEASE netbsd-6-0-RC2 matt-nb6-plus-base netbsd-6-0-RC1 yamt-pagecache-base5 yamt-pagecache-base4 netbsd-6-base yamt-pagecache-base3 yamt-pagecache-base2 yamt-pagecache-base
# 1.31 16-Sep-2011 plunky

branches: 1.31.8; 1.31.18;
NULL does not need a cast, here


# 1.30 16-Aug-2011 christos

re-enable -Wconversion


# 1.29 29-Jul-2011 christos

pass -Wconversion


# 1.28 29-Jul-2011 christos

- fix unused params
- unconditionalize vis.h


# 1.27 29-Jul-2011 christos

KNF return (\1); -> return \1;


# 1.26 28-Jul-2011 christos

kill ptr_t and ioctl_t, add * sizeof(*foo) to all allocations.


# 1.25 28-Jul-2011 christos

fix unused variable warnings on systems without _r functions


# 1.24 28-Jul-2011 christos

eliminate alloca for portability
portable getpw{nam,uid}


Revision tags: cherry-xenmp-base bouyer-quota2-nbase bouyer-quota2-base matt-mips64-premerge-20101231
# 1.23 06-Dec-2010 dholland

Improve previous to avoid changing the interface of an externally
exposed function. (But note that this function is neither documented
nor declared in any installed header file, and it probably should not
be externally exposed.) Related to PR 44183, closes PR 44186.


# 1.22 02-Dec-2010 dholland

add const, from PR 44183.


# 1.21 02-Dec-2010 dholland

Fix up bodgy code for printing completion matches; it used to sometimes
skip entries, print (null), run off the end of the array, or occasionally
receive SIGSEGV, and now will, hopefully at least, do none of that.

Based in part on the patch in PR 44183 from Sergio Acereda; I also
did some tidyup and fixed it to print top-to-bottom first like ls(1).


# 1.20 15-Nov-2010 christos

don't increment i twice in the loop. From Michael Byrnes


# 1.19 01-Jun-2010 christos

tidy up memory allocation and don't unnecessarily print "./" before names.


# 1.18 18-Jan-2010 christos

PR/42637: Joachim Kuebart: Shell tab completion crashes due to libedit stack
smashing


# 1.17 30-Dec-2009 christos

Wide character support (UTF-8) from Johny Mattsson; currently disabled.


# 1.16 28-Dec-2009 christos

improve on the listing display by printing only one character after the
filename not two, and no trailing blanks. I will revisit this when I write
the ls-F code.


Revision tags: matt-premerge-20091211 jym-xensuspend-nbase jym-xensuspend-base
# 1.15 16-Feb-2009 christos

fix sign compare issues.


# 1.14 15-Feb-2009 christos

pass lint on _LP64.


# 1.13 26-Jan-2009 apb

branches: 1.13.2;
Define HAVE_STRUCT_DIRENT_D_NAMLEN in config,h, and test it when
deciding whether to use entry->d_namlen or strlen(entry->d_name).
Addresses PR 40477 by Robert Millan.


# 1.12 11-Jan-2009 christos

- insert a space after the recognized string if it was an exact match
- initialize properly the string used for completion.
From Alex Bligh alex at alex dot org dot uk
- Make char constants consistent


Revision tags: netbsd-5-2-3-RELEASE netbsd-5-1-5-RELEASE netbsd-5-2-2-RELEASE netbsd-5-1-4-RELEASE netbsd-5-2-1-RELEASE netbsd-5-1-3-RELEASE netbsd-5-2-RELEASE netbsd-5-2-RC1 netbsd-5-1-2-RELEASE netbsd-5-1-1-RELEASE matt-nb5-mips64-premerge-20101231 matt-nb5-pq3-base netbsd-5-1-RELEASE netbsd-5-1-RC4 matt-nb5-mips64-k15 netbsd-5-1-RC3 netbsd-5-1-RC2 netbsd-5-1-RC1 netbsd-5-0-2-RELEASE matt-nb5-mips64-premerge-20091211 matt-nb5-mips64-u2-k2-k4-k7-k8-k9 matt-nb4-mips64-k7-u2a-k9b matt-nb5-mips64-u1-k1-k5 netbsd-5-0-1-RELEASE netbsd-5-0-RELEASE netbsd-5-0-RC4 netbsd-5-0-RC3 netbsd-5-0-RC2 netbsd-5-0-RC1 netbsd-5-base matt-mips64-base2 wrstuden-revivesa-base-3 wrstuden-revivesa-base-2 wrstuden-revivesa-base-1 yamt-pf42-base4 yamt-pf42-base3 hpcarm-cleanup-nbase yamt-pf42-base2 wrstuden-revivesa-base
# 1.11 29-Apr-2008 martin

Convert to new 2 clause license


Revision tags: netbsd-4-0-1-RELEASE wrstuden-fixsa-newbase yamt-pf42-baseX yamt-pf42-base keiichi-mipv6-base matt-armv6-nbase matt-armv6-prevmlocking wrstuden-fixsa-base-1 netbsd-4-0-RELEASE cube-autoconf-base netbsd-4-0-RC5 netbsd-4-0-RC4 netbsd-4-0-RC3 netbsd-4-0-RC2 netbsd-4-0-RC1 matt-armv6-base matt-mips64-base hpcarm-cleanup-base wrstuden-fixsa-base netbsd-4-base
# 1.10 09-Nov-2006 christos

branches: 1.10.16;
don't use alloca with ssp.


# 1.9 21-Aug-2006 christos

Change to a 3 clause copyright after permission of the holders.


Revision tags: abandoned-netbsd-4-base
# 1.8 03-Aug-2005 christos

Fix reversed test; from Gerry Swislow


# 1.7 11-Jun-2005 christos

PR/30500: Paul Shupak: Inconsistent definition of tilde_expand().
Provide a layer of indirection between the readline compatibility functions
and our internal implementation, so that we have the freedom to change the
function signature.


# 1.6 10-Jun-2005 christos

Bug reported from Martin Dietze:

The place to change the completion_append_character is
usually somewhere in the `rl_completion_entry_function'
callback which is where one usually can distinguish between
file- or dir-like entries to append a slash for dirs etc.

This does no longer work since `fn_complete()' takes the
`append_character' as argument before the callback is executed,
so that changes to the variable `rl_completion_append_character'
have in fact no effect for the current completion.

Fix by adding a function that returns the rl_completion_append_character,
when it gets passed in a filename in readline emulation.


# 1.5 18-May-2005 christos

Make completion_matches non-static since readline wants it.


# 1.4 12-May-2005 christos

PR/30215: Kouichirou Hiratsuka: /bin/sh dumps core with tabcomplete
Don't core-dump when trying to complete an empty line; instead assume ./


# 1.3 09-May-2005 dsl

Use getpwuid_r(getuid(), ...) to expand ~/....
Don't replace ~xyz with /home/xyz when expanding ~


# 1.2 07-May-2005 dsl

gdb directly calls filename_completion_function()


# 1.1 07-May-2005 dsl

Separate out the filename completion functions from the readline() code.
Pass in loads of parameters instead of relying on shed-loads of global
variables to modify the behaviour.
The filename completion code can now be enabled by code that uses el_gets().
(eg /bin/sh)


# 1.70 12-Mar-2022 christos

Fix filename autocompletion for strings like a\)b

An escaped character should unconditionally be skipped together with the
character that does the escaping. For example, in "a\)b" only the ")b"
part was skipped but then the loop stopped at the "\" since it's one of
the characters listed in word_break. (Piotr P. Stefaniak)


# 1.69 26-Sep-2021 christos

- Completion should not add a quote at the end of the line to match an
already quoted quote. (Piotr Stefaniak)
- fix lint unconst warnings for strchr


Revision tags: cjep_sun2x-base1 cjep_sun2x-base cjep_staticlib_x-base1 cjep_staticlib_x-base
# 1.68 05-May-2021 christos

PR/56147: Miroslav Lichvar: Avoid memory leak if strdup fails.


# 1.67 28-Mar-2021 christos

Only unescape when we are quoting and don't add a space if we are quoting
(we already did) (Piotr Stefaniak)


# 1.66 28-Mar-2021 christos

Pass the unescaped filename the the append function so it has to do less work
(for example it can call stat(2) directly (Piotr Stefaniak)


# 1.65 27-Mar-2021 christos

Add fn_complete2() that controls the quoting of the returned match.
Before it was based on the heuristic that we were not supplied an
attempted_completion_function, which worked well because programs
that supplied that function were not shells and did not want/understand
shell quoting. Recently though Piotr Stefaniak wanted to enhance command
completion for the Bourne Shell and this could benefit quoting the returned
command. This function adds an extra flags argument that controls that quoting.


Revision tags: phil-wifi-20200421 phil-wifi-20200411 is-mlppp-base phil-wifi-20200406
# 1.64 05-Jan-2020 abhinav

PR lib/54510 - when user supplied completion function is there,
don't unescape the string to be completed.


# 1.63 05-Jan-2020 tih

Summary: Remove over-simplified extraneous test

The file name matching code in libedit tries to adjust to the presence
of explicit " or ' characters in the input line, but tries too hard.
Remove a conditional that goes overboard, and causes the completion
code to fail if a quoted string is seen before the filename to be
expanded, as in

grep 'foo' bar<TAB>

Before this change, the above would not expand any possible
completions, even if they existed, because it would choose to look for
files whose names started with " bar".


# 1.62 10-Dec-2019 christos

When 'attempted_completion_function' non-NULL, with a 'single_match'
match, the expected space is not being added. Problem observed with
"chronyc" and "sqlite3" tab completion. That functionality got
moved to escape_filename() for the !attempted_completion_function
case, but the non-NULL 'attempted_completion_function' case must
also be handled. (Lonnie Abelbeck)


Revision tags: phil-wifi-20191119
# 1.61 09-Oct-2019 christos

add +1 to strlcpy's (Patrick Welche)


# 1.60 08-Oct-2019 christos

remore error(1) comment


# 1.59 08-Oct-2019 christos

Change strncpy to either memcpy (when we know the len), or strlcpy (when
we used to NUL terminate explicitly.


# 1.58 08-Sep-2019 abhinav

PR lib/54510: Fix file completion inside quotes which broke in rev 1.53

While there also fix handling character appending in the file completions when
inside quotes. For example when inside a quote, if the completion is a directory then
append a '/' but don't close the quote. On the other hand when inside a quote if the
completion is a file name and it is the only match then we can close the quote.


Revision tags: netbsd-9-2-RELEASE netbsd-9-1-RELEASE netbsd-9-0-RELEASE netbsd-9-0-RC2 netbsd-9-0-RC1 netbsd-9-base
# 1.57 28-Jul-2019 christos

PR/54415: Ricky Zhou: libedit stats completions for non-file completions
Use the proper completion function and account for the character appended
by the function when computing the number of columns.


# 1.56 23-Jul-2019 christos

PR/54399: S�ren Tempel: Uninitialized memory access in libedit history.
Initialize the buffer using calloc. While here change all malloc(a * sizeof(b))
to calloc(a, sizeof(b)). XXX: should fix realloc similarly.


Revision tags: phil-wifi-20190609
# 1.55 20-Apr-2019 abhinav

PR lib/54131 - declare the loop variable outside the for loop


# 1.54 12-Apr-2019 christos

PR/54117: Paavo Helde: Fix memory overrun: Account for the closing quote
in memory allocation if quoted.


# 1.53 31-Mar-2019 abhinav

Perform quoting of filename completions when there are multiple matches as well

Quoting of special characters in filename completion was implemented for single match
case, this enables it for multiple matches as well. For example:

$ touch 'foo bar'
$ touch 'foo baz'
$ ls fo<TAB>
autocompletes to =>
$ ls foo\ ba
hitting <TAB> again shows:
foo bar foo baz

This required unescaping escape sequences generated during last completion
in order to find the word to complete.

While there, also update the test to include cases for multiple matches.

Reviewed by christos


# 1.52 24-Mar-2019 abhinav

Only quote the completion matches if we are doing filename completion

If the user supplies a value for the attempted_completion_function parameter
then we cannot be sure if the completion is for filename or something else, in such
a case don't attempt to quote the completion matches.

Reviewed by christos

This should address PR lib/54067


Revision tags: pgoyette-compat-20190127 pgoyette-compat-20190118 pgoyette-compat-1226 pgoyette-compat-1126 pgoyette-compat-1020 pgoyette-compat-0930 pgoyette-compat-0906 pgoyette-compat-0728 phil-wifi-base pgoyette-compat-0625 pgoyette-compat-0521
# 1.51 04-May-2018 christos

branches: 1.51.2;
fix uninitialized


# 1.50 04-May-2018 abhinav

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


# 1.49 02-May-2018 abhinav

Add '*' and '[' to the list of characters which need escaping during autocompletion


Revision tags: pgoyette-compat-0502 pgoyette-compat-0422 pgoyette-compat-0415 pgoyette-compat-0407 pgoyette-compat-0330 pgoyette-compat-0322 pgoyette-compat-0315 pgoyette-compat-base
# 1.48 27-Oct-2017 abhinav

branches: 1.48.2;
Add NULL check after doing memory allocation at a couple of places

ok christos@


# 1.47 15-Oct-2017 abhinav

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.


# 1.46 16-Sep-2017 abhinav

Fix indentation (convert spaces to tab)


Revision tags: netbsd-8-2-RELEASE netbsd-8-1-RELEASE netbsd-8-1-RC1 netbsd-8-0-RELEASE netbsd-8-0-RC2 netbsd-8-0-RC1 matt-nb8-mediatek-base perseant-stdc-iso10646-base netbsd-8-base prg-localcount2-base3 prg-localcount2-base2 prg-localcount2-base1 prg-localcount2-base pgoyette-localcount-20170426 bouyer-socketcan-base1
# 1.45 21-Apr-2017 abhinav

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@


Revision tags: pgoyette-localcount-20170320 bouyer-socketcan-base pgoyette-localcount-20170107 pgoyette-localcount-20161104
# 1.44 31-Oct-2016 abhinav

branches: 1.44.2;
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.


Revision tags: localcount-20160914 pgoyette-localcount-20160806 pgoyette-localcount-20160726 pgoyette-localcount-base
# 1.43 11-Apr-2016 christos

branches: 1.43.2;
Char -> wchar_t from Ingo Schwarze.


# 1.42 11-Apr-2016 christos

more macro WIDECHAR undoing from Ingo Schwarze.


# 1.41 09-Apr-2016 christos

Change some 0's to NULL's from Pedro Giffuni


# 1.40 17-Feb-2016 christos

whitespace and header sorting changes (Ingo Schwarze). No functional changes.


# 1.39 16-Feb-2016 christos

More header cleanups from Ingo Schwarze.


# 1.38 16-Feb-2016 christos

more include file cleanup (Ingo Schwarze)


# 1.37 16-Feb-2016 christos

From Ingo Scharze:
Let "el.h" include everything needed for struct editline,
and don't include that stuff multiple times. That also improves
consistency, also avoids circular inclusions, and also makes it
easier to follow what is going on, even though not quite as nice.
But it seems like the best we can do...


# 1.36 16-Feb-2016 christos

cleanup chartype.h includes (Ingo Schwarze)


# 1.35 16-Feb-2016 christos

cleanup inclusion of histedit.h (Ingo Schwarze)


# 1.34 18-Oct-2014 riz

callers's -> caller's


# 1.33 18-Oct-2014 snj

src is too big these days to tolerate superfluous apostrophes. It's
"its", people!


Revision tags: netbsd-7-base tls-earlyentropy-base tls-maxphys-base
# 1.32 05-Jun-2014 christos

branches: 1.32.2;
PR/48876: Dmitriy Grigoryev: Core dump in readline lib on attempted expansion
Make sure we have 2 matches before calling strcmp().


Revision tags: netbsd-6-0-6-RELEASE netbsd-6-1-5-RELEASE yamt-pagecache-base9 yamt-pagecache-tag8 netbsd-6-1-4-RELEASE netbsd-6-0-5-RELEASE riastradh-xf86-video-intel-2-7-1-pre-2-21-15 riastradh-drm2-base3 netbsd-6-1-3-RELEASE netbsd-6-0-4-RELEASE netbsd-6-1-2-RELEASE netbsd-6-0-3-RELEASE netbsd-6-1-1-RELEASE riastradh-drm2-base2 riastradh-drm2-base1 riastradh-drm2-base netbsd-6-0-2-RELEASE netbsd-6-1-RELEASE netbsd-6-1-RC4 netbsd-6-1-RC3 agc-symver-base netbsd-6-1-RC2 netbsd-6-1-RC1 yamt-pagecache-base8 netbsd-6-0-1-RELEASE yamt-pagecache-base7 matt-nb6-plus-nbase yamt-pagecache-base6 netbsd-6-0-RELEASE netbsd-6-0-RC2 matt-nb6-plus-base netbsd-6-0-RC1 yamt-pagecache-base5 yamt-pagecache-base4 netbsd-6-base yamt-pagecache-base3 yamt-pagecache-base2 yamt-pagecache-base
# 1.31 16-Sep-2011 plunky

branches: 1.31.8; 1.31.18;
NULL does not need a cast, here


# 1.30 16-Aug-2011 christos

re-enable -Wconversion


# 1.29 29-Jul-2011 christos

pass -Wconversion


# 1.28 29-Jul-2011 christos

- fix unused params
- unconditionalize vis.h


# 1.27 29-Jul-2011 christos

KNF return (\1); -> return \1;


# 1.26 28-Jul-2011 christos

kill ptr_t and ioctl_t, add * sizeof(*foo) to all allocations.


# 1.25 28-Jul-2011 christos

fix unused variable warnings on systems without _r functions


# 1.24 28-Jul-2011 christos

eliminate alloca for portability
portable getpw{nam,uid}


Revision tags: cherry-xenmp-base bouyer-quota2-nbase bouyer-quota2-base matt-mips64-premerge-20101231
# 1.23 06-Dec-2010 dholland

Improve previous to avoid changing the interface of an externally
exposed function. (But note that this function is neither documented
nor declared in any installed header file, and it probably should not
be externally exposed.) Related to PR 44183, closes PR 44186.


# 1.22 02-Dec-2010 dholland

add const, from PR 44183.


# 1.21 02-Dec-2010 dholland

Fix up bodgy code for printing completion matches; it used to sometimes
skip entries, print (null), run off the end of the array, or occasionally
receive SIGSEGV, and now will, hopefully at least, do none of that.

Based in part on the patch in PR 44183 from Sergio Acereda; I also
did some tidyup and fixed it to print top-to-bottom first like ls(1).


# 1.20 15-Nov-2010 christos

don't increment i twice in the loop. From Michael Byrnes


# 1.19 01-Jun-2010 christos

tidy up memory allocation and don't unnecessarily print "./" before names.


# 1.18 18-Jan-2010 christos

PR/42637: Joachim Kuebart: Shell tab completion crashes due to libedit stack
smashing


# 1.17 30-Dec-2009 christos

Wide character support (UTF-8) from Johny Mattsson; currently disabled.


# 1.16 28-Dec-2009 christos

improve on the listing display by printing only one character after the
filename not two, and no trailing blanks. I will revisit this when I write
the ls-F code.


Revision tags: matt-premerge-20091211 jym-xensuspend-nbase jym-xensuspend-base
# 1.15 16-Feb-2009 christos

fix sign compare issues.


# 1.14 15-Feb-2009 christos

pass lint on _LP64.


# 1.13 26-Jan-2009 apb

branches: 1.13.2;
Define HAVE_STRUCT_DIRENT_D_NAMLEN in config,h, and test it when
deciding whether to use entry->d_namlen or strlen(entry->d_name).
Addresses PR 40477 by Robert Millan.


# 1.12 11-Jan-2009 christos

- insert a space after the recognized string if it was an exact match
- initialize properly the string used for completion.
From Alex Bligh alex at alex dot org dot uk
- Make char constants consistent


Revision tags: netbsd-5-2-3-RELEASE netbsd-5-1-5-RELEASE netbsd-5-2-2-RELEASE netbsd-5-1-4-RELEASE netbsd-5-2-1-RELEASE netbsd-5-1-3-RELEASE netbsd-5-2-RELEASE netbsd-5-2-RC1 netbsd-5-1-2-RELEASE netbsd-5-1-1-RELEASE matt-nb5-mips64-premerge-20101231 matt-nb5-pq3-base netbsd-5-1-RELEASE netbsd-5-1-RC4 matt-nb5-mips64-k15 netbsd-5-1-RC3 netbsd-5-1-RC2 netbsd-5-1-RC1 netbsd-5-0-2-RELEASE matt-nb5-mips64-premerge-20091211 matt-nb5-mips64-u2-k2-k4-k7-k8-k9 matt-nb4-mips64-k7-u2a-k9b matt-nb5-mips64-u1-k1-k5 netbsd-5-0-1-RELEASE netbsd-5-0-RELEASE netbsd-5-0-RC4 netbsd-5-0-RC3 netbsd-5-0-RC2 netbsd-5-0-RC1 netbsd-5-base matt-mips64-base2 wrstuden-revivesa-base-3 wrstuden-revivesa-base-2 wrstuden-revivesa-base-1 yamt-pf42-base4 yamt-pf42-base3 hpcarm-cleanup-nbase yamt-pf42-base2 wrstuden-revivesa-base
# 1.11 29-Apr-2008 martin

Convert to new 2 clause license


Revision tags: netbsd-4-0-1-RELEASE wrstuden-fixsa-newbase yamt-pf42-baseX yamt-pf42-base keiichi-mipv6-base matt-armv6-nbase matt-armv6-prevmlocking wrstuden-fixsa-base-1 netbsd-4-0-RELEASE cube-autoconf-base netbsd-4-0-RC5 netbsd-4-0-RC4 netbsd-4-0-RC3 netbsd-4-0-RC2 netbsd-4-0-RC1 matt-armv6-base matt-mips64-base hpcarm-cleanup-base wrstuden-fixsa-base netbsd-4-base
# 1.10 09-Nov-2006 christos

branches: 1.10.16;
don't use alloca with ssp.


# 1.9 21-Aug-2006 christos

Change to a 3 clause copyright after permission of the holders.


Revision tags: abandoned-netbsd-4-base
# 1.8 03-Aug-2005 christos

Fix reversed test; from Gerry Swislow


# 1.7 11-Jun-2005 christos

PR/30500: Paul Shupak: Inconsistent definition of tilde_expand().
Provide a layer of indirection between the readline compatibility functions
and our internal implementation, so that we have the freedom to change the
function signature.


# 1.6 10-Jun-2005 christos

Bug reported from Martin Dietze:

The place to change the completion_append_character is
usually somewhere in the `rl_completion_entry_function'
callback which is where one usually can distinguish between
file- or dir-like entries to append a slash for dirs etc.

This does no longer work since `fn_complete()' takes the
`append_character' as argument before the callback is executed,
so that changes to the variable `rl_completion_append_character'
have in fact no effect for the current completion.

Fix by adding a function that returns the rl_completion_append_character,
when it gets passed in a filename in readline emulation.


# 1.5 18-May-2005 christos

Make completion_matches non-static since readline wants it.


# 1.4 12-May-2005 christos

PR/30215: Kouichirou Hiratsuka: /bin/sh dumps core with tabcomplete
Don't core-dump when trying to complete an empty line; instead assume ./


# 1.3 09-May-2005 dsl

Use getpwuid_r(getuid(), ...) to expand ~/....
Don't replace ~xyz with /home/xyz when expanding ~


# 1.2 07-May-2005 dsl

gdb directly calls filename_completion_function()


# 1.1 07-May-2005 dsl

Separate out the filename completion functions from the readline() code.
Pass in loads of parameters instead of relying on shed-loads of global
variables to modify the behaviour.
The filename completion code can now be enabled by code that uses el_gets().
(eg /bin/sh)


# 1.69 26-Sep-2021 christos

- Completion should not add a quote at the end of the line to match an
already quoted quote. (Piotr Stefaniak)
- fix lint unconst warnings for strchr


Revision tags: cjep_sun2x-base1 cjep_sun2x-base cjep_staticlib_x-base1 cjep_staticlib_x-base
# 1.68 05-May-2021 christos

PR/56147: Miroslav Lichvar: Avoid memory leak if strdup fails.


# 1.67 28-Mar-2021 christos

Only unescape when we are quoting and don't add a space if we are quoting
(we already did) (Piotr Stefaniak)


# 1.66 28-Mar-2021 christos

Pass the unescaped filename the the append function so it has to do less work
(for example it can call stat(2) directly (Piotr Stefaniak)


# 1.65 27-Mar-2021 christos

Add fn_complete2() that controls the quoting of the returned match.
Before it was based on the heuristic that we were not supplied an
attempted_completion_function, which worked well because programs
that supplied that function were not shells and did not want/understand
shell quoting. Recently though Piotr Stefaniak wanted to enhance command
completion for the Bourne Shell and this could benefit quoting the returned
command. This function adds an extra flags argument that controls that quoting.


Revision tags: phil-wifi-20200421 phil-wifi-20200411 is-mlppp-base phil-wifi-20200406
# 1.64 05-Jan-2020 abhinav

PR lib/54510 - when user supplied completion function is there,
don't unescape the string to be completed.


# 1.63 05-Jan-2020 tih

Summary: Remove over-simplified extraneous test

The file name matching code in libedit tries to adjust to the presence
of explicit " or ' characters in the input line, but tries too hard.
Remove a conditional that goes overboard, and causes the completion
code to fail if a quoted string is seen before the filename to be
expanded, as in

grep 'foo' bar<TAB>

Before this change, the above would not expand any possible
completions, even if they existed, because it would choose to look for
files whose names started with " bar".


# 1.62 10-Dec-2019 christos

When 'attempted_completion_function' non-NULL, with a 'single_match'
match, the expected space is not being added. Problem observed with
"chronyc" and "sqlite3" tab completion. That functionality got
moved to escape_filename() for the !attempted_completion_function
case, but the non-NULL 'attempted_completion_function' case must
also be handled. (Lonnie Abelbeck)


Revision tags: phil-wifi-20191119
# 1.61 09-Oct-2019 christos

add +1 to strlcpy's (Patrick Welche)


# 1.60 08-Oct-2019 christos

remore error(1) comment


# 1.59 08-Oct-2019 christos

Change strncpy to either memcpy (when we know the len), or strlcpy (when
we used to NUL terminate explicitly.


# 1.58 08-Sep-2019 abhinav

PR lib/54510: Fix file completion inside quotes which broke in rev 1.53

While there also fix handling character appending in the file completions when
inside quotes. For example when inside a quote, if the completion is a directory then
append a '/' but don't close the quote. On the other hand when inside a quote if the
completion is a file name and it is the only match then we can close the quote.


Revision tags: netbsd-9-2-RELEASE netbsd-9-1-RELEASE netbsd-9-0-RELEASE netbsd-9-0-RC2 netbsd-9-0-RC1 netbsd-9-base
# 1.57 28-Jul-2019 christos

PR/54415: Ricky Zhou: libedit stats completions for non-file completions
Use the proper completion function and account for the character appended
by the function when computing the number of columns.


# 1.56 23-Jul-2019 christos

PR/54399: S�ren Tempel: Uninitialized memory access in libedit history.
Initialize the buffer using calloc. While here change all malloc(a * sizeof(b))
to calloc(a, sizeof(b)). XXX: should fix realloc similarly.


Revision tags: phil-wifi-20190609
# 1.55 20-Apr-2019 abhinav

PR lib/54131 - declare the loop variable outside the for loop


# 1.54 12-Apr-2019 christos

PR/54117: Paavo Helde: Fix memory overrun: Account for the closing quote
in memory allocation if quoted.


# 1.53 31-Mar-2019 abhinav

Perform quoting of filename completions when there are multiple matches as well

Quoting of special characters in filename completion was implemented for single match
case, this enables it for multiple matches as well. For example:

$ touch 'foo bar'
$ touch 'foo baz'
$ ls fo<TAB>
autocompletes to =>
$ ls foo\ ba
hitting <TAB> again shows:
foo bar foo baz

This required unescaping escape sequences generated during last completion
in order to find the word to complete.

While there, also update the test to include cases for multiple matches.

Reviewed by christos


# 1.52 24-Mar-2019 abhinav

Only quote the completion matches if we are doing filename completion

If the user supplies a value for the attempted_completion_function parameter
then we cannot be sure if the completion is for filename or something else, in such
a case don't attempt to quote the completion matches.

Reviewed by christos

This should address PR lib/54067


Revision tags: pgoyette-compat-20190127 pgoyette-compat-20190118 pgoyette-compat-1226 pgoyette-compat-1126 pgoyette-compat-1020 pgoyette-compat-0930 pgoyette-compat-0906 pgoyette-compat-0728 phil-wifi-base pgoyette-compat-0625 pgoyette-compat-0521
# 1.51 04-May-2018 christos

branches: 1.51.2;
fix uninitialized


# 1.50 04-May-2018 abhinav

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


# 1.49 02-May-2018 abhinav

Add '*' and '[' to the list of characters which need escaping during autocompletion


Revision tags: pgoyette-compat-0502 pgoyette-compat-0422 pgoyette-compat-0415 pgoyette-compat-0407 pgoyette-compat-0330 pgoyette-compat-0322 pgoyette-compat-0315 pgoyette-compat-base
# 1.48 27-Oct-2017 abhinav

branches: 1.48.2;
Add NULL check after doing memory allocation at a couple of places

ok christos@


# 1.47 15-Oct-2017 abhinav

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.


# 1.46 16-Sep-2017 abhinav

Fix indentation (convert spaces to tab)


Revision tags: netbsd-8-2-RELEASE netbsd-8-1-RELEASE netbsd-8-1-RC1 netbsd-8-0-RELEASE netbsd-8-0-RC2 netbsd-8-0-RC1 matt-nb8-mediatek-base perseant-stdc-iso10646-base netbsd-8-base prg-localcount2-base3 prg-localcount2-base2 prg-localcount2-base1 prg-localcount2-base pgoyette-localcount-20170426 bouyer-socketcan-base1
# 1.45 21-Apr-2017 abhinav

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@


Revision tags: pgoyette-localcount-20170320 bouyer-socketcan-base pgoyette-localcount-20170107 pgoyette-localcount-20161104
# 1.44 31-Oct-2016 abhinav

branches: 1.44.2;
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.


Revision tags: localcount-20160914 pgoyette-localcount-20160806 pgoyette-localcount-20160726 pgoyette-localcount-base
# 1.43 11-Apr-2016 christos

branches: 1.43.2;
Char -> wchar_t from Ingo Schwarze.


# 1.42 11-Apr-2016 christos

more macro WIDECHAR undoing from Ingo Schwarze.


# 1.41 09-Apr-2016 christos

Change some 0's to NULL's from Pedro Giffuni


# 1.40 17-Feb-2016 christos

whitespace and header sorting changes (Ingo Schwarze). No functional changes.


# 1.39 16-Feb-2016 christos

More header cleanups from Ingo Schwarze.


# 1.38 16-Feb-2016 christos

more include file cleanup (Ingo Schwarze)


# 1.37 16-Feb-2016 christos

From Ingo Scharze:
Let "el.h" include everything needed for struct editline,
and don't include that stuff multiple times. That also improves
consistency, also avoids circular inclusions, and also makes it
easier to follow what is going on, even though not quite as nice.
But it seems like the best we can do...


# 1.36 16-Feb-2016 christos

cleanup chartype.h includes (Ingo Schwarze)


# 1.35 16-Feb-2016 christos

cleanup inclusion of histedit.h (Ingo Schwarze)


# 1.34 18-Oct-2014 riz

callers's -> caller's


# 1.33 18-Oct-2014 snj

src is too big these days to tolerate superfluous apostrophes. It's
"its", people!


Revision tags: netbsd-7-base tls-earlyentropy-base tls-maxphys-base
# 1.32 05-Jun-2014 christos

branches: 1.32.2;
PR/48876: Dmitriy Grigoryev: Core dump in readline lib on attempted expansion
Make sure we have 2 matches before calling strcmp().


Revision tags: netbsd-6-0-6-RELEASE netbsd-6-1-5-RELEASE yamt-pagecache-base9 yamt-pagecache-tag8 netbsd-6-1-4-RELEASE netbsd-6-0-5-RELEASE riastradh-xf86-video-intel-2-7-1-pre-2-21-15 riastradh-drm2-base3 netbsd-6-1-3-RELEASE netbsd-6-0-4-RELEASE netbsd-6-1-2-RELEASE netbsd-6-0-3-RELEASE netbsd-6-1-1-RELEASE riastradh-drm2-base2 riastradh-drm2-base1 riastradh-drm2-base netbsd-6-0-2-RELEASE netbsd-6-1-RELEASE netbsd-6-1-RC4 netbsd-6-1-RC3 agc-symver-base netbsd-6-1-RC2 netbsd-6-1-RC1 yamt-pagecache-base8 netbsd-6-0-1-RELEASE yamt-pagecache-base7 matt-nb6-plus-nbase yamt-pagecache-base6 netbsd-6-0-RELEASE netbsd-6-0-RC2 matt-nb6-plus-base netbsd-6-0-RC1 yamt-pagecache-base5 yamt-pagecache-base4 netbsd-6-base yamt-pagecache-base3 yamt-pagecache-base2 yamt-pagecache-base
# 1.31 16-Sep-2011 plunky

branches: 1.31.8; 1.31.18;
NULL does not need a cast, here


# 1.30 16-Aug-2011 christos

re-enable -Wconversion


# 1.29 29-Jul-2011 christos

pass -Wconversion


# 1.28 29-Jul-2011 christos

- fix unused params
- unconditionalize vis.h


# 1.27 29-Jul-2011 christos

KNF return (\1); -> return \1;


# 1.26 28-Jul-2011 christos

kill ptr_t and ioctl_t, add * sizeof(*foo) to all allocations.


# 1.25 28-Jul-2011 christos

fix unused variable warnings on systems without _r functions


# 1.24 28-Jul-2011 christos

eliminate alloca for portability
portable getpw{nam,uid}


Revision tags: cherry-xenmp-base bouyer-quota2-nbase bouyer-quota2-base matt-mips64-premerge-20101231
# 1.23 06-Dec-2010 dholland

Improve previous to avoid changing the interface of an externally
exposed function. (But note that this function is neither documented
nor declared in any installed header file, and it probably should not
be externally exposed.) Related to PR 44183, closes PR 44186.


# 1.22 02-Dec-2010 dholland

add const, from PR 44183.


# 1.21 02-Dec-2010 dholland

Fix up bodgy code for printing completion matches; it used to sometimes
skip entries, print (null), run off the end of the array, or occasionally
receive SIGSEGV, and now will, hopefully at least, do none of that.

Based in part on the patch in PR 44183 from Sergio Acereda; I also
did some tidyup and fixed it to print top-to-bottom first like ls(1).


# 1.20 15-Nov-2010 christos

don't increment i twice in the loop. From Michael Byrnes


# 1.19 01-Jun-2010 christos

tidy up memory allocation and don't unnecessarily print "./" before names.


# 1.18 18-Jan-2010 christos

PR/42637: Joachim Kuebart: Shell tab completion crashes due to libedit stack
smashing


# 1.17 30-Dec-2009 christos

Wide character support (UTF-8) from Johny Mattsson; currently disabled.


# 1.16 28-Dec-2009 christos

improve on the listing display by printing only one character after the
filename not two, and no trailing blanks. I will revisit this when I write
the ls-F code.


Revision tags: matt-premerge-20091211 jym-xensuspend-nbase jym-xensuspend-base
# 1.15 16-Feb-2009 christos

fix sign compare issues.


# 1.14 15-Feb-2009 christos

pass lint on _LP64.


# 1.13 26-Jan-2009 apb

branches: 1.13.2;
Define HAVE_STRUCT_DIRENT_D_NAMLEN in config,h, and test it when
deciding whether to use entry->d_namlen or strlen(entry->d_name).
Addresses PR 40477 by Robert Millan.


# 1.12 11-Jan-2009 christos

- insert a space after the recognized string if it was an exact match
- initialize properly the string used for completion.
From Alex Bligh alex at alex dot org dot uk
- Make char constants consistent


Revision tags: netbsd-5-2-3-RELEASE netbsd-5-1-5-RELEASE netbsd-5-2-2-RELEASE netbsd-5-1-4-RELEASE netbsd-5-2-1-RELEASE netbsd-5-1-3-RELEASE netbsd-5-2-RELEASE netbsd-5-2-RC1 netbsd-5-1-2-RELEASE netbsd-5-1-1-RELEASE matt-nb5-mips64-premerge-20101231 matt-nb5-pq3-base netbsd-5-1-RELEASE netbsd-5-1-RC4 matt-nb5-mips64-k15 netbsd-5-1-RC3 netbsd-5-1-RC2 netbsd-5-1-RC1 netbsd-5-0-2-RELEASE matt-nb5-mips64-premerge-20091211 matt-nb5-mips64-u2-k2-k4-k7-k8-k9 matt-nb4-mips64-k7-u2a-k9b matt-nb5-mips64-u1-k1-k5 netbsd-5-0-1-RELEASE netbsd-5-0-RELEASE netbsd-5-0-RC4 netbsd-5-0-RC3 netbsd-5-0-RC2 netbsd-5-0-RC1 netbsd-5-base matt-mips64-base2 wrstuden-revivesa-base-3 wrstuden-revivesa-base-2 wrstuden-revivesa-base-1 yamt-pf42-base4 yamt-pf42-base3 hpcarm-cleanup-nbase yamt-pf42-base2 wrstuden-revivesa-base
# 1.11 29-Apr-2008 martin

Convert to new 2 clause license


Revision tags: netbsd-4-0-1-RELEASE wrstuden-fixsa-newbase yamt-pf42-baseX yamt-pf42-base keiichi-mipv6-base matt-armv6-nbase matt-armv6-prevmlocking wrstuden-fixsa-base-1 netbsd-4-0-RELEASE cube-autoconf-base netbsd-4-0-RC5 netbsd-4-0-RC4 netbsd-4-0-RC3 netbsd-4-0-RC2 netbsd-4-0-RC1 matt-armv6-base matt-mips64-base hpcarm-cleanup-base wrstuden-fixsa-base netbsd-4-base
# 1.10 09-Nov-2006 christos

branches: 1.10.16;
don't use alloca with ssp.


# 1.9 21-Aug-2006 christos

Change to a 3 clause copyright after permission of the holders.


Revision tags: abandoned-netbsd-4-base
# 1.8 03-Aug-2005 christos

Fix reversed test; from Gerry Swislow


# 1.7 11-Jun-2005 christos

PR/30500: Paul Shupak: Inconsistent definition of tilde_expand().
Provide a layer of indirection between the readline compatibility functions
and our internal implementation, so that we have the freedom to change the
function signature.


# 1.6 10-Jun-2005 christos

Bug reported from Martin Dietze:

The place to change the completion_append_character is
usually somewhere in the `rl_completion_entry_function'
callback which is where one usually can distinguish between
file- or dir-like entries to append a slash for dirs etc.

This does no longer work since `fn_complete()' takes the
`append_character' as argument before the callback is executed,
so that changes to the variable `rl_completion_append_character'
have in fact no effect for the current completion.

Fix by adding a function that returns the rl_completion_append_character,
when it gets passed in a filename in readline emulation.


# 1.5 18-May-2005 christos

Make completion_matches non-static since readline wants it.


# 1.4 12-May-2005 christos

PR/30215: Kouichirou Hiratsuka: /bin/sh dumps core with tabcomplete
Don't core-dump when trying to complete an empty line; instead assume ./


# 1.3 09-May-2005 dsl

Use getpwuid_r(getuid(), ...) to expand ~/....
Don't replace ~xyz with /home/xyz when expanding ~


# 1.2 07-May-2005 dsl

gdb directly calls filename_completion_function()


# 1.1 07-May-2005 dsl

Separate out the filename completion functions from the readline() code.
Pass in loads of parameters instead of relying on shed-loads of global
variables to modify the behaviour.
The filename completion code can now be enabled by code that uses el_gets().
(eg /bin/sh)


# 1.68 05-May-2021 christos

PR/56147: Miroslav Lichvar: Avoid memory leak if strdup fails.


# 1.67 28-Mar-2021 christos

Only unescape when we are quoting and don't add a space if we are quoting
(we already did) (Piotr Stefaniak)


# 1.66 28-Mar-2021 christos

Pass the unescaped filename the the append function so it has to do less work
(for example it can call stat(2) directly (Piotr Stefaniak)


# 1.65 27-Mar-2021 christos

Add fn_complete2() that controls the quoting of the returned match.
Before it was based on the heuristic that we were not supplied an
attempted_completion_function, which worked well because programs
that supplied that function were not shells and did not want/understand
shell quoting. Recently though Piotr Stefaniak wanted to enhance command
completion for the Bourne Shell and this could benefit quoting the returned
command. This function adds an extra flags argument that controls that quoting.


Revision tags: phil-wifi-20200421 phil-wifi-20200411 is-mlppp-base phil-wifi-20200406
# 1.64 05-Jan-2020 abhinav

PR lib/54510 - when user supplied completion function is there,
don't unescape the string to be completed.


# 1.63 05-Jan-2020 tih

Summary: Remove over-simplified extraneous test

The file name matching code in libedit tries to adjust to the presence
of explicit " or ' characters in the input line, but tries too hard.
Remove a conditional that goes overboard, and causes the completion
code to fail if a quoted string is seen before the filename to be
expanded, as in

grep 'foo' bar<TAB>

Before this change, the above would not expand any possible
completions, even if they existed, because it would choose to look for
files whose names started with " bar".


# 1.62 10-Dec-2019 christos

When 'attempted_completion_function' non-NULL, with a 'single_match'
match, the expected space is not being added. Problem observed with
"chronyc" and "sqlite3" tab completion. That functionality got
moved to escape_filename() for the !attempted_completion_function
case, but the non-NULL 'attempted_completion_function' case must
also be handled. (Lonnie Abelbeck)


Revision tags: phil-wifi-20191119
# 1.61 09-Oct-2019 christos

add +1 to strlcpy's (Patrick Welche)


# 1.60 08-Oct-2019 christos

remore error(1) comment


# 1.59 08-Oct-2019 christos

Change strncpy to either memcpy (when we know the len), or strlcpy (when
we used to NUL terminate explicitly.


# 1.58 08-Sep-2019 abhinav

PR lib/54510: Fix file completion inside quotes which broke in rev 1.53

While there also fix handling character appending in the file completions when
inside quotes. For example when inside a quote, if the completion is a directory then
append a '/' but don't close the quote. On the other hand when inside a quote if the
completion is a file name and it is the only match then we can close the quote.


Revision tags: netbsd-9-1-RELEASE netbsd-9-0-RELEASE netbsd-9-0-RC2 netbsd-9-0-RC1 netbsd-9-base
# 1.57 28-Jul-2019 christos

PR/54415: Ricky Zhou: libedit stats completions for non-file completions
Use the proper completion function and account for the character appended
by the function when computing the number of columns.


# 1.56 23-Jul-2019 christos

PR/54399: S�ren Tempel: Uninitialized memory access in libedit history.
Initialize the buffer using calloc. While here change all malloc(a * sizeof(b))
to calloc(a, sizeof(b)). XXX: should fix realloc similarly.


Revision tags: phil-wifi-20190609
# 1.55 20-Apr-2019 abhinav

PR lib/54131 - declare the loop variable outside the for loop


# 1.54 12-Apr-2019 christos

PR/54117: Paavo Helde: Fix memory overrun: Account for the closing quote
in memory allocation if quoted.


# 1.53 31-Mar-2019 abhinav

Perform quoting of filename completions when there are multiple matches as well

Quoting of special characters in filename completion was implemented for single match
case, this enables it for multiple matches as well. For example:

$ touch 'foo bar'
$ touch 'foo baz'
$ ls fo<TAB>
autocompletes to =>
$ ls foo\ ba
hitting <TAB> again shows:
foo bar foo baz

This required unescaping escape sequences generated during last completion
in order to find the word to complete.

While there, also update the test to include cases for multiple matches.

Reviewed by christos


# 1.52 24-Mar-2019 abhinav

Only quote the completion matches if we are doing filename completion

If the user supplies a value for the attempted_completion_function parameter
then we cannot be sure if the completion is for filename or something else, in such
a case don't attempt to quote the completion matches.

Reviewed by christos

This should address PR lib/54067


Revision tags: pgoyette-compat-20190127 pgoyette-compat-20190118 pgoyette-compat-1226 pgoyette-compat-1126 pgoyette-compat-1020 pgoyette-compat-0930 pgoyette-compat-0906 pgoyette-compat-0728 phil-wifi-base pgoyette-compat-0625 pgoyette-compat-0521
# 1.51 04-May-2018 christos

branches: 1.51.2;
fix uninitialized


# 1.50 04-May-2018 abhinav

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


# 1.49 02-May-2018 abhinav

Add '*' and '[' to the list of characters which need escaping during autocompletion


Revision tags: pgoyette-compat-0502 pgoyette-compat-0422 pgoyette-compat-0415 pgoyette-compat-0407 pgoyette-compat-0330 pgoyette-compat-0322 pgoyette-compat-0315 pgoyette-compat-base
# 1.48 27-Oct-2017 abhinav

branches: 1.48.2;
Add NULL check after doing memory allocation at a couple of places

ok christos@


# 1.47 15-Oct-2017 abhinav

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.


# 1.46 16-Sep-2017 abhinav

Fix indentation (convert spaces to tab)


Revision tags: netbsd-8-2-RELEASE netbsd-8-1-RELEASE netbsd-8-1-RC1 netbsd-8-0-RELEASE netbsd-8-0-RC2 netbsd-8-0-RC1 matt-nb8-mediatek-base perseant-stdc-iso10646-base netbsd-8-base prg-localcount2-base3 prg-localcount2-base2 prg-localcount2-base1 prg-localcount2-base pgoyette-localcount-20170426 bouyer-socketcan-base1
# 1.45 21-Apr-2017 abhinav

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@


Revision tags: pgoyette-localcount-20170320 bouyer-socketcan-base pgoyette-localcount-20170107 pgoyette-localcount-20161104
# 1.44 31-Oct-2016 abhinav

branches: 1.44.2;
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.


Revision tags: localcount-20160914 pgoyette-localcount-20160806 pgoyette-localcount-20160726 pgoyette-localcount-base
# 1.43 11-Apr-2016 christos

branches: 1.43.2;
Char -> wchar_t from Ingo Schwarze.


# 1.42 11-Apr-2016 christos

more macro WIDECHAR undoing from Ingo Schwarze.


# 1.41 09-Apr-2016 christos

Change some 0's to NULL's from Pedro Giffuni


# 1.40 17-Feb-2016 christos

whitespace and header sorting changes (Ingo Schwarze). No functional changes.


# 1.39 16-Feb-2016 christos

More header cleanups from Ingo Schwarze.


# 1.38 16-Feb-2016 christos

more include file cleanup (Ingo Schwarze)


# 1.37 16-Feb-2016 christos

From Ingo Scharze:
Let "el.h" include everything needed for struct editline,
and don't include that stuff multiple times. That also improves
consistency, also avoids circular inclusions, and also makes it
easier to follow what is going on, even though not quite as nice.
But it seems like the best we can do...


# 1.36 16-Feb-2016 christos

cleanup chartype.h includes (Ingo Schwarze)


# 1.35 16-Feb-2016 christos

cleanup inclusion of histedit.h (Ingo Schwarze)


# 1.34 18-Oct-2014 riz

callers's -> caller's


# 1.33 18-Oct-2014 snj

src is too big these days to tolerate superfluous apostrophes. It's
"its", people!


Revision tags: netbsd-7-base tls-earlyentropy-base tls-maxphys-base
# 1.32 05-Jun-2014 christos

branches: 1.32.2;
PR/48876: Dmitriy Grigoryev: Core dump in readline lib on attempted expansion
Make sure we have 2 matches before calling strcmp().


Revision tags: netbsd-6-0-6-RELEASE netbsd-6-1-5-RELEASE yamt-pagecache-base9 yamt-pagecache-tag8 netbsd-6-1-4-RELEASE netbsd-6-0-5-RELEASE riastradh-xf86-video-intel-2-7-1-pre-2-21-15 riastradh-drm2-base3 netbsd-6-1-3-RELEASE netbsd-6-0-4-RELEASE netbsd-6-1-2-RELEASE netbsd-6-0-3-RELEASE netbsd-6-1-1-RELEASE riastradh-drm2-base2 riastradh-drm2-base1 riastradh-drm2-base netbsd-6-0-2-RELEASE netbsd-6-1-RELEASE netbsd-6-1-RC4 netbsd-6-1-RC3 agc-symver-base netbsd-6-1-RC2 netbsd-6-1-RC1 yamt-pagecache-base8 netbsd-6-0-1-RELEASE yamt-pagecache-base7 matt-nb6-plus-nbase yamt-pagecache-base6 netbsd-6-0-RELEASE netbsd-6-0-RC2 matt-nb6-plus-base netbsd-6-0-RC1 yamt-pagecache-base5 yamt-pagecache-base4 netbsd-6-base yamt-pagecache-base3 yamt-pagecache-base2 yamt-pagecache-base
# 1.31 16-Sep-2011 plunky

branches: 1.31.8; 1.31.18;
NULL does not need a cast, here


# 1.30 16-Aug-2011 christos

re-enable -Wconversion


# 1.29 29-Jul-2011 christos

pass -Wconversion


# 1.28 29-Jul-2011 christos

- fix unused params
- unconditionalize vis.h


# 1.27 29-Jul-2011 christos

KNF return (\1); -> return \1;


# 1.26 28-Jul-2011 christos

kill ptr_t and ioctl_t, add * sizeof(*foo) to all allocations.


# 1.25 28-Jul-2011 christos

fix unused variable warnings on systems without _r functions


# 1.24 28-Jul-2011 christos

eliminate alloca for portability
portable getpw{nam,uid}


Revision tags: cherry-xenmp-base bouyer-quota2-nbase bouyer-quota2-base matt-mips64-premerge-20101231
# 1.23 06-Dec-2010 dholland

Improve previous to avoid changing the interface of an externally
exposed function. (But note that this function is neither documented
nor declared in any installed header file, and it probably should not
be externally exposed.) Related to PR 44183, closes PR 44186.


# 1.22 02-Dec-2010 dholland

add const, from PR 44183.


# 1.21 02-Dec-2010 dholland

Fix up bodgy code for printing completion matches; it used to sometimes
skip entries, print (null), run off the end of the array, or occasionally
receive SIGSEGV, and now will, hopefully at least, do none of that.

Based in part on the patch in PR 44183 from Sergio Acereda; I also
did some tidyup and fixed it to print top-to-bottom first like ls(1).


# 1.20 15-Nov-2010 christos

don't increment i twice in the loop. From Michael Byrnes


# 1.19 01-Jun-2010 christos

tidy up memory allocation and don't unnecessarily print "./" before names.


# 1.18 18-Jan-2010 christos

PR/42637: Joachim Kuebart: Shell tab completion crashes due to libedit stack
smashing


# 1.17 30-Dec-2009 christos

Wide character support (UTF-8) from Johny Mattsson; currently disabled.


# 1.16 28-Dec-2009 christos

improve on the listing display by printing only one character after the
filename not two, and no trailing blanks. I will revisit this when I write
the ls-F code.


Revision tags: matt-premerge-20091211 jym-xensuspend-nbase jym-xensuspend-base
# 1.15 16-Feb-2009 christos

fix sign compare issues.


# 1.14 15-Feb-2009 christos

pass lint on _LP64.


# 1.13 26-Jan-2009 apb

branches: 1.13.2;
Define HAVE_STRUCT_DIRENT_D_NAMLEN in config,h, and test it when
deciding whether to use entry->d_namlen or strlen(entry->d_name).
Addresses PR 40477 by Robert Millan.


# 1.12 11-Jan-2009 christos

- insert a space after the recognized string if it was an exact match
- initialize properly the string used for completion.
From Alex Bligh alex at alex dot org dot uk
- Make char constants consistent


Revision tags: netbsd-5-2-3-RELEASE netbsd-5-1-5-RELEASE netbsd-5-2-2-RELEASE netbsd-5-1-4-RELEASE netbsd-5-2-1-RELEASE netbsd-5-1-3-RELEASE netbsd-5-2-RELEASE netbsd-5-2-RC1 netbsd-5-1-2-RELEASE netbsd-5-1-1-RELEASE matt-nb5-mips64-premerge-20101231 matt-nb5-pq3-base netbsd-5-1-RELEASE netbsd-5-1-RC4 matt-nb5-mips64-k15 netbsd-5-1-RC3 netbsd-5-1-RC2 netbsd-5-1-RC1 netbsd-5-0-2-RELEASE matt-nb5-mips64-premerge-20091211 matt-nb5-mips64-u2-k2-k4-k7-k8-k9 matt-nb4-mips64-k7-u2a-k9b matt-nb5-mips64-u1-k1-k5 netbsd-5-0-1-RELEASE netbsd-5-0-RELEASE netbsd-5-0-RC4 netbsd-5-0-RC3 netbsd-5-0-RC2 netbsd-5-0-RC1 netbsd-5-base matt-mips64-base2 wrstuden-revivesa-base-3 wrstuden-revivesa-base-2 wrstuden-revivesa-base-1 yamt-pf42-base4 yamt-pf42-base3 hpcarm-cleanup-nbase yamt-pf42-base2 wrstuden-revivesa-base
# 1.11 29-Apr-2008 martin

Convert to new 2 clause license


Revision tags: netbsd-4-0-1-RELEASE wrstuden-fixsa-newbase yamt-pf42-baseX yamt-pf42-base keiichi-mipv6-base matt-armv6-nbase matt-armv6-prevmlocking wrstuden-fixsa-base-1 netbsd-4-0-RELEASE cube-autoconf-base netbsd-4-0-RC5 netbsd-4-0-RC4 netbsd-4-0-RC3 netbsd-4-0-RC2 netbsd-4-0-RC1 matt-armv6-base matt-mips64-base hpcarm-cleanup-base wrstuden-fixsa-base netbsd-4-base
# 1.10 09-Nov-2006 christos

branches: 1.10.16;
don't use alloca with ssp.


# 1.9 21-Aug-2006 christos

Change to a 3 clause copyright after permission of the holders.


Revision tags: abandoned-netbsd-4-base
# 1.8 03-Aug-2005 christos

Fix reversed test; from Gerry Swislow


# 1.7 11-Jun-2005 christos

PR/30500: Paul Shupak: Inconsistent definition of tilde_expand().
Provide a layer of indirection between the readline compatibility functions
and our internal implementation, so that we have the freedom to change the
function signature.


# 1.6 10-Jun-2005 christos

Bug reported from Martin Dietze:

The place to change the completion_append_character is
usually somewhere in the `rl_completion_entry_function'
callback which is where one usually can distinguish between
file- or dir-like entries to append a slash for dirs etc.

This does no longer work since `fn_complete()' takes the
`append_character' as argument before the callback is executed,
so that changes to the variable `rl_completion_append_character'
have in fact no effect for the current completion.

Fix by adding a function that returns the rl_completion_append_character,
when it gets passed in a filename in readline emulation.


# 1.5 18-May-2005 christos

Make completion_matches non-static since readline wants it.


# 1.4 12-May-2005 christos

PR/30215: Kouichirou Hiratsuka: /bin/sh dumps core with tabcomplete
Don't core-dump when trying to complete an empty line; instead assume ./


# 1.3 09-May-2005 dsl

Use getpwuid_r(getuid(), ...) to expand ~/....
Don't replace ~xyz with /home/xyz when expanding ~


# 1.2 07-May-2005 dsl

gdb directly calls filename_completion_function()


# 1.1 07-May-2005 dsl

Separate out the filename completion functions from the readline() code.
Pass in loads of parameters instead of relying on shed-loads of global
variables to modify the behaviour.
The filename completion code can now be enabled by code that uses el_gets().
(eg /bin/sh)


# 1.67 28-Mar-2021 christos

Only unescape when we are quoting and don't add a space if we are quoting
(we already did) (Piotr Stefaniak)


# 1.66 28-Mar-2021 christos

Pass the unescaped filename the the append function so it has to do less work
(for example it can call stat(2) directly (Piotr Stefaniak)


# 1.65 27-Mar-2021 christos

Add fn_complete2() that controls the quoting of the returned match.
Before it was based on the heuristic that we were not supplied an
attempted_completion_function, which worked well because programs
that supplied that function were not shells and did not want/understand
shell quoting. Recently though Piotr Stefaniak wanted to enhance command
completion for the Bourne Shell and this could benefit quoting the returned
command. This function adds an extra flags argument that controls that quoting.


Revision tags: phil-wifi-20200421 phil-wifi-20200411 is-mlppp-base phil-wifi-20200406
# 1.64 05-Jan-2020 abhinav

PR lib/54510 - when user supplied completion function is there,
don't unescape the string to be completed.


# 1.63 05-Jan-2020 tih

Summary: Remove over-simplified extraneous test

The file name matching code in libedit tries to adjust to the presence
of explicit " or ' characters in the input line, but tries too hard.
Remove a conditional that goes overboard, and causes the completion
code to fail if a quoted string is seen before the filename to be
expanded, as in

grep 'foo' bar<TAB>

Before this change, the above would not expand any possible
completions, even if they existed, because it would choose to look for
files whose names started with " bar".


# 1.62 10-Dec-2019 christos

When 'attempted_completion_function' non-NULL, with a 'single_match'
match, the expected space is not being added. Problem observed with
"chronyc" and "sqlite3" tab completion. That functionality got
moved to escape_filename() for the !attempted_completion_function
case, but the non-NULL 'attempted_completion_function' case must
also be handled. (Lonnie Abelbeck)


Revision tags: phil-wifi-20191119
# 1.61 09-Oct-2019 christos

add +1 to strlcpy's (Patrick Welche)


# 1.60 08-Oct-2019 christos

remore error(1) comment


# 1.59 08-Oct-2019 christos

Change strncpy to either memcpy (when we know the len), or strlcpy (when
we used to NUL terminate explicitly.


# 1.58 08-Sep-2019 abhinav

PR lib/54510: Fix file completion inside quotes which broke in rev 1.53

While there also fix handling character appending in the file completions when
inside quotes. For example when inside a quote, if the completion is a directory then
append a '/' but don't close the quote. On the other hand when inside a quote if the
completion is a file name and it is the only match then we can close the quote.


Revision tags: netbsd-9-1-RELEASE netbsd-9-0-RELEASE netbsd-9-0-RC2 netbsd-9-0-RC1 netbsd-9-base
# 1.57 28-Jul-2019 christos

PR/54415: Ricky Zhou: libedit stats completions for non-file completions
Use the proper completion function and account for the character appended
by the function when computing the number of columns.


# 1.56 23-Jul-2019 christos

PR/54399: S�ren Tempel: Uninitialized memory access in libedit history.
Initialize the buffer using calloc. While here change all malloc(a * sizeof(b))
to calloc(a, sizeof(b)). XXX: should fix realloc similarly.


Revision tags: phil-wifi-20190609
# 1.55 20-Apr-2019 abhinav

PR lib/54131 - declare the loop variable outside the for loop


# 1.54 12-Apr-2019 christos

PR/54117: Paavo Helde: Fix memory overrun: Account for the closing quote
in memory allocation if quoted.


# 1.53 31-Mar-2019 abhinav

Perform quoting of filename completions when there are multiple matches as well

Quoting of special characters in filename completion was implemented for single match
case, this enables it for multiple matches as well. For example:

$ touch 'foo bar'
$ touch 'foo baz'
$ ls fo<TAB>
autocompletes to =>
$ ls foo\ ba
hitting <TAB> again shows:
foo bar foo baz

This required unescaping escape sequences generated during last completion
in order to find the word to complete.

While there, also update the test to include cases for multiple matches.

Reviewed by christos


# 1.52 24-Mar-2019 abhinav

Only quote the completion matches if we are doing filename completion

If the user supplies a value for the attempted_completion_function parameter
then we cannot be sure if the completion is for filename or something else, in such
a case don't attempt to quote the completion matches.

Reviewed by christos

This should address PR lib/54067


Revision tags: pgoyette-compat-20190127 pgoyette-compat-20190118 pgoyette-compat-1226 pgoyette-compat-1126 pgoyette-compat-1020 pgoyette-compat-0930 pgoyette-compat-0906 pgoyette-compat-0728 phil-wifi-base pgoyette-compat-0625 pgoyette-compat-0521
# 1.51 04-May-2018 christos

branches: 1.51.2;
fix uninitialized


# 1.50 04-May-2018 abhinav

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


# 1.49 02-May-2018 abhinav

Add '*' and '[' to the list of characters which need escaping during autocompletion


Revision tags: pgoyette-compat-0502 pgoyette-compat-0422 pgoyette-compat-0415 pgoyette-compat-0407 pgoyette-compat-0330 pgoyette-compat-0322 pgoyette-compat-0315 pgoyette-compat-base
# 1.48 27-Oct-2017 abhinav

branches: 1.48.2;
Add NULL check after doing memory allocation at a couple of places

ok christos@


# 1.47 15-Oct-2017 abhinav

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.


# 1.46 16-Sep-2017 abhinav

Fix indentation (convert spaces to tab)


Revision tags: netbsd-8-2-RELEASE netbsd-8-1-RELEASE netbsd-8-1-RC1 netbsd-8-0-RELEASE netbsd-8-0-RC2 netbsd-8-0-RC1 matt-nb8-mediatek-base perseant-stdc-iso10646-base netbsd-8-base prg-localcount2-base3 prg-localcount2-base2 prg-localcount2-base1 prg-localcount2-base pgoyette-localcount-20170426 bouyer-socketcan-base1
# 1.45 21-Apr-2017 abhinav

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@


Revision tags: pgoyette-localcount-20170320 bouyer-socketcan-base pgoyette-localcount-20170107 pgoyette-localcount-20161104
# 1.44 31-Oct-2016 abhinav

branches: 1.44.2;
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.


Revision tags: localcount-20160914 pgoyette-localcount-20160806 pgoyette-localcount-20160726 pgoyette-localcount-base
# 1.43 11-Apr-2016 christos

branches: 1.43.2;
Char -> wchar_t from Ingo Schwarze.


# 1.42 11-Apr-2016 christos

more macro WIDECHAR undoing from Ingo Schwarze.


# 1.41 09-Apr-2016 christos

Change some 0's to NULL's from Pedro Giffuni


# 1.40 17-Feb-2016 christos

whitespace and header sorting changes (Ingo Schwarze). No functional changes.


# 1.39 16-Feb-2016 christos

More header cleanups from Ingo Schwarze.


# 1.38 16-Feb-2016 christos

more include file cleanup (Ingo Schwarze)


# 1.37 16-Feb-2016 christos

From Ingo Scharze:
Let "el.h" include everything needed for struct editline,
and don't include that stuff multiple times. That also improves
consistency, also avoids circular inclusions, and also makes it
easier to follow what is going on, even though not quite as nice.
But it seems like the best we can do...


# 1.36 16-Feb-2016 christos

cleanup chartype.h includes (Ingo Schwarze)


# 1.35 16-Feb-2016 christos

cleanup inclusion of histedit.h (Ingo Schwarze)


# 1.34 18-Oct-2014 riz

callers's -> caller's


# 1.33 18-Oct-2014 snj

src is too big these days to tolerate superfluous apostrophes. It's
"its", people!


Revision tags: netbsd-7-base tls-earlyentropy-base tls-maxphys-base
# 1.32 05-Jun-2014 christos

branches: 1.32.2;
PR/48876: Dmitriy Grigoryev: Core dump in readline lib on attempted expansion
Make sure we have 2 matches before calling strcmp().


Revision tags: netbsd-6-0-6-RELEASE netbsd-6-1-5-RELEASE yamt-pagecache-base9 yamt-pagecache-tag8 netbsd-6-1-4-RELEASE netbsd-6-0-5-RELEASE riastradh-xf86-video-intel-2-7-1-pre-2-21-15 riastradh-drm2-base3 netbsd-6-1-3-RELEASE netbsd-6-0-4-RELEASE netbsd-6-1-2-RELEASE netbsd-6-0-3-RELEASE netbsd-6-1-1-RELEASE riastradh-drm2-base2 riastradh-drm2-base1 riastradh-drm2-base netbsd-6-0-2-RELEASE netbsd-6-1-RELEASE netbsd-6-1-RC4 netbsd-6-1-RC3 agc-symver-base netbsd-6-1-RC2 netbsd-6-1-RC1 yamt-pagecache-base8 netbsd-6-0-1-RELEASE yamt-pagecache-base7 matt-nb6-plus-nbase yamt-pagecache-base6 netbsd-6-0-RELEASE netbsd-6-0-RC2 matt-nb6-plus-base netbsd-6-0-RC1 yamt-pagecache-base5 yamt-pagecache-base4 netbsd-6-base yamt-pagecache-base3 yamt-pagecache-base2 yamt-pagecache-base
# 1.31 16-Sep-2011 plunky

branches: 1.31.8; 1.31.18;
NULL does not need a cast, here


# 1.30 16-Aug-2011 christos

re-enable -Wconversion


# 1.29 29-Jul-2011 christos

pass -Wconversion


# 1.28 29-Jul-2011 christos

- fix unused params
- unconditionalize vis.h


# 1.27 29-Jul-2011 christos

KNF return (\1); -> return \1;


# 1.26 28-Jul-2011 christos

kill ptr_t and ioctl_t, add * sizeof(*foo) to all allocations.


# 1.25 28-Jul-2011 christos

fix unused variable warnings on systems without _r functions


# 1.24 28-Jul-2011 christos

eliminate alloca for portability
portable getpw{nam,uid}


Revision tags: cherry-xenmp-base bouyer-quota2-nbase bouyer-quota2-base matt-mips64-premerge-20101231
# 1.23 06-Dec-2010 dholland

Improve previous to avoid changing the interface of an externally
exposed function. (But note that this function is neither documented
nor declared in any installed header file, and it probably should not
be externally exposed.) Related to PR 44183, closes PR 44186.


# 1.22 02-Dec-2010 dholland

add const, from PR 44183.


# 1.21 02-Dec-2010 dholland

Fix up bodgy code for printing completion matches; it used to sometimes
skip entries, print (null), run off the end of the array, or occasionally
receive SIGSEGV, and now will, hopefully at least, do none of that.

Based in part on the patch in PR 44183 from Sergio Acereda; I also
did some tidyup and fixed it to print top-to-bottom first like ls(1).


# 1.20 15-Nov-2010 christos

don't increment i twice in the loop. From Michael Byrnes


# 1.19 01-Jun-2010 christos

tidy up memory allocation and don't unnecessarily print "./" before names.


# 1.18 18-Jan-2010 christos

PR/42637: Joachim Kuebart: Shell tab completion crashes due to libedit stack
smashing


# 1.17 30-Dec-2009 christos

Wide character support (UTF-8) from Johny Mattsson; currently disabled.


# 1.16 28-Dec-2009 christos

improve on the listing display by printing only one character after the
filename not two, and no trailing blanks. I will revisit this when I write
the ls-F code.


Revision tags: matt-premerge-20091211 jym-xensuspend-nbase jym-xensuspend-base
# 1.15 16-Feb-2009 christos

fix sign compare issues.


# 1.14 15-Feb-2009 christos

pass lint on _LP64.


# 1.13 26-Jan-2009 apb

branches: 1.13.2;
Define HAVE_STRUCT_DIRENT_D_NAMLEN in config,h, and test it when
deciding whether to use entry->d_namlen or strlen(entry->d_name).
Addresses PR 40477 by Robert Millan.


# 1.12 11-Jan-2009 christos

- insert a space after the recognized string if it was an exact match
- initialize properly the string used for completion.
From Alex Bligh alex at alex dot org dot uk
- Make char constants consistent


Revision tags: netbsd-5-2-3-RELEASE netbsd-5-1-5-RELEASE netbsd-5-2-2-RELEASE netbsd-5-1-4-RELEASE netbsd-5-2-1-RELEASE netbsd-5-1-3-RELEASE netbsd-5-2-RELEASE netbsd-5-2-RC1 netbsd-5-1-2-RELEASE netbsd-5-1-1-RELEASE matt-nb5-mips64-premerge-20101231 matt-nb5-pq3-base netbsd-5-1-RELEASE netbsd-5-1-RC4 matt-nb5-mips64-k15 netbsd-5-1-RC3 netbsd-5-1-RC2 netbsd-5-1-RC1 netbsd-5-0-2-RELEASE matt-nb5-mips64-premerge-20091211 matt-nb5-mips64-u2-k2-k4-k7-k8-k9 matt-nb4-mips64-k7-u2a-k9b matt-nb5-mips64-u1-k1-k5 netbsd-5-0-1-RELEASE netbsd-5-0-RELEASE netbsd-5-0-RC4 netbsd-5-0-RC3 netbsd-5-0-RC2 netbsd-5-0-RC1 netbsd-5-base matt-mips64-base2 wrstuden-revivesa-base-3 wrstuden-revivesa-base-2 wrstuden-revivesa-base-1 yamt-pf42-base4 yamt-pf42-base3 hpcarm-cleanup-nbase yamt-pf42-base2 wrstuden-revivesa-base
# 1.11 29-Apr-2008 martin

Convert to new 2 clause license


Revision tags: netbsd-4-0-1-RELEASE wrstuden-fixsa-newbase yamt-pf42-baseX yamt-pf42-base keiichi-mipv6-base matt-armv6-nbase matt-armv6-prevmlocking wrstuden-fixsa-base-1 netbsd-4-0-RELEASE cube-autoconf-base netbsd-4-0-RC5 netbsd-4-0-RC4 netbsd-4-0-RC3 netbsd-4-0-RC2 netbsd-4-0-RC1 matt-armv6-base matt-mips64-base hpcarm-cleanup-base wrstuden-fixsa-base netbsd-4-base
# 1.10 09-Nov-2006 christos

branches: 1.10.16;
don't use alloca with ssp.


# 1.9 21-Aug-2006 christos

Change to a 3 clause copyright after permission of the holders.


Revision tags: abandoned-netbsd-4-base
# 1.8 03-Aug-2005 christos

Fix reversed test; from Gerry Swislow


# 1.7 11-Jun-2005 christos

PR/30500: Paul Shupak: Inconsistent definition of tilde_expand().
Provide a layer of indirection between the readline compatibility functions
and our internal implementation, so that we have the freedom to change the
function signature.


# 1.6 10-Jun-2005 christos

Bug reported from Martin Dietze:

The place to change the completion_append_character is
usually somewhere in the `rl_completion_entry_function'
callback which is where one usually can distinguish between
file- or dir-like entries to append a slash for dirs etc.

This does no longer work since `fn_complete()' takes the
`append_character' as argument before the callback is executed,
so that changes to the variable `rl_completion_append_character'
have in fact no effect for the current completion.

Fix by adding a function that returns the rl_completion_append_character,
when it gets passed in a filename in readline emulation.


# 1.5 18-May-2005 christos

Make completion_matches non-static since readline wants it.


# 1.4 12-May-2005 christos

PR/30215: Kouichirou Hiratsuka: /bin/sh dumps core with tabcomplete
Don't core-dump when trying to complete an empty line; instead assume ./


# 1.3 09-May-2005 dsl

Use getpwuid_r(getuid(), ...) to expand ~/....
Don't replace ~xyz with /home/xyz when expanding ~


# 1.2 07-May-2005 dsl

gdb directly calls filename_completion_function()


# 1.1 07-May-2005 dsl

Separate out the filename completion functions from the readline() code.
Pass in loads of parameters instead of relying on shed-loads of global
variables to modify the behaviour.
The filename completion code can now be enabled by code that uses el_gets().
(eg /bin/sh)


# 1.67 28-Mar-2021 christos

Only unescape when we are quoting and don't add a space if we are quoting
(we already did) (Piotr Stefaniak)


# 1.66 28-Mar-2021 christos

Pass the unescaped filename the the append function so it has to do less work
(for example it can call stat(2) directly (Piotr Stefaniak)


# 1.65 27-Mar-2021 christos

Add fn_complete2() that controls the quoting of the returned match.
Before it was based on the heuristic that we were not supplied an
attempted_completion_function, which worked well because programs
that supplied that function were not shells and did not want/understand
shell quoting. Recently though Piotr Stefaniak wanted to enhance command
completion for the Bourne Shell and this could benefit quoting the returned
command. This function adds an extra flags argument that controls that quoting.


Revision tags: phil-wifi-20200421 phil-wifi-20200411 is-mlppp-base phil-wifi-20200406
# 1.64 05-Jan-2020 abhinav

PR lib/54510 - when user supplied completion function is there,
don't unescape the string to be completed.


# 1.63 05-Jan-2020 tih

Summary: Remove over-simplified extraneous test

The file name matching code in libedit tries to adjust to the presence
of explicit " or ' characters in the input line, but tries too hard.
Remove a conditional that goes overboard, and causes the completion
code to fail if a quoted string is seen before the filename to be
expanded, as in

grep 'foo' bar<TAB>

Before this change, the above would not expand any possible
completions, even if they existed, because it would choose to look for
files whose names started with " bar".


# 1.62 10-Dec-2019 christos

When 'attempted_completion_function' non-NULL, with a 'single_match'
match, the expected space is not being added. Problem observed with
"chronyc" and "sqlite3" tab completion. That functionality got
moved to escape_filename() for the !attempted_completion_function
case, but the non-NULL 'attempted_completion_function' case must
also be handled. (Lonnie Abelbeck)


Revision tags: phil-wifi-20191119
# 1.61 09-Oct-2019 christos

add +1 to strlcpy's (Patrick Welche)


# 1.60 08-Oct-2019 christos

remore error(1) comment


# 1.59 08-Oct-2019 christos

Change strncpy to either memcpy (when we know the len), or strlcpy (when
we used to NUL terminate explicitly.


# 1.58 08-Sep-2019 abhinav

PR lib/54510: Fix file completion inside quotes which broke in rev 1.53

While there also fix handling character appending in the file completions when
inside quotes. For example when inside a quote, if the completion is a directory then
append a '/' but don't close the quote. On the other hand when inside a quote if the
completion is a file name and it is the only match then we can close the quote.


Revision tags: netbsd-9-1-RELEASE netbsd-9-0-RELEASE netbsd-9-0-RC2 netbsd-9-0-RC1 netbsd-9-base
# 1.57 28-Jul-2019 christos

PR/54415: Ricky Zhou: libedit stats completions for non-file completions
Use the proper completion function and account for the character appended
by the function when computing the number of columns.


# 1.56 23-Jul-2019 christos

PR/54399: S�ren Tempel: Uninitialized memory access in libedit history.
Initialize the buffer using calloc. While here change all malloc(a * sizeof(b))
to calloc(a, sizeof(b)). XXX: should fix realloc similarly.


Revision tags: phil-wifi-20190609
# 1.55 20-Apr-2019 abhinav

PR lib/54131 - declare the loop variable outside the for loop


# 1.54 12-Apr-2019 christos

PR/54117: Paavo Helde: Fix memory overrun: Account for the closing quote
in memory allocation if quoted.


# 1.53 31-Mar-2019 abhinav

Perform quoting of filename completions when there are multiple matches as well

Quoting of special characters in filename completion was implemented for single match
case, this enables it for multiple matches as well. For example:

$ touch 'foo bar'
$ touch 'foo baz'
$ ls fo<TAB>
autocompletes to =>
$ ls foo\ ba
hitting <TAB> again shows:
foo bar foo baz

This required unescaping escape sequences generated during last completion
in order to find the word to complete.

While there, also update the test to include cases for multiple matches.

Reviewed by christos


# 1.52 24-Mar-2019 abhinav

Only quote the completion matches if we are doing filename completion

If the user supplies a value for the attempted_completion_function parameter
then we cannot be sure if the completion is for filename or something else, in such
a case don't attempt to quote the completion matches.

Reviewed by christos

This should address PR lib/54067


Revision tags: pgoyette-compat-20190127 pgoyette-compat-20190118 pgoyette-compat-1226 pgoyette-compat-1126 pgoyette-compat-1020 pgoyette-compat-0930 pgoyette-compat-0906 pgoyette-compat-0728 phil-wifi-base pgoyette-compat-0625 pgoyette-compat-0521
# 1.51 04-May-2018 christos

branches: 1.51.2;
fix uninitialized


# 1.50 04-May-2018 abhinav

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


# 1.49 02-May-2018 abhinav

Add '*' and '[' to the list of characters which need escaping during autocompletion


Revision tags: pgoyette-compat-0502 pgoyette-compat-0422 pgoyette-compat-0415 pgoyette-compat-0407 pgoyette-compat-0330 pgoyette-compat-0322 pgoyette-compat-0315 pgoyette-compat-base
# 1.48 27-Oct-2017 abhinav

branches: 1.48.2;
Add NULL check after doing memory allocation at a couple of places

ok christos@


# 1.47 15-Oct-2017 abhinav

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.


# 1.46 16-Sep-2017 abhinav

Fix indentation (convert spaces to tab)


Revision tags: netbsd-8-2-RELEASE netbsd-8-1-RELEASE netbsd-8-1-RC1 netbsd-8-0-RELEASE netbsd-8-0-RC2 netbsd-8-0-RC1 matt-nb8-mediatek-base perseant-stdc-iso10646-base netbsd-8-base prg-localcount2-base3 prg-localcount2-base2 prg-localcount2-base1 prg-localcount2-base pgoyette-localcount-20170426 bouyer-socketcan-base1
# 1.45 21-Apr-2017 abhinav

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@


Revision tags: pgoyette-localcount-20170320 bouyer-socketcan-base pgoyette-localcount-20170107 pgoyette-localcount-20161104
# 1.44 31-Oct-2016 abhinav

branches: 1.44.2;
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.


Revision tags: localcount-20160914 pgoyette-localcount-20160806 pgoyette-localcount-20160726 pgoyette-localcount-base
# 1.43 11-Apr-2016 christos

branches: 1.43.2;
Char -> wchar_t from Ingo Schwarze.


# 1.42 11-Apr-2016 christos

more macro WIDECHAR undoing from Ingo Schwarze.


# 1.41 09-Apr-2016 christos

Change some 0's to NULL's from Pedro Giffuni


# 1.40 17-Feb-2016 christos

whitespace and header sorting changes (Ingo Schwarze). No functional changes.


# 1.39 16-Feb-2016 christos

More header cleanups from Ingo Schwarze.


# 1.38 16-Feb-2016 christos

more include file cleanup (Ingo Schwarze)


# 1.37 16-Feb-2016 christos

From Ingo Scharze:
Let "el.h" include everything needed for struct editline,
and don't include that stuff multiple times. That also improves
consistency, also avoids circular inclusions, and also makes it
easier to follow what is going on, even though not quite as nice.
But it seems like the best we can do...


# 1.36 16-Feb-2016 christos

cleanup chartype.h includes (Ingo Schwarze)


# 1.35 16-Feb-2016 christos

cleanup inclusion of histedit.h (Ingo Schwarze)


# 1.34 18-Oct-2014 riz

callers's -> caller's


# 1.33 18-Oct-2014 snj

src is too big these days to tolerate superfluous apostrophes. It's
"its", people!


Revision tags: netbsd-7-base tls-earlyentropy-base tls-maxphys-base
# 1.32 05-Jun-2014 christos

branches: 1.32.2;
PR/48876: Dmitriy Grigoryev: Core dump in readline lib on attempted expansion
Make sure we have 2 matches before calling strcmp().


Revision tags: netbsd-6-0-6-RELEASE netbsd-6-1-5-RELEASE yamt-pagecache-base9 yamt-pagecache-tag8 netbsd-6-1-4-RELEASE netbsd-6-0-5-RELEASE riastradh-xf86-video-intel-2-7-1-pre-2-21-15 riastradh-drm2-base3 netbsd-6-1-3-RELEASE netbsd-6-0-4-RELEASE netbsd-6-1-2-RELEASE netbsd-6-0-3-RELEASE netbsd-6-1-1-RELEASE riastradh-drm2-base2 riastradh-drm2-base1 riastradh-drm2-base netbsd-6-0-2-RELEASE netbsd-6-1-RELEASE netbsd-6-1-RC4 netbsd-6-1-RC3 agc-symver-base netbsd-6-1-RC2 netbsd-6-1-RC1 yamt-pagecache-base8 netbsd-6-0-1-RELEASE yamt-pagecache-base7 matt-nb6-plus-nbase yamt-pagecache-base6 netbsd-6-0-RELEASE netbsd-6-0-RC2 matt-nb6-plus-base netbsd-6-0-RC1 yamt-pagecache-base5 yamt-pagecache-base4 netbsd-6-base yamt-pagecache-base3 yamt-pagecache-base2 yamt-pagecache-base
# 1.31 16-Sep-2011 plunky

branches: 1.31.8; 1.31.18;
NULL does not need a cast, here


# 1.30 16-Aug-2011 christos

re-enable -Wconversion


# 1.29 29-Jul-2011 christos

pass -Wconversion


# 1.28 29-Jul-2011 christos

- fix unused params
- unconditionalize vis.h


# 1.27 29-Jul-2011 christos

KNF return (\1); -> return \1;


# 1.26 28-Jul-2011 christos

kill ptr_t and ioctl_t, add * sizeof(*foo) to all allocations.


# 1.25 28-Jul-2011 christos

fix unused variable warnings on systems without _r functions


# 1.24 28-Jul-2011 christos

eliminate alloca for portability
portable getpw{nam,uid}


Revision tags: cherry-xenmp-base bouyer-quota2-nbase bouyer-quota2-base matt-mips64-premerge-20101231
# 1.23 06-Dec-2010 dholland

Improve previous to avoid changing the interface of an externally
exposed function. (But note that this function is neither documented
nor declared in any installed header file, and it probably should not
be externally exposed.) Related to PR 44183, closes PR 44186.


# 1.22 02-Dec-2010 dholland

add const, from PR 44183.


# 1.21 02-Dec-2010 dholland

Fix up bodgy code for printing completion matches; it used to sometimes
skip entries, print (null), run off the end of the array, or occasionally
receive SIGSEGV, and now will, hopefully at least, do none of that.

Based in part on the patch in PR 44183 from Sergio Acereda; I also
did some tidyup and fixed it to print top-to-bottom first like ls(1).


# 1.20 15-Nov-2010 christos

don't increment i twice in the loop. From Michael Byrnes


# 1.19 01-Jun-2010 christos

tidy up memory allocation and don't unnecessarily print "./" before names.


# 1.18 18-Jan-2010 christos

PR/42637: Joachim Kuebart: Shell tab completion crashes due to libedit stack
smashing


# 1.17 30-Dec-2009 christos

Wide character support (UTF-8) from Johny Mattsson; currently disabled.


# 1.16 28-Dec-2009 christos

improve on the listing display by printing only one character after the
filename not two, and no trailing blanks. I will revisit this when I write
the ls-F code.


Revision tags: matt-premerge-20091211 jym-xensuspend-nbase jym-xensuspend-base
# 1.15 16-Feb-2009 christos

fix sign compare issues.


# 1.14 15-Feb-2009 christos

pass lint on _LP64.


# 1.13 26-Jan-2009 apb

branches: 1.13.2;
Define HAVE_STRUCT_DIRENT_D_NAMLEN in config,h, and test it when
deciding whether to use entry->d_namlen or strlen(entry->d_name).
Addresses PR 40477 by Robert Millan.


# 1.12 11-Jan-2009 christos

- insert a space after the recognized string if it was an exact match
- initialize properly the string used for completion.
From Alex Bligh alex at alex dot org dot uk
- Make char constants consistent


Revision tags: netbsd-5-2-3-RELEASE netbsd-5-1-5-RELEASE netbsd-5-2-2-RELEASE netbsd-5-1-4-RELEASE netbsd-5-2-1-RELEASE netbsd-5-1-3-RELEASE netbsd-5-2-RELEASE netbsd-5-2-RC1 netbsd-5-1-2-RELEASE netbsd-5-1-1-RELEASE matt-nb5-mips64-premerge-20101231 matt-nb5-pq3-base netbsd-5-1-RELEASE netbsd-5-1-RC4 matt-nb5-mips64-k15 netbsd-5-1-RC3 netbsd-5-1-RC2 netbsd-5-1-RC1 netbsd-5-0-2-RELEASE matt-nb5-mips64-premerge-20091211 matt-nb5-mips64-u2-k2-k4-k7-k8-k9 matt-nb4-mips64-k7-u2a-k9b matt-nb5-mips64-u1-k1-k5 netbsd-5-0-1-RELEASE netbsd-5-0-RELEASE netbsd-5-0-RC4 netbsd-5-0-RC3 netbsd-5-0-RC2 netbsd-5-0-RC1 netbsd-5-base matt-mips64-base2 wrstuden-revivesa-base-3 wrstuden-revivesa-base-2 wrstuden-revivesa-base-1 yamt-pf42-base4 yamt-pf42-base3 hpcarm-cleanup-nbase yamt-pf42-base2 wrstuden-revivesa-base
# 1.11 29-Apr-2008 martin

Convert to new 2 clause license


Revision tags: netbsd-4-0-1-RELEASE wrstuden-fixsa-newbase yamt-pf42-baseX yamt-pf42-base keiichi-mipv6-base matt-armv6-nbase matt-armv6-prevmlocking wrstuden-fixsa-base-1 netbsd-4-0-RELEASE cube-autoconf-base netbsd-4-0-RC5 netbsd-4-0-RC4 netbsd-4-0-RC3 netbsd-4-0-RC2 netbsd-4-0-RC1 matt-armv6-base matt-mips64-base hpcarm-cleanup-base wrstuden-fixsa-base netbsd-4-base
# 1.10 09-Nov-2006 christos

branches: 1.10.16;
don't use alloca with ssp.


# 1.9 21-Aug-2006 christos

Change to a 3 clause copyright after permission of the holders.


Revision tags: abandoned-netbsd-4-base
# 1.8 03-Aug-2005 christos

Fix reversed test; from Gerry Swislow


# 1.7 11-Jun-2005 christos

PR/30500: Paul Shupak: Inconsistent definition of tilde_expand().
Provide a layer of indirection between the readline compatibility functions
and our internal implementation, so that we have the freedom to change the
function signature.


# 1.6 10-Jun-2005 christos

Bug reported from Martin Dietze:

The place to change the completion_append_character is
usually somewhere in the `rl_completion_entry_function'
callback which is where one usually can distinguish between
file- or dir-like entries to append a slash for dirs etc.

This does no longer work since `fn_complete()' takes the
`append_character' as argument before the callback is executed,
so that changes to the variable `rl_completion_append_character'
have in fact no effect for the current completion.

Fix by adding a function that returns the rl_completion_append_character,
when it gets passed in a filename in readline emulation.


# 1.5 18-May-2005 christos

Make completion_matches non-static since readline wants it.


# 1.4 12-May-2005 christos

PR/30215: Kouichirou Hiratsuka: /bin/sh dumps core with tabcomplete
Don't core-dump when trying to complete an empty line; instead assume ./


# 1.3 09-May-2005 dsl

Use getpwuid_r(getuid(), ...) to expand ~/....
Don't replace ~xyz with /home/xyz when expanding ~


# 1.2 07-May-2005 dsl

gdb directly calls filename_completion_function()


# 1.1 07-May-2005 dsl

Separate out the filename completion functions from the readline() code.
Pass in loads of parameters instead of relying on shed-loads of global
variables to modify the behaviour.
The filename completion code can now be enabled by code that uses el_gets().
(eg /bin/sh)


# 1.64 05-Jan-2020 abhinav

PR lib/54510 - when user supplied completion function is there,
don't unescape the string to be completed.


# 1.63 05-Jan-2020 tih

Summary: Remove over-simplified extraneous test

The file name matching code in libedit tries to adjust to the presence
of explicit " or ' characters in the input line, but tries too hard.
Remove a conditional that goes overboard, and causes the completion
code to fail if a quoted string is seen before the filename to be
expanded, as in

grep 'foo' bar<TAB>

Before this change, the above would not expand any possible
completions, even if they existed, because it would choose to look for
files whose names started with " bar".


# 1.62 10-Dec-2019 christos

When 'attempted_completion_function' non-NULL, with a 'single_match'
match, the expected space is not being added. Problem observed with
"chronyc" and "sqlite3" tab completion. That functionality got
moved to escape_filename() for the !attempted_completion_function
case, but the non-NULL 'attempted_completion_function' case must
also be handled. (Lonnie Abelbeck)


Revision tags: phil-wifi-20191119
# 1.61 09-Oct-2019 christos

add +1 to strlcpy's (Patrick Welche)


# 1.60 08-Oct-2019 christos

remore error(1) comment


# 1.59 08-Oct-2019 christos

Change strncpy to either memcpy (when we know the len), or strlcpy (when
we used to NUL terminate explicitly.


# 1.58 08-Sep-2019 abhinav

PR lib/54510: Fix file completion inside quotes which broke in rev 1.53

While there also fix handling character appending in the file completions when
inside quotes. For example when inside a quote, if the completion is a directory then
append a '/' but don't close the quote. On the other hand when inside a quote if the
completion is a file name and it is the only match then we can close the quote.


Revision tags: netbsd-9-0-RC1 netbsd-9-base
# 1.57 28-Jul-2019 christos

PR/54415: Ricky Zhou: libedit stats completions for non-file completions
Use the proper completion function and account for the character appended
by the function when computing the number of columns.


# 1.56 23-Jul-2019 christos

PR/54399: S�ren Tempel: Uninitialized memory access in libedit history.
Initialize the buffer using calloc. While here change all malloc(a * sizeof(b))
to calloc(a, sizeof(b)). XXX: should fix realloc similarly.


Revision tags: phil-wifi-20190609
# 1.55 20-Apr-2019 abhinav

PR lib/54131 - declare the loop variable outside the for loop


# 1.54 12-Apr-2019 christos

PR/54117: Paavo Helde: Fix memory overrun: Account for the closing quote
in memory allocation if quoted.


# 1.53 31-Mar-2019 abhinav

Perform quoting of filename completions when there are multiple matches as well

Quoting of special characters in filename completion was implemented for single match
case, this enables it for multiple matches as well. For example:

$ touch 'foo bar'
$ touch 'foo baz'
$ ls fo<TAB>
autocompletes to =>
$ ls foo\ ba
hitting <TAB> again shows:
foo bar foo baz

This required unescaping escape sequences generated during last completion
in order to find the word to complete.

While there, also update the test to include cases for multiple matches.

Reviewed by christos


# 1.52 24-Mar-2019 abhinav

Only quote the completion matches if we are doing filename completion

If the user supplies a value for the attempted_completion_function parameter
then we cannot be sure if the completion is for filename or something else, in such
a case don't attempt to quote the completion matches.

Reviewed by christos

This should address PR lib/54067


Revision tags: pgoyette-compat-20190127 pgoyette-compat-20190118 pgoyette-compat-1226 pgoyette-compat-1126 pgoyette-compat-1020 pgoyette-compat-0930 pgoyette-compat-0906 pgoyette-compat-0728 phil-wifi-base pgoyette-compat-0625 pgoyette-compat-0521
# 1.51 04-May-2018 christos

branches: 1.51.2;
fix uninitialized


# 1.50 04-May-2018 abhinav

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


# 1.49 02-May-2018 abhinav

Add '*' and '[' to the list of characters which need escaping during autocompletion


Revision tags: pgoyette-compat-0502 pgoyette-compat-0422 pgoyette-compat-0415 pgoyette-compat-0407 pgoyette-compat-0330 pgoyette-compat-0322 pgoyette-compat-0315 pgoyette-compat-base
# 1.48 27-Oct-2017 abhinav

branches: 1.48.2;
Add NULL check after doing memory allocation at a couple of places

ok christos@


# 1.47 15-Oct-2017 abhinav

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.


# 1.46 16-Sep-2017 abhinav

Fix indentation (convert spaces to tab)


Revision tags: netbsd-8-1-RELEASE netbsd-8-1-RC1 netbsd-8-0-RELEASE netbsd-8-0-RC2 netbsd-8-0-RC1 matt-nb8-mediatek-base perseant-stdc-iso10646-base netbsd-8-base prg-localcount2-base3 prg-localcount2-base2 prg-localcount2-base1 prg-localcount2-base pgoyette-localcount-20170426 bouyer-socketcan-base1
# 1.45 21-Apr-2017 abhinav

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@


Revision tags: pgoyette-localcount-20170320 bouyer-socketcan-base pgoyette-localcount-20170107 pgoyette-localcount-20161104
# 1.44 31-Oct-2016 abhinav

branches: 1.44.2;
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.


Revision tags: localcount-20160914 pgoyette-localcount-20160806 pgoyette-localcount-20160726 pgoyette-localcount-base
# 1.43 11-Apr-2016 christos

branches: 1.43.2;
Char -> wchar_t from Ingo Schwarze.


# 1.42 11-Apr-2016 christos

more macro WIDECHAR undoing from Ingo Schwarze.


# 1.41 09-Apr-2016 christos

Change some 0's to NULL's from Pedro Giffuni


# 1.40 17-Feb-2016 christos

whitespace and header sorting changes (Ingo Schwarze). No functional changes.


# 1.39 16-Feb-2016 christos

More header cleanups from Ingo Schwarze.


# 1.38 16-Feb-2016 christos

more include file cleanup (Ingo Schwarze)


# 1.37 16-Feb-2016 christos

From Ingo Scharze:
Let "el.h" include everything needed for struct editline,
and don't include that stuff multiple times. That also improves
consistency, also avoids circular inclusions, and also makes it
easier to follow what is going on, even though not quite as nice.
But it seems like the best we can do...


# 1.36 16-Feb-2016 christos

cleanup chartype.h includes (Ingo Schwarze)


# 1.35 16-Feb-2016 christos

cleanup inclusion of histedit.h (Ingo Schwarze)


# 1.34 18-Oct-2014 riz

callers's -> caller's


# 1.33 18-Oct-2014 snj

src is too big these days to tolerate superfluous apostrophes. It's
"its", people!


Revision tags: netbsd-7-base tls-earlyentropy-base tls-maxphys-base
# 1.32 05-Jun-2014 christos

branches: 1.32.2;
PR/48876: Dmitriy Grigoryev: Core dump in readline lib on attempted expansion
Make sure we have 2 matches before calling strcmp().


Revision tags: netbsd-6-0-6-RELEASE netbsd-6-1-5-RELEASE yamt-pagecache-base9 yamt-pagecache-tag8 netbsd-6-1-4-RELEASE netbsd-6-0-5-RELEASE riastradh-xf86-video-intel-2-7-1-pre-2-21-15 riastradh-drm2-base3 netbsd-6-1-3-RELEASE netbsd-6-0-4-RELEASE netbsd-6-1-2-RELEASE netbsd-6-0-3-RELEASE netbsd-6-1-1-RELEASE riastradh-drm2-base2 riastradh-drm2-base1 riastradh-drm2-base netbsd-6-0-2-RELEASE netbsd-6-1-RELEASE netbsd-6-1-RC4 netbsd-6-1-RC3 agc-symver-base netbsd-6-1-RC2 netbsd-6-1-RC1 yamt-pagecache-base8 netbsd-6-0-1-RELEASE yamt-pagecache-base7 matt-nb6-plus-nbase yamt-pagecache-base6 netbsd-6-0-RELEASE netbsd-6-0-RC2 matt-nb6-plus-base netbsd-6-0-RC1 yamt-pagecache-base5 yamt-pagecache-base4 netbsd-6-base yamt-pagecache-base3 yamt-pagecache-base2 yamt-pagecache-base
# 1.31 16-Sep-2011 plunky

branches: 1.31.8; 1.31.18;
NULL does not need a cast, here


# 1.30 16-Aug-2011 christos

re-enable -Wconversion


# 1.29 29-Jul-2011 christos

pass -Wconversion


# 1.28 29-Jul-2011 christos

- fix unused params
- unconditionalize vis.h


# 1.27 29-Jul-2011 christos

KNF return (\1); -> return \1;


# 1.26 28-Jul-2011 christos

kill ptr_t and ioctl_t, add * sizeof(*foo) to all allocations.


# 1.25 28-Jul-2011 christos

fix unused variable warnings on systems without _r functions


# 1.24 28-Jul-2011 christos

eliminate alloca for portability
portable getpw{nam,uid}


Revision tags: cherry-xenmp-base bouyer-quota2-nbase bouyer-quota2-base matt-mips64-premerge-20101231
# 1.23 06-Dec-2010 dholland

Improve previous to avoid changing the interface of an externally
exposed function. (But note that this function is neither documented
nor declared in any installed header file, and it probably should not
be externally exposed.) Related to PR 44183, closes PR 44186.


# 1.22 02-Dec-2010 dholland

add const, from PR 44183.


# 1.21 02-Dec-2010 dholland

Fix up bodgy code for printing completion matches; it used to sometimes
skip entries, print (null), run off the end of the array, or occasionally
receive SIGSEGV, and now will, hopefully at least, do none of that.

Based in part on the patch in PR 44183 from Sergio Acereda; I also
did some tidyup and fixed it to print top-to-bottom first like ls(1).


# 1.20 15-Nov-2010 christos

don't increment i twice in the loop. From Michael Byrnes


# 1.19 01-Jun-2010 christos

tidy up memory allocation and don't unnecessarily print "./" before names.


# 1.18 18-Jan-2010 christos

PR/42637: Joachim Kuebart: Shell tab completion crashes due to libedit stack
smashing


# 1.17 30-Dec-2009 christos

Wide character support (UTF-8) from Johny Mattsson; currently disabled.


# 1.16 28-Dec-2009 christos

improve on the listing display by printing only one character after the
filename not two, and no trailing blanks. I will revisit this when I write
the ls-F code.


Revision tags: matt-premerge-20091211 jym-xensuspend-nbase jym-xensuspend-base
# 1.15 16-Feb-2009 christos

fix sign compare issues.


# 1.14 15-Feb-2009 christos

pass lint on _LP64.


# 1.13 26-Jan-2009 apb

branches: 1.13.2;
Define HAVE_STRUCT_DIRENT_D_NAMLEN in config,h, and test it when
deciding whether to use entry->d_namlen or strlen(entry->d_name).
Addresses PR 40477 by Robert Millan.


# 1.12 11-Jan-2009 christos

- insert a space after the recognized string if it was an exact match
- initialize properly the string used for completion.
From Alex Bligh alex at alex dot org dot uk
- Make char constants consistent


Revision tags: netbsd-5-2-3-RELEASE netbsd-5-1-5-RELEASE netbsd-5-2-2-RELEASE netbsd-5-1-4-RELEASE netbsd-5-2-1-RELEASE netbsd-5-1-3-RELEASE netbsd-5-2-RELEASE netbsd-5-2-RC1 netbsd-5-1-2-RELEASE netbsd-5-1-1-RELEASE matt-nb5-mips64-premerge-20101231 matt-nb5-pq3-base netbsd-5-1-RELEASE netbsd-5-1-RC4 matt-nb5-mips64-k15 netbsd-5-1-RC3 netbsd-5-1-RC2 netbsd-5-1-RC1 netbsd-5-0-2-RELEASE matt-nb5-mips64-premerge-20091211 matt-nb5-mips64-u2-k2-k4-k7-k8-k9 matt-nb4-mips64-k7-u2a-k9b matt-nb5-mips64-u1-k1-k5 netbsd-5-0-1-RELEASE netbsd-5-0-RELEASE netbsd-5-0-RC4 netbsd-5-0-RC3 netbsd-5-0-RC2 netbsd-5-0-RC1 netbsd-5-base matt-mips64-base2 wrstuden-revivesa-base-3 wrstuden-revivesa-base-2 wrstuden-revivesa-base-1 yamt-pf42-base4 yamt-pf42-base3 hpcarm-cleanup-nbase yamt-pf42-base2 wrstuden-revivesa-base
# 1.11 29-Apr-2008 martin

Convert to new 2 clause license


Revision tags: netbsd-4-0-1-RELEASE wrstuden-fixsa-newbase yamt-pf42-baseX yamt-pf42-base keiichi-mipv6-base matt-armv6-nbase matt-armv6-prevmlocking wrstuden-fixsa-base-1 netbsd-4-0-RELEASE cube-autoconf-base netbsd-4-0-RC5 netbsd-4-0-RC4 netbsd-4-0-RC3 netbsd-4-0-RC2 netbsd-4-0-RC1 matt-armv6-base matt-mips64-base hpcarm-cleanup-base wrstuden-fixsa-base netbsd-4-base
# 1.10 09-Nov-2006 christos

branches: 1.10.16;
don't use alloca with ssp.


# 1.9 21-Aug-2006 christos

Change to a 3 clause copyright after permission of the holders.


Revision tags: abandoned-netbsd-4-base
# 1.8 03-Aug-2005 christos

Fix reversed test; from Gerry Swislow


# 1.7 11-Jun-2005 christos

PR/30500: Paul Shupak: Inconsistent definition of tilde_expand().
Provide a layer of indirection between the readline compatibility functions
and our internal implementation, so that we have the freedom to change the
function signature.


# 1.6 10-Jun-2005 christos

Bug reported from Martin Dietze:

The place to change the completion_append_character is
usually somewhere in the `rl_completion_entry_function'
callback which is where one usually can distinguish between
file- or dir-like entries to append a slash for dirs etc.

This does no longer work since `fn_complete()' takes the
`append_character' as argument before the callback is executed,
so that changes to the variable `rl_completion_append_character'
have in fact no effect for the current completion.

Fix by adding a function that returns the rl_completion_append_character,
when it gets passed in a filename in readline emulation.


# 1.5 18-May-2005 christos

Make completion_matches non-static since readline wants it.


# 1.4 12-May-2005 christos

PR/30215: Kouichirou Hiratsuka: /bin/sh dumps core with tabcomplete
Don't core-dump when trying to complete an empty line; instead assume ./


# 1.3 09-May-2005 dsl

Use getpwuid_r(getuid(), ...) to expand ~/....
Don't replace ~xyz with /home/xyz when expanding ~


# 1.2 07-May-2005 dsl

gdb directly calls filename_completion_function()


# 1.1 07-May-2005 dsl

Separate out the filename completion functions from the readline() code.
Pass in loads of parameters instead of relying on shed-loads of global
variables to modify the behaviour.
The filename completion code can now be enabled by code that uses el_gets().
(eg /bin/sh)


# 1.62 10-Dec-2019 christos

When 'attempted_completion_function' non-NULL, with a 'single_match'
match, the expected space is not being added. Problem observed with
"chronyc" and "sqlite3" tab completion. That functionality got
moved to escape_filename() for the !attempted_completion_function
case, but the non-NULL 'attempted_completion_function' case must
also be handled. (Lonnie Abelbeck)


Revision tags: phil-wifi-20191119
# 1.61 09-Oct-2019 christos

add +1 to strlcpy's (Patrick Welche)


# 1.60 08-Oct-2019 christos

remore error(1) comment


# 1.59 08-Oct-2019 christos

Change strncpy to either memcpy (when we know the len), or strlcpy (when
we used to NUL terminate explicitly.


# 1.58 08-Sep-2019 abhinav

PR lib/54510: Fix file completion inside quotes which broke in rev 1.53

While there also fix handling character appending in the file completions when
inside quotes. For example when inside a quote, if the completion is a directory then
append a '/' but don't close the quote. On the other hand when inside a quote if the
completion is a file name and it is the only match then we can close the quote.


Revision tags: netbsd-9-0-RC1 netbsd-9-base
# 1.57 28-Jul-2019 christos

PR/54415: Ricky Zhou: libedit stats completions for non-file completions
Use the proper completion function and account for the character appended
by the function when computing the number of columns.


# 1.56 23-Jul-2019 christos

PR/54399: S�ren Tempel: Uninitialized memory access in libedit history.
Initialize the buffer using calloc. While here change all malloc(a * sizeof(b))
to calloc(a, sizeof(b)). XXX: should fix realloc similarly.


Revision tags: phil-wifi-20190609
# 1.55 20-Apr-2019 abhinav

PR lib/54131 - declare the loop variable outside the for loop


# 1.54 12-Apr-2019 christos

PR/54117: Paavo Helde: Fix memory overrun: Account for the closing quote
in memory allocation if quoted.


# 1.53 31-Mar-2019 abhinav

Perform quoting of filename completions when there are multiple matches as well

Quoting of special characters in filename completion was implemented for single match
case, this enables it for multiple matches as well. For example:

$ touch 'foo bar'
$ touch 'foo baz'
$ ls fo<TAB>
autocompletes to =>
$ ls foo\ ba
hitting <TAB> again shows:
foo bar foo baz

This required unescaping escape sequences generated during last completion
in order to find the word to complete.

While there, also update the test to include cases for multiple matches.

Reviewed by christos


# 1.52 24-Mar-2019 abhinav

Only quote the completion matches if we are doing filename completion

If the user supplies a value for the attempted_completion_function parameter
then we cannot be sure if the completion is for filename or something else, in such
a case don't attempt to quote the completion matches.

Reviewed by christos

This should address PR lib/54067


Revision tags: pgoyette-compat-20190127 pgoyette-compat-20190118 pgoyette-compat-1226 pgoyette-compat-1126 pgoyette-compat-1020 pgoyette-compat-0930 pgoyette-compat-0906 pgoyette-compat-0728 phil-wifi-base pgoyette-compat-0625 pgoyette-compat-0521
# 1.51 04-May-2018 christos

branches: 1.51.2;
fix uninitialized


# 1.50 04-May-2018 abhinav

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


# 1.49 02-May-2018 abhinav

Add '*' and '[' to the list of characters which need escaping during autocompletion


Revision tags: pgoyette-compat-0502 pgoyette-compat-0422 pgoyette-compat-0415 pgoyette-compat-0407 pgoyette-compat-0330 pgoyette-compat-0322 pgoyette-compat-0315 pgoyette-compat-base
# 1.48 27-Oct-2017 abhinav

branches: 1.48.2;
Add NULL check after doing memory allocation at a couple of places

ok christos@


# 1.47 15-Oct-2017 abhinav

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.


# 1.46 16-Sep-2017 abhinav

Fix indentation (convert spaces to tab)


Revision tags: netbsd-8-1-RELEASE netbsd-8-1-RC1 netbsd-8-0-RELEASE netbsd-8-0-RC2 netbsd-8-0-RC1 matt-nb8-mediatek-base perseant-stdc-iso10646-base netbsd-8-base prg-localcount2-base3 prg-localcount2-base2 prg-localcount2-base1 prg-localcount2-base pgoyette-localcount-20170426 bouyer-socketcan-base1
# 1.45 21-Apr-2017 abhinav

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@


Revision tags: pgoyette-localcount-20170320 bouyer-socketcan-base pgoyette-localcount-20170107 pgoyette-localcount-20161104
# 1.44 31-Oct-2016 abhinav

branches: 1.44.2;
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.


Revision tags: localcount-20160914 pgoyette-localcount-20160806 pgoyette-localcount-20160726 pgoyette-localcount-base
# 1.43 11-Apr-2016 christos

branches: 1.43.2;
Char -> wchar_t from Ingo Schwarze.


# 1.42 11-Apr-2016 christos

more macro WIDECHAR undoing from Ingo Schwarze.


# 1.41 09-Apr-2016 christos

Change some 0's to NULL's from Pedro Giffuni


# 1.40 17-Feb-2016 christos

whitespace and header sorting changes (Ingo Schwarze). No functional changes.


# 1.39 16-Feb-2016 christos

More header cleanups from Ingo Schwarze.


# 1.38 16-Feb-2016 christos

more include file cleanup (Ingo Schwarze)


# 1.37 16-Feb-2016 christos

From Ingo Scharze:
Let "el.h" include everything needed for struct editline,
and don't include that stuff multiple times. That also improves
consistency, also avoids circular inclusions, and also makes it
easier to follow what is going on, even though not quite as nice.
But it seems like the best we can do...


# 1.36 16-Feb-2016 christos

cleanup chartype.h includes (Ingo Schwarze)


# 1.35 16-Feb-2016 christos

cleanup inclusion of histedit.h (Ingo Schwarze)


# 1.34 18-Oct-2014 riz

callers's -> caller's


# 1.33 18-Oct-2014 snj

src is too big these days to tolerate superfluous apostrophes. It's
"its", people!


Revision tags: netbsd-7-base tls-earlyentropy-base tls-maxphys-base
# 1.32 05-Jun-2014 christos

branches: 1.32.2;
PR/48876: Dmitriy Grigoryev: Core dump in readline lib on attempted expansion
Make sure we have 2 matches before calling strcmp().


Revision tags: netbsd-6-0-6-RELEASE netbsd-6-1-5-RELEASE yamt-pagecache-base9 yamt-pagecache-tag8 netbsd-6-1-4-RELEASE netbsd-6-0-5-RELEASE riastradh-xf86-video-intel-2-7-1-pre-2-21-15 riastradh-drm2-base3 netbsd-6-1-3-RELEASE netbsd-6-0-4-RELEASE netbsd-6-1-2-RELEASE netbsd-6-0-3-RELEASE netbsd-6-1-1-RELEASE riastradh-drm2-base2 riastradh-drm2-base1 riastradh-drm2-base netbsd-6-0-2-RELEASE netbsd-6-1-RELEASE netbsd-6-1-RC4 netbsd-6-1-RC3 agc-symver-base netbsd-6-1-RC2 netbsd-6-1-RC1 yamt-pagecache-base8 netbsd-6-0-1-RELEASE yamt-pagecache-base7 matt-nb6-plus-nbase yamt-pagecache-base6 netbsd-6-0-RELEASE netbsd-6-0-RC2 matt-nb6-plus-base netbsd-6-0-RC1 yamt-pagecache-base5 yamt-pagecache-base4 netbsd-6-base yamt-pagecache-base3 yamt-pagecache-base2 yamt-pagecache-base
# 1.31 16-Sep-2011 plunky

branches: 1.31.8; 1.31.18;
NULL does not need a cast, here


# 1.30 16-Aug-2011 christos

re-enable -Wconversion


# 1.29 29-Jul-2011 christos

pass -Wconversion


# 1.28 29-Jul-2011 christos

- fix unused params
- unconditionalize vis.h


# 1.27 29-Jul-2011 christos

KNF return (\1); -> return \1;


# 1.26 28-Jul-2011 christos

kill ptr_t and ioctl_t, add * sizeof(*foo) to all allocations.


# 1.25 28-Jul-2011 christos

fix unused variable warnings on systems without _r functions


# 1.24 28-Jul-2011 christos

eliminate alloca for portability
portable getpw{nam,uid}


Revision tags: cherry-xenmp-base bouyer-quota2-nbase bouyer-quota2-base matt-mips64-premerge-20101231
# 1.23 06-Dec-2010 dholland

Improve previous to avoid changing the interface of an externally
exposed function. (But note that this function is neither documented
nor declared in any installed header file, and it probably should not
be externally exposed.) Related to PR 44183, closes PR 44186.


# 1.22 02-Dec-2010 dholland

add const, from PR 44183.


# 1.21 02-Dec-2010 dholland

Fix up bodgy code for printing completion matches; it used to sometimes
skip entries, print (null), run off the end of the array, or occasionally
receive SIGSEGV, and now will, hopefully at least, do none of that.

Based in part on the patch in PR 44183 from Sergio Acereda; I also
did some tidyup and fixed it to print top-to-bottom first like ls(1).


# 1.20 15-Nov-2010 christos

don't increment i twice in the loop. From Michael Byrnes


# 1.19 01-Jun-2010 christos

tidy up memory allocation and don't unnecessarily print "./" before names.


# 1.18 18-Jan-2010 christos

PR/42637: Joachim Kuebart: Shell tab completion crashes due to libedit stack
smashing


# 1.17 30-Dec-2009 christos

Wide character support (UTF-8) from Johny Mattsson; currently disabled.


# 1.16 28-Dec-2009 christos

improve on the listing display by printing only one character after the
filename not two, and no trailing blanks. I will revisit this when I write
the ls-F code.


Revision tags: matt-premerge-20091211 jym-xensuspend-nbase jym-xensuspend-base
# 1.15 16-Feb-2009 christos

fix sign compare issues.


# 1.14 15-Feb-2009 christos

pass lint on _LP64.


# 1.13 26-Jan-2009 apb

branches: 1.13.2;
Define HAVE_STRUCT_DIRENT_D_NAMLEN in config,h, and test it when
deciding whether to use entry->d_namlen or strlen(entry->d_name).
Addresses PR 40477 by Robert Millan.


# 1.12 11-Jan-2009 christos

- insert a space after the recognized string if it was an exact match
- initialize properly the string used for completion.
From Alex Bligh alex at alex dot org dot uk
- Make char constants consistent


Revision tags: netbsd-5-2-3-RELEASE netbsd-5-1-5-RELEASE netbsd-5-2-2-RELEASE netbsd-5-1-4-RELEASE netbsd-5-2-1-RELEASE netbsd-5-1-3-RELEASE netbsd-5-2-RELEASE netbsd-5-2-RC1 netbsd-5-1-2-RELEASE netbsd-5-1-1-RELEASE matt-nb5-mips64-premerge-20101231 matt-nb5-pq3-base netbsd-5-1-RELEASE netbsd-5-1-RC4 matt-nb5-mips64-k15 netbsd-5-1-RC3 netbsd-5-1-RC2 netbsd-5-1-RC1 netbsd-5-0-2-RELEASE matt-nb5-mips64-premerge-20091211 matt-nb5-mips64-u2-k2-k4-k7-k8-k9 matt-nb4-mips64-k7-u2a-k9b matt-nb5-mips64-u1-k1-k5 netbsd-5-0-1-RELEASE netbsd-5-0-RELEASE netbsd-5-0-RC4 netbsd-5-0-RC3 netbsd-5-0-RC2 netbsd-5-0-RC1 netbsd-5-base matt-mips64-base2 wrstuden-revivesa-base-3 wrstuden-revivesa-base-2 wrstuden-revivesa-base-1 yamt-pf42-base4 yamt-pf42-base3 hpcarm-cleanup-nbase yamt-pf42-base2 wrstuden-revivesa-base
# 1.11 29-Apr-2008 martin

Convert to new 2 clause license


Revision tags: netbsd-4-0-1-RELEASE wrstuden-fixsa-newbase yamt-pf42-baseX yamt-pf42-base keiichi-mipv6-base matt-armv6-nbase matt-armv6-prevmlocking wrstuden-fixsa-base-1 netbsd-4-0-RELEASE cube-autoconf-base netbsd-4-0-RC5 netbsd-4-0-RC4 netbsd-4-0-RC3 netbsd-4-0-RC2 netbsd-4-0-RC1 matt-armv6-base matt-mips64-base hpcarm-cleanup-base wrstuden-fixsa-base netbsd-4-base
# 1.10 09-Nov-2006 christos

branches: 1.10.16;
don't use alloca with ssp.


# 1.9 21-Aug-2006 christos

Change to a 3 clause copyright after permission of the holders.


Revision tags: abandoned-netbsd-4-base
# 1.8 03-Aug-2005 christos

Fix reversed test; from Gerry Swislow


# 1.7 11-Jun-2005 christos

PR/30500: Paul Shupak: Inconsistent definition of tilde_expand().
Provide a layer of indirection between the readline compatibility functions
and our internal implementation, so that we have the freedom to change the
function signature.


# 1.6 10-Jun-2005 christos

Bug reported from Martin Dietze:

The place to change the completion_append_character is
usually somewhere in the `rl_completion_entry_function'
callback which is where one usually can distinguish between
file- or dir-like entries to append a slash for dirs etc.

This does no longer work since `fn_complete()' takes the
`append_character' as argument before the callback is executed,
so that changes to the variable `rl_completion_append_character'
have in fact no effect for the current completion.

Fix by adding a function that returns the rl_completion_append_character,
when it gets passed in a filename in readline emulation.


# 1.5 18-May-2005 christos

Make completion_matches non-static since readline wants it.


# 1.4 12-May-2005 christos

PR/30215: Kouichirou Hiratsuka: /bin/sh dumps core with tabcomplete
Don't core-dump when trying to complete an empty line; instead assume ./


# 1.3 09-May-2005 dsl

Use getpwuid_r(getuid(), ...) to expand ~/....
Don't replace ~xyz with /home/xyz when expanding ~


# 1.2 07-May-2005 dsl

gdb directly calls filename_completion_function()


# 1.1 07-May-2005 dsl

Separate out the filename completion functions from the readline() code.
Pass in loads of parameters instead of relying on shed-loads of global
variables to modify the behaviour.
The filename completion code can now be enabled by code that uses el_gets().
(eg /bin/sh)


# 1.61 09-Oct-2019 christos

add +1 to strlcpy's (Patrick Welche)


# 1.60 08-Oct-2019 christos

remore error(1) comment


# 1.59 08-Oct-2019 christos

Change strncpy to either memcpy (when we know the len), or strlcpy (when
we used to NUL terminate explicitly.


# 1.58 08-Sep-2019 abhinav

PR lib/54510: Fix file completion inside quotes which broke in rev 1.53

While there also fix handling character appending in the file completions when
inside quotes. For example when inside a quote, if the completion is a directory then
append a '/' but don't close the quote. On the other hand when inside a quote if the
completion is a file name and it is the only match then we can close the quote.


Revision tags: netbsd-9-base
# 1.57 28-Jul-2019 christos

PR/54415: Ricky Zhou: libedit stats completions for non-file completions
Use the proper completion function and account for the character appended
by the function when computing the number of columns.


# 1.56 23-Jul-2019 christos

PR/54399: S�ren Tempel: Uninitialized memory access in libedit history.
Initialize the buffer using calloc. While here change all malloc(a * sizeof(b))
to calloc(a, sizeof(b)). XXX: should fix realloc similarly.


Revision tags: phil-wifi-20190609
# 1.55 20-Apr-2019 abhinav

PR lib/54131 - declare the loop variable outside the for loop


# 1.54 12-Apr-2019 christos

PR/54117: Paavo Helde: Fix memory overrun: Account for the closing quote
in memory allocation if quoted.


# 1.53 31-Mar-2019 abhinav

Perform quoting of filename completions when there are multiple matches as well

Quoting of special characters in filename completion was implemented for single match
case, this enables it for multiple matches as well. For example:

$ touch 'foo bar'
$ touch 'foo baz'
$ ls fo<TAB>
autocompletes to =>
$ ls foo\ ba
hitting <TAB> again shows:
foo bar foo baz

This required unescaping escape sequences generated during last completion
in order to find the word to complete.

While there, also update the test to include cases for multiple matches.

Reviewed by christos


# 1.52 24-Mar-2019 abhinav

Only quote the completion matches if we are doing filename completion

If the user supplies a value for the attempted_completion_function parameter
then we cannot be sure if the completion is for filename or something else, in such
a case don't attempt to quote the completion matches.

Reviewed by christos

This should address PR lib/54067


Revision tags: pgoyette-compat-20190127 pgoyette-compat-20190118 pgoyette-compat-1226 pgoyette-compat-1126 pgoyette-compat-1020 pgoyette-compat-0930 pgoyette-compat-0906 pgoyette-compat-0728 phil-wifi-base pgoyette-compat-0625 pgoyette-compat-0521
# 1.51 04-May-2018 christos

branches: 1.51.2;
fix uninitialized


# 1.50 04-May-2018 abhinav

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


# 1.49 02-May-2018 abhinav

Add '*' and '[' to the list of characters which need escaping during autocompletion


Revision tags: pgoyette-compat-0502 pgoyette-compat-0422 pgoyette-compat-0415 pgoyette-compat-0407 pgoyette-compat-0330 pgoyette-compat-0322 pgoyette-compat-0315 pgoyette-compat-base
# 1.48 27-Oct-2017 abhinav

branches: 1.48.2;
Add NULL check after doing memory allocation at a couple of places

ok christos@


# 1.47 15-Oct-2017 abhinav

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.


# 1.46 16-Sep-2017 abhinav

Fix indentation (convert spaces to tab)


Revision tags: netbsd-8-1-RELEASE netbsd-8-1-RC1 netbsd-8-0-RELEASE netbsd-8-0-RC2 netbsd-8-0-RC1 matt-nb8-mediatek-base perseant-stdc-iso10646-base netbsd-8-base prg-localcount2-base3 prg-localcount2-base2 prg-localcount2-base1 prg-localcount2-base pgoyette-localcount-20170426 bouyer-socketcan-base1
# 1.45 21-Apr-2017 abhinav

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@


Revision tags: pgoyette-localcount-20170320 bouyer-socketcan-base pgoyette-localcount-20170107 pgoyette-localcount-20161104
# 1.44 31-Oct-2016 abhinav

branches: 1.44.2;
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.


Revision tags: localcount-20160914 pgoyette-localcount-20160806 pgoyette-localcount-20160726 pgoyette-localcount-base
# 1.43 11-Apr-2016 christos

branches: 1.43.2;
Char -> wchar_t from Ingo Schwarze.


# 1.42 11-Apr-2016 christos

more macro WIDECHAR undoing from Ingo Schwarze.


# 1.41 09-Apr-2016 christos

Change some 0's to NULL's from Pedro Giffuni


# 1.40 17-Feb-2016 christos

whitespace and header sorting changes (Ingo Schwarze). No functional changes.


# 1.39 16-Feb-2016 christos

More header cleanups from Ingo Schwarze.


# 1.38 16-Feb-2016 christos

more include file cleanup (Ingo Schwarze)


# 1.37 16-Feb-2016 christos

From Ingo Scharze:
Let "el.h" include everything needed for struct editline,
and don't include that stuff multiple times. That also improves
consistency, also avoids circular inclusions, and also makes it
easier to follow what is going on, even though not quite as nice.
But it seems like the best we can do...


# 1.36 16-Feb-2016 christos

cleanup chartype.h includes (Ingo Schwarze)


# 1.35 16-Feb-2016 christos

cleanup inclusion of histedit.h (Ingo Schwarze)


# 1.34 18-Oct-2014 riz

callers's -> caller's


# 1.33 18-Oct-2014 snj

src is too big these days to tolerate superfluous apostrophes. It's
"its", people!


Revision tags: netbsd-7-base tls-earlyentropy-base tls-maxphys-base
# 1.32 05-Jun-2014 christos

branches: 1.32.2;
PR/48876: Dmitriy Grigoryev: Core dump in readline lib on attempted expansion
Make sure we have 2 matches before calling strcmp().


Revision tags: netbsd-6-0-6-RELEASE netbsd-6-1-5-RELEASE yamt-pagecache-base9 yamt-pagecache-tag8 netbsd-6-1-4-RELEASE netbsd-6-0-5-RELEASE riastradh-xf86-video-intel-2-7-1-pre-2-21-15 riastradh-drm2-base3 netbsd-6-1-3-RELEASE netbsd-6-0-4-RELEASE netbsd-6-1-2-RELEASE netbsd-6-0-3-RELEASE netbsd-6-1-1-RELEASE riastradh-drm2-base2 riastradh-drm2-base1 riastradh-drm2-base netbsd-6-0-2-RELEASE netbsd-6-1-RELEASE netbsd-6-1-RC4 netbsd-6-1-RC3 agc-symver-base netbsd-6-1-RC2 netbsd-6-1-RC1 yamt-pagecache-base8 netbsd-6-0-1-RELEASE yamt-pagecache-base7 matt-nb6-plus-nbase yamt-pagecache-base6 netbsd-6-0-RELEASE netbsd-6-0-RC2 matt-nb6-plus-base netbsd-6-0-RC1 yamt-pagecache-base5 yamt-pagecache-base4 netbsd-6-base yamt-pagecache-base3 yamt-pagecache-base2 yamt-pagecache-base
# 1.31 16-Sep-2011 plunky

branches: 1.31.8; 1.31.18;
NULL does not need a cast, here


# 1.30 16-Aug-2011 christos

re-enable -Wconversion


# 1.29 29-Jul-2011 christos

pass -Wconversion


# 1.28 29-Jul-2011 christos

- fix unused params
- unconditionalize vis.h


# 1.27 29-Jul-2011 christos

KNF return (\1); -> return \1;


# 1.26 28-Jul-2011 christos

kill ptr_t and ioctl_t, add * sizeof(*foo) to all allocations.


# 1.25 28-Jul-2011 christos

fix unused variable warnings on systems without _r functions


# 1.24 28-Jul-2011 christos

eliminate alloca for portability
portable getpw{nam,uid}


Revision tags: cherry-xenmp-base bouyer-quota2-nbase bouyer-quota2-base matt-mips64-premerge-20101231
# 1.23 06-Dec-2010 dholland

Improve previous to avoid changing the interface of an externally
exposed function. (But note that this function is neither documented
nor declared in any installed header file, and it probably should not
be externally exposed.) Related to PR 44183, closes PR 44186.


# 1.22 02-Dec-2010 dholland

add const, from PR 44183.


# 1.21 02-Dec-2010 dholland

Fix up bodgy code for printing completion matches; it used to sometimes
skip entries, print (null), run off the end of the array, or occasionally
receive SIGSEGV, and now will, hopefully at least, do none of that.

Based in part on the patch in PR 44183 from Sergio Acereda; I also
did some tidyup and fixed it to print top-to-bottom first like ls(1).


# 1.20 15-Nov-2010 christos

don't increment i twice in the loop. From Michael Byrnes


# 1.19 01-Jun-2010 christos

tidy up memory allocation and don't unnecessarily print "./" before names.


# 1.18 18-Jan-2010 christos

PR/42637: Joachim Kuebart: Shell tab completion crashes due to libedit stack
smashing


# 1.17 30-Dec-2009 christos

Wide character support (UTF-8) from Johny Mattsson; currently disabled.


# 1.16 28-Dec-2009 christos

improve on the listing display by printing only one character after the
filename not two, and no trailing blanks. I will revisit this when I write
the ls-F code.


Revision tags: matt-premerge-20091211 jym-xensuspend-nbase jym-xensuspend-base
# 1.15 16-Feb-2009 christos

fix sign compare issues.


# 1.14 15-Feb-2009 christos

pass lint on _LP64.


# 1.13 26-Jan-2009 apb

branches: 1.13.2;
Define HAVE_STRUCT_DIRENT_D_NAMLEN in config,h, and test it when
deciding whether to use entry->d_namlen or strlen(entry->d_name).
Addresses PR 40477 by Robert Millan.


# 1.12 11-Jan-2009 christos

- insert a space after the recognized string if it was an exact match
- initialize properly the string used for completion.
From Alex Bligh alex at alex dot org dot uk
- Make char constants consistent


Revision tags: netbsd-5-2-3-RELEASE netbsd-5-1-5-RELEASE netbsd-5-2-2-RELEASE netbsd-5-1-4-RELEASE netbsd-5-2-1-RELEASE netbsd-5-1-3-RELEASE netbsd-5-2-RELEASE netbsd-5-2-RC1 netbsd-5-1-2-RELEASE netbsd-5-1-1-RELEASE matt-nb5-mips64-premerge-20101231 matt-nb5-pq3-base netbsd-5-1-RELEASE netbsd-5-1-RC4 matt-nb5-mips64-k15 netbsd-5-1-RC3 netbsd-5-1-RC2 netbsd-5-1-RC1 netbsd-5-0-2-RELEASE matt-nb5-mips64-premerge-20091211 matt-nb5-mips64-u2-k2-k4-k7-k8-k9 matt-nb4-mips64-k7-u2a-k9b matt-nb5-mips64-u1-k1-k5 netbsd-5-0-1-RELEASE netbsd-5-0-RELEASE netbsd-5-0-RC4 netbsd-5-0-RC3 netbsd-5-0-RC2 netbsd-5-0-RC1 netbsd-5-base matt-mips64-base2 wrstuden-revivesa-base-3 wrstuden-revivesa-base-2 wrstuden-revivesa-base-1 yamt-pf42-base4 yamt-pf42-base3 hpcarm-cleanup-nbase yamt-pf42-base2 wrstuden-revivesa-base
# 1.11 29-Apr-2008 martin

Convert to new 2 clause license


Revision tags: netbsd-4-0-1-RELEASE wrstuden-fixsa-newbase yamt-pf42-baseX yamt-pf42-base keiichi-mipv6-base matt-armv6-nbase matt-armv6-prevmlocking wrstuden-fixsa-base-1 netbsd-4-0-RELEASE cube-autoconf-base netbsd-4-0-RC5 netbsd-4-0-RC4 netbsd-4-0-RC3 netbsd-4-0-RC2 netbsd-4-0-RC1 matt-armv6-base matt-mips64-base hpcarm-cleanup-base wrstuden-fixsa-base netbsd-4-base
# 1.10 09-Nov-2006 christos

branches: 1.10.16;
don't use alloca with ssp.


# 1.9 21-Aug-2006 christos

Change to a 3 clause copyright after permission of the holders.


Revision tags: abandoned-netbsd-4-base
# 1.8 03-Aug-2005 christos

Fix reversed test; from Gerry Swislow


# 1.7 11-Jun-2005 christos

PR/30500: Paul Shupak: Inconsistent definition of tilde_expand().
Provide a layer of indirection between the readline compatibility functions
and our internal implementation, so that we have the freedom to change the
function signature.


# 1.6 10-Jun-2005 christos

Bug reported from Martin Dietze:

The place to change the completion_append_character is
usually somewhere in the `rl_completion_entry_function'
callback which is where one usually can distinguish between
file- or dir-like entries to append a slash for dirs etc.

This does no longer work since `fn_complete()' takes the
`append_character' as argument before the callback is executed,
so that changes to the variable `rl_completion_append_character'
have in fact no effect for the current completion.

Fix by adding a function that returns the rl_completion_append_character,
when it gets passed in a filename in readline emulation.


# 1.5 18-May-2005 christos

Make completion_matches non-static since readline wants it.


# 1.4 12-May-2005 christos

PR/30215: Kouichirou Hiratsuka: /bin/sh dumps core with tabcomplete
Don't core-dump when trying to complete an empty line; instead assume ./


# 1.3 09-May-2005 dsl

Use getpwuid_r(getuid(), ...) to expand ~/....
Don't replace ~xyz with /home/xyz when expanding ~


# 1.2 07-May-2005 dsl

gdb directly calls filename_completion_function()


# 1.1 07-May-2005 dsl

Separate out the filename completion functions from the readline() code.
Pass in loads of parameters instead of relying on shed-loads of global
variables to modify the behaviour.
The filename completion code can now be enabled by code that uses el_gets().
(eg /bin/sh)


# 1.60 08-Oct-2019 christos

remore error(1) comment


# 1.59 08-Oct-2019 christos

Change strncpy to either memcpy (when we know the len), or strlcpy (when
we used to NUL terminate explicitly.


# 1.58 08-Sep-2019 abhinav

PR lib/54510: Fix file completion inside quotes which broke in rev 1.53

While there also fix handling character appending in the file completions when
inside quotes. For example when inside a quote, if the completion is a directory then
append a '/' but don't close the quote. On the other hand when inside a quote if the
completion is a file name and it is the only match then we can close the quote.


Revision tags: netbsd-9-base
# 1.57 28-Jul-2019 christos

PR/54415: Ricky Zhou: libedit stats completions for non-file completions
Use the proper completion function and account for the character appended
by the function when computing the number of columns.


# 1.56 23-Jul-2019 christos

PR/54399: S�ren Tempel: Uninitialized memory access in libedit history.
Initialize the buffer using calloc. While here change all malloc(a * sizeof(b))
to calloc(a, sizeof(b)). XXX: should fix realloc similarly.


Revision tags: phil-wifi-20190609
# 1.55 20-Apr-2019 abhinav

PR lib/54131 - declare the loop variable outside the for loop


# 1.54 12-Apr-2019 christos

PR/54117: Paavo Helde: Fix memory overrun: Account for the closing quote
in memory allocation if quoted.


# 1.53 31-Mar-2019 abhinav

Perform quoting of filename completions when there are multiple matches as well

Quoting of special characters in filename completion was implemented for single match
case, this enables it for multiple matches as well. For example:

$ touch 'foo bar'
$ touch 'foo baz'
$ ls fo<TAB>
autocompletes to =>
$ ls foo\ ba
hitting <TAB> again shows:
foo bar foo baz

This required unescaping escape sequences generated during last completion
in order to find the word to complete.

While there, also update the test to include cases for multiple matches.

Reviewed by christos


# 1.52 24-Mar-2019 abhinav

Only quote the completion matches if we are doing filename completion

If the user supplies a value for the attempted_completion_function parameter
then we cannot be sure if the completion is for filename or something else, in such
a case don't attempt to quote the completion matches.

Reviewed by christos

This should address PR lib/54067


Revision tags: pgoyette-compat-20190127 pgoyette-compat-20190118 pgoyette-compat-1226 pgoyette-compat-1126 pgoyette-compat-1020 pgoyette-compat-0930 pgoyette-compat-0906 pgoyette-compat-0728 phil-wifi-base pgoyette-compat-0625 pgoyette-compat-0521
# 1.51 04-May-2018 christos

branches: 1.51.2;
fix uninitialized


# 1.50 04-May-2018 abhinav

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


# 1.49 02-May-2018 abhinav

Add '*' and '[' to the list of characters which need escaping during autocompletion


Revision tags: pgoyette-compat-0502 pgoyette-compat-0422 pgoyette-compat-0415 pgoyette-compat-0407 pgoyette-compat-0330 pgoyette-compat-0322 pgoyette-compat-0315 pgoyette-compat-base
# 1.48 27-Oct-2017 abhinav

branches: 1.48.2;
Add NULL check after doing memory allocation at a couple of places

ok christos@


# 1.47 15-Oct-2017 abhinav

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.


# 1.46 16-Sep-2017 abhinav

Fix indentation (convert spaces to tab)


Revision tags: netbsd-8-1-RELEASE netbsd-8-1-RC1 netbsd-8-0-RELEASE netbsd-8-0-RC2 netbsd-8-0-RC1 matt-nb8-mediatek-base perseant-stdc-iso10646-base netbsd-8-base prg-localcount2-base3 prg-localcount2-base2 prg-localcount2-base1 prg-localcount2-base pgoyette-localcount-20170426 bouyer-socketcan-base1
# 1.45 21-Apr-2017 abhinav

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@


Revision tags: pgoyette-localcount-20170320 bouyer-socketcan-base pgoyette-localcount-20170107 pgoyette-localcount-20161104
# 1.44 31-Oct-2016 abhinav

branches: 1.44.2;
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.


Revision tags: localcount-20160914 pgoyette-localcount-20160806 pgoyette-localcount-20160726 pgoyette-localcount-base
# 1.43 11-Apr-2016 christos

branches: 1.43.2;
Char -> wchar_t from Ingo Schwarze.


# 1.42 11-Apr-2016 christos

more macro WIDECHAR undoing from Ingo Schwarze.


# 1.41 09-Apr-2016 christos

Change some 0's to NULL's from Pedro Giffuni


# 1.40 17-Feb-2016 christos

whitespace and header sorting changes (Ingo Schwarze). No functional changes.


# 1.39 16-Feb-2016 christos

More header cleanups from Ingo Schwarze.


# 1.38 16-Feb-2016 christos

more include file cleanup (Ingo Schwarze)


# 1.37 16-Feb-2016 christos

From Ingo Scharze:
Let "el.h" include everything needed for struct editline,
and don't include that stuff multiple times. That also improves
consistency, also avoids circular inclusions, and also makes it
easier to follow what is going on, even though not quite as nice.
But it seems like the best we can do...


# 1.36 16-Feb-2016 christos

cleanup chartype.h includes (Ingo Schwarze)


# 1.35 16-Feb-2016 christos

cleanup inclusion of histedit.h (Ingo Schwarze)


# 1.34 18-Oct-2014 riz

callers's -> caller's


# 1.33 18-Oct-2014 snj

src is too big these days to tolerate superfluous apostrophes. It's
"its", people!


Revision tags: netbsd-7-base tls-earlyentropy-base tls-maxphys-base
# 1.32 05-Jun-2014 christos

branches: 1.32.2;
PR/48876: Dmitriy Grigoryev: Core dump in readline lib on attempted expansion
Make sure we have 2 matches before calling strcmp().


Revision tags: netbsd-6-0-6-RELEASE netbsd-6-1-5-RELEASE yamt-pagecache-base9 yamt-pagecache-tag8 netbsd-6-1-4-RELEASE netbsd-6-0-5-RELEASE riastradh-xf86-video-intel-2-7-1-pre-2-21-15 riastradh-drm2-base3 netbsd-6-1-3-RELEASE netbsd-6-0-4-RELEASE netbsd-6-1-2-RELEASE netbsd-6-0-3-RELEASE netbsd-6-1-1-RELEASE riastradh-drm2-base2 riastradh-drm2-base1 riastradh-drm2-base netbsd-6-0-2-RELEASE netbsd-6-1-RELEASE netbsd-6-1-RC4 netbsd-6-1-RC3 agc-symver-base netbsd-6-1-RC2 netbsd-6-1-RC1 yamt-pagecache-base8 netbsd-6-0-1-RELEASE yamt-pagecache-base7 matt-nb6-plus-nbase yamt-pagecache-base6 netbsd-6-0-RELEASE netbsd-6-0-RC2 matt-nb6-plus-base netbsd-6-0-RC1 yamt-pagecache-base5 yamt-pagecache-base4 netbsd-6-base yamt-pagecache-base3 yamt-pagecache-base2 yamt-pagecache-base
# 1.31 16-Sep-2011 plunky

branches: 1.31.8; 1.31.18;
NULL does not need a cast, here


# 1.30 16-Aug-2011 christos

re-enable -Wconversion


# 1.29 29-Jul-2011 christos

pass -Wconversion


# 1.28 29-Jul-2011 christos

- fix unused params
- unconditionalize vis.h


# 1.27 29-Jul-2011 christos

KNF return (\1); -> return \1;


# 1.26 28-Jul-2011 christos

kill ptr_t and ioctl_t, add * sizeof(*foo) to all allocations.


# 1.25 28-Jul-2011 christos

fix unused variable warnings on systems without _r functions


# 1.24 28-Jul-2011 christos

eliminate alloca for portability
portable getpw{nam,uid}


Revision tags: cherry-xenmp-base bouyer-quota2-nbase bouyer-quota2-base matt-mips64-premerge-20101231
# 1.23 06-Dec-2010 dholland

Improve previous to avoid changing the interface of an externally
exposed function. (But note that this function is neither documented
nor declared in any installed header file, and it probably should not
be externally exposed.) Related to PR 44183, closes PR 44186.


# 1.22 02-Dec-2010 dholland

add const, from PR 44183.


# 1.21 02-Dec-2010 dholland

Fix up bodgy code for printing completion matches; it used to sometimes
skip entries, print (null), run off the end of the array, or occasionally
receive SIGSEGV, and now will, hopefully at least, do none of that.

Based in part on the patch in PR 44183 from Sergio Acereda; I also
did some tidyup and fixed it to print top-to-bottom first like ls(1).


# 1.20 15-Nov-2010 christos

don't increment i twice in the loop. From Michael Byrnes


# 1.19 01-Jun-2010 christos

tidy up memory allocation and don't unnecessarily print "./" before names.


# 1.18 18-Jan-2010 christos

PR/42637: Joachim Kuebart: Shell tab completion crashes due to libedit stack
smashing


# 1.17 30-Dec-2009 christos

Wide character support (UTF-8) from Johny Mattsson; currently disabled.


# 1.16 28-Dec-2009 christos

improve on the listing display by printing only one character after the
filename not two, and no trailing blanks. I will revisit this when I write
the ls-F code.


Revision tags: matt-premerge-20091211 jym-xensuspend-nbase jym-xensuspend-base
# 1.15 16-Feb-2009 christos

fix sign compare issues.


# 1.14 15-Feb-2009 christos

pass lint on _LP64.


# 1.13 26-Jan-2009 apb

branches: 1.13.2;
Define HAVE_STRUCT_DIRENT_D_NAMLEN in config,h, and test it when
deciding whether to use entry->d_namlen or strlen(entry->d_name).
Addresses PR 40477 by Robert Millan.


# 1.12 11-Jan-2009 christos

- insert a space after the recognized string if it was an exact match
- initialize properly the string used for completion.
From Alex Bligh alex at alex dot org dot uk
- Make char constants consistent


Revision tags: netbsd-5-2-3-RELEASE netbsd-5-1-5-RELEASE netbsd-5-2-2-RELEASE netbsd-5-1-4-RELEASE netbsd-5-2-1-RELEASE netbsd-5-1-3-RELEASE netbsd-5-2-RELEASE netbsd-5-2-RC1 netbsd-5-1-2-RELEASE netbsd-5-1-1-RELEASE matt-nb5-mips64-premerge-20101231 matt-nb5-pq3-base netbsd-5-1-RELEASE netbsd-5-1-RC4 matt-nb5-mips64-k15 netbsd-5-1-RC3 netbsd-5-1-RC2 netbsd-5-1-RC1 netbsd-5-0-2-RELEASE matt-nb5-mips64-premerge-20091211 matt-nb5-mips64-u2-k2-k4-k7-k8-k9 matt-nb4-mips64-k7-u2a-k9b matt-nb5-mips64-u1-k1-k5 netbsd-5-0-1-RELEASE netbsd-5-0-RELEASE netbsd-5-0-RC4 netbsd-5-0-RC3 netbsd-5-0-RC2 netbsd-5-0-RC1 netbsd-5-base matt-mips64-base2 wrstuden-revivesa-base-3 wrstuden-revivesa-base-2 wrstuden-revivesa-base-1 yamt-pf42-base4 yamt-pf42-base3 hpcarm-cleanup-nbase yamt-pf42-base2 wrstuden-revivesa-base
# 1.11 29-Apr-2008 martin

Convert to new 2 clause license


Revision tags: netbsd-4-0-1-RELEASE wrstuden-fixsa-newbase yamt-pf42-baseX yamt-pf42-base keiichi-mipv6-base matt-armv6-nbase matt-armv6-prevmlocking wrstuden-fixsa-base-1 netbsd-4-0-RELEASE cube-autoconf-base netbsd-4-0-RC5 netbsd-4-0-RC4 netbsd-4-0-RC3 netbsd-4-0-RC2 netbsd-4-0-RC1 matt-armv6-base matt-mips64-base hpcarm-cleanup-base wrstuden-fixsa-base netbsd-4-base
# 1.10 09-Nov-2006 christos

branches: 1.10.16;
don't use alloca with ssp.


# 1.9 21-Aug-2006 christos

Change to a 3 clause copyright after permission of the holders.


Revision tags: abandoned-netbsd-4-base
# 1.8 03-Aug-2005 christos

Fix reversed test; from Gerry Swislow


# 1.7 11-Jun-2005 christos

PR/30500: Paul Shupak: Inconsistent definition of tilde_expand().
Provide a layer of indirection between the readline compatibility functions
and our internal implementation, so that we have the freedom to change the
function signature.


# 1.6 10-Jun-2005 christos

Bug reported from Martin Dietze:

The place to change the completion_append_character is
usually somewhere in the `rl_completion_entry_function'
callback which is where one usually can distinguish between
file- or dir-like entries to append a slash for dirs etc.

This does no longer work since `fn_complete()' takes the
`append_character' as argument before the callback is executed,
so that changes to the variable `rl_completion_append_character'
have in fact no effect for the current completion.

Fix by adding a function that returns the rl_completion_append_character,
when it gets passed in a filename in readline emulation.


# 1.5 18-May-2005 christos

Make completion_matches non-static since readline wants it.


# 1.4 12-May-2005 christos

PR/30215: Kouichirou Hiratsuka: /bin/sh dumps core with tabcomplete
Don't core-dump when trying to complete an empty line; instead assume ./


# 1.3 09-May-2005 dsl

Use getpwuid_r(getuid(), ...) to expand ~/....
Don't replace ~xyz with /home/xyz when expanding ~


# 1.2 07-May-2005 dsl

gdb directly calls filename_completion_function()


# 1.1 07-May-2005 dsl

Separate out the filename completion functions from the readline() code.
Pass in loads of parameters instead of relying on shed-loads of global
variables to modify the behaviour.
The filename completion code can now be enabled by code that uses el_gets().
(eg /bin/sh)


# 1.58 08-Sep-2019 abhinav

PR lib/54510: Fix file completion inside quotes which broke in rev 1.53

While there also fix handling character appending in the file completions when
inside quotes. For example when inside a quote, if the completion is a directory then
append a '/' but don't close the quote. On the other hand when inside a quote if the
completion is a file name and it is the only match then we can close the quote.


Revision tags: netbsd-9-base
# 1.57 28-Jul-2019 christos

PR/54415: Ricky Zhou: libedit stats completions for non-file completions
Use the proper completion function and account for the character appended
by the function when computing the number of columns.


# 1.56 23-Jul-2019 christos

PR/54399: S�ren Tempel: Uninitialized memory access in libedit history.
Initialize the buffer using calloc. While here change all malloc(a * sizeof(b))
to calloc(a, sizeof(b)). XXX: should fix realloc similarly.


Revision tags: phil-wifi-20190609
# 1.55 20-Apr-2019 abhinav

PR lib/54131 - declare the loop variable outside the for loop


# 1.54 12-Apr-2019 christos

PR/54117: Paavo Helde: Fix memory overrun: Account for the closing quote
in memory allocation if quoted.


# 1.53 31-Mar-2019 abhinav

Perform quoting of filename completions when there are multiple matches as well

Quoting of special characters in filename completion was implemented for single match
case, this enables it for multiple matches as well. For example:

$ touch 'foo bar'
$ touch 'foo baz'
$ ls fo<TAB>
autocompletes to =>
$ ls foo\ ba
hitting <TAB> again shows:
foo bar foo baz

This required unescaping escape sequences generated during last completion
in order to find the word to complete.

While there, also update the test to include cases for multiple matches.

Reviewed by christos


# 1.52 24-Mar-2019 abhinav

Only quote the completion matches if we are doing filename completion

If the user supplies a value for the attempted_completion_function parameter
then we cannot be sure if the completion is for filename or something else, in such
a case don't attempt to quote the completion matches.

Reviewed by christos

This should address PR lib/54067


Revision tags: pgoyette-compat-20190127 pgoyette-compat-20190118 pgoyette-compat-1226 pgoyette-compat-1126 pgoyette-compat-1020 pgoyette-compat-0930 pgoyette-compat-0906 pgoyette-compat-0728 phil-wifi-base pgoyette-compat-0625 pgoyette-compat-0521
# 1.51 04-May-2018 christos

branches: 1.51.2;
fix uninitialized


# 1.50 04-May-2018 abhinav

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


# 1.49 02-May-2018 abhinav

Add '*' and '[' to the list of characters which need escaping during autocompletion


Revision tags: pgoyette-compat-0502 pgoyette-compat-0422 pgoyette-compat-0415 pgoyette-compat-0407 pgoyette-compat-0330 pgoyette-compat-0322 pgoyette-compat-0315 pgoyette-compat-base
# 1.48 27-Oct-2017 abhinav

branches: 1.48.2;
Add NULL check after doing memory allocation at a couple of places

ok christos@


# 1.47 15-Oct-2017 abhinav

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.


# 1.46 16-Sep-2017 abhinav

Fix indentation (convert spaces to tab)


Revision tags: netbsd-8-1-RELEASE netbsd-8-1-RC1 netbsd-8-0-RELEASE netbsd-8-0-RC2 netbsd-8-0-RC1 matt-nb8-mediatek-base perseant-stdc-iso10646-base netbsd-8-base prg-localcount2-base3 prg-localcount2-base2 prg-localcount2-base1 prg-localcount2-base pgoyette-localcount-20170426 bouyer-socketcan-base1
# 1.45 21-Apr-2017 abhinav

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@


Revision tags: pgoyette-localcount-20170320 bouyer-socketcan-base pgoyette-localcount-20170107 pgoyette-localcount-20161104
# 1.44 31-Oct-2016 abhinav

branches: 1.44.2;
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.


Revision tags: localcount-20160914 pgoyette-localcount-20160806 pgoyette-localcount-20160726 pgoyette-localcount-base
# 1.43 11-Apr-2016 christos

branches: 1.43.2;
Char -> wchar_t from Ingo Schwarze.


# 1.42 11-Apr-2016 christos

more macro WIDECHAR undoing from Ingo Schwarze.


# 1.41 09-Apr-2016 christos

Change some 0's to NULL's from Pedro Giffuni


# 1.40 17-Feb-2016 christos

whitespace and header sorting changes (Ingo Schwarze). No functional changes.


# 1.39 16-Feb-2016 christos

More header cleanups from Ingo Schwarze.


# 1.38 16-Feb-2016 christos

more include file cleanup (Ingo Schwarze)


# 1.37 16-Feb-2016 christos

From Ingo Scharze:
Let "el.h" include everything needed for struct editline,
and don't include that stuff multiple times. That also improves
consistency, also avoids circular inclusions, and also makes it
easier to follow what is going on, even though not quite as nice.
But it seems like the best we can do...


# 1.36 16-Feb-2016 christos

cleanup chartype.h includes (Ingo Schwarze)


# 1.35 16-Feb-2016 christos

cleanup inclusion of histedit.h (Ingo Schwarze)


# 1.34 18-Oct-2014 riz

callers's -> caller's


# 1.33 18-Oct-2014 snj

src is too big these days to tolerate superfluous apostrophes. It's
"its", people!


Revision tags: netbsd-7-base tls-earlyentropy-base tls-maxphys-base
# 1.32 05-Jun-2014 christos

branches: 1.32.2;
PR/48876: Dmitriy Grigoryev: Core dump in readline lib on attempted expansion
Make sure we have 2 matches before calling strcmp().


Revision tags: netbsd-6-0-6-RELEASE netbsd-6-1-5-RELEASE yamt-pagecache-base9 yamt-pagecache-tag8 netbsd-6-1-4-RELEASE netbsd-6-0-5-RELEASE riastradh-xf86-video-intel-2-7-1-pre-2-21-15 riastradh-drm2-base3 netbsd-6-1-3-RELEASE netbsd-6-0-4-RELEASE netbsd-6-1-2-RELEASE netbsd-6-0-3-RELEASE netbsd-6-1-1-RELEASE riastradh-drm2-base2 riastradh-drm2-base1 riastradh-drm2-base netbsd-6-0-2-RELEASE netbsd-6-1-RELEASE netbsd-6-1-RC4 netbsd-6-1-RC3 agc-symver-base netbsd-6-1-RC2 netbsd-6-1-RC1 yamt-pagecache-base8 netbsd-6-0-1-RELEASE yamt-pagecache-base7 matt-nb6-plus-nbase yamt-pagecache-base6 netbsd-6-0-RELEASE netbsd-6-0-RC2 matt-nb6-plus-base netbsd-6-0-RC1 yamt-pagecache-base5 yamt-pagecache-base4 netbsd-6-base yamt-pagecache-base3 yamt-pagecache-base2 yamt-pagecache-base
# 1.31 16-Sep-2011 plunky

branches: 1.31.8; 1.31.18;
NULL does not need a cast, here


# 1.30 16-Aug-2011 christos

re-enable -Wconversion


# 1.29 29-Jul-2011 christos

pass -Wconversion


# 1.28 29-Jul-2011 christos

- fix unused params
- unconditionalize vis.h


# 1.27 29-Jul-2011 christos

KNF return (\1); -> return \1;


# 1.26 28-Jul-2011 christos

kill ptr_t and ioctl_t, add * sizeof(*foo) to all allocations.


# 1.25 28-Jul-2011 christos

fix unused variable warnings on systems without _r functions


# 1.24 28-Jul-2011 christos

eliminate alloca for portability
portable getpw{nam,uid}


Revision tags: cherry-xenmp-base bouyer-quota2-nbase bouyer-quota2-base matt-mips64-premerge-20101231
# 1.23 06-Dec-2010 dholland

Improve previous to avoid changing the interface of an externally
exposed function. (But note that this function is neither documented
nor declared in any installed header file, and it probably should not
be externally exposed.) Related to PR 44183, closes PR 44186.


# 1.22 02-Dec-2010 dholland

add const, from PR 44183.


# 1.21 02-Dec-2010 dholland

Fix up bodgy code for printing completion matches; it used to sometimes
skip entries, print (null), run off the end of the array, or occasionally
receive SIGSEGV, and now will, hopefully at least, do none of that.

Based in part on the patch in PR 44183 from Sergio Acereda; I also
did some tidyup and fixed it to print top-to-bottom first like ls(1).


# 1.20 15-Nov-2010 christos

don't increment i twice in the loop. From Michael Byrnes


# 1.19 01-Jun-2010 christos

tidy up memory allocation and don't unnecessarily print "./" before names.


# 1.18 18-Jan-2010 christos

PR/42637: Joachim Kuebart: Shell tab completion crashes due to libedit stack
smashing


# 1.17 30-Dec-2009 christos

Wide character support (UTF-8) from Johny Mattsson; currently disabled.


# 1.16 28-Dec-2009 christos

improve on the listing display by printing only one character after the
filename not two, and no trailing blanks. I will revisit this when I write
the ls-F code.


Revision tags: matt-premerge-20091211 jym-xensuspend-nbase jym-xensuspend-base
# 1.15 16-Feb-2009 christos

fix sign compare issues.


# 1.14 15-Feb-2009 christos

pass lint on _LP64.


# 1.13 26-Jan-2009 apb

branches: 1.13.2;
Define HAVE_STRUCT_DIRENT_D_NAMLEN in config,h, and test it when
deciding whether to use entry->d_namlen or strlen(entry->d_name).
Addresses PR 40477 by Robert Millan.


# 1.12 11-Jan-2009 christos

- insert a space after the recognized string if it was an exact match
- initialize properly the string used for completion.
From Alex Bligh alex at alex dot org dot uk
- Make char constants consistent


Revision tags: netbsd-5-2-3-RELEASE netbsd-5-1-5-RELEASE netbsd-5-2-2-RELEASE netbsd-5-1-4-RELEASE netbsd-5-2-1-RELEASE netbsd-5-1-3-RELEASE netbsd-5-2-RELEASE netbsd-5-2-RC1 netbsd-5-1-2-RELEASE netbsd-5-1-1-RELEASE matt-nb5-mips64-premerge-20101231 matt-nb5-pq3-base netbsd-5-1-RELEASE netbsd-5-1-RC4 matt-nb5-mips64-k15 netbsd-5-1-RC3 netbsd-5-1-RC2 netbsd-5-1-RC1 netbsd-5-0-2-RELEASE matt-nb5-mips64-premerge-20091211 matt-nb5-mips64-u2-k2-k4-k7-k8-k9 matt-nb4-mips64-k7-u2a-k9b matt-nb5-mips64-u1-k1-k5 netbsd-5-0-1-RELEASE netbsd-5-0-RELEASE netbsd-5-0-RC4 netbsd-5-0-RC3 netbsd-5-0-RC2 netbsd-5-0-RC1 netbsd-5-base matt-mips64-base2 wrstuden-revivesa-base-3 wrstuden-revivesa-base-2 wrstuden-revivesa-base-1 yamt-pf42-base4 yamt-pf42-base3 hpcarm-cleanup-nbase yamt-pf42-base2 wrstuden-revivesa-base
# 1.11 29-Apr-2008 martin

Convert to new 2 clause license


Revision tags: netbsd-4-0-1-RELEASE wrstuden-fixsa-newbase yamt-pf42-baseX yamt-pf42-base keiichi-mipv6-base matt-armv6-nbase matt-armv6-prevmlocking wrstuden-fixsa-base-1 netbsd-4-0-RELEASE cube-autoconf-base netbsd-4-0-RC5 netbsd-4-0-RC4 netbsd-4-0-RC3 netbsd-4-0-RC2 netbsd-4-0-RC1 matt-armv6-base matt-mips64-base hpcarm-cleanup-base wrstuden-fixsa-base netbsd-4-base
# 1.10 09-Nov-2006 christos

branches: 1.10.16;
don't use alloca with ssp.


# 1.9 21-Aug-2006 christos

Change to a 3 clause copyright after permission of the holders.


Revision tags: abandoned-netbsd-4-base
# 1.8 03-Aug-2005 christos

Fix reversed test; from Gerry Swislow


# 1.7 11-Jun-2005 christos

PR/30500: Paul Shupak: Inconsistent definition of tilde_expand().
Provide a layer of indirection between the readline compatibility functions
and our internal implementation, so that we have the freedom to change the
function signature.


# 1.6 10-Jun-2005 christos

Bug reported from Martin Dietze:

The place to change the completion_append_character is
usually somewhere in the `rl_completion_entry_function'
callback which is where one usually can distinguish between
file- or dir-like entries to append a slash for dirs etc.

This does no longer work since `fn_complete()' takes the
`append_character' as argument before the callback is executed,
so that changes to the variable `rl_completion_append_character'
have in fact no effect for the current completion.

Fix by adding a function that returns the rl_completion_append_character,
when it gets passed in a filename in readline emulation.


# 1.5 18-May-2005 christos

Make completion_matches non-static since readline wants it.


# 1.4 12-May-2005 christos

PR/30215: Kouichirou Hiratsuka: /bin/sh dumps core with tabcomplete
Don't core-dump when trying to complete an empty line; instead assume ./


# 1.3 09-May-2005 dsl

Use getpwuid_r(getuid(), ...) to expand ~/....
Don't replace ~xyz with /home/xyz when expanding ~


# 1.2 07-May-2005 dsl

gdb directly calls filename_completion_function()


# 1.1 07-May-2005 dsl

Separate out the filename completion functions from the readline() code.
Pass in loads of parameters instead of relying on shed-loads of global
variables to modify the behaviour.
The filename completion code can now be enabled by code that uses el_gets().
(eg /bin/sh)


# 1.57 28-Jul-2019 christos

PR/54415: Ricky Zhou: libedit stats completions for non-file completions
Use the proper completion function and account for the character appended
by the function when computing the number of columns.


# 1.56 23-Jul-2019 christos

PR/54399: S�ren Tempel: Uninitialized memory access in libedit history.
Initialize the buffer using calloc. While here change all malloc(a * sizeof(b))
to calloc(a, sizeof(b)). XXX: should fix realloc similarly.


Revision tags: phil-wifi-20190609
# 1.55 20-Apr-2019 abhinav

PR lib/54131 - declare the loop variable outside the for loop


# 1.54 12-Apr-2019 christos

PR/54117: Paavo Helde: Fix memory overrun: Account for the closing quote
in memory allocation if quoted.


# 1.53 31-Mar-2019 abhinav

Perform quoting of filename completions when there are multiple matches as well

Quoting of special characters in filename completion was implemented for single match
case, this enables it for multiple matches as well. For example:

$ touch 'foo bar'
$ touch 'foo baz'
$ ls fo<TAB>
autocompletes to =>
$ ls foo\ ba
hitting <TAB> again shows:
foo bar foo baz

This required unescaping escape sequences generated during last completion
in order to find the word to complete.

While there, also update the test to include cases for multiple matches.

Reviewed by christos


# 1.52 24-Mar-2019 abhinav

Only quote the completion matches if we are doing filename completion

If the user supplies a value for the attempted_completion_function parameter
then we cannot be sure if the completion is for filename or something else, in such
a case don't attempt to quote the completion matches.

Reviewed by christos

This should address PR lib/54067


Revision tags: pgoyette-compat-20190127 pgoyette-compat-20190118 pgoyette-compat-1226 pgoyette-compat-1126 pgoyette-compat-1020 pgoyette-compat-0930 pgoyette-compat-0906 pgoyette-compat-0728 phil-wifi-base pgoyette-compat-0625 pgoyette-compat-0521
# 1.51 04-May-2018 christos

branches: 1.51.2;
fix uninitialized


# 1.50 04-May-2018 abhinav

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


# 1.49 02-May-2018 abhinav

Add '*' and '[' to the list of characters which need escaping during autocompletion


Revision tags: pgoyette-compat-0502 pgoyette-compat-0422 pgoyette-compat-0415 pgoyette-compat-0407 pgoyette-compat-0330 pgoyette-compat-0322 pgoyette-compat-0315 pgoyette-compat-base
# 1.48 27-Oct-2017 abhinav

branches: 1.48.2;
Add NULL check after doing memory allocation at a couple of places

ok christos@


# 1.47 15-Oct-2017 abhinav

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.


# 1.46 16-Sep-2017 abhinav

Fix indentation (convert spaces to tab)


Revision tags: netbsd-8-1-RELEASE netbsd-8-1-RC1 netbsd-8-0-RELEASE netbsd-8-0-RC2 netbsd-8-0-RC1 matt-nb8-mediatek-base perseant-stdc-iso10646-base netbsd-8-base prg-localcount2-base3 prg-localcount2-base2 prg-localcount2-base1 prg-localcount2-base pgoyette-localcount-20170426 bouyer-socketcan-base1
# 1.45 21-Apr-2017 abhinav

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@


Revision tags: pgoyette-localcount-20170320 bouyer-socketcan-base pgoyette-localcount-20170107 pgoyette-localcount-20161104
# 1.44 31-Oct-2016 abhinav

branches: 1.44.2;
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.


Revision tags: localcount-20160914 pgoyette-localcount-20160806 pgoyette-localcount-20160726 pgoyette-localcount-base
# 1.43 11-Apr-2016 christos

branches: 1.43.2;
Char -> wchar_t from Ingo Schwarze.


# 1.42 11-Apr-2016 christos

more macro WIDECHAR undoing from Ingo Schwarze.


# 1.41 09-Apr-2016 christos

Change some 0's to NULL's from Pedro Giffuni


# 1.40 17-Feb-2016 christos

whitespace and header sorting changes (Ingo Schwarze). No functional changes.


# 1.39 16-Feb-2016 christos

More header cleanups from Ingo Schwarze.


# 1.38 16-Feb-2016 christos

more include file cleanup (Ingo Schwarze)


# 1.37 16-Feb-2016 christos

From Ingo Scharze:
Let "el.h" include everything needed for struct editline,
and don't include that stuff multiple times. That also improves
consistency, also avoids circular inclusions, and also makes it
easier to follow what is going on, even though not quite as nice.
But it seems like the best we can do...


# 1.36 16-Feb-2016 christos

cleanup chartype.h includes (Ingo Schwarze)


# 1.35 16-Feb-2016 christos

cleanup inclusion of histedit.h (Ingo Schwarze)


# 1.34 18-Oct-2014 riz

callers's -> caller's


# 1.33 18-Oct-2014 snj

src is too big these days to tolerate superfluous apostrophes. It's
"its", people!


Revision tags: netbsd-7-base tls-earlyentropy-base tls-maxphys-base
# 1.32 05-Jun-2014 christos

branches: 1.32.2;
PR/48876: Dmitriy Grigoryev: Core dump in readline lib on attempted expansion
Make sure we have 2 matches before calling strcmp().


Revision tags: netbsd-6-0-6-RELEASE netbsd-6-1-5-RELEASE yamt-pagecache-base9 yamt-pagecache-tag8 netbsd-6-1-4-RELEASE netbsd-6-0-5-RELEASE riastradh-xf86-video-intel-2-7-1-pre-2-21-15 riastradh-drm2-base3 netbsd-6-1-3-RELEASE netbsd-6-0-4-RELEASE netbsd-6-1-2-RELEASE netbsd-6-0-3-RELEASE netbsd-6-1-1-RELEASE riastradh-drm2-base2 riastradh-drm2-base1 riastradh-drm2-base netbsd-6-0-2-RELEASE netbsd-6-1-RELEASE netbsd-6-1-RC4 netbsd-6-1-RC3 agc-symver-base netbsd-6-1-RC2 netbsd-6-1-RC1 yamt-pagecache-base8 netbsd-6-0-1-RELEASE yamt-pagecache-base7 matt-nb6-plus-nbase yamt-pagecache-base6 netbsd-6-0-RELEASE netbsd-6-0-RC2 matt-nb6-plus-base netbsd-6-0-RC1 yamt-pagecache-base5 yamt-pagecache-base4 netbsd-6-base yamt-pagecache-base3 yamt-pagecache-base2 yamt-pagecache-base
# 1.31 16-Sep-2011 plunky

branches: 1.31.8; 1.31.18;
NULL does not need a cast, here


# 1.30 16-Aug-2011 christos

re-enable -Wconversion


# 1.29 29-Jul-2011 christos

pass -Wconversion


# 1.28 29-Jul-2011 christos

- fix unused params
- unconditionalize vis.h


# 1.27 29-Jul-2011 christos

KNF return (\1); -> return \1;


# 1.26 28-Jul-2011 christos

kill ptr_t and ioctl_t, add * sizeof(*foo) to all allocations.


# 1.25 28-Jul-2011 christos

fix unused variable warnings on systems without _r functions


# 1.24 28-Jul-2011 christos

eliminate alloca for portability
portable getpw{nam,uid}


Revision tags: cherry-xenmp-base bouyer-quota2-nbase bouyer-quota2-base matt-mips64-premerge-20101231
# 1.23 06-Dec-2010 dholland

Improve previous to avoid changing the interface of an externally
exposed function. (But note that this function is neither documented
nor declared in any installed header file, and it probably should not
be externally exposed.) Related to PR 44183, closes PR 44186.


# 1.22 02-Dec-2010 dholland

add const, from PR 44183.


# 1.21 02-Dec-2010 dholland

Fix up bodgy code for printing completion matches; it used to sometimes
skip entries, print (null), run off the end of the array, or occasionally
receive SIGSEGV, and now will, hopefully at least, do none of that.

Based in part on the patch in PR 44183 from Sergio Acereda; I also
did some tidyup and fixed it to print top-to-bottom first like ls(1).


# 1.20 15-Nov-2010 christos

don't increment i twice in the loop. From Michael Byrnes


# 1.19 01-Jun-2010 christos

tidy up memory allocation and don't unnecessarily print "./" before names.


# 1.18 18-Jan-2010 christos

PR/42637: Joachim Kuebart: Shell tab completion crashes due to libedit stack
smashing


# 1.17 30-Dec-2009 christos

Wide character support (UTF-8) from Johny Mattsson; currently disabled.


# 1.16 28-Dec-2009 christos

improve on the listing display by printing only one character after the
filename not two, and no trailing blanks. I will revisit this when I write
the ls-F code.


Revision tags: matt-premerge-20091211 jym-xensuspend-nbase jym-xensuspend-base
# 1.15 16-Feb-2009 christos

fix sign compare issues.


# 1.14 15-Feb-2009 christos

pass lint on _LP64.


# 1.13 26-Jan-2009 apb

branches: 1.13.2;
Define HAVE_STRUCT_DIRENT_D_NAMLEN in config,h, and test it when
deciding whether to use entry->d_namlen or strlen(entry->d_name).
Addresses PR 40477 by Robert Millan.


# 1.12 11-Jan-2009 christos

- insert a space after the recognized string if it was an exact match
- initialize properly the string used for completion.
From Alex Bligh alex at alex dot org dot uk
- Make char constants consistent


Revision tags: netbsd-5-2-3-RELEASE netbsd-5-1-5-RELEASE netbsd-5-2-2-RELEASE netbsd-5-1-4-RELEASE netbsd-5-2-1-RELEASE netbsd-5-1-3-RELEASE netbsd-5-2-RELEASE netbsd-5-2-RC1 netbsd-5-1-2-RELEASE netbsd-5-1-1-RELEASE matt-nb5-mips64-premerge-20101231 matt-nb5-pq3-base netbsd-5-1-RELEASE netbsd-5-1-RC4 matt-nb5-mips64-k15 netbsd-5-1-RC3 netbsd-5-1-RC2 netbsd-5-1-RC1 netbsd-5-0-2-RELEASE matt-nb5-mips64-premerge-20091211 matt-nb5-mips64-u2-k2-k4-k7-k8-k9 matt-nb4-mips64-k7-u2a-k9b matt-nb5-mips64-u1-k1-k5 netbsd-5-0-1-RELEASE netbsd-5-0-RELEASE netbsd-5-0-RC4 netbsd-5-0-RC3 netbsd-5-0-RC2 netbsd-5-0-RC1 netbsd-5-base matt-mips64-base2 wrstuden-revivesa-base-3 wrstuden-revivesa-base-2 wrstuden-revivesa-base-1 yamt-pf42-base4 yamt-pf42-base3 hpcarm-cleanup-nbase yamt-pf42-base2 wrstuden-revivesa-base
# 1.11 29-Apr-2008 martin

Convert to new 2 clause license


Revision tags: netbsd-4-0-1-RELEASE wrstuden-fixsa-newbase yamt-pf42-baseX yamt-pf42-base keiichi-mipv6-base matt-armv6-nbase matt-armv6-prevmlocking wrstuden-fixsa-base-1 netbsd-4-0-RELEASE cube-autoconf-base netbsd-4-0-RC5 netbsd-4-0-RC4 netbsd-4-0-RC3 netbsd-4-0-RC2 netbsd-4-0-RC1 matt-armv6-base matt-mips64-base hpcarm-cleanup-base wrstuden-fixsa-base netbsd-4-base
# 1.10 09-Nov-2006 christos

branches: 1.10.16;
don't use alloca with ssp.


# 1.9 21-Aug-2006 christos

Change to a 3 clause copyright after permission of the holders.


Revision tags: abandoned-netbsd-4-base
# 1.8 03-Aug-2005 christos

Fix reversed test; from Gerry Swislow


# 1.7 11-Jun-2005 christos

PR/30500: Paul Shupak: Inconsistent definition of tilde_expand().
Provide a layer of indirection between the readline compatibility functions
and our internal implementation, so that we have the freedom to change the
function signature.


# 1.6 10-Jun-2005 christos

Bug reported from Martin Dietze:

The place to change the completion_append_character is
usually somewhere in the `rl_completion_entry_function'
callback which is where one usually can distinguish between
file- or dir-like entries to append a slash for dirs etc.

This does no longer work since `fn_complete()' takes the
`append_character' as argument before the callback is executed,
so that changes to the variable `rl_completion_append_character'
have in fact no effect for the current completion.

Fix by adding a function that returns the rl_completion_append_character,
when it gets passed in a filename in readline emulation.


# 1.5 18-May-2005 christos

Make completion_matches non-static since readline wants it.


# 1.4 12-May-2005 christos

PR/30215: Kouichirou Hiratsuka: /bin/sh dumps core with tabcomplete
Don't core-dump when trying to complete an empty line; instead assume ./


# 1.3 09-May-2005 dsl

Use getpwuid_r(getuid(), ...) to expand ~/....
Don't replace ~xyz with /home/xyz when expanding ~


# 1.2 07-May-2005 dsl

gdb directly calls filename_completion_function()


# 1.1 07-May-2005 dsl

Separate out the filename completion functions from the readline() code.
Pass in loads of parameters instead of relying on shed-loads of global
variables to modify the behaviour.
The filename completion code can now be enabled by code that uses el_gets().
(eg /bin/sh)


# 1.56 23-Jul-2019 christos

PR/54399: S�ren Tempel: Uninitialized memory access in libedit history.
Initialize the buffer using calloc. While here change all malloc(a * sizeof(b))
to calloc(a, sizeof(b)). XXX: should fix realloc similarly.


Revision tags: phil-wifi-20190609
# 1.55 20-Apr-2019 abhinav

PR lib/54131 - declare the loop variable outside the for loop


# 1.54 12-Apr-2019 christos

PR/54117: Paavo Helde: Fix memory overrun: Account for the closing quote
in memory allocation if quoted.


# 1.53 31-Mar-2019 abhinav

Perform quoting of filename completions when there are multiple matches as well

Quoting of special characters in filename completion was implemented for single match
case, this enables it for multiple matches as well. For example:

$ touch 'foo bar'
$ touch 'foo baz'
$ ls fo<TAB>
autocompletes to =>
$ ls foo\ ba
hitting <TAB> again shows:
foo bar foo baz

This required unescaping escape sequences generated during last completion
in order to find the word to complete.

While there, also update the test to include cases for multiple matches.

Reviewed by christos


# 1.52 24-Mar-2019 abhinav

Only quote the completion matches if we are doing filename completion

If the user supplies a value for the attempted_completion_function parameter
then we cannot be sure if the completion is for filename or something else, in such
a case don't attempt to quote the completion matches.

Reviewed by christos

This should address PR lib/54067


Revision tags: pgoyette-compat-20190127 pgoyette-compat-20190118 pgoyette-compat-1226 pgoyette-compat-1126 pgoyette-compat-1020 pgoyette-compat-0930 pgoyette-compat-0906 pgoyette-compat-0728 phil-wifi-base pgoyette-compat-0625 pgoyette-compat-0521
# 1.51 04-May-2018 christos

branches: 1.51.2;
fix uninitialized


# 1.50 04-May-2018 abhinav

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


# 1.49 02-May-2018 abhinav

Add '*' and '[' to the list of characters which need escaping during autocompletion


Revision tags: pgoyette-compat-0502 pgoyette-compat-0422 pgoyette-compat-0415 pgoyette-compat-0407 pgoyette-compat-0330 pgoyette-compat-0322 pgoyette-compat-0315 pgoyette-compat-base
# 1.48 27-Oct-2017 abhinav

branches: 1.48.2;
Add NULL check after doing memory allocation at a couple of places

ok christos@


# 1.47 15-Oct-2017 abhinav

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.


# 1.46 16-Sep-2017 abhinav

Fix indentation (convert spaces to tab)


Revision tags: netbsd-8-1-RELEASE netbsd-8-1-RC1 netbsd-8-0-RELEASE netbsd-8-0-RC2 netbsd-8-0-RC1 matt-nb8-mediatek-base perseant-stdc-iso10646-base netbsd-8-base prg-localcount2-base3 prg-localcount2-base2 prg-localcount2-base1 prg-localcount2-base pgoyette-localcount-20170426 bouyer-socketcan-base1
# 1.45 21-Apr-2017 abhinav

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@


Revision tags: pgoyette-localcount-20170320 bouyer-socketcan-base pgoyette-localcount-20170107 pgoyette-localcount-20161104
# 1.44 31-Oct-2016 abhinav

branches: 1.44.2;
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.


Revision tags: localcount-20160914 pgoyette-localcount-20160806 pgoyette-localcount-20160726 pgoyette-localcount-base
# 1.43 11-Apr-2016 christos

branches: 1.43.2;
Char -> wchar_t from Ingo Schwarze.


# 1.42 11-Apr-2016 christos

more macro WIDECHAR undoing from Ingo Schwarze.


# 1.41 09-Apr-2016 christos

Change some 0's to NULL's from Pedro Giffuni


# 1.40 17-Feb-2016 christos

whitespace and header sorting changes (Ingo Schwarze). No functional changes.


# 1.39 16-Feb-2016 christos

More header cleanups from Ingo Schwarze.


# 1.38 16-Feb-2016 christos

more include file cleanup (Ingo Schwarze)


# 1.37 16-Feb-2016 christos

From Ingo Scharze:
Let "el.h" include everything needed for struct editline,
and don't include that stuff multiple times. That also improves
consistency, also avoids circular inclusions, and also makes it
easier to follow what is going on, even though not quite as nice.
But it seems like the best we can do...


# 1.36 16-Feb-2016 christos

cleanup chartype.h includes (Ingo Schwarze)


# 1.35 16-Feb-2016 christos

cleanup inclusion of histedit.h (Ingo Schwarze)


# 1.34 18-Oct-2014 riz

callers's -> caller's


# 1.33 18-Oct-2014 snj

src is too big these days to tolerate superfluous apostrophes. It's
"its", people!


Revision tags: netbsd-7-base tls-earlyentropy-base tls-maxphys-base
# 1.32 05-Jun-2014 christos

branches: 1.32.2;
PR/48876: Dmitriy Grigoryev: Core dump in readline lib on attempted expansion
Make sure we have 2 matches before calling strcmp().


Revision tags: netbsd-6-0-6-RELEASE netbsd-6-1-5-RELEASE yamt-pagecache-base9 yamt-pagecache-tag8 netbsd-6-1-4-RELEASE netbsd-6-0-5-RELEASE riastradh-xf86-video-intel-2-7-1-pre-2-21-15 riastradh-drm2-base3 netbsd-6-1-3-RELEASE netbsd-6-0-4-RELEASE netbsd-6-1-2-RELEASE netbsd-6-0-3-RELEASE netbsd-6-1-1-RELEASE riastradh-drm2-base2 riastradh-drm2-base1 riastradh-drm2-base netbsd-6-0-2-RELEASE netbsd-6-1-RELEASE netbsd-6-1-RC4 netbsd-6-1-RC3 agc-symver-base netbsd-6-1-RC2 netbsd-6-1-RC1 yamt-pagecache-base8 netbsd-6-0-1-RELEASE yamt-pagecache-base7 matt-nb6-plus-nbase yamt-pagecache-base6 netbsd-6-0-RELEASE netbsd-6-0-RC2 matt-nb6-plus-base netbsd-6-0-RC1 yamt-pagecache-base5 yamt-pagecache-base4 netbsd-6-base yamt-pagecache-base3 yamt-pagecache-base2 yamt-pagecache-base
# 1.31 16-Sep-2011 plunky

branches: 1.31.8; 1.31.18;
NULL does not need a cast, here


# 1.30 16-Aug-2011 christos

re-enable -Wconversion


# 1.29 29-Jul-2011 christos

pass -Wconversion


# 1.28 29-Jul-2011 christos

- fix unused params
- unconditionalize vis.h


# 1.27 29-Jul-2011 christos

KNF return (\1); -> return \1;


# 1.26 28-Jul-2011 christos

kill ptr_t and ioctl_t, add * sizeof(*foo) to all allocations.


# 1.25 28-Jul-2011 christos

fix unused variable warnings on systems without _r functions


# 1.24 28-Jul-2011 christos

eliminate alloca for portability
portable getpw{nam,uid}


Revision tags: cherry-xenmp-base bouyer-quota2-nbase bouyer-quota2-base matt-mips64-premerge-20101231
# 1.23 06-Dec-2010 dholland

Improve previous to avoid changing the interface of an externally
exposed function. (But note that this function is neither documented
nor declared in any installed header file, and it probably should not
be externally exposed.) Related to PR 44183, closes PR 44186.


# 1.22 02-Dec-2010 dholland

add const, from PR 44183.


# 1.21 02-Dec-2010 dholland

Fix up bodgy code for printing completion matches; it used to sometimes
skip entries, print (null), run off the end of the array, or occasionally
receive SIGSEGV, and now will, hopefully at least, do none of that.

Based in part on the patch in PR 44183 from Sergio Acereda; I also
did some tidyup and fixed it to print top-to-bottom first like ls(1).


# 1.20 15-Nov-2010 christos

don't increment i twice in the loop. From Michael Byrnes


# 1.19 01-Jun-2010 christos

tidy up memory allocation and don't unnecessarily print "./" before names.


# 1.18 18-Jan-2010 christos

PR/42637: Joachim Kuebart: Shell tab completion crashes due to libedit stack
smashing


# 1.17 30-Dec-2009 christos

Wide character support (UTF-8) from Johny Mattsson; currently disabled.


# 1.16 28-Dec-2009 christos

improve on the listing display by printing only one character after the
filename not two, and no trailing blanks. I will revisit this when I write
the ls-F code.


Revision tags: matt-premerge-20091211 jym-xensuspend-nbase jym-xensuspend-base
# 1.15 16-Feb-2009 christos

fix sign compare issues.


# 1.14 15-Feb-2009 christos

pass lint on _LP64.


# 1.13 26-Jan-2009 apb

branches: 1.13.2;
Define HAVE_STRUCT_DIRENT_D_NAMLEN in config,h, and test it when
deciding whether to use entry->d_namlen or strlen(entry->d_name).
Addresses PR 40477 by Robert Millan.


# 1.12 11-Jan-2009 christos

- insert a space after the recognized string if it was an exact match
- initialize properly the string used for completion.
From Alex Bligh alex at alex dot org dot uk
- Make char constants consistent


Revision tags: netbsd-5-2-3-RELEASE netbsd-5-1-5-RELEASE netbsd-5-2-2-RELEASE netbsd-5-1-4-RELEASE netbsd-5-2-1-RELEASE netbsd-5-1-3-RELEASE netbsd-5-2-RELEASE netbsd-5-2-RC1 netbsd-5-1-2-RELEASE netbsd-5-1-1-RELEASE matt-nb5-mips64-premerge-20101231 matt-nb5-pq3-base netbsd-5-1-RELEASE netbsd-5-1-RC4 matt-nb5-mips64-k15 netbsd-5-1-RC3 netbsd-5-1-RC2 netbsd-5-1-RC1 netbsd-5-0-2-RELEASE matt-nb5-mips64-premerge-20091211 matt-nb5-mips64-u2-k2-k4-k7-k8-k9 matt-nb4-mips64-k7-u2a-k9b matt-nb5-mips64-u1-k1-k5 netbsd-5-0-1-RELEASE netbsd-5-0-RELEASE netbsd-5-0-RC4 netbsd-5-0-RC3 netbsd-5-0-RC2 netbsd-5-0-RC1 netbsd-5-base matt-mips64-base2 wrstuden-revivesa-base-3 wrstuden-revivesa-base-2 wrstuden-revivesa-base-1 yamt-pf42-base4 yamt-pf42-base3 hpcarm-cleanup-nbase yamt-pf42-base2 wrstuden-revivesa-base
# 1.11 29-Apr-2008 martin

Convert to new 2 clause license


Revision tags: netbsd-4-0-1-RELEASE wrstuden-fixsa-newbase yamt-pf42-baseX yamt-pf42-base keiichi-mipv6-base matt-armv6-nbase matt-armv6-prevmlocking wrstuden-fixsa-base-1 netbsd-4-0-RELEASE cube-autoconf-base netbsd-4-0-RC5 netbsd-4-0-RC4 netbsd-4-0-RC3 netbsd-4-0-RC2 netbsd-4-0-RC1 matt-armv6-base matt-mips64-base hpcarm-cleanup-base wrstuden-fixsa-base netbsd-4-base
# 1.10 09-Nov-2006 christos

branches: 1.10.16;
don't use alloca with ssp.


# 1.9 21-Aug-2006 christos

Change to a 3 clause copyright after permission of the holders.


Revision tags: abandoned-netbsd-4-base
# 1.8 03-Aug-2005 christos

Fix reversed test; from Gerry Swislow


# 1.7 11-Jun-2005 christos

PR/30500: Paul Shupak: Inconsistent definition of tilde_expand().
Provide a layer of indirection between the readline compatibility functions
and our internal implementation, so that we have the freedom to change the
function signature.


# 1.6 10-Jun-2005 christos

Bug reported from Martin Dietze:

The place to change the completion_append_character is
usually somewhere in the `rl_completion_entry_function'
callback which is where one usually can distinguish between
file- or dir-like entries to append a slash for dirs etc.

This does no longer work since `fn_complete()' takes the
`append_character' as argument before the callback is executed,
so that changes to the variable `rl_completion_append_character'
have in fact no effect for the current completion.

Fix by adding a function that returns the rl_completion_append_character,
when it gets passed in a filename in readline emulation.


# 1.5 18-May-2005 christos

Make completion_matches non-static since readline wants it.


# 1.4 12-May-2005 christos

PR/30215: Kouichirou Hiratsuka: /bin/sh dumps core with tabcomplete
Don't core-dump when trying to complete an empty line; instead assume ./


# 1.3 09-May-2005 dsl

Use getpwuid_r(getuid(), ...) to expand ~/....
Don't replace ~xyz with /home/xyz when expanding ~


# 1.2 07-May-2005 dsl

gdb directly calls filename_completion_function()


# 1.1 07-May-2005 dsl

Separate out the filename completion functions from the readline() code.
Pass in loads of parameters instead of relying on shed-loads of global
variables to modify the behaviour.
The filename completion code can now be enabled by code that uses el_gets().
(eg /bin/sh)


# 1.55 20-Apr-2019 abhinav

PR lib/54131 - declare the loop variable outside the for loop


# 1.54 12-Apr-2019 christos

PR/54117: Paavo Helde: Fix memory overrun: Account for the closing quote
in memory allocation if quoted.


# 1.53 31-Mar-2019 abhinav

Perform quoting of filename completions when there are multiple matches as well

Quoting of special characters in filename completion was implemented for single match
case, this enables it for multiple matches as well. For example:

$ touch 'foo bar'
$ touch 'foo baz'
$ ls fo<TAB>
autocompletes to =>
$ ls foo\ ba
hitting <TAB> again shows:
foo bar foo baz

This required unescaping escape sequences generated during last completion
in order to find the word to complete.

While there, also update the test to include cases for multiple matches.

Reviewed by christos


# 1.52 24-Mar-2019 abhinav

Only quote the completion matches if we are doing filename completion

If the user supplies a value for the attempted_completion_function parameter
then we cannot be sure if the completion is for filename or something else, in such
a case don't attempt to quote the completion matches.

Reviewed by christos

This should address PR lib/54067


Revision tags: pgoyette-compat-20190127 pgoyette-compat-20190118 pgoyette-compat-1226 pgoyette-compat-1126 pgoyette-compat-1020 pgoyette-compat-0930 pgoyette-compat-0906 pgoyette-compat-0728 phil-wifi-base pgoyette-compat-0625 pgoyette-compat-0521
# 1.51 04-May-2018 christos

fix uninitialized


# 1.50 04-May-2018 abhinav

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


# 1.49 02-May-2018 abhinav

Add '*' and '[' to the list of characters which need escaping during autocompletion


Revision tags: pgoyette-compat-0502 pgoyette-compat-0422 pgoyette-compat-0415 pgoyette-compat-0407 pgoyette-compat-0330 pgoyette-compat-0322 pgoyette-compat-0315 pgoyette-compat-base
# 1.48 27-Oct-2017 abhinav

branches: 1.48.2;
Add NULL check after doing memory allocation at a couple of places

ok christos@


# 1.47 15-Oct-2017 abhinav

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.


# 1.46 16-Sep-2017 abhinav

Fix indentation (convert spaces to tab)


Revision tags: netbsd-8-0-RELEASE netbsd-8-0-RC2 netbsd-8-0-RC1 matt-nb8-mediatek-base perseant-stdc-iso10646-base netbsd-8-base prg-localcount2-base3 prg-localcount2-base2 prg-localcount2-base1 prg-localcount2-base pgoyette-localcount-20170426 bouyer-socketcan-base1
# 1.45 21-Apr-2017 abhinav

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@


Revision tags: pgoyette-localcount-20170320 bouyer-socketcan-base pgoyette-localcount-20170107 pgoyette-localcount-20161104
# 1.44 31-Oct-2016 abhinav

branches: 1.44.2;
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.


Revision tags: localcount-20160914 pgoyette-localcount-20160806 pgoyette-localcount-20160726 pgoyette-localcount-base
# 1.43 11-Apr-2016 christos

branches: 1.43.2;
Char -> wchar_t from Ingo Schwarze.


# 1.42 11-Apr-2016 christos

more macro WIDECHAR undoing from Ingo Schwarze.


# 1.41 09-Apr-2016 christos

Change some 0's to NULL's from Pedro Giffuni


# 1.40 17-Feb-2016 christos

whitespace and header sorting changes (Ingo Schwarze). No functional changes.


# 1.39 16-Feb-2016 christos

More header cleanups from Ingo Schwarze.


# 1.38 16-Feb-2016 christos

more include file cleanup (Ingo Schwarze)


# 1.37 16-Feb-2016 christos

From Ingo Scharze:
Let "el.h" include everything needed for struct editline,
and don't include that stuff multiple times. That also improves
consistency, also avoids circular inclusions, and also makes it
easier to follow what is going on, even though not quite as nice.
But it seems like the best we can do...


# 1.36 16-Feb-2016 christos

cleanup chartype.h includes (Ingo Schwarze)


# 1.35 16-Feb-2016 christos

cleanup inclusion of histedit.h (Ingo Schwarze)


# 1.34 18-Oct-2014 riz

callers's -> caller's


# 1.33 18-Oct-2014 snj

src is too big these days to tolerate superfluous apostrophes. It's
"its", people!


Revision tags: netbsd-7-base tls-earlyentropy-base tls-maxphys-base
# 1.32 05-Jun-2014 christos

branches: 1.32.2;
PR/48876: Dmitriy Grigoryev: Core dump in readline lib on attempted expansion
Make sure we have 2 matches before calling strcmp().


Revision tags: netbsd-6-0-6-RELEASE netbsd-6-1-5-RELEASE yamt-pagecache-base9 yamt-pagecache-tag8 netbsd-6-1-4-RELEASE netbsd-6-0-5-RELEASE riastradh-xf86-video-intel-2-7-1-pre-2-21-15 riastradh-drm2-base3 netbsd-6-1-3-RELEASE netbsd-6-0-4-RELEASE netbsd-6-1-2-RELEASE netbsd-6-0-3-RELEASE netbsd-6-1-1-RELEASE riastradh-drm2-base2 riastradh-drm2-base1 riastradh-drm2-base netbsd-6-0-2-RELEASE netbsd-6-1-RELEASE netbsd-6-1-RC4 netbsd-6-1-RC3 agc-symver-base netbsd-6-1-RC2 netbsd-6-1-RC1 yamt-pagecache-base8 netbsd-6-0-1-RELEASE yamt-pagecache-base7 matt-nb6-plus-nbase yamt-pagecache-base6 netbsd-6-0-RELEASE netbsd-6-0-RC2 matt-nb6-plus-base netbsd-6-0-RC1 yamt-pagecache-base5 yamt-pagecache-base4 netbsd-6-base yamt-pagecache-base3 yamt-pagecache-base2 yamt-pagecache-base
# 1.31 16-Sep-2011 plunky

branches: 1.31.8; 1.31.18;
NULL does not need a cast, here


# 1.30 16-Aug-2011 christos

re-enable -Wconversion


# 1.29 29-Jul-2011 christos

pass -Wconversion


# 1.28 29-Jul-2011 christos

- fix unused params
- unconditionalize vis.h


# 1.27 29-Jul-2011 christos

KNF return (\1); -> return \1;


# 1.26 28-Jul-2011 christos

kill ptr_t and ioctl_t, add * sizeof(*foo) to all allocations.


# 1.25 28-Jul-2011 christos

fix unused variable warnings on systems without _r functions


# 1.24 28-Jul-2011 christos

eliminate alloca for portability
portable getpw{nam,uid}


Revision tags: cherry-xenmp-base bouyer-quota2-nbase bouyer-quota2-base matt-mips64-premerge-20101231
# 1.23 06-Dec-2010 dholland

Improve previous to avoid changing the interface of an externally
exposed function. (But note that this function is neither documented
nor declared in any installed header file, and it probably should not
be externally exposed.) Related to PR 44183, closes PR 44186.


# 1.22 02-Dec-2010 dholland

add const, from PR 44183.


# 1.21 02-Dec-2010 dholland

Fix up bodgy code for printing completion matches; it used to sometimes
skip entries, print (null), run off the end of the array, or occasionally
receive SIGSEGV, and now will, hopefully at least, do none of that.

Based in part on the patch in PR 44183 from Sergio Acereda; I also
did some tidyup and fixed it to print top-to-bottom first like ls(1).


# 1.20 15-Nov-2010 christos

don't increment i twice in the loop. From Michael Byrnes


# 1.19 01-Jun-2010 christos

tidy up memory allocation and don't unnecessarily print "./" before names.


# 1.18 18-Jan-2010 christos

PR/42637: Joachim Kuebart: Shell tab completion crashes due to libedit stack
smashing


# 1.17 30-Dec-2009 christos

Wide character support (UTF-8) from Johny Mattsson; currently disabled.


# 1.16 28-Dec-2009 christos

improve on the listing display by printing only one character after the
filename not two, and no trailing blanks. I will revisit this when I write
the ls-F code.


Revision tags: matt-premerge-20091211 jym-xensuspend-nbase jym-xensuspend-base
# 1.15 16-Feb-2009 christos

fix sign compare issues.


# 1.14 15-Feb-2009 christos

pass lint on _LP64.


# 1.13 26-Jan-2009 apb

branches: 1.13.2;
Define HAVE_STRUCT_DIRENT_D_NAMLEN in config,h, and test it when
deciding whether to use entry->d_namlen or strlen(entry->d_name).
Addresses PR 40477 by Robert Millan.


# 1.12 11-Jan-2009 christos

- insert a space after the recognized string if it was an exact match
- initialize properly the string used for completion.
From Alex Bligh alex at alex dot org dot uk
- Make char constants consistent


Revision tags: netbsd-5-2-3-RELEASE netbsd-5-1-5-RELEASE netbsd-5-2-2-RELEASE netbsd-5-1-4-RELEASE netbsd-5-2-1-RELEASE netbsd-5-1-3-RELEASE netbsd-5-2-RELEASE netbsd-5-2-RC1 netbsd-5-1-2-RELEASE netbsd-5-1-1-RELEASE matt-nb5-mips64-premerge-20101231 matt-nb5-pq3-base netbsd-5-1-RELEASE netbsd-5-1-RC4 matt-nb5-mips64-k15 netbsd-5-1-RC3 netbsd-5-1-RC2 netbsd-5-1-RC1 netbsd-5-0-2-RELEASE matt-nb5-mips64-premerge-20091211 matt-nb5-mips64-u2-k2-k4-k7-k8-k9 matt-nb4-mips64-k7-u2a-k9b matt-nb5-mips64-u1-k1-k5 netbsd-5-0-1-RELEASE netbsd-5-0-RELEASE netbsd-5-0-RC4 netbsd-5-0-RC3 netbsd-5-0-RC2 netbsd-5-0-RC1 netbsd-5-base matt-mips64-base2 wrstuden-revivesa-base-3 wrstuden-revivesa-base-2 wrstuden-revivesa-base-1 yamt-pf42-base4 yamt-pf42-base3 hpcarm-cleanup-nbase yamt-pf42-base2 wrstuden-revivesa-base
# 1.11 29-Apr-2008 martin

Convert to new 2 clause license


Revision tags: netbsd-4-0-1-RELEASE wrstuden-fixsa-newbase yamt-pf42-baseX yamt-pf42-base keiichi-mipv6-base matt-armv6-nbase matt-armv6-prevmlocking wrstuden-fixsa-base-1 netbsd-4-0-RELEASE cube-autoconf-base netbsd-4-0-RC5 netbsd-4-0-RC4 netbsd-4-0-RC3 netbsd-4-0-RC2 netbsd-4-0-RC1 matt-armv6-base matt-mips64-base hpcarm-cleanup-base wrstuden-fixsa-base netbsd-4-base
# 1.10 09-Nov-2006 christos

branches: 1.10.16;
don't use alloca with ssp.


# 1.9 21-Aug-2006 christos

Change to a 3 clause copyright after permission of the holders.


Revision tags: abandoned-netbsd-4-base
# 1.8 03-Aug-2005 christos

Fix reversed test; from Gerry Swislow


# 1.7 11-Jun-2005 christos

PR/30500: Paul Shupak: Inconsistent definition of tilde_expand().
Provide a layer of indirection between the readline compatibility functions
and our internal implementation, so that we have the freedom to change the
function signature.


# 1.6 10-Jun-2005 christos

Bug reported from Martin Dietze:

The place to change the completion_append_character is
usually somewhere in the `rl_completion_entry_function'
callback which is where one usually can distinguish between
file- or dir-like entries to append a slash for dirs etc.

This does no longer work since `fn_complete()' takes the
`append_character' as argument before the callback is executed,
so that changes to the variable `rl_completion_append_character'
have in fact no effect for the current completion.

Fix by adding a function that returns the rl_completion_append_character,
when it gets passed in a filename in readline emulation.


# 1.5 18-May-2005 christos

Make completion_matches non-static since readline wants it.


# 1.4 12-May-2005 christos

PR/30215: Kouichirou Hiratsuka: /bin/sh dumps core with tabcomplete
Don't core-dump when trying to complete an empty line; instead assume ./


# 1.3 09-May-2005 dsl

Use getpwuid_r(getuid(), ...) to expand ~/....
Don't replace ~xyz with /home/xyz when expanding ~


# 1.2 07-May-2005 dsl

gdb directly calls filename_completion_function()


# 1.1 07-May-2005 dsl

Separate out the filename completion functions from the readline() code.
Pass in loads of parameters instead of relying on shed-loads of global
variables to modify the behaviour.
The filename completion code can now be enabled by code that uses el_gets().
(eg /bin/sh)


# 1.48 27-Oct-2017 abhinav

Add NULL check after doing memory allocation at a couple of places

ok christos@


# 1.47 15-Oct-2017 abhinav

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.


# 1.46 16-Sep-2017 abhinav

Fix indentation (convert spaces to tab)


Revision tags: matt-nb8-mediatek-base perseant-stdc-iso10646-base netbsd-8-base prg-localcount2-base3 prg-localcount2-base2 prg-localcount2-base1 prg-localcount2-base pgoyette-localcount-20170426 bouyer-socketcan-base1
# 1.45 21-Apr-2017 abhinav

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@


Revision tags: pgoyette-localcount-20170320 bouyer-socketcan-base pgoyette-localcount-20170107 pgoyette-localcount-20161104
# 1.44 31-Oct-2016 abhinav

branches: 1.44.2;
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.


Revision tags: localcount-20160914 pgoyette-localcount-20160806 pgoyette-localcount-20160726 pgoyette-localcount-base
# 1.43 11-Apr-2016 christos

branches: 1.43.2;
Char -> wchar_t from Ingo Schwarze.


# 1.42 11-Apr-2016 christos

more macro WIDECHAR undoing from Ingo Schwarze.


# 1.41 09-Apr-2016 christos

Change some 0's to NULL's from Pedro Giffuni


# 1.40 17-Feb-2016 christos

whitespace and header sorting changes (Ingo Schwarze). No functional changes.


# 1.39 16-Feb-2016 christos

More header cleanups from Ingo Schwarze.


# 1.38 16-Feb-2016 christos

more include file cleanup (Ingo Schwarze)


# 1.37 16-Feb-2016 christos

From Ingo Scharze:
Let "el.h" include everything needed for struct editline,
and don't include that stuff multiple times. That also improves
consistency, also avoids circular inclusions, and also makes it
easier to follow what is going on, even though not quite as nice.
But it seems like the best we can do...


# 1.36 16-Feb-2016 christos

cleanup chartype.h includes (Ingo Schwarze)


# 1.35 16-Feb-2016 christos

cleanup inclusion of histedit.h (Ingo Schwarze)


# 1.34 18-Oct-2014 riz

callers's -> caller's


# 1.33 18-Oct-2014 snj

src is too big these days to tolerate superfluous apostrophes. It's
"its", people!


Revision tags: netbsd-7-base tls-earlyentropy-base tls-maxphys-base
# 1.32 05-Jun-2014 christos

branches: 1.32.2;
PR/48876: Dmitriy Grigoryev: Core dump in readline lib on attempted expansion
Make sure we have 2 matches before calling strcmp().


Revision tags: netbsd-6-0-6-RELEASE netbsd-6-1-5-RELEASE yamt-pagecache-base9 yamt-pagecache-tag8 netbsd-6-1-4-RELEASE netbsd-6-0-5-RELEASE riastradh-xf86-video-intel-2-7-1-pre-2-21-15 riastradh-drm2-base3 netbsd-6-1-3-RELEASE netbsd-6-0-4-RELEASE netbsd-6-1-2-RELEASE netbsd-6-0-3-RELEASE netbsd-6-1-1-RELEASE riastradh-drm2-base2 riastradh-drm2-base1 riastradh-drm2-base netbsd-6-0-2-RELEASE netbsd-6-1-RELEASE netbsd-6-1-RC4 netbsd-6-1-RC3 agc-symver-base netbsd-6-1-RC2 netbsd-6-1-RC1 yamt-pagecache-base8 netbsd-6-0-1-RELEASE yamt-pagecache-base7 matt-nb6-plus-nbase yamt-pagecache-base6 netbsd-6-0-RELEASE netbsd-6-0-RC2 matt-nb6-plus-base netbsd-6-0-RC1 yamt-pagecache-base5 yamt-pagecache-base4 netbsd-6-base yamt-pagecache-base3 yamt-pagecache-base2 yamt-pagecache-base
# 1.31 16-Sep-2011 plunky

branches: 1.31.8; 1.31.18;
NULL does not need a cast, here


# 1.30 16-Aug-2011 christos

re-enable -Wconversion


# 1.29 29-Jul-2011 christos

pass -Wconversion


# 1.28 29-Jul-2011 christos

- fix unused params
- unconditionalize vis.h


# 1.27 29-Jul-2011 christos

KNF return (\1); -> return \1;


# 1.26 28-Jul-2011 christos

kill ptr_t and ioctl_t, add * sizeof(*foo) to all allocations.


# 1.25 28-Jul-2011 christos

fix unused variable warnings on systems without _r functions


# 1.24 28-Jul-2011 christos

eliminate alloca for portability
portable getpw{nam,uid}


Revision tags: cherry-xenmp-base bouyer-quota2-nbase bouyer-quota2-base matt-mips64-premerge-20101231
# 1.23 06-Dec-2010 dholland

Improve previous to avoid changing the interface of an externally
exposed function. (But note that this function is neither documented
nor declared in any installed header file, and it probably should not
be externally exposed.) Related to PR 44183, closes PR 44186.


# 1.22 02-Dec-2010 dholland

add const, from PR 44183.


# 1.21 02-Dec-2010 dholland

Fix up bodgy code for printing completion matches; it used to sometimes
skip entries, print (null), run off the end of the array, or occasionally
receive SIGSEGV, and now will, hopefully at least, do none of that.

Based in part on the patch in PR 44183 from Sergio Acereda; I also
did some tidyup and fixed it to print top-to-bottom first like ls(1).


# 1.20 15-Nov-2010 christos

don't increment i twice in the loop. From Michael Byrnes


# 1.19 01-Jun-2010 christos

tidy up memory allocation and don't unnecessarily print "./" before names.


# 1.18 18-Jan-2010 christos

PR/42637: Joachim Kuebart: Shell tab completion crashes due to libedit stack
smashing


# 1.17 30-Dec-2009 christos

Wide character support (UTF-8) from Johny Mattsson; currently disabled.


# 1.16 28-Dec-2009 christos

improve on the listing display by printing only one character after the
filename not two, and no trailing blanks. I will revisit this when I write
the ls-F code.


Revision tags: matt-premerge-20091211 jym-xensuspend-nbase jym-xensuspend-base
# 1.15 16-Feb-2009 christos

fix sign compare issues.


# 1.14 15-Feb-2009 christos

pass lint on _LP64.


# 1.13 26-Jan-2009 apb

branches: 1.13.2;
Define HAVE_STRUCT_DIRENT_D_NAMLEN in config,h, and test it when
deciding whether to use entry->d_namlen or strlen(entry->d_name).
Addresses PR 40477 by Robert Millan.


# 1.12 11-Jan-2009 christos

- insert a space after the recognized string if it was an exact match
- initialize properly the string used for completion.
From Alex Bligh alex at alex dot org dot uk
- Make char constants consistent


Revision tags: netbsd-5-2-3-RELEASE netbsd-5-1-5-RELEASE netbsd-5-2-2-RELEASE netbsd-5-1-4-RELEASE netbsd-5-2-1-RELEASE netbsd-5-1-3-RELEASE netbsd-5-2-RELEASE netbsd-5-2-RC1 netbsd-5-1-2-RELEASE netbsd-5-1-1-RELEASE matt-nb5-mips64-premerge-20101231 matt-nb5-pq3-base netbsd-5-1-RELEASE netbsd-5-1-RC4 matt-nb5-mips64-k15 netbsd-5-1-RC3 netbsd-5-1-RC2 netbsd-5-1-RC1 netbsd-5-0-2-RELEASE matt-nb5-mips64-premerge-20091211 matt-nb5-mips64-u2-k2-k4-k7-k8-k9 matt-nb4-mips64-k7-u2a-k9b matt-nb5-mips64-u1-k1-k5 netbsd-5-0-1-RELEASE netbsd-5-0-RELEASE netbsd-5-0-RC4 netbsd-5-0-RC3 netbsd-5-0-RC2 netbsd-5-0-RC1 netbsd-5-base matt-mips64-base2 wrstuden-revivesa-base-3 wrstuden-revivesa-base-2 wrstuden-revivesa-base-1 yamt-pf42-base4 yamt-pf42-base3 hpcarm-cleanup-nbase yamt-pf42-base2 wrstuden-revivesa-base
# 1.11 29-Apr-2008 martin

Convert to new 2 clause license


Revision tags: netbsd-4-0-1-RELEASE wrstuden-fixsa-newbase yamt-pf42-baseX yamt-pf42-base keiichi-mipv6-base matt-armv6-nbase matt-armv6-prevmlocking wrstuden-fixsa-base-1 netbsd-4-0-RELEASE cube-autoconf-base netbsd-4-0-RC5 netbsd-4-0-RC4 netbsd-4-0-RC3 netbsd-4-0-RC2 netbsd-4-0-RC1 matt-armv6-base matt-mips64-base hpcarm-cleanup-base wrstuden-fixsa-base netbsd-4-base
# 1.10 09-Nov-2006 christos

branches: 1.10.16;
don't use alloca with ssp.


# 1.9 21-Aug-2006 christos

Change to a 3 clause copyright after permission of the holders.


Revision tags: abandoned-netbsd-4-base
# 1.8 03-Aug-2005 christos

Fix reversed test; from Gerry Swislow


# 1.7 11-Jun-2005 christos

PR/30500: Paul Shupak: Inconsistent definition of tilde_expand().
Provide a layer of indirection between the readline compatibility functions
and our internal implementation, so that we have the freedom to change the
function signature.


# 1.6 10-Jun-2005 christos

Bug reported from Martin Dietze:

The place to change the completion_append_character is
usually somewhere in the `rl_completion_entry_function'
callback which is where one usually can distinguish between
file- or dir-like entries to append a slash for dirs etc.

This does no longer work since `fn_complete()' takes the
`append_character' as argument before the callback is executed,
so that changes to the variable `rl_completion_append_character'
have in fact no effect for the current completion.

Fix by adding a function that returns the rl_completion_append_character,
when it gets passed in a filename in readline emulation.


# 1.5 18-May-2005 christos

Make completion_matches non-static since readline wants it.


# 1.4 12-May-2005 christos

PR/30215: Kouichirou Hiratsuka: /bin/sh dumps core with tabcomplete
Don't core-dump when trying to complete an empty line; instead assume ./


# 1.3 09-May-2005 dsl

Use getpwuid_r(getuid(), ...) to expand ~/....
Don't replace ~xyz with /home/xyz when expanding ~


# 1.2 07-May-2005 dsl

gdb directly calls filename_completion_function()


# 1.1 07-May-2005 dsl

Separate out the filename completion functions from the readline() code.
Pass in loads of parameters instead of relying on shed-loads of global
variables to modify the behaviour.
The filename completion code can now be enabled by code that uses el_gets().
(eg /bin/sh)


# 1.47 15-Oct-2017 abhinav

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.


# 1.46 16-Sep-2017 abhinav

Fix indentation (convert spaces to tab)


Revision tags: perseant-stdc-iso10646-base netbsd-8-base prg-localcount2-base3 prg-localcount2-base2 prg-localcount2-base1 prg-localcount2-base pgoyette-localcount-20170426 bouyer-socketcan-base1
# 1.45 21-Apr-2017 abhinav

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@


Revision tags: pgoyette-localcount-20170320 bouyer-socketcan-base pgoyette-localcount-20170107 pgoyette-localcount-20161104
# 1.44 31-Oct-2016 abhinav

branches: 1.44.2;
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.


Revision tags: localcount-20160914 pgoyette-localcount-20160806 pgoyette-localcount-20160726 pgoyette-localcount-base
# 1.43 11-Apr-2016 christos

branches: 1.43.2;
Char -> wchar_t from Ingo Schwarze.


# 1.42 11-Apr-2016 christos

more macro WIDECHAR undoing from Ingo Schwarze.


# 1.41 09-Apr-2016 christos

Change some 0's to NULL's from Pedro Giffuni


# 1.40 17-Feb-2016 christos

whitespace and header sorting changes (Ingo Schwarze). No functional changes.


# 1.39 16-Feb-2016 christos

More header cleanups from Ingo Schwarze.


# 1.38 16-Feb-2016 christos

more include file cleanup (Ingo Schwarze)


# 1.37 16-Feb-2016 christos

From Ingo Scharze:
Let "el.h" include everything needed for struct editline,
and don't include that stuff multiple times. That also improves
consistency, also avoids circular inclusions, and also makes it
easier to follow what is going on, even though not quite as nice.
But it seems like the best we can do...


# 1.36 16-Feb-2016 christos

cleanup chartype.h includes (Ingo Schwarze)


# 1.35 16-Feb-2016 christos

cleanup inclusion of histedit.h (Ingo Schwarze)


# 1.34 18-Oct-2014 riz

callers's -> caller's


# 1.33 18-Oct-2014 snj

src is too big these days to tolerate superfluous apostrophes. It's
"its", people!


Revision tags: netbsd-7-base tls-earlyentropy-base tls-maxphys-base
# 1.32 05-Jun-2014 christos

branches: 1.32.2;
PR/48876: Dmitriy Grigoryev: Core dump in readline lib on attempted expansion
Make sure we have 2 matches before calling strcmp().


Revision tags: netbsd-6-0-6-RELEASE netbsd-6-1-5-RELEASE yamt-pagecache-base9 yamt-pagecache-tag8 netbsd-6-1-4-RELEASE netbsd-6-0-5-RELEASE riastradh-xf86-video-intel-2-7-1-pre-2-21-15 riastradh-drm2-base3 netbsd-6-1-3-RELEASE netbsd-6-0-4-RELEASE netbsd-6-1-2-RELEASE netbsd-6-0-3-RELEASE netbsd-6-1-1-RELEASE riastradh-drm2-base2 riastradh-drm2-base1 riastradh-drm2-base netbsd-6-0-2-RELEASE netbsd-6-1-RELEASE netbsd-6-1-RC4 netbsd-6-1-RC3 agc-symver-base netbsd-6-1-RC2 netbsd-6-1-RC1 yamt-pagecache-base8 netbsd-6-0-1-RELEASE yamt-pagecache-base7 matt-nb6-plus-nbase yamt-pagecache-base6 netbsd-6-0-RELEASE netbsd-6-0-RC2 matt-nb6-plus-base netbsd-6-0-RC1 yamt-pagecache-base5 yamt-pagecache-base4 netbsd-6-base yamt-pagecache-base3 yamt-pagecache-base2 yamt-pagecache-base
# 1.31 16-Sep-2011 plunky

branches: 1.31.8; 1.31.18;
NULL does not need a cast, here


# 1.30 16-Aug-2011 christos

re-enable -Wconversion


# 1.29 29-Jul-2011 christos

pass -Wconversion


# 1.28 29-Jul-2011 christos

- fix unused params
- unconditionalize vis.h


# 1.27 29-Jul-2011 christos

KNF return (\1); -> return \1;


# 1.26 28-Jul-2011 christos

kill ptr_t and ioctl_t, add * sizeof(*foo) to all allocations.


# 1.25 28-Jul-2011 christos

fix unused variable warnings on systems without _r functions


# 1.24 28-Jul-2011 christos

eliminate alloca for portability
portable getpw{nam,uid}


Revision tags: cherry-xenmp-base bouyer-quota2-nbase bouyer-quota2-base matt-mips64-premerge-20101231
# 1.23 06-Dec-2010 dholland

Improve previous to avoid changing the interface of an externally
exposed function. (But note that this function is neither documented
nor declared in any installed header file, and it probably should not
be externally exposed.) Related to PR 44183, closes PR 44186.


# 1.22 02-Dec-2010 dholland

add const, from PR 44183.


# 1.21 02-Dec-2010 dholland

Fix up bodgy code for printing completion matches; it used to sometimes
skip entries, print (null), run off the end of the array, or occasionally
receive SIGSEGV, and now will, hopefully at least, do none of that.

Based in part on the patch in PR 44183 from Sergio Acereda; I also
did some tidyup and fixed it to print top-to-bottom first like ls(1).


# 1.20 15-Nov-2010 christos

don't increment i twice in the loop. From Michael Byrnes


# 1.19 01-Jun-2010 christos

tidy up memory allocation and don't unnecessarily print "./" before names.


# 1.18 18-Jan-2010 christos

PR/42637: Joachim Kuebart: Shell tab completion crashes due to libedit stack
smashing


# 1.17 30-Dec-2009 christos

Wide character support (UTF-8) from Johny Mattsson; currently disabled.


# 1.16 28-Dec-2009 christos

improve on the listing display by printing only one character after the
filename not two, and no trailing blanks. I will revisit this when I write
the ls-F code.


Revision tags: matt-premerge-20091211 jym-xensuspend-nbase jym-xensuspend-base
# 1.15 16-Feb-2009 christos

fix sign compare issues.


# 1.14 15-Feb-2009 christos

pass lint on _LP64.


# 1.13 26-Jan-2009 apb

branches: 1.13.2;
Define HAVE_STRUCT_DIRENT_D_NAMLEN in config,h, and test it when
deciding whether to use entry->d_namlen or strlen(entry->d_name).
Addresses PR 40477 by Robert Millan.


# 1.12 11-Jan-2009 christos

- insert a space after the recognized string if it was an exact match
- initialize properly the string used for completion.
From Alex Bligh alex at alex dot org dot uk
- Make char constants consistent


Revision tags: netbsd-5-2-3-RELEASE netbsd-5-1-5-RELEASE netbsd-5-2-2-RELEASE netbsd-5-1-4-RELEASE netbsd-5-2-1-RELEASE netbsd-5-1-3-RELEASE netbsd-5-2-RELEASE netbsd-5-2-RC1 netbsd-5-1-2-RELEASE netbsd-5-1-1-RELEASE matt-nb5-mips64-premerge-20101231 matt-nb5-pq3-base netbsd-5-1-RELEASE netbsd-5-1-RC4 matt-nb5-mips64-k15 netbsd-5-1-RC3 netbsd-5-1-RC2 netbsd-5-1-RC1 netbsd-5-0-2-RELEASE matt-nb5-mips64-premerge-20091211 matt-nb5-mips64-u2-k2-k4-k7-k8-k9 matt-nb4-mips64-k7-u2a-k9b matt-nb5-mips64-u1-k1-k5 netbsd-5-0-1-RELEASE netbsd-5-0-RELEASE netbsd-5-0-RC4 netbsd-5-0-RC3 netbsd-5-0-RC2 netbsd-5-0-RC1 netbsd-5-base matt-mips64-base2 wrstuden-revivesa-base-3 wrstuden-revivesa-base-2 wrstuden-revivesa-base-1 yamt-pf42-base4 yamt-pf42-base3 hpcarm-cleanup-nbase yamt-pf42-base2 wrstuden-revivesa-base
# 1.11 29-Apr-2008 martin

Convert to new 2 clause license


Revision tags: netbsd-4-0-1-RELEASE wrstuden-fixsa-newbase yamt-pf42-baseX yamt-pf42-base keiichi-mipv6-base matt-armv6-nbase matt-armv6-prevmlocking wrstuden-fixsa-base-1 netbsd-4-0-RELEASE cube-autoconf-base netbsd-4-0-RC5 netbsd-4-0-RC4 netbsd-4-0-RC3 netbsd-4-0-RC2 netbsd-4-0-RC1 matt-armv6-base matt-mips64-base hpcarm-cleanup-base wrstuden-fixsa-base netbsd-4-base
# 1.10 09-Nov-2006 christos

branches: 1.10.16;
don't use alloca with ssp.


# 1.9 21-Aug-2006 christos

Change to a 3 clause copyright after permission of the holders.


Revision tags: abandoned-netbsd-4-base
# 1.8 03-Aug-2005 christos

Fix reversed test; from Gerry Swislow


# 1.7 11-Jun-2005 christos

PR/30500: Paul Shupak: Inconsistent definition of tilde_expand().
Provide a layer of indirection between the readline compatibility functions
and our internal implementation, so that we have the freedom to change the
function signature.


# 1.6 10-Jun-2005 christos

Bug reported from Martin Dietze:

The place to change the completion_append_character is
usually somewhere in the `rl_completion_entry_function'
callback which is where one usually can distinguish between
file- or dir-like entries to append a slash for dirs etc.

This does no longer work since `fn_complete()' takes the
`append_character' as argument before the callback is executed,
so that changes to the variable `rl_completion_append_character'
have in fact no effect for the current completion.

Fix by adding a function that returns the rl_completion_append_character,
when it gets passed in a filename in readline emulation.


# 1.5 18-May-2005 christos

Make completion_matches non-static since readline wants it.


# 1.4 12-May-2005 christos

PR/30215: Kouichirou Hiratsuka: /bin/sh dumps core with tabcomplete
Don't core-dump when trying to complete an empty line; instead assume ./


# 1.3 09-May-2005 dsl

Use getpwuid_r(getuid(), ...) to expand ~/....
Don't replace ~xyz with /home/xyz when expanding ~


# 1.2 07-May-2005 dsl

gdb directly calls filename_completion_function()


# 1.1 07-May-2005 dsl

Separate out the filename completion functions from the readline() code.
Pass in loads of parameters instead of relying on shed-loads of global
variables to modify the behaviour.
The filename completion code can now be enabled by code that uses el_gets().
(eg /bin/sh)


# 1.46 16-Sep-2017 abhinav

Fix indentation (convert spaces to tab)


Revision tags: perseant-stdc-iso10646-base netbsd-8-base prg-localcount2-base3 prg-localcount2-base2 prg-localcount2-base1 prg-localcount2-base pgoyette-localcount-20170426 bouyer-socketcan-base1
# 1.45 21-Apr-2017 abhinav

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@


Revision tags: pgoyette-localcount-20170320 bouyer-socketcan-base pgoyette-localcount-20170107 pgoyette-localcount-20161104
# 1.44 31-Oct-2016 abhinav

branches: 1.44.2;
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.


Revision tags: localcount-20160914 pgoyette-localcount-20160806 pgoyette-localcount-20160726 pgoyette-localcount-base
# 1.43 11-Apr-2016 christos

branches: 1.43.2;
Char -> wchar_t from Ingo Schwarze.


# 1.42 11-Apr-2016 christos

more macro WIDECHAR undoing from Ingo Schwarze.


# 1.41 09-Apr-2016 christos

Change some 0's to NULL's from Pedro Giffuni


# 1.40 17-Feb-2016 christos

whitespace and header sorting changes (Ingo Schwarze). No functional changes.


# 1.39 16-Feb-2016 christos

More header cleanups from Ingo Schwarze.


# 1.38 16-Feb-2016 christos

more include file cleanup (Ingo Schwarze)


# 1.37 16-Feb-2016 christos

From Ingo Scharze:
Let "el.h" include everything needed for struct editline,
and don't include that stuff multiple times. That also improves
consistency, also avoids circular inclusions, and also makes it
easier to follow what is going on, even though not quite as nice.
But it seems like the best we can do...


# 1.36 16-Feb-2016 christos

cleanup chartype.h includes (Ingo Schwarze)


# 1.35 16-Feb-2016 christos

cleanup inclusion of histedit.h (Ingo Schwarze)


# 1.34 18-Oct-2014 riz

callers's -> caller's


# 1.33 18-Oct-2014 snj

src is too big these days to tolerate superfluous apostrophes. It's
"its", people!


Revision tags: netbsd-7-base tls-earlyentropy-base tls-maxphys-base
# 1.32 05-Jun-2014 christos

branches: 1.32.2;
PR/48876: Dmitriy Grigoryev: Core dump in readline lib on attempted expansion
Make sure we have 2 matches before calling strcmp().


Revision tags: netbsd-6-0-6-RELEASE netbsd-6-1-5-RELEASE yamt-pagecache-base9 yamt-pagecache-tag8 netbsd-6-1-4-RELEASE netbsd-6-0-5-RELEASE riastradh-xf86-video-intel-2-7-1-pre-2-21-15 riastradh-drm2-base3 netbsd-6-1-3-RELEASE netbsd-6-0-4-RELEASE netbsd-6-1-2-RELEASE netbsd-6-0-3-RELEASE netbsd-6-1-1-RELEASE riastradh-drm2-base2 riastradh-drm2-base1 riastradh-drm2-base netbsd-6-0-2-RELEASE netbsd-6-1-RELEASE netbsd-6-1-RC4 netbsd-6-1-RC3 agc-symver-base netbsd-6-1-RC2 netbsd-6-1-RC1 yamt-pagecache-base8 netbsd-6-0-1-RELEASE yamt-pagecache-base7 matt-nb6-plus-nbase yamt-pagecache-base6 netbsd-6-0-RELEASE netbsd-6-0-RC2 matt-nb6-plus-base netbsd-6-0-RC1 yamt-pagecache-base5 yamt-pagecache-base4 netbsd-6-base yamt-pagecache-base3 yamt-pagecache-base2 yamt-pagecache-base
# 1.31 16-Sep-2011 plunky

branches: 1.31.8; 1.31.18;
NULL does not need a cast, here


# 1.30 16-Aug-2011 christos

re-enable -Wconversion


# 1.29 29-Jul-2011 christos

pass -Wconversion


# 1.28 29-Jul-2011 christos

- fix unused params
- unconditionalize vis.h


# 1.27 29-Jul-2011 christos

KNF return (\1); -> return \1;


# 1.26 28-Jul-2011 christos

kill ptr_t and ioctl_t, add * sizeof(*foo) to all allocations.


# 1.25 28-Jul-2011 christos

fix unused variable warnings on systems without _r functions


# 1.24 28-Jul-2011 christos

eliminate alloca for portability
portable getpw{nam,uid}


Revision tags: cherry-xenmp-base bouyer-quota2-nbase bouyer-quota2-base matt-mips64-premerge-20101231
# 1.23 06-Dec-2010 dholland

Improve previous to avoid changing the interface of an externally
exposed function. (But note that this function is neither documented
nor declared in any installed header file, and it probably should not
be externally exposed.) Related to PR 44183, closes PR 44186.


# 1.22 02-Dec-2010 dholland

add const, from PR 44183.


# 1.21 02-Dec-2010 dholland

Fix up bodgy code for printing completion matches; it used to sometimes
skip entries, print (null), run off the end of the array, or occasionally
receive SIGSEGV, and now will, hopefully at least, do none of that.

Based in part on the patch in PR 44183 from Sergio Acereda; I also
did some tidyup and fixed it to print top-to-bottom first like ls(1).


# 1.20 15-Nov-2010 christos

don't increment i twice in the loop. From Michael Byrnes


# 1.19 01-Jun-2010 christos

tidy up memory allocation and don't unnecessarily print "./" before names.


# 1.18 18-Jan-2010 christos

PR/42637: Joachim Kuebart: Shell tab completion crashes due to libedit stack
smashing


# 1.17 30-Dec-2009 christos

Wide character support (UTF-8) from Johny Mattsson; currently disabled.


# 1.16 28-Dec-2009 christos

improve on the listing display by printing only one character after the
filename not two, and no trailing blanks. I will revisit this when I write
the ls-F code.


Revision tags: matt-premerge-20091211 jym-xensuspend-nbase jym-xensuspend-base
# 1.15 16-Feb-2009 christos

fix sign compare issues.


# 1.14 15-Feb-2009 christos

pass lint on _LP64.


# 1.13 26-Jan-2009 apb

branches: 1.13.2;
Define HAVE_STRUCT_DIRENT_D_NAMLEN in config,h, and test it when
deciding whether to use entry->d_namlen or strlen(entry->d_name).
Addresses PR 40477 by Robert Millan.


# 1.12 11-Jan-2009 christos

- insert a space after the recognized string if it was an exact match
- initialize properly the string used for completion.
From Alex Bligh alex at alex dot org dot uk
- Make char constants consistent


Revision tags: netbsd-5-2-3-RELEASE netbsd-5-1-5-RELEASE netbsd-5-2-2-RELEASE netbsd-5-1-4-RELEASE netbsd-5-2-1-RELEASE netbsd-5-1-3-RELEASE netbsd-5-2-RELEASE netbsd-5-2-RC1 netbsd-5-1-2-RELEASE netbsd-5-1-1-RELEASE matt-nb5-mips64-premerge-20101231 matt-nb5-pq3-base netbsd-5-1-RELEASE netbsd-5-1-RC4 matt-nb5-mips64-k15 netbsd-5-1-RC3 netbsd-5-1-RC2 netbsd-5-1-RC1 netbsd-5-0-2-RELEASE matt-nb5-mips64-premerge-20091211 matt-nb5-mips64-u2-k2-k4-k7-k8-k9 matt-nb4-mips64-k7-u2a-k9b matt-nb5-mips64-u1-k1-k5 netbsd-5-0-1-RELEASE netbsd-5-0-RELEASE netbsd-5-0-RC4 netbsd-5-0-RC3 netbsd-5-0-RC2 netbsd-5-0-RC1 netbsd-5-base matt-mips64-base2 wrstuden-revivesa-base-3 wrstuden-revivesa-base-2 wrstuden-revivesa-base-1 yamt-pf42-base4 yamt-pf42-base3 hpcarm-cleanup-nbase yamt-pf42-base2 wrstuden-revivesa-base
# 1.11 29-Apr-2008 martin

Convert to new 2 clause license


Revision tags: netbsd-4-0-1-RELEASE wrstuden-fixsa-newbase yamt-pf42-baseX yamt-pf42-base keiichi-mipv6-base matt-armv6-nbase matt-armv6-prevmlocking wrstuden-fixsa-base-1 netbsd-4-0-RELEASE cube-autoconf-base netbsd-4-0-RC5 netbsd-4-0-RC4 netbsd-4-0-RC3 netbsd-4-0-RC2 netbsd-4-0-RC1 matt-armv6-base matt-mips64-base hpcarm-cleanup-base wrstuden-fixsa-base netbsd-4-base
# 1.10 09-Nov-2006 christos

branches: 1.10.16;
don't use alloca with ssp.


# 1.9 21-Aug-2006 christos

Change to a 3 clause copyright after permission of the holders.


Revision tags: abandoned-netbsd-4-base
# 1.8 03-Aug-2005 christos

Fix reversed test; from Gerry Swislow


# 1.7 11-Jun-2005 christos

PR/30500: Paul Shupak: Inconsistent definition of tilde_expand().
Provide a layer of indirection between the readline compatibility functions
and our internal implementation, so that we have the freedom to change the
function signature.


# 1.6 10-Jun-2005 christos

Bug reported from Martin Dietze:

The place to change the completion_append_character is
usually somewhere in the `rl_completion_entry_function'
callback which is where one usually can distinguish between
file- or dir-like entries to append a slash for dirs etc.

This does no longer work since `fn_complete()' takes the
`append_character' as argument before the callback is executed,
so that changes to the variable `rl_completion_append_character'
have in fact no effect for the current completion.

Fix by adding a function that returns the rl_completion_append_character,
when it gets passed in a filename in readline emulation.


# 1.5 18-May-2005 christos

Make completion_matches non-static since readline wants it.


# 1.4 12-May-2005 christos

PR/30215: Kouichirou Hiratsuka: /bin/sh dumps core with tabcomplete
Don't core-dump when trying to complete an empty line; instead assume ./


# 1.3 09-May-2005 dsl

Use getpwuid_r(getuid(), ...) to expand ~/....
Don't replace ~xyz with /home/xyz when expanding ~


# 1.2 07-May-2005 dsl

gdb directly calls filename_completion_function()


# 1.1 07-May-2005 dsl

Separate out the filename completion functions from the readline() code.
Pass in loads of parameters instead of relying on shed-loads of global
variables to modify the behaviour.
The filename completion code can now be enabled by code that uses el_gets().
(eg /bin/sh)


Revision tags: prg-localcount2-base pgoyette-localcount-20170426 bouyer-socketcan-base1
# 1.45 21-Apr-2017 abhinav

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@


Revision tags: pgoyette-localcount-20170320 bouyer-socketcan-base pgoyette-localcount-20170107 pgoyette-localcount-20161104
# 1.44 31-Oct-2016 abhinav

branches: 1.44.2;
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.


Revision tags: localcount-20160914 pgoyette-localcount-20160806 pgoyette-localcount-20160726 pgoyette-localcount-base
# 1.43 11-Apr-2016 christos

branches: 1.43.2;
Char -> wchar_t from Ingo Schwarze.


# 1.42 11-Apr-2016 christos

more macro WIDECHAR undoing from Ingo Schwarze.


# 1.41 09-Apr-2016 christos

Change some 0's to NULL's from Pedro Giffuni


# 1.40 17-Feb-2016 christos

whitespace and header sorting changes (Ingo Schwarze). No functional changes.


# 1.39 16-Feb-2016 christos

More header cleanups from Ingo Schwarze.


# 1.38 16-Feb-2016 christos

more include file cleanup (Ingo Schwarze)


# 1.37 16-Feb-2016 christos

From Ingo Scharze:
Let "el.h" include everything needed for struct editline,
and don't include that stuff multiple times. That also improves
consistency, also avoids circular inclusions, and also makes it
easier to follow what is going on, even though not quite as nice.
But it seems like the best we can do...


# 1.36 16-Feb-2016 christos

cleanup chartype.h includes (Ingo Schwarze)


# 1.35 16-Feb-2016 christos

cleanup inclusion of histedit.h (Ingo Schwarze)


# 1.34 18-Oct-2014 riz

callers's -> caller's


# 1.33 18-Oct-2014 snj

src is too big these days to tolerate superfluous apostrophes. It's
"its", people!


Revision tags: netbsd-7-base tls-earlyentropy-base tls-maxphys-base
# 1.32 05-Jun-2014 christos

branches: 1.32.2;
PR/48876: Dmitriy Grigoryev: Core dump in readline lib on attempted expansion
Make sure we have 2 matches before calling strcmp().


Revision tags: netbsd-6-0-6-RELEASE netbsd-6-1-5-RELEASE yamt-pagecache-base9 yamt-pagecache-tag8 netbsd-6-1-4-RELEASE netbsd-6-0-5-RELEASE riastradh-xf86-video-intel-2-7-1-pre-2-21-15 riastradh-drm2-base3 netbsd-6-1-3-RELEASE netbsd-6-0-4-RELEASE netbsd-6-1-2-RELEASE netbsd-6-0-3-RELEASE netbsd-6-1-1-RELEASE riastradh-drm2-base2 riastradh-drm2-base1 riastradh-drm2-base netbsd-6-0-2-RELEASE netbsd-6-1-RELEASE netbsd-6-1-RC4 netbsd-6-1-RC3 agc-symver-base netbsd-6-1-RC2 netbsd-6-1-RC1 yamt-pagecache-base8 netbsd-6-0-1-RELEASE yamt-pagecache-base7 matt-nb6-plus-nbase yamt-pagecache-base6 netbsd-6-0-RELEASE netbsd-6-0-RC2 matt-nb6-plus-base netbsd-6-0-RC1 yamt-pagecache-base5 yamt-pagecache-base4 netbsd-6-base yamt-pagecache-base3 yamt-pagecache-base2 yamt-pagecache-base
# 1.31 16-Sep-2011 plunky

branches: 1.31.8; 1.31.18;
NULL does not need a cast, here


# 1.30 16-Aug-2011 christos

re-enable -Wconversion


# 1.29 29-Jul-2011 christos

pass -Wconversion


# 1.28 29-Jul-2011 christos

- fix unused params
- unconditionalize vis.h


# 1.27 29-Jul-2011 christos

KNF return (\1); -> return \1;


# 1.26 28-Jul-2011 christos

kill ptr_t and ioctl_t, add * sizeof(*foo) to all allocations.


# 1.25 28-Jul-2011 christos

fix unused variable warnings on systems without _r functions


# 1.24 28-Jul-2011 christos

eliminate alloca for portability
portable getpw{nam,uid}


Revision tags: cherry-xenmp-base bouyer-quota2-nbase bouyer-quota2-base matt-mips64-premerge-20101231
# 1.23 06-Dec-2010 dholland

Improve previous to avoid changing the interface of an externally
exposed function. (But note that this function is neither documented
nor declared in any installed header file, and it probably should not
be externally exposed.) Related to PR 44183, closes PR 44186.


# 1.22 02-Dec-2010 dholland

add const, from PR 44183.


# 1.21 02-Dec-2010 dholland

Fix up bodgy code for printing completion matches; it used to sometimes
skip entries, print (null), run off the end of the array, or occasionally
receive SIGSEGV, and now will, hopefully at least, do none of that.

Based in part on the patch in PR 44183 from Sergio Acereda; I also
did some tidyup and fixed it to print top-to-bottom first like ls(1).


# 1.20 15-Nov-2010 christos

don't increment i twice in the loop. From Michael Byrnes


# 1.19 01-Jun-2010 christos

tidy up memory allocation and don't unnecessarily print "./" before names.


# 1.18 18-Jan-2010 christos

PR/42637: Joachim Kuebart: Shell tab completion crashes due to libedit stack
smashing


# 1.17 30-Dec-2009 christos

Wide character support (UTF-8) from Johny Mattsson; currently disabled.


# 1.16 28-Dec-2009 christos

improve on the listing display by printing only one character after the
filename not two, and no trailing blanks. I will revisit this when I write
the ls-F code.


Revision tags: matt-premerge-20091211 jym-xensuspend-nbase jym-xensuspend-base
# 1.15 16-Feb-2009 christos

fix sign compare issues.


# 1.14 15-Feb-2009 christos

pass lint on _LP64.


# 1.13 26-Jan-2009 apb

branches: 1.13.2;
Define HAVE_STRUCT_DIRENT_D_NAMLEN in config,h, and test it when
deciding whether to use entry->d_namlen or strlen(entry->d_name).
Addresses PR 40477 by Robert Millan.


# 1.12 11-Jan-2009 christos

- insert a space after the recognized string if it was an exact match
- initialize properly the string used for completion.
From Alex Bligh alex at alex dot org dot uk
- Make char constants consistent


Revision tags: netbsd-5-2-3-RELEASE netbsd-5-1-5-RELEASE netbsd-5-2-2-RELEASE netbsd-5-1-4-RELEASE netbsd-5-2-1-RELEASE netbsd-5-1-3-RELEASE netbsd-5-2-RELEASE netbsd-5-2-RC1 netbsd-5-1-2-RELEASE netbsd-5-1-1-RELEASE matt-nb5-mips64-premerge-20101231 matt-nb5-pq3-base netbsd-5-1-RELEASE netbsd-5-1-RC4 matt-nb5-mips64-k15 netbsd-5-1-RC3 netbsd-5-1-RC2 netbsd-5-1-RC1 netbsd-5-0-2-RELEASE matt-nb5-mips64-premerge-20091211 matt-nb5-mips64-u2-k2-k4-k7-k8-k9 matt-nb4-mips64-k7-u2a-k9b matt-nb5-mips64-u1-k1-k5 netbsd-5-0-1-RELEASE netbsd-5-0-RELEASE netbsd-5-0-RC4 netbsd-5-0-RC3 netbsd-5-0-RC2 netbsd-5-0-RC1 netbsd-5-base matt-mips64-base2 wrstuden-revivesa-base-3 wrstuden-revivesa-base-2 wrstuden-revivesa-base-1 yamt-pf42-base4 yamt-pf42-base3 hpcarm-cleanup-nbase yamt-pf42-base2 wrstuden-revivesa-base
# 1.11 29-Apr-2008 martin

Convert to new 2 clause license


Revision tags: netbsd-4-0-1-RELEASE wrstuden-fixsa-newbase yamt-pf42-baseX yamt-pf42-base keiichi-mipv6-base matt-armv6-nbase matt-armv6-prevmlocking wrstuden-fixsa-base-1 netbsd-4-0-RELEASE cube-autoconf-base netbsd-4-0-RC5 netbsd-4-0-RC4 netbsd-4-0-RC3 netbsd-4-0-RC2 netbsd-4-0-RC1 matt-armv6-base matt-mips64-base hpcarm-cleanup-base wrstuden-fixsa-base netbsd-4-base
# 1.10 09-Nov-2006 christos

branches: 1.10.16;
don't use alloca with ssp.


# 1.9 21-Aug-2006 christos

Change to a 3 clause copyright after permission of the holders.


Revision tags: abandoned-netbsd-4-base
# 1.8 03-Aug-2005 christos

Fix reversed test; from Gerry Swislow


# 1.7 11-Jun-2005 christos

PR/30500: Paul Shupak: Inconsistent definition of tilde_expand().
Provide a layer of indirection between the readline compatibility functions
and our internal implementation, so that we have the freedom to change the
function signature.


# 1.6 10-Jun-2005 christos

Bug reported from Martin Dietze:

The place to change the completion_append_character is
usually somewhere in the `rl_completion_entry_function'
callback which is where one usually can distinguish between
file- or dir-like entries to append a slash for dirs etc.

This does no longer work since `fn_complete()' takes the
`append_character' as argument before the callback is executed,
so that changes to the variable `rl_completion_append_character'
have in fact no effect for the current completion.

Fix by adding a function that returns the rl_completion_append_character,
when it gets passed in a filename in readline emulation.


# 1.5 18-May-2005 christos

Make completion_matches non-static since readline wants it.


# 1.4 12-May-2005 christos

PR/30215: Kouichirou Hiratsuka: /bin/sh dumps core with tabcomplete
Don't core-dump when trying to complete an empty line; instead assume ./


# 1.3 09-May-2005 dsl

Use getpwuid_r(getuid(), ...) to expand ~/....
Don't replace ~xyz with /home/xyz when expanding ~


# 1.2 07-May-2005 dsl

gdb directly calls filename_completion_function()


# 1.1 07-May-2005 dsl

Separate out the filename completion functions from the readline() code.
Pass in loads of parameters instead of relying on shed-loads of global
variables to modify the behaviour.
The filename completion code can now be enabled by code that uses el_gets().
(eg /bin/sh)


Revision tags: pgoyette-localcount-20161104
# 1.44 31-Oct-2016 abhinav

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.


Revision tags: localcount-20160914 pgoyette-localcount-20160806 pgoyette-localcount-20160726 pgoyette-localcount-base
# 1.43 11-Apr-2016 christos

branches: 1.43.2;
Char -> wchar_t from Ingo Schwarze.


# 1.42 11-Apr-2016 christos

more macro WIDECHAR undoing from Ingo Schwarze.


# 1.41 09-Apr-2016 christos

Change some 0's to NULL's from Pedro Giffuni


# 1.40 17-Feb-2016 christos

whitespace and header sorting changes (Ingo Schwarze). No functional changes.


# 1.39 16-Feb-2016 christos

More header cleanups from Ingo Schwarze.


# 1.38 16-Feb-2016 christos

more include file cleanup (Ingo Schwarze)


# 1.37 16-Feb-2016 christos

From Ingo Scharze:
Let "el.h" include everything needed for struct editline,
and don't include that stuff multiple times. That also improves
consistency, also avoids circular inclusions, and also makes it
easier to follow what is going on, even though not quite as nice.
But it seems like the best we can do...


# 1.36 16-Feb-2016 christos

cleanup chartype.h includes (Ingo Schwarze)


# 1.35 16-Feb-2016 christos

cleanup inclusion of histedit.h (Ingo Schwarze)


# 1.34 18-Oct-2014 riz

callers's -> caller's


# 1.33 18-Oct-2014 snj

src is too big these days to tolerate superfluous apostrophes. It's
"its", people!


Revision tags: netbsd-7-base tls-earlyentropy-base tls-maxphys-base
# 1.32 05-Jun-2014 christos

branches: 1.32.2;
PR/48876: Dmitriy Grigoryev: Core dump in readline lib on attempted expansion
Make sure we have 2 matches before calling strcmp().


Revision tags: netbsd-6-0-6-RELEASE netbsd-6-1-5-RELEASE yamt-pagecache-base9 yamt-pagecache-tag8 netbsd-6-1-4-RELEASE netbsd-6-0-5-RELEASE riastradh-xf86-video-intel-2-7-1-pre-2-21-15 riastradh-drm2-base3 netbsd-6-1-3-RELEASE netbsd-6-0-4-RELEASE netbsd-6-1-2-RELEASE netbsd-6-0-3-RELEASE netbsd-6-1-1-RELEASE riastradh-drm2-base2 riastradh-drm2-base1 riastradh-drm2-base netbsd-6-0-2-RELEASE netbsd-6-1-RELEASE netbsd-6-1-RC4 netbsd-6-1-RC3 agc-symver-base netbsd-6-1-RC2 netbsd-6-1-RC1 yamt-pagecache-base8 netbsd-6-0-1-RELEASE yamt-pagecache-base7 matt-nb6-plus-nbase yamt-pagecache-base6 netbsd-6-0-RELEASE netbsd-6-0-RC2 matt-nb6-plus-base netbsd-6-0-RC1 yamt-pagecache-base5 yamt-pagecache-base4 netbsd-6-base yamt-pagecache-base3 yamt-pagecache-base2 yamt-pagecache-base
# 1.31 16-Sep-2011 plunky

branches: 1.31.8; 1.31.18;
NULL does not need a cast, here


# 1.30 16-Aug-2011 christos

re-enable -Wconversion


# 1.29 29-Jul-2011 christos

pass -Wconversion


# 1.28 29-Jul-2011 christos

- fix unused params
- unconditionalize vis.h


# 1.27 29-Jul-2011 christos

KNF return (\1); -> return \1;


# 1.26 28-Jul-2011 christos

kill ptr_t and ioctl_t, add * sizeof(*foo) to all allocations.


# 1.25 28-Jul-2011 christos

fix unused variable warnings on systems without _r functions


# 1.24 28-Jul-2011 christos

eliminate alloca for portability
portable getpw{nam,uid}


Revision tags: cherry-xenmp-base bouyer-quota2-nbase bouyer-quota2-base matt-mips64-premerge-20101231
# 1.23 06-Dec-2010 dholland

Improve previous to avoid changing the interface of an externally
exposed function. (But note that this function is neither documented
nor declared in any installed header file, and it probably should not
be externally exposed.) Related to PR 44183, closes PR 44186.


# 1.22 02-Dec-2010 dholland

add const, from PR 44183.


# 1.21 02-Dec-2010 dholland

Fix up bodgy code for printing completion matches; it used to sometimes
skip entries, print (null), run off the end of the array, or occasionally
receive SIGSEGV, and now will, hopefully at least, do none of that.

Based in part on the patch in PR 44183 from Sergio Acereda; I also
did some tidyup and fixed it to print top-to-bottom first like ls(1).


# 1.20 15-Nov-2010 christos

don't increment i twice in the loop. From Michael Byrnes


# 1.19 01-Jun-2010 christos

tidy up memory allocation and don't unnecessarily print "./" before names.


# 1.18 18-Jan-2010 christos

PR/42637: Joachim Kuebart: Shell tab completion crashes due to libedit stack
smashing


# 1.17 30-Dec-2009 christos

Wide character support (UTF-8) from Johny Mattsson; currently disabled.


# 1.16 28-Dec-2009 christos

improve on the listing display by printing only one character after the
filename not two, and no trailing blanks. I will revisit this when I write
the ls-F code.


Revision tags: matt-premerge-20091211 jym-xensuspend-nbase jym-xensuspend-base
# 1.15 16-Feb-2009 christos

fix sign compare issues.


# 1.14 15-Feb-2009 christos

pass lint on _LP64.


# 1.13 26-Jan-2009 apb

branches: 1.13.2;
Define HAVE_STRUCT_DIRENT_D_NAMLEN in config,h, and test it when
deciding whether to use entry->d_namlen or strlen(entry->d_name).
Addresses PR 40477 by Robert Millan.


# 1.12 11-Jan-2009 christos

- insert a space after the recognized string if it was an exact match
- initialize properly the string used for completion.
From Alex Bligh alex at alex dot org dot uk
- Make char constants consistent


Revision tags: netbsd-5-2-3-RELEASE netbsd-5-1-5-RELEASE netbsd-5-2-2-RELEASE netbsd-5-1-4-RELEASE netbsd-5-2-1-RELEASE netbsd-5-1-3-RELEASE netbsd-5-2-RELEASE netbsd-5-2-RC1 netbsd-5-1-2-RELEASE netbsd-5-1-1-RELEASE matt-nb5-mips64-premerge-20101231 matt-nb5-pq3-base netbsd-5-1-RELEASE netbsd-5-1-RC4 matt-nb5-mips64-k15 netbsd-5-1-RC3 netbsd-5-1-RC2 netbsd-5-1-RC1 netbsd-5-0-2-RELEASE matt-nb5-mips64-premerge-20091211 matt-nb5-mips64-u2-k2-k4-k7-k8-k9 matt-nb4-mips64-k7-u2a-k9b matt-nb5-mips64-u1-k1-k5 netbsd-5-0-1-RELEASE netbsd-5-0-RELEASE netbsd-5-0-RC4 netbsd-5-0-RC3 netbsd-5-0-RC2 netbsd-5-0-RC1 netbsd-5-base matt-mips64-base2 wrstuden-revivesa-base-3 wrstuden-revivesa-base-2 wrstuden-revivesa-base-1 yamt-pf42-base4 yamt-pf42-base3 hpcarm-cleanup-nbase yamt-pf42-base2 wrstuden-revivesa-base
# 1.11 29-Apr-2008 martin

Convert to new 2 clause license


Revision tags: netbsd-4-0-1-RELEASE wrstuden-fixsa-newbase yamt-pf42-baseX yamt-pf42-base keiichi-mipv6-base matt-armv6-nbase matt-armv6-prevmlocking wrstuden-fixsa-base-1 netbsd-4-0-RELEASE cube-autoconf-base netbsd-4-0-RC5 netbsd-4-0-RC4 netbsd-4-0-RC3 netbsd-4-0-RC2 netbsd-4-0-RC1 matt-armv6-base matt-mips64-base hpcarm-cleanup-base wrstuden-fixsa-base netbsd-4-base
# 1.10 09-Nov-2006 christos

branches: 1.10.16;
don't use alloca with ssp.


# 1.9 21-Aug-2006 christos

Change to a 3 clause copyright after permission of the holders.


Revision tags: abandoned-netbsd-4-base
# 1.8 03-Aug-2005 christos

Fix reversed test; from Gerry Swislow


# 1.7 11-Jun-2005 christos

PR/30500: Paul Shupak: Inconsistent definition of tilde_expand().
Provide a layer of indirection between the readline compatibility functions
and our internal implementation, so that we have the freedom to change the
function signature.


# 1.6 10-Jun-2005 christos

Bug reported from Martin Dietze:

The place to change the completion_append_character is
usually somewhere in the `rl_completion_entry_function'
callback which is where one usually can distinguish between
file- or dir-like entries to append a slash for dirs etc.

This does no longer work since `fn_complete()' takes the
`append_character' as argument before the callback is executed,
so that changes to the variable `rl_completion_append_character'
have in fact no effect for the current completion.

Fix by adding a function that returns the rl_completion_append_character,
when it gets passed in a filename in readline emulation.


# 1.5 18-May-2005 christos

Make completion_matches non-static since readline wants it.


# 1.4 12-May-2005 christos

PR/30215: Kouichirou Hiratsuka: /bin/sh dumps core with tabcomplete
Don't core-dump when trying to complete an empty line; instead assume ./


# 1.3 09-May-2005 dsl

Use getpwuid_r(getuid(), ...) to expand ~/....
Don't replace ~xyz with /home/xyz when expanding ~


# 1.2 07-May-2005 dsl

gdb directly calls filename_completion_function()


# 1.1 07-May-2005 dsl

Separate out the filename completion functions from the readline() code.
Pass in loads of parameters instead of relying on shed-loads of global
variables to modify the behaviour.
The filename completion code can now be enabled by code that uses el_gets().
(eg /bin/sh)