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