| 1 | musl libc does currently not have an equivalent of GNU Libc's error.h
|
| 2 |
|
| 3 | The following is a mix of a workaround from aufs (https://sourceforge.net/p/aufs/aufs-util/ci/aufs4.14/tree/extlib/non-glibc/)
|
| 4 | and the stranded error.{h,c} implementation https://www.openwall.com/lists/musl/2019/08/06/21
|
| 5 |
|
| 6 | |
| 7 | |
| 8 | @@ -25,7 +25,11 @@
|
| 9 | #include <stdint.h>
|
| 10 | #include <string.h>
|
| 11 | #include <errno.h>
|
| 12 | +#ifdef __GNU_LIBRARY__
|
| 13 | #include <error.h>
|
| 14 | +#else
|
| 15 | +#include "hidrd/util/error_at_line.h"
|
| 16 | +#endif
|
| 17 | #include <stdio.h>
|
| 18 | #include "hidrd/fmt/xml.h"
|
| 19 |
|
| 20 | |
| 21 | |
| 22 | @@ -27,7 +27,11 @@
|
| 23 | #include <stdint.h>
|
| 24 | #include <string.h>
|
| 25 | #include <errno.h>
|
| 26 | +#ifdef __GNU_LIBRARY__
|
| 27 | #include <error.h>
|
| 28 | +#else
|
| 29 | +#include "hidrd/util/error_at_line.h"
|
| 30 | +#endif
|
| 31 | #include <stdio.h>
|
| 32 | #include "hidrd/item.h"
|
| 33 |
|
| 34 | |
| 35 | |
| 36 | @@ -26,7 +26,11 @@
|
| 37 | #include <stdlib.h>
|
| 38 | #include <string.h>
|
| 39 | #include <errno.h>
|
| 40 | +#ifdef __GNU_LIBRARY__
|
| 41 | #include <error.h>
|
| 42 | +#else
|
| 43 | +#include "hidrd/util/error_at_line.h"
|
| 44 | +#endif
|
| 45 | #include <stdio.h>
|
| 46 | #include "hidrd/opt/spec_list.h"
|
| 47 | #include "hidrd/opt/list.h"
|
| 48 | |
| 49 | |
| 50 | @@ -23,7 +23,11 @@
|
| 51 | */
|
| 52 |
|
| 53 | #include <assert.h>
|
| 54 | +#ifdef __GNU_LIBRARY__
|
| 55 | #include <error.h>
|
| 56 | +#else
|
| 57 | +#include "hidrd/util/error_at_line.h"
|
| 58 | +#endif
|
| 59 | #include <stdlib.h>
|
| 60 | #include <string.h>
|
| 61 | #include "hidrd/util/num.h"
|
| 62 | |
| 63 | |
| 64 | @@ -24,7 +24,11 @@
|
| 65 |
|
| 66 | #include <assert.h>
|
| 67 | #include <errno.h>
|
| 68 | +#ifdef __GNU_LIBRARY__
|
| 69 | #include <error.h>
|
| 70 | +#else
|
| 71 | +#include "hidrd/util/error_at_line.h"
|
| 72 | +#endif
|
| 73 | #include <stdlib.h>
|
| 74 | #include <string.h>
|
| 75 | #include <stdio.h>
|
| 76 | |
| 77 | |
| 78 | @@ -36,7 +36,7 @@
|
| 79 |
|
| 80 | TESTS = hidrd_opt_test
|
| 81 |
|
| 82 | -hidrd_opt_test_SOURCES = test.c
|
| 83 | +hidrd_opt_test_SOURCES = test.c ../util/error_at_line.c
|
| 84 | hidrd_opt_test_LDADD = $(lib_LTLIBRARIES)
|
| 85 |
|
| 86 | bin_PROGRAMS =
|
| 87 | |
| 88 | |
| 89 | @@ -39,10 +39,10 @@
|
| 90 | bin_PROGRAMS =
|
| 91 | check_PROGRAMS = hidrd_num_test hidrd_ttbl_test
|
| 92 |
|
| 93 | -hidrd_num_test_SOURCES = num_test.c
|
| 94 | +hidrd_num_test_SOURCES = num_test.c error_at_line.c
|
| 95 | hidrd_num_test_LDADD = $(lib_LTLIBRARIES)
|
| 96 |
|
| 97 | -hidrd_ttbl_test_SOURCES = ttbl_test.c
|
| 98 | +hidrd_ttbl_test_SOURCES = ttbl_test.c error_at_line.c
|
| 99 | hidrd_ttbl_test_LDADD = $(lib_LTLIBRARIES)
|
| 100 |
|
| 101 | TESTS = hidrd_num_test hidrd_ttbl_test
|
| 102 | |
| 103 | |
| 104 | @@ -0,0 +1,69 @@
|
| 105 | +#include "hidrd/util/error_at_line.h"
|
| 106 | +#include <errno.h>
|
| 107 | +#include <stdarg.h>
|
| 108 | +#include <stdio.h>
|
| 109 | +#include <stdlib.h>
|
| 110 | +#include <string.h>
|
| 111 | +
|
| 112 | +void (*error_print_progname) (void) = 0;
|
| 113 | +unsigned int error_message_count = 0;
|
| 114 | +int error_one_per_line = 0;
|
| 115 | +
|
| 116 | +static unsigned int saved_linenum = 0;
|
| 117 | +static const char *saved_file = 0;
|
| 118 | +
|
| 119 | +static void errorv(int status, int errnum, const char *file, unsigned int linenum, const char *fmt, va_list ap)
|
| 120 | +{
|
| 121 | + ++error_message_count;
|
| 122 | +
|
| 123 | + fflush(stdout);
|
| 124 | + flockfile(stderr);
|
| 125 | +
|
| 126 | + if (error_print_progname)
|
| 127 | + error_print_progname();
|
| 128 | + else {
|
| 129 | + fprintf(stderr, "%s:", program_invocation_name);
|
| 130 | + if (!file)
|
| 131 | + fputc(' ', stderr);
|
| 132 | + }
|
| 133 | +
|
| 134 | + if (file)
|
| 135 | + fprintf(stderr, "%s:%u: ", file, linenum);
|
| 136 | +
|
| 137 | + vfprintf(stderr, fmt, ap);
|
| 138 | + if (errnum)
|
| 139 | + fprintf(stderr, ": %s", strerror(errnum));
|
| 140 | + fputc('\n', stderr);
|
| 141 | +
|
| 142 | + funlockfile(stderr);
|
| 143 | +
|
| 144 | + if (status)
|
| 145 | + exit(status);
|
| 146 | +}
|
| 147 | +
|
| 148 | +void error(int status, int errnum, const char *fmt, ...)
|
| 149 | +{
|
| 150 | + va_list ap;
|
| 151 | + va_start(ap, fmt);
|
| 152 | + errorv(status, errnum, NULL, 0, fmt, ap);
|
| 153 | + va_end(ap);
|
| 154 | +}
|
| 155 | +
|
| 156 | +void error_at_line(int status, int errnum, const char *file, unsigned int linenum, const char *fmt, ...)
|
| 157 | +{
|
| 158 | + if (error_one_per_line) {
|
| 159 | + if(saved_linenum == linenum && file != NULL &&
|
| 160 | + saved_file != NULL && !strcmp(file, saved_file))
|
| 161 | + return;
|
| 162 | + saved_linenum = linenum;
|
| 163 | + // Assuming that the lifetime of the passed in file name extends
|
| 164 | + // until the next call is rather questionable, but appears to be
|
| 165 | + // the expected semantics.
|
| 166 | + saved_file = file;
|
| 167 | + }
|
| 168 | +
|
| 169 | + va_list ap;
|
| 170 | + va_start(ap, fmt);
|
| 171 | + errorv(status, errnum, file, linenum, fmt, ap);
|
| 172 | + va_end(ap);
|
| 173 | +}
|
| 174 | |
| 175 | |
| 176 | @@ -0,0 +1,21 @@
|
| 177 | +#ifndef _ERROR_H
|
| 178 | +#define _ERROR_H
|
| 179 | +
|
| 180 | +#include <features.h>
|
| 181 | +
|
| 182 | +#ifdef __cplusplus
|
| 183 | +extern "C" {
|
| 184 | +#endif
|
| 185 | +
|
| 186 | +extern void (*error_print_progname) (void);
|
| 187 | +extern unsigned int error_message_count;
|
| 188 | +extern int error_one_per_line;
|
| 189 | +
|
| 190 | +void error(int, int, const char *, ...);
|
| 191 | +void error_at_line(int, int, const char *, unsigned int, const char *, ...);
|
| 192 | +
|
| 193 | +#ifdef __cplusplus
|
| 194 | +}
|
| 195 | +#endif
|
| 196 | +
|
| 197 | +#endif
|
| 198 |
|