source: mainline/uspace/app/bdsh/input.c@ 36a75a2

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

Move text input from Bdsh to a separate library, clui.

  • Property mode set to 100644
File size: 3.4 KB
Line 
1/* Copyright (c) 2008, Tim Post <tinkertim@gmail.com>
2 * All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are met:
6 *
7 * Redistributions of source code must retain the above copyright notice, this
8 * list of conditions and the following disclaimer.
9 *
10 * Redistributions in binary form must reproduce the above copyright notice,
11 * this list of conditions and the following disclaimer in the documentation
12 * and/or other materials provided with the distribution.
13 *
14 * Neither the name of the original program's authors nor the names of its
15 * contributors may be used to endorse or promote products derived from this
16 * software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
22 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 * POSSIBILITY OF SUCH DAMAGE.
29 */
30
31#include <stdio.h>
32#include <stdlib.h>
33#include <str.h>
34#include <io/console.h>
35#include <io/keycode.h>
36#include <io/style.h>
37#include <io/color.h>
38#include <vfs/vfs.h>
39#include <clipboard.h>
40#include <macros.h>
41#include <errno.h>
42#include <assert.h>
43#include <bool.h>
44#include <tinput.h>
45
46#include "config.h"
47#include "util.h"
48#include "scli.h"
49#include "input.h"
50#include "errors.h"
51#include "exec.h"
52
53/** Text input field. */
54static tinput_t *tinput;
55
56/* Tokenizes input from console, sees if the first word is a built-in, if so
57 * invokes the built-in entry point (a[0]) passing all arguments in a[] to
58 * the handler */
59int tok_input(cliuser_t *usr)
60{
61 char *cmd[WORD_MAX];
62 int n = 0, i = 0;
63 int rc = 0;
64 char *tmp;
65
66 if (NULL == usr->line)
67 return CL_EFAIL;
68
69 tmp = str_dup(usr->line);
70
71 cmd[n] = strtok(tmp, " ");
72 while (cmd[n] && n < WORD_MAX) {
73 cmd[++n] = strtok(NULL, " ");
74 }
75
76 /* We have rubbish */
77 if (NULL == cmd[0]) {
78 rc = CL_ENOENT;
79 goto finit;
80 }
81
82 /* Its a builtin command ? */
83 if ((i = (is_builtin(cmd[0]))) > -1) {
84 rc = run_builtin(i, cmd, usr);
85 goto finit;
86 /* Its a module ? */
87 } else if ((i = (is_module(cmd[0]))) > -1) {
88 rc = run_module(i, cmd);
89 goto finit;
90 }
91
92 /* See what try_exec thinks of it */
93 rc = try_exec(cmd[0], cmd);
94
95finit:
96 if (NULL != usr->line) {
97 free(usr->line);
98 usr->line = (char *) NULL;
99 }
100 if (NULL != tmp)
101 free(tmp);
102
103 return rc;
104}
105
106void get_input(cliuser_t *usr)
107{
108 char *str;
109
110 fflush(stdout);
111 console_set_style(fphone(stdout), STYLE_EMPHASIS);
112 printf("%s", usr->prompt);
113 fflush(stdout);
114 console_set_style(fphone(stdout), STYLE_NORMAL);
115
116 str = tinput_read(tinput);
117
118 /* Check for empty input. */
119 if (str_cmp(str, "") == 0) {
120 free(str);
121 return;
122 }
123
124 usr->line = str;
125 return;
126}
127
128int input_init(void)
129{
130 tinput = tinput_new();
131 if (tinput == NULL) {
132 printf("Failed to initialize input.\n");
133 return 1;
134 }
135
136 return 0;
137}
Note: See TracBrowser for help on using the repository browser.