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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since a02aa5c was a02aa5c, checked in by Matthieu Riolo <matthieu.riolo@…>, 7 years ago

correcting formatting according to ccheck.sh

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