4 * Copyright(c) 2010-2012 Intel Corporation. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
17 * * Neither the name of Intel Corporation nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 * Copyright (c) 2009, Olivier MATZ <zer0@droids-corp.org>
37 * All rights reserved.
38 * Redistribution and use in source and binary forms, with or without
39 * modification, are permitted provided that the following conditions are met:
41 * * Redistributions of source code must retain the above copyright
42 * notice, this list of conditions and the following disclaimer.
43 * * Redistributions in binary form must reproduce the above copyright
44 * notice, this list of conditions and the following disclaimer in the
45 * documentation and/or other materials provided with the distribution.
46 * * Neither the name of the University of California, Berkeley nor the
47 * names of its contributors may be used to endorse or promote products
48 * derived from this software without specific prior written permission.
50 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND ANY
51 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
52 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
53 * DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND CONTRIBUTORS BE LIABLE FOR ANY
54 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
55 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
56 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
57 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
58 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
59 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
66 * This file is a small equivalent to the GNU readline library, but it
67 * was originally designed for small systems, like Atmel AVR
68 * microcontrollers (8 bits). Indeed, we don't use any malloc that is
69 * sometimes not implemented (or just not recommended) on such
72 * Obviously, it does not support as many things as the GNU readline,
73 * but at least it supports some interesting features like a kill
74 * buffer and a command history.
76 * It also have a feature that does not have the GNU readline (as far
77 * as I know): we can have several instances of it running at the same
78 * time, even on a monothread program, since it works with callbacks.
80 * The lib is designed for a client-side or a server-side use:
81 * - server-side: the server receives all data from a socket, including
82 * control chars, like arrows, tabulations, ... The client is
83 * very simple, it can be a telnet or a minicom through a serial line.
84 * - client-side: the client receives its data through its stdin for
88 #include <cmdline_cirbuf.h>
89 #include <cmdline_vt100.h>
96 #define RDLINE_BUF_SIZE 256
97 #define RDLINE_PROMPT_SIZE 32
98 #define RDLINE_VT100_BUF_SIZE 8
99 #define RDLINE_HISTORY_BUF_SIZE BUFSIZ
100 #define RDLINE_HISTORY_MAX_LINE 64
110 typedef int (rdline_write_char_t)(struct rdline *rdl, char);
111 typedef void (rdline_validate_t)(struct rdline *rdl,
112 const char *buf, unsigned int size);
113 typedef int (rdline_complete_t)(struct rdline *rdl, const char *buf,
114 char *dstbuf, unsigned int dstsize,
118 enum rdline_status status;
122 char left_buf[RDLINE_BUF_SIZE+2]; /* reserve 2 chars for the \n\0 */
123 char right_buf[RDLINE_BUF_SIZE];
125 char prompt[RDLINE_PROMPT_SIZE];
126 unsigned int prompt_size;
128 #ifndef NO_RDLINE_KILL_BUF
129 char kill_buf[RDLINE_BUF_SIZE];
130 unsigned int kill_size;
133 #ifndef NO_RDLINE_HISTORY
135 struct cirbuf history;
136 char history_buf[RDLINE_HISTORY_BUF_SIZE];
137 int history_cur_line;
140 /* callbacks and func pointers */
141 rdline_write_char_t *write_char;
142 rdline_validate_t *validate;
143 rdline_complete_t *complete;
146 struct cmdline_vt100 vt100;
153 * Init fields for a struct rdline. Call this only once at the beginning
155 * \param rdl A pointer to an uninitialized struct rdline
156 * \param write_char The function used by the function to write a character
157 * \param validate A pointer to the function to execute when the
158 * user validates the buffer.
159 * \param complete A pointer to the function to execute when the
160 * user completes the buffer.
162 void rdline_init(struct rdline *rdl,
163 rdline_write_char_t *write_char,
164 rdline_validate_t *validate,
165 rdline_complete_t *complete);
169 * Init the current buffer, and display a prompt.
170 * \param rdl A pointer to a struct rdline
171 * \param prompt A string containing the prompt
173 void rdline_newline(struct rdline *rdl, const char *prompt);
176 * Call it and all received chars will be ignored.
177 * \param rdl A pointer to a struct rdline
179 void rdline_stop(struct rdline *rdl);
182 * Same than rdline_stop() except that next calls to rdline_char_in()
183 * will return RDLINE_RES_EXITED.
184 * \param rdl A pointer to a struct rdline
186 void rdline_quit(struct rdline *rdl);
189 * Restart after a call to rdline_stop() or rdline_quit()
190 * \param rdl A pointer to a struct rdline
192 void rdline_restart(struct rdline *rdl);
195 * Redisplay the current buffer
196 * \param rdl A pointer to a struct rdline
198 void rdline_redisplay(struct rdline *rdl);
201 * Reset the current buffer and setup for a new line.
202 * \param rdl A pointer to a struct rdline
204 void rdline_reset(struct rdline *rdl);
207 /* return status for rdline_char_in() */
208 #define RDLINE_RES_SUCCESS 0
209 #define RDLINE_RES_VALIDATED 1
210 #define RDLINE_RES_COMPLETE 2
211 #define RDLINE_RES_NOT_RUNNING -1
212 #define RDLINE_RES_EOF -2
213 #define RDLINE_RES_EXITED -3
216 * append a char to the readline buffer.
217 * Return RDLINE_RES_VALIDATE when the line has been validated.
218 * Return RDLINE_RES_COMPLETE when the user asked to complete the buffer.
219 * Return RDLINE_RES_NOT_RUNNING if it is not running.
220 * Return RDLINE_RES_EOF if EOF (ctrl-d on an empty line).
221 * Else return RDLINE_RES_SUCCESS.
222 * XXX error case when the buffer is full ?
224 * \param rdl A pointer to a struct rdline
225 * \param c The character to append
227 int rdline_char_in(struct rdline *rdl, char c);
230 * Return the current buffer, terminated by '\0'.
231 * \param rdl A pointer to a struct rdline
233 const char *rdline_get_buffer(struct rdline *rdl);
237 * Add the buffer to history.
238 * return < 0 on error.
239 * \param rdl A pointer to a struct rdline
240 * \param buf A buffer that is terminated by '\0'
242 int rdline_add_history(struct rdline *rdl, const char *buf);
245 * Clear current history
246 * \param rdl A pointer to a struct rdline
248 void rdline_clear_history(struct rdline *rdl);
251 * Get the i-th history item
253 char *rdline_get_history_item(struct rdline *rdl, unsigned int i);
259 #endif /* _RDLINE_H_ */