split builtin pwd into its own module

This commit is contained in:
Kurtis Rader 2017-06-14 13:31:05 -07:00
parent 175570b7ed
commit 070d204d9b
5 changed files with 89 additions and 20 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 \ 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_commandline.o obj/builtin_emit.o obj/builtin_functions.o \
obj/builtin_history.o obj/builtin_status.o obj/builtin_read.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_source.o obj/builtin_random.o obj/builtin_echo.o \
obj/builtin_cd.o obj/builtin_disown.o obj/builtin_function.o \ obj/builtin_cd.o obj/builtin_disown.o obj/builtin_function.o \
obj/builtin_builtin.o obj/builtin_command.o obj/builtin_contains.o \ obj/builtin_builtin.o obj/builtin_command.o obj/builtin_contains.o \

View file

@ -44,6 +44,7 @@
#include "builtin_history.h" #include "builtin_history.h"
#include "builtin_jobs.h" #include "builtin_jobs.h"
#include "builtin_printf.h" #include "builtin_printf.h"
#include "builtin_pwd.h"
#include "builtin_random.h" #include "builtin_random.h"
#include "builtin_read.h" #include "builtin_read.h"
#include "builtin_set.h" #include "builtin_set.h"
@ -278,24 +279,6 @@ static int builtin_generic(parser_t &parser, io_streams_t &streams, wchar_t **ar
return STATUS_CMD_ERROR; return STATUS_CMD_ERROR;
} }
/// The pwd builtin. We don't respect -P to resolve symbolic links because we
/// try to always resolve them.
static int builtin_pwd(parser_t &parser, io_streams_t &streams, wchar_t **argv) {
UNUSED(parser);
if (argv[1] != NULL) {
streams.err.append_format(BUILTIN_ERR_ARG_COUNT1, argv[0], 0, builtin_count_args(argv));
return STATUS_INVALID_ARGS;
}
wcstring res = wgetcwd();
if (res.empty()) {
return STATUS_CMD_ERROR;
}
streams.out.append(res);
streams.out.push_back(L'\n');
return STATUS_CMD_OK;
}
/// The exit builtin. Calls reader_exit to exit and returns the value specified. /// The exit builtin. Calls reader_exit to exit and returns the value specified.
static int builtin_exit(parser_t &parser, io_streams_t &streams, wchar_t **argv) { static int builtin_exit(parser_t &parser, io_streams_t &streams, wchar_t **argv) {
int argc = builtin_count_args(argv); int argc = builtin_count_args(argv);

View file

@ -37,7 +37,7 @@ static int parse_cmd_opts(struct cmd_opts *opts, int *optind, int argc, wchar_t
return STATUS_CMD_OK; return STATUS_CMD_OK;
} }
case '?': { case '?': {
builtin_unknown_option(parser, streams, argv[0], argv[w.woptind - 1]); builtin_unknown_option(parser, streams, cmd, argv[w.woptind - 1]);
return STATUS_INVALID_ARGS; return STATUS_INVALID_ARGS;
} }
default: { default: {

77
src/builtin_pwd.cpp Normal file
View file

@ -0,0 +1,77 @@
// Implementation of the pwd builtin.
#include "config.h" // IWYU pragma: keep
#include <stddef.h>
#include "builtin.h"
#include "builtin_pwd.h"
#include "common.h"
#include "event.h"
#include "fallback.h" // IWYU pragma: keep
#include "io.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,
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) { //!OCLINT(too few branches)
case 'h': {
opts->print_help = true;
return STATUS_CMD_OK;
}
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 pwd builtin. We don't respect -P to resolve symbolic links because we
/// try to always resolve them.
int builtin_pwd(parser_t &parser, io_streams_t &streams, wchar_t **argv) {
UNUSED(parser);
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 != argc) {
streams.err.append_format(BUILTIN_ERR_ARG_COUNT1, cmd, 0, argc - 1);
return STATUS_INVALID_ARGS;
}
wcstring res = wgetcwd();
if (res.empty()) {
return STATUS_CMD_ERROR;
}
streams.out.append(res);
streams.out.push_back(L'\n');
return STATUS_CMD_OK;
}

9
src/builtin_pwd.h Normal file
View file

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