summaryrefslogtreecommitdiffstats
path: root/ffi
diff options
context:
space:
mode:
authorJustus Winter <justus@sequoia-pgp.org>2019-03-22 16:19:39 +0100
committerJustus Winter <justus@sequoia-pgp.org>2019-03-22 16:19:39 +0100
commit6a9e2743d149fecd5d7706b273dc69a45fe25ea2 (patch)
treef1d3d40d578467416659d96cee6a2a68b9c31889 /ffi
parent1d35557a8f7866141f84b2764287f384a8e12872 (diff)
openpgp-ffi: Replace glibc's error function.
- To make the tests and examples more portable, provide our own roughly compatible replacement for glibc's error(3).
Diffstat (limited to 'ffi')
-rw-r--r--ffi/examples/configure.c13
-rw-r--r--ffi/examples/keyserver.c11
-rw-r--r--ffi/tests/c-tests.rs28
3 files changed, 43 insertions, 9 deletions
diff --git a/ffi/examples/configure.c b/ffi/examples/configure.c
index 38b15c05..23e243ac 100644
--- a/ffi/examples/configure.c
+++ b/ffi/examples/configure.c
@@ -1,9 +1,18 @@
#define _GNU_SOURCE
#include <assert.h>
-#include <error.h>
+/* Roughly glibc compatible error reporting. */
+#define error(S, E, F, ...) do { \
+ fprintf (stderr, (F), __VA_ARGS__); \
+ int s = (S), e = (E); \
+ if (e) { fprintf (stderr, ": %s", strerror (e)); } \
+ fprintf (stderr, "\n"); \
+ fflush (stderr); \
+ if (s) { exit (s); } \
+ } while (0)
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
+#include <string.h>
#include <sequoia.h>
@@ -34,7 +43,7 @@ main (int argc, char **argv)
pgp_error_free (err);
}
else
- error (1, 0, "This should not be allowed");
+ assert (! "reachable");
sq_keyserver_free (ks);
sq_context_free (ctx);
diff --git a/ffi/examples/keyserver.c b/ffi/examples/keyserver.c
index f7437472..1e6ca135 100644
--- a/ffi/examples/keyserver.c
+++ b/ffi/examples/keyserver.c
@@ -1,8 +1,17 @@
#define _GNU_SOURCE
-#include <error.h>
+/* Roughly glibc compatible error reporting. */
+#define error(S, E, F, ...) do { \
+ fprintf (stderr, (F), __VA_ARGS__); \
+ int s = (S), e = (E); \
+ if (e) { fprintf (stderr, ": %s", strerror (e)); } \
+ fprintf (stderr, "\n"); \
+ fflush (stderr); \
+ if (s) { exit (s); } \
+ } while (0)
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
+#include <string.h>
#include <sequoia.h>
diff --git a/ffi/tests/c-tests.rs b/ffi/tests/c-tests.rs
index 5dfa56b7..71e6fa9c 100644
--- a/ffi/tests/c-tests.rs
+++ b/ffi/tests/c-tests.rs
@@ -263,9 +263,7 @@ fn run(ldpath: &Path, exe: &Path) -> io::Result<()> {
/// Wraps the code in a main function if none exists.
fn wrap_with_main(test: &mut Vec<String>, offset: usize) {
- if has_main(test) {
- return;
- }
+ let needs_wrapping = ! has_main(test);
let mut last_include = 0;
for (n, line) in test.iter().enumerate() {
@@ -274,10 +272,28 @@ fn wrap_with_main(test: &mut Vec<String>, offset: usize) {
}
}
- test.insert(last_include + 1, "int main() {".into());
- test.insert(last_include + 2, format!("#line {}", last_include + 1 + offset));
+ test.insert(last_include + 1,
+ "#define error(S, E, F, ...) do { \\\n\
+ fprintf (stderr, (F), __VA_ARGS__); \\\n\
+ int s = (S), e = (E); \\\n\
+ if (e) { fprintf (stderr, \": %s\", strerror (e)); } \\\n\
+ fprintf (stderr, \"\\n\"); \\\n\
+ fflush (stderr); \\\n\
+ if (s) { exit (s); } \\\n\
+ } while (0)".into());
+
+ if needs_wrapping {
+ test.insert(last_include + 1, "int main() {".into());
+ }
+ test.insert(last_include + 2, format!("#line {}", last_include + offset
+ + if needs_wrapping {1} else {0}));
let last = test.len();
- test.insert(last, "}".into());
+ if needs_wrapping {
+ test.insert(last, "}".into());
+ }
+ test.insert(0, "#include <string.h>".into());
+ test.insert(0, "#include <stdlib.h>".into());
+ test.insert(0, "#include <stdio.h>".into());
test.insert(0, "#define _GNU_SOURCE".into());
}