source: mainline/uspace/app/bdsh/input.c@ 81bc309

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 81bc309 was 81bc309, checked in by Petr Koupy <petr.koupy@…>, 14 years ago

batch command rewritten as builtin command.

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