| 1 | /*
 | 
|---|
| 2 |  * Copyright (c) 2011 Jiri Svoboda
 | 
|---|
| 3 |  * All rights reserved.
 | 
|---|
| 4 |  *
 | 
|---|
| 5 |  * Redistribution and use in source and binary forms, with or without
 | 
|---|
| 6 |  * modification, are permitted provided that the following conditions
 | 
|---|
| 7 |  * are met:
 | 
|---|
| 8 |  *
 | 
|---|
| 9 |  * - Redistributions of source code must retain the above copyright
 | 
|---|
| 10 |  *   notice, this list of conditions and the following disclaimer.
 | 
|---|
| 11 |  * - Redistributions in binary form must reproduce the above copyright
 | 
|---|
| 12 |  *   notice, this list of conditions and the following disclaimer in the
 | 
|---|
| 13 |  *   documentation and/or other materials provided with the distribution.
 | 
|---|
| 14 |  * - The name of the author may not be used to endorse or promote products
 | 
|---|
| 15 |  *   derived from this software without specific prior written permission.
 | 
|---|
| 16 |  *
 | 
|---|
| 17 |  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
 | 
|---|
| 18 |  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 | 
|---|
| 19 |  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 | 
|---|
| 20 |  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
 | 
|---|
| 21 |  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 | 
|---|
| 22 |  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 | 
|---|
| 23 |  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 | 
|---|
| 24 |  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 | 
|---|
| 25 |  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
 | 
|---|
| 26 |  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 | 
|---|
| 27 |  */
 | 
|---|
| 28 | 
 | 
|---|
| 29 | /** @addtogroup libclui
 | 
|---|
| 30 |  * @{
 | 
|---|
| 31 |  */
 | 
|---|
| 32 | /**
 | 
|---|
| 33 |  * @file
 | 
|---|
| 34 |  */
 | 
|---|
| 35 | 
 | 
|---|
| 36 | #ifndef LIBCLUI_TINPUT_H_
 | 
|---|
| 37 | #define LIBCLUI_TINPUT_H_
 | 
|---|
| 38 | 
 | 
|---|
| 39 | #include <inttypes.h>
 | 
|---|
| 40 | #include <io/console.h>
 | 
|---|
| 41 | #include <stdio.h>
 | 
|---|
| 42 | 
 | 
|---|
| 43 | #define HISTORY_LEN     10
 | 
|---|
| 44 | #define INPUT_MAX_SIZE  1024
 | 
|---|
| 45 | 
 | 
|---|
| 46 | /** Begin enumeration of text completions.
 | 
|---|
| 47 |  *
 | 
|---|
| 48 |  * When user requests text completion, tinput will call this function to start
 | 
|---|
| 49 |  * text completion operation. @a *cstart should be set to the position
 | 
|---|
| 50 |  * (character index) of the first character of the 'word' that is being
 | 
|---|
| 51 |  * completed. The resulting text is obtained by replacing the range of text
 | 
|---|
| 52 |  * starting at @a *cstart and ending at @a pos with the completion text.
 | 
|---|
| 53 |  *
 | 
|---|
| 54 |  * The function can pass information to the get_next and fini functions
 | 
|---|
| 55 |  * via @a state. The init function allocates the state object and stores
 | 
|---|
| 56 |  * a pointer to it to @a *state. The fini function destroys the state object.
 | 
|---|
| 57 |  *
 | 
|---|
| 58 |  * @param text          Current contents of edit buffer (null-terminated).
 | 
|---|
| 59 |  * @param pos           Current caret position.
 | 
|---|
| 60 |  * @param cstart        Output, position in text where completion begins from.
 | 
|---|
| 61 |  * @param state         Output, pointer to a client state object.
 | 
|---|
| 62 |  *
 | 
|---|
| 63 |  * @return              EOK on success, error code on failure.
 | 
|---|
| 64 |  */
 | 
|---|
| 65 | typedef errno_t (*tinput_compl_init_fn)(wchar_t *text, size_t pos, size_t *cstart,
 | 
|---|
| 66 |     void **state);
 | 
|---|
| 67 | 
 | 
|---|
| 68 | /** Obtain one text completion alternative.
 | 
|---|
| 69 |  *
 | 
|---|
| 70 |  * Upon success the function sets @a *compl to point to a string, the
 | 
|---|
| 71 |  * completion text. The caller (Tinput) should not modify or free the text.
 | 
|---|
| 72 |  * The pointer is only valid until the next invocation of any completion
 | 
|---|
| 73 |  * function.
 | 
|---|
| 74 |  *
 | 
|---|
| 75 |  * @param state         Pointer to state object created by the init funtion.
 | 
|---|
| 76 |  * @param compl         Output, the completion text, ownership retained.
 | 
|---|
| 77 |  *
 | 
|---|
| 78 |  * @return              EOK on success, error code on failure.
 | 
|---|
| 79 |  */
 | 
|---|
| 80 | typedef errno_t (*tinput_compl_get_next_fn)(void *state, char **compl);
 | 
|---|
| 81 | 
 | 
|---|
| 82 | /** Finish enumeration of text completions.
 | 
|---|
| 83 |  *
 | 
|---|
| 84 |  * The function must deallocate any state information allocated by the init
 | 
|---|
| 85 |  * function or temporary data allocated by the get_next function.
 | 
|---|
| 86 |  *
 | 
|---|
| 87 |  * @param state         Pointer to state object created by the init funtion.
 | 
|---|
| 88 |  */
 | 
|---|
| 89 | typedef void (*tinput_compl_fini_fn)(void *state);
 | 
|---|
| 90 | 
 | 
|---|
| 91 | /** Text completion ops. */
 | 
|---|
| 92 | typedef struct {
 | 
|---|
| 93 |         tinput_compl_init_fn init;
 | 
|---|
| 94 |         tinput_compl_get_next_fn get_next;
 | 
|---|
| 95 |         tinput_compl_fini_fn fini;
 | 
|---|
| 96 | } tinput_compl_ops_t;
 | 
|---|
| 97 | 
 | 
|---|
| 98 | /** Text input field (command line).
 | 
|---|
| 99 |  *
 | 
|---|
| 100 |  * Applications should treat this structure as opaque.
 | 
|---|
| 101 |  */
 | 
|---|
| 102 | typedef struct {
 | 
|---|
| 103 |         /** Console */
 | 
|---|
| 104 |         console_ctrl_t *console;
 | 
|---|
| 105 | 
 | 
|---|
| 106 |         /** Prompt string */
 | 
|---|
| 107 |         char *prompt;
 | 
|---|
| 108 | 
 | 
|---|
| 109 |         /** Completion ops. */
 | 
|---|
| 110 |         tinput_compl_ops_t *compl_ops;
 | 
|---|
| 111 | 
 | 
|---|
| 112 |         /** Buffer holding text currently being edited */
 | 
|---|
| 113 |         wchar_t buffer[INPUT_MAX_SIZE + 1];
 | 
|---|
| 114 | 
 | 
|---|
| 115 |         /** Linear position on screen where the prompt starts */
 | 
|---|
| 116 |         unsigned prompt_coord;
 | 
|---|
| 117 |         /** Linear position on screen where the text field starts */
 | 
|---|
| 118 |         unsigned text_coord;
 | 
|---|
| 119 | 
 | 
|---|
| 120 |         /** Screen dimensions */
 | 
|---|
| 121 |         sysarg_t con_cols;
 | 
|---|
| 122 |         sysarg_t con_rows;
 | 
|---|
| 123 | 
 | 
|---|
| 124 |         /** Number of characters in @c buffer */
 | 
|---|
| 125 |         size_t nc;
 | 
|---|
| 126 | 
 | 
|---|
| 127 |         /** Caret position within buffer */
 | 
|---|
| 128 |         size_t pos;
 | 
|---|
| 129 | 
 | 
|---|
| 130 |         /** Selection mark position within buffer */
 | 
|---|
| 131 |         size_t sel_start;
 | 
|---|
| 132 | 
 | 
|---|
| 133 |         /** History (dynamically allocated strings) */
 | 
|---|
| 134 |         char *history[HISTORY_LEN + 1];
 | 
|---|
| 135 | 
 | 
|---|
| 136 |         /** Number of entries in @c history, not counting [0] */
 | 
|---|
| 137 |         size_t hnum;
 | 
|---|
| 138 | 
 | 
|---|
| 139 |         /** Current position in history */
 | 
|---|
| 140 |         size_t hpos;
 | 
|---|
| 141 | 
 | 
|---|
| 142 |         /** @c true if finished with this line (return to caller) */
 | 
|---|
| 143 |         bool done;
 | 
|---|
| 144 | 
 | 
|---|
| 145 |         /** @c true if user requested to abort interactive loop */
 | 
|---|
| 146 |         bool exit_clui;
 | 
|---|
| 147 | 
 | 
|---|
| 148 |         /** @c true if left shift key is currently held */
 | 
|---|
| 149 |         bool lshift_held;
 | 
|---|
| 150 | 
 | 
|---|
| 151 |         /** @c true if right shift key is currently held */
 | 
|---|
| 152 |         bool rshift_held;
 | 
|---|
| 153 | } tinput_t;
 | 
|---|
| 154 | 
 | 
|---|
| 155 | extern tinput_t *tinput_new(void);
 | 
|---|
| 156 | extern errno_t tinput_set_prompt(tinput_t *, const char *);
 | 
|---|
| 157 | extern void tinput_set_compl_ops(tinput_t *, tinput_compl_ops_t *);
 | 
|---|
| 158 | extern void tinput_destroy(tinput_t *);
 | 
|---|
| 159 | extern errno_t tinput_read(tinput_t *, char **);
 | 
|---|
| 160 | extern errno_t tinput_read_i(tinput_t *, const char *, char **);
 | 
|---|
| 161 | 
 | 
|---|
| 162 | #endif
 | 
|---|
| 163 | 
 | 
|---|
| 164 | /** @}
 | 
|---|
| 165 |  */
 | 
|---|