fork/fish-shell
git clone https://git.julia.blue/fork/fish-shell
6eaad2cd8
master
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_table0101010 2002200 2103210 4440000 4541010 6442200 6543210 9 10function __fish_umask_add 11set -l mask_digit$argv [ 1 ] 12set -l to_add$argv [ 2 ] 13 14set -l mask_table0000000 15if test $mask_digit -gt 0 16set mask_table$__fish_umask_add_table [ $mask_digit ] 17end 18set -l new_vals( string split'' $mask_table ) 19echo $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_table1335577 3236767 3337777 5674567 5775577 7676767 7777777 25 26function __fish_umask_remove 27set -l mask_digit$argv [ 1 ] 28set -l to_remove$argv [ 2 ] 29 30set -l mask_table1234567 31if test $mask_digit -gt 0 32set mask_table$__fish_umask_remove_table [ $mask_digit ] 33end 34set -l new_vals( string split'' $mask_table ) 35echo $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_table6 5 4 3 2 1 0 41function __fish_umask_set 42set -l to_set$argv [ 1 ] 43if test $to_set -eq 0 44echo 7 45return 46end 47echo $__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. 55if string match -qr'^0?[0-7]{1,3}$' --$argv 56string sub -s-4 0000 $argv 57return 0 58end 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. 63set -l basic_pattern'([ugoa]*)([=+-]?)([rwx]*)' 64if not string match -qr"^ $basic_pattern (, $basic_pattern )*\$" --$argv 65printf ( _ "%s: Invalid mask '%s'\n" ) umask$argv >&2 66return 1 67end 68 69# Split umask into individual digits. We erase the first one because it should always be zero. 70set -l res( string split'' $umask ) 71set -e res[ 1 ] 72 73for rightsin ( string split ,$argv ) 74set -l match( string match -r"^ $basic_pattern \$" $rights ) 75set -l scope$match [ 2 ] 76set -l mode$match [ 3 ] 77set -l perms$match [ 4 ] 78if test -n " $scope " -a -z " $mode " 79printf ( _ "%s: Invalid mask '%s'\n" ) umask$argv >&2 80return 1 81end 82if test -z " $scope " 83set scope a 84end 85if test -z " $mode " 86set mode = 87end 88 89set -l scopes_to_modify 90string match -q'*u*' $scope 91and set scopes_to_modify1 92string match -q'*g*' $scope 93and set -a scopes_to_modify2 94string match -q'*o*' $scope 95and set -a scopes_to_modify3 96string match -q'*a*' $scope 97and set scopes_to_modify1 2 3 98 99set -l val0 100if string match -q'*r*' $perms 101set val4 102end 103if string match -q'*w*' $perms 104set val( math $val +2 ) 105end 106if string match -q'*x*' $perms 107set val( math $val +1 ) 108end 109 110for jin $scopes_to_modify 111switch $mode 112case '=' 113set res[ $j ] ( __fish_umask_set $val ) 114 115case '+' 116set res[ $j ] ( __fish_umask_add $res [ $j ] $val ) 117 118case - 119set res[ $j ] ( __fish_umask_remove $res [ $j ] $val ) 120end 121end 122end 123 124echo 0 $res [ 1 ] $res [ 2 ] $res [ 3 ] 125return 0 126end 127 128function __fish_umask_print_symbolic 129set -l val 130set -l res"" 131set -l letter a u g o 132 133for iin 2 3 4 134set res$res ,$letter [ $i ] = 135set val( echo $umask | cut -c$i ) 136 137if contains $val 0 1 2 3 138set res{ $res } r 139end 140 141if contains $val 0 1 4 5 142set res{ $res } w 143end 144 145if contains $val 0 2 4 6 146set res{ $res } x 147end 148 149end 150 151echo ( string split -m1 '' --$res )[ 2 ] 152end 153 154function umask --description"Set default file permission mask" 155set -l options h/help p/as-command S/symbolic 156argparse -n umask$options --$argv 157or return 158 159if set -q _flag_help 160__fish_print_help umask 161return 0 162end 163 164switch ( count $argv ) 165case 0 166set -q umask 167or set -g umask113 168 169if set -q _flag_as_command 170echo umask$umask 171else if set -q _flag_symbolic 172__fish_umask_print_symbolic $umask 173else 174echo $umask 175end 176 177case 1 178if set -l parsed( __fish_umask_parse $argv ) 179set -g umask$parsed 180return 0 181end 182return 1 183 184case '*' 185{ 186 printf( _ '%s: Too many arguments' ) umask 187echo 188} >&2 189return 1 190end 191end