summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMattias Andrée <maandree@kth.se>2022-01-04 21:15:13 +0100
committerMattias Andrée <maandree@kth.se>2022-01-04 21:15:13 +0100
commitcb0d932597377a6dbc595dbe13f55c9a2cf1ad00 (patch)
tree80ec6488e06a260229acb8a45afc44f5c84a3a48
parentRename to makel (diff)
downloadmakel-cb0d932597377a6dbc595dbe13f55c9a2cf1ad00.tar.gz
makel-cb0d932597377a6dbc595dbe13f55c9a2cf1ad00.tar.bz2
makel-cb0d932597377a6dbc595dbe13f55c9a2cf1ad00.tar.xz
Replace dependency on libsimple with boilerplate code
Signed-off-by: Mattias Andrée <maandree@kth.se>
-rw-r--r--Makefile4
-rw-r--r--arg.h38
-rw-r--r--common.h25
-rw-r--r--config.mk2
-rw-r--r--makel.c10
-rw-r--r--util.c61
6 files changed, 133 insertions, 7 deletions
diff --git a/Makefile b/Makefile
index 13b29b2..864b5a5 100644
--- a/Makefile
+++ b/Makefile
@@ -7,9 +7,11 @@ OBJ =\
makel.o\
makefile.o\
text.o\
- ui.o
+ ui.o\
+ util.o
HDR =\
+ arg.h\
common.h
all: makel
diff --git a/arg.h b/arg.h
new file mode 100644
index 0000000..1231320
--- /dev/null
+++ b/arg.h
@@ -0,0 +1,38 @@
+/* Trivial code, not subject to copyright, use as you see fit.
+ * Reimplementation of 20h's arg.h */
+
+#ifndef ARG_H
+#define ARG_H
+
+#include <stddef.h>
+
+
+extern const char *argv0;
+
+
+#define ARGBEGIN do {\
+ char arg_h_flag_, arg_h_break_;\
+ if (!argc)\
+ break;\
+ argv0 = argv[0];\
+ while (--argc, *++argv && argv[0][0] == '-' && argv[0][1]) {\
+ if (argv[0][1] == '-' && !argv[0][2]) {\
+ argv++;\
+ argc--;\
+ break;\
+ }\
+ for (arg_h_break_ = 0; !arg_h_break_ && *++*argv;) {\
+ switch ((arg_h_flag_ = **argv))
+
+#define ARGEND }\
+ }\
+ } while (0)
+
+
+#define FLAG() (arg_h_flag_)
+
+#define ARG() (arg_h_break_ = 1, argv[0][1] ? &argv[0][1] :\
+ argv[1] ? (argc--, *++argv) :\
+ (usage(), NULL))
+
+#endif
diff --git a/common.h b/common.h
index 35988cc..8d2a5f4 100644
--- a/common.h
+++ b/common.h
@@ -1,10 +1,19 @@
/* See LICENSE file for copyright and license details. */
+#include <ctype.h>
+#include <errno.h>
+#include <fcntl.h>
#include <locale.h>
+#include <stdarg.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+#include <wchar.h>
-#include <libsimple.h>
-#include <libsimple-arg.h>
#include <grapheme.h>
+#include "arg.h"
+
#if defined(__GNUC__)
# pragma GCC diagnostic ignored "-Wsuggest-attribute=format"
@@ -24,6 +33,10 @@
#define EXIT_ERROR 8
+#define ELEMSOF(ARRAY) (sizeof(ARRAY) / sizeof(*(ARRAY)))
+#define MAX(A, B) ((A) > (B) ? (A) : (B))
+
+
#define LIST_WARNING_CLASSES(X)\
X(WC_MAKEFILE, "makefile", INFORM)\
X(WC_EXTRA_MAKEFILE, "extra-makefile", WARN)\
@@ -121,3 +134,11 @@ void xprintwarningf(enum warning_class class, int severity, const char *fmt, ...
#define warnf_undefined(CLASS, ...) xprintwarningf(CLASS, EXIT_UNDEFINED, __VA_ARGS__)
void printerrorf(const char *fmt, ...);
void printtipf(enum warning_class class, const char *fmt, ...);
+
+
+/* util.c */
+void *erealloc(void *, size_t);
+void *ecalloc(size_t, size_t);
+void *emalloc(size_t);
+void *ememdup(const void *, size_t);
+void eprintf(const char *, ...);
diff --git a/config.mk b/config.mk
index bf92326..7c7782d 100644
--- a/config.mk
+++ b/config.mk
@@ -5,4 +5,4 @@ CC = cc
CPPFLAGS = -D_DEFAULT_SOURCE -D_BSD_SOURCE -D_XOPEN_SOURCE=700 -D_GNU_SOURCE
CFLAGS = -std=c99 -Wall -g
-LDFLAGS = -lsimple -lgrapheme
+LDFLAGS = -lgrapheme
diff --git a/makel.c b/makel.c
index 514be21..eeda146 100644
--- a/makel.c
+++ b/makel.c
@@ -1,7 +1,13 @@
/* See LICENSE file for copyright and license details. */
#include "common.h"
-NUSAGE(EXIT_ERROR, "[-f makefile]");
+const char *argv0 = "makel";
+
+static void
+usage(void) {
+ fprintf(stderr, "%s [-f makefile]\n", argv0);
+ exit(EXIT_ERROR);
+}
int exit_status = 0;
@@ -149,8 +155,6 @@ main(int argc, char *argv[])
size_t nlines;
size_t i;
- libsimple_default_failure_exit = EXIT_ERROR;
-
/* make(1) shall support mixing of options and operands (up to --) */
ARGBEGIN {
case 'f':
diff --git a/util.c b/util.c
new file mode 100644
index 0000000..07ebfc0
--- /dev/null
+++ b/util.c
@@ -0,0 +1,61 @@
+/* See LICENSE file for copyright and license details. */
+#include "common.h"
+
+
+void *
+erealloc(void *ptr, size_t n)
+{
+ void *ret = realloc(ptr, n);
+ if (!ret)
+ eprintf("realloc %zu:", n);
+ return ret;
+}
+
+
+void *
+ecalloc(size_t n, size_t m)
+{
+ void *ret = calloc(n, m);
+ if (!ret)
+ eprintf("calloc %zu %zu:", n, m);
+ return ret;
+}
+
+
+void *
+emalloc(size_t n)
+{
+ void *ret = malloc(n);
+ if (!ret)
+ eprintf("malloc %zu:", n);
+ return ret;
+}
+
+
+void *
+ememdup(const void *ptr, size_t n)
+{
+ void *ret = emalloc(n);
+ memcpy(ret, ptr, n);
+ return ret;
+}
+
+
+void
+eprintf(const char *fmt, ...)
+{
+ va_list ap;
+ int err = errno;
+ char end = *fmt ? strchr(fmt, '\0')[-1] : '\0';
+ va_start(ap, fmt);
+ fprintf(stderr, "%s: ", argv0);
+ vfprintf(stderr, fmt, ap);
+ if (end == '\0')
+ fprintf(stderr, "%s\n", strerror(err));
+ else if (end == ':')
+ fprintf(stderr, " %s\n", strerror(err));
+ else if (end != '\n')
+ fprintf(stderr, "\n");
+ va_end(ap);
+ exit(EXIT_ERROR);
+}