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.
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"
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
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);
79 enum rdline_status status;
83 char left_buf[RDLINE_BUF_SIZE+2]; /* reserve 2 chars for the \n\0 */
84 char right_buf[RDLINE_BUF_SIZE];
86 char prompt[RDLINE_PROMPT_SIZE];
89 #ifdef CONFIG_MODULE_RDLINE_KILL_BUF
90 char kill_buf[RDLINE_BUF_SIZE];
94 #ifdef CONFIG_MODULE_RDLINE_HISTORY
96 struct cirbuf history;
97 char history_buf[RDLINE_HISTORY_BUF_SIZE];
98 int8_t history_cur_line;
101 /* callbacks and func pointers */
102 rdline_write_char_t *write_char;
103 rdline_validate_t *validate;
104 rdline_complete_t *complete;
111 * Init fields for a struct rdline. Call this only once at the beginning
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.
120 void rdline_init(struct rdline *rdl,
121 rdline_write_char_t *write_char,
122 rdline_validate_t *validate,
123 rdline_complete_t *complete);
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
131 void rdline_newline(struct rdline *rdl, const char *prompt);
134 * Call it and all received chars will be ignored.
135 * \param rdl A pointer to a struct rdline
137 void rdline_stop(struct rdline *rdl);
140 * Restart after a call to rdline_stop()
141 * \param rdl A pointer to a struct rdline
143 void rdline_restart(struct rdline *rdl);
146 * Redisplay the current buffer
147 * \param rdl A pointer to a struct rdline
149 void rdline_redisplay(struct rdline *rdl);
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).
159 * XXX error case when the buffer is full ?
161 * \param rdl A pointer to a struct rdline
162 * \param c The character to append
164 int8_t rdline_char_in(struct rdline * rdl, char c);
167 * Return the current buffer, terminated by '\0'.
168 * \param rdl A pointer to a struct rdline
170 const char *rdline_get_buffer(struct rdline *rdl);
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'
179 int8_t rdline_add_history(struct rdline *rdl, const char *buf);
182 * Clear current history
183 * \param rdl A pointer to a struct rdline
185 void rdline_clear_history(struct rdline *rdl);
188 * Get the i-th history item
190 char *rdline_get_history_item(struct rdline *rdl, uint8_t i);
192 #endif /* _RDLINE_H_ */