702a0ab89c55e73c6df8286802502b37f22e3d93
[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 #define vt100_bell         "\007"
92 #define vt100_bs           "\010"
93 #define vt100_bs_clear     "\010 \010"
94 #define vt100_tab          "\011"
95 #define vt100_crnl         "\012\015"
96 #define vt100_clear_right  "\033[0K"
97 #define vt100_clear_left   "\033[1K"
98 #define vt100_clear_down   "\033[0J"
99 #define vt100_clear_up     "\033[1J"
100 #define vt100_clear_line   "\033[2K"
101 #define vt100_clear_screen "\033[2J"
102 #define vt100_up_arr       "\033\133\101"
103 #define vt100_down_arr     "\033\133\102"
104 #define vt100_right_arr    "\033\133\103"
105 #define vt100_left_arr     "\033\133\104"
106 #define vt100_multi_right  "\033\133%uC"
107 #define vt100_multi_left   "\033\133%uD"
108 #define vt100_suppr        "\033\133\063\176"
109 #define vt100_home         "\033M\033E"
110 #define vt100_word_left    "\033\142"
111 #define vt100_word_right   "\033\146"
112
113 /* configuration */
114 #define RDLINE_BUF_SIZE 256
115 #define RDLINE_PROMPT_SIZE  32
116 #define RDLINE_VT100_BUF_SIZE  8
117 #define RDLINE_HISTORY_BUF_SIZE BUFSIZ
118 #define RDLINE_HISTORY_MAX_LINE 64
119
120 enum rdline_status {
121         RDLINE_INIT,
122         RDLINE_RUNNING
123 };
124
125 struct rdline;
126
127 typedef void (rdline_write_char_t)(struct rdline *rdl, char);
128 typedef void (rdline_validate_t)(struct rdline *rdl,
129                                  const char *buf, unsigned int size);
130 typedef int (rdline_complete_t)(struct rdline *rdl, const char *buf,
131                                 char *dstbuf, unsigned int dstsize,
132                                 int *state);
133
134 struct rdline {
135         enum rdline_status status;
136         /* rdline bufs */
137         struct cirbuf left;
138         struct cirbuf right;
139         char left_buf[RDLINE_BUF_SIZE+2]; /* reserve 2 chars for the \n\0 */
140         char right_buf[RDLINE_BUF_SIZE];
141
142         char prompt[RDLINE_PROMPT_SIZE];
143         unsigned int prompt_size;
144
145 #ifndef NO_RDLINE_KILL_BUF
146         char kill_buf[RDLINE_BUF_SIZE];
147         unsigned int kill_size;
148 #endif
149
150 #ifndef NO_RDLINE_HISTORY
151         /* history */
152         struct cirbuf history;
153         char history_buf[RDLINE_HISTORY_BUF_SIZE];
154         int history_cur_line;
155 #endif
156
157         /* callbacks and func pointers */
158         rdline_write_char_t *write_char;
159         rdline_validate_t *validate;
160         rdline_complete_t *complete;
161
162         /* vt100 parser */
163         struct cmdline_vt100 vt100;
164
165         /* opaque pointer */
166         void *opaque;
167 };
168
169 /**
170  * Init fields for a struct rdline. Call this only once at the beginning
171  * of your program.
172  * \param rdl A pointer to an uninitialized struct rdline
173  * \param write_char The function used by the function to write a character
174  * \param validate A pointer to the function to execute when the
175  *                 user validates the buffer.
176  * \param complete A pointer to the function to execute when the
177  *                 user completes the buffer.
178  */
179 void rdline_init(struct rdline *rdl,
180                  rdline_write_char_t *write_char,
181                  rdline_validate_t *validate,
182                  rdline_complete_t *complete);
183
184
185 /**
186  * Init the current buffer, and display a prompt.
187  * \param rdl A pointer to a struct rdline
188  * \param prompt A string containing the prompt
189  */
190 void rdline_newline(struct rdline *rdl, const char *prompt);
191
192 /**
193  * Call it and all received chars will be ignored.
194  * \param rdl A pointer to a struct rdline
195  */
196 void rdline_stop(struct rdline *rdl);
197
198 /**
199  * Restart after a call to rdline_stop()
200  * \param rdl A pointer to a struct rdline
201  */
202 void rdline_restart(struct rdline *rdl);
203
204 /**
205  * Redisplay the current buffer
206  * \param rdl A pointer to a struct rdline
207  */
208 void rdline_redisplay(struct rdline *rdl);
209
210
211 /**
212  * append a char to the readline buffer.
213  * Return 1 when the line has been validated.
214  * Return 2 when the user asked to complete the buffer.
215  * Return -1 if it is not running.
216  * Return -2 if EOF (ctrl-d on an empty line).
217  * Else return 0.
218  * XXX error case when the buffer is full ?
219  *
220  * \param rdl A pointer to a struct rdline
221  * \param c The character to append
222  */
223 int rdline_char_in(struct rdline *rdl, char c);
224
225 /**
226  * Return the current buffer, terminated by '\0'.
227  * \param rdl A pointer to a struct rdline
228  */
229 const char *rdline_get_buffer(struct rdline *rdl);
230
231
232 /**
233  * Add the buffer to history.
234  * return < 0 on error.
235  * \param rdl A pointer to a struct rdline
236  * \param buf A buffer that is terminated by '\0'
237  */
238 int rdline_add_history(struct rdline *rdl, const char *buf);
239
240 /**
241  * Clear current history
242  * \param rdl A pointer to a struct rdline
243  */
244 void rdline_clear_history(struct rdline *rdl);
245
246 /**
247  * Get the i-th history item
248  */
249 char *rdline_get_history_item(struct rdline *rdl, unsigned int i);
250
251 #endif /* _RDLINE_H_ */