at query is not NULL when receiving xmit status
[protos/xbee-avr.git] / 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
37 #define vt100_bell         "\007"
38 #define vt100_bs           "\010"
39 #define vt100_bs_clear     "\010 \010"
40 #define vt100_tab          "\011"
41 #define vt100_crnl         "\012\015"
42 #define vt100_clear_right  "\033[0K"
43 #define vt100_clear_left   "\033[1K"
44 #define vt100_clear_down   "\033[0J"
45 #define vt100_clear_up     "\033[1J"
46 #define vt100_clear_line   "\033[2K"
47 #define vt100_clear_screen "\033[2J"
48 #define vt100_up_arr       "\033\133\101"
49 #define vt100_down_arr     "\033\133\102"
50 #define vt100_right_arr    "\033\133\103"
51 #define vt100_left_arr     "\033\133\104"
52 #define vt100_multi_right  "\033\133%uC"
53 #define vt100_multi_left   "\033\133%uD"
54 #define vt100_suppr        "\033\133\063\176"
55 #define vt100_home         "\033M\033E"
56 #define vt100_word_left    "\033\142"
57 #define vt100_word_right   "\033\146"
58
59 /* configuration */
60 #define RDLINE_BUF_SIZE 64
61 #define RDLINE_PROMPT_SIZE  16
62 #define RDLINE_VT100_BUF_SIZE  8
63 #define RDLINE_HISTORY_BUF_SIZE 128
64 #define RDLINE_HISTORY_MAX_LINE 64
65
66 enum rdline_status {
67         RDLINE_STOPPED,
68         RDLINE_RUNNING,
69 };
70
71 struct rdline;
72
73 typedef void (rdline_write_char_t)(char);
74 typedef void (rdline_validate_t)(const char *buf, uint8_t size);
75 typedef int8_t (rdline_complete_t)(const char *buf, char *dstbuf,
76                                 uint8_t dstsize, int16_t *state);
77
78 struct rdline {
79         enum rdline_status status;
80         /* rdline bufs */
81         struct cirbuf left;
82         struct cirbuf right;
83         char left_buf[RDLINE_BUF_SIZE+2]; /* reserve 2 chars for the \n\0 */
84         char right_buf[RDLINE_BUF_SIZE];
85
86         char prompt[RDLINE_PROMPT_SIZE];
87         uint8_t prompt_size;
88
89 #ifdef CONFIG_MODULE_RDLINE_KILL_BUF
90         char kill_buf[RDLINE_BUF_SIZE];
91         uint8_t kill_size;
92 #endif
93
94 #ifdef CONFIG_MODULE_RDLINE_HISTORY
95         /* history */
96         struct cirbuf history;
97         char history_buf[RDLINE_HISTORY_BUF_SIZE];
98         int8_t history_cur_line;
99 #endif
100
101         /* callbacks and func pointers */
102         rdline_write_char_t *write_char;
103         rdline_validate_t *validate;
104         rdline_complete_t *complete;
105
106         /* vt100 parser */
107         struct vt100 vt100;
108 };
109
110 /**
111  * Init fields for a struct rdline. Call this only once at the beginning
112  * of your program.
113  * \param rdl A pointer to an uninitialized struct rdline
114  * \param write_char The function used by the function to write a character
115  * \param validate A pointer to the function to execute when the 
116  *                 user validates the buffer.
117  * \param complete A pointer to the function to execute when the 
118  *                 user completes the buffer.
119  */
120 void rdline_init(struct rdline *rdl, 
121                  rdline_write_char_t *write_char,
122                  rdline_validate_t *validate,
123                  rdline_complete_t *complete);
124
125
126 /**
127  * Init the current buffer, and display a prompt.
128  * \param rdl A pointer to a struct rdline
129  * \param prompt A string containing the prompt
130  */
131 void rdline_newline(struct rdline *rdl, const char *prompt);
132
133 /**
134  * Call it and all received chars will be ignored.
135  * \param rdl A pointer to a struct rdline
136  */
137 void rdline_stop(struct rdline *rdl);
138
139 /**
140  * Restart after a call to rdline_stop()
141  * \param rdl A pointer to a struct rdline
142  */
143 void rdline_restart(struct rdline *rdl);
144
145 /**
146  * Redisplay the current buffer
147  * \param rdl A pointer to a struct rdline
148  */
149 void rdline_redisplay(struct rdline *rdl);
150
151
152 /**
153  * append a char to the readline buffer. 
154  * Return 1 when the line has been validated.
155  * Return 2 when the user asked to complete the buffer.
156  * Return -1 if it is not running.
157  * Return -2 if EOF (ctrl-d on an empty line).
158  * Else return 0.
159  * XXX error case when the buffer is full ?
160  *
161  * \param rdl A pointer to a struct rdline
162  * \param c The character to append
163  */
164 int8_t rdline_char_in(struct rdline * rdl, char c);
165
166 /**
167  * Return the current buffer, terminated by '\0'.
168  * \param rdl A pointer to a struct rdline
169  */
170 const char *rdline_get_buffer(struct rdline *rdl);
171
172
173 /**
174  * Add the buffer to history.
175  * return < 0 on error.
176  * \param rdl A pointer to a struct rdline
177  * \param buf A buffer that is terminated by '\0'
178  */
179 int8_t rdline_add_history(struct rdline *rdl, const char *buf);
180
181 /**
182  * Clear current history
183  * \param rdl A pointer to a struct rdline
184  */
185 void rdline_clear_history(struct rdline *rdl);
186
187 /**
188  * Get the i-th history item
189  */
190 char *rdline_get_history_item(struct rdline *rdl, uint8_t i);
191
192 #endif /* _RDLINE_H_ */