first public release
[dpdk.git] / lib / librte_cmdline / cmdline_rdline.h
1 /*-
2  *   BSD LICENSE
3  * 
4  *   Copyright(c) 2010-2012 Intel Corporation. All rights reserved.
5  *   All rights reserved.
6  * 
7  *   Redistribution and use in source and binary forms, with or without 
8  *   modification, are permitted provided that the following conditions 
9  *   are met:
10  * 
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 
16  *       distribution.
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 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.
32  * 
33  *  version: DPDK.L.1.2.3-3
34  */
35
36 /*
37  * Copyright (c) 2009, Olivier MATZ <zer0@droids-corp.org>
38  * All rights reserved.
39  * Redistribution and use in source and binary forms, with or without
40  * modification, are permitted provided that the following conditions are met:
41  *
42  *     * Redistributions of source code must retain the above copyright
43  *       notice, this list of conditions and the following disclaimer.
44  *     * Redistributions in binary form must reproduce the above copyright
45  *       notice, this list of conditions and the following disclaimer in the
46  *       documentation and/or other materials provided with the distribution.
47  *     * Neither the name of the University of California, Berkeley nor the
48  *       names of its contributors may be used to endorse or promote products
49  *       derived from this software without specific prior written permission.
50  *
51  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND ANY
52  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
53  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
54  * DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND CONTRIBUTORS BE LIABLE FOR ANY
55  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
56  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
57  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
58  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
59  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
60  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
61  */
62
63 #ifndef _RDLINE_H_
64 #define _RDLINE_H_
65
66 /**
67  * This file is a small equivalent to the GNU readline library, but it
68  * was originally designed for small systems, like Atmel AVR
69  * microcontrollers (8 bits). Indeed, we don't use any malloc that is
70  * sometimes not implemented (or just not recommended) on such
71  * systems.
72  *
73  * Obviously, it does not support as many things as the GNU readline,
74  * but at least it supports some interesting features like a kill
75  * buffer and a command history.
76  *
77  * It also have a feature that does not have the GNU readline (as far
78  * as I know): we can have several instances of it running at the same
79  * time, even on a monothread program, since it works with callbacks.
80  *
81  * The lib is designed for a client-side or a server-side use:
82  * - server-side: the server receives all data from a socket, including
83  *   control chars, like arrows, tabulations, ... The client is
84  *   very simple, it can be a telnet or a minicom through a serial line.
85  * - client-side: the client receives its data through its stdin for
86  *   instance.
87  */
88
89 #include <cmdline_cirbuf.h>
90 #include <cmdline_vt100.h>
91
92 #ifdef __cplusplus
93 extern "C" {
94 #endif
95
96 /* configuration */
97 #define RDLINE_BUF_SIZE 256
98 #define RDLINE_PROMPT_SIZE  32
99 #define RDLINE_VT100_BUF_SIZE  8
100 #define RDLINE_HISTORY_BUF_SIZE BUFSIZ
101 #define RDLINE_HISTORY_MAX_LINE 64
102
103 enum rdline_status {
104         RDLINE_INIT,
105         RDLINE_RUNNING,
106         RDLINE_EXITED
107 };
108
109 struct rdline;
110
111 typedef int (rdline_write_char_t)(struct rdline *rdl, char);
112 typedef void (rdline_validate_t)(struct rdline *rdl,
113                                  const char *buf, unsigned int size);
114 typedef int (rdline_complete_t)(struct rdline *rdl, const char *buf,
115                                 char *dstbuf, unsigned int dstsize,
116                                 int *state);
117
118 struct rdline {
119         enum rdline_status status;
120         /* rdline bufs */
121         struct cirbuf left;
122         struct cirbuf right;
123         char left_buf[RDLINE_BUF_SIZE+2]; /* reserve 2 chars for the \n\0 */
124         char right_buf[RDLINE_BUF_SIZE];
125
126         char prompt[RDLINE_PROMPT_SIZE];
127         unsigned int prompt_size;
128
129 #ifndef NO_RDLINE_KILL_BUF
130         char kill_buf[RDLINE_BUF_SIZE];
131         unsigned int kill_size;
132 #endif
133
134 #ifndef NO_RDLINE_HISTORY
135         /* history */
136         struct cirbuf history;
137         char history_buf[RDLINE_HISTORY_BUF_SIZE];
138         int history_cur_line;
139 #endif
140
141         /* callbacks and func pointers */
142         rdline_write_char_t *write_char;
143         rdline_validate_t *validate;
144         rdline_complete_t *complete;
145
146         /* vt100 parser */
147         struct cmdline_vt100 vt100;
148
149         /* opaque pointer */
150         void *opaque;
151 };
152
153 /**
154  * Init fields for a struct rdline. Call this only once at the beginning
155  * of your program.
156  * \param rdl A pointer to an uninitialized struct rdline
157  * \param write_char The function used by the function to write a character
158  * \param validate A pointer to the function to execute when the
159  *                 user validates the buffer.
160  * \param complete A pointer to the function to execute when the
161  *                 user completes the buffer.
162  */
163 void rdline_init(struct rdline *rdl,
164                  rdline_write_char_t *write_char,
165                  rdline_validate_t *validate,
166                  rdline_complete_t *complete);
167
168
169 /**
170  * Init the current buffer, and display a prompt.
171  * \param rdl A pointer to a struct rdline
172  * \param prompt A string containing the prompt
173  */
174 void rdline_newline(struct rdline *rdl, const char *prompt);
175
176 /**
177  * Call it and all received chars will be ignored.
178  * \param rdl A pointer to a struct rdline
179  */
180 void rdline_stop(struct rdline *rdl);
181
182 /**
183  * Same than rdline_stop() except that next calls to rdline_char_in()
184  * will return RDLINE_RES_EXITED.
185  * \param rdl A pointer to a struct rdline
186  */
187 void rdline_quit(struct rdline *rdl);
188
189 /**
190  * Restart after a call to rdline_stop() or rdline_quit()
191  * \param rdl A pointer to a struct rdline
192  */
193 void rdline_restart(struct rdline *rdl);
194
195 /**
196  * Redisplay the current buffer
197  * \param rdl A pointer to a struct rdline
198  */
199 void rdline_redisplay(struct rdline *rdl);
200
201 /**
202  * Reset the current buffer and setup for a new line.
203  *  \param rdl A pointer to a struct rdline
204  */
205 void rdline_reset(struct rdline *rdl);
206
207
208 /* return status for rdline_char_in() */
209 #define RDLINE_RES_SUCCESS       0
210 #define RDLINE_RES_VALIDATED     1
211 #define RDLINE_RES_COMPLETE      2
212 #define RDLINE_RES_NOT_RUNNING  -1
213 #define RDLINE_RES_EOF          -2
214 #define RDLINE_RES_EXITED       -3
215
216 /**
217  * append a char to the readline buffer.
218  * Return RDLINE_RES_VALIDATE when the line has been validated.
219  * Return RDLINE_RES_COMPLETE when the user asked to complete the buffer.
220  * Return RDLINE_RES_NOT_RUNNING if it is not running.
221  * Return RDLINE_RES_EOF if EOF (ctrl-d on an empty line).
222  * Else return RDLINE_RES_SUCCESS.
223  * XXX error case when the buffer is full ?
224  *
225  * \param rdl A pointer to a struct rdline
226  * \param c The character to append
227  */
228 int rdline_char_in(struct rdline *rdl, char c);
229
230 /**
231  * Return the current buffer, terminated by '\0'.
232  * \param rdl A pointer to a struct rdline
233  */
234 const char *rdline_get_buffer(struct rdline *rdl);
235
236
237 /**
238  * Add the buffer to history.
239  * return < 0 on error.
240  * \param rdl A pointer to a struct rdline
241  * \param buf A buffer that is terminated by '\0'
242  */
243 int rdline_add_history(struct rdline *rdl, const char *buf);
244
245 /**
246  * Clear current history
247  * \param rdl A pointer to a struct rdline
248  */
249 void rdline_clear_history(struct rdline *rdl);
250
251 /**
252  * Get the i-th history item
253  */
254 char *rdline_get_history_item(struct rdline *rdl, unsigned int i);
255
256 #ifdef __cplusplus
257 }
258 #endif
259
260 #endif /* _RDLINE_H_ */