split builtin return into its own module

This commit is contained in:
Kurtis Rader 2017-06-14 19:13:45 -07:00
parent 422b1bd066
commit b530d175e7
4 changed files with 119 additions and 45 deletions

View File

@ -100,7 +100,7 @@ HAVE_DOXYGEN=@HAVE_DOXYGEN@
#
FISH_OBJS := obj/autoload.o obj/builtin.o obj/builtin_bind.o obj/builtin_block.o \
obj/builtin_commandline.o obj/builtin_emit.o obj/builtin_functions.o \
obj/builtin_bg.o obj/builtin_fg.o \
obj/builtin_bg.o obj/builtin_fg.o obj/builtin_return.o \
obj/builtin_history.o obj/builtin_status.o obj/builtin_read.o obj/builtin_pwd.o \
obj/builtin_source.o obj/builtin_random.o obj/builtin_echo.o \
obj/builtin_cd.o obj/builtin_disown.o obj/builtin_function.o \

View File

@ -48,6 +48,7 @@
#include "builtin_pwd.h"
#include "builtin_random.h"
#include "builtin_read.h"
#include "builtin_return.h"
#include "builtin_set.h"
#include "builtin_set_color.h"
#include "builtin_source.h"
@ -397,50 +398,6 @@ static int builtin_breakpoint(parser_t &parser, io_streams_t &streams, wchar_t *
return proc_get_last_status();
}
/// Function for handling the \c return builtin.
static int builtin_return(parser_t &parser, io_streams_t &streams, wchar_t **argv) {
int argc = builtin_count_args(argv);
if (argc > 2) {
streams.err.append_format(BUILTIN_ERR_TOO_MANY_ARGUMENTS, argv[0]);
builtin_print_help(parser, streams, argv[0], streams.err);
return STATUS_INVALID_ARGS;
}
int status;
if (argc == 2) {
status = fish_wcstoi(argv[1]);
if (errno) {
streams.err.append_format(_(L"%ls: Argument '%ls' must be an integer\n"), argv[0],
argv[1]);
builtin_print_help(parser, streams, argv[0], streams.err);
return STATUS_INVALID_ARGS;
}
} else {
status = proc_get_last_status();
}
// Find the function block.
size_t function_block_idx;
for (function_block_idx = 0; function_block_idx < parser.block_count(); function_block_idx++) {
const block_t *b = parser.block_at_index(function_block_idx);
if (b->type() == FUNCTION_CALL || b->type() == FUNCTION_CALL_NO_SHADOW) break;
}
if (function_block_idx >= parser.block_count()) {
streams.err.append_format(_(L"%ls: Not inside of function\n"), argv[0]);
builtin_print_help(parser, streams, argv[0], streams.err);
return STATUS_CMD_ERROR;
}
// Skip everything up to and including the function block.
for (size_t i = 0; i <= function_block_idx; i++) {
block_t *b = parser.block_at_index(i);
b->skip = true;
}
return status;
}
int builtin_true(parser_t &parser, io_streams_t &streams, wchar_t **argv) {
UNUSED(parser);
UNUSED(streams);

108
src/builtin_return.cpp Normal file
View File

@ -0,0 +1,108 @@
// Implementation of the return builtin.
#include "config.h" // IWYU pragma: keep
#include <errno.h>
#include <stddef.h>
#include "builtin.h"
#include "builtin_return.h"
#include "common.h"
#include "fallback.h" // IWYU pragma: keep
#include "io.h"
#include "parser.h"
#include "proc.h"
#include "wgetopt.h"
#include "wutil.h" // IWYU pragma: keep
struct cmd_opts {
bool print_help = false;
};
static const wchar_t *short_options = L"h";
static const struct woption long_options[] = {{L"help", no_argument, NULL, 'h'},
{NULL, 0, NULL, 0}};
static int parse_cmd_opts(struct cmd_opts *opts, int *optind, //!OCLINT(high ncss method)
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': {
opts->print_help = true;
return STATUS_CMD_OK;
}
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;
}
/// Function for handling the return builtin.
int builtin_return(parser_t &parser, io_streams_t &streams, wchar_t **argv) {
const wchar_t *cmd = argv[0];
int argc = builtin_count_args(argv);
struct cmd_opts opts;
int optind;
int retval = parse_cmd_opts(&opts, &optind, argc, argv, parser, streams);
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);
builtin_print_help(parser, streams, cmd, streams.err);
return STATUS_INVALID_ARGS;
}
if (optind == argc) {
retval = proc_get_last_status();
} else {
retval = fish_wcstoi(argv[1]);
if (errno) {
streams.err.append_format(_(L"%ls: Argument '%ls' must be an integer\n"), cmd, argv[1]);
builtin_print_help(parser, streams, cmd, streams.err);
return STATUS_INVALID_ARGS;
}
retval &= 0xFF;
}
// Find the function block.
size_t function_block_idx;
for (function_block_idx = 0; function_block_idx < parser.block_count(); function_block_idx++) {
const block_t *b = parser.block_at_index(function_block_idx);
if (b->type() == FUNCTION_CALL || b->type() == FUNCTION_CALL_NO_SHADOW) break;
}
if (function_block_idx >= parser.block_count()) {
streams.err.append_format(_(L"%ls: Not inside of function\n"), cmd);
builtin_print_help(parser, streams, cmd, streams.err);
return STATUS_CMD_ERROR;
}
// Skip everything up to and including the function block.
for (size_t i = 0; i <= function_block_idx; i++) {
block_t *b = parser.block_at_index(i);
b->skip = true;
}
return retval;
}

9
src/builtin_return.h Normal file
View File

@ -0,0 +1,9 @@
// Prototypes for executing builtin_return function.
#ifndef FISH_BUILTIN_RETURN_H
#define FISH_BUILTIN_RETURN_H
class parser_t;
struct io_streams_t;
int builtin_return(parser_t &parser, io_streams_t &streams, wchar_t **argv);
#endif