2017-06-15 09:25:51 +08:00
|
|
|
// Implementation of the bg builtin.
|
|
|
|
#include "config.h" // IWYU pragma: keep
|
|
|
|
|
|
|
|
#include <errno.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
|
|
|
#include <memory>
|
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
#include "builtin.h"
|
|
|
|
#include "builtin_bg.h"
|
|
|
|
#include "common.h"
|
|
|
|
#include "fallback.h" // IWYU pragma: keep
|
|
|
|
#include "io.h"
|
2019-05-05 13:12:31 +08:00
|
|
|
#include "parser.h"
|
2017-06-15 09:25:51 +08:00
|
|
|
#include "proc.h"
|
|
|
|
#include "wutil.h" // IWYU pragma: keep
|
|
|
|
|
|
|
|
/// Helper function for builtin_bg().
|
|
|
|
static int send_to_bg(parser_t &parser, io_streams_t &streams, job_t *j) {
|
|
|
|
assert(j != NULL);
|
2018-10-03 01:30:23 +08:00
|
|
|
if (!j->get_flag(job_flag_t::JOB_CONTROL)) {
|
2017-06-15 09:25:51 +08:00
|
|
|
streams.err.append_format(
|
|
|
|
_(L"%ls: Can't put job %d, '%ls' to background because it is not under job control\n"),
|
|
|
|
L"bg", j->job_id, j->command_wcstr());
|
|
|
|
builtin_print_help(parser, streams, L"bg", streams.err);
|
|
|
|
return STATUS_CMD_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
streams.err.append_format(_(L"Send job %d '%ls' to background\n"), j->job_id,
|
|
|
|
j->command_wcstr());
|
2019-05-05 13:12:31 +08:00
|
|
|
parser.job_promote(j);
|
2018-10-03 01:30:23 +08:00
|
|
|
j->set_flag(job_flag_t::FOREGROUND, false);
|
2019-05-05 13:12:31 +08:00
|
|
|
j->continue_job(parser, true, j->is_stopped());
|
2017-06-15 09:25:51 +08:00
|
|
|
return STATUS_CMD_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Builtin for putting a job in the background.
|
|
|
|
int builtin_bg(parser_t &parser, io_streams_t &streams, wchar_t **argv) {
|
|
|
|
const wchar_t *cmd = argv[0];
|
|
|
|
int argc = builtin_count_args(argv);
|
2017-06-17 11:42:33 +08:00
|
|
|
help_only_cmd_opts_t opts;
|
2017-06-15 09:25:51 +08:00
|
|
|
|
|
|
|
int optind;
|
2017-06-17 11:42:33 +08:00
|
|
|
int retval = parse_help_only_cmd_opts(opts, &optind, argc, argv, parser, streams);
|
2017-06-15 09:25:51 +08:00
|
|
|
if (retval != STATUS_CMD_OK) return retval;
|
|
|
|
|
|
|
|
if (opts.print_help) {
|
|
|
|
builtin_print_help(parser, streams, cmd, streams.out);
|
|
|
|
return STATUS_CMD_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (optind == argc) {
|
|
|
|
// No jobs were specified so use the most recent (i.e., last) job.
|
2018-12-31 11:25:16 +08:00
|
|
|
job_t *job = nullptr;
|
2019-05-05 13:12:31 +08:00
|
|
|
for (const auto &j : parser.jobs()) {
|
2018-10-03 01:30:23 +08:00
|
|
|
if (j->is_stopped() && j->get_flag(job_flag_t::JOB_CONTROL) && (!j->is_completed())) {
|
2018-12-31 11:25:16 +08:00
|
|
|
job = j.get();
|
2017-06-15 09:25:51 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-31 11:25:16 +08:00
|
|
|
if (!job) {
|
2017-06-15 09:25:51 +08:00
|
|
|
streams.err.append_format(_(L"%ls: There are no suitable jobs\n"), cmd);
|
|
|
|
retval = STATUS_CMD_ERROR;
|
|
|
|
} else {
|
2018-12-31 11:25:16 +08:00
|
|
|
retval = send_to_bg(parser, streams, job);
|
2017-06-15 09:25:51 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
|
|
|
// The user specified at least one job to be backgrounded.
|
|
|
|
std::vector<int> pids;
|
|
|
|
|
|
|
|
// If one argument is not a valid pid (i.e. integer >= 0), fail without backgrounding anything,
|
|
|
|
// but still print errors for all of them.
|
|
|
|
for (int i = optind; argv[i]; i++) {
|
|
|
|
int pid = fish_wcstoi(argv[i]);
|
|
|
|
if (errno || pid < 0) {
|
|
|
|
streams.err.append_format(_(L"%ls: '%ls' is not a valid job specifier\n"), L"bg",
|
|
|
|
argv[i]);
|
|
|
|
retval = STATUS_INVALID_ARGS;
|
|
|
|
}
|
|
|
|
pids.push_back(pid);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (retval != STATUS_CMD_OK) return retval;
|
|
|
|
|
|
|
|
// Background all existing jobs that match the pids.
|
|
|
|
// Non-existent jobs aren't an error, but information about them is useful.
|
|
|
|
for (auto p : pids) {
|
2018-10-03 01:30:23 +08:00
|
|
|
if (job_t *j = job_t::from_pid(p)) {
|
2017-06-15 09:25:51 +08:00
|
|
|
retval |= send_to_bg(parser, streams, j);
|
|
|
|
} else {
|
|
|
|
streams.err.append_format(_(L"%ls: Could not find job '%d'\n"), cmd, p);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return retval;
|
|
|
|
}
|