source: mainline/uspace/app/bdsh/input.c@ e866806

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

Add wstr_to_astr() for easy conversion from wide string to string.

  • Property mode set to 100644
File size: 8.2 KB
Line 
1/* Copyright (c) 2008, Tim Post <tinkertim@gmail.com>
2 * All rights reserved.
3 * Copyright (c) 2008, Jiri Svoboda - 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 are met:
7 *
8 * Redistributions of source code must retain the above copyright notice, this
9 * list of conditions and the following disclaimer.
10 *
11 * Redistributions in binary form must reproduce the above copyright notice,
12 * this list of conditions and the following disclaimer in the documentation
13 * and/or other materials provided with the distribution.
14 *
15 * Neither the name of the original program's authors nor the names of its
16 * contributors may be used to endorse or promote products derived from this
17 * software without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
23 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 */
31
32#include <stdio.h>
33#include <stdlib.h>
34#include <string.h>
35#include <io/console.h>
36#include <io/keycode.h>
37#include <io/style.h>
38#include <vfs/vfs.h>
39#include <errno.h>
40#include <assert.h>
41#include <bool.h>
42
43#include "config.h"
44#include "util.h"
45#include "scli.h"
46#include "input.h"
47#include "errors.h"
48#include "exec.h"
49
50#define HISTORY_LEN 10
51
52typedef struct {
53 wchar_t buffer[INPUT_MAX];
54 int col0, row0;
55 int con_cols, con_rows;
56 int nc;
57 int pos;
58
59 char *history[1 + HISTORY_LEN];
60 int hnum;
61 int hpos;
62} tinput_t;
63
64typedef enum {
65 seek_backward = -1,
66 seek_forward = 1
67} seek_dir_t;
68
69static tinput_t tinput;
70
71static char *tinput_read(tinput_t *ti);
72
73/* Tokenizes input from console, sees if the first word is a built-in, if so
74 * invokes the built-in entry point (a[0]) passing all arguments in a[] to
75 * the handler */
76int tok_input(cliuser_t *usr)
77{
78 char *cmd[WORD_MAX];
79 int n = 0, i = 0;
80 int rc = 0;
81 char *tmp;
82
83 if (NULL == usr->line)
84 return CL_EFAIL;
85
86 tmp = str_dup(usr->line);
87
88 cmd[n] = strtok(tmp, " ");
89 while (cmd[n] && n < WORD_MAX) {
90 cmd[++n] = strtok(NULL, " ");
91 }
92
93 /* We have rubbish */
94 if (NULL == cmd[0]) {
95 rc = CL_ENOENT;
96 goto finit;
97 }
98
99 /* Its a builtin command ? */
100 if ((i = (is_builtin(cmd[0]))) > -1) {
101 rc = run_builtin(i, cmd, usr);
102 goto finit;
103 /* Its a module ? */
104 } else if ((i = (is_module(cmd[0]))) > -1) {
105 rc = run_module(i, cmd);
106 goto finit;
107 }
108
109 /* See what try_exec thinks of it */
110 rc = try_exec(cmd[0], cmd);
111
112finit:
113 if (NULL != usr->line) {
114 free(usr->line);
115 usr->line = (char *) NULL;
116 }
117 if (NULL != tmp)
118 free(tmp);
119
120 return rc;
121}
122
123static void tinput_display_tail(tinput_t *ti, int start, int pad)
124{
125 int i;
126
127 console_goto(fphone(stdout), ti->col0 + start, ti->row0);
128 printf("%ls", ti->buffer + start);
129 for (i = 0; i < pad; ++i)
130 putchar(' ');
131 fflush(stdout);
132}
133
134static char *tinput_get_str(tinput_t *ti)
135{
136 return wstr_to_astr(ti->buffer);
137}
138
139static void tinput_position_caret(tinput_t *ti)
140{
141 console_goto(fphone(stdout), ti->col0 + ti->pos, ti->row0);
142}
143
144static void tinput_insert_char(tinput_t *ti, wchar_t c)
145{
146 int i;
147
148 if (ti->nc == INPUT_MAX)
149 return;
150
151 if (ti->col0 + ti->nc >= ti->con_cols - 1)
152 return;
153
154 for (i = ti->nc; i > ti->pos; --i)
155 ti->buffer[i] = ti->buffer[i - 1];
156
157 ti->buffer[ti->pos] = c;
158 ti->pos += 1;
159 ti->nc += 1;
160 ti->buffer[ti->nc] = '\0';
161
162 tinput_display_tail(ti, ti->pos - 1, 0);
163 tinput_position_caret(ti);
164}
165
166static void tinput_backspace(tinput_t *ti)
167{
168 int i;
169
170 if (ti->pos == 0)
171 return;
172
173 for (i = ti->pos; i < ti->nc; ++i)
174 ti->buffer[i - 1] = ti->buffer[i];
175 ti->pos -= 1;
176 ti->nc -= 1;
177 ti->buffer[ti->nc] = '\0';
178
179 tinput_display_tail(ti, ti->pos, 1);
180 tinput_position_caret(ti);
181}
182
183static void tinput_delete(tinput_t *ti)
184{
185 if (ti->pos == ti->nc)
186 return;
187
188 ti->pos += 1;
189 tinput_backspace(ti);
190}
191
192static void tinput_seek_cell(tinput_t *ti, seek_dir_t dir)
193{
194 if (dir == seek_forward) {
195 if (ti->pos < ti->nc)
196 ti->pos += 1;
197 } else {
198 if (ti->pos > 0)
199 ti->pos -= 1;
200 }
201
202 tinput_position_caret(ti);
203}
204
205static void tinput_seek_word(tinput_t *ti, seek_dir_t dir)
206{
207 if (dir == seek_forward) {
208 if (ti->pos == ti->nc)
209 return;
210
211 while (1) {
212 ti->pos += 1;
213
214 if (ti->pos == ti->nc)
215 break;
216
217 if (ti->buffer[ti->pos - 1] == ' ' &&
218 ti->buffer[ti->pos] != ' ')
219 break;
220 }
221 } else {
222 if (ti->pos == 0)
223 return;
224
225 while (1) {
226 ti->pos -= 1;
227
228 if (ti->pos == 0)
229 break;
230
231 if (ti->buffer[ti->pos - 1] == ' ' &&
232 ti->buffer[ti->pos] != ' ')
233 break;
234 }
235
236 }
237
238 tinput_position_caret(ti);
239}
240
241static void tinput_seek_max(tinput_t *ti, seek_dir_t dir)
242{
243 if (dir == seek_backward)
244 ti->pos = 0;
245 else
246 ti->pos = ti->nc;
247
248 tinput_position_caret(ti);
249}
250
251static void tinput_history_insert(tinput_t *ti, char *str)
252{
253 int i;
254
255 if (ti->hnum < HISTORY_LEN) {
256 ti->hnum += 1;
257 } else {
258 if (ti->history[HISTORY_LEN] != NULL)
259 free(ti->history[HISTORY_LEN]);
260 }
261
262 for (i = ti->hnum; i > 1; --i)
263 ti->history[i] = ti->history[i - 1];
264
265 ti->history[1] = str_dup(str);
266
267 if (ti->history[0] != NULL) {
268 free(ti->history[0]);
269 ti->history[0] = NULL;
270 }
271}
272
273static void tinput_set_str(tinput_t *ti, char *str)
274{
275 str_to_wstr(ti->buffer, INPUT_MAX, str);
276 ti->nc = wstr_length(ti->buffer);
277 ti->pos = ti->nc;
278}
279
280static void tinput_history_seek(tinput_t *ti, int offs)
281{
282 int pad;
283
284 if (ti->hpos + offs < 0 || ti->hpos + offs > ti->hnum)
285 return;
286
287 if (ti->history[ti->hpos] != NULL) {
288 free(ti->history[ti->hpos]);
289 ti->history[ti->hpos] = NULL;
290 }
291
292 ti->history[ti->hpos] = tinput_get_str(ti);
293 ti->hpos += offs;
294
295 pad = ti->nc - str_length(ti->history[ti->hpos]);
296 if (pad < 0) pad = 0;
297
298 tinput_set_str(ti, ti->history[ti->hpos]);
299 tinput_display_tail(ti, 0, pad);
300 tinput_position_caret(ti);
301}
302
303static void tinput_init(tinput_t *ti)
304{
305 ti->hnum = 0;
306 ti->hpos = 0;
307 ti->history[0] = NULL;
308}
309
310static char *tinput_read(tinput_t *ti)
311{
312 console_event_t ev;
313 char *str;
314
315 fflush(stdout);
316
317 if (console_get_size(fphone(stdin), &ti->con_cols, &ti->con_rows) != EOK)
318 return NULL;
319 if (console_get_pos(fphone(stdin), &ti->col0, &ti->row0) != EOK)
320 return NULL;
321
322 ti->pos = 0;
323 ti->nc = 0;
324
325 while (true) {
326 fflush(stdout);
327 if (!console_get_event(fphone(stdin), &ev))
328 return NULL;
329
330 if (ev.type != KEY_PRESS)
331 continue;
332
333 if ((ev.mods & KM_CTRL) != 0 &&
334 (ev.mods & (KM_ALT | KM_SHIFT)) == 0) {
335 switch (ev.key) {
336 case KC_LEFT:
337 tinput_seek_word(ti, seek_backward);
338 break;
339 case KC_RIGHT:
340 tinput_seek_word(ti, seek_forward);
341 break;
342 }
343 }
344
345 if ((ev.mods & (KM_CTRL | KM_ALT | KM_SHIFT)) == 0) {
346 switch (ev.key) {
347 case KC_ENTER:
348 case KC_NENTER:
349 goto done;
350 case KC_BACKSPACE:
351 tinput_backspace(ti);
352 break;
353 case KC_DELETE:
354 tinput_delete(ti);
355 break;
356 case KC_LEFT:
357 tinput_seek_cell(ti, seek_backward);
358 break;
359 case KC_RIGHT:
360 tinput_seek_cell(ti, seek_forward);
361 break;
362 case KC_HOME:
363 tinput_seek_max(ti, seek_backward);
364 break;
365 case KC_END:
366 tinput_seek_max(ti, seek_forward);
367 break;
368 case KC_UP:
369 tinput_history_seek(ti, +1);
370 break;
371 case KC_DOWN:
372 tinput_history_seek(ti, -1);
373 break;
374 }
375 }
376
377 if (ev.c >= ' ') {
378 tinput_insert_char(ti, ev.c);
379 }
380 }
381
382done:
383 putchar('\n');
384
385 str = tinput_get_str(ti);
386 if (str_cmp(str, "") != 0)
387 tinput_history_insert(ti, str);
388
389 ti->hpos = 0;
390
391 return str;
392}
393
394void get_input(cliuser_t *usr)
395{
396 char *str;
397
398 fflush(stdout);
399 console_set_style(fphone(stdout), STYLE_EMPHASIS);
400 printf("%s", usr->prompt);
401 fflush(stdout);
402 console_set_style(fphone(stdout), STYLE_NORMAL);
403
404 str = tinput_read(&tinput);
405
406 /* Check for empty input. */
407 if (str_cmp(str, "") == 0) {
408 free(str);
409 return;
410 }
411
412 usr->line = str;
413 return;
414}
415
416void input_init(void)
417{
418 tinput_init(&tinput);
419}
Note: See TracBrowser for help on using the repository browser.