From 7530057c6ee71a6a6611db1120b50d44130c12a2 Mon Sep 17 00:00:00 2001 From: Siteshwar Vashisht Date: Tue, 10 Jan 2012 00:00:54 +0530 Subject: [PATCH 01/16] Modified builting.cpp to use env_get_string() --- builtin.cpp | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/builtin.cpp b/builtin.cpp index ca0d5a73a..bbd9464cb 100644 --- a/builtin.cpp +++ b/builtin.cpp @@ -2007,7 +2007,7 @@ static int builtin_read( wchar_t **argv ) { wchar_t *buff=0; int i, argc = builtin_count_args( argv ); - const wchar_t *ifs; + wcstring ifs; int place = ENV_USER; wchar_t *nxt; const wchar_t *prompt = DEFAULT_READ_PROMPT; @@ -2292,11 +2292,11 @@ static int builtin_read( wchar_t **argv ) wchar_t *state; - ifs = env_get( L"IFS" ); - if( ifs == 0 ) + ifs = env_get_string( L"IFS" ); + if( ifs.empty() ) ifs = L""; - nxt = wcstok( buff, (i Date: Tue, 10 Jan 2012 00:05:51 +0530 Subject: [PATCH 02/16] Further changes in builtin.cpp to use env_get_string() --- builtin.cpp | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/builtin.cpp b/builtin.cpp index bbd9464cb..339c1af81 100644 --- a/builtin.cpp +++ b/builtin.cpp @@ -2613,7 +2613,7 @@ static int builtin_exit( wchar_t **argv ) */ static int builtin_cd( wchar_t **argv ) { - wchar_t *dir_in; + wcstring dir_in; wchar_t *dir; int res=STATUS_BUILTIN_OK; void *context = halloc( 0, 0 ); @@ -2621,8 +2621,8 @@ static int builtin_cd( wchar_t **argv ) if( argv[1] == 0 ) { - dir_in = env_get( L"HOME" ); - if( !dir_in ) + dir_in = env_get_string( L"HOME" ); + if( dir_in.empty() ) { sb_printf( sb_err, _( L"%ls: Could not find home directory\n" ), @@ -2632,7 +2632,7 @@ static int builtin_cd( wchar_t **argv ) else dir_in = argv[1]; - dir = path_get_cdpath( context, dir_in ); + dir = path_get_cdpath( context, dir_in.empty()?0:dir_in.c_str() ); if( !dir ) { @@ -2641,21 +2641,21 @@ static int builtin_cd( wchar_t **argv ) sb_printf( sb_err, _( L"%ls: '%ls' is not a directory\n" ), argv[0], - dir_in ); + dir_in.c_str() ); } else if( errno == ENOENT ) { sb_printf( sb_err, _( L"%ls: The directory '%ls' does not exist\n" ), argv[0], - dir_in ); + dir_in.c_str() ); } else if( errno == EROTTEN ) { sb_printf( sb_err, _( L"%ls: '%ls' is a rotten symlink\n" ), argv[0], - dir_in ); + dir_in.c_str() ); } else @@ -2663,7 +2663,7 @@ static int builtin_cd( wchar_t **argv ) sb_printf( sb_err, _( L"%ls: Unknown error trying to locate directory '%ls'\n" ), argv[0], - dir_in ); + dir_in.c_str() ); } From 7b3377e78c16b3ba74edb141ca0dd2ec895806b9 Mon Sep 17 00:00:00 2001 From: Siteshwar Vashisht Date: Tue, 10 Jan 2012 01:19:37 +0530 Subject: [PATCH 03/16] Modified builtin_set.cpp to use env_get(), added functions like update_values2(), env_set2() etc. to support wcstring and vector instead of using array_list_t --- builtin_set.cpp | 337 ++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 294 insertions(+), 43 deletions(-) diff --git a/builtin_set.cpp b/builtin_set.cpp index 840fd4b13..80102ab0d 100644 --- a/builtin_set.cpp +++ b/builtin_set.cpp @@ -12,7 +12,8 @@ Functions used for implementing the set builtin. #include #include #include - +#include +#include #include "fallback.h" #include "util.h" @@ -72,7 +73,7 @@ static int my_env_set( const wchar_t *key, array_list_t *val, int scope ) int show_hint = 0; struct stat buff; - wchar_t *dir = (wchar_t *)al_get( val, i ); + const wchar_t *dir = (wchar_t *)al_get( val, i ); if( wstat( dir, &buff ) ) { @@ -88,7 +89,7 @@ static int my_env_set( const wchar_t *key, array_list_t *val, int scope ) if( error ) { - wchar_t *colon; + const wchar_t *colon; sb_printf( sb_err, _(BUILTIN_SET_PATH_ERROR), @@ -173,6 +174,126 @@ static int my_env_set( const wchar_t *key, array_list_t *val, int scope ) return retcode; } +static int my_env_set2( const wchar_t *key, wcstring_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< val.size() ; i++ ) + { + int show_perror = 0; + int show_hint = 0; + + struct stat buff; + const wchar_t *dir = val[ i ].c_str(); + + if( wstat( dir, &buff ) ) + { + error = 1; + show_perror = 1; + } + + if( !( S_ISDIR(buff.st_mode) ) ) + { + error = 1; + + } + + if( error ) + { + const wchar_t *colon; + + sb_printf( sb_err, + _(BUILTIN_SET_PATH_ERROR), + L"set", + dir, + key ); + + colon = wcschr( dir, L':' ); + + if( colon && *(colon+1) ) + { + show_hint = 1; + } + + } + + 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; + } + + } + + sb_init( &sb ); + + if( val.size() ) + { + for( i=0; i< val.size() ; i++ ) + { + const wchar_t *next = val[ i ].c_str(); + sb_append( &sb, next?next:L"" ); + if( 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; +} + /** Update a list \c list by writing copies (using wcsdup) of the values specified by \c values to the indexes specified by \c @@ -295,7 +492,32 @@ static int update_values( array_list_t *list, return 0; } +static int update_values2( wcstring_list_t &list, + std::vector &indexes, + wcstring_list_t &values ) +{ + int 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; +} /** Return 1 if an array list of longs contains the specified value, 0 otherwise @@ -345,6 +567,35 @@ static void erase_values(array_list_t *list, array_list_t *indexes) al_destroy(&result); } +/** + Erase from a list of wcstring values at specified indexes +*/ +static void erase_values2 (wcstring_list_t &list, std::vector &indexes) +{ + long i; + wcstring_list_t result; + +// al_init(&result); + + for (i = 0; i < list.size(); i++) + { + if (std::find(indexes.begin(), indexes.end(), i + 1) != indexes.end()) + { + result.push_back( list[ i ] ); + } + else + { +// free( (void *)al_get(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, @@ -369,23 +620,23 @@ static void print_variables(int include_values, int esc, int scope) if( include_values ) { - wchar_t *value = env_get(key); + wcstring value = env_get_string(key); wchar_t *e_value; - if( value ) + if( !value.empty() ) { int shorten = 0; - if( wcslen( value ) > 64 ) + if( value.length() > 64 ) { shorten = 1; - value = wcsndup( value, 60 ); - if( !value ) + value = wcsndup( value.c_str(), 60 ); + if( value.empty() ) { DIE_MEM(); } } - e_value = esc ? expand_escape_variable(value) : wcsdup(value); + e_value = esc ? expand_escape_variable(value.c_str()) : wcsdup(value.c_str()); sb_append(sb_out, L" ", e_value, NULL); free(e_value); @@ -393,7 +644,7 @@ static void print_variables(int include_values, int esc, int scope) if( shorten ) { sb_append(sb_out, L"\u2026"); - free( value ); +// free( value ); } } @@ -632,25 +883,25 @@ static int builtin_set( wchar_t **argv ) if( slice ) { - array_list_t indexes; - array_list_t result; + std::vector indexes; + wcstring_list_t result; int j; - al_init( &result ); - al_init( &indexes ); +// al_init( &result ); +// al_init( &indexes ); - tokenize_variable_array( env_get( dest ), &result ); + tokenize_variable_array2( env_get_string( dest ), result ); - if( !parse_index( &indexes, arg, dest, al_get_count( &result ) ) ) + if( !parse_index2( indexes, arg, dest, result.size() ) ) { builtin_print_help( argv[0], sb_err ); retcode = 1; break; } - for( j=0; j al_get_count( &result ) ) + long idx = indexes[j]; + if( idx < 1 || idx > result.size() ) { retcode++; } @@ -747,19 +998,19 @@ static int builtin_set( wchar_t **argv ) Slice mode */ int idx_count, val_count; - array_list_t values; - array_list_t indexes; - array_list_t result; + wcstring_list_t values; + std::vector indexes; + wcstring_list_t result; - al_init(&values); - al_init(&indexes); - al_init(&result); +// al_init(&values); +// al_init(&indexes); +// al_init(&result); - tokenize_variable_array( env_get(dest), &result ); + tokenize_variable_array2( env_get_string(dest), result ); for( ; woptind Date: Tue, 10 Jan 2012 01:40:03 +0530 Subject: [PATCH 04/16] Modified complete.cpp to use env_get_string(); Fixed env_get_string() return an empty wcstring instead of returning 0. --- complete.cpp | 22 ++++++++++++---------- env.cpp | 4 ++-- 2 files changed, 14 insertions(+), 12 deletions(-) diff --git a/complete.cpp b/complete.cpp index 5a288176b..c1f8b7449 100644 --- a/complete.cpp +++ b/complete.cpp @@ -1080,15 +1080,16 @@ static void complete_cmd( const wchar_t *cmd, int use_builtin, int use_command ) { - wchar_t *path; + wcstring path; wchar_t *path_cpy; wchar_t *nxt_path; wchar_t *state; array_list_t possible_comp; wchar_t *nxt_completion; - wchar_t *cdpath = env_get(L"CDPATH"); - wchar_t *cdpath_cpy = wcsdup( cdpath?cdpath:L"." ); + const wcstring cdpath = env_get_string(L"CDPATH"); +// wchar_t *cdpath_cpy = wcsdup( cdpath?cdpath:L"." ); + wchar_t *cdpath_cpy = wcsdup( !cdpath.empty()?cdpath.c_str():L"." ); if( (wcschr( cmd, L'/') != 0) || (cmd[0] == L'~' ) ) { @@ -1110,11 +1111,11 @@ static void complete_cmd( const wchar_t *cmd, if( use_command ) { - path = env_get(L"PATH"); - if( path ) + path = env_get_string(L"PATH"); + if( !path.empty() ) { - path_cpy = wcsdup( path ); + path_cpy = wcsdup( path.c_str() ); for( nxt_path = wcstok( path_cpy, ARRAY_SEP_STR, &state ); nxt_path != 0; @@ -1650,10 +1651,11 @@ static int complete_variable( const wchar_t *whole_var, if( match || match_no_case ) { - wchar_t *value_unescaped, *value; + wcstring value_unescaped; + wchar_t *value; - value_unescaped = env_get( name ); - if( value_unescaped ) + value_unescaped = env_get_string( name ); + if( !value_unescaped.empty() ) { string_buffer_t desc; string_buffer_t comp; @@ -1673,7 +1675,7 @@ static int complete_variable( const wchar_t *whole_var, flags = COMPLETE_NO_CASE | COMPLETE_DONT_ESCAPE; } - value = expand_escape_variable( value_unescaped ); + value = expand_escape_variable( value_unescaped.c_str() ); sb_init( &desc ); sb_printf( &desc, COMPLETE_VAR_DESC_VAL, value ); diff --git a/env.cpp b/env.cpp index b84e08d6c..b8e1ceafb 100644 --- a/env.cpp +++ b/env.cpp @@ -1199,7 +1199,7 @@ wcstring env_get_string( const wchar_t *key ) { if( wcscmp( res->val, ENV_NULL )==0) { - return 0; + return wcstring(L""); } else { @@ -1226,7 +1226,7 @@ wcstring env_get_string( const wchar_t *key ) if( !item || (wcscmp( item, ENV_NULL )==0)) { - return 0; + return wcstring(L""); } else { From 9f8a1168e6db0788e5741bd97b1bd1d9e443116d Mon Sep 17 00:00:00 2001 From: Siteshwar Vashisht Date: Thu, 12 Jan 2012 03:57:38 +0530 Subject: [PATCH 05/16] Modified env.cpp to use env_get_string() --- env.cpp | 66 ++++++++++++++++++++++++++++-------------------------- screen.cpp | 4 ++-- 2 files changed, 36 insertions(+), 34 deletions(-) diff --git a/env.cpp b/env.cpp index b8e1ceafb..b5e639f50 100644 --- a/env.cpp +++ b/env.cpp @@ -300,8 +300,8 @@ static int is_locale( const wchar_t *key ) */ static void handle_locale() { - const wchar_t *lc_all = env_get( L"LC_ALL" ); - const wchar_t *lang; + const wcstring lc_all = env_get_string( L"LC_ALL" ); + wcstring lang; int i; wchar_t *old = wcsdup(wsetlocale( LC_MESSAGES, NULL )); @@ -321,25 +321,25 @@ static void handle_locale() } ; - if( lc_all ) + if( !lc_all.empty() ) { - wsetlocale( LC_ALL, lc_all ); + wsetlocale( LC_ALL, lc_all.c_str() ); } else { - lang = env_get( L"LANG" ); - if( lang ) + lang = env_get_string( L"LANG" ); + if( !lang.empty() ) { - wsetlocale( LC_ALL, lang ); + wsetlocale( LC_ALL, lang.c_str() ); } for( i=2; locale_variable[i]; i++ ) { - const wchar_t *val = env_get( locale_variable[i] ); + const wcstring val = env_get_string( locale_variable[i] ); - if( val ) + if( !val.empty() ) { - wsetlocale( cat[i], val ); + wsetlocale( cat[i], val.c_str() ); } } } @@ -426,7 +426,7 @@ static void universal_callback( int type, */ static void setup_path() { - wchar_t *path; + wcstring path; size_t i; int j; @@ -441,9 +441,9 @@ static void setup_path() } ; - path = env_get( L"PATH" ); + path = env_get_string( L"PATH" ); - if( path ) + if( !path.empty() ) { tokenize_variable_array2( path, lst ); } @@ -476,18 +476,18 @@ static void setup_path() debug( 3, L"directory %ls was missing", path_el[j] ); - if( path ) + if( !path.empty() ) { - buffer += path; + buffer += path; } - buffer += ARRAY_SEP_STR; - buffer += path_el[j]; + buffer += ARRAY_SEP_STR; + buffer += path_el[j]; - env_set( L"PATH", buffer.c_str(), ENV_GLOBAL | ENV_EXPORT ); + env_set( L"PATH", buffer.empty()?NULL:buffer.c_str(), ENV_GLOBAL | ENV_EXPORT ); - path = env_get( L"PATH" ); - lst.resize(0); + path = env_get_string( L"PATH" ); + lst.resize(0); tokenize_variable_array2( path, lst ); } } @@ -511,7 +511,7 @@ int env_set_pwd() static void env_set_defaults() { - if( !env_get( L"USER" ) ) + if( env_get_string( L"USER" ).empty() ) { struct passwd *pw = getpwuid( getuid()); wchar_t *unam = str2wcs( pw->pw_name ); @@ -519,10 +519,10 @@ static void env_set_defaults() free( unam ); } - if( !env_get( L"HOME" ) ) + if( env_get_string( L"HOME" ).empty() ) { - wchar_t *unam = env_get( L"USER" ); - char *unam_narrow = wcs2str( unam ); + const wcstring unam = env_get_string( L"USER" ); + char *unam_narrow = wcs2str( unam.c_str() ); struct passwd *pw = getpwnam( unam_narrow ); wchar_t *dir = str2wcs( pw->pw_dir ); env_set( L"HOME", dir, ENV_GLOBAL ); @@ -540,7 +540,6 @@ void env_init() struct passwd *pw; wchar_t *uname; wchar_t *version; - wchar_t *shlvl; sb_init( &dyn_var ); b_init( &export_buffer ); @@ -656,16 +655,19 @@ void env_init() version = str2wcs( PACKAGE_VERSION ); env_set( L"version", version, ENV_GLOBAL ); free( version ); - - env_universal_init( env_get( L"FISHD_SOCKET_DIR"), - env_get( L"USER" ), + + wchar_t * fishd_dir = const_cast(env_get_string( L"FISHD_SOCKET_DIR").c_str()); + wchar_t * user_dir = const_cast(env_get_string( L"USER" ).c_str()); + + env_universal_init(fishd_dir , user_dir , &start_fishd, &universal_callback ); /* Set up SHLVL variable */ - shlvl = env_get( L"SHLVL" ); + const wchar_t *shlvl = env_get_string( L"SHLVL" ).empty()?NULL:env_get_string( L"SHLVL" ).c_str(); + if ( shlvl ) { wchar_t *nshlvl, **end_nshlvl; @@ -1722,9 +1724,9 @@ env_vars::env_vars(const wchar_t * const *keys) { ASSERT_IS_MAIN_THREAD(); for (size_t i=0; keys[i]; i++) { - const wchar_t *val = env_get(keys[i]); - if (val) { - vars[keys[i]] = val; + const wcstring val = env_get_string(keys[i]); + if (!val.empty()) { + vars[keys[i]] = wcsdup(val.c_str()); } } } diff --git a/screen.cpp b/screen.cpp index 8481170f6..f4bc39bbc 100644 --- a/screen.cpp +++ b/screen.cpp @@ -204,8 +204,8 @@ static int calc_prompt_width( const wchar_t *prompt ) { if( prompt[j+1] == L'k' ) { - wchar_t *term_name = env_get( L"TERM" ); - if( term_name && wcsstr( term_name, L"screen" ) == term_name ) + wcstring term_name = env_get_string( L"TERM" ); + if( !term_name.empty() && wcsstr( term_name.c_str(), L"screen" ) == term_name ) { const wchar_t *end; j+=2; From 7f49d37a5176b11ca6d5f7b40b4405000d4e3c3f Mon Sep 17 00:00:00 2001 From: Siteshwar Vashisht Date: Thu, 12 Jan 2012 04:01:08 +0530 Subject: [PATCH 06/16] Modified exec.cpp to use env_get_string() --- exec.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/exec.cpp b/exec.cpp index ab347a98e..384fa4e36 100644 --- a/exec.cpp +++ b/exec.cpp @@ -1740,7 +1740,7 @@ int exec_subshell( const wchar_t *cmd, CHECK( cmd, -1 ); - ifs = env_get(L"IFS"); + ifs = env_get_string(L"IFS").empty()?NULL:env_get_string(L"IFS"); if( ifs && ifs[0] ) { From 48655e882ef02efe50e1b5a05e1b0ae653861c3d Mon Sep 17 00:00:00 2001 From: Siteshwar Vashisht Date: Thu, 12 Jan 2012 04:19:04 +0530 Subject: [PATCH 07/16] Modified expand.cpp and exec.cpp to use env_get_string() --- exec.cpp | 2 +- expand.cpp | 10 ++++++---- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/exec.cpp b/exec.cpp index 384fa4e36..e49644e15 100644 --- a/exec.cpp +++ b/exec.cpp @@ -1740,7 +1740,7 @@ int exec_subshell( const wchar_t *cmd, CHECK( cmd, -1 ); - ifs = env_get_string(L"IFS").empty()?NULL:env_get_string(L"IFS"); + ifs = env_get_string(L"IFS").empty()?NULL:env_get_string(L"IFS").c_str(); if( ifs && ifs[0] ) { diff --git a/expand.cpp b/expand.cpp index 11bc43172..3dc94068a 100644 --- a/expand.cpp +++ b/expand.cpp @@ -176,11 +176,11 @@ int expand_is_clean( const wchar_t *in ) /** Return the environment variable value for the string starting at \c in. */ -static wchar_t *expand_var( wchar_t *in ) +static wcstring expand_var( wchar_t *in ) { if( !in ) return 0; - return env_get( in ); + return env_get_string( in ); } /** @@ -958,7 +958,8 @@ static int expand_variables( wchar_t *in, array_list_t *out, int last_idx ) int start_pos = i+1; int stop_pos; int var_len, new_len; - wchar_t * var_val; + const wchar_t * var_val; + wcstring var_val_wstr; wchar_t * new_in; int is_single = (c==VARIABLE_EXPAND_SINGLE); int var_name_stop_pos; @@ -991,7 +992,8 @@ static int expand_variables( wchar_t *in, array_list_t *out, int last_idx ) sb_append_substring( var_tmp, &in[start_pos], var_len ); - var_val = expand_var( (wchar_t *)var_tmp->buff ); + var_val_wstr = expand_var( (wchar_t *)var_tmp->buff ); + var_val = var_val_wstr.empty()?NULL:var_val_wstr.c_str(); if( var_val ) { From 9b56b67c0e1bbd7f043ca6d17715427ab6d40e9f Mon Sep 17 00:00:00 2001 From: Siteshwar Vashisht Date: Thu, 12 Jan 2012 04:21:11 +0530 Subject: [PATCH 08/16] Revert "Modified expand.cpp and exec.cpp to use env_get_string()" This reverts commit 48655e882ef02efe50e1b5a05e1b0ae653861c3d. --- exec.cpp | 2 +- expand.cpp | 10 ++++------ 2 files changed, 5 insertions(+), 7 deletions(-) diff --git a/exec.cpp b/exec.cpp index e49644e15..384fa4e36 100644 --- a/exec.cpp +++ b/exec.cpp @@ -1740,7 +1740,7 @@ int exec_subshell( const wchar_t *cmd, CHECK( cmd, -1 ); - ifs = env_get_string(L"IFS").empty()?NULL:env_get_string(L"IFS").c_str(); + ifs = env_get_string(L"IFS").empty()?NULL:env_get_string(L"IFS"); if( ifs && ifs[0] ) { diff --git a/expand.cpp b/expand.cpp index 3dc94068a..11bc43172 100644 --- a/expand.cpp +++ b/expand.cpp @@ -176,11 +176,11 @@ int expand_is_clean( const wchar_t *in ) /** Return the environment variable value for the string starting at \c in. */ -static wcstring expand_var( wchar_t *in ) +static wchar_t *expand_var( wchar_t *in ) { if( !in ) return 0; - return env_get_string( in ); + return env_get( in ); } /** @@ -958,8 +958,7 @@ static int expand_variables( wchar_t *in, array_list_t *out, int last_idx ) int start_pos = i+1; int stop_pos; int var_len, new_len; - const wchar_t * var_val; - wcstring var_val_wstr; + wchar_t * var_val; wchar_t * new_in; int is_single = (c==VARIABLE_EXPAND_SINGLE); int var_name_stop_pos; @@ -992,8 +991,7 @@ static int expand_variables( wchar_t *in, array_list_t *out, int last_idx ) sb_append_substring( var_tmp, &in[start_pos], var_len ); - var_val_wstr = expand_var( (wchar_t *)var_tmp->buff ); - var_val = var_val_wstr.empty()?NULL:var_val_wstr.c_str(); + var_val = expand_var( (wchar_t *)var_tmp->buff ); if( var_val ) { From ee687ee433c30ad5f6f02229f4bccc078d60ad57 Mon Sep 17 00:00:00 2001 From: Siteshwar Vashisht Date: Thu, 12 Jan 2012 21:39:00 +0530 Subject: [PATCH 09/16] Modified exec.cpp to use env_get_string(); rolled back changes to expand.cpp in last commit, because pressing ^D results into an error with the changes --- exec.cpp | 4 ++-- expand.cpp | 5 ++--- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/exec.cpp b/exec.cpp index 384fa4e36..bd08f1d45 100644 --- a/exec.cpp +++ b/exec.cpp @@ -1739,8 +1739,8 @@ int exec_subshell( const wchar_t *cmd, char sep=0; CHECK( cmd, -1 ); - - ifs = env_get_string(L"IFS").empty()?NULL:env_get_string(L"IFS"); +// ifs = env_get(L"IFS"); + ifs = env_get_string(L"IFS").empty()?NULL:env_get_string(L"IFS").c_str(); if( ifs && ifs[0] ) { diff --git a/expand.cpp b/expand.cpp index 11bc43172..e8e05fd50 100644 --- a/expand.cpp +++ b/expand.cpp @@ -176,7 +176,7 @@ int expand_is_clean( const wchar_t *in ) /** Return the environment variable value for the string starting at \c in. */ -static wchar_t *expand_var( wchar_t *in ) +static const wchar_t* expand_var( wchar_t *in ) { if( !in ) return 0; @@ -958,7 +958,7 @@ static int expand_variables( wchar_t *in, array_list_t *out, int last_idx ) int start_pos = i+1; int stop_pos; int var_len, new_len; - wchar_t * var_val; + const wchar_t * var_val; wchar_t * new_in; int is_single = (c==VARIABLE_EXPAND_SINGLE); int var_name_stop_pos; @@ -990,7 +990,6 @@ static int expand_variables( wchar_t *in, array_list_t *out, int last_idx ) } sb_append_substring( var_tmp, &in[start_pos], var_len ); - var_val = expand_var( (wchar_t *)var_tmp->buff ); if( var_val ) From 9cc2217a262b40418f87caa4b33045bec73a7c67 Mon Sep 17 00:00:00 2001 From: Siteshwar Vashisht Date: Thu, 12 Jan 2012 21:58:42 +0530 Subject: [PATCH 10/16] Fixed connection error to fishd server in env.cpp --- env.cpp | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/env.cpp b/env.cpp index b5e639f50..53d0659b8 100644 --- a/env.cpp +++ b/env.cpp @@ -656,8 +656,11 @@ void env_init() env_set( L"version", version, ENV_GLOBAL ); free( version ); - wchar_t * fishd_dir = const_cast(env_get_string( L"FISHD_SOCKET_DIR").c_str()); - wchar_t * user_dir = const_cast(env_get_string( L"USER" ).c_str()); + const wcstring fishd_dir_wstr = env_get_string( L"FISHD_SOCKET_DIR"); + const wcstring user_dir_wstr = env_get_string( L"USER" ); + + wchar_t * fishd_dir = fishd_dir_wstr.empty()?NULL:const_cast(fishd_dir_wstr.c_str()); + wchar_t * user_dir = user_dir_wstr.empty()?NULL:const_cast(user_dir_wstr.c_str()); env_universal_init(fishd_dir , user_dir , &start_fishd, From 3bb4d0b276cb3b1e437e05f5869b7b665eab82cd Mon Sep 17 00:00:00 2001 From: Siteshwar Vashisht Date: Thu, 12 Jan 2012 22:32:50 +0530 Subject: [PATCH 11/16] Modified reader.cpp to use env_get_string() --- reader.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/reader.cpp b/reader.cpp index 572057cdc..8a7829912 100644 --- a/reader.cpp +++ b/reader.cpp @@ -606,7 +606,8 @@ int reader_interrupted() void reader_write_title() { const wchar_t *title; - wchar_t *term = env_get( L"TERM" ); + const wcstring term_str = env_get_string( L"TERM" ); + const wchar_t *term = term_str.empty()?NULL:term_str.c_str(); /* This is a pretty lame heuristic for detecting terminals that do From 943cc68f54b2b4d8aadc3b262992e2b150edaf3a Mon Sep 17 00:00:00 2001 From: Siteshwar Vashisht Date: Thu, 12 Jan 2012 22:56:53 +0530 Subject: [PATCH 12/16] Modified parse_util.cpp, parser.cpp, path.cpp to use env_get_string() --- parse_util.cpp | 10 +++++----- parser.cpp | 4 +++- path.cpp | 22 +++++++++++++--------- 3 files changed, 21 insertions(+), 15 deletions(-) diff --git a/parse_util.cpp b/parse_util.cpp index 971e258b5..9a3e217f8 100644 --- a/parse_util.cpp +++ b/parse_util.cpp @@ -800,7 +800,7 @@ int parse_util_load( const wcstring &cmd, void (*on_load)(const wchar_t *cmd), int reload ) { - wchar_t *path_var; + wcstring path_var; int res; int c, c2; @@ -811,12 +811,12 @@ int parse_util_load( const wcstring &cmd, // debug( 0, L"Autoload %ls in %ls", cmd, path_var_name ); parse_util_autounload( path_var_name.c_str(), cmd.c_str(), on_load ); - path_var = env_get( path_var_name.c_str() ); + path_var = env_get_string( path_var_name.c_str() ); /* Do we know where to look? */ - if( !path_var ) + if( path_var.empty() ) { return 0; } @@ -856,9 +856,9 @@ int parse_util_load( const wcstring &cmd, We have never tried to autoload using this path name before, set up initial data */ -// debug( 0, L"Create brand new autoload_t for %ls->%ls", path_var_name, path_var ); +// debug( 0, L"Create brand new autoload_t for %ls->%ls", path_var_name, path_var.c_str() ); loaded = &all_loaded_map[path_var_name]; - loaded->old_path = path_var; + loaded->old_path = wcsdup(path_var.c_str()); } std::vector path_list; diff --git a/parser.cpp b/parser.cpp index ed9cab0e7..ed2e7cd56 100644 --- a/parser.cpp +++ b/parser.cpp @@ -2043,7 +2043,9 @@ static int parse_job( process_t *p, } else if(cmd[0]==L'$') { - wchar_t *val = env_get( cmd+1 ); + + const wcstring val_wstr = env_get_string( cmd+1 ); + const wchar_t *val = val_wstr.empty()?NULL:val_wstr.c_str(); if( val ) { debug( 0, diff --git a/path.cpp b/path.cpp index 00f747c2c..2171720ec 100644 --- a/path.cpp +++ b/path.cpp @@ -130,7 +130,7 @@ bool path_get_path_string(const wcstring &cmd_str, wcstring &output, const env_v wchar_t *path_get_path( void *context, const wchar_t *cmd ) { - wchar_t *path; + const wchar_t *path; int err = ENOENT; @@ -166,7 +166,8 @@ wchar_t *path_get_path( void *context, const wchar_t *cmd ) } else { - path = env_get(L"PATH"); + const wcstring path_wstr = env_get_string(L"PATH"); + path = path_wstr.empty()?NULL:path_wstr.c_str(); if( path == 0 ) { if( contains( PREFIX L"/bin", L"/bin", L"/usr/bin" ) ) @@ -189,7 +190,7 @@ wchar_t *path_get_path( void *context, const wchar_t *cmd ) its arguments */ wchar_t *path_cpy = wcsdup( path ); - wchar_t *nxt_path = path; + const wchar_t *nxt_path = path; wchar_t *state; if( (new_cmd==0) || (path_cpy==0) ) @@ -356,20 +357,21 @@ wchar_t *path_get_cdpath( void *context, const wchar_t *dir ) } else { - wchar_t *path; + const wchar_t *path; wchar_t *path_cpy; wchar_t *nxt_path; wchar_t *state; wchar_t *whole_path; - path = env_get(L"CDPATH"); + const wcstring path_wstr = env_get_string(L"CDPATH"); + path = path_wstr.empty()?NULL:path_wstr.c_str(); if( !path || !wcslen(path) ) { path = L"."; } - nxt_path = path; + nxt_path = const_cast(path); path_cpy = wcsdup( path ); if( !path_cpy ) @@ -437,11 +439,12 @@ wchar_t *path_get_cdpath( void *context, const wchar_t *dir ) wchar_t *path_get_config( void *context) { - wchar_t *xdg_dir, *home; + const wchar_t *xdg_dir, *home; int done = 0; wchar_t *res = 0; - xdg_dir = env_get( L"XDG_CONFIG_HOME" ); + const wcstring xdg_dir_wstr = env_get_string( L"XDG_CONFIG_HOME" ); + xdg_dir = xdg_dir_wstr.empty()?NULL:xdg_dir_wstr.c_str(); if( xdg_dir ) { res = wcsdupcat( xdg_dir, L"/fish" ); @@ -457,7 +460,8 @@ wchar_t *path_get_config( void *context) } else { - home = env_get( L"HOME" ); + const wcstring home_wstr = env_get_string( L"HOME" ); + home = home_wstr.empty()?NULL:home_wstr.c_str(); if( home ) { res = wcsdupcat( home, L"/.config/fish" ); From 15296dedd56275b1a780f200df0e7719776fd74c Mon Sep 17 00:00:00 2001 From: Siteshwar Vashisht Date: Thu, 12 Jan 2012 23:07:10 +0530 Subject: [PATCH 13/16] Modified kill.cpp to use env_get_string(). --- kill.cpp | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/kill.cpp b/kill.cpp index 36426af51..36d4d63cb 100644 --- a/kill.cpp +++ b/kill.cpp @@ -104,11 +104,13 @@ void kill_add( wchar_t *str ) I couldn't think of a safe way to allow overide of the echo command too, so, the command used must accept the input via stdin. */ - wchar_t *clipboard; - if( (clipboard = env_get(L"FISH_CLIPBOARD_CMD")) ) + + const wcstring clipboard_wstr = env_get_string(L"FISH_CLIPBOARD_CMD"); +// const wchar_t *clipboard = clipboard_wstr.empty()?NULL:clipboard.c_str(); + if( !clipboard_wstr.empty() ) { escaped_str = escape( str, 1 ); - cmd = wcsdupcat(L"echo -n ", escaped_str, clipboard); + cmd = wcsdupcat(L"echo -n ", escaped_str, clipboard_wstr.c_str()); } else { @@ -117,8 +119,9 @@ void kill_add( wchar_t *str ) return; } - wchar_t *disp; - if( (disp = env_get( L"DISPLAY" )) ) + const wcstring disp_wstr = env_get_string( L"DISPLAY" ); +// wchar_t *disp = disp_wstr.empty()?NULL:disp_wstr.c_str(); + if( !disp_wstr.empty() ) { escaped_str = escape( str, 1 ); cmd = wcsdupcat(L"echo ", escaped_str, L"|xsel -b" ); @@ -215,13 +218,13 @@ wchar_t *kill_yank_rotate() */ static void kill_check_x_buffer() { - wchar_t *disp; + wcstring disp; if( !has_xsel() ) return; - if( (disp = env_get( L"DISPLAY" )) ) + if( (!(disp = env_get_string( L"DISPLAY" )).empty()) ) { size_t i; wcstring cmd = L"xsel -t 500 -b"; From 72cfdbbb4e75faeb9a795c04d8606a322cea7f56 Mon Sep 17 00:00:00 2001 From: Siteshwar Vashisht Date: Thu, 12 Jan 2012 23:11:07 +0530 Subject: [PATCH 14/16] Modified input.cpp, output.cpp to use env_get_string() --- input.cpp | 2 +- output.cpp | 2 +- output.h | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/input.cpp b/input.cpp index 7204031d7..e9cf4037c 100644 --- a/input.cpp +++ b/input.cpp @@ -324,7 +324,7 @@ int input_init() debug( 0, _( L"Could not set up terminal" ) ); exit(1); } - output_set_term( env_get( L"TERM" ) ); + output_set_term( env_get_string( L"TERM" ).c_str() ); input_terminfo_init(); diff --git a/output.cpp b/output.cpp index 0bc4e252d..8902628ac 100644 --- a/output.cpp +++ b/output.cpp @@ -587,7 +587,7 @@ int output_color_code( const wchar_t *val ) } -void output_set_term( wchar_t *term ) +void output_set_term( const wchar_t *term ) { current_term = halloc_wcsdup(global_context, term); } diff --git a/output.h b/output.h index 0c8faa4b1..896e40fea 100644 --- a/output.h +++ b/output.h @@ -154,7 +154,7 @@ int (*output_get_writer())(char) ; /** Set the terminal name */ -void output_set_term( wchar_t *term ); +void output_set_term( const wchar_t *term ); /** Return the terminal name */ From d0e18e3d20a247ffb1b25dcbba4dae1b0c961f11 Mon Sep 17 00:00:00 2001 From: Siteshwar Vashisht Date: Thu, 12 Jan 2012 23:19:05 +0530 Subject: [PATCH 15/16] Modified highlight.cpp to use env_get_string(). --- highlight.cpp | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/highlight.cpp b/highlight.cpp index acc7f6886..3467f9b80 100644 --- a/highlight.cpp +++ b/highlight.cpp @@ -190,19 +190,24 @@ int highlight_get_color( int highlight ) } } - wchar_t *val = env_get( highlight_var[idx]); + wcstring val_wstr = env_get_string( highlight_var[idx]); + const wchar_t *val = val_wstr.empty()?NULL:val_wstr.c_str(); // debug( 1, L"%d -> %d -> %ls", highlight, idx, val ); - if( val == 0 ) - val = env_get( highlight_var[0]); + if( val == 0 ) { + val_wstr = env_get_string( highlight_var[0]); + val = val_wstr.empty()?NULL:val_wstr.c_str(); + } if( val ) result = output_color_code( val ); if( highlight & HIGHLIGHT_VALID_PATH ) { - wchar_t *val2 = env_get( L"fish_color_valid_path" ); + wcstring val2_wstr = env_get_string( L"fish_color_valid_path" ); + const wchar_t *val2 = val2_wstr.empty()?NULL:val2_wstr.c_str(); + int result2 = output_color_code( val2 ); if( result == FISH_COLOR_NORMAL ) result = result2; From 610246c48f2834180eca90628c385350574b8c2c Mon Sep 17 00:00:00 2001 From: Siteshwar Vashisht Date: Thu, 12 Jan 2012 23:22:38 +0530 Subject: [PATCH 16/16] Modified function.cpp to use env_get_string(). --- function.cpp | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/function.cpp b/function.cpp index a9e1f6196..ddea8e869 100644 --- a/function.cpp +++ b/function.cpp @@ -153,8 +153,9 @@ static void autoload_names( array_list_t *out, int get_hidden ) int i; array_list_t path_list; - const wchar_t *path_var = env_get( L"fish_function_path" ); - + const wcstring path_var_wstr = env_get_string( L"fish_function_path" ); + const wchar_t *path_var = path_var_wstr.empty()?NULL:path_var_wstr.c_str(); + if( ! path_var ) return;