From cfc081f9c4f5f538145cbd93f98f714d0be1dad0 Mon Sep 17 00:00:00 2001 From: Mattias Andrée Date: Sat, 3 Apr 2021 11:06:36 +0200 Subject: m + Add multicall binary MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Mattias Andrée --- .gitignore | 3 +++ Makefile | 47 +++++++++++++++++++++++++++++++++++++++-------- TODO | 1 - common.h | 20 +++++++++++++++++--- config.mk | 4 ++-- contacts.c.in | 30 ++++++++++++++++++++++++++++++ 6 files changed, 91 insertions(+), 14 deletions(-) create mode 100644 contacts.c.in diff --git a/.gitignore b/.gitignore index fb34b0a..1411d9a 100644 --- a/.gitignore +++ b/.gitignore @@ -6,6 +6,7 @@ *.so.* *.su *.lo +*.bo /find-contact-by-email /find-contact-by-name /find-contact-by-pgpkey @@ -30,3 +31,5 @@ /set-contact-organisations /set-contact-pgpkeys /set-contact-sites +/contacts +/contacts.c diff --git a/Makefile b/Makefile index 86040fa..362ada4 100644 --- a/Makefile +++ b/Makefile @@ -32,18 +32,28 @@ HDR =\ common.h OBJ = $(BIN:=.o) +BOBJ = $(BIN:=.bo) -all: $(BIN) +all: $(BIN) contacts $(OBJ): $(@:.o=.c) $(HDR) - -libcontacts.a: $(OBJ) - $(AR) rc $@ $(OBJ) - $(AR) -s $@ +$(BOBJ): $(@:.bo=.c) $(HDR) .c.o: $(CC) -c -o $@ $< $(CFLAGS) $(CPPFLAGS) +.c.bo: + $(CC) -c -o $@ $< $(CFLAGS) $(CPPFLAGS) -Dmain="$$(printf '%s\n' $*_main | tr - _)" -DMULTICALL_BINARY + +contacts: contacts.o $(BOBJ) + $(CC) -o $@ $@.o $(BOBJ) $(LDFLAGS) + +contacts.c: contacts.c.in Makefile + printf '#define LIST_COMMANDS' > $@ + printf '\\\n\tX(%s)' $(BIN) | tr - _ >> $@ + printf '\n\n' >> $@ + cat contacts.c.in >> $@ + find-contact-by-email: find-contact-by-email.o $(CC) -o $@ $@.o $(LDFLAGS) @@ -120,13 +130,34 @@ install: $(BIN) mkdir -p -- "$(DESTDIR)$(PREFIX)/bin" cp -- $(BIN) "$(DESTDIR)$(PREFIX)/bin/" +install-mcb: contacts + mkdir -p -- "$(DESTDIR)$(PREFIX)/bin" + set -- $(BIN) &&\ + cp -- "$$1" "$(DESTDIR)$(PREFIX)/bin/$$1" &&\ + linkto="$$1" &&\ + shift 1 &&\ + cd -- "$(DESTDIR)$(PREFIX)/bin/" &&\ + for f; do\ + ln -- "$$linkto" "$$f" || exit 1;\ + done + +install-mcb-symlinks: contacts + mkdir -p -- "$(DESTDIR)$(PREFIX)/lib" + mkdir -p -- "$(DESTDIR)$(PREFIX)/bin" + cp -- contacts "$(DESTDIR)$(PREFIX)/lib/" + cd -- "$(DESTDIR)$(PREFIX)/bin/" &&\ + for f in $(BIN); do\ + ln -s -- ../lib/contacts "$$f" || exit 1;\ + done + uninstall: -cd -- "$(DESTDIR)$(PREFIX)/bin" && rm -f -- $(BIN) + -rm -f -- "$(DESTDIR)$(PREFIX)/lib/contacts" clean: - -rm -f -- *.o *.a *.lo *.so *.su $(BIN) + -rm -f -- *.o *.a *.lo *.so *.bo *.su $(BIN) contacts contacts.c .SUFFIXES: -.SUFFIXES: .c .o +.SUFFIXES: .c .o .bo -.PHONY: all install uninstall clean +.PHONY: all install install-mcb install-mcb-symlinks uninstall clean diff --git a/TODO b/TODO index 33d0025..33e964e 100644 --- a/TODO +++ b/TODO @@ -27,4 +27,3 @@ Test set-contact-sites Add man pages Add readme Add tests -Add multicall binary diff --git a/common.h b/common.h index 14c3319..f4e1dad 100644 --- a/common.h +++ b/common.h @@ -1,4 +1,18 @@ /* See LICENSE file for copyright and license details. */ -#include "libcontacts.h" -#include "libsimple.h" -#include "libsimple-arg.h" +#include +#include +#include + + +#ifdef MULTICALL_BINARY +# undef NUSAGE +# define NUSAGE(STATUS, SYNOPSIS)\ + static _LIBSIMPLE_NORETURN void\ + usage(void)\ + {\ + const char *syn = SYNOPSIS ? SYNOPSIS : "";\ + fprintf(stderr, "usage: %s%s%s\n", argv0, *syn ? " " : "", syn);\ + exit(STATUS);\ + }\ + extern char *argv0 +#endif diff --git a/config.mk b/config.mk index 34514f2..2c119c1 100644 --- a/config.mk +++ b/config.mk @@ -3,6 +3,6 @@ MANPREFIX = $(PREFIX)/share/man CC = cc -CPPFLAGS = -D_DEFAULT_SOURCE -D_BSD_SOURCE -D_XOPEN_SOURCE=700 +CPPFLAGS = -D_DEFAULT_SOURCE -D_BSD_SOURCE -D_XOPEN_SOURCE=700 -I../libcontacts CFLAGS = -std=c99 -Wall -O2 -LDFLAGS = -s -lcontacts -lsimple +LDFLAGS = -s -L../libcontacts -lcontacts -lsimple diff --git a/contacts.c.in b/contacts.c.in new file mode 100644 index 0000000..5e470ac --- /dev/null +++ b/contacts.c.in @@ -0,0 +1,30 @@ +/* See LICENSE file for copyright and license details. */ +#include +#include + +#define X(NAM) int NAM##_main(int, char *[]); +LIST_COMMANDS +#undef X + +char *argv0 = NULL; + +int +main(int argc, char *argv[]) +{ + char *name, *p; + + name = strrchr(*argv, '/'); + if (!name++) + name = *argv; + for (p = name; (p = strchr(p, '-'));) + *p++ = '_'; + +#define X(NAM)\ + if (!strcmp(name, #NAM))\ + return NAM##_main(argc, argv); + LIST_COMMANDS; +#undef X + + fprintf(stderr, "%s: unrecognised command for multicall binary\n", *argv); + return 127; +} -- cgit v1.2.3-70-g09d2