code:
# vasnprintf.m4 serial 36
dnl Copyright (C) 2002-2004, 2006-2017 Free Software Foundation, Inc.
dnl This file is free software; the Free Software Foundation
dnl gives unlimited permission to copy and/or distribute it,
dnl with or without modifications, as long as this notice is preserved.

AC_DEFUN([gl_FUNC_VASNPRINTF],
[
  AC_CHECK_FUNCS_ONCE([vasnprintf])
  if test $ac_cv_func_vasnprintf = no; then
    gl_REPLACE_VASNPRINTF
  fi
])

AC_DEFUN([gl_REPLACE_VASNPRINTF],
[
  AC_CHECK_FUNCS_ONCE([vasnprintf])
  AC_LIBOBJ([vasnprintf])
  AC_LIBOBJ([printf-args])
  AC_LIBOBJ([printf-parse])
  AC_LIBOBJ([asnprintf])
  if test $ac_cv_func_vasnprintf = yes; then
    AC_DEFINE([REPLACE_VASNPRINTF], [1],
      [Define if vasnprintf exists but is overridden by gnulib.])
  fi
  gl_PREREQ_PRINTF_ARGS
  gl_PREREQ_PRINTF_PARSE
  gl_PREREQ_VASNPRINTF
  gl_PREREQ_ASNPRINTF
])

# Prerequisites of lib/printf-args.h, lib/printf-args.c.
AC_DEFUN([gl_PREREQ_PRINTF_ARGS],
[
  AC_REQUIRE([AC_TYPE_LONG_LONG_INT])
  AC_REQUIRE([gt_TYPE_WCHAR_T])
  AC_REQUIRE([gt_TYPE_WINT_T])
])

# Prerequisites of lib/printf-parse.h, lib/printf-parse.c.
AC_DEFUN([gl_PREREQ_PRINTF_PARSE],
[
  AC_REQUIRE([gl_FEATURES_H])
  AC_REQUIRE([AC_TYPE_LONG_LONG_INT])
  AC_REQUIRE([gt_TYPE_WCHAR_T])
  AC_REQUIRE([gt_TYPE_WINT_T])
  AC_REQUIRE([AC_TYPE_SIZE_T])
  AC_CHECK_TYPE([ptrdiff_t], ,
    [AC_DEFINE([ptrdiff_t], [long],
       [Define as the type of the result of subtracting two pointers, if the system doesn't define it.])
    ])
  AC_REQUIRE([gt_AC_TYPE_INTMAX_T])
])

# Prerequisites of lib/vasnprintf.c.
AC_DEFUN_ONCE([gl_PREREQ_VASNPRINTF],
[
  AC_REQUIRE([AC_FUNC_ALLOCA])
  AC_REQUIRE([AC_TYPE_LONG_LONG_INT])
  AC_REQUIRE([gt_TYPE_WCHAR_T])
  AC_REQUIRE([gt_TYPE_WINT_T])
  AC_CHECK_FUNCS([snprintf strnlen wcslen wcsnlen mbrtowc wcrtomb])
  dnl Use the _snprintf function only if it is declared (because on NetBSD it
  dnl is defined as a weak alias of snprintf; we prefer to use the latter).
  AC_CHECK_DECLS([_snprintf], , , [[#include <stdio.h>]])
  dnl Knowing DBL_EXPBIT0_WORD and DBL_EXPBIT0_BIT enables an optimization
  dnl in the code for NEED_PRINTF_LONG_DOUBLE || NEED_PRINTF_DOUBLE.
  AC_REQUIRE([gl_DOUBLE_EXPONENT_LOCATION])
  dnl We can avoid a lot of code by assuming that snprintf's return value
  dnl conforms to ISO C99. So check that.
  AC_REQUIRE([gl_SNPRINTF_RETVAL_C99])
  case "$gl_cv_func_snprintf_retval_c99" in
    *yes)
      AC_DEFINE([HAVE_SNPRINTF_RETVAL_C99], [1],
        [Define if the return value of the snprintf function is the number of
         of bytes (excluding the terminating NUL) that would have been produced
         if the buffer had been large enough.])
      ;;
  esac
])

# Extra prerequisites of lib/vasnprintf.c for supporting 'long double'
# arguments.
AC_DEFUN_ONCE([gl_PREREQ_VASNPRINTF_LONG_DOUBLE],
[
  AC_REQUIRE([gl_PRINTF_LONG_DOUBLE])
  case "$gl_cv_func_printf_long_double" in
    *yes)
      ;;
    *)
      AC_DEFINE([NEED_PRINTF_LONG_DOUBLE], [1],
        [Define if the vasnprintf implementation needs special code for
         'long double' arguments.])
      ;;
  esac
])

# Extra prerequisites of lib/vasnprintf.c for supporting infinite 'double'
# arguments.
AC_DEFUN([gl_PREREQ_VASNPRINTF_INFINITE_DOUBLE],
[
  AC_REQUIRE([gl_PRINTF_INFINITE])
  case "$gl_cv_func_printf_infinite" in
    *yes)
      ;;
    *)
      AC_DEFINE([NEED_PRINTF_INFINITE_DOUBLE], [1],
        [Define if the vasnprintf implementation needs special code for
         infinite 'double' arguments.])
      ;;
  esac
])

# Extra prerequisites of lib/vasnprintf.c for supporting infinite 'long double'
# arguments.
AC_DEFUN([gl_PREREQ_VASNPRINTF_INFINITE_LONG_DOUBLE],
[
  AC_REQUIRE([gl_PRINTF_INFINITE_LONG_DOUBLE])
  dnl There is no need to set NEED_PRINTF_INFINITE_LONG_DOUBLE if
  dnl NEED_PRINTF_LONG_DOUBLE is already set.
  AC_REQUIRE([gl_PREREQ_VASNPRINTF_LONG_DOUBLE])
  case "$gl_cv_func_printf_long_double" in
    *yes)
      case "$gl_cv_func_printf_infinite_long_double" in
        *yes)
          ;;
        *)
          AC_DEFINE([NEED_PRINTF_INFINITE_LONG_DOUBLE], [1],
            [Define if the vasnprintf implementation needs special code for
             infinite 'long double' arguments.])
          ;;
      esac
      ;;
  esac
])

# Extra prerequisites of lib/vasnprintf.c for supporting the 'a' directive.
AC_DEFUN([gl_PREREQ_VASNPRINTF_DIRECTIVE_A],
[
  AC_REQUIRE([gl_PRINTF_DIRECTIVE_A])
  case "$gl_cv_func_printf_directive_a" in
    *yes)
      ;;
    *)
      AC_DEFINE([NEED_PRINTF_DIRECTIVE_A], [1],
        [Define if the vasnprintf implementation needs special code for
         the 'a' and 'A' directives.])
      AC_CHECK_FUNCS([nl_langinfo])
      ;;
  esac
])

# Extra prerequisites of lib/vasnprintf.c for supporting the 'F' directive.
AC_DEFUN([gl_PREREQ_VASNPRINTF_DIRECTIVE_F],
[
  AC_REQUIRE([gl_PRINTF_DIRECTIVE_F])
  case "$gl_cv_func_printf_directive_f" in
    *yes)
      ;;
    *)
      AC_DEFINE([NEED_PRINTF_DIRECTIVE_F], [1],
        [Define if the vasnprintf implementation needs special code for
         the 'F' directive.])
      ;;
  esac
])

# Extra prerequisites of lib/vasnprintf.c for supporting the 'ls' directive.
AC_DEFUN([gl_PREREQ_VASNPRINTF_DIRECTIVE_LS],
[
  AC_REQUIRE([gl_PRINTF_DIRECTIVE_LS])
  case "$gl_cv_func_printf_directive_ls" in
    *yes)
      ;;
    *)
      AC_DEFINE([NEED_PRINTF_DIRECTIVE_LS], [1],
        [Define if the vasnprintf implementation needs special code for
         the 'ls' directive.])
      ;;
  esac
])

# Extra prerequisites of lib/vasnprintf.c for supporting the ' flag.
AC_DEFUN([gl_PREREQ_VASNPRINTF_FLAG_GROUPING],
[
  AC_REQUIRE([gl_PRINTF_FLAG_GROUPING])
  case "$gl_cv_func_printf_flag_grouping" in
    *yes)
      ;;
    *)
      AC_DEFINE([NEED_PRINTF_FLAG_GROUPING], [1],
        [Define if the vasnprintf implementation needs special code for the
         ' flag.])
      ;;
  esac
])

# Extra prerequisites of lib/vasnprintf.c for supporting the '-' flag.
AC_DEFUN([gl_PREREQ_VASNPRINTF_FLAG_LEFTADJUST],
[
  AC_REQUIRE([gl_PRINTF_FLAG_LEFTADJUST])
  case "$gl_cv_func_printf_flag_leftadjust" in
    *yes)
      ;;
    *)
      AC_DEFINE([NEED_PRINTF_FLAG_LEFTADJUST], [1],
        [Define if the vasnprintf implementation needs special code for the
         '-' flag.])
      ;;
  esac
])

# Extra prerequisites of lib/vasnprintf.c for supporting the 0 flag.
AC_DEFUN([gl_PREREQ_VASNPRINTF_FLAG_ZERO],
[
  AC_REQUIRE([gl_PRINTF_FLAG_ZERO])
  case "$gl_cv_func_printf_flag_zero" in
    *yes)
      ;;
    *)
      AC_DEFINE([NEED_PRINTF_FLAG_ZERO], [1],
        [Define if the vasnprintf implementation needs special code for the
         0 flag.])
      ;;
  esac
])

# Extra prerequisites of lib/vasnprintf.c for supporting large precisions.
AC_DEFUN([gl_PREREQ_VASNPRINTF_PRECISION],
[
  AC_REQUIRE([gl_PRINTF_PRECISION])
  case "$gl_cv_func_printf_precision" in
    *yes)
      ;;
    *)
      AC_DEFINE([NEED_PRINTF_UNBOUNDED_PRECISION], [1],
        [Define if the vasnprintf implementation needs special code for
         supporting large precisions without arbitrary bounds.])
      AC_DEFINE([NEED_PRINTF_DOUBLE], [1],
        [Define if the vasnprintf implementation needs special code for
         'double' arguments.])
      AC_DEFINE([NEED_PRINTF_LONG_DOUBLE], [1],
        [Define if the vasnprintf implementation needs special code for
         'long double' arguments.])
      ;;
  esac
])

# Extra prerequisites of lib/vasnprintf.c for surviving out-of-memory
# conditions.
AC_DEFUN([gl_PREREQ_VASNPRINTF_ENOMEM],
[
  AC_REQUIRE([gl_PRINTF_ENOMEM])
  case "$gl_cv_func_printf_enomem" in
    *yes)
      ;;
    *)
      AC_DEFINE([NEED_PRINTF_ENOMEM], [1],
        [Define if the vasnprintf implementation needs special code for
         surviving out-of-memory conditions.])
      AC_DEFINE([NEED_PRINTF_DOUBLE], [1],
        [Define if the vasnprintf implementation needs special code for
         'double' arguments.])
      AC_DEFINE([NEED_PRINTF_LONG_DOUBLE], [1],
        [Define if the vasnprintf implementation needs special code for
         'long double' arguments.])
      ;;
  esac
])

# Prerequisites of lib/vasnprintf.c including all extras for POSIX compliance.
AC_DEFUN([gl_PREREQ_VASNPRINTF_WITH_EXTRAS],
[
  AC_REQUIRE([gl_PREREQ_VASNPRINTF])
  gl_PREREQ_VASNPRINTF_LONG_DOUBLE
  gl_PREREQ_VASNPRINTF_INFINITE_DOUBLE
  gl_PREREQ_VASNPRINTF_INFINITE_LONG_DOUBLE
  gl_PREREQ_VASNPRINTF_DIRECTIVE_A
  gl_PREREQ_VASNPRINTF_DIRECTIVE_F
  gl_PREREQ_VASNPRINTF_DIRECTIVE_LS
  gl_PREREQ_VASNPRINTF_FLAG_GROUPING
  gl_PREREQ_VASNPRINTF_FLAG_LEFTADJUST
  gl_PREREQ_VASNPRINTF_FLAG_ZERO
  gl_PREREQ_VASNPRINTF_PRECISION
  gl_PREREQ_VASNPRINTF_ENOMEM
])

# Prerequisites of lib/asnprintf.c.
AC_DEFUN([gl_PREREQ_ASNPRINTF],
[
])


code:
# vasprintf.m4 serial 6
dnl Copyright (C) 2002-2003, 2006-2007, 2009-2017 Free Software Foundation,
dnl Inc.
dnl This file is free software; the Free Software Foundation
dnl gives unlimited permission to copy and/or distribute it,
dnl with or without modifications, as long as this notice is preserved.

AC_DEFUN([gl_FUNC_VASPRINTF],
[
  AC_CHECK_FUNCS([vasprintf])
  if test $ac_cv_func_vasprintf = no; then
    gl_REPLACE_VASPRINTF
  fi
])

AC_DEFUN([gl_REPLACE_VASPRINTF],
[
  AC_LIBOBJ([vasprintf])
  AC_LIBOBJ([asprintf])
  AC_REQUIRE([gl_STDIO_H_DEFAULTS])
  if test $ac_cv_func_vasprintf = yes; then
    REPLACE_VASPRINTF=1
  else
    HAVE_VASPRINTF=0
  fi
  gl_PREREQ_VASPRINTF_H
  gl_PREREQ_VASPRINTF
  gl_PREREQ_ASPRINTF
])

# Prerequisites of the vasprintf portion of lib/stdio.h.
AC_DEFUN([gl_PREREQ_VASPRINTF_H],
[
  dnl Persuade glibc <stdio.h> to declare asprintf() and vasprintf().
  AC_REQUIRE([AC_USE_SYSTEM_EXTENSIONS])
])

# Prerequisites of lib/vasprintf.c.
AC_DEFUN([gl_PREREQ_VASPRINTF],
[
])

# Prerequisites of lib/asprintf.c.
AC_DEFUN([gl_PREREQ_ASPRINTF],
[
])

repo_name: mthl/texinfo

license: mit


code:
# visibility.m4 serial 5 (gettext-0.18.2)
dnl Copyright (C) 2005, 2008, 2010-2017 Free Software Foundation, Inc.
dnl This file is free software; the Free Software Foundation
dnl gives unlimited permission to copy and/or distribute it,
dnl with or without modifications, as long as this notice is preserved.

dnl From Bruno Haible.

dnl Tests whether the compiler supports the command-line option
dnl -fvisibility=hidden and the function and variable attributes
dnl __attribute__((__visibility__("hidden"))) and
dnl __attribute__((__visibility__("default"))).
dnl Does *not* test for __visibility__("protected") - which has tricky
dnl semantics (see the 'vismain' test in glibc) and does not exist e.g. on
dnl Mac OS X.
dnl Does *not* test for __visibility__("internal") - which has processor
dnl dependent semantics.
dnl Does *not* test for #pragma GCC visibility push(hidden) - which is
dnl "really only recommended for legacy code".
dnl Set the variable CFLAG_VISIBILITY.
dnl Defines and sets the variable HAVE_VISIBILITY.

AC_DEFUN([gl_VISIBILITY],
[
  AC_REQUIRE([AC_PROG_CC])
  CFLAG_VISIBILITY=
  HAVE_VISIBILITY=0
  if test -n "$GCC"; then
    dnl First, check whether -Werror can be added to the command line, or
    dnl whether it leads to an error because of some other option that the
    dnl user has put into $CC $CFLAGS $CPPFLAGS.
    AC_MSG_CHECKING([whether the -Werror option is usable])
    AC_CACHE_VAL([gl_cv_cc_vis_werror], [
      gl_save_CFLAGS="$CFLAGS"
      CFLAGS="$CFLAGS -Werror"
      AC_COMPILE_IFELSE(
        [AC_LANG_PROGRAM([[]], [[]])],
        [gl_cv_cc_vis_werror=yes],
        [gl_cv_cc_vis_werror=no])
      CFLAGS="$gl_save_CFLAGS"])
    AC_MSG_RESULT([$gl_cv_cc_vis_werror])
    dnl Now check whether visibility declarations are supported.
    AC_MSG_CHECKING([for simple visibility declarations])
    AC_CACHE_VAL([gl_cv_cc_visibility], [
      gl_save_CFLAGS="$CFLAGS"
      CFLAGS="$CFLAGS -fvisibility=hidden"
      dnl We use the option -Werror and a function dummyfunc, because on some
      dnl platforms (Cygwin 1.7) the use of -fvisibility triggers a warning
      dnl "visibility attribute not supported in this configuration; ignored"
      dnl at the first function definition in every compilation unit, and we
      dnl don't want to use the option in this case.
      if test $gl_cv_cc_vis_werror = yes; then
        CFLAGS="$CFLAGS -Werror"
      fi
      AC_COMPILE_IFELSE(
        [AC_LANG_PROGRAM(
           [[extern __attribute__((__visibility__("hidden"))) int hiddenvar;
             extern __attribute__((__visibility__("default"))) int exportedvar;
             extern __attribute__((__visibility__("hidden"))) int hiddenfunc (void);
             extern __attribute__((__visibility__("default"))) int exportedfunc (void);
             void dummyfunc (void) {}
           ]],
           [[]])],
        [gl_cv_cc_visibility=yes],
        [gl_cv_cc_visibility=no])
      CFLAGS="$gl_save_CFLAGS"])
    AC_MSG_RESULT([$gl_cv_cc_visibility])
    if test $gl_cv_cc_visibility = yes; then
      CFLAG_VISIBILITY="-fvisibility=hidden"
      HAVE_VISIBILITY=1
    fi
  fi
  AC_SUBST([CFLAG_VISIBILITY])
  AC_SUBST([HAVE_VISIBILITY])
  AC_DEFINE_UNQUOTED([HAVE_VISIBILITY], [$HAVE_VISIBILITY],
    [Define to 1 or 0, depending whether the compiler supports simple visibility declarations.])
])


code:
# warn-on-use.m4 serial 5
dnl Copyright (C) 2010-2017 Free Software Foundation, Inc.
dnl This file is free software; the Free Software Foundation
dnl gives unlimited permission to copy and/or distribute it,
dnl with or without modifications, as long as this notice is preserved.

# gl_WARN_ON_USE_PREPARE(INCLUDES, NAMES)
# ---------------------------------------
# For each whitespace-separated element in the list of NAMES, define
# HAVE_RAW_DECL_name if the function has a declaration among INCLUDES
# even after being undefined as a macro.
#
# See warn-on-use.h for some hints on how to poison function names, as
# well as ideas on poisoning global variables and macros.  NAMES may
# include global variables, but remember that only functions work with
# _GL_WARN_ON_USE.  Typically, INCLUDES only needs to list a single
# header, but if the replacement header pulls in other headers because
# some systems declare functions in the wrong header, then INCLUDES
# should do likewise.
#
# It is generally safe to assume declarations for functions declared
# in the intersection of C89 and C11 (such as printf) without
# needing gl_WARN_ON_USE_PREPARE.
AC_DEFUN([gl_WARN_ON_USE_PREPARE],
[
  m4_foreach_w([gl_decl], [$2],
    [AH_TEMPLATE([HAVE_RAW_DECL_]AS_TR_CPP(m4_defn([gl_decl])),
      [Define to 1 if ]m4_defn([gl_decl])[ is declared even after
       undefining macros.])])dnl
dnl FIXME: gl_Symbol must be used unquoted until we can assume
dnl autoconf 2.64 or newer.
  for gl_func in m4_flatten([$2]); do
    AS_VAR_PUSHDEF([gl_Symbol], [gl_cv_have_raw_decl_$gl_func])dnl
    AC_CACHE_CHECK([whether $gl_func is declared without a macro],
      gl_Symbol,
      [AC_COMPILE_IFELSE([AC_LANG_PROGRAM([$1],
[@%:@undef $gl_func
  (void) $gl_func;])],
        [AS_VAR_SET(gl_Symbol, [yes])], [AS_VAR_SET(gl_Symbol, [no])])])
    AS_VAR_IF(gl_Symbol, [yes],
      [AC_DEFINE_UNQUOTED(AS_TR_CPP([HAVE_RAW_DECL_$gl_func]), [1])
       dnl shortcut - if the raw declaration exists, then set a cache
       dnl variable to allow skipping any later AC_CHECK_DECL efforts
       eval ac_cv_have_decl_$gl_func=yes])
    AS_VAR_POPDEF([gl_Symbol])dnl
  done
])


code:
dnl A placeholder for ISO C99 <wchar.h>, for platforms that have issues.

dnl Copyright (C) 2007-2017 Free Software Foundation, Inc.
dnl This file is free software; the Free Software Foundation
dnl gives unlimited permission to copy and/or distribute it,
dnl with or without modifications, as long as this notice is preserved.

dnl Written by Eric Blake.

# wchar_h.m4 serial 40

AC_DEFUN([gl_WCHAR_H],
[
  AC_REQUIRE([gl_WCHAR_H_DEFAULTS])
  AC_REQUIRE([gl_WCHAR_H_INLINE_OK])
  dnl Prepare for creating substitute <wchar.h>.
  dnl Check for <wchar.h> (missing in Linux uClibc when built without wide
  dnl character support).
  dnl <wchar.h> is always overridden, because of GNULIB_POSIXCHECK.
  gl_CHECK_NEXT_HEADERS([wchar.h])
  if test $ac_cv_header_wchar_h = yes; then
    HAVE_WCHAR_H=1
  else
    HAVE_WCHAR_H=0
  fi
  AC_SUBST([HAVE_WCHAR_H])

  AC_REQUIRE([gl_FEATURES_H])

  AC_REQUIRE([gt_TYPE_WINT_T])
  if test $gt_cv_c_wint_t = yes; then
    HAVE_WINT_T=1
  else
    HAVE_WINT_T=0
  fi
  AC_SUBST([HAVE_WINT_T])

  dnl Check for declarations of anything we want to poison if the
  dnl corresponding gnulib module is not in use.
  gl_WARN_ON_USE_PREPARE([[
/* Tru64 with Desktop Toolkit C has a bug: <stdio.h> must be included before
   <wchar.h>.
   BSD/OS 4.0.1 has a bug: <stddef.h>, <stdio.h> and <time.h> must be
   included before <wchar.h>.  */
#if !(defined __GLIBC__ && !defined __UCLIBC__)
# include <stddef.h>
# include <stdio.h>
# include <time.h>
#endif
#include <wchar.h>
    ]],
    [btowc wctob mbsinit mbrtowc mbrlen mbsrtowcs mbsnrtowcs wcrtomb
     wcsrtombs wcsnrtombs wcwidth wmemchr wmemcmp wmemcpy wmemmove wmemset
     wcslen wcsnlen wcscpy wcpcpy wcsncpy wcpncpy wcscat wcsncat wcscmp
     wcsncmp wcscasecmp wcsncasecmp wcscoll wcsxfrm wcsdup wcschr wcsrchr
     wcscspn wcsspn wcspbrk wcsstr wcstok wcswidth
    ])
])

dnl Check whether <wchar.h> is usable at all.
AC_DEFUN([gl_WCHAR_H_INLINE_OK],
[
  dnl Test whether <wchar.h> suffers due to the transition from '__inline' to
  dnl 'gnu_inline'. See <http://sourceware.org/bugzilla/show_bug.cgi?id=4022>
  dnl and <http://gcc.gnu.org/bugzilla/show_bug.cgi?id=42440>. In summary,
  dnl glibc version 2.5 or older, together with gcc version 4.3 or newer and
  dnl the option -std=c99 or -std=gnu99, leads to a broken <wchar.h>.
  AC_CACHE_CHECK([whether <wchar.h> uses 'inline' correctly],
    [gl_cv_header_wchar_h_correct_inline],
    [gl_cv_header_wchar_h_correct_inline=yes
     AC_LANG_CONFTEST([
       AC_LANG_SOURCE([[#define wcstod renamed_wcstod
/* Tru64 with Desktop Toolkit C has a bug: <stdio.h> must be included before
   <wchar.h>.
   BSD/OS 4.0.1 has a bug: <stddef.h>, <stdio.h> and <time.h> must be
   included before <wchar.h>.  */
#include <stddef.h>
#include <stdio.h>
#include <time.h>
#include <wchar.h>
extern int zero (void);
int main () { return zero(); }
]])])
     dnl Do not rename the object file from conftest.$ac_objext to
     dnl conftest1.$ac_objext, as this will cause the link to fail on
     dnl z/OS when using the XPLINK object format (due to duplicate
     dnl CSECT names). Instead, temporarily redefine $ac_compile so
     dnl that the object file has the latter name from the start.
     save_ac_compile="$ac_compile"
     ac_compile=`echo "$save_ac_compile" | sed s/conftest/conftest1/`
     if AC_TRY_EVAL([ac_compile]); then
       AC_LANG_CONFTEST([
         AC_LANG_SOURCE([[#define wcstod renamed_wcstod
/* Tru64 with Desktop Toolkit C has a bug: <stdio.h> must be included before
   <wchar.h>.
   BSD/OS 4.0.1 has a bug: <stddef.h>, <stdio.h> and <time.h> must be
   included before <wchar.h>.  */
#include <stddef.h>
#include <stdio.h>
#include <time.h>
#include <wchar.h>
int zero (void) { return 0; }
]])])
       dnl See note above about renaming object files.
       ac_compile=`echo "$save_ac_compile" | sed s/conftest/conftest2/`
       if AC_TRY_EVAL([ac_compile]); then
         if $CC -o conftest$ac_exeext $CFLAGS $LDFLAGS conftest1.$ac_objext conftest2.$ac_objext $LIBS >&AS_MESSAGE_LOG_FD 2>&1; then
           :
         else
           gl_cv_header_wchar_h_correct_inline=no
         fi
       fi
     fi
     ac_compile="$save_ac_compile"
     rm -f conftest1.$ac_objext conftest2.$ac_objext conftest$ac_exeext
    ])
  if test $gl_cv_header_wchar_h_correct_inline = no; then
    AC_MSG_ERROR([<wchar.h> cannot be used with this compiler ($CC $CFLAGS $CPPFLAGS).
This is a known interoperability problem of glibc <= 2.5 with gcc >= 4.3 in
C99 mode. You have four options:
  - Add the flag -fgnu89-inline to CC and reconfigure, or
  - Fix your include files, using parts of
    <http://sourceware.org/git/?p=glibc.git;a=commitdiff;h=b037a293a48718af30d706c2e18c929d0e69a621>, or
  - Use a gcc version older than 4.3, or
  - Don't use the flags -std=c99 or -std=gnu99.
Configuration aborted.])
  fi
])

AC_DEFUN([gl_WCHAR_MODULE_INDICATOR],
[
  dnl Use AC_REQUIRE here, so that the default settings are expanded once only.
  AC_REQUIRE([gl_WCHAR_H_DEFAULTS])
  gl_MODULE_INDICATOR_SET_VARIABLE([$1])
  dnl Define it also as a C macro, for the benefit of the unit tests.
  gl_MODULE_INDICATOR_FOR_TESTS([$1])
])

AC_DEFUN([gl_WCHAR_H_DEFAULTS],
[
  GNULIB_BTOWC=0;       AC_SUBST([GNULIB_BTOWC])
  GNULIB_WCTOB=0;       AC_SUBST([GNULIB_WCTOB])
  GNULIB_MBSINIT=0;     AC_SUBST([GNULIB_MBSINIT])
  GNULIB_MBRTOWC=0;     AC_SUBST([GNULIB_MBRTOWC])
  GNULIB_MBRLEN=0;      AC_SUBST([GNULIB_MBRLEN])
  GNULIB_MBSRTOWCS=0;   AC_SUBST([GNULIB_MBSRTOWCS])
  GNULIB_MBSNRTOWCS=0;  AC_SUBST([GNULIB_MBSNRTOWCS])
  GNULIB_WCRTOMB=0;     AC_SUBST([GNULIB_WCRTOMB])
  GNULIB_WCSRTOMBS=0;   AC_SUBST([GNULIB_WCSRTOMBS])
  GNULIB_WCSNRTOMBS=0;  AC_SUBST([GNULIB_WCSNRTOMBS])
  GNULIB_WCWIDTH=0;     AC_SUBST([GNULIB_WCWIDTH])
  GNULIB_WMEMCHR=0;     AC_SUBST([GNULIB_WMEMCHR])
  GNULIB_WMEMCMP=0;     AC_SUBST([GNULIB_WMEMCMP])
  GNULIB_WMEMCPY=0;     AC_SUBST([GNULIB_WMEMCPY])
  GNULIB_WMEMMOVE=0;    AC_SUBST([GNULIB_WMEMMOVE])
  GNULIB_WMEMSET=0;     AC_SUBST([GNULIB_WMEMSET])
  GNULIB_WCSLEN=0;      AC_SUBST([GNULIB_WCSLEN])
  GNULIB_WCSNLEN=0;     AC_SUBST([GNULIB_WCSNLEN])
  GNULIB_WCSCPY=0;      AC_SUBST([GNULIB_WCSCPY])
  GNULIB_WCPCPY=0;      AC_SUBST([GNULIB_WCPCPY])
  GNULIB_WCSNCPY=0;     AC_SUBST([GNULIB_WCSNCPY])
  GNULIB_WCPNCPY=0;     AC_SUBST([GNULIB_WCPNCPY])
  GNULIB_WCSCAT=0;      AC_SUBST([GNULIB_WCSCAT])
  GNULIB_WCSNCAT=0;     AC_SUBST([GNULIB_WCSNCAT])
  GNULIB_WCSCMP=0;      AC_SUBST([GNULIB_WCSCMP])
  GNULIB_WCSNCMP=0;     AC_SUBST([GNULIB_WCSNCMP])
  GNULIB_WCSCASECMP=0;  AC_SUBST([GNULIB_WCSCASECMP])
  GNULIB_WCSNCASECMP=0; AC_SUBST([GNULIB_WCSNCASECMP])
  GNULIB_WCSCOLL=0;     AC_SUBST([GNULIB_WCSCOLL])
  GNULIB_WCSXFRM=0;     AC_SUBST([GNULIB_WCSXFRM])
  GNULIB_WCSDUP=0;      AC_SUBST([GNULIB_WCSDUP])
  GNULIB_WCSCHR=0;      AC_SUBST([GNULIB_WCSCHR])
  GNULIB_WCSRCHR=0;     AC_SUBST([GNULIB_WCSRCHR])
  GNULIB_WCSCSPN=0;     AC_SUBST([GNULIB_WCSCSPN])
  GNULIB_WCSSPN=0;      AC_SUBST([GNULIB_WCSSPN])
  GNULIB_WCSPBRK=0;     AC_SUBST([GNULIB_WCSPBRK])
  GNULIB_WCSSTR=0;      AC_SUBST([GNULIB_WCSSTR])
  GNULIB_WCSTOK=0;      AC_SUBST([GNULIB_WCSTOK])
  GNULIB_WCSWIDTH=0;    AC_SUBST([GNULIB_WCSWIDTH])
  dnl Assume proper GNU behavior unless another module says otherwise.
  HAVE_BTOWC=1;         AC_SUBST([HAVE_BTOWC])
  HAVE_MBSINIT=1;       AC_SUBST([HAVE_MBSINIT])
  HAVE_MBRTOWC=1;       AC_SUBST([HAVE_MBRTOWC])
  HAVE_MBRLEN=1;        AC_SUBST([HAVE_MBRLEN])
  HAVE_MBSRTOWCS=1;     AC_SUBST([HAVE_MBSRTOWCS])
  HAVE_MBSNRTOWCS=1;    AC_SUBST([HAVE_MBSNRTOWCS])
  HAVE_WCRTOMB=1;       AC_SUBST([HAVE_WCRTOMB])
  HAVE_WCSRTOMBS=1;     AC_SUBST([HAVE_WCSRTOMBS])
  HAVE_WCSNRTOMBS=1;    AC_SUBST([HAVE_WCSNRTOMBS])
  HAVE_WMEMCHR=1;       AC_SUBST([HAVE_WMEMCHR])
  HAVE_WMEMCMP=1;       AC_SUBST([HAVE_WMEMCMP])
  HAVE_WMEMCPY=1;       AC_SUBST([HAVE_WMEMCPY])
  HAVE_WMEMMOVE=1;      AC_SUBST([HAVE_WMEMMOVE])
  HAVE_WMEMSET=1;       AC_SUBST([HAVE_WMEMSET])
  HAVE_WCSLEN=1;        AC_SUBST([HAVE_WCSLEN])
  HAVE_WCSNLEN=1;       AC_SUBST([HAVE_WCSNLEN])
  HAVE_WCSCPY=1;        AC_SUBST([HAVE_WCSCPY])
  HAVE_WCPCPY=1;        AC_SUBST([HAVE_WCPCPY])
  HAVE_WCSNCPY=1;       AC_SUBST([HAVE_WCSNCPY])
  HAVE_WCPNCPY=1;       AC_SUBST([HAVE_WCPNCPY])
  HAVE_WCSCAT=1;        AC_SUBST([HAVE_WCSCAT])
  HAVE_WCSNCAT=1;       AC_SUBST([HAVE_WCSNCAT])
  HAVE_WCSCMP=1;        AC_SUBST([HAVE_WCSCMP])
  HAVE_WCSNCMP=1;       AC_SUBST([HAVE_WCSNCMP])
  HAVE_WCSCASECMP=1;    AC_SUBST([HAVE_WCSCASECMP])
  HAVE_WCSNCASECMP=1;   AC_SUBST([HAVE_WCSNCASECMP])
  HAVE_WCSCOLL=1;       AC_SUBST([HAVE_WCSCOLL])
  HAVE_WCSXFRM=1;       AC_SUBST([HAVE_WCSXFRM])
  HAVE_WCSDUP=1;        AC_SUBST([HAVE_WCSDUP])
  HAVE_WCSCHR=1;        AC_SUBST([HAVE_WCSCHR])
  HAVE_WCSRCHR=1;       AC_SUBST([HAVE_WCSRCHR])
  HAVE_WCSCSPN=1;       AC_SUBST([HAVE_WCSCSPN])
  HAVE_WCSSPN=1;        AC_SUBST([HAVE_WCSSPN])
  HAVE_WCSPBRK=1;       AC_SUBST([HAVE_WCSPBRK])
  HAVE_WCSSTR=1;        AC_SUBST([HAVE_WCSSTR])
  HAVE_WCSTOK=1;        AC_SUBST([HAVE_WCSTOK])
  HAVE_WCSWIDTH=1;      AC_SUBST([HAVE_WCSWIDTH])
  HAVE_DECL_WCTOB=1;    AC_SUBST([HAVE_DECL_WCTOB])
  HAVE_DECL_WCWIDTH=1;  AC_SUBST([HAVE_DECL_WCWIDTH])
  REPLACE_MBSTATE_T=0;  AC_SUBST([REPLACE_MBSTATE_T])
  REPLACE_BTOWC=0;      AC_SUBST([REPLACE_BTOWC])
  REPLACE_WCTOB=0;      AC_SUBST([REPLACE_WCTOB])
  REPLACE_MBSINIT=0;    AC_SUBST([REPLACE_MBSINIT])
  REPLACE_MBRTOWC=0;    AC_SUBST([REPLACE_MBRTOWC])
  REPLACE_MBRLEN=0;     AC_SUBST([REPLACE_MBRLEN])
  REPLACE_MBSRTOWCS=0;  AC_SUBST([REPLACE_MBSRTOWCS])
  REPLACE_MBSNRTOWCS=0; AC_SUBST([REPLACE_MBSNRTOWCS])
  REPLACE_WCRTOMB=0;    AC_SUBST([REPLACE_WCRTOMB])
  REPLACE_WCSRTOMBS=0;  AC_SUBST([REPLACE_WCSRTOMBS])
  REPLACE_WCSNRTOMBS=0; AC_SUBST([REPLACE_WCSNRTOMBS])
  REPLACE_WCWIDTH=0;    AC_SUBST([REPLACE_WCWIDTH])
  REPLACE_WCSWIDTH=0;   AC_SUBST([REPLACE_WCSWIDTH])
])


code:
# wchar_t.m4 serial 4 (gettext-0.18.2)
dnl Copyright (C) 2002-2003, 2008-2017 Free Software Foundation, Inc.
dnl This file is free software; the Free Software Foundation
dnl gives unlimited permission to copy and/or distribute it,
dnl with or without modifications, as long as this notice is preserved.

dnl From Bruno Haible.
dnl Test whether <stddef.h> has the 'wchar_t' type.
dnl Prerequisite: AC_PROG_CC

AC_DEFUN([gt_TYPE_WCHAR_T],
[
  AC_CACHE_CHECK([for wchar_t], [gt_cv_c_wchar_t],
    [AC_COMPILE_IFELSE(
       [AC_LANG_PROGRAM(
          [[#include <stddef.h>
            wchar_t foo = (wchar_t)'\0';]],
          [[]])],
       [gt_cv_c_wchar_t=yes],
       [gt_cv_c_wchar_t=no])])
  if test $gt_cv_c_wchar_t = yes; then
    AC_DEFINE([HAVE_WCHAR_T], [1], [Define if you have the 'wchar_t' type.])
  fi
])


code:
# wcrtomb.m4 serial 11
dnl Copyright (C) 2008-2017 Free Software Foundation, Inc.
dnl This file is free software; the Free Software Foundation
dnl gives unlimited permission to copy and/or distribute it,
dnl with or without modifications, as long as this notice is preserved.

AC_DEFUN([gl_FUNC_WCRTOMB],
[
  AC_REQUIRE([gl_WCHAR_H_DEFAULTS])

  AC_REQUIRE([AC_TYPE_MBSTATE_T])
  gl_MBSTATE_T_BROKEN

  AC_CHECK_FUNCS_ONCE([wcrtomb])
  if test $ac_cv_func_wcrtomb = no; then
    HAVE_WCRTOMB=0
    AC_CHECK_DECLS([wcrtomb],,, [[
/* Tru64 with Desktop Toolkit C has a bug: <stdio.h> must be included before
   <wchar.h>.
   BSD/OS 4.0.1 has a bug: <stddef.h>, <stdio.h> and <time.h> must be
   included before <wchar.h>.  */
#include <stddef.h>
#include <stdio.h>
#include <time.h>
#include <wchar.h>
]])
    if test $ac_cv_have_decl_wcrtomb = yes; then
      dnl On Minix 3.1.8, the system's <wchar.h> declares wcrtomb() although
      dnl it does not have the function. Avoid a collision with gnulib's
      dnl replacement.
      REPLACE_WCRTOMB=1
    fi
  else
    if test $REPLACE_MBSTATE_T = 1; then
      REPLACE_WCRTOMB=1
    else
      dnl On AIX 4.3, OSF/1 5.1 and Solaris 10, wcrtomb (NULL, 0, NULL) sometimes
      dnl returns 0 instead of 1.
      AC_REQUIRE([AC_PROG_CC])
      AC_REQUIRE([gt_LOCALE_FR])
      AC_REQUIRE([gt_LOCALE_FR_UTF8])
      AC_REQUIRE([gt_LOCALE_JA])
      AC_REQUIRE([gt_LOCALE_ZH_CN])
      AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
      AC_CACHE_CHECK([whether wcrtomb return value is correct],
        [gl_cv_func_wcrtomb_retval],
        [
          dnl Initial guess, used when cross-compiling or when no suitable locale
          dnl is present.
changequote(,)dnl
          case "$host_os" in
                                     # Guess no on AIX 4, OSF/1 and Solaris.
            aix4* | osf* | solaris*) gl_cv_func_wcrtomb_retval="guessing no" ;;
                                     # Guess yes otherwise.
            *)                       gl_cv_func_wcrtomb_retval="guessing yes" ;;
          esac
changequote([,])dnl
          if test $LOCALE_FR != none || test $LOCALE_FR_UTF8 != none || test $LOCALE_JA != none || test $LOCALE_ZH_CN != none; then
            AC_RUN_IFELSE(
              [AC_LANG_SOURCE([[
#include <locale.h>
#include <string.h>
/* Tru64 with Desktop Toolkit C has a bug: <stdio.h> must be included before
   <wchar.h>.
   BSD/OS 4.0.1 has a bug: <stddef.h>, <stdio.h> and <time.h> must be
   included before <wchar.h>.  */
#include <stddef.h>
#include <stdio.h>
#include <time.h>
#include <wchar.h>
int main ()
{
  int result = 0;
  if (setlocale (LC_ALL, "$LOCALE_FR") != NULL)
    {
      if (wcrtomb (NULL, 0, NULL) != 1)
        result |= 1;
    }
  if (setlocale (LC_ALL, "$LOCALE_FR_UTF8") != NULL)
    {
      if (wcrtomb (NULL, 0, NULL) != 1)
        result |= 2;
    }
  if (setlocale (LC_ALL, "$LOCALE_JA") != NULL)
    {
      if (wcrtomb (NULL, 0, NULL) != 1)
        result |= 4;
    }
  if (setlocale (LC_ALL, "$LOCALE_ZH_CN") != NULL)
    {
      if (wcrtomb (NULL, 0, NULL) != 1)
        result |= 8;
    }
  return result;
}]])],
              [gl_cv_func_wcrtomb_retval=yes],
              [gl_cv_func_wcrtomb_retval=no],
              [:])
          fi
        ])
      case "$gl_cv_func_wcrtomb_retval" in
        *yes) ;;
        *) REPLACE_WCRTOMB=1 ;;
      esac
    fi
  fi
])

# Prerequisites of lib/wcrtomb.c.
AC_DEFUN([gl_PREREQ_WCRTOMB], [
  :
])


code:
# wctype_h.m4 serial 19

dnl A placeholder for ISO C99 <wctype.h>, for platforms that lack it.

dnl Copyright (C) 2006-2017 Free Software Foundation, Inc.
dnl This file is free software; the Free Software Foundation
dnl gives unlimited permission to copy and/or distribute it,
dnl with or without modifications, as long as this notice is preserved.

dnl Written by Paul Eggert.

AC_DEFUN([gl_WCTYPE_H],
[
  AC_REQUIRE([gl_WCTYPE_H_DEFAULTS])
  AC_REQUIRE([AC_PROG_CC])
  AC_REQUIRE([AC_CANONICAL_HOST])
  AC_CHECK_FUNCS_ONCE([iswcntrl])
  if test $ac_cv_func_iswcntrl = yes; then
    HAVE_ISWCNTRL=1
  else
    HAVE_ISWCNTRL=0
  fi
  AC_SUBST([HAVE_ISWCNTRL])

  AC_REQUIRE([gt_TYPE_WINT_T])
  if test $gt_cv_c_wint_t = yes; then
    HAVE_WINT_T=1
  else
    HAVE_WINT_T=0
  fi
  AC_SUBST([HAVE_WINT_T])

  gl_CHECK_NEXT_HEADERS([wctype.h])
  if test $ac_cv_header_wctype_h = yes; then
    if test $ac_cv_func_iswcntrl = yes; then
      dnl Linux libc5 has an iswprint function that returns 0 for all arguments.
      dnl The other functions are likely broken in the same way.
      AC_CACHE_CHECK([whether iswcntrl works], [gl_cv_func_iswcntrl_works],
        [
          AC_RUN_IFELSE(
            [AC_LANG_SOURCE([[
               /* Tru64 with Desktop Toolkit C has a bug: <stdio.h> must be
                  included before <wchar.h>.
                  BSD/OS 4.0.1 has a bug: <stddef.h>, <stdio.h> and <time.h>
                  must be included before <wchar.h>.  */
               #include <stddef.h>
               #include <stdio.h>
               #include <time.h>
               #include <wchar.h>
               #include <wctype.h>
               int main () { return iswprint ('x') == 0; }
            ]])],
            [gl_cv_func_iswcntrl_works=yes], [gl_cv_func_iswcntrl_works=no],
            [AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[#include <stdlib.h>
                          #if __GNU_LIBRARY__ == 1
                          Linux libc5 i18n is broken.
                          #endif]], [])],
              [gl_cv_func_iswcntrl_works="guessing yes"],
              [gl_cv_func_iswcntrl_works="guessing no"])
            ])
        ])
    fi
    HAVE_WCTYPE_H=1
  else
    HAVE_WCTYPE_H=0
  fi
  AC_SUBST([HAVE_WCTYPE_H])

  AC_CHECK_HEADERS_ONCE([crtdefs.h])
  if test $ac_cv_header_crtdefs_h = yes; then
    HAVE_CRTDEFS_H=1
  else
    HAVE_CRTDEFS_H=0
  fi
  AC_SUBST([HAVE_CRTDEFS_H])

  case "$gl_cv_func_iswcntrl_works" in
    *yes) REPLACE_ISWCNTRL=0 ;;
    *)    REPLACE_ISWCNTRL=1 ;;
  esac
  AC_SUBST([REPLACE_ISWCNTRL])

  if test $HAVE_ISWCNTRL = 0 || test $REPLACE_ISWCNTRL = 1; then
    dnl Redefine all of iswcntrl, ..., iswxdigit in <wctype.h>.
    :
  fi

  if test $REPLACE_ISWCNTRL = 1; then
    REPLACE_TOWLOWER=1
  else
    AC_CHECK_FUNCS([towlower])
    if test $ac_cv_func_towlower = yes; then
      REPLACE_TOWLOWER=0
    else
      AC_CHECK_DECLS([towlower],,,
        [[/* Tru64 with Desktop Toolkit C has a bug: <stdio.h> must be
             included before <wchar.h>.
             BSD/OS 4.0.1 has a bug: <stddef.h>, <stdio.h> and <time.h>
             must be included before <wchar.h>.  */
          #include <stddef.h>
          #include <stdio.h>
          #include <time.h>
          #include <wchar.h>
          #if HAVE_WCTYPE_H
          # include <wctype.h>
          #endif
        ]])
      if test $ac_cv_have_decl_towlower = yes; then
        dnl On Minix 3.1.8, the system's <wctype.h> declares towlower() and
        dnl towupper() although it does not have the functions. Avoid a
        dnl collision with gnulib's replacement.
        REPLACE_TOWLOWER=1
      else
        REPLACE_TOWLOWER=0
      fi
    fi
  fi
  AC_SUBST([REPLACE_TOWLOWER])

  if test $HAVE_ISWCNTRL = 0 || test $REPLACE_TOWLOWER = 1; then
    dnl Redefine towlower, towupper in <wctype.h>.
    :
  fi

  dnl We assume that the wctype() and iswctype() functions exist if and only
  dnl if the type wctype_t is defined in <wchar.h> or in <wctype.h> if that
  dnl exists.
  dnl HP-UX 11.00 declares all these in <wchar.h> and lacks <wctype.h>.
  AC_CACHE_CHECK([for wctype_t], [gl_cv_type_wctype_t],
    [AC_COMPILE_IFELSE(
       [AC_LANG_PROGRAM(
          [[/* Tru64 with Desktop Toolkit C has a bug: <stdio.h> must be
               included before <wchar.h>.
               BSD/OS 4.0.1 has a bug: <stddef.h>, <stdio.h> and <time.h>
               must be included before <wchar.h>.  */
            #include <stddef.h>
            #include <stdio.h>
            #include <time.h>
            #include <wchar.h>
            #if HAVE_WCTYPE_H
            # include <wctype.h>
            #endif
            wctype_t a;
          ]],
          [[]])],
       [gl_cv_type_wctype_t=yes],
       [gl_cv_type_wctype_t=no])
    ])
  if test $gl_cv_type_wctype_t = no; then
    HAVE_WCTYPE_T=0
  fi

  dnl We assume that the wctrans() and towctrans() functions exist if and only
  dnl if the type wctrans_t is defined in <wctype.h>.
  AC_CACHE_CHECK([for wctrans_t], [gl_cv_type_wctrans_t],
    [AC_COMPILE_IFELSE(
       [AC_LANG_PROGRAM(
          [[/* Tru64 with Desktop Toolkit C has a bug: <stdio.h> must be
               included before <wchar.h>.
               BSD/OS 4.0.1 has a bug: <stddef.h>, <stdio.h> and <time.h>
               must be included before <wchar.h>.  */
            #include <stddef.h>
            #include <stdio.h>
            #include <time.h>
            #include <wchar.h>
            #include <wctype.h>
            wctrans_t a;
          ]],
          [[]])],
       [gl_cv_type_wctrans_t=yes],
       [gl_cv_type_wctrans_t=no])
    ])
  if test $gl_cv_type_wctrans_t = no; then
    HAVE_WCTRANS_T=0
  fi

  dnl Check for declarations of anything we want to poison if the
  dnl corresponding gnulib module is not in use.
  gl_WARN_ON_USE_PREPARE([[
/* Tru64 with Desktop Toolkit C has a bug: <stdio.h> must be included before
   <wchar.h>.
   BSD/OS 4.0.1 has a bug: <stddef.h>, <stdio.h> and <time.h> must be
   included before <wchar.h>.  */
#if !(defined __GLIBC__ && !defined __UCLIBC__)
# include <stddef.h>
# include <stdio.h>
# include <time.h>
# include <wchar.h>
#endif
#include <wctype.h>
    ]],
    [wctype iswctype wctrans towctrans
    ])
])

AC_DEFUN([gl_WCTYPE_MODULE_INDICATOR],
[
  dnl Use AC_REQUIRE here, so that the default settings are expanded once only.
  AC_REQUIRE([gl_WCTYPE_H_DEFAULTS])
  gl_MODULE_INDICATOR_SET_VARIABLE([$1])
  dnl Define it also as a C macro, for the benefit of the unit tests.
  gl_MODULE_INDICATOR_FOR_TESTS([$1])
])

AC_DEFUN([gl_WCTYPE_H_DEFAULTS],
[
  GNULIB_ISWBLANK=0;    AC_SUBST([GNULIB_ISWBLANK])
  GNULIB_WCTYPE=0;      AC_SUBST([GNULIB_WCTYPE])
  GNULIB_ISWCTYPE=0;    AC_SUBST([GNULIB_ISWCTYPE])
  GNULIB_WCTRANS=0;     AC_SUBST([GNULIB_WCTRANS])
  GNULIB_TOWCTRANS=0;   AC_SUBST([GNULIB_TOWCTRANS])
  dnl Assume proper GNU behavior unless another module says otherwise.
  HAVE_ISWBLANK=1;      AC_SUBST([HAVE_ISWBLANK])
  HAVE_WCTYPE_T=1;      AC_SUBST([HAVE_WCTYPE_T])
  HAVE_WCTRANS_T=1;     AC_SUBST([HAVE_WCTRANS_T])
  REPLACE_ISWBLANK=0;   AC_SUBST([REPLACE_ISWBLANK])
])


code:
# wcwidth.m4 serial 23
dnl Copyright (C) 2006-2017 Free Software Foundation, Inc.
dnl This file is free software; the Free Software Foundation
dnl gives unlimited permission to copy and/or distribute it,
dnl with or without modifications, as long as this notice is preserved.

AC_DEFUN([gl_FUNC_WCWIDTH],
[
  AC_REQUIRE([gl_WCHAR_H_DEFAULTS])
  AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles

  dnl Persuade glibc <wchar.h> to declare wcwidth().
  AC_REQUIRE([AC_USE_SYSTEM_EXTENSIONS])

  AC_REQUIRE([gt_TYPE_WCHAR_T])
  AC_REQUIRE([gt_TYPE_WINT_T])

  AC_CHECK_HEADERS_ONCE([wchar.h])
  AC_CHECK_FUNCS_ONCE([wcwidth])

  AC_CHECK_DECLS([wcwidth], [], [], [[
/* AIX 3.2.5 declares wcwidth in <string.h>. */
#include <string.h>
/* Tru64 with Desktop Toolkit C has a bug: <stdio.h> must be included before
   <wchar.h>.
   BSD/OS 4.0.1 has a bug: <stddef.h>, <stdio.h> and <time.h> must be included
   before <wchar.h>.  */
#include <stddef.h>
#include <stdio.h>
#include <time.h>
#include <wchar.h>
]])
  if test $ac_cv_have_decl_wcwidth != yes; then
    HAVE_DECL_WCWIDTH=0
  fi

  if test $ac_cv_func_wcwidth = yes; then
    HAVE_WCWIDTH=1
    dnl On Mac OS X 10.3, wcwidth(0x0301) (COMBINING ACUTE ACCENT) returns 1.
    dnl On OpenBSD 5.0, wcwidth(0x05B0) (HEBREW POINT SHEVA) returns 1.
    dnl On OSF/1 5.1, wcwidth(0x200B) (ZERO WIDTH SPACE) returns 1.
    dnl On OpenBSD 5.8, wcwidth(0xFF1A) (FULLWIDTH COLON) returns 0.
    dnl This leads to bugs in 'ls' (coreutils).
    AC_CACHE_CHECK([whether wcwidth works reasonably in UTF-8 locales],
      [gl_cv_func_wcwidth_works],
      [
        AC_RUN_IFELSE(
          [AC_LANG_SOURCE([[
#include <locale.h>
/* AIX 3.2.5 declares wcwidth in <string.h>. */
#include <string.h>
/* Tru64 with Desktop Toolkit C has a bug: <stdio.h> must be included before
   <wchar.h>.
   BSD/OS 4.0.1 has a bug: <stddef.h>, <stdio.h> and <time.h> must be included
   before <wchar.h>.  */
#include <stddef.h>
#include <stdio.h>
#include <time.h>
#include <wchar.h>
#if !HAVE_DECL_WCWIDTH
extern
# ifdef __cplusplus
"C"
# endif
int wcwidth (int);
#endif
int main ()
{
  int result = 0;
  if (setlocale (LC_ALL, "fr_FR.UTF-8") != NULL)
    {
      if (wcwidth (0x0301) > 0)
        result |= 1;
      if (wcwidth (0x05B0) > 0)
        result |= 2;
      if (wcwidth (0x200B) > 0)
        result |= 4;
      if (wcwidth (0xFF1A) == 0)
        result |= 8;
    }
  return result;
}]])],
          [gl_cv_func_wcwidth_works=yes],
          [gl_cv_func_wcwidth_works=no],
          [
changequote(,)dnl
           case "$host_os" in
                     # Guess yes on glibc and AIX 7 systems.
             *-gnu* | aix[7-9]*) gl_cv_func_wcwidth_works="guessing yes";;
             *)                  gl_cv_func_wcwidth_works="guessing no";;
           esac
changequote([,])dnl
          ])
      ])
    case "$gl_cv_func_wcwidth_works" in
      *yes) ;;
      *no) REPLACE_WCWIDTH=1 ;;
    esac
  else
    HAVE_WCWIDTH=0
  fi
  dnl We don't substitute HAVE_WCWIDTH. We assume that if the system does not
  dnl have the wcwidth function, then it does not declare it.
])


code:
# wint_t.m4 serial 6
dnl Copyright (C) 2003, 2007-2017 Free Software Foundation, Inc.
dnl This file is free software; the Free Software Foundation
dnl gives unlimited permission to copy and/or distribute it,
dnl with or without modifications, as long as this notice is preserved.

dnl From Bruno Haible.
dnl Test whether <wchar.h> has the 'wint_t' type and whether gnulib's
dnl <wchar.h> or <wctype.h> would, if present, override 'wint_t'.
dnl Prerequisite: AC_PROG_CC

AC_DEFUN([gt_TYPE_WINT_T],
[
  AC_CACHE_CHECK([for wint_t], [gt_cv_c_wint_t],
    [AC_COMPILE_IFELSE(
       [AC_LANG_PROGRAM(
          [[
/* Tru64 with Desktop Toolkit C has a bug: <stdio.h> must be included before
   <wchar.h>.
   BSD/OS 4.0.1 has a bug: <stddef.h>, <stdio.h> and <time.h> must be included
   before <wchar.h>.  */
#include <stddef.h>
#include <stdio.h>
#include <time.h>
#include <wchar.h>
            wint_t foo = (wchar_t)'\0';]],
          [[]])],
       [gt_cv_c_wint_t=yes],
       [gt_cv_c_wint_t=no])])
  if test $gt_cv_c_wint_t = yes; then
    AC_DEFINE([HAVE_WINT_T], [1], [Define if you have the 'wint_t' type.])

    dnl Determine whether gnulib's <wchar.h> or <wctype.h> would, if present,
    dnl override 'wint_t'.
    AC_CACHE_CHECK([whether wint_t is too small],
      [gl_cv_type_wint_t_too_small],
      [AC_COMPILE_IFELSE(
           [AC_LANG_PROGRAM([[
/* Tru64 with Desktop Toolkit C has a bug: <stdio.h> must be included before
   <wchar.h>.
   BSD/OS 4.0.1 has a bug: <stddef.h>, <stdio.h> and <time.h> must be
   included before <wchar.h>.  */
#if !(defined __GLIBC__ && !defined __UCLIBC__)
# include <stddef.h>
# include <stdio.h>
# include <time.h>
#endif
#include <wchar.h>
              int verify[sizeof (wint_t) < sizeof (int) ? -1 : 1];
              ]])],
           [gl_cv_type_wint_t_too_small=no],
           [gl_cv_type_wint_t_too_small=yes])])
    if test $gl_cv_type_wint_t_too_small = yes; then
      GNULIB_OVERRIDES_WINT_T=1
    else
      GNULIB_OVERRIDES_WINT_T=0
    fi
  else
    GNULIB_OVERRIDES_WINT_T=0
  fi
  AC_SUBST([GNULIB_OVERRIDES_WINT_T])
])


code:
# xalloc.m4 serial 18
dnl Copyright (C) 2002-2006, 2009-2017 Free Software Foundation, Inc.
dnl This file is free software; the Free Software Foundation
dnl gives unlimited permission to copy and/or distribute it,
dnl with or without modifications, as long as this notice is preserved.

AC_DEFUN([gl_XALLOC], [:])


code:
# xsize.m4 serial 5
dnl Copyright (C) 2003-2004, 2008-2017 Free Software Foundation, Inc.
dnl This file is free software; the Free Software Foundation
dnl gives unlimited permission to copy and/or distribute it,
dnl with or without modifications, as long as this notice is preserved.

AC_DEFUN([gl_XSIZE],
[
  dnl Prerequisites of lib/xsize.h.
  AC_REQUIRE([gl_SIZE_MAX])
  AC_CHECK_HEADERS([stdint.h])
])


code:
define redo
file ginfo
end

#set env MALLOC_CHECK_ 2
#set env INFOPATH :/usr/local/info
set env TERM xterm

#set args --restore $ttests/setscreen.drib
#set args somedoc
#set args --restore /tmp/q ./foobar
#set args -O info

#set args --restore $ttests/drib.isearch
#set env INFOPATH /usr/share/info
#set args ./imagetxt

set env LINES 64
set env COLUMNS 208
set args make --restore $ttests/drib.iptab


code:
# $Id$
# Makefile.am for texinfo/info.
#
# Copyright 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2012,
# 2013, 2014, 2015, 2016 Free Software Foundation, Inc.
#
# This file is free software; as a special exception the author gives
# unlimited permission to copy and/or distribute it, with or without
# modifications, as long as this notice is preserved.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY, to the extent permitted by law; without even the
# implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

# Use `ginfo' for building to avoid confusion with the standard `info'
# target.  Removes the `g' in the install rule before applying any
# user-specified name transformations.
bin_PROGRAMS = ginfo
transform = s/ginfo/info/; $(program_transform_name)

AM_CPPFLAGS =                                   \
  -I$(top_srcdir)                               \
  -I$(top_srcdir)/gnulib/lib                    \
  -I$(top_builddir)/gnulib/lib                  \
  -DLOCALEDIR=\"$(datadir)/locale\"             \
  -DINFODIR=\"$(infodir)\"                      \
  -DINFODIR2=\"$(datadir)/info\"

LDADD = $(top_builddir)/gnulib/lib/libgnu.a $(TERMLIBS) $(LIBINTL) $(LIBICONV)

EXTRA_DIST = pcterm.c

ginfo_SOURCES = dir.c display.c display.h doc.h dribble.c dribble.h \
  echo-area.c echo-area.h \
  filesys.c filesys.h footnotes.c footnotes.h indices.c indices.h \
  info-utils.c info-utils.h info.c info.h infodoc.c infokey.c \
  infomap.c infomap.h infopath.c m-x.c man.c man.h nodemenu.c \
  nodes.c nodes.h search.c search.h session.c session.h signals.c signals.h \
  tag.c tag.h termdep.h terminal.c terminal.h tilde.c tilde.h \
  variables.c variables.h window.c window.h
nodist_ginfo_SOURCES = doc.c funs.h

# The files `doc.c' and `funs.h' are created by ./makedoc run over the
# source files which contain DECLARE_INFO_COMMAND.  `funs.h' is a header file
# listing the functions found.  `doc.c' is a structure containing pointers
# to those functions along with completable names and documentation strings.
#
generated_sources = doc.c funs.h

# We need a linear ordering to prevent race condition in parallel make.
# See the node ``(automake)Multiple Outputs'' for an explanation.
# Make sure this matches with order in which makedoc.c creates these
# files.
doc.c: funs.h

# These files are not distributed.
DISTCLEANFILES = $(generated_sources)

noinst_PROGRAMS = makedoc
makedoc_SOURCES = makedoc.c

# Files with Info commands defined that makedoc should read.
cmd_sources = $(srcdir)/session.c $(srcdir)/echo-area.c $(srcdir)/infodoc.c \
  $(srcdir)/m-x.c $(srcdir)/indices.c $(srcdir)/nodemenu.c \
  $(srcdir)/footnotes.c $(srcdir)/variables.c

# Make the target only funs.h even though we create all the
# $(generated_sources) so that parallel makes won't do the rm && makedoc
# more than once.
funs.h: makedoc$(EXEEXT) $(cmd_sources)
	rm -f $(generated_sources)
	$(top_builddir)/$(native_tools)/info/makedoc $(cmd_sources)

# The following hack is necessary to hint make before the automatic
# dependencies are built.
BUILT_SOURCES = funs.h

# Test suite

if have_ptys
check_PROGRAMS = pseudotty
pseudotty_SOURCES = pseudotty.c
endif

TESTS = \
	t/file.sh \
	t/file-node.sh \
	t/file-nodes.sh \
	t/file-node-with-dot.sh \
	t/file-index.sh \
	t/file-index-colons.sh \
	t/file-file-with-dot.sh \
	t/no-file.sh \
	t/node-no-file.sh \
	t/split.sh \
	t/relative-path.sh \
	t/file-relative-path.sh \
	t/dir.sh \
	t/dir-file.sh \
	t/dir-file-sloppily.sh \
	t/dir-merge.sh \
	t/dir-nondir.sh \
	t/dir-no-file.sh \
	t/dir-file-menu.sh \
	t/dir-file-no-menu.sh \
	t/dir-file-node.sh \
	t/dir-file-index.sh \
	t/dir-3-menus.sh \
	t/dir-menus-sloppily.sh \
	t/where-dir-file.sh \
	t/dir-dangling-entry.sh \
	t/dir-entry-to-subdir.sh \
	t/spec-file-node.sh \
	t/spec-no-node.sh \
	t/spec-menu.sh \
	t/all-only.sh \
	t/empty.sh \
	t/cr-tag-table.sh \
	t/inc-sea-bs-after-strip-cr.sh \
	t/tab.sh \
	t/tab-argument.sh \
	t/tab-no-xref.sh \
	t/tab-skip-node.sh \
	t/xref-across-lines.sh \
	t/body-start.sh \
	t/end-of-line.sh \
	t/goal-column.sh \
	t/star-note-non-whitespace.sh \
	t/c-u-m-x-scroll-forward.sh \
	t/last-no-history.sh \
	t/split-footnotes.sh \
	t/window-split-dir.sh \
	t/last-node-not-top-level.sh \
	t/adjust-anchors.sh \
	t/xref-to-anchor.sh \
	t/search-after-tag.sh \
	t/split-file-menu.sh \
	t/search-split-after-index.sh \
	t/index.sh \
	t/no-index.sh \
	t/index-apropos.sh \
	t/index-search.sh \
	t/index-completing.sh \
	t/split-index.sh \
	t/index-long-nodeline.sh \
	t/empty-infopath.sh \
	t/menu-sequence.sh \
	t/relative-reference.sh \
	t/resize-in-completions.sh \
	t/replace-viewed.sh \
	t/search-skip-screen.sh \
	t/search-empty.sh \
	t/inc-sea-forward.sh \
	t/inc-sea-forward-nonregex.sh \
	t/inc-sea-insensitive.sh \
	t/inc-sea-history.sh \
	t/gc-split.sh \
	t/anchor-positions.sh \
	t/quoted-label-as-target.sh \
	t/quoted-target.sh \
	t/quoted-label-and-target.sh \
	t/quoted-manual-in-target.sh \
	t/quoted-manual-in-label.sh \
	t/goto-quoted.sh \
	t/next-quoted.sh \
	t/help.sh

EXTRA_DIST += $(TESTS) $(XFAIL_TESTS) \
  t/README t/infodir \
  t/Init-inter.inc t/Init-test.inc t/Timeout-test.inc \
  t/Infokey-config t/node-target
dist-hook:
	rm -rf `find $(distdir)/t/infodir -name '*~'`


code:
/* dir.c -- how to build a special "dir" node from "localdir" files.
   $Id$

   Copyright 1993, 1997, 1998, 2004, 2007, 2008, 2009, 2012,
   2013, 2014, 2015, 2016 Free Software Foundation, Inc.

   This program is free software: you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation, either version 3 of the License, or
   (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program.  If not, see <http://www.gnu.org/licenses/>.

   Originally written by Brian Fox. */

#include "info.h"
#include "info-utils.h"
#include "filesys.h"
#include "tilde.h"

/* The "dir" node can be built from the contents of a file called "dir",
   with the addition of the menus of every file named in the array
   dirs_to_add which are found in INFOPATH. */

static void add_menu_to_node (char *contents, size_t size, NODE *node);
static void insert_text_into_node (NODE *node, long start,
    char *text, int textlen);

static char *dirs_to_add[] = {
  "dir", "localdir", NULL
};

static NODE *dir_node = 0;

static NODE *build_dir_node (void);

/* Return composite directory node.  Return value should be freed by caller,
   but none of its fields should be. */
NODE *
get_dir_node (void)
{
  NODE *node;

  if (!dir_node)
    dir_node = build_dir_node ();

  node = xmalloc (sizeof (NODE));
  *node = *dir_node;

  return node;
}

static char *dir_contents;

static NODE *
build_dir_node (void)
{
  int path_index;
  char *this_dir;
  NODE *node;

  node = info_create_node ();
  node->nodename = xstrdup ("Top");
  node->fullpath = xstrdup ("dir");
  node->contents = xstrdup (

"File: dir,	Node: Top,	This is the top of the INFO tree.\n"
"\n"
"This is the Info main menu (aka directory node).\n"
"A few useful Info commands:\n"
"\n"
"  'q' quits;\n"
"  'H' lists all Info commands;\n"
"  'h' starts the Info tutorial;\n"
"  'mTexinfo RET' visits the Texinfo manual, etc.\n"

  );

  node->nodelen = strlen (node->contents);

  /* Using each element of the path, check for one of the files in
     DIRS_TO_ADD.  Do not check for "localdir.info.Z" or anything else.
     Only files explictly named are eligible.  This is a design decision.
     There can be an info file name "localdir.info" which contains
     information on the setting up of "localdir" files. */
  for (this_dir = infopath_first (&path_index); this_dir;
       this_dir = infopath_next (&path_index))
    {
      register int da_index;
      char *from_file;

      /* Expand a leading tilde if one is present. */
      if (*this_dir == '~')
        {
          char *tilde_expanded_dirname;

          tilde_expanded_dirname = tilde_expand_word (this_dir);
          if (tilde_expanded_dirname != this_dir)
            {
              this_dir = tilde_expanded_dirname;
            }
        }

      /* For every different file named in DIRS_TO_ADD found in the
         search path, add that file's menu to our "dir" node. */
      for (da_index = 0; (from_file = dirs_to_add[da_index]); da_index++)
        {
          struct stat finfo;
          int statable;
          int namelen = strlen (from_file);
          char *fullpath = xmalloc (3 + strlen (this_dir) + namelen);

          strcpy (fullpath, this_dir);
          if (!IS_SLASH (fullpath[strlen (fullpath) - 1]))
            strcat (fullpath, "/");
          strcat (fullpath, from_file);

          statable = (stat (fullpath, &finfo) == 0);

          if (statable && S_ISREG (finfo.st_mode))
            {
              size_t filesize;
	      int compressed;
              char *contents = filesys_read_info_file (fullpath, &filesize,
                                                       &finfo, &compressed);
              if (contents)
                {
                  add_menu_to_node (contents, filesize, node);
                  free (contents);
                }
            }

          free (fullpath);
        }
    }

  node->flags |= N_IsDir;
  dir_contents = node->contents;
  scan_node_contents (node, 0, 0);
  return node;
}

/* Given CONTENTS and NODE, add the menu found in CONTENTS to the menu
   found in NODE->contents.  SIZE is the total size of CONTENTS. */
static void
add_menu_to_node (char *contents, size_t size, NODE *node)
{
  SEARCH_BINDING contents_binding, fb_binding;
  long contents_offset, fb_offset;

  contents_binding.buffer = contents;
  contents_binding.start = 0;
  contents_binding.end = size;
  contents_binding.flags = S_FoldCase | S_SkipDest;

  fb_binding.buffer = node->contents;
  fb_binding.start = 0;
  fb_binding.end = node->nodelen;
  fb_binding.flags = S_FoldCase | S_SkipDest;

  /* Move to the start of the menus in CONTENTS and NODE. */
  if (search_forward (INFO_MENU_LABEL, &contents_binding, &contents_offset)
      != search_success)
    /* If there is no menu in CONTENTS, quit now. */
    return;

  /* There is a menu in CONTENTS, and contents_offset points to the first
     character following the menu starter string.  Skip all whitespace
     and newline characters. */
  contents_offset += skip_whitespace_and_newlines (contents + contents_offset);

  /* If there is no menu in NODE, make one. */
  if (search_forward (INFO_MENU_LABEL, &fb_binding, &fb_offset)
      != search_success)
    {
      fb_binding.start = node->nodelen;

      insert_text_into_node
        (node, fb_binding.start, INFO_MENU_LABEL, strlen (INFO_MENU_LABEL));

      fb_binding.buffer = node->contents;
      fb_binding.start = 0;
      fb_binding.end = node->nodelen;
      if (search_forward (INFO_MENU_LABEL, &fb_binding, &fb_offset)
	  != search_success)
        abort ();
    }

  /* CONTENTS_OFFSET and FB_OFFSET point to the starts of the menus that
     appear in their respective buffers.  Add the remainder of CONTENTS
     to the end of NODE's menu. */
  fb_binding.start = fb_offset;
  fb_offset = find_node_separator (&fb_binding);
  if (fb_offset != -1)
    fb_binding.start = fb_offset;
  else
    fb_binding.start = fb_binding.end;

  /* Leave exactly one blank line between directory entries. */
  {
    int num_found = 0;

    while ((fb_binding.start > 0) &&
           (whitespace_or_newline (fb_binding.buffer[fb_binding.start - 1])))
      {
        num_found++;
        fb_binding.start--;
      }

    /* Optimize if possible. */
    if (num_found >= 2)
      {
        fb_binding.buffer[fb_binding.start++] = '\n';
        fb_binding.buffer[fb_binding.start++] = '\n';
      }
    else
      {
        /* Do it the hard way. */
        insert_text_into_node (node, fb_binding.start, "\n\n", 2);
        fb_binding.start += 2;
      }
  }

  /* Insert the new menu. */
  insert_text_into_node
    (node, fb_binding.start, contents + contents_offset, size - contents_offset);
}

static void
insert_text_into_node (NODE *node, long start, char *text, int textlen)
{
  char *contents;
  long end;

  end = node->nodelen;

  contents = xmalloc (node->nodelen + textlen + 1);
  memcpy (contents, node->contents, start);
  memcpy (contents + start, text, textlen);
  memcpy (contents + start + textlen, node->contents + start, end - start + 1);
  free (node->contents);
  node->contents = contents;
  node->nodelen += textlen;
}

/* Return directory entry.  Return value should not be freed or modified. */
REFERENCE *
lookup_dir_entry (char *label, int sloppy)
{
  REFERENCE *entry;

  if (!dir_node)
    dir_node = build_dir_node ();

  entry = info_get_menu_entry_by_label (dir_node, label, sloppy);

  return entry;
}

/* Look up entry in "dir" and "localdir" in search directory.  Return
   value is a pointer to a newly allocated REFERENCE. */
REFERENCE *
dir_entry_of_infodir (char *label, char *searchdir)
{
  int da_index;
  char *dir_filename;
  char *dir_fullpath;

  struct stat dummy;
  char *entry_fullpath;

  NODE *dir_node;
  REFERENCE *entry;

  for (da_index = 0; (dir_filename = dirs_to_add[da_index]); da_index++)
    {
      dir_fullpath = info_add_extension (searchdir, dir_filename, &dummy);
      if (!dir_fullpath)
        continue;

      dir_node = info_get_node (dir_fullpath, "Top");
      free (dir_fullpath);
      entry = info_get_menu_entry_by_label (dir_node, label, 1);
      if (!entry || !entry->filename)
        {
          free_history_node (dir_node);
          continue;
          /* A dir entry with no filename is unlikely, but not impossible. */
        }

      entry = info_copy_reference (entry);
      entry_fullpath = info_add_extension (searchdir, entry->filename, &dummy);
      if (entry_fullpath)
        {
          free (entry->filename);
          entry->filename = entry_fullpath;
        }

      free_history_node (dir_node);
      return entry;
    }
  return 0;
}


code:
/* display.c -- How to display Info windows.
   $Id$

   Copyright 1993, 1997, 2003, 2004, 2006, 2007, 2008, 2012, 2013,
   2014, 2015, 2016, 2017 Free Software Foundation, Inc.

   This program is free software: you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation, either version 3 of the License, or
   (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program.  If not, see <http://www.gnu.org/licenses/>.

   Originally written by Brian Fox. */

#include "info.h"
#include "display.h"
#include "session.h"
#include "tag.h"
#include "signals.h"
#include "variables.h"

static void free_display (DISPLAY_LINE **display);
static DISPLAY_LINE **make_display (int width, int height);

/* An array of display lines which tell us what is currently visible on
   the display.  */
DISPLAY_LINE **the_display = NULL;

/* Non-zero means do no output. */
int display_inhibited = 0;

/* Initialize THE_DISPLAY to WIDTH and HEIGHT, with nothing in it. */
void
display_initialize_display (int width, int height)
{
  free_display (the_display);
  the_display = make_display (width, height);
  display_clear_display (the_display);
}

/* Clear all of the lines in DISPLAY making the screen blank. */
void
display_clear_display (DISPLAY_LINE **display)
{
  register int i;

  signal_block_winch ();
  for (i = 0; display[i]; i++)
    {
      display[i]->text[0] = '\0';
      display[i]->textlen = 0;
      display[i]->inverse = 0;
    }
  signal_unblock_winch ();
}

/* Non-zero if we didn't completely redisplay a window. */
int display_was_interrupted_p = 0;

/* Check each window on the screen, and update it if it needs updating. */
void
display_update_display (void)
{
  register WINDOW *win;

  /* Block window resize signals (SIGWINCH) while accessing the the_display
     object, because the signal handler may reallocate it out from under our
     feet. */
  signal_block_winch ();
  display_was_interrupted_p = 0;

  for (win = windows; win; win = win->next)
    {
      /* Only re-display visible windows which need updating. */
      if ((win->flags & W_WindowVisible) == 0
          || (win->flags & W_UpdateWindow) == 0
          || win->height == 0)
        continue;

      display_update_one_window (win);
      if (display_was_interrupted_p)
        break;
    }

  /* Always update the echo area. */
  display_update_one_window (the_echo_area);
  signal_unblock_winch ();
}

/* Return the screen column of where to write to screen to update line to
   match A, given that B contains the current state of the line.  *PPOS gets
   the offset into the string A to write from. */
static int
find_diff (const char *a, size_t alen, const char *b, size_t blen, int *ppos)
{
  mbi_iterator_t itra, itrb;
  int i;
  int pos = 0;
  int first_escape = -1;
  int escape_pos = -1;

  for (i = 0, mbi_init (itra, a, alen), mbi_init (itrb, b, blen);
       mbi_avail (itra) && mbi_avail (itrb);
       i += wcwidth (itra.cur.wc), mbi_advance (itra), mbi_advance (itrb))
    {
      if (mb_cmp (mbi_cur (itra), mbi_cur (itrb)))
        break;

      if (first_escape == -1 && *mbi_cur_ptr (itra) == '\033')
        {
          first_escape = i;
          escape_pos = pos;
        }
      pos += mb_len (mbi_cur (itra));
    }

  if (mbi_avail (itra) || mbi_avail (itrb))
    {
      if (first_escape != -1)
        {
          *ppos = escape_pos;
          return first_escape;
        }
      else
        {
          /* If there was a difference in the line, and there was an escape
             character, return the position of the escape character, as it could
             start a terminal escape sequence. */
          *ppos = pos;
          return i;
        }
    }

  /* Otherwise, no redrawing is required. */
  return -1;
}

/* Update line PL_NUM of the screen to be PRINTED_LINE, which is PL_BYTES long
   and takes up PL_CHARS columns. */
static int
display_update_line (long pl_num, char *printed_line,
                     long pl_bytes, long pl_chars)
{
  DISPLAY_LINE **display = the_display;
  DISPLAY_LINE *entry;

 entry = display[pl_num];

  /* We have the exact line as it should appear on the screen.
     Check to see if this line matches the one already appearing
     on the screen. */

  /* If the window is very small, entry might be NULL. */
  if (entry)
    {
      int i, off;

      /* If the screen line is inversed, or if the entry is marked as
         invalid, then clear the line from the screen first. */
      if (entry->inverse)
	{
	  terminal_goto_xy (0, pl_num);
	  terminal_clear_to_eol ();
	  entry->inverse = 0;
	  entry->text[0] = '\0';
	  entry->textlen = 0;
	}

      i = find_diff (printed_line, pl_bytes,
		     entry->text, strlen (entry->text), &off);

      /* If the lines differed at all, we must do some redrawing. */
      if (i != -1)
	{
	  /* Move to the proper point on the terminal. */
	  terminal_goto_xy (i, pl_num);

	  /* If there is any text to print, print it. */
          terminal_put_text (printed_line + off);

	  /* If the printed text didn't extend all the way to the edge
	     of the screen, and text was appearing between here and the
	     edge of the screen, clear from here to the end of the
	     line. */
	  if ((pl_chars < screenwidth && pl_chars < entry->textlen)
	      || entry->inverse)
	    terminal_clear_to_eol ();

	  fflush (stdout);

	  /* Update the display text buffer. */
	  if (strlen (printed_line) > (unsigned int) screenwidth)
	    /* printed_line[] can include more than screenwidth
	       characters, e.g. if multibyte encoding is used or
	       if we are under -R and there are escape sequences
	       in it.  However, entry->text was allocated (in
	       display_initialize_display) for screenwidth
	       bytes only.  */
	    entry->text = xrealloc (entry->text, strlen (printed_line) + 1);
	  strcpy (entry->text + off, printed_line + off);
	  entry->textlen = pl_chars;

	  /* Lines showing node text are not in inverse.  Only modelines
	     have that distinction. */
	  entry->inverse = 0;
	}
    }

  /* A line has been displayed, and the screen reflects that state.
     If there is typeahead pending, then let that typeahead be read
     now, instead of continuing with the display. */
  if (info_any_buffered_input_p ())
    {
      display_was_interrupted_p = 1;
      return 1;
    }
  return 0;
}

/* Similar to decide_if_in_match, but used for reference highlighting.
   Given an array REFERENCES with regions, starting at *REF_INDEX decide
   if we are inside a region at offset OFF.  The regions are assumed not
   to overlap and to be in order. */
static void
decide_if_in_reference (long off, int *in_ref, REFERENCE **references,
                        int *ref_index)
{
  int i = *ref_index;
  int m = *in_ref;

  for (; (references[i]); i++)
    {
      if (references[i]->start > off)
        break;

      m = 1;

      if (references[i]->end > off)
        break;

      m = 0;
    }

  *ref_index = i;
  *in_ref = m;
}

/* Used when processing a line to be displayed from a node.  DEFAULT is the
   value when the line has no special styles like underlined references or
   highlighted search matches.  Otherwise, a line is processed once with
   COLLECT as the value, and if it differs to what is on the display already,
   it is processed with WRITEOUT and written to the display. */
static int writing_out;
#define DEFAULT 0
#define COLLECT 1
#define WRITEOUT 2 /* Values for writing_out global. */

/* Combine rendition masks that are active, in order of priority,
   then check what's currently active on the display, and output
   the necessary codes to switch.  The list of rendition masks is
   the complete information about what the style should now be.
   RENDITION3 takes priority over RENDITION2, which in turn takes
   priority over RENDITION1. */
static void
wrap_terminal_switch_rendition (struct text_buffer *printed_line,
                                 RENDITION rendition1,
                                 RENDITION rendition2,
                                 RENDITION rendition3)
{
  long int desired_rendition = 0;
  desired_rendition = rendition1.value;
  desired_rendition &= ~rendition2.mask;
  desired_rendition |= rendition2.value;
  desired_rendition &= ~rendition3.mask;
  desired_rendition |= rendition3.value;

  if (writing_out == WRITEOUT)
    terminal_switch_rendition (desired_rendition);
  else
    {
      /* Guarantee that each byte is non-zero, by having at least one
         non-zero bit in it.  See ZERO1_MASK symbol in display.c. */
      desired_rendition = ~desired_rendition;

      /* The text added here is only used internally to see when the
         display has changed, and is not output to the terminal. */
      text_buffer_add_string (printed_line, "\033", 1);
      text_buffer_add_string (printed_line, (char *) &desired_rendition,
                              sizeof (long));
    }
}

/* Set in display_update_node_text if matches or references are to be
   distinguished with terminal appearance modes. */
static MATCH_STATE *matches;
static REFERENCE **refs;
static size_t match_index;
static int ref_index;

/* Number of screen columns output so far in a line. */
static int pl_chars;

/* Whether we are currently outputting a highlighted reference.  This can be
   carried over from one line to another. */
static int ref_highlighted;

static int pl_num; /* Number of printed lines done so far. */

RENDITION ref_rendition = {UNDERLINE_MASK, UNDERLINE_MASK};
RENDITION hl_ref_rendition = {UNDERLINE_MASK, 0};
RENDITION match_rendition = {STANDOUT_MASK, STANDOUT_MASK};

/* Process a line from the node in WIN starting at ITER, and advancing ITER
   to the end of the line.  What is done with the line depends on the value
   of WRITING_OUT.
   If the line ends in a newline character, set *DELIM to 1. */
static void
display_process_line (WINDOW *win,
                 mbi_iterator_t *iter_inout,
                 struct text_buffer *tb_printed_line,
                 int *delim)
{
  mbi_iterator_t iter;
  const char *cur_ptr;
  size_t pchars = 0; /* Printed chars */
  size_t pbytes = 0; /* Bytes to output. */
  char *rep;
  int in_match = 0;
  int in_ref = 0, in_ref_proper = 0;
  RENDITION empty = {0, 0};

  int point_in_line;

  if (win->point >= win->line_starts[win->pagetop + pl_num]
      && win->point < win->line_starts[win->pagetop + pl_num + 1])
    point_in_line = 1;
  else
    point_in_line = 0;

  iter = *iter_inout;

  while (1)
    {
      int was_in_ref_proper = in_ref_proper;
      int was_in_match = in_match;

      if (!mbi_avail (iter))
        break;
      cur_ptr = mbi_cur_ptr (iter);

      if (matches && matches_ready (matches)
          && !at_end_of_matches (matches, match_index))
        {
          int was_in_match = in_match;
          decide_if_in_match (cur_ptr - win->node->contents,
                              &in_match, matches, &match_index);

          if (!was_in_match && in_match && writing_out == DEFAULT)
            writing_out = COLLECT;
        }

      if (refs && refs[ref_index])
        {
          int was_in_ref = in_ref;
          decide_if_in_reference (cur_ptr - win->node->contents,
                                  &in_ref, refs, &ref_index);

          if (was_in_ref && !in_ref)
            {
              in_ref_proper = ref_highlighted = 0;
            }
          else if (!was_in_ref && in_ref)
            {
              if (writing_out == DEFAULT)
                writing_out = COLLECT;

              /* Decide if this reference should be highlighted. */
              if (point_in_line && win->point < refs[ref_index]->end)
                {
                  /* The reference in is the part of the line after
                     the cursor, or the reference contains the cursor. */
                  point_in_line = 0;
                  ref_highlighted = 1;
                }
              else if (point_in_line
                       && (!refs[ref_index + 1]
                           || refs[ref_index + 1]->start
                              >= win->line_starts[win->pagetop + pl_num + 1]))
                {
                  /* The reference label is before the cursor in
                     the current line and none occurs after it in
                     the current line. */
                  point_in_line = 0;
                  ref_highlighted = 1;
                }
              else if (win->point >= refs[ref_index]->start
                       && win->point < refs[ref_index]->end)
                {
                  /* The point is in a cross-reference, but not in the
                     current line. */
                  ref_highlighted = 1;
                }
              else if (win->point >= win->line_starts
                                                  [win->pagetop + pl_num + 1]
                       && win->point < win->line_starts
                                                    [win->pagetop + pl_num + 2]
                       && refs[ref_index]->end
                          >= win->line_starts[win->pagetop + pl_num + 1]
                       && (!refs[ref_index + 1]
                           || refs[ref_index + 1]->start
                              >= win->line_starts[win->pagetop + pl_num + 2]))
                {
                  /* Point is in the next line, not inside this reference,
                     but this reference continues onto the next line and
                     no other reference follows it in the line. */
                  ref_highlighted = 1;
                }
            }
        }

      if (in_ref && !in_ref_proper && !strchr (" \t", *cur_ptr))
        in_ref_proper = 1;

      if (was_in_ref_proper != in_ref_proper || was_in_match != in_match)
        {
          /* Calculate the new rendition for output characters, and call
             the function to switch to it. */
          RENDITION ref = {0, 0};
          RENDITION match = {0, 0};

          if (in_ref_proper)
            ref = ref_highlighted && hl_ref_rendition.mask
                    ? hl_ref_rendition : ref_rendition;
          if (in_match)
            match = match_rendition;
          if (!ref_highlighted)
            {
              wrap_terminal_switch_rendition (tb_printed_line,
                                              ref, match, empty);
            }
          else
            {
              wrap_terminal_switch_rendition (tb_printed_line,
                                              match, ref, empty);
            }
        }

      rep = printed_representation (&iter, delim, pl_chars,
                                    &pchars, &pbytes);

      /* If a newline character has been seen, or we have reached the
         edge of the display.  */
      if (*delim || pl_chars + pchars >= win->width)
        break;

      if (rep)
        {
          if (writing_out != WRITEOUT)
            text_buffer_add_string (tb_printed_line, rep, pbytes);
          else
            terminal_write_chars (rep, pbytes);

          pl_chars += pchars;
        }
      mbi_advance (iter);
    }

  if (writing_out != DEFAULT)
    wrap_terminal_switch_rendition (tb_printed_line, empty, empty, empty);

  *iter_inout = iter;
}

/* Update the part of WIN containing text from a node, i.e. not the blank
   part at the end or a modeline.
   Print each line in the window into our local buffer, and then
   check the contents of that buffer against the display.  If they
   differ, update the display.  Return number of lines printed. */
int
display_update_node_text (WINDOW *win)
{
  static struct text_buffer tb_printed_line;  /* Buffer for a printed line.  */

  mbi_iterator_t iter;  /* Used to iterate through part of node displayed.  */
  mbi_iterator_t bol_iter; /* Keep reference to beginning of each line.  */
  int bol_ref_index = 0, bol_match_index = 0;
  int bol_ref_highlighted;

  int finish;

  matches = 0;
  refs = 0;
  if (match_rendition.mask)
    matches = &win->matches;
  if (ref_rendition.mask || hl_ref_rendition.mask)
    refs = win->node->references;

  pl_num = 0;

  ref_highlighted = 0;

  writing_out = DEFAULT; /* Global variable, declared above. */
  ref_index = match_index = 0;

  mbi_init (iter, win->node->contents + win->line_starts[win->pagetop],
            win->node->nodelen - win->line_starts[win->pagetop]);
  mbi_avail (iter);
  while (1)
    {
      int delim;
      mbi_copy (&bol_iter, &iter);
      bol_ref_index = ref_index;
      bol_match_index = match_index;
      bol_ref_highlighted = ref_highlighted;

      /* Come back here at end of line when write_out == COLLECT */
start_of_line:
      pl_chars = 0;

      text_buffer_reset (&tb_printed_line);

      delim = 0;
      /* Check if we have processed all the lines in the window. */
      if (pl_num == win->height)
        break;

      /* Check if this line of the window is off the screen.  This might
         happen if the screen was resized very small. */
      if (win->first_row + pl_num >= screenheight)
        break;

      display_process_line (win, &iter, &tb_printed_line, &delim);

      /* End of printed line. */
      text_buffer_add_char (&tb_printed_line, '\0');

      finish = 0;
      /* If there are no highlighted regions in a line, we output the line with
         display_update_line, which does some optimization of the redisplay.
         Otherwise, the entire line is output in this function. */
      if (writing_out == DEFAULT)
        {
          finish = display_update_line (win->first_row + pl_num,
                      text_buffer_base (&tb_printed_line),
                      text_buffer_off (&tb_printed_line) - 1,
                      pl_chars);
        }
      else if (writing_out == COLLECT)
        {
          /* Check if the line differs from what is already on the display,
             and if so, go back to the start of the line and display it for
             real. */
          DISPLAY_LINE *entry = the_display[win->first_row + pl_num];
          if (strcmp (tb_printed_line.base,
                      the_display[win->first_row + pl_num]->text))
            {
              if (tb_printed_line.off > screenwidth)
                {
                  entry->text = xrealloc (entry->text,
                                          tb_printed_line.off + 1);
                }
              strcpy (entry->text, tb_printed_line.base);
              /* Record that the contents of this DISPLAY_LINE isn't
                 literally what is on the display. */
              entry->textlen = 0;
              entry->inverse = 1;
              mbi_copy (&iter, &bol_iter);
              mbi_avail (bol_iter);
              ref_index = bol_ref_index;
              match_index = bol_match_index;
              terminal_goto_xy (0, win->first_row + pl_num);
              ref_highlighted = bol_ref_highlighted;
              writing_out = WRITEOUT;
              goto start_of_line;
            }
          else
            writing_out = DEFAULT;
        }
      else /* writing_out == WRITEOUT */
        {
          /* We have just written out this line to the display. */
          terminal_clear_to_eol ();
          writing_out = DEFAULT;
        }

      /* Check if a line continuation character should be displayed.
         Don't print one on the very last line of the display, as this could
         cause it to scroll. */
      if (delim)
        mbi_advance (iter);
      else if (win->first_row + pl_num <= the_screen->height - 2)
        {
          terminal_goto_xy (win->width - 1, win->first_row + pl_num);

          if (!(win->flags & W_NoWrap))
            terminal_put_text ("\\");
          else
            {
              terminal_put_text ("$");

              /* If this window has chosen not to wrap lines, skip to the
                 end of the logical line in the buffer, and start a new
                 line here. */
              for (; mbi_avail (iter); mbi_advance (iter))
                if (mb_len (mbi_cur (iter)) == 1
                    && *mbi_cur_ptr (iter) == '\n')
                  {
                    mbi_advance (iter);
                    break;
                  }
            }
          fflush (stdout);
        }

      pl_num++;
      if (finish)
        break; /* Display was interrupted by typed input. */

      if (!mbi_avail (iter))
        break;
    }

  /* Unlike search match highlighting, we always turn reference highlighting
     off at the end of each line, so the following isn't needed. */
  /* terminal_end_underline (); */

  return pl_num;
}
#undef DEFAULT
#undef COLLECT
#undef WRITEOUT /* values for writing_out global */

/* Update one window on the screen. */
void
display_update_one_window (WINDOW *win)
{
  size_t line_index = 0;
  DISPLAY_LINE **display = the_display;

  signal_block_winch ();

  /* If display is inhibited, that counts as an interrupted display. */
  if (display_inhibited)
    {
      display_was_interrupted_p = 1;
      goto funexit;
    }

  /* If the window has no height, quit now.  Strictly speaking, it
     should only be necessary to test if the values are equal to zero, since
     window_new_screen_size should ensure that the window height/width never
     becomes negative, but since historically this has often been the culprit
     for crashes, do our best to be doubly safe.  */
  if (win->height <= 0 || win->width <= 0)
    goto funexit;

  /* If the window's first row doesn't appear in the_screen, then it
     cannot be displayed.  This can happen when the_echo_area is the
     window to be displayed, and the screen has shrunk to less than one
     line. */
  if ((win->first_row < 0) || (win->first_row > the_screen->height))
    goto funexit;

  /* If this window has a modeline, it might need to be redisplayed.  Do
     this before the rest of the window to aid in navigation in case the
     rest of the window is slow to update (for example, if it has lots of
     search matches to be displayed). */
  if (!(win->flags & W_InhibitMode))
    {
      window_make_modeline (win);
      line_index = win->first_row + win->height;

      /* This display line must both be in inverse, and have the same
         contents. */
      if ((!display[line_index]->inverse
           || (strcmp (display[line_index]->text, win->modeline) != 0))
          /* Check screen isn't very small. */
          && line_index < the_screen->height)
        {
          terminal_goto_xy (0, line_index);
          terminal_begin_inverse ();
          terminal_put_text (win->modeline);
          terminal_end_inverse ();
          strcpy (display[line_index]->text, win->modeline);
          display[line_index]->inverse = 1;
          display[line_index]->textlen = strlen (win->modeline);
        }
    }

  if (win->node)
    {
      if (!win->line_starts)
        calculate_line_starts (win);
      line_index = display_update_node_text (win);

      if (display_was_interrupted_p)
	goto funexit;
    }

  /* We have reached the end of the node or the end of the window.  If it
     is the end of the node, then clear the lines of the window from here
     to the end of the window. */
  for (; line_index < win->height; line_index++)
    {
      DISPLAY_LINE *entry = display[win->first_row + line_index];

      /* If this line has text on it, or if we don't know what is on the line,
         clear this line. */
      if (entry->textlen || entry->inverse)
        {
          entry->textlen = 0;
          entry->text[0] = '\0';
          entry->inverse = 0;

          terminal_goto_xy (0, win->first_row + line_index);
          terminal_clear_to_eol ();
          fflush (stdout);

          if (info_any_buffered_input_p ())
            {
              display_was_interrupted_p = 1;
              goto funexit;
            }
        }
    }

  fflush (stdout);

  /* Okay, this window doesn't need updating anymore. */
  win->flags &= ~W_UpdateWindow;
funexit:
  signal_unblock_winch ();
}

/* Scroll screen lines from START inclusive to END exclusive down
   by AMOUNT lines.  Negative AMOUNT means move them up. */
static void
display_scroll_region (int start, int end, int amount)
{
  int i;
  DISPLAY_LINE *temp;

  /* Do it on the screen. */
  terminal_scroll_region (start, end, amount);

  /* Now do it in the display buffer so our contents match the screen. */
  if (amount > 0)
    {
      for (i = end - 1; i >= start + amount; i--)
        {
          /* Swap rows i and (i - amount). */
          temp = the_display[i];
          the_display[i] = the_display[i - amount];
          the_display[i - amount] = temp;
        }

      /* Clear vacated lines */
      for (i = start; i < start + amount && i < end; i++)
        {
          the_display[i]->text[0] = '\0';
          the_display[i]->textlen = 0;
          the_display[i]->inverse = 0;
        }
    }
  else
    {
      amount *= -1;
      for (i = start; i <= end - 1 - amount; i++)
        {
          /* Swap rows i and (i + amount). */
          temp = the_display[i];
          the_display[i] = the_display[i + amount];
          the_display[i + amount] = temp;
        }

      /* Clear vacated lines */
      for (i = end - 1; i >= end - amount && i >= start; i--)
        {
          the_display[i]->text[0] = '\0';
          the_display[i]->textlen = 0;
          the_display[i]->inverse = 0;
        }
    }
}

/* Scroll the region of the_display starting at START, ending at END, and
   moving the lines AMOUNT lines.  If AMOUNT is less than zero, the lines
   are moved up in the screen, otherwise down.  Actually, it is possible
   for no scrolling to take place in the case that the terminal doesn't
   support it.  This doesn't matter to us. */
void
display_scroll_display (int start, int end, int amount)
{
  register int i, last;
  DISPLAY_LINE *temp;

  /* If this terminal cannot do scrolling, give up now. */
  if (!terminal_can_scroll && !terminal_can_scroll_region)
    return;

  /* If there isn't anything displayed on the screen because it is too
     small, quit now. */
  if (!the_display[0])
    return;

  /* If there is typeahead pending, then don't actually do any scrolling. */
  if (info_any_buffered_input_p ())
    return;

  /* Use scrolling region if we can because it doesn't affect the area
     below the area we want to scroll. */
  if (terminal_can_scroll_region)
    {
      display_scroll_region (start, end, amount);
      return;
    }

  /* Otherwise scroll by deleting and inserting lines. */

  if (amount < 0)
    start -= amount;
  else
    end -= amount;

  /* Do it on the screen. */
  terminal_scroll_terminal (start, end, amount);

  /* Now do it in the display buffer so our contents match the screen. */
  if (amount > 0)
    {
      last = end + amount;

      /* Shift the lines to scroll right into place. */
      for (i = 1; i <= (end - start); i++)
        {
          temp = the_display[last - i];
          the_display[last - i] = the_display[end - i];
          the_display[end - i] = temp;
        }

      /* The lines have been shifted down in the buffer.  Clear all of the
         lines that were vacated. */
      for (i = start; i != (start + amount); i++)
        {
          the_display[i]->text[0] = '\0';
          the_display[i]->textlen = 0;
          the_display[i]->inverse = 0;
        }
    }
  else
    {
      last = start + amount;
      for (i = 0; i < (end - start); i++)
        {
          temp = the_display[last + i];
          the_display[last + i] = the_display[start + i];
          the_display[start + i] = temp;
        }

      /* The lines have been shifted up in the buffer.  Clear all of the
         lines that are left over. */
      for (i = end + amount; i != end; i++)
        {
          the_display[i]->text[0] = '\0';
          the_display[i]->textlen = 0;
          the_display[i]->inverse = 0;
        }
    }
}

/* Try to scroll lines in WINDOW.  OLD_PAGETOP is the pagetop of WINDOW before
   having had its line starts recalculated.  OLD_STARTS is the list of line
   starts that used to appear in this window.  OLD_COUNT is the number of lines
   that appear in the OLD_STARTS array. */
void
display_scroll_line_starts (WINDOW *window, int old_pagetop,
    long *old_starts, int old_count)
{
  register int i, old, new;     /* Indices into the line starts arrays. */
  int last_new, last_old;       /* Index of the last visible line. */
  int old_first, new_first;     /* Index of the first changed line. */
  int unchanged_at_top = 0;
  int already_scrolled = 0;

  /* Locate the first line which was displayed on the old window. */
  old_first = old_pagetop;
  new_first = window->pagetop;

  /* Find the last line currently visible in this window. */
  last_new = window->pagetop + (window->height - 1);
  if (last_new > window->line_count)
    last_new = window->line_count - 1;

  /* Find the last line which used to be currently visible in this window. */
  last_old = old_pagetop + (window->height - 1);
  if (last_old > old_count)
    last_old = old_count - 1;

  for (old = old_first, new = new_first;
       old < last_old && new < last_new;
       old++, new++)
    if (old_starts[old] != window->line_starts[new])
      break;
    else
      unchanged_at_top++;

  /* Loop through the old lines looking for a match in the new lines. */
  for (old = old_first + unchanged_at_top; old < last_old; old++)
    {
      for (new = new_first; new < last_new; new++)
        if (old_starts[old] == window->line_starts[new])
          {
            /* Find the extent of the matching lines. */
            for (i = 0; (old + i) < last_old; i++)
              if (old_starts[old + i] != window->line_starts[new + i])
                break;

            /* Scroll these lines if there are enough of them. */
            {
              int start, end, amount;

              start = (window->first_row
                       + ((old + already_scrolled) - old_pagetop));
              amount = new - (old + already_scrolled);
              end = window->first_row + window->height;

              /* If we are shifting the block of lines down, then the last
                 AMOUNT lines will become invisible.  Thus, don't bother
                 scrolling them. */
              if (amount > 0)
                end -= amount;

              if ((end - start) > 0)
                {
                  display_scroll_display (start, end, amount);

                  /* Some lines have been scrolled.  Simulate the scrolling
                     by offsetting the value of the old index. */
                  old += i;
                  already_scrolled += amount;
                }
            }
          }
    }
}

/* Move the screen cursor to directly over the current character in WINDOW. */
void
display_cursor_at_point (WINDOW *window)
{
  int vpos, hpos;

  vpos = window_line_of_point (window) - window->pagetop + window->first_row;
  hpos = window_get_cursor_column (window);
  terminal_goto_xy (hpos, vpos);
  fflush (stdout);
}

/* **************************************************************** */
/*                                                                  */
/*                   Functions Static to this File                  */
/*                                                                  */
/* **************************************************************** */

/* Make a DISPLAY_LINE ** with width and height. */
static DISPLAY_LINE **
make_display (int width, int height)
{
  register int i;
  DISPLAY_LINE **display;

  display = xmalloc ((1 + height) * sizeof (DISPLAY_LINE *));

  for (i = 0; i < height; i++)
    {
      display[i] = xmalloc (sizeof (DISPLAY_LINE));
      display[i]->text = xmalloc (1 + width);
      display[i]->textlen = 0;
      display[i]->inverse = 0;
    }
  display[i] = NULL;
  return display;
}

/* Free the storage allocated to DISPLAY. */
static void
free_display (DISPLAY_LINE **display)
{
  register int i;
  register DISPLAY_LINE *display_line;

  if (!display)
    return;

  for (i = 0; (display_line = display[i]); i++)
    {
      free (display_line->text);
      free (display_line);
    }
  free (display);
}


code:
/* display.h -- How the display in Info is done.
   $Id$

   Copyright 1993, 1997, 2004, 2007, 2008, 2011, 2012, 2013, 2014, 2015
   Free Software Foundation, Inc.

   This program is free software: you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation, either version 3 of the License, or
   (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program.  If not, see <http://www.gnu.org/licenses/>.

   Originally written by Brian Fox. */

#ifndef INFO_DISPLAY_H
#define INFO_DISPLAY_H

#include "info-utils.h"
#include "terminal.h"

typedef struct {
  char *text;		/* Text of the line as it appears. */
  int textlen;		/* Printable Length of TEXT. */
  int inverse;		/* Screen line is either in inverse video, or
                           'text' does not represent what is on the screen. */
} DISPLAY_LINE;

/* An array of display lines which tell us what is currently visible on
   the display.  */
extern DISPLAY_LINE **the_display;

/* Non-zero means do no output. */
extern int display_inhibited;

/* Non-zero if we didn't completely redisplay a window. */
extern int display_was_interrupted_p;

/* Initialize THE_DISPLAY to WIDTH and HEIGHT, with nothing in it. */
void display_initialize_display (int width, int height);

/* Clear all of the lines in DISPLAY making the screen blank. */
void display_clear_display (DISPLAY_LINE **display);

/* Update the windows on the display. */
void display_update_display (void);

/* Display WIN on THE_DISPLAY.  Unlike display_update_display (), this
   function only does one window. */
void display_update_one_window (WINDOW *win);

/* Move the screen cursor to directly over the current character in WINDOW. */
void display_cursor_at_point (WINDOW *window);

/* Scroll the region of the_display starting at START, ending at END, and
   moving the lines AMOUNT lines.  If AMOUNT is less than zero, the lines
   are moved up in the screen, otherwise down.  Actually, it is possible
   for no scrolling to take place in the case that the terminal doesn't
   support it.  This doesn't matter to us. */
void display_scroll_display (int start, int end, int amount);

/* Try to scroll lines in WINDOW.  OLD_PAGETOP is the pagetop of WINDOW before
   having had its line starts recalculated.  OLD_STARTS is the list of line
   starts that used to appear in this window.  OLD_COUNT is the number of lines
   that appear in the OLD_STARTS array. */
void display_scroll_line_starts (WINDOW *window, int old_pagetop,
    long *old_starts, int old_count);

#endif /* not INFO_DISPLAY_H */


code:
/* doc.h -- Structures associating function pointers with documentation.
   $Id$

   Copyright 1993, 2001, 2004, 2007, 2011, 2013, 2014
   Free Software Foundation, Inc.

   This program is free software: you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation, either version 3 of the License, or
   (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program.  If not, see <http://www.gnu.org/licenses/>.

   Originally written by Brian Fox. */

#if !defined (DOC_H)
#define DOC_H

#include "info.h"

/* For each function, we keep track of the first defined key sequence
   which invokes that function, for each different map.  This is so that
   the dynamic documentation generation in infodoc.c (a) doesn't have to
   search through copious KEYMAP_ENTRYs, and, more importantly, (b) the
   user and programmer can choose the preferred key sequence that is
   printed for any given function -- it's just the first one that
   appears in the user's infokey file or the default keymaps in
   infomap.c.

   Each FUNCTION_DOC has a linked list of FUNCTION_KEYSEQ structs
   hanging off it, which are created on startup when the user and/or
   default keymaps are being parsed.  */
typedef struct function_keyseq
{
  struct function_keyseq *next;
  struct keymap_entry *map;
  int *keyseq;
} FUNCTION_KEYSEQ;

/* Structure describing an Info command. */
typedef struct
{
  VFunction *func;        /* Pointer to function implementing command. */
  char *func_name;        /* Name of this command. */
  FUNCTION_KEYSEQ *keys;  /* Key sequences that could invoke this command. */
  char *doc;              /* Documentation string. */
} FUNCTION_DOC;

/* Array of FUNCTION_DOC structures defined in doc.c, generated
   by the makedoc utility. */
extern FUNCTION_DOC function_doc_array[];

typedef FUNCTION_DOC InfoCommand;
#define InfoCmd(fn) (&function_doc_array[A_##fn])

#include "infomap.h" /* for Keymap.  */

extern char *function_name (InfoCommand *cmd);
extern InfoCommand *named_function (char *name);

extern char *function_documentation (InfoCommand *cmd);
extern char *pretty_keyname (int key);
extern char *pretty_keyseq (int *keyseq);
extern char *where_is (Keymap map, InfoCommand *cmd);
extern char *replace_in_documentation (const char *string,
    int help_is_only_window_p);
extern void dump_map_to_message_buffer (char *prefix, Keymap map);

#endif /* !DOC_H */


code:
/* dribble.c -- dribble files for Info.
   $Id$

   Copyright 1993, 1998, 2004, 2007, 2008, 2013
   Free Software Foundation, Inc.

   This program is free software: you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation, either version 3 of the License, or
   (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program.  If not, see <http://www.gnu.org/licenses/>.

   Originally written by Brian Fox. */

#include "info.h"
#include "dribble.h"

/* When non-zero, it is a stream to write all input characters to for the
   duration of this info session. */
FILE *info_dribble_file = NULL;

/* Open a dribble file named NAME, perhaps closing an already open one.
   This sets the global variable INFO_DRIBBLE_FILE to the open stream. */
void
open_dribble_file (char *name)
{
  /* Perhaps close existing dribble file. */
  close_dribble_file ();

  /* Keystrokes can be non-printable characters, so we need binary I/O.  */
  info_dribble_file = fopen (name, FOPEN_WBIN);

#if defined (HAVE_SETVBUF)
  if (info_dribble_file)
#  if defined (SETVBUF_REVERSED)
    setvbuf (info_dribble_file, _IONBF, NULL, 1);
#  else
    setvbuf (info_dribble_file, NULL, _IONBF, 1);
#  endif /* !SETVBUF_REVERSED */
#endif /* HAVE_SETVBUF */
}

/* If there is a dribble file already open, close it. */
void
close_dribble_file (void)
{
  if (info_dribble_file)
    {
      fflush (info_dribble_file);
      fclose (info_dribble_file);
      info_dribble_file = NULL;
    }
}

/* Write some output to our existing dribble file. */
void
dribble (unsigned char byte)
{
  if (info_dribble_file)
    fwrite (&byte, sizeof (unsigned char), 1, info_dribble_file);
}


code:
/* dribble.h -- Functions and vars declared in dribble.c.
   $Id$

   Copyright 1993, 2004, 2007, 2013 Free Software Foundation, Inc.

   This program is free software: you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation, either version 3 of the License, or
   (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program.  If not, see <http://www.gnu.org/licenses/>.

   Originally written by Brian Fox. */

#if !defined (_DRIBBLE_H_)
#define _DRIBBLE_H_

/* When non-zero, it is a stream to write all input characters to for the
   duration of this info session. */
extern FILE *info_dribble_file;

/* Open a dribble file named NAME, perhaps closing an already open one.
   This sets the global variable INFO_DRIBBLE_FILE to the open stream. */
extern void open_dribble_file (char *name);

/* If there is a dribble file already open, close it. */
extern void close_dribble_file (void);

/* Write some output to our existing dribble file. */
extern void dribble (unsigned char byte);

#endif /* !_DRIBBLE_H_ */