e99abb77c4633368e512020f588bdc1db62a62f2
[aversive.git] / modules / ihm / rdline / rdline.h
1 /*  
2  *  Copyright Droids Corporation (2007)
3  *  Olivier MATZ <zer0@droids-corp.org>
4  * 
5  *  This program is free software; you can redistribute it and/or modify
6  *  it under the terms of the GNU General Public License as published by
7  *  the Free Software Foundation; either version 2 of the License, or
8  *  (at your option) any later version.
9  *
10  *  This program is distributed in the hope that it will be useful,
11  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  *  GNU General Public License for more details.
14  *
15  *  You should have received a copy of the GNU General Public License
16  *  along with this program; if not, write to the Free Software
17  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18  *
19  *  Revision : $Id: rdline.h,v 1.1.2.6 2009-02-27 21:41:31 zer0 Exp $
20  *
21  *
22  */
23
24 #ifndef _RDLINE_H_
25 #define _RDLINE_H_
26
27 /**
28  * This library is a small equivalent to the GNU readline library, but
29  * it is designed for small systems, like Atmel AVR microcontrollers
30  * (8 bits). Indeed, we don't use any malloc that is sometimes not
31  * implemented on such systems.
32  */
33
34 #include <cirbuf.h>
35 #include <vt100.h>
36 #include <rdline_config.h>
37
38 #define vt100_bell         "\007"
39 #define vt100_bs           "\010"
40 #define vt100_bs_clear     "\010 \010"
41 #define vt100_tab          "\011"
42 #define vt100_crnl         "\012\015"
43 #define vt100_clear_right  "\033[0K"
44 #define vt100_clear_left   "\033[1K"
45 #define vt100_clear_down   "\033[0J"
46 #define vt100_clear_up     "\033[1J"
47 #define vt100_clear_line   "\033[2K"
48 #define vt100_clear_screen "\033[2J"
49 #define vt100_up_arr       "\033\133\101"
50 #define vt100_down_arr     "\033\133\102"
51 #define vt100_right_arr    "\033\133\103"
52 #define vt100_left_arr     "\033\133\104"
53 #define vt100_multi_right  "\033\133%uC"
54 #define vt100_multi_left   "\033\133%uD"
55 #define vt100_suppr        "\033\133\063\176"
56 #define vt100_home         "\033M\033E"
57 #define vt100_word_left    "\033\142"
58 #define vt100_word_right   "\033\146"
59
60 /* default configuration */
61 #ifndef RDLINE_BUF_SIZE
62 #define RDLINE_BUF_SIZE 64
63 #endif
64 #ifndef RDLINE_PROMPT_SIZE
65 #define RDLINE_PROMPT_SIZE  16
66 #endif
67 #ifndef RDLINE_VT100_BUF_SIZE
68 #define RDLINE_VT100_BUF_SIZE  8
69 #endif
70 #ifndef RDLINE_HISTORY_BUF_SIZE
71 #define RDLINE_HISTORY_BUF_SIZE 128
72 #endif
73
74 enum rdline_status {
75         RDLINE_STOPPED,
76         RDLINE_RUNNING,
77 };
78
79 struct rdline;
80
81 typedef void (rdline_write_char_t)(char);
82 typedef void (rdline_validate_t)(const char *buf, uint8_t size);
83 typedef int8_t (rdline_complete_t)(const char *buf, char *dstbuf,
84                                 uint8_t dstsize, int16_t *state);
85
86 struct rdline {
87         enum rdline_status status;
88         /* rdline bufs */
89         struct cirbuf left;
90         struct cirbuf right;
91         char left_buf[RDLINE_BUF_SIZE+2]; /* reserve 2 chars for the \n\0 */
92         char right_buf[RDLINE_BUF_SIZE];
93
94         char prompt[RDLINE_PROMPT_SIZE];
95         uint8_t prompt_size;
96
97 #ifdef CONFIG_MODULE_RDLINE_KILL_BUF
98         char kill_buf[RDLINE_BUF_SIZE];
99         uint8_t kill_size;
100 #endif
101
102 #ifdef CONFIG_MODULE_RDLINE_HISTORY
103         /* history */
104         struct cirbuf history;
105         char history_buf[RDLINE_HISTORY_BUF_SIZE];
106         int8_t history_cur_line;
107 #endif
108
109         /* callbacks and func pointers */
110         rdline_write_char_t *write_char;
111         rdline_validate_t *validate;
112         rdline_complete_t *complete;
113
114         /* vt100 parser */
115         struct vt100 vt100;
116 };
117
118 /**
119  * Init fields for a struct rdline. Call this only once at the beginning
120  * of your program.
121  * \param rdl A pointer to an uninitialized struct rdline
122  * \param write_char The function used by the function to write a character
123  * \param validate A pointer to the function to execute when the 
124  *                 user validates the buffer.
125  * \param complete A pointer to the function to execute when the 
126  *                 user completes the buffer.
127  */
128 void rdline_init(struct rdline *rdl, 
129                  rdline_write_char_t *write_char,
130                  rdline_validate_t *validate,
131                  rdline_complete_t *complete);
132
133
134 /**
135  * Init the current buffer, and display a prompt.
136  * \param rdl A pointer to a struct rdline
137  * \param prompt A string containing the prompt
138  */
139 void rdline_newline(struct rdline *rdl, const char *prompt);
140
141 /**
142  * Call it and all received chars will be ignored.
143  * \param rdl A pointer to a struct rdline
144  */
145 void rdline_stop(struct rdline *rdl);
146
147 /**
148  * Restart after a call to rdline_stop()
149  * \param rdl A pointer to a struct rdline
150  */
151 void rdline_restart(struct rdline *rdl);
152
153 /**
154  * Redisplay the current buffer
155  * \param rdl A pointer to a struct rdline
156  */
157 void rdline_redisplay(struct rdline *rdl);
158
159
160 /**
161  * append a char to the readline buffer. 
162  * Return 1 when the line has been validated.
163  * Return 2 when the user asked to complete the buffer.
164  * Return -1 if it is not running.
165  * Return -2 if EOF (ctrl-d on an empty line).
166  * Else return 0.
167  * XXX error case when the buffer is full ?
168  *
169  * \param rdl A pointer to a struct rdline
170  * \param c The character to append
171  */
172 int8_t rdline_char_in(struct rdline * rdl, char c);
173
174 /**
175  * Return the current buffer, terminated by '\0'.
176  * \param rdl A pointer to a struct rdline
177  */
178 const char *rdline_get_buffer(struct rdline *rdl);
179
180
181 /**
182  * Add the buffer to history.
183  * return < 0 on error.
184  * \param rdl A pointer to a struct rdline
185  * \param buf A buffer that is terminated by '\0'
186  */
187 int8_t rdline_add_history(struct rdline *rdl, const char *buf);
188
189 /**
190  * Clear current history
191  * \param rdl A pointer to a struct rdline
192  */
193 void rdline_clear_history(struct rdline *rdl);
194
195 /**
196  * Get the i-th history item
197  */
198 char *rdline_get_history_item(struct rdline *rdl, uint8_t i);
199
200 #endif /* _RDLINE_H_ */