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

Committer:
wvd_vegt
Date:
Tue Nov 22 12:41:57 2011 +0000
Revision:
21:d22068e2bbd1
Parent:
20:62318ba945de
Child:
22:5192a468d7fa
Added a printerror method.

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 21:d22068e2bbd1 502 /** printerror prints an inifile Error Section Header and Error Msg Key=Value pair.
wvd_vegt 21:d22068e2bbd1 503 * like:
wvd_vegt 21:d22068e2bbd1 504 *
wvd_vegt 21:d22068e2bbd1 505 * [Error]\r\nmsg={errormsg}\r\n
wvd_vegt 21:d22068e2bbd1 506 *
wvd_vegt 21:d22068e2bbd1 507 * Usage: cmdb.printerror("Data Size Incorrect");
wvd_vegt 21:d22068e2bbd1 508 *
wvd_vegt 21:d22068e2bbd1 509 * @parm errormsg the error msg to print.
wvd_vegt 21:d22068e2bbd1 510 *
wvd_vegt 21:d22068e2bbd1 511 * @returns the printf return value.
wvd_vegt 21:d22068e2bbd1 512 */
wvd_vegt 21:d22068e2bbd1 513 int printerror(const char *errormsg);
wvd_vegt 21:d22068e2bbd1 514
wvd_vegt 20:62318ba945de 515 /** printvalue prints an inifile Key/Value Pair
wvd_vegt 20:62318ba945de 516 * like:
wvd_vegt 20:62318ba945de 517 *
wvd_vegt 20:62318ba945de 518 * Key=Value ;comment\r\n
wvd_vegt 20:62318ba945de 519 *
wvd_vegt 20:62318ba945de 520 * Note: the Comment is (if present) located at position 72.
wvd_vegt 20:62318ba945de 521 *
wvd_vegt 20:62318ba945de 522 * Usage: cmdb.printvaluef("Value", Cmdb::DefComPos, "Hex", "0x%8.8X", LPC_RTC->GPREG0);
wvd_vegt 20:62318ba945de 523 *
wvd_vegt 20:62318ba945de 524 * @parm key the key to print.
wvd_vegt 20:62318ba945de 525 * @parm comment the comment to print.
wvd_vegt 20:62318ba945de 526 * @parm width the location of the comment to print.
wvd_vegt 20:62318ba945de 527 * @parm format the value to print.
wvd_vegt 20:62318ba945de 528 * @parm parameter to print.
wvd_vegt 20:62318ba945de 529 *
wvd_vegt 20:62318ba945de 530 * @returns the printf return value.
wvd_vegt 20:62318ba945de 531 */
wvd_vegt 20:62318ba945de 532 int printvaluef(const char *key, const int width, const char *comment, const char *format, ...);
wvd_vegt 20:62318ba945de 533
wvd_vegt 20:62318ba945de 534 /** printvalue prints an inifile Key/Value Pair
wvd_vegt 20:62318ba945de 535 * like:
wvd_vegt 20:62318ba945de 536 *
wvd_vegt 20:62318ba945de 537 * Key=Value\r\n
wvd_vegt 20:62318ba945de 538 *
wvd_vegt 20:62318ba945de 539 * Usage: cmdb.printvaluef("Value", "0x%8.8X", LPC_RTC->GPREG0);
wvd_vegt 20:62318ba945de 540 *
wvd_vegt 20:62318ba945de 541 * @parm key the key to print.
wvd_vegt 20:62318ba945de 542 * @parm format the value to print.
wvd_vegt 20:62318ba945de 543 * @parm parameter to print.
wvd_vegt 20:62318ba945de 544 *
wvd_vegt 20:62318ba945de 545 * @returns the printf return value.
wvd_vegt 20:62318ba945de 546 */
wvd_vegt 20:62318ba945de 547 int printvaluef(const char *key, const char *format, ...);
wvd_vegt 20:62318ba945de 548
wvd_vegt 20:62318ba945de 549 /** printvalue prints an inifile Key/Value Pair
wvd_vegt 20:62318ba945de 550 * like:
wvd_vegt 20:62318ba945de 551 *
wvd_vegt 20:62318ba945de 552 * Key=Value ;comment\r\n
wvd_vegt 20:62318ba945de 553 *
wvd_vegt 20:62318ba945de 554 * Note the Comment is (if present) located at position 72.
wvd_vegt 20:62318ba945de 555 *
wvd_vegt 20:62318ba945de 556 * @parm key the key to print.
wvd_vegt 20:62318ba945de 557 * @parm value the value to print.
wvd_vegt 20:62318ba945de 558 * @parm comment the comment to print.
wvd_vegt 20:62318ba945de 559 * @parm width the location of the comment to print.
wvd_vegt 20:62318ba945de 560 *
wvd_vegt 20:62318ba945de 561 * @returns the printf return value.
wvd_vegt 20:62318ba945de 562 */
wvd_vegt 20:62318ba945de 563 int printvalue(const char *key, const char *value, const char *comment = NoComment, const int width = DefComPos);
wvd_vegt 20:62318ba945de 564
wvd_vegt 20:62318ba945de 565 //------------------------------------------------------------------------------
wvd_vegt 20:62318ba945de 566
wvd_vegt 20:62318ba945de 567 /** Initializes the parser (called by the constructor).
wvd_vegt 20:62318ba945de 568 *
wvd_vegt 20:62318ba945de 569 * @parm full if true the macro buffer is also cleared else only the command interpreter is reset.
wvd_vegt 20:62318ba945de 570 */
wvd_vegt 20:62318ba945de 571 void init(const char full);
wvd_vegt 20:62318ba945de 572
wvd_vegt 20:62318ba945de 573 //------------------------------------------------------------------------------
wvd_vegt 20:62318ba945de 574 //----These helper functions retieve parameters in the correct format.
wvd_vegt 20:62318ba945de 575 //------------------------------------------------------------------------------
wvd_vegt 20:62318ba945de 576
wvd_vegt 20:62318ba945de 577 /** Typecasts parameter ndx to a bool.
wvd_vegt 20:62318ba945de 578 *
wvd_vegt 20:62318ba945de 579 * mask: %bu
wvd_vegt 20:62318ba945de 580 *
wvd_vegt 20:62318ba945de 581 * @parm the parameter index
wvd_vegt 20:62318ba945de 582 *
wvd_vegt 20:62318ba945de 583 * @return a bool
wvd_vegt 20:62318ba945de 584 */
wvd_vegt 20:62318ba945de 585 bool BOOLPARM(int ndx) {
wvd_vegt 20:62318ba945de 586 return parms[ndx].val.uc!=0;
wvd_vegt 20:62318ba945de 587 }
wvd_vegt 20:62318ba945de 588
wvd_vegt 20:62318ba945de 589 /** Typecasts parameter ndx to a byte/unsigned char.
wvd_vegt 20:62318ba945de 590 *
wvd_vegt 20:62318ba945de 591 * mask: %bu
wvd_vegt 20:62318ba945de 592 *
wvd_vegt 20:62318ba945de 593 * @parm the parameter index
wvd_vegt 20:62318ba945de 594 *
wvd_vegt 20:62318ba945de 595 * @return a byte/unsigned char
wvd_vegt 20:62318ba945de 596 */
wvd_vegt 20:62318ba945de 597 unsigned char BYTEPARM(int ndx) {
wvd_vegt 20:62318ba945de 598 return parms[ndx].val.uc;
wvd_vegt 20:62318ba945de 599 }
wvd_vegt 20:62318ba945de 600
wvd_vegt 20:62318ba945de 601 /** Typecasts parameter ndx to a char.
wvd_vegt 20:62318ba945de 602 *
wvd_vegt 20:62318ba945de 603 * mask: %c
wvd_vegt 20:62318ba945de 604 *
wvd_vegt 20:62318ba945de 605 * @parm the parameter index
wvd_vegt 20:62318ba945de 606 *
wvd_vegt 20:62318ba945de 607 * @return a char
wvd_vegt 20:62318ba945de 608 */
wvd_vegt 20:62318ba945de 609 char CHARPARM(int ndx) {
wvd_vegt 20:62318ba945de 610 return parms[ndx].val.c;
wvd_vegt 20:62318ba945de 611 }
wvd_vegt 20:62318ba945de 612
wvd_vegt 20:62318ba945de 613 /** Typecasts parameter ndx to word/unsigned int.
wvd_vegt 20:62318ba945de 614 *
wvd_vegt 20:62318ba945de 615 * mask: %hu
wvd_vegt 20:62318ba945de 616 *
wvd_vegt 20:62318ba945de 617 * @parm the parameter index
wvd_vegt 20:62318ba945de 618 *
wvd_vegt 20:62318ba945de 619 * @return a word/unsigned int
wvd_vegt 20:62318ba945de 620 */
wvd_vegt 20:62318ba945de 621 unsigned int WORDPARM(int ndx) {
wvd_vegt 20:62318ba945de 622 return parms[ndx].val.ui;
wvd_vegt 20:62318ba945de 623 }
wvd_vegt 20:62318ba945de 624
wvd_vegt 20:62318ba945de 625 /** Typecasts parameter ndx to a unsigned int.
wvd_vegt 20:62318ba945de 626 *
wvd_vegt 20:62318ba945de 627 * mask: %u
wvd_vegt 20:62318ba945de 628 *
wvd_vegt 20:62318ba945de 629 * @parm the parameter index
wvd_vegt 20:62318ba945de 630 *
wvd_vegt 20:62318ba945de 631 * @return a unsigned int
wvd_vegt 20:62318ba945de 632 */
wvd_vegt 20:62318ba945de 633 unsigned int UINTPARM(int ndx) {
wvd_vegt 20:62318ba945de 634 return parms[ndx].val.ui;
wvd_vegt 20:62318ba945de 635 }
wvd_vegt 20:62318ba945de 636
wvd_vegt 20:62318ba945de 637 /** Typecasts parameter ndx to a int.
wvd_vegt 20:62318ba945de 638 *
wvd_vegt 20:62318ba945de 639 * mask: %i
wvd_vegt 20:62318ba945de 640 *
wvd_vegt 20:62318ba945de 641 * @parm the parameter index
wvd_vegt 20:62318ba945de 642 *
wvd_vegt 20:62318ba945de 643 * @return a int
wvd_vegt 20:62318ba945de 644 */
wvd_vegt 20:62318ba945de 645 int INTPARM(int ndx) {
wvd_vegt 20:62318ba945de 646 return parms[ndx].val.i;
wvd_vegt 20:62318ba945de 647 }
wvd_vegt 20:62318ba945de 648
wvd_vegt 20:62318ba945de 649 /** Typecasts parameter ndx to a bool.
wvd_vegt 20:62318ba945de 650 *
wvd_vegt 20:62318ba945de 651 * mask: %lu
wvd_vegt 20:62318ba945de 652 *
wvd_vegt 20:62318ba945de 653 * @parm the parameter index
wvd_vegt 20:62318ba945de 654 *
wvd_vegt 20:62318ba945de 655 * @return a bool
wvd_vegt 20:62318ba945de 656 */
wvd_vegt 20:62318ba945de 657 unsigned long DWORDPARM(int ndx) {
wvd_vegt 20:62318ba945de 658 return parms[ndx].val.ul;
wvd_vegt 20:62318ba945de 659 }
wvd_vegt 20:62318ba945de 660
wvd_vegt 20:62318ba945de 661 /** Typecasts parameter ndx to a long.
wvd_vegt 20:62318ba945de 662 *
wvd_vegt 20:62318ba945de 663 * mask: %li
wvd_vegt 20:62318ba945de 664 *
wvd_vegt 20:62318ba945de 665 * @parm the parameter index
wvd_vegt 20:62318ba945de 666 *
wvd_vegt 20:62318ba945de 667 * @return a long
wvd_vegt 20:62318ba945de 668 */
wvd_vegt 20:62318ba945de 669 long LONGPARM(int ndx) {
wvd_vegt 20:62318ba945de 670 return parms[ndx].val.l;
wvd_vegt 20:62318ba945de 671 }
wvd_vegt 20:62318ba945de 672
wvd_vegt 20:62318ba945de 673 /** Typecasts parameter ndx to a float.
wvd_vegt 20:62318ba945de 674 *
wvd_vegt 20:62318ba945de 675 * mask: %f
wvd_vegt 20:62318ba945de 676 *
wvd_vegt 20:62318ba945de 677 * @parm the parameter index
wvd_vegt 20:62318ba945de 678 *
wvd_vegt 20:62318ba945de 679 * @return a float
wvd_vegt 20:62318ba945de 680 */
wvd_vegt 20:62318ba945de 681 float FLOATPARM(int ndx) {
wvd_vegt 20:62318ba945de 682 return parms[ndx].val.f;
wvd_vegt 20:62318ba945de 683 }
wvd_vegt 20:62318ba945de 684
wvd_vegt 20:62318ba945de 685 /** Typecasts parameter ndx to a string.
wvd_vegt 20:62318ba945de 686 *
wvd_vegt 20:62318ba945de 687 * @note spaces are not allowed as it makes parsing so much harder.
wvd_vegt 20:62318ba945de 688 *
wvd_vegt 20:62318ba945de 689 * mask: %s
wvd_vegt 20:62318ba945de 690 *
wvd_vegt 20:62318ba945de 691 * @parm the parameter index
wvd_vegt 20:62318ba945de 692 *
wvd_vegt 20:62318ba945de 693 * @return a string
wvd_vegt 20:62318ba945de 694 */
wvd_vegt 20:62318ba945de 695 char* STRINGPARM(int ndx) {
wvd_vegt 20:62318ba945de 696 return parms[ndx].val.s;
wvd_vegt 20:62318ba945de 697 }
wvd_vegt 20:62318ba945de 698
wvd_vegt 20:62318ba945de 699 bool present(char *cmdstr) {
wvd_vegt 20:62318ba945de 700 return cmdid_search(cmdstr)!=CID_LAST;
wvd_vegt 20:62318ba945de 701 }
wvd_vegt 20:62318ba945de 702
wvd_vegt 20:62318ba945de 703 void replace(std::vector<cmd>& newcmds) {
wvd_vegt 20:62318ba945de 704 cmds.assign(newcmds.begin(), newcmds.end());
wvd_vegt 20:62318ba945de 705 }
wvd_vegt 20:62318ba945de 706
wvd_vegt 20:62318ba945de 707
wvd_vegt 20:62318ba945de 708 int indexof(int cid) {
wvd_vegt 20:62318ba945de 709 return cmdid_index(cid);
wvd_vegt 20:62318ba945de 710 }
wvd_vegt 20:62318ba945de 711
wvd_vegt 20:62318ba945de 712 //FAILS...
wvd_vegt 20:62318ba945de 713 /*
wvd_vegt 20:62318ba945de 714 void insert(int cid, cmd newcmd) {
wvd_vegt 20:62318ba945de 715 //Add Command (update our original and then assign/replace cmdb's copy)...
wvd_vegt 20:62318ba945de 716 vector<cmd>::iterator iter;
wvd_vegt 20:62318ba945de 717
wvd_vegt 20:62318ba945de 718 std::vector<cmd> newcmds = std::vector<cmd>(cmds);
wvd_vegt 20:62318ba945de 719
wvd_vegt 20:62318ba945de 720 iter = newcmds.begin();
wvd_vegt 20:62318ba945de 721
wvd_vegt 20:62318ba945de 722 newcmds.insert(iter+indexof(cid)+1,newcmd);
wvd_vegt 20:62318ba945de 723
wvd_vegt 20:62318ba945de 724 replace(newcmds);
wvd_vegt 20:62318ba945de 725
wvd_vegt 20:62318ba945de 726 printf("Index: %d\r\n", ndx);
wvd_vegt 20:62318ba945de 727
wvd_vegt 20:62318ba945de 728 print("check #1\r\n");
wvd_vegt 20:62318ba945de 729 print("check #2\r\n");
wvd_vegt 20:62318ba945de 730
wvd_vegt 20:62318ba945de 731 vector<cmd>::iterator it;
wvd_vegt 20:62318ba945de 732 it=newcmds.begin();
wvd_vegt 20:62318ba945de 733
wvd_vegt 20:62318ba945de 734 print("check #3\r\n");
wvd_vegt 20:62318ba945de 735 ndx++;
wvd_vegt 20:62318ba945de 736 newcmds.insert(it, newcmd);
wvd_vegt 20:62318ba945de 737 print("check #4\r\n");
wvd_vegt 20:62318ba945de 738
wvd_vegt 20:62318ba945de 739 //cmds.push_back(c1);
wvd_vegt 20:62318ba945de 740 cmds.assign(newcmds.begin(), newcmds.end());
wvd_vegt 20:62318ba945de 741 }
wvd_vegt 20:62318ba945de 742 */
wvd_vegt 20:62318ba945de 743
wvd_vegt 20:62318ba945de 744 private:
wvd_vegt 20:62318ba945de 745
wvd_vegt 20:62318ba945de 746 /** Internal Serial Port Storage.
wvd_vegt 20:62318ba945de 747 */
wvd_vegt 20:62318ba945de 748 Serial serial;
wvd_vegt 20:62318ba945de 749
wvd_vegt 20:62318ba945de 750 /** Internal Command Table Vector Storage.
wvd_vegt 20:62318ba945de 751 *
wvd_vegt 20:62318ba945de 752 * @see http://www.cplusplus.com/reference/stl/vector/
wvd_vegt 20:62318ba945de 753 */
wvd_vegt 20:62318ba945de 754 std::vector<cmd> cmds;
wvd_vegt 20:62318ba945de 755
wvd_vegt 20:62318ba945de 756 /** C callback function
wvd_vegt 20:62318ba945de 757 *
wvd_vegt 20:62318ba945de 758 * @see See http://www.newty.de/fpt/fpt.html#chapter2 for function pointers.
wvd_vegt 20:62318ba945de 759 *
wvd_vegt 20:62318ba945de 760 * C++ member equivalent would be void (Cmdb::*callback)(Cmdb&,int);
wvd_vegt 20:62318ba945de 761 */
wvd_vegt 20:62318ba945de 762 void (*user_callback)(Cmdb&,int);
wvd_vegt 20:62318ba945de 763
wvd_vegt 20:62318ba945de 764 /** Searches the escape code list for a match.
wvd_vegt 20:62318ba945de 765 *
wvd_vegt 20:62318ba945de 766 * @param char* escstr the escape code to lookup.
wvd_vegt 20:62318ba945de 767 *
wvd_vegt 20:62318ba945de 768 * @returns the index of the escape code or -1.
wvd_vegt 20:62318ba945de 769 */
wvd_vegt 20:62318ba945de 770 int escid_search(char *escstr);
wvd_vegt 20:62318ba945de 771
wvd_vegt 20:62318ba945de 772 /** Checks if the command table for a match.
wvd_vegt 20:62318ba945de 773 *
wvd_vegt 20:62318ba945de 774 * @param char* cmdstr the command to lookup.
wvd_vegt 20:62318ba945de 775 *
wvd_vegt 20:62318ba945de 776 * @returns the id of the command or -1.
wvd_vegt 20:62318ba945de 777 */
wvd_vegt 20:62318ba945de 778 int cmdid_search(char *cmdstr);
wvd_vegt 20:62318ba945de 779
wvd_vegt 20:62318ba945de 780 /** Converts an command id to an index of the command table.
wvd_vegt 20:62318ba945de 781 *
wvd_vegt 20:62318ba945de 782 * @param cmdid the command id to lookup.
wvd_vegt 20:62318ba945de 783 *
wvd_vegt 20:62318ba945de 784 * @returns the index of the command or -1.
wvd_vegt 20:62318ba945de 785 */
wvd_vegt 20:62318ba945de 786 int cmdid_index(int cmdid);
wvd_vegt 20:62318ba945de 787
wvd_vegt 20:62318ba945de 788 /** Writes a prompt to the serial port.
wvd_vegt 20:62318ba945de 789 *
wvd_vegt 20:62318ba945de 790 */
wvd_vegt 20:62318ba945de 791 void prompt(void);
wvd_vegt 20:62318ba945de 792
wvd_vegt 20:62318ba945de 793 /** Called by cmd_dispatch it parses the command against the command table.
wvd_vegt 20:62318ba945de 794 *
wvd_vegt 20:62318ba945de 795 * @param cmd the command and paramaters to parse.
wvd_vegt 20:62318ba945de 796 *
wvd_vegt 20:62318ba945de 797 * @returns the id of the parsed command.
wvd_vegt 20:62318ba945de 798 */
wvd_vegt 20:62318ba945de 799 int parse(char *cmd);
wvd_vegt 20:62318ba945de 800
wvd_vegt 20:62318ba945de 801 /** Called by scan it processes the arguments and dispatches the command.
wvd_vegt 20:62318ba945de 802 *
wvd_vegt 20:62318ba945de 803 * Note: This member calls the callback callback function.
wvd_vegt 20:62318ba945de 804 *
wvd_vegt 20:62318ba945de 805 * @param cmd the command to dispatch.
wvd_vegt 20:62318ba945de 806 */
wvd_vegt 20:62318ba945de 807 void cmd_dispatcher(char *cmd);
wvd_vegt 20:62318ba945de 808
wvd_vegt 20:62318ba945de 809 /** Generates Help from the command table and prints it.
wvd_vegt 20:62318ba945de 810 *
wvd_vegt 20:62318ba945de 811 * @param pre leading text
wvd_vegt 20:62318ba945de 812 * @param ndx the index of the command in the command table.
wvd_vegt 20:62318ba945de 813 * @param post trailing text.
wvd_vegt 20:62318ba945de 814 */
wvd_vegt 20:62318ba945de 815 void cmd_help(char *pre, int ndx, char *post);
wvd_vegt 20:62318ba945de 816
wvd_vegt 20:62318ba945de 817 /** Dumps all commands in ini file format.
wvd_vegt 20:62318ba945de 818 */
wvd_vegt 20:62318ba945de 819 void cmd_dump();
wvd_vegt 20:62318ba945de 820
wvd_vegt 20:62318ba945de 821 /** memset wrapper.
wvd_vegt 20:62318ba945de 822 *
wvd_vegt 20:62318ba945de 823 * @param p The string to be cleared.
wvd_vegt 20:62318ba945de 824 * @param siz The string size.
wvd_vegt 20:62318ba945de 825 */
wvd_vegt 20:62318ba945de 826 void zeromemory(char *p,unsigned int siz);
wvd_vegt 20:62318ba945de 827
wvd_vegt 20:62318ba945de 828 /** Case insensitive compare.
wvd_vegt 20:62318ba945de 829 *
wvd_vegt 20:62318ba945de 830 * @see strcmp.
wvd_vegt 20:62318ba945de 831 *
wvd_vegt 20:62318ba945de 832 * @param s1
wvd_vegt 20:62318ba945de 833 * @param s2 the second string to compare.
wvd_vegt 20:62318ba945de 834 *
wvd_vegt 20:62318ba945de 835 * @returns 0 if s1=s2, -1 if s1<s2 or +1 if s1>s2.
wvd_vegt 20:62318ba945de 836 */
wvd_vegt 20:62318ba945de 837 int stricmp (char *s1, char *s2);
wvd_vegt 20:62318ba945de 838
wvd_vegt 20:62318ba945de 839 /** Internal Echo Flag Storage.
wvd_vegt 20:62318ba945de 840 */
wvd_vegt 20:62318ba945de 841 bool echo;
wvd_vegt 20:62318ba945de 842
wvd_vegt 20:62318ba945de 843 /** Internal VT100 Bold Flag Storage.
wvd_vegt 20:62318ba945de 844 */
wvd_vegt 20:62318ba945de 845 bool bold;
wvd_vegt 20:62318ba945de 846
wvd_vegt 20:62318ba945de 847 /** Internal Command Table Length Storage.
wvd_vegt 20:62318ba945de 848 */
wvd_vegt 20:62318ba945de 849 //int CMD_TBL_LEN;
wvd_vegt 20:62318ba945de 850
wvd_vegt 20:62318ba945de 851 //Macro's.
wvd_vegt 20:62318ba945de 852 /** Internal Macro Pointer.
wvd_vegt 20:62318ba945de 853 */
wvd_vegt 20:62318ba945de 854 int macro_ptr;
wvd_vegt 20:62318ba945de 855
wvd_vegt 20:62318ba945de 856 /** Internal Macro Buffer.
wvd_vegt 20:62318ba945de 857 */
wvd_vegt 20:62318ba945de 858 char macro_buf[1 + MAX_CMD_LEN];
wvd_vegt 20:62318ba945de 859
wvd_vegt 20:62318ba945de 860 /** Used for parsing parameters.
wvd_vegt 20:62318ba945de 861 */
wvd_vegt 20:62318ba945de 862 enum parmtype {
wvd_vegt 20:62318ba945de 863 PARM_UNUSED, //0
wvd_vegt 20:62318ba945de 864
wvd_vegt 20:62318ba945de 865 PARM_FLOAT, //1 (f)
wvd_vegt 20:62318ba945de 866
wvd_vegt 20:62318ba945de 867 PARM_LONG, //2 (l/ul)
wvd_vegt 20:62318ba945de 868 PARM_INT, //3 (i/ui)
wvd_vegt 20:62318ba945de 869 PARM_SHORT, //4 (w/uw)
wvd_vegt 20:62318ba945de 870
wvd_vegt 20:62318ba945de 871 PARM_CHAR, //5 (c/uc)
wvd_vegt 20:62318ba945de 872 PARM_STRING //6 (s)
wvd_vegt 20:62318ba945de 873 };
wvd_vegt 20:62318ba945de 874
wvd_vegt 20:62318ba945de 875 /** Used for parsing parameters.
wvd_vegt 20:62318ba945de 876 */
wvd_vegt 20:62318ba945de 877 union value {
wvd_vegt 20:62318ba945de 878 float f;
wvd_vegt 20:62318ba945de 879
wvd_vegt 20:62318ba945de 880 unsigned long ul;
wvd_vegt 20:62318ba945de 881 long l;
wvd_vegt 20:62318ba945de 882
wvd_vegt 20:62318ba945de 883 int i;
wvd_vegt 20:62318ba945de 884 unsigned int ui;
wvd_vegt 20:62318ba945de 885
wvd_vegt 20:62318ba945de 886 short w;
wvd_vegt 20:62318ba945de 887 unsigned short uw;
wvd_vegt 20:62318ba945de 888
wvd_vegt 20:62318ba945de 889 char c;
wvd_vegt 20:62318ba945de 890 unsigned char uc;
wvd_vegt 20:62318ba945de 891
wvd_vegt 20:62318ba945de 892 char s[MAX_PARM_LEN];
wvd_vegt 20:62318ba945de 893 };
wvd_vegt 20:62318ba945de 894
wvd_vegt 20:62318ba945de 895 /** Used for parsing parameters.
wvd_vegt 20:62318ba945de 896 */
wvd_vegt 20:62318ba945de 897 struct parm {
wvd_vegt 20:62318ba945de 898 enum parmtype type;
wvd_vegt 20:62318ba945de 899 union value val;
wvd_vegt 20:62318ba945de 900 };
wvd_vegt 20:62318ba945de 901
wvd_vegt 20:62318ba945de 902 //------------------------------------------------------------------------------
wvd_vegt 20:62318ba945de 903 //----Buffers & Storage.
wvd_vegt 20:62318ba945de 904 //------------------------------------------------------------------------------
wvd_vegt 20:62318ba945de 905
wvd_vegt 20:62318ba945de 906 /** Command Buffer.
wvd_vegt 20:62318ba945de 907 */
wvd_vegt 20:62318ba945de 908 char cmdbuf [1 + MAX_CMD_LEN]; // command buffer
wvd_vegt 20:62318ba945de 909
wvd_vegt 20:62318ba945de 910 /** Command Buffer Pointer.
wvd_vegt 20:62318ba945de 911 */
wvd_vegt 20:62318ba945de 912 char cmdndx; // command index
wvd_vegt 20:62318ba945de 913
wvd_vegt 20:62318ba945de 914 /** Last Command Buffer (Used when pressing Cursor Up).
wvd_vegt 20:62318ba945de 915 */
wvd_vegt 20:62318ba945de 916 char lstbuf [1 + MAX_CMD_LEN]; // last command buffer
wvd_vegt 20:62318ba945de 917
wvd_vegt 20:62318ba945de 918 /** Escape Code Buffer.
wvd_vegt 20:62318ba945de 919 */
wvd_vegt 20:62318ba945de 920 char escbuf [1 + MAX_ESC_LEN];
wvd_vegt 20:62318ba945de 921
wvd_vegt 20:62318ba945de 922 /** Escape Code Buffer Pointer.
wvd_vegt 20:62318ba945de 923 */
wvd_vegt 20:62318ba945de 924 unsigned char escndx;
wvd_vegt 20:62318ba945de 925
wvd_vegt 20:62318ba945de 926 /** Storage for Parsed Parameters
wvd_vegt 20:62318ba945de 927 */
wvd_vegt 20:62318ba945de 928 struct parm parms[MAX_ARGS];
wvd_vegt 20:62318ba945de 929
wvd_vegt 20:62318ba945de 930 /** Parsed Parameters Pointer.
wvd_vegt 20:62318ba945de 931 */
wvd_vegt 20:62318ba945de 932 int noparms;
wvd_vegt 20:62318ba945de 933
wvd_vegt 20:62318ba945de 934 /** Current Selected Subsystem (-1 for Global).
wvd_vegt 20:62318ba945de 935 */
wvd_vegt 20:62318ba945de 936 int subsystem;
wvd_vegt 20:62318ba945de 937
wvd_vegt 20:62318ba945de 938 /** No of arguments found in command.
wvd_vegt 20:62318ba945de 939 */
wvd_vegt 20:62318ba945de 940 int argcnt;
wvd_vegt 20:62318ba945de 941
wvd_vegt 20:62318ba945de 942 /** No of arguments to find in parameter definition (Command Table).
wvd_vegt 20:62318ba945de 943 */
wvd_vegt 20:62318ba945de 944 int argfnd;
wvd_vegt 20:62318ba945de 945
wvd_vegt 20:62318ba945de 946 /** strtoXX() Error detection.
wvd_vegt 20:62318ba945de 947 */
wvd_vegt 20:62318ba945de 948 int error;
wvd_vegt 20:62318ba945de 949 };
wvd_vegt 20:62318ba945de 950
wvd_vegt 20:62318ba945de 951 extern "C" void mbed_reset();
wvd_vegt 20:62318ba945de 952
wvd_vegt 20:62318ba945de 953 #endif