3 This file is part of the AVR-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 <avr/pgmspace.h>
34 #include "string-extras.h"
38 cli_rx_fpt cli_rx = NULL;
39 cli_tx_fpt cli_tx = NULL;
42 void cli_putc(char c){
47 uint16_t cli_getc(void){
50 return ((uint16_t)-1);
54 uint16_t cli_getc_cecho(void){
58 if(cli_tx && cli_echo)
62 return ((uint16_t)-1);
65 void cli_putstr(char* s){
72 void cli_putstr_P(PGM_P s){
77 c = pgm_read_byte(s++);
84 void cli_hexdump(void* data, uint16_t length){
85 char hex_tab[] = {'0', '1', '2', '3',
92 cli_tx(hex_tab[(*((uint8_t*)data))>>4]);
93 cli_tx(hex_tab[(*((uint8_t*)data))&0xf]);
94 data = (uint8_t*)data +1;
98 void cli_hexdump2(void* data, uint16_t length){
99 char hex_tab[] = {'0', '1', '2', '3',
106 cli_tx(hex_tab[(*((uint8_t*)data))>>4]);
107 cli_tx(hex_tab[(*((uint8_t*)data))&0xf]);
109 data = (uint8_t*)data +1;
114 void cli_auto_help(uint16_t maxcmdlength, PGM_VOID_P cmdlist){
115 cmdlist_entry_t item;
120 cli_putstr_P(PSTR("\r\n[auto help] available commands:\r\n"
121 " <command> - <params> - <address>\r\n"));
123 item.cmd_name = (void*)pgm_read_word(cmdlist+0);
124 item.cmd_param_str = (void*)pgm_read_word(cmdlist+2);
125 item.cmd_function = (void_fpt)pgm_read_word(cmdlist+4);
126 cmdlist = (uint8_t*)cmdlist+CMDLIST_ENTRY_SIZE;
127 if(item.cmd_name==NULL){
131 cli_putstr_P(item.cmd_name);
132 i=maxcmdlength-strlen_P(item.cmd_name);
135 cli_putstr_P(PSTR(" - "));
136 if(item.cmd_param_str==NULL){
137 cli_putstr_P(PSTR("none \t- 0x"));
139 if(item.cmd_param_str==(void*)1){
140 cli_putstr_P(PSTR("yes \t- 0x"));
142 cli_putstr_P(item.cmd_param_str);
143 cli_putstr_P(PSTR(" \t- 0x"));
146 cli_hexdump(&item.cmd_function, 2);
147 cli_putstr_P(PSTR("\r\n"));
151 void echo_ctrl(char* s){
153 if(s==NULL || *s=='\0'){
154 cli_putstr_P(PSTR("\r\necho is "));
155 cli_putstr_P(cli_echo?PSTR("on"):PSTR("off"));
156 cli_putstr_P(PSTR("\r\n"));
159 if(!strcmp_P(s, PSTR("true")) || !strcmp_P(s, PSTR("on")) || *s=='1'){
162 if(!strcmp_P(s, PSTR("false")) || !strcmp_P(s, PSTR("off")) || *s=='0'){
167 typedef void(*str_fpt)(char*);
169 #define CLI_BACKSPACE 8
170 #define CLI_TABULATOR 9
172 int8_t search_and_call(char* cmd, uint16_t maxcmdlength, PGM_VOID_P cmdlist){
173 PGM_VOID_P cmdlist_orig = cmdlist;
174 if(*cmd=='\0' || *cmd=='#')
176 if(!strcmp_P(cmd, PSTR("exit")))
178 if((!strcmp_P(cmd, PSTR("help"))) || (!strcmp_P(cmd, PSTR("?")))){
179 cli_auto_help(maxcmdlength, cmdlist);
182 uint16_t fwlength=firstword_length(cmd);
184 memcpy(fw, cmd, fwlength);
186 cmdlist_entry_t item;
188 item.cmd_name = (void*)pgm_read_word(cmdlist+0);
189 item.cmd_param_str = (void*)pgm_read_word(cmdlist+2);
190 item.cmd_function = (void_fpt)pgm_read_word(cmdlist+4);
191 cmdlist = (uint8_t*)cmdlist+CMDLIST_ENTRY_SIZE;
192 }while(item.cmd_name!=NULL && strcmp_P(fw, item.cmd_name));
193 if(item.cmd_name==NULL){
194 cli_auto_help(maxcmdlength, cmdlist_orig);
196 if(item.cmd_function==NULL)
198 switch((uint16_t)item.cmd_param_str){
203 if(cmd[fwlength]=='\0'){
204 ((str_fpt)item.cmd_function)(cmd+fwlength);
206 ((str_fpt)item.cmd_function)(cmd+fwlength+1);
210 cli_putstr_P(PSTR("\r\nparam parsing currently not implemented!\r\n"));
218 uint16_t max_cmd_length(PGM_VOID_P cmdlist){
222 str = (char*)pgm_read_word(cmdlist);
223 cmdlist = (uint8_t*)cmdlist + CMDLIST_ENTRY_SIZE;
232 uint8_t cli_completion(char* buffer, uint16_t maxcmdlength, PGM_VOID_P cmdlist){
234 char ref[maxcmdlength+1];
237 /* check if we are behind the first word */
239 if(!isgraph(buffer[i++]))
243 itemstr = (char*)pgm_read_word(cmdlist);
246 cmdlist = (uint8_t*)cmdlist +CMDLIST_ENTRY_SIZE;
247 if(!strncmp_P(buffer, itemstr, i)){
249 strcpy_P(ref, itemstr);
251 ref[stridentcnt_P(ref, itemstr)]='\0';
255 i = strcmp(buffer, ref);
261 void cli_option_listing(char* buffer, PGM_VOID_P cmdlist){
263 uint16_t len=strlen(buffer);
265 itemstr = (char*)pgm_read_word(cmdlist);
267 cli_putstr_P(PSTR("\r\n>"));
271 cmdlist = (uint8_t*)cmdlist +CMDLIST_ENTRY_SIZE;
272 if(!strncmp_P(buffer, itemstr, len)){
273 cli_putstr_P(PSTR("\r\n "));
274 cli_putstr_P(itemstr);
279 int8_t cmd_interface(PGM_VOID_P cmd_desc){
280 uint16_t cli_buffer_size;
281 uint16_t cli_buffer_index;
283 uint8_t completion_failed=0;
286 uint16_t maxcmdlength = max_cmd_length(cmd_desc);
287 cli_buffer = calloc(1,cli_buffer_size=maxcmdlength+2);
297 if((exit_code=search_and_call(cli_buffer, maxcmdlength, cmd_desc))<=0){
301 memset(cli_buffer, 0, cli_buffer_size);
303 cli_putstr_P(PSTR("\r\n>"));
308 if(cli_buffer_index==0)
311 cli_buffer[cli_buffer_index] = '\0';
312 if(cli_echo && cli_tx){
317 if(completion_failed || cli_buffer_index==0){
319 cli_option_listing(cli_buffer, cmd_desc);
321 uint16_t old_idx = cli_buffer_index;
323 ~cli_completion(cli_buffer, maxcmdlength, cmd_desc);
324 cli_buffer_index = strlen(cli_buffer);
325 if(cli_echo && cli_tx){
326 while(old_idx<cli_buffer_index){
327 cli_tx(cli_buffer[old_idx++]);
334 if(cli_echo && cli_tx){
337 if(cli_buffer_index+1==cli_buffer_size){
338 cli_buffer = realloc(cli_buffer, cli_buffer_size+=CLI_BUFFER_BS);
342 memset(cli_buffer+cli_buffer_index+1, 0, CLI_BUFFER_BS);
344 cli_buffer[cli_buffer_index++] = c;