iash  v0.5
Library to provide a bash-like shell in an application
iash.h
1 /* iash -- Internal Applicaiton SHell
2  *
3  * This is a library designed to provide shell functionality
4  * within a terminal application.
5  *
6  * License: GNU GPLv3
7  *
8  * Copyright (C) 2014 Paul Bonnen
9  *
10  * This program is free software: you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation, either version 3 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program. If not, see <http://www.gnu.org/licenses/>.
22  */
23 
24 #ifndef IASH_H
25 #define IASH_H
26 
27 #define IASH_VERSION_MAJOR 0
28 #define IASH_VERSION_MINOR 5
29 #define IASH_VERSION_PATCH 0
30 
31 #include <iostream>
32 #include <string>
33 #include <utility>
34 
35 #include "Environment.h"
36 #include "CommandDispatcher.h"
37 #include "Directory.h"
38 
39 class Command;
40 class UserCommand;
41 
56 class iash
57 {
58  friend class ExitCommand;
59 public:
67  iash(const std::string &appName="iash");
68 
69  //IASH INTERNAL OBJECT RETRIEVAL *******************************************
70 
76  Environment* getEnv ();
77 
87  const Directory* getCwd () const;
88 
89  //COMMAND REGISTRATION INTERFACE *******************************************
90 
108  void addCommand (Command* cmd);
109 
127  template <typename ExtCommand, typename... Args>
128  void addCommand (Args&&... args)
129  {
130  m_dispatcher.registerCommand<ExtCommand>(std::forward<Args>(args)...);
131  }
132 
133  //SHELL INVOCATION *********************************************************
147  int runInteractive ();
148 
166  int runScript (const char *fname);
167 
176  int exec (std::string &cmd);
177 
185  int exec (UserCommand *cmd);
186 private:
187  int run (std::istream& cmdin, bool showPrompt=false);
188  void exitShell ();
189 
190  CommandDispatcher m_dispatcher;
191  Environment m_env;
192  Directory m_iashCwd;
193  std::string m_appName;
194  bool m_exitFlag;
195 };
196 
197 #endif //IASH_H
void registerCommand(Command *cmd)
Registers a given Command with this CommandDispatcher instance.
Definition: CommandDispatcher.cpp:23
iash(const std::string &appName="iash")
Creates an iash shell object with the given application name (for use in the prompt).
Definition: iash.cpp:26
void addCommand(Command *cmd)
Adds a Command instance to this shell&#39;s CommandDispatcher registry.
Definition: iash.cpp:46
Represents a directory on the filesystem.
Definition: Directory.h:26
int runInteractive()
Runs the shell in interactive mode.
Definition: iash.cpp:51
Class to register and execute Commands in an iash shell.
Definition: CommandDispatcher.h:30
Environment * getEnv()
Gets the current Environment object for this shell instance.
Definition: iash.cpp:36
The main shell class for iash.
Definition: iash.h:56
int runScript(const char *fname)
Executes the commands in the specified script and quits.
Definition: iash.cpp:57
int exec(std::string &cmd)
Executes the specified command and quits.
Abstract interface for a command registered with the shell.
Definition: Command.h:56
Used to retain command arguments and the proper input and output streams for a command call...
Definition: UserCommand.h:28
void addCommand(Args &&... args)
Adds a Command instance of the given type to this shell&#39;s CommandDispatcher registry.
Definition: iash.h:128
Environment variable manager for iash.
Definition: Environment.h:33
const Directory * getCwd() const
Gets the current working directory for this shell.
Definition: iash.cpp:41