Change rl_complete() and rl_list_possib() to be function pointers instead.

This is a much cleaner design and also works with or without the configure
--enable-default-complete option.

See the examples for details.
This commit is contained in:
Joachim Nilsson 2008-10-02 09:09:09 +02:00
parent 5c9f0047bb
commit 62e900a061
11 changed files with 75 additions and 33 deletions

26
configure vendored
View File

@ -1,6 +1,6 @@
#! /bin/sh #! /bin/sh
# Guess values for system-dependent variables and create Makefiles. # Guess values for system-dependent variables and create Makefiles.
# Generated by GNU Autoconf 2.61 for Minix editline 0.2.1. # Generated by GNU Autoconf 2.61 for Minix editline 0.2.2.
# #
# Report bugs to <joachim@vmlinux.org>. # Report bugs to <joachim@vmlinux.org>.
# #
@ -574,8 +574,8 @@ SHELL=${CONFIG_SHELL-/bin/sh}
# Identity of this package. # Identity of this package.
PACKAGE_NAME='Minix editline' PACKAGE_NAME='Minix editline'
PACKAGE_TARNAME='minix-editline' PACKAGE_TARNAME='minix-editline'
PACKAGE_VERSION='0.2.1' PACKAGE_VERSION='0.2.2'
PACKAGE_STRING='Minix editline 0.2.1' PACKAGE_STRING='Minix editline 0.2.2'
PACKAGE_BUGREPORT='joachim@vmlinux.org' PACKAGE_BUGREPORT='joachim@vmlinux.org'
ac_unique_file="src/editline.c" ac_unique_file="src/editline.c"
@ -1210,7 +1210,7 @@ if test "$ac_init_help" = "long"; then
# Omit some internal or obsolete options to make the list less imposing. # Omit some internal or obsolete options to make the list less imposing.
# This message is too long to be a string in the A/UX 3.1 sh. # This message is too long to be a string in the A/UX 3.1 sh.
cat <<_ACEOF cat <<_ACEOF
\`configure' configures Minix editline 0.2.1 to adapt to many kinds of systems. \`configure' configures Minix editline 0.2.2 to adapt to many kinds of systems.
Usage: $0 [OPTION]... [VAR=VALUE]... Usage: $0 [OPTION]... [VAR=VALUE]...
@ -1276,7 +1276,7 @@ fi
if test -n "$ac_init_help"; then if test -n "$ac_init_help"; then
case $ac_init_help in case $ac_init_help in
short | recursive ) echo "Configuration of Minix editline 0.2.1:";; short | recursive ) echo "Configuration of Minix editline 0.2.2:";;
esac esac
cat <<\_ACEOF cat <<\_ACEOF
@ -1361,7 +1361,7 @@ fi
test -n "$ac_init_help" && exit $ac_status test -n "$ac_init_help" && exit $ac_status
if $ac_init_version; then if $ac_init_version; then
cat <<\_ACEOF cat <<\_ACEOF
Minix editline configure 0.2.1 Minix editline configure 0.2.2
generated by GNU Autoconf 2.61 generated by GNU Autoconf 2.61
Copyright (C) 1992, 1993, 1994, 1995, 1996, 1998, 1999, 2000, 2001, Copyright (C) 1992, 1993, 1994, 1995, 1996, 1998, 1999, 2000, 2001,
@ -1375,7 +1375,7 @@ cat >config.log <<_ACEOF
This file contains any messages produced by compilers while This file contains any messages produced by compilers while
running configure, to aid debugging if configure makes a mistake. running configure, to aid debugging if configure makes a mistake.
It was created by Minix editline $as_me 0.2.1, which was It was created by Minix editline $as_me 0.2.2, which was
generated by GNU Autoconf 2.61. Invocation command line was generated by GNU Autoconf 2.61. Invocation command line was
$ $0 $@ $ $0 $@
@ -2070,7 +2070,7 @@ fi
# Define the identity of the package. # Define the identity of the package.
PACKAGE='minix-editline' PACKAGE='minix-editline'
VERSION='0.2.1' VERSION='0.2.2'
cat >>confdefs.h <<_ACEOF cat >>confdefs.h <<_ACEOF
@ -4973,9 +4973,9 @@ fi
done done
# Check whether --enable-complete was given. # Check whether --enable-default-complete was given.
if test "${enable_complete+set}" = set; then if test "${enable_default_complete+set}" = set; then
enableval=$enable_complete; enableval=$enable_default_complete;
case "${enableval}" in case "${enableval}" in
yes) yes)
complete=true complete=true
@ -5421,7 +5421,7 @@ exec 6>&1
# report actual input values of CONFIG_FILES etc. instead of their # report actual input values of CONFIG_FILES etc. instead of their
# values after options handling. # values after options handling.
ac_log=" ac_log="
This file was extended by Minix editline $as_me 0.2.1, which was This file was extended by Minix editline $as_me 0.2.2, which was
generated by GNU Autoconf 2.61. Invocation command line was generated by GNU Autoconf 2.61. Invocation command line was
CONFIG_FILES = $CONFIG_FILES CONFIG_FILES = $CONFIG_FILES
@ -5474,7 +5474,7 @@ Report bugs to <bug-autoconf@gnu.org>."
_ACEOF _ACEOF
cat >>$CONFIG_STATUS <<_ACEOF cat >>$CONFIG_STATUS <<_ACEOF
ac_cs_version="\\ ac_cs_version="\\
Minix editline config.status 0.2.1 Minix editline config.status 0.2.2
configured by $0, generated by GNU Autoconf 2.61, configured by $0, generated by GNU Autoconf 2.61,
with options \\"`echo "$ac_configure_args" | sed 's/^ //; s/[\\""\`\$]/\\\\&/g'`\\" with options \\"`echo "$ac_configure_args" | sed 's/^ //; s/[\\""\`\$]/\\\\&/g'`\\"

View File

@ -2,7 +2,7 @@
# Process this file with autoconf to produce a configure script. # Process this file with autoconf to produce a configure script.
AC_PREREQ(2.61) AC_PREREQ(2.61)
AC_INIT(Minix editline, 0.2.1, joachim@vmlinux.org) AC_INIT(Minix editline, 0.2.2, joachim@vmlinux.org)
AC_CONFIG_SRCDIR([src/editline.c]) AC_CONFIG_SRCDIR([src/editline.c])
AC_CONFIG_HEADER([config.h]) AC_CONFIG_HEADER([config.h])
@ -40,7 +40,7 @@ AC_PROG_GCC_TRADITIONAL
AC_FUNC_STAT AC_FUNC_STAT
AC_CHECK_FUNCS([strchr strdup strrchr tcgetattr]) AC_CHECK_FUNCS([strchr strdup strrchr tcgetattr])
AC_ARG_ENABLE([complete], AC_ARG_ENABLE([default-complete],
[ --enable-default-complete Enable default completion handler.],[ [ --enable-default-complete Enable default completion handler.],[
case "${enableval}" in case "${enableval}" in
yes) yes)

View File

@ -5,6 +5,3 @@ AM_CFLAGS = -I$(top_srcdir)/src -I$(top_srcdir)/include
# TODO: Port "fileman" example from BSD editline # TODO: Port "fileman" example from BSD editline
noinst_PROGRAMS = testit cli noinst_PROGRAMS = testit cli
testit_SOURCES = testit.c
#fileman_SOURCES = fileman.c

View File

@ -45,8 +45,8 @@ cli_SOURCES = cli.c
cli_OBJECTS = cli.$(OBJEXT) cli_OBJECTS = cli.$(OBJEXT)
cli_LDADD = $(LDADD) cli_LDADD = $(LDADD)
cli_DEPENDENCIES = $(top_builddir)/src/libedit.a cli_DEPENDENCIES = $(top_builddir)/src/libedit.a
am_testit_OBJECTS = testit.$(OBJEXT) testit_SOURCES = testit.c
testit_OBJECTS = $(am_testit_OBJECTS) testit_OBJECTS = testit.$(OBJEXT)
testit_LDADD = $(LDADD) testit_LDADD = $(LDADD)
testit_DEPENDENCIES = $(top_builddir)/src/libedit.a testit_DEPENDENCIES = $(top_builddir)/src/libedit.a
DEFAULT_INCLUDES = -I.@am__isrc@ -I$(top_builddir) DEFAULT_INCLUDES = -I.@am__isrc@ -I$(top_builddir)
@ -56,8 +56,8 @@ COMPILE = $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) \
$(CPPFLAGS) $(AM_CFLAGS) $(CFLAGS) $(CPPFLAGS) $(AM_CFLAGS) $(CFLAGS)
CCLD = $(CC) CCLD = $(CC)
LINK = $(CCLD) $(AM_CFLAGS) $(CFLAGS) $(AM_LDFLAGS) $(LDFLAGS) -o $@ LINK = $(CCLD) $(AM_CFLAGS) $(CFLAGS) $(AM_LDFLAGS) $(LDFLAGS) -o $@
SOURCES = cli.c $(testit_SOURCES) SOURCES = cli.c testit.c
DIST_SOURCES = cli.c $(testit_SOURCES) DIST_SOURCES = cli.c testit.c
ETAGS = etags ETAGS = etags
CTAGS = ctags CTAGS = ctags
DISTFILES = $(DIST_COMMON) $(DIST_SOURCES) $(TEXINFOS) $(EXTRA_DIST) DISTFILES = $(DIST_COMMON) $(DIST_SOURCES) $(TEXINFOS) $(EXTRA_DIST)
@ -149,7 +149,6 @@ top_srcdir = @top_srcdir@
AUTOMAKE_OPTIONS = foreign AUTOMAKE_OPTIONS = foreign
LDADD = $(top_builddir)/src/libedit.a LDADD = $(top_builddir)/src/libedit.a
AM_CFLAGS = -I$(top_srcdir)/src -I$(top_srcdir)/include AM_CFLAGS = -I$(top_srcdir)/src -I$(top_srcdir)/include
testit_SOURCES = testit.c
all: all-am all: all-am
.SUFFIXES: .SUFFIXES:
@ -389,7 +388,6 @@ uninstall-am:
mostlyclean-generic pdf pdf-am ps ps-am tags uninstall \ mostlyclean-generic pdf pdf-am ps ps-am tags uninstall \
uninstall-am uninstall-am
#fileman_SOURCES = fileman.c
# Tell versions [3.59,3.63) of GNU make to not export all variables. # Tell versions [3.59,3.63) of GNU make to not export all variables.
# Otherwise a system limit (for SysV at least) may be exceeded. # Otherwise a system limit (for SysV at least) may be exceeded.
.NOEXPORT: .NOEXPORT:

View File

@ -1,6 +1,4 @@
/* The "testit" micro shell, now with command completion. /* Custom CLI command completion. */
* To be able to run, don't "--enable-default-complete".
*/
#include "editline.h" #include "editline.h"
#include <string.h> #include <string.h>
@ -12,7 +10,7 @@ char *list[] = {
** Attempt to complete the pathname, returning an allocated copy. ** Attempt to complete the pathname, returning an allocated copy.
** Fill in *unique if we completed it, or set it to 0 if ambiguous. ** Fill in *unique if we completed it, or set it to 0 if ambiguous.
*/ */
char *rl_complete(char *token, int *match) char *my_rl_complete(char *token, int *match)
{ {
int i; int i;
int index = -1; int index = -1;
@ -43,7 +41,7 @@ char *rl_complete(char *token, int *match)
/* /*
** Return all possible completions. ** Return all possible completions.
*/ */
int rl_list_possib(char *token, char ***av) int my_rl_list_possib(char *token, char ***av)
{ {
int i, num, total = 0; int i, num, total = 0;
char **copy; char **copy;
@ -69,6 +67,10 @@ int main(int ac, char *av[])
char *line; char *line;
char *prompt = "cli> "; char *prompt = "cli> ";
/* Setup callbacks */
rl_complete = &my_rl_complete;
rl_list_possib = &my_rl_list_possib;
while ((line = readline(prompt)) != NULL) { while ((line = readline(prompt)) != NULL) {
(void)printf("\t\t\t|%s|\n", line); (void)printf("\t\t\t|%s|\n", line);
free(line); free(line);

View File

@ -2,6 +2,11 @@
#ifndef __EDITLINE_H__ #ifndef __EDITLINE_H__
#define __EDITLINE_H__ #define __EDITLINE_H__
/* Assign these to get command completion, see cli.c for
* example usage. */
char *(*rl_complete)(char *token, int *match);
int (*rl_list_possib)(char *token, char ***av);
/* /*
** For compatibility with FSF readline. ** For compatibility with FSF readline.
*/ */

View File

@ -6,4 +6,5 @@ libedit_a_SOURCES = editline.c editline.h sysunix.c unix.h
if COMPLETE if COMPLETE
# Built-in completion handler. # Built-in completion handler.
libedit_a_SOURCES += complete.c libedit_a_SOURCES += complete.c
AM_CPPFLAGS = -DCOMPLETE
endif endif

View File

@ -161,6 +161,7 @@ AUTOMAKE_OPTIONS = foreign
lib_LIBRARIES = libedit.a lib_LIBRARIES = libedit.a
libedit_a_SOURCES = editline.c editline.h sysunix.c unix.h \ libedit_a_SOURCES = editline.c editline.h sysunix.c unix.h \
$(am__append_1) $(am__append_1)
@COMPLETE_TRUE@AM_CPPFLAGS = -DCOMPLETE
all: all-am all: all-am
.SUFFIXES: .SUFFIXES:

View File

@ -161,7 +161,7 @@ SplitPath(path, dirpart, filepart)
** Fill in *unique if we completed it, or set it to 0 if ambiguous. ** Fill in *unique if we completed it, or set it to 0 if ambiguous.
*/ */
char * char *
rl_complete(pathname, unique) default_rl_complete(pathname, unique)
char *pathname; char *pathname;
int *unique; int *unique;
{ {
@ -233,7 +233,7 @@ rl_complete(pathname, unique)
** Return all possible completions. ** Return all possible completions.
*/ */
int int
rl_list_possib(pathname, avp) default_rl_list_possib(pathname, avp)
char *pathname; char *pathname;
char ***avp; char ***avp;
{ {

View File

@ -97,6 +97,11 @@ STATIC int TTYrows;
/* Display print 8-bit chars as `M-x' or as the actual 8-bit char? */ /* Display print 8-bit chars as `M-x' or as the actual 8-bit char? */
int rl_meta_chars = 1; int rl_meta_chars = 1;
/* User definable callbacks. */
char *(*rl_complete)(char *token, int *match);
int (*rl_list_possib)(char *token, char ***av);
/* /*
** Declarations. ** Declarations.
*/ */
@ -1033,6 +1038,20 @@ rl_reset_terminal(p)
void void
rl_initialize(void) rl_initialize(void)
{ {
#ifdef COMPLETE
int done = 0;
if (!done)
{
if (!rl_complete)
rl_complete = &default_rl_complete;
if (!rl_list_possib)
rl_list_possib = &default_rl_list_possib;
done = 1;
}
#endif
} }
char * char *
@ -1042,6 +1061,9 @@ readline(prompt)
CHAR *line; CHAR *line;
int s; int s;
/* Unless called by the user already. */
rl_initialize ();
if (!isatty(0)) { if (!isatty(0)) {
TTYflush(); TTYflush();
return read_redirected(); return read_redirected();
@ -1177,6 +1199,10 @@ c_possible(void)
CHAR *word; CHAR *word;
int ac; int ac;
if (!rl_list_possib) {
return ring_bell();
}
word = find_word(); word = find_word();
ac = rl_list_possib((char *)word, (char ***)&av); ac = rl_list_possib((char *)word, (char ***)&av);
if (word) if (word)
@ -1200,6 +1226,10 @@ c_complete(void)
int unique; int unique;
STATUS s; STATUS s;
if (!rl_complete) {
return ring_bell();
}
word = find_word(); word = find_word();
p = (CHAR *)rl_complete((char *)word, &unique); p = (CHAR *)rl_complete((char *)word, &unique);
if (word) if (word)

View File

@ -2,6 +2,9 @@
** **
** Internal header file for editline library. ** Internal header file for editline library.
*/ */
#ifndef __PRIVATE_EDITLINE_H__
#define __PRIVATE_EDITLINE_H__
#include <config.h> #include <config.h>
#include <stdio.h> #include <stdio.h>
#ifdef HAVE_MALLOC_H #ifdef HAVE_MALLOC_H
@ -70,8 +73,10 @@ extern int rl_quit;
#if defined(DO_SIGTSTP) #if defined(DO_SIGTSTP)
extern int rl_susp; extern int rl_susp;
#endif /* defined(DO_SIGTSTP) */ #endif /* defined(DO_SIGTSTP) */
extern char *rl_complete(); #ifdef COMPLETE
extern int rl_list_possib(char *pathname, char ***avp); extern char *default_rl_complete();
extern int default_rl_list_possib(char *pathname, char ***avp);
#endif
extern void rl_ttyset(); extern void rl_ttyset();
extern void rl_add_slash(); extern void rl_add_slash();
@ -93,3 +98,6 @@ extern int strncmp();
#if defined(NEED_STRDUP) #if defined(NEED_STRDUP)
extern char *strdup(); extern char *strdup();
#endif #endif
#include "../include/editline.h"
#endif /* __PRIVATE_EDITLINE_H__ */