1#compdef tar gtar star bsdtar
2
3# Tar completion.  Features:
4#  - Tries to collect tar commands from second position, single letter
5#    option, and long options.
6#  - `tar' can be called anything, will use the correct name
7#  - Uses the function `_tar_archive' to complete archive files.
8#  - Tries to find out if compressed archives should be used.
9#  - Completes files inside archive.  This is supposed to look pretty
10#    much as if the files are in an ordinary directory hierarchy.
11#    Handles extraction from compressed archives (GNU tar).
12#  - Anywhere -- appears, gets a list of long options to complete from
13#    tar itself (GNU tar)
14#  - Things like --directory=... are also completed correctly.
15
16local _tar_cmd tf tmp tmpb del index
17
18# First we collect in `_tar_cmd' single letter options describing what
19# should be done with the archive and if it is compressed. This
20# collected from options arguments that start with only one hyphen,
21# from some of the possible long options, and from the second word if
22# that does not start with a hyphen.
23
24if _pick_variant gnu=GNU libarchive=libarchive unix --version; then
25  case "$($service --version)" in
26    ("tar (GNU tar) "(#b)([0-9.-]##)*)
27    autoload -z is-at-least
28    is-at-least 1.14.91 "$match[1]" || _cmd_variant[$service]="gnu-old"
29    ;;
30  esac
31fi
32
33tmp=("${(@M)words:#-[^-]*}")
34_tar_cmd="${(j::)tmp#-}"
35
36(( $words[(I)--(un|)gzip] ))     && _tar_cmd="z$_tar_cmd"
37(( $words[(I)--(un|)compress] )) && _tar_cmd="Z$_tar_cmd"
38(( $words[(I)--bzip2] ))         && _tar_cmd="j$_tar_cmd"
39(( $words[(I)--xz] ))            && _tar_cmd="J$_tar_cmd"
40(( $words[(I)--list] ))          && _tar_cmd="t$_tar_cmd"
41(( $words[(I)--(extract|get)] )) && _tar_cmd="x$_tar_cmd"
42(( $words[(I)--create] ))        && _tar_cmd="c$_tar_cmd"
43
44# Other ways of finding out what we're doing:  first
45# look in the first argument if it's not an option
46if [[ "$words[2]" = *[txcdruA]*~-* ]]; then
47  _tar_cmd="$words[2]$_tar_cmd"
48elif [[ $_tar_cmd != *[txcdruA]* && CURRENT -gt 2 ]]; then
49  # look for more obscure long options: these aren't all handled.
50  (( $words[(I)--(diff|compare)] )) && _tar_cmd="d$_tar_cmd"
51  (( $words[(I)--append] ))         && _tar_cmd="r$_tar_cmd"
52  (( $words[(I)--update] ))         && _tar_cmd="u$_tar_cmd"
53  (( $words[(I)--(con|)catenate] )) && _tar_cmd="A$_tar_cmd"
54  (( $words[(I)--delete] ))         && del=1
55fi
56
57# Next, we try to find the archive name and store it in `tf'. The name
58# is searched after a `--file=' long option, in the third word if the
59# second one didn't start with a hyphen but contained a `f', and after
60# an option argument starting with only one hyphen and containing a `f'.
61# unless that option argument also contains a `C'.
62
63tmp="$words[(I)--file=*]"
64tmpb="$words[(I)-*Cf*~--*]"
65
66if (( tmp )); then
67  tf=${~words[tmp][8,-1]}
68  _tar_cmd="f$_tar_cmd"
69elif [[ "$words[2]" != -* && "$words[2]" = *f* ]]; then
70  tf=${~words[3]}
71  _tar_cmd="f$_tar_cmd"
72elif (( tmpb )); then
73  tf=${~words[tmpb+2]}
74  wdir=${~words[tmpb+1]}
75  _tar_cmd="Cf$_tar_cmd"
76else
77  tmp="${words[(I)-*f*~--*]}"
78  if (( tmp )); then
79    tf=${~words[tmp+1]}
80    _tar_cmd="f$_tar_cmd"
81  fi
82fi 2>/dev/null
83
84# See if we should use a path prefix.  We have to use eval as the dir can
85# be any unevaluated thing which appears on the command line, including a
86# parameter.
87
88# This isn't used right now.
89
90tmp=${words[(r)--dir[a-z]#=*]}
91
92if [[ -n $tmp ]]; then
93  eval "wdir=(${tmp#*=})"
94fi
95
96# Now we complete...
97
98if [[ "$PREFIX" = --* ]]; then
99
100  # ...long options after `--'.
101
102  _arguments '-f+:' '-C+:' '*: : true' -- -l '--owner=*:user:_users' \
103		 '--group=*:group:_groups' \
104		 '--atime-preserve*::method:(replace system)' \
105		 '--*-script=NAME:script file:_files' \
106		 '--format=*:format:(gnu oldgnu pax posix ustar v7)' \
107		 '--quoting-style=*:quoting style:(literal shell shell-always c c-maybe escape locale clocale)' \
108		 '--totals*=SIGNAL*::signal:(HUP QUIT INT USR1 USR2)' \
109                 '*=(PROG|COMMAND)*:program:_command_names -e' \
110		 '*=ARCHIVE*:archive: _tar_archive' \
111		 '*=FILE*:file:_files' \
112		 '*=DIR*:directory:_files -/' \
113		 '*=CONTROL*::version control:(t numbered nil existing never simple)'
114
115elif [[ ( CURRENT -gt 2 && "$words[CURRENT-1]" = -[^C]#f* &&
116          "$words[CURRENT-1]" != --* ) ||
117        ( CURRENT -eq 3 && "$words[2]" = [^C]#f* && "$words[2]" != -* ) ||
118        ( CURRENT -gt 2 && "$words[CURRENT-2]" = -*C*f* &&
119          "$words[CURRENT-2]" != --* && "$words[CURRENT-1]" != --* ) ||
120        ( CURRENT -eq 4 && "$words[2]" = *C*f* && "$words[2]" != -* ) ]]; then
121
122  # ...archive files if we think they are wanted here.
123
124  _tar_archive
125
126elif [[ ( CURRENT -gt 2 && "$words[CURRENT-1]" = -[^f]#C*) ||
127        ( CURRENT -eq 3 && "$words[2]" = [^f]#C* ) ]]; then
128
129  # a directory for -C
130
131  _directories
132
133elif [[ ( "$_tar_cmd" = *[xt]* || -n $del ) && -n "$tf" ]]; then
134
135  # ...and files from the archive if we found an archive name and tar
136  # commands. We run `tar t...' on the file, keeping the list of
137  # filenames cached, plus the name of the tarfile so we know if it
138  # changes.  We skip this test if the alleged archive is not a file.
139
140  local largs=-tf expl
141
142  if [[ $_tar_cmd = *z* ]]; then
143    largs=-tzf
144  elif [[ $_tar_cmd = *j* ]]; then
145    largs=-tjf
146  elif [[ $_tar_cmd = *y* ]]; then
147    largs=-tyf
148  elif [[ $_tar_cmd = *Z* ]]; then
149    largs=-tZf
150  elif [[ $_tar_cmd = *I* ]]; then
151    largs=-tIf
152  elif [[ $_tar_cmd = *J* ]]; then
153    largs=-tJf
154  else
155    # Some random compression program
156    tmp="${words[(r)--use-comp*]}"
157    [[ -n $tmp ]] && largs=($tmp -tf)
158  fi
159
160  if [[ $tf != $_tar_cache_name && -f $tf ]]; then
161    _tar_cache_list=("${(@f)$($words[1] $largs $tf)}")
162    _tar_cache_name=$tf
163  fi
164
165  _wanted files expl 'file from archive' _multi_parts / _tar_cache_list
166elif (( CURRENT == 2 )); then
167  # ignore leading - since we complete option letters anyway
168  compset -P -
169  _values -s '' 'tar function' \
170    '(c t u x)A[append to an archive]' \
171    '(A t u x)c[create a new archive]' \
172    '(A c u x)t[list archive contents]' \
173    '(A c t x)u[update archive]' \
174    '(A c t u)x[extract files from an archive]' \
175    'v[verbose output]' \
176    'f[specify archive file or device]'
177else
178  if ! (( index=$words[(I)-*C*] )); then
179    if [[ $words[2] = [^f]#C* ]]; then
180      index=1
181    elif [[ $words[2] = *f*C* ]]; then
182      index=2
183    fi
184  fi
185  if (( index )); then
186    index=${~${(Q)words[index+1]}}
187    [[ $index = (.|..|)/* ]] || index=~+/$index
188    _files -W $index
189  else
190    _files
191  fi
192fi
193