Skip to content

Commit

Permalink
Merge pull request #51 from rkraats/libedit-compatilibity
Browse files Browse the repository at this point in the history
Improve BSD editline compatibility
  • Loading branch information
bovine authored Aug 13, 2024
2 parents be509a8 + e6c2b96 commit 9915a12
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 11 deletions.
3 changes: 3 additions & 0 deletions config.h.in
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
/* Define the name of the executing macro variable in libreadline. */
#undef EXECUTING_MACRO_NAME

/* Define if rl_extend_line_buffer is resolved in libreadline. */
#undef EXTEND_LINE_BUFFER

/* Define to 1 if you have the <dlfcn.h> header file. */
#undef HAVE_DLFCN_H

Expand Down
31 changes: 31 additions & 0 deletions configure
Original file line number Diff line number Diff line change
Expand Up @@ -12689,6 +12689,37 @@ $as_echo "yes" >&6; };

$as_echo "#define CLEANUP_AFER_SIGNAL 1" >>confdefs.h

else
{ $as_echo "$as_me:${as_lineno-$LINENO}: result: no" >&5
$as_echo "no" >&6; }
fi
rm -f core conftest.err conftest.$ac_objext \
conftest$ac_exeext conftest.$ac_ext

# check for readline's rl_extend_line_buffer

{ $as_echo "$as_me:${as_lineno-$LINENO}: checking for rl_extend_line_buffer() in -lreadline" >&5
$as_echo_n "checking for rl_extend_line_buffer() in -lreadline... " >&6; }
cat confdefs.h - <<_ACEOF >conftest.$ac_ext
/* end confdefs.h. */
int
main ()
{
extern void rl_extend_line_buffer(int len);
rl_extend_line_buffer(1);
;
return 0;
}
_ACEOF
if ac_fn_c_try_link "$LINENO"; then :
{ $as_echo "$as_me:${as_lineno-$LINENO}: result: yes" >&5
$as_echo "yes" >&6; };

$as_echo "#define EXTEND_LINE_BUFFER 1" >>confdefs.h

else
{ $as_echo "$as_me:${as_lineno-$LINENO}: result: no" >&5
$as_echo "no" >&6; }
Expand Down
12 changes: 12 additions & 0 deletions configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,18 @@ AC_TRY_LINK(,[
[ Define if rl_cleanup_after_signal is resolved in libreadline. ]),
AC_MSG_RESULT(no))

# check for readline's rl_extend_line_buffer

AC_MSG_CHECKING([for rl_extend_line_buffer() in -lreadline])
AC_TRY_LINK(,[
extern void rl_extend_line_buffer(int len);
rl_extend_line_buffer(1);
],
AC_MSG_RESULT(yes);
AC_DEFINE(EXTEND_LINE_BUFFER, 1,
[ Define if rl_extend_line_buffer is resolved in libreadline. ]),
AC_MSG_RESULT(no))


AC_MSG_CHECKING([for the readline version number])
AC_TRY_RUN([
Expand Down
30 changes: 19 additions & 11 deletions tclreadline.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,13 @@
#endif


#ifdef EXTEND_LINE_BUFFER
/*
* this prototype may be missing
* in readline.h
*/
void rl_extend_line_buffer(int len);
#endif

#ifdef EXECUTING_MACRO_HACK
/**
Expand Down Expand Up @@ -524,29 +526,32 @@ TclReadlineLineCompleteHandler(char* ptr)
*/

char* expansion = (char*) NULL;
char* expand_output = (char*) NULL;
if (tclrl_use_history_expansion) {
int status = history_expand(ptr, &expansion);
int status = history_expand(ptr, &expand_output);

if (status >= 2) {
/* TODO: make this a valid tcl output */
printf("%s\n", expansion);
printf("%s\n", expand_output);
FREE(ptr);
FREE(expansion);
FREE(expand_output);
return;
} else if (status <= -1) {
Tcl_AppendResult
(tclrl_interp, "error in history expansion: ", expansion, "\n", (char*) NULL);
(tclrl_interp, "error in history expansion: ", expand_output, "\n", (char*) NULL);
TclReadlineTerminate(TCL_ERROR);
FREE(ptr);
FREE(expansion);
FREE(expand_output);
return;
} else {
Tcl_AppendResult(tclrl_interp, expansion, (char*) NULL);
} else if (status == 0) {
expansion = ptr;
} else { /* status == 1 */
expansion = expand_output;
}
} else {
Tcl_AppendResult(tclrl_interp, ptr, (char*) NULL);
expansion = ptr;
}
Tcl_AppendResult(tclrl_interp, expansion, (char*) NULL);

#ifdef EXECUTING_MACRO_NAME
/**
Expand Down Expand Up @@ -576,9 +581,7 @@ TclReadlineLineCompleteHandler(char* ptr)
*/
TclReadlineTerminate(LINE_COMPLETE);
FREE(ptr);
if (tclrl_use_history_expansion) {
FREE(expansion);
}
FREE(expand_output);
}
}

Expand Down Expand Up @@ -699,6 +702,10 @@ TclReadlineCompletion(char* text, int start, int end)
int status;
rl_completion_append_character = ' '; /* reset, just in case ... */

/* Only enable history expansion like '!!<TAB>' if the rl_extend_line_buffer
* function is available; e.g. libedit doesn't provide it, and alternative
* approaches to replace the line buffer don't give the desired behavior */
#ifdef EXTEND_LINE_BUFFER
if (tclrl_use_history_expansion && text && ('!' == text[0]
|| (start && rl_line_buffer[start - 1] == '!' /* for '$' */))) {
char* expansion = (char*) NULL;
Expand All @@ -720,6 +727,7 @@ TclReadlineCompletion(char* text, int start, int end)
}
FREE(expansion);
}
#endif

if (tclrl_custom_completer) {
char start_s[BUFSIZ], end_s[BUFSIZ];
Expand Down

0 comments on commit 9915a12

Please sign in to comment.