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, 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 |
|
---|
99 | /** Text input field (command line).
|
---|
100 | *
|
---|
101 | * Applications should treat this structure as opaque.
|
---|
102 | */
|
---|
103 | typedef struct {
|
---|
104 | /** Console */
|
---|
105 | console_ctrl_t *console;
|
---|
106 |
|
---|
107 | /** Prompt string */
|
---|
108 | char *prompt;
|
---|
109 |
|
---|
110 | /** Completion ops. */
|
---|
111 | tinput_compl_ops_t *compl_ops;
|
---|
112 |
|
---|
113 | /** Buffer holding text currently being edited */
|
---|
114 | wchar_t buffer[INPUT_MAX_SIZE + 1];
|
---|
115 |
|
---|
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;
|
---|
120 |
|
---|
121 | /** Screen dimensions */
|
---|
122 | sysarg_t con_cols;
|
---|
123 | sysarg_t con_rows;
|
---|
124 |
|
---|
125 | /** Number of characters in @c buffer */
|
---|
126 | size_t nc;
|
---|
127 |
|
---|
128 | /** Caret position within buffer */
|
---|
129 | size_t pos;
|
---|
130 |
|
---|
131 | /** Selection mark position within buffer */
|
---|
132 | size_t sel_start;
|
---|
133 |
|
---|
134 | /** History (dynamically allocated strings) */
|
---|
135 | char *history[HISTORY_LEN + 1];
|
---|
136 |
|
---|
137 | /** Number of entries in @c history, not counting [0] */
|
---|
138 | size_t hnum;
|
---|
139 |
|
---|
140 | /** Current position in history */
|
---|
141 | size_t hpos;
|
---|
142 |
|
---|
143 | /** @c true if finished with this line (return to caller) */
|
---|
144 | bool done;
|
---|
145 |
|
---|
146 | /** @c true if user requested to abort interactive loop */
|
---|
147 | bool exit_clui;
|
---|
148 |
|
---|
149 | /** @c true if left shift key is currently held */
|
---|
150 | bool lshift_held;
|
---|
151 |
|
---|
152 | /** @c true if right shift key is currently held */
|
---|
153 | bool rshift_held;
|
---|
154 | } tinput_t;
|
---|
155 |
|
---|
156 | extern tinput_t *tinput_new(void);
|
---|
157 | extern int tinput_set_prompt(tinput_t *, const char *);
|
---|
158 | extern void tinput_set_compl_ops(tinput_t *, tinput_compl_ops_t *);
|
---|
159 | extern void tinput_destroy(tinput_t *);
|
---|
160 | extern int tinput_read(tinput_t *, char **);
|
---|
161 |
|
---|
162 | #endif
|
---|
163 |
|
---|
164 | /** @}
|
---|
165 | */
|
---|