Basic support for custom completion handlers with the two "standard"

rl_complete() and rl_list_possib().  Simply leave out complete.o from
the default build and in all programs require these two functions to
be supplied.

A better alternative would be to use function pointers and check those
for NULL in the running code.  With this code, and no completion handler
the editline code will die.
This commit is contained in:
Joachim Nilsson
2008-10-02 01:52:40 +02:00
parent 4a3fbd9187
commit 5c9f0047bb
7 changed files with 164 additions and 13 deletions

View File

@@ -1,10 +1,10 @@
AUTOMAKE_OPTIONS = foreign
LDADD = $(top_builddir)/src/libedit.a
AM_CFLAGS = -I$(top_srcdir)/src
AM_CFLAGS = -I$(top_srcdir)/src -I$(top_srcdir)/include
# TODO: Port "fileman" example from BSD editline
noinst_PROGRAMS = testit
noinst_PROGRAMS = testit cli
testit_SOURCES = testit.c
#fileman_SOURCES = fileman.c

View File

@@ -30,7 +30,7 @@ POST_INSTALL = :
NORMAL_UNINSTALL = :
PRE_UNINSTALL = :
POST_UNINSTALL = :
noinst_PROGRAMS = testit$(EXEEXT)
noinst_PROGRAMS = testit$(EXEEXT) cli$(EXEEXT)
subdir = examples
DIST_COMMON = $(srcdir)/Makefile.am $(srcdir)/Makefile.in
ACLOCAL_M4 = $(top_srcdir)/aclocal.m4
@@ -41,6 +41,10 @@ mkinstalldirs = $(install_sh) -d
CONFIG_HEADER = $(top_builddir)/config.h
CONFIG_CLEAN_FILES =
PROGRAMS = $(noinst_PROGRAMS)
cli_SOURCES = cli.c
cli_OBJECTS = cli.$(OBJEXT)
cli_LDADD = $(LDADD)
cli_DEPENDENCIES = $(top_builddir)/src/libedit.a
am_testit_OBJECTS = testit.$(OBJEXT)
testit_OBJECTS = $(am_testit_OBJECTS)
testit_LDADD = $(LDADD)
@@ -52,8 +56,8 @@ COMPILE = $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) \
$(CPPFLAGS) $(AM_CFLAGS) $(CFLAGS)
CCLD = $(CC)
LINK = $(CCLD) $(AM_CFLAGS) $(CFLAGS) $(AM_LDFLAGS) $(LDFLAGS) -o $@
SOURCES = $(testit_SOURCES)
DIST_SOURCES = $(testit_SOURCES)
SOURCES = cli.c $(testit_SOURCES)
DIST_SOURCES = cli.c $(testit_SOURCES)
ETAGS = etags
CTAGS = ctags
DISTFILES = $(DIST_COMMON) $(DIST_SOURCES) $(TEXINFOS) $(EXTRA_DIST)
@@ -144,7 +148,7 @@ top_builddir = @top_builddir@
top_srcdir = @top_srcdir@
AUTOMAKE_OPTIONS = foreign
LDADD = $(top_builddir)/src/libedit.a
AM_CFLAGS = -I$(top_srcdir)/src
AM_CFLAGS = -I$(top_srcdir)/src -I$(top_srcdir)/include
testit_SOURCES = testit.c
all: all-am
@@ -182,6 +186,9 @@ $(ACLOCAL_M4): $(am__aclocal_m4_deps)
clean-noinstPROGRAMS:
-test -z "$(noinst_PROGRAMS)" || rm -f $(noinst_PROGRAMS)
cli$(EXEEXT): $(cli_OBJECTS) $(cli_DEPENDENCIES)
@rm -f cli$(EXEEXT)
$(LINK) $(cli_OBJECTS) $(cli_LDADD) $(LIBS)
testit$(EXEEXT): $(testit_OBJECTS) $(testit_DEPENDENCIES)
@rm -f testit$(EXEEXT)
$(LINK) $(testit_OBJECTS) $(testit_LDADD) $(LIBS)
@@ -192,6 +199,7 @@ mostlyclean-compile:
distclean-compile:
-rm -f *.tab.c
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/cli.Po@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/testit.Po@am__quote@
.c.o:

78
examples/cli.c Normal file
View File

@@ -0,0 +1,78 @@
/* The "testit" micro shell, now with command completion.
* To be able to run, don't "--enable-default-complete".
*/
#include "editline.h"
#include <string.h>
char *list[] = {
"foo ", "bar ", "bsd ", "cli ", "ls ", "cd ", "malloc ", "tee ", NULL
};
/*
** Attempt to complete the pathname, returning an allocated copy.
** Fill in *unique if we completed it, or set it to 0 if ambiguous.
*/
char *rl_complete(char *token, int *match)
{
int i;
int index = -1;
int matchlen = 0;
int count = 0;
for (i = 0; list[i]; i++)
{
int partlen = strlen (token); /* Part of token */
if (!strncmp (list[i], token, partlen))
{
index = i;
matchlen = partlen;
count ++;
}
}
if (count == 1)
{
*match = 1;
return strdup (list[index] + matchlen);
}
return NULL;
}
/*
** Return all possible completions.
*/
int rl_list_possib(char *token, char ***av)
{
int i, num, total = 0;
char **copy;
for (num = 0; list[num]; num++)
;
copy = (char **) malloc (num * sizeof(char *));
for (i = 0; i < num; i++)
{
if (!strncmp (list[i], token, strlen (token)))
{
copy[total] = strdup (list[i]);
total ++;
}
}
*av = copy;
return total;
}
int main(int ac, char *av[])
{
char *line;
char *prompt = "cli> ";
while ((line = readline(prompt)) != NULL) {
(void)printf("\t\t\t|%s|\n", line);
free(line);
}
return 0;
}