File: chmod

package info (click to toggle)
yash 2.50-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 5,036 kB
  • sloc: ansic: 33,211; makefile: 839; sh: 477; sed: 16
file content (150 lines) | stat: -rw-r--r-- 3,984 bytes parent folder | download | duplicates (5)
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
# (C) 2010-2013 magicant

# Completion script for the "chmod" command.
# Supports POSIX 2008, GNU coreutils 8.4, FreeBSD 8.1, OpenBSD 4.8, NetBSD 5.0,
# Mac OS X 10.6.3, SunOS 5.10, HP-UX 11i v3.

function completion/chmod {

	case $("${WORDS[1]}" --version 2>/dev/null) in
		(*'coreutils'*) typeset type=GNU ;;
		(*)             typeset type="$(uname 2>/dev/null)" ;;
	esac
	case $type in
		(GNU) typeset long=true ;;
		(*)   typeset long= ;;
	esac

	typeset OPTIONS POSIXOPTIONS ADDOPTIONS ARGOPT PREFIX
	POSIXOPTIONS=( #>#
	"R ${long:+--recursive}; recursively change the permission of files in a directory"
	) #<#

	ADDOPTIONS=()
	case $type in (GNU|FreeBSD|NetBSD|Darwin|SunOS)
		ADDOPTIONS=("$ADDOPTIONS" #>#
		"f ${long:+--silent --quiet}; ignore errors"
		) #<#
		case $type in (GNU|FreeBSD|Darwin)
			ADDOPTIONS=("$ADDOPTIONS" #>#
			"v ${long:+--verbose}; print a message for each file processed"
			) #<#
		esac
		case $type in (FreeBSD|NetBSD|Darwin)
			ADDOPTIONS=("$ADDOPTIONS" #>#
			"h; change the mode of symbolic links"
			) #<#
		esac
	esac
	case $type in (*BSD|Darwin)
		ADDOPTIONS=("$ADDOPTIONS" #>#
		"H; follow symbolic links in operands (with -R)"
		"L; follow all symbolic links (with -R)"
		"P; don't follow symbolic links (with -R)"
		) #<#
	esac
	case $type in
	(GNU)
		ADDOPTIONS=("$ADDOPTIONS" #>#
		"c --changes; print a message when a change has been made"
		"--no-preserve-root; cancel the --preserve-root option"
		"--preserve-root; don't recursively change the permission of the root directory"
		"--reference:; specify a file to whose permission changes are made"
		"--help"
		"--version"
		) #<#
		;;
	(HP-UX)
		ADDOPTIONS=("$ADDOPTIONS" #>#
		"A; preserve access control list entries"
		) #<#
		;;
	esac

	OPTIONS=("$POSIXOPTIONS" "$ADDOPTIONS")
	unset POSIXOPTIONS ADDOPTIONS

	command -f completion//parseoptions ${long:+-es}
	case $ARGOPT in
	(-)
		command -f completion//completeoptions
		;;
	(--reference)
		complete -P "$PREFIX" -f
		;;
	(*)
		command -f completion//getoperands
		if [ ${WORDS[#]} -eq 0 ]; then
			command -f completion/chmod::mode chmod
		else
			complete -f
		fi
		;;
	esac

}

function completion/chmod::mode {

	typeset word="${TARGETWORD#"$PREFIX"}"

	# we don't complete a numeric mode value
	case $word in (*[[:digit:]]*)
		return
	esac

	word=${word##*,}
	case $word in (*[-+=]*)
		case $word in
		(*[-+=]*[ugo]*)
			return
			;;
		(*[-+=]) #>>#
			complete -P "$TARGETWORD" -D "copy from the user part of permission" u
			complete -P "$TARGETWORD" -D "copy from the group part of permission" g
			complete -P "$TARGETWORD" -D "copy from the other part of permission" o
			;; #<<#
		esac
		#>>#
		complete -P "$TARGETWORD" -D "read permission" r
		complete -P "$TARGETWORD" -D "write permission" w
		complete -P "$TARGETWORD" -D "execute permission" x
		complete -P "$TARGETWORD" -D "execute permission (only when there's already one)" X
		#<<#
		case ${1-} in (chmod|find|mkdir|tar|umask) #>>#
			complete -P "$TARGETWORD" -D "set-user/group-ID" s
		esac #<<#
		case ${1-} in (chmod|find|mkdir|tar) #>>#
			complete -P "$TARGETWORD" -D "sticky bit" t
		esac #<<#
		return
	esac

	case $word in ('')
		case ${1-} in (rsync) #>>#
			complete -T -P "$TARGETWORD" -D "affect directories only" D
			complete -T -P "$TARGETWORD" -D "affect non-directories only" F
		esac #<<#
	esac

	case $word in
	(*a*)
		;;
	(*) #>>#
		complete -T -P "$TARGETWORD" -D "affect the user part of the permission" u
		complete -T -P "$TARGETWORD" -D "affect the group part of the permission" g
		complete -T -P "$TARGETWORD" -D "affect the other part of the permission" o
		complete -T -P "$TARGETWORD" -D "affect all parts of the permission" a
		;; #<<#
	esac

	#>>#
	complete -T -P "$TARGETWORD" -D "add the specified permission" -- +
	complete -T -P "$TARGETWORD" -D "remove the specified permission" -- -
	complete -T -P "$TARGETWORD" -D "set the permission to the specified one" -- =
	#<<#

}


# vim: set ft=sh ts=8 sts=8 sw=8 noet: