]> git.cryptolib.org Git - avr-crypto-lib.git/blob - test_src/cli.c
+BlueMidnightWish
[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 #include "hexdigit_tab.h"
38
39 cli_rx_fpt cli_rx = NULL;
40 cli_tx_fpt cli_tx = NULL;
41 uint8_t cli_echo=1;
42
43 /**
44  * \brief output a character to the console
45  * 
46  */
47
48 void cli_putc(char c){
49         if(cli_tx)
50                 cli_tx(c);
51 }
52
53 /**
54  * \brief get a character from the console
55  * Gets a character from the console input and blocks
56  * until a character is recieved
57  */
58 uint16_t cli_getc(void){
59         if(cli_rx)
60                 return cli_rx();
61         return ((uint16_t)-1);
62 }
63
64 /**
65  * \brief get a character from the console
66  * Gets a char from the console input (like cli_getc())
67  * and echos it back to the console if echo is enabled.
68  */
69 uint16_t cli_getc_cecho(void){
70         char c;
71         if(cli_rx){
72                 c = cli_rx();
73                 if(cli_tx && cli_echo)
74                         cli_tx(c);
75                 return c;
76         }
77         return ((uint16_t)-1);
78 }
79
80 /**
81  * \brief ouputs a zero-terminated string from ram to the console 
82  */
83 void cli_putstr(const char* s){
84         if(!cli_tx)
85                 return;
86         while(*s)
87                 cli_tx(*s++);
88 }
89
90
91 /**
92  * \brief ouputs a zero-terminated string from flash to the console 
93  */
94 void cli_putstr_P(PGM_P s){
95         char c;
96         if(!cli_tx)
97                 return;
98         for(;;){
99                 c = pgm_read_byte(s++);
100                 if(!c)
101                         return;
102                 cli_tx(c);
103         }
104 }
105
106 /**
107  * \brief reads a line or max n characters from the console
108  * Writes characters from the console into the supplyed buffer until a '\r'
109  * character is recieved or until n character a read (whatever happens first).
110  * The string will always be terminated by a '\0' character, so the buffer
111  * should have at least a size of n+1. 
112  */
113 uint8_t cli_getsn(char* s, uint16_t n){
114         char c;
115         if(n==0)
116                 return 2;
117         while((c=cli_getc_cecho())!='\0' && c!='\r' && n--){
118                 *s++=c;
119         }
120         *s='\0';
121         return (c=='\r')?0:1;
122 }
123
124 /**
125  * \brief dumps the contents of a buffer to the console
126  * Dumps length bytes from data to the console ouput. The dump
127  * will have 2*n continous hexadecimal characters.
128  */
129 void cli_hexdump(const void* data, uint16_t length){
130         if(!cli_tx)
131                 return;
132         while(length--){
133                 cli_tx(pgm_read_byte(hexdigit_tab_P +((*((uint8_t*)data))>>4)));
134                 cli_tx(pgm_read_byte(hexdigit_tab_P +((*((uint8_t*)data))&0xf)));
135                 data = (uint8_t*)data +1;
136         }
137 }
138
139 /**
140  * \brief dumps the contents of a buffer to the console
141  * This function behaves like cli_hexdump except that the
142  * bytes are dumped in reverse order. This is usefull to dump
143  * integers which ar e in little endian order.
144  */
145 void cli_hexdump_rev(const void* data, uint16_t length){
146         if(!cli_tx)
147                 return;
148         data = (uint8_t*)data + length -1;
149         while(length--){
150                 cli_tx(pgm_read_byte(hexdigit_tab_P +((*((uint8_t*)data))>>4)));
151                 cli_tx(pgm_read_byte(hexdigit_tab_P +((*((uint8_t*)data))&0xf)));
152                 data = (uint8_t*)data -1;
153         }
154 }
155
156 /**
157  * \brief dumps the contents of a buffer to the console
158  * Like cli_hexdump but bytes are seperated with a single space
159  * on the console output.
160  */
161 void cli_hexdump2(const void* data, uint16_t length){
162         if(!cli_tx)
163                 return;
164         while(length--){
165                 cli_tx(pgm_read_byte(hexdigit_tab_P +((*((uint8_t*)data))>>4)));
166                 cli_tx(pgm_read_byte(hexdigit_tab_P +((*((uint8_t*)data))&0xf)));
167                 cli_tx(' ');
168                 data = (uint8_t*)data +1;
169         }
170 }
171
172 /**
173  * \brief dumps the contents of a buffer to the console
174  * Like cli_hexdump but bytes are seperated with a single space
175  * on the console output.
176  */
177 void cli_hexdump_block(const void* data, uint16_t length, uint8_t indent, uint8_t width){
178         uint16_t i;
179         uint8_t  j;
180         if(!cli_tx)
181                 return;
182         for(i=0; i<length; ++i){
183                 if(i%width==0){
184                         cli_putstr_P(PSTR("\r\n"));
185                         for(j=0; j<indent; ++j){
186                                 cli_tx(' ');
187                         }
188                 }
189                 cli_tx(pgm_read_byte(hexdigit_tab_P +((*((uint8_t*)data))>>4)));
190                 cli_tx(pgm_read_byte(hexdigit_tab_P +((*((uint8_t*)data))&0xf)));
191                 cli_tx(' ');
192                 data = (uint8_t*)data +1;
193         }
194 }
195
196 static
197 void cli_auto_help(uint16_t maxcmdlength, PGM_VOID_P cmdlist){
198         cmdlist_entry_t item;
199         uint16_t i;
200         if(!cli_tx)
201                 return;
202         
203         cli_putstr_P(PSTR("\r\n[auto help] available commands:\r\n"
204                           " <command> - <params> - <address>\r\n"));
205         for(;;){
206                 item.cmd_name      = (void*)pgm_read_word(cmdlist+0);
207                 item.cmd_param_str = (void*)pgm_read_word(cmdlist+2);
208                 item.cmd_function  = (void_fpt)pgm_read_word(cmdlist+4);
209                 cmdlist = (uint8_t*)cmdlist+CMDLIST_ENTRY_SIZE;
210                 if(item.cmd_name==NULL){
211                         return;
212                 }
213                 cli_tx(' ');
214                 cli_putstr_P(item.cmd_name);
215                 i=maxcmdlength-strlen_P(item.cmd_name);
216                 while(i--)
217                         cli_tx(' ');
218                 cli_putstr_P(PSTR(" - "));
219                 if(item.cmd_param_str==NULL){
220                         cli_putstr_P(PSTR("none \t- 0x"));
221                 } else {
222                         if(item.cmd_param_str==(void*)1){
223                                 cli_putstr_P(PSTR("yes  \t- 0x"));
224                         } else {
225                                 cli_putstr_P(item.cmd_param_str);
226                                 cli_putstr_P(PSTR(" \t- 0x"));
227                         }
228                 }
229                 cli_hexdump_rev(&item.cmd_function, 2); 
230                 cli_putstr_P(PSTR("\r\n"));
231         }
232 }
233
234 void echo_ctrl(char* s){
235         s = strstrip(s);
236         if(s==NULL || *s=='\0'){
237                 cli_putstr_P(PSTR("\r\necho is "));
238                 cli_putstr_P(cli_echo?PSTR("on"):PSTR("off"));
239                 cli_putstr_P(PSTR("\r\n"));             
240         }
241         strlwr(s);
242         if(!strcmp_P(s, PSTR("true")) || !strcmp_P(s, PSTR("on")) || *s=='1'){
243                 cli_echo=1;
244         }
245         if(!strcmp_P(s, PSTR("false")) || !strcmp_P(s, PSTR("off")) || *s=='0'){
246                 cli_echo=0;
247         }
248 }
249
250 typedef void(*str_fpt)(char*);
251 #define CLI_ENTER     13
252 #define CLI_BACKSPACE  8
253 #define CLI_TABULATOR  9
254
255 int8_t search_and_call(char* cmd, uint16_t maxcmdlength, PGM_VOID_P cmdlist){
256         PGM_VOID_P cmdlist_orig = cmdlist;
257         if(*cmd=='\0' || *cmd=='#')
258                 return 1;
259         if(!strcmp_P(cmd, PSTR("exit")))
260                 return 0;
261         if((!strcmp_P(cmd, PSTR("help"))) || (!strcmp_P(cmd, PSTR("?")))){
262                 cli_auto_help(maxcmdlength, cmdlist);
263                 return 1;
264         }
265         uint16_t fwlength=firstword_length(cmd);
266         char fw[fwlength+1];
267         memcpy(fw, cmd, fwlength);
268         fw[fwlength] = '\0';
269         cmdlist_entry_t item;
270         do{
271                 item.cmd_name =      (void*)pgm_read_word(cmdlist+0);
272                 item.cmd_param_str = (void*)pgm_read_word(cmdlist+2);
273                 item.cmd_function =  (void_fpt)pgm_read_word(cmdlist+4);
274                 cmdlist = (uint8_t*)cmdlist+CMDLIST_ENTRY_SIZE;
275         }while(item.cmd_name!=NULL && strcmp_P(fw, item.cmd_name));
276         if(item.cmd_name==NULL){
277                 cli_auto_help(maxcmdlength, cmdlist_orig);
278         } else {
279                 if(item.cmd_function==NULL)
280                         return 2;
281                 switch((uint16_t)item.cmd_param_str){
282                         case 0:
283                                 item.cmd_function();
284                                 break;
285                         case 1:
286                                 if(cmd[fwlength]=='\0'){
287                                         ((str_fpt)item.cmd_function)(cmd+fwlength);
288                                 } else {
289                                         ((str_fpt)item.cmd_function)(cmd+fwlength+1);
290                                 }
291                                 break;
292                         default:
293                                 cli_putstr_P(PSTR("\r\nparam parsing currently not implemented!\r\n"));
294                                 break;
295                 }       
296                 
297         }       
298         return 1;        
299 }
300
301 uint16_t max_cmd_length(PGM_VOID_P cmdlist){
302         uint16_t t,ret=0;
303         char* str;
304         for(;;){
305                 str = (char*)pgm_read_word(cmdlist);
306                 cmdlist = (uint8_t*)cmdlist + CMDLIST_ENTRY_SIZE;
307                 if(str==NULL)
308                         return ret;
309                 t = strlen_P(str);
310                 if(t>ret)
311                         ret=t;
312         }
313 }
314
315 uint8_t cli_completion(char* buffer, uint16_t maxcmdlength, PGM_VOID_P cmdlist){
316         uint8_t i=0;
317         char ref[maxcmdlength+1];
318         char* itemstr;
319         ref[0]='\0';
320         /* check if we are behind the first word */
321         while(buffer[i]){
322                 if(!isgraph(buffer[i++]))
323                         return 0;
324         }
325         for(;;){
326                 itemstr = (char*)pgm_read_word(cmdlist);
327                 if(itemstr==NULL)
328                         break;
329                 cmdlist = (uint8_t*)cmdlist +CMDLIST_ENTRY_SIZE;
330                 if(!strncmp_P(buffer, itemstr, i)){
331                         if(!ref[0]){
332                                 strcpy_P(ref, itemstr);
333                         }else{
334                                 ref[stridentcnt_P(ref, itemstr)]='\0';
335                         }
336                 }
337         }
338         i = strcmp(buffer, ref);
339         if(i)
340                 strcpy(buffer, ref);
341         return ~i;
342 }
343
344 void cli_option_listing(char* buffer, PGM_VOID_P cmdlist){
345         char* itemstr;
346         uint16_t len=strlen(buffer);
347         for(;;){
348                 itemstr = (char*)pgm_read_word(cmdlist);
349                 if(itemstr==NULL){
350                         cli_putstr_P(PSTR("\r\n>"));
351                         cli_putstr(buffer);
352                         return;
353                 }
354                 cmdlist = (uint8_t*)cmdlist +CMDLIST_ENTRY_SIZE;
355                 if(!strncmp_P(buffer, itemstr, len)){
356                         cli_putstr_P(PSTR("\r\n    "));
357                         cli_putstr_P(itemstr);
358                 }
359         }
360 }
361
362 int8_t cmd_interface(PGM_VOID_P cmd_desc){
363         uint16_t cli_buffer_size;
364         uint16_t cli_buffer_index;
365         int8_t exit_code;
366         uint8_t completion_failed=0;
367         char* cli_buffer;
368         char c;
369         uint16_t maxcmdlength = max_cmd_length(cmd_desc);
370         cli_buffer = calloc(1,cli_buffer_size=maxcmdlength+2);
371         cli_buffer_index=0;
372         if(!cli_rx)
373                 return -1;
374         if(cli_tx)
375                 cli_tx('>');
376         for(;;){
377                 c = cli_rx();
378                 switch (c){
379                 case CLI_ENTER:
380                         if((exit_code=search_and_call(cli_buffer, maxcmdlength, cmd_desc))<=0){
381                                 free(cli_buffer);
382                                 return exit_code;
383                         }
384                         memset(cli_buffer, 0, cli_buffer_size);
385                         cli_buffer_index=0;
386                         cli_putstr_P(PSTR("\r\n>"));
387                         completion_failed=0;
388                         break;
389                 case CLI_BACKSPACE:
390                         completion_failed=0;
391                         if(cli_buffer_index==0)
392                                 break;
393                         cli_buffer_index--;
394                         cli_buffer[cli_buffer_index] = '\0';
395                         if(cli_echo && cli_tx){
396                                 cli_tx(c);
397                         }
398                         break;
399                 case CLI_TABULATOR:
400                         if(completion_failed || cli_buffer_index==0){
401                                 if(cli_tx)
402                                         cli_option_listing(cli_buffer, cmd_desc);
403                         } else {
404                                 uint16_t old_idx = cli_buffer_index;
405                                 completion_failed = 
406                                         ~cli_completion(cli_buffer, maxcmdlength, cmd_desc);
407                                 cli_buffer_index = strlen(cli_buffer);
408                                 if(cli_echo && cli_tx){
409                                         while(old_idx<cli_buffer_index){
410                                                 cli_tx(cli_buffer[old_idx++]);
411                                         }
412                                 }
413                         }
414                         break;
415                 default:
416                         completion_failed=0;
417                         if(cli_echo && cli_tx){
418                                 cli_tx(c);
419                         }
420                         if(cli_buffer_index+1==cli_buffer_size){
421                                 cli_buffer = realloc(cli_buffer, cli_buffer_size+=CLI_BUFFER_BS);
422                                 if(!cli_buffer){
423                                         return -2;
424                                 }
425                                 memset(cli_buffer+cli_buffer_index+1, 0, CLI_BUFFER_BS);
426                         }
427                         cli_buffer[cli_buffer_index++] = c;
428                 }
429         }
430 }