commit 3c36fb417738b5830f9f22b0ac88a266dd6eed5b
Author: Roberto E. Vargas Caballero <[email protected]>
AuthorDate: Tue Sep 26 19:26:47 2023 +0200
Commit: Roberto E. Vargas Caballero <[email protected]>
CommitDate: Wed Sep 27 07:10:05 2023 +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.
diff --git a/.gitignore b/.gitignore
index 15a18c8..c2504af 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,4 +1,5 @@
*.o
+/build
/getconf.h
/libutf.a
/libutil.a
diff --git a/Makefile b/Makefile
index 681eb4d..77858ed 100644
--- 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/[email protected]
- echo '#include <stdio.h>'
>> build/[email protected]
- echo '#include <stdlib.h>'
>> build/[email protected]
- echo '#include <string.h>'
>> build/[email protected]
- echo '#include "util.h"'
>> build/[email protected]
- for f in $(SRC); do echo "int $$(echo "$${f%.c}" | sed
s/-/_/g)_main(int, char **);"; done >>
build/[email protected]
- echo 'int main(int argc, char *argv[]) { char *s = basename(argv[0]);'
>> build/[email protected]
- echo 'if(!strcmp(s,"sbase-box")) { argc--; argv++; s =
basename(argv[0]); } if(0) ;' >>
build/[email protected]
- echo "else if (!strcmp(s, \"install\")) return xinstall_main(argc,
argv);" >> build/[email protected]
- echo "else if (!strcmp(s, \"[\")) return test_main(argc, argv);"
>> build/[email protected]
- for f in $(SRC); do echo "else if(!strcmp(s, \"$${f%.c}\")) return
$$(echo "$${f%.c}" | sed s/-/_/g)_main(argc, argv);"; done >> build/[email protected]
- echo 'else { fputs("[ ", stdout);'
>> build/[email protected]
- for f in $(SRC); do echo "fputs(\"$${f%.c} \", stdout);"; done
>> build/[email protected]
- echo 'putchar(0xa); }; return 0; }'
>> build/[email protected]
+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
new file mode 100755
index 0000000..92f428f
--- /dev/null
+++ 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