Skip to content

Commit c36ad27

Browse files
committed
stop subclassing env_var_t from wcstring
This is the first step to implementing issue #4200 is to stop subclassing env_var_t from wcstring. Not too surprisingly doing this identified several places that were incorrectly treating env_var_t and wcstring as interchangeable types. I'm not talking about those places that passed an env_var_t instance to a function that takes a wcstring. I'm talking about doing things like assigning the former to the latter type, relying on the implicit conversion, and thus losing information. We also rename `env_get_string()` to `env_get()` for symmetry with `env_set()` and to make it clear the function does not return a string.
1 parent d87c042 commit c36ad27

22 files changed

+204
-185
lines changed

src/autoload.cpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ int autoload_t::load(const wcstring &cmd, bool reload) {
6969
CHECK_BLOCK(0);
7070
ASSERT_IS_MAIN_THREAD();
7171

72-
env_var_t path_var = env_get_string(env_var_name);
72+
env_var_t path_var = env_get(env_var_name);
7373

7474
// Do we know where to look?
7575
if (path_var.empty()) return 0;
@@ -79,7 +79,7 @@ int autoload_t::load(const wcstring &cmd, bool reload) {
7979
if (path_var != this->last_path) {
8080
this->last_path = path_var;
8181
this->last_path_tokenized.clear();
82-
tokenize_variable_array(this->last_path, this->last_path_tokenized);
82+
this->last_path.to_list(this->last_path_tokenized);
8383

8484
scoped_lock locker(lock);
8585
this->evict_all_nodes();
@@ -115,7 +115,7 @@ bool autoload_t::can_load(const wcstring &cmd, const env_vars_snapshot_t &vars)
115115
if (path_var.missing_or_empty()) return false;
116116

117117
std::vector<wcstring> path_list;
118-
tokenize_variable_array(path_var, path_list);
118+
path_var.to_list(path_list);
119119
return this->locate_file_and_maybe_load_it(cmd, false, false, path_list);
120120
}
121121

src/autoload.h

+2-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include <set>
99

1010
#include "common.h"
11+
#include "env.h"
1112
#include "lru.h"
1213

1314
/// Record of an attempt to access a file.
@@ -50,7 +51,7 @@ class autoload_t : public lru_cache_t<autoload_t, autoload_function_t> {
5051
/// The environment variable name.
5152
const wcstring env_var_name;
5253
/// The path from which we most recently autoloaded.
53-
wcstring last_path;
54+
env_var_t last_path;
5455
/// the most reecently autoloaded path, tokenized (split on separators).
5556
wcstring_list_t last_path_tokenized;
5657
/// A table containing all the files that are currently being loaded.

src/builtin_cd.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ int builtin_cd(parser_t &parser, io_streams_t &streams, wchar_t **argv) {
3939
if (argv[optind]) {
4040
dir_in = env_var_t(argv[optind]);
4141
} else {
42-
dir_in = env_get_string(L"HOME");
42+
dir_in = env_get(L"HOME");
4343
if (dir_in.missing_or_empty()) {
4444
streams.err.append_format(_(L"%ls: Could not find home directory\n"), cmd);
4545
return STATUS_CMD_ERROR;

src/builtin_functions.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ static wcstring functions_def(const wcstring &name) {
193193
it != end; ++it) {
194194
wcstring_list_t lst;
195195
if (!it->second.missing()) {
196-
tokenize_variable_array(it->second, lst);
196+
it->second.to_list(lst);
197197
}
198198

199199
// This forced tab is crummy, but we don't know what indentation style the function uses.

src/builtin_read.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -427,9 +427,9 @@ int builtin_read(parser_t &parser, io_streams_t &streams, wchar_t **argv) {
427427
}
428428

429429
if (!opts.have_delimiter) {
430-
env_var_t ifs = env_get_string(L"IFS");
430+
env_var_t ifs = env_get(L"IFS");
431431
if (!ifs.missing_or_empty()) {
432-
opts.delimiter = ifs;
432+
opts.delimiter = ifs.as_string();
433433
}
434434
}
435435
if (opts.delimiter.empty()) {

src/builtin_set.cpp

+22-22
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ static int validate_cmd_opts(const wchar_t *cmd, set_cmd_opts_t &opts, //!OCLIN
212212
static int check_global_scope_exists(const wchar_t *cmd, set_cmd_opts_t &opts, const wchar_t *dest,
213213
io_streams_t &streams) {
214214
if (opts.universal) {
215-
env_var_t global_dest = env_get_string(dest, ENV_GLOBAL);
215+
env_var_t global_dest = env_get(dest, ENV_GLOBAL);
216216
if (!global_dest.missing()) {
217217
streams.err.append_format(
218218
_(L"%ls: Warning: universal scope selected, but a global variable '%ls' exists.\n"),
@@ -239,9 +239,8 @@ static int my_env_path_setup(const wchar_t *cmd, const wchar_t *key, //!OCLINT(
239239
// not the (missing) local value. Also don't bother to complain about relative paths, which
240240
// don't start with /.
241241
wcstring_list_t existing_values;
242-
const env_var_t existing_variable = env_get_string(key, ENV_DEFAULT);
243-
if (!existing_variable.missing_or_empty())
244-
tokenize_variable_array(existing_variable, existing_values);
242+
const env_var_t existing_variable = env_get(key, ENV_DEFAULT);
243+
if (!existing_variable.missing_or_empty()) existing_variable.to_list(existing_values);
245244

246245
for (size_t i = 0; i < list.size(); i++) {
247246
const wcstring &dir = list.at(i);
@@ -346,9 +345,9 @@ static int parse_index(std::vector<long> &indexes, wchar_t *src, int scope, io_s
346345
*p = L'\0'; // split the var name from the indexes/slices
347346
p++;
348347

349-
env_var_t var_str = env_get_string(src, scope);
348+
env_var_t var_str = env_get(src, scope);
350349
wcstring_list_t var;
351-
if (!var_str.missing()) tokenize_variable_array(var_str, var);
350+
if (!var_str.missing()) var_str.to_list(var);
352351

353352
int count = 0;
354353

@@ -457,15 +456,16 @@ static int builtin_set_list(const wchar_t *cmd, set_cmd_opts_t &opts, int argc,
457456
streams.out.append(e_key);
458457

459458
if (!names_only) {
460-
env_var_t value = env_get_string(key, compute_scope(opts));
461-
if (!value.missing()) {
459+
env_var_t var = env_get(key, compute_scope(opts));
460+
if (!var.missing()) {
462461
bool shorten = false;
463-
if (opts.shorten_ok && value.length() > 64) {
462+
wcstring val = var.as_string();
463+
if (opts.shorten_ok && val.length() > 64) {
464464
shorten = true;
465-
value.resize(60);
465+
val.resize(60);
466466
}
467467

468-
wcstring e_value = expand_escape_variable(value);
468+
wcstring e_value = expand_escape_variable(val);
469469
streams.out.append(L" ");
470470
streams.out.append(e_value);
471471

@@ -500,8 +500,8 @@ static int builtin_set_query(const wchar_t *cmd, set_cmd_opts_t &opts, int argc,
500500

501501
if (idx_count) {
502502
wcstring_list_t result;
503-
env_var_t dest_str = env_get_string(dest, scope);
504-
if (!dest_str.missing()) tokenize_variable_array(dest_str, result);
503+
env_var_t dest_str = env_get(dest, scope);
504+
if (!dest_str.missing()) dest_str.to_list(result);
505505

506506
for (auto idx : indexes) {
507507
if (idx < 1 || (size_t)idx > result.size()) retval++;
@@ -538,12 +538,12 @@ static void show_scope(const wchar_t *var_name, int scope, io_streams_t &streams
538538
}
539539

540540
if (env_exist(var_name, scope)) {
541-
const env_var_t evar = env_get_string(var_name, scope | ENV_EXPORT | ENV_USER);
541+
const env_var_t evar = env_get(var_name, scope | ENV_EXPORT | ENV_USER);
542542
const wchar_t *exportv = evar.missing() ? _(L"unexported") : _(L"exported");
543543

544-
const env_var_t var = env_get_string(var_name, scope | ENV_USER);
544+
const env_var_t var = env_get(var_name, scope | ENV_USER);
545545
wcstring_list_t result;
546-
if (!var.empty()) tokenize_variable_array(var, result);
546+
if (!var.empty()) var.to_list(result);
547547

548548
streams.out.append_format(_(L"$%ls: set in %ls scope, %ls, with %d elements\n"), var_name,
549549
scope_name, exportv, result.size());
@@ -632,10 +632,10 @@ static int builtin_set_erase(const wchar_t *cmd, set_cmd_opts_t &opts, int argc,
632632
if (idx_count == 0) { // unset the var
633633
retval = env_remove(dest, scope);
634634
} else { // remove just the specified indexes of the var
635-
const env_var_t dest_var = env_get_string(dest, scope);
635+
const env_var_t dest_var = env_get(dest, scope);
636636
if (dest_var.missing()) return STATUS_CMD_ERROR;
637637
wcstring_list_t result;
638-
tokenize_variable_array(dest_var, result);
638+
dest_var.to_list(result);
639639
erase_values(result, indexes);
640640
retval = my_env_set(cmd, dest, result, scope, streams);
641641
}
@@ -658,9 +658,9 @@ static int set_var_array(const wchar_t *cmd, set_cmd_opts_t &opts, const wchar_t
658658
for (int i = 0; i < argc; i++) new_values.push_back(argv[i]);
659659
}
660660

661-
env_var_t var_str = env_get_string(varname, scope);
661+
env_var_t var_str = env_get(varname, scope);
662662
wcstring_list_t var_array;
663-
if (!var_str.missing()) tokenize_variable_array(var_str, var_array);
663+
if (!var_str.missing()) var_str.to_list(var_array);
664664
new_values.insert(new_values.end(), var_array.begin(), var_array.end());
665665

666666
if (opts.append) {
@@ -691,8 +691,8 @@ static int set_var_slices(const wchar_t *cmd, set_cmd_opts_t &opts, const wchar_
691691
}
692692

693693
int scope = compute_scope(opts); // calculate the variable scope based on the provided options
694-
const env_var_t var_str = env_get_string(varname, scope);
695-
if (!var_str.missing()) tokenize_variable_array(var_str, new_values);
694+
const env_var_t var_str = env_get(varname, scope);
695+
if (!var_str.missing()) var_str.to_list(new_values);
696696

697697
// Slice indexes have been calculated, do the actual work.
698698
wcstring_list_t result;

src/common.cpp

+4-4
Original file line numberDiff line numberDiff line change
@@ -1554,8 +1554,8 @@ static void validate_new_termsize(struct winsize *new_termsize) {
15541554
}
15551555
#endif
15561556
// Fallback to the environment vars.
1557-
env_var_t col_var = env_get_string(L"COLUMNS");
1558-
env_var_t row_var = env_get_string(L"LINES");
1557+
env_var_t col_var = env_get(L"COLUMNS");
1558+
env_var_t row_var = env_get(L"LINES");
15591559
if (!col_var.missing_or_empty() && !row_var.missing_or_empty()) {
15601560
// Both vars have to have valid values.
15611561
int col = fish_wcstoi(col_var.c_str());
@@ -1582,11 +1582,11 @@ static void validate_new_termsize(struct winsize *new_termsize) {
15821582
/// Export the new terminal size as env vars and to the kernel if possible.
15831583
static void export_new_termsize(struct winsize *new_termsize) {
15841584
wchar_t buf[64];
1585-
env_var_t cols = env_get_string(L"COLUMNS", ENV_EXPORT);
1585+
env_var_t cols = env_get(L"COLUMNS", ENV_EXPORT);
15861586
swprintf(buf, 64, L"%d", (int)new_termsize->ws_col);
15871587
env_set(L"COLUMNS", buf, ENV_GLOBAL | (cols.missing_or_empty() ? 0 : ENV_EXPORT));
15881588

1589-
env_var_t lines = env_get_string(L"LINES", ENV_EXPORT);
1589+
env_var_t lines = env_get(L"LINES", ENV_EXPORT);
15901590
swprintf(buf, 64, L"%d", (int)new_termsize->ws_row);
15911591
env_set(L"LINES", buf, ENV_GLOBAL | (lines.missing_or_empty() ? 0 : ENV_EXPORT));
15921592

src/complete.cpp

+5-4
Original file line numberDiff line numberDiff line change
@@ -1106,12 +1106,13 @@ bool completer_t::complete_variable(const wcstring &str, size_t start_offset) {
11061106
wcstring desc;
11071107
if (this->wants_descriptions()) {
11081108
// Can't use this->vars here, it could be any variable.
1109-
env_var_t value_unescaped = env_get_string(env_name);
1110-
if (value_unescaped.missing()) continue;
1109+
env_var_t var = env_get(env_name);
1110+
if (var.missing()) continue;
11111111

1112-
wcstring value = expand_escape_variable(value_unescaped);
1113-
if (this->type() != COMPLETE_AUTOSUGGEST)
1112+
wcstring value = expand_escape_variable(var.as_string());
1113+
if (this->type() != COMPLETE_AUTOSUGGEST) {
11141114
desc = format_string(COMPLETE_VAR_DESC_VAL, value.c_str());
1115+
}
11151116
}
11161117

11171118
append_completion(&this->completions, comp, desc, flags, match);

0 commit comments

Comments
 (0)