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
# 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>.
#
@ -574,8 +574,8 @@ SHELL=${CONFIG_SHELL-/bin/sh}
# Identity of this package.
PACKAGE_NAME='Minix editline'
PACKAGE_TARNAME='minix-editline'
PACKAGE_VERSION='0.2.1'
PACKAGE_STRING='Minix editline 0.2.1'
PACKAGE_VERSION='0.2.2'
PACKAGE_STRING='Minix editline 0.2.2'
PACKAGE_BUGREPORT='joachim@vmlinux.org'
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.
# This message is too long to be a string in the A/UX 3.1 sh.
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]...
@ -1276,7 +1276,7 @@ fi
if test -n "$ac_init_help"; then
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
cat <<\_ACEOF
@ -1361,7 +1361,7 @@ fi
test -n "$ac_init_help" && exit $ac_status
if $ac_init_version; then
cat <<\_ACEOF
Minix editline configure 0.2.1
Minix editline configure 0.2.2
generated by GNU Autoconf 2.61
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
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
$ $0 $@
@ -2070,7 +2070,7 @@ fi
# Define the identity of the package.
PACKAGE='minix-editline'
VERSION='0.2.1'
VERSION='0.2.2'
cat >>confdefs.h <<_ACEOF
@ -4973,9 +4973,9 @@ fi
done
# Check whether --enable-complete was given.
if test "${enable_complete+set}" = set; then
enableval=$enable_complete;
# Check whether --enable-default-complete was given.
if test "${enable_default_complete+set}" = set; then
enableval=$enable_default_complete;
case "${enableval}" in
yes)
complete=true
@ -5421,7 +5421,7 @@ exec 6>&1
# report actual input values of CONFIG_FILES etc. instead of their
# values after options handling.
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
CONFIG_FILES = $CONFIG_FILES
@ -5474,7 +5474,7 @@ Report bugs to <bug-autoconf@gnu.org>."
_ACEOF
cat >>$CONFIG_STATUS <<_ACEOF
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,
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.
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_HEADER([config.h])
@ -40,7 +40,7 @@ AC_PROG_GCC_TRADITIONAL
AC_FUNC_STAT
AC_CHECK_FUNCS([strchr strdup strrchr tcgetattr])
AC_ARG_ENABLE([complete],
AC_ARG_ENABLE([default-complete],
[ --enable-default-complete Enable default completion handler.],[
case "${enableval}" in
yes)

View File

@ -5,6 +5,3 @@ AM_CFLAGS = -I$(top_srcdir)/src -I$(top_srcdir)/include
# TODO: Port "fileman" example from BSD editline
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_LDADD = $(LDADD)
cli_DEPENDENCIES = $(top_builddir)/src/libedit.a
am_testit_OBJECTS = testit.$(OBJEXT)
testit_OBJECTS = $(am_testit_OBJECTS)
testit_SOURCES = testit.c
testit_OBJECTS = testit.$(OBJEXT)
testit_LDADD = $(LDADD)
testit_DEPENDENCIES = $(top_builddir)/src/libedit.a
DEFAULT_INCLUDES = -I.@am__isrc@ -I$(top_builddir)
@ -56,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 = cli.c $(testit_SOURCES)
DIST_SOURCES = cli.c $(testit_SOURCES)
SOURCES = cli.c testit.c
DIST_SOURCES = cli.c testit.c
ETAGS = etags
CTAGS = ctags
DISTFILES = $(DIST_COMMON) $(DIST_SOURCES) $(TEXINFOS) $(EXTRA_DIST)
@ -149,7 +149,6 @@ top_srcdir = @top_srcdir@
AUTOMAKE_OPTIONS = foreign
LDADD = $(top_builddir)/src/libedit.a
AM_CFLAGS = -I$(top_srcdir)/src -I$(top_srcdir)/include
testit_SOURCES = testit.c
all: all-am
.SUFFIXES:
@ -389,7 +388,6 @@ uninstall-am:
mostlyclean-generic pdf pdf-am ps ps-am tags uninstall \
uninstall-am
#fileman_SOURCES = fileman.c
# 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.
.NOEXPORT:

View File

@ -1,6 +1,4 @@
/* The "testit" micro shell, now with command completion.
* To be able to run, don't "--enable-default-complete".
*/
/* Custom CLI command completion. */
#include "editline.h"
#include <string.h>
@ -12,7 +10,7 @@ char *list[] = {
** 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)
char *my_rl_complete(char *token, int *match)
{
int i;
int index = -1;
@ -43,7 +41,7 @@ char *rl_complete(char *token, int *match)
/*
** 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;
char **copy;
@ -69,6 +67,10 @@ int main(int ac, char *av[])
char *line;
char *prompt = "cli> ";
/* Setup callbacks */
rl_complete = &my_rl_complete;
rl_list_possib = &my_rl_list_possib;
while ((line = readline(prompt)) != NULL) {
(void)printf("\t\t\t|%s|\n", line);
free(line);

View File

@ -2,6 +2,11 @@
#ifndef __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.
*/

View File

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

View File

@ -161,6 +161,7 @@ AUTOMAKE_OPTIONS = foreign
lib_LIBRARIES = libedit.a
libedit_a_SOURCES = editline.c editline.h sysunix.c unix.h \
$(am__append_1)
@COMPLETE_TRUE@AM_CPPFLAGS = -DCOMPLETE
all: all-am
.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.
*/
char *
rl_complete(pathname, unique)
default_rl_complete(pathname, unique)
char *pathname;
int *unique;
{
@ -233,7 +233,7 @@ rl_complete(pathname, unique)
** Return all possible completions.
*/
int
rl_list_possib(pathname, avp)
default_rl_list_possib(pathname, avp)
char *pathname;
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? */
int rl_meta_chars = 1;
/* User definable callbacks. */
char *(*rl_complete)(char *token, int *match);
int (*rl_list_possib)(char *token, char ***av);
/*
** Declarations.
*/
@ -1033,6 +1038,20 @@ rl_reset_terminal(p)
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 *
@ -1042,6 +1061,9 @@ readline(prompt)
CHAR *line;
int s;
/* Unless called by the user already. */
rl_initialize ();
if (!isatty(0)) {
TTYflush();
return read_redirected();
@ -1177,6 +1199,10 @@ c_possible(void)
CHAR *word;
int ac;
if (!rl_list_possib) {
return ring_bell();
}
word = find_word();
ac = rl_list_possib((char *)word, (char ***)&av);
if (word)
@ -1200,6 +1226,10 @@ c_complete(void)
int unique;
STATUS s;
if (!rl_complete) {
return ring_bell();
}
word = find_word();
p = (CHAR *)rl_complete((char *)word, &unique);
if (word)

View File

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