2 * Copyright (c) 2009-2015, 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.
29 * Copyright (c) <2010>, Intel Corporation
30 * All rights reserved.
32 * Redistribution and use in source and binary forms, with or without
33 * modification, are permitted provided that the following conditions
36 * - Redistributions of source code must retain the above copyright
37 * notice, this list of conditions and the following disclaimer.
39 * - Redistributions in binary form must reproduce the above copyright
40 * notice, this list of conditions and the following disclaimer in
41 * the documentation and/or other materials provided with the
44 * - Neither the name of Intel Corporation nor the names of its
45 * contributors may be used to endorse or promote products derived
46 * from this software without specific prior written permission.
48 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
49 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
50 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
51 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
52 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
53 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
54 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
55 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
56 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
57 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
58 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
59 * OF THE POSSIBILITY OF SUCH DAMAGE.
66 * This file is a small equivalent to the GNU readline library, it was
67 * originally designed for very small systems (an 8-bits Atmel AVR
68 * microcontroller), but the library can now run on many emmbedded
69 * systems with or without OS.
71 * Obviously, it does not support as many things as the GNU readline,
72 * but at least it supports some interresting features like a kill
73 * buffer and a command history.
75 * It also have a feature that does not have the GNU readline (as far
76 * as I know): it is possible to have several instances of readline
77 * running at the same time, even on a monothread program, since it
78 * works with callbacks.
81 #include <sys/types.h>
84 #include <ucg_cirbuf.h>
85 #include <ucg_cmd_vt100.h>
88 #define UCG_RDLINE_BUF_SIZE 32
89 #define UCG_RDLINE_PROMPT_SIZE 16
90 #define UCG_RDLINE_VT100_BUF_SIZE 8
91 #define UCG_RDLINE_HISTORY_BUF_SIZE 64
92 #define UCG_RDLINE_MAX_LINES 23 /* pager */
94 enum ucg_rdline_status {
103 * type of callback given to ucg_rdline_help() to display the content of
104 * the help. The first argument is the rdline pointer. The other args
105 * are buffer and size.
107 typedef int (ucg_rdline_printf_t)(struct ucg_rdline *rdl,
108 const char *fmt, ...);
111 * type of callback invoked when a command is parsed. "rdl" is a pointer
112 * to the ucg_rdline structure, "line" is a pointer to the current line
113 * string ('\0' terminated).
115 typedef void (ucg_rdline_validate_t)(struct ucg_rdline *rdl,
119 * type of callback invoked when a completion is requested. "rdl" is a
120 * pointer to the ucg_rdline structure, "line" is a pointer to the
121 * current line string ('\0' terminated). The characters to append
122 * are written to dstbuf.
123 * Return 0 on success: dstbuf contains the characters to append to
124 * the current line ('\0' terminated)
125 * Else return a negative value if no completion is performed.
127 typedef int (ucg_rdline_complete_t)(struct ucg_rdline *rdl, const char *line,
128 char *dstbuf, unsigned int dstsize);
131 * callback invoked when a the help is requested. "rdl" is a pointer to
132 * the ucg_rdline structure, "line" is a pointer to the current line string
135 typedef void (ucg_rdline_help_t)(struct ucg_rdline *rdl, const char *line);
137 typedef void (ucg_rdline_pager_cb_t)(struct ucg_rdline *, void *);
140 enum ucg_rdline_status status;
145 struct ucg_cirbuf left;
146 struct ucg_cirbuf right;
147 char left_buf[UCG_RDLINE_BUF_SIZE+1]; /* reserve 1 char for the \0 */
148 char right_buf[UCG_RDLINE_BUF_SIZE];
150 char prompt[UCG_RDLINE_PROMPT_SIZE];
152 #ifndef UCG_CMD_NO_RDLINE_KILL_BUF
153 char kill_buf[UCG_RDLINE_BUF_SIZE];
154 unsigned int kill_size;
157 #ifndef UCG_CMD_NO_RDLINE_HISTORY
159 struct ucg_cirbuf history;
160 char history_buf[UCG_RDLINE_HISTORY_BUF_SIZE];
161 int history_cur_line;
164 /* callbacks and func pointers */
165 ucg_rdline_validate_t *validate;
166 ucg_rdline_complete_t *complete;
167 ucg_rdline_help_t *help;
170 struct ucg_cmd_vt100 vt100;
175 #ifndef UCG_CMD_NO_PAGER
176 char *pager_buf; /* buffer used to store paged data */
177 int pager_len; /* total len of buffer */
178 int pager_off; /* offset of next data */
179 int pager_lines; /* number of lines displayed */
180 ucg_rdline_pager_cb_t *pager_cb; /* callback once paging is finished */
181 void *pager_arg; /* argument of callback */
182 int pager_ret; /* saved return value */
187 * Init fields for a struct ucg_rdline.
189 * @param rdl A pointer to an uninitialized struct ucg_rdline
191 * Input file descriptor
193 * Output file descriptor
195 * A pointer to the function to execute when the user validates the
198 * A pointer to the function to execute when the user completes the
201 * A pointer to the function to execute when the user ask for
204 void ucg_rdline_init(struct ucg_rdline *rdl,
205 FILE *f_in, FILE *f_out,
206 ucg_rdline_validate_t *validate,
207 ucg_rdline_complete_t *complete,
208 ucg_rdline_help_t *help);
212 * Init the current buffer, and display a prompt.
214 * Also set the rdline status to "running", overriding previous
215 * ucg_rdline_stop() or ucg_rdline_quit().
218 * A pointer to an initialized struct ucg_rdline
220 * A string containing the prompt, or NULL to keep existing one
222 void ucg_rdline_newline(struct ucg_rdline *rdl, const char *prompt);
225 * Ignore all subsequent received chars.
228 * A pointer to a struct ucg_rdline
230 void ucg_rdline_stop(struct ucg_rdline *rdl);
233 * Exit from running rdline loop
235 * Same than ucg_rdline_stop() except that next calls to ucg_rdline_char_in()
236 * will return UCG_RDLINE_RES_EXITED. Hence, any running rdline() function is
240 * A pointer to a struct ucg_rdline
242 void ucg_rdline_quit(struct ucg_rdline *rdl);
245 * Restart after a call to ucg_rdline_stop() or ucg_rdline_quit()
248 * A pointer to a struct ucg_rdline
250 void ucg_rdline_restart(struct ucg_rdline *rdl);
253 * Redisplay the current buffer
256 * A pointer to a struct ucg_rdline
258 void ucg_rdline_redisplay(struct ucg_rdline *rdl);
260 /* return status for ucg_rdline_char_in() */
261 #define UCG_RDLINE_RES_SUCCESS 0
262 #define UCG_RDLINE_RES_VALIDATED 1
263 #define UCG_RDLINE_RES_COMPLETED 2
264 #define UCG_RDLINE_RES_NOT_RUNNING -1
265 #define UCG_RDLINE_RES_EOF -2
266 #define UCG_RDLINE_RES_EXITED -3
267 #define UCG_RDLINE_RES_CANNOT_COMPLETE -3
270 * Append a char to the readline buffer.
273 * A pointer to a struct ucg_rdline
275 * The character to append
277 * - UCG_RDLINE_RES_VALIDATED when the line has been validated.
278 * - UCG_RDLINE_RES_NOT_RUNNING if it is not running.
279 * - UCG_RDLINE_RES_EOF if EOF (ctrl-d on an empty line).
280 * - UCG_RDLINE_RES_EXITED if user called ucg_rdline_quit()
281 * - Else return UCG_RDLINE_RES_SUCCESS.
283 int ucg_rdline_char_in(struct ucg_rdline *rdl, char c);
285 #define UCG_RDLINE_F_IGNORE_EOF 0x0001 /**< ignore eof when polling */
288 * Read (and edit) a line
291 * A pointer to a struct ucg_rdline
295 * Any flags from UCG_RDLINE_F_*
296 * - UCG_RDLINE_F_IGNORE_EOF: on eof, clear error and continue polling
298 * - UCG_RDLINE_RES_VALIDATED when the line has been validated.
299 * - UCG_RDLINE_RES_NOT_RUNNING if it is not running.
300 * - UCG_RDLINE_RES_EOF if EOF (ctrl-d on an empty line).
301 * - UCG_RDLINE_RES_EXITED if user called ucg_rdline_quit()
303 int ucg_rdline(struct ucg_rdline *rdl, const char *prompt, unsigned flags);
306 * write a buffer on rdline file descriptor
309 * The rdline descriptor
311 * Pointer to the buffer
313 * Number of bytes to write
315 * On success, the number of bytes written is returned (zero
316 * indicates nothing was written). On error, -1 is returned, and
317 * errno is set appropriately
319 ssize_t ucg_rdline_write(struct ucg_rdline *rdl, void *buf, size_t count);
322 * write on rdline file descriptor according to a format string
325 * The rdline descriptor
329 * On success, return the number of characters printed (not including
330 * the trailing '\0'). On error, a negative value is returned.
332 int ucg_rdline_printf(struct ucg_rdline *rdl, const char *fmt, ...);
335 * write on rdline file descriptor according to a format string
338 * The rdline descriptor
342 * Variable argument list
344 * Upon successful return, these functions return the number of
345 * characters printed (not including the trailing '\0' used to end
346 * output to strings). On error, a negative value is returned.
348 int ucg_rdline_vprintf(struct ucg_rdline *rdl, const char *fmt, va_list ap);
351 * Return the current buffer, terminated by '\0'.
354 * A pointer to a struct ucg_rdline
358 const char *ucg_rdline_get_buffer(struct ucg_rdline *rdl);
361 * Add the buffer to history.
364 * A pointer to a struct ucg_rdline
366 * A buffer that is terminated by '\0'
369 * - negative on error
371 int ucg_rdline_add_history(struct ucg_rdline *rdl, const char *buf);
374 * Clear current history
377 * A pointer to a struct ucg_rdline
379 void ucg_rdline_clear_history(struct ucg_rdline *rdl);
382 * Get the i-th history item
385 * A pointer to a struct ucg_rdline
387 * The index of the history item
389 * The i-th string of history, or NULL on error.
391 const char *ucg_rdline_get_history_item(struct ucg_rdline *rdl, unsigned int i);
393 #ifndef UCG_CMD_NO_PAGER
395 * Write data asynchronously (using pager if needed)
397 * If there is enough place to print data on the current page, it is
398 * printed synchronously. Else, a temporary buffer is allocated and
399 * the data is stored in it. When the main rdline is called again, the
400 * pager is flushed before parsing any other commands.
403 * The rdline descriptor
407 * Length of buffer to be sent
409 * On success, the number of bytes written is returned (zero
410 * indicates nothing was written). On error, -1 is returned, and
411 * errno is set appropriately
413 ssize_t ucg_rdline_pager_write(struct ucg_rdline *rdl, void *buf, size_t len);
416 * Print data asynchronously (using pager if needed)
418 * If there is enough place to print data on the current page, it is
419 * printed synchronously. Else, a temporary buffer is allocated and
420 * the data is stored in it. When the main rdline is called again, the
421 * pager is flushed before parsing any other commands.
424 * The rdline descriptor
428 * Upon successful return, these functions return the number of
429 * characters printed (not including the trailing '\0' used to end
430 * output to strings). On error, a negative value is returned.
432 int ucg_rdline_pager_printf(struct ucg_rdline *rdl, const char *fmt, ...);
435 * Set the callback for the pager
437 * If there is some data in the pager to be printed, set a callback
438 * function that will be called when all the data will be printed. If
439 * the pager is empty, don't do anything and return -1.
441 * The rdline descriptor
443 * - 0 if there is some data in the pager buffer and the callback
445 * - -1 if there is no data in pager buffer (in this case the callback
448 int ucg_rdline_pager_set_cb(struct ucg_rdline *rdl,
449 ucg_rdline_pager_cb_t *cb, void *arg);
452 #endif /* UCG_CMD_RDLINE_H_ */