source: mainline/uspace/app/bdsh/input.c@ 09ab0a9a

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

Fix vertical spacing with new Ccheck revision.

  • Property mode set to 100644
File size: 7.0 KB
RevLine 
[36ab7c7]1/*
2 * Copyright (c) 2008 Tim Post
[9be9c4d]3 * Copyright (c) 2011 Jiri Svoboda
[b2727f18]4 * Copyright (c) 2011 Martin Sucha
[216d6fc]5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
[36ab7c7]8 * modification, are permitted provided that the following conditions
9 * are met:
[216d6fc]10 *
[36ab7c7]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.
[216d6fc]18 *
[36ab7c7]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.
[216d6fc]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>
[3e6a98c5]43#include <stdbool.h>
[36a75a2]44#include <tinput.h>
[216d6fc]45
46#include "config.h"
[9be9c4d]47#include "compl.h"
[216d6fc]48#include "util.h"
49#include "scli.h"
50#include "input.h"
51#include "errors.h"
52#include "exec.h"
[6939edb]53#include "tok.h"
[216d6fc]54
[5db9084]55extern volatile unsigned int cli_quit;
56
[ed372da]57/** Text input field. */
[36a75a2]58static tinput_t *tinput;
[88944695]59
[6939edb]60/* Private helpers */
[6ea9a1d]61static int run_command(char **, cliuser_t *, iostate_t *);
[ae45201]62static void print_pipe_usage(void);
[659e9473]63
[7c3fb9b]64/*
65 * Tokenizes input from console, sees if the first word is a built-in, if so
[216d6fc]66 * invokes the built-in entry point (a[0]) passing all arguments in a[] to
[7c3fb9b]67 * the handler
68 */
[b7fd2a0]69errno_t process_input(cliuser_t *usr)
[216d6fc]70{
[01e397ac]71 token_t *tokens_buf = calloc(WORD_MAX, sizeof(token_t));
72 if (tokens_buf == NULL)
[b9ae539]73 return ENOMEM;
[01e397ac]74 token_t *tokens = tokens_buf;
[a35b458]75
[216d6fc]76 char *cmd[WORD_MAX];
[b7fd2a0]77 errno_t rc = EOK;
[6939edb]78 tokenizer_t tok;
[0662451]79 unsigned int i, pipe_count, processed_pipes;
80 unsigned int pipe_pos[2];
[ae45201]81 char *redir_from = NULL;
82 char *redir_to = NULL;
[216d6fc]83
[b48d046]84 if (usr->line == NULL) {
[01e397ac]85 free(tokens_buf);
[1569a9b]86 return EINVAL;
[b9ae539]87 }
[216d6fc]88
[0662451]89 rc = tok_init(&tok, usr->line, tokens, WORD_MAX);
[6939edb]90 if (rc != EOK) {
91 goto finit;
[216d6fc]92 }
[a35b458]93
[0662451]94 size_t tokens_length;
95 rc = tok_tokenize(&tok, &tokens_length);
[6939edb]96 if (rc != EOK) {
97 goto finit;
98 }
[a35b458]99
[0662451]100 if (tokens_length > 0 && tokens[0].type == TOKTYPE_SPACE) {
101 tokens++;
102 tokens_length--;
103 }
[a35b458]104
[1433ecda]105 if (tokens_length > 0 && tokens[tokens_length - 1].type == TOKTYPE_SPACE) {
[0662451]106 tokens_length--;
107 }
[a35b458]108
[7c3fb9b]109 /*
110 * Until full support for pipes is implemented, allow for a simple case:
[ae45201]111 * [from <file> |] command [| to <file>]
[1b20da0]112 *
[ae45201]113 * First find the pipes and check that there are no more
114 */
[0662451]115 for (i = 0, pipe_count = 0; i < tokens_length; i++) {
116 if (tokens[i].type == TOKTYPE_PIPE) {
[ae45201]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 }
[a35b458]126
[0662451]127 unsigned int cmd_token_start = 0;
128 unsigned int cmd_token_end = tokens_length;
[a35b458]129
[ae45201]130 processed_pipes = 0;
[a35b458]131
[ae45201]132 /* Check if the first part (from <file> |) is present */
[0662451]133 if (pipe_count > 0 && (pipe_pos[0] == 3 || pipe_pos[0] == 4) && str_cmp(tokens[0].text, "from") == 0) {
[ae45201]134 /* Ignore the first three tokens (from, file, pipe) and set from */
[0662451]135 redir_from = tokens[2].text;
[1433ecda]136 cmd_token_start = pipe_pos[0] + 1;
[ae45201]137 processed_pipes++;
138 }
[a35b458]139
[ae45201]140 /* Check if the second part (| to <file>) is present */
141 if ((pipe_count - processed_pipes) > 0 &&
[0662451]142 (pipe_pos[processed_pipes] == tokens_length - 4 ||
143 (pipe_pos[processed_pipes] == tokens_length - 5 &&
[1433ecda]144 tokens[tokens_length - 4].type == TOKTYPE_SPACE)) &&
145 str_cmp(tokens[tokens_length - 3].text, "to") == 0) {
[ae45201]146 /* Ignore the last three tokens (pipe, to, file) and set to */
[1433ecda]147 redir_to = tokens[tokens_length - 1].text;
[0662451]148 cmd_token_end = pipe_pos[processed_pipes];
[ae45201]149 processed_pipes++;
150 }
[a35b458]151
[ae45201]152 if (processed_pipes != pipe_count) {
153 print_pipe_usage();
154 rc = ENOTSUP;
155 goto finit;
156 }
[a35b458]157
[0662451]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;
[a35b458]166
[0662451]167 if (cmd[0] == NULL) {
[ae45201]168 print_pipe_usage();
169 rc = ENOTSUP;
170 goto finit;
171 }
[a35b458]172
[6ea9a1d]173 iostate_t new_iostate = {
174 .stdin = stdin,
175 .stdout = stdout,
176 .stderr = stderr
177 };
[a35b458]178
[6ea9a1d]179 FILE *from = NULL;
180 FILE *to = NULL;
[a35b458]181
[ae45201]182 if (redir_from) {
[6ea9a1d]183 from = fopen(redir_from, "r");
[ae45201]184 if (from == NULL) {
185 printf("Cannot open file %s\n", redir_from);
186 rc = errno;
[6ea9a1d]187 goto finit_with_files;
[ae45201]188 }
[6ea9a1d]189 new_iostate.stdin = from;
[ae45201]190 }
[a35b458]191
[ae45201]192 if (redir_to) {
[6ea9a1d]193 to = fopen(redir_to, "w");
[ae45201]194 if (to == NULL) {
195 printf("Cannot open file %s\n", redir_to);
196 rc = errno;
[6ea9a1d]197 goto finit_with_files;
[ae45201]198 }
[6ea9a1d]199 new_iostate.stdout = to;
[ae45201]200 }
[10f4b47c]201
[1569a9b]202 if (run_command(cmd, usr, &new_iostate) == 0) {
203 rc = EOK;
204 } else {
205 rc = EINVAL;
206 }
[a35b458]207
[6ea9a1d]208finit_with_files:
209 if (from != NULL) {
210 fclose(from);
211 }
212 if (to != NULL) {
213 fclose(to);
214 }
[a35b458]215
[6939edb]216finit:
[216d6fc]217 if (NULL != usr->line) {
218 free(usr->line);
219 usr->line = (char *) NULL;
220 }
[6939edb]221 tok_fini(&tok);
[01e397ac]222 free(tokens_buf);
[216d6fc]223
224 return rc;
225}
226
[193d280c]227void print_pipe_usage(void)
[ae45201]228{
229 printf("Invalid syntax!\n");
230 printf("Usage of redirection (pipes in the future):\n");
231 printf("from filename | command ...\n");
232 printf("from filename | command ... | to filename\n");
233 printf("command ... | to filename\n");
[a35b458]234
[ae45201]235}
236
[6ea9a1d]237int run_command(char **cmd, cliuser_t *usr, iostate_t *new_iostate)
[659e9473]238{
239 int id = 0;
[a35b458]240
[659e9473]241 /* We have rubbish */
242 if (NULL == cmd[0]) {
243 return CL_ENOENT;
244 }
[a35b458]245
[659e9473]246 /* Is it a builtin command ? */
247 if ((id = (is_builtin(cmd[0]))) > -1) {
[6ea9a1d]248 return run_builtin(id, cmd, usr, new_iostate);
[659e9473]249 }
[a35b458]250
[659e9473]251 /* Is it a module ? */
252 if ((id = (is_module(cmd[0]))) > -1) {
[6ea9a1d]253 return run_module(id, cmd, new_iostate);
[659e9473]254 }
255
256 /* See what try_exec thinks of it */
[6ea9a1d]257 return try_exec(cmd[0], cmd, new_iostate);
[659e9473]258}
259
[216d6fc]260void get_input(cliuser_t *usr)
261{
[19528516]262 char *str;
[b7fd2a0]263 errno_t rc;
[a35b458]264
[9be9c4d]265 tinput_set_prompt(tinput, usr->prompt);
[9805cde]266
[5db9084]267 rc = tinput_read(tinput, &str);
268 if (rc == ENOENT) {
269 /* User requested exit */
270 cli_quit = 1;
271 putchar('\n');
272 return;
273 }
274
275 if (rc != EOK) {
276 /* Error in communication with console */
[6d5e378]277 cli_quit = 1;
[5db9084]278 return;
279 }
[19528516]280
281 /* Check for empty input. */
282 if (str_cmp(str, "") == 0) {
283 free(str);
[216d6fc]284 return;
[19528516]285 }
[216d6fc]286
[19528516]287 usr->line = str;
[216d6fc]288 return;
289}
[da2bd08]290
[36a75a2]291int input_init(void)
[da2bd08]292{
[36a75a2]293 tinput = tinput_new();
294 if (tinput == NULL) {
295 printf("Failed to initialize input.\n");
296 return 1;
297 }
298
[9be9c4d]299 tinput_set_compl_ops(tinput, &compl_ops);
300
[36a75a2]301 return 0;
[da2bd08]302}
Note: See TracBrowser for help on using the repository browser.