1 /* Copyright (C) 2014 by Jacob Alexander
3 * Permission is hereby granted, free of charge, to any person obtaining a copy
4 * of this software and associated documentation files (the "Software"), to deal
5 * in the Software without restriction, including without limitation the rights
6 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7 * copies of the Software, and to permit persons to whom the Software is
8 * furnished to do so, subject to the following conditions:
10 * The above copyright notice and this permission notice shall be included in
11 * all copies or substantial portions of the Software.
13 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 // ----- Includes -----
33 // ----- Variables -----
35 // Basic command dictionary
36 CLIDictItem basicCLIDict[] = {
37 { "help", "This command :P", cliFunc_help },
38 { "version", "Version information about this firmware.", cliFunc_version },
39 { 0, 0, 0 } // Null entry for dictionary end
44 // ----- Functions -----
51 inline void init_cli()
53 // Reset the Line Buffer
54 CLILineBufferCurrent = 0;
59 // Register first dictionary
60 CLIDictionariesUsed = 0;
61 registerDictionary_cli( basicCLIDict );
66 // Current buffer position
67 uint8_t prev_buf_pos = CLILineBufferCurrent;
69 // Process each character while available
73 // No more characters to process
74 result = usb_serial_getchar(); // Retrieve from serial module // TODO Make USB agnostic
78 char cur_char = (char)result;
80 // Make sure buffer isn't full
81 if ( CLILineBufferCurrent >= CLILineBufferMaxSize )
84 erro_print("Serial line buffer is full, dropping character and resetting...");
87 CLILineBufferCurrent = 0;
95 // Place into line buffer
96 CLILineBuffer[CLILineBufferCurrent++] = cur_char;
99 // If buffer has changed, output to screen while there are still characters in the buffer not displayed
100 while ( CLILineBufferCurrent > prev_buf_pos )
102 // Check for control characters
103 switch ( CLILineBuffer[prev_buf_pos] )
106 CLILineBufferCurrent--; // Remove the Enter
108 // Process the current line buffer
112 CLILineBufferCurrent = 0;
114 // Reset the prompt after processing has finished
118 // XXX There is a potential bug here when resetting the buffer (losing valid keypresses)
119 // Doesn't look like it will happen *that* often, so not handling it for now -HaaTa
123 // Tab completion for the current command
128 // Check for escape sequence
133 case 0x7F: // Backspace
134 // TODO - Does not handle case for arrow editing (arrows disabled atm)
135 CLILineBufferCurrent--; // Remove the backspace
137 // If there are characters in the buffer
138 if ( CLILineBufferCurrent > 0 )
140 // Remove character from current position in the line buffer
141 CLILineBufferCurrent--;
143 // Remove character from tty
150 // Place a null on the end (to use with string print)
151 CLILineBuffer[CLILineBufferCurrent] = '\0';
153 // Output buffer to screen
154 dPrint( &CLILineBuffer[prev_buf_pos] );
162 /* TODO Enable via option
163 uint8_t pos = prev_buf_pos;
164 while ( CLILineBuffer[pos] != 0 )
166 printHex( CLILineBuffer[pos++] );
175 void commandLookup_cli()
177 // Ignore command if buffer is 0 length
178 if ( CLILineBufferCurrent == 0 )
181 // Set the last+1 character of the buffer to NULL for string processing
182 CLILineBuffer[CLILineBufferCurrent] = '\0';
184 // Mark out the first argument
185 // This is done by finding the first space after a list of non-spaces and setting it NULL
186 char* cmdPtr = CLILineBuffer - 1;
187 while ( *++cmdPtr == ' ' ); // Skips leading spaces, and points to first character of cmd
189 // Locates first space delimiter, and points to first character of args or a NULL (no args)
190 char* argPtr = cmdPtr;
193 } while ( *argPtr != ' ' && *argPtr != '\0' );
195 // Set the space delimiter as a NULL
198 // Scan array of dictionaries for a valid command match
199 for ( uint8_t dict = 0; dict < CLIDictionariesUsed; dict++ )
201 // Parse each cmd until a null command entry is found, or an argument match
202 for ( uint8_t cmd = 0; CLIDict[dict][cmd].name != 0; cmd++ )
204 // Compare the first argument and each command entry
205 if ( eqStr( cmdPtr, CLIDict[dict][cmd].name ) )
207 // Run the specified command function pointer
208 // argPtr is already pointing at the first character of the arguments
209 (*CLIDict[dict][cmd].function)( argPtr );
216 // No match for the command...
218 erro_dPrint("\"", CLILineBuffer, "\" is not a valid command...try help");
221 void registerDictionary_cli( CLIDictItem *cmdDict )
223 // Make sure this max limit of dictionaries hasn't been reached
224 if ( CLIDictionariesUsed >= CLIMaxDictionaries )
226 erro_print("Max number of dictionaries defined already...");
231 CLIDict[CLIDictionariesUsed++] = cmdDict;
236 // ----- CLI Command Functions -----
238 void cliFunc_help( char* args )
245 void cliFunc_version( char* args )