[36a75a2] | 1 | /*
|
---|
[9be9c4d] | 2 | * Copyright (c) 2011 Jiri Svoboda
|
---|
[36a75a2] | 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 | * @{
|
---|
[9f1362d4] | 31 | */
|
---|
[36a75a2] | 32 | /**
|
---|
| 33 | * @file
|
---|
| 34 | */
|
---|
| 35 |
|
---|
| 36 | #ifndef LIBCLUI_TINPUT_H_
|
---|
| 37 | #define LIBCLUI_TINPUT_H_
|
---|
| 38 |
|
---|
[79ae36dd] | 39 | #include <inttypes.h>
|
---|
| 40 | #include <io/console.h>
|
---|
[9be9c4d] | 41 | #include <stdio.h>
|
---|
[79ae36dd] | 42 |
|
---|
[9f1362d4] | 43 | #define HISTORY_LEN 10
|
---|
| 44 | #define INPUT_MAX_SIZE 1024
|
---|
[36a75a2] | 45 |
|
---|
[9be9c4d] | 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, negative error code on failure.
|
---|
| 64 | */
|
---|
| 65 | typedef int (*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, negative error code on failure.
|
---|
| 79 | */
|
---|
| 80 | typedef int (*tinput_compl_get_next_fn)(void *state, char **compl);
|
---|
| 81 |
|
---|
| 82 |
|
---|
| 83 | /** Finish enumeration of text completions.
|
---|
| 84 | *
|
---|
| 85 | * The function must deallocate any state information allocated by the init
|
---|
| 86 | * function or temporary data allocated by the get_next function.
|
---|
| 87 | *
|
---|
| 88 | * @param state Pointer to state object created by the init funtion.
|
---|
| 89 | */
|
---|
| 90 | typedef void (*tinput_compl_fini_fn)(void *state);
|
---|
| 91 |
|
---|
| 92 | /** Text completion ops. */
|
---|
| 93 | typedef struct {
|
---|
| 94 | tinput_compl_init_fn init;
|
---|
| 95 | tinput_compl_get_next_fn get_next;
|
---|
| 96 | tinput_compl_fini_fn fini;
|
---|
| 97 | } tinput_compl_ops_t;
|
---|
| 98 |
|
---|
[36a75a2] | 99 | /** Text input field (command line).
|
---|
| 100 | *
|
---|
| 101 | * Applications should treat this structure as opaque.
|
---|
| 102 | */
|
---|
| 103 | typedef struct {
|
---|
[79ae36dd] | 104 | /** Console */
|
---|
| 105 | console_ctrl_t *console;
|
---|
| 106 |
|
---|
[9be9c4d] | 107 | /** Prompt string */
|
---|
| 108 | char *prompt;
|
---|
| 109 |
|
---|
| 110 | /** Completion ops. */
|
---|
| 111 | tinput_compl_ops_t *compl_ops;
|
---|
| 112 |
|
---|
[36a75a2] | 113 | /** Buffer holding text currently being edited */
|
---|
| 114 | wchar_t buffer[INPUT_MAX_SIZE + 1];
|
---|
[9f1362d4] | 115 |
|
---|
[9be9c4d] | 116 | /** Linear position on screen where the prompt starts */
|
---|
| 117 | unsigned prompt_coord;
|
---|
| 118 | /** Linear position on screen where the text field starts */
|
---|
| 119 | unsigned text_coord;
|
---|
[9f1362d4] | 120 |
|
---|
[36a75a2] | 121 | /** Screen dimensions */
|
---|
[96b02eb9] | 122 | sysarg_t con_cols;
|
---|
| 123 | sysarg_t con_rows;
|
---|
[9f1362d4] | 124 |
|
---|
[36a75a2] | 125 | /** Number of characters in @c buffer */
|
---|
[9f1362d4] | 126 | size_t nc;
|
---|
| 127 |
|
---|
[36a75a2] | 128 | /** Caret position within buffer */
|
---|
[9f1362d4] | 129 | size_t pos;
|
---|
| 130 |
|
---|
[36a75a2] | 131 | /** Selection mark position within buffer */
|
---|
[9f1362d4] | 132 | size_t sel_start;
|
---|
| 133 |
|
---|
[36a75a2] | 134 | /** History (dynamically allocated strings) */
|
---|
[9f1362d4] | 135 | char *history[HISTORY_LEN + 1];
|
---|
| 136 |
|
---|
[36a75a2] | 137 | /** Number of entries in @c history, not counting [0] */
|
---|
[9f1362d4] | 138 | size_t hnum;
|
---|
| 139 |
|
---|
[36a75a2] | 140 | /** Current position in history */
|
---|
[9f1362d4] | 141 | size_t hpos;
|
---|
| 142 |
|
---|
[5db9084] | 143 | /** @c true if finished with this line (return to caller) */
|
---|
[36a75a2] | 144 | bool done;
|
---|
[9f1362d4] | 145 |
|
---|
[5db9084] | 146 | /** @c true if user requested to abort interactive loop */
|
---|
| 147 | bool exit_clui;
|
---|
[36a75a2] | 148 | } tinput_t;
|
---|
| 149 |
|
---|
| 150 | extern tinput_t *tinput_new(void);
|
---|
[9be9c4d] | 151 | extern int tinput_set_prompt(tinput_t *, const char *);
|
---|
| 152 | extern void tinput_set_compl_ops(tinput_t *, tinput_compl_ops_t *);
|
---|
[9f1362d4] | 153 | extern void tinput_destroy(tinput_t *);
|
---|
| 154 | extern int tinput_read(tinput_t *, char **);
|
---|
[36a75a2] | 155 |
|
---|
| 156 | #endif
|
---|
| 157 |
|
---|
| 158 | /** @}
|
---|
| 159 | */
|
---|