Clean up env_var_table_t. Switch from storing var_uni_entry_t* to var_uni_entry_t. Various other cleanups.

This commit is contained in:
ridiculousfish 2013-02-11 23:16:50 -08:00
parent be23c0755e
commit 2a7fc9c3a5
9 changed files with 106 additions and 127 deletions

15
env.cpp
View file

@ -1029,13 +1029,6 @@ int env_remove(const wcstring &key, int var_mode)
return !erased; 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 const wchar_t *env_var_t::c_str(void) const
{ {
assert(! is_missing); assert(! is_missing);
@ -1119,7 +1112,7 @@ env_var_t env_get_string(const wcstring &key)
env_universal_barrier(); 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)) 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) bool env_exist(const wchar_t *key, int mode)
{ {
env_node_t *env; env_node_t *env;
wchar_t *item=0; const wchar_t *item = NULL;
CHECK(key, false); CHECK(key, false);
@ -1387,7 +1380,7 @@ wcstring_list_t env_get_names(int flags)
{ {
wcstring_list_t uni_list; wcstring_list_t uni_list;
env_universal_get_names2(uni_list, env_universal_get_names(uni_list,
show_exported, show_exported,
show_unexported); show_unexported);
names.insert(uni_list.begin(), uni_list.end()); 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); get_exported(top, vals);
wcstring_list_t uni; wcstring_list_t uni;
env_universal_get_names2(uni, 1, 0); env_universal_get_names(uni, 1, 0);
for (i=0; i<uni.size(); i++) for (i=0; i<uni.size(); i++)
{ {
const wcstring &key = uni.at(i); const wcstring &key = uni.at(i);

46
env.h
View file

@ -108,20 +108,31 @@ class env_var_t : public wcstring
private: private:
bool is_missing; bool is_missing;
public: public:
static env_var_t missing_var(void); static env_var_t missing_var(void)
{
env_var_t result(L"");
result.is_missing = true;
return result;
}
env_var_t(const env_var_t &x) : wcstring(x), is_missing(x.is_missing) { } env_var_t(const env_var_t &x) : wcstring(x), is_missing(x.is_missing) { }
env_var_t(const wcstring & x) : wcstring(x), is_missing(false) { } env_var_t(const wcstring & x) : wcstring(x), is_missing(false) { }
env_var_t(const wchar_t *x) : wcstring(x), is_missing(false) { } env_var_t(const wchar_t *x) : wcstring(x), is_missing(false) { }
env_var_t() : wcstring(L""), is_missing(false) { } env_var_t() : wcstring(L""), is_missing(false) { }
bool missing(void) const bool missing(void) const
{ {
return is_missing; return is_missing;
} }
bool missing_or_empty(void) const bool missing_or_empty(void) const
{ {
return missing() || empty(); return missing() || empty();
} }
const wchar_t *c_str(void) const; const wchar_t *c_str(void) const;
env_var_t &operator=(const env_var_t &s) env_var_t &operator=(const env_var_t &s)
{ {
is_missing = s.is_missing; is_missing = s.is_missing;
@ -131,14 +142,35 @@ public:
bool operator==(const env_var_t &s) const bool operator==(const env_var_t &s) const
{ {
if (is_missing && s.is_missing) return is_missing == s.is_missing && static_cast<const wcstring &>(*this) == static_cast<const wcstring &>(s);
return true;
else if (s.is_missing || s.is_missing)
return false;
else
return *static_cast<const wcstring *>(this) == *static_cast<const wcstring *>(&s);
} }
bool operator==(const wcstring &s) const
{
return ! is_missing && static_cast<const wcstring &>(*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<const wcstring &>(*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. Gets the variable with the specified name, or an empty string if it does not exist.

View file

@ -49,9 +49,9 @@
connection_t env_universal_server; 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 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() static void check_connection()
{ {
if (!init) if (! s_env_univeral_inited)
return; return;
if (env_universal_server.killme) if (env_universal_server.killme)
@ -258,10 +258,10 @@ static void reconnect()
debug(3, L"Get new fishd connection"); 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.buffer_consumed = env_universal_server.buffer_used = 0;
env_universal_server.fd = get_socket(); env_universal_server.fd = get_socket();
init = 1; s_env_univeral_inited = true;
if (env_universal_server.fd >= 0) if (env_universal_server.fd >= 0)
{ {
env_universal_remove_all(); env_universal_remove_all();
@ -285,7 +285,7 @@ void env_universal_init(wchar_t * p,
env_universal_server.fd = get_socket(); env_universal_server.fd = get_socket();
env_universal_common_init(&callback); env_universal_common_init(&callback);
env_universal_read_all(); env_universal_read_all();
init = 1; s_env_univeral_inited = true;
if (env_universal_server.fd >= 0) if (env_universal_server.fd >= 0)
{ {
env_universal_barrier(); env_universal_barrier();
@ -308,8 +308,7 @@ void env_universal_destroy()
connection_destroy(&env_universal_server); connection_destroy(&env_universal_server);
env_universal_server.fd =-1; env_universal_server.fd =-1;
env_universal_common_destroy(); s_env_univeral_inited = false;
init = 0;
} }
@ -318,7 +317,7 @@ void env_universal_destroy()
*/ */
int env_universal_read_all() int env_universal_read_all()
{ {
if (!init) if (! s_env_univeral_inited)
return 0; return 0;
if (env_universal_server.fd == -1) 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) if (!s_env_univeral_inited)
return 0; return NULL;
return env_universal_common_get(name); return env_universal_common_get(name);
} }
bool env_universal_get_export(const wcstring &name) bool env_universal_get_export(const wcstring &name)
{ {
if (!init) if (!s_env_univeral_inited)
return false; return false;
return env_universal_common_get_export(name); return env_universal_common_get_export(name);
@ -363,7 +362,7 @@ void env_universal_barrier()
message_t *msg; message_t *msg;
fd_set fds; fd_set fds;
if (!init || is_dead()) if (!s_env_univeral_inited || is_dead())
return; return;
barrier_reply = 0; barrier_reply = 0;
@ -424,7 +423,7 @@ void env_universal_set(const wcstring &name, const wcstring &value, bool exportv
{ {
message_t *msg; message_t *msg;
if (!init) if (!s_env_univeral_inited)
return; return;
debug(3, L"env_universal_set( \"%ls\", \"%ls\" )", name.c_str(), value.c_str()); 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; int res;
message_t *msg; message_t *msg;
if (!init) if (!s_env_univeral_inited)
return 1; return 1;
CHECK(name, 1); CHECK(name, 1);
@ -481,11 +480,11 @@ int env_universal_remove(const wchar_t *name)
return res; return res;
} }
void env_universal_get_names2(wcstring_list_t &lst, void env_universal_get_names(wcstring_list_t &lst,
bool show_exported, bool show_exported,
bool show_unexported) bool show_unexported)
{ {
if (!init) if (!s_env_univeral_inited)
return; return;
env_universal_common_get_names(lst, env_universal_common_get_names(lst,

View file

@ -29,7 +29,7 @@ void env_universal_destroy();
/** /**
Get the value of a universal variable 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 Get the export flag of the variable with the specified
@ -60,7 +60,7 @@ int env_universal_read_all();
\param show_exported whether exported variables should be shown \param show_exported whether exported variables should be shown
\param show_unexported whether unexported variables should be shown \param show_unexported whether unexported variables should be shown
*/ */
void env_universal_get_names2(wcstring_list_t &list, void env_universal_get_names(wcstring_list_t &list,
bool show_exported, bool show_exported,
bool show_unexported); bool show_unexported);

View file

@ -106,7 +106,7 @@ static void parse_message(wchar_t *msg,
/** /**
The table of all universal variables The table of all universal variables
*/ */
typedef std::map<wcstring, var_uni_entry_t*> env_var_table_t; typedef std::map<wcstring, var_uni_entry_t> env_var_table_t;
env_var_table_t env_universal_var; 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; 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 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) void env_universal_common_remove(const wcstring &name)
{ {
env_var_table_t::iterator result = env_universal_var.find(name); env_universal_var.erase(name);
if (result != env_universal_var.end())
{
var_uni_entry_t* v = result->second;
env_universal_var.erase(result);
delete v;
}
} }
/** /**
@ -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) void env_universal_common_set(const wchar_t *key, const wchar_t *val, bool exportv)
{ {
var_uni_entry_t *entry;
CHECK(key,); CHECK(key,);
CHECK(val,); 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) if (callback)
{ {
callback(exportv?SET_EXPORT:SET, key, val); callback(exportv?SET_EXPORT:SET, key, val);
@ -910,30 +887,26 @@ void env_universal_common_get_names(wcstring_list_t &lst,
for (iter = env_universal_var.begin(); iter != env_universal_var.end(); ++iter) for (iter = env_universal_var.begin(); iter != env_universal_var.end(); ++iter)
{ {
const wcstring &key = iter->first; const wcstring &key = iter->first;
const var_uni_entry_t *e = iter->second; const var_uni_entry_t &e = iter->second;
if ((e->exportv && show_exported) || if ((e.exportv && show_exported) || (! e.exportv && show_unexported))
(!e->exportv && show_unexported))
{ {
lst.push_back(key); 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); env_var_table_t::const_iterator result = env_universal_var.find(name);
if (result != env_universal_var.end()) if (result != env_universal_var.end())
{ {
const var_uni_entry_t *e = result->second; const var_uni_entry_t &e = result->second;
if (e) return const_cast<wchar_t*>(e.val.c_str());
return const_cast<wchar_t*>(e->val.c_str());
} }
return 0; return NULL;
} }
bool env_universal_common_get_export(const wcstring &name) 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); env_var_table_t::const_iterator result = env_universal_var.find(name);
if (result != env_universal_var.end()) if (result != env_universal_var.end())
{ {
const var_uni_entry_t *e = result->second; const var_uni_entry_t &e = result->second;
if (e != NULL) return e.exportv;
return e->exportv;
} }
return false; return false;
} }
@ -951,13 +923,12 @@ bool env_universal_common_get_export(const wcstring &name)
void enqueue_all(connection_t *c) void enqueue_all(connection_t *c)
{ {
env_var_table_t::const_iterator iter; env_var_table_t::const_iterator iter;
for (iter = env_universal_var.begin(); iter != env_universal_var.end(); ++iter) for (iter = env_universal_var.begin(); iter != env_universal_var.end(); ++iter)
{ {
const wcstring &key = iter->first; 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; msg->count=1;
c->unsent->push(msg); c->unsent->push(msg);
} }

View file

@ -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)); 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 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 This function operate agains the local copy of all universal
variables, it does not communicate with any other process. 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 Get the export flag of the variable with the specified

View file

@ -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; const char *env = getenv(key);
wchar_t *res; if (env != NULL)
nkey = wcs2str(key);
nres = getenv(nkey);
free(nkey);
if (nres)
{ {
wcstring tmp = str2wcstring(nres); return env_var_t(str2wcstring(env));
return wcsdup(tmp.c_str());
} }
else else
{ {
res = env_universal_common_get(key); const wcstring wkey = str2wcstring(key);
if (res) const wchar_t *tmp = env_universal_common_get(wkey);
res = wcsdup(res); return tmp ? env_var_t(tmp) : env_var_t::missing_var();
return res;
} }
} }
@ -740,12 +733,11 @@ static wchar_t *fishd_env_get(const wchar_t *key)
*/ */
static wcstring fishd_get_config() static wcstring fishd_get_config()
{ {
wchar_t *xdg_dir, *home;
bool done = false; bool done = false;
wcstring result; wcstring result;
xdg_dir = fishd_env_get(L"XDG_CONFIG_HOME"); env_var_t xdg_dir = fishd_env_get("XDG_CONFIG_HOME");
if (xdg_dir) if (! xdg_dir.missing_or_empty())
{ {
result = xdg_dir; result = xdg_dir;
append_path_component(result, L"/fish"); append_path_component(result, L"/fish");
@ -753,12 +745,11 @@ static wcstring fishd_get_config()
{ {
done = true; done = true;
} }
free(xdg_dir);
} }
else else
{ {
home = fishd_env_get(L"HOME"); env_var_t home = fishd_env_get("HOME");
if (home) if (! home.missing_or_empty())
{ {
result = home; result = home;
append_path_component(result, L"/.config/fish"); append_path_component(result, L"/.config/fish");
@ -766,7 +757,6 @@ static wcstring fishd_get_config()
{ {
done = 1; done = 1;
} }
free(home);
} }
} }
@ -1100,7 +1090,6 @@ int main(int argc, char ** argv)
{ {
debug(0, L"No more clients. Quitting"); debug(0, L"No more clients. Quitting");
save(); save();
env_universal_common_destroy();
break; break;
} }