~emersion/mrsh

90610a21bb3d9ba4f8be39c30bc8e61b96b4d197 — Simon Ser 7 months ago 4474b68
shell: rename mrsh_state.fd to term_fd
4 files changed, 16 insertions(+), 17 deletions(-)

M include/mrsh/shell.h
M main.c
M shell/job.c
M shell/shell.c
M include/mrsh/shell.h => include/mrsh/shell.h +1 -1
@@ 86,7 86,7 @@ struct mrsh_job;

struct mrsh_state {
	int exit;
	int fd;
	int term_fd;
	uint32_t options; // enum mrsh_option
	struct mrsh_call_frame *frame;
	bool interactive;

M main.c => main.c +5 -6
@@ 40,19 40,19 @@ int main(int argc, char *argv[]) {
		}
	}

	state.fd = -1;
	state.term_fd = STDIN_FILENO;

	struct mrsh_buffer parser_buffer = {0};
	struct mrsh_parser *parser;
	int fd = -1;
	if (state.interactive) {
		interactive_init(&state);
		parser = mrsh_parser_with_buffer(&parser_buffer);
		state.fd = STDIN_FILENO;
	} else {
		if (init_args.command_str) {
			parser = mrsh_parser_with_data(init_args.command_str,
				strlen(init_args.command_str));
		} else {
			int fd;
			if (init_args.command_file) {
				fd = open(init_args.command_file, O_RDONLY | O_CLOEXEC);
				if (fd < 0) {


@@ 65,7 65,6 @@ int main(int argc, char *argv[]) {
			}

			parser = mrsh_parser_with_fd(fd);
			state.fd = fd;
		}
	}
	mrsh_state_set_parser_alias_func(&state, parser);


@@ 146,8 145,8 @@ int main(int argc, char *argv[]) {
	mrsh_parser_destroy(parser);
	mrsh_buffer_finish(&parser_buffer);
	mrsh_state_finish(&state);
	if (state.fd >= 0) {
		close(state.fd);
	if (fd >= 0) {
		close(fd);
	}

	return state.exit;

M shell/job.c => shell/job.c +9 -9
@@ 25,7 25,7 @@ static const size_t IGNORED_SIGNALS_LEN =
	sizeof(ignored_signals) / sizeof(ignored_signals[0]);

bool mrsh_set_job_control(struct mrsh_state *state, bool enabled) {
	assert(state->fd >= 0);
	assert(state->term_fd >= 0);

	if (state->job_control == enabled) {
		return true;


@@ 35,7 35,7 @@ bool mrsh_set_job_control(struct mrsh_state *state, bool enabled) {
		// Loop until we are in the foreground
		while (true) {
			pid_t pgid = getpgrp();
			if (tcgetpgrp(state->fd) == pgid) {
			if (tcgetpgrp(state->term_fd) == pgid) {
				break;
			}
			kill(-pgid, SIGTTIN);


@@ 62,12 62,12 @@ bool mrsh_set_job_control(struct mrsh_state *state, bool enabled) {
		}

		// Grab control of the terminal
		if (tcsetpgrp(state->fd, state->pgid) != 0) {
		if (tcsetpgrp(state->term_fd, state->pgid) != 0) {
			perror("tcsetpgrp");
			return false;
		}
		// Save default terminal attributes for the shell
		if (tcgetattr(state->fd, &state->term_modes) != 0) {
		if (tcgetattr(state->term_fd, &state->term_modes) != 0) {
			perror("tcgetattr");
			return false;
		}


@@ 153,22 153,22 @@ bool job_set_foreground(struct mrsh_job *job, bool foreground, bool cont) {
	if (foreground && state->foreground_job != job) {
		assert(state->foreground_job == NULL);
		// Put the job in the foreground
		tcsetpgrp(state->fd, job->pgid);
		tcsetpgrp(state->term_fd, job->pgid);
		if (cont) {
			// Restore the job's terminal modes
			tcsetattr(state->fd, TCSADRAIN, &job->term_modes);
			tcsetattr(state->term_fd, TCSADRAIN, &job->term_modes);
		}
		state->foreground_job = job;
	}

	if (!foreground && state->foreground_job == job) {
		// Put the shell back in the foreground
		tcsetpgrp(state->fd, state->pgid);
		tcsetpgrp(state->term_fd, state->pgid);
		// Save the job's terminal modes, to restore them if it's put in the
		// foreground again
		tcgetattr(state->fd, &job->term_modes);
		tcgetattr(state->term_fd, &job->term_modes);
		// Restore the shell’s terminal modes
		tcsetattr(state->fd, TCSADRAIN, &state->term_modes);
		tcsetattr(state->term_fd, TCSADRAIN, &state->term_modes);
		state->foreground_job = NULL;
	}


M shell/shell.c => shell/shell.c +1 -1
@@ 20,7 20,7 @@ void function_destroy(struct mrsh_function *fn) {

void mrsh_state_init(struct mrsh_state *state) {
	state->exit = -1;
	state->fd = -1;
	state->term_fd = -1;
	state->interactive = isatty(STDIN_FILENO);
	state->options = state->interactive ? MRSH_OPT_INTERACTIVE : 0;
	state->frame = calloc(1, sizeof(struct mrsh_call_frame));