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