From 070d204d9b404bde240549baea013157c258d6e8 Mon Sep 17 00:00:00 2001 From: Kurtis Rader Date: Wed, 14 Jun 2017 13:31:05 -0700 Subject: [PATCH] split builtin pwd into its own module --- Makefile.in | 2 +- src/builtin.cpp | 19 +---------- src/builtin_disown.cpp | 2 +- src/builtin_pwd.cpp | 77 ++++++++++++++++++++++++++++++++++++++++++ src/builtin_pwd.h | 9 +++++ 5 files changed, 89 insertions(+), 20 deletions(-) create mode 100644 src/builtin_pwd.cpp create mode 100644 src/builtin_pwd.h diff --git a/Makefile.in b/Makefile.in index 0c4a5550e..fb8fd0420 100644 --- a/Makefile.in +++ b/Makefile.in @@ -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_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_cd.o obj/builtin_disown.o obj/builtin_function.o \ obj/builtin_builtin.o obj/builtin_command.o obj/builtin_contains.o \ diff --git a/src/builtin.cpp b/src/builtin.cpp index 01bfbb0ff..9c75a93aa 100644 --- a/src/builtin.cpp +++ b/src/builtin.cpp @@ -44,6 +44,7 @@ #include "builtin_history.h" #include "builtin_jobs.h" #include "builtin_printf.h" +#include "builtin_pwd.h" #include "builtin_random.h" #include "builtin_read.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; } -/// 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. static int builtin_exit(parser_t &parser, io_streams_t &streams, wchar_t **argv) { int argc = builtin_count_args(argv); diff --git a/src/builtin_disown.cpp b/src/builtin_disown.cpp index 7e228c9f2..79cf8c53b 100644 --- a/src/builtin_disown.cpp +++ b/src/builtin_disown.cpp @@ -37,7 +37,7 @@ static int parse_cmd_opts(struct cmd_opts *opts, int *optind, int argc, wchar_t return STATUS_CMD_OK; } 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; } default: { diff --git a/src/builtin_pwd.cpp b/src/builtin_pwd.cpp new file mode 100644 index 000000000..a0ba64d40 --- /dev/null +++ b/src/builtin_pwd.cpp @@ -0,0 +1,77 @@ +// Implementation of the pwd builtin. +#include "config.h" // IWYU pragma: keep + +#include + +#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; +} diff --git a/src/builtin_pwd.h b/src/builtin_pwd.h new file mode 100644 index 000000000..431a52ff5 --- /dev/null +++ b/src/builtin_pwd.h @@ -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