From cb0d932597377a6dbc595dbe13f55c9a2cf1ad00 Mon Sep 17 00:00:00 2001 From: Mattias Andrée Date: Tue, 4 Jan 2022 21:15:13 +0100 Subject: Replace dependency on libsimple with boilerplate code MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Mattias Andrée --- Makefile | 4 +++- arg.h | 38 ++++++++++++++++++++++++++++++++++++++ common.h | 25 +++++++++++++++++++++++-- config.mk | 2 +- makel.c | 10 +++++++--- util.c | 61 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 133 insertions(+), 7 deletions(-) create mode 100644 arg.h create mode 100644 util.c 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 + + +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 +#include +#include #include +#include +#include +#include +#include +#include +#include -#include -#include #include +#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); +} -- cgit v1.2.3-70-g09d2