1e831a56020979aee0159e50e4ab5c173c5c2e19 — Simon Ser 28 days 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 @@ 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 @@ }
  
  	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 @@ && (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;
  		}