Skip to content

Commit

Permalink
Eliminate str2wcs
Browse files Browse the repository at this point in the history
  • Loading branch information
ridiculousfish committed Dec 19, 2012
1 parent b0a9a5a commit 644607c
Show file tree
Hide file tree
Showing 16 changed files with 161 additions and 210 deletions.
7 changes: 3 additions & 4 deletions builtin.cpp
Expand Up @@ -188,12 +188,11 @@ static void builtin_wperror(const wchar_t *s)
stderr_buffer.append(L": ");
}
char *err = strerror(errno);
wchar_t *werr = str2wcs(err);
if (werr)
if (err)
{
const wcstring werr = str2wcstring(err);
stderr_buffer.append(werr);
stderr_buffer.push_back(L'\n');
free(werr);
}
}

Expand Down Expand Up @@ -3065,7 +3064,7 @@ static int builtin_source(parser_t &parser, wchar_t ** argv)
return STATUS_BUILTIN_ERROR;
}

fn = wrealpath(argv[1], 0);
fn = wrealpath(argv[1], NULL);

if (!fn)
{
Expand Down
46 changes: 31 additions & 15 deletions common.cpp
Expand Up @@ -81,6 +81,7 @@ parts of fish.
#include "fallback.cpp"


static wchar_t *str2wcs_internal(const char *in, const size_t in_len, wchar_t *out);

struct termios shell_modes;

Expand Down Expand Up @@ -163,23 +164,31 @@ int fgetws2(wcstring *s, FILE *f)
}
}

wchar_t *str2wcs(const char *in)
static wchar_t *str2wcs(const char *in)
{
wchar_t *out;
size_t len = strlen(in);

out = (wchar_t *)malloc(sizeof(wchar_t)*(len+1));

wchar_t *out = (wchar_t *)malloc(sizeof(wchar_t)*(len+1));
if (!out)
{
DIE_MEM();
}

return str2wcs_internal(in, out);
return str2wcs_internal(in, strlen(in), out);
}

wcstring str2wcstring(const char *in, size_t len)
{
assert(in != NULL);
std::string tmp_str(in, len);
wchar_t *tmp = str2wcs(tmp_str.c_str());
wcstring result = tmp;
free(tmp);
return result;
}

wcstring str2wcstring(const char *in)
{
assert(in != NULL);
wchar_t *tmp = str2wcs(in);
wcstring result = tmp;
free(tmp);
Expand All @@ -194,24 +203,31 @@ wcstring str2wcstring(const std::string &in)
return result;
}

wchar_t *str2wcs_internal(const char *in, wchar_t *out)
/**
Converts the narrow character string \c in into it's wide
equivalent, stored in \c out. \c out must have enough space to fit
the entire string.
The string may contain embedded nulls.
This function encodes illegal character sequences in a reversible
way using the private use area.
*/
static wchar_t *str2wcs_internal(const char *in, const size_t in_len, wchar_t *out)
{
size_t res=0;
size_t in_pos=0;
size_t out_pos = 0;
mbstate_t state;
size_t len;

CHECK(in, 0);
CHECK(out, 0);

len = strlen(in);

memset(&state, 0, sizeof(state));

while (in[in_pos])
{
res = mbrtowc(&out[out_pos], &in[in_pos], len-in_pos, &state);
res = mbrtowc(&out[out_pos], &in[in_pos], in_len-in_pos, &state);

if (((out[out_pos] >= ENCODE_DIRECT_BASE) &&
(out[out_pos] < ENCODE_DIRECT_BASE+256)) ||
Expand Down Expand Up @@ -298,12 +314,12 @@ std::string wcs2string(const wcstring &input)
{
std::string result;
result.reserve(input.size());

mbstate_t state;
memset(&state, 0, sizeof(state));

char converted[MB_LEN_MAX + 1];

for (size_t i=0; i < input.size(); i++)
{
wchar_t wc = input[i];
Expand All @@ -330,7 +346,7 @@ std::string wcs2string(const wcstring &input)
}
}
}

return result;
}

Expand Down
21 changes: 2 additions & 19 deletions common.h
Expand Up @@ -206,35 +206,18 @@ void show_stackframe();
*/
int fgetws2(wcstring *s, FILE *f);

/**
Returns a newly allocated wide character string equivalent of the
specified multibyte character string
This function encodes illegal character sequences in a reversible
way using the private use area.
*/
wchar_t *str2wcs(const char *in);

/**
Returns a newly allocated wide character string equivalent of the
Returns a wide character string equivalent of the
specified multibyte character string
This function encodes illegal character sequences in a reversible
way using the private use area.
*/
wcstring str2wcstring(const char *in);
wcstring str2wcstring(const char *in, size_t len);
wcstring str2wcstring(const std::string &in);

/**
Converts the narrow character string \c in into it's wide
equivalent, stored in \c out. \c out must have enough space to fit
the entire string.
This function encodes illegal character sequences in a reversible
way using the private use area.
*/
wchar_t *str2wcs_internal(const char *in, wchar_t *out);

/**
Returns a newly allocated multibyte character string equivalent of
the specified wide character string
Expand Down
8 changes: 3 additions & 5 deletions complete.cpp
Expand Up @@ -1719,17 +1719,16 @@ bool completer_t::try_complete_user(const wcstring &str)
while ((pw=getpwent()) != 0)
{
double current_time = timef();
wchar_t *pw_name;

if (current_time - start_time > 0.2)
{
return 1;
}

pw_name = str2wcs(pw->pw_name);

if (pw_name)
if (pw->pw_name)
{
const wcstring pw_name_str = str2wcstring(pw->pw_name);
const wchar_t *pw_name = pw_name_str.c_str();
if (wcsncmp(user_name, pw_name, name_len)==0)
{
wcstring desc = format_string(COMPLETE_USER_DESC, pw_name);
Expand All @@ -1751,7 +1750,6 @@ bool completer_t::try_complete_user(const wcstring &str)
COMPLETE_NO_CASE | COMPLETE_DONT_ESCAPE | COMPLETE_NO_SPACE);
res=1;
}
free(pw_name);
}
}
endpwent();
Expand Down
74 changes: 28 additions & 46 deletions env.cpp
Expand Up @@ -518,19 +518,23 @@ static void env_set_defaults()
if (env_get_string(L"USER").missing())
{
struct passwd *pw = getpwuid(getuid());
wchar_t *unam = str2wcs(pw->pw_name);
env_set(L"USER", unam, ENV_GLOBAL);
free(unam);
if (pw->pw_name != NULL)
{
const wcstring wide_name = str2wcstring(pw->pw_name);
env_set(L"USER", NULL, ENV_GLOBAL);
}
}

if (env_get_string(L"HOME").missing())
{
const env_var_t 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);
free(dir);
if (pw->pw_dir != NULL)
{
const wcstring dir = str2wcstring(pw->pw_dir);
env_set(L"HOME", dir.c_str(), ENV_GLOBAL);
}
free(unam_narrow);
}

Expand All @@ -539,9 +543,9 @@ static void env_set_defaults()
}

// Some variables should not be arrays. This used to be handled by a startup script, but we'd like to get down to 0 forks for startup, so handle it here.
static bool variable_can_be_array(const wchar_t *key)
static bool variable_can_be_array(const wcstring &key)
{
if (! wcscmp(key, L"DISPLAY"))
if (key == L"DISPLAY")
{
return false;
}
Expand All @@ -554,9 +558,6 @@ static bool variable_can_be_array(const wchar_t *key)
void env_init(const struct config_paths_t *paths /* or NULL */)
{
char **p;
struct passwd *pw;
wchar_t *uname;
wchar_t *version;

/*
env_read_only variables can not be altered directly by the user
Expand Down Expand Up @@ -610,41 +611,24 @@ void env_init(const struct config_paths_t *paths /* or NULL */)
*/
for (p=environ?environ:__environ; p && *p; p++)
{
wchar_t *key, *val;

key = str2wcs(*p);

if (!key)
const wcstring key_and_val = str2wcstring(*p); //like foo=bar
size_t eql = key_and_val.find(L'=');
if (eql == wcstring::npos)
{
continue;
}

val = wcschr(key, L'=');

if (val == 0)
{
env_set(key, L"", ENV_EXPORT);
// no equals found
env_set(key_and_val, L"", ENV_EXPORT);
}
else
{
*val = L'\0';
val++;

//fwprintf( stderr, L"Set $%ls to %ls\n", key, val );
wcstring key = key_and_val.substr(0, eql);
wcstring val = key_and_val.substr(eql + 1);
if (variable_can_be_array(val))
{
for (size_t i=0; val[i] != L'\0'; i++)
{
if (val[i] == L':')
{
val[i] = ARRAY_SEP;
}
}
std::replace(val.begin(), val.end(), L':', ARRAY_SEP);
}

env_set(key, val, ENV_EXPORT | ENV_GLOBAL);
env_set(key, val.c_str(), ENV_EXPORT | ENV_GLOBAL);
}
free(key);
}

/* Set the given paths in the environment, if we have any */
Expand All @@ -664,21 +648,19 @@ void env_init(const struct config_paths_t *paths /* or NULL */)
/*
Set up the USER variable
*/
pw = getpwuid(getuid());
if (pw)
const struct passwd *pw = getpwuid(getuid());
if (pw && pw->pw_name)
{
uname = str2wcs(pw->pw_name);
env_set(L"USER", uname, ENV_GLOBAL | ENV_EXPORT);
free(uname);
const wcstring uname = str2wcstring(pw->pw_name);
env_set(L"USER", uname.c_str(), ENV_GLOBAL | ENV_EXPORT);
}

/*
Set up the version variables
*/
version = str2wcs(PACKAGE_VERSION);
env_set(L"version", version, ENV_GLOBAL);
env_set(L"FISH_VERSION", version, ENV_GLOBAL);
free(version);
wcstring version = str2wcstring(PACKAGE_VERSION);
env_set(L"version", version.c_str(), ENV_GLOBAL);
env_set(L"FISH_VERSION", version.c_str(), ENV_GLOBAL);

const env_var_t fishd_dir_wstr = env_get_string(L"FISHD_SOCKET_DIR");
const env_var_t user_dir_wstr = env_get_string(L"USER");
Expand Down
34 changes: 9 additions & 25 deletions exec.cpp
Expand Up @@ -1211,12 +1211,12 @@ void exec(parser_t &parser, job_t *j)

/* Get the strings we'll write before we fork (since they call malloc) */
const wcstring &out = get_stdout_buffer(), &err = get_stderr_buffer();

/* These strings may contain embedded nulls, so don't treat them as C strings */
const std::string outbuff_str = wcs2string(out);
const char *outbuff = outbuff_str.data();
size_t outbuff_len = outbuff_str.size();

const std::string errbuff_str = wcs2string(err);
const char *errbuff = errbuff_str.data();
size_t errbuff_len = errbuff_str.size();
Expand Down Expand Up @@ -1466,45 +1466,29 @@ static int exec_subshell_internal(const wcstring &cmd, wcstring_list_t *lst)

begin=end=io_buffer->out_buffer_ptr();

//REWRITEME

if (lst)
{
while (1)
{
if (*end == 0)
{
assert(begin != NULL);
if (begin != end)
{
wchar_t *el = str2wcs(begin);
if (el)
{
lst->push_back(el);

free(el);
}
else
{
debug(2, L"Got null string on line %d of file %s", __LINE__, __FILE__);
}
const wcstring el = str2wcstring(begin);
lst->push_back(el);
}
io_buffer_destroy(io_buffer);

return status;
}
else if (*end == sep)
{
wchar_t *el;
*end=0;
el = str2wcs(begin);
if (el)
{
lst->push_back(el);

free(el);
}
else
{
debug(2, L"Got null string on line %d of file %s", __LINE__, __FILE__);
}
const wcstring el = str2wcstring(begin);
lst->push_back(el);
begin = end+1;
}
end++;
Expand Down

0 comments on commit 644607c

Please sign in to comment.