A Command Interpreter with support for used defined commands, subsystems, macros, help and parameter parsing.

Committer:
wvd_vegt
Date:
Tue Nov 22 12:57:13 2011 +0000
Revision:
22:5192a468d7fa
Parent:
21:d22068e2bbd1
Child:
23:73a4f087c1b9
Corrected printnerror\s return value
Added printmsg()

Who changed what in which revision?

UserRevisionLine numberNew contents of line
wvd_vegt 20:62318ba945de 1 /* mbed Command Interpreter Library
wvd_vegt 20:62318ba945de 2 * Copyright (c) 2011 wvd_vegt
wvd_vegt 20:62318ba945de 3 *
wvd_vegt 20:62318ba945de 4 * Permission is hereby granted, free of charge, to any person obtaining a copy
wvd_vegt 20:62318ba945de 5 * of this software and associated documentation files (the "Software"), to deal
wvd_vegt 20:62318ba945de 6 * in the Software without restriction, including without limitation the rights
wvd_vegt 20:62318ba945de 7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
wvd_vegt 20:62318ba945de 8 * copies of the Software, and to permit persons to whom the Software is
wvd_vegt 20:62318ba945de 9 * furnished to do so, subject to the following conditions:
wvd_vegt 20:62318ba945de 10 *
wvd_vegt 20:62318ba945de 11 * The above copyright notice and this permission notice shall be included in
wvd_vegt 20:62318ba945de 12 * all copies or substantial portions of the Software.
wvd_vegt 20:62318ba945de 13 *
wvd_vegt 20:62318ba945de 14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
wvd_vegt 20:62318ba945de 15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
wvd_vegt 20:62318ba945de 16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
wvd_vegt 20:62318ba945de 17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
wvd_vegt 20:62318ba945de 18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
wvd_vegt 20:62318ba945de 19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
wvd_vegt 20:62318ba945de 20 * THE SOFTWARE.
wvd_vegt 20:62318ba945de 21 */
wvd_vegt 20:62318ba945de 22
wvd_vegt 20:62318ba945de 23 #ifndef MBED_CMDB_H
wvd_vegt 20:62318ba945de 24 #define MBED_CMDB_H
wvd_vegt 20:62318ba945de 25
wvd_vegt 20:62318ba945de 26 #include "mbed.h"
wvd_vegt 20:62318ba945de 27
wvd_vegt 20:62318ba945de 28 #include <vector>
wvd_vegt 20:62318ba945de 29 #include <limits>
wvd_vegt 20:62318ba945de 30
wvd_vegt 20:62318ba945de 31 //------------------------------------------------------------------------------
wvd_vegt 20:62318ba945de 32
wvd_vegt 20:62318ba945de 33 /** Max size of an Ansi escape code.
wvd_vegt 20:62318ba945de 34 */
wvd_vegt 20:62318ba945de 35 #define MAX_ESC_LEN 5
wvd_vegt 20:62318ba945de 36
wvd_vegt 20:62318ba945de 37 /** Max (strlen) of a Param.
wvd_vegt 20:62318ba945de 38 */
wvd_vegt 20:62318ba945de 39 #define MAX_PARM_LEN 32
wvd_vegt 20:62318ba945de 40
wvd_vegt 20:62318ba945de 41 /** Max eight parms.
wvd_vegt 20:62318ba945de 42 */
wvd_vegt 20:62318ba945de 43 #define MAX_ARGS 8
wvd_vegt 20:62318ba945de 44
wvd_vegt 20:62318ba945de 45 /** Max 132 characters commandline.
wvd_vegt 20:62318ba945de 46 */
wvd_vegt 20:62318ba945de 47 #define MAX_CMD_LEN 132
wvd_vegt 20:62318ba945de 48
wvd_vegt 20:62318ba945de 49 /** 'Show' hidden subsystems and commands.
wvd_vegt 20:62318ba945de 50 */
wvd_vegt 20:62318ba945de 51 #define SHOWHIDDEN
wvd_vegt 20:62318ba945de 52
wvd_vegt 20:62318ba945de 53 /** Enable macro commands.
wvd_vegt 20:62318ba945de 54 */
wvd_vegt 20:62318ba945de 55 #define ENABLEMACROS
wvd_vegt 20:62318ba945de 56
wvd_vegt 20:62318ba945de 57 /** Enable statemachine.
wvd_vegt 20:62318ba945de 58 *
wvd_vegt 20:62318ba945de 59 * Used to implement a series of commands running at power-up.
wvd_vegt 20:62318ba945de 60 *
wvd_vegt 20:62318ba945de 61 * @note Not Implemented!
wvd_vegt 20:62318ba945de 62 */
wvd_vegt 20:62318ba945de 63 #undef STATEMACHINE
wvd_vegt 20:62318ba945de 64
wvd_vegt 20:62318ba945de 65 /** Enable subsystem prompts.
wvd_vegt 20:62318ba945de 66 *
wvd_vegt 20:62318ba945de 67 * When defined, prompts will reflect the SubSystem.
wvd_vegt 20:62318ba945de 68 */
wvd_vegt 20:62318ba945de 69 #define SUBSYSTEMPROMPTS
wvd_vegt 20:62318ba945de 70
wvd_vegt 20:62318ba945de 71 //------------------------------------------------------------------------------
wvd_vegt 20:62318ba945de 72
wvd_vegt 20:62318ba945de 73 /** 8 bit limits.
wvd_vegt 20:62318ba945de 74 *
wvd_vegt 20:62318ba945de 75 * @see http://www.daniweb.com/forums/thread18963.html
wvd_vegt 20:62318ba945de 76 */
wvd_vegt 20:62318ba945de 77 #define MIN_BYTE std::numeric_limits<signed char>::min()
wvd_vegt 20:62318ba945de 78
wvd_vegt 20:62318ba945de 79 /** 8 bit limits.
wvd_vegt 20:62318ba945de 80 *
wvd_vegt 20:62318ba945de 81 * @see http://www.daniweb.com/forums/thread18963.html
wvd_vegt 20:62318ba945de 82 */
wvd_vegt 20:62318ba945de 83 #define MAX_BYTE std::numeric_limits<signed char>::max()
wvd_vegt 20:62318ba945de 84
wvd_vegt 20:62318ba945de 85 /** 16 bit limits.
wvd_vegt 20:62318ba945de 86 *
wvd_vegt 20:62318ba945de 87 * @see http://www.daniweb.com/forums/thread18963.html
wvd_vegt 20:62318ba945de 88 */
wvd_vegt 20:62318ba945de 89 #define MIN_SHORT std::numeric_limits<short int>::min()
wvd_vegt 20:62318ba945de 90
wvd_vegt 20:62318ba945de 91 /** 16 bit limits.
wvd_vegt 20:62318ba945de 92 *
wvd_vegt 20:62318ba945de 93 * @see http://www.daniweb.com/forums/thread18963.html
wvd_vegt 20:62318ba945de 94 */
wvd_vegt 20:62318ba945de 95 #define MAX_SHORT std::numeric_limits<short int>::max()
wvd_vegt 20:62318ba945de 96
wvd_vegt 20:62318ba945de 97 /** 32 bit limits.
wvd_vegt 20:62318ba945de 98 *
wvd_vegt 20:62318ba945de 99 * @see http://www.daniweb.com/forums/thread18963.html
wvd_vegt 20:62318ba945de 100 */
wvd_vegt 20:62318ba945de 101 #define MIN_INT std::numeric_limits<int>::min()
wvd_vegt 20:62318ba945de 102 #define MAX_INT std::numeric_limits<int>::max()
wvd_vegt 20:62318ba945de 103
wvd_vegt 20:62318ba945de 104 /** 32 bit limits.
wvd_vegt 20:62318ba945de 105 *
wvd_vegt 20:62318ba945de 106 * @see http://www.daniweb.com/forums/thread18963.html
wvd_vegt 20:62318ba945de 107 */
wvd_vegt 20:62318ba945de 108 #define MIN_LONG std::numeric_limits<long>::min()
wvd_vegt 20:62318ba945de 109 #define MAX_LONG std::numeric_limits<long>::max()
wvd_vegt 20:62318ba945de 110
wvd_vegt 20:62318ba945de 111 //------------------------------------------------------------------------------
wvd_vegt 20:62318ba945de 112
wvd_vegt 20:62318ba945de 113 /** Description of a command.
wvd_vegt 20:62318ba945de 114 */
wvd_vegt 20:62318ba945de 115 struct cmd {
wvd_vegt 20:62318ba945de 116 public:
wvd_vegt 20:62318ba945de 117 const char *cmdstr;
wvd_vegt 20:62318ba945de 118 int subs;
wvd_vegt 20:62318ba945de 119 int cid;
wvd_vegt 20:62318ba945de 120 const char *parms;
wvd_vegt 20:62318ba945de 121 const char *cmddescr;
wvd_vegt 20:62318ba945de 122 const char *parmdescr;
wvd_vegt 20:62318ba945de 123 };
wvd_vegt 20:62318ba945de 124
wvd_vegt 20:62318ba945de 125 //------------------------------------------------------------------------------
wvd_vegt 20:62318ba945de 126
wvd_vegt 20:62318ba945de 127 /** Cr.
wvd_vegt 20:62318ba945de 128 */
wvd_vegt 20:62318ba945de 129 static const char cr = '\r';
wvd_vegt 20:62318ba945de 130
wvd_vegt 20:62318ba945de 131 /** Lf.
wvd_vegt 20:62318ba945de 132 */
wvd_vegt 20:62318ba945de 133 static const char lf = '\n';
wvd_vegt 20:62318ba945de 134
wvd_vegt 20:62318ba945de 135 /** Bell.
wvd_vegt 20:62318ba945de 136 */
wvd_vegt 20:62318ba945de 137 static const char bell = '\7';
wvd_vegt 20:62318ba945de 138
wvd_vegt 20:62318ba945de 139 /** Escape.
wvd_vegt 20:62318ba945de 140 */
wvd_vegt 20:62318ba945de 141 static const char esc = '\033';
wvd_vegt 20:62318ba945de 142
wvd_vegt 20:62318ba945de 143 /** Space.
wvd_vegt 20:62318ba945de 144 */
wvd_vegt 20:62318ba945de 145 static const char sp = ' ';
wvd_vegt 20:62318ba945de 146
wvd_vegt 20:62318ba945de 147 /** CrLf.
wvd_vegt 20:62318ba945de 148 */
wvd_vegt 20:62318ba945de 149 static const char crlf[] = "\r\n";
wvd_vegt 20:62318ba945de 150
wvd_vegt 20:62318ba945de 151 /** Backspace that 'tries' to wipe the last character.
wvd_vegt 20:62318ba945de 152 */
wvd_vegt 20:62318ba945de 153 static const char bs[] = "\b \b";
wvd_vegt 20:62318ba945de 154
wvd_vegt 20:62318ba945de 155 /** VT100 Bold Command.
wvd_vegt 20:62318ba945de 156 */
wvd_vegt 20:62318ba945de 157 static const char boldon[] = "\033[1m";
wvd_vegt 20:62318ba945de 158
wvd_vegt 20:62318ba945de 159 /** VT100 Normal Command.
wvd_vegt 20:62318ba945de 160 */
wvd_vegt 20:62318ba945de 161 static const char boldoff[] = "\033[0m";
wvd_vegt 20:62318ba945de 162
wvd_vegt 20:62318ba945de 163 /** VT100 Cls Command.
wvd_vegt 20:62318ba945de 164 */
wvd_vegt 20:62318ba945de 165 static const char cls[] = "\033[2J";
wvd_vegt 20:62318ba945de 166
wvd_vegt 20:62318ba945de 167 /** VT100 Home Command.
wvd_vegt 20:62318ba945de 168 */
wvd_vegt 20:62318ba945de 169 static const char home[] = "\033[H";
wvd_vegt 20:62318ba945de 170
wvd_vegt 20:62318ba945de 171 /** The default command prompt.
wvd_vegt 20:62318ba945de 172 */
wvd_vegt 20:62318ba945de 173 static const char PROMPT[] = "CMD>";
wvd_vegt 20:62318ba945de 174
wvd_vegt 20:62318ba945de 175 //------------------------------------------------------------------------------
wvd_vegt 20:62318ba945de 176
wvd_vegt 20:62318ba945de 177 /** Subsystem Id for a Subsystem.
wvd_vegt 20:62318ba945de 178 */
wvd_vegt 20:62318ba945de 179 #define SUBSYSTEM -1
wvd_vegt 20:62318ba945de 180
wvd_vegt 20:62318ba945de 181 /** Subsystem Id for a Global Command (always available).
wvd_vegt 20:62318ba945de 182 */
wvd_vegt 20:62318ba945de 183 #define GLOBALCMD -2
wvd_vegt 20:62318ba945de 184
wvd_vegt 20:62318ba945de 185 /** Subsystem Id for a Hidden Subsystem (ommitted from help).
wvd_vegt 20:62318ba945de 186 */
wvd_vegt 20:62318ba945de 187 #define HIDDENSUB -3
wvd_vegt 20:62318ba945de 188
wvd_vegt 20:62318ba945de 189 /** Predefined Dump Command.
wvd_vegt 20:62318ba945de 190 */
wvd_vegt 20:62318ba945de 191 #define CID_COMMANDS 9989
wvd_vegt 20:62318ba945de 192
wvd_vegt 20:62318ba945de 193 /** Predefined Boot Command.
wvd_vegt 20:62318ba945de 194 */
wvd_vegt 20:62318ba945de 195 #define CID_BOOT 9990
wvd_vegt 20:62318ba945de 196
wvd_vegt 20:62318ba945de 197 /** Predefined Macro Command.
wvd_vegt 20:62318ba945de 198 *
wvd_vegt 20:62318ba945de 199 * This command will take a string with spaces replace by _ and cr replace by | for later replay with run.
wvd_vegt 20:62318ba945de 200 */
wvd_vegt 20:62318ba945de 201 #define CID_MACRO 9991
wvd_vegt 20:62318ba945de 202
wvd_vegt 20:62318ba945de 203 /** Predefined Macro Command.
wvd_vegt 20:62318ba945de 204 *
wvd_vegt 20:62318ba945de 205 * This command replay a macro.
wvd_vegt 20:62318ba945de 206 */
wvd_vegt 20:62318ba945de 207 #define CID_RUN 9992
wvd_vegt 20:62318ba945de 208
wvd_vegt 20:62318ba945de 209 /** Predefined Macro Command.
wvd_vegt 20:62318ba945de 210 *
wvd_vegt 20:62318ba945de 211 * This command print the current macro.
wvd_vegt 20:62318ba945de 212 */
wvd_vegt 20:62318ba945de 213 #define CID_MACROS 9993
wvd_vegt 20:62318ba945de 214
wvd_vegt 20:62318ba945de 215 /** Predefined Echo Command.
wvd_vegt 20:62318ba945de 216 *
wvd_vegt 20:62318ba945de 217 * This command turn echo on or off.
wvd_vegt 20:62318ba945de 218 */
wvd_vegt 20:62318ba945de 219 #define CID_ECHO 9994
wvd_vegt 20:62318ba945de 220
wvd_vegt 20:62318ba945de 221 /** Predefined VT100 Bold Command.
wvd_vegt 20:62318ba945de 222 *
wvd_vegt 20:62318ba945de 223 * This command turn VT100 bold usage on or off.
wvd_vegt 20:62318ba945de 224 */
wvd_vegt 20:62318ba945de 225 #define CID_BOLD 9995
wvd_vegt 20:62318ba945de 226
wvd_vegt 20:62318ba945de 227 /** Predefined VT100 Cls Command.
wvd_vegt 20:62318ba945de 228 *
wvd_vegt 20:62318ba945de 229 * This command will clear the screen.
wvd_vegt 20:62318ba945de 230 */
wvd_vegt 20:62318ba945de 231 #define CID_CLS 9996
wvd_vegt 20:62318ba945de 232
wvd_vegt 20:62318ba945de 233 /** Predefined Idle Command.
wvd_vegt 20:62318ba945de 234 *
wvd_vegt 20:62318ba945de 235 * This command will return to the global command level, leaving the active subsystem.
wvd_vegt 20:62318ba945de 236 */
wvd_vegt 20:62318ba945de 237 #define CID_IDLE 9997
wvd_vegt 20:62318ba945de 238
wvd_vegt 20:62318ba945de 239 /** Predefined Help Command.
wvd_vegt 20:62318ba945de 240 *
wvd_vegt 20:62318ba945de 241 * This command will either print all active command (without parameters) or a more detailed
wvd_vegt 20:62318ba945de 242 * help for a command passed as parameter.
wvd_vegt 20:62318ba945de 243 */
wvd_vegt 20:62318ba945de 244 #define CID_HELP 9998
wvd_vegt 20:62318ba945de 245
wvd_vegt 20:62318ba945de 246 /** Predefided Semi Command.
wvd_vegt 20:62318ba945de 247 *
wvd_vegt 20:62318ba945de 248 * CID_LAST only functions as a special Commend Id to signal unknown commands.
wvd_vegt 20:62318ba945de 249 */
wvd_vegt 20:62318ba945de 250 #define CID_LAST 9999
wvd_vegt 20:62318ba945de 251
wvd_vegt 20:62318ba945de 252 //------------------------------------------------------------------------------
wvd_vegt 20:62318ba945de 253
wvd_vegt 20:62318ba945de 254 /** The Boot Command.
wvd_vegt 20:62318ba945de 255 *
wvd_vegt 20:62318ba945de 256 * @note: this command can be used to list all commands in Windows ini file format for host processing.
wvd_vegt 20:62318ba945de 257 *
wvd_vegt 20:62318ba945de 258 * Optional.
wvd_vegt 20:62318ba945de 259 */
wvd_vegt 20:62318ba945de 260 static const cmd COMMANDS = {"Commands",GLOBALCMD,CID_COMMANDS,"","Dump Commands"};
wvd_vegt 20:62318ba945de 261
wvd_vegt 20:62318ba945de 262 /** The Boot Command.
wvd_vegt 20:62318ba945de 263 *
wvd_vegt 20:62318ba945de 264 * Optional.
wvd_vegt 20:62318ba945de 265 */
wvd_vegt 20:62318ba945de 266 static const cmd BOOT = {"Boot",GLOBALCMD,CID_BOOT,"","Boot mBed"};
wvd_vegt 20:62318ba945de 267
wvd_vegt 20:62318ba945de 268 /** The Macro Command.
wvd_vegt 20:62318ba945de 269 *
wvd_vegt 20:62318ba945de 270 * Optional.
wvd_vegt 20:62318ba945de 271 */
wvd_vegt 20:62318ba945de 272 static const cmd MACRO = {"Macro",GLOBALCMD,CID_MACRO,"%s","Define macro (sp->_, cr->|)","command(s)"};
wvd_vegt 20:62318ba945de 273
wvd_vegt 20:62318ba945de 274 /** The Run Command.
wvd_vegt 20:62318ba945de 275 *
wvd_vegt 20:62318ba945de 276 * Optional.
wvd_vegt 20:62318ba945de 277 */
wvd_vegt 20:62318ba945de 278 static const cmd RUN = {"Run",GLOBALCMD,CID_RUN,"","Run a macro"};
wvd_vegt 20:62318ba945de 279
wvd_vegt 20:62318ba945de 280 /** The Macros Command.
wvd_vegt 20:62318ba945de 281 *
wvd_vegt 20:62318ba945de 282 * Optional.
wvd_vegt 20:62318ba945de 283 */
wvd_vegt 20:62318ba945de 284 static const cmd MACROS = {"Macros",GLOBALCMD,CID_MACROS,"","List macro(s)"};
wvd_vegt 20:62318ba945de 285
wvd_vegt 20:62318ba945de 286 /** The Echo Command.
wvd_vegt 20:62318ba945de 287 *
wvd_vegt 20:62318ba945de 288 * Optional.
wvd_vegt 20:62318ba945de 289 */
wvd_vegt 20:62318ba945de 290 static const cmd ECHO = {"Echo",GLOBALCMD,CID_ECHO,"%bu","Echo On|Off (1|0)","state"};
wvd_vegt 20:62318ba945de 291
wvd_vegt 20:62318ba945de 292 /** The Bold Command.
wvd_vegt 20:62318ba945de 293 *
wvd_vegt 20:62318ba945de 294 * Optional.
wvd_vegt 20:62318ba945de 295 */
wvd_vegt 20:62318ba945de 296 static const cmd BOLD = {"Bold",GLOBALCMD,CID_BOLD,"%bu","Bold On|Off (1|0)","state"};
wvd_vegt 20:62318ba945de 297
wvd_vegt 20:62318ba945de 298 /** The Cls Command.
wvd_vegt 20:62318ba945de 299 *
wvd_vegt 20:62318ba945de 300 * Optional.
wvd_vegt 20:62318ba945de 301 */
wvd_vegt 20:62318ba945de 302 static const cmd CLS = {"Cls",GLOBALCMD,CID_CLS,"","Clears the terminal screen"};
wvd_vegt 20:62318ba945de 303
wvd_vegt 20:62318ba945de 304 /** The Idle Command.
wvd_vegt 20:62318ba945de 305 *
wvd_vegt 20:62318ba945de 306 * Mandatory if you use subsystems.
wvd_vegt 20:62318ba945de 307 */
wvd_vegt 20:62318ba945de 308 static const cmd IDLE = {"Idle",GLOBALCMD,CID_IDLE,"","Deselect Subsystems"};
wvd_vegt 20:62318ba945de 309
wvd_vegt 20:62318ba945de 310 /** The Help Command.
wvd_vegt 20:62318ba945de 311 *
wvd_vegt 20:62318ba945de 312 * Mandatory.
wvd_vegt 20:62318ba945de 313 */
wvd_vegt 20:62318ba945de 314 static const cmd HELP = {"Help",GLOBALCMD,CID_HELP,"%s","Help"};
wvd_vegt 20:62318ba945de 315
wvd_vegt 20:62318ba945de 316 //------------------------------------------------------------------------------
wvd_vegt 20:62318ba945de 317
wvd_vegt 20:62318ba945de 318 /** We'll only define the 4 cursor codes at the moment.
wvd_vegt 20:62318ba945de 319 */
wvd_vegt 20:62318ba945de 320 #define ESC_TBL_LEN 4
wvd_vegt 20:62318ba945de 321
wvd_vegt 20:62318ba945de 322 /** Escape code definition struct.
wvd_vegt 20:62318ba945de 323 */
wvd_vegt 20:62318ba945de 324 struct esc {
wvd_vegt 20:62318ba945de 325 char *escstr;
wvd_vegt 20:62318ba945de 326 int id;
wvd_vegt 20:62318ba945de 327 };
wvd_vegt 20:62318ba945de 328
wvd_vegt 20:62318ba945de 329 /** The Escape Code Id's.
wvd_vegt 20:62318ba945de 330 */
wvd_vegt 20:62318ba945de 331 enum {
wvd_vegt 20:62318ba945de 332 EID_CURSOR_UP,
wvd_vegt 20:62318ba945de 333 EID_CURSOR_DOWN,
wvd_vegt 20:62318ba945de 334 EID_CURSOR_RIGHT,
wvd_vegt 20:62318ba945de 335 EID_CURSOR_LEFT,
wvd_vegt 20:62318ba945de 336 EID_LAST
wvd_vegt 20:62318ba945de 337 };
wvd_vegt 20:62318ba945de 338
wvd_vegt 20:62318ba945de 339 /** The Escape Codes Table.
wvd_vegt 20:62318ba945de 340 */
wvd_vegt 20:62318ba945de 341 static const struct esc esc_tbl [ESC_TBL_LEN] = {
wvd_vegt 20:62318ba945de 342 { "\033[A", EID_CURSOR_UP },
wvd_vegt 20:62318ba945de 343 { "\033[B", EID_CURSOR_DOWN },
wvd_vegt 20:62318ba945de 344 { "\033[C", EID_CURSOR_RIGHT },
wvd_vegt 20:62318ba945de 345 { "\033[D", EID_CURSOR_LEFT },
wvd_vegt 20:62318ba945de 346 };
wvd_vegt 20:62318ba945de 347
wvd_vegt 20:62318ba945de 348 //------------------------------------------------------------------------------
wvd_vegt 20:62318ba945de 349
wvd_vegt 20:62318ba945de 350 /** The Command Interpreter Version.
wvd_vegt 20:62318ba945de 351 */
wvd_vegt 20:62318ba945de 352 #define CMDB_VERSION 0.81
wvd_vegt 20:62318ba945de 353
wvd_vegt 20:62318ba945de 354 //------------------------------------------------------------------------------
wvd_vegt 20:62318ba945de 355
wvd_vegt 20:62318ba945de 356 /** Command Interpreter class.
wvd_vegt 20:62318ba945de 357 *
wvd_vegt 20:62318ba945de 358 * Steps to take:
wvd_vegt 20:62318ba945de 359 *
wvd_vegt 20:62318ba945de 360 * 1) Create a std::vector<cmd> and fill it with at least
wvd_vegt 20:62318ba945de 361 * the mandatory commands IDLE and HELP.
wvd_vegt 20:62318ba945de 362 *
wvd_vegt 20:62318ba945de 363 * 2) Create an Cmdb class instance and pass it the vector,
wvd_vegt 20:62318ba945de 364 * a Serial port object like Serial serial(USBTX, USBRX);
wvd_vegt 20:62318ba945de 365 * and finally a command dispatcher function.
wvd_vegt 20:62318ba945de 366 *
wvd_vegt 20:62318ba945de 367 * 3) Feed the interpreter with characters received from your serial port.
wvd_vegt 20:62318ba945de 368 * Note: Cmdb self does not retrieve input it must be handed to it.
wvd_vegt 20:62318ba945de 369 * It implements basic members for checking/reading the serial port.
wvd_vegt 20:62318ba945de 370 *
wvd_vegt 20:62318ba945de 371 * 4) Handle commands added by the application by the Cid and parameters passed.
wvd_vegt 20:62318ba945de 372 *
wvd_vegt 20:62318ba945de 373 * Note: Predefined commands and all subsystems transitions are handled by the internal dispatcher.
wvd_vegt 20:62318ba945de 374 * So the passed dispatcher only has to handle user/application defined commands'.
wvd_vegt 20:62318ba945de 375 *
wvd_vegt 20:62318ba945de 376 * @see main.cpp for a demo.
wvd_vegt 20:62318ba945de 377 */
wvd_vegt 20:62318ba945de 378 class Cmdb {
wvd_vegt 20:62318ba945de 379 public:
wvd_vegt 20:62318ba945de 380 /** Create a Command Interpreter.
wvd_vegt 20:62318ba945de 381 *
wvd_vegt 20:62318ba945de 382 * @see http://www.newty.de/fpt/fpt.html#chapter2 for function pointers.
wvd_vegt 20:62318ba945de 383 * @see http://stackoverflow.com/questions/9410/how-do-you-pass-a-function-as-a-parameter-in-c
wvd_vegt 20:62318ba945de 384 * @see http://www.daniweb.com/forums/thread293338.html
wvd_vegt 20:62318ba945de 385 *
wvd_vegt 20:62318ba945de 386 * @param serial a Serial port used for communication.
wvd_vegt 20:62318ba945de 387 * @param cmds a vector with the command table.
wvd_vegt 20:62318ba945de 388 */
wvd_vegt 20:62318ba945de 389 Cmdb(const Serial& _serial, std::vector<cmd>& _cmds, void (*_callback)(Cmdb&,int) );
wvd_vegt 20:62318ba945de 390
wvd_vegt 20:62318ba945de 391 /** The version of the Command Interpreter.
wvd_vegt 20:62318ba945de 392 *
wvd_vegt 20:62318ba945de 393 * returns the version.
wvd_vegt 20:62318ba945de 394 */
wvd_vegt 20:62318ba945de 395 static float version() {
wvd_vegt 20:62318ba945de 396 return CMDB_VERSION;
wvd_vegt 20:62318ba945de 397 }
wvd_vegt 20:62318ba945de 398
wvd_vegt 20:62318ba945de 399 /** NULL is used as No Comment Value.
wvd_vegt 20:62318ba945de 400 */
wvd_vegt 20:62318ba945de 401 static const char* NoComment;
wvd_vegt 20:62318ba945de 402
wvd_vegt 20:62318ba945de 403 /** Column 72 is used as Default Comment Starting Position.
wvd_vegt 20:62318ba945de 404 */
wvd_vegt 20:62318ba945de 405 static int DefComPos;
wvd_vegt 20:62318ba945de 406
wvd_vegt 20:62318ba945de 407 /** Checks if the macro buffer has any characters left.
wvd_vegt 20:62318ba945de 408 *
wvd_vegt 20:62318ba945de 409 * @returns true if any characters left.
wvd_vegt 20:62318ba945de 410 */
wvd_vegt 20:62318ba945de 411 bool macro_hasnext();
wvd_vegt 20:62318ba945de 412
wvd_vegt 20:62318ba945de 413 /** Gets the next character from the macro buffer and
wvd_vegt 20:62318ba945de 414 * advances the macro buffer pointer.
wvd_vegt 20:62318ba945de 415 *
wvd_vegt 20:62318ba945de 416 * @note Do not call if no more characters are left!
wvd_vegt 20:62318ba945de 417 *
wvd_vegt 20:62318ba945de 418 * @returns the next character.
wvd_vegt 20:62318ba945de 419 */
wvd_vegt 20:62318ba945de 420 char macro_next();
wvd_vegt 20:62318ba945de 421
wvd_vegt 20:62318ba945de 422 /** Gets the next character from the macro buffer
wvd_vegt 20:62318ba945de 423 * but does not advance the macro buffer pointer.
wvd_vegt 20:62318ba945de 424 *
wvd_vegt 20:62318ba945de 425 * @note Do not call if no more characters are left!
wvd_vegt 20:62318ba945de 426 *
wvd_vegt 20:62318ba945de 427 * @returns the next character.
wvd_vegt 20:62318ba945de 428 */
wvd_vegt 20:62318ba945de 429 char macro_peek();
wvd_vegt 20:62318ba945de 430
wvd_vegt 20:62318ba945de 431 /** Resets the macro buffer and macro buffer pointer.
wvd_vegt 20:62318ba945de 432 *
wvd_vegt 20:62318ba945de 433 */
wvd_vegt 20:62318ba945de 434 void macro_reset();
wvd_vegt 20:62318ba945de 435
wvd_vegt 20:62318ba945de 436 /** Checks if the serial port has any characters
wvd_vegt 20:62318ba945de 437 * left to read by calling serial.readable().
wvd_vegt 20:62318ba945de 438 *
wvd_vegt 20:62318ba945de 439 * @returns true if any characters available.
wvd_vegt 20:62318ba945de 440 */
wvd_vegt 20:62318ba945de 441 bool hasnext();
wvd_vegt 20:62318ba945de 442
wvd_vegt 20:62318ba945de 443 /** Gets the next character from the serial port by
wvd_vegt 20:62318ba945de 444 * calling serial.getc().
wvd_vegt 20:62318ba945de 445 *
wvd_vegt 20:62318ba945de 446 * Do not call if no characters are left!
wvd_vegt 20:62318ba945de 447 *
wvd_vegt 20:62318ba945de 448 * @returns the next character.
wvd_vegt 20:62318ba945de 449 */
wvd_vegt 20:62318ba945de 450 char next();
wvd_vegt 20:62318ba945de 451
wvd_vegt 20:62318ba945de 452 /** Add a character to the command being processed.
wvd_vegt 20:62318ba945de 453 * If a cr is added, the command is parsed and executed if possible
wvd_vegt 20:62318ba945de 454 * If supported special keys are encountered (like backspace, delete and cursor up) they are processed.
wvd_vegt 20:62318ba945de 455 *
wvd_vegt 20:62318ba945de 456 * @param c the character to add.
wvd_vegt 20:62318ba945de 457 *
wvd_vegt 20:62318ba945de 458 * @returns true if a command was recognized and executed.
wvd_vegt 20:62318ba945de 459 */
wvd_vegt 20:62318ba945de 460 bool scan(const char c);
wvd_vegt 20:62318ba945de 461
wvd_vegt 20:62318ba945de 462 /** printf substitute using the serial parameter passed to the constructor.
wvd_vegt 20:62318ba945de 463 *
wvd_vegt 20:62318ba945de 464 * @see http://www.cplusplus.com/reference/clibrary/cstdio/printf/
wvd_vegt 20:62318ba945de 465 *
wvd_vegt 20:62318ba945de 466 * @parm format the printf format string.
wvd_vegt 20:62318ba945de 467 * @parm ... optional paramaters to be merged into the format string.
wvd_vegt 20:62318ba945de 468 *
wvd_vegt 20:62318ba945de 469 * @returns the printf return value.
wvd_vegt 20:62318ba945de 470 */
wvd_vegt 20:62318ba945de 471 int printf(const char *format, ...);
wvd_vegt 20:62318ba945de 472
wvd_vegt 20:62318ba945de 473 /** print is simply printf without parameters using the serial parameter passed to the constructor.
wvd_vegt 20:62318ba945de 474 *
wvd_vegt 20:62318ba945de 475 * @parm msg the string to print.
wvd_vegt 20:62318ba945de 476 *
wvd_vegt 20:62318ba945de 477 * @returns the printf return value.
wvd_vegt 20:62318ba945de 478 */
wvd_vegt 20:62318ba945de 479 int print(const char *msg);
wvd_vegt 20:62318ba945de 480
wvd_vegt 20:62318ba945de 481 /** printch is simply putc subsitute using the serial parameter passed to the constructor.
wvd_vegt 20:62318ba945de 482 *
wvd_vegt 20:62318ba945de 483 * @parm msg the string to print.
wvd_vegt 20:62318ba945de 484 *
wvd_vegt 20:62318ba945de 485 * @returns the printf return value.
wvd_vegt 20:62318ba945de 486 */
wvd_vegt 20:62318ba945de 487 char printch(const char ch);
wvd_vegt 20:62318ba945de 488
wvd_vegt 20:62318ba945de 489 /** printsection prints an inifile Section Header
wvd_vegt 20:62318ba945de 490 * like:
wvd_vegt 20:62318ba945de 491 *
wvd_vegt 20:62318ba945de 492 * [Section]\r\n
wvd_vegt 20:62318ba945de 493 *
wvd_vegt 20:62318ba945de 494 * Usage: cmdb.printsection("GP");
wvd_vegt 20:62318ba945de 495 *
wvd_vegt 20:62318ba945de 496 * @parm section the section to print.
wvd_vegt 20:62318ba945de 497 *
wvd_vegt 20:62318ba945de 498 * @returns the printf return value.
wvd_vegt 20:62318ba945de 499 */
wvd_vegt 20:62318ba945de 500 int printsection(const char *section);
wvd_vegt 20:62318ba945de 501
wvd_vegt 22:5192a468d7fa 502 /** printmsg prints an inifile Msg Key=Value pair.
wvd_vegt 22:5192a468d7fa 503 * like:
wvd_vegt 22:5192a468d7fa 504 *
wvd_vegt 22:5192a468d7fa 505 * Msg={msg}\r\n
wvd_vegt 22:5192a468d7fa 506 *
wvd_vegt 22:5192a468d7fa 507 * Usage: cmdb.printmsg("Validation successfull");
wvd_vegt 22:5192a468d7fa 508 *
wvd_vegt 22:5192a468d7fa 509 * @parm msg the msg to print.
wvd_vegt 22:5192a468d7fa 510 *
wvd_vegt 22:5192a468d7fa 511 * @returns the printf return value.
wvd_vegt 22:5192a468d7fa 512 */
wvd_vegt 22:5192a468d7fa 513 int Cmdb::printmsg(const char *msg);
wvd_vegt 22:5192a468d7fa 514
wvd_vegt 22:5192a468d7fa 515 /** printerror prints an inifile Error Section Header and Error Msg Key=Value pair.
wvd_vegt 21:d22068e2bbd1 516 * like:
wvd_vegt 21:d22068e2bbd1 517 *
wvd_vegt 21:d22068e2bbd1 518 * [Error]\r\nmsg={errormsg}\r\n
wvd_vegt 21:d22068e2bbd1 519 *
wvd_vegt 21:d22068e2bbd1 520 * Usage: cmdb.printerror("Data Size Incorrect");
wvd_vegt 21:d22068e2bbd1 521 *
wvd_vegt 21:d22068e2bbd1 522 * @parm errormsg the error msg to print.
wvd_vegt 21:d22068e2bbd1 523 *
wvd_vegt 21:d22068e2bbd1 524 * @returns the printf return value.
wvd_vegt 21:d22068e2bbd1 525 */
wvd_vegt 21:d22068e2bbd1 526 int printerror(const char *errormsg);
wvd_vegt 22:5192a468d7fa 527
wvd_vegt 20:62318ba945de 528 /** printvalue prints an inifile Key/Value Pair
wvd_vegt 20:62318ba945de 529 * like:
wvd_vegt 20:62318ba945de 530 *
wvd_vegt 20:62318ba945de 531 * Key=Value ;comment\r\n
wvd_vegt 20:62318ba945de 532 *
wvd_vegt 20:62318ba945de 533 * Note: the Comment is (if present) located at position 72.
wvd_vegt 20:62318ba945de 534 *
wvd_vegt 20:62318ba945de 535 * Usage: cmdb.printvaluef("Value", Cmdb::DefComPos, "Hex", "0x%8.8X", LPC_RTC->GPREG0);
wvd_vegt 20:62318ba945de 536 *
wvd_vegt 20:62318ba945de 537 * @parm key the key to print.
wvd_vegt 20:62318ba945de 538 * @parm comment the comment to print.
wvd_vegt 20:62318ba945de 539 * @parm width the location of the comment to print.
wvd_vegt 20:62318ba945de 540 * @parm format the value to print.
wvd_vegt 20:62318ba945de 541 * @parm parameter to print.
wvd_vegt 20:62318ba945de 542 *
wvd_vegt 20:62318ba945de 543 * @returns the printf return value.
wvd_vegt 20:62318ba945de 544 */
wvd_vegt 20:62318ba945de 545 int printvaluef(const char *key, const int width, const char *comment, const char *format, ...);
wvd_vegt 20:62318ba945de 546
wvd_vegt 20:62318ba945de 547 /** printvalue prints an inifile Key/Value Pair
wvd_vegt 20:62318ba945de 548 * like:
wvd_vegt 20:62318ba945de 549 *
wvd_vegt 20:62318ba945de 550 * Key=Value\r\n
wvd_vegt 20:62318ba945de 551 *
wvd_vegt 20:62318ba945de 552 * Usage: cmdb.printvaluef("Value", "0x%8.8X", LPC_RTC->GPREG0);
wvd_vegt 20:62318ba945de 553 *
wvd_vegt 20:62318ba945de 554 * @parm key the key to print.
wvd_vegt 20:62318ba945de 555 * @parm format the value to print.
wvd_vegt 20:62318ba945de 556 * @parm parameter to print.
wvd_vegt 20:62318ba945de 557 *
wvd_vegt 20:62318ba945de 558 * @returns the printf return value.
wvd_vegt 20:62318ba945de 559 */
wvd_vegt 20:62318ba945de 560 int printvaluef(const char *key, const char *format, ...);
wvd_vegt 20:62318ba945de 561
wvd_vegt 20:62318ba945de 562 /** printvalue prints an inifile Key/Value Pair
wvd_vegt 20:62318ba945de 563 * like:
wvd_vegt 20:62318ba945de 564 *
wvd_vegt 20:62318ba945de 565 * Key=Value ;comment\r\n
wvd_vegt 20:62318ba945de 566 *
wvd_vegt 20:62318ba945de 567 * Note the Comment is (if present) located at position 72.
wvd_vegt 20:62318ba945de 568 *
wvd_vegt 20:62318ba945de 569 * @parm key the key to print.
wvd_vegt 20:62318ba945de 570 * @parm value the value to print.
wvd_vegt 20:62318ba945de 571 * @parm comment the comment to print.
wvd_vegt 20:62318ba945de 572 * @parm width the location of the comment to print.
wvd_vegt 20:62318ba945de 573 *
wvd_vegt 20:62318ba945de 574 * @returns the printf return value.
wvd_vegt 20:62318ba945de 575 */
wvd_vegt 20:62318ba945de 576 int printvalue(const char *key, const char *value, const char *comment = NoComment, const int width = DefComPos);
wvd_vegt 20:62318ba945de 577
wvd_vegt 20:62318ba945de 578 //------------------------------------------------------------------------------
wvd_vegt 20:62318ba945de 579
wvd_vegt 20:62318ba945de 580 /** Initializes the parser (called by the constructor).
wvd_vegt 20:62318ba945de 581 *
wvd_vegt 20:62318ba945de 582 * @parm full if true the macro buffer is also cleared else only the command interpreter is reset.
wvd_vegt 20:62318ba945de 583 */
wvd_vegt 20:62318ba945de 584 void init(const char full);
wvd_vegt 20:62318ba945de 585
wvd_vegt 20:62318ba945de 586 //------------------------------------------------------------------------------
wvd_vegt 20:62318ba945de 587 //----These helper functions retieve parameters in the correct format.
wvd_vegt 20:62318ba945de 588 //------------------------------------------------------------------------------
wvd_vegt 20:62318ba945de 589
wvd_vegt 20:62318ba945de 590 /** Typecasts parameter ndx to a bool.
wvd_vegt 20:62318ba945de 591 *
wvd_vegt 20:62318ba945de 592 * mask: %bu
wvd_vegt 20:62318ba945de 593 *
wvd_vegt 20:62318ba945de 594 * @parm the parameter index
wvd_vegt 20:62318ba945de 595 *
wvd_vegt 20:62318ba945de 596 * @return a bool
wvd_vegt 20:62318ba945de 597 */
wvd_vegt 20:62318ba945de 598 bool BOOLPARM(int ndx) {
wvd_vegt 20:62318ba945de 599 return parms[ndx].val.uc!=0;
wvd_vegt 20:62318ba945de 600 }
wvd_vegt 20:62318ba945de 601
wvd_vegt 20:62318ba945de 602 /** Typecasts parameter ndx to a byte/unsigned char.
wvd_vegt 20:62318ba945de 603 *
wvd_vegt 20:62318ba945de 604 * mask: %bu
wvd_vegt 20:62318ba945de 605 *
wvd_vegt 20:62318ba945de 606 * @parm the parameter index
wvd_vegt 20:62318ba945de 607 *
wvd_vegt 20:62318ba945de 608 * @return a byte/unsigned char
wvd_vegt 20:62318ba945de 609 */
wvd_vegt 20:62318ba945de 610 unsigned char BYTEPARM(int ndx) {
wvd_vegt 20:62318ba945de 611 return parms[ndx].val.uc;
wvd_vegt 20:62318ba945de 612 }
wvd_vegt 20:62318ba945de 613
wvd_vegt 20:62318ba945de 614 /** Typecasts parameter ndx to a char.
wvd_vegt 20:62318ba945de 615 *
wvd_vegt 20:62318ba945de 616 * mask: %c
wvd_vegt 20:62318ba945de 617 *
wvd_vegt 20:62318ba945de 618 * @parm the parameter index
wvd_vegt 20:62318ba945de 619 *
wvd_vegt 20:62318ba945de 620 * @return a char
wvd_vegt 20:62318ba945de 621 */
wvd_vegt 20:62318ba945de 622 char CHARPARM(int ndx) {
wvd_vegt 20:62318ba945de 623 return parms[ndx].val.c;
wvd_vegt 20:62318ba945de 624 }
wvd_vegt 20:62318ba945de 625
wvd_vegt 20:62318ba945de 626 /** Typecasts parameter ndx to word/unsigned int.
wvd_vegt 20:62318ba945de 627 *
wvd_vegt 20:62318ba945de 628 * mask: %hu
wvd_vegt 20:62318ba945de 629 *
wvd_vegt 20:62318ba945de 630 * @parm the parameter index
wvd_vegt 20:62318ba945de 631 *
wvd_vegt 20:62318ba945de 632 * @return a word/unsigned int
wvd_vegt 20:62318ba945de 633 */
wvd_vegt 20:62318ba945de 634 unsigned int WORDPARM(int ndx) {
wvd_vegt 20:62318ba945de 635 return parms[ndx].val.ui;
wvd_vegt 20:62318ba945de 636 }
wvd_vegt 20:62318ba945de 637
wvd_vegt 20:62318ba945de 638 /** Typecasts parameter ndx to a unsigned int.
wvd_vegt 20:62318ba945de 639 *
wvd_vegt 20:62318ba945de 640 * mask: %u
wvd_vegt 20:62318ba945de 641 *
wvd_vegt 20:62318ba945de 642 * @parm the parameter index
wvd_vegt 20:62318ba945de 643 *
wvd_vegt 20:62318ba945de 644 * @return a unsigned int
wvd_vegt 20:62318ba945de 645 */
wvd_vegt 20:62318ba945de 646 unsigned int UINTPARM(int ndx) {
wvd_vegt 20:62318ba945de 647 return parms[ndx].val.ui;
wvd_vegt 20:62318ba945de 648 }
wvd_vegt 20:62318ba945de 649
wvd_vegt 20:62318ba945de 650 /** Typecasts parameter ndx to a int.
wvd_vegt 20:62318ba945de 651 *
wvd_vegt 20:62318ba945de 652 * mask: %i
wvd_vegt 20:62318ba945de 653 *
wvd_vegt 20:62318ba945de 654 * @parm the parameter index
wvd_vegt 20:62318ba945de 655 *
wvd_vegt 20:62318ba945de 656 * @return a int
wvd_vegt 20:62318ba945de 657 */
wvd_vegt 20:62318ba945de 658 int INTPARM(int ndx) {
wvd_vegt 20:62318ba945de 659 return parms[ndx].val.i;
wvd_vegt 20:62318ba945de 660 }
wvd_vegt 20:62318ba945de 661
wvd_vegt 20:62318ba945de 662 /** Typecasts parameter ndx to a bool.
wvd_vegt 20:62318ba945de 663 *
wvd_vegt 20:62318ba945de 664 * mask: %lu
wvd_vegt 20:62318ba945de 665 *
wvd_vegt 20:62318ba945de 666 * @parm the parameter index
wvd_vegt 20:62318ba945de 667 *
wvd_vegt 20:62318ba945de 668 * @return a bool
wvd_vegt 20:62318ba945de 669 */
wvd_vegt 20:62318ba945de 670 unsigned long DWORDPARM(int ndx) {
wvd_vegt 20:62318ba945de 671 return parms[ndx].val.ul;
wvd_vegt 20:62318ba945de 672 }
wvd_vegt 20:62318ba945de 673
wvd_vegt 20:62318ba945de 674 /** Typecasts parameter ndx to a long.
wvd_vegt 20:62318ba945de 675 *
wvd_vegt 20:62318ba945de 676 * mask: %li
wvd_vegt 20:62318ba945de 677 *
wvd_vegt 20:62318ba945de 678 * @parm the parameter index
wvd_vegt 20:62318ba945de 679 *
wvd_vegt 20:62318ba945de 680 * @return a long
wvd_vegt 20:62318ba945de 681 */
wvd_vegt 20:62318ba945de 682 long LONGPARM(int ndx) {
wvd_vegt 20:62318ba945de 683 return parms[ndx].val.l;
wvd_vegt 20:62318ba945de 684 }
wvd_vegt 20:62318ba945de 685
wvd_vegt 20:62318ba945de 686 /** Typecasts parameter ndx to a float.
wvd_vegt 20:62318ba945de 687 *
wvd_vegt 20:62318ba945de 688 * mask: %f
wvd_vegt 20:62318ba945de 689 *
wvd_vegt 20:62318ba945de 690 * @parm the parameter index
wvd_vegt 20:62318ba945de 691 *
wvd_vegt 20:62318ba945de 692 * @return a float
wvd_vegt 20:62318ba945de 693 */
wvd_vegt 20:62318ba945de 694 float FLOATPARM(int ndx) {
wvd_vegt 20:62318ba945de 695 return parms[ndx].val.f;
wvd_vegt 20:62318ba945de 696 }
wvd_vegt 20:62318ba945de 697
wvd_vegt 20:62318ba945de 698 /** Typecasts parameter ndx to a string.
wvd_vegt 20:62318ba945de 699 *
wvd_vegt 20:62318ba945de 700 * @note spaces are not allowed as it makes parsing so much harder.
wvd_vegt 20:62318ba945de 701 *
wvd_vegt 20:62318ba945de 702 * mask: %s
wvd_vegt 20:62318ba945de 703 *
wvd_vegt 20:62318ba945de 704 * @parm the parameter index
wvd_vegt 20:62318ba945de 705 *
wvd_vegt 20:62318ba945de 706 * @return a string
wvd_vegt 20:62318ba945de 707 */
wvd_vegt 20:62318ba945de 708 char* STRINGPARM(int ndx) {
wvd_vegt 20:62318ba945de 709 return parms[ndx].val.s;
wvd_vegt 20:62318ba945de 710 }
wvd_vegt 20:62318ba945de 711
wvd_vegt 20:62318ba945de 712 bool present(char *cmdstr) {
wvd_vegt 20:62318ba945de 713 return cmdid_search(cmdstr)!=CID_LAST;
wvd_vegt 20:62318ba945de 714 }
wvd_vegt 20:62318ba945de 715
wvd_vegt 20:62318ba945de 716 void replace(std::vector<cmd>& newcmds) {
wvd_vegt 20:62318ba945de 717 cmds.assign(newcmds.begin(), newcmds.end());
wvd_vegt 20:62318ba945de 718 }
wvd_vegt 20:62318ba945de 719
wvd_vegt 20:62318ba945de 720
wvd_vegt 20:62318ba945de 721 int indexof(int cid) {
wvd_vegt 20:62318ba945de 722 return cmdid_index(cid);
wvd_vegt 20:62318ba945de 723 }
wvd_vegt 20:62318ba945de 724
wvd_vegt 20:62318ba945de 725 //FAILS...
wvd_vegt 20:62318ba945de 726 /*
wvd_vegt 20:62318ba945de 727 void insert(int cid, cmd newcmd) {
wvd_vegt 20:62318ba945de 728 //Add Command (update our original and then assign/replace cmdb's copy)...
wvd_vegt 20:62318ba945de 729 vector<cmd>::iterator iter;
wvd_vegt 20:62318ba945de 730
wvd_vegt 20:62318ba945de 731 std::vector<cmd> newcmds = std::vector<cmd>(cmds);
wvd_vegt 20:62318ba945de 732
wvd_vegt 20:62318ba945de 733 iter = newcmds.begin();
wvd_vegt 20:62318ba945de 734
wvd_vegt 20:62318ba945de 735 newcmds.insert(iter+indexof(cid)+1,newcmd);
wvd_vegt 20:62318ba945de 736
wvd_vegt 20:62318ba945de 737 replace(newcmds);
wvd_vegt 20:62318ba945de 738
wvd_vegt 20:62318ba945de 739 printf("Index: %d\r\n", ndx);
wvd_vegt 20:62318ba945de 740
wvd_vegt 20:62318ba945de 741 print("check #1\r\n");
wvd_vegt 20:62318ba945de 742 print("check #2\r\n");
wvd_vegt 20:62318ba945de 743
wvd_vegt 20:62318ba945de 744 vector<cmd>::iterator it;
wvd_vegt 20:62318ba945de 745 it=newcmds.begin();
wvd_vegt 20:62318ba945de 746
wvd_vegt 20:62318ba945de 747 print("check #3\r\n");
wvd_vegt 20:62318ba945de 748 ndx++;
wvd_vegt 20:62318ba945de 749 newcmds.insert(it, newcmd);
wvd_vegt 20:62318ba945de 750 print("check #4\r\n");
wvd_vegt 20:62318ba945de 751
wvd_vegt 20:62318ba945de 752 //cmds.push_back(c1);
wvd_vegt 20:62318ba945de 753 cmds.assign(newcmds.begin(), newcmds.end());
wvd_vegt 20:62318ba945de 754 }
wvd_vegt 20:62318ba945de 755 */
wvd_vegt 20:62318ba945de 756
wvd_vegt 20:62318ba945de 757 private:
wvd_vegt 20:62318ba945de 758
wvd_vegt 20:62318ba945de 759 /** Internal Serial Port Storage.
wvd_vegt 20:62318ba945de 760 */
wvd_vegt 20:62318ba945de 761 Serial serial;
wvd_vegt 20:62318ba945de 762
wvd_vegt 20:62318ba945de 763 /** Internal Command Table Vector Storage.
wvd_vegt 20:62318ba945de 764 *
wvd_vegt 20:62318ba945de 765 * @see http://www.cplusplus.com/reference/stl/vector/
wvd_vegt 20:62318ba945de 766 */
wvd_vegt 20:62318ba945de 767 std::vector<cmd> cmds;
wvd_vegt 20:62318ba945de 768
wvd_vegt 20:62318ba945de 769 /** C callback function
wvd_vegt 20:62318ba945de 770 *
wvd_vegt 20:62318ba945de 771 * @see See http://www.newty.de/fpt/fpt.html#chapter2 for function pointers.
wvd_vegt 20:62318ba945de 772 *
wvd_vegt 20:62318ba945de 773 * C++ member equivalent would be void (Cmdb::*callback)(Cmdb&,int);
wvd_vegt 20:62318ba945de 774 */
wvd_vegt 20:62318ba945de 775 void (*user_callback)(Cmdb&,int);
wvd_vegt 20:62318ba945de 776
wvd_vegt 20:62318ba945de 777 /** Searches the escape code list for a match.
wvd_vegt 20:62318ba945de 778 *
wvd_vegt 20:62318ba945de 779 * @param char* escstr the escape code to lookup.
wvd_vegt 20:62318ba945de 780 *
wvd_vegt 20:62318ba945de 781 * @returns the index of the escape code or -1.
wvd_vegt 20:62318ba945de 782 */
wvd_vegt 20:62318ba945de 783 int escid_search(char *escstr);
wvd_vegt 20:62318ba945de 784
wvd_vegt 20:62318ba945de 785 /** Checks if the command table for a match.
wvd_vegt 20:62318ba945de 786 *
wvd_vegt 20:62318ba945de 787 * @param char* cmdstr the command to lookup.
wvd_vegt 20:62318ba945de 788 *
wvd_vegt 20:62318ba945de 789 * @returns the id of the command or -1.
wvd_vegt 20:62318ba945de 790 */
wvd_vegt 20:62318ba945de 791 int cmdid_search(char *cmdstr);
wvd_vegt 20:62318ba945de 792
wvd_vegt 20:62318ba945de 793 /** Converts an command id to an index of the command table.
wvd_vegt 20:62318ba945de 794 *
wvd_vegt 20:62318ba945de 795 * @param cmdid the command id to lookup.
wvd_vegt 20:62318ba945de 796 *
wvd_vegt 20:62318ba945de 797 * @returns the index of the command or -1.
wvd_vegt 20:62318ba945de 798 */
wvd_vegt 20:62318ba945de 799 int cmdid_index(int cmdid);
wvd_vegt 20:62318ba945de 800
wvd_vegt 20:62318ba945de 801 /** Writes a prompt to the serial port.
wvd_vegt 20:62318ba945de 802 *
wvd_vegt 20:62318ba945de 803 */
wvd_vegt 20:62318ba945de 804 void prompt(void);
wvd_vegt 20:62318ba945de 805
wvd_vegt 20:62318ba945de 806 /** Called by cmd_dispatch it parses the command against the command table.
wvd_vegt 20:62318ba945de 807 *
wvd_vegt 20:62318ba945de 808 * @param cmd the command and paramaters to parse.
wvd_vegt 20:62318ba945de 809 *
wvd_vegt 20:62318ba945de 810 * @returns the id of the parsed command.
wvd_vegt 20:62318ba945de 811 */
wvd_vegt 20:62318ba945de 812 int parse(char *cmd);
wvd_vegt 20:62318ba945de 813
wvd_vegt 20:62318ba945de 814 /** Called by scan it processes the arguments and dispatches the command.
wvd_vegt 20:62318ba945de 815 *
wvd_vegt 20:62318ba945de 816 * Note: This member calls the callback callback function.
wvd_vegt 20:62318ba945de 817 *
wvd_vegt 20:62318ba945de 818 * @param cmd the command to dispatch.
wvd_vegt 20:62318ba945de 819 */
wvd_vegt 20:62318ba945de 820 void cmd_dispatcher(char *cmd);
wvd_vegt 20:62318ba945de 821
wvd_vegt 20:62318ba945de 822 /** Generates Help from the command table and prints it.
wvd_vegt 20:62318ba945de 823 *
wvd_vegt 20:62318ba945de 824 * @param pre leading text
wvd_vegt 20:62318ba945de 825 * @param ndx the index of the command in the command table.
wvd_vegt 20:62318ba945de 826 * @param post trailing text.
wvd_vegt 20:62318ba945de 827 */
wvd_vegt 20:62318ba945de 828 void cmd_help(char *pre, int ndx, char *post);
wvd_vegt 20:62318ba945de 829
wvd_vegt 20:62318ba945de 830 /** Dumps all commands in ini file format.
wvd_vegt 20:62318ba945de 831 */
wvd_vegt 20:62318ba945de 832 void cmd_dump();
wvd_vegt 20:62318ba945de 833
wvd_vegt 20:62318ba945de 834 /** memset wrapper.
wvd_vegt 20:62318ba945de 835 *
wvd_vegt 20:62318ba945de 836 * @param p The string to be cleared.
wvd_vegt 20:62318ba945de 837 * @param siz The string size.
wvd_vegt 20:62318ba945de 838 */
wvd_vegt 20:62318ba945de 839 void zeromemory(char *p,unsigned int siz);
wvd_vegt 20:62318ba945de 840
wvd_vegt 20:62318ba945de 841 /** Case insensitive compare.
wvd_vegt 20:62318ba945de 842 *
wvd_vegt 20:62318ba945de 843 * @see strcmp.
wvd_vegt 20:62318ba945de 844 *
wvd_vegt 20:62318ba945de 845 * @param s1
wvd_vegt 20:62318ba945de 846 * @param s2 the second string to compare.
wvd_vegt 20:62318ba945de 847 *
wvd_vegt 20:62318ba945de 848 * @returns 0 if s1=s2, -1 if s1<s2 or +1 if s1>s2.
wvd_vegt 20:62318ba945de 849 */
wvd_vegt 20:62318ba945de 850 int stricmp (char *s1, char *s2);
wvd_vegt 20:62318ba945de 851
wvd_vegt 20:62318ba945de 852 /** Internal Echo Flag Storage.
wvd_vegt 20:62318ba945de 853 */
wvd_vegt 20:62318ba945de 854 bool echo;
wvd_vegt 20:62318ba945de 855
wvd_vegt 20:62318ba945de 856 /** Internal VT100 Bold Flag Storage.
wvd_vegt 20:62318ba945de 857 */
wvd_vegt 20:62318ba945de 858 bool bold;
wvd_vegt 20:62318ba945de 859
wvd_vegt 20:62318ba945de 860 /** Internal Command Table Length Storage.
wvd_vegt 20:62318ba945de 861 */
wvd_vegt 20:62318ba945de 862 //int CMD_TBL_LEN;
wvd_vegt 20:62318ba945de 863
wvd_vegt 20:62318ba945de 864 //Macro's.
wvd_vegt 20:62318ba945de 865 /** Internal Macro Pointer.
wvd_vegt 20:62318ba945de 866 */
wvd_vegt 20:62318ba945de 867 int macro_ptr;
wvd_vegt 20:62318ba945de 868
wvd_vegt 20:62318ba945de 869 /** Internal Macro Buffer.
wvd_vegt 20:62318ba945de 870 */
wvd_vegt 20:62318ba945de 871 char macro_buf[1 + MAX_CMD_LEN];
wvd_vegt 20:62318ba945de 872
wvd_vegt 20:62318ba945de 873 /** Used for parsing parameters.
wvd_vegt 20:62318ba945de 874 */
wvd_vegt 20:62318ba945de 875 enum parmtype {
wvd_vegt 20:62318ba945de 876 PARM_UNUSED, //0
wvd_vegt 20:62318ba945de 877
wvd_vegt 20:62318ba945de 878 PARM_FLOAT, //1 (f)
wvd_vegt 20:62318ba945de 879
wvd_vegt 20:62318ba945de 880 PARM_LONG, //2 (l/ul)
wvd_vegt 20:62318ba945de 881 PARM_INT, //3 (i/ui)
wvd_vegt 20:62318ba945de 882 PARM_SHORT, //4 (w/uw)
wvd_vegt 20:62318ba945de 883
wvd_vegt 20:62318ba945de 884 PARM_CHAR, //5 (c/uc)
wvd_vegt 20:62318ba945de 885 PARM_STRING //6 (s)
wvd_vegt 20:62318ba945de 886 };
wvd_vegt 20:62318ba945de 887
wvd_vegt 20:62318ba945de 888 /** Used for parsing parameters.
wvd_vegt 20:62318ba945de 889 */
wvd_vegt 20:62318ba945de 890 union value {
wvd_vegt 20:62318ba945de 891 float f;
wvd_vegt 20:62318ba945de 892
wvd_vegt 20:62318ba945de 893 unsigned long ul;
wvd_vegt 20:62318ba945de 894 long l;
wvd_vegt 20:62318ba945de 895
wvd_vegt 20:62318ba945de 896 int i;
wvd_vegt 20:62318ba945de 897 unsigned int ui;
wvd_vegt 20:62318ba945de 898
wvd_vegt 20:62318ba945de 899 short w;
wvd_vegt 20:62318ba945de 900 unsigned short uw;
wvd_vegt 20:62318ba945de 901
wvd_vegt 20:62318ba945de 902 char c;
wvd_vegt 20:62318ba945de 903 unsigned char uc;
wvd_vegt 20:62318ba945de 904
wvd_vegt 20:62318ba945de 905 char s[MAX_PARM_LEN];
wvd_vegt 20:62318ba945de 906 };
wvd_vegt 20:62318ba945de 907
wvd_vegt 20:62318ba945de 908 /** Used for parsing parameters.
wvd_vegt 20:62318ba945de 909 */
wvd_vegt 20:62318ba945de 910 struct parm {
wvd_vegt 20:62318ba945de 911 enum parmtype type;
wvd_vegt 20:62318ba945de 912 union value val;
wvd_vegt 20:62318ba945de 913 };
wvd_vegt 20:62318ba945de 914
wvd_vegt 20:62318ba945de 915 //------------------------------------------------------------------------------
wvd_vegt 20:62318ba945de 916 //----Buffers & Storage.
wvd_vegt 20:62318ba945de 917 //------------------------------------------------------------------------------
wvd_vegt 20:62318ba945de 918
wvd_vegt 20:62318ba945de 919 /** Command Buffer.
wvd_vegt 20:62318ba945de 920 */
wvd_vegt 20:62318ba945de 921 char cmdbuf [1 + MAX_CMD_LEN]; // command buffer
wvd_vegt 20:62318ba945de 922
wvd_vegt 20:62318ba945de 923 /** Command Buffer Pointer.
wvd_vegt 20:62318ba945de 924 */
wvd_vegt 20:62318ba945de 925 char cmdndx; // command index
wvd_vegt 20:62318ba945de 926
wvd_vegt 20:62318ba945de 927 /** Last Command Buffer (Used when pressing Cursor Up).
wvd_vegt 20:62318ba945de 928 */
wvd_vegt 20:62318ba945de 929 char lstbuf [1 + MAX_CMD_LEN]; // last command buffer
wvd_vegt 20:62318ba945de 930
wvd_vegt 20:62318ba945de 931 /** Escape Code Buffer.
wvd_vegt 20:62318ba945de 932 */
wvd_vegt 20:62318ba945de 933 char escbuf [1 + MAX_ESC_LEN];
wvd_vegt 20:62318ba945de 934
wvd_vegt 20:62318ba945de 935 /** Escape Code Buffer Pointer.
wvd_vegt 20:62318ba945de 936 */
wvd_vegt 20:62318ba945de 937 unsigned char escndx;
wvd_vegt 20:62318ba945de 938
wvd_vegt 20:62318ba945de 939 /** Storage for Parsed Parameters
wvd_vegt 20:62318ba945de 940 */
wvd_vegt 20:62318ba945de 941 struct parm parms[MAX_ARGS];
wvd_vegt 20:62318ba945de 942
wvd_vegt 20:62318ba945de 943 /** Parsed Parameters Pointer.
wvd_vegt 20:62318ba945de 944 */
wvd_vegt 20:62318ba945de 945 int noparms;
wvd_vegt 20:62318ba945de 946
wvd_vegt 20:62318ba945de 947 /** Current Selected Subsystem (-1 for Global).
wvd_vegt 20:62318ba945de 948 */
wvd_vegt 20:62318ba945de 949 int subsystem;
wvd_vegt 20:62318ba945de 950
wvd_vegt 20:62318ba945de 951 /** No of arguments found in command.
wvd_vegt 20:62318ba945de 952 */
wvd_vegt 20:62318ba945de 953 int argcnt;
wvd_vegt 20:62318ba945de 954
wvd_vegt 20:62318ba945de 955 /** No of arguments to find in parameter definition (Command Table).
wvd_vegt 20:62318ba945de 956 */
wvd_vegt 20:62318ba945de 957 int argfnd;
wvd_vegt 20:62318ba945de 958
wvd_vegt 20:62318ba945de 959 /** strtoXX() Error detection.
wvd_vegt 20:62318ba945de 960 */
wvd_vegt 20:62318ba945de 961 int error;
wvd_vegt 20:62318ba945de 962 };
wvd_vegt 20:62318ba945de 963
wvd_vegt 20:62318ba945de 964 extern "C" void mbed_reset();
wvd_vegt 20:62318ba945de 965
wvd_vegt 20:62318ba945de 966 #endif