tests: fix test programs using old uart API
[aversive.git] / modules / ihm / parse / test / main.c
1 /*
2  *  Copyright Droids Corporation (2007)
3  * 
4  *  This program is free software; you can redistribute it and/or modify
5  *  it under the terms of the GNU General Public License as published by
6  *  the Free Software Foundation; either version 2 of the License, or
7  *  (at your option) any later version.
8  *
9  *  This program is distributed in the hope that it will be useful,
10  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  *  GNU General Public License for more details.
13  *
14  *  You should have received a copy of the GNU General Public License
15  *  along with this program; if not, write to the Free Software
16  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17  *
18  *  Revision : $Id: main.c,v 1.1.2.6 2007-11-24 22:57:54 zer0 Exp $
19  *
20  */
21
22
23 #include <stdio.h>
24 #include <string.h>
25 #include <math.h>
26
27 #include <aversive/pgmspace.h>
28 #include <aversive/error.h>
29 #include <aversive/wait.h>
30
31 #include <rdline.h>
32 #include <parse.h>
33
34 struct rdline rdl;
35 char prompt[RDLINE_PROMPT_SIZE];
36 extern parse_pgm_ctx_t main_ctx[];
37 extern void cmd_event_parsed(void * parsed_result, void * data);
38
39 #include <stdlib.h>
40 #include <stdarg.h>
41 #include <inttypes.h>
42 #include <ctype.h>
43
44 #ifdef HOST_VERSION
45 #include <unistd.h>
46 #include <termios.h>
47
48 /* for rdline */
49 void
50 write_char(char c) {
51         write(1, &c, 1);
52 }
53
54 #else
55 #include <uart.h>
56
57 void
58 write_char(char c) {
59         uart_send(0, c);
60 }
61 #endif
62
63 void 
64 valid_buffer(const char * buf, uint8_t size) 
65 {
66         int8_t ret;
67         ret = parse(main_ctx, buf);
68         if (ret == PARSE_AMBIGUOUS)
69                 printf_P(PSTR("Ambiguous command\n"));
70         else if (ret == PARSE_NOMATCH)
71                 printf_P(PSTR("Command not found\n"));
72         else if (ret == PARSE_BAD_ARGS)
73                 printf_P(PSTR("Bad arguments\n"));
74 }
75
76 int8_t 
77 complete_buffer(const char * buf, char * dstbuf, uint8_t dstsize,
78                 int16_t * state)
79 {
80         return complete(main_ctx, buf, state, dstbuf, dstsize);
81 }
82
83
84 /*** main */
85
86 int main(void) 
87 {
88 #ifdef HOST_VERSION
89         struct termios oldterm, term;
90         int n;
91 #endif
92         int8_t ret;
93         char c;
94
95 #ifdef HOST_VERSION
96         tcgetattr(0, &oldterm);
97         memcpy(&term, &oldterm, sizeof(term));
98         term.c_lflag &= ~(ICANON | ECHO | ISIG);
99         tcsetattr(0, TCSANOW, &term);
100         setbuf(stdin, NULL);
101 #else
102         fdevopen(uart0_dev_send, uart0_dev_recv);
103         uart_init();
104         sei();
105 #endif
106
107         printf_P(PSTR("Start\n"));
108         wait_ms(500);
109
110         rdline_init(&rdl, write_char, valid_buffer, complete_buffer);
111         snprintf(prompt, sizeof(prompt), "main > ");    
112
113         rdline_newline(&rdl, prompt);
114
115         c = -1;
116         while (1) {
117 #ifdef HOST_VERSION
118                 n=read(0, &c, 1);
119                 if (n<=0)
120                         break;
121 #else
122                 c=uart0_recv();
123 #endif
124                 
125                 ret = rdline_char_in(&rdl, c);
126                 if (ret == -2) 
127                         break;
128
129                 if (ret != 2 && ret != 0) {
130                         rdline_add_history(&rdl, rdline_get_buffer(&rdl));
131                         rdline_newline(&rdl, prompt);
132                 }
133         }
134
135 #ifdef HOST_VERSION
136         tcsetattr(0, TCSANOW, &oldterm);
137 #endif
138         printf("\n");
139
140         return 0;
141 }
142
143