Blame m4/snprintf.m4

Packit 0b5880
# $Id: snprintf.m4,v 1.1.1.1 2008/01/06 03:24:00 holger Exp $
Packit 0b5880
Packit 0b5880
# Copyright (c) 2008 Holger Weiss <holger@jhweiss.de>.
Packit 0b5880
#
Packit 0b5880
# This code may freely be used, modified and/or redistributed for any purpose.
Packit 0b5880
# It would be nice if additions and fixes to this file (including trivial code
Packit 0b5880
# cleanups) would be sent back in order to let me include them in the version
Packit 0b5880
# available at <http://www.jhweiss.de/software/snprintf.html>.  However, this is
Packit 0b5880
# not a requirement for using or redistributing (possibly modified) versions of
Packit 0b5880
# this file, nor is leaving this notice intact mandatory.
Packit 0b5880
Packit 0b5880
# HW_HEADER_STDARG_H
Packit 0b5880
# ------------------
Packit 0b5880
# Define HAVE_STDARG_H to 1 if <stdarg.h> is available.
Packit 0b5880
AC_DEFUN([HW_HEADER_STDARG_H],
Packit 0b5880
[
Packit 0b5880
  AC_PREREQ([2.60])dnl Older releases should work if AC_CHECK_HEADERS is used.
Packit 0b5880
  AC_CHECK_HEADERS_ONCE([stdarg.h])
Packit 0b5880
])# HW_HEADER_STDARG_H
Packit 0b5880
Packit 0b5880
# HW_HEADER_VARARGS_H
Packit 0b5880
# -------------------
Packit 0b5880
# Define HAVE_VARARGS_H to 1 if <varargs.h> is available.
Packit 0b5880
AC_DEFUN([HW_HEADER_VARARGS_H],
Packit 0b5880
[
Packit 0b5880
  AC_PREREQ([2.60])dnl Older releases should work if AC_CHECK_HEADERS is used.
Packit 0b5880
  AC_CHECK_HEADERS_ONCE([varargs.h])
Packit 0b5880
])# HW_HEADER_VARARGS_H
Packit 0b5880
Packit 0b5880
# HW_FUNC_VA_COPY
Packit 0b5880
# ---------------
Packit 0b5880
# Set $hw_cv_func_va_copy to "yes" or "no".  Define HAVE_VA_COPY to 1 if
Packit 0b5880
# $hw_cv_func_va_copy is set to "yes".  Note that it's "unspecified whether
Packit 0b5880
# va_copy and va_end are macros or identifiers declared with external linkage."
Packit 0b5880
# (C99: 7.15.1, 1)  Therefore, the presence of va_copy(3) cannot simply "be
Packit 0b5880
# tested with #ifdef", as suggested by the Autoconf manual (5.5.1).
Packit 0b5880
AC_DEFUN([HW_FUNC_VA_COPY],
Packit 0b5880
[
Packit 0b5880
  AC_REQUIRE([HW_HEADER_STDARG_H])dnl Our check evaluates HAVE_STDARG_H.
Packit 0b5880
  AC_REQUIRE([HW_HEADER_VARARGS_H])dnl Our check evaluates HAVE_VARARGS_H.
Packit 0b5880
  AC_CACHE_CHECK([for va_copy],
Packit 0b5880
    [hw_cv_func_va_copy],
Packit 0b5880
    [AC_RUN_IFELSE(
Packit 0b5880
      [AC_LANG_PROGRAM(
Packit 0b5880
        [[#if HAVE_STDARG_H
Packit 0b5880
        #include <stdarg.h>
Packit 0b5880
        #elif HAVE_VARARGS_H
Packit 0b5880
        #include <varargs.h>
Packit 0b5880
        #endif]],
Packit 0b5880
        [[va_list ap, aq; va_copy(aq, ap);]])],
Packit 0b5880
      [hw_cv_func_va_copy=yes],
Packit 0b5880
      [hw_cv_func_va_copy=no],
Packit 0b5880
      [hw_cv_func_va_copy=no])])
Packit 0b5880
  AS_IF([test "$hw_cv_func_va_copy" = yes],
Packit 0b5880
    [AC_DEFINE([HAVE_VA_COPY], [1],
Packit 0b5880
      [Define to 1 if you have the `va_copy' function or macro.])])
Packit 0b5880
])# HW_FUNC_VA_COPY
Packit 0b5880
Packit 0b5880
# HW_FUNC___VA_COPY
Packit 0b5880
# -----------------
Packit 0b5880
# Set $hw_cv_func___va_copy to "yes" or "no".  Define HAVE___VA_COPY to 1 if
Packit 0b5880
# $hw_cv_func___va_copy is set to "yes".
Packit 0b5880
AC_DEFUN([HW_FUNC___VA_COPY],
Packit 0b5880
[
Packit 0b5880
  AC_REQUIRE([HW_HEADER_STDARG_H])dnl Our check evaluates HAVE_STDARG_H.
Packit 0b5880
  AC_REQUIRE([HW_HEADER_VARARGS_H])dnl Our check evaluates HAVE_VARARGS_H.
Packit 0b5880
  AC_CACHE_CHECK([for __va_copy],
Packit 0b5880
    [hw_cv_func___va_copy],
Packit 0b5880
    [AC_RUN_IFELSE(
Packit 0b5880
      [AC_LANG_PROGRAM(
Packit 0b5880
        [[#if HAVE_STDARG_H
Packit 0b5880
        #include <stdarg.h>
Packit 0b5880
        #elif HAVE_VARARGS_H
Packit 0b5880
        #include <varargs.h>
Packit 0b5880
        #endif]],
Packit 0b5880
        [[va_list ap, aq; __va_copy(aq, ap);]])],
Packit 0b5880
      [hw_cv_func___va_copy=yes],
Packit 0b5880
      [hw_cv_func___va_copy=no],
Packit 0b5880
      [hw_cv_func___va_copy=no])])
Packit 0b5880
  AS_IF([test "$hw_cv_func___va_copy" = yes],
Packit 0b5880
    [AC_DEFINE([HAVE___VA_COPY], [1],
Packit 0b5880
      [Define to 1 if you have the `__va_copy' function or macro.])])
Packit 0b5880
])# HW_FUNC___VA_COPY
Packit 0b5880
Packit 0b5880
# HW_FUNC_VSNPRINTF
Packit 0b5880
# -----------------
Packit 0b5880
# Set $hw_cv_func_vsnprintf and $hw_cv_func_vsnprintf_c99 to "yes" or "no",
Packit 0b5880
# respectively.  Define HAVE_VSNPRINTF to 1 only if $hw_cv_func_vsnprintf_c99
Packit 0b5880
# is set to "yes".  Otherwise, define vsnprintf to rpl_vsnprintf and make sure
Packit 0b5880
# the replacement function will be built.
Packit 0b5880
#
Packit 0b5880
# If enable_snprintf_replacement=true, the replacement is forced to be built.
Packit 0b5880
AC_DEFUN([HW_FUNC_VSNPRINTF],
Packit 0b5880
[
Packit 0b5880
  AC_PREREQ([2.60])dnl 2.59 should work if some AC_TYPE_* macros are replaced.
Packit 0b5880
  AC_REQUIRE([HW_HEADER_STDARG_H])dnl Our check evaluates HAVE_STDARG_H.
Packit 0b5880
Packit 0b5880
  if test "xtrue" != x"$enable_snprintf_replacement"; then
Packit 0b5880
	  AC_CHECK_FUNC([vsnprintf],
Packit 0b5880
		[hw_cv_func_vsnprintf=yes],
Packit 0b5880
		[hw_cv_func_vsnprintf=no])
Packit 0b5880
	  AS_IF([test "$hw_cv_func_vsnprintf" = yes],
Packit 0b5880
		[AC_CACHE_CHECK([whether vsnprintf is C99 compliant],
Packit 0b5880
		  [hw_cv_func_vsnprintf_c99],
Packit 0b5880
		  [AC_RUN_IFELSE(
Packit 0b5880
			[AC_LANG_PROGRAM(
Packit 0b5880
			  [[#if HAVE_STDARG_H
Packit 0b5880
			  #include <stdarg.h>
Packit 0b5880
			  #endif
Packit 0b5880
			  #include <stdio.h>
Packit 0b5880
			  static int testprintf(char *buf, size_t size, const char *format, ...)
Packit 0b5880
			  {
Packit 0b5880
				int result;
Packit 0b5880
				va_list ap;
Packit 0b5880
				va_start(ap, format);
Packit 0b5880
				result = vsnprintf(buf, size, format, ap);
Packit 0b5880
				va_end(ap);
Packit 0b5880
				return result;
Packit 0b5880
			  }]],
Packit 0b5880
			  [[char buf[43];
Packit 0b5880
			  if (testprintf(buf, 4, "The answer is %27.2g.", 42.0) != 42 ||
Packit 0b5880
				  testprintf(buf, 0, "No, it's %32zu.", (size_t)42) != 42 ||
Packit 0b5880
				  buf[0] != 'T' || buf[3] != '\0')
Packit 0b5880
				return 1;]])],
Packit 0b5880
			[hw_cv_func_vsnprintf_c99=yes],
Packit 0b5880
			[hw_cv_func_vsnprintf_c99=no],
Packit 0b5880
			[hw_cv_func_vsnprintf_c99=no])])],
Packit 0b5880
		[hw_cv_func_snprintf_c99=no])
Packit 0b5880
	  AS_IF([test "$hw_cv_func_vsnprintf_c99" = yes],
Packit 0b5880
		[AC_DEFINE([HAVE_VSNPRINTF], [1],
Packit 0b5880
		  [Define to 1 if you have a C99 compliant 'vsnprintf' function.])])
Packit 0b5880
  else
Packit 0b5880
      hw_cv_func_vsnprintf_c99=no
Packit 0b5880
  fi
Packit 0b5880
Packit 0b5880
  AS_IF([test "$hw_cv_func_vsnprintf_c99" = no],
Packit 0b5880
    [AC_DEFINE([vsnprintf], [rpl_vsnprintf],
Packit 0b5880
      [Define to rpl_vsnprintf if the replacement function should be used.])
Packit 0b5880
    AC_CHECK_HEADERS([inttypes.h locale.h stddef.h stdint.h])
Packit 0b5880
    AC_CHECK_MEMBERS([struct lconv.decimal_point, struct lconv.thousands_sep],
Packit 0b5880
      [], [], [#include <locale.h>])
Packit 0b5880
    AC_TYPE_LONG_DOUBLE
Packit 0b5880
    AC_TYPE_LONG_LONG_INT
Packit 0b5880
    AC_TYPE_UNSIGNED_LONG_LONG_INT
Packit 0b5880
    AC_TYPE_SIZE_T
Packit 0b5880
    AC_TYPE_INTMAX_T
Packit 0b5880
    AC_TYPE_UINTMAX_T
Packit 0b5880
    AC_TYPE_UINTPTR_T
Packit 0b5880
    AC_CHECK_TYPES([ptrdiff_t])
Packit 0b5880
    AC_CHECK_FUNCS([localeconv])
Packit 0b5880
    _HW_FUNC_XPRINTF_REPLACE])
Packit 0b5880
])# HW_FUNC_VSNPRINTF
Packit 0b5880
Packit 0b5880
# HW_FUNC_SNPRINTF
Packit 0b5880
# ----------------
Packit 0b5880
# Set $hw_cv_func_snprintf and $hw_cv_func_snprintf_c99 to "yes" or "no",
Packit 0b5880
# respectively.  Define HAVE_SNPRINTF to 1 only if $hw_cv_func_snprintf_c99
Packit 0b5880
# is set to "yes".  Otherwise, define snprintf to rpl_snprintf and make sure
Packit 0b5880
# the replacement function will be built.
Packit 0b5880
#
Packit 0b5880
# If enable_snprintf_replacement=true, the replacement is forced to be built.
Packit 0b5880
AC_DEFUN([HW_FUNC_SNPRINTF],
Packit 0b5880
[
Packit 0b5880
  AC_REQUIRE([HW_FUNC_VSNPRINTF])dnl Our snprintf(3) calls vsnprintf(3).
Packit 0b5880
Packit 0b5880
  if test "xtrue" != x"$enable_snprintf_replacement"; then
Packit 0b5880
	  AC_CHECK_FUNC([snprintf],
Packit 0b5880
		[hw_cv_func_snprintf=yes],
Packit 0b5880
		[hw_cv_func_snprintf=no])
Packit 0b5880
	  AS_IF([test "$hw_cv_func_snprintf" = yes],
Packit 0b5880
		[AC_CACHE_CHECK([whether snprintf is C99 compliant],
Packit 0b5880
		  [hw_cv_func_snprintf_c99],
Packit 0b5880
		  [AC_RUN_IFELSE(
Packit 0b5880
			[AC_LANG_PROGRAM([[#include <stdio.h>]],
Packit 0b5880
			  [[char buf[43];
Packit 0b5880
			  if (snprintf(buf, 4, "The answer is %27.2g.", 42.0) != 42 ||
Packit 0b5880
				  snprintf(buf, 0, "No, it's %32zu.", (size_t)42) != 42 ||
Packit 0b5880
				  buf[0] != 'T' || buf[3] != '\0')
Packit 0b5880
				return 1;]])],
Packit 0b5880
			[hw_cv_func_snprintf_c99=yes],
Packit 0b5880
			[hw_cv_func_snprintf_c99=no],
Packit 0b5880
			[hw_cv_func_snprintf_c99=no])])],
Packit 0b5880
		[hw_cv_func_snprintf_c99=no])
Packit 0b5880
  else
Packit 0b5880
      hw_cv_func_snprintf_c99=no
Packit 0b5880
  fi
Packit 0b5880
Packit 0b5880
  AS_IF([test "$hw_cv_func_snprintf_c99" = yes],
Packit 0b5880
    [AC_DEFINE([HAVE_SNPRINTF], [1],
Packit 0b5880
      [Define to 1 if you have a C99 compliant 'snprintf' function.])],
Packit 0b5880
    [AC_DEFINE([snprintf], [rpl_snprintf],
Packit 0b5880
      [Define to rpl_snprintf if the replacement function should be used.])
Packit 0b5880
    _HW_FUNC_XPRINTF_REPLACE])
Packit 0b5880
])# HW_FUNC_SNPRINTF
Packit 0b5880
Packit 0b5880
# HW_FUNC_VASPRINTF
Packit 0b5880
# -----------------
Packit 0b5880
# Set $hw_cv_func_vasprintf to "yes" or "no".  Define HAVE_VASPRINTF to 1 if
Packit 0b5880
# $hw_cv_func_vasprintf is set to "yes".  Otherwise, define vasprintf to
Packit 0b5880
# rpl_vasprintf and make sure the replacement function will be built.
Packit 0b5880
#
Packit 0b5880
# If enable_snprintf_replacement=true, the replacement is forced to be built.
Packit 0b5880
AC_DEFUN([HW_FUNC_VASPRINTF],
Packit 0b5880
[
Packit 0b5880
  AC_REQUIRE([HW_FUNC_VSNPRINTF])dnl Our vasprintf(3) calls vsnprintf(3).
Packit 0b5880
Packit 0b5880
  if test "xtrue" != x"$enable_snprintf_replacement"; then
Packit 0b5880
	  AC_CHECK_FUNCS([vasprintf],
Packit 0b5880
		[hw_cv_func_vasprintf=yes],
Packit 0b5880
		[hw_cv_func_vasprintf=no])
Packit 0b5880
  else
Packit 0b5880
      hw_cv_func_vasprintf=no
Packit 0b5880
  fi
Packit 0b5880
Packit 0b5880
  AS_IF([test "$hw_cv_func_vasprintf" = no],
Packit 0b5880
    [AC_DEFINE([vasprintf], [rpl_vasprintf],
Packit 0b5880
      [Define to rpl_vasprintf if the replacement function should be used.])
Packit 0b5880
    AC_CHECK_HEADERS([stdlib.h])
Packit 0b5880
    HW_FUNC_VA_COPY
Packit 0b5880
    AS_IF([test "$hw_cv_func_va_copy" = no],
Packit 0b5880
      [HW_FUNC___VA_COPY])
Packit 0b5880
    _HW_FUNC_XPRINTF_REPLACE])
Packit 0b5880
])# HW_FUNC_VASPRINTF
Packit 0b5880
Packit 0b5880
# HW_FUNC_ASPRINTF
Packit 0b5880
# ----------------
Packit 0b5880
# Set $hw_cv_func_asprintf to "yes" or "no".  Define HAVE_ASPRINTF to 1 if
Packit 0b5880
# $hw_cv_func_asprintf is set to "yes".  Otherwise, define asprintf to
Packit 0b5880
# rpl_asprintf and make sure the replacement function will be built.
Packit 0b5880
#
Packit 0b5880
# If enable_snprintf_replacement=true, the replacement is forced to be built.
Packit 0b5880
AC_DEFUN([HW_FUNC_ASPRINTF],
Packit 0b5880
[
Packit 0b5880
  AC_REQUIRE([HW_FUNC_VASPRINTF])dnl Our asprintf(3) calls vasprintf(3).
Packit 0b5880
Packit 0b5880
  if test "xtrue" != x"$enable_snprintf_replacement"; then
Packit 0b5880
	  AC_CHECK_FUNCS([asprintf],
Packit 0b5880
		[hw_cv_func_asprintf=yes],
Packit 0b5880
		[hw_cv_func_asprintf=no])
Packit 0b5880
  else
Packit 0b5880
      hw_cv_func_asprintf=no
Packit 0b5880
  fi
Packit 0b5880
  
Packit 0b5880
  AS_IF([test "$hw_cv_func_asprintf" = no],
Packit 0b5880
    [AC_DEFINE([asprintf], [rpl_asprintf],
Packit 0b5880
      [Define to rpl_asprintf if the replacement function should be used.])
Packit 0b5880
    _HW_FUNC_XPRINTF_REPLACE])
Packit 0b5880
])# HW_FUNC_ASPRINTF
Packit 0b5880
Packit 0b5880
# _HW_FUNC_XPRINTF_REPLACE
Packit 0b5880
# ------------------------
Packit 0b5880
# Arrange for building snprintf.c.  Must be called if one or more of the
Packit 0b5880
# functions provided by snprintf.c are needed.
Packit 0b5880
AC_DEFUN([_HW_FUNC_XPRINTF_REPLACE],
Packit 0b5880
[
Packit 0b5880
  AS_IF([test "x$_hw_cv_func_xprintf_replace_done" != xyes],
Packit 0b5880
    [AC_C_CONST
Packit 0b5880
    HW_HEADER_STDARG_H
Packit 0b5880
    AC_LIBOBJ([snprintf])
Packit 0b5880
    _hw_cv_func_xprintf_replace_done=yes])
Packit 0b5880
])# _HW_FUNC_XPRINTF_REPLACE
Packit 0b5880
Packit 0b5880
dnl vim: set joinspaces textwidth=80: