split builtin builtin into its own module

This commit is contained in:
Kurtis Rader 2017-06-13 22:03:27 -07:00
parent 551bd39889
commit bda6426bf7
4 changed files with 94 additions and 49 deletions

View File

@ -103,7 +103,7 @@ FISH_OBJS := obj/autoload.o obj/builtin.o obj/builtin_bind.o obj/builtin_block.o
obj/builtin_history.o obj/builtin_status.o obj/builtin_read.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 \
obj/builtin_command.o \
obj/builtin_builtin.o obj/builtin_command.o \
obj/builtin_complete.o obj/builtin_jobs.o obj/builtin_printf.o \
obj/builtin_set.o obj/builtin_set_color.o obj/builtin_string.o \
obj/builtin_test.o obj/builtin_ulimit.o obj/color.o obj/common.o \

View File

@ -31,6 +31,7 @@
#include "builtin.h"
#include "builtin_bind.h"
#include "builtin_block.h"
#include "builtin_builtin.h"
#include "builtin_cd.h"
#include "builtin_command.h"
#include "builtin_commandline.h"
@ -238,54 +239,6 @@ void builtin_missing_argument(parser_t &parser, io_streams_t &streams, const wch
builtin_print_help(parser, streams, cmd, streams.err);
}
/// The builtin builtin, used for giving builtins precedence over functions. Mostly handled by the
/// parser. All this code does is some additional operational modes, such as printing a list of all
/// builtins, printing help, etc.
static int builtin_builtin(parser_t &parser, io_streams_t &streams, wchar_t **argv) {
int argc = builtin_count_args(argv);
int list = 0;
static const wchar_t *short_options = L"hn";
static const struct woption long_options[] = {
{L"names", no_argument, 0, 'n'}, {L"help", no_argument, 0, 'h'}, {0, 0, 0, 0}};
int opt;
wgetopter_t w;
while ((opt = w.wgetopt_long(argc, argv, short_options, long_options, NULL)) != -1) {
switch (opt) {
case 'h': {
builtin_print_help(parser, streams, argv[0], streams.out);
return STATUS_CMD_OK;
}
case 'n': {
list = 1;
break;
}
case '?': {
builtin_unknown_option(parser, streams, argv[0], argv[w.woptind - 1]);
return STATUS_INVALID_ARGS;
}
default: {
DIE("unexpected retval from wgetopt_long");
break;
}
}
}
if (list) {
wcstring_list_t names = builtin_get_names();
sort(names.begin(), names.end());
for (size_t i = 0; i < names.size(); i++) {
const wchar_t *el = names.at(i).c_str();
streams.out.append(el);
streams.out.append(L"\n");
}
}
return STATUS_CMD_OK;
}
/// A generic bultin that only supports showing a help message. This is only a placeholder that
/// prints the help message. Useful for commands that live in the parser.
static int builtin_generic(parser_t &parser, io_streams_t &streams, wchar_t **argv) {

83
src/builtin_builtin.cpp Normal file
View File

@ -0,0 +1,83 @@
// Implementation of the builtin builtin.
#include "config.h" // IWYU pragma: keep
#include <unistd.h>
#include "builtin.h"
#include "builtin_builtin.h"
#include "common.h"
#include "fallback.h" // IWYU pragma: keep
#include "io.h"
#include "path.h"
#include "wgetopt.h"
#include "wutil.h" // IWYU pragma: keep
struct cmd_opts {
bool print_help = false;
bool list_names = false;
};
static const wchar_t *short_options = L"hn";
static const struct woption long_options[] = {
{L"help", no_argument, NULL, 'h'}, {L"names", no_argument, NULL, 'n'}, {NULL, 0, NULL, 0}};
static int parse_cmd_opts(struct cmd_opts *opts, int *optind, int argc, wchar_t **argv,
parser_t &parser, io_streams_t &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) {
case 'h': {
opts->print_help = true;
return STATUS_CMD_OK;
}
case 'n': {
opts->list_names = true;
break;
}
case '?': {
builtin_unknown_option(parser, streams, cmd, argv[w.woptind - 1]);
return STATUS_INVALID_ARGS;
}
default: {
DIE("unexpected retval from wgetopt_long");
break;
}
}
}
*optind = w.woptind;
return STATUS_CMD_OK;
}
/// The builtin builtin, used for giving builtins precedence over functions. Mostly handled by the
/// parser. All this code does is some additional operational modes, such as printing a list of all
/// builtins, printing help, etc.
int builtin_builtin(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 (opts.list_names) {
wcstring_list_t names = builtin_get_names();
sort(names.begin(), names.end());
for (size_t i = 0; i < names.size(); i++) {
const wchar_t *el = names.at(i).c_str();
streams.out.append(el);
streams.out.append(L"\n");
}
}
return STATUS_CMD_OK;
}

9
src/builtin_builtin.h Normal file
View File

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