Use read_fd to get pipe command from stdin instead of ad-hoc code
read_fd will also now throw on read error instead of just returning the data read so far as if nothing failed.
This commit is contained in:
parent
398b2b115c
commit
250886a9e1
10
src/file.cc
10
src/file.cc
|
@ -41,6 +41,9 @@ public:
|
|||
file_access_error(StringView filename,
|
||||
StringView error_desc)
|
||||
: runtime_error(format("{}: {}", filename, error_desc)) {}
|
||||
|
||||
file_access_error(int fd, StringView error_desc)
|
||||
: runtime_error(format("fd {}: {}", fd, error_desc)) {}
|
||||
};
|
||||
|
||||
String parse_filename(StringView filename)
|
||||
|
@ -168,11 +171,10 @@ String read_fd(int fd, bool text)
|
|||
String content;
|
||||
constexpr size_t bufsize = 256;
|
||||
char buf[bufsize];
|
||||
while (true)
|
||||
while (ssize_t size = read(fd, buf, bufsize))
|
||||
{
|
||||
ssize_t size = read(fd, buf, bufsize);
|
||||
if (size == -1 or size == 0)
|
||||
break;
|
||||
if (size == -1)
|
||||
throw file_access_error{fd, strerror(errno)};
|
||||
|
||||
if (text)
|
||||
{
|
||||
|
|
13
src/main.cc
13
src/main.cc
|
@ -744,20 +744,9 @@ int run_filter(StringView keystr, StringView commands, ConstArrayView<StringView
|
|||
|
||||
int run_pipe(StringView session)
|
||||
{
|
||||
char buf[512];
|
||||
String command;
|
||||
while (ssize_t count = read(0, buf, 512))
|
||||
{
|
||||
if (count < 0)
|
||||
{
|
||||
write_stderr("error while reading stdin\n");
|
||||
return -1;
|
||||
}
|
||||
command += StringView{buf, buf + count};
|
||||
}
|
||||
try
|
||||
{
|
||||
send_command(session, command);
|
||||
send_command(session, read_fd(0));
|
||||
}
|
||||
catch (disconnected& e)
|
||||
{
|
||||
|
|
Loading…
Reference in New Issue
Block a user