2005-09-20 13:26:39 +00:00
|
|
|
/** \file builtin_set.c Functions defining the set builtin
|
|
|
|
|
2011-12-27 03:18:46 +00:00
|
|
|
Functions used for implementing the set builtin.
|
2005-09-20 13:26:39 +00:00
|
|
|
|
|
|
|
*/
|
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>
|
2012-01-09 19:49:37 +00:00
|
|
|
#include <vector>
|
|
|
|
#include <algorithm>
|
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
|
|
|
|
2012-02-22 18:51:06 +00:00
|
|
|
/* We know about these buffers */
|
|
|
|
extern wcstring stdout_buffer, stderr_buffer;
|
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 )
|
|
|
|
{
|
2012-05-06 21:53:19 +00:00
|
|
|
return contains(env, L"PATH", L"CDPATH" );
|
2006-05-09 16:40:55 +00:00
|
|
|
}
|
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
|
|
|
*/
|
2012-02-08 07:53:34 +00:00
|
|
|
static int my_env_set( const wchar_t *key, wcstring_list_t &val, int scope )
|
2012-01-09 19:49:37 +00:00
|
|
|
{
|
2012-01-14 11:41:50 +00:00
|
|
|
size_t i;
|
2012-01-09 19:49:37 +00:00
|
|
|
int retcode = 0;
|
2012-02-22 20:00:02 +00:00
|
|
|
const wchar_t *val_str=NULL;
|
2012-01-09 19:49:37 +00:00
|
|
|
|
|
|
|
if( is_path_variable( key ) )
|
|
|
|
{
|
|
|
|
int error = 0;
|
|
|
|
|
|
|
|
for( i=0; i< val.size() ; i++ )
|
|
|
|
{
|
|
|
|
int show_perror = 0;
|
|
|
|
int show_hint = 0;
|
|
|
|
|
|
|
|
struct stat buff;
|
2012-03-03 23:28:16 +00:00
|
|
|
const wchar_t *dir = val[i].c_str();
|
2012-01-09 19:49:37 +00:00
|
|
|
|
|
|
|
if( wstat( dir, &buff ) )
|
|
|
|
{
|
|
|
|
error = 1;
|
|
|
|
show_perror = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if( !( S_ISDIR(buff.st_mode) ) )
|
|
|
|
{
|
|
|
|
error = 1;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if( error )
|
|
|
|
{
|
|
|
|
const wchar_t *colon;
|
2012-03-03 23:20:30 +00:00
|
|
|
append_format(stderr_buffer, _(BUILTIN_SET_PATH_ERROR), L"set", dir, key);
|
2012-01-09 19:49:37 +00:00
|
|
|
colon = wcschr( dir, L':' );
|
|
|
|
|
|
|
|
if( colon && *(colon+1) )
|
|
|
|
{
|
|
|
|
show_hint = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if( show_perror )
|
|
|
|
{
|
|
|
|
builtin_wperror( L"set" );
|
|
|
|
}
|
|
|
|
|
|
|
|
if( show_hint )
|
|
|
|
{
|
2012-02-22 18:51:06 +00:00
|
|
|
append_format(stderr_buffer, _(BUILTIN_SET_PATH_HINT), L"set", key, key, wcschr( dir, L':' )+1);
|
2012-01-09 19:49:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if( error )
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if( error )
|
|
|
|
{
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2012-02-22 20:00:02 +00:00
|
|
|
wcstring sb;
|
2012-01-09 19:49:37 +00:00
|
|
|
if( val.size() )
|
|
|
|
{
|
|
|
|
for( i=0; i< val.size() ; i++ )
|
|
|
|
{
|
2012-02-22 20:00:02 +00:00
|
|
|
sb.append(val[i]);
|
2012-01-09 19:49:37 +00:00
|
|
|
if( i<val.size() - 1 )
|
|
|
|
{
|
2012-02-22 20:00:02 +00:00
|
|
|
sb.append( ARRAY_SEP_STR );
|
2012-01-09 19:49:37 +00:00
|
|
|
}
|
|
|
|
}
|
2012-02-22 20:00:02 +00:00
|
|
|
val_str = sb.c_str();
|
2012-01-09 19:49:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
switch( env_set( key, val_str, scope | ENV_USER ) )
|
|
|
|
{
|
|
|
|
case ENV_PERM:
|
|
|
|
{
|
2012-02-22 18:51:06 +00:00
|
|
|
append_format(stderr_buffer, _(L"%ls: Tried to change the read-only variable '%ls'\n"), L"set", key);
|
2012-01-09 19:49:37 +00:00
|
|
|
retcode=1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case ENV_INVALID:
|
|
|
|
{
|
2012-02-22 18:51:06 +00:00
|
|
|
append_format(stderr_buffer, _(L"%ls: Unknown error"), L"set" );
|
2012-01-09 19:49:37 +00:00
|
|
|
retcode=1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return retcode;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2011-12-27 03:18:46 +00:00
|
|
|
/**
|
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
|
2011-12-27 03:18:46 +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
|
|
|
*/
|
2012-01-14 11:41:50 +00:00
|
|
|
static int parse_index( std::vector<long> &indexes,
|
2012-01-09 19:49:37 +00:00
|
|
|
const wchar_t *src,
|
|
|
|
const wchar_t *name,
|
|
|
|
int var_count )
|
|
|
|
{
|
|
|
|
size_t len;
|
|
|
|
|
|
|
|
int count = 0;
|
|
|
|
const wchar_t *src_orig = src;
|
|
|
|
|
|
|
|
if (src == 0)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (*src != L'\0' && (iswalnum(*src) || *src == L'_'))
|
|
|
|
{
|
|
|
|
src++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (*src != L'[')
|
|
|
|
{
|
2012-02-22 18:51:06 +00:00
|
|
|
append_format(stderr_buffer, _(BUILTIN_SET_ARG_COUNT), L"set" );
|
2012-01-09 19:49:37 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
len = src-src_orig;
|
|
|
|
|
|
|
|
if( (wcsncmp( src_orig, name, len )!=0) || (wcslen(name) != (len)) )
|
|
|
|
{
|
2012-02-22 18:51:06 +00:00
|
|
|
append_format(stderr_buffer,
|
2012-01-09 19:49:37 +00:00
|
|
|
_(L"%ls: Multiple variable names specified in single call (%ls and %.*ls)\n"),
|
|
|
|
L"set",
|
|
|
|
name,
|
|
|
|
len,
|
|
|
|
src_orig);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
src++;
|
|
|
|
|
|
|
|
while (iswspace(*src))
|
|
|
|
{
|
|
|
|
src++;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (*src != L']')
|
|
|
|
{
|
|
|
|
wchar_t *end;
|
|
|
|
|
|
|
|
long l_ind;
|
|
|
|
|
|
|
|
errno = 0;
|
|
|
|
|
|
|
|
l_ind = wcstol(src, &end, 10);
|
|
|
|
|
|
|
|
if( end==src || errno )
|
|
|
|
{
|
2012-02-22 18:51:06 +00:00
|
|
|
append_format(stderr_buffer, _(L"%ls: Invalid index starting at '%ls'\n"), L"set", src);
|
2012-01-09 19:49:37 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if( l_ind < 0 )
|
|
|
|
{
|
|
|
|
l_ind = var_count+l_ind+1;
|
|
|
|
}
|
|
|
|
|
|
|
|
indexes.push_back( l_ind );
|
|
|
|
src = end;
|
|
|
|
count++;
|
|
|
|
while (iswspace(*src)) src++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
2012-01-14 11:41:50 +00:00
|
|
|
static int update_values( wcstring_list_t &list,
|
2012-01-09 19:49:37 +00:00
|
|
|
std::vector<long> &indexes,
|
|
|
|
wcstring_list_t &values )
|
|
|
|
{
|
2012-01-14 11:41:50 +00:00
|
|
|
size_t i;
|
2005-09-20 13:26:39 +00:00
|
|
|
|
2012-01-09 19:49:37 +00:00
|
|
|
/* Replace values where needed */
|
|
|
|
for( i = 0; i < indexes.size(); i++ )
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
The '- 1' below is because the indices in fish are
|
2012-02-10 09:37:30 +00:00
|
|
|
one-based, but the vector uses zero-based indices
|
2012-01-09 19:49:37 +00:00
|
|
|
*/
|
|
|
|
long ind = indexes[i] - 1;
|
|
|
|
const wcstring newv = values[ i ];
|
|
|
|
if( ind < 0 )
|
|
|
|
{
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// free((void *) al_get(list, ind));
|
|
|
|
list[ ind ] = newv;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2005-09-20 13:26:39 +00:00
|
|
|
|
2012-01-09 19:49:37 +00:00
|
|
|
/**
|
|
|
|
Erase from a list of wcstring values at specified indexes
|
|
|
|
*/
|
2012-05-10 08:04:18 +00:00
|
|
|
static void erase_values(wcstring_list_t &list, const std::vector<long> &indexes)
|
2012-01-09 19:49:37 +00:00
|
|
|
{
|
2012-05-10 08:04:18 +00:00
|
|
|
// Make a set of indexes.
|
|
|
|
// This both sorts them into ascending order and removes duplicates.
|
|
|
|
const std::set<long> indexes_set(indexes.begin(), indexes.end());
|
|
|
|
|
|
|
|
// Now walk the set backwards, so we encounter larger indexes first, and remove elements at the given (1-based) indexes.
|
|
|
|
std::set<long>::const_reverse_iterator iter;
|
|
|
|
for (iter = indexes_set.rbegin(); iter != indexes_set.rend(); iter++) {
|
|
|
|
long val = *iter;
|
|
|
|
if (val > 0 && val <= list.size()) {
|
|
|
|
// One-based indexing!
|
|
|
|
list.erase(list.begin() + val - 1);
|
|
|
|
}
|
|
|
|
}
|
2012-01-09 19:49:37 +00:00
|
|
|
}
|
|
|
|
|
2005-09-20 13:26:39 +00:00
|
|
|
|
|
|
|
/**
|
2012-03-19 18:52:18 +00:00
|
|
|
Print the names of all environment variables in the scope, with or without shortening,
|
|
|
|
with or without values, with or without escaping
|
2005-09-20 13:26:39 +00:00
|
|
|
*/
|
2012-03-19 18:52:18 +00:00
|
|
|
static void print_variables(int include_values, int esc, bool shorten_ok, int scope)
|
2005-09-20 13:26:39 +00:00
|
|
|
{
|
2012-02-04 02:39:41 +00:00
|
|
|
wcstring_list_t names = env_get_names(scope);
|
|
|
|
sort(names.begin(), names.end());
|
2011-12-27 03:18:46 +00:00
|
|
|
|
2012-02-04 02:39:41 +00:00
|
|
|
for( size_t i = 0; i < names.size(); i++ )
|
2005-09-20 13:26:39 +00:00
|
|
|
{
|
2012-02-04 02:39:41 +00:00
|
|
|
const wcstring key = names.at(i);
|
|
|
|
const wcstring e_key = escape_string(key, 0);
|
2005-10-07 10:36:51 +00:00
|
|
|
|
2012-02-22 18:51:06 +00:00
|
|
|
stdout_buffer.append(e_key);
|
2011-12-27 03:18:46 +00:00
|
|
|
|
|
|
|
if( include_values )
|
2005-09-20 13:26:39 +00:00
|
|
|
{
|
2012-02-23 19:25:46 +00:00
|
|
|
env_var_t value = env_get_string(key);
|
2012-01-14 10:42:17 +00:00
|
|
|
if( !value.missing() )
|
2006-08-31 15:44:00 +00:00
|
|
|
{
|
2006-10-04 18:19:01 +00:00
|
|
|
int shorten = 0;
|
2011-12-27 03:18:46 +00:00
|
|
|
|
2012-03-19 18:52:18 +00:00
|
|
|
if( shorten_ok && value.length() > 64 )
|
2006-10-04 18:19:01 +00:00
|
|
|
{
|
|
|
|
shorten = 1;
|
2012-01-14 10:42:17 +00:00
|
|
|
value.resize(60);
|
2006-10-04 18:19:01 +00:00
|
|
|
}
|
2011-12-27 03:18:46 +00:00
|
|
|
|
2012-02-08 08:15:06 +00:00
|
|
|
wcstring e_value = esc ? expand_escape_variable(value) : value;
|
2011-12-27 03:18:46 +00:00
|
|
|
|
2012-02-22 18:51:06 +00:00
|
|
|
stdout_buffer.append(L" ");
|
|
|
|
stdout_buffer.append(e_value);
|
2011-12-27 03:18:46 +00:00
|
|
|
|
2006-10-04 18:19:01 +00:00
|
|
|
if( shorten )
|
|
|
|
{
|
2012-02-22 18:51:06 +00:00
|
|
|
stdout_buffer.append(L"\u2026");
|
2006-10-04 18:19:01 +00:00
|
|
|
}
|
2006-10-26 10:22:53 +00:00
|
|
|
|
2006-08-31 15:44:00 +00:00
|
|
|
}
|
2005-09-20 13:26:39 +00:00
|
|
|
}
|
2011-12-27 03:18:46 +00:00
|
|
|
|
2012-02-22 18:51:06 +00:00
|
|
|
stdout_buffer.append(L"\n");
|
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
|
|
|
*/
|
2012-01-16 20:10:08 +00:00
|
|
|
static int builtin_set( parser_t &parser, wchar_t **argv )
|
2005-09-20 13:26:39 +00:00
|
|
|
{
|
2011-12-27 03:18:46 +00:00
|
|
|
|
2006-04-19 12:43:15 +00:00
|
|
|
/**
|
|
|
|
Variables used for parsing the argument list
|
|
|
|
*/
|
2010-10-08 00:43:57 +00:00
|
|
|
static const struct woption
|
2011-12-27 03:18:46 +00:00
|
|
|
long_options[] =
|
2005-09-20 13:26:39 +00:00
|
|
|
{
|
2011-12-27 03:18:46 +00:00
|
|
|
{
|
|
|
|
L"export", no_argument, 0, 'x'
|
2006-05-26 11:24:02 +00:00
|
|
|
}
|
|
|
|
,
|
2011-12-27 03:18:46 +00:00
|
|
|
{
|
|
|
|
L"global", no_argument, 0, 'g'
|
2006-05-26 11:24:02 +00:00
|
|
|
}
|
|
|
|
,
|
2011-12-27 03:18:46 +00:00
|
|
|
{
|
|
|
|
L"local", no_argument, 0, 'l'
|
2006-05-26 11:24:02 +00:00
|
|
|
}
|
|
|
|
,
|
2011-12-27 03:18:46 +00:00
|
|
|
{
|
|
|
|
L"erase", no_argument, 0, 'e'
|
2006-05-26 11:24:02 +00:00
|
|
|
}
|
|
|
|
,
|
2011-12-27 03:18:46 +00:00
|
|
|
{
|
|
|
|
L"names", no_argument, 0, 'n'
|
|
|
|
}
|
2006-05-26 11:24:02 +00:00
|
|
|
,
|
2011-12-27 03:18:46 +00:00
|
|
|
{
|
|
|
|
L"unexport", no_argument, 0, 'u'
|
|
|
|
}
|
2006-05-26 11:24:02 +00:00
|
|
|
,
|
2011-12-27 03:18:46 +00:00
|
|
|
{
|
2012-03-19 18:52:18 +00:00
|
|
|
L"universal", no_argument, 0, 'U'
|
|
|
|
}
|
|
|
|
,
|
|
|
|
{
|
|
|
|
L"long", no_argument, 0, 'L'
|
2011-12-27 03:18:46 +00:00
|
|
|
}
|
2006-05-26 11:24:02 +00:00
|
|
|
,
|
2011-12-27 03:18:46 +00:00
|
|
|
{
|
|
|
|
L"query", no_argument, 0, 'q'
|
|
|
|
}
|
2006-05-26 11:24:02 +00:00
|
|
|
,
|
2011-12-27 03:18:46 +00:00
|
|
|
{
|
|
|
|
L"help", no_argument, 0, 'h'
|
|
|
|
}
|
2006-05-26 11:24:02 +00:00
|
|
|
,
|
2011-12-27 03:18:46 +00:00
|
|
|
{
|
|
|
|
0, 0, 0, 0
|
2005-09-20 13:26:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
;
|
2011-12-27 03:18:46 +00:00
|
|
|
|
2012-03-19 18:52:18 +00:00
|
|
|
const wchar_t *short_options = L"+xglenuULqh";
|
2005-09-20 13:26:39 +00:00
|
|
|
|
|
|
|
int argc = builtin_count_args(argv);
|
|
|
|
|
|
|
|
/*
|
|
|
|
Flags to set the work mode
|
|
|
|
*/
|
2011-12-27 03:18:46 +00:00
|
|
|
int local = 0, global = 0, exportv = 0;
|
2005-09-20 13:26:39 +00:00
|
|
|
int erase = 0, list = 0, unexport=0;
|
2005-09-22 20:16:52 +00:00
|
|
|
int universal = 0, query=0;
|
2012-03-19 18:52:18 +00:00
|
|
|
bool shorten_ok = true;
|
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;
|
2011-12-27 03:18:46 +00:00
|
|
|
|
2006-08-26 01:37:22 +00:00
|
|
|
wchar_t *bad_char;
|
2011-12-27 03:18:46 +00:00
|
|
|
|
|
|
|
|
2005-09-20 13:26:39 +00:00
|
|
|
/* Parse options to obtain the requested operation and the modifiers */
|
|
|
|
woptind = 0;
|
2011-12-27 03:18:46 +00:00
|
|
|
while (1)
|
2005-09-20 13:26:39 +00:00
|
|
|
{
|
|
|
|
int c = wgetopt_long(argc, argv, short_options, long_options, 0);
|
|
|
|
|
2011-12-27 03:18:46 +00:00
|
|
|
if (c == -1)
|
2005-09-20 13:26:39 +00:00
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
2011-12-27 03:18:46 +00:00
|
|
|
|
|
|
|
switch(c)
|
2005-09-20 13:26:39 +00:00
|
|
|
{
|
|
|
|
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':
|
2011-12-27 03:18:46 +00:00
|
|
|
exportv = 1;
|
2005-09-20 13:26:39 +00:00
|
|
|
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;
|
2012-03-19 18:52:18 +00:00
|
|
|
|
|
|
|
case 'L':
|
|
|
|
shorten_ok = false;
|
|
|
|
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':
|
2012-02-22 18:51:06 +00:00
|
|
|
builtin_print_help( parser, argv[0], stdout_buffer );
|
2006-05-26 11:24:02 +00:00
|
|
|
return 0;
|
|
|
|
|
2005-09-20 13:26:39 +00:00
|
|
|
case '?':
|
2012-01-16 20:10:08 +00:00
|
|
|
builtin_unknown_option( parser, 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
|
|
|
|
*/
|
|
|
|
|
2012-06-18 17:20:40 +00:00
|
|
|
if( query && (erase || list) )
|
2005-09-22 20:16:52 +00:00
|
|
|
{
|
2012-02-22 18:51:06 +00:00
|
|
|
append_format(stderr_buffer,
|
2007-01-31 16:03:17 +00:00
|
|
|
BUILTIN_ERR_COMBO,
|
|
|
|
argv[0] );
|
2011-12-27 03:18:46 +00:00
|
|
|
|
2012-02-22 18:51:06 +00:00
|
|
|
builtin_print_help( parser, argv[0], stderr_buffer );
|
2005-09-22 20:16:52 +00:00
|
|
|
return 1;
|
|
|
|
}
|
2011-12-27 03:18:46 +00:00
|
|
|
|
2005-09-22 20:16:52 +00:00
|
|
|
|
2006-04-19 12:43:15 +00:00
|
|
|
/* We can't both list and erase varaibles */
|
2011-12-27 03:18:46 +00:00
|
|
|
if( erase && list )
|
2005-09-20 13:26:39 +00:00
|
|
|
{
|
2012-02-22 18:51:06 +00:00
|
|
|
append_format(stderr_buffer,
|
2007-01-31 16:03:17 +00:00
|
|
|
BUILTIN_ERR_COMBO,
|
2011-12-27 03:18:46 +00:00
|
|
|
argv[0] );
|
2006-01-04 12:51:02 +00:00
|
|
|
|
2012-02-22 18:51:06 +00:00
|
|
|
builtin_print_help( parser, argv[0], stderr_buffer );
|
2005-09-20 13:26:39 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2006-04-19 12:43:15 +00:00
|
|
|
/*
|
|
|
|
Variables can only have one scope
|
|
|
|
*/
|
2011-12-27 03:18:46 +00:00
|
|
|
if( local + global + universal > 1 )
|
2005-09-20 13:26:39 +00:00
|
|
|
{
|
2012-02-22 18:51:06 +00:00
|
|
|
append_format(stderr_buffer,
|
2005-09-20 13:26:39 +00:00
|
|
|
BUILTIN_ERR_GLOCAL,
|
2007-01-31 16:03:17 +00:00
|
|
|
argv[0] );
|
2012-02-22 18:51:06 +00:00
|
|
|
builtin_print_help( parser, argv[0], stderr_buffer );
|
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
|
|
|
|
*/
|
2011-12-27 03:18:46 +00:00
|
|
|
if( exportv && unexport )
|
2005-09-20 13:26:39 +00:00
|
|
|
{
|
2012-02-22 18:51:06 +00:00
|
|
|
append_format(stderr_buffer,
|
2005-09-20 13:26:39 +00:00
|
|
|
BUILTIN_ERR_EXPUNEXP,
|
2007-01-31 16:03:17 +00:00
|
|
|
argv[0] );
|
2012-02-22 18:51:06 +00:00
|
|
|
builtin_print_help( parser, argv[0], stderr_buffer );
|
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
|
|
|
|
*/
|
2011-12-27 03:18:46 +00:00
|
|
|
scope = (local ? ENV_LOCAL : 0) | (global ? ENV_GLOBAL : 0) | (exportv ? ENV_EXPORT : 0) | (unexport ? ENV_UNEXPORT : 0) | (universal ? ENV_UNIVERSAL:0) | ENV_USER;
|
2006-05-09 16:40:55 +00:00
|
|
|
|
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)))
|
|
|
|
{
|
2011-12-27 03:18:46 +00:00
|
|
|
DIE_MEM();
|
2007-01-30 20:43:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if( wcschr( dest, L'[' ) )
|
|
|
|
{
|
|
|
|
slice = 1;
|
|
|
|
*wcschr( dest, L'[' )=0;
|
|
|
|
}
|
2011-12-27 03:18:46 +00:00
|
|
|
|
2007-01-30 20:43:44 +00:00
|
|
|
if( slice )
|
|
|
|
{
|
2012-01-09 19:49:37 +00:00
|
|
|
std::vector<long> indexes;
|
|
|
|
wcstring_list_t result;
|
2012-01-14 11:41:50 +00:00
|
|
|
size_t j;
|
2011-12-27 03:18:46 +00:00
|
|
|
|
2012-01-14 10:42:17 +00:00
|
|
|
env_var_t dest_str = env_get_string(dest);
|
|
|
|
if (! dest_str.missing())
|
2012-02-10 09:37:30 +00:00
|
|
|
tokenize_variable_array( dest_str, result );
|
2011-12-27 03:18:46 +00:00
|
|
|
|
2012-01-14 11:41:50 +00:00
|
|
|
if( !parse_index( indexes, arg, dest, result.size() ) )
|
2007-01-30 20:43:44 +00:00
|
|
|
{
|
2012-02-22 18:51:06 +00:00
|
|
|
builtin_print_help( parser, argv[0], stderr_buffer );
|
2007-01-30 20:43:44 +00:00
|
|
|
retcode = 1;
|
|
|
|
break;
|
|
|
|
}
|
2012-01-09 19:49:37 +00:00
|
|
|
for( j=0; j < indexes.size() ; j++ )
|
2007-01-30 20:43:44 +00:00
|
|
|
{
|
2012-01-09 19:49:37 +00:00
|
|
|
long idx = indexes[j];
|
2012-01-14 11:41:50 +00:00
|
|
|
if( idx < 1 || (size_t)idx > result.size() )
|
2007-01-30 20:43:44 +00:00
|
|
|
{
|
|
|
|
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
|
|
|
}
|
2011-12-27 03:18:46 +00:00
|
|
|
|
2007-01-30 20:43:44 +00:00
|
|
|
free( dest );
|
2011-12-27 03:18:46 +00:00
|
|
|
|
2005-09-22 20:16:52 +00:00
|
|
|
}
|
|
|
|
return retcode;
|
|
|
|
}
|
2011-12-27 03:18:46 +00:00
|
|
|
|
|
|
|
if( list )
|
2006-08-31 15:44:00 +00:00
|
|
|
{
|
|
|
|
/* Maybe we should issue an error if there are any other arguments? */
|
2012-03-19 18:52:18 +00:00
|
|
|
print_variables(0, 0, shorten_ok, scope);
|
2006-08-31 15:44:00 +00:00
|
|
|
return 0;
|
2011-12-27 03:18:46 +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
|
|
|
/*
|
|
|
|
Print values of variables
|
|
|
|
*/
|
|
|
|
|
2011-12-27 03:18:46 +00:00
|
|
|
if( erase )
|
2005-12-07 14:48:49 +00:00
|
|
|
{
|
2012-02-22 18:51:06 +00:00
|
|
|
append_format(stderr_buffer,
|
2012-06-17 21:49:45 +00:00
|
|
|
_(L"%ls: Erase needs a variable name\n"),
|
2007-01-31 16:03:17 +00:00
|
|
|
argv[0] );
|
2011-12-27 03:18:46 +00:00
|
|
|
|
2012-02-22 18:51:06 +00:00
|
|
|
builtin_print_help( parser, argv[0], stderr_buffer );
|
2006-05-09 16:40:55 +00:00
|
|
|
retcode = 1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-03-19 18:52:18 +00:00
|
|
|
print_variables( 1, 1, shorten_ok, scope );
|
2006-05-09 16:40:55 +00:00
|
|
|
}
|
2011-12-27 03:18:46 +00:00
|
|
|
|
2006-05-09 16:40:55 +00:00
|
|
|
return retcode;
|
2005-09-20 13:26:39 +00:00
|
|
|
}
|
|
|
|
|
2006-05-09 16:40:55 +00:00
|
|
|
if( !(dest = wcsdup(argv[woptind])))
|
|
|
|
{
|
2011-12-27 03:18:46 +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;
|
|
|
|
}
|
2011-12-27 03:18:46 +00:00
|
|
|
|
2006-05-09 16:40:55 +00:00
|
|
|
if( !wcslen( dest ) )
|
|
|
|
{
|
|
|
|
free( dest );
|
2012-02-22 18:51:06 +00:00
|
|
|
append_format(stderr_buffer, BUILTIN_ERR_VARNAME_ZERO, argv[0] );
|
|
|
|
builtin_print_help( parser, argv[0], stderr_buffer );
|
2006-05-09 16:40:55 +00:00
|
|
|
return 1;
|
2005-09-20 13:26:39 +00:00
|
|
|
}
|
2011-12-27 03:18:46 +00:00
|
|
|
|
2006-08-26 01:37:22 +00:00
|
|
|
if( (bad_char = wcsvarname( dest ) ) )
|
|
|
|
{
|
2012-02-22 18:51:06 +00:00
|
|
|
append_format(stderr_buffer, BUILTIN_ERR_VARCHAR, argv[0], *bad_char );
|
|
|
|
builtin_print_help( parser, argv[0], stderr_buffer );
|
2006-08-26 01:37:22 +00:00
|
|
|
free( dest );
|
|
|
|
return 1;
|
|
|
|
}
|
2011-12-27 03:18:46 +00:00
|
|
|
|
2006-06-04 20:14:51 +00:00
|
|
|
if( slice && erase && (scope != ENV_USER) )
|
|
|
|
{
|
|
|
|
free( dest );
|
2012-02-22 18:51:06 +00:00
|
|
|
append_format(stderr_buffer, _(L"%ls: Can not specify scope when erasing array slice\n"), argv[0] );
|
|
|
|
builtin_print_help( parser, argv[0], stderr_buffer );
|
2006-06-04 20:14:51 +00:00
|
|
|
return 1;
|
|
|
|
}
|
2011-12-27 03:18:46 +00:00
|
|
|
|
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.
|
|
|
|
*/
|
2011-12-27 03:18:46 +00:00
|
|
|
|
2006-05-09 16:40:55 +00:00
|
|
|
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;
|
2012-01-09 19:49:37 +00:00
|
|
|
wcstring_list_t values;
|
|
|
|
std::vector<long> indexes;
|
|
|
|
wcstring_list_t result;
|
2011-12-27 03:18:46 +00:00
|
|
|
|
2012-01-14 10:42:17 +00:00
|
|
|
const env_var_t dest_str = env_get_string(dest);
|
|
|
|
if (! dest_str.missing())
|
2012-02-10 09:37:30 +00:00
|
|
|
tokenize_variable_array( dest_str, result );
|
2011-12-27 03:18:46 +00:00
|
|
|
|
2006-05-09 16:40:55 +00:00
|
|
|
for( ; woptind<argc; woptind++ )
|
2011-12-27 03:18:46 +00:00
|
|
|
{
|
2012-01-14 11:41:50 +00:00
|
|
|
if( !parse_index( indexes, argv[woptind], dest, result.size() ) )
|
2006-04-20 18:29:44 +00:00
|
|
|
{
|
2012-02-22 18:51:06 +00:00
|
|
|
builtin_print_help( parser, argv[0], stderr_buffer );
|
2006-05-09 16:40:55 +00:00
|
|
|
retcode = 1;
|
|
|
|
break;
|
2006-04-20 18:29:44 +00:00
|
|
|
}
|
2011-12-27 03:18:46 +00:00
|
|
|
|
2006-05-09 16:40:55 +00:00
|
|
|
val_count = argc-woptind-1;
|
2012-01-09 19:49:37 +00:00
|
|
|
idx_count = indexes.size();
|
2006-05-09 16:40:55 +00:00
|
|
|
|
|
|
|
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
|
|
|
{
|
2012-02-22 18:51:06 +00:00
|
|
|
append_format(stderr_buffer, _(BUILTIN_SET_ARG_COUNT), argv[0] );
|
|
|
|
builtin_print_help( parser, argv[0], stderr_buffer );
|
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
|
|
|
}
|
|
|
|
}
|
2011-12-27 03:18:46 +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 )
|
|
|
|
{
|
2012-01-14 11:41:50 +00:00
|
|
|
erase_values(result, indexes);
|
2012-02-08 07:53:34 +00:00
|
|
|
my_env_set( dest, result, scope);
|
2006-05-09 16:40:55 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-01-09 19:49:37 +00:00
|
|
|
wcstring_list_t value;
|
|
|
|
// al_init(&value);
|
2005-09-20 13:26:39 +00:00
|
|
|
|
2011-12-27 03:18:46 +00:00
|
|
|
while( woptind < argc )
|
2006-05-09 16:40:55 +00:00
|
|
|
{
|
2012-01-09 19:49:37 +00:00
|
|
|
value.push_back( argv[woptind++] );
|
2006-05-09 16:40:55 +00:00
|
|
|
}
|
2005-09-20 13:26:39 +00:00
|
|
|
|
2012-01-14 11:41:50 +00:00
|
|
|
if( update_values( result,
|
2012-01-09 19:49:37 +00:00
|
|
|
indexes,
|
|
|
|
value ) )
|
2006-06-04 20:14:51 +00:00
|
|
|
{
|
2012-02-22 18:51:06 +00:00
|
|
|
append_format(stderr_buffer, L"%ls: ", argv[0] );
|
|
|
|
append_format(stderr_buffer, ARRAY_BOUNDS_ERR );
|
|
|
|
stderr_buffer.push_back(L'\n');
|
2006-06-04 20:14:51 +00:00
|
|
|
}
|
2011-12-27 03:18:46 +00:00
|
|
|
|
2012-02-08 07:53:34 +00:00
|
|
|
my_env_set(dest, result, scope);
|
2011-12-27 03:18:46 +00:00
|
|
|
|
2012-01-09 19:49:37 +00:00
|
|
|
// al_destroy( &value );
|
2011-12-27 03:18:46 +00:00
|
|
|
|
|
|
|
}
|
2006-05-09 16:40:55 +00:00
|
|
|
}
|
2005-09-20 13:26:39 +00:00
|
|
|
|
2012-01-09 19:49:37 +00:00
|
|
|
// al_foreach( &result, &free );
|
|
|
|
// al_destroy( &result );
|
2006-06-04 09:35:32 +00:00
|
|
|
|
2012-01-09 19:49:37 +00:00
|
|
|
// al_destroy(&indexes);
|
|
|
|
// al_destroy(&values);
|
2011-12-27 03:18:46 +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++;
|
2011-12-27 03:18:46 +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
|
|
|
{
|
2012-02-22 18:51:06 +00:00
|
|
|
append_format(stderr_buffer,
|
2007-01-31 16:03:17 +00:00
|
|
|
_(L"%ls: Values cannot be specfied with erase\n"),
|
|
|
|
argv[0] );
|
2012-02-22 18:51:06 +00:00
|
|
|
builtin_print_help( parser, argv[0], stderr_buffer );
|
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
|
|
|
{
|
2012-02-08 07:53:34 +00:00
|
|
|
wcstring_list_t val;
|
2006-05-09 16:40:55 +00:00
|
|
|
for( i=woptind; i<argc; i++ )
|
2012-02-08 07:53:34 +00:00
|
|
|
val.push_back(argv[i]);
|
|
|
|
retcode = my_env_set( dest, val, scope );
|
2011-12-27 03:18:46 +00:00
|
|
|
}
|
2005-09-20 13:26:39 +00:00
|
|
|
}
|
2011-12-27 03:18:46 +00:00
|
|
|
|
2006-05-09 16:40:55 +00:00
|
|
|
free( dest );
|
2011-12-27 03:18:46 +00:00
|
|
|
|
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
|
|
|
}
|
|
|
|
|