use ShellManager in do_pipe

This commit is contained in:
Maxime Coste 2012-05-03 07:33:13 +00:00
parent 0c596a9d64
commit 7158aa00b7

View File

@ -580,44 +580,9 @@ void do_pipe(Editor& editor, int count)
editor.buffer().begin_undo_group(); editor.buffer().begin_undo_group();
for (auto& sel : const_cast<const Editor&>(editor).selections()) for (auto& sel : const_cast<const Editor&>(editor).selections())
{ {
int write_pipe[2]; String new_content = ShellManager::instance().eval(cmdline, main_context);
int read_pipe[2]; editor.buffer().modify(Modification::make_erase(sel.begin(), sel.end()));
editor.buffer().modify(Modification::make_insert(sel.begin(), new_content));
pipe(write_pipe);
pipe(read_pipe);
if (pid_t pid = fork())
{
close(write_pipe[0]);
close(read_pipe[1]);
String content = editor.buffer().string(sel.begin(), sel.end());
memoryview<char> data = content.data();
write(write_pipe[1], data.pointer(), data.size());
close(write_pipe[1]);
String new_content;
char buffer[1024];
while (size_t size = read(read_pipe[0], buffer, 1024))
{
new_content += String(buffer, buffer+size);
}
close(read_pipe[0]);
waitpid(pid, NULL, 0);
editor.buffer().modify(Modification::make_erase(sel.begin(), sel.end()));
editor.buffer().modify(Modification::make_insert(sel.begin(), new_content));
}
else
{
close(write_pipe[1]);
close(read_pipe[0]);
dup2(read_pipe[1], 1);
dup2(write_pipe[0], 0);
execlp("sh", "sh", "-c", cmdline.c_str(), NULL);
}
} }
editor.buffer().end_undo_group(); editor.buffer().end_undo_group();
} }