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"
37 #include "hexdigit_tab.h"
39 cli_rx_fpt cli_rx = NULL;
40 cli_tx_fpt cli_tx = NULL;
43 void echo_ctrl(char* s);
44 uint16_t max_cmd_length(PGM_VOID_P cmdlist);
45 int8_t search_and_call(char* cmd, uint16_t maxcmdlength, PGM_VOID_P cmdlist);
46 void cli_option_listing(char* buffer, PGM_VOID_P cmdlist);
49 void cli_auto_help(uint16_t maxcmdlength, PGM_VOID_P cmdlist);/*
56 cli_putstr_P(PSTR("\r\n[auto help] available commands:\r\n"
57 " <command> - <params> - <address>\r\n"));
59 item.cmd_name = (void*)pgm_read_word(cmdlist+0);
60 item.cmd_param_str = (void*)pgm_read_word(cmdlist+2);
61 item.cmd_function = (void_fpt)pgm_read_word(cmdlist+4);
62 cmdlist = (uint8_t*)cmdlist+CMDLIST_ENTRY_SIZE;
63 if(item.cmd_name==NULL){
67 cli_putstr_P(item.cmd_name);
68 i=maxcmdlength-strlen_P(item.cmd_name);
71 cli_putstr_P(PSTR(" - "));
72 if(item.cmd_param_str==NULL){
73 cli_putstr_P(PSTR("none \t- 0x"));
75 if(item.cmd_param_str==(void*)1){
76 cli_putstr_P(PSTR("yes \t- 0x"));
78 cli_putstr_P(item.cmd_param_str);
79 cli_putstr_P(PSTR(" \t- 0x"));
82 cli_hexdump_rev(&item.cmd_function, 2);
83 cli_putstr_P(PSTR("\r\n"));
87 typedef void(*str_fpt)(char*);
89 #define CLI_BACKSPACE 8
90 #define CLI_TABULATOR 9
92 uint8_t cli_completion(char* buffer, uint16_t maxcmdlength, PGM_VOID_P cmdlist){
94 char ref[maxcmdlength+1];
97 /* check if we are behind the first word */
99 if(!isgraph(buffer[i++]))
103 itemstr = (char*)pgm_read_word(cmdlist);
106 cmdlist = (uint8_t*)cmdlist +CMDLIST_ENTRY_SIZE;
107 if(!strncmp_P(buffer, itemstr, i)){
109 strcpy_P(ref, itemstr);
111 ref[stridentcnt_P(ref, itemstr)]='\0';
115 i = strcmp(buffer, ref);
121 int8_t cmd_interface(PGM_VOID_P cmd_desc){
122 uint16_t cli_buffer_size;
123 uint16_t cli_buffer_index;
125 uint8_t completion_failed=0;
128 uint16_t maxcmdlength = max_cmd_length(cmd_desc);
129 cli_buffer = calloc(1,cli_buffer_size=maxcmdlength+2);
139 if((exit_code=search_and_call(cli_buffer, maxcmdlength, cmd_desc))<=0){
143 memset(cli_buffer, 0, cli_buffer_size);
145 cli_putstr_P(PSTR("\r\n>"));
150 if(cli_buffer_index==0)
153 cli_buffer[cli_buffer_index] = '\0';
154 if(cli_echo && cli_tx){
159 if(completion_failed || cli_buffer_index==0){
161 cli_option_listing(cli_buffer, cmd_desc);
163 uint16_t old_idx = cli_buffer_index;
165 ~cli_completion(cli_buffer, maxcmdlength, cmd_desc);
166 cli_buffer_index = strlen(cli_buffer);
167 if(cli_echo && cli_tx){
168 while(old_idx<cli_buffer_index){
169 cli_tx(cli_buffer[old_idx++]);
176 if(cli_echo && cli_tx){
179 if(cli_buffer_index+1==cli_buffer_size){
180 cli_buffer = realloc(cli_buffer, cli_buffer_size+=CLI_BUFFER_BS);
184 memset(cli_buffer+cli_buffer_index+1, 0, CLI_BUFFER_BS);
186 cli_buffer[cli_buffer_index++] = c;