Executor.hh
Go to the documentation of this file.
1 //
2 // Copyright (c) 2020 Fraunhofer Institute for Applied Information Technology (FIT)
3 // Network Research Group (NET)
4 // Schloss Birlinghoven, 53754 Sankt Augustin, GERMANY
5 // Contact: support@wiback.org
6 //
7 // This file is part of the SENF code tree.
8 // It is licensed under the 3-clause BSD License (aka New BSD License).
9 // See LICENSE.txt in the top level directory for details or visit
10 // https://opensource.org/licenses/BSD-3-Clause
11 //
12 
13 
17 #ifndef HH_SENF_Utils_Console_Executor_
18 #define HH_SENF_Utils_Console_Executor_ 1
19 
20 // Custom includes
21 #include <boost/noncopyable.hpp>
22 #include "Parse.hh"
24 #include "Node.hh"
25 
26 //#include "Executor.mpp"
27 //-/////////////////////////////////////////////////////////////////////////////////////////////////
28 
29 namespace senf {
30 namespace console {
31 
48  class Executor
49  : boost::noncopyable
50  {
52  SENF_LOG_DEFAULT_LEVEL( senf::log::VERBOSE );
53  public:
54  //-////////////////////////////////////////////////////////////////////////
55  // Types
56 
58  struct ExitException {};
59 
61  typedef boost::function<void (DirectoryNode &,std::string const &)> SecurityPolicy;
62 
65 
66  typedef void result_type;
67 
68  //-////////////////////////////////////////////////////////////////////////
69  //\/name Structors and default members
70  //\{
71 
72  Executor();
73 
74  //\}
75  //-////////////////////////////////////////////////////////////////////////
76 
77  void execute(std::ostream & output, ParseCommandInfo const & command);
79 
82  void operator()(std::ostream & output, ParseCommandInfo const & command);
84 
86  GenericNode & getNode(ParseCommandInfo const & command);
87  DirectoryNode & cwd() const;
88  void cwd(DirectoryNode & dir);
89  std::string cwdPath() const;
90  bool skipping() const;
91 
92  bool autocd() const;
93 
97  Executor & autocd(bool v);
98 
100  bool autocomplete() const;
101 
106  Executor & autocomplete(bool v);
107 
110  DirectoryNode & chroot() const;
111 
115  Executor & chroot(DirectoryNode & node);
116 
121  Executor & policy(SecurityPolicy policy = SecurityPolicy());
122 
125  protected:
126 
127  private:
128  typedef std::vector<DirectoryNode::weak_ptr> Path;
129 
130  void exec(std::ostream & output, ParseCommandInfo const & command);
131 
132  void cd(ParseCommandInfo::TokensRange dir);
133  void ls(std::ostream & output, ParseCommandInfo::TokensRange dir);
134  void ll(std::ostream & output, ParseCommandInfo::TokensRange dir);
135  void lr(std::ostream & output, ParseCommandInfo::TokensRange dir);
136  void echo(std::ostream & output, ParseCommandInfo::TokensRange args);
137  void pushd(ParseCommandInfo::TokensRange dir);
138  void popd();
139  void exit();
140  void help(std::ostream & output, ParseCommandInfo::TokensRange path);
141 
142  GenericNode & traverseNode(ParseCommandInfo::TokensRange const & path);
143  void traverseDirectory(ParseCommandInfo::TokensRange const & path,
144  Path & dir);
145  std::string complete(DirectoryNode & dir, std::string const & name);
146 
147  struct InvalidPathException {
148  std::string path;
149  InvalidPathException() : path() {}
150  InvalidPathException(std::string path_) : path(path_) {}
151 
152  };
153  struct InvalidDirectoryException {
154  std::string path;
155  InvalidDirectoryException() : path() {}
156  InvalidDirectoryException(std::string path_) : path(path_) {}
157  };
158  struct InvalidCommandException {};
159 
160  DirectoryNode::ptr root_;
161  SecurityPolicy policy_;
162  mutable Path cwd_;
163  Path oldCwd_;
164 
165  typedef std::vector<Path> DirStack;
166  DirStack dirstack_;
167 
168  bool autocd_;
169  bool autocomplete_;
170  };
171 
172  void senf_console_format_value(DirectoryNode::ptr value, std::ostream & os);
173 
174 }}
175 
176 //-/////////////////////////////////////////////////////////////////////////////////////////////////
177 #include "Executor.cci"
178 //#include "Executor.ct"
179 //#include "Executor.cti"
180 #endif
181 
182 
183 // Local Variables:
184 // mode: c++
185 // fill-column: 100
186 // comment-column: 40
187 // c-file-style: "senf"
188 // indent-tabs-mode: nil
189 // ispell-local-dictionary: "american"
190 // compile-command: "scons -u test"
191 // End:
void operator()(std::ostream &output, ParseCommandInfo const &command)
Execute command.
#define SENF_LOG_CLASS_AREA()
boost::shared_ptr< DirectoryNode > ptr
Definition: Node.hh:414
Config/console tree directory node.
Definition: Node.hh:406
Node public header.
void senf_console_format_value(DirectoryNode::ptr value, std::ostream &os)
Definition: Executor.cc:473
void execute(std::ostream &output, ParseCommandInfo const &command)
Execute command.
Definition: Executor.cc:71
Executor & policy(SecurityPolicy policy=SecurityPolicy())
Set security policy.
void complete(LineEditor &editor, Completer completer)
bool skipping() const
true, if currently skipping a directory group
Thrown by the SecurityPolicy to silently ignore a command.
Definition: Executor.hh:64
Definition: Config.hh:28
bool autocd() const
Get current autocd status.
Single parsed console command.
Definition: Parse.hh:363
boost::function< void(DirectoryNode &, std::string const &)> SecurityPolicy
Executor policy function.
Definition: Executor.hh:61
Execute config/console commands.
Definition: Executor.hh:48
Thrown by built-in &#39;exit&#39; command.
Definition: Executor.hh:58
GenericNode & getNode(ParseCommandInfo const &command)
Definition: Executor.cc:169
bool autocomplete() const
Get current autocomplete status.
DirectoryNode & cwd() const
Current working directory.
Definition: Executor.cc:49
boost::iterator_range< token_iterator > TokensRange
Definition: Parse.hh:378
unspecified_keyword_type name
Argument name.
Parse public header.
Config/console node tree base-class.
Definition: Node.hh:250
DirectoryNode & chroot() const
Get root node.
std::string cwdPath() const
Return pathname of current directory.
Definition: Executor.cc:58