summaryrefslogtreecommitdiff
path: root/err.c
diff options
context:
space:
mode:
authorLeo Tenenbaum <pommicket@gmail.com>2019-09-27 23:34:29 -0400
committerLeo Tenenbaum <pommicket@gmail.com>2019-09-27 23:34:29 -0400
commit991d739bb02b3506da5a0af0edd9a4fd505373a3 (patch)
tree75b32cd37e9a99f5609171f2a9156d2f2dc3f13f /err.c
parent685460f382e2e7039bd6ddf5c336abdb803a0372 (diff)
added converter from named arguments to non-named
Diffstat (limited to 'err.c')
-rw-r--r--err.c129
1 files changed, 129 insertions, 0 deletions
diff --git a/err.c b/err.c
new file mode 100644
index 0000000..ac620ec
--- /dev/null
+++ b/err.c
@@ -0,0 +1,129 @@
+#define USE_COLORED_TEXT 1
+
+#if USE_COLORED_TEXT
+#define TEXT_ERROR(x) "\x1b[91m" x "\x1b[0m"
+#define TEXT_INFO(x) "\x1b[94m" x "\x1b[0m"
+#define TEXT_WARN(x) "\x1b[93m" x "\x1b[0m"
+#define TEXT_IMPORTANT(x) "\x1b[1m" x "\x1b[0m"
+#else
+#define TEXT_ERROR(x) x
+#define TEXT_INFO(x) x
+#define TEXT_WARN(x) x
+#define TEXT_IMPORTANT(x) x
+#endif
+
+static inline const char *ordinals(size_t x) {
+ switch (x % 10) {
+ case 1: return "st";
+ case 2: return "nd";
+ case 3: return "rd";
+ default: return "th";
+ }
+}
+
+/* Write directly to the error file */
+static void err_fwrite(const void *data, size_t size, size_t n) {
+ fwrite(data, size, n, stderr);
+}
+
+static void err_fprint(const char *fmt, ...) {
+ va_list args;
+ va_start(args, fmt);
+ vfprintf(stderr, fmt, args);
+ va_end(args);
+}
+
+static void err_vfprint(const char *fmt, va_list args) {
+ vfprintf(stderr, fmt, args);
+}
+
+static void err_print_header_(Location where) {
+ err_fprint(TEXT_ERROR("error:") " at line %lu of %s:\n", (unsigned long)where.line, where.filename);
+}
+
+static void info_print_header_(Location where) {
+ err_fprint(TEXT_INFO("info:") " at line %lu of %s:\n", (unsigned long)where.line, where.filename);
+}
+
+static void warn_print_header_(Location where) {
+ err_fprint(TEXT_WARN("warning:") " at line %lu of %s:\n", (unsigned long)where.line, where.filename);
+}
+
+static void err_print_footer_(const char *context) {
+ const char *end = strchr(context, '\n');
+ int has_newline = end != NULL;
+ if (!has_newline)
+ end = strchr(context, '\0');
+ assert(end);
+ err_fprint("\n\there: --> ");
+ err_fwrite(context, 1, (size_t)(end - context));
+ if (!has_newline)
+ err_fprint("<end of file>");
+ err_fprint("\n");
+}
+
+/* Write nicely-formatted errors to the error file */
+
+
+static void err_vprint(Location where, const char *fmt, va_list args) {
+ err_print_header_(where);
+ err_vfprint(fmt, args);
+ err_print_footer_(where.code);
+}
+
+static void err_print_(int line, const char *file, Location where, const char *fmt, ...) {
+ va_list args;
+ if (file)
+ err_fprint("Generated by line %d of %s:\n", line, file);
+ va_start(args, fmt);
+ err_vprint(where, fmt, args);
+ va_end(args);
+}
+
+#define err_print(...) err_print_(__LINE__, __FILE__, __VA_ARGS__)
+
+static void info_print(Location where, const char *fmt, ...) {
+ va_list args;
+ va_start(args, fmt);
+ info_print_header_(where);
+ err_vfprint(fmt, args);
+ err_print_footer_(where.code);
+ va_end(args);
+}
+
+static void warn_print(Location where, const char *fmt, ...) {
+ va_list args;
+ va_start(args, fmt);
+ warn_print_header_(where);
+ err_vfprint(fmt, args);
+ err_print_footer_(where.code);
+ va_end(args);
+}
+
+static void *err_malloc(size_t size) {
+ void *ret = malloc(size);
+ if (!ret) {
+ fprintf(stderr, "Error: Out of memory.\n");
+ abort();
+ }
+ return ret;
+}
+
+/* static void *err_calloc(size_t n, size_t size) { */
+/* void *ret = calloc(n, size); */
+/* if (!ret) { */
+/* fprintf(stderr, "Error: Out of memory.\n"); */
+/* abort(); */
+/* } */
+/* return ret; */
+/* } */
+
+static void *err_realloc(void *data, size_t new_size) {
+ void *ret = realloc(data, new_size);
+ if (!ret) {
+ fprintf(stderr, "Error: Out of memory.\n");
+ abort();
+ }
+ return ret;
+}
+