diff options
-rw-r--r-- | Makefile | 4 | ||||
-rw-r--r-- | arg.h | 38 | ||||
-rw-r--r-- | common.h | 25 | ||||
-rw-r--r-- | config.mk | 2 | ||||
-rw-r--r-- | makel.c | 10 | ||||
-rw-r--r-- | util.c | 61 |
6 files changed, 133 insertions, 7 deletions
@@ -7,9 +7,11 @@ OBJ =\ makel.o\ makefile.o\ text.o\ - ui.o + ui.o\ + util.o HDR =\ + arg.h\ common.h all: makel @@ -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 @@ -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 *, ...); @@ -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 @@ -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': @@ -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); +} |