3 This file is part of the ARM-Crypto-Lib.
4 Copyright (C) 2008 Daniel Otte (daniel.otte@rub.de)
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.
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.
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/>.
22 * email: daniel.otte@rub.de
23 * license: GPLv3 or later
25 * components to help implementing simple command based interaction
33 #include "string-extras.h"
35 #include "hexdigit_tab.h"
38 #define MAX(a,b) ((a>b)?(a):(b))
42 cli_rx_fpt cli_rx = NULL;
43 cli_tx_fpt cli_tx = NULL;
47 * \brief output a character to the console
51 void cli_putc(char c){
57 * \brief get a character from the console
58 * Gets a character from the console input and blocks
59 * until a character is recieved
61 uint16_t cli_getc(void){
64 return ((uint16_t)-1);
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.
72 uint16_t cli_getc_cecho(void){
76 if(cli_tx && cli_echo)
80 return ((uint16_t)-1);
84 * \brief ouputs a zero-terminated string from ram to the console
86 void cli_putstr(const char* s){
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.
101 uint8_t cli_getsn(char* s, uint32_t n){
105 while((c=cli_getc_cecho())!='\0' && c!='\r' && n){
110 return (c=='\r')?0:1;
113 void cli_hexdump_byte(uint8_t byte){
114 cli_tx(hexdigit_tab[byte>>4]);
115 cli_tx(hexdigit_tab[byte & 0xf]);
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.
123 void cli_hexdump(const void* data, uint32_t length){
127 cli_hexdump_byte(*((uint8_t*)data));
128 data = (uint8_t*)data +1;
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.
138 void cli_hexdump_rev(const void* data, uint32_t length){
141 data = (uint8_t*)data + length -1;
143 cli_hexdump_byte(*((uint8_t*)data));
144 data = (uint8_t*)data -1;
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.
153 void cli_hexdump2(const void* data, uint32_t length){
157 cli_hexdump_byte(*((uint8_t*)data));
159 data = (uint8_t*)data +1;
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.
168 void cli_hexdump_block(const void* data, uint32_t length, uint8_t indent, uint8_t width){
173 for(i=0; i<length; ++i){
176 for(j=0; j<indent; ++j){
180 cli_hexdump_byte(*((uint8_t*)data));
182 data = (uint8_t*)data +1;
187 void cli_auto_help(uint32_t maxcmdlength, const cmdlist_entry_t* cmdlist){
188 cmdlist_entry_t item;
193 cli_putstr("\r\n[auto help] available commands:\r\n"
194 " <command> - <params> - <address>\r\n");
196 memcpy(&item, cmdlist, sizeof(cmdlist_entry_t));
198 if(item.cmd_name==NULL){
202 cli_putstr(item.cmd_name);
203 i=MAX(maxcmdlength, strlen("<command>"))-strlen(item.cmd_name);
207 if(item.cmd_param_str==NULL){
208 cli_putstr("none \t- 0x");
210 if(item.cmd_param_str==(void*)1){
211 cli_putstr("yes \t- 0x");
213 cli_putstr(item.cmd_param_str);
214 cli_putstr(" \t- 0x");
217 cli_hexdump_rev(&item.cmd_function, sizeof(void*));
222 void echo_ctrl(char* s){
224 if(s==NULL || *s=='\0'){
225 cli_putstr("\r\necho is ");
226 cli_putstr(cli_echo?"on":"off");
230 if(!strcmp(s, "true") || !strcmp(s, "on") || *s=='1'){
233 if(!strcmp(s, "false") || !strcmp(s, "off") || *s=='0'){
239 typedef void(*str_fpt)(char*);
241 #define CLI_BACKSPACE 8
242 #define CLI_TABULATOR 9
245 int8_t search_and_call(char* cmd, uint32_t maxcmdlength, const cmdlist_entry_t* cmdlist){
246 const cmdlist_entry_t* cmdlist_orig=cmdlist;
250 if(*cmd=='\0' || *cmd=='#')
252 if(!strcmp(cmd, "exit"))
254 if((!strcmp(cmd, "help")) || (!strcmp(cmd, "?"))){
255 cli_auto_help(maxcmdlength, cmdlist);
258 uint16_t fwlength=firstword_length(cmd);
260 memcpy(fw, cmd, fwlength);
262 cmdlist_entry_t item;
263 while(cmdlist->cmd_name && strcmp(fw, cmdlist->cmd_name)){
266 if(!cmdlist->cmd_name){
267 cli_auto_help(maxcmdlength, cmdlist_orig);
269 memcpy(&item, cmdlist, sizeof(cmdlist_entry_t));
270 switch((uint32_t)item.cmd_param_str){
275 if(cmd[fwlength]=='\0'){
276 ((str_fpt)item.cmd_function)(cmd+fwlength);
278 ((str_fpt)item.cmd_function)(cmd+fwlength+1);
282 cli_putstr("\r\nparam parsing currently not implemented!\r\n");
291 uint16_t max_cmd_length(const cmdlist_entry_t* cmdlist){
295 str = cmdlist->cmd_name;
307 uint8_t cli_completion(char* buffer, uint16_t maxcmdlength, const cmdlist_entry_t* cmdlist){
309 char ref[maxcmdlength+1];
312 /* check if we are behind the first word */
314 if(!isgraph((uint8_t)(buffer[i++])))
318 itemstr = cmdlist->cmd_name;
322 if(!strncmp(buffer, itemstr, i)){
324 strcpy(ref, itemstr);
326 ref[stridentcnt(ref, itemstr)]='\0';
330 i = strcmp(buffer, ref);
336 void cli_option_listing(char* buffer, const cmdlist_entry_t* cmdlist){
338 uint16_t len=strlen(buffer);
340 itemstr = cmdlist->cmd_name;
347 if(!strncmp(buffer, itemstr, len)){
354 int8_t cmd_interface(const cmdlist_entry_t* cmd_desc){
355 uint16_t cli_buffer_size;
356 uint16_t cli_buffer_index;
358 uint8_t completion_failed=0;
361 uint16_t maxcmdlength = max_cmd_length(cmd_desc);
362 cli_buffer = calloc(1,cli_buffer_size=maxcmdlength+2);
372 if((exit_code=search_and_call(cli_buffer, maxcmdlength, cmd_desc))<=0){
376 memset(cli_buffer, 0, cli_buffer_size);
383 if(cli_buffer_index==0)
386 cli_buffer[cli_buffer_index] = '\0';
387 if(cli_echo && cli_tx){
392 if(completion_failed || cli_buffer_index==0){
394 cli_option_listing(cli_buffer, cmd_desc);
396 uint16_t old_idx = cli_buffer_index;
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++]);
409 if(cli_echo && cli_tx){
412 if(cli_buffer_index+1==cli_buffer_size){
413 cli_buffer = realloc(cli_buffer, cli_buffer_size+=CLI_BUFFER_BS);
417 memset(cli_buffer+cli_buffer_index+1, 0, CLI_BUFFER_BS);
419 cli_buffer[cli_buffer_index++] = c;