1# -*- shell-script -*- 2# Things related to file handling. 3# 4# Copyright (C) 2002-2004, 2006, 2008-2010, 2014, 2018 Rocky Bernstein 5# rocky@gnu.org 6# 7# bashdb is free software; you can redistribute it and/or modify it under 8# the terms of the GNU General Public License as published by the Free 9# Software Foundation; either version 2, or (at your option) any later 10# version. 11# 12# bashdb is distributed in the hope that it will be useful, but WITHOUT ANY 13# WARRANTY; without even the implied warranty of MERCHANTABILITY or 14# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 15# for more details. 16# 17# You should have received a copy of the GNU General Public License along 18# with bashdb; see the file COPYING. If not, write to the Free Software 19# Foundation, 59 Temple Place, Suite 330, Boston, MA 02111 USA. 20 21# Directory search patch for unqualified file names 22 23typeset -a _Dbg_dir 24_Dbg_dir=('\$cdir' '\$cwd' ) 25 26# _Dbg_cdir is the directory in which the script is located. 27[[ -z ${_Dbg_cdir} ]] && typeset _Dbg_cdir=${_Dbg_source_file%/*} 28[[ -z ${_Dbg_cdir} ]] && typeset _Dbg_cdir=$(pwd) 29 30# Either fill out or strip filename as determined by "basename_only" 31# and annotate settings 32_Dbg_adjust_filename() { 33 typeset -r filename="$1" 34 if (( _Dbg_set_annotate == 1 )) ; then 35 echo "$(_Dbg_resolve_expand_filename "$filename")" 36 elif ((_Dbg_set_basename)) ; then 37 echo "${filename##*/}" 38 else 39 echo "$filename" 40 fi 41} 42 43# Canonicalize $1. For now if _Dbg_set_basename is set we just want 44# the basename of that. 45function _Dbg_file_canonic { 46 if (( $# != 1 )) ; then 47 echo '??' 48 return 1 49 fi 50 typeset filename="$1" 51 (( _Dbg_set_basename )) && filename=${filename##*/} 52 echo "$filename" 53 return 0 54} 55 56# $1 contains the name you want to glob. return 0 if exists and is 57# readable or 1 if not. 58# The result will be in variable $filename which is assumed to be 59# local'd by the caller 60_Dbg_tilde_expand_filename() { 61 typeset cmd="filename=\$(builtin echo $1)" 62 eval $cmd 63 [[ -r "$filename" ]] 64} 65 66# 67# Resolve $1 to a full file name which exists. First see if filename has been 68# mentioned in a debugger "file" command. If not and the file name 69# is a relative name use _Dbg_dir to substitute a relative directory name. 70# 71function _Dbg_resolve_expand_filename { 72 73 if (( $# == 0 )) ; then 74 _Dbg_errmsg \ 75 "Internal debug error _Dbg_resolve_expand_filename(): null file to find" 76 echo '' 77 return 1 78 fi 79 typeset find_file="$1" 80 81 # Is this one of the files we've that has been specified in a debugger 82 # "FILE" command? 83 typeset found_file 84 found_file="${_Dbg_file2canonic[$find_file]}" 85 if [[ -n $found_file ]] ; then 86 echo "$found_file" 87 return 0 88 fi 89 90 if [[ ${find_file:0:1} == '/' ]] ; then 91 # Absolute file name 92 full_find_file=$(_Dbg_expand_filename "$find_file") 93 echo "$full_find_file" 94 return 0 95 elif [[ ${find_file:0:1} == '.' ]] ; then 96 # Relative file name 97 full_find_file=$(_Dbg_expand_filename "${_Dbg_init_cwd}/$find_file") 98 if [[ -z "$full_find_file" ]] || [[ ! -r $full_find_file ]]; then 99 # Try using cwd rather that Dbg_init_cwd 100 full_find_file=$(_Dbg_expand_filename "$find_file") 101 fi 102 echo "$full_find_file" 103 return 0 104 else 105 # Resolve file using _Dbg_dir 106 typeset -i n=${#_Dbg_dir[@]} 107 typeset -i i 108 for (( i=0 ; i < n; i++ )) ; do 109 typeset basename="${_Dbg_dir[i]}" 110 if [[ "$basename" == '\$cdir' ]] ; then 111 basename=$_Dbg_cdir 112 elif [[ "$basename" == '\$cwd' ]] ; then 113 basename=$(pwd) 114 fi 115 if [[ -f "$basename/$find_file" ]] ; then 116 echo "$basename/$find_file" 117 return 0 118 fi 119 done 120 fi 121 echo '' 122 return 1 123} 124