source: mainline/uspace/app/bdsh/input.c@ 904b1bc

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

Fix block comment formatting (ccheck).

  • Property mode set to 100644
File size: 7.0 KB
Line 
1/*
2 * Copyright (c) 2008 Tim Post
3 * Copyright (c) 2011 Jiri Svoboda
4 * Copyright (c) 2011 Martin Sucha
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 *
11 * - Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * - Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * - The name of the author may not be used to endorse or promote products
17 * derived from this software without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28 * THIS SOFTWARE, EVEN IF ADVISED OF THE 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 <stdbool.h>
44#include <tinput.h>
45
46#include "config.h"
47#include "compl.h"
48#include "util.h"
49#include "scli.h"
50#include "input.h"
51#include "errors.h"
52#include "exec.h"
53#include "tok.h"
54
55extern volatile unsigned int cli_quit;
56
57/** Text input field. */
58static tinput_t *tinput;
59
60/* Private helpers */
61static int run_command(char **, cliuser_t *, iostate_t *);
62static void print_pipe_usage(void);
63
64/*
65 * Tokenizes input from console, sees if the first word is a built-in, if so
66 * invokes the built-in entry point (a[0]) passing all arguments in a[] to
67 * the handler
68 */
69errno_t process_input(cliuser_t *usr)
70{
71 token_t *tokens_buf = calloc(WORD_MAX, sizeof(token_t));
72 if (tokens_buf == NULL)
73 return ENOMEM;
74 token_t *tokens = tokens_buf;
75
76 char *cmd[WORD_MAX];
77 errno_t rc = EOK;
78 tokenizer_t tok;
79 unsigned int i, pipe_count, processed_pipes;
80 unsigned int pipe_pos[2];
81 char *redir_from = NULL;
82 char *redir_to = NULL;
83
84 if (usr->line == NULL) {
85 free(tokens_buf);
86 return EINVAL;
87 }
88
89 rc = tok_init(&tok, usr->line, tokens, WORD_MAX);
90 if (rc != EOK) {
91 goto finit;
92 }
93
94 size_t tokens_length;
95 rc = tok_tokenize(&tok, &tokens_length);
96 if (rc != EOK) {
97 goto finit;
98 }
99
100 if (tokens_length > 0 && tokens[0].type == TOKTYPE_SPACE) {
101 tokens++;
102 tokens_length--;
103 }
104
105 if (tokens_length > 0 && tokens[tokens_length - 1].type == TOKTYPE_SPACE) {
106 tokens_length--;
107 }
108
109 /*
110 * Until full support for pipes is implemented, allow for a simple case:
111 * [from <file> |] command [| to <file>]
112 *
113 * First find the pipes and check that there are no more
114 */
115 for (i = 0, pipe_count = 0; i < tokens_length; i++) {
116 if (tokens[i].type == TOKTYPE_PIPE) {
117 if (pipe_count >= 2) {
118 print_pipe_usage();
119 rc = ENOTSUP;
120 goto finit;
121 }
122 pipe_pos[pipe_count] = i;
123 pipe_count++;
124 }
125 }
126
127 unsigned int cmd_token_start = 0;
128 unsigned int cmd_token_end = tokens_length;
129
130 processed_pipes = 0;
131
132 /* Check if the first part (from <file> |) is present */
133 if (pipe_count > 0 && (pipe_pos[0] == 3 || pipe_pos[0] == 4) && str_cmp(tokens[0].text, "from") == 0) {
134 /* Ignore the first three tokens (from, file, pipe) and set from */
135 redir_from = tokens[2].text;
136 cmd_token_start = pipe_pos[0] + 1;
137 processed_pipes++;
138 }
139
140 /* Check if the second part (| to <file>) is present */
141 if ((pipe_count - processed_pipes) > 0 &&
142 (pipe_pos[processed_pipes] == tokens_length - 4 ||
143 (pipe_pos[processed_pipes] == tokens_length - 5 &&
144 tokens[tokens_length - 4].type == TOKTYPE_SPACE)) &&
145 str_cmp(tokens[tokens_length - 3].text, "to") == 0) {
146 /* Ignore the last three tokens (pipe, to, file) and set to */
147 redir_to = tokens[tokens_length - 1].text;
148 cmd_token_end = pipe_pos[processed_pipes];
149 processed_pipes++;
150 }
151
152 if (processed_pipes != pipe_count) {
153 print_pipe_usage();
154 rc = ENOTSUP;
155 goto finit;
156 }
157
158 /* Convert tokens of the command to string array */
159 unsigned int cmd_pos = 0;
160 for (i = cmd_token_start; i < cmd_token_end; i++) {
161 if (tokens[i].type != TOKTYPE_SPACE) {
162 cmd[cmd_pos++] = tokens[i].text;
163 }
164 }
165 cmd[cmd_pos++] = NULL;
166
167 if (cmd[0] == NULL) {
168 print_pipe_usage();
169 rc = ENOTSUP;
170 goto finit;
171 }
172
173 iostate_t new_iostate = {
174 .stdin = stdin,
175 .stdout = stdout,
176 .stderr = stderr
177 };
178
179 FILE *from = NULL;
180 FILE *to = NULL;
181
182 if (redir_from) {
183 from = fopen(redir_from, "r");
184 if (from == NULL) {
185 printf("Cannot open file %s\n", redir_from);
186 rc = errno;
187 goto finit_with_files;
188 }
189 new_iostate.stdin = from;
190 }
191
192
193 if (redir_to) {
194 to = fopen(redir_to, "w");
195 if (to == NULL) {
196 printf("Cannot open file %s\n", redir_to);
197 rc = errno;
198 goto finit_with_files;
199 }
200 new_iostate.stdout = to;
201 }
202
203 if (run_command(cmd, usr, &new_iostate) == 0) {
204 rc = EOK;
205 } else {
206 rc = EINVAL;
207 }
208
209finit_with_files:
210 if (from != NULL) {
211 fclose(from);
212 }
213 if (to != NULL) {
214 fclose(to);
215 }
216
217finit:
218 if (NULL != usr->line) {
219 free(usr->line);
220 usr->line = (char *) NULL;
221 }
222 tok_fini(&tok);
223 free(tokens_buf);
224
225 return rc;
226}
227
228void print_pipe_usage(void)
229{
230 printf("Invalid syntax!\n");
231 printf("Usage of redirection (pipes in the future):\n");
232 printf("from filename | command ...\n");
233 printf("from filename | command ... | to filename\n");
234 printf("command ... | to filename\n");
235
236}
237
238int run_command(char **cmd, cliuser_t *usr, iostate_t *new_iostate)
239{
240 int id = 0;
241
242 /* We have rubbish */
243 if (NULL == cmd[0]) {
244 return CL_ENOENT;
245 }
246
247 /* Is it a builtin command ? */
248 if ((id = (is_builtin(cmd[0]))) > -1) {
249 return run_builtin(id, cmd, usr, new_iostate);
250 }
251
252 /* Is it a module ? */
253 if ((id = (is_module(cmd[0]))) > -1) {
254 return run_module(id, cmd, new_iostate);
255 }
256
257 /* See what try_exec thinks of it */
258 return try_exec(cmd[0], cmd, new_iostate);
259}
260
261void get_input(cliuser_t *usr)
262{
263 char *str;
264 errno_t rc;
265
266 tinput_set_prompt(tinput, usr->prompt);
267
268 rc = tinput_read(tinput, &str);
269 if (rc == ENOENT) {
270 /* User requested exit */
271 cli_quit = 1;
272 putchar('\n');
273 return;
274 }
275
276 if (rc != EOK) {
277 /* Error in communication with console */
278 cli_quit = 1;
279 return;
280 }
281
282 /* Check for empty input. */
283 if (str_cmp(str, "") == 0) {
284 free(str);
285 return;
286 }
287
288 usr->line = str;
289 return;
290}
291
292int input_init(void)
293{
294 tinput = tinput_new();
295 if (tinput == NULL) {
296 printf("Failed to initialize input.\n");
297 return 1;
298 }
299
300 tinput_set_compl_ops(tinput, &compl_ops);
301
302 return 0;
303}
Note: See TracBrowser for help on using the repository browser.