sbase

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

mkbox (1723B)


      1 #!/bin/sh
      2 
      3 trap "rm -rf build" INT QUIT TERM
      4 
      5 rm -rf build
      6 mkdir -p build
      7 
      8 cp *.h build
      9 
     10 cat > build/sbase-box.c <<EOF
     11 #include <unistd.h>
     12 
     13 #include <libgen.h>
     14 #include <errno.h>
     15 #include <stdio.h>
     16 #include <stdlib.h>
     17 #include <string.h>
     18 
     19 #include "util.h"
     20 #include "sbase-box.h"
     21 
     22 struct cmd {
     23 	char *name;
     24 	int (*fn)(int, char **);
     25 } cmds[] = {
     26 	{"install", xinstall_main},
     27 	{"[", test_main},
     28 $(grep -l ^main *.c |
     29 while read f
     30 do
     31 	sed -n '
     32 	/^main/ {
     33 		s/main/'${f%.c}'_main/
     34 		s/-/_/g
     35 		w build/'$f'
     36 		s/\(^.*\)(.*)/	{"'${f%.c}'", \1},/p
     37 		d
     38 	}
     39 	w 'build/$f $f
     40 done)
     41 	{NULL},
     42 };
     43 
     44 static void
     45 install(char *path)
     46 {
     47 	int r;
     48 	struct cmd *bp;
     49 	char fname[FILENAME_MAX];
     50 
     51 	if (path == NULL) {
     52 		fputs("sbase-box [-i path] [command]\n", stderr);
     53 		exit(1);
     54 	}
     55 
     56 	for (bp = cmds; bp->name; ++bp) {
     57 		r = snprintf(fname, sizeof(fname), "%s/%s", path, bp->name);
     58 		if (r < 0 || r >= sizeof(fname)) {
     59 			fprintf(stderr,
     60 			        "sbase-box: destination path truncated for '%s'\n",
     61 			        bp->name);
     62 			exit(1);
     63 		}
     64 		remove(fname);
     65 		if (symlink("sbase-box", fname) < 0) {
     66 			fprintf(stderr,
     67 			        "sbase-box: %s: %s\n",
     68 			        bp->name, strerror(errno));
     69 			exit(1);
     70 		}
     71 	}
     72 }
     73 
     74 int
     75 main(int argc, char *argv[])
     76 {
     77 	char *s = basename(argv[0]);
     78 	struct cmd *bp;
     79 
     80 	if (!strcmp(s, "sbase-box") && argc > 1) {
     81 		argc--; argv++;
     82 		if (strcmp(argv[0], "-i") == 0) {
     83 			install(argv[1]);
     84 			exit(0);
     85 		}
     86 		s = basename(argv[0]);
     87 	}
     88 
     89 	for (bp = cmds; bp->name; ++bp) {
     90 		if (strcmp(bp->name, s) == 0)
     91 			return (*bp->fn)(argc, argv);
     92 	}
     93 
     94 	for (bp = cmds; bp->name; ++bp)
     95 		printf("%s ", bp->name);
     96 	putchar('\n');
     97 
     98 	return 0;
     99 }
    100 EOF
    101 
    102 sed -n 's/.* \(.*_main\).*/int \1(int, char **);/p'\
    103 	build/sbase-box.c > build/sbase-box.h