aruu

[MIRROR] Actual replacement for Busybox | suckless
git clone https://git.daat.foo/aruu.git
Log | Files | Refs | README | LICENSE

genbox.sh (6255B)


      1 #!/bin/sh
      2 # genbox.sh - generate the aruu-box dispatch table
      3 #
      4 # given the BOX_OBJ output paths (".box/cmd/<category>/<tool>.o") on
      5 # argv, print the c source of the dispatch table and main() on stdout
      6 #
      7 set -e
      8 
      9 [ $# -gt 0 ] || {
     10         printf "genbox: no object paths given\n" >&2
     11         exit 1
     12 }
     13 
     14 # derive category, tool and c symbol from one BOX_OBJ output path. the
     15 # transform (tr '.-' '__', append _main) must match the BOX_OBJ objcopy
     16 # step, see the CATEGORY_OF comment in ninqu.rules
     17 path_category() {
     18         rest=${1#.box/cmd/}
     19         printf "%s\n" "${rest%%/*}"
     20 }
     21 
     22 path_tool() {
     23         rest=${1##*/}
     24         printf "%s\n" "${rest%.o}"
     25 }
     26 
     27 path_symbol() {
     28         tool=$(path_tool "$1")
     29         printf "%s\n" "$(echo "$tool" | tr '.-' '__')_main"
     30 }
     31 
     32 cat << 'HEAD'
     33 /* this file is autogenerated by genbox.sh. do not edit */
     34 
     35 # include "wexec.h"
     36 # include "util.h"
     37 
     38 # include <stdio.h>
     39 # include <stdlib.h>
     40 # include <string.h>
     41 # include <unistd.h>
     42 
     43 HEAD
     44 
     45 # extern decls, in the order ninqu passed the objects in (BOX_OBJ glob
     46 # order, one category at a time)
     47 for obj in "$@"; do
     48         sym=$(path_symbol "$obj")
     49         echo "int ${sym}(int, char **);"
     50 done
     51 
     52 cat << 'MID'
     53 
     54 struct Cmd {
     55         const char *name;
     56         int (*fn)(int, char **);
     57         const char *category;
     58 };
     59 
     60 static struct Cmd cmds[] = {
     61 MID
     62 
     63 categories=$(
     64         for obj in "$@"; do
     65                 path_category "$obj"
     66         done | sort -u
     67 )
     68 
     69 for category in $categories; do
     70         for obj in "$@"; do
     71                 [ "$(path_category "$obj")" = "$category" ] || continue
     72                 tool=$(path_tool "$obj")
     73                 sym=$(path_symbol "$obj")
     74                 printf '\t{ "%s", %s, "%s" },\n' "$tool" "$sym" "$category"
     75                 if [ "$tool" = "test" ]; then
     76                         printf '\t{ "[", %s, "%s" },\n' "$sym" "$category"
     77                 fi
     78                 if [ "$tool" = "pgrep" ]; then
     79                         printf '\t{ "pkill", %s, "%s" },\n' "$sym" "$category"
     80                 fi
     81                 if [ "$tool" = "fsck.fat" ]; then
     82                         printf '\t{ "fsck.vfat", %s, "%s" },\n' "$sym" "$category"
     83                 fi
     84                 if [ "$tool" = "grep" ]; then
     85                         printf '\t{ "egrep", %s, "%s" },\n' "$sym" "$category"
     86                         printf '\t{ "fgrep", %s, "%s" },\n' "$sym" "$category"
     87                 fi
     88                 if [ "$tool" = "mount" ]; then
     89                         printf '\t{ "mount.iso9660", %s, "%s" },\n' "$sym" "$category"
     90                         printf '\t{ "mount.ext4", %s, "%s" },\n' "$sym" "$category"
     91                         printf '\t{ "mount.ext3", %s, "%s" },\n' "$sym" "$category"
     92                         printf '\t{ "mount.ext2", %s, "%s" },\n' "$sym" "$category"
     93                         printf '\t{ "mount.vfat", %s, "%s" },\n' "$sym" "$category"
     94                         printf '\t{ "mount.f2fs", %s, "%s" },\n' "$sym" "$category"
     95                         printf '\t{ "mount.auto", %s, "%s" },\n' "$sym" "$category"
     96                 fi
     97         done
     98 done
     99 
    100 cat << 'TAIL'
    101         { NULL, NULL, NULL }
    102 };
    103 
    104 static void
    105 usage(void)
    106 {
    107         struct Cmd *c;
    108         const char *last;
    109         int col;
    110 
    111         last = NULL;
    112         col = 0;
    113         fprintf(stderr, "usage: %s [-i path] [command [args...]]\n", argv0);
    114         for (c = cmds; c->name; c++) {
    115                 if (!last || strcmp(c->category, last) != 0) {
    116                         if (last)
    117                                 fprintf(stderr, "\n\n");
    118                         last = c->category;
    119                         fprintf(stderr, "%s:\n", last);
    120                         col = 0;
    121                 }
    122                 if (col == 0) {
    123                         fprintf(stderr, "\t%s", c->name);
    124                         col = 8 + strlen(c->name);
    125                 } else if (col + 2 + strlen(c->name) >= 78) {
    126                         fprintf(stderr, ",\n\t%s", c->name);
    127                         col = 8 + strlen(c->name);
    128                 } else {
    129                         fprintf(stderr, ", %s", c->name);
    130                         col += 2 + strlen(c->name);
    131                 }
    132         }
    133         if (last)
    134                 fputc('\n', stderr);
    135         exit(1);
    136 }
    137 
    138 static void
    139 x_install(const char *path)
    140 {
    141         struct Cmd *c;
    142         char buf[4096];
    143         int r;
    144 
    145         for (c = cmds; c->name; c++) {
    146                 r = snprintf(buf, sizeof buf, "%s/%s", path, c->name);
    147                 if (r < 0 || r >= (int)sizeof buf)
    148                         eprintf("install: '%s/%s': path too long\n", path, c->name);
    149                 remove(buf);
    150                 if (symlink("aruu-box", buf) < 0)
    151                         eprintf("install: symlink '%s':", buf);
    152         }
    153 }
    154 
    155 int
    156 main(int argc, char *argv[])
    157 {
    158         struct Cmd *c;
    159         char *s;
    160         int dash;
    161 
    162         argv0 = argv[0];
    163 
    164         for (c = cmds; c->name; c++)
    165                 wexec_register(c->name, c->fn);
    166 
    167         s = strrchr(argv[0], '/');
    168         if (s)
    169                 s++;
    170         else
    171                 s = argv[0];
    172 
    173         if (strcmp(s, "aruu-box") == 0) {
    174                 if (argc < 2)
    175                         usage();
    176                 argc--;
    177                 argv++;
    178                 if (strcmp(argv[0], "-i") == 0) {
    179                         if (argc < 2)
    180                                 eprintf("usage: aruu-box -i path\n");
    181                         x_install(argv[1]);
    182                         return 0;
    183                 }
    184                 s = strrchr(argv[0], '/');
    185                 if (s)
    186                         s++;
    187                 else
    188                         s = argv[0];
    189         }
    190         /* login exec's a shell with a '-' prefixed to argv[0]; match the
    191            applet without it, and leave argv[0] alone so the applet can
    192            still see it was invoked as a login shell */
    193         dash = (*s == '-');
    194         if (dash)
    195                 s++;
    196 
    197         for (c = cmds; c->name; c++) {
    198                 if (strcmp(c->name, s) == 0) {
    199                         argv0 = (char *)c->name;
    200                         if (!dash)
    201                                 argv[0] = (char *)c->name;
    202                         return c->fn(argc, argv);
    203                 }
    204         }
    205         enprintf(127, "%s: not found\n", s);
    206         return 1;
    207 }
    208 TAIL