If a #() command doesn't exit, use its most recent line of output (it

must be a full line). Don't let it redraw the status line more than once
a second.

Requested by someone about 10 years ago...
This commit is contained in:
nicm
2017-04-20 09:20:22 +00:00
parent f184c6f06c
commit 0b44ad99b5
9 changed files with 106 additions and 46 deletions

49
job.c
View File

@ -33,8 +33,9 @@
* output.
*/
static void job_callback(struct bufferevent *, short, void *);
static void job_read_callback(struct bufferevent *, void *);
static void job_write_callback(struct bufferevent *, void *);
static void job_error_callback(struct bufferevent *, short, void *);
/* All jobs list. */
struct joblist all_jobs = LIST_HEAD_INITIALIZER(all_jobs);
@ -42,7 +43,8 @@ struct joblist all_jobs = LIST_HEAD_INITIALIZER(all_jobs);
/* Start a job running, if it isn't already. */
struct job *
job_run(const char *cmd, struct session *s, const char *cwd,
void (*callbackfn)(struct job *), void (*freefn)(void *), void *data)
job_update_cb updatecb, job_complete_cb completecb, job_free_cb freecb,
void *data)
{
struct job *job;
struct environ *env;
@ -104,17 +106,18 @@ job_run(const char *cmd, struct session *s, const char *cwd,
job->pid = pid;
job->status = 0;
LIST_INSERT_HEAD(&all_jobs, job, lentry);
LIST_INSERT_HEAD(&all_jobs, job, entry);
job->callbackfn = callbackfn;
job->freefn = freefn;
job->updatecb = updatecb;
job->completecb = completecb;
job->freecb = freecb;
job->data = data;
job->fd = out[0];
setblocking(job->fd, 0);
job->event = bufferevent_new(job->fd, NULL, job_write_callback,
job_callback, job);
job->event = bufferevent_new(job->fd, job_read_callback,
job_write_callback, job_error_callback, job);
bufferevent_enable(job->event, EV_READ|EV_WRITE);
log_debug("run job %p: %s, pid %ld", job, job->cmd, (long) job->pid);
@ -127,11 +130,11 @@ job_free(struct job *job)
{
log_debug("free job %p: %s", job, job->cmd);
LIST_REMOVE(job, lentry);
LIST_REMOVE(job, entry);
free(job->cmd);
if (job->freefn != NULL && job->data != NULL)
job->freefn(job->data);
if (job->freecb != NULL && job->data != NULL)
job->freecb(job->data);
if (job->pid != -1)
kill(job->pid, SIGTERM);
@ -143,7 +146,21 @@ job_free(struct job *job)
free(job);
}
/* Called when output buffer falls below low watermark (default is 0). */
/* Job buffer read callback. */
static void
job_read_callback(__unused struct bufferevent *bufev, void *data)
{
struct job *job = data;
if (job->updatecb != NULL)
job->updatecb (job);
}
/*
* Job buffer write callback. Fired when the buffer falls below watermark
* (default is empty). If all the data has been written, disable the write
* event.
*/
static void
job_write_callback(__unused struct bufferevent *bufev, void *data)
{
@ -161,7 +178,7 @@ job_write_callback(__unused struct bufferevent *bufev, void *data)
/* Job buffer error callback. */
static void
job_callback(__unused struct bufferevent *bufev, __unused short events,
job_error_callback(__unused struct bufferevent *bufev, __unused short events,
void *data)
{
struct job *job = data;
@ -169,8 +186,8 @@ job_callback(__unused struct bufferevent *bufev, __unused short events,
log_debug("job error %p: %s, pid %ld", job, job->cmd, (long) job->pid);
if (job->state == JOB_DEAD) {
if (job->callbackfn != NULL)
job->callbackfn(job);
if (job->completecb != NULL)
job->completecb(job);
job_free(job);
} else {
bufferevent_disable(job->event, EV_READ);
@ -187,8 +204,8 @@ job_died(struct job *job, int status)
job->status = status;
if (job->state == JOB_CLOSED) {
if (job->callbackfn != NULL)
job->callbackfn(job);
if (job->completecb != NULL)
job->completecb(job);
job_free(job);
} else {
job->pid = -1;