fork/fish-shell

git clone https://git.julia.blue/fork/fish-shell

Johannes AltmanningerRemove some redundant translation sources6eaad2cd8

master
5.5 KiB191 linesraw
1# localization: tier1
2
3# Support the usual (i.e., bash compatible) `umask` UI. This reports or modifies the magic global
4# `umask` variable which is monitored by the fish process.
5
6# This table is indexed by the base umask value to be modified. Each digit represents the new umask
7# value when the permissions to add are applied to the base umask value.
8set __fish_umask_add_table 0101010 2002200 2103210 4440000 4541010 6442200 6543210
9
10function __fish_umask_add
11    set -l mask_digit $argv[1]
12    set -l to_add $argv[2]
13
14    set -l mask_table 0000000
15    if test $mask_digit -gt 0
16        set mask_table $__fish_umask_add_table[$mask_digit]
17    end
18    set -l new_vals (string split '' $mask_table)
19    echo $new_vals[$to_add]
20end
21
22# This table is indexed by the base umask value to be modified. Each digit represents the new umask
23# value when the permissions to remove are applied to the base umask value.
24set __fish_umask_remove_table 1335577 3236767 3337777 5674567 5775577 7676767 7777777
25
26function __fish_umask_remove
27    set -l mask_digit $argv[1]
28    set -l to_remove $argv[2]
29
30    set -l mask_table 1234567
31    if test $mask_digit -gt 0
32        set mask_table $__fish_umask_remove_table[$mask_digit]
33    end
34    set -l new_vals (string split '' $mask_table)
35    echo $new_vals[$to_remove]
36end
37
38# This returns the mask corresponding to allowing the permissions to allow. In other words it
39# returns the inverse of the mask passed in.
40set __fish_umask_set_table 6 5 4 3 2 1 0
41function __fish_umask_set
42    set -l to_set $argv[1]
43    if test $to_set -eq 0
44        echo 7
45        return
46    end
47    echo $__fish_umask_set_table[$to_set]
48end
49
50# Given a umask string, possibly in symbolic mode, return an octal value with leading zeros.
51# This function expects to be called with a single value.
52function __fish_umask_parse
53    # Test if already a valid octal mask. If so pad it with zeros and return it.
54    # Note that umask values are always base 8 so they don't require a leading zero.
55    if string match -qr '^0?[0-7]{1,3}$' -- $argv
56        string sub -s -4 0000$argv
57        return 0
58    end
59
60    # Test if argument is a valid symbolic mask. Note that the basic pattern allows one illegal
61    # pattern: who and perms without a mode such as "urw". We test for that below after using the
62    # pattern to split the rights then testing for that invalid combination.
63    set -l basic_pattern '([ugoa]*)([=+-]?)([rwx]*)'
64    if not string match -qr "^$basic_pattern(,$basic_pattern)*\$" -- $argv
65        printf (_ "%s: Invalid mask '%s'\n") umask $argv >&2
66        return 1
67    end
68
69    # Split umask into individual digits. We erase the first one because it should always be zero.
70    set -l res (string split '' $umask)
71    set -e res[1]
72
73    for rights in (string split , $argv)
74        set -l match (string match -r "^$basic_pattern\$" $rights)
75        set -l scope $match[2]
76        set -l mode $match[3]
77        set -l perms $match[4]
78        if test -n "$scope" -a -z "$mode"
79            printf (_ "%s: Invalid mask '%s'\n") umask $argv >&2
80            return 1
81        end
82        if test -z "$scope"
83            set scope a
84        end
85        if test -z "$mode"
86            set mode =
87        end
88
89        set -l scopes_to_modify
90        string match -q '*u*' $scope
91        and set scopes_to_modify 1
92        string match -q '*g*' $scope
93        and set -a scopes_to_modify 2
94        string match -q '*o*' $scope
95        and set -a scopes_to_modify 3
96        string match -q '*a*' $scope
97        and set scopes_to_modify 1 2 3
98
99        set -l val 0
100        if string match -q '*r*' $perms
101            set val 4
102        end
103        if string match -q '*w*' $perms
104            set val (math $val + 2)
105        end
106        if string match -q '*x*' $perms
107            set val (math $val + 1)
108        end
109
110        for j in $scopes_to_modify
111            switch $mode
112                case '='
113                    set res[$j] (__fish_umask_set $val)
114
115                case '+'
116                    set res[$j] (__fish_umask_add $res[$j] $val)
117
118                case -
119                    set res[$j] (__fish_umask_remove $res[$j] $val)
120            end
121        end
122    end
123
124    echo 0$res[1]$res[2]$res[3]
125    return 0
126end
127
128function __fish_umask_print_symbolic
129    set -l val
130    set -l res ""
131    set -l letter a u g o
132
133    for i in 2 3 4
134        set res $res,$letter[$i]=
135        set val (echo $umask|cut -c $i)
136
137        if contains $val 0 1 2 3
138            set res {$res}r
139        end
140
141        if contains $val 0 1 4 5
142            set res {$res}w
143        end
144
145        if contains $val 0 2 4 6
146            set res {$res}x
147        end
148
149    end
150
151    echo (string split -m 1 '' -- $res)[2]
152end
153
154function umask --description "Set default file permission mask"
155    set -l options h/help p/as-command S/symbolic
156    argparse -n umask $options -- $argv
157    or return
158
159    if set -q _flag_help
160        __fish_print_help umask
161        return 0
162    end
163
164    switch (count $argv)
165        case 0
166            set -q umask
167            or set -g umask 113
168
169            if set -q _flag_as_command
170                echo umask $umask
171            else if set -q _flag_symbolic
172                __fish_umask_print_symbolic $umask
173            else
174                echo $umask
175            end
176
177        case 1
178            if set -l parsed (__fish_umask_parse $argv)
179                set -g umask $parsed
180                return 0
181            end
182            return 1
183
184        case '*'
185            {
186                printf (_ '%s: Too many arguments') umask
187                echo
188            } >&2
189            return 1
190    end
191end