mirror of
https://github.com/fish-shell/fish-shell.git
synced 2024-11-25 18:03:37 +08:00
Eliminate narrow_string_rep_t
This was used to cache a narrow string representation of commands, so that if certain system calls returned errors after fork, we could output error messages without allocating memory. But in practice these errors are very uncommon, as are commands that have wide characters. It is simpler to do a best-effort output of the wide string, instead of caching a narrow string unconditionally.
This commit is contained in:
parent
3633c51ad8
commit
9151ec7092
|
@ -795,6 +795,24 @@ void format_long_safe(wchar_t buff[64], long val)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void narrow_string_safe(char buff[64], const wchar_t *s)
|
||||||
|
{
|
||||||
|
size_t idx = 0;
|
||||||
|
for (size_t widx = 0; s[widx] != L'\0'; widx++)
|
||||||
|
{
|
||||||
|
wchar_t c = s[widx];
|
||||||
|
if (c <= 127)
|
||||||
|
{
|
||||||
|
buff[idx++] = char(c);
|
||||||
|
if (idx + 1 == 64)
|
||||||
|
{
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
buff[idx] = '\0';
|
||||||
|
}
|
||||||
|
|
||||||
wcstring reformat_for_screen(const wcstring &msg)
|
wcstring reformat_for_screen(const wcstring &msg)
|
||||||
{
|
{
|
||||||
wcstring buff;
|
wcstring buff;
|
||||||
|
|
33
src/common.h
33
src/common.h
|
@ -396,6 +396,9 @@ void debug_safe(int level, const char *msg, const char *param1 = NULL, const cha
|
||||||
void format_long_safe(char buff[64], long val);
|
void format_long_safe(char buff[64], long val);
|
||||||
void format_long_safe(wchar_t buff[64], long val);
|
void format_long_safe(wchar_t buff[64], long val);
|
||||||
|
|
||||||
|
/** "Narrows" a wide character string. This just grabs any ASCII characters and trunactes. */
|
||||||
|
void narrow_string_safe(char buff[64], const wchar_t *s);
|
||||||
|
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
T from_string(const wcstring &x)
|
T from_string(const wcstring &x)
|
||||||
|
@ -526,36 +529,6 @@ public:
|
||||||
/* Helper function to convert from a null_terminated_array_t<wchar_t> to a null_terminated_array_t<char_t> */
|
/* Helper function to convert from a null_terminated_array_t<wchar_t> to a null_terminated_array_t<char_t> */
|
||||||
void convert_wide_array_to_narrow(const null_terminated_array_t<wchar_t> &arr, null_terminated_array_t<char> *output);
|
void convert_wide_array_to_narrow(const null_terminated_array_t<wchar_t> &arr, null_terminated_array_t<char> *output);
|
||||||
|
|
||||||
/* Helper class to cache a narrow version of a wcstring in a malloc'd buffer, so that we can read it after fork() */
|
|
||||||
class narrow_string_rep_t
|
|
||||||
{
|
|
||||||
private:
|
|
||||||
const char *str;
|
|
||||||
|
|
||||||
/* No copying */
|
|
||||||
narrow_string_rep_t &operator=(const narrow_string_rep_t &);
|
|
||||||
narrow_string_rep_t(const narrow_string_rep_t &x);
|
|
||||||
|
|
||||||
public:
|
|
||||||
~narrow_string_rep_t()
|
|
||||||
{
|
|
||||||
free((void *)str);
|
|
||||||
}
|
|
||||||
|
|
||||||
narrow_string_rep_t() : str(NULL) {}
|
|
||||||
|
|
||||||
void set(const wcstring &s)
|
|
||||||
{
|
|
||||||
free((void *)str);
|
|
||||||
str = wcs2str(s.c_str());
|
|
||||||
}
|
|
||||||
|
|
||||||
const char *get() const
|
|
||||||
{
|
|
||||||
return str;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
bool is_forked_child();
|
bool is_forked_child();
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -56,7 +56,6 @@ static void debug_safe_int(int level, const char *format, int val)
|
||||||
debug_safe(level, format, buff);
|
debug_safe(level, format, buff);
|
||||||
}
|
}
|
||||||
|
|
||||||
// PCA These calls to debug are rather sketchy because they may allocate memory. Fortunately they only occur if an error occurs.
|
|
||||||
int set_child_group(job_t *j, process_t *p, int print_errors)
|
int set_child_group(job_t *j, process_t *p, int print_errors)
|
||||||
{
|
{
|
||||||
int res = 0;
|
int res = 0;
|
||||||
|
@ -76,18 +75,22 @@ int set_child_group(job_t *j, process_t *p, int print_errors)
|
||||||
char job_id_buff[128];
|
char job_id_buff[128];
|
||||||
char getpgid_buff[128];
|
char getpgid_buff[128];
|
||||||
char job_pgid_buff[128];
|
char job_pgid_buff[128];
|
||||||
|
char argv0[64];
|
||||||
|
char command[64];
|
||||||
|
|
||||||
format_long_safe(pid_buff, p->pid);
|
format_long_safe(pid_buff, p->pid);
|
||||||
format_long_safe(job_id_buff, j->job_id);
|
format_long_safe(job_id_buff, j->job_id);
|
||||||
format_long_safe(getpgid_buff, getpgid(p->pid));
|
format_long_safe(getpgid_buff, getpgid(p->pid));
|
||||||
format_long_safe(job_pgid_buff, j->pgid);
|
format_long_safe(job_pgid_buff, j->pgid);
|
||||||
|
narrow_string_safe(argv0, p->argv0());
|
||||||
|
narrow_string_safe(command, j->command_wcstr());
|
||||||
|
|
||||||
debug_safe(1,
|
debug_safe(1,
|
||||||
"Could not send process %s, '%s' in job %s, '%s' from group %s to group %s",
|
"Could not send process %s, '%s' in job %s, '%s' from group %s to group %s",
|
||||||
pid_buff,
|
pid_buff,
|
||||||
p->argv0_cstr(),
|
argv0,
|
||||||
job_id_buff,
|
job_id_buff,
|
||||||
j->command_cstr(),
|
command,
|
||||||
getpgid_buff,
|
getpgid_buff,
|
||||||
job_pgid_buff);
|
job_pgid_buff);
|
||||||
|
|
||||||
|
@ -105,9 +108,11 @@ int set_child_group(job_t *j, process_t *p, int print_errors)
|
||||||
{
|
{
|
||||||
if (tcsetpgrp(0, j->pgid) && print_errors)
|
if (tcsetpgrp(0, j->pgid) && print_errors)
|
||||||
{
|
{
|
||||||
char job_id_buff[128];
|
char job_id_buff[64];
|
||||||
|
char command_buff[64];
|
||||||
format_long_safe(job_id_buff, j->job_id);
|
format_long_safe(job_id_buff, j->job_id);
|
||||||
debug_safe(1, "Could not send job %s ('%s') to foreground", job_id_buff, j->command_cstr());
|
narrow_string_safe(command_buff, j->command_wcstr());
|
||||||
|
debug_safe(1, "Could not send job %s ('%s') to foreground", job_id_buff, command_buff);
|
||||||
safe_perror("tcsetpgrp");
|
safe_perror("tcsetpgrp");
|
||||||
res = -1;
|
res = -1;
|
||||||
}
|
}
|
||||||
|
|
|
@ -453,11 +453,7 @@ static void handle_child_status(pid_t pid, int status)
|
||||||
}
|
}
|
||||||
|
|
||||||
process_t::process_t() :
|
process_t::process_t() :
|
||||||
argv_array(),
|
|
||||||
argv0_narrow(),
|
|
||||||
type(),
|
|
||||||
internal_block_node(NODE_OFFSET_INVALID),
|
internal_block_node(NODE_OFFSET_INVALID),
|
||||||
actual_cmd(),
|
|
||||||
pid(0),
|
pid(0),
|
||||||
pipe_write_fd(0),
|
pipe_write_fd(0),
|
||||||
pipe_read_fd(0),
|
pipe_read_fd(0),
|
||||||
|
@ -480,8 +476,6 @@ process_t::~process_t()
|
||||||
}
|
}
|
||||||
|
|
||||||
job_t::job_t(job_id_t jobid, const io_chain_t &bio) :
|
job_t::job_t(job_id_t jobid, const io_chain_t &bio) :
|
||||||
command_str(),
|
|
||||||
command_narrow(),
|
|
||||||
block_io(bio),
|
block_io(bio),
|
||||||
first_process(NULL),
|
first_process(NULL),
|
||||||
pgid(0),
|
pgid(0),
|
||||||
|
|
22
src/proc.h
22
src/proc.h
|
@ -123,9 +123,6 @@ private:
|
||||||
|
|
||||||
null_terminated_array_t<wchar_t> argv_array;
|
null_terminated_array_t<wchar_t> argv_array;
|
||||||
|
|
||||||
/* narrow copy of argv0 so we don't have to convert after fork */
|
|
||||||
narrow_string_rep_t argv0_narrow;
|
|
||||||
|
|
||||||
io_chain_t process_io_chain;
|
io_chain_t process_io_chain;
|
||||||
|
|
||||||
/* No copying */
|
/* No copying */
|
||||||
|
@ -151,7 +148,6 @@ public:
|
||||||
void set_argv(const wcstring_list_t &argv)
|
void set_argv(const wcstring_list_t &argv)
|
||||||
{
|
{
|
||||||
argv_array.set(argv);
|
argv_array.set(argv);
|
||||||
argv0_narrow.set(argv.empty() ? L"" : argv[0]);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Returns argv */
|
/** Returns argv */
|
||||||
|
@ -173,18 +169,12 @@ public:
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Returns argv[0], or NULL */
|
/** Returns argv[0], or NULL */
|
||||||
const wchar_t *argv0(void) const
|
const wchar_t *argv0() const
|
||||||
{
|
{
|
||||||
const wchar_t * const *argv = argv_array.get();
|
const wchar_t * const *argv = argv_array.get();
|
||||||
return argv ? argv[0] : NULL;
|
return argv ? argv[0] : NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Returns argv[0] as a char * */
|
|
||||||
const char *argv0_cstr(void) const
|
|
||||||
{
|
|
||||||
return argv0_narrow.get();
|
|
||||||
}
|
|
||||||
|
|
||||||
/* IO chain getter and setter */
|
/* IO chain getter and setter */
|
||||||
const io_chain_t &io_chain() const
|
const io_chain_t &io_chain() const
|
||||||
{
|
{
|
||||||
|
@ -278,9 +268,6 @@ class job_t
|
||||||
*/
|
*/
|
||||||
wcstring command_str;
|
wcstring command_str;
|
||||||
|
|
||||||
/* narrow copy so we don't have to convert after fork */
|
|
||||||
narrow_string_rep_t command_narrow;
|
|
||||||
|
|
||||||
/* The IO chain associated with the block */
|
/* The IO chain associated with the block */
|
||||||
const io_chain_t block_io;
|
const io_chain_t block_io;
|
||||||
|
|
||||||
|
@ -311,17 +298,10 @@ public:
|
||||||
return command_str;
|
return command_str;
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Returns the command as a char *. */
|
|
||||||
const char *command_cstr() const
|
|
||||||
{
|
|
||||||
return command_narrow.get();
|
|
||||||
}
|
|
||||||
|
|
||||||
/** Sets the command */
|
/** Sets the command */
|
||||||
void set_command(const wcstring &cmd)
|
void set_command(const wcstring &cmd)
|
||||||
{
|
{
|
||||||
command_str = cmd;
|
command_str = cmd;
|
||||||
command_narrow.set(cmd);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
Loading…
Reference in New Issue
Block a user