chiark / gitweb /
lib: move error-reporting to their own source file
authorRichard Kettlewell <rjk@terraraq.org.uk>
Sat, 23 Feb 2013 13:57:31 +0000 (13:57 +0000)
committerRichard Kettlewell <rjk@terraraq.org.uk>
Sat, 23 Feb 2013 13:57:31 +0000 (13:57 +0000)
This allows safer use of libraries that have a symbol clash with
utils.c.

lib/Makefile.am
lib/error.c [new file with mode: 0644]
lib/error.h [new file with mode: 0644]
lib/utils.c
lib/utils.h

index 1350d3a..4d863ee 100644 (file)
@@ -21,7 +21,7 @@
 noinst_PROGRAMS=seen-t
 noinst_LIBRARIES=libmisc.a libmiscpp.a
 libmisc_a_SOURCES=getdelim.c getline.c nntp.c open_memstream.c utils.c \
-nntp.h utils.h capture.c io.h io.c recode.c seen.c seen.h
+nntp.h utils.h capture.c io.h io.c recode.c seen.c seen.h error.h error.c
 libmiscpp_a_SOURCES=cpputils.h split.cc Timezones.h Timezones.cc       \
 HTML.h Escape.cc Quote.cc Header.cc case.cc parse_date.cc parse_csv.cc \
 compact_kilo.cc round_kilo.cc thead.cc
diff --git a/lib/error.c b/lib/error.c
new file mode 100644 (file)
index 0000000..d1c1944
--- /dev/null
@@ -0,0 +1,60 @@
+/*
+ * This file is part of rjk-nntp-tools.
+ * Copyright (C) 2005-08, 2010-11 Richard Kettlewell
+ *
+ * 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 2 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, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
+ * USA
+ */
+#include <config.h>
+#include "error.h"
+#include <stdarg.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+/* called by fatal() */
+void (*exitfn)(int) attribute((noreturn)) = exit;
+
+/* count of errors */
+int errors;
+
+/* report a non-fatal error */
+void error(int errno_value, const char *msg, ...) {
+  va_list ap;
+
+  fprintf(stderr, "ERROR: ");
+  va_start(ap, msg);
+  vfprintf(stderr, msg, ap);
+  va_end(ap);
+  if(errno_value)
+    fprintf(stderr, ": %s", strerror(errno_value));
+  fputc('\n',  stderr);
+  ++errors;
+}
+
+/* report a fatal error and exit via EXITFN (which might be _exit, in a
+ * subprocess) */
+void fatal(int errno_value, const char *msg, ...) {
+  va_list ap;
+
+  fprintf(stderr, "FATAL: ");
+  va_start(ap, msg);
+  vfprintf(stderr, msg, ap);
+  va_end(ap);
+  if(errno_value)
+    fprintf(stderr, ": %s", strerror(errno_value));
+  fputc('\n',  stderr);
+  exitfn(1);
+}
diff --git a/lib/error.h b/lib/error.h
new file mode 100644 (file)
index 0000000..2aa1c3b
--- /dev/null
@@ -0,0 +1,42 @@
+/*
+ * This file is part of rjk-nntp-tools.
+ * Copyright (C) 2013 Richard Kettlewell
+ *
+ * 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 2 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, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
+ * USA
+ */
+
+#ifndef ERROR_H
+#define ERROR_H
+
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+/* Error-reporting functions */
+
+void error(int errno_value, const char *msg, ...);
+void fatal(int errno_value, const char *msg, ...) attribute((noreturn));
+
+extern void (*exitfn)(int) attribute((noreturn));
+
+extern int errors;
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* ERROR_H */
index a59271d..7195438 100644 (file)
 
 /* --- utilities ----------------------------------------------------------- */
 
-/* called by fatal() */
-static void (*exitfn)(int) attribute((noreturn)) = exit;
-
-/* report a non-fatal error */
-void error(int errno_value, const char *msg, ...) {
-  va_list ap;
-
-  fprintf(stderr, "ERROR: ");
-  va_start(ap, msg);
-  vfprintf(stderr, msg, ap);
-  va_end(ap);
-  if(errno_value)
-    fprintf(stderr, ": %s", strerror(errno_value));
-  fputc('\n',  stderr);
-}
-
-/* report a fatal error and exit via EXITFN (which might be _exit, in a
- * subprocess) */
-void fatal(int errno_value, const char *msg, ...) {
-  va_list ap;
-
-  fprintf(stderr, "FATAL: ");
-  va_start(ap, msg);
-  vfprintf(stderr, msg, ap);
-  va_end(ap);
-  if(errno_value)
-    fprintf(stderr, ": %s", strerror(errno_value));
-  fputc('\n',  stderr);
-  exitfn(1);
-}
-
 /* Allocate memory, call fatal() on error */
 void *xmalloc(size_t n) {
   void *ptr;
index 4d3b736..dca6c90 100644 (file)
 #include <pthread.h>
 #include <sys/types.h>
 
+#include "error.h"
+
 #ifdef __cplusplus
 extern "C" {
 #endif
 
 /* Utility functions */
 
-void error(int errno_value, const char *msg, ...);
-void fatal(int errno_value, const char *msg, ...) attribute((noreturn));
 void *xmalloc(size_t n);
 void *xrealloc(void *ptr, size_t n) ;
 char *xstrdup(const char *s);