diff --git a/env.cpp b/env.cpp index 634f64043..7e6e363b6 100644 --- a/env.cpp +++ b/env.cpp @@ -1029,13 +1029,6 @@ int env_remove(const wcstring &key, int var_mode) return !erased; } -env_var_t env_var_t::missing_var(void) -{ - env_var_t result(L""); - result.is_missing = true; - return result; -} - const wchar_t *env_var_t::c_str(void) const { assert(! is_missing); @@ -1119,7 +1112,7 @@ env_var_t env_get_string(const wcstring &key) env_universal_barrier(); } - wchar_t *item = env_universal_get(key); + const wchar_t *item = env_universal_get(key); if (!item || (wcscmp(item, ENV_NULL)==0)) { @@ -1135,7 +1128,7 @@ env_var_t env_get_string(const wcstring &key) bool env_exist(const wchar_t *key, int mode) { env_node_t *env; - wchar_t *item=0; + const wchar_t *item = NULL; CHECK(key, false); @@ -1387,9 +1380,9 @@ wcstring_list_t env_get_names(int flags) { wcstring_list_t uni_list; - env_universal_get_names2(uni_list, - show_exported, - show_unexported); + env_universal_get_names(uni_list, + show_exported, + show_unexported); names.insert(uni_list.begin(), uni_list.end()); } @@ -1472,7 +1465,7 @@ static void update_export_array_if_necessary(bool recalc) get_exported(top, vals); wcstring_list_t uni; - env_universal_get_names2(uni, 1, 0); + env_universal_get_names(uni, 1, 0); for (i=0; i(this) == *static_cast(&s); + return is_missing == s.is_missing && static_cast(*this) == static_cast(s); } + bool operator==(const wcstring &s) const + { + return ! is_missing && static_cast(*this) == s; + } + + bool operator!=(const env_var_t &s) const + { + return !(*this == s); + } + + bool operator!=(const wcstring &s) const + { + return !(*this == s); + } + + bool operator==(const wchar_t *s) const + { + return ! is_missing && static_cast(*this) == s; + } + + bool operator!=(const wchar_t *s) const + { + return !(*this == s); + } + + }; /** Gets the variable with the specified name, or an empty string if it does not exist. diff --git a/env_universal.cpp b/env_universal.cpp index f39a608fa..28180b6b5 100644 --- a/env_universal.cpp +++ b/env_universal.cpp @@ -49,9 +49,9 @@ connection_t env_universal_server; /** - Set to 1 after initialization has been performed + Set to true after initialization has been performed */ -static int init = 0; +static bool s_env_univeral_inited = false; /** The number of attempts to start fishd @@ -207,7 +207,7 @@ static void callback(fish_message_type_t type, const wchar_t *name, const wchar_ */ static void check_connection() { - if (!init) + if (! s_env_univeral_inited) return; if (env_universal_server.killme) @@ -258,10 +258,10 @@ static void reconnect() debug(3, L"Get new fishd connection"); - init = 0; + s_env_univeral_inited = false; env_universal_server.buffer_consumed = env_universal_server.buffer_used = 0; env_universal_server.fd = get_socket(); - init = 1; + s_env_univeral_inited = true; if (env_universal_server.fd >= 0) { env_universal_remove_all(); @@ -285,7 +285,7 @@ void env_universal_init(wchar_t * p, env_universal_server.fd = get_socket(); env_universal_common_init(&callback); env_universal_read_all(); - init = 1; + s_env_univeral_inited = true; if (env_universal_server.fd >= 0) { env_universal_barrier(); @@ -308,8 +308,7 @@ void env_universal_destroy() connection_destroy(&env_universal_server); env_universal_server.fd =-1; - env_universal_common_destroy(); - init = 0; + s_env_univeral_inited = false; } @@ -318,7 +317,7 @@ void env_universal_destroy() */ int env_universal_read_all() { - if (!init) + if (! s_env_univeral_inited) return 0; if (env_universal_server.fd == -1) @@ -341,17 +340,17 @@ int env_universal_read_all() } } -wchar_t *env_universal_get(const wcstring &name) +const wchar_t *env_universal_get(const wcstring &name) { - if (!init) - return 0; + if (!s_env_univeral_inited) + return NULL; return env_universal_common_get(name); } bool env_universal_get_export(const wcstring &name) { - if (!init) + if (!s_env_univeral_inited) return false; return env_universal_common_get_export(name); @@ -363,7 +362,7 @@ void env_universal_barrier() message_t *msg; fd_set fds; - if (!init || is_dead()) + if (!s_env_univeral_inited || is_dead()) return; barrier_reply = 0; @@ -424,7 +423,7 @@ void env_universal_set(const wcstring &name, const wcstring &value, bool exportv { message_t *msg; - if (!init) + if (!s_env_univeral_inited) return; debug(3, L"env_universal_set( \"%ls\", \"%ls\" )", name.c_str(), value.c_str()); @@ -456,7 +455,7 @@ int env_universal_remove(const wchar_t *name) int res; message_t *msg; - if (!init) + if (!s_env_univeral_inited) return 1; CHECK(name, 1); @@ -481,11 +480,11 @@ int env_universal_remove(const wchar_t *name) return res; } -void env_universal_get_names2(wcstring_list_t &lst, - bool show_exported, - bool show_unexported) +void env_universal_get_names(wcstring_list_t &lst, + bool show_exported, + bool show_unexported) { - if (!init) + if (!s_env_univeral_inited) return; env_universal_common_get_names(lst, diff --git a/env_universal.h b/env_universal.h index 3637ba675..4f38fe796 100644 --- a/env_universal.h +++ b/env_universal.h @@ -29,7 +29,7 @@ void env_universal_destroy(); /** Get the value of a universal variable */ -wchar_t *env_universal_get(const wcstring &name); +const wchar_t *env_universal_get(const wcstring &name); /** Get the export flag of the variable with the specified @@ -60,9 +60,9 @@ int env_universal_read_all(); \param show_exported whether exported variables should be shown \param show_unexported whether unexported variables should be shown */ -void env_universal_get_names2(wcstring_list_t &list, - bool show_exported, - bool show_unexported); +void env_universal_get_names(wcstring_list_t &list, + bool show_exported, + bool show_unexported); /** Synchronize with fishd diff --git a/env_universal_common.cpp b/env_universal_common.cpp index c7fc2cb37..52df1ae88 100644 --- a/env_universal_common.cpp +++ b/env_universal_common.cpp @@ -106,7 +106,7 @@ static void parse_message(wchar_t *msg, /** The table of all universal variables */ -typedef std::map env_var_table_t; +typedef std::map env_var_table_t; env_var_table_t env_universal_var; /** @@ -416,18 +416,6 @@ void env_universal_common_init(void (*cb)(fish_message_type_t type, const wchar_ callback = cb; } - -void env_universal_common_destroy() -{ - env_var_table_t::iterator iter; - - for (iter = env_universal_var.begin(); iter != env_universal_var.end(); ++iter) - { - var_uni_entry_t* value = iter->second; - delete value; - } -} - /** Read one byte of date form the specified connection */ @@ -550,13 +538,7 @@ void read_message(connection_t *src) */ void env_universal_common_remove(const wcstring &name) { - env_var_table_t::iterator result = env_universal_var.find(name); - if (result != env_universal_var.end()) - { - var_uni_entry_t* v = result->second; - env_universal_var.erase(result); - delete v; - } + env_universal_var.erase(name); } /** @@ -576,18 +558,13 @@ static bool match(const wchar_t *msg, const wchar_t *cmd) void env_universal_common_set(const wchar_t *key, const wchar_t *val, bool exportv) { - var_uni_entry_t *entry; - CHECK(key,); CHECK(val,); - entry = new var_uni_entry_t; + var_uni_entry_t &entry = env_universal_var[key]; + entry.exportv=exportv; + entry.val = val; - entry->exportv=exportv; - entry->val = val; - env_universal_common_remove(key); - - env_universal_var[key] = entry; if (callback) { callback(exportv?SET_EXPORT:SET, key, val); @@ -909,31 +886,27 @@ void env_universal_common_get_names(wcstring_list_t &lst, env_var_table_t::const_iterator iter; for (iter = env_universal_var.begin(); iter != env_universal_var.end(); ++iter) { - const wcstring& key = iter->first; - const var_uni_entry_t *e = iter->second; - if ((e->exportv && show_exported) || - (!e->exportv && show_unexported)) + const wcstring &key = iter->first; + const var_uni_entry_t &e = iter->second; + if ((e.exportv && show_exported) || (! e.exportv && show_unexported)) { lst.push_back(key); } - } } -wchar_t *env_universal_common_get(const wcstring &name) +const wchar_t *env_universal_common_get(const wcstring &name) { env_var_table_t::const_iterator result = env_universal_var.find(name); - if (result != env_universal_var.end()) { - const var_uni_entry_t *e = result->second; - if (e) - return const_cast(e->val.c_str()); + const var_uni_entry_t &e = result->second; + return const_cast(e.val.c_str()); } - return 0; + return NULL; } bool env_universal_common_get_export(const wcstring &name) @@ -941,9 +914,8 @@ bool env_universal_common_get_export(const wcstring &name) env_var_table_t::const_iterator result = env_universal_var.find(name); if (result != env_universal_var.end()) { - const var_uni_entry_t *e = result->second; - if (e != NULL) - return e->exportv; + const var_uni_entry_t &e = result->second; + return e.exportv; } return false; } @@ -951,13 +923,12 @@ bool env_universal_common_get_export(const wcstring &name) void enqueue_all(connection_t *c) { env_var_table_t::const_iterator iter; - for (iter = env_universal_var.begin(); iter != env_universal_var.end(); ++iter) { const wcstring &key = iter->first; - const var_uni_entry_t *val = iter->second; + const var_uni_entry_t &entry = iter->second; - message_t *msg = create_message(val->exportv?SET_EXPORT:SET, key.c_str(), val->val.c_str()); + message_t *msg = create_message(entry.exportv ? SET_EXPORT : SET, key.c_str(), entry.val.c_str()); msg->count=1; c->unsent->push(msg); } diff --git a/env_universal_common.h b/env_universal_common.h index 4625725f9..21fcd8eb9 100644 --- a/env_universal_common.h +++ b/env_universal_common.h @@ -139,11 +139,6 @@ message_t *create_message(fish_message_type_t type, const wchar_t *key, const wc */ void env_universal_common_init(void (*cb)(fish_message_type_t type, const wchar_t *key, const wchar_t *val)); -/** - Destroy library data -*/ -void env_universal_common_destroy(); - /** Add all variable names to the specified list @@ -182,7 +177,7 @@ void env_universal_common_remove(const wcstring &key); This function operate agains the local copy of all universal variables, it does not communicate with any other process. */ -wchar_t *env_universal_common_get(const wcstring &name); +const wchar_t *env_universal_common_get(const wcstring &name); /** Get the export flag of the variable with the specified diff --git a/fishd.cpp b/fishd.cpp index 6cfd02b31..d1364a894 100644 --- a/fishd.cpp +++ b/fishd.cpp @@ -708,27 +708,20 @@ static void daemonize() } /** - Get environment variable value. The resulting string needs to be free'd. + Get environment variable value. */ -static wchar_t *fishd_env_get(const wchar_t *key) +static env_var_t fishd_env_get(const char *key) { - char *nres, *nkey; - wchar_t *res; - - nkey = wcs2str(key); - nres = getenv(nkey); - free(nkey); - if (nres) + const char *env = getenv(key); + if (env != NULL) { - wcstring tmp = str2wcstring(nres); - return wcsdup(tmp.c_str()); + return env_var_t(str2wcstring(env)); } else { - res = env_universal_common_get(key); - if (res) - res = wcsdup(res); - return res; + const wcstring wkey = str2wcstring(key); + const wchar_t *tmp = env_universal_common_get(wkey); + return tmp ? env_var_t(tmp) : env_var_t::missing_var(); } } @@ -740,12 +733,11 @@ static wchar_t *fishd_env_get(const wchar_t *key) */ static wcstring fishd_get_config() { - wchar_t *xdg_dir, *home; bool done = false; wcstring result; - xdg_dir = fishd_env_get(L"XDG_CONFIG_HOME"); - if (xdg_dir) + env_var_t xdg_dir = fishd_env_get("XDG_CONFIG_HOME"); + if (! xdg_dir.missing_or_empty()) { result = xdg_dir; append_path_component(result, L"/fish"); @@ -753,12 +745,11 @@ static wcstring fishd_get_config() { done = true; } - free(xdg_dir); } else { - home = fishd_env_get(L"HOME"); - if (home) + env_var_t home = fishd_env_get("HOME"); + if (! home.missing_or_empty()) { result = home; append_path_component(result, L"/.config/fish"); @@ -766,7 +757,6 @@ static wcstring fishd_get_config() { done = 1; } - free(home); } } @@ -1100,7 +1090,6 @@ int main(int argc, char ** argv) { debug(0, L"No more clients. Quitting"); save(); - env_universal_common_destroy(); break; } diff --git a/reader.cpp b/reader.cpp index 8689fe643..223b3c603 100644 --- a/reader.cpp +++ b/reader.cpp @@ -668,7 +668,7 @@ int reader_reading_interrupted() { reader_exit(1, 0); parser_t::skip_all_blocks(); - // We handled the interrupt ourselves, our caller doesn't need to + // We handled the interrupt ourselves, our caller doesn't need to // handle it. return 0; } diff --git a/reader.h b/reader.h index ccd0dbf67..174be574d 100644 --- a/reader.h +++ b/reader.h @@ -118,25 +118,25 @@ size_t reader_get_cursor_pos(); int reader_interrupted(); /** - Clear the interrupted flag unconditionally without handling anything. The - flag could have been set e.g. when an interrupt arrived just as we were - ending an earlier \c reader_readline invocation but before the - \c is_interactive_read flag was cleared. + Clear the interrupted flag unconditionally without handling anything. The + flag could have been set e.g. when an interrupt arrived just as we were + ending an earlier \c reader_readline invocation but before the + \c is_interactive_read flag was cleared. */ void reader_reset_interrupted(); /** Return the value of the interrupted flag, which is set by the sigint handler, and clear it if it was set. If the current reader is interruptible, - call \c reader_exit(). + call \c reader_exit(). */ int reader_reading_interrupted(); /** - Returns true if the current reader generation count does not equal the - generation count the current thread was started with. + Returns true if the current reader generation count does not equal the + generation count the current thread was started with. Note 1: currently only valid for autocompletion threads! Other threads don't - set the threadlocal generation count when they start up. + set the threadlocal generation count when they start up. */ bool reader_thread_job_is_stale();