From: Michael D. Lowis Date: Tue, 8 May 2018 15:24:03 +0000 (-0400) Subject: implemented pipe_write() to allow filtering text through external commands X-Git-Url: https://git.mdlowis.com/?a=commitdiff_plain;h=895b6e9388a5a113f440dcaee8c5d86f1e864e91;p=projs%2Ftide.git implemented pipe_write() to allow filtering text through external commands --- diff --git a/lib/job.c b/lib/job.c index 1199302..e9c3459 100644 --- a/lib/job.c +++ b/lib/job.c @@ -34,7 +34,19 @@ static void pipe_read(Job* job) { } static void pipe_write(Job* job) { - job->writefn = NULL; + struct PipeData* pipedata = job->data; + char* chunk = pipedata->data + pipedata->nwrite; + long nwrite = write(job->fd, chunk, pipedata->ndata); + if (nwrite >= 0) { + pipedata->ndata -= nwrite; + pipedata->nwrite += nwrite; + } + if (nwrite < 0 || pipedata->ndata <= 0) { + free(pipedata->data); + pipedata->data = NULL; + job->writefn = NULL; + shutdown(job->fd, SHUT_WR); + } } static void job_finish(Job* job) { @@ -57,8 +69,6 @@ static void job_process(int fd, int events) { while (job && job->fd != fd) job = job->next; if (!job) return; - //if (fd > 3) - // printf("%d -> %d %d\n", fd, (events & POLLIN) > 0, (events & POLLOUT) > 0); if (job->readfn && (events & POLLIN)) job->readfn(job); if (job->writefn && (events & POLLOUT))