1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2010-2014 Intel Corporation.
3 * Copyright (c) 2009, Olivier MATZ <zer0@droids-corp.org>
11 * This file is a small equivalent to the GNU readline library, but it
12 * was originally designed for small systems, like Atmel AVR
13 * microcontrollers (8 bits). It only uses malloc() on object creation.
15 * Obviously, it does not support as many things as the GNU readline,
16 * but at least it supports some interesting features like a kill
17 * buffer and a command history.
19 * It also have a feature that does not have the GNU readline (as far
20 * as I know): we can have several instances of it running at the same
21 * time, even on a monothread program, since it works with callbacks.
23 * The lib is designed for a client-side or a server-side use:
24 * - server-side: the server receives all data from a socket, including
25 * control chars, like arrows, tabulations, ... The client is
26 * very simple, it can be a telnet or a minicom through a serial line.
27 * - client-side: the client receives its data through its stdin for
32 #include <rte_compat.h>
33 #include <cmdline_cirbuf.h>
34 #include <cmdline_vt100.h>
42 typedef int (rdline_write_char_t)(struct rdline *rdl, char);
43 typedef void (rdline_validate_t)(struct rdline *rdl,
44 const char *buf, unsigned int size);
45 typedef int (rdline_complete_t)(struct rdline *rdl, const char *buf,
46 char *dstbuf, unsigned int dstsize,
50 * Allocate and initialize a new rdline instance.
52 * \param write_char The function used by the function to write a character
53 * \param validate A pointer to the function to execute when the
54 * user validates the buffer.
55 * \param complete A pointer to the function to execute when the
56 * user completes the buffer.
57 * \param opaque User data for use in the callbacks.
59 * \return New rdline object on success, NULL on failure.
62 struct rdline *rdline_new(rdline_write_char_t *write_char,
63 rdline_validate_t *validate,
64 rdline_complete_t *complete,
68 * Free an rdline instance.
70 * \param rdl A pointer to an initialized struct rdline.
71 * If NULL, this function is a no-op.
74 void rdline_free(struct rdline *rdl);
77 * Init the current buffer, and display a prompt.
78 * \param rdl A pointer to a struct rdline
79 * \param prompt A string containing the prompt
81 void rdline_newline(struct rdline *rdl, const char *prompt);
84 * Call it and all received chars will be ignored.
85 * \param rdl A pointer to a struct rdline
87 void rdline_stop(struct rdline *rdl);
90 * Same than rdline_stop() except that next calls to rdline_char_in()
91 * will return RDLINE_RES_EXITED.
92 * \param rdl A pointer to a struct rdline
94 void rdline_quit(struct rdline *rdl);
97 * Restart after a call to rdline_stop() or rdline_quit()
98 * \param rdl A pointer to a struct rdline
100 void rdline_restart(struct rdline *rdl);
103 * Redisplay the current buffer
104 * \param rdl A pointer to a struct rdline
106 void rdline_redisplay(struct rdline *rdl);
109 * Reset the current buffer and setup for a new line.
110 * \param rdl A pointer to a struct rdline
112 void rdline_reset(struct rdline *rdl);
115 /* return status for rdline_char_in() */
116 #define RDLINE_RES_SUCCESS 0
117 #define RDLINE_RES_VALIDATED 1
118 #define RDLINE_RES_COMPLETE 2
119 #define RDLINE_RES_NOT_RUNNING -1
120 #define RDLINE_RES_EOF -2
121 #define RDLINE_RES_EXITED -3
124 * append a char to the readline buffer.
125 * Return RDLINE_RES_VALIDATE when the line has been validated.
126 * Return RDLINE_RES_COMPLETE when the user asked to complete the buffer.
127 * Return RDLINE_RES_NOT_RUNNING if it is not running.
128 * Return RDLINE_RES_EOF if EOF (ctrl-d on an empty line).
129 * Else return RDLINE_RES_SUCCESS.
130 * XXX error case when the buffer is full ?
132 * \param rdl A pointer to a struct rdline
133 * \param c The character to append
135 int rdline_char_in(struct rdline *rdl, char c);
138 * Return the current buffer, terminated by '\0'.
139 * \param rdl A pointer to a struct rdline
141 const char *rdline_get_buffer(struct rdline *rdl);
145 * Add the buffer to history.
146 * return < 0 on error.
147 * \param rdl A pointer to a struct rdline
148 * \param buf A buffer that is terminated by '\0'
150 int rdline_add_history(struct rdline *rdl, const char *buf);
153 * Clear current history
154 * \param rdl A pointer to a struct rdline
156 void rdline_clear_history(struct rdline *rdl);
159 * Get the i-th history item
161 char *rdline_get_history_item(struct rdline *rdl, unsigned int i);
164 * Get maximum history buffer size.
167 size_t rdline_get_history_buffer_size(struct rdline *rdl);
170 * Get the opaque pointer supplied on struct rdline creation.
173 void *rdline_get_opaque(struct rdline *rdl);
179 #endif /* _RDLINE_H_ */