]> git.donarmstrong.com Git - kiibohd-controller.git/blob - Debug/cli/cli.c
Added help command and function'ized the arugmentIsolator.
[kiibohd-controller.git] / Debug / cli / cli.c
1 /* Copyright (C) 2014 by Jacob Alexander
2  *
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:
9  *
10  * The above copyright notice and this permission notice shall be included in
11  * all copies or substantial portions of the Software.
12  *
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
19  * THE SOFTWARE.
20  */
21
22 // ----- Includes -----
23
24 // Compiler Includes
25 //#include <stdarg.h>
26
27 // Project Includes
28 #include "cli.h"
29 #include <print.h>
30
31
32
33 // ----- Variables -----
34
35 // Basic command dictionary
36 CLIDictItem basicCLIDict[] = {
37         { "help",    "You're looking at it :P", cliFunc_help },
38         { "version", "Version information about this firmware.", cliFunc_version },
39         { 0, 0, 0 } // Null entry for dictionary end
40 };
41
42
43
44 // ----- Functions -----
45
46 inline void prompt()
47 {
48         print(": ");
49 }
50
51 inline void init_cli()
52 {
53         // Reset the Line Buffer
54         CLILineBufferCurrent = 0;
55
56         // Set prompt
57         prompt();
58
59         // Register first dictionary
60         CLIDictionariesUsed = 0;
61         registerDictionary_cli( basicCLIDict );
62 }
63
64 void process_cli()
65 {
66         // Current buffer position
67         uint8_t prev_buf_pos = CLILineBufferCurrent;
68
69         // Process each character while available
70         int result = 0;
71         while ( 1 )
72         {
73                 // No more characters to process
74                 result = usb_serial_getchar(); // Retrieve from serial module // TODO Make USB agnostic
75                 if ( result == -1 )
76                         break;
77
78                 char cur_char = (char)result;
79
80                 // Make sure buffer isn't full
81                 if ( CLILineBufferCurrent >= CLILineBufferMaxSize )
82                 {
83                         print( NL );
84                         erro_print("Serial line buffer is full, dropping character and resetting...");
85
86                         // Clear buffer
87                         CLILineBufferCurrent = 0;
88
89                         // Reset the prompt
90                         prompt();
91
92                         return;
93                 }
94
95                 // Place into line buffer
96                 CLILineBuffer[CLILineBufferCurrent++] = cur_char;
97         }
98
99         // If buffer has changed, output to screen while there are still characters in the buffer not displayed
100         while ( CLILineBufferCurrent > prev_buf_pos )
101         {
102                 // Check for control characters
103                 switch ( CLILineBuffer[prev_buf_pos] )
104                 {
105                 case 0x0D: // Enter
106                         CLILineBufferCurrent--; // Remove the Enter
107
108                         // Process the current line buffer
109                         commandLookup_cli();
110
111                         // Reset the buffer
112                         CLILineBufferCurrent = 0;
113
114                         // Reset the prompt after processing has finished
115                         print( NL );
116                         prompt();
117
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
120                         return;
121
122                 case 0x09: // Tab
123                         // Tab completion for the current command
124                         // TODO
125                         return;
126
127                 case 0x1B: // Esc
128                         // Check for escape sequence
129                         // TODO
130                         return;
131
132                 case 0x08:
133                 case 0x7F: // Backspace
134                         // TODO - Does not handle case for arrow editing (arrows disabled atm)
135                         CLILineBufferCurrent--; // Remove the backspace
136
137                         // If there are characters in the buffer
138                         if ( CLILineBufferCurrent > 0 )
139                         {
140                                 // Remove character from current position in the line buffer
141                                 CLILineBufferCurrent--;
142
143                                 // Remove character from tty
144                                 print("\b \b");
145                         }
146
147                         break;
148
149                 default:
150                         // Place a null on the end (to use with string print)
151                         CLILineBuffer[CLILineBufferCurrent] = '\0';
152
153                         // Output buffer to screen
154                         dPrint( &CLILineBuffer[prev_buf_pos] );
155
156                         // Buffer reset
157                         prev_buf_pos++;
158
159                         break;
160                 }
161
162                 /* TODO Enable via option
163                 uint8_t pos = prev_buf_pos;
164                 while ( CLILineBuffer[pos] != 0 )
165                 {
166                         printHex( CLILineBuffer[pos++] );
167                         print(" ");
168                 }
169
170                 print( NL );
171                 */
172         }
173 }
174
175 // Takes a string, returns two pointers
176 //  One to the first non-space character
177 //  The second to the next argument (first NULL if there isn't an argument). delimited by a space
178 //  Places a NULL at the first space after the first argument
179 inline void argumentIsolation_cli( char* string, char** first, char** second )
180 {
181         // Mark out the first argument
182         // This is done by finding the first space after a list of non-spaces and setting it NULL
183         char* cmdPtr = string - 1;
184         while ( *++cmdPtr == ' ' ); // Skips leading spaces, and points to first character of cmd
185
186         // Locates first space delimiter
187         char* argPtr = cmdPtr + 1;
188         while ( *argPtr != ' ' && *argPtr != '\0' )
189                 argPtr++;
190
191         // Point to the first character of args or a NULL (no args) and set the space delimiter as a NULL
192         (++argPtr)[-1] = '\0';
193
194         // Set return variables
195         *first = cmdPtr;
196         *second = argPtr;
197 }
198
199 void commandLookup_cli()
200 {
201         // Ignore command if buffer is 0 length
202         if ( CLILineBufferCurrent == 0 )
203                 return;
204
205         // Set the last+1 character of the buffer to NULL for string processing
206         CLILineBuffer[CLILineBufferCurrent] = '\0';
207
208         // Retrieve pointers to command and beginning of arguments
209         // Places a NULL at the first space after the command
210         char* cmdPtr;
211         char* argPtr;
212         argumentIsolation_cli( CLILineBuffer, &cmdPtr, &argPtr );
213
214         // Scan array of dictionaries for a valid command match
215         for ( uint8_t dict = 0; dict < CLIDictionariesUsed; dict++ )
216         {
217                 // Parse each cmd until a null command entry is found, or an argument match
218                 for ( uint8_t cmd = 0; CLIDict[dict][cmd].name != 0; cmd++ )
219                 {
220                         // Compare the first argument and each command entry
221                         if ( eqStr( cmdPtr, CLIDict[dict][cmd].name ) )
222                         {
223                                 // Run the specified command function pointer
224                                 //   argPtr is already pointing at the first character of the arguments
225                                 (*CLIDict[dict][cmd].function)( argPtr );
226
227                                 return;
228                         }
229                 }
230         }
231
232         // No match for the command...
233         print( NL );
234         erro_dPrint("\"", CLILineBuffer, "\" is not a valid command...type \033[35mhelp\033[0m");
235 }
236
237 inline void registerDictionary_cli( CLIDictItem *cmdDict )
238 {
239         // Make sure this max limit of dictionaries hasn't been reached
240         if ( CLIDictionariesUsed >= CLIMaxDictionaries )
241         {
242                 erro_print("Max number of dictionaries defined already...");
243                 return;
244         }
245
246         // Add dictionary
247         CLIDict[CLIDictionariesUsed++] = cmdDict;
248 }
249
250
251
252 // ----- CLI Command Functions -----
253
254 void cliFunc_help( char* args )
255 {
256         // Scan array of dictionaries and print every description
257         //  (no alphabetical here, too much processing/memory to sort...)
258         for ( uint8_t dict = 0; dict < CLIDictionariesUsed; dict++ )
259         {
260                 print( NL "\033[1;32mCOMMAND SET\033[0m " );
261                 printInt8( dict + 1 );
262                 print( NL );
263
264                 // Parse each cmd/description until a null command entry is found
265                 for ( uint8_t cmd = 0; CLIDict[dict][cmd].name != 0; cmd++ )
266                 {
267                         dPrintStrs( " \033[35m", CLIDict[dict][cmd].name, NL, "\033[0m    ", CLIDict[dict][cmd].description, NL );
268                 }
269         }
270 }
271
272 void cliFunc_version( char* args )
273 {
274         print( NL );
275         print("Version!");
276         dPrint( args );
277 }
278