]> git.cryptolib.org Git - avr-crypto-lib.git/blob - test_src/cli-stub.c
backporting uart_i and cli
[avr-crypto-lib.git] / test_src / cli-stub.c
1 /* cli.c */
2 /*
3     This file is part of the AVR-Crypto-Lib.
4     Copyright (C) 2008  Daniel Otte (daniel.otte@rub.de)
5
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.
10
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.
15
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/>.
18 */
19 /**
20  * 
21  * author: Daniel Otte
22  * email:  daniel.otte@rub.de
23  * license: GPLv3 or later
24  * 
25  * components to help implementing simple command based interaction
26  * 
27  **/
28  
29 #include <stdlib.h> 
30 #include <stdint.h>
31 #include <ctype.h>
32 #include <string.h>
33 #include <avr/pgmspace.h>
34 #include "string-extras.h"
35 #include "cli.h"
36 #include "config.h"
37 #include "hexdigit_tab.h"
38
39 cli_rx_fpt cli_rx = NULL;
40 cli_tx_fpt cli_tx = NULL;
41 uint8_t cli_echo=1;
42
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         
48
49 void cli_auto_help(uint16_t maxcmdlength, PGM_VOID_P cmdlist);/*
50 {
51         cmdlist_entry_t item;
52         uint16_t i;
53         if(!cli_tx)
54                 return;
55         
56         cli_putstr_P(PSTR("\r\n[auto help] available commands:\r\n"
57                           " <command> - <params> - <address>\r\n"));
58         for(;;){
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){
64                         return;
65                 }
66                 cli_tx(' ');
67                 cli_putstr_P(item.cmd_name);
68                 i=maxcmdlength-strlen_P(item.cmd_name);
69                 while(i--)
70                         cli_tx(' ');
71                 cli_putstr_P(PSTR(" - "));
72                 if(item.cmd_param_str==NULL){
73                         cli_putstr_P(PSTR("none \t- 0x"));
74                 } else {
75                         if(item.cmd_param_str==(void*)1){
76                                 cli_putstr_P(PSTR("yes  \t- 0x"));
77                         } else {
78                                 cli_putstr_P(item.cmd_param_str);
79                                 cli_putstr_P(PSTR(" \t- 0x"));
80                         }
81                 }
82                 cli_hexdump_rev(&item.cmd_function, 2); 
83                 cli_putstr_P(PSTR("\r\n"));
84         }
85 }
86 */
87 typedef void(*str_fpt)(char*);
88 #define CLI_ENTER     13
89 #define CLI_BACKSPACE  8
90 #define CLI_TABULATOR  9
91
92 uint8_t cli_completion(char* buffer, uint16_t maxcmdlength, PGM_VOID_P cmdlist){
93         uint8_t i=0;
94         char ref[maxcmdlength+1];
95         char* itemstr;
96         ref[0]='\0';
97         /* check if we are behind the first word */
98         while(buffer[i]){
99                 if(!isgraph(buffer[i++]))
100                         return 0;
101         }
102         for(;;){
103                 itemstr = (char*)pgm_read_word(cmdlist);
104                 if(itemstr==NULL)
105                         break;
106                 cmdlist = (uint8_t*)cmdlist +CMDLIST_ENTRY_SIZE;
107                 if(!strncmp_P(buffer, itemstr, i)){
108                         if(!ref[0]){
109                                 strcpy_P(ref, itemstr);
110                         }else{
111                                 ref[stridentcnt_P(ref, itemstr)]='\0';
112                         }
113                 }
114         }
115         i = strcmp(buffer, ref);
116         if(i)
117                 strcpy(buffer, ref);
118         return ~i;
119 }
120
121 int8_t cmd_interface(PGM_VOID_P cmd_desc){
122         uint16_t cli_buffer_size;
123         uint16_t cli_buffer_index;
124         int8_t exit_code;
125         uint8_t completion_failed=0;
126         char* cli_buffer;
127         char c;
128         uint16_t maxcmdlength = max_cmd_length(cmd_desc);
129         cli_buffer = calloc(1,cli_buffer_size=maxcmdlength+2);
130         cli_buffer_index=0;
131         if(!cli_rx)
132                 return -1;
133         if(cli_tx)
134                 cli_tx('>');
135         for(;;){
136                 c = cli_rx();
137                 switch (c){
138                 case CLI_ENTER:
139                         if((exit_code=search_and_call(cli_buffer, maxcmdlength, cmd_desc))<=0){
140                                 free(cli_buffer);
141                                 return exit_code;
142                         }
143                         memset(cli_buffer, 0, cli_buffer_size);
144                         cli_buffer_index=0;
145                         cli_putstr_P(PSTR("\r\n>"));
146                         completion_failed=0;
147                         break;
148                 case CLI_BACKSPACE:
149                         completion_failed=0;
150                         if(cli_buffer_index==0)
151                                 break;
152                         cli_buffer_index--;
153                         cli_buffer[cli_buffer_index] = '\0';
154                         if(cli_echo && cli_tx){
155                                 cli_tx(c);
156                         }
157                         break;
158                 case CLI_TABULATOR:
159                         if(completion_failed || cli_buffer_index==0){
160                                 if(cli_tx)
161                                         cli_option_listing(cli_buffer, cmd_desc);
162                         } else {
163                                 uint16_t old_idx = cli_buffer_index;
164                                 completion_failed = 
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++]);
170                                         }
171                                 }
172                         }
173                         break;
174                 default:
175                         completion_failed=0;
176                         if(cli_echo && cli_tx){
177                                 cli_tx(c);
178                         }
179                         if(cli_buffer_index+1==cli_buffer_size){
180                                 cli_buffer = realloc(cli_buffer, cli_buffer_size+=CLI_BUFFER_BS);
181                                 if(!cli_buffer){
182                                         return -2;
183                                 }
184                                 memset(cli_buffer+cli_buffer_index+1, 0, CLI_BUFFER_BS);
185                         }
186                         cli_buffer[cli_buffer_index++] = c;
187                 }
188         }
189 }