]> git.cryptolib.org Git - avr-crypto-lib.git/blob - test_src/cli.c
renaming to AVR-Crypto-Lib
[avr-crypto-lib.git] / test_src / cli.c
1 /* cli.c */
2 /*
3     This file is part of the 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 <stdint.h>
30 #include <string.h>
31 #include <avr/pgmspace.h>
32 #include "config.h"
33
34 int16_t findstring_d0(const char* str, const char* v){
35         uint8_t i=0;
36         while(*v){      
37                 if(!strcmp(str, v)){
38                         return i;
39                 }
40                 while(*v++) /* go to the next string */
41                 ;
42                 ++i;
43         }
44         return -1;
45 }
46  
47 int16_t findstring_d0_P(const char* str, PGM_P v){
48         uint8_t i=0;
49         while(pgm_read_byte(v)){        
50                 if(!strcmp_P(str, v)){
51                         return i;
52                 }
53                 while(pgm_read_byte(v++)) /* go to the next string */
54                 ;
55                 ++i;
56         }
57         return -1;
58
59
60 #ifdef CLI_AUTO_HELP
61 #include "uart.h"
62
63 void cli_auto_help_P(PGM_P dbzstr){
64         char c;
65         uart_putstr_P(PSTR("\r\n[auto help] available commands are:\r\n\t"));
66         do{
67                 while((c=pgm_read_byte(dbzstr++))!=0){
68                         uart_putc(c);
69                 }
70                 uart_putstr_P(PSTR("\r\n\t"));
71         }while((c=pgm_read_byte(dbzstr))!=0);
72         uart_putstr_P(PSTR("\r\n"));
73 }
74
75 #endif
76
77 int16_t execcommand_d0_P(const char* str, PGM_P v, void(*fpt[])(void) ){
78         int16_t i=0;
79         i=findstring_d0_P(str, v);
80         if(i!=-1){
81                 if(fpt[i])
82                         fpt[i]();
83                 return i;
84         }else{
85                 cli_auto_help_P(v);
86                 return -1;
87         }
88 }
89
90