2 * Copyright Droids Corporation (2007)
3 * Olivier MATZ <zer0@droids-corp.org>
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.
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.
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
19 * Revision : $Id: rdline.h,v 1.1.2.6 2009-02-27 21:41:31 zer0 Exp $
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.
36 #include <rdline_config.h>
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"
60 /* default configuration */
61 #ifndef RDLINE_BUF_SIZE
62 #define RDLINE_BUF_SIZE 64
64 #ifndef RDLINE_PROMPT_SIZE
65 #define RDLINE_PROMPT_SIZE 16
67 #ifndef RDLINE_VT100_BUF_SIZE
68 #define RDLINE_VT100_BUF_SIZE 8
70 #ifndef RDLINE_HISTORY_BUF_SIZE
71 #define RDLINE_HISTORY_BUF_SIZE 128
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);
87 enum rdline_status status;
91 char left_buf[RDLINE_BUF_SIZE+2]; /* reserve 2 chars for the \n\0 */
92 char right_buf[RDLINE_BUF_SIZE];
94 char prompt[RDLINE_PROMPT_SIZE];
97 #ifdef CONFIG_MODULE_RDLINE_KILL_BUF
98 char kill_buf[RDLINE_BUF_SIZE];
102 #ifdef CONFIG_MODULE_RDLINE_HISTORY
104 struct cirbuf history;
105 char history_buf[RDLINE_HISTORY_BUF_SIZE];
106 int8_t history_cur_line;
109 /* callbacks and func pointers */
110 rdline_write_char_t *write_char;
111 rdline_validate_t *validate;
112 rdline_complete_t *complete;
119 * Init fields for a struct rdline. Call this only once at the beginning
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.
128 void rdline_init(struct rdline *rdl,
129 rdline_write_char_t *write_char,
130 rdline_validate_t *validate,
131 rdline_complete_t *complete);
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
139 void rdline_newline(struct rdline *rdl, const char *prompt);
142 * Call it and all received chars will be ignored.
143 * \param rdl A pointer to a struct rdline
145 void rdline_stop(struct rdline *rdl);
148 * Restart after a call to rdline_stop()
149 * \param rdl A pointer to a struct rdline
151 void rdline_restart(struct rdline *rdl);
154 * Redisplay the current buffer
155 * \param rdl A pointer to a struct rdline
157 void rdline_redisplay(struct rdline *rdl);
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).
167 * XXX error case when the buffer is full ?
169 * \param rdl A pointer to a struct rdline
170 * \param c The character to append
172 int8_t rdline_char_in(struct rdline * rdl, char c);
175 * Return the current buffer, terminated by '\0'.
176 * \param rdl A pointer to a struct rdline
178 const char *rdline_get_buffer(struct rdline *rdl);
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'
187 int8_t rdline_add_history(struct rdline *rdl, const char *buf);
190 * Clear current history
191 * \param rdl A pointer to a struct rdline
193 void rdline_clear_history(struct rdline *rdl);
196 * Get the i-th history item
198 char *rdline_get_history_item(struct rdline *rdl, uint8_t i);
200 #endif /* _RDLINE_H_ */