1/*-
2 *   BSD LICENSE
3 *
4 *   Copyright(c) 2010-2014 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
34/*
35 * Copyright (c) 2009, Olivier MATZ <zer0@droids-corp.org>
36 * All rights reserved.
37 * Redistribution and use in source and binary forms, with or without
38 * modification, are permitted provided that the following conditions are met:
39 *
40 *     * Redistributions of source code must retain the above copyright
41 *       notice, this list of conditions and the following disclaimer.
42 *     * Redistributions in binary form must reproduce the above copyright
43 *       notice, this list of conditions and the following disclaimer in the
44 *       documentation and/or other materials provided with the distribution.
45 *     * Neither the name of the University of California, Berkeley nor the
46 *       names of its contributors may be used to endorse or promote products
47 *       derived from this software without specific prior written permission.
48 *
49 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND ANY
50 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
51 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
52 * DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND CONTRIBUTORS BE LIABLE FOR ANY
53 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
54 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
55 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
56 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
57 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
58 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
59 */
60
61#ifndef _RDLINE_H_
62#define _RDLINE_H_
63
64/**
65 * This file is a small equivalent to the GNU readline library, but it
66 * was originally designed for small systems, like Atmel AVR
67 * microcontrollers (8 bits). Indeed, we don't use any malloc that is
68 * sometimes not implemented (or just not recommended) on such
69 * systems.
70 *
71 * Obviously, it does not support as many things as the GNU readline,
72 * but at least it supports some interesting features like a kill
73 * buffer and a command history.
74 *
75 * It also have a feature that does not have the GNU readline (as far
76 * as I know): we can have several instances of it running at the same
77 * time, even on a monothread program, since it works with callbacks.
78 *
79 * The lib is designed for a client-side or a server-side use:
80 * - server-side: the server receives all data from a socket, including
81 *   control chars, like arrows, tabulations, ... The client is
82 *   very simple, it can be a telnet or a minicom through a serial line.
83 * - client-side: the client receives its data through its stdin for
84 *   instance.
85 */
86
87#include <stdio.h>
88#include <cmdline_cirbuf.h>
89#include <cmdline_vt100.h>
90
91#ifdef __cplusplus
92extern "C" {
93#endif
94
95/* configuration */
96#define RDLINE_BUF_SIZE 512
97#define RDLINE_PROMPT_SIZE  32
98#define RDLINE_VT100_BUF_SIZE  8
99#define RDLINE_HISTORY_BUF_SIZE BUFSIZ
100#define RDLINE_HISTORY_MAX_LINE 64
101
102enum rdline_status {
103	RDLINE_INIT,
104	RDLINE_RUNNING,
105	RDLINE_EXITED
106};
107
108struct rdline;
109
110typedef int (rdline_write_char_t)(struct rdline *rdl, char);
111typedef void (rdline_validate_t)(struct rdline *rdl,
112				 const char *buf, unsigned int size);
113typedef int (rdline_complete_t)(struct rdline *rdl, const char *buf,
114				char *dstbuf, unsigned int dstsize,
115				int *state);
116
117struct rdline {
118	enum rdline_status status;
119	/* rdline bufs */
120	struct cirbuf left;
121	struct cirbuf right;
122	char left_buf[RDLINE_BUF_SIZE+2]; /* reserve 2 chars for the \n\0 */
123	char right_buf[RDLINE_BUF_SIZE];
124
125	char prompt[RDLINE_PROMPT_SIZE];
126	unsigned int prompt_size;
127
128	char kill_buf[RDLINE_BUF_SIZE];
129	unsigned int kill_size;
130
131	/* history */
132	struct cirbuf history;
133	char history_buf[RDLINE_HISTORY_BUF_SIZE];
134	int history_cur_line;
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 */
158int 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 */
169void 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 */
175void 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 */
182void 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 */
188void rdline_restart(struct rdline *rdl);
189
190/**
191 * Redisplay the current buffer
192 * \param rdl A pointer to a struct rdline
193 */
194void rdline_redisplay(struct rdline *rdl);
195
196/**
197 * Reset the current buffer and setup for a new line.
198 *  \param rdl A pointer to a struct rdline
199 */
200void rdline_reset(struct rdline *rdl);
201
202
203/* return status for rdline_char_in() */
204#define RDLINE_RES_SUCCESS       0
205#define RDLINE_RES_VALIDATED     1
206#define RDLINE_RES_COMPLETE      2
207#define RDLINE_RES_NOT_RUNNING  -1
208#define RDLINE_RES_EOF          -2
209#define RDLINE_RES_EXITED       -3
210
211/**
212 * append a char to the readline buffer.
213 * Return RDLINE_RES_VALIDATE when the line has been validated.
214 * Return RDLINE_RES_COMPLETE when the user asked to complete the buffer.
215 * Return RDLINE_RES_NOT_RUNNING if it is not running.
216 * Return RDLINE_RES_EOF if EOF (ctrl-d on an empty line).
217 * Else return RDLINE_RES_SUCCESS.
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 */
223int 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 */
229const 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 */
238int 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 */
244void rdline_clear_history(struct rdline *rdl);
245
246/**
247 * Get the i-th history item
248 */
249char *rdline_get_history_item(struct rdline *rdl, unsigned int i);
250
251#ifdef __cplusplus
252}
253#endif
254
255#endif /* _RDLINE_H_ */
256