2005-09-20 13:26:39 +00:00
|
|
|
/** \file builtin_set.c Functions defining the set builtin
|
|
|
|
|
|
|
|
Functions used for implementing the set builtin.
|
|
|
|
|
|
|
|
*/
|
2006-08-11 01:18:35 +00:00
|
|
|
#include "config.h"
|
|
|
|
|
2005-09-20 13:26:39 +00:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <wchar.h>
|
|
|
|
#include <wctype.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <termios.h>
|
|
|
|
#include <signal.h>
|
|
|
|
|
2006-02-28 13:17:16 +00:00
|
|
|
#include "fallback.h"
|
2005-09-20 13:26:39 +00:00
|
|
|
#include "util.h"
|
2006-02-28 13:17:16 +00:00
|
|
|
|
2006-01-20 14:27:21 +00:00
|
|
|
#include "wutil.h"
|
2005-09-20 13:26:39 +00:00
|
|
|
#include "builtin.h"
|
|
|
|
#include "env.h"
|
|
|
|
#include "expand.h"
|
|
|
|
#include "common.h"
|
|
|
|
#include "wgetopt.h"
|
|
|
|
#include "proc.h"
|
|
|
|
#include "parser.h"
|
2006-07-19 22:55:49 +00:00
|
|
|
|
2005-09-20 13:26:39 +00:00
|
|
|
|
2006-05-19 08:58:13 +00:00
|
|
|
/**
|
|
|
|
Error message for invalid path operations
|
|
|
|
*/
|
2006-04-20 18:29:44 +00:00
|
|
|
#define BUILTIN_SET_PATH_ERROR L"%ls: Could not add component %ls to %ls.\n"
|
2006-05-19 08:58:13 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
Hint for invalid path operation with a colon
|
|
|
|
*/
|
2006-04-20 18:29:44 +00:00
|
|
|
#define BUILTIN_SET_PATH_HINT L"%ls: Did you mean 'set %ls $%ls %ls'?\n"
|
2006-05-19 08:58:13 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
Error for mismatch between index count and elements
|
|
|
|
*/
|
2006-05-09 16:40:55 +00:00
|
|
|
#define BUILTIN_SET_ARG_COUNT L"%ls: The number of variable indexes does not match the number of values\n"
|
|
|
|
|
2006-05-19 08:58:13 +00:00
|
|
|
/**
|
|
|
|
Test if the specified variable should be subject to path validation
|
|
|
|
*/
|
2006-05-09 16:40:55 +00:00
|
|
|
static int is_path_variable( const wchar_t *env )
|
|
|
|
{
|
|
|
|
return contains_str( env,
|
|
|
|
L"PATH",
|
|
|
|
L"CDPATH",
|
|
|
|
(void *)0 );
|
|
|
|
}
|
2006-04-20 18:29:44 +00:00
|
|
|
|
2006-04-19 12:43:15 +00:00
|
|
|
/**
|
2006-05-09 16:40:55 +00:00
|
|
|
Call env_set. If this is a path variable, e.g. PATH, validate the
|
|
|
|
elements. On error, print a description of the problem to stderr.
|
2006-04-19 12:43:15 +00:00
|
|
|
*/
|
2006-05-09 16:40:55 +00:00
|
|
|
static int my_env_set( const wchar_t *key, array_list_t *val, int scope )
|
2006-04-10 15:36:26 +00:00
|
|
|
{
|
2006-05-09 16:40:55 +00:00
|
|
|
string_buffer_t sb;
|
|
|
|
int i;
|
|
|
|
int retcode = 0;
|
|
|
|
wchar_t *val_str=0;
|
|
|
|
|
|
|
|
if( is_path_variable( key ) )
|
2006-04-10 15:36:26 +00:00
|
|
|
{
|
2006-05-09 16:40:55 +00:00
|
|
|
int error = 0;
|
|
|
|
|
|
|
|
for( i=0; i<al_get_count( val ); i++ )
|
2006-04-10 15:36:26 +00:00
|
|
|
{
|
2006-05-09 16:40:55 +00:00
|
|
|
int show_perror = 0;
|
|
|
|
int show_hint = 0;
|
|
|
|
|
|
|
|
struct stat buff;
|
|
|
|
wchar_t *dir = (wchar_t *)al_get( val, i );
|
|
|
|
|
|
|
|
if( wstat( dir, &buff ) )
|
|
|
|
{
|
|
|
|
error = 1;
|
|
|
|
show_perror = 1;
|
|
|
|
}
|
|
|
|
|
2006-08-01 00:24:11 +00:00
|
|
|
if( !( S_ISDIR(buff.st_mode) ) )
|
2006-05-09 16:40:55 +00:00
|
|
|
{
|
|
|
|
error = 1;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if( error )
|
|
|
|
{
|
|
|
|
wchar_t *colon;
|
|
|
|
|
|
|
|
sb_printf( sb_err,
|
|
|
|
_(BUILTIN_SET_PATH_ERROR),
|
|
|
|
L"set",
|
|
|
|
dir,
|
|
|
|
key );
|
2006-05-19 08:58:13 +00:00
|
|
|
|
2006-05-09 16:40:55 +00:00
|
|
|
colon = wcschr( dir, L':' );
|
|
|
|
|
|
|
|
if( colon && *(colon+1) )
|
|
|
|
{
|
|
|
|
show_hint = 1;
|
|
|
|
}
|
2006-05-19 08:58:13 +00:00
|
|
|
|
2006-05-09 16:40:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if( show_perror )
|
|
|
|
{
|
|
|
|
builtin_wperror( L"set" );
|
|
|
|
}
|
|
|
|
|
|
|
|
if( show_hint )
|
|
|
|
{
|
|
|
|
sb_printf( sb_err,
|
|
|
|
_(BUILTIN_SET_PATH_HINT),
|
|
|
|
L"set",
|
|
|
|
key,
|
|
|
|
key,
|
|
|
|
wcschr( dir, L':' )+1);
|
|
|
|
}
|
|
|
|
|
|
|
|
if( error )
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if( error )
|
|
|
|
{
|
|
|
|
return 1;
|
2006-04-10 15:36:26 +00:00
|
|
|
}
|
2006-05-09 16:40:55 +00:00
|
|
|
|
2006-04-10 15:36:26 +00:00
|
|
|
}
|
|
|
|
|
2006-05-09 16:40:55 +00:00
|
|
|
sb_init( &sb );
|
2005-09-20 13:26:39 +00:00
|
|
|
|
2006-05-09 16:40:55 +00:00
|
|
|
if( al_get_count( val ) )
|
2005-09-20 13:26:39 +00:00
|
|
|
{
|
2006-05-09 16:40:55 +00:00
|
|
|
for( i=0; i<al_get_count( val ); i++ )
|
|
|
|
{
|
2007-01-28 13:43:31 +00:00
|
|
|
wchar_t *next =(wchar_t *)al_get( val, i );
|
|
|
|
sb_append( &sb, next?next:L"" );
|
2006-05-09 16:40:55 +00:00
|
|
|
if( i<al_get_count( val )-1 )
|
|
|
|
{
|
|
|
|
sb_append( &sb, ARRAY_SEP_STR );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
val_str = (wchar_t *)sb.buff;
|
|
|
|
|
2005-09-20 13:26:39 +00:00
|
|
|
}
|
|
|
|
|
2006-05-09 16:40:55 +00:00
|
|
|
switch( env_set( key, val_str, scope | ENV_USER ) )
|
2005-09-20 13:26:39 +00:00
|
|
|
{
|
2006-05-09 16:40:55 +00:00
|
|
|
case ENV_PERM:
|
|
|
|
{
|
|
|
|
sb_printf( sb_err, _(L"%ls: Tried to change the read-only variable '%ls'\n"), L"set", key );
|
|
|
|
retcode=1;
|
|
|
|
break;
|
|
|
|
}
|
2006-06-08 23:52:12 +00:00
|
|
|
|
|
|
|
case ENV_INVALID:
|
|
|
|
{
|
|
|
|
sb_printf( sb_err, _(L"%ls: Unknown error"), L"set" );
|
|
|
|
retcode=1;
|
|
|
|
break;
|
|
|
|
}
|
2005-09-20 13:26:39 +00:00
|
|
|
}
|
|
|
|
|
2006-05-09 16:40:55 +00:00
|
|
|
sb_destroy( &sb );
|
2005-09-20 13:26:39 +00:00
|
|
|
|
2006-05-09 16:40:55 +00:00
|
|
|
return retcode;
|
|
|
|
}
|
2005-09-20 13:26:39 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
Extract indexes from a destination argument of the form name[index1 index2...]
|
2006-05-09 16:40:55 +00:00
|
|
|
|
|
|
|
\param indexes the list to insert the new indexes into
|
|
|
|
\param src the source string to parse
|
|
|
|
\param name the name of the element. Return null if the name in \c src does not match this name
|
2006-06-20 21:20:16 +00:00
|
|
|
\param var_count the number of elements in the array to parse.
|
2006-05-09 16:40:55 +00:00
|
|
|
|
2006-06-17 13:07:08 +00:00
|
|
|
\return the total number of indexes parsed, or -1 on error
|
2005-09-20 13:26:39 +00:00
|
|
|
*/
|
2006-05-09 16:40:55 +00:00
|
|
|
static int parse_index( array_list_t *indexes,
|
|
|
|
const wchar_t *src,
|
2006-06-04 09:35:32 +00:00
|
|
|
const wchar_t *name,
|
|
|
|
int var_count )
|
2005-09-20 13:26:39 +00:00
|
|
|
{
|
2006-05-09 16:40:55 +00:00
|
|
|
int len;
|
|
|
|
|
2005-09-20 13:26:39 +00:00
|
|
|
int count = 0;
|
2006-05-09 16:40:55 +00:00
|
|
|
const wchar_t *src_orig = src;
|
|
|
|
|
2005-09-20 13:26:39 +00:00
|
|
|
if (src == 0)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (*src != L'\0' && (iswalnum(*src) || *src == L'_'))
|
|
|
|
{
|
|
|
|
src++;
|
|
|
|
}
|
|
|
|
|
2006-05-09 16:40:55 +00:00
|
|
|
if (*src != L'[')
|
2005-09-20 13:26:39 +00:00
|
|
|
{
|
2006-05-09 16:40:55 +00:00
|
|
|
sb_printf( sb_err, _(BUILTIN_SET_ARG_COUNT), L"set" );
|
2005-09-20 13:26:39 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2006-05-19 08:58:13 +00:00
|
|
|
|
2006-05-09 16:40:55 +00:00
|
|
|
len = src-src_orig;
|
2005-09-20 13:26:39 +00:00
|
|
|
|
2006-05-09 16:40:55 +00:00
|
|
|
if( (wcsncmp( src_orig, name, len )!=0) || (wcslen(name) != (len)) )
|
2005-09-20 13:26:39 +00:00
|
|
|
{
|
2006-05-09 16:40:55 +00:00
|
|
|
sb_printf( sb_err,
|
|
|
|
_(L"%ls: Multiple variable names specified in single call (%ls and %.*ls)\n"),
|
|
|
|
L"set",
|
|
|
|
name,
|
|
|
|
len,
|
|
|
|
src_orig);
|
|
|
|
return 0;
|
2005-09-20 13:26:39 +00:00
|
|
|
}
|
2006-05-09 16:40:55 +00:00
|
|
|
|
|
|
|
src++;
|
|
|
|
|
2005-11-25 14:44:34 +00:00
|
|
|
while (iswspace(*src))
|
2005-09-20 13:26:39 +00:00
|
|
|
{
|
|
|
|
src++;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (*src != L']')
|
|
|
|
{
|
|
|
|
wchar_t *end;
|
2006-06-04 09:35:32 +00:00
|
|
|
|
2007-01-09 03:20:05 +00:00
|
|
|
long l_ind;
|
|
|
|
|
|
|
|
errno = 0;
|
|
|
|
|
|
|
|
l_ind = wcstol(src, &end, 10);
|
|
|
|
|
|
|
|
if( end==src || errno )
|
2005-09-20 13:26:39 +00:00
|
|
|
{
|
2006-01-04 12:51:02 +00:00
|
|
|
sb_printf(sb_err, _(L"%ls: Invalid index starting at '%ls'\n"), L"set", src);
|
2006-05-09 16:40:55 +00:00
|
|
|
return 0;
|
2005-09-20 13:26:39 +00:00
|
|
|
}
|
2006-06-04 09:35:32 +00:00
|
|
|
|
|
|
|
if( l_ind < 0 )
|
|
|
|
{
|
|
|
|
l_ind = var_count+l_ind+1;
|
|
|
|
}
|
2006-05-09 16:40:55 +00:00
|
|
|
|
2006-07-31 16:17:19 +00:00
|
|
|
al_push_long(indexes, l_ind);
|
2005-09-20 13:26:39 +00:00
|
|
|
src = end;
|
|
|
|
count++;
|
2005-11-25 14:44:34 +00:00
|
|
|
while (iswspace(*src)) src++;
|
2005-09-20 13:26:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2006-04-19 12:43:15 +00:00
|
|
|
Update a list \c list by writing copies (using wcsdup) of the
|
|
|
|
values specified by \c values to the indexes specified by \c
|
|
|
|
indexes. The previous entries at the specidied position will be
|
|
|
|
free'd.
|
|
|
|
|
2006-06-04 20:14:51 +00:00
|
|
|
\return 0 if the operation was successfull, non-zero otherwise
|
2005-09-20 13:26:39 +00:00
|
|
|
*/
|
|
|
|
static int update_values( array_list_t *list,
|
|
|
|
array_list_t *indexes,
|
|
|
|
array_list_t *values )
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
/* Replace values where needed */
|
|
|
|
for( i = 0; i < al_get_count(indexes); i++ )
|
|
|
|
{
|
2006-07-31 16:17:19 +00:00
|
|
|
/*
|
|
|
|
The '- 1' below is because the indices in fish are
|
2006-12-08 13:31:36 +00:00
|
|
|
one-based, but the array_list_t uses zero-based indices
|
2006-07-31 16:17:19 +00:00
|
|
|
*/
|
|
|
|
long ind = al_get_long(indexes, i) - 1;
|
2005-09-20 13:26:39 +00:00
|
|
|
void *new = (void *) al_get(values, i);
|
2006-12-08 13:31:36 +00:00
|
|
|
if( ind < 0 )
|
2006-06-04 20:14:51 +00:00
|
|
|
{
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2006-04-19 12:43:15 +00:00
|
|
|
free((void *) al_get(list, ind));
|
2005-09-20 13:26:39 +00:00
|
|
|
al_set(list, ind, new != 0 ? wcsdup(new) : wcsdup(L""));
|
|
|
|
}
|
|
|
|
|
2006-06-04 20:14:51 +00:00
|
|
|
return 0;
|
2005-09-20 13:26:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2006-07-31 16:17:19 +00:00
|
|
|
Return 1 if an array list of longs contains the specified
|
2005-09-20 13:26:39 +00:00
|
|
|
value, 0 otherwise
|
|
|
|
*/
|
2006-07-31 16:17:19 +00:00
|
|
|
static int al_contains_long( array_list_t *list,
|
|
|
|
long val)
|
2005-09-20 13:26:39 +00:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < al_get_count(list); i++)
|
|
|
|
{
|
2006-07-31 16:17:19 +00:00
|
|
|
long current = al_get_long(list, i);
|
|
|
|
if( current != 0 && current == val )
|
2005-09-20 13:26:39 +00:00
|
|
|
{
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
Erase from a list values at specified indexes
|
|
|
|
*/
|
2006-05-09 16:40:55 +00:00
|
|
|
static void erase_values(array_list_t *list, array_list_t *indexes)
|
2005-09-20 13:26:39 +00:00
|
|
|
{
|
2006-10-26 10:22:53 +00:00
|
|
|
long i;
|
2005-09-20 13:26:39 +00:00
|
|
|
array_list_t result;
|
|
|
|
|
|
|
|
al_init(&result);
|
|
|
|
|
|
|
|
for (i = 0; i < al_get_count(list); i++)
|
|
|
|
{
|
2006-10-26 10:22:53 +00:00
|
|
|
if (!al_contains_long(indexes, i + 1))
|
2005-09-20 13:26:39 +00:00
|
|
|
{
|
|
|
|
al_push(&result, al_get(list, i));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2006-10-26 10:22:53 +00:00
|
|
|
free( (void *)al_get(list, i));
|
2005-09-20 13:26:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
al_truncate(list,0);
|
|
|
|
al_push_all( list, &result );
|
|
|
|
al_destroy(&result);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
Print the names of all environment variables in the scope, with or without values,
|
|
|
|
with or without escaping
|
|
|
|
*/
|
2005-10-07 10:36:51 +00:00
|
|
|
static void print_variables(int include_values, int esc, int scope)
|
2005-09-20 13:26:39 +00:00
|
|
|
{
|
|
|
|
array_list_t names;
|
|
|
|
int i;
|
2005-09-20 22:46:40 +00:00
|
|
|
|
2005-09-20 13:26:39 +00:00
|
|
|
al_init( &names );
|
|
|
|
env_get_names( &names, scope );
|
2005-09-20 22:46:40 +00:00
|
|
|
|
|
|
|
sort_list( &names );
|
|
|
|
|
2005-09-20 13:26:39 +00:00
|
|
|
for( i = 0; i < al_get_count(&names); i++ )
|
|
|
|
{
|
2005-09-22 20:16:52 +00:00
|
|
|
wchar_t *key = (wchar_t *)al_get( &names, i );
|
2006-10-19 15:24:18 +00:00
|
|
|
wchar_t *e_key = esc ? escape(key, 0) : wcsdup(key);
|
2005-10-07 10:36:51 +00:00
|
|
|
|
2005-09-20 13:26:39 +00:00
|
|
|
sb_append(sb_out, e_key);
|
2005-09-20 22:46:40 +00:00
|
|
|
|
2005-09-20 13:26:39 +00:00
|
|
|
if( include_values )
|
|
|
|
{
|
|
|
|
wchar_t *value = env_get(key);
|
2005-10-07 10:36:51 +00:00
|
|
|
wchar_t *e_value;
|
2006-08-31 15:44:00 +00:00
|
|
|
if( value )
|
|
|
|
{
|
2006-10-04 18:19:01 +00:00
|
|
|
int shorten = 0;
|
|
|
|
|
|
|
|
if( wcslen( value ) > 64 )
|
|
|
|
{
|
|
|
|
shorten = 1;
|
|
|
|
value = wcsndup( value, 60 );
|
|
|
|
if( !value )
|
2006-10-26 10:22:53 +00:00
|
|
|
{
|
2006-10-04 18:19:01 +00:00
|
|
|
DIE_MEM();
|
2006-10-26 10:22:53 +00:00
|
|
|
}
|
2006-10-04 18:19:01 +00:00
|
|
|
}
|
2006-10-26 10:22:53 +00:00
|
|
|
|
2006-08-31 15:44:00 +00:00
|
|
|
e_value = esc ? expand_escape_variable(value) : wcsdup(value);
|
2006-10-26 10:22:53 +00:00
|
|
|
|
2006-08-31 15:44:00 +00:00
|
|
|
sb_append2(sb_out, L" ", e_value, (void *)0);
|
|
|
|
free(e_value);
|
2006-10-26 10:22:53 +00:00
|
|
|
|
2006-10-04 18:19:01 +00:00
|
|
|
if( shorten )
|
|
|
|
{
|
|
|
|
sb_append(sb_out, L"\u2026");
|
|
|
|
free( value );
|
|
|
|
}
|
2006-10-26 10:22:53 +00:00
|
|
|
|
2006-08-31 15:44:00 +00:00
|
|
|
}
|
2005-09-20 13:26:39 +00:00
|
|
|
}
|
2005-09-20 22:46:40 +00:00
|
|
|
|
2005-09-20 13:26:39 +00:00
|
|
|
sb_append(sb_out, L"\n");
|
|
|
|
free(e_key);
|
|
|
|
}
|
2005-09-20 22:46:40 +00:00
|
|
|
al_destroy(&names);
|
2005-09-20 13:26:39 +00:00
|
|
|
}
|
|
|
|
|
2006-04-20 18:29:44 +00:00
|
|
|
|
|
|
|
|
2005-09-20 13:26:39 +00:00
|
|
|
/**
|
2006-05-19 08:58:13 +00:00
|
|
|
The set builtin. Creates, updates and erases environment variables
|
|
|
|
and environemnt variable arrays.
|
2005-09-20 13:26:39 +00:00
|
|
|
*/
|
2006-06-13 13:43:28 +00:00
|
|
|
static int builtin_set( wchar_t **argv )
|
2005-09-20 13:26:39 +00:00
|
|
|
{
|
2006-04-19 12:43:15 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
Variables used for parsing the argument list
|
|
|
|
*/
|
2005-09-20 13:26:39 +00:00
|
|
|
const static struct woption
|
|
|
|
long_options[] =
|
|
|
|
{
|
|
|
|
{
|
|
|
|
L"export", no_argument, 0, 'x'
|
2006-05-26 11:24:02 +00:00
|
|
|
}
|
|
|
|
,
|
2005-09-20 13:26:39 +00:00
|
|
|
{
|
|
|
|
L"global", no_argument, 0, 'g'
|
2006-05-26 11:24:02 +00:00
|
|
|
}
|
|
|
|
,
|
2005-09-20 13:26:39 +00:00
|
|
|
{
|
|
|
|
L"local", no_argument, 0, 'l'
|
2006-05-26 11:24:02 +00:00
|
|
|
}
|
|
|
|
,
|
2005-09-20 13:26:39 +00:00
|
|
|
{
|
|
|
|
L"erase", no_argument, 0, 'e'
|
2006-05-26 11:24:02 +00:00
|
|
|
}
|
|
|
|
,
|
2005-09-20 13:26:39 +00:00
|
|
|
{
|
|
|
|
L"names", no_argument, 0, 'n'
|
2006-05-26 11:24:02 +00:00
|
|
|
}
|
|
|
|
,
|
2005-09-20 13:26:39 +00:00
|
|
|
{
|
|
|
|
L"unexport", no_argument, 0, 'u'
|
2006-05-26 11:24:02 +00:00
|
|
|
}
|
|
|
|
,
|
2005-09-20 13:26:39 +00:00
|
|
|
{
|
|
|
|
L"universal", no_argument, 0, 'U'
|
2006-05-26 11:24:02 +00:00
|
|
|
}
|
|
|
|
,
|
2005-09-22 20:16:52 +00:00
|
|
|
{
|
|
|
|
L"query", no_argument, 0, 'q'
|
2006-05-26 11:24:02 +00:00
|
|
|
}
|
|
|
|
,
|
|
|
|
{
|
|
|
|
L"help", no_argument, 0, 'h'
|
|
|
|
}
|
|
|
|
,
|
2005-09-20 13:26:39 +00:00
|
|
|
{
|
|
|
|
0, 0, 0, 0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
;
|
|
|
|
|
2006-05-26 11:24:02 +00:00
|
|
|
const wchar_t *short_options = L"+xglenuUqh";
|
2005-09-20 13:26:39 +00:00
|
|
|
|
|
|
|
int argc = builtin_count_args(argv);
|
|
|
|
|
|
|
|
/*
|
|
|
|
Flags to set the work mode
|
|
|
|
*/
|
|
|
|
int local = 0, global = 0, export = 0;
|
|
|
|
int erase = 0, list = 0, unexport=0;
|
2005-09-22 20:16:52 +00:00
|
|
|
int universal = 0, query=0;
|
2005-09-20 13:26:39 +00:00
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
Variables used for performing the actual work
|
|
|
|
*/
|
|
|
|
wchar_t *dest = 0;
|
|
|
|
int retcode=0;
|
2006-05-09 16:40:55 +00:00
|
|
|
int scope;
|
|
|
|
int slice=0;
|
|
|
|
int i;
|
|
|
|
|
2006-08-26 01:37:22 +00:00
|
|
|
wchar_t *bad_char;
|
|
|
|
|
2006-05-09 16:40:55 +00:00
|
|
|
|
2005-09-20 13:26:39 +00:00
|
|
|
/* Parse options to obtain the requested operation and the modifiers */
|
|
|
|
woptind = 0;
|
|
|
|
while (1)
|
|
|
|
{
|
|
|
|
int c = wgetopt_long(argc, argv, short_options, long_options, 0);
|
|
|
|
|
|
|
|
if (c == -1)
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch(c)
|
|
|
|
{
|
|
|
|
case 0:
|
|
|
|
break;
|
2006-05-26 11:24:02 +00:00
|
|
|
|
2005-09-20 13:26:39 +00:00
|
|
|
case 'e':
|
|
|
|
erase = 1;
|
|
|
|
break;
|
2006-05-26 11:24:02 +00:00
|
|
|
|
2005-09-20 13:26:39 +00:00
|
|
|
case 'n':
|
|
|
|
list = 1;
|
|
|
|
break;
|
2006-05-26 11:24:02 +00:00
|
|
|
|
2005-09-20 13:26:39 +00:00
|
|
|
case 'x':
|
|
|
|
export = 1;
|
|
|
|
break;
|
2006-05-26 11:24:02 +00:00
|
|
|
|
2005-09-20 13:26:39 +00:00
|
|
|
case 'l':
|
|
|
|
local = 1;
|
|
|
|
break;
|
2006-05-26 11:24:02 +00:00
|
|
|
|
2005-09-20 13:26:39 +00:00
|
|
|
case 'g':
|
|
|
|
global = 1;
|
|
|
|
break;
|
2006-05-26 11:24:02 +00:00
|
|
|
|
2005-09-20 13:26:39 +00:00
|
|
|
case 'u':
|
|
|
|
unexport = 1;
|
|
|
|
break;
|
2006-05-26 11:24:02 +00:00
|
|
|
|
2005-09-20 13:26:39 +00:00
|
|
|
case 'U':
|
|
|
|
universal = 1;
|
|
|
|
break;
|
2006-05-26 11:24:02 +00:00
|
|
|
|
2005-09-22 20:16:52 +00:00
|
|
|
case 'q':
|
|
|
|
query = 1;
|
|
|
|
break;
|
2006-05-26 11:24:02 +00:00
|
|
|
|
|
|
|
case 'h':
|
|
|
|
builtin_print_help( argv[0], sb_out );
|
|
|
|
return 0;
|
|
|
|
|
2005-09-20 13:26:39 +00:00
|
|
|
case '?':
|
2007-01-21 14:55:27 +00:00
|
|
|
builtin_unknown_option( argv[0], argv[woptind-1] );
|
2005-09-20 13:26:39 +00:00
|
|
|
return 1;
|
2006-05-26 11:24:02 +00:00
|
|
|
|
2005-09-20 13:26:39 +00:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-04-19 12:43:15 +00:00
|
|
|
/*
|
|
|
|
Ok, all arguments have been parsed, let's validate them
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
If we are checking the existance of a variable (-q) we can not
|
|
|
|
also specify scope
|
|
|
|
*/
|
|
|
|
|
2005-09-22 20:16:52 +00:00
|
|
|
if( query && (erase || list || global || local || universal || export || unexport ) )
|
|
|
|
{
|
2006-01-04 12:51:02 +00:00
|
|
|
sb_printf(sb_err,
|
2007-01-31 16:03:17 +00:00
|
|
|
BUILTIN_ERR_COMBO,
|
|
|
|
argv[0] );
|
2006-01-04 12:51:02 +00:00
|
|
|
|
2006-05-26 11:24:02 +00:00
|
|
|
builtin_print_help( argv[0], sb_err );
|
2005-09-22 20:16:52 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-04-19 12:43:15 +00:00
|
|
|
/* We can't both list and erase varaibles */
|
2005-09-20 13:26:39 +00:00
|
|
|
if( erase && list )
|
|
|
|
{
|
2006-01-04 12:51:02 +00:00
|
|
|
sb_printf(sb_err,
|
2007-01-31 16:03:17 +00:00
|
|
|
BUILTIN_ERR_COMBO,
|
|
|
|
argv[0] );
|
2006-01-04 12:51:02 +00:00
|
|
|
|
2006-05-26 11:24:02 +00:00
|
|
|
builtin_print_help( argv[0], sb_err );
|
2005-09-20 13:26:39 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2006-04-19 12:43:15 +00:00
|
|
|
/*
|
|
|
|
Variables can only have one scope
|
|
|
|
*/
|
2005-09-20 13:26:39 +00:00
|
|
|
if( local + global + universal > 1 )
|
|
|
|
{
|
|
|
|
sb_printf( sb_err,
|
|
|
|
BUILTIN_ERR_GLOCAL,
|
2007-01-31 16:03:17 +00:00
|
|
|
argv[0] );
|
2006-05-26 11:24:02 +00:00
|
|
|
builtin_print_help( argv[0], sb_err );
|
2005-09-20 13:26:39 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2006-04-19 12:43:15 +00:00
|
|
|
/*
|
|
|
|
Variables can only have one export status
|
|
|
|
*/
|
2005-09-20 13:26:39 +00:00
|
|
|
if( export && unexport )
|
|
|
|
{
|
2006-01-04 12:51:02 +00:00
|
|
|
sb_printf( sb_err,
|
2005-09-20 13:26:39 +00:00
|
|
|
BUILTIN_ERR_EXPUNEXP,
|
2007-01-31 16:03:17 +00:00
|
|
|
argv[0] );
|
2006-05-26 11:24:02 +00:00
|
|
|
builtin_print_help( argv[0], sb_err );
|
2005-09-20 13:26:39 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2006-05-09 16:40:55 +00:00
|
|
|
/*
|
|
|
|
Calculate the scope value for variable assignement
|
|
|
|
*/
|
|
|
|
scope = (local ? ENV_LOCAL : 0) | (global ? ENV_GLOBAL : 0) | (export ? ENV_EXPORT : 0) | (unexport ? ENV_UNEXPORT : 0) | (universal ? ENV_UNIVERSAL:0) | ENV_USER;
|
|
|
|
|
2005-09-22 20:16:52 +00:00
|
|
|
if( query )
|
|
|
|
{
|
|
|
|
/*
|
2006-06-04 20:14:51 +00:00
|
|
|
Query mode. Return the number of variables that do not exist
|
2006-04-19 12:43:15 +00:00
|
|
|
out of the specified variables.
|
2005-09-22 20:16:52 +00:00
|
|
|
*/
|
|
|
|
int i;
|
|
|
|
for( i=woptind; i<argc; i++ )
|
|
|
|
{
|
2007-01-30 20:43:44 +00:00
|
|
|
wchar_t *arg = argv[i];
|
|
|
|
int slice=0;
|
|
|
|
|
|
|
|
if( !(dest = wcsdup(arg)))
|
|
|
|
{
|
|
|
|
DIE_MEM();
|
|
|
|
}
|
|
|
|
|
|
|
|
if( wcschr( dest, L'[' ) )
|
|
|
|
{
|
|
|
|
slice = 1;
|
|
|
|
*wcschr( dest, L'[' )=0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if( slice )
|
|
|
|
{
|
|
|
|
array_list_t indexes;
|
|
|
|
array_list_t result;
|
|
|
|
int j;
|
|
|
|
|
|
|
|
al_init( &result );
|
|
|
|
al_init( &indexes );
|
|
|
|
|
|
|
|
tokenize_variable_array( env_get( dest ), &result );
|
|
|
|
|
|
|
|
if( !parse_index( &indexes, arg, dest, al_get_count( &result ) ) )
|
|
|
|
{
|
|
|
|
builtin_print_help( argv[0], sb_err );
|
|
|
|
retcode = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
for( j=0; j<al_get_count( &indexes ); j++ )
|
|
|
|
{
|
|
|
|
long idx = al_get_long( &indexes, j );
|
|
|
|
if( idx < 1 || idx > al_get_count( &result ) )
|
|
|
|
{
|
|
|
|
retcode++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
2006-04-19 12:43:15 +00:00
|
|
|
{
|
2007-01-30 20:43:44 +00:00
|
|
|
if( !env_exist( arg, scope ) )
|
|
|
|
{
|
|
|
|
retcode++;
|
|
|
|
}
|
2006-04-19 12:43:15 +00:00
|
|
|
}
|
2005-09-22 20:16:52 +00:00
|
|
|
|
2007-01-30 20:43:44 +00:00
|
|
|
free( dest );
|
|
|
|
|
2005-09-22 20:16:52 +00:00
|
|
|
}
|
|
|
|
return retcode;
|
|
|
|
}
|
|
|
|
|
2006-08-31 15:44:00 +00:00
|
|
|
if( list )
|
|
|
|
{
|
|
|
|
/* Maybe we should issue an error if there are any other arguments? */
|
|
|
|
print_variables(0, 0, scope);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2006-05-09 16:40:55 +00:00
|
|
|
if( woptind == argc )
|
2005-09-20 13:26:39 +00:00
|
|
|
{
|
2006-05-09 16:40:55 +00:00
|
|
|
/*
|
|
|
|
Print values of variables
|
|
|
|
*/
|
|
|
|
|
|
|
|
if( erase )
|
2005-12-07 14:48:49 +00:00
|
|
|
{
|
2006-05-09 16:40:55 +00:00
|
|
|
sb_printf( sb_err,
|
|
|
|
_(L"%ls: Erase needs a variable name\n%ls\n"),
|
2007-01-31 16:03:17 +00:00
|
|
|
argv[0] );
|
2006-05-09 16:40:55 +00:00
|
|
|
|
2006-05-26 11:24:02 +00:00
|
|
|
builtin_print_help( argv[0], sb_err );
|
2006-05-09 16:40:55 +00:00
|
|
|
retcode = 1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
print_variables( 1, 1, scope );
|
|
|
|
}
|
|
|
|
|
|
|
|
return retcode;
|
2005-09-20 13:26:39 +00:00
|
|
|
}
|
|
|
|
|
2006-05-09 16:40:55 +00:00
|
|
|
if( !(dest = wcsdup(argv[woptind])))
|
|
|
|
{
|
2006-07-03 10:39:57 +00:00
|
|
|
DIE_MEM();
|
2006-05-09 16:40:55 +00:00
|
|
|
}
|
2005-09-20 13:26:39 +00:00
|
|
|
|
2006-05-09 16:40:55 +00:00
|
|
|
if( wcschr( dest, L'[' ) )
|
2005-09-20 13:26:39 +00:00
|
|
|
{
|
2006-05-09 16:40:55 +00:00
|
|
|
slice = 1;
|
|
|
|
*wcschr( dest, L'[' )=0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if( !wcslen( dest ) )
|
|
|
|
{
|
|
|
|
free( dest );
|
|
|
|
sb_printf( sb_err, BUILTIN_ERR_VARNAME_ZERO, argv[0] );
|
2006-06-03 22:31:50 +00:00
|
|
|
builtin_print_help( argv[0], sb_err );
|
2006-05-09 16:40:55 +00:00
|
|
|
return 1;
|
2005-09-20 13:26:39 +00:00
|
|
|
}
|
2006-08-28 12:02:44 +00:00
|
|
|
|
2006-08-26 01:37:22 +00:00
|
|
|
if( (bad_char = wcsvarname( dest ) ) )
|
|
|
|
{
|
|
|
|
sb_printf( sb_err, BUILTIN_ERR_VARCHAR, argv[0], *bad_char );
|
|
|
|
builtin_print_help( argv[0], sb_err );
|
|
|
|
free( dest );
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2006-06-04 20:14:51 +00:00
|
|
|
if( slice && erase && (scope != ENV_USER) )
|
|
|
|
{
|
|
|
|
free( dest );
|
|
|
|
sb_printf( sb_err, _(L"%ls: Can not specify scope when erasing array slice\n"), argv[0] );
|
|
|
|
builtin_print_help( argv[0], sb_err );
|
|
|
|
return 1;
|
|
|
|
}
|
2006-05-09 16:40:55 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
set assignment can work in two modes, either using slices or
|
|
|
|
using the whole array. We detect which mode is used here.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if( slice )
|
2006-04-20 18:29:44 +00:00
|
|
|
{
|
|
|
|
|
2006-05-09 16:40:55 +00:00
|
|
|
/*
|
|
|
|
Slice mode
|
|
|
|
*/
|
|
|
|
int idx_count, val_count;
|
|
|
|
array_list_t values;
|
|
|
|
array_list_t indexes;
|
2006-06-04 09:35:32 +00:00
|
|
|
array_list_t result;
|
2006-05-09 16:40:55 +00:00
|
|
|
|
|
|
|
al_init(&values);
|
|
|
|
al_init(&indexes);
|
2006-06-04 09:35:32 +00:00
|
|
|
al_init(&result);
|
2006-08-28 12:02:44 +00:00
|
|
|
|
2006-06-04 09:35:32 +00:00
|
|
|
tokenize_variable_array( env_get(dest), &result );
|
2006-08-26 01:37:22 +00:00
|
|
|
|
2006-05-09 16:40:55 +00:00
|
|
|
for( ; woptind<argc; woptind++ )
|
|
|
|
{
|
2006-06-04 09:35:32 +00:00
|
|
|
if( !parse_index( &indexes, argv[woptind], dest, al_get_count( &result ) ) )
|
2006-04-20 18:29:44 +00:00
|
|
|
{
|
2006-06-03 22:31:50 +00:00
|
|
|
builtin_print_help( argv[0], sb_err );
|
2006-05-09 16:40:55 +00:00
|
|
|
retcode = 1;
|
|
|
|
break;
|
2006-04-20 18:29:44 +00:00
|
|
|
}
|
|
|
|
|
2006-05-09 16:40:55 +00:00
|
|
|
val_count = argc-woptind-1;
|
|
|
|
idx_count = al_get_count( &indexes );
|
|
|
|
|
|
|
|
if( !erase )
|
2006-04-20 18:29:44 +00:00
|
|
|
{
|
2006-05-09 16:40:55 +00:00
|
|
|
if( val_count < idx_count )
|
2006-04-20 18:29:44 +00:00
|
|
|
{
|
2006-05-09 16:40:55 +00:00
|
|
|
sb_printf( sb_err, _(BUILTIN_SET_ARG_COUNT), argv[0] );
|
2006-06-03 22:31:50 +00:00
|
|
|
builtin_print_help( argv[0], sb_err );
|
2006-05-09 16:40:55 +00:00
|
|
|
retcode=1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if( val_count == idx_count )
|
|
|
|
{
|
|
|
|
woptind++;
|
|
|
|
break;
|
2006-04-20 18:29:44 +00:00
|
|
|
}
|
|
|
|
}
|
2006-05-09 16:40:55 +00:00
|
|
|
}
|
2006-04-20 18:29:44 +00:00
|
|
|
|
2006-05-09 16:40:55 +00:00
|
|
|
if( !retcode )
|
2006-04-20 18:29:44 +00:00
|
|
|
{
|
2006-05-09 16:40:55 +00:00
|
|
|
/*
|
|
|
|
Slice indexes have been calculated, do the actual work
|
|
|
|
*/
|
2006-04-20 18:29:44 +00:00
|
|
|
|
2006-05-09 16:40:55 +00:00
|
|
|
if( erase )
|
|
|
|
{
|
|
|
|
erase_values(&result, &indexes);
|
|
|
|
my_env_set( dest, &result, scope);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
array_list_t value;
|
|
|
|
al_init(&value);
|
2005-09-20 13:26:39 +00:00
|
|
|
|
2006-05-09 16:40:55 +00:00
|
|
|
while( woptind < argc )
|
|
|
|
{
|
|
|
|
al_push(&value, argv[woptind++]);
|
|
|
|
}
|
2005-09-20 13:26:39 +00:00
|
|
|
|
2006-06-04 20:14:51 +00:00
|
|
|
if( update_values( &result,
|
|
|
|
&indexes,
|
|
|
|
&value ) )
|
|
|
|
{
|
|
|
|
sb_printf( sb_err, L"%ls: ", argv[0] );
|
|
|
|
sb_printf( sb_err, ARRAY_BOUNDS_ERR );
|
|
|
|
sb_append( sb_err, L"\n" );
|
|
|
|
}
|
2006-05-09 16:40:55 +00:00
|
|
|
|
|
|
|
my_env_set(dest,
|
|
|
|
&result,
|
|
|
|
scope);
|
|
|
|
|
|
|
|
al_destroy( &value );
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
2005-09-20 13:26:39 +00:00
|
|
|
|
2006-06-12 21:47:42 +00:00
|
|
|
al_foreach( &result, &free );
|
2006-06-04 09:35:32 +00:00
|
|
|
al_destroy( &result );
|
|
|
|
|
2006-05-09 16:40:55 +00:00
|
|
|
al_destroy(&indexes);
|
|
|
|
al_destroy(&values);
|
2006-04-19 12:43:15 +00:00
|
|
|
|
2005-09-20 13:26:39 +00:00
|
|
|
}
|
2006-05-09 16:40:55 +00:00
|
|
|
else
|
2005-09-20 13:26:39 +00:00
|
|
|
{
|
2006-05-09 16:40:55 +00:00
|
|
|
woptind++;
|
2005-09-20 13:26:39 +00:00
|
|
|
|
2006-05-09 16:40:55 +00:00
|
|
|
/*
|
|
|
|
No slicing
|
|
|
|
*/
|
|
|
|
if( erase )
|
2005-09-20 13:26:39 +00:00
|
|
|
{
|
2006-05-09 16:40:55 +00:00
|
|
|
if( woptind != argc )
|
2005-09-20 13:26:39 +00:00
|
|
|
{
|
2006-05-09 16:40:55 +00:00
|
|
|
sb_printf( sb_err,
|
2007-01-31 16:03:17 +00:00
|
|
|
_(L"%ls: Values cannot be specfied with erase\n"),
|
|
|
|
argv[0] );
|
2006-06-03 22:31:50 +00:00
|
|
|
builtin_print_help( argv[0], sb_err );
|
2006-05-09 16:40:55 +00:00
|
|
|
retcode=1;
|
2005-09-20 13:26:39 +00:00
|
|
|
}
|
2006-05-09 16:40:55 +00:00
|
|
|
else
|
2005-09-20 13:26:39 +00:00
|
|
|
{
|
2006-06-04 20:14:51 +00:00
|
|
|
retcode = env_remove( dest, scope );
|
2005-09-20 13:26:39 +00:00
|
|
|
}
|
|
|
|
}
|
2006-05-09 16:40:55 +00:00
|
|
|
else
|
2005-09-20 13:26:39 +00:00
|
|
|
{
|
2006-05-09 16:40:55 +00:00
|
|
|
array_list_t val;
|
|
|
|
al_init( &val );
|
|
|
|
|
|
|
|
for( i=woptind; i<argc; i++ )
|
2005-09-20 13:26:39 +00:00
|
|
|
{
|
2006-05-09 16:40:55 +00:00
|
|
|
al_push( &val, argv[i] );
|
2005-09-20 13:26:39 +00:00
|
|
|
}
|
|
|
|
|
2006-05-09 16:40:55 +00:00
|
|
|
retcode = my_env_set( dest, &val, scope );
|
|
|
|
|
|
|
|
al_destroy( &val );
|
|
|
|
|
|
|
|
}
|
2005-09-20 13:26:39 +00:00
|
|
|
}
|
|
|
|
|
2006-05-09 16:40:55 +00:00
|
|
|
free( dest );
|
2005-09-20 13:26:39 +00:00
|
|
|
|
|
|
|
return retcode;
|
2006-05-09 16:40:55 +00:00
|
|
|
|
2005-09-20 13:26:39 +00:00
|
|
|
}
|
|
|
|
|