]> git.donarmstrong.com Git - kiibohd-controller.git/blob - Debug/cli/cli.c
Fixing CLI command processing bug.
[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",    "This command :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 void commandLookup_cli()
176 {
177         // Ignore command if buffer is 0 length
178         if ( CLILineBufferCurrent == 0 )
179                 return;
180
181         // Set the last+1 character of the buffer to NULL for string processing
182         CLILineBuffer[CLILineBufferCurrent] = '\0';
183
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
188
189         // Locates first space delimiter
190         char* argPtr = cmdPtr + 1;
191         while ( *argPtr != ' ' && *argPtr != '\0' )
192                 argPtr++;
193
194         // Point to the first character of args or a NULL (no args) and set the space delimiter as a NULL
195         (++argPtr)[-1] = '\0';
196
197         // Scan array of dictionaries for a valid command match
198         for ( uint8_t dict = 0; dict < CLIDictionariesUsed; dict++ )
199         {
200                 // Parse each cmd until a null command entry is found, or an argument match
201                 for ( uint8_t cmd = 0; CLIDict[dict][cmd].name != 0; cmd++ )
202                 {
203                         // Compare the first argument and each command entry
204                         if ( eqStr( cmdPtr, CLIDict[dict][cmd].name ) )
205                         {
206                                 // Run the specified command function pointer
207                                 //   argPtr is already pointing at the first character of the arguments
208                                 (*CLIDict[dict][cmd].function)( argPtr );
209
210                                 return;
211                         }
212                 }
213         }
214
215         // No match for the command...
216         print( NL );
217         erro_dPrint("\"", CLILineBuffer, "\" is not a valid command...try help");
218 }
219
220 void registerDictionary_cli( CLIDictItem *cmdDict )
221 {
222         // Make sure this max limit of dictionaries hasn't been reached
223         if ( CLIDictionariesUsed >= CLIMaxDictionaries )
224         {
225                 erro_print("Max number of dictionaries defined already...");
226                 return;
227         }
228
229         // Add dictionary
230         CLIDict[CLIDictionariesUsed++] = cmdDict;
231 }
232
233
234
235 // ----- CLI Command Functions -----
236
237 void cliFunc_help( char* args )
238 {
239         print( NL );
240         print("Help!");
241         dPrint( args );
242 }
243
244 void cliFunc_version( char* args )
245 {
246         print( NL );
247         print("Version!");
248         dPrint( args );
249 }
250