1 /* SPDX-License-Identifier: BSD-3-Clause 2 * Copyright(c) 2010-2014 Intel Corporation. 3 * Copyright (c) 2009, Olivier MATZ <zer0@droids-corp.org> 4 * All rights reserved. 5 */ 6 7 #ifndef _RDLINE_H_ 8 #define _RDLINE_H_ 9 10 /** 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. 14 * 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. 18 * 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. 22 * 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 28 * instance. 29 */ 30 31 #include <stdio.h> 32 33 #include <cmdline_cirbuf.h> 34 #include <cmdline_vt100.h> 35 36 #ifdef __cplusplus 37 extern "C" { 38 #endif 39 40 struct rdline; 41 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, 47 int *state); 48 49 /** 50 * Allocate and initialize a new rdline instance. 51 * 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. 58 * 59 * \return New rdline object on success, NULL on failure. 60 */ 61 struct rdline *rdline_new(rdline_write_char_t *write_char, 62 rdline_validate_t *validate, 63 rdline_complete_t *complete, 64 void *opaque); 65 66 /** 67 * Free an rdline instance. 68 * 69 * \param rdl A pointer to an initialized struct rdline. 70 * If NULL, this function is a no-op. 71 */ 72 void rdline_free(struct rdline *rdl); 73 74 /** 75 * Init the current buffer, and display a prompt. 76 * \param rdl A pointer to a struct rdline 77 * \param prompt A string containing the prompt 78 */ 79 void rdline_newline(struct rdline *rdl, const char *prompt); 80 81 /** 82 * Call it and all received chars will be ignored. 83 * \param rdl A pointer to a struct rdline 84 */ 85 void rdline_stop(struct rdline *rdl); 86 87 /** 88 * Same than rdline_stop() except that next calls to rdline_char_in() 89 * will return RDLINE_RES_EXITED. 90 * \param rdl A pointer to a struct rdline 91 */ 92 void rdline_quit(struct rdline *rdl); 93 94 /** 95 * Restart after a call to rdline_stop() or rdline_quit() 96 * \param rdl A pointer to a struct rdline 97 */ 98 void rdline_restart(struct rdline *rdl); 99 100 /** 101 * Redisplay the current buffer 102 * \param rdl A pointer to a struct rdline 103 */ 104 void rdline_redisplay(struct rdline *rdl); 105 106 /** 107 * Reset the current buffer and setup for a new line. 108 * \param rdl A pointer to a struct rdline 109 */ 110 void rdline_reset(struct rdline *rdl); 111 112 113 /* return status for rdline_char_in() */ 114 #define RDLINE_RES_SUCCESS 0 115 #define RDLINE_RES_VALIDATED 1 116 #define RDLINE_RES_COMPLETE 2 117 #define RDLINE_RES_NOT_RUNNING -1 118 #define RDLINE_RES_EOF -2 119 #define RDLINE_RES_EXITED -3 120 121 /** 122 * append a char to the readline buffer. 123 * Return RDLINE_RES_VALIDATE when the line has been validated. 124 * Return RDLINE_RES_COMPLETE when the user asked to complete the buffer. 125 * Return RDLINE_RES_NOT_RUNNING if it is not running. 126 * Return RDLINE_RES_EOF if EOF (ctrl-d on an empty line). 127 * Else return RDLINE_RES_SUCCESS. 128 * XXX error case when the buffer is full ? 129 * 130 * \param rdl A pointer to a struct rdline 131 * \param c The character to append 132 */ 133 int rdline_char_in(struct rdline *rdl, char c); 134 135 /** 136 * Return the current buffer, terminated by '\0'. 137 * \param rdl A pointer to a struct rdline 138 */ 139 const char *rdline_get_buffer(struct rdline *rdl); 140 141 142 /** 143 * Add the buffer to history. 144 * return < 0 on error. 145 * \param rdl A pointer to a struct rdline 146 * \param buf A buffer that is terminated by '\0' 147 */ 148 int rdline_add_history(struct rdline *rdl, const char *buf); 149 150 /** 151 * Clear current history 152 * \param rdl A pointer to a struct rdline 153 */ 154 void rdline_clear_history(struct rdline *rdl); 155 156 /** 157 * Get the i-th history item 158 */ 159 char *rdline_get_history_item(struct rdline *rdl, unsigned int i); 160 161 /** 162 * Get maximum history buffer size. 163 */ 164 size_t rdline_get_history_buffer_size(struct rdline *rdl); 165 166 /** 167 * Get the opaque pointer supplied on struct rdline creation. 168 */ 169 void *rdline_get_opaque(struct rdline *rdl); 170 171 #ifdef __cplusplus 172 } 173 #endif 174 175 #endif /* _RDLINE_H_ */ 176