2 * Copyright (c) 2009, Olivier MATZ <zer0@droids-corp.org>
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are met:
7 * * Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * * Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 * * Neither the name of the University of California, Berkeley nor the
13 * names of its contributors may be used to endorse or promote products
14 * derived from this software without specific prior written permission.
16 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND ANY
17 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19 * DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND CONTRIBUTORS BE LIABLE FOR ANY
20 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 * This file is a small equivalent to the GNU readline library, but it
33 * was originally designed for small systems, like Atmel AVR
34 * microcontrollers (8 bits). Indeed, we don't use any malloc that is
35 * sometimes not implemented (or just not recommended) on such
38 * Obviously, it does not support as many things as the GNU readline,
39 * but at least it supports some interresting features like a kill
40 * buffer and a command history.
42 * It also have a feature that does not have the GNU readline (as far
43 * as I know): we can have several instances of it running at the same
44 * time, even on a monothread program, since it works with callbacks.
46 * The lib is designed for a client-side or a server-side use:
47 * - server-side: the server receives all data from a socket, including
48 * control chars, like arrows, tabulations, ... The client is
49 * very simple, it can be a telnet or a minicom through a serial line.
50 * - client-side: the client receives its data through its stdin for
54 #include <cmdline_cirbuf.h>
55 #include <cmdline_vt100.h>
57 #define vt100_bell "\007"
58 #define vt100_bs "\010"
59 #define vt100_bs_clear "\010 \010"
60 #define vt100_tab "\011"
61 #define vt100_crnl "\012\015"
62 #define vt100_clear_right "\033[0K"
63 #define vt100_clear_left "\033[1K"
64 #define vt100_clear_down "\033[0J"
65 #define vt100_clear_up "\033[1J"
66 #define vt100_clear_line "\033[2K"
67 #define vt100_clear_screen "\033[2J"
68 #define vt100_up_arr "\033\133\101"
69 #define vt100_down_arr "\033\133\102"
70 #define vt100_right_arr "\033\133\103"
71 #define vt100_left_arr "\033\133\104"
72 #define vt100_multi_right "\033\133%uC"
73 #define vt100_multi_left "\033\133%uD"
74 #define vt100_suppr "\033\133\063\176"
75 #define vt100_home "\033M\033E"
76 #define vt100_word_left "\033\142"
77 #define vt100_word_right "\033\146"
80 #define RDLINE_BUF_SIZE 256
81 #define RDLINE_PROMPT_SIZE 32
82 #define RDLINE_VT100_BUF_SIZE 8
83 #define RDLINE_HISTORY_BUF_SIZE BUFSIZ
84 #define RDLINE_HISTORY_MAX_LINE 64
93 typedef void (rdline_write_char_t)(struct rdline *rdl, char);
94 typedef void (rdline_validate_t)(struct rdline *rdl,
95 const char *buf, unsigned int size);
96 typedef int (rdline_complete_t)(struct rdline *rdl, const char *buf,
97 char *dstbuf, unsigned int dstsize,
101 enum rdline_status status;
105 char left_buf[RDLINE_BUF_SIZE+2]; /* reserve 2 chars for the \n\0 */
106 char right_buf[RDLINE_BUF_SIZE];
108 char prompt[RDLINE_PROMPT_SIZE];
109 unsigned int prompt_size;
111 #ifndef NO_RDLINE_KILL_BUF
112 char kill_buf[RDLINE_BUF_SIZE];
113 unsigned int kill_size;
116 #ifndef NO_RDLINE_HISTORY
118 struct cirbuf history;
119 char history_buf[RDLINE_HISTORY_BUF_SIZE];
120 int history_cur_line;
123 /* callbacks and func pointers */
124 rdline_write_char_t *write_char;
125 rdline_validate_t *validate;
126 rdline_complete_t *complete;
129 struct cmdline_vt100 vt100;
136 * Init fields for a struct rdline. Call this only once at the beginning
138 * \param rdl A pointer to an uninitialized struct rdline
139 * \param write_char The function used by the function to write a character
140 * \param validate A pointer to the function to execute when the
141 * user validates the buffer.
142 * \param complete A pointer to the function to execute when the
143 * user completes the buffer.
145 void rdline_init(struct rdline *rdl,
146 rdline_write_char_t *write_char,
147 rdline_validate_t *validate,
148 rdline_complete_t *complete);
152 * Init the current buffer, and display a prompt.
153 * \param rdl A pointer to a struct rdline
154 * \param prompt A string containing the prompt
156 void rdline_newline(struct rdline *rdl, const char *prompt);
159 * Call it and all received chars will be ignored.
160 * \param rdl A pointer to a struct rdline
162 void rdline_stop(struct rdline *rdl);
165 * Restart after a call to rdline_stop()
166 * \param rdl A pointer to a struct rdline
168 void rdline_restart(struct rdline *rdl);
171 * Redisplay the current buffer
172 * \param rdl A pointer to a struct rdline
174 void rdline_redisplay(struct rdline *rdl);
178 * append a char to the readline buffer.
179 * Return 1 when the line has been validated.
180 * Return 2 when the user asked to complete the buffer.
181 * Return -1 if it is not running.
182 * Return -2 if EOF (ctrl-d on an empty line).
184 * XXX error case when the buffer is full ?
186 * \param rdl A pointer to a struct rdline
187 * \param c The character to append
189 int rdline_char_in(struct rdline *rdl, char c);
192 * Return the current buffer, terminated by '\0'.
193 * \param rdl A pointer to a struct rdline
195 const char *rdline_get_buffer(struct rdline *rdl);
199 * Add the buffer to history.
200 * return < 0 on error.
201 * \param rdl A pointer to a struct rdline
202 * \param buf A buffer that is terminated by '\0'
204 int rdline_add_history(struct rdline *rdl, const char *buf);
207 * Clear current history
208 * \param rdl A pointer to a struct rdline
210 void rdline_clear_history(struct rdline *rdl);
213 * Get the i-th history item
215 char *rdline_get_history_item(struct rdline *rdl, unsigned int i);
217 #endif /* _RDLINE_H_ */