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