Code cleanups and API documentation updates

darcs-hash:20051023121429-ac50b-6ff72171b5a90b6e398bd84e748388c1dba831d9.gz
This commit is contained in:
axel 2005-10-23 22:14:29 +10:00
parent ba6ad5025e
commit f8de9de13d
4 changed files with 80 additions and 67 deletions

View file

@ -563,6 +563,7 @@ certain environment variables.
- \c CDPATH, which is an array of directories in which to search for the new directory for the \c cd builtin. - \c CDPATH, which is an array of directories in which to search for the new directory for the \c cd builtin.
- \c fish_color_normal, \c fish_color_command, \c fish_color_substitution, \c fish_color_redirection, \c fish_color_end, \c fish_color_error, \c fish_color_param, \c fish_color_comment, \c fish_color_match, \c fish_color_search_match, \c fish_color_cwd, \c fish_pager_color_prefix, \c fish_pager_color_completion, \c fish_pager_color_description and \c fish_pager_color_progress are used to change the color of various elements in \c fish. These variables are universal, i.e. when changing them, their new value will be used by all running fish sessions. The new value will also be retained when restarting fish. - \c fish_color_normal, \c fish_color_command, \c fish_color_substitution, \c fish_color_redirection, \c fish_color_end, \c fish_color_error, \c fish_color_param, \c fish_color_comment, \c fish_color_match, \c fish_color_search_match, \c fish_color_cwd, \c fish_pager_color_prefix, \c fish_pager_color_completion, \c fish_pager_color_description and \c fish_pager_color_progress are used to change the color of various elements in \c fish. These variables are universal, i.e. when changing them, their new value will be used by all running fish sessions. The new value will also be retained when restarting fish.
- \c PATH, which is an array of directories in which to search for commands - \c PATH, which is an array of directories in which to search for commands
- \c umask, which is the current file creation mask. The preffered way to change the umask variable is through the <a href="commands.html#umask">umask shellscript function</a>. An attempt to set umask to an invalid value will always fail.
\c fish also sends additional information to the user through the \c fish also sends additional information to the user through the
values of certain environment variables. The user can not change the values of these variables. They are: values of certain environment variables. The user can not change the values of these variables. They are:
@ -635,6 +636,7 @@ builtins or shellscript functions, and can only be used inside fish.
- <a href="builtins.html#switch">switch</a>, conditionally execute a block of commands - <a href="builtins.html#switch">switch</a>, conditionally execute a block of commands
- <a href="commands.html#tokenize">tokenize</a>, split a string up into multiple tokens - <a href="commands.html#tokenize">tokenize</a>, split a string up into multiple tokens
- <a href="builtins.html#ulimit">ulimit</a>, set or get the shells resurce usage limits - <a href="builtins.html#ulimit">ulimit</a>, set or get the shells resurce usage limits
- <a href="commandss.html#umask">umask</a>, set or get the file creation mask
- <a href="builtins.html#while">while</a>, perform a block of commands while a condition is met - <a href="builtins.html#while">while</a>, perform a block of commands while a condition is met
For more information about these commands, use the <tt>--help</tt> For more information about these commands, use the <tt>--help</tt>

View file

@ -10,9 +10,30 @@ With no argument, the current file-creation mask is printed, if an
argument is specified, it is the new file creation mask. The mask may argument is specified, it is the new file creation mask. The mask may
be specified as an octal number, in which case it is interpreted as be specified as an octal number, in which case it is interpreted as
the rights that should be masked away, i.e. it is the inverse of the the rights that should be masked away, i.e. it is the inverse of the
file permissions any new files will have. If a synbolic mask is file permissions any new files will have.
specified, the actual file permission bits, and not the inverse, are
specified. If a symbolic mask is specified, the actual file permission bits, and
not the inverse, should be specified. A symbolic mask is a comma
separated list of rights. Each right consists of three parts:
- The first part specifies to whom this set of right applies, and can
be one of \c u, \c g, \c o or \c a, where \c u specifies the user who
owns the file, \c g specifies the group owner of the file, \c o
specifiec other users rights and \c a specifies all three should be
changed.
- The second part of a right specifies the mode, and can be one of \c
=, \c + or \c -, where \c = specifies that the rights should be set to
the new value, \c + specifies that the specified right should be added
to those previously specified and \c - specifies that the specified
rights should be removed from those previously specified.
- The third part of a right specifies what rights should be changed
and can be any compination of \c r, \c w and \c x, representing
read, write and execute rights.
If the first and second parts are skipped, they are assumed to be \c a
and \c =, respectively. As an example, <code>r,u+w</code> means all
users should have read access and the file owner should also have
write access.
- <code>-h</code> or <code>--help</code> print this message - <code>-h</code> or <code>--help</code> print this message
- <code>-S</code> or <code>--symbolic</code> prints the file-creation mask in symbolic form instead of octal form. Use <code>man chmod</code> for more information. - <code>-S</code> or <code>--symbolic</code> prints the file-creation mask in symbolic form instead of octal form. Use <code>man chmod</code> for more information.

60
env.c
View file

@ -116,6 +116,11 @@ static hash_table_t *global;
*/ */
static hash_table_t env_read_only; static hash_table_t env_read_only;
/**
Table of variables whose value is dynamically calculated, such as umask, status, etc
*/
static hash_table_t env_electric;
/** /**
Exported variable array used by execv Exported variable array used by execv
*/ */
@ -194,6 +199,21 @@ static void start_fishd()
sb_destroy( &cmd ); sb_destroy( &cmd );
} }
/**
Return the current umask value.
*/
static mode_t get_umask()
{
mode_t res;
res = umask( 0 );
umask( res );
return res;
}
/**
Universal variable callback function. This function makes sure the
proper events are triggered when an event occurs.
*/
static void universal_callback( int type, static void universal_callback( int type,
const wchar_t *name, const wchar_t *name,
const wchar_t *val ) const wchar_t *val )
@ -254,6 +274,14 @@ void env_init()
hash_put( &env_read_only, L"COLUMNS", L"" ); hash_put( &env_read_only, L"COLUMNS", L"" );
hash_put( &env_read_only, L"PWD", L"" ); hash_put( &env_read_only, L"PWD", L"" );
/*
Names of all dynamically calculated variables
*/
hash_init( &env_electric, &hash_wcs_func, &hash_wcs_cmp );
hash_put( &env_electric, L"history", L"" );
hash_put( &env_electric, L"status", L"" );
hash_put( &env_electric, L"umask", L"" );
/* /*
HOME and USER should be writeable by root, since this can be a HOME and USER should be writeable by root, since this can be a
convenient way to install software. convenient way to install software.
@ -398,17 +426,24 @@ void env_set( const wchar_t *key,
{ {
wchar_t *end; wchar_t *end;
int mask; int mask;
/*
Set the new umask
*/
if( val && wcslen(val) ) if( val && wcslen(val) )
{ {
errno=0; errno=0;
mask = wcstol( val, &end, 8 ); mask = wcstol( val, &end, 8 );
if( !errno && !*end ) if( !errno && (!*end) && (mask <= 0777) && (mask >= 0) )
{ {
umask( mask ); umask( mask );
} }
} }
/*
Do not actually create a umask variable, on env_get, it will be calculated dynamically
*/
return;
} }
@ -665,6 +700,12 @@ wchar_t *env_get( const wchar_t *key )
sb_printf( &dyn_var, L"%d", proc_get_last_status() ); sb_printf( &dyn_var, L"%d", proc_get_last_status() );
return (wchar_t *)dyn_var.buff; return (wchar_t *)dyn_var.buff;
} }
else if( wcscmp( key, L"umask" )==0 )
{
sb_clear( &dyn_var );
sb_printf( &dyn_var, L"0%0.3o", get_umask() );
return (wchar_t *)dyn_var.buff;
}
while( env != 0 ) while( env != 0 )
{ {
@ -703,7 +744,7 @@ int env_exist( const wchar_t *key )
env_node_t *env = top; env_node_t *env = top;
wchar_t *item; wchar_t *item;
if( hash_get( &env_read_only, key ) ) if( hash_get( &env_read_only, key ) || hash_get( &env_electric, key ) )
{ {
return 1; return 1;
} }
@ -807,6 +848,13 @@ static void add_to_hash( const void *k, void *aux )
0 ); 0 );
} }
static void add_key_to_list( const void * key,
const void * val,
void *aux )
{
al_push( (array_list_t *)aux, key );
}
void env_get_names( array_list_t *l, int flags ) void env_get_names( array_list_t *l, int flags )
{ {
@ -853,11 +901,9 @@ void env_get_names( array_list_t *l, int flags )
hash_foreach2( &global_env->env, hash_foreach2( &global_env->env,
add_key_to_hash, add_key_to_hash,
&names ); &names );
if( get_names_show_unexported ) if( get_names_show_unexported )
{ hash_foreach2( &env_electric, &add_key_to_list, l );
al_push( l, L"history" );
al_push( l, L"status" );
}
if( get_names_show_exported ) if( get_names_show_exported )
{ {

View file

@ -724,36 +724,6 @@ function type -d "Print the type of a command"
return $status return $status
end end
function __fish_umask_help
set bullet \*
if count $LANG >/dev/null
if test (expr match $LANG ".*UTF") -gt 0
set bullet \u2022
end
end
echo \tumask - Set or get the user file-creation mask
echo
echo (__bold Synopsis)
echo
echo \t(set_color $fish_color_command)umask(set_color normal) [OPTIONS] [mask]
echo
echo (__bold Description)
echo
echo \tWith no argument, the current file-creation mask is printed, if an\n\targument is specified, it is the new file creation mask.
echo
echo \t$bullet (__bold -h) or (__bold --help) print this message
echo \t$bullet (__bold -S) or (__bold --symbolic) prints the file-creation mask in symbolic\n\t\ \ form instead of octal form. Use \'(set_color $fish_color_command)man(set_color $fish_color_normal) chmod\' for more information.
echo \t$bullet (__bold -p) or (__bold --as-command) prints any output in a form that may be reused\n\t\ \ as input
echo
echo (__bold Example)
echo
echo \t\'(set_color $fish_color_command)umask(set_color normal) 600\' sets the file creation mask to read and write for the\n\towner and no permissions at all for any other users.
echo
end
function __fish_umask_parse -d "Parses a file permission specification as into an octal version" function __fish_umask_parse -d "Parses a file permission specification as into an octal version"
# Test if already a valid octal mask, and pad it with zeros # Test if already a valid octal mask, and pad it with zeros
if echo $argv | grep -E '^(0|)[0-7]{1,3}$' >/dev/null if echo $argv | grep -E '^(0|)[0-7]{1,3}$' >/dev/null
@ -768,19 +738,6 @@ function __fish_umask_parse -d "Parses a file permission specification as into a
set -e implicit_all set -e implicit_all
# Make sure the current umask is defined
if not set -q umask
set umask 0000
end
# If umask is invalid, reset it
if not echo $umask | grep -E '^(0|)[0-7]{1,3}$' >/dev/null
set umask 0000
end
# Pad umask with zeros
for i in (seq (echo 5-(echo $umask|wc -c)|bc)); set -- argv 0$umask; end
# Insert inverted umask into res variable # Insert inverted umask into res variable
set tmp $umask set tmp $umask
@ -875,19 +832,6 @@ function __fish_umask_print_symbolic
set -l res "" set -l res ""
set -l letter a u g o set -l letter a u g o
# Make sure the current umask is defined
if not set -q umask
set umask 0000
end
# If umask is invalid, reset it
if not echo $umask | grep -E '^(0|)[0-7]{1,3}$' >/dev/null
set umask 0000
end
# Pad umask with zeros
for i in (seq (echo 5-(echo $umask|wc -c)|bc)); set -- argv 0$umask; end
for i in 2 3 4 for i in 2 3 4
set res $res,$letter[$i]= set res $res,$letter[$i]=
set val (echo $umask|cut -c $i) set val (echo $umask|cut -c $i)
@ -933,7 +877,7 @@ function umask -d "Set default file permission mask"
switch $opt[1] switch $opt[1]
case -h --help case -h --help
__fish_umask_help help umask
return 0 return 0
case -p --as-command case -p --as-command