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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
|
# Gentoo Linux Bash Shell Command Completion
#
# $Id$
#
# Copyright 1999-2005 Gentoo Foundation
# Distributed under the terms of the GNU General Public License, v2 or later
#
# Retrieve PORTDIR/PORTDIR_OVERLAY location from user's make.conf or, if it
# is not defined there, from make.globals.
#
_portdir()
{
sed -n -e '/^PORTDIR=/ { s/^[^=]\+="\?\([^"]\+\|\S\+\).*/\1/p ; q }' \
/etc/make.{conf,globals}
if [[ $1 == '-o' ]] ; then
sed -n -e '/^PORTDIR_OVERLAY=/ { s/^[^=]\+="\?\([^"]\+\|\S\+\).*/\1/p ; q }' \
/etc/make.conf
fi
}
# like _pkgname but completes on package names only (no category)
_pkgname_only()
{
local i pd
local cur="$1"
shift
local dir="$@"
COMPREPLY=($(compgen -W "$(\
for pd in $dir ; do \
builtin cd ${pd}; \
for i in *-*/${cur}*; do \
[[ -d ${i} ]] && { local x=${i##*/} ; echo ${x%-[0-9]*}; } \
done ; \
done)" -- ${cur}))
}
#
# This function completes package names.
#
# usage: pkgname <mode> <current-directory>
#
# Where mode is one of:
# -A Search all available packages (except for those in the overlays)
# -I Only search the installed packages
#
# TODO: Look at breaking this function out and making it a "universal"
# category/package name completion function.
#
_pkgname()
{
local mode cur portdir only
mode="$1"
cur="$2"
portdir=$(_portdir -o)
# Ignore '=' at the beginning of the current completion
[[ ${cur:1:1} == "=" ]] && cur=${cur:2}
[[ ${cur:0:1} == "=" ]] && cur=${cur:1}
case $mode in
-I)
# Complete either the category or the complete package name
if [[ $cur == */* ]]; then
COMPREPLY=($(builtin cd /var/db/pkg; compgen -W "$(compgen -G "$cur*" )" -- $cur))
else
COMPREPLY=($(builtin cd /var/db/pkg; compgen -W "$(compgen -G "$cur*" -S /)" -- $cur))
fi
# We may just have finished completing the category.
# Make sure there isn't anything more to complete now.
if [[ ${#COMPREPLY[@]} == 1 ]]; then
COMPREPLY=($(builtin cd /var/db/pkg; compgen -W "$(compgen -G "$COMPREPLY*")" -- $cur))
fi
if [[ -z "${COMPREPLY}" ]] ; then
only=1
_pkgname_only ${cur} /var/db/pkg
fi
;;
-A)
# Complete either the category or the complete package name
if [[ $cur == */* ]]; then
# Once the category has been completed, it's safe to use ${portdir}
# to continue completion.
COMPREPLY=($(\
for pd in ${portdir} ; do \
builtin cd ${pd}; \
compgen -W "$(compgen -G "${cur}*")" -- "${cur}" ; \
done))
# When we've completed most of the name, also display the version for
# possible completion.
if [[ ${#COMPREPLY[@]} -le 1 || ${cur:${#cur}-1:1} == "-" ]] && [[ ${cur} != */ ]]; then
# The portage cache is appropriate to complete specific versions from.
COMPREPLY=(${COMPREPLY[@]} $(\
for pd in ${portdir} ; do \
if [[ -d ${pd}/metadata/cache ]] ; then \
builtin cd ${pd}/metadata/cache; \
compgen -W "$(compgen -G "${cur}*")" --
"${cur}" ; \
fi ; \
done))
fi
else
COMPREPLY=( $(compgen -W "$(\
for pd in ${portdir} ; do \
if [[ -d ${pd}/metadata/cache ]] ; then
builtin cd ${pd}/metadata/cache; \
compgen -G "$cur*" -S / ; \
fi ; \
done)" -- $cur) )
if [[ ${#COMPREPLY[@]} == 1 ]]; then
COMPREPLY=($(compgen -W "$(\
for pd in ${portdir} ; do \
if [[ -d ${pd}/metadata/cache ]] ; then
builtin cd ${pd}/metadata/cache; \
compgen -G "$COMPREPLY*" ; \
fi ; \
done)" -- $cur))
fi
fi
if [[ -z "${COMPREPLY}" ]] ; then
only=1
_pkgname_only ${cur} ${portdir}
fi
;;
*)
# Somebody screwed up! :-)
;;
esac
# 'equery' wants an '=' in front of specific package versions.
# Add it if there is only one selected package and it isn't there already.
if [[ ${#COMPREPLY[@]} == 1 && ${COMP_WORDS[COMP_CWORD]:0:1} != "=" ]]
then
[[ -z "${only}" ]] && COMPREPLY=("="$COMPREPLY)
fi
}
#
# emerge completion command
#
have emerge && {
_emerge()
{
local c cur curword numwords compwords opts cond prepend
local words stopre stophere i x
local action actionpos actionre sysactions eactions pkgpos
local version_mode searchdesc_mode help_mode resume_mode
local portdir=$(_portdir -o)
COMPREPLY=()
cur="${COMP_WORDS[COMP_CWORD]}"
numwords=${#COMP_WORDS[*]}
curword=${COMP_CWORD}
compwords="${COMP_WORDS[*]}"
actionre=' @(-[CPcis]|@(metadata|s@(y@(nc|stem)|earch)|regen|@(unmerg|prune)e|world|@(@(dep)@(clean)|@(in@(fo|ject)))))'
stopre=' @(depclean|info|metadata|regen|sy@(nc|stem)|world|--@(resume|s@(kipfirst|ync)))'
opts=''
# find action
for x in ${compwords} ; do
if [[ ${x} == ${actionre} ]] ; then
action=${x}
break
fi
done
if [[ -n "${action}" ]]; then
for ((i = 0; i < ${numwords}; i++ )); do
if [[ ${COMP_WORDS[${i}]} == "${action}" ]]; then
actionpos=${i}
pkgpos=$((actionpos + 1))
break
fi
done
if [[ ${#action} -eq 2 ]]; then
case "${action}" in
'-C') action='unmerge' ;;
'-P') action='prune' ;;
'-c') action='clean' ;;
'-i') action='inject' ;;
'-s') action='search' ;;
esac
fi
else
for ((i = 1; i < ${numwords}; i++ )); do
if [[ ! "${COMP_WORDS[$i]}" == -* ]]; then
pkgpos=${i}
break
fi
done
[[ -n "${pkgpos}" ]] || pkgpos=${numwords}
fi
# Check for special cases.
[[ ${compwords} == *-@(V|-version)* ]] && version_mode=1
[[ ${compwords} == *-@(S|-searchdesc)* ]] && searchdesc_mode=1
[[ ${compwords} == *-@(h|-help)* ]] && help_mode=1
# Handle special cases.
if [[ "${action}" == 'search' ]] || [[ -n "${searchdesc_mode}" ]] || \
[[ -n "${version_mode}" ]] || [[ "${action}" == 'metadata' ]]
then
unset COMPREPLY
return 0
elif [[ -n "${help_mode}" ]]; then
unset COMPREPLY
[[ ${curword} -eq 2 ]] && COMPREPLY=($(compgen -W 'config sync system' -- ${cur}))
return 0
fi
# Complete on options.
if [[ "${cur}" == -* ]]; then
# If a resume option was specified, it needs special handling.
[[ ${compwords} == *--@(resume|skipfirst)* ]] && resume_mode=1
if [[ -n "${resume_mode}" ]]; then
if [[ "${cur}" == --* ]]; then
opts="--ask --pretend --resume --skipfirst"
elif [[ "${cur}" == -* ]]; then
[[ ${compwords} == *--@(ask|pretend)* ]] && ask_premode=1
[[ -n "${ask_premode}" ]] && opts="-a -p"
fi
elif [[ "${cur}" == --* ]]; then
# Complete on long options.
opts="--ask --autoclean \
--buildpkg --buildpkgonly \
--changelog --clean --columns \
--debug --deep \
--emptytree \
--fetch-all-uri --fetchonly \
--getbinpkg --getbinpkgonly \
--newuse --noconfmem --nodeps --noreplace --nospinner
--oneshot --onlydeps \
--pretend \
--quiet \
--sync \
--tree \
--update --upgradeonly --usepkg --usepkgonly \
--verbose"
if [[ ${curword} -eq 1 ]] && [[ ${numwords} -eq 2 ]] ; then
opts="${opts} --help --resume --searchdesc --version"
fi
elif [[ "${cur}" == -* ]]; then
# Complete on short options.
opts="-B -D -G -K -O -U -a -b -d -e -f -g -k -l -n -o -p -q -t -u -v"
if [[ ${curword} -eq 1 ]] && [[ ${numwords} -eq 2 ]] ; then
opts="${opts} -h -S -V"
fi
if [[ -z "${action}" ]] && [[ ${curword} -eq $((pkgpos - 1)) ]] ; then
opts="${opts} -C -P -c -i -s"
fi
fi
COMPREPLY=($(compgen -W "${opts}" -- ${cur}))
# NOTE: This slows things down!
# (Adapted from bash_completion by Ian Macdonald <ian@caliban.org>)
# This removes any options from the list of completions that have
# already been specified on the command line.
COMPREPLY=($(echo "${COMP_WORDS[@]}" | \
(while read -d ' ' i; do
[[ "${i}" == "" ]] && continue
# flatten array with spaces on either side,
# otherwise we cannot grep on word boundaries of
# first and last word
COMPREPLY=" ${COMPREPLY[@]} "
# remove word from list of completions
COMPREPLY=(${COMPREPLY/ ${i%% *} / })
done
echo ${COMPREPLY[@]})))
return 0
fi
# Stop completion if a special case is encountered.
if [[ ${compwords} == *${stopre}* ]] ; then
unset COMPREPLY
return 0
fi
# Complete on installed packages when unmerging.
if [[ "${action}" == 'unmerge' ]]; then
if [[ -n "${cur}" ]] ; then
if [[ "${cur}" == */* ]]; then
words=$(builtin cd /var/db/pkg; compgen -G "${cur}*")
else
words=$(builtin cd /var/db/pkg; compgen -S '/' -G "${cur}*")
local n=0
for i in ${words} ; do
[[ ${i} == ${cur}* ]] && n=$((n+1))
done
if [[ ${n} -eq 1 ]] ; then
words="$(builtin cd /var/db/pkg ; compgen -G "*-*/*")"
fi
fi
COMPREPLY=($(for i in ${words} ; do \
[[ ${i} == ${cur}* ]] && echo ${i} ; \
done))
else
COMPREPLY=($(builtin cd /var/db/pkg ; compgen -S '/' -G "*-*"))
fi
[[ -z "${COMPREPLY}" ]] && _pkgname_only ${cur} /var/db/pkg
return 0
fi
# Check for conditional.
cond="${cur%%[A-Za-z0-9]*}"
cur="${cur:${#cond}}"
if [[ ${cond:0:1} == "'" || ${cond:0:1} == '"' ]] ; then
prepend="-P ${cond:1}"
c="${cond:1}"
else
c="${cond}"
fi
# Handle cases where a conditional is specified.
if [[ -n "${cond}" ]]; then
if [[ -n "${cur}" ]]; then
if [[ ${cur} == */* ]]; then
if [[ ${cur} == *-[0-9]* ]] ; then
words="$(\
for pd in ${portdir} ; do \
builtin cd ${pd} ; \
local cat="${cur%/*}" ; \
local pkg="$(echo ${cur%-[0-9]*})" ; \
pkg="${pkg##*/}" ; \
for x in ${cat}/${pkg}/*.ebuild ; do \
[[ -f ${x} ]] || continue ; \
x="${x/${pkg}\/}" ; \
echo "${x%*.ebuild}" ; \
done ; \
done)"
else
words="$(\
for pd in ${portdir} ; do \
builtin cd ${pd}; \
compgen -X "*metadata.xml" -G "${cur}*" -- ${cur} ; \
done)"
fi
local w
for x in $words ; do
w="${x}\n${w}"
done
words=$(echo -ne ${w} | sort | uniq)
COMPREPLY=( ${words} )
# Complete on the specific versions (if appropriate).
if [[ ${#COMPREPLY[@]} -le 1 ]] ; then
COMPREPLY=($(\
for pd in ${portdir} ; do \
if [[ -d ${pd}/metadata/cache ]] ; then
builtin cd ${pd}/metadata/cache; \
compgen ${prepend} -G "${cur}*" -- "${cur}" ; \
else \
builtin cd ${pd} ; \
local cat="${cur%/*}" ; \
local pkg="$(echo ${cur%-[0-9]*}*)" ; \
pkg="${pkg##*/}" ; \
for x in ${cat}/${pkg}/*.ebuild ; do \
[[ -f "${x}" ]] || continue ; \
x="${x/${pkg}\/}" ; \
echo "${c}${x%*.ebuild}" ; \
done ; \
fi ; \
done))
else
COMPREPLY=($(compgen ${prepend} -W "${words}" -- $cur))
fi
else
words="$(\
for pd in ${portdir} ; do \
builtin cd ${pd} ; \
compgen ${prepend} -S '/' -G "${cur}*" -- "${cur}" ; \
done)"
local w
for x in words ; do
w="${x}\n${w}"
done
COMPREPLY=($(echo -e ${w} | uniq))
[[ ${#COMPREPLY[@]} = 1 ]] && \
COMPREPLY=($(\
for pd in ${portdir} ; do \
builtin cd ${pd} ; \
compgen ${prepend} -G "${cur}*/*" -- "${cur}" ; \
done))
fi
else
words="$(\
for pd in ${portdir} ; do \
builtin cd ${pd}; \
compgen -G "*-*"; \
done)"
COMPREPLY=($(compgen -W "${words}" -- "${cur}"))
fi
# If all else fails, try to complete on package names without the
# category being specified.
if [[ -z "${COMPREPLY}" ]]; then
words="$(\
for pd in ${portdir} ; do \
builtin cd ${pd}; \
for i in *-*/${cur}*; do \
[[ -d $i ]] && echo ${i##*/}; \
done ; \
done)"
COMPREPLY=($(compgen ${prepend} -W "${words}" -- ${cur}))
if [[ ${#COMPREPLY[@]} -le 1 ]]; then
# Now complete on the specific versions.
words="$(\
for pd in ${portdir} ; do \
if [[ -d ${pd}/metadata/cache ]] ; then \
builtin cd ${pd}/metadata/cache; \
for i in */${cur}*; do \
echo ${i##*/}; \
done ; \
fi ; \
done)"
COMPREPLY=($(compgen ${prepend} -W "${words}" -- "${cur}"))
fi
fi
return 0
fi
# Complete on packages.
if [[ ${COMP_CWORD} -eq 1 ]] ; then
if [[ $numwords -le 2 ]]; then
sysactions=$'\n'"system"$'\n'"world"$'\n'"sync"$'\n'"metadata"
else
sysactions=$'\n'"system"$'\n'"world"$'\n'"sync"
fi
else
# Only allow these actions if no packages have been specified.
#
# TODO: This doesn't block these actions if no categories are
# specified. Please fix me.
#
# e.g. emerge -a gentoo-dev-sources
#
# will still allow system and world actions to be specified,
# as opposed to
#
# emerge -a sys-kernel/gentoo-dev-sources
#
if [[ ! " ${compwords} " == *" "*[/]*" "* ]]; then
sysactions=$'\n'"system"$'\n'"world"
else
sysactions=''
fi
fi
if [[ -z "${action}" ]] && [[ ${curword} -le ${pkgpos} ]] ; then
eactions=$'\n'"clean"$'\n'"depclean"$'\n'"inject"$'\n'"prune"$'\n'"regen"$'\n'"search"$'\n'"unmerge"
fi
if [[ -n "${cur}" ]] ; then
if [[ "${cur}" == */* ]] ; then
words=$(\
for pd in ${portdir} ; do \
builtin cd ${pd}; \
compgen -X "*metadata.xml" -G "${cur}*" ; \
done)"${sysactions}""${eactions}"
else
local ww=$(\
for pd in ${portdir} ; do \
builtin cd ${pd} ; \
compgen -S '/' -G "${cur}*"; \
done)"${sysactions}""${eactions}"
local w
for x in ${ww} ; do w="${x}\n${w}" ; done
words=$(echo -e ${w} | uniq)
local n=0
for i in ${words} ; do
[[ ${i} == ${cur}* ]] && n=$((n+1))
done
if [[ ${n} -eq 1 ]] ; then
words=$(for pd in ${portdir} ; do \
builtin cd ${pd} ; \
compgen -G "*-*/*" ; \
done)"${sysactions}""${eactions}"
fi
fi
COMPREPLY=($(for i in ${words} ; do \
[[ ${i} == ${cur}* ]] && echo ${i} ; \
done))
else
words="$(\
for pd in ${portdir} ; do \
builtin cd ${pd} ; \
compgen -S '/' -G "*-*" ; \
done)""${sysactions}""${eactions}"
COMPREPLY=($(compgen -W "${words}" -- ${cur}))
fi
# If all else fails, try to complete on package names without the
# category being specified.
if [[ -z "${COMPREPLY}" ]]; then
words="$(\
for pd in ${portdir} ; do \
builtin cd ${pd}; \
for i in [a-z]*-[a-z0-9]*/${cur}*; do \
[[ -d $i ]] && echo ${i##*/}; \
done ; \
done)"
COMPREPLY=($(compgen -W "${words}" -- ${cur}))
fi
return 0
}
complete -F _emerge emerge
}
#
# ebuild completion command
#
have ebuild && {
_ebuild()
{
local cur opts
COMPREPLY=()
cur="${COMP_WORDS[COMP_CWORD]}"
opts="help setup fetch digest manifest unpack compile test preinst \
postinst install qmerge merge prerm postrm unmerge config package \
rpm clean"
if [[ $COMP_CWORD -eq 1 ]] ; then
COMPREPLY=($(compgen -f -X "!*.ebuild" -- ${cur}) \
$(compgen -d -- ${cur}) \
$(compgen -W '--debug' -- ${cur}))
elif [[ $COMP_CWORD -eq 2 && "${COMP_WORDS[1]}" = "--debug" ]] ; then
COMPREPLY=($(compgen -f -X "!*.ebuild" -- ${cur}) $(compgen -d -- ${cur}))
elif [[ $COMP_CWORD -ge 2 ]] ; then
COMPREPLY=($(compgen -W "${opts}" -- ${cur}))
fi
return 0
}
complete $filenames -F _ebuild ebuild
}
#
# Gentoo init.d completion
#
_gentoo_style_init()
{
local script="${COMP_WORDS[0]}"
local cur="${COMP_WORDS[$COMP_CWORD]}"
if [[ ( -f "${script}" || -h "${script}" ) && -r "${script}" ]] \
&& [[ "${script}" != *.sh ]] \
&& [[ "$(head -n 1 "${script}")" = "#!/sbin/runscript" ]]
then
[[ $COMP_CWORD -gt 1 ]] && return 1
COMPREPLY=($(opts="start stop status restart pause zap ineed needsme iuse usesme broken"; \
eval "$(grep '^opts=' "${script}")"; echo "${opts}"))
[[ -n "$COMPREPLY" ]] || COMPREPLY=(start stop restart zap)
COMPREPLY=($(compgen -W "${COMPREPLY[*]}" -- "${cur}"))
else
COMPREPLY=($(compgen -o default -- "${cur}"))
fi
return 0
}
complete -F _gentoo_style_init /etc/init.d/*
#
# rc completion command
#
have rc && {
_rc()
{
local cur
COMPREPLY=()
cur="${COMP_WORDS[COMP_CWORD]}"
if [[ ${#COMP_WORDS[*]} -le 2 ]]; then
COMPREPLY=($(compgen -W "$(for i in /etc/runlevels/*; do echo ${i##*/}; done)" -- $cur))
fi
return 0
}
complete -F _rc rc
}
#
# rc-status completion command
#
have rc-status && {
_rcstatus()
{
local cur
cur="${COMP_WORDS[COMP_CWORD]}"
if [[ $COMP_CWORD -eq 1 ]]; then
if [[ "${cur}" == --* ]]; then
COMPREPLY=($(compgen -W '--all --list --unused' -- ${cur}))
elif [[ "${cur}" == -* ]]; then
COMPREPLY=($(compgen -W '-a -l -u' -- ${cur}))
else
COMPREPLY=($(compgen -W "$(rc-status --list)" -- ${cur}))
fi
else
unset COMPREPLY
fi
return 0
}
complete -F _rcstatus rc-status
}
#
# rc-update completion command
#
have rc-update && {
_rcupdate()
{
local cur show
COMPREPLY=()
cur="${COMP_WORDS[COMP_CWORD]}"
if [[ $COMP_CWORD -eq 1 ]]; then
if [[ "${cur}" == -* ]]; then
COMPREPLY=($(compgen -W '-a -d -s' -- ${cur}))
else
COMPREPLY=($(compgen -W 'add del show' ${cur}))
fi
else
if [[ "${COMP_WORDS[1]}" == "show" ]] || [[ "${COMP_WORDS[1]}" == "-s" ]]; then
show="TRUE"
fi
if ([[ $COMP_CWORD -eq 3 ]] && [[ -z "$show" ]]) || \
([[ $COMP_CWORD -eq 2 ]] && [[ -n "$show" ]])
then
COMPREPLY=($(compgen -W "$(for i in /etc/runlevels/*; do echo ${i##*/}; done)" -- $cur))
elif [[ $COMP_CWORD -eq 2 ]]; then
COMPREPLY=($(compgen -X "*.@(c|sh|test)" -W "$(for i in /etc/init.d/*; do echo ${i##*/}; done)" $cur))
elif [[ ${#COMP_WORDS[*]} -gt 2 ]] ; then
COMPREPLY=($(compgen -W "$(for i in /etc/runlevels/*; do echo ${i##*/}; done)" -- $cur))
else
unset COMPREPLY
fi
fi
return 0
}
complete -F _rcupdate rc-update
}
#
# gcc-config completion command
#
have gcc-config && {
_gcc_config() {
local cur prev opts
COMPREPLY=()
cur="${COMP_WORDS[COMP_CWORD]}"
prev="${COMP_WORDS[COMP_CWORD-1]}"
opts="-O --use-old \
-P --use-portage-chost \
-c --get-current-profile \
-l --list-profiles \
-E --print-environ \
-B --get-bin-path \
-L --get-lib-path \
-X --get-stdcxx-incdir"
if [[ "${cur}" == -* ]] ; then
COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )
return 0
elif [[ ${COMP_CWORD} -eq 1 ]] ; then
COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) \
$(compgen -W "$(gcc-config -l | sed -r -e 's/(\[([^]]*)\]) //g')" \
-- ${cur}) )
return 0
fi
case "${prev}" in
-O|--use-old|-P|--use-portage-chost|-c|--get-current-profile|-l|--list-profiles)
COMPREPLY=()
;;
*)
COMPREPLY=( $(compgen -W "\
$(gcc-config -l | sed -r -e 's/(\[([^]]*)\]) //g')" -- ${cur}) )
;;
esac
}
complete -F _gcc_config gcc-config
}
#
# distcc-config completion command
#
have distcc-config && {
_distccconfig()
{
local cur curword numwords opts
COMPREPLY=()
cur="${COMP_WORDS[COMP_CWORD]}"
numwords=${#COMP_WORDS[*]}
curword=${COMP_CWORD}
if [[ ${numwords} -gt 3 ]]; then
unset COMPREPLY
return 0
fi
if [[ "${cur}" == -* ]] || [ ${curword} -eq 1 ]; then
if [[ ${numwords} -le 2 ]] && [[ ${curword} -eq 1 ]]; then
opts="--get-hosts \
--get-verbose \
--get-log \
--set-hosts \
--set-verbose \
--set-log \
--add-path \
--no-path"
else
opts=""
fi
else
opts=""
fi
COMPREPLY=($(compgen -W "${opts}" | grep ^$cur))
return 0
}
complete -F _distccconfig distcc-config
}
#
# java-config completion command
#
have java-config && {
_javaconfig()
{
local cur prev curword numwords opts args arg spec flag sedcmd grepcmd
local multiplepkgs pkgs execopts
COMPREPLY=()
cur="${COMP_WORDS[COMP_CWORD]}"
prev="${COMP_WORDS[COMP_CWORD-1]}"
numwords=${#COMP_WORDS[*]}
curword=${COMP_CWORD}
opts=""
args=""
pkgs=""
sedcmd="sed -r -e s/\[([^]]+)\].*/\1/"
vmsedcmd="sed -r -e s/\[([^]]+)\]/\1/"
grepcmd="egrep -o (--set-(system|user)-(classpath|vm)=)"
multiplepkgs=""
execopts="HtmlConverter JavaPluginControlPanel \
appletviewer awt_robot \
extcheck \
idlj \
j2sdk-config jar jarsigner \
java java-rmi.cgi java_vm javac javadoc javah javap jdb \
keytool kinit klist ktab \
native2ascii \
oldjava oldjavac oldjdb orbd \
policytool \
realpath rmic rmid rmiregistry \
serialver servertool \
tnameserv"
if [[ "${cur}" == -* ]] || [ ${curword} -eq 1 ]; then
case "${cur}" in
--java)
opts="--java --javac --java-version"
;;
--j@(a@(r|va@(c|-version))|@(dk|re)-home))
opts=""
;;
--list-available-@(packages|vms))
opts=""
;;
--@(exec|set-@(user|system)-@(classpath|vm)))
opts="${cur}="
;;
--set-@(user|system)-@(classpath|vm)=)
if [[ "${cur}" == "--set-system-vm=" ]] || [[ "${cur}" == "--set-user-vm=" ]]; then
flag="--list-available-vms"
args=$(java-config --nocolor "${flag}" | cut --delimiter=' ' --fields=2 | ${vmsedcmd})
else
flag="--list-available-packages"
args=$(java-config --nocolor "${flag}" | ${sedcmd})
fi
for arg in ${args}; do
[[ -n "${opts}" ]] && opts="${opts} ${arg}" || opts="${arg}"
done
COMPREPLY=($(compgen $nospace -W "${opts}"))
return 0
;;
--exec=)
COMPREPLY=($(compgen $nospace -W "${execopts}"))
return 0
;;
*)
if [[ "${cur}" == "--set-system-vm="* ]] || [[ "${cur}" == "--set-user-vm="* ]]; then
args=$(java-config --nocolor --list-available-vms | cut --delimiter=' ' --fields=2 | ${vmsedcmd})
if [[ "${cur}" == "--set-system-vm="* ]]; then
spec=${cur##--set-system-vm=}
else
spec=${cur##--set-user-vm=}
fi
for arg in ${args}; do
if [[ "${arg:0:${#spec}}" == "${spec}" ]]; then
[[ -n "${opts}" ]] && opts="${opts} ${arg}" || opts="${arg}"
fi
done
[[ "${opts}" == "${spec}" ]] && opts=""
COMPREPLY=($(compgen -W "${opts}"))
return 0
elif [[ "${cur}" == "--set-system-classpath="* ]] || [[ "${cur}" == "--set-user-classpath="* ]]; then
args=$(java-config --nocolor --list-available-packages | ${sedcmd})
[[ $(echo "${cur}" | grep -c ",") -gt 0 ]] && multiplepkgs="true"
if [[ "${cur}" == "--set-system-classpath="* ]]; then
spec="${cur##--set-system-classpath=}"
else
spec="${cur##--set-user-classpath=}"
fi
if [[ -n "${multiplepkgs}" ]]; then
pkgs="${spec%,*}"
spec="${spec##*,}"
fi
if [[ -n "${multiplepkgs}" ]]; then
for arg in ${args}; do
if [[ "${spec}" ]]; then
if [[ "${arg:0:${#spec}}" == "${spec}" ]] \
&& [[ ! $(echo "${cur}" | egrep -o "(=|,)${arg},") ]]
then
[[ -n "${opts}" ]] && opts="${opts} ${pkgs},${arg}" || opts="${pkgs},${arg}"
fi
else
if [[ ! $(echo "${cur}" | egrep -o "(=|,)${arg},") ]]; then
[[ -n "${opts}" ]] && opts="${opts} ${pkgs},${arg}" || opts="${pkgs},${arg}"
fi
fi
done
[[ "${opts}" == "${pkgs},${spec}" ]] && opts=""
else
for arg in ${args}; do
if [[ "${spec}" ]] && [[ "${arg:0:${#spec}}" == "${spec}" ]]; then
[[ -n "${opts}" ]] && opts="${opts} ${arg}" || opts="${arg}"
fi
done
[[ "${opts}" == "${spec}" ]] && opts=""
fi
COMPREPLY=($(compgen -W "${opts}"))
return 0
elif [[ "${cur}" == "--exec="* ]]; then
spec=${cur##--exec=}
for arg in ${execopts}; do
if [[ "${arg:0:${#spec}}" == "${spec}" ]]; then
[[ -n "${opts}" ]] && opts="${opts} ${arg}" || opts="${arg}"
fi
done
[[ "${opts}" == "${spec}" ]] && opts=""
COMPREPLY=($(compgen -W "${opts}"))
return 0
else
opts="--classpath --clean-system-classpath --clean-user-classpath \
--exec \
--full-classpath \
--jar --java --javac --java-version --jdk-home --jre-home \
--list-available-packages --list-available-vms \
--nocolor \
--set-system-classpath --set-system-vm --set-user-classpath --set-user-vm"
[[ "$prev" == "--nocolor" ]] && opts="${opts/--nocolor}"
fi
;;
esac
elif [[ "$prev" == "--nocolor" ]] && [ ${curword} -eq 2 ] && [ $numwords -le 3 ]; then
opts="--classpath --clean-system-classpath --clean-user-classpath \
--exec \
--full-classpath \
--jar --java --javac --java-version --jdk-home --jre-home \
--list-available-packages --list-available-vms \
--set-system-classpath --set-system-vm --set-user-classpath --set-user-vm"
fi
COMPREPLY=($(compgen $nospace -W "${opts}" -- ${cur}))
return 0
}
complete $nospace -F _javaconfig java-config
}
#
# opengl-update completion command
#
have opengl-update && {
_openglupdate()
{
local cur
COMPREPLY=()
cur="${COMP_WORDS[COMP_CWORD]}"
if [[ ${#COMP_WORDS[*]} -le 2 ]]; then
COMPREPLY=($(compgen -W "$(for i in /usr/lib/opengl/*; do [ -d $i ] && echo ${i##*/}; done)" $cur))
else
unset COMPREPLY
fi
return 0
}
complete -F _openglupdate opengl-update
}
#
# browser-config completion command
#
have browser-config && {
_browserconfig()
{
local cur prev
COMPREPLY=()
cur="${COMP_WORDS[COMP_CWORD]}"
prev="${COMP_WORDS[COMP_CWORD-1]}"
if [[ ${COMP_CWORD} -eq 1 ]]; then
COMPREPLY=($(compgen -W '-b -h -m' -- ${cur}))
elif [[ "${prev}" == "-b" ]]; then
COMPREPLY=($(compgen -W "$(for i in /usr/share/browser-config/*; do [ -f $i ] && echo ${i##*/}; done)" $cur))
elif [[ "${prev}" == "-m" ]]; then
COMPREPLY=($(compgen -W "same_window new_window new_tab new_browser" -- ${cur}))
if [[ -z "${COMPREPLY}" ]]; then
COMPREPLY=''
fi
else
unset COMPREPLY
fi
return 0
}
complete -F _browserconfig browser-config
}
#
# Bash completion for the Gentoo 'equery' command
#
have equery && {
_equery()
{
local cur prev mode portdir i j
portdir=$(_portdir)
mode="GLOBAL"
COMPREPLY=()
cur="${COMP_WORDS[COMP_CWORD]}"
prev="${COMP_WORDS[COMP_CWORD-1]}"
# Find out what we're currently doing here.
j=0
for i in "${COMP_WORDS[@]}"; do
if [[ $j -lt $COMP_CWORD ]]; then
j=$((j + 1))
case $i in
@(belongs|ch@(anges|eck)|dep@(ends|graph)|files|glsa|list|s@(ize|tats)|uses|which|hasuse|b|c|k|d|g|f|a|h|l|s|t|u|w))
mode=$i
;;
esac
fi
done
# Now get to work.
case $mode in
GLOBAL)
# Complete commands and global options.
case $cur in
-*)
COMPREPLY=($(compgen -W "-q --quiet -C --nocolor -h --help -V --version" -- $cur))
;;
*)
COMPREPLY=($(compgen -W "belongs changes check depends depgraph files glsa hasuse list size stats uses which" -- $cur))
;;
esac
;;
c?(hanges)|a|glsa|t|stats)
# These commands have not been implemented in 'equery' yet ...
echo -n "# Not implemented! "
;;
f?(iles))
# Only complete if the previous entry on the command line is not
# a package name.
if [[ ${prev} == ${mode} || ${prev:0:1} == "-" ]]; then
case $cur in
-*)
COMPREPLY=($(compgen -W "--timestamp --md5sum --type" -- $cur))
;;
*)
# Only installed packages can have their files listed.
_pkgname -I $cur
;;
esac
fi
;;
l?(ist))
# Only complete if the previous entry on the command line is not
# a package name.
if [[ ${prev} == ${mode} || ${prev:0:1} == "-" ]]; then
case "${cur}" in
-*)
COMPREPLY=($(compgen -W "-i --installed -I --exclude-installed -p --portage-tree -o --overlay-tree" -- $cur))
;;
*)
case "${prev}" in
-i|--installed)
_pkgname -I $cur
;;
*)
# Complete on all package names.
_pkgname -A $cur
;;
esac
;;
esac
fi
;;
b?(elongs))
# Only complete if the previous entry on the command line is not
# a file name.
if \
[[ \
${prev} == ${mode} || \
${prev:0:1} == "-" || \
${COMP_WORDS[COMP_CWORD-2]} == "-c" || \
${COMP_WORDS[COMP_CWORD-2]} == "--category" \
]] && \
[[ \
${prev} != "-c" && \
${prev} != "--category" \
]]
then
case $cur in
-*)
COMPREPLY=($(compgen -W "-c --category -e --earlyout" -- $cur))
;;
*)
COMPREPLY=($(compgen -f -- $cur) \
$(compgen -d -S '/' -- $cur))
;;
esac
# Are we completing a category?
elif [[ ${prev} == "-c" || ${prev} == "--category" ]]; then
COMPREPLY=($(builtin cd ${portdir}/metadata/cache; compgen -W "$(compgen -G '*')" -- $cur))
fi
;;
@(u?(ses)|w?(hich)))
# Only complete if the previous entry on the command line is not
# a package name.
if [[ ${prev} == ${mode} ]]; then
# Complete on all package names.
_pkgname -A $cur
fi
;;
g|depgraph)
# Only complete if the previous entry on the command line is not
# a package name.
if [[ ${prev} == ${mode} || ${prev:0:1} == "-" ]]; then
case $cur in
-*)
COMPREPLY=($(compgen -W "-U --no-useflags -l --linear" -- $cur))
;;
*)
# Complete on installed package names.
_pkgname -I $cur
;;
esac
fi
;;
d?(epends))
# Only complete if the previous entry on the command line is not
# a package name.
if [[ ${prev} == ${mode} || ${prev:0:1} == "-" ]]; then
case $cur in
-*)
COMPREPLY=($(compgen -W "-a --all-packages -d --direct -D --indirect" -- $cur))
;;
*)
case $prev in
-a|--all-packages)
# Complete on all package names.
_pkgname -A $cur
;;
*)
# Complete on installed package names.
_pkgname -I $cur
;;
esac
;;
esac
fi
;;
k|check)
# Only complete if the previous entry on the command line is not
# a package name.
if [[ ${prev} == ${mode} ]]; then
# Only installed packages can have their integrity verified.
_pkgname -I $cur
fi
;;
s?(ize))
# Only complete if the previous entry on the command line is not
# a package name.
if [[ ${prev} == ${mode} || ${prev:0:1} == "-" ]]; then
case $cur in
-*)
COMPREPLY=($(compgen -W "-b --bytes" -- $cur))
;;
*)
# Only installed packages can have their size calculated.
_pkgname -I $cur
;;
esac
fi
;;
h?(asuse))
# Only complete if the previous entry on the command line is not
# a package name.
if [[ ${prev} == ${mode} || ${prev:0:1} == "-" ]]; then
case $cur in
-*)
COMPREPLY=($(compgen -W "-i --installed -I --exclude-installed -p --portage-tree -o --overlay" -- $cur))
;;
*)
local glob loc
glob=$(sed -n -e 's/^\([^ ]\+\) - .*$/\1/p' ${portdir}/profiles/use.desc)
loc=$(sed -n -e 's/^[^ ]\+:\([^ ]*\) - .*$/\1/p' ${portdir}/profiles/use.local.desc)
COMPREPLY=($(compgen -W "$glob $loc" -- $cur))
;;
esac
fi
;;
esac
return 0
}
complete -F _equery equery
}
#
# ekeyword completion
#
have ekeyword && {
_ekeyword()
{
local cur portdir archl_s archl_u archl_r archl_m arch
COMPREPLY=()
cur="${COMP_WORDS[COMP_CWORD]}"
portdir=$(_portdir)
for arch in all $(< ${portdir}/profiles/arch.list) ; do
archl_m="${archl_m} -${arch}"
archl_u="${archl_u} ~${arch}"
archl_r="${archl_r} ^${arch}"
archl_s="${archl_s} ${arch}"
done
case $cur in
-*)
COMPREPLY=( $( compgen -W "${archl_m}" -- $cur ) )
;;
~*)
COMPREPLY=( $( compgen -W "${archl_u}" -- $cur ) )
;;
^*)
COMPREPLY=( $( compgen -W "${archl_r}" -- $cur ) )
;;
*)
if [ ${COMP_CWORD} -eq 1 ] ; then
COMPREPLY=( $(compgen -W "${archl_s}" -- $cur ) )
else
COMPREPLY=( $( compgen -W "${archl_s}" -- $cur ) )
_filedir 'ebuild'
fi
;;
esac
}
complete $filenames -F _ekeyword ekeyword
}
#
# portageq completion
#
have portageq && {
_portageq() {
local cur prev opts
COMPREPLY=()
cur="${COMP_WORDS[COMP_CWORD]}"
prev="${COMP_WORDS[COMP_CWORD-1]}"
opts="config_protect_mask \
config_protect \
vdb_path \
gentoo_mirrors \
all_best_visible \
match \
best_visible \
mass_best_visible \
has_version \
portdir \
envvar \
mass_best_version \
best_version \
pkgdir \
portdir_overlay \
distdir"
if [[ $COMP_CWORD -eq 1 ]] ; then
# would always be correct, but it's pretty slow...
#COMPREPLY=($(compgen -W "$(portageq | grep '^ [[:lower:]]' | \
# sed -e 's/^.*[[:space:]]\([[:lower:]_]\+\)[[:space:]].*$/\1/')" \
# -- ${cur}))
COMPREPLY=($(compgen -W "${opts}" -- ${cur}))
fi
case "${prev}" in
config*|vdb_path|gentoo_mirrors|*dir*)
COMPREPLY=()
;;
# this also isn't the fastest, but I welcome an alternative method
envvar)
COMPREPLY=($(compgen -W "$(emerge -v --info | \
sed -n -e '/^[[:upper:]].*=".*"/s/^\(.*\)=".*$/\1/p')" -- ${cur}))
;;
*v@(isible|ersion)|match)
COMPREPLY=($(compgen $nospace -W '/' -- $cur))
;;
# $prev is a path, so complete on category/package
*/*)
local x a=0
for x in ${COMP_WORDS[@]} ; do
# This is the only one
if [[ "${x}" == "all_best_visible" ]] ; then
a=1
break
fi
done
if [[ ${a} -eq 1 ]] ; then
COMPREPLY=()
else
# Check for conditional.
# cond="${cur%%[A-Za-z0-9]*}"
# cur="${cur:${#cond}}"
# if [[ -n "${cond}" ]] ; then
# _pkgname -A $cur
# else
_pkgname -A $cur
# fi
fi
;;
esac
}
complete -F _portageq portageq
}
#
# webapp-config completion
#
have webapp-config && {
_webapp_complete_appver()
{
local x proot ibase cur="$2"
eval $(. /etc/vhosts/webapp-config ; \
echo proot="${MY_PERSISTROOT:-/var/db/webapps}" ; \
echo ibase="${WA_INSTALLSBASE:-installs}")
case "$1" in
# complete on installed
installed)
COMPREPLY=($(compgen -W "$(\
for x in ${proot}/*/*/installs ; do \
if [[ -f "${x}" ]] ; then \
local y="${x%/*}" ; \
y="${y%/*}" ; \
echo "${y##*/}" ; \
fi ; \
done)" -- ${cur}))
;;
# complete on uninstalled
uninstalled)
COMPREPLY=($(compgen -W "$(\
for x in ${proot}/*/* ; do \
if [[ ! -f "${x}/${ibase}" ]] ; then \
local y="${x%/*}" ; \
echo "${y##*/}" ; \
fi ; \
done)" -- ${cur}))
;;
# all
all)
COMPREPLY=($(compgen -W "$(\
for x in ${proot}/* ; do \
[[ -d "${x}" ]] && echo "${x##*/}" ; \
done)" -- ${cur}))
;;
# complete on version
*)
[[ -d "${proot}/$1" ]] || return 1
COMPREPLY=($(compgen -W "$(\
for x in ${proot}/$1/* ; do \
[[ -d "${x}" ]] && echo "${x##*/}" ; \
done)" -- ${cur}))
;;
esac
}
_webapp_config()
{
local cur prev actions opts hostroot
COMPREPLY=()
cur="${COMP_WORDS[COMP_CWORD]}"
prev="${COMP_WORDS[COMP_CWORD-1]}"
actions="-I --install -U --upgrade -C --clean --list-installs \
--list-unused-installs --show-installed --show-postinst \
--help -v --version"
opts="--bug-report --pretend -p -u --user -g --group \
-d --dir -h --host -V --verbose --soft --secure --virtual-dirs \
--virtual-files --force-virtual"
eval $(. /etc/vhosts/webapp-config ; \
echo hostroot="${VHOST_ROOT:-/var/www}")
# --bug-report, --pretend, and -p can only be used as first arg
if [[ ${COMP_CWORD} -gt 1 ]] ; then
opts="${opts/--bug-report --pretend -p}"
fi
if [[ "${cur}" == -* ]] || [[ ${COMP_CWORD} -eq 1 ]] ; then
COMPREPLY=($(compgen -W "${opts} ${actions}" -- ${cur}))
return 0
fi
case "${prev}" in
--bug-report|-p|--pretend)
COMPREPLY=($(compgen -W "${opts} ${actions}" -- ${cur}))
;;
-I|--install)
_webapp_complete_appver all ${cur}
;;
-U|--upgrade)
_webapp_complete_appver installed ${cur}
;;
# only complete on -d since it is required if -C is specified
-C|--clean)
COMPREPLY=($(compgen -W "-d" -- ${cur}))
;;
--list-unused-installs)
_webapp_complete_appver uninstalled ${cur}
;;
--list-installs|--show-postinst)
_webapp_complete_appver all ${cur}
;;
# hrm... anyone know a better way to reliably do this?
-h|--host)
local x
COMPREPLY=($(compgen -W "$(\
for x in ${hostroot}/* ; do \
[[ -d "${x}" ]] && echo "${x##*/}" ; \
done)" -- ${cur}))
;;
--virtual*)
COMPREPLY=($(compgen -W "server-owned config-owned virtual" \
-- ${cur}))
;;
-d|--dir)
local host x i=0
# see if --host has been specified, and if so, get the value
# that was passed to it.
for x in ${COMP_WORDS[@]} ; do
if [[ "${x}" == "-h" || "${x}" == "--host" ]] ; then
host="${COMP_WORDS[((i+1))]}"
break
fi
i=$((i+1))
done
# otherwise, use the default host
if [[ "${host}" == "" ]] ; then
eval $(. /etc/vhosts/webapp-config ; \
echo host="${VHOST_HOSTNAME:-localhost}")
fi
COMPREPLY=($(compgen -W "$(\
for x in ${hostroot}${host}/* ; do \
[[ -d "${x}" ]] && echo "${x}" ; \
done)" -- ${cur}))
;;
-u|--user)
COMPREPLY=($(compgen -u -- ${cur}))
;;
-g|--group)
COMPREPLY=($(compgen -g -- ${cur}))
;;
# we haven't recognized it yet, so more than likely ${prev}
# is a 'app-name' ; assuming it is indeed a valid 'app-name'
# (_webapp_complete_appver does the check), complete on available
# 'app-version's
*)
_webapp_complete_appver ${prev} ${cur} || \
_webapp_complete_appver all ${cur}
;;
esac
}
complete -F _webapp_config webapp-config
}
# vim: set ft=sh tw=80 sw=4 et :
|