source: mainline/uspace/app/bdsh/input.c@ 28ee877e

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 28ee877e was 28ee877e, checked in by Martin Sucha <sucha14@…>, 15 years ago

Rename tok_input to process_input

  • Property mode set to 100644
File size: 3.8 KB
RevLine 
[216d6fc]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>
[19f857a]33#include <str.h>
[73878c1]34#include <io/console.h>
35#include <io/keycode.h>
36#include <io/style.h>
[7e0cb78]37#include <io/color.h>
[3bf907a]38#include <vfs/vfs.h>
[371a012]39#include <clipboard.h>
[f1b37d6]40#include <macros.h>
[6071a8f]41#include <errno.h>
[da2bd08]42#include <assert.h>
[6071a8f]43#include <bool.h>
[36a75a2]44#include <tinput.h>
[216d6fc]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"
[6939edb]52#include "tok.h"
[216d6fc]53
[5db9084]54extern volatile unsigned int cli_quit;
55
[ed372da]56/** Text input field. */
[36a75a2]57static tinput_t *tinput;
[88944695]58
[6939edb]59/* Private helpers */
[659e9473]60static int run_command(char **, cliuser_t *);
61
[216d6fc]62/* Tokenizes input from console, sees if the first word is a built-in, if so
63 * invokes the built-in entry point (a[0]) passing all arguments in a[] to
64 * the handler */
[28ee877e]65int process_input(cliuser_t *usr)
[216d6fc]66{
67 char *cmd[WORD_MAX];
68 int rc = 0;
[6939edb]69 tokenizer_t tok;
[216d6fc]70
71 if (NULL == usr->line)
72 return CL_EFAIL;
73
[6939edb]74 rc = tok_init(&tok, usr->line, cmd, WORD_MAX);
75 if (rc != EOK) {
76 goto finit;
[216d6fc]77 }
[6939edb]78
79 rc = tok_tokenize(&tok);
80 if (rc != EOK) {
81 goto finit;
82 }
83
[659e9473]84 rc = run_command(cmd, usr);
[6939edb]85
86finit:
[216d6fc]87 if (NULL != usr->line) {
88 free(usr->line);
89 usr->line = (char *) NULL;
90 }
[6939edb]91 tok_fini(&tok);
[216d6fc]92
93 return rc;
94}
95
[659e9473]96int run_command(char **cmd, cliuser_t *usr)
97{
98 int id = 0;
99
100 /* We have rubbish */
101 if (NULL == cmd[0]) {
102 return CL_ENOENT;
103 }
104
105 /* Is it a builtin command ? */
106 if ((id = (is_builtin(cmd[0]))) > -1) {
107 return run_builtin(id, cmd, usr);
108 }
109
110 /* Is it a module ? */
111 if ((id = (is_module(cmd[0]))) > -1) {
112 return run_module(id, cmd);
113 }
114
115 /* See what try_exec thinks of it */
116 return try_exec(cmd[0], cmd);
117}
118
[216d6fc]119void get_input(cliuser_t *usr)
120{
[19528516]121 char *str;
[5db9084]122 int rc;
[79ae36dd]123
124 console_flush(tinput->console);
125 console_set_style(tinput->console, STYLE_EMPHASIS);
[216d6fc]126 printf("%s", usr->prompt);
[79ae36dd]127 console_flush(tinput->console);
128 console_set_style(tinput->console, STYLE_NORMAL);
[9805cde]129
[5db9084]130 rc = tinput_read(tinput, &str);
131 if (rc == ENOENT) {
132 /* User requested exit */
133 cli_quit = 1;
134 putchar('\n');
135 return;
136 }
137
138 if (rc != EOK) {
139 /* Error in communication with console */
140 return;
141 }
[19528516]142
143 /* Check for empty input. */
144 if (str_cmp(str, "") == 0) {
145 free(str);
[216d6fc]146 return;
[19528516]147 }
[216d6fc]148
[19528516]149 usr->line = str;
[216d6fc]150 return;
151}
[da2bd08]152
[36a75a2]153int input_init(void)
[da2bd08]154{
[36a75a2]155 tinput = tinput_new();
156 if (tinput == NULL) {
157 printf("Failed to initialize input.\n");
158 return 1;
159 }
160
161 return 0;
[da2bd08]162}
Note: See TracBrowser for help on using the repository browser.