~emersion/mrsh

1e831a56020979aee0159e50e4ab5c173c5c2e19 — Simon Ser 1 year, 1 month ago 63b2433
Put jobs in the foreground
2 files changed, 18 insertions(+), 2 deletions(-)

M shell/task/command_process.c
M shell/task/task.c
M shell/task/command_process.c => shell/task/command_process.c +8 -2
@@ 41,9 41,11 @@ static bool task_process_start(struct task_command *tc, struct context *ctx) {
		fprintf(stderr, "failed to fork(): %s\n", strerror(errno));
		return false;
	} else if (pid == 0) {
		put_into_process_group(ctx, getpid());
		struct mrsh_job *job = put_into_process_group(ctx, getpid());
		if (ctx->state->interactive && !ctx->background) {
			tcsetpgrp(ctx->state->fd, job->pgid);
		}
		init_job_child_process(ctx->state);
		// TODO: give the terminal to the process group, if foreground job

		for (size_t i = 0; i < sc->assignments.len; ++i) {
			struct mrsh_assignment *assign = sc->assignments.data[i];


@@ 92,9 94,13 @@ static bool task_process_start(struct task_command *tc, struct context *ctx) {
	}

	struct mrsh_job *job = put_into_process_group(ctx, pid);
	if (ctx->state->interactive && !ctx->background) {
		tcsetpgrp(ctx->state->fd, job->pgid);
	}

	tc->process = process_create(ctx->state, pid);
	job_add_process(job, tc->process);

	return true;
}


M shell/task/task.c => shell/task/task.c +10 -0
@@ 4,6 4,7 @@
#include <stdlib.h>
#include <string.h>
#include <sys/wait.h>
#include <unistd.h>
#include "shell/job.h"
#include "shell/task.h"



@@ 51,6 52,15 @@ int task_run(struct task *task, struct context *ctx) {
					&& (ctx->state->options & MRSH_OPT_ERREXIT)) {
				ctx->state->exit = ret;
			}

			// Put the shell back in the foreground
			tcsetpgrp(ctx->state->fd, ctx->state->pgid);
			// Restore the shell’s terminal modes
			// tcgetattr(ctx->state->fd, &job->term_modes); // TODO
			tcsetattr(ctx->state->fd, TCSADRAIN, &ctx->state->term_modes);

			destroy_finished_jobs(ctx->state);

			return ret;
		}