Tree @HEAD (Download .tar.gz)
- ..
- widecharwidth
- ast.cpp
- ast.h
- ast_node_types.inc
- autoload.cpp
- autoload.h
- builtin.cpp
- builtin.h
- builtin_argparse.cpp
- builtin_argparse.h
- builtin_bg.cpp
- builtin_bg.h
- builtin_bind.cpp
- builtin_bind.h
- builtin_block.cpp
- builtin_block.h
- builtin_builtin.cpp
- builtin_builtin.h
- builtin_cd.cpp
- builtin_cd.h
- builtin_command.cpp
- builtin_command.h
- builtin_commandline.cpp
- builtin_commandline.h
- builtin_complete.cpp
- builtin_complete.h
- builtin_contains.cpp
- builtin_contains.h
- builtin_disown.cpp
- builtin_disown.h
- builtin_echo.cpp
- builtin_echo.h
- builtin_emit.cpp
- builtin_emit.h
- builtin_eval.cpp
- builtin_eval.h
- builtin_exit.cpp
- builtin_exit.h
- builtin_fg.cpp
- builtin_fg.h
- builtin_function.cpp
- builtin_function.h
- builtin_functions.cpp
- builtin_functions.h
- builtin_history.cpp
- builtin_history.h
- builtin_jobs.cpp
- builtin_jobs.h
- builtin_math.cpp
- builtin_math.h
- builtin_printf.cpp
- builtin_printf.h
- builtin_pwd.cpp
- builtin_pwd.h
- builtin_random.cpp
- builtin_random.h
- builtin_read.cpp
- builtin_read.h
- builtin_realpath.cpp
- builtin_realpath.h
- builtin_return.cpp
- builtin_return.h
- builtin_set.cpp
- builtin_set.h
- builtin_set_color.cpp
- builtin_set_color.h
- builtin_source.cpp
- builtin_source.h
- builtin_status.cpp
- builtin_status.h
- builtin_string.cpp
- builtin_string.h
- builtin_test.cpp
- builtin_test.h
- builtin_type.cpp
- builtin_type.h
- builtin_ulimit.cpp
- builtin_ulimit.h
- builtin_wait.cpp
- builtin_wait.h
- color.cpp
- color.h
- common.cpp
- common.h
- complete.cpp
- complete.h
- enum_set.h
- env.cpp
- env.h
- env_dispatch.cpp
- env_dispatch.h
- env_universal_common.cpp
- env_universal_common.h
- event.cpp
- event.h
- exec.cpp
- exec.h
- expand.cpp
- expand.h
- fallback.cpp
- fallback.h
- fd_monitor.cpp
- fd_monitor.h
- fds.cpp
- fds.h
- fish.cpp
- fish_indent.cpp
- fish_key_reader.cpp
- fish_test_helper.cpp
- fish_tests.cpp
- fish_version.cpp
- fish_version.h
- flog.cpp
- flog.h
- function.cpp
- function.h
- future_feature_flags.cpp
- future_feature_flags.h
- global_safety.h
- highlight.cpp
- highlight.h
- history.cpp
- history.h
- history_file.cpp
- history_file.h
- input.cpp
- input.h
- input_common.cpp
- input_common.h
- intern.cpp
- intern.h
- io.cpp
- io.h
- iothread.cpp
- iothread.h
- job_group.cpp
- job_group.h
- kill.cpp
- kill.h
- lru.h
- maybe.h
- null_terminated_array.cpp
- null_terminated_array.h
- operation_context.cpp
- operation_context.h
- output.cpp
- output.h
- pager.cpp
- pager.h
- parse_constants.h
- parse_execution.cpp
- parse_execution.h
- parse_tree.cpp
- parse_tree.h
- parse_util.cpp
- parse_util.h
- parser.cpp
- parser.h
- parser_keywords.cpp
- parser_keywords.h
- path.cpp
- path.h
- postfork.cpp
- postfork.h
- print_help.cpp
- print_help.h
- proc.cpp
- proc.h
- reader.cpp
- reader.h
- redirection.cpp
- redirection.h
- sanity.cpp
- sanity.h
- screen.cpp
- screen.h
- signal.cpp
- signal.h
- termsize.cpp
- termsize.h
- timer.cpp
- timer.h
- tinyexpr.cpp
- tinyexpr.h
- tokenizer.cpp
- tokenizer.h
- topic_monitor.cpp
- topic_monitor.h
- trace.cpp
- trace.h
- utf8.cpp
- utf8.h
- util.cpp
- util.h
- wcstringutil.cpp
- wcstringutil.h
- wgetopt.cpp
- wgetopt.h
- wildcard.cpp
- wildcard.h
- wutil.cpp
- wutil.h
redirection.h @HEAD — raw · history · blame
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 | #ifndef FISH_REDIRECTION_H #define FISH_REDIRECTION_H #include <vector> #include "common.h" #include "maybe.h" /// This file supports specifying and applying redirections. enum class redirection_mode_t { overwrite, // normal redirection: > file.txt append, // appending redirection: >> file.txt input, // input redirection: < file.txt fd, // fd redirection: 2>&1 noclob // noclobber redirection: >? file.txt }; class io_chain_t; /// A struct which represents a redirection specification from the user. /// Here the file descriptors don't represent open files - it's purely textual. struct redirection_spec_t { /// The redirected fd, or -1 on overflow. /// In the common case of a pipe, this is 1 (STDOUT_FILENO). /// For example, in the case of "3>&1" this will be 3. int fd{-1}; /// The redirection mode. redirection_mode_t mode{redirection_mode_t::overwrite}; /// The target of the redirection. /// For example in "3>&1", this will be "1". /// In "< file.txt" this will be "file.txt". wcstring target{}; /// \return if this is a close-type redirection. bool is_close() const { return mode == redirection_mode_t::fd && target == L"-"; } /// Attempt to parse target as an fd. Return the fd, or none() if none. maybe_t<int> get_target_as_fd() const; /// \return the open flags for this redirection. int oflags() const; redirection_spec_t(int fd, redirection_mode_t mode, wcstring target) : fd(fd), mode(mode), target(std::move(target)) {} }; using redirection_spec_list_t = std::vector<redirection_spec_t>; /// A class representing a sequence of basic redirections. class dup2_list_t { public: /// A type that represents the action dup2(src, target). /// If target is negative, this represents close(src). /// Note none of the fds here are considered 'owned'. struct action_t { int src; int target; }; private: /// The list of actions. std::vector<action_t> actions_; /// Append a dup2 action. void add_dup2(int src, int target) { assert(src >= 0 && target >= 0 && "Invalid fd in add_dup2"); // Note: record these even if src and target is the same. // This is a note that we must clear the CLO_EXEC bit. actions_.push_back(action_t{src, target}); } /// Append a close action. void add_close(int fd) { assert(fd >= 0 && "Invalid fd in add_close"); actions_.push_back(action_t{fd, -1}); } dup2_list_t() = default; public: ~dup2_list_t(); /// Disable copying. dup2_list_t(const dup2_list_t &) = delete; void operator=(const dup2_list_t &) = delete; dup2_list_t(dup2_list_t &&) = default; dup2_list_t &operator=(dup2_list_t &&) = default; /// \return the list of dup2 actions. const std::vector<action_t> &get_actions() const { return actions_; } /// Produce a dup_fd_list_t from an io_chain. This may not be called before fork(). /// The result contains the list of fd actions (dup2 and close), as well as the list /// of fds opened. static dup2_list_t resolve_chain(const io_chain_t &); /// \return the fd ultimately dup'd to a target fd, or -1 if the target is closed. /// For example, if target fd is 1, and we have a dup2 chain 5->3 and 3->1, then we will /// return 5. If the target is not referenced in the chain, returns target. int fd_for_target_fd(int target) const; }; #endif |