]> git.cryptolib.org Git - avr-crypto-lib.git/blob - test_src/cli.c
new hash function abstraction layer + shavs + dump util + ...
[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 void cli_putc(char c){
43         if(cli_tx)
44                 cli_tx(c);
45 }
46
47 uint16_t cli_getc(void){
48         if(cli_rx)
49                 return cli_rx();
50         return ((uint16_t)-1);
51 }
52
53
54 uint16_t cli_getc_cecho(void){
55         char c;
56         if(cli_rx){
57                 c = cli_rx();
58                 if(cli_tx && cli_echo)
59                         cli_tx(c);
60                 return c;
61         }
62         return ((uint16_t)-1);
63 }
64
65 void cli_putstr(char* s){
66         if(!cli_tx)
67                 return;
68         while(*s)
69                 cli_tx(*s++);
70 }
71
72 void cli_putstr_P(PGM_P s){
73         char c;
74         if(!cli_tx)
75                 return;
76         for(;;){
77                 c = pgm_read_byte(s++);
78                 if(!c)
79                         return;
80                 cli_tx(c);
81         }
82 }
83
84 void cli_hexdump(void* data, uint16_t length){
85         char hex_tab[] = {'0', '1', '2', '3', 
86                           '4', '5', '6', '7', 
87                                           '8', '9', 'A', 'B', 
88                                           'C', 'D', 'E', 'F'};
89         if(!cli_tx)
90                 return;
91         while(length--){
92                 cli_tx(hex_tab[(*((uint8_t*)data))>>4]);
93                 cli_tx(hex_tab[(*((uint8_t*)data))&0xf]);
94                 data = (uint8_t*)data +1;
95         }
96 }
97
98 void cli_hexdump2(void* data, uint16_t length){
99         char hex_tab[] = {'0', '1', '2', '3', 
100                           '4', '5', '6', '7', 
101                                           '8', '9', 'A', 'B', 
102                                           'C', 'D', 'E', 'F'};
103         if(!cli_tx)
104                 return;
105         while(length--){
106                 cli_tx(hex_tab[(*((uint8_t*)data))>>4]);
107                 cli_tx(hex_tab[(*((uint8_t*)data))&0xf]);
108                 cli_tx(' ');
109                 data = (uint8_t*)data +1;
110         }
111 }
112
113 static
114 void cli_auto_help(uint16_t maxcmdlength, PGM_VOID_P cmdlist){
115         cmdlist_entry_t item;
116         uint16_t i;
117         if(!cli_tx)
118                 return;
119         
120         cli_putstr_P(PSTR("\r\n[auto help] available commands:\r\n"
121                           " <command> - <params> - <address>\r\n"));
122         for(;;){
123                 item.cmd_name      = (void*)pgm_read_word(cmdlist+0);
124                 item.cmd_param_str = (void*)pgm_read_word(cmdlist+2);
125                 item.cmd_function  = (void_fpt)pgm_read_word(cmdlist+4);
126                 cmdlist = (uint8_t*)cmdlist+CMDLIST_ENTRY_SIZE;
127                 if(item.cmd_name==NULL){
128                         return;
129                 }
130                 cli_tx(' ');
131                 cli_putstr_P(item.cmd_name);
132                 i=maxcmdlength-strlen_P(item.cmd_name);
133                 while(i--)
134                         cli_tx(' ');
135                 cli_putstr_P(PSTR(" - "));
136                 if(item.cmd_param_str==NULL){
137                         cli_putstr_P(PSTR("none \t- 0x"));
138                 } else {
139                         if(item.cmd_param_str==(void*)1){
140                                 cli_putstr_P(PSTR("yes  \t- 0x"));
141                         } else {
142                                 cli_putstr_P(item.cmd_param_str);
143                                 cli_putstr_P(PSTR(" \t- 0x"));
144                         }
145                 }
146                 cli_hexdump(&item.cmd_function, 2);     
147                 cli_putstr_P(PSTR("\r\n"));
148         }
149 }
150
151 void echo_ctrl(char* s){
152         s = strstrip(s);
153         if(s==NULL || *s=='\0'){
154                 cli_putstr_P(PSTR("\r\necho is "));
155                 cli_putstr_P(cli_echo?PSTR("on"):PSTR("off"));
156                 cli_putstr_P(PSTR("\r\n"));             
157         }
158         strlwr(s);
159         if(!strcmp_P(s, PSTR("true")) || !strcmp_P(s, PSTR("on")) || *s=='1'){
160                 cli_echo=1;
161         }
162         if(!strcmp_P(s, PSTR("false")) || !strcmp_P(s, PSTR("off")) || *s=='0'){
163                 cli_echo=0;
164         }
165 }
166
167 typedef void(*str_fpt)(char*);
168 #define CLI_ENTER     13
169 #define CLI_BACKSPACE  8
170 #define CLI_TABULATOR  9
171
172 int8_t search_and_call(char* cmd, uint16_t maxcmdlength, PGM_VOID_P cmdlist){
173         PGM_VOID_P cmdlist_orig = cmdlist;
174         if(*cmd=='\0' || *cmd=='#')
175                 return 1;
176         if(!strcmp_P(cmd, PSTR("exit")))
177                 return 0;
178         if((!strcmp_P(cmd, PSTR("help"))) || (!strcmp_P(cmd, PSTR("?")))){
179                 cli_auto_help(maxcmdlength, cmdlist);
180                 return 1;
181         }
182         uint16_t fwlength=firstword_length(cmd);
183         char fw[fwlength+1];
184         memcpy(fw, cmd, fwlength);
185         fw[fwlength] = '\0';
186         cmdlist_entry_t item;
187         do{
188                 item.cmd_name =      (void*)pgm_read_word(cmdlist+0);
189                 item.cmd_param_str = (void*)pgm_read_word(cmdlist+2);
190                 item.cmd_function =  (void_fpt)pgm_read_word(cmdlist+4);
191                 cmdlist = (uint8_t*)cmdlist+CMDLIST_ENTRY_SIZE;
192         }while(item.cmd_name!=NULL && strcmp_P(fw, item.cmd_name));
193         if(item.cmd_name==NULL){
194                 cli_auto_help(maxcmdlength, cmdlist_orig);
195         } else {
196                 if(item.cmd_function==NULL)
197                         return 2;
198                 switch((uint16_t)item.cmd_param_str){
199                         case 0:
200                                 item.cmd_function();
201                                 break;
202                         case 1:
203                                 if(cmd[fwlength]=='\0'){
204                                         ((str_fpt)item.cmd_function)(cmd+fwlength);
205                                 } else {
206                                         ((str_fpt)item.cmd_function)(cmd+fwlength+1);
207                                 }
208                                 break;
209                         default:
210                                 cli_putstr_P(PSTR("\r\nparam parsing currently not implemented!\r\n"));
211                                 break;
212                 }       
213                 
214         }       
215         return 1;        
216 }
217
218 uint16_t max_cmd_length(PGM_VOID_P cmdlist){
219         uint16_t t,ret=0;
220         char* str;
221         for(;;){
222                 str = (char*)pgm_read_word(cmdlist);
223                 cmdlist = (uint8_t*)cmdlist + CMDLIST_ENTRY_SIZE;
224                 if(str==NULL)
225                         return ret;
226                 t = strlen_P(str);
227                 if(t>ret)
228                         ret=t;
229         }
230 }
231
232 uint8_t cli_completion(char* buffer, uint16_t maxcmdlength, PGM_VOID_P cmdlist){
233         uint8_t i=0;
234         char ref[maxcmdlength+1];
235         char* itemstr;
236         ref[0]='\0';
237         /* check if we are behind the first word */
238         while(buffer[i]){
239                 if(!isgraph(buffer[i++]))
240                         return 0;
241         }
242         for(;;){
243                 itemstr = (char*)pgm_read_word(cmdlist);
244                 if(itemstr==NULL)
245                         break;
246                 cmdlist = (uint8_t*)cmdlist +CMDLIST_ENTRY_SIZE;
247                 if(!strncmp_P(buffer, itemstr, i)){
248                         if(!ref[0]){
249                                 strcpy_P(ref, itemstr);
250                         }else{
251                                 ref[stridentcnt_P(ref, itemstr)]='\0';
252                         }
253                 }
254         }
255         i = strcmp(buffer, ref);
256         if(i)
257                 strcpy(buffer, ref);
258         return ~i;
259 }
260
261 void cli_option_listing(char* buffer, PGM_VOID_P cmdlist){
262         char* itemstr;
263         uint16_t len=strlen(buffer);
264         for(;;){
265                 itemstr = (char*)pgm_read_word(cmdlist);
266                 if(itemstr==NULL){
267                         cli_putstr_P(PSTR("\r\n>"));
268                         cli_putstr(buffer);
269                         return;
270                 }
271                 cmdlist = (uint8_t*)cmdlist +CMDLIST_ENTRY_SIZE;
272                 if(!strncmp_P(buffer, itemstr, len)){
273                         cli_putstr_P(PSTR("\r\n    "));
274                         cli_putstr_P(itemstr);
275                 }
276         }
277 }
278
279 int8_t cmd_interface(PGM_VOID_P cmd_desc){
280         uint16_t cli_buffer_size;
281         uint16_t cli_buffer_index;
282         int8_t exit_code;
283         uint8_t completion_failed=0;
284         char* cli_buffer;
285         char c;
286         uint16_t maxcmdlength = max_cmd_length(cmd_desc);
287         cli_buffer = calloc(1,cli_buffer_size=maxcmdlength+2);
288         cli_buffer_index=0;
289         if(!cli_rx)
290                 return -1;
291         if(cli_tx)
292                 cli_tx('>');
293         for(;;){
294                 c = cli_rx();
295                 switch (c){
296                 case CLI_ENTER:
297                         if((exit_code=search_and_call(cli_buffer, maxcmdlength, cmd_desc))<=0){
298                                 free(cli_buffer);
299                                 return exit_code;
300                         }
301                         memset(cli_buffer, 0, cli_buffer_size);
302                         cli_buffer_index=0;
303                         cli_putstr_P(PSTR("\r\n>"));
304                         completion_failed=0;
305                         break;
306                 case CLI_BACKSPACE:
307                         completion_failed=0;
308                         if(cli_buffer_index==0)
309                                 break;
310                         cli_buffer_index--;
311                         cli_buffer[cli_buffer_index] = '\0';
312                         if(cli_echo && cli_tx){
313                                 cli_tx(c);
314                         }
315                         break;
316                 case CLI_TABULATOR:
317                         if(completion_failed || cli_buffer_index==0){
318                                 if(cli_tx)
319                                         cli_option_listing(cli_buffer, cmd_desc);
320                         } else {
321                                 uint16_t old_idx = cli_buffer_index;
322                                 completion_failed = 
323                                         ~cli_completion(cli_buffer, maxcmdlength, cmd_desc);
324                                 cli_buffer_index = strlen(cli_buffer);
325                                 if(cli_echo && cli_tx){
326                                         while(old_idx<cli_buffer_index){
327                                                 cli_tx(cli_buffer[old_idx++]);
328                                         }
329                                 }
330                         }
331                         break;
332                 default:
333                         completion_failed=0;
334                         if(cli_echo && cli_tx){
335                                 cli_tx(c);
336                         }
337                         if(cli_buffer_index+1==cli_buffer_size){
338                                 cli_buffer = realloc(cli_buffer, cli_buffer_size+=CLI_BUFFER_BS);
339                                 if(!cli_buffer){
340                                         return -2;
341                                 }
342                                 memset(cli_buffer+cli_buffer_index+1, 0, CLI_BUFFER_BS);
343                         }
344                         cli_buffer[cli_buffer_index++] = c;
345                 }
346         }
347 }