aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBruce Hill <bruce@bruce-hill.com>2020-02-24 02:57:12 -0800
committerBruce Hill <bruce@bruce-hill.com>2020-02-24 02:57:12 -0800
commit1dbece8b2ef96eb03ccbc7f6b5e794d66a647d38 (patch)
treefdcc7599c23799bc6a8c7b1610ff94969743802d
parent9a7938ba2d0814b14003e7dcae904d98ca79ad8e (diff)
Added a slightly hacky fix to make sure `bb` can be run from the local
build directory and still have the helper scripts, bbstartup.sh, and the bindings work.
-rw-r--r--bb.c23
-rw-r--r--bb.h15
-rwxr-xr-xbbstartup.sh12
3 files changed, 36 insertions, 14 deletions
diff --git a/bb.c b/bb.c
index af06a4d..c6df4e8 100644
--- a/bb.c
+++ b/bb.c
@@ -1187,10 +1187,29 @@ int main(int argc, char *argv[])
sprintf(xdg_data_home, "%s/.local/share", getenv("HOME"));
setenv("XDG_DATA_HOME", xdg_data_home, 0);
setenv("sysconfdir", "/etc", 0);
+
char *newpath;
- if (asprintf(&newpath, "%s/xdg/bb/helpers:%s/bb/helpers:%s", getenv("sysconfdir"), getenv("XDG_CONFIG_HOME"), getenv("PATH")) < 0)
- err("Could not allocate memory");
+ static char bbpath[PATH_MAX];
+ // Hacky fix to allow `bb` to be run out of its build directory:
+ if (strncmp(argv[0], "./", 2) == 0) {
+ if (realpath(argv[0], bbpath) == NULL)
+ err("Could not resolve path: %s", bbpath);
+ char *slash = strrchr(bbpath, '/');
+ if (!slash) err("No slash found in real path: %s", bbpath);
+ *slash = '\0';
+ setenv("BBPATH", bbpath, 1);
+ }
+ if (getenv("BBPATH")) {
+ if (asprintf(&newpath, "%s/helpers:%s/bb/helpers:%s",
+ getenv("BBPATH"), getenv("XDG_CONFIG_HOME"), getenv("PATH")) < 0)
+ err("Could not allocate memory for PATH");
+ } else {
+ if (asprintf(&newpath, "%s/xdg/bb/helpers:%s/bb/helpers:%s",
+ getenv("sysconfdir"), getenv("XDG_CONFIG_HOME"), getenv("PATH")) < 0)
+ err("Could not allocate memory for PATH");
+ }
setenv("PATH", newpath, 1);
+
setenv("SHELL", "bash", 0);
setenv("EDITOR", "nano", 0);
char *curdepth = getenv("BBDEPTH");
diff --git a/bb.h b/bb.h
index 21bbe89..479943f 100644
--- a/bb.h
+++ b/bb.h
@@ -244,11 +244,14 @@ static const char *description_str = "bb - an itty bitty console TUI file browse
static const char *usage_str = "Usage: bb (-h/--help | -v/--version | -s | -d | -0 | +command)* [[--] directory]\n";
static const char *runstartup =
-"for path in \"$XDG_CONFIG_HOME/bb\" \"$sysconfdir/xdg/bb\" .; do\n"
-" if [ -e \"$path/bbstartup.sh\" ]; then\n"
-" . \"$path/bbstartup.sh\";\n"
-" break;\n"
-" fi;\n"
-"done\n";
+"if [ \"$BBPATH\" ]; then\n"
+" . \"$BBPATH/bbstartup.sh\"\n"
+"else\n"
+" for path in \"$XDG_CONFIG_HOME/bb\" \"$sysconfdir/xdg/bb\"; do\n"
+" [ -e \"$path/bbstartup.sh\" ] || continue\n"
+" . \"$path/bbstartup.sh\"\n"
+" break\n"
+" done\n"
+"fi\n";
// vim: ts=4 sw=0 et cino=L2,l1,(0,W4,m1
diff --git a/bbstartup.sh b/bbstartup.sh
index 9058169..e316373 100755
--- a/bbstartup.sh
+++ b/bbstartup.sh
@@ -5,13 +5,13 @@
# Load key bindings
# first check ~/.config/bb/bindings.bb, then /etc/xdg/bb/bindings.bb, then ./bindings.bb
[ ! -d "$XDG_DATA_HOME/bb" ] && mkdir -p "$XDG_DATA_HOME/bb"
-if [ ! -e "$XDG_CONFIG_HOME/bb/bindings.bb" ] && [ ! -e "$sysconfdir/xdg/bb/bindings.bb" ]; then
- cat "./bindings.bb" 2>/dev/null | awk '/^#/ {next} /^[^ ]/ {printf "\0bind:"} {print $0} END {printf "\0"}' >> "$BBCMD"
+
+if [ "$BBPATH" ]; then
+ cat "$BBPATH/bindings.bb" 2>/dev/null
else
- for path in "$sysconfdir/xdg/bb" "$XDG_CONFIG_HOME/bb"; do
- cat "$path/bindings.bb" 2>/dev/null
- done | awk '/^#/ {next} /^[^ ]/ {printf "\0bind:"} {print $0} END {printf "\0"}' >> "$BBCMD"
-fi
+ cat "$sysconfdir/xdg/bb/bindings.bb" "$XDG_CONFIG_HOME/bb/bindings.bb" 2>/dev/null
+fi | awk '/^#/ {next} /^[^ ]/ {printf "\0bind:"} {print $0} END {printf "\0"}' >> "$BBCMD"
+
if [ -e "$XDG_DATA_HOME/bb/state.sh" ]; then
. "$XDG_DATA_HOME/bb/state.sh"
fi