X-Git-Url: https://git.cryptolib.org/?a=blobdiff_plain;f=test_src%2Fcli.c;h=95b027215edaf495ce724af25af67eb0894544e4;hb=55961d766375e8415baf7bc1e74fe9ba087e18ad;hp=47bd6adca27dd460cf01e22e237eb87cfbccdc4a;hpb=572b35bb7409fb362441e0812cb62eab9f2411f0;p=avr-crypto-lib.git diff --git a/test_src/cli.c b/test_src/cli.c index 47bd6ad..95b0272 100644 --- a/test_src/cli.c +++ b/test_src/cli.c @@ -31,80 +31,66 @@ #include #include #include +#include "string-extras.h" #include "cli.h" #include "config.h" +#include "hexdigit_tab.h" -int16_t findstring_d0(const char* str, const char* v){ - uint8_t i=0; - while(*v){ - if(!strcmp(str, v)){ - return i; - } - while(*v++) /* go to the next string */ - ; - ++i; - } - return -1; -} +cli_rx_fpt cli_rx = NULL; +cli_tx_fpt cli_tx = NULL; +uint8_t cli_echo=1; -#ifdef CLI_OLD - -int16_t findstring_d0_P(const char* str, PGM_P v){ - uint8_t i=0; - while(pgm_read_byte(v)){ - if(!strcmp_P(str, v)){ - return i; - } - while(pgm_read_byte(v++)) /* go to the next string */ - ; - ++i; - } - return -1; -} +/** + * \brief output a character to the console + * + */ -#ifdef CLI_AUTO_HELP -#include "uart.h" +void cli_putc(char c){ + if(cli_tx) + cli_tx(c); +} -void cli_auto_help_P(PGM_P dbzstr){ - char c; - uart_putstr_P(PSTR("\r\n[auto help] available commands are:\r\n\t")); - do{ - while((c=pgm_read_byte(dbzstr++))!=0){ - uart_putc(c); - } - uart_putstr_P(PSTR("\r\n\t")); - }while((c=pgm_read_byte(dbzstr))!=0); - uart_putstr_P(PSTR("\r\n")); +/** + * \brief get a character from the console + * Gets a character from the console input and blocks + * until a character is recieved + */ +uint16_t cli_getc(void){ + if(cli_rx) + return cli_rx(); + return ((uint16_t)-1); } -int16_t execcommand_d0_P(const char* str, PGM_P v, void(*fpt[])(void) ){ - int16_t i=0; - i=findstring_d0_P(str, v); - if(i!=-1){ - if(fpt[i]) - fpt[i](); - return i; - }else{ - cli_auto_help_P(v); - return -1; +/** + * \brief get a character from the console + * Gets a char from the console input (like cli_getc()) + * and echos it back to the console if echo is enabled. + */ +uint16_t cli_getc_cecho(void){ + char c; + if(cli_rx){ + c = cli_rx(); + if(cli_tx && cli_echo) + cli_tx(c); + return c; } + return ((uint16_t)-1); } -#endif - -#else /* CLI_OLD */ - -cli_rx_fpt cli_rx = NULL; -cli_tx_fpt cli_tx = NULL; -uint8_t cli_echo=1; - -void cli_putstr(char* s){ +/** + * \brief ouputs a zero-terminated string from ram to the console + */ +void cli_putstr(const char* s){ if(!cli_tx) return; while(*s) cli_tx(*s++); } + +/** + * \brief ouputs a zero-terminated string from flash to the console + */ void cli_putstr_P(PGM_P s){ char c; if(!cli_tx) @@ -117,20 +103,97 @@ void cli_putstr_P(PGM_P s){ } } -void cli_hexdump(void* data, uint16_t length){ - char hex_tab[] = {'0', '1', '2', '3', - '4', '5', '6', '7', - '8', '9', 'A', 'B', - 'C', 'D', 'E', 'F'}; +/** + * \brief reads a line or max n characters from the console + * Writes characters from the console into the supplyed buffer until a '\r' + * character is recieved or until n character a read (whatever happens first). + * The string will always be terminated by a '\0' character, so the buffer + * should have at least a size of n+1. + */ +uint8_t cli_getsn(char* s, uint16_t n){ + char c; + if(n==0) + return 2; + while((c=cli_getc_cecho())!='\0' && c!='\r' && n--){ + *s++=c; + } + *s='\0'; + return (c=='\r')?0:1; +} + +/** + * \brief dumps the contents of a buffer to the console + * Dumps length bytes from data to the console ouput. The dump + * will have 2*n continous hexadecimal characters. + */ +void cli_hexdump(const void* data, uint16_t length){ if(!cli_tx) return; while(length--){ - cli_tx(hex_tab[(*((uint8_t*)data))>>4]); - cli_tx(hex_tab[(*((uint8_t*)data))&0xf]); + cli_tx(pgm_read_byte(hexdigit_tab_P +((*((uint8_t*)data))>>4))); + cli_tx(pgm_read_byte(hexdigit_tab_P +((*((uint8_t*)data))&0xf))); data = (uint8_t*)data +1; } } +/** + * \brief dumps the contents of a buffer to the console + * This function behaves like cli_hexdump except that the + * bytes are dumped in reverse order. This is usefull to dump + * integers which ar e in little endian order. + */ +void cli_hexdump_rev(const void* data, uint16_t length){ + if(!cli_tx) + return; + data = (uint8_t*)data + length -1; + while(length--){ + cli_tx(pgm_read_byte(hexdigit_tab_P +((*((uint8_t*)data))>>4))); + cli_tx(pgm_read_byte(hexdigit_tab_P +((*((uint8_t*)data))&0xf))); + data = (uint8_t*)data -1; + } +} + +/** + * \brief dumps the contents of a buffer to the console + * Like cli_hexdump but bytes are seperated with a single space + * on the console output. + */ +void cli_hexdump2(const void* data, uint16_t length){ + if(!cli_tx) + return; + while(length--){ + cli_tx(pgm_read_byte(hexdigit_tab_P +((*((uint8_t*)data))>>4))); + cli_tx(pgm_read_byte(hexdigit_tab_P +((*((uint8_t*)data))&0xf))); + cli_tx(' '); + data = (uint8_t*)data +1; + } +} + +/** + * \brief dumps the contents of a buffer to the console + * Like cli_hexdump but bytes are seperated with a single space + * on the console output. + */ +void cli_hexdump_block(const void* data, uint16_t length, uint8_t indent, uint8_t width){ + uint16_t i; + uint8_t j; + if(!cli_tx) + return; + for(i=0; i>4))); + cli_tx(pgm_read_byte(hexdigit_tab_P +((*((uint8_t*)data))&0xf))); + cli_tx(' '); + data = (uint8_t*)data +1; + } +} + +static void cli_auto_help(uint16_t maxcmdlength, PGM_VOID_P cmdlist){ cmdlist_entry_t item; uint16_t i; @@ -143,7 +206,7 @@ void cli_auto_help(uint16_t maxcmdlength, PGM_VOID_P cmdlist){ item.cmd_name = (void*)pgm_read_word(cmdlist+0); item.cmd_param_str = (void*)pgm_read_word(cmdlist+2); item.cmd_function = (void_fpt)pgm_read_word(cmdlist+4); - cmdlist = (uint8_t*)cmdlist+6; + cmdlist = (uint8_t*)cmdlist+CMDLIST_ENTRY_SIZE; if(item.cmd_name==NULL){ return; } @@ -163,19 +226,13 @@ void cli_auto_help(uint16_t maxcmdlength, PGM_VOID_P cmdlist){ cli_putstr_P(PSTR(" \t- 0x")); } } - cli_hexdump(&item.cmd_function, 2); + cli_hexdump_rev(&item.cmd_function, 2); cli_putstr_P(PSTR("\r\n")); } } -uint16_t firstword_length(char* s){ - uint16_t ret=0; - while(isalnum(*s++)) - ret++; - return ret; -} - void echo_ctrl(char* s){ + s = strstrip(s); if(s==NULL || *s=='\0'){ cli_putstr_P(PSTR("\r\necho is ")); cli_putstr_P(cli_echo?PSTR("on"):PSTR("off")); @@ -214,7 +271,7 @@ int8_t search_and_call(char* cmd, uint16_t maxcmdlength, PGM_VOID_P cmdlist){ item.cmd_name = (void*)pgm_read_word(cmdlist+0); item.cmd_param_str = (void*)pgm_read_word(cmdlist+2); item.cmd_function = (void_fpt)pgm_read_word(cmdlist+4); - cmdlist = (uint8_t*)cmdlist+6; + cmdlist = (uint8_t*)cmdlist+CMDLIST_ENTRY_SIZE; }while(item.cmd_name!=NULL && strcmp_P(fw, item.cmd_name)); if(item.cmd_name==NULL){ cli_auto_help(maxcmdlength, cmdlist_orig); @@ -246,7 +303,7 @@ uint16_t max_cmd_length(PGM_VOID_P cmdlist){ char* str; for(;;){ str = (char*)pgm_read_word(cmdlist); - cmdlist = (uint8_t*)cmdlist + 6; + cmdlist = (uint8_t*)cmdlist + CMDLIST_ENTRY_SIZE; if(str==NULL) return ret; t = strlen_P(str); @@ -255,18 +312,6 @@ uint16_t max_cmd_length(PGM_VOID_P cmdlist){ } } -uint16_t stridentcnt_P(char* a, PGM_P b){ - uint16_t i=0; - char c; - for(;;){ - c = pgm_read_byte(b++); - if(*a != c || c=='\0') - return i; - i++; - a++; - } -} - uint8_t cli_completion(char* buffer, uint16_t maxcmdlength, PGM_VOID_P cmdlist){ uint8_t i=0; char ref[maxcmdlength+1]; @@ -274,14 +319,14 @@ uint8_t cli_completion(char* buffer, uint16_t maxcmdlength, PGM_VOID_P cmdlist){ ref[0]='\0'; /* check if we are behind the first word */ while(buffer[i]){ - if(!isalnum(buffer[i++])) + if(!isgraph(buffer[i++])) return 0; } for(;;){ itemstr = (char*)pgm_read_word(cmdlist); if(itemstr==NULL) break; - cmdlist = (uint8_t*)cmdlist +6; + cmdlist = (uint8_t*)cmdlist +CMDLIST_ENTRY_SIZE; if(!strncmp_P(buffer, itemstr, i)){ if(!ref[0]){ strcpy_P(ref, itemstr); @@ -306,7 +351,7 @@ void cli_option_listing(char* buffer, PGM_VOID_P cmdlist){ cli_putstr(buffer); return; } - cmdlist = (uint8_t*)cmdlist +6; + cmdlist = (uint8_t*)cmdlist +CMDLIST_ENTRY_SIZE; if(!strncmp_P(buffer, itemstr, len)){ cli_putstr_P(PSTR("\r\n ")); cli_putstr_P(itemstr); @@ -383,6 +428,3 @@ int8_t cmd_interface(PGM_VOID_P cmd_desc){ } } } - -#endif -