aboutsummaryrefslogtreecommitdiff
path: root/Functions/Zftp/zfrglob
blob: f9d67b3f276a432c3526f7bff4ef6dafac97eacb (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# function zfrglob {
# Do the remote globbing for zfput, etc.
# We have two choices:
#  (1) Get the entire file list and match it one by one
#      locally against the pattern.
#      Causes problems if we are globbing directories (rare, presumably).
#      But: we can cache the current directory, which
#      we need for completion anyway.  Works on any OS if you
#      stick with a single directory.  This is the default.
#  (2) Use remote globbing, i.e. pass it to ls at the site.
#      Faster, but only works with UNIX, and only basic globbing.
#      We do this if $zfrglob is non-null.

# There is only one argument, the variable containing the
# pattern to be globbed.  We set this back to an array containing
# all the matches.

emulate -L zsh
setopt extendedglob

local pat dir nondir files i

eval pat=\$$1

# Check if we really need to do anything.  Look for standard
# globbing characters, and if we are
# using zsh for the actual pattern matching also look for
# extendedglob characters.
if [[ $pat != *[][*?]* &&
  ( -n $zfrglob || $pat != *[(|)#^]* ) ]]; then
  return 0
fi
local tmpf=${TMPPREFIX}zfrglob$$

if [[ $zfrglob != '' ]]; then
  zftp ls "$pat" >$tmpf 2>/dev/null
  eval "$1=(\$(<\$tmpf))"
  rm -f $tmpf
else
  if [[ $ZFTP_SYSTEM = UNIX* && $pat = */* ]]; then
    # not the current directory and we know how to handle paths
    if [[ $pat = ?*/* ]]; then
      # careful not to remove too many slashes
      dir=${pat%/*}
    else
      dir=/
    fi
    nondir=${pat##*/}
    zftp ls "$dir" 2>/dev/null >$tmpf
    files=($(<$tmpf))
    files=(${files:t})
    rm -f $tmpf
  else
    # we just have to do an ls and hope that's right
    nondir=$pat
    if (( $#zftp_fcache == 0 )); then
      # Why does `zftp_fcache=($(zftp ls))' sometimes not work?
      zftp ls >$tmpf
      zftp_fcache=($(<$tmpf))
      rm -f $tmpf
    fi
    files=($zftp_fcache)
  fi
  # now we want to see which of the $files match $nondir:
  # ${...:/foo} deletes occurrences of foo matching a complete word,
  # while the ^ inverts the sense so that anything not matching the
  # pattern in $nondir is excluded.
  eval "$1=(\${files:/^\${~nondir}})"
fi
# }