2017-06-15 12:19:49 +08:00
|
|
|
// Implementation of the exit builtin.
|
|
|
|
#include "config.h" // IWYU pragma: keep
|
|
|
|
|
|
|
|
#include <errno.h>
|
|
|
|
#include <stddef.h>
|
|
|
|
|
|
|
|
#include "builtin.h"
|
|
|
|
#include "builtin_exit.h"
|
|
|
|
#include "common.h"
|
|
|
|
#include "fallback.h" // IWYU pragma: keep
|
|
|
|
#include "io.h"
|
2019-05-13 05:00:44 +08:00
|
|
|
#include "parser.h"
|
2017-06-15 12:19:49 +08:00
|
|
|
#include "proc.h"
|
|
|
|
#include "reader.h"
|
|
|
|
#include "wgetopt.h"
|
|
|
|
#include "wutil.h" // IWYU pragma: keep
|
|
|
|
|
2017-06-16 08:57:37 +08:00
|
|
|
struct exit_cmd_opts_t {
|
2017-06-15 12:19:49 +08:00
|
|
|
bool print_help = false;
|
|
|
|
};
|
2018-09-29 11:45:56 +08:00
|
|
|
static const wchar_t *const short_options = L":h";
|
2017-06-15 12:19:49 +08:00
|
|
|
static const struct woption long_options[] = {{L"help", no_argument, NULL, 'h'},
|
|
|
|
{NULL, 0, NULL, 0}};
|
|
|
|
|
2017-06-16 08:57:37 +08:00
|
|
|
static int parse_cmd_opts(exit_cmd_opts_t &opts, int *optind, //!OCLINT(high ncss method)
|
2017-06-15 12:19:49 +08:00
|
|
|
int argc, wchar_t **argv, parser_t &parser, io_streams_t &streams) {
|
|
|
|
UNUSED(parser);
|
|
|
|
UNUSED(streams);
|
|
|
|
wchar_t *cmd = argv[0];
|
|
|
|
int opt;
|
|
|
|
wgetopter_t w;
|
|
|
|
while ((opt = w.wgetopt_long(argc, argv, short_options, long_options, NULL)) != -1) {
|
|
|
|
switch (opt) { //!OCLINT(too few branches)
|
|
|
|
case 'h': {
|
2017-06-16 08:57:37 +08:00
|
|
|
opts.print_help = true;
|
2017-06-15 13:12:29 +08:00
|
|
|
break;
|
2017-06-15 12:19:49 +08:00
|
|
|
}
|
2017-06-30 12:49:57 +08:00
|
|
|
case ':': {
|
2017-07-02 05:03:47 +08:00
|
|
|
builtin_missing_argument(parser, streams, cmd, argv[w.woptind - 1]);
|
2017-06-30 12:49:57 +08:00
|
|
|
return STATUS_INVALID_ARGS;
|
|
|
|
}
|
2017-06-15 12:19:49 +08: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");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
*optind = w.woptind;
|
|
|
|
return STATUS_CMD_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// The exit builtin. Calls reader_exit to exit and returns the value specified.
|
|
|
|
int builtin_exit(parser_t &parser, io_streams_t &streams, wchar_t **argv) {
|
|
|
|
const wchar_t *cmd = argv[0];
|
|
|
|
int argc = builtin_count_args(argv);
|
2017-06-16 08:57:37 +08:00
|
|
|
exit_cmd_opts_t opts;
|
2017-06-15 12:19:49 +08:00
|
|
|
|
|
|
|
int optind;
|
2017-06-16 08:57:37 +08:00
|
|
|
int retval = parse_cmd_opts(opts, &optind, argc, argv, parser, streams);
|
2017-06-15 12:19:49 +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 + 1 < argc) {
|
|
|
|
streams.err.append_format(BUILTIN_ERR_TOO_MANY_ARGUMENTS, cmd);
|
2019-03-27 02:13:01 +08:00
|
|
|
builtin_print_error_trailer(parser, streams.err, cmd);
|
2017-06-15 12:19:49 +08:00
|
|
|
return STATUS_INVALID_ARGS;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (optind == argc) {
|
2019-05-13 05:00:44 +08:00
|
|
|
retval = parser.get_last_status();
|
2017-06-15 12:19:49 +08:00
|
|
|
} else {
|
|
|
|
retval = fish_wcstoi(argv[optind]);
|
|
|
|
if (errno) {
|
|
|
|
streams.err.append_format(_(L"%ls: Argument '%ls' must be an integer\n"), cmd,
|
|
|
|
argv[optind]);
|
2019-03-27 02:13:01 +08:00
|
|
|
builtin_print_error_trailer(parser, streams.err, cmd);
|
2017-06-15 12:19:49 +08:00
|
|
|
return STATUS_INVALID_ARGS;
|
|
|
|
}
|
|
|
|
}
|
2019-02-25 05:24:03 +08:00
|
|
|
reader_set_end_loop(true);
|
2017-06-15 12:19:49 +08:00
|
|
|
return retval;
|
|
|
|
}
|