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);
47 void cli_auto_help(uint16_t maxcmdlength, PGM_VOID_P cmdlist);
49 typedef void(*str_fpt)(char*);
51 #define CLI_BACKSPACE 8
52 #define CLI_TABULATOR 9
54 uint8_t cli_completion(char* buffer, uint16_t maxcmdlength, PGM_VOID_P cmdlist){
56 char ref[maxcmdlength+1];
59 /* check if we are behind the first word */
61 if(!isgraph(buffer[i++]))
65 itemstr = (char*)pgm_read_word(cmdlist);
68 cmdlist = (uint8_t*)cmdlist +CMDLIST_ENTRY_SIZE;
69 if(!strncmp_P(buffer, itemstr, i)){
71 strcpy_P(ref, itemstr);
73 ref[stridentcnt_P(ref, itemstr)]='\0';
77 i = strcmp(buffer, ref);
83 int8_t cmd_interface(PGM_VOID_P cmd_desc){
84 uint16_t cli_buffer_size;
85 uint16_t cli_buffer_index;
87 uint8_t completion_failed=0;
90 uint16_t maxcmdlength = max_cmd_length(cmd_desc);
91 cli_buffer = calloc(1,cli_buffer_size=maxcmdlength+2);
101 if((exit_code = search_and_call(cli_buffer, maxcmdlength, cmd_desc)) <=0 ){
105 /* cli_putstr(cli_buffer); */
107 memset(cli_buffer, 0, cli_buffer_size);
109 /* cli_putstr_P(PSTR(" DONE\r\n>")); */
110 cli_putstr_P(PSTR("\r\n>"));
115 if(cli_buffer_index==0)
118 cli_buffer[cli_buffer_index] = '\0';
119 if(cli_echo && cli_tx){
124 if(completion_failed || cli_buffer_index==0){
126 cli_option_listing(cli_buffer, cmd_desc);
128 uint16_t old_idx = cli_buffer_index;
130 ~cli_completion(cli_buffer, maxcmdlength, cmd_desc);
131 cli_buffer_index = strlen(cli_buffer);
132 if(cli_echo && cli_tx){
133 while(old_idx<cli_buffer_index){
134 cli_tx(cli_buffer[old_idx++]);
141 if(cli_echo && cli_tx){
144 if(cli_buffer_index+1==cli_buffer_size){
145 cli_buffer = realloc(cli_buffer, cli_buffer_size+=CLI_BUFFER_BS);
149 memset(cli_buffer+cli_buffer_index+1, 0, CLI_BUFFER_BS);
151 cli_buffer[cli_buffer_index++] = c;