summaryrefslogtreecommitdiff
blob: 5a46801f29b08c2943ccea5902ffa7de5420e487 (plain)
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
# -*- shell-script -*-
#
# Gentoo Linux command completion.
#
# Copyright 1999-2002 Gentoo Technologies, Inc.
# Distributed under the terms of the GNU General Public License, v2 or later
#
# Author: Geert Bevin  <gbevin@theleaf.be>
# Author: Zach Forrest <zach@disinformation.ca>
#

#
# rc & rc-status completion command
#
_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

_rcstatus()
{
	local cur opts
	COMPREPLY=()
	cur=${COMP_WORDS[COMP_CWORD]}
	if [ ${#COMP_WORDS[*]} -le 2 ]; then
		opts="-a -all -l --list -u --unused"
		COMPREPLY=( $( compgen -W "${opts} $(for i in /etc/runlevels/*; do echo ${i##*/}; done)" -- $cur ) )

	fi
	return 0
}
complete -F _rcstatus rc-status

#
# distcc-config completion command
#
_distccconfig()
{
	local cur curword numwords opts
	COMPREPLY=()
	cur=${COMP_WORDS[COMP_CWORD]}
	numwords=${#COMP_WORDS[*]}
	curword=${COMP_CWORD}
	if [ ${numwords} -gt 3 ]; then
		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

#
# simple completion for Gentoo style init
#
complete -W "start stop restart pause zap ineed needsme iuse usesme broken" \
	$(for i in /etc/init.d/*; do echo ${i##*/}; done)

#
# emerge completion command
#
_emerge()
{
	local cur prev grepcmd sedcmd systemactions setsma setbig portagedir origdir
	local mode words opts curword numwords pkgdbdir

	origdir="${PWD}"
	portagedir=/usr/portage
	pkgdbdir=/var/db/pkg
	COMPREPLY=()
	cur="${COMP_WORDS[COMP_CWORD]}"
	prev="${COMP_WORDS[COMP_CWORD-1]}"
	words="${COMP_WORDS[*]}"
	numwords=${#COMP_WORDS[*]}
	curword=${COMP_CWORD}
	mode='ALL'

	# Determine which mode we're running in.

	if [ ${curword} -eq 1 ] && [ ${numwords} -eq 2 ]; then
		# This is set when we nothing but the command name has been given.
		mode='ALL'
	elif [[ " ${words} " == *" --unmerge "* ]]; then
		mode='UNMERGE'
	elif [[ " ${words} " == *" --safe "* ]]; then
		mode='SAFE'
	elif [[ " ${words} " == *" --search "* ]]; then
		mode='SEARCH'
	elif [[ " ${words} " == *" --world "* ]]; then
		mode='WORLD'
	elif [[ " ${words} " == *" --help "* ]]; then
		mode='HELP'
	elif [[ " ${words} " == *" --clean "* ]]; then
		mode='CLEAN'
	elif [[ " ${words} " == *" --pretend "* ]]; then
		mode='PRETEND'
	else
		# All except '--clean'
		mode='NOCLEAN'
	fi

	if [[ "${cur}" == -* ]]; then

		# Process Options

		case "${mode}" in
			"ALL")
				opts='--clean --help --verbose --debug --fetchonly \
					--onlydeps --noreplace --usepkg --buildpkg \
					--autoclean --pretend --unmerge --safe \
					--search --world'
				;;

			"UNMERGE")
				opts='--clean --verbose --debug --pretend \
					--unmerge --safe'
				;;

			"SAFE")
				opts='--unmerge --verbose --debug'
				;;

			"SEARCH")
				opts='--verbose --debug'
				;;

			"WORLD")
				opts='--verbose --debug --pretend --world'
				;;

			"HELP")
				opts=''
				;;

			"CLEAN")
				opts='--verbose --debug --clean'
				;;

			"PRETEND")
				opts='--verbose --debug --fetchonly --onlydeps \
					--noreplace --usepkg --buildpkg \
					--autoclean --pretend --unmerge --safe \
					--world'
				;;

			"NOCLEAN")
				opts='--help --verbose --debug --fetchonly \
					--onlydeps --noreplace --usepkg --buildpkg \
					--autoclean --pretend --unmerge --safe \
					--search --world'
				;;
			*)
				# Same as ALL
				opts='--clean --help --verbose --debug --fetchonly \
					--onlydeps --noreplace --usepkg --buildpkg \
					--autoclean --pretend --unmerge --safe \
					--search --world'
				;;

		esac
		
		# Generate the reply.
		COMPREPLY=($(compgen -W "${opts}" | grep ^$cur))

	elif [ "${mode}" == "CLEAN" ]; then
		# "rsync" is the only option that can follow "--clean"
		COMPREPLY=($(compgen -W 'rsync'))

	elif [ "${mode}" == "WORLD" ]; then
		# "update" is the only option that can follow "--world"
		#COMPREPLY=($(compgen -W 'update'))
		COMPREPLY=($(compgen -W 'update' | grep ^${cur}))

	elif [ "${mode}" == "HELP" ]; then
		if [[ ! " ${words} " == *" "@(system|rsync)" "* ]]; then
			COMPREPLY=($(compgen -W 'rsync system' | grep ^${cur}))
		else
			COMPREPLY=''
		fi

	elif [ "${mode}" == "SEARCH" ]; then
		# We don't need to interfere here as the search string is a
		# regular expression. (But wouldn't intelligent, dynamic,
		# commandline completion of regular expressions be great!)
		COMPREPLY=''

	elif [ "${mode}" == "UNMERGE" ]; then
		# Ignore >, >=, <, <=, and = for the purpose of completion.
		sedcmd="sed -e s:=:: -e s:>:: -e s:<::"
		cur=$(echo "${cur}" | ${sedcmd})

		# If the current completion (minus conditional characters)
		# starts with one of the following characters, then complete
		# on filenames (i.e. an ebuild file) rather than referencing
		# the package database.
		if [[ "${cur} " == @('/'|'.'|'~'|'$')* ]]; then
			# Setting this to nothing with tell bash to use default
			# completion (i.e. pathname completion).
			COMPREPLY=''
		else
			cd "${pkgdbdir}"
			grepcmd="grep -E ^$cur.*"

			if [ "${cur}" ]; then
				if [ $(echo "${cur}" | grep '/') ]; then
					setbig=$(compgen -G "${cur}*")
					COMPREPLY=($(echo "${setbig}" | ${grepcmd}))
				else
					setsma=$(compgen -S '/' -G "${cur}*")
					if [ $(echo "${setsma}" | ${grepcmd} | grep '/' | wc -l) = 1 ]; then
						setbig=$(compgen -G "*/*")
						COMPREPLY=($(echo "${setbig}" | ${grepcmd}))
					else
						COMPREPLY=($(echo "${setsma}" | ${grepcmd}))
					fi
				fi
			else
				setsma=$(compgen -S '/' -G "${cur}*")
				COMPREPLY=($(echo "${setsma}"))
			fi

			cd "${origdir}"
		fi

	elif [ "${mode}" == "SAFE" ]; then
		COMPREPLY=($(compgen -W '--unmerge' | grep ^${cur}))

	elif [[ " ${words} " == *" "@(update|system|rsync)" "* ]]; then
 		# syncing the portage tree is a lonely job
 		# updating should be done by itself (also lonely)
  		COMPREPLY=''

	elif [[ " ${words} " == *" --usepkg "* ]] && [[ "${cur} " == @('/'|'.'|'~'|'$')* ]]; then
		# In order to complete on a package file, the name must be
		# an absolute or realative pathname (even if it is in the
		# current directory). This allows completion on both package
		# names in /usr/portage and on arbitrary *.tbz2 packages.
		#
		# I'm planning to come up with a more elegant solution, but
		# that will have to wait for another update. (The only thing
		# that is really annoying right now is when specifying a
		# *.tbz2 file in or below the current directory, you have
		# to prefix the path with "./".)
		COMPREPLY=''

	else
		cd "${portagedir}"
		grepcmd="grep -E ^${cur}.*"
		sedcmd="sed -e /^[^-]*$/d"

		if [ ${COMP_CWORD} -eq 1 ]; then
			# If emerge hasn't been given any args yet, include "rsync"
			# among the system actions. The case where the "--clean" flag
			# has been specified is handled above.
			systemactions=$'\n'"system"$'\n'"update"$'\n'"rsync"
		else
			# Only allow these actions if no packages have been specified.
			if [[ ! " ${words} " == *" "*[/]*" "* ]]; then
				systemactions=$'\n'"system"$'\n'"update"
			else
				systemactions=''
			fi
		fi

		if [ "${cur}" ]; then
			if [[ "${cur} " == @('/'|'.'|'~'|'$')* ]]; then
				COMPREPLY=''
			elif [ $(echo "${cur}" | grep '/') ]; then
				setbig=$(compgen -G "${cur}*" | ${sedcmd})"${systemactions}"
				COMPREPLY=($(echo "${setbig}" | $grepcmd))
			else
				setsma=$(compgen -S '/' -G "${cur}*" | ${sedcmd})"${systemactions}"
				if [ $(echo "${setsma}" | ${grepcmd} | grep '/' | wc -l) = 1 ]; then
					setbig=$(compgen -G "*/*" | ${sedcmd})"${systemactions}"
					COMPREPLY=($(echo "${setbig}" | ${grepcmd}))
				else
					COMPREPLY=($(echo "${setsma}" | ${grepcmd}))
				fi
			fi
		else
                        setsma=$(compgen -S '/' -G "${cur}*" | ${sedcmd})"${systemactions}"
                        COMPREPLY=($(echo "${setsma}"))
		fi

		cd "${origdir}"
	fi

	# (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
}
complete -o default -F _emerge emerge

#
# ebuild completion command
#
_ebuild()
{
	local cur prev
	COMPREPLY=()
	cur=${COMP_WORDS[COMP_CWORD]}
	prev=${COMP_WORDS[COMP_CWORD-1]}
	if [ $COMP_CWORD -eq 1 ]; then
		#COMPREPLY=( $( compgen -o filenames -X '!*.ebuild' $cur ) )
		COMPREPLY=($(compgen -o filenames))
	elif [ $COMP_CWORD -eq 2 ]; then
		COMPREPLY=( $( compgen -W 'check \
			clean compile config \
			depend digest \
			fetch \
			help \
			install \
			merge \
			package postinst postrm preinst prerm \
			qmerge \
			rcheck remerge rpm \
			setup \
			touch \
			unmerge unpack' $cur ) )
	fi
	return 0
}
complete -o default -F _ebuild ebuild

#
# rc-update completion command
#
_rcupdate()
{
	local cur prev initdir runlvdir origdir
	origdir=${PWD}
	initdir=/etc/init.d
	runlvdir=/etc/runlevels
	COMPREPLY=()
	cur=${COMP_WORDS[COMP_CWORD]}
	prev=${COMP_WORDS[COMP_CWORD-1]}
	if [ $COMP_CWORD -eq 1 ]; then
		COMPREPLY=( $( compgen -W 'add del' ${cur} ) )
	elif [ $COMP_CWORD -eq 2 ]; then
		cd ${initdir}
		COMPREPLY=( $( compgen -G "${cur}*" ) )
		cd ${origdir}
	elif [ $COMP_CWORD -eq 3 ]; then
		cd ${runlvdir}
		COMPREPLY=( $( compgen -G "${cur}*" ) )
		cd ${origdir}
	fi
	return 0
}
complete -F _rcupdate rc-update

#
# gcc-config completion command
#
_gccconfig()
{
	local cur curword numwords opts
	COMPREPLY=()
	cur=${COMP_WORDS[COMP_CWORD]}
	numwords=${#COMP_WORDS[*]}
	curword=${COMP_CWORD}
	if [ ${numwords} -gt 3 ]; then
		return 0
	fi
	if [[ "${cur}" == -* ]] || [ ${curword} -eq 1 ]; then
		if [ ${numwords} -le 2 ] && [ ${curword} -eq 1 ]; then
			opts="--use-old \
				--use-portage-chost \
				--get-current-profile \
				--list-profiles \
				--print-environ \
				--get-bin-path \
				--get-lib-path \
				--get-stdcxx-incdir"
		else
			opts=""
		fi
	elif [ ${numwords} -le 3 ] && [ ${curword} -eq 2 ]; then
		case "${COMP_WORDS[1]}" in
			"--print-environ"|"--get-bin-path"|"--get-lib-path"|"--get-stdcxx-incdir")
				opts=$(gcc-config --list-profiles)
				;;
			*)
				opts=""
				;;
		esac
	else
		opts=""
	fi
	COMPREPLY=($(compgen -W "${opts}" | grep ^$cur))
	return 0
}
complete -F _gccconfig gcc-config

_javaconfig()
{
	local cur curword numwords opts args arg spec flag sedcmd grepcmd
	local multiplepkgs pkgs execopts
	COMPREPLY=()
	cur=${COMP_WORDS[COMP_CWORD]}
	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.cgijava_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 "${flag}" | cut --delimiter=' ' --fields=2 | ${vmsedcmd})
				else
					flag="--list-available-packages"
					args=$(java-config "${flag}" | ${sedcmd})
				fi
				#echo "$(java-config ${flag})" >> output
				#args=$(java-config "${flag}" | ${sedcmd})
				for arg in ${args}
				do
					[ "${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 --list-available-vms | cut --delimiter=' ' --fields=2 | ${vmsedcmd})
					# spec=$(echo "${cur}" | egrep -o "(--set-(system|user)-(classpath|vm)=)")
					# spec=$(echo "${cur}" | ${grepcmd})
					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
							[ "${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 --list-available-packages | ${sedcmd})
					[ $(echo "${cur}" | grep -c ",") -gt 0 ] && multiplepkgs="true"
					# spec=$(echo "${cur}" | ${grepcmd})
					if [[ "${cur}" == "--set-system-classpath="* ]]; then
						spec="${cur##--set-system-classpath=}"
					else
						spec="${cur##--set-user-classpath=}"
					fi
					if [[ "${multiplepkgs}" ]]; then
						pkgs="${spec%,*}"
						spec="${spec##*,}"
					fi
					if [[ "${multiplepkgs}" ]]; then
						for arg in ${args}
						do
							if [[ "${spec}" ]]; then
								if [[ "${arg:0:${#spec}}" == "${spec}" ]] \
										&& [[ ! $(echo "${cur}" | egrep -o "(=|,)${arg},") ]]; then
									[ "${opts}" ] && opts="${opts} ${pkgs},${arg}" || opts="${pkgs},${arg}"
								fi
							else
								if [[ ! $(echo "${cur}" | egrep -o "(=|,)${arg},") ]]; then
									[ "${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
								[ "${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
							[ "${opts}" ] && opts="${opts} ${arg}" || opts="${arg}"
						fi
					done
					[[ "${opts}" == "${spec}" ]] && opts=""
					COMPREPLY=($(compgen -W "${opts}"))
					return 0
				else
					opts="--javac \
						--java \
						--jar \
						--jdk-home \
						--jre-home \
						--java-version \
						--classpath \
						--full-classpath \
						--exec \
						--list-available-packages \
						--list-available-vms \
						--set-system-vm \
						--set-user-vm \
						--set-system-classpath \
						--set-user-classpath \
						--clean-system-classpath"
				fi
				;;
		esac
	else
		opts=""
	fi
	COMPREPLY=($(compgen $nospace -W "${opts}" | grep ^${cur}))
	return 0
}
complete $nospace -F _javaconfig java-config

_openglupdate()
{
	local cur opts
	COMPREPLY=()
	cur=${COMP_WORDS[COMP_CWORD]}
	numwords=${#COMP_WORDS[*]}
	if [ ${numwords} -gt 2 ]; then
		return 0
	fi
	COMPREPLY=($(compgen -W 'nvidia xfree' $cur))
	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
}
complete -F _openglupdate opengl-update