ArduinoLibs
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
Shell.h
1 /*
2  * Copyright (C) 2016 Southern Storm Software, Pty Ltd.
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included
12  * in all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
20  * DEALINGS IN THE SOFTWARE.
21  */
22 
23 #ifndef SHELL_h
24 #define SHELL_h
25 
26 #include "Terminal.h"
27 #include <Client.h>
28 
29 class Shell;
30 class ShellArguments;
31 class LoginShell;
32 
33 #if defined(__arm__)
34 #define SHELL_MAX_CMD_LEN 256
35 #else
36 #define SHELL_MAX_CMD_LEN 64
37 #endif
38 
39 typedef void (*ShellCommandFunc)(Shell &shell, int argc, const ShellArguments &argv);
40 
43 typedef struct
44 {
45  const char *name;
46  const char *help;
47  ShellCommandFunc func;
48 
49 } ShellCommandInfo;
50 
51 class ShellCommandRegister
52 {
53 public:
54  inline ShellCommandRegister(const ShellCommandInfo *_info);
55 
56  const ShellCommandInfo *info;
57  ShellCommandRegister *next;
58 };
59 
62 class Shell : public Terminal
63 {
64 public:
65  Shell();
66  virtual ~Shell();
67 
68  bool begin(Stream &stream, size_t maxHistory = 0, Terminal::Mode mode = Serial);
69  bool begin(Client &client, size_t maxHistory = 0, Terminal::Mode mode = Telnet);
70  void end();
71 
72  void loop();
73 
74  static void registerCommand(ShellCommandRegister *cmd);
75 
76  const char *prompt() const { return prom; }
77  void setPrompt(const char *prompt) { prom = prompt; }
78 
79  int userid() const { return uid; }
80  void setUserid(int userid) { uid = userid; }
81 
82  void help();
83  void exit();
84 
85 protected:
86  virtual void beginSession();
87  virtual void printPrompt();
88  virtual void execute();
89 
90 private:
91  char buffer[SHELL_MAX_CMD_LEN];
92  size_t curStart;
93  size_t curLen;
94  size_t curMax;
95  char *history;
96  size_t historyWrite;
97  size_t historyRead;
98  size_t historySize;
99  const char *prom;
100  bool isClient;
101  uint8_t lineMode;
102  int uid;
103  unsigned long timer;
104 
105  // Disable copy constructor and operator=().
106  Shell(const Shell &other) {}
107  Shell &operator=(const Shell &) { return *this; }
108 
109  bool beginShell(Stream &stream, size_t maxHistory, Terminal::Mode mode);
110  bool execute(const ShellArguments &argv);
111  void executeBuiltin(const char *cmd);
112  void clearCharacters(size_t len);
113  void changeHistory(bool up);
114  void clearHistory();
115 
116  friend class LoginShell;
117 };
118 
120 {
121  friend class Shell;
122 private:
123  ShellArguments(char *buffer, size_t len);
124  ~ShellArguments() {}
125 public:
126 
127  int count() const { return argc; }
128  const char *operator[](int index) const;
129 
130 private:
131  const char *line;
132  size_t size;
133  int argc;
134  mutable int currentIndex;
135  mutable size_t currentPosn;
136 
137  // Disable copy constructor and operator=().
138  ShellArguments(const ShellArguments &other) {}
139  ShellArguments &operator=(const ShellArguments &) { return *this; }
140 };
141 
144 inline ShellCommandRegister::ShellCommandRegister(const ShellCommandInfo *_info)
145  : info(_info)
146  , next(0)
147 {
149 }
150 
153 #define ShellCommand(name,help,function) \
154  static char const shell_id_##name[] PROGMEM = #name; \
155  static char const shell_help_##name[] PROGMEM = help; \
156  static ShellCommandInfo const shell_info_##name PROGMEM = { \
157  shell_id_##name, \
158  shell_help_##name, \
159  (function) \
160  }; \
161  static ShellCommandRegister shell_cmd_##name(&shell_info_##name)
162 
163 #endif
const char * operator[](int index) const
Gets a specific argument for the command.
Definition: Shell.cpp:927
virtual void beginSession()
Begins a login session.
Definition: Shell.cpp:594
void help()
Displays help for all supported commands.
Definition: Shell.cpp:542
bool begin(Stream &stream, size_t maxHistory=0, Terminal::Mode mode=Serial)
Begin shell handling on an underlying character stream.
Definition: Shell.cpp:176
virtual void printPrompt()
Prints the current prompt string.
Definition: Shell.cpp:603
static void registerCommand(ShellCommandRegister *cmd)
Registers a command with the shell.
Definition: Shell.cpp:472
Operates the terminal in serial mode.
Definition: Terminal.h:44
void exit()
Exit from the shell back to the login prompt.
Definition: Shell.cpp:577
Operates the terminal in telnet mode.
Definition: Terminal.h:45
Terminal::Mode mode() const
Returns the mode this terminal is operating in, Serial or Telnet.
Definition: Terminal.h:52
void loop()
Performs regular activities on the shell.
Definition: Shell.cpp:294
void setPrompt(const char *prompt)
Sets the prompt string to display in the shell.
Definition: Shell.h:77
Extended stream interface for terminal operations.
Definition: Terminal.h:36
Shell()
Constructs a new Shell instance.
Definition: Shell.cpp:122
Mode
Mode to operate in, Serial or Telnet.
Definition: Terminal.h:42
Stream * stream() const
Returns a pointer to the underlying Stream, or NULL if the stream has not been set with begin() yet...
Definition: Terminal.h:51
ShellCommandFunc
Type of functions that provide shell command handlers.
int count() const
Returns the number of arguments, including the name of the command.
Definition: Shell.h:127
const char * prompt() const
Gets the prompt string to display in the shell.
Definition: Shell.h:76
void setUserid(int userid)
Sets the user identifier for the currently logged in user.
Definition: Shell.h:80
virtual void execute()
Executes the command in the buffer.
Definition: Shell.cpp:613
Convenience class that encapsulates an array of shell command arguments.
Definition: Shell.h:119
Command-line shell access via a login shell.
Definition: LoginShell.h:30
void end()
Ends shell processing on the underlying stream.
Definition: Shell.cpp:262
virtual ~Shell()
Destroys this Shell object.
Definition: Shell.cpp:141
Command-line shell access.
Definition: Shell.h:62
int userid() const
Gets the user identifier for the currently logged in user, or -1 if there is no user logged in curren...
Definition: Shell.h:79