diff options
author | Ulrich Müller <ulm@gentoo.org> | 2019-09-02 12:25:45 +0200 |
---|---|---|
committer | Ulrich Müller <ulm@gentoo.org> | 2019-09-02 12:25:45 +0200 |
commit | 0905154aae24c2f17b137b45f5a1dd11fb4774fb (patch) | |
tree | 98d070daf670e806a1e0827da3e355820d4973d1 /libs | |
parent | ChangeLog: Use UTF-8. (diff) | |
download | eselect-0905154aae24c2f17b137b45f5a1dd11fb4774fb.tar.gz eselect-0905154aae24c2f17b137b45f5a1dd11fb4774fb.tar.bz2 eselect-0905154aae24c2f17b137b45f5a1dd11fb4774fb.zip |
Support relative pathnames in editor-variable library.
* libs/editor-variable.bash.in (find_in_path): New function, looks
up its first argument in EDITOR_PATH, and tests if it exists.
(find_targets, do_set): Use it.
* modules/pager.eselect (EDITOR_LIST):
* modules/editor.eselect (EDITOR_LIST): Don't use absolute paths.
* man/editor.eselect.5:
* man/pager.eselect.5: Update.
Signed-off-by: Ulrich Müller <ulm@gentoo.org>
Diffstat (limited to 'libs')
-rw-r--r-- | libs/editor-variable.bash.in | 47 |
1 files changed, 30 insertions, 17 deletions
diff --git a/libs/editor-variable.bash.in b/libs/editor-variable.bash.in index ac71ecf..a3bfdc7 100644 --- a/libs/editor-variable.bash.in +++ b/libs/editor-variable.bash.in @@ -21,26 +21,49 @@ # EDITOR_VAR is the name of the environment variable, e.g. "EDITOR". # EDITOR_ENVFILE is the path to the config file where the variable should be # stored, e.g. "/etc/env.d/99editor". Several modules may share the same file. -# EDITOR_LIST is a space-separated list of available programs (full pathnames) -# e.g. "/bin/nano /usr/bin/emacs /usr/bin/vi". Alternatively, items can be of -# the form "name:/path/to/binary". +# EDITOR_LIST is a space-separated list of available programs (with or without +# full pathname), e.g., "nano emacs /usr/bin/vi". Alternatively, items can be +# of the form "name:/path/to/binary". # EDITOR_PATH (optional) is a colon-separated list of directories where to # search for available programs. Default is "/bin:/usr/bin". inherit config +# find file in EDITOR_PATH +find_in_path() { + local file=$1 + + # do we already have an absolute path? + if [[ ${file} == /* ]]; then + [[ -f ${ROOT}${file} ]] + return + fi + + # try to find it + local IFS=: + for dir in ${EDITOR_PATH-/bin:/usr/bin}; do + [[ -f ${EROOT}${dir}/${file} ]] && return 0 + done + return 1 +} + # find a list of valid targets find_targets() { - local cur i + local cur i file for i in ${EDITOR_LIST}; do - [[ -f ${EROOT}${i#*:} ]] && echo "${EPREFIX}${i%%:*}" + file=${i#*:} + [[ ${file} == /* ]] && file=${EPREFIX}${file} + if find_in_path "${file}"; then + [[ ${i} == *:* ]] && echo "${i%%:*}" || echo "${file}" + fi done # also output the current value if it isn't in our list cur=$(read_env_value) - [[ -n ${cur} && ${EDITOR_LIST} != *:* && -f ${ROOT}${cur} ]] \ + [[ -n ${cur} && ${EDITOR_LIST} != *:* ]] \ && ! has "${cur#${EPREFIX}}" ${EDITOR_LIST} \ + && find_in_path "${cur}" \ && echo "${cur}" } @@ -130,18 +153,8 @@ do_set() { fi if [[ ${EDITOR_LIST} != *:* ]]; then - # is the target an absolute path? if not, try to find it - if [[ ${target} != /* ]]; then - local ifs_save=${IFS} IFS=: - for dir in ${EDITOR_PATH-/bin:/usr/bin}; do - [[ -f ${EROOT}${dir}/${target} ]] || continue - target=${EPREFIX}${dir}/${target} - break - done - IFS=${ifs_save} - fi # target is valid if it's a path to an existing binary - [[ ${target} == /* && -f ${ROOT}${target} ]] \ + find_in_path "${target}" \ || die -q "Target \"${target}\" doesn't appear to be valid!" else # target is valid only if it's in our list |