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.
This commit is contained in:
Bruce Hill 2020-02-24 02:57:12 -08:00
parent 9a7938ba2d
commit 1dbece8b2e
3 changed files with 36 additions and 14 deletions

23
bb.c
View File

@ -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");

15
bb.h
View File

@ -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

View File

@ -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