cmdline (merge-intel): fix whitespaces
[libcmdline.git] / src / lib / cmdline_rdline.h
1 /*
2  * Copyright (c) 2009, Olivier MATZ <zer0@droids-corp.org>
3  * All rights reserved.
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are met:
6  *
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.
15  *
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.
26  */
27
28 #ifndef _RDLINE_H_
29 #define _RDLINE_H_
30
31 /**
32  * This file is a small equivalent to the GNU readline library, but it
33  * was originally designed for small systems, like Atmel AVR
34  * microcontrollers (8 bits). Indeed, we don't use any malloc that is
35  * sometimes not implemented (or just not recommended) on such
36  * systems.
37  *
38  * Obviously, it does not support as many things as the GNU readline,
39  * but at least it supports some interresting features like a kill
40  * buffer and a command history.
41  *
42  * It also have a feature that does not have the GNU readline (as far
43  * as I know): we can have several instances of it running at the same
44  * time, even on a monothread program, since it works with callbacks.
45  *
46  * The lib is designed for a client-side or a server-side use:
47  * - server-side: the server receives all data from a socket, including
48  *   control chars, like arrows, tabulations, ... The client is
49  *   very simple, it can be a telnet or a minicom through a serial line.
50  * - client-side: the client receives its data through its stdin for
51  *   instance.
52  */
53
54 #include <cmdline_cirbuf.h>
55 #include <cmdline_vt100.h>
56
57 #define vt100_bell         "\007"
58 #define vt100_bs           "\010"
59 #define vt100_bs_clear     "\010 \010"
60 #define vt100_tab          "\011"
61 #define vt100_crnl         "\012\015"
62 #define vt100_clear_right  "\033[0K"
63 #define vt100_clear_left   "\033[1K"
64 #define vt100_clear_down   "\033[0J"
65 #define vt100_clear_up     "\033[1J"
66 #define vt100_clear_line   "\033[2K"
67 #define vt100_clear_screen "\033[2J"
68 #define vt100_up_arr       "\033\133\101"
69 #define vt100_down_arr     "\033\133\102"
70 #define vt100_right_arr    "\033\133\103"
71 #define vt100_left_arr     "\033\133\104"
72 #define vt100_multi_right  "\033\133%uC"
73 #define vt100_multi_left   "\033\133%uD"
74 #define vt100_suppr        "\033\133\063\176"
75 #define vt100_home         "\033M\033E"
76 #define vt100_word_left    "\033\142"
77 #define vt100_word_right   "\033\146"
78
79 /* configuration */
80 #define RDLINE_BUF_SIZE 256
81 #define RDLINE_PROMPT_SIZE  32
82 #define RDLINE_VT100_BUF_SIZE  8
83 #define RDLINE_HISTORY_BUF_SIZE BUFSIZ
84 #define RDLINE_HISTORY_MAX_LINE 64
85
86 enum rdline_status {
87         RDLINE_INIT,
88         RDLINE_RUNNING,
89 };
90
91 struct rdline;
92
93 typedef void (rdline_write_char_t)(struct rdline *rdl, char);
94 typedef void (rdline_validate_t)(struct rdline *rdl,
95                                  const char *buf, unsigned int size);
96 typedef int (rdline_complete_t)(struct rdline *rdl, const char *buf,
97                                 char *dstbuf, unsigned int dstsize,
98                                 int *state);
99
100 struct rdline {
101         enum rdline_status status;
102         /* rdline bufs */
103         struct cirbuf left;
104         struct cirbuf right;
105         char left_buf[RDLINE_BUF_SIZE+2]; /* reserve 2 chars for the \n\0 */
106         char right_buf[RDLINE_BUF_SIZE];
107
108         char prompt[RDLINE_PROMPT_SIZE];
109         unsigned int prompt_size;
110
111 #ifndef NO_RDLINE_KILL_BUF
112         char kill_buf[RDLINE_BUF_SIZE];
113         unsigned int kill_size;
114 #endif
115
116 #ifndef NO_RDLINE_HISTORY
117         /* history */
118         struct cirbuf history;
119         char history_buf[RDLINE_HISTORY_BUF_SIZE];
120         int history_cur_line;
121 #endif
122
123         /* callbacks and func pointers */
124         rdline_write_char_t *write_char;
125         rdline_validate_t *validate;
126         rdline_complete_t *complete;
127
128         /* vt100 parser */
129         struct cmdline_vt100 vt100;
130
131         /* opaque pointer */
132         void *opaque;
133 };
134
135 /**
136  * Init fields for a struct rdline. Call this only once at the beginning
137  * of your program.
138  * \param rdl A pointer to an uninitialized struct rdline
139  * \param write_char The function used by the function to write a character
140  * \param validate A pointer to the function to execute when the
141  *                 user validates the buffer.
142  * \param complete A pointer to the function to execute when the
143  *                 user completes the buffer.
144  */
145 void rdline_init(struct rdline *rdl,
146                  rdline_write_char_t *write_char,
147                  rdline_validate_t *validate,
148                  rdline_complete_t *complete);
149
150
151 /**
152  * Init the current buffer, and display a prompt.
153  * \param rdl A pointer to a struct rdline
154  * \param prompt A string containing the prompt
155  */
156 void rdline_newline(struct rdline *rdl, const char *prompt);
157
158 /**
159  * Call it and all received chars will be ignored.
160  * \param rdl A pointer to a struct rdline
161  */
162 void rdline_stop(struct rdline *rdl);
163
164 /**
165  * Restart after a call to rdline_stop()
166  * \param rdl A pointer to a struct rdline
167  */
168 void rdline_restart(struct rdline *rdl);
169
170 /**
171  * Redisplay the current buffer
172  * \param rdl A pointer to a struct rdline
173  */
174 void rdline_redisplay(struct rdline *rdl);
175
176
177 /**
178  * append a char to the readline buffer.
179  * Return 1 when the line has been validated.
180  * Return 2 when the user asked to complete the buffer.
181  * Return -1 if it is not running.
182  * Return -2 if EOF (ctrl-d on an empty line).
183  * Else return 0.
184  * XXX error case when the buffer is full ?
185  *
186  * \param rdl A pointer to a struct rdline
187  * \param c The character to append
188  */
189 int rdline_char_in(struct rdline *rdl, char c);
190
191 /**
192  * Return the current buffer, terminated by '\0'.
193  * \param rdl A pointer to a struct rdline
194  */
195 const char *rdline_get_buffer(struct rdline *rdl);
196
197
198 /**
199  * Add the buffer to history.
200  * return < 0 on error.
201  * \param rdl A pointer to a struct rdline
202  * \param buf A buffer that is terminated by '\0'
203  */
204 int rdline_add_history(struct rdline *rdl, const char *buf);
205
206 /**
207  * Clear current history
208  * \param rdl A pointer to a struct rdline
209  */
210 void rdline_clear_history(struct rdline *rdl);
211
212 /**
213  * Get the i-th history item
214  */
215 char *rdline_get_history_item(struct rdline *rdl, unsigned int i);
216
217 #endif /* _RDLINE_H_ */