From 6c8bf642a4118f45647076abc3368037163befb4 Mon Sep 17 00:00:00 2001 From: Trent Huber Date: Sat, 6 Sep 2025 02:56:40 -0400 Subject: [PATCH] Slight refactoring to bg.c --- src/builtins/bg.c | 18 +++++++++--------- src/builtins/bg.h | 10 +++------- src/builtins/fg.c | 2 +- src/run.c | 4 ++-- 4 files changed, 15 insertions(+), 19 deletions(-) diff --git a/src/builtins/bg.c b/src/builtins/bg.c index 6c50e1c..2f78950 100644 --- a/src/builtins/bg.c +++ b/src/builtins/bg.c @@ -29,14 +29,14 @@ void initbg(void) { bgjobs.free = bgjobs.entries; } -int fullbg(void) { +int bgfull(void) { return !bgjobs.free; } int pushbg(struct bgjob job) { struct bglink *p; - if (fullbg()) return 0; + if (bgfull()) return 0; (p = bgjobs.free)->job = job; bgjobs.free = p->next; @@ -46,6 +46,10 @@ int pushbg(struct bgjob job) { return 1; } +int pushbgid(pid_t id) { + return pushbg((struct bgjob){.id = id, .config = canonical}); +} + int peekbg(struct bgjob *job) { if (bgjobs.active && job) *job = bgjobs.active->job; @@ -63,11 +67,7 @@ int searchbg(pid_t id, struct bgjob *job) { return 0; } -int pushid(pid_t id) { - return pushbg((struct bgjob){.id = id, .config = canonical}); -} - -void removeid(pid_t id) { +void removebg(pid_t id) { struct bglink *p, *prev; for (prev = NULL, p = bgjobs.active; p; prev = p, p = p->next) @@ -96,7 +96,7 @@ void waitbg(int sig) { if (id == -1) { id = p->job.id; p = p->next; - removeid(id); + removebg(id); } else p = p->next; } errno = e; @@ -147,7 +147,7 @@ BUILTIN(bg) { note("Unable to wake up suspended job %d", job.id); return EXIT_FAILURE; } - removeid(job.id); + removebg(job.id); job.suspended = 0; pushbg(job); diff --git a/src/builtins/bg.h b/src/builtins/bg.h index 6b6c135..b933134 100644 --- a/src/builtins/bg.h +++ b/src/builtins/bg.h @@ -5,15 +5,11 @@ struct bgjob { }; void initbg(void); - -int fullbg(void); +int bgfull(void); int pushbg(struct bgjob job); +int pushbgid(pid_t id); int peekbg(struct bgjob *job); int searchbg(pid_t id, struct bgjob *job); - -int pushid(pid_t id); -void removeid(pid_t id); - +void removebg(pid_t id); void waitbg(int sig); - void deinitbg(void); diff --git a/src/builtins/fg.c b/src/builtins/fg.c index 70fe125..6da9a07 100644 --- a/src/builtins/fg.c +++ b/src/builtins/fg.c @@ -77,7 +77,7 @@ int runfg(pid_t id) { note("Unable to wake up job %d", id); return 0; } - removeid(id); + removebg(id); /* SIGCHLD handler is really the function that reaps the foreground process, * the waitpid() below is just to block the current thread of execution until diff --git a/src/run.c b/src/run.c index c896062..a0452e2 100644 --- a/src/run.c +++ b/src/run.c @@ -77,7 +77,7 @@ int run(struct context *c) { islist = c->prev.term > BG || c->current.term > BG; if (c->t) { - if (c->current.term == BG && fullbg()) { + if (c->current.term == BG && bgfull()) { note("Unable to place job in background, too many background jobs"); return quit(c); } @@ -135,7 +135,7 @@ int run(struct context *c) { return quit(c); } if (ispipestart || c->current.term == BG) { - pushid(jobid); + pushbgid(jobid); return 1; } if (c->current.term != PIPE && !runfg(jobid)) return quit(c); -- 2.51.0