2017-06-14 21:19:49 -07:00
|
|
|
// Implementation of the exit builtin.
|
|
|
|
#include "config.h" // IWYU pragma: keep
|
|
|
|
|
2021-11-09 17:07:15 -08:00
|
|
|
#include "exit.h"
|
2019-10-13 15:50:48 -07:00
|
|
|
|
2019-11-18 17:11:16 -08:00
|
|
|
#include <cerrno>
|
|
|
|
#include <cstddef>
|
2017-06-14 21:19:49 -07:00
|
|
|
|
2021-11-09 17:07:15 -08:00
|
|
|
#include "../builtin.h"
|
|
|
|
#include "../common.h"
|
|
|
|
#include "../fallback.h" // IWYU pragma: keep
|
|
|
|
#include "../io.h"
|
|
|
|
#include "../parser.h"
|
|
|
|
#include "../proc.h"
|
|
|
|
#include "../reader.h"
|
|
|
|
#include "../wgetopt.h"
|
|
|
|
#include "../wutil.h" // IWYU pragma: keep
|
2017-06-14 21:19:49 -07:00
|
|
|
|
2017-06-15 17:57:37 -07:00
|
|
|
struct exit_cmd_opts_t {
|
2017-06-14 21:19:49 -07:00
|
|
|
bool print_help = false;
|
|
|
|
};
|
2018-09-28 23:45:56 -04:00
|
|
|
static const wchar_t *const short_options = L":h";
|
2022-04-01 14:25:02 -07:00
|
|
|
static const struct woption long_options[] = {{L"help", no_argument, nullptr, 'h'}, {}};
|
2017-06-14 21:19:49 -07:00
|
|
|
|
2017-06-15 17:57:37 -07:00
|
|
|
static int parse_cmd_opts(exit_cmd_opts_t &opts, int *optind, //!OCLINT(high ncss method)
|
2021-02-13 18:41:09 -08:00
|
|
|
int argc, const wchar_t **argv, parser_t &parser, io_streams_t &streams) {
|
2017-06-14 21:19:49 -07:00
|
|
|
UNUSED(parser);
|
|
|
|
UNUSED(streams);
|
2021-02-13 18:41:09 -08:00
|
|
|
const wchar_t *cmd = argv[0];
|
2017-06-14 21:19:49 -07:00
|
|
|
int opt;
|
|
|
|
wgetopter_t w;
|
2019-11-18 18:34:50 -08:00
|
|
|
while ((opt = w.wgetopt_long(argc, argv, short_options, long_options, nullptr)) != -1) {
|
2017-06-14 21:19:49 -07:00
|
|
|
switch (opt) { //!OCLINT(too few branches)
|
|
|
|
case 'h': {
|
2017-06-15 17:57:37 -07:00
|
|
|
opts.print_help = true;
|
2017-06-14 22:12:29 -07:00
|
|
|
break;
|
2017-06-14 21:19:49 -07:00
|
|
|
}
|
2017-06-29 21:49:57 -07:00
|
|
|
case ':': {
|
2017-07-01 14:03:47 -07:00
|
|
|
builtin_missing_argument(parser, streams, cmd, argv[w.woptind - 1]);
|
2017-06-29 21:49:57 -07:00
|
|
|
return STATUS_INVALID_ARGS;
|
|
|
|
}
|
2017-06-14 21:19:49 -07:00
|
|
|
case '?': {
|
|
|
|
// We would normally invoke builtin_unknown_option() and return an error.
|
|
|
|
// But for this command we want to let it try and parse the value as a negative
|
|
|
|
// return value.
|
|
|
|
*optind = w.woptind - 1;
|
|
|
|
return STATUS_CMD_OK;
|
|
|
|
}
|
|
|
|
default: {
|
|
|
|
DIE("unexpected retval from wgetopt_long");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
*optind = w.woptind;
|
|
|
|
return STATUS_CMD_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// The exit builtin. Calls reader_exit to exit and returns the value specified.
|
2021-02-13 18:41:09 -08:00
|
|
|
maybe_t<int> builtin_exit(parser_t &parser, io_streams_t &streams, const wchar_t **argv) {
|
2017-06-14 21:19:49 -07:00
|
|
|
const wchar_t *cmd = argv[0];
|
|
|
|
int argc = builtin_count_args(argv);
|
2017-06-15 17:57:37 -07:00
|
|
|
exit_cmd_opts_t opts;
|
2017-06-14 21:19:49 -07:00
|
|
|
|
|
|
|
int optind;
|
2017-06-15 17:57:37 -07:00
|
|
|
int retval = parse_cmd_opts(opts, &optind, argc, argv, parser, streams);
|
2017-06-14 21:19:49 -07:00
|
|
|
if (retval != STATUS_CMD_OK) return retval;
|
|
|
|
|
|
|
|
if (opts.print_help) {
|
2019-10-20 11:38:17 +02:00
|
|
|
builtin_print_help(parser, streams, cmd);
|
2017-06-14 21:19:49 -07:00
|
|
|
return STATUS_CMD_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (optind + 1 < argc) {
|
|
|
|
streams.err.append_format(BUILTIN_ERR_TOO_MANY_ARGUMENTS, cmd);
|
2019-03-26 19:13:01 +01:00
|
|
|
builtin_print_error_trailer(parser, streams.err, cmd);
|
2017-06-14 21:19:49 -07:00
|
|
|
return STATUS_INVALID_ARGS;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (optind == argc) {
|
2019-05-12 14:00:44 -07:00
|
|
|
retval = parser.get_last_status();
|
2017-06-14 21:19:49 -07:00
|
|
|
} else {
|
|
|
|
retval = fish_wcstoi(argv[optind]);
|
|
|
|
if (errno) {
|
2019-09-17 22:00:08 -07:00
|
|
|
streams.err.append_format(BUILTIN_ERR_NOT_NUMBER, cmd, argv[optind]);
|
2019-03-26 19:13:01 +01:00
|
|
|
builtin_print_error_trailer(parser, streams.err, cmd);
|
2017-06-14 21:19:49 -07:00
|
|
|
return STATUS_INVALID_ARGS;
|
|
|
|
}
|
|
|
|
}
|
2020-08-15 14:41:11 -07:00
|
|
|
// Mark that we are exiting in the parser.
|
|
|
|
// TODO: in concurrent mode this won't successfully exit a pipeline, as there are other parsers
|
|
|
|
// involved. That is, `exit | sleep 1000` may not exit as hoped. Need to rationalize what
|
|
|
|
// behavior we want here.
|
|
|
|
parser.libdata().exit_current_script = true;
|
2017-06-14 21:19:49 -07:00
|
|
|
return retval;
|
|
|
|
}
|