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