sbase

suckless unix tools
git clone git://git.suckless.org/sbase
Log | Files | Refs | README | LICENSE

commit 3c36fb417738b5830f9f22b0ac88a266dd6eed5b
parent 8ca12835a58ead392822bfd52241e68eba7cd99f
Author: Roberto E. Vargas Caballero <k0ga@shike2.com>
Date:   Tue, 26 Sep 2023 19:26:47 +0200

sbase-box: Simplify Makefile rule

The Makefile rule was too complex and these cases is better to just
move it to a script where will be eassier to use sed properly
and not looping over all the files 4 times.

Diffstat:
M.gitignore | 1+
MMakefile | 23+++--------------------
Ascripts/mkbox | 66++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
3 files changed, 70 insertions(+), 20 deletions(-)

diff --git a/.gitignore b/.gitignore @@ -1,4 +1,5 @@ *.o +/build /getconf.h /libutf.a /libutil.a diff --git a/Makefile b/Makefile @@ -236,27 +236,9 @@ dist: clean gzip sbase-$(VERSION).tar rm -rf sbase-$(VERSION) -sbase-box: $(LIB) $(SRC) getconf.h - mkdir -p build - cp $(HDR) build - cp getconf.h build - for f in $(SRC); do sed "s/^main(/$$(echo "$${f%.c}" | sed s/-/_/g)_&/" < $$f > build/$$f; done - echo '#include <libgen.h>' > build/$@.c - echo '#include <stdio.h>' >> build/$@.c - echo '#include <stdlib.h>' >> build/$@.c - echo '#include <string.h>' >> build/$@.c - echo '#include "util.h"' >> build/$@.c - for f in $(SRC); do echo "int $$(echo "$${f%.c}" | sed s/-/_/g)_main(int, char **);"; done >> build/$@.c - echo 'int main(int argc, char *argv[]) { char *s = basename(argv[0]);' >> build/$@.c - echo 'if(!strcmp(s,"sbase-box")) { argc--; argv++; s = basename(argv[0]); } if(0) ;' >> build/$@.c - echo "else if (!strcmp(s, \"install\")) return xinstall_main(argc, argv);" >> build/$@.c - echo "else if (!strcmp(s, \"[\")) return test_main(argc, argv);" >> build/$@.c - for f in $(SRC); do echo "else if(!strcmp(s, \"$${f%.c}\")) return $$(echo "$${f%.c}" | sed s/-/_/g)_main(argc, argv);"; done >> build/$@.c - echo 'else { fputs("[ ", stdout);' >> build/$@.c - for f in $(SRC); do echo "fputs(\"$${f%.c} \", stdout);"; done >> build/$@.c - echo 'putchar(0xa); }; return 0; }' >> build/$@.c +sbase-box: $(BIN) + scripts/mkbox $(CC) $(CFLAGS) $(CPPFLAGS) $(LDFLAGS) -o $@ build/*.c $(LIB) - rm -r build sbase-box-install: sbase-box mkdir -p $(DESTDIR)$(PREFIX)/bin @@ -276,5 +258,6 @@ sbase-box-uninstall: uninstall clean: rm -f $(BIN) $(OBJ) $(LIB) sbase-box sbase-$(VERSION).tar.gz rm -f getconf.h + rm -rf build .PHONY: all install uninstall dist sbase-box-install sbase-box-uninstall clean diff --git a/scripts/mkbox b/scripts/mkbox @@ -0,0 +1,66 @@ +#!/bin/sh + +trap "rm -rf build" INT QUIT TERM + +rm -rf build +mkdir -p build + +cp *.h build + +cat > build/sbase-box.c <<EOF +#include <libgen.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> + +#include "util.h" +#include "sbase-box.h" + +struct cmd { + char *name; + int (*fn)(int, char **); +} cmds[] = { + {"install", xinstall_main}, + {"[", test_main}, +$(grep -l ^main *.c | +while read f +do + sed -n ' + /^main/ { + s/main/'${f%.c}'_main/ + s/-/_/g + w build/'$f' + s/\(^.*\)(.*)/ {"'${f%.c}'", \1},/p + d + } + w 'build/$f $f +done) + {NULL}, +}; + +int +main(int argc, char *argv[]) +{ + char *s = basename(argv[0]); + struct cmd *bp; + + if(!strcmp(s,"sbase-box")) { + argc--; argv++; + s = basename(argv[0]); + } + + for (bp = cmds; bp->name; ++bp) { + if (strcmp(bp->name, s) == 0) + return (*bp->fn)(argc, argv); + } + + for (bp = cmds; bp->name; ++bp) + printf("%s ", bp->name); + putchar('\n'); + + return 0; +} +EOF + +sed -n 's/.* \(.*_main\).*/int \1(int, char **);/p'\ + build/sbase-box.c > build/sbase-box.h