source: mainline/uspace/lib/clui/tinput.h@ bf31e3f

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since bf31e3f was 9be9c4d, checked in by Jiri Svoboda <jiri@…>, 14 years ago

Shell command and file-name completion.

  • Property mode set to 100644
File size: 5.1 KB
RevLine 
[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
[9be9c4d]39#include <adt/list.h>
[79ae36dd]40#include <async.h>
41#include <inttypes.h>
42#include <io/console.h>
[9be9c4d]43#include <stdio.h>
[79ae36dd]44
[9f1362d4]45#define HISTORY_LEN 10
46#define INPUT_MAX_SIZE 1024
[36a75a2]47
[9be9c4d]48/** Begin enumeration of text completions.
49 *
50 * When user requests text completion, tinput will call this function to start
51 * text completion operation. @a *cstart should be set to the position
52 * (character index) of the first character of the 'word' that is being
53 * completed. The resulting text is obtained by replacing the range of text
54 * starting at @a *cstart and ending at @a pos with the completion text.
55 *
56 * The function can pass information to the get_next and fini functions
57 * via @a state. The init function allocates the state object and stores
58 * a pointer to it to @a *state. The fini function destroys the state object.
59 *
60 * @param text Current contents of edit buffer (null-terminated).
61 * @param pos Current caret position.
62 * @param cstart Output, position in text where completion begins from.
63 * @param state Output, pointer to a client state object.
64 *
65 * @return EOK on success, negative error code on failure.
66 */
67typedef int (*tinput_compl_init_fn)(wchar_t *text, size_t pos, size_t *cstart,
68 void **state);
69
70/** Obtain one text completion alternative.
71 *
72 * Upon success the function sets @a *compl to point to a string, the
73 * completion text. The caller (Tinput) should not modify or free the text.
74 * The pointer is only valid until the next invocation of any completion
75 * function.
76 *
77 * @param state Pointer to state object created by the init funtion.
78 * @param compl Output, the completion text, ownership retained.
79 *
80 * @return EOK on success, negative error code on failure.
81 */
82typedef int (*tinput_compl_get_next_fn)(void *state, char **compl);
83
84
85/** Finish enumeration of text completions.
86 *
87 * The function must deallocate any state information allocated by the init
88 * function or temporary data allocated by the get_next function.
89 *
90 * @param state Pointer to state object created by the init funtion.
91 */
92typedef void (*tinput_compl_fini_fn)(void *state);
93
94/** Text completion ops. */
95typedef struct {
96 tinput_compl_init_fn init;
97 tinput_compl_get_next_fn get_next;
98 tinput_compl_fini_fn fini;
99} tinput_compl_ops_t;
100
[36a75a2]101/** Text input field (command line).
102 *
103 * Applications should treat this structure as opaque.
104 */
105typedef struct {
[79ae36dd]106 /** Console */
107 console_ctrl_t *console;
108
[9be9c4d]109 /** Prompt string */
110 char *prompt;
111
112 /** Completion ops. */
113 tinput_compl_ops_t *compl_ops;
114
[36a75a2]115 /** Buffer holding text currently being edited */
116 wchar_t buffer[INPUT_MAX_SIZE + 1];
[9f1362d4]117
[9be9c4d]118 /** Linear position on screen where the prompt starts */
119 unsigned prompt_coord;
120 /** Linear position on screen where the text field starts */
121 unsigned text_coord;
[9f1362d4]122
[36a75a2]123 /** Screen dimensions */
[96b02eb9]124 sysarg_t con_cols;
125 sysarg_t con_rows;
[9f1362d4]126
[36a75a2]127 /** Number of characters in @c buffer */
[9f1362d4]128 size_t nc;
129
[36a75a2]130 /** Caret position within buffer */
[9f1362d4]131 size_t pos;
132
[36a75a2]133 /** Selection mark position within buffer */
[9f1362d4]134 size_t sel_start;
135
[36a75a2]136 /** History (dynamically allocated strings) */
[9f1362d4]137 char *history[HISTORY_LEN + 1];
138
[36a75a2]139 /** Number of entries in @c history, not counting [0] */
[9f1362d4]140 size_t hnum;
141
[36a75a2]142 /** Current position in history */
[9f1362d4]143 size_t hpos;
144
[5db9084]145 /** @c true if finished with this line (return to caller) */
[36a75a2]146 bool done;
[9f1362d4]147
[5db9084]148 /** @c true if user requested to abort interactive loop */
149 bool exit_clui;
[36a75a2]150} tinput_t;
151
152extern tinput_t *tinput_new(void);
[9be9c4d]153extern int tinput_set_prompt(tinput_t *, const char *);
154extern void tinput_set_compl_ops(tinput_t *, tinput_compl_ops_t *);
[9f1362d4]155extern void tinput_destroy(tinput_t *);
156extern int tinput_read(tinput_t *, char **);
[36a75a2]157
158#endif
159
160/** @}
161 */
Note: See TracBrowser for help on using the repository browser.