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