aboutsummaryrefslogtreecommitdiff
path: root/m4
diff options
context:
space:
mode:
authorRuss Allbery <rra@stanford.edu>2007-10-04 22:21:19 +0000
committerRuss Allbery <rra@stanford.edu>2007-10-04 22:21:19 +0000
commit9ff667addf39128f43d08d4ec56a6a94ec3bb062 (patch)
tree41cd39045fb2d37d343608af57aebf844ecd5690 /m4
parent2f9387bdf0e047bbd193532c4fed209acabd0e7a (diff)
Initial import of a C portability framework and utility functions from
remctl so that the wallet client error handling can rest on a firmer foundation.
Diffstat (limited to 'm4')
-rw-r--r--m4/snprintf.m455
-rw-r--r--m4/vamacros.m443
2 files changed, 98 insertions, 0 deletions
diff --git a/m4/snprintf.m4 b/m4/snprintf.m4
new file mode 100644
index 0000000..a09833f
--- /dev/null
+++ b/m4/snprintf.m4
@@ -0,0 +1,55 @@
+dnl snprintf.m4 -- Test for a working C99 snprintf.
+dnl $Id$
+dnl
+dnl Check for a working snprintf. Some systems have an snprintf that doesn't
+dnl nul-terminate if the buffer isn't large enough. Others return -1 if the
+dnl string doesn't fit into the buffer instead of returning the number of
+dnl characters that would have been formatted. Still others don't support
+dnl NULL as the buffer argument (just to get a count of the formatted length).
+dnl
+dnl Provides RRA_FUNC_SNPRINTF, which adds snprintf.o to LIBOBJS unless a
+dnl fully working snprintf is found.
+dnl
+dnl Written by Russ Allbery <rra@stanford.edu>
+dnl Copyright 2006 Board of Trustees, Leland Stanford Jr. University
+dnl See README for licensing terms.
+
+dnl Source used by RRA_FUNC_SNPRINTF.
+define([_RRA_FUNC_SNPRINTF_SOURCE],
+[[#include <stdio.h>
+#include <stdarg.h>
+
+char buf[2];
+
+int
+test(char *format, ...)
+{
+ va_list args;
+ int count;
+
+ va_start(args, format);
+ count = vsnprintf(buf, sizeof buf, format, args);
+ va_end(args);
+ return count;
+}
+
+int
+main()
+{
+ return ((test("%s", "abcd") == 4 && buf[0] == 'a' && buf[1] == '\0'
+ && snprintf(NULL, 0, "%s", "abcd") == 4) ? 0 : 1);
+}]])
+
+dnl The user-callable test.
+AC_DEFUN([RRA_FUNC_SNPRINTF],
+[AC_CACHE_CHECK([for working snprintf], [rra_cv_func_snprintf_works],
+ [AC_TRY_RUN(_RRA_FUNC_SNPRINTF_SOURCE(),
+ [rra_cv_func_snprintf_works=yes],
+ [rra_cv_func_snprintf_works=no],
+ [rra_cv_func_snprintf_works=no])])
+if test "$rra_cv_func_snprintf_works" = yes ; then
+ AC_DEFINE([HAVE_SNPRINTF], 1,
+ [Define if your system has a working snprintf function.])
+else
+ AC_LIBOBJ([snprintf])
+fi])
diff --git a/m4/vamacros.m4 b/m4/vamacros.m4
new file mode 100644
index 0000000..1dd0f9f
--- /dev/null
+++ b/m4/vamacros.m4
@@ -0,0 +1,43 @@
+dnl vamacros.m4 -- Check for support for variadic macros.
+dnl $Id$
+dnl
+dnl This file defines two macros for probing for compiler support for variadic
+dnl macros. Provided are RRA_C_C99_VAMACROS, which checks for support for the
+dnl C99 variadic macro syntax, namely:
+dnl
+dnl #define macro(...) fprintf(stderr, __VA_ARGS__)
+dnl
+dnl and RRA_C_GNU_VAMACROS, which checks for support for the older GNU
+dnl variadic macro syntax, namely:
+dnl
+dnl #define macro(args...) fprintf(stderr, args)
+dnl
+dnl They set HAVE_C99_VAMACROS or HAVE_GNU_VAMACROS as appropriate.
+dnl
+dnl Written by Russ Allbery <rra@stanford.edu>
+dnl Copyright 2006 Board of Trustees, Leland Stanford Jr. University
+dnl See README for licensing terms.
+
+AC_DEFUN([RRA_C_C99_VAMACROS],
+[AC_CACHE_CHECK([for C99 variadic macros], [rra_cv_c_c99_vamacros],
+[AC_TRY_COMPILE(
+[#include <stdio.h>
+#define error(...) fprintf(stderr, __VA_ARGS__)],
+[error("foo"); error("foo %d", 0); return 0;],
+[rra_cv_c_c99_vamacros=yes], [rra_cv_c_c99_vamacros=no])])
+if test $rra_cv_c_c99_vamacros = yes ; then
+ AC_DEFINE([HAVE_C99_VAMACROS], 1,
+ [Define if the compiler supports C99 variadic macros.])
+fi])
+
+AC_DEFUN([RRA_C_GNU_VAMACROS],
+[AC_CACHE_CHECK([for GNU-style variadic macros], [rra_cv_c_gnu_vamacros],
+[AC_TRY_COMPILE(
+[#include <stdio.h>
+#define error(args...) fprintf(stderr, args)],
+[error("foo"); error("foo %d", 0); return 0;],
+[rra_cv_c_gnu_vamacros=yes], [rra_cv_c_gnu_vamacros=no])])
+if test $rra_cv_c_gnu_vamacros = yes ; then
+ AC_DEFINE([HAVE_GNU_VAMACROS], 1,
+ [Define if the compiler supports GNU-style variadic macros.])
+fi])