]> git.cryptolib.org Git - avr-crypto-lib.git/blob - test_src/cli.c
extended HFAL
[avr-crypto-lib.git] / test_src / cli.c
1 /* cli.c */
2 /*
3     This file is part of the AVR-Crypto-Lib.
4     Copyright (C) 2008  Daniel Otte (daniel.otte@rub.de)
5
6     This program is free software: you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation, either version 3 of the License, or
9     (at your option) any later version.
10
11     This program is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15
16     You should have received a copy of the GNU General Public License
17     along with this program.  If not, see <http://www.gnu.org/licenses/>.
18 */
19 /**
20  * 
21  * author: Daniel Otte
22  * email:  daniel.otte@rub.de
23  * license: GPLv3 or later
24  * 
25  * components to help implementing simple command based interaction
26  * 
27  **/
28  
29 #include <stdlib.h> 
30 #include <stdint.h>
31 #include <ctype.h>
32 #include <string.h>
33 #include <avr/pgmspace.h>
34 #include "string-extras.h"
35 #include "cli.h"
36 #include "config.h"
37
38 cli_rx_fpt cli_rx = NULL;
39 cli_tx_fpt cli_tx = NULL;
40 uint8_t cli_echo=1;
41
42 /**
43  * \brief output a character to the console
44  * 
45  */
46
47 void cli_putc(char c){
48         if(cli_tx)
49                 cli_tx(c);
50 }
51
52 /**
53  * \brief get a character from the console
54  * Gets a character from the console input and blocks
55  * until a character is recieved
56  */
57 uint16_t cli_getc(void){
58         if(cli_rx)
59                 return cli_rx();
60         return ((uint16_t)-1);
61 }
62
63 /**
64  * \brief get a character from the console
65  * Gets a char from the console input (like cli_getc())
66  * and echos it back to the console if echo is enabled.
67  */
68 uint16_t cli_getc_cecho(void){
69         char c;
70         if(cli_rx){
71                 c = cli_rx();
72                 if(cli_tx && cli_echo)
73                         cli_tx(c);
74                 return c;
75         }
76         return ((uint16_t)-1);
77 }
78
79 /**
80  * \brief ouputs a zero-terminated string from ram to the console 
81  */
82 void cli_putstr(char* s){
83         if(!cli_tx)
84                 return;
85         while(*s)
86                 cli_tx(*s++);
87 }
88
89
90 /**
91  * \brief ouputs a zero-terminated string from flash to the console 
92  */
93 void cli_putstr_P(PGM_P s){
94         char c;
95         if(!cli_tx)
96                 return;
97         for(;;){
98                 c = pgm_read_byte(s++);
99                 if(!c)
100                         return;
101                 cli_tx(c);
102         }
103 }
104
105 /**
106  * \brief reads a line or max n characters from the console
107  * Writes characters from the console into the supplyed buffer until a '\r'
108  * character is recieved or until n character a read (whatever happens first).
109  * The string will always be terminated by a '\0' character, so the buffer
110  * should have at least a size of n+1. 
111  */
112 uint8_t cli_getsn(char* s, uint16_t n){
113         char c;
114         if(n==0)
115                 return 2;
116         while((c=cli_getc_cecho())!='\0' && c!='\r' && n--){
117                 *s++=c;
118         }
119         *s='\0';
120         return (c=='\r')?0:1;
121 }
122
123 /**
124  * \brief dumps the contents of a buffer to the console
125  * Dumps length bytes from data to the console ouput. The dump
126  * will have 2*n continous hexadecimal characters.
127  */
128 void cli_hexdump(void* data, uint16_t length){
129         char hex_tab[] = {'0', '1', '2', '3', 
130                           '4', '5', '6', '7', 
131                                           '8', '9', 'A', 'B', 
132                                           'C', 'D', 'E', 'F'};
133         if(!cli_tx)
134                 return;
135         while(length--){
136                 cli_tx(hex_tab[(*((uint8_t*)data))>>4]);
137                 cli_tx(hex_tab[(*((uint8_t*)data))&0xf]);
138                 data = (uint8_t*)data +1;
139         }
140 }
141
142 /**
143  * \brief dumps the contents of a buffer to the console
144  * Like cli_hexdump but bytes are seperated with a single space
145  * on the console output.
146  */
147 void cli_hexdump2(void* data, uint16_t length){
148         char hex_tab[] = {'0', '1', '2', '3', 
149                           '4', '5', '6', '7', 
150                                           '8', '9', 'A', 'B', 
151                                           'C', 'D', 'E', 'F'};
152         if(!cli_tx)
153                 return;
154         while(length--){
155                 cli_tx(hex_tab[(*((uint8_t*)data))>>4]);
156                 cli_tx(hex_tab[(*((uint8_t*)data))&0xf]);
157                 cli_tx(' ');
158                 data = (uint8_t*)data +1;
159         }
160 }
161
162
163 static
164 void cli_auto_help(uint16_t maxcmdlength, PGM_VOID_P cmdlist){
165         cmdlist_entry_t item;
166         uint16_t i;
167         if(!cli_tx)
168                 return;
169         
170         cli_putstr_P(PSTR("\r\n[auto help] available commands:\r\n"
171                           " <command> - <params> - <address>\r\n"));
172         for(;;){
173                 item.cmd_name      = (void*)pgm_read_word(cmdlist+0);
174                 item.cmd_param_str = (void*)pgm_read_word(cmdlist+2);
175                 item.cmd_function  = (void_fpt)pgm_read_word(cmdlist+4);
176                 cmdlist = (uint8_t*)cmdlist+CMDLIST_ENTRY_SIZE;
177                 if(item.cmd_name==NULL){
178                         return;
179                 }
180                 cli_tx(' ');
181                 cli_putstr_P(item.cmd_name);
182                 i=maxcmdlength-strlen_P(item.cmd_name);
183                 while(i--)
184                         cli_tx(' ');
185                 cli_putstr_P(PSTR(" - "));
186                 if(item.cmd_param_str==NULL){
187                         cli_putstr_P(PSTR("none \t- 0x"));
188                 } else {
189                         if(item.cmd_param_str==(void*)1){
190                                 cli_putstr_P(PSTR("yes  \t- 0x"));
191                         } else {
192                                 cli_putstr_P(item.cmd_param_str);
193                                 cli_putstr_P(PSTR(" \t- 0x"));
194                         }
195                 }
196                 cli_hexdump(&item.cmd_function, 2);     
197                 cli_putstr_P(PSTR("\r\n"));
198         }
199 }
200
201 void echo_ctrl(char* s){
202         s = strstrip(s);
203         if(s==NULL || *s=='\0'){
204                 cli_putstr_P(PSTR("\r\necho is "));
205                 cli_putstr_P(cli_echo?PSTR("on"):PSTR("off"));
206                 cli_putstr_P(PSTR("\r\n"));             
207         }
208         strlwr(s);
209         if(!strcmp_P(s, PSTR("true")) || !strcmp_P(s, PSTR("on")) || *s=='1'){
210                 cli_echo=1;
211         }
212         if(!strcmp_P(s, PSTR("false")) || !strcmp_P(s, PSTR("off")) || *s=='0'){
213                 cli_echo=0;
214         }
215 }
216
217 typedef void(*str_fpt)(char*);
218 #define CLI_ENTER     13
219 #define CLI_BACKSPACE  8
220 #define CLI_TABULATOR  9
221
222 int8_t search_and_call(char* cmd, uint16_t maxcmdlength, PGM_VOID_P cmdlist){
223         PGM_VOID_P cmdlist_orig = cmdlist;
224         if(*cmd=='\0' || *cmd=='#')
225                 return 1;
226         if(!strcmp_P(cmd, PSTR("exit")))
227                 return 0;
228         if((!strcmp_P(cmd, PSTR("help"))) || (!strcmp_P(cmd, PSTR("?")))){
229                 cli_auto_help(maxcmdlength, cmdlist);
230                 return 1;
231         }
232         uint16_t fwlength=firstword_length(cmd);
233         char fw[fwlength+1];
234         memcpy(fw, cmd, fwlength);
235         fw[fwlength] = '\0';
236         cmdlist_entry_t item;
237         do{
238                 item.cmd_name =      (void*)pgm_read_word(cmdlist+0);
239                 item.cmd_param_str = (void*)pgm_read_word(cmdlist+2);
240                 item.cmd_function =  (void_fpt)pgm_read_word(cmdlist+4);
241                 cmdlist = (uint8_t*)cmdlist+CMDLIST_ENTRY_SIZE;
242         }while(item.cmd_name!=NULL && strcmp_P(fw, item.cmd_name));
243         if(item.cmd_name==NULL){
244                 cli_auto_help(maxcmdlength, cmdlist_orig);
245         } else {
246                 if(item.cmd_function==NULL)
247                         return 2;
248                 switch((uint16_t)item.cmd_param_str){
249                         case 0:
250                                 item.cmd_function();
251                                 break;
252                         case 1:
253                                 if(cmd[fwlength]=='\0'){
254                                         ((str_fpt)item.cmd_function)(cmd+fwlength);
255                                 } else {
256                                         ((str_fpt)item.cmd_function)(cmd+fwlength+1);
257                                 }
258                                 break;
259                         default:
260                                 cli_putstr_P(PSTR("\r\nparam parsing currently not implemented!\r\n"));
261                                 break;
262                 }       
263                 
264         }       
265         return 1;        
266 }
267
268 uint16_t max_cmd_length(PGM_VOID_P cmdlist){
269         uint16_t t,ret=0;
270         char* str;
271         for(;;){
272                 str = (char*)pgm_read_word(cmdlist);
273                 cmdlist = (uint8_t*)cmdlist + CMDLIST_ENTRY_SIZE;
274                 if(str==NULL)
275                         return ret;
276                 t = strlen_P(str);
277                 if(t>ret)
278                         ret=t;
279         }
280 }
281
282 uint8_t cli_completion(char* buffer, uint16_t maxcmdlength, PGM_VOID_P cmdlist){
283         uint8_t i=0;
284         char ref[maxcmdlength+1];
285         char* itemstr;
286         ref[0]='\0';
287         /* check if we are behind the first word */
288         while(buffer[i]){
289                 if(!isgraph(buffer[i++]))
290                         return 0;
291         }
292         for(;;){
293                 itemstr = (char*)pgm_read_word(cmdlist);
294                 if(itemstr==NULL)
295                         break;
296                 cmdlist = (uint8_t*)cmdlist +CMDLIST_ENTRY_SIZE;
297                 if(!strncmp_P(buffer, itemstr, i)){
298                         if(!ref[0]){
299                                 strcpy_P(ref, itemstr);
300                         }else{
301                                 ref[stridentcnt_P(ref, itemstr)]='\0';
302                         }
303                 }
304         }
305         i = strcmp(buffer, ref);
306         if(i)
307                 strcpy(buffer, ref);
308         return ~i;
309 }
310
311 void cli_option_listing(char* buffer, PGM_VOID_P cmdlist){
312         char* itemstr;
313         uint16_t len=strlen(buffer);
314         for(;;){
315                 itemstr = (char*)pgm_read_word(cmdlist);
316                 if(itemstr==NULL){
317                         cli_putstr_P(PSTR("\r\n>"));
318                         cli_putstr(buffer);
319                         return;
320                 }
321                 cmdlist = (uint8_t*)cmdlist +CMDLIST_ENTRY_SIZE;
322                 if(!strncmp_P(buffer, itemstr, len)){
323                         cli_putstr_P(PSTR("\r\n    "));
324                         cli_putstr_P(itemstr);
325                 }
326         }
327 }
328
329 int8_t cmd_interface(PGM_VOID_P cmd_desc){
330         uint16_t cli_buffer_size;
331         uint16_t cli_buffer_index;
332         int8_t exit_code;
333         uint8_t completion_failed=0;
334         char* cli_buffer;
335         char c;
336         uint16_t maxcmdlength = max_cmd_length(cmd_desc);
337         cli_buffer = calloc(1,cli_buffer_size=maxcmdlength+2);
338         cli_buffer_index=0;
339         if(!cli_rx)
340                 return -1;
341         if(cli_tx)
342                 cli_tx('>');
343         for(;;){
344                 c = cli_rx();
345                 switch (c){
346                 case CLI_ENTER:
347                         if((exit_code=search_and_call(cli_buffer, maxcmdlength, cmd_desc))<=0){
348                                 free(cli_buffer);
349                                 return exit_code;
350                         }
351                         memset(cli_buffer, 0, cli_buffer_size);
352                         cli_buffer_index=0;
353                         cli_putstr_P(PSTR("\r\n>"));
354                         completion_failed=0;
355                         break;
356                 case CLI_BACKSPACE:
357                         completion_failed=0;
358                         if(cli_buffer_index==0)
359                                 break;
360                         cli_buffer_index--;
361                         cli_buffer[cli_buffer_index] = '\0';
362                         if(cli_echo && cli_tx){
363                                 cli_tx(c);
364                         }
365                         break;
366                 case CLI_TABULATOR:
367                         if(completion_failed || cli_buffer_index==0){
368                                 if(cli_tx)
369                                         cli_option_listing(cli_buffer, cmd_desc);
370                         } else {
371                                 uint16_t old_idx = cli_buffer_index;
372                                 completion_failed = 
373                                         ~cli_completion(cli_buffer, maxcmdlength, cmd_desc);
374                                 cli_buffer_index = strlen(cli_buffer);
375                                 if(cli_echo && cli_tx){
376                                         while(old_idx<cli_buffer_index){
377                                                 cli_tx(cli_buffer[old_idx++]);
378                                         }
379                                 }
380                         }
381                         break;
382                 default:
383                         completion_failed=0;
384                         if(cli_echo && cli_tx){
385                                 cli_tx(c);
386                         }
387                         if(cli_buffer_index+1==cli_buffer_size){
388                                 cli_buffer = realloc(cli_buffer, cli_buffer_size+=CLI_BUFFER_BS);
389                                 if(!cli_buffer){
390                                         return -2;
391                                 }
392                                 memset(cli_buffer+cli_buffer_index+1, 0, CLI_BUFFER_BS);
393                         }
394                         cli_buffer[cli_buffer_index++] = c;
395                 }
396         }
397 }