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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since b48d046 was b48d046, checked in by Martin Decky <martin@…>, 14 years ago

cstyle
(no change in functionality)

  • Property mode set to 100644
File size: 6.9 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>
[6071a8f]43#include <bool.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
[216d6fc]64/* Tokenizes input from console, sees if the first word is a built-in, if so
65 * invokes the built-in entry point (a[0]) passing all arguments in a[] to
66 * the handler */
[28ee877e]67int process_input(cliuser_t *usr)
[216d6fc]68{
[b9ae539]69 token_t *tokens = calloc(WORD_MAX, sizeof(token_t));
70 if (tokens == NULL)
71 return ENOMEM;
[b48d046]72
73 char *cmd[WORD_MAX];
[216d6fc]74 int rc = 0;
[6939edb]75 tokenizer_t tok;
[0662451]76 unsigned int i, pipe_count, processed_pipes;
77 unsigned int pipe_pos[2];
[ae45201]78 char *redir_from = NULL;
79 char *redir_to = NULL;
[216d6fc]80
[b48d046]81 if (usr->line == NULL) {
[b9ae539]82 free(tokens);
[216d6fc]83 return CL_EFAIL;
[b9ae539]84 }
[216d6fc]85
[0662451]86 rc = tok_init(&tok, usr->line, tokens, WORD_MAX);
[6939edb]87 if (rc != EOK) {
88 goto finit;
[216d6fc]89 }
[6939edb]90
[0662451]91 size_t tokens_length;
92 rc = tok_tokenize(&tok, &tokens_length);
[6939edb]93 if (rc != EOK) {
94 goto finit;
95 }
96
[0662451]97 if (tokens_length > 0 && tokens[0].type == TOKTYPE_SPACE) {
98 tokens++;
99 tokens_length--;
100 }
101
102 if (tokens_length > 0 && tokens[tokens_length-1].type == TOKTYPE_SPACE) {
103 tokens_length--;
104 }
105
[ae45201]106 /* Until full support for pipes is implemented, allow for a simple case:
107 * [from <file> |] command [| to <file>]
108 *
109 * First find the pipes and check that there are no more
110 */
[0662451]111 for (i = 0, pipe_count = 0; i < tokens_length; i++) {
112 if (tokens[i].type == TOKTYPE_PIPE) {
[ae45201]113 if (pipe_count >= 2) {
114 print_pipe_usage();
115 rc = ENOTSUP;
116 goto finit;
117 }
118 pipe_pos[pipe_count] = i;
119 pipe_count++;
120 }
121 }
122
[0662451]123 unsigned int cmd_token_start = 0;
124 unsigned int cmd_token_end = tokens_length;
125
[ae45201]126 processed_pipes = 0;
127
128 /* Check if the first part (from <file> |) is present */
[0662451]129 if (pipe_count > 0 && (pipe_pos[0] == 3 || pipe_pos[0] == 4) && str_cmp(tokens[0].text, "from") == 0) {
[ae45201]130 /* Ignore the first three tokens (from, file, pipe) and set from */
[0662451]131 redir_from = tokens[2].text;
132 cmd_token_start = pipe_pos[0]+1;
[ae45201]133 processed_pipes++;
134 }
135
136 /* Check if the second part (| to <file>) is present */
137 if ((pipe_count - processed_pipes) > 0 &&
[0662451]138 (pipe_pos[processed_pipes] == tokens_length - 4 ||
139 (pipe_pos[processed_pipes] == tokens_length - 5 &&
140 tokens[tokens_length-4].type == TOKTYPE_SPACE )) &&
141 str_cmp(tokens[tokens_length-3].text, "to") == 0) {
[ae45201]142 /* Ignore the last three tokens (pipe, to, file) and set to */
[0662451]143 redir_to = tokens[tokens_length-1].text;
144 cmd_token_end = pipe_pos[processed_pipes];
[ae45201]145 processed_pipes++;
146 }
147
148 if (processed_pipes != pipe_count) {
149 print_pipe_usage();
150 rc = ENOTSUP;
151 goto finit;
152 }
153
[0662451]154 /* Convert tokens of the command to string array */
155 unsigned int cmd_pos = 0;
156 for (i = cmd_token_start; i < cmd_token_end; i++) {
157 if (tokens[i].type != TOKTYPE_SPACE) {
158 cmd[cmd_pos++] = tokens[i].text;
159 }
160 }
161 cmd[cmd_pos++] = NULL;
162
163 if (cmd[0] == NULL) {
[ae45201]164 print_pipe_usage();
165 rc = ENOTSUP;
166 goto finit;
167 }
168
[6ea9a1d]169 iostate_t new_iostate = {
170 .stdin = stdin,
171 .stdout = stdout,
172 .stderr = stderr
173 };
174
175 FILE *from = NULL;
176 FILE *to = NULL;
[ae45201]177
178 if (redir_from) {
[6ea9a1d]179 from = fopen(redir_from, "r");
[ae45201]180 if (from == NULL) {
181 printf("Cannot open file %s\n", redir_from);
182 rc = errno;
[6ea9a1d]183 goto finit_with_files;
[ae45201]184 }
[6ea9a1d]185 new_iostate.stdin = from;
[ae45201]186 }
187
[6ea9a1d]188
[ae45201]189 if (redir_to) {
[6ea9a1d]190 to = fopen(redir_to, "w");
[ae45201]191 if (to == NULL) {
192 printf("Cannot open file %s\n", redir_to);
193 rc = errno;
[6ea9a1d]194 goto finit_with_files;
[ae45201]195 }
[6ea9a1d]196 new_iostate.stdout = to;
[ae45201]197 }
198
[0662451]199 rc = run_command(cmd, usr, &new_iostate);
[6ea9a1d]200
201finit_with_files:
202 if (from != NULL) {
203 fclose(from);
204 }
205 if (to != NULL) {
206 fclose(to);
207 }
[6939edb]208
209finit:
[216d6fc]210 if (NULL != usr->line) {
211 free(usr->line);
212 usr->line = (char *) NULL;
213 }
[6939edb]214 tok_fini(&tok);
[b9ae539]215 free(tokens);
[216d6fc]216
217 return rc;
218}
219
[ae45201]220void print_pipe_usage()
221{
222 printf("Invalid syntax!\n");
223 printf("Usage of redirection (pipes in the future):\n");
224 printf("from filename | command ...\n");
225 printf("from filename | command ... | to filename\n");
226 printf("command ... | to filename\n");
227
228}
229
[6ea9a1d]230int run_command(char **cmd, cliuser_t *usr, iostate_t *new_iostate)
[659e9473]231{
232 int id = 0;
233
234 /* We have rubbish */
235 if (NULL == cmd[0]) {
236 return CL_ENOENT;
237 }
238
239 /* Is it a builtin command ? */
240 if ((id = (is_builtin(cmd[0]))) > -1) {
[6ea9a1d]241 return run_builtin(id, cmd, usr, new_iostate);
[659e9473]242 }
243
244 /* Is it a module ? */
245 if ((id = (is_module(cmd[0]))) > -1) {
[6ea9a1d]246 return run_module(id, cmd, new_iostate);
[659e9473]247 }
248
249 /* See what try_exec thinks of it */
[6ea9a1d]250 return try_exec(cmd[0], cmd, new_iostate);
[659e9473]251}
252
[216d6fc]253void get_input(cliuser_t *usr)
254{
[19528516]255 char *str;
[5db9084]256 int rc;
[79ae36dd]257
[9be9c4d]258 tinput_set_prompt(tinput, usr->prompt);
[9805cde]259
[5db9084]260 rc = tinput_read(tinput, &str);
261 if (rc == ENOENT) {
262 /* User requested exit */
263 cli_quit = 1;
264 putchar('\n');
265 return;
266 }
267
268 if (rc != EOK) {
269 /* Error in communication with console */
270 return;
271 }
[19528516]272
273 /* Check for empty input. */
274 if (str_cmp(str, "") == 0) {
275 free(str);
[216d6fc]276 return;
[19528516]277 }
[216d6fc]278
[19528516]279 usr->line = str;
[216d6fc]280 return;
281}
[da2bd08]282
[36a75a2]283int input_init(void)
[da2bd08]284{
[36a75a2]285 tinput = tinput_new();
286 if (tinput == NULL) {
287 printf("Failed to initialize input.\n");
288 return 1;
289 }
290
[9be9c4d]291 tinput_set_compl_ops(tinput, &compl_ops);
292
[36a75a2]293 return 0;
[da2bd08]294}
Note: See TracBrowser for help on using the repository browser.