From: Dave Hart Date: Sun, 3 May 2009 01:47:51 +0000 (+0000) Subject: [Bug 1171] line editing in ntpq/ntpdc X-Git-Tag: NTP_4_2_5P171~3^2~5 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=495454b70c30ed1f1e809aab49e411a012f0a836;p=thirdparty%2Fntp.git [Bug 1171] line editing in ntpq/ntpdc [Bug 1173] NMEA PPSAPI fails on Solaris bk: 49fcf7c7BzZobbVdJuIWaq5RKyWb9A --- diff --git a/ChangeLog b/ChangeLog index f008ac99b..2413a7574 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,5 @@ +* [Bug 1171] line editing libs found without headers (Solaris 11) +* [Bug 1173] NMEA refclock fails with Solaris PPSAPI (4.2.5p170) 2009/05/02 Released by Harlan Stenn * [Bug 1152] adjust PARSE to new refclock_pps logic * Include (4.2.4p7-RC5) diff --git a/include/ntp_lineedit.h b/include/ntp_lineedit.h new file mode 100644 index 000000000..623c6f402 --- /dev/null +++ b/include/ntp_lineedit.h @@ -0,0 +1,13 @@ + +/* + * ntp_lineedit.h - generic interface to various line editing libs + */ + +int ntp_readline_init(const char *prompt); +void ntp_readline_uninit(void); + +/* + * strings returned by ntp_readline go home to free() + */ +char * ntp_readline(int *pcount); + diff --git a/libntp/Makefile.am b/libntp/Makefile.am index 790a8fb81..a7d78a408 100644 --- a/libntp/Makefile.am +++ b/libntp/Makefile.am @@ -8,7 +8,7 @@ libntp_a_SRCS = a_md5encrypt.c adjtime.c atoint.c atolfp.c atouint.c \ clocktypes.c decodenetnum.c dofptoa.c dolfptoa.c emalloc.c \ findconfig.c fptoa.c fptoms.c getopt.c hextoint.c \ hextolfp.c humandate.c icom.c inttoa.c iosignal.c \ - lib_strbuf.c machines.c memmove.c mfptoa.c \ + lib_strbuf.c machines.c memmove.c mfptoa.c ntp_lineedit.c \ mfptoms.c mktime.c modetoa.c mstolfp.c msutotsf.c msyslog.c netof.c \ ntp_rfc2553.c numtoa.c numtohost.c octtoint.c prettydate.c \ ntp_random.c recvbuff.c refnumtoa.c snprintf.c socktoa.c socktohost.c \ diff --git a/libntp/ntp_lineedit.c b/libntp/ntp_lineedit.c new file mode 100644 index 000000000..aa402308f --- /dev/null +++ b/libntp/ntp_lineedit.c @@ -0,0 +1,234 @@ +/* + * ntp_lineedit.c - generic interface to various line editing libs + */ +#ifdef HAVE_CONFIG_H + #include +#endif + +#include +#include +#include + +#if defined(HAVE_READLINE_HISTORY) + #include + #include +#else + #if defined(HAVE_HISTEDIT_H) + #include + #endif +#endif + +#include "ntp.h" +#include "ntp_lineedit.h" + +#define MAXEDITLINE 512 + +/* + * external references + */ + +extern char * progname; + +/* + * globals, private prototypes + */ + +static int ntp_readline_initted; +static char * lineedit_prompt; + + +#if !defined(HAVE_READLINE_HISTORY) && defined(HAVE_HISTEDIT_H) + + static EditLine * ntp_el; + static History * ntp_hist; + static HistEvent hev; + + char * ntp_prompt_callback(EditLine *); + +#endif /* !HAVE_READLINE_HISTORY_H && HAVE_HISTEDIT_H */ + + +/* + * ntp_readline_init - setup, set or reset prompt string + */ + +int +ntp_readline_init( + const char * prompt + ) +{ + int success; + + success = 1; + + if (prompt) { + if (lineedit_prompt) + free(lineedit_prompt); + lineedit_prompt = strdup(prompt); + } + + #if !defined(HAVE_READLINE_HISTORY) && defined(HAVE_HISTEDIT_H) + + if (NULL == ntp_el) { + + ntp_el = el_init(progname, stdin, stdout, stderr); + if (ntp_el) { + + el_set(ntp_el, EL_PROMPT, + ntp_prompt_callback); + el_set(ntp_el, EL_EDITOR, "emacs"); + + ntp_hist = history_init(); + + if (NULL == ntp_hist) { + + fprintf(stderr, + "history_init(): %s\n", + strerror(errno)); + fflush(stderr); + + el_end(ntp_el); + ntp_el = NULL; + + success = 0; + + } else { + memset(&hev, 0, sizeof hev); + + history(ntp_hist, &hev, + H_SETSIZE, 128); + + el_set(ntp_el, EL_HIST, + history, ntp_hist); + + /* use any .editrc */ + el_source(ntp_el, NULL); + } + + } else + success = 0; + } + + #endif /* !HAVE_READLINE_HISTORY && HAVE_HISTEDIT_H */ + + ntp_readline_initted = success; + + return success; +} + + +/* + * ntp_readline_uninit - release resources + */ + +void +ntp_readline_uninit( + void + ) +{ + #if !defined(HAVE_READLINE_HISTORY) && defined(HAVE_HISTEDIT_H) + + if (ntp_el) { + el_end(ntp_el); + ntp_el = NULL; + + history_end(ntp_hist); + ntp_hist = NULL; + } + + #endif /* !HAVE_READLINE_HISTORY && HAVE_HISTEDIT_H */ + + if (lineedit_prompt) { + free(lineedit_prompt); + lineedit_prompt = NULL; + } + + ntp_readline_initted = 0; +} + + +/* + * ntp_readline - read a line with the line editor available + * + * The string returned must be released with free() + */ + +char * +ntp_readline( + int * pcount + ) +{ + char * line; +#if !defined(HAVE_READLINE_HISTORY) && defined(HAVE_HISTEDIT_H) + const char * cline; +#endif + + if (!ntp_readline_initted) + return NULL; + + *pcount = 0; + + #if defined(HAVE_READLINE_HISTORY) + + line = readline(lineedit_prompt ? lineedit_prompt : ""); + if (NULL != line) { + if (*line) { + add_history(line); + *pcount = strlen(line); + } else { + free(line); + line = NULL; + } + } + + #endif + #if !defined(HAVE_READLINE_HISTORY) && defined(HAVE_HISTEDIT_H) + + cline = el_gets(ntp_el, pcount); + + if (NULL != cline && *cline) { + history(ntp_hist, &hev, H_ENTER, cline); + *pcount = strlen(cline); + line = strdup(cline); + } else + line = NULL; + + #else /* stone hammers */ + { + char line_buf[MAXEDITLINE]; + + if (lineedit_prompt) { + #ifdef VMS + /* work around problem mixing stdout & stderr */ + fputs("",stdout); + #endif + + fputs(lineedit_prompt, stderr); + fflush(stderr); + } + + line = fgets(line_buf, sizeof line_buf, stdin); + if (NULL != line && *line) { + *pcount = strlen(line); + line = strdup(line); + } else + line = NULL; + } + #endif /* stone hammers */ + + return line; +} + + +#if !defined(HAVE_READLINE_HISTORY) && defined(HAVE_HISTEDIT_H) + char * + ntp_prompt_callback( + EditLine *el + ) + { + UNUSED_ARG(el); + + return lineedit_prompt; + } +#endif /* !HAVE_READLINE_HISTORY_H && HAVE_HISTEDIT_H */ + diff --git a/m4/ntp_lineeditlibs.m4 b/m4/ntp_lineeditlibs.m4 index 07f24cdd9..99c79211e 100644 --- a/m4/ntp_lineeditlibs.m4 +++ b/m4/ntp_lineeditlibs.m4 @@ -1,5 +1,5 @@ AC_DEFUN([NTP_LINEEDITLIBS], [ - ORIG_LIBS="$LIBS" + NTP_ORIG_LIBS="$LIBS" AC_ARG_WITH([lineeditlibs], [AC_HELP_STRING([--with-lineeditlibs], [edit,editline (readline may be specified if desired)])], [use_lineeditlibs="$withval"], @@ -13,43 +13,51 @@ AC_DEFUN([NTP_LINEEDITLIBS], [ ;; *) for lineedit_lib in `echo $use_lineeditlibs | sed -e 's/,/ /'`; do - for term_lib in "" termcap curses ncurses; do - case "$term_lib" in - '') TRY_LIB="-l$lineedit_lib" ;; - *) TRY_LIB="-l$lineedit_lib -l$term_lib" ;; - esac - LIBS="$ORIG_LIBS $TRY_LIB" - AC_MSG_CHECKING([for readline() with $TRY_LIB]) - AC_TRY_LINK_FUNC([readline], [ntp_lib_lineedit="$TRY_LIB"]) - case "$ntp_lib_lineedit" in - '') - AC_MSG_RESULT([no]) - ;; - *) # Use readline() - AC_MSG_RESULT([yes]) - break - ;; - esac - AC_MSG_CHECKING([for el_gets() with $TRY_LIB]) - AC_TRY_LINK_FUNC([el_gets], [ntp_lib_lineedit="$TRY_LIB"]) + for term_lib in "" termcap curses ncurses; do + case "$term_lib" in + '') + TRY_LIB="-l$lineedit_lib" + ;; + *) TRY_LIB="-l$lineedit_lib -l$term_lib" + esac # $term_lib + LIBS="$NTP_ORIG_LIBS $TRY_LIB" + AC_MSG_CHECKING([for readline() with $TRY_LIB]) + AC_TRY_LINK_FUNC([readline], [ntp_lib_lineedit="$TRY_LIB"]) + case "$ntp_lib_lineedit" in + '') + AC_MSG_RESULT([no]) + ;; + *) + # Use readline() + AC_MSG_RESULT([yes]) + break + esac # $ntp_lib_lineedit + case "$term_lib" in + '') + # do not try el_gets without a terminal library + ;; + *) + AC_MSG_CHECKING([for el_gets() with $TRY_LIB]) + AC_TRY_LINK_FUNC([el_gets], [ntp_lib_lineedit="$TRY_LIB"]) + case "$ntp_lib_lineedit" in + '') + AC_MSG_RESULT([no]) + ;; + *) # Use el_gets() + AC_MSG_RESULT([yes]) + break + ;; + esac # $ntp_lib_lineedit + esac # $term_lib + done case "$ntp_lib_lineedit" in - '') - AC_MSG_RESULT([no]) - ;; - *) # Use el_gets() - AC_MSG_RESULT([yes]) - break - ;; + '') ;; + *) break ;; esac - done - case "$ntp_lib_lineedit" in - '') ;; - *) break ;; - esac done - LIBS="$ORIG_LIBS" + LIBS="$NTP_ORIG_LIBS" ;; - esac + esac # $use_lineeditlibs case "$ntp_lib_lineedit" in '') @@ -61,31 +69,23 @@ AC_DEFUN([NTP_LINEEDITLIBS], [ EDITLINE_LIBS="$ntp_lib_lineedit" AC_SUBST(EDITLINE_LIBS) ;; - esac + esac # $ntp_lib_lineedit case "$ntp_lib_lineedit" in no) ;; - -ledit) - AC_DEFINE(HAVE_LIBEDIT, 1, - [Define if you have libedit]) - # we want to also check for readline.h - AC_CHECK_HEADERS(histedit.h) - ;; - -leditline) - AC_MSG_WARN([editline is not yet supported]) - ;; *) - AC_DEFINE(HAVE_LIBREADLINE, 1, - [Define if you have a readline compatible library]) - AC_CHECK_HEADERS(readline.h readline/readline.h) + dnl AC_DEFINE(HAVE_LIBREADLINE, 1, + dnl [Define if you have a readline compatible library]) + AC_CHECK_HEADERS([readline.h readline/readline.h histedit.h]) + AC_CHECK_HEADERS([history.h readline/history.h]) AC_MSG_CHECKING([whether readline supports history]) ntp_lib_lineedit_history="no" ORIG_LIBS="$LIBS" LIBS="$ORIG_LIBS $ntp_lib_lineedit" - AC_TRY_LINK_FUNC(add_history, ntp_lib_lineedit_history="yes") + AC_TRY_LINK_FUNC([add_history], [ntp_lib_lineedit_history="yes"]) LIBS="$ORIG_LIBS" AC_MSG_RESULT([$ntp_lib_lineedit_history]) @@ -94,9 +94,9 @@ AC_DEFUN([NTP_LINEEDITLIBS], [ yes) AC_DEFINE(HAVE_READLINE_HISTORY, 1, [Define if your readline library has \`add_history']) - AC_CHECK_HEADERS(history.h readline/history.h) - ;; - esac - ;; - esac + esac # $ntp_lib_lineedit_history + esac # $ntp_lib_lineedit + dnl when oldest supported autoconf has AS_UNSET + dnl AS_UNSET([NTP_ORIG_LIBS TRY_LIB use_lineeditlibs]) + $as_unset NTP_ORIG_LIBS TRY_LIB use_lineeditlibs ])dnl diff --git a/ntpd/refclock_nmea.c b/ntpd/refclock_nmea.c index 67e9e77d8..5de620820 100644 --- a/ntpd/refclock_nmea.c +++ b/ntpd/refclock_nmea.c @@ -370,6 +370,7 @@ nmea_ppsapi( return (0); } memset(&up->pps_params, 0, sizeof(pps_params_t)); + up->pps_params.api_version = PPS_API_VERS_1; if (enb_clear) up->pps_params.mode = capability & PPS_CAPTURECLEAR; else diff --git a/ntpdc/ntpdc.c b/ntpdc/ntpdc.c index 74cd12a11..6e9950f57 100644 --- a/ntpdc/ntpdc.c +++ b/ntpdc/ntpdc.c @@ -12,6 +12,7 @@ #include "ntp_select.h" #include "ntp_io.h" #include "ntp_stdlib.h" +#include "ntp_lineedit.h" /* Don't include ISC's version of IPv6 variables and structures */ #define ISC_IPV6_H 1 #include "isc/net.h" @@ -30,15 +31,6 @@ # endif /* OPENSSL */ #endif /* SYS_WINNT */ -#if defined(HAVE_LIBREADLINE) -# include -# include -#endif /* HAVE_LIBREADLINE */ - -#if defined (HAVE_LIBEDIT) -# include -#endif /* HAVE_LIBEDIT */ - #ifdef SYS_VXWORKS /* vxWorks needs mode flag -casey*/ # define open(name, flags) open(name, flags, 0777) @@ -1124,33 +1116,20 @@ again: static void getcmds(void) { -#if defined(HAVE_LIBREADLINE) - char *line; + char * line; + int count; + + ntp_readline_init(interactive ? prompt : NULL); for (;;) { - if ((line = readline(interactive?prompt:"")) == NULL) return; - if (*line) add_history(line); + line = ntp_readline(&count); + if (NULL == line) + break; docmd(line); free(line); } -#else /* not (HAVE_LIBREADLINE) */ - char line[MAXLINE]; - for (;;) { - if (interactive) { -#ifdef VMS /* work around a problem with mixing stdout & stderr */ - fputs("",stdout); -#endif - (void) fputs(prompt, stderr); - (void) fflush(stderr); - } - - if (fgets(line, sizeof line, stdin) == NULL) - return; - - docmd(line); - } -#endif /* not HAVE_LIBREADLINE || HAVE_LIBEDIT */ + ntp_readline_uninit(); } diff --git a/ntpq/ntpq.c b/ntpq/ntpq.c index 5f7420085..428f4dc11 100644 --- a/ntpq/ntpq.c +++ b/ntpq/ntpq.c @@ -17,6 +17,7 @@ #include "ntp_select.h" #include "ntp_stdlib.h" #include "ntp_assert.h" +#include "ntp_lineedit.h" /* Don't include ISC's version of IPv6 variables and structures */ #define ISC_IPV6_H 1 #include "isc/net.h" @@ -35,15 +36,6 @@ # endif /* OPENSSL */ #endif /* SYS_WINNT */ -#if defined(HAVE_LIBREADLINE) -# include -# include -#endif /* HAVE_LIBREADLINE */ - -#if defined(HAVE_LIBEDIT) -# include -#endif /* HAVE_LIBEDIT */ - #ifdef SYS_VXWORKS /* vxWorks needs mode flag -casey*/ # define open(name, flags) open(name, flags, 0777) @@ -1455,36 +1447,24 @@ doquery( static void getcmds(void) { -#if defined(HAVE_LIBREADLINE) - char *line; + char * line; + int count; + + ntp_readline_init(interactive ? prompt : NULL); for (;;) { - if ((line = readline(interactive?prompt:"")) == NULL) return; - if (*line) add_history(line); + line = ntp_readline(&count); + if (NULL == line) + break; docmd(line); free(line); } -#else /* not (HAVE_LIBREADLINE) */ - char line[MAXLINE]; - for (;;) { - if (interactive) { -#ifdef VMS /* work around a problem with mixing stdout & stderr */ - fputs("",stdout); -#endif - (void) fputs(prompt, stderr); - (void) fflush(stderr); - } - - if (fgets(line, sizeof line, stdin) == NULL) - return; - - docmd(line); - } -#endif /* not (HAVE_LIBREADLINE || HAVE_LIBEDIT) */ + ntp_readline_uninit(); } #endif /* !BUILD_AS_LIB */ + #if !defined(SYS_WINNT) && !defined(BUILD_AS_LIB) /* * abortcmd - catch interrupts and abort the current command diff --git a/ports/winnt/libntp/libntp.vcproj b/ports/winnt/libntp/libntp.vcproj index 53f637ac3..97bfbe52a 100644 --- a/ports/winnt/libntp/libntp.vcproj +++ b/ports/winnt/libntp/libntp.vcproj @@ -1319,6 +1319,10 @@ /> + + @@ -2008,6 +2012,10 @@ RelativePath="..\include\ntp_iocompletionport.h" > + + diff --git a/ports/winnt/ntpdc/ntpdc.vcproj b/ports/winnt/ntpdc/ntpdc.vcproj index cee1866b7..739e5833c 100644 --- a/ports/winnt/ntpdc/ntpdc.vcproj +++ b/ports/winnt/ntpdc/ntpdc.vcproj @@ -316,6 +316,10 @@ Name="Header Files" Filter="h;hpp;hxx;hm;inl" > + + diff --git a/ports/winnt/ntpq/ntpq.vcproj b/ports/winnt/ntpq/ntpq.vcproj index 7803454f1..d044d65e9 100644 --- a/ports/winnt/ntpq/ntpq.vcproj +++ b/ports/winnt/ntpq/ntpq.vcproj @@ -310,6 +310,10 @@ Name="Header Files" Filter="h;hpp;hxx;hm;inl" > + +