diff --git a/examples/cli.c b/examples/cli.c index c9e0345..58cdbf5 100644 --- a/examples/cli.c +++ b/examples/cli.c @@ -84,8 +84,8 @@ int main(int ac __attribute__ ((unused)), char *av[] __attribute__ ((unused))) char *prompt = "cli> "; /* Setup callbacks */ - rl_complete = &my_rl_complete; - rl_list_possib = &my_rl_list_possib; + rl_set_complete_func(&my_rl_complete); + rl_set_list_possib_func(&my_rl_list_possib); while ((line = readline(prompt)) != NULL) { (void)printf("\t\t\t|%s|\n", line); diff --git a/include/editline.h b/include/editline.h index 06e7259..802700f 100644 --- a/include/editline.h +++ b/include/editline.h @@ -21,12 +21,18 @@ #ifndef __EDITLINE_H__ #define __EDITLINE_H__ +/* Editline specific types, despite rl_ prefix. From Heimdal project. */ +typedef char* (*rl_complete_func_t)(char*, int*); +typedef int (*rl_list_possib_func_t)(char*, char***); + /* Display print 8-bit chars as `M-x' or as the actual 8-bit char? (Default:1) */ extern int rl_meta_chars; -/* Assign these to get command completion, see cli.c for example usage. */ -extern char *(*rl_complete) (char *token, int *match); -extern int (*rl_list_possib)(char *token, char ***av); +/* Use these functions to set custom command/file completion, see cli.c for example usage. */ +rl_complete_func_t rl_set_complete_func(rl_complete_func_t func); +rl_list_possib_func_t rl_set_list_possib_func(rl_list_possib_func_t func); + +/* Editline specific functions. */ /* For compatibility with FSF readline. */ extern int rl_point; diff --git a/src/complete.c b/src/complete.c index fe70ecf..8900fa1 100644 --- a/src/complete.c +++ b/src/complete.c @@ -156,9 +156,19 @@ static int SplitPath(char *path, char **dirpart, char **filepart) return 0; } +static rl_complete_func_t el_complete_func = NULL; + +/* For compatibility with the Heimdal project. */ +rl_complete_func_t rl_set_complete_func(rl_complete_func_t func) +{ + rl_complete_func_t old = el_complete_func; + el_complete_func = func; + return old; +} + /* Attempt to complete the pathname, returning an allocated copy. - * Fill in *unique if we completed it, or set it to 0 if ambiguous. */ -char *default_rl_complete(char *pathname, int *unique) + * Fill in *match if we completed it, or set it to 0 if ambiguous. */ +char *el_filename_complete(char *pathname, int *match) { char **av; char *dir; @@ -185,7 +195,7 @@ char *default_rl_complete(char *pathname, int *unique) len = strlen(file); if (ac == 1) { /* Exactly one match -- finish it off. */ - *unique = 1; + *match = 1; j = strlen(av[0]) - len + 2; p = malloc(sizeof(char) * (j + 1)); if (p) { @@ -200,7 +210,7 @@ char *default_rl_complete(char *pathname, int *unique) } } else { - *unique = 0; + *match = 0; if (len) { /* Find largest matching substring. */ for (i = len, end = strlen(av[0]); i < end; i++) @@ -229,8 +239,30 @@ char *default_rl_complete(char *pathname, int *unique) return p; } -/* Return all possible completions. */ -int default_rl_list_possib(char *pathname, char ***avp) +char *rl_complete(char *token, int *match) +{ + if (el_complete_func) + return el_complete_func(token, match); + +#ifdef CONFIG_DEFAULT_COMPLETE + return el_filename_complete(token, match); +#else + return NULL; +#endif +} + +static rl_list_possib_func_t el_list_possib_func = NULL; + +/* For compatibility with the Heimdal project. */ +rl_list_possib_func_t rl_set_list_possib_func(rl_list_possib_func_t func) +{ + rl_list_possib_func_t old = el_list_possib_func; + el_list_possib_func = func; + return old; +} + +/* Default possible completions. */ +int el_filename_list_possib(char *pathname, char ***av) { char *dir; char *file; @@ -239,13 +271,27 @@ int default_rl_list_possib(char *pathname, char ***avp) if (SplitPath(pathname, &dir, &file) < 0) return 0; - ac = FindMatches(dir, file, avp); + ac = FindMatches(dir, file, av); free(dir); free(file); return ac; } +/* Return all possible completions. */ +int rl_list_possib(char *token, char ***av) +{ + if (el_list_possib_func) + return el_list_possib_func(token, av); + +#ifdef CONFIG_DEFAULT_COMPLETE + return el_filename_list_possib(token, av); +#else + return 0; +#endif +} + + /** * Local Variables: * version-control: t diff --git a/src/editline.c b/src/editline.c index 5bef448..1cf62e6 100644 --- a/src/editline.c +++ b/src/editline.c @@ -115,8 +115,6 @@ const char *rl_readline_name;/* Set by calling program, for conditional pa /* User definable callbacks. */ char **(*rl_attempted_completion_function)(const char *token, int start, int end); -char *(*rl_comlete)(char *token, int *match); -int (*rl_list_possib)(char *token, char ***av); /* Declarations. */ static char *editinput(void); @@ -1018,20 +1016,6 @@ void rl_reset_terminal(char *p __attribute__((__unused__))) void rl_initialize(void) { -#ifdef CONFIG_DEFAULT_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 *readline(const char *prompt) @@ -1155,10 +1139,6 @@ static el_status_t 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) @@ -1171,6 +1151,7 @@ static el_status_t c_possible(void) return CSmove; } + return ring_bell(); } @@ -1182,10 +1163,6 @@ static el_status_t c_complete(void) int unique; el_status_t s = 0; - if (!rl_complete) { - return ring_bell(); - } - word = find_word(); p = (char *)rl_complete((char *)word, &unique); if (word) diff --git a/src/editline.h b/src/editline.h index 8539927..ef165e2 100644 --- a/src/editline.h +++ b/src/editline.h @@ -73,12 +73,10 @@ extern int rl_quit; #ifdef CONFIG_SIGSTOP extern int rl_susp; #endif -#ifdef CONFIG_DEFAULT_COMPLETE -extern char *default_rl_complete(char *pathname, int *unique); -extern int default_rl_list_possib(char *pathname, char ***avp); -#endif -extern void rl_ttyset(int Reset); -extern void rl_add_slash(char *path, char *p); +void rl_ttyset(int Reset); +void rl_add_slash(char *path, char *p); +char *rl_complete(char *token, int *match); +int rl_list_possib(char *token, char ***av); #ifndef HAVE_STDLIB_H extern char *getenv(const char *name);