From 6f30ac188d687522f56b41ebafe7e49c322af038 Mon Sep 17 00:00:00 2001 From: Case Duckworth Date: Wed, 13 Apr 2022 17:04:46 -0500 Subject: Add path_add_unsafe and change path munging --- profile/00_functions.sh | 62 +++++++++++++++++++++++++++++++++++++++++++++++++ profile/01_path.sh | 6 +++++ profile/go.sh | 2 ++ profile/infopath.sh | 8 ++++++- profile/manpath.sh | 16 +++++++++---- profile/path.sh | 30 ------------------------ 6 files changed, 88 insertions(+), 36 deletions(-) create mode 100644 profile/00_functions.sh create mode 100644 profile/01_path.sh create mode 100644 profile/go.sh delete mode 100644 profile/path.sh (limited to 'profile') diff --git a/profile/00_functions.sh b/profile/00_functions.sh new file mode 100644 index 0000000..a94eaee --- /dev/null +++ b/profile/00_functions.sh @@ -0,0 +1,62 @@ +# utility functions for all shells +# these should be POSIX-compatible. + +# add a path to PATH, but only if it's not already there +path_add_to_PATH() { # path_add [-a] PATH... + # -a appends (default is prepend) + APPEND=false + if [ "x$1" = "x-a" ]; then + APPEND=true + shift + fi + + for p; do + case ":$PATH:" in + *:"$p":*) ;; + *) + if $APPEND; then + PATH="$PATH:$p" + else + PATH="$p:$PATH" + fi + ;; + esac + done + + unset APPEND +} + +# Generalization of `path_add_to_PATH' for any variable. +path_add_unsafe() { #path_add_unsafe [-a] [-d DELIM] VAR PATH... + ## Add PATH... to VAR, delimiting with DELIM (default :). + # By default, the VAR will be prepended to; passing -a will append the + # variable. -d DELIM defines the delimiter. + # + # This function has the _unsafe suffix because it uses `eval' to set + # variables. + APPEND=false; DELIM=: + while getopts ad: opt; do + case "$opt" in + a) APPEND=true ;; + d) DELIM="$OPTARG" ;; + *) return 1 ;; + esac + done + shift $(expr $OPTIND - 1) + + var="$1"; shift + + for path; do + case ":$(eval "echo \$$var"):" in + *:"$path":*) ;; + *) + if $APPEND; then + eval "$var=\$$var${var:+$DELIM}$path" + else + eval "$var=$path${var:+$DELIM}\$$var" + fi + ;; + esac + done + unset -v APPEND DELIM var +} diff --git a/profile/01_path.sh b/profile/01_path.sh new file mode 100644 index 0000000..8082cc6 --- /dev/null +++ b/profile/01_path.sh @@ -0,0 +1,6 @@ +# PATH + +# see 00_functions.sh for `path_add' +path_add_to_PATH "$HOME/bin" "$HOME/usr/bin" + +command -v luarocks >/dev/null 2>&1 && path_add "$HOME/.luarocks/bin" diff --git a/profile/go.sh b/profile/go.sh new file mode 100644 index 0000000..077833e --- /dev/null +++ b/profile/go.sh @@ -0,0 +1,2 @@ +export GOPATH="${XDG_DATA_HOME:=$HOME/.local/share}/go" +command -v go >/dev/null 2>&1 && path_add_to_PATH "$(go env GOPATH)/bin" diff --git a/profile/infopath.sh b/profile/infopath.sh index ad1ec3f..80080b8 100644 --- a/profile/infopath.sh +++ b/profile/infopath.sh @@ -1 +1,7 @@ -export INFOPATH="/usr/share/info:${XDG_DATA_HOME:-$HOME/.local/share}/info" +# See 00_functions.sh for `path_add_unsafe'. + +path_add_unsafe INFOPATH \ + /usr/share/info \ + "${XDG_DATA_HOME:-$HOME/.local/share}/info" + +export INFOPATH diff --git a/profile/manpath.sh b/profile/manpath.sh index def1963..944059c 100644 --- a/profile/manpath.sh +++ b/profile/manpath.sh @@ -1,9 +1,15 @@ -# $MANPATH shouldn't be too extra complicated (as opposed to $PATH), -# so I'm not going to include a whole other function. AT SOME POINT, -# I suppose I should generalize that function to set /any/ path-type -# variable, not just $PATH. +# See 00_functions.sh for `path_add_unsafe'. + +path_add_unsafe MANPATH \ + "${XDG_DATA_HOME:-$HOME/.local/share}/man" \ + "$HOME/.local/local/man" \ + "$HOME/usr/local/man" + # $MANPATH ends with `:' so that manpath(1) will prepend it to its # special thing. +case "$MANPATH" in + *:) ;; + *) MANPATH="$MANPATH:" ;; +esac -MANPATH="${XDG_DATA_HOME:-$HOME/.local/share}/man:" export MANPATH diff --git a/profile/path.sh b/profile/path.sh deleted file mode 100644 index 56bdc39..0000000 --- a/profile/path.sh +++ /dev/null @@ -1,30 +0,0 @@ -# PATH - -# add a path to PATH, but only if it's not already there -path_add() { # path_add [-a] PATH... - # -a appends (default is prepend) - APPEND=false - if [ "x$1" = "x-a" ]; then - APPEND=true - shift - fi - - for p; do - case ":$PATH:" in - *:"$p":*) ;; - *) - if $APPEND; then - PATH="$PATH:$p" - else - PATH="$p:$PATH" - fi - ;; - esac - done - - unset APPEND -} - -path_add "$HOME/bin" "$HOME/usr/bin" - -command -v luarocks >/dev/null 2>&1 && path_add "$HOME/.luarocks/bin" -- cgit 1.4.1-21-gabe81