~emersion/mrsh

afbb30431e8c06ba2d705f3f8ed94d26eafd819c — Simon Ser 2 years ago b0cbe5e
process: remove global state
M include/mrsh/shell.h => include/mrsh/shell.h +1 -0
@@ 89,6 89,7 @@ struct mrsh_state {
	struct mrsh_hashtable variables; // mrsh_variable *
	struct mrsh_hashtable aliases; // char *
	struct mrsh_hashtable functions; // mrsh_function *
	struct mrsh_array processes;
	int last_status;

	bool job_control;

M include/shell/process.h => include/shell/process.h +4 -2
@@ 1,6 1,7 @@
#ifndef SHELL_PROCESS_H
#define SHELL_PROCESS_H

#include <mrsh/shell.h>
#include <stdbool.h>
#include <sys/types.h>



@@ 9,13 10,14 @@
 */
struct process {
	pid_t pid;
	struct mrsh_state *state;
	bool finished;
	int stat;
};

void process_init(struct process *process, pid_t pid);
void process_init(struct process *process, struct mrsh_state *state, pid_t pid);
void process_finish(struct process *process);
int process_poll(struct process *process);
void process_notify(pid_t pid, int stat);
void process_notify(struct mrsh_state *state, pid_t pid, int stat);

#endif

M shell/job.c => shell/job.c +1 -1
@@ 127,7 127,7 @@ void job_add_process(struct job *job, struct process *proc) {
}

void job_notify(struct mrsh_state *state, pid_t pid, int stat) {
	process_notify(pid, stat);
	process_notify(state, pid, stat);

	for (size_t i = 0; i < state->jobs.len; ++i) {
		struct job *job = state->jobs.data[i];

M shell/process.c => shell/process.c +10 -12
@@ 5,13 5,10 @@
#include <sys/wait.h>
#include "shell/process.h"

// TODO: don't use a global
// TODO: use a linked list instead
static struct mrsh_array running_processes = {0};

void process_init(struct process *proc, pid_t pid) {
	mrsh_array_add(&running_processes, proc);
void process_init(struct process *proc, struct mrsh_state *state, pid_t pid) {
	mrsh_array_add(&state->processes, proc);
	proc->pid = pid;
	proc->state = state;
	proc->finished = false;
	proc->stat = 0;
}


@@ 30,9 27,10 @@ static void array_remove(struct mrsh_array *array, size_t i) {
}

static void process_remove(struct process *proc) {
	for (size_t i = 0; i < running_processes.len; ++i) {
		if (running_processes.data[i] == proc) {
			array_remove(&running_processes, i);
	struct mrsh_state *state = proc->state;
	for (size_t i = 0; i < state->processes.len; ++i) {
		if (state->processes.data[i] == proc) {
			array_remove(&state->processes, i);
			break;
		}
	}


@@ 42,11 40,11 @@ void process_finish(struct process *proc) {
	process_remove(proc);
}

void process_notify(pid_t pid, int stat) {
void process_notify(struct mrsh_state *state, pid_t pid, int stat) {
	struct process *proc = NULL;
	bool found = false;
	for (size_t i = 0; i < running_processes.len; ++i) {
		proc = running_processes.data[i];
	for (size_t i = 0; i < state->processes.len; ++i) {
		proc = state->processes.data[i];
		if (proc->pid == pid) {
			found = true;
			break;

M shell/shell.c => shell/shell.c +1 -0
@@ 73,6 73,7 @@ void mrsh_state_finish(struct mrsh_state *state) {
	mrsh_hashtable_for_each(&state->aliases,
		state_string_finish_iterator, NULL);
	mrsh_hashtable_finish(&state->aliases);
	mrsh_array_finish(&state->processes);
	for (size_t i = 0; i < state->jobs.len; ++i) {
		job_destroy(state->jobs.data[i]);
	}

M shell/task/command.c => shell/task/command.c +3 -1
@@ 14,7 14,9 @@ static void task_command_destroy(struct task *task) {
	mrsh_array_finish(&tc->args);
	switch (tc->type) {
	case TASK_COMMAND_PROCESS:
		process_finish(&tc->process);
		if (tc->started) {
			process_finish(&tc->process);
		}
		break;
	case TASK_COMMAND_BUILTIN:
		break;

M shell/task/command_process.c => shell/task/command_process.c +1 -1
@@ 93,7 93,7 @@ static bool task_process_start(struct task_command *tc, struct context *ctx) {

	struct job *job = put_into_process_group(ctx, pid);

	process_init(&tc->process, pid);
	process_init(&tc->process, ctx->state, pid);
	job_add_process(job, &tc->process);
	return true;
}

M shell/task/subshell.c => shell/task/subshell.c +1 -1
@@ 39,7 39,7 @@ static bool task_subshell_start(struct task_subshell *ts, struct context *ctx) {
		}
		exit(ret);
	} else {
		process_init(&ts->process, pid);
		process_init(&ts->process, ctx->state, pid);
		return true;
	}
}

M shell/task/word.c => shell/task/word.c +1 -1
@@ 90,7 90,7 @@ static bool task_word_command_start(struct task_word *tw,
	}

	close(fds[1]);
	process_init(&tw->process, pid);
	process_init(&tw->process, ctx->state, pid);
	tw->fd = fds[0];
	return true;
}