Bayeux  3.4.1
Core Foundation library for SuperNEMO
basic_shell.h
Go to the documentation of this file.
1 //
4 // Copyright (c) 2015 by François Mauger <mauger@lpccaen.in2p3.fr>
5 //
6 // This file is part of datatools.
7 //
8 // datatools is free software: you can redistribute it and/or modify
9 // it under the terms of the GNU General Public License as published by
10 // the Free Software Foundation, either version 3 of the License, or
11 // (at your option) any later version.
12 //
13 // datatools is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 // GNU General Public License for more details.
17 //
18 // You should have received a copy of the GNU General Public License
19 // along with datatools. If not, see <http://www.gnu.org/licenses/>.
20 
21 #ifndef DATATOOLS_UI_BASIC_SHELL_H
22 #define DATATOOLS_UI_BASIC_SHELL_H
23 
24 // Standard libraries:
25 #include <cstdlib>
26 #include <string>
27 #include <iostream>
28 #include <sstream>
29 #include <exception>
30 #include <vector>
31 #include <memory>
32 
33 // Third Party:
34 // - Boost:
35 #include <boost/optional.hpp>
36 #include <boost/utility.hpp>
37 
38 // This project:
39 #include <datatools/logger.h>
42 #include <datatools/properties.h>
43 #include <datatools/version_id.h>
44 #include <datatools/handle.h>
46 #include <datatools/i_tree_dump.h>
47 #include <datatools/bit_mask.h>
48 #include <datatools/ui/ihs.h>
50 
51 namespace datatools {
52 
53  namespace ui {
54 
55  class shell_command_interface;
56 
60  , private boost::noncopyable
61  {
62  public:
63 
65 
68  RC_NONE = 0,
72  };
73 
74  static const unsigned int DEFAULT_HISTORY_TRUNCATE = 200;
75 
77  static const std::string & system_interface_name();
78 
80  static const std::string & default_prompt();
81 
83  static const std::string & default_continuation_prompt();
84 
86  basic_shell(const std::string & name_ = "");
87 
89  virtual ~basic_shell();
90 
93 
96 
98  bool has_name() const;
99 
101  void set_name(const std::string &);
102 
104  const std::string & get_name() const;
105 
107  bool has_title() const;
108 
110  void set_title(const std::string &);
111 
113  const std::string & get_title() const;
114 
116  bool has_version() const;
117 
119  void set_version(const ::datatools::version_id &);
120 
122  const ::datatools::version_id & get_version() const;
123 
136  void set_prompt(const std::string &);
137 
139  const std::string & get_prompt() const;
140 
143  void set_continuation_prompt(const std::string &);
144 
146  const std::string & get_continuation_prompt() const;
147 
149  std::string get_effective_prompt() const;
150 
152  std::string get_effective_continuation_prompt() const;
153 
155  bool has_user() const;
156 
158  void set_user(const std::string &);
159 
161  const std::string & get_user() const;
162 
164  bool has_host() const;
165 
167  void set_host(const std::string &);
168 
170  const std::string & get_host() const;
171 
173  void set_exit_on_error(bool);
174 
176  bool is_exit_on_error() const;
177 
179  void set_using_splash(bool);
180 
182  bool is_using_splash() const;
183 
185  void set_using_readline(bool);
186 
188  bool is_using_readline() const;
189 
191  void set_using_history(bool);
192 
194  bool is_using_history() const;
195 
197  void set_history_filename(const std::string &);
198 
200  const std::string & get_history_filename() const;
201 
203  void set_history_truncate(unsigned int);
204 
206  unsigned int get_history_truncate();
207 
210 
212  bool is_history_add_only_on_success() const;
213 
215  bool has_default_path() const;
216 
218  void set_default_path(const std::string &);
219 
221  const std::string & get_default_path() const;
222 
224  bool has_current_path() const;
225 
227  void set_current_path(const std::string & p_);
228 
230  const std::string & get_current_path() const;
231 
233  bool has_services() const;
234 
236  void set_services(::datatools::service_manager & services_);
237 
240 
242  const ::datatools::service_manager & get_services() const;
243 
245  void reset_services();
246 
248  bool has_ihs() const;
249 
251  void set_ihs(ihs & ihs_);
252 
254  ihs & grab_ihs();
255 
257  const ihs & get_ihs() const;
258 
260  void builtin_command_names(std::vector<std::string> & cmd_names_) const;
261 
263  bool has_builtin_command(const std::string & cmd_name_) const;
264 
266  const base_command & get_builtin_command(const std::string & cmd_name_) const;
267 
269  bool is_initialized() const;
270 
272  void initialize_simple();
273 
275  void initialize(const datatools::properties &);
276 
278  void reset();
279 
281  int run(std::istream * in_ = nullptr);
282 
284  static void make_title_upper(const std::string & title_,
285  std::string & upper_);
286 
288  std::string canonical_path(const std::string & path_) const;
289 
291  bool is_absolute_path(const std::string & path_) const;
292 
294  bool is_stop_requested() const;
295 
297  void set_stop_requested();
298 
300  void load_macro(const std::string & macro_, uint32_t flags_ = 0);
301 
303  void build_current_list_of_interfaces(std::vector<std::string> & names_,
304  uint32_t flags_ = 0,
305  std::size_t depth_ = 0) const;
306 
308  bool has_system_interface() const;
309 
312 
324  virtual void print_tree(std::ostream & out_ = std::clog,
325  const boost::property_tree::ptree & options_ = datatools::i_tree_dumpable::empty_options()) const;
326 
327  protected:
328 
330  void _set_defaults();
331 
333  void _expand_prompt(const std::string &, std::string &) const;
334 
336  void _print_splash(std::ostream & out_ = std::cout);
337 
339  void _print_help(std::ostream & out_ = std::cout);
340 
342  virtual void _at_init(const datatools::properties & config_);
343 
345  virtual void _at_reset();
346 
348  virtual void _at_run_start();
349 
351  datatools::command::returned_info _run_core(std::istream * in_ = nullptr,
352  uint32_t flags_ = RC_NONE);
353 
355  datatools::command::returned_info _run_command(const std::string & command_line_);
356 
358  virtual void _at_run_stop();
359 
362 
365 
368  virtual bool _compute_continue_condition();
369 
370  private:
371 
372  // Control/management:
373  bool _initialized_;
374  datatools::logger::priority _logging_;
375 
376  // Configuration:
377  std::string _name_;
378  std::string _title_;
379  boost::optional<version_id> _version_;
380  std::string _prompt_;
381  std::string _continuation_prompt_;
382  std::string _default_path_;
383  std::string _user_;
384  std::string _host_;
385  bool _exit_on_error_;
386  bool _using_splash_;
387  bool _using_readline_;
388  bool _using_history_;
389  bool _history_add_only_on_success_;
390  std::string _history_filename_;
391  unsigned int _history_truncate_;
392  datatools::service_manager * _services_;
393 
394  // Working data:
395  ihs * _ihs_;
396  std::string _current_working_path_;
397  std::unique_ptr<shell_command_interface_type> _system_interface_;
398  shell_command_interface_type * _external_system_interface_;
399 
400  // Private data:
401  struct pimpl_type;
402  std::unique_ptr<pimpl_type> _pimpl_;
403 
405  pimpl_type & _grab_pimpl();
406 
408  const pimpl_type & _get_pimpl() const;
409 
410  // Shell command interface has special priviledeges:
411  // friend class shell_command_interface;
412 
413  };
414 
415  } // namespace ui
416 
417 } // namespace datatools
418 
419 #endif // DATATOOLS_UI_BASIC_SHELL_H
420 
421 // Local Variables: --
422 // mode: c++ --
423 // c-file-style: "gnu" --
424 // tab-width: 2 --
425 // End: --
static const boost::property_tree::ptree & empty_options()
static const std::string & system_interface_name()
Retour the name of the builtin system interface.
Interface Hierarchy System.
Definition: ihs.h:49
const ::datatools::version_id & get_version() const
Return the version.
datatools::logger::priority get_logging() const
Return the logging priority threshold.
bool has_host() const
Check if the host identifier is set.
void set_logging(datatools::logger::priority)
Set the logging priority threshold.
bool has_builtin_command(const std::string &cmd_name_) const
Check if a builtin command is available.
basic_shell(const std::string &name_="")
Default constructor.
priority
Priority levels for logging from most to least critical.
Definition: logger.h:82
void reset_services()
Reset the handle.
bool is_exit_on_error() const
Return the abort-on-error flag.
virtual void _at_reset()
At reset.
int run(std::istream *in_=nullptr)
Main run loop.
void set_exit_on_error(bool)
Set the abort-on-error flag.
void set_using_splash(bool)
Set the using splash.
An interface with utilities for printable objects.
Definition: i_tree_dump.h:36
static const uint32_t bit01
Definition: bit_mask.h:28
void set_ihs(ihs &ihs_)
Set the handle to an external IHS.
std::string canonical_path(const std::string &path_) const
Build the canonical full path associated to a given relative or absolute path.
const std::string & get_history_filename() const
Return the history filename.
virtual void _at_init(const datatools::properties &config_)
At initialization.
void set_services(::datatools::service_manager &services_)
Set the handle to an external service manager.
void set_user(const std::string &)
Set the user identifier.
const std::string & get_user() const
Return the user identifier.
Target command interface.
bool is_initialized() const
Check initialization.
Inhibit the history.
Definition: basic_shell.h:69
void set_stop_requested()
Set the stop requested flag.
run_core_flags
Special flags for run core.
Definition: basic_shell.h:67
bool has_services() const
Check if services are defined.
const std::string & get_continuation_prompt() const
Return the continuation prompt.
void set_default_path(const std::string &)
Set the default path.
Command returned information.
Definition: command_utils.h:78
const std::string & get_prompt() const
Return the prompt.
std::string get_effective_continuation_prompt() const
Return the current effective prompt.
bool is_absolute_path(const std::string &path_) const
Check if a path is an absolute existing path.
bool has_ihs() const
Check if IHS is set.
virtual void _at_run_start()
At run start.
bool is_using_history() const
Return the using history flag.
std::string get_effective_prompt() const
Return the current effective prompt.
datatools::command::returned_info _run_command(const std::string &command_line_)
Run command.
bool is_stop_requested() const
Check if stop is requested.
void set_name(const std::string &)
Set the name:
void set_version(const ::datatools::version_id &)
Set the version.
Normal run.
Definition: basic_shell.h:68
virtual void _at_run_stop()
At run stop.
const std::string & get_name() const
Return the name.
const ::datatools::service_manager & get_services() const
Return a non mutable reference to the service manager.
void _set_defaults()
Set default values to attributes.
bool has_name() const
Check is the name is set.
::datatools::service_manager & grab_services()
Return a mutable reference to the service manager.
bool has_default_path() const
Check if the default path is set.
bool is_history_add_only_on_success() const
Return the flag to add only valid command line in history.
static const uint32_t bit02
Definition: bit_mask.h:29
Inhibit readline (only interactive session)
Definition: basic_shell.h:70
void _print_help(std::ostream &out_=std::cout)
Print online help.
void set_history_truncate(unsigned int)
Set the history truncate.
virtual ~basic_shell()
Destructor.
void set_system_interface(shell_command_interface_type &)
Set an external system interface.
unsigned int get_history_truncate()
Return the history truncate.
void initialize_simple()
Initialization.
const base_command & get_builtin_command(const std::string &cmd_name_) const
Return a const reference to a builtin command.
datatools::command::returned_info _run_core(std::istream *in_=nullptr, uint32_t flags_=RC_NONE)
At run loop.
void set_using_readline(bool)
Set the using readline.
Base command.
Definition: base_command.h:48
const std::string & get_default_path() const
Return the default path.
target_command_interface< basic_shell > shell_command_interface_type
Definition: basic_shell.h:64
void set_prompt(const std::string &)
The interface hierachical system.
bool has_version() const
Check if version identifier is set.
static const std::string & default_prompt()
Retour the default prompt.
void load_macro(const std::string &macro_, uint32_t flags_=0)
Load a macro file.
void _print_splash(std::ostream &out_=std::cout)
Print the splash screen.
void initialize(const datatools::properties &)
Initialization.
static const std::string & default_continuation_prompt()
Retour the default continuation prompt.
void set_using_history(bool)
Set the using history.
The Bayeux/datatools library top-level namespace.
Definition: algo.h:13
Utilities for logging information.
void set_current_path(const std::string &p_)
Set the current path.
virtual bool _compute_continue_condition()
void set_continuation_prompt(const std::string &)
void _expand_prompt(const std::string &, std::string &) const
Update the effective prompt.
bool has_system_interface() const
Check if system interface is set.
static void make_title_upper(const std::string &title_, std::string &upper_)
Build a upper and spaced title.
const std::string & get_title() const
Return the title.
void set_host(const std::string &)
Set the host identifier.
shell_command_interface_type & _grab_system_interface()
Return the system interface.
bool has_title() const
Check is the title is set.
static const uint32_t bit00
Definition: bit_mask.h:27
void builtin_command_names(std::vector< std::string > &cmd_names_) const
Build the list of builtin commands.
bool is_using_readline() const
Return the using readline flag.
void set_history_add_only_on_success(bool)
Set the flag to add only valid command line in history.
virtual void print_tree(std::ostream &out_=std::clog, const boost::property_tree::ptree &options_=datatools::i_tree_dumpable::empty_options()) const
static const unsigned int DEFAULT_HISTORY_TRUNCATE
Definition: basic_shell.h:74
ihs & grab_ihs()
Return a mutable reference to the IHS.
void set_title(const std::string &)
Set the title:
const ihs & get_ihs() const
Return a non mutable reference to the IHS.
const shell_command_interface_type & _get_system_interface() const
Return the system interface.
bool has_user() const
Check if the user identifier is set.
void set_history_filename(const std::string &)
Set the history filename.
void build_current_list_of_interfaces(std::vector< std::string > &names_, uint32_t flags_=0, std::size_t depth_=0) const
Build list of interfaces.
Service management class.
Definition: service_manager.h:57
const std::string & get_host() const
Return the host identifier.
bool is_using_splash() const
Return the using splash flag.
const std::string & get_current_path() const
Return the current path.
bool has_current_path() const
Check if the current path is set.
Command line shell interface.
Definition: basic_shell.h:58
A dictionary of arbitrary properties.
Definition: properties.h:125
Force the shell to exit when an error is met.
Definition: basic_shell.h:71