87a17890b1679a5480d7ebdf26bb8e8e48b1acb8
[libcmdline.git] / src / lib / cmdline_rdline.h
1 /*-
2  * Copyright (c) <2010>, Intel Corporation
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * - Redistributions of source code must retain the above copyright
10  *   notice, this list of conditions and the following disclaimer.
11  *
12  * - Redistributions in binary form must reproduce the above copyright
13  *   notice, this list of conditions and the following disclaimer in
14  *   the documentation and/or other materials provided with the
15  *   distribution.
16  *
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.
20  *
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
24  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
27  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
28  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
30  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
32  * OF THE POSSIBILITY OF SUCH DAMAGE.
33  */
34
35 /*
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:
40  *
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.
49  *
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.
60  */
61
62 #ifndef _RDLINE_H_
63 #define _RDLINE_H_
64
65 /**
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
70  * systems.
71  *
72  * Obviously, it does not support as many things as the GNU readline,
73  * but at least it supports some interresting features like a kill
74  * buffer and a command history.
75  *
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.
79  *
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
85  *   instance.
86  */
87
88 #include <cmdline_cirbuf.h>
89 #include <cmdline_vt100.h>
90
91 /* configuration */
92 #define RDLINE_BUF_SIZE 256
93 #define RDLINE_PROMPT_SIZE  32
94 #define RDLINE_VT100_BUF_SIZE  8
95 #define RDLINE_HISTORY_BUF_SIZE BUFSIZ
96 #define RDLINE_HISTORY_MAX_LINE 64
97
98 enum rdline_status {
99         RDLINE_INIT,
100         RDLINE_RUNNING,
101         RDLINE_EXITED
102 };
103
104 struct rdline;
105
106 typedef int (rdline_write_char_t)(struct rdline *rdl, char);
107 typedef void (rdline_validate_t)(struct rdline *rdl,
108                                  const char *buf, unsigned int size);
109 typedef int (rdline_complete_t)(struct rdline *rdl, const char *buf,
110                                 char *dstbuf, unsigned int dstsize,
111                                 int *state);
112
113 struct rdline {
114         enum rdline_status status;
115         /* rdline bufs */
116         struct cirbuf left;
117         struct cirbuf right;
118         char left_buf[RDLINE_BUF_SIZE+2]; /* reserve 2 chars for the \n\0 */
119         char right_buf[RDLINE_BUF_SIZE];
120
121         char prompt[RDLINE_PROMPT_SIZE];
122         unsigned int prompt_size;
123
124 #ifndef NO_RDLINE_KILL_BUF
125         char kill_buf[RDLINE_BUF_SIZE];
126         unsigned int kill_size;
127 #endif
128
129 #ifndef NO_RDLINE_HISTORY
130         /* history */
131         struct cirbuf history;
132         char history_buf[RDLINE_HISTORY_BUF_SIZE];
133         int history_cur_line;
134 #endif
135
136         /* callbacks and func pointers */
137         rdline_write_char_t *write_char;
138         rdline_validate_t *validate;
139         rdline_complete_t *complete;
140
141         /* vt100 parser */
142         struct cmdline_vt100 vt100;
143
144         /* opaque pointer */
145         void *opaque;
146 };
147
148 /**
149  * Init fields for a struct rdline. Call this only once at the beginning
150  * of your program.
151  * \param rdl A pointer to an uninitialized struct rdline
152  * \param write_char The function used by the function to write a character
153  * \param validate A pointer to the function to execute when the
154  *                 user validates the buffer.
155  * \param complete A pointer to the function to execute when the
156  *                 user completes the buffer.
157  */
158 void rdline_init(struct rdline *rdl,
159                  rdline_write_char_t *write_char,
160                  rdline_validate_t *validate,
161                  rdline_complete_t *complete);
162
163
164 /**
165  * Init the current buffer, and display a prompt.
166  * \param rdl A pointer to a struct rdline
167  * \param prompt A string containing the prompt
168  */
169 void rdline_newline(struct rdline *rdl, const char *prompt);
170
171 /**
172  * Call it and all received chars will be ignored.
173  * \param rdl A pointer to a struct rdline
174  */
175 void rdline_stop(struct rdline *rdl);
176
177 /**
178  * Same than rdline_stop() except that next calls to rdline_char_in()
179  * will return RDLINE_RES_EXITED.
180  * \param rdl A pointer to a struct rdline
181  */
182 void rdline_quit(struct rdline *rdl);
183
184 /**
185  * Restart after a call to rdline_stop() or rdline_quit()
186  * \param rdl A pointer to a struct rdline
187  */
188 void rdline_restart(struct rdline *rdl);
189
190 /**
191  * Redisplay the current buffer
192  * \param rdl A pointer to a struct rdline
193  */
194 void rdline_redisplay(struct rdline *rdl);
195
196
197 /* return status for rdline_char_in() */
198 #define RDLINE_RES_SUCCESS       0
199 #define RDLINE_RES_VALIDATED     1
200 #define RDLINE_RES_COMPLETE      2
201 #define RDLINE_RES_NOT_RUNNING  -1
202 #define RDLINE_RES_EOF          -2
203 #define RDLINE_RES_EXITED       -3
204
205 /**
206  * append a char to the readline buffer.
207  * Return RDLINE_RES_VALIDATE when the line has been validated.
208  * Return RDLINE_RES_COMPLETE when the user asked to complete the buffer.
209  * Return RDLINE_RES_NOT_RUNNING if it is not running.
210  * Return RDLINE_RES_EOF if EOF (ctrl-d on an empty line).
211  * Else return RDLINE_RES_SUCCESS.
212  * XXX error case when the buffer is full ?
213  *
214  * \param rdl A pointer to a struct rdline
215  * \param c The character to append
216  */
217 int rdline_char_in(struct rdline *rdl, char c);
218
219 /**
220  * Return the current buffer, terminated by '\0'.
221  * \param rdl A pointer to a struct rdline
222  */
223 const char *rdline_get_buffer(struct rdline *rdl);
224
225
226 /**
227  * Add the buffer to history.
228  * return < 0 on error.
229  * \param rdl A pointer to a struct rdline
230  * \param buf A buffer that is terminated by '\0'
231  */
232 int rdline_add_history(struct rdline *rdl, const char *buf);
233
234 /**
235  * Clear current history
236  * \param rdl A pointer to a struct rdline
237  */
238 void rdline_clear_history(struct rdline *rdl);
239
240 /**
241  * Get the i-th history item
242  */
243 char *rdline_get_history_item(struct rdline *rdl, unsigned int i);
244
245 #endif /* _RDLINE_H_ */