/** \file builtin_set.c Functions defining the set builtin Functions used for implementing the set builtin. */ #include "config.h" #include #include #include #include #include #include #include #include #include #include "fallback.h" #include "util.h" #include "wutil.h" #include "builtin.h" #include "env.h" #include "expand.h" #include "common.h" #include "wgetopt.h" #include "proc.h" #include "parser.h" /** Error message for invalid path operations */ #define BUILTIN_SET_PATH_ERROR L"%ls: Could not add component %ls to %ls.\n" /** Hint for invalid path operation with a colon */ #define BUILTIN_SET_PATH_HINT L"%ls: Did you mean 'set %ls $%ls %ls'?\n" /** Error for mismatch between index count and elements */ #define BUILTIN_SET_ARG_COUNT L"%ls: The number of variable indexes does not match the number of values\n" /** Test if the specified variable should be subject to path validation */ static int is_path_variable( const wchar_t *env ) { return contains( env, L"PATH", L"CDPATH" ); } /** 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. */ static int my_env_set( const wchar_t *key, array_list_t *val, int scope ) { string_buffer_t sb; int i; int retcode = 0; wchar_t *val_str=0; if( is_path_variable( key ) ) { int error = 0; for( i=0; i &indexes, 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'[') { sb_printf( sb_err, _(BUILTIN_SET_ARG_COUNT), L"set" ); return 0; } len = src-src_orig; if( (wcsncmp( src_orig, name, len )!=0) || (wcslen(name) != (len)) ) { 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; } 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 ) { sb_printf(sb_err, _(L"%ls: Invalid index starting at '%ls'\n"), L"set", src); 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; } static int update_values( wcstring_list_t &list, std::vector &indexes, wcstring_list_t &values ) { size_t i; /* Replace values where needed */ for( i = 0; i < indexes.size(); i++ ) { /* The '- 1' below is because the indices in fish are one-based, but the array_list_t uses zero-based indices */ 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; } /** Erase from a list of wcstring values at specified indexes */ static void erase_values(wcstring_list_t &list, std::vector &indexes) { size_t i; wcstring_list_t result; for (i = 0; i < list.size(); i++) { if (std::find(indexes.begin(), indexes.end(), i + 1) != indexes.end()) { result.push_back( list[ i ] ); } } // al_truncate(list,0); list.clear(); copy(result.begin(),result.end(),back_inserter( list ) ); // al_destroy(&result); } /** Print the names of all environment variables in the scope, with or without values, with or without escaping */ static void print_variables(int include_values, int esc, int scope) { wcstring_list_t names = env_get_names(scope); sort(names.begin(), names.end()); for( size_t i = 0; i < names.size(); i++ ) { const wcstring key = names.at(i); const wcstring e_key = escape_string(key, 0); sb_append(sb_out, e_key.c_str()); if( include_values ) { env_var_t value = env_get_string(key.c_str()); if( !value.missing() ) { int shorten = 0; if( value.length() > 64 ) { shorten = 1; value.resize(60); } wcstring e_value = esc ? expand_escape_variable2(value) : value; sb_append(sb_out, L" ", e_value.c_str(), NULL); if( shorten ) { sb_append(sb_out, L"\u2026"); } } } sb_append(sb_out, L"\n"); } } /** The set builtin. Creates, updates and erases environment variables and environemnt variable arrays. */ static int builtin_set( parser_t &parser, wchar_t **argv ) { /** Variables used for parsing the argument list */ static const struct woption long_options[] = { { L"export", no_argument, 0, 'x' } , { L"global", no_argument, 0, 'g' } , { L"local", no_argument, 0, 'l' } , { L"erase", no_argument, 0, 'e' } , { L"names", no_argument, 0, 'n' } , { L"unexport", no_argument, 0, 'u' } , { L"universal", no_argument, 0, 'U' } , { L"query", no_argument, 0, 'q' } , { L"help", no_argument, 0, 'h' } , { 0, 0, 0, 0 } } ; const wchar_t *short_options = L"+xglenuUqh"; int argc = builtin_count_args(argv); /* Flags to set the work mode */ int local = 0, global = 0, exportv = 0; int erase = 0, list = 0, unexport=0; int universal = 0, query=0; /* Variables used for performing the actual work */ wchar_t *dest = 0; int retcode=0; int scope; int slice=0; int i; wchar_t *bad_char; /* 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; case 'e': erase = 1; break; case 'n': list = 1; break; case 'x': exportv = 1; break; case 'l': local = 1; break; case 'g': global = 1; break; case 'u': unexport = 1; break; case 'U': universal = 1; break; case 'q': query = 1; break; case 'h': builtin_print_help( parser, argv[0], sb_out ); return 0; case '?': builtin_unknown_option( parser, argv[0], argv[woptind-1] ); return 1; default: break; } } /* 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 */ if( query && (erase || list || global || local || universal || exportv || unexport ) ) { sb_printf(sb_err, BUILTIN_ERR_COMBO, argv[0] ); builtin_print_help( parser, argv[0], sb_err ); return 1; } /* We can't both list and erase varaibles */ if( erase && list ) { sb_printf(sb_err, BUILTIN_ERR_COMBO, argv[0] ); builtin_print_help( parser, argv[0], sb_err ); return 1; } /* Variables can only have one scope */ if( local + global + universal > 1 ) { sb_printf( sb_err, BUILTIN_ERR_GLOCAL, argv[0] ); builtin_print_help( parser, argv[0], sb_err ); return 1; } /* Variables can only have one export status */ if( exportv && unexport ) { sb_printf( sb_err, BUILTIN_ERR_EXPUNEXP, argv[0] ); builtin_print_help( parser, argv[0], sb_err ); return 1; } /* Calculate the scope value for variable assignement */ scope = (local ? ENV_LOCAL : 0) | (global ? ENV_GLOBAL : 0) | (exportv ? ENV_EXPORT : 0) | (unexport ? ENV_UNEXPORT : 0) | (universal ? ENV_UNIVERSAL:0) | ENV_USER; if( query ) { /* Query mode. Return the number of variables that do not exist out of the specified variables. */ int i; for( i=woptind; i indexes; wcstring_list_t result; size_t j; // al_init( &result ); // al_init( &indexes ); env_var_t dest_str = env_get_string(dest); if (! dest_str.missing()) tokenize_variable_array2( dest_str, result ); if( !parse_index( indexes, arg, dest, result.size() ) ) { builtin_print_help( parser, argv[0], sb_err ); retcode = 1; break; } for( j=0; j < indexes.size() ; j++ ) { long idx = indexes[j]; if( idx < 1 || (size_t)idx > result.size() ) { retcode++; } } } else { if( !env_exist( arg, scope ) ) { retcode++; } } free( dest ); } return retcode; } if( list ) { /* Maybe we should issue an error if there are any other arguments? */ print_variables(0, 0, scope); return 0; } if( woptind == argc ) { /* Print values of variables */ if( erase ) { sb_printf( sb_err, _(L"%ls: Erase needs a variable name\n%ls\n"), argv[0] ); builtin_print_help( parser, argv[0], sb_err ); retcode = 1; } else { print_variables( 1, 1, scope ); } return retcode; } if( !(dest = wcsdup(argv[woptind]))) { DIE_MEM(); } if( wcschr( dest, L'[' ) ) { slice = 1; *wcschr( dest, L'[' )=0; } if( !wcslen( dest ) ) { free( dest ); sb_printf( sb_err, BUILTIN_ERR_VARNAME_ZERO, argv[0] ); builtin_print_help( parser, argv[0], sb_err ); return 1; } if( (bad_char = wcsvarname( dest ) ) ) { sb_printf( sb_err, BUILTIN_ERR_VARCHAR, argv[0], *bad_char ); builtin_print_help( parser, argv[0], sb_err ); free( dest ); return 1; } 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( parser, argv[0], sb_err ); return 1; } /* set assignment can work in two modes, either using slices or using the whole array. We detect which mode is used here. */ if( slice ) { /* Slice mode */ int idx_count, val_count; wcstring_list_t values; std::vector indexes; wcstring_list_t result; const env_var_t dest_str = env_get_string(dest); if (! dest_str.missing()) tokenize_variable_array2( dest_str, result ); for( ; woptind