glibc has (see http://udrepper.livejournal.com/20948.html) hooks for adding
to printf() support for new types, format specifiers and modifiers. These
functions are used, for example, by the libquadmath library (for 128-bit
quadruple-precision floating point), to add the ability to print 128-bit
floating point numbers.

This patch does not fully implement these hooks - doing this will require
extensive modifications to our printf() implementation, which comes from
Musl. Rather it is just a stub, which prints a warning (once) and returns
an error. Hopefully, this level of support would be enough for applications
which want to use libquadmath (for example) for calculations but do not
intend to use its printf extensions.

Refs issue #765 (this issue should not be closed until we have a full
implementation of these functions, or make a decision that we never will).

Signed-off-by: Nadav Har'El <n...@scylladb.com>
---
 libc/stdio/printf-hooks.cc | 37 +++++++++++++++++++++++++++++++++++++
 Makefile                   |  1 +
 include/api/printf.h       | 37 +++++++++++++++++++++++++++++++++++++
 3 files changed, 75 insertions(+)
 create mode 100644 libc/stdio/printf-hooks.cc
 create mode 100644 include/api/printf.h

diff --git a/libc/stdio/printf-hooks.cc b/libc/stdio/printf-hooks.cc
new file mode 100644
index 0000000..3e71739
--- /dev/null
+++ b/libc/stdio/printf-hooks.cc
@@ -0,0 +1,37 @@
+/*
+ * Copyright (C) 2016 ScyllaDB Ltd.
+ *
+ * This work is open source software, licensed under the terms of the
+ * BSD license as described in the LICENSE file in the top-level directory.
+ */
+
+// Since 2009 (see blog post http://udrepper.livejournal.com/20948.html),
+// glibc has hooks for extending printf() to support additional types,
+// format specifiers and modifiers.
+//
+// We do not support these hooks in OSv yet (doing so would require heavy
+// modifications to Musl's printf implementation), but just offer here a
+// stub, do-nothing implementation which returns -1 for every function
+// (telling the caller that the attempt was not successful).
+// This implementation is only enough in case a library wants to add
+// additional types to printf (e.g., libquadmath does this to support
+// 128-bit quadruple-precision floating point types), but the application
+// does not really try to print out such values.
+
+#include <printf.h>
+
+#include <osv/stubbing.hh>
+
+int register_printf_specifier(int, printf_function, 
printf_arginfo_size_function)
+{
+    WARN_STUBBED();
+    return -1;
+}
+int register_printf_modifier(const wchar_t *) {
+    WARN_STUBBED();
+    return -1;
+}
+int register_printf_type(printf_va_arg_function) {
+    WARN_STUBBED();
+    return -1;
+}
diff --git a/Makefile b/Makefile
index 7e15d0e..feb6d54 100644
--- a/Makefile
+++ b/Makefile
@@ -1482,6 +1482,7 @@ musl += stdio/vwprintf.o
 musl += stdio/vwscanf.o
 musl += stdio/wprintf.o
 musl += stdio/wscanf.o
+libc += stdio/printf-hooks.o
 
 musl += stdlib/abs.o
 musl += stdlib/atof.o
diff --git a/include/api/printf.h b/include/api/printf.h
new file mode 100644
index 0000000..90f4c0a
--- /dev/null
+++ b/include/api/printf.h
@@ -0,0 +1,37 @@
+/*
+ * Copyright (C) 2016 ScyllaDB Ltd.
+ *
+ * This work is open source software, licensed under the terms of the
+ * BSD license as described in the LICENSE file in the top-level directory.
+ */
+
+// This is a Linux-specific header files for non-standard printf() hooks.
+
+#ifndef INCLUDED_PRINTF_H
+#define INCLUDED_PRINTF_H
+
+#define __NEED_size_t
+#define __NEED_wchar_t
+#define __NEED_va_list
+#define __NEED_FILE
+#include <bits/alltypes.h>
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+typedef int printf_function(
+        FILE *, const struct printf_info *, const void *const *);
+typedef int printf_arginfo_size_function(
+        const struct printf_info *, size_t, int *, int *);
+typedef void printf_va_arg_function(void *, va_list *);
+
+int register_printf_specifier(int, printf_function, 
printf_arginfo_size_function);
+int register_printf_modifier(const wchar_t *);
+int register_printf_type(printf_va_arg_function);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif
-- 
2.5.5

-- 
You received this message because you are subscribed to the Google Groups "OSv 
Development" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to osv-dev+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to