~emersion/mrsh

ref: 537899b8a63adf88d442790b4cf6a38edd9288d9 mrsh/builtin/bg.c -rw-r--r-- 991 bytes
537899b8Simon Ser shell/arithm: strip mrsh_ prefix for static functions 2 years ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#include <mrsh/getopt.h>
#include <stdio.h>
#include <stdlib.h>
#include "builtin.h"
#include "shell/job.h"
#include "shell/shell.h"
#include "shell/task.h"

static const char bg_usage[] = "usage: bg [job_id...]\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) {
		struct mrsh_job *job = job_by_id(state, "%%", true);
		if (!job) {
			return EXIT_FAILURE;
		}
		if (!job_set_foreground(job, false, true)) {
			return EXIT_FAILURE;
		}
		return EXIT_SUCCESS;
	}

	for (int i = mrsh_optind; i < argc; ++i) {
		struct mrsh_job *job = job_by_id(state, argv[i], true);
		if (!job) {
			return EXIT_FAILURE;
		}
		if (!job_set_foreground(job, false, true)) {
			return EXIT_FAILURE;
		}
	}
	return EXIT_SUCCESS;
}