]> git.cryptolib.org Git - avr-crypto-lib.git/blob - test_src/cli-stub.c
fixing E-Mail-Address & Copyright
[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) 2006-2015 Daniel Otte (bg@nerilex.org)
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:  bg@nerilex.org
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 void     cli_auto_help(uint16_t maxcmdlength, PGM_VOID_P cmdlist);
48
49 typedef void(*str_fpt)(char*);
50 #define CLI_ENTER     13
51 #define CLI_BACKSPACE  8
52 #define CLI_TABULATOR  9
53
54 uint8_t cli_completion(char *buffer, uint16_t maxcmdlength, PGM_VOID_P cmdlist){
55         uint8_t i=0;
56         char ref[maxcmdlength+1];
57         char *itemstr;
58         ref[0]='\0';
59         /* check if we are behind the first word */
60         while(buffer[i]){
61                 if(!isgraph(buffer[i++]))
62                         return 0;
63         }
64         for(;;){
65                 itemstr = (char*)pgm_read_word(cmdlist);
66                 if(itemstr==NULL)
67                         break;
68                 cmdlist = (uint8_t*)cmdlist +CMDLIST_ENTRY_SIZE;
69                 if(!strncmp_P(buffer, itemstr, i)){
70                         if(!ref[0]){
71                                 strcpy_P(ref, itemstr);
72                         }else{
73                                 ref[stridentcnt_P(ref, itemstr)]='\0';
74                         }
75                 }
76         }
77         i = strcmp(buffer, ref);
78         if(i)
79                 strcpy(buffer, ref);
80         return ~i;
81 }
82
83 int8_t cmd_interface(PGM_VOID_P cmd_desc){
84         uint16_t cli_buffer_size;
85         uint16_t cli_buffer_index;
86         int8_t exit_code;
87         uint8_t completion_failed=0;
88         char *cli_buffer;
89         char c;
90         uint16_t maxcmdlength = max_cmd_length(cmd_desc);
91         cli_buffer = calloc(1,cli_buffer_size=maxcmdlength+2);
92         cli_buffer_index=0;
93         if(!cli_rx)
94                 return -1;
95         if(cli_tx)
96                 cli_tx('>');
97         for(;;){
98                 c = cli_rx();
99                 switch (c){
100                 case CLI_ENTER:
101                         if((exit_code = search_and_call(cli_buffer, maxcmdlength, cmd_desc)) <=0 ){
102                                 free(cli_buffer);
103                                 return exit_code;
104                         }
105                         /* cli_putstr(cli_buffer); */
106
107                         memset(cli_buffer, 0, cli_buffer_size);
108                         cli_buffer_index=0;
109                         /* cli_putstr_P(PSTR(" DONE\r\n>")); */
110                         cli_putstr_P(PSTR("\r\n>"));
111                         completion_failed=0;
112                         break;
113                 case CLI_BACKSPACE:
114                         completion_failed=0;
115                         if(cli_buffer_index==0)
116                                 break;
117                         cli_buffer_index--;
118                         cli_buffer[cli_buffer_index] = '\0';
119                         if(cli_echo && cli_tx){
120                                 cli_tx(c);
121                         }
122                         break;
123                 case CLI_TABULATOR:
124                         if(completion_failed || cli_buffer_index==0){
125                                 if(cli_tx)
126                                         cli_option_listing(cli_buffer, cmd_desc);
127                         } else {
128                                 uint16_t old_idx = cli_buffer_index;
129                                 completion_failed =
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++]);
135                                         }
136                                 }
137                         }
138                         break;
139                 default:
140                         completion_failed=0;
141                         if(cli_echo && cli_tx){
142                                 cli_tx(c);
143                         }
144                         if(cli_buffer_index+1==cli_buffer_size){
145                                 cli_buffer = realloc(cli_buffer, cli_buffer_size+=CLI_BUFFER_BS);
146                                 if(!cli_buffer){
147                                         return -2;
148                                 }
149                                 memset(cli_buffer+cli_buffer_index+1, 0, CLI_BUFFER_BS);
150                         }
151                         cli_buffer[cli_buffer_index++] = c;
152                 }
153         }
154 }