~emersion/mrsh

ref: 537899b8a63adf88d442790b4cf6a38edd9288d9 mrsh/builtin/fg.c -rw-r--r-- 872 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
#include <mrsh/getopt.h>
#include <stdio.h>
#include <stdlib.h>
#include "builtin.h"
#include "shell/job.h"
#include "shell/shell.h"

static const char fg_usage[] = "usage: fg [job_id]\n";

int builtin_fg(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, "fg: unknown option -- %c\n", mrsh_optopt);
			fprintf(stderr, fg_usage);
			return EXIT_FAILURE;
		}
	}

	struct mrsh_job *job;
	if (mrsh_optind == argc) {
		job = job_by_id(state, "%%", true);
	} else if (mrsh_optind == argc - 1) {
		job = job_by_id(state, argv[mrsh_optind], true);
	} else {
		fprintf(stderr, fg_usage);
		return EXIT_FAILURE;
	}
	if (!job) {
		return EXIT_FAILURE;
	}

	if (!job_set_foreground(job, true, true)) {
		return EXIT_FAILURE;
	}
	return job_wait(job);
}