7723619f004ac57dcc4304e5417011b98c589e06 — Simon Ser 29 days ago ffa7daf
builtin: add bg
4 files changed, 47 insertions(+), 0 deletions(-)

A builtin/bg.c
M builtin/builtin.c
M include/builtin.h
M meson.build
A builtin/bg.c => builtin/bg.c +44 -0
@@ 0,0 1,44 @@
+ #include <mrsh/getopt.h>
+ #include <stdio.h>
+ #include <stdlib.h>
+ #include "builtin.h"
+ #include "shell/job.h"
+ #include "shell/shell.h"
+ 
+ // TODO: bg [job_id]
+ static const char bg_usage[] = "usage: bg\n";
+ 
+ int builtin_bg(struct mrsh_state *state, int argc, char *argv[]) {
+ 	mrsh_optind = 0;
+ 	int opt;
+ 	while ((opt = mrsh_getopt(argc, argv, ":")) != -1) {
+ 		switch (opt) {
+ 		default:
+ 			fprintf(stderr, "bg: unknown option -- %c\n", mrsh_optopt);
+ 			fprintf(stderr, bg_usage);
+ 			return EXIT_FAILURE;
+ 		}
+ 	}
+ 	if (mrsh_optind < argc) {
+ 		fprintf(stderr, bg_usage);
+ 		return EXIT_FAILURE;
+ 	}
+ 
+ 	struct mrsh_job *stopped = NULL;
+ 	for (ssize_t i = state->jobs.len - 1; i >= 0; --i) {
+ 		struct mrsh_job *job = state->jobs.data[i];
+ 		if (job_stopped(job)) {
+ 			stopped = job;
+ 			break;
+ 		}
+ 	}
+ 	if (stopped == NULL) {
+ 		fprintf(stderr, "bg: no current job");
+ 		return EXIT_FAILURE;
+ 	}
+ 
+ 	if (!job_set_foreground(stopped, false, true)) {
+ 		return EXIT_FAILURE;
+ 	}
+ 	return EXIT_SUCCESS;
+ }

M builtin/builtin.c => builtin/builtin.c +1 -0
@@ 17,6 17,7 @@ { ".", builtin_dot, true },
  	{ ":", builtin_colon, true },
  	{ "alias", builtin_alias, false },
+ 	{ "bg", builtin_bg, false },
  	{ "cd", builtin_cd, false },
  	{ "eval", builtin_eval, true },
  	{ "exit", builtin_exit, true },

M include/builtin.h => include/builtin.h +1 -0
@@ 9,6 9,7 @@ void print_escaped(const char *value);
  
  int builtin_alias(struct mrsh_state *state, int argc, char *argv[]);
+ int builtin_bg(struct mrsh_state *state, int argc, char *argv[]);
  int builtin_cd(struct mrsh_state *state, int argc, char *argv[]);
  int builtin_colon(struct mrsh_state *state, int argc, char *argv[]);
  int builtin_dot(struct mrsh_state *state, int argc, char *argv[]);

M meson.build => meson.build +1 -0
@@ 73,6 73,7 @@ 'ast.c',
  		'buffer.c',
  		'builtin/alias.c',
+ 		'builtin/bg.c',
  		'builtin/builtin.c',
  		'builtin/cd.c',
  		'builtin/colon.c',