| [36ab7c7] | 1 | /*
|
|---|
| 2 | * Copyright (c) 2008 Tim Post
|
|---|
| [9be9c4d] | 3 | * Copyright (c) 2011 Jiri Svoboda
|
|---|
| [b2727f18] | 4 | * Copyright (c) 2011 Martin Sucha
|
|---|
| [63087f1d] | 5 | * Copyright (c) 2018 Matthieu Riolo
|
|---|
| [216d6fc] | 6 | * All rights reserved.
|
|---|
| 7 | *
|
|---|
| 8 | * Redistribution and use in source and binary forms, with or without
|
|---|
| [36ab7c7] | 9 | * modification, are permitted provided that the following conditions
|
|---|
| 10 | * are met:
|
|---|
| [216d6fc] | 11 | *
|
|---|
| [36ab7c7] | 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.
|
|---|
| [216d6fc] | 19 | *
|
|---|
| [36ab7c7] | 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.
|
|---|
| [216d6fc] | 30 | */
|
|---|
| 31 |
|
|---|
| 32 | #include <stdio.h>
|
|---|
| 33 | #include <stdlib.h>
|
|---|
| [19f857a] | 34 | #include <str.h>
|
|---|
| [73878c1] | 35 | #include <io/console.h>
|
|---|
| 36 | #include <io/keycode.h>
|
|---|
| 37 | #include <io/style.h>
|
|---|
| [7e0cb78] | 38 | #include <io/color.h>
|
|---|
| [3bf907a] | 39 | #include <vfs/vfs.h>
|
|---|
| [371a012] | 40 | #include <clipboard.h>
|
|---|
| [f1b37d6] | 41 | #include <macros.h>
|
|---|
| [6071a8f] | 42 | #include <errno.h>
|
|---|
| [da2bd08] | 43 | #include <assert.h>
|
|---|
| [3e6a98c5] | 44 | #include <stdbool.h>
|
|---|
| [36a75a2] | 45 | #include <tinput.h>
|
|---|
| [55e35a22] | 46 | #include <adt/odict.h>
|
|---|
| [216d6fc] | 47 |
|
|---|
| 48 | #include "config.h"
|
|---|
| [9be9c4d] | 49 | #include "compl.h"
|
|---|
| [216d6fc] | 50 | #include "util.h"
|
|---|
| 51 | #include "scli.h"
|
|---|
| 52 | #include "input.h"
|
|---|
| 53 | #include "errors.h"
|
|---|
| 54 | #include "exec.h"
|
|---|
| [6939edb] | 55 | #include "tok.h"
|
|---|
| [216d6fc] | 56 |
|
|---|
| [5db9084] | 57 | extern volatile unsigned int cli_quit;
|
|---|
| 58 |
|
|---|
| [ed372da] | 59 | /** Text input field. */
|
|---|
| [36a75a2] | 60 | static tinput_t *tinput;
|
|---|
| [88944695] | 61 |
|
|---|
| [6939edb] | 62 | /* Private helpers */
|
|---|
| [6ea9a1d] | 63 | static int run_command(char **, cliuser_t *, iostate_t *);
|
|---|
| [ae45201] | 64 | static void print_pipe_usage(void);
|
|---|
| [659e9473] | 65 |
|
|---|
| [7c3fb9b] | 66 | /*
|
|---|
| 67 | * Tokenizes input from console, sees if the first word is a built-in, if so
|
|---|
| [216d6fc] | 68 | * invokes the built-in entry point (a[0]) passing all arguments in a[] to
|
|---|
| [7c3fb9b] | 69 | * the handler
|
|---|
| 70 | */
|
|---|
| [b7fd2a0] | 71 | errno_t process_input(cliuser_t *usr)
|
|---|
| [216d6fc] | 72 | {
|
|---|
| [01e397ac] | 73 | token_t *tokens_buf = calloc(WORD_MAX, sizeof(token_t));
|
|---|
| 74 | if (tokens_buf == NULL)
|
|---|
| [b9ae539] | 75 | return ENOMEM;
|
|---|
| [01e397ac] | 76 | token_t *tokens = tokens_buf;
|
|---|
| [a35b458] | 77 |
|
|---|
| [216d6fc] | 78 | char *cmd[WORD_MAX];
|
|---|
| [b7fd2a0] | 79 | errno_t rc = EOK;
|
|---|
| [6939edb] | 80 | tokenizer_t tok;
|
|---|
| [0662451] | 81 | unsigned int i, pipe_count, processed_pipes;
|
|---|
| 82 | unsigned int pipe_pos[2];
|
|---|
| [ae45201] | 83 | char *redir_from = NULL;
|
|---|
| 84 | char *redir_to = NULL;
|
|---|
| [216d6fc] | 85 |
|
|---|
| [b48d046] | 86 | if (usr->line == NULL) {
|
|---|
| [01e397ac] | 87 | free(tokens_buf);
|
|---|
| [1569a9b] | 88 | return EINVAL;
|
|---|
| [b9ae539] | 89 | }
|
|---|
| [216d6fc] | 90 |
|
|---|
| [0662451] | 91 | rc = tok_init(&tok, usr->line, tokens, WORD_MAX);
|
|---|
| [6939edb] | 92 | if (rc != EOK) {
|
|---|
| 93 | goto finit;
|
|---|
| [216d6fc] | 94 | }
|
|---|
| [a35b458] | 95 |
|
|---|
| [0662451] | 96 | size_t tokens_length;
|
|---|
| 97 | rc = tok_tokenize(&tok, &tokens_length);
|
|---|
| [6939edb] | 98 | if (rc != EOK) {
|
|---|
| 99 | goto finit;
|
|---|
| 100 | }
|
|---|
| [a35b458] | 101 |
|
|---|
| [0662451] | 102 | if (tokens_length > 0 && tokens[0].type == TOKTYPE_SPACE) {
|
|---|
| 103 | tokens++;
|
|---|
| 104 | tokens_length--;
|
|---|
| 105 | }
|
|---|
| [a35b458] | 106 |
|
|---|
| [1433ecda] | 107 | if (tokens_length > 0 && tokens[tokens_length - 1].type == TOKTYPE_SPACE) {
|
|---|
| [0662451] | 108 | tokens_length--;
|
|---|
| 109 | }
|
|---|
| [a35b458] | 110 |
|
|---|
| [7c3fb9b] | 111 | /*
|
|---|
| 112 | * Until full support for pipes is implemented, allow for a simple case:
|
|---|
| [ae45201] | 113 | * [from <file> |] command [| to <file>]
|
|---|
| [1b20da0] | 114 | *
|
|---|
| [ae45201] | 115 | * First find the pipes and check that there are no more
|
|---|
| 116 | */
|
|---|
| [0662451] | 117 | for (i = 0, pipe_count = 0; i < tokens_length; i++) {
|
|---|
| 118 | if (tokens[i].type == TOKTYPE_PIPE) {
|
|---|
| [ae45201] | 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 | }
|
|---|
| [a35b458] | 128 |
|
|---|
| [0662451] | 129 | unsigned int cmd_token_start = 0;
|
|---|
| 130 | unsigned int cmd_token_end = tokens_length;
|
|---|
| [a35b458] | 131 |
|
|---|
| [ae45201] | 132 | processed_pipes = 0;
|
|---|
| [a35b458] | 133 |
|
|---|
| [ae45201] | 134 | /* Check if the first part (from <file> |) is present */
|
|---|
| [0662451] | 135 | if (pipe_count > 0 && (pipe_pos[0] == 3 || pipe_pos[0] == 4) && str_cmp(tokens[0].text, "from") == 0) {
|
|---|
| [ae45201] | 136 | /* Ignore the first three tokens (from, file, pipe) and set from */
|
|---|
| [0662451] | 137 | redir_from = tokens[2].text;
|
|---|
| [1433ecda] | 138 | cmd_token_start = pipe_pos[0] + 1;
|
|---|
| [ae45201] | 139 | processed_pipes++;
|
|---|
| 140 | }
|
|---|
| [a35b458] | 141 |
|
|---|
| [ae45201] | 142 | /* Check if the second part (| to <file>) is present */
|
|---|
| 143 | if ((pipe_count - processed_pipes) > 0 &&
|
|---|
| [0662451] | 144 | (pipe_pos[processed_pipes] == tokens_length - 4 ||
|
|---|
| 145 | (pipe_pos[processed_pipes] == tokens_length - 5 &&
|
|---|
| [1433ecda] | 146 | tokens[tokens_length - 4].type == TOKTYPE_SPACE)) &&
|
|---|
| 147 | str_cmp(tokens[tokens_length - 3].text, "to") == 0) {
|
|---|
| [ae45201] | 148 | /* Ignore the last three tokens (pipe, to, file) and set to */
|
|---|
| [1433ecda] | 149 | redir_to = tokens[tokens_length - 1].text;
|
|---|
| [0662451] | 150 | cmd_token_end = pipe_pos[processed_pipes];
|
|---|
| [ae45201] | 151 | processed_pipes++;
|
|---|
| 152 | }
|
|---|
| [a35b458] | 153 |
|
|---|
| [ae45201] | 154 | if (processed_pipes != pipe_count) {
|
|---|
| 155 | print_pipe_usage();
|
|---|
| 156 | rc = ENOTSUP;
|
|---|
| 157 | goto finit;
|
|---|
| 158 | }
|
|---|
| [a35b458] | 159 |
|
|---|
| [0662451] | 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;
|
|---|
| [a35b458] | 168 |
|
|---|
| [0662451] | 169 | if (cmd[0] == NULL) {
|
|---|
| [ae45201] | 170 | print_pipe_usage();
|
|---|
| 171 | rc = ENOTSUP;
|
|---|
| 172 | goto finit;
|
|---|
| 173 | }
|
|---|
| [a35b458] | 174 |
|
|---|
| [4bf08aa5] | 175 |
|
|---|
| 176 |
|
|---|
| [55e35a22] | 177 | /* test if the passed cmd is an alias */
|
|---|
| 178 | odlink_t* alias_link = odict_find_eq(&alias_dict, (void*)cmd[0], NULL);
|
|---|
| 179 | if (alias_link != NULL) {
|
|---|
| 180 | alias_t* data = odict_get_instance(alias_link, alias_t, odict);
|
|---|
| [4bf08aa5] | 181 | char* oldLine = usr->line;
|
|---|
| [55e35a22] | 182 |
|
|---|
| 183 |
|
|---|
| 184 | const size_t input_length = str_size(usr->line) - str_size(cmd[0]) + str_size(data->value) + 1;
|
|---|
| [b29c083] | 185 | usr->line = (char*)malloc(input_length * sizeof(char));
|
|---|
| [55e35a22] | 186 | usr->line[0] = '\0';
|
|---|
| 187 |
|
|---|
| 188 | unsigned int cmd_replace_index = cmd_token_start;
|
|---|
| 189 | for (i = 0; i < tokens_length; i++) {
|
|---|
| [d1744fd] | 190 | if (i == cmd_replace_index) {
|
|---|
| [55e35a22] | 191 | //if there is a pipe symbol than cmd_token_start will point at the SPACE after the pipe symbol
|
|---|
| [d1744fd] | 192 | if (tokens[i].type == TOKTYPE_SPACE) {
|
|---|
| [55e35a22] | 193 | cmd_replace_index++;
|
|---|
| 194 | str_append(usr->line, input_length, tokens[i].text);
|
|---|
| 195 | continue;
|
|---|
| 196 | }
|
|---|
| 197 |
|
|---|
| 198 | str_append(usr->line, input_length, data->value);
|
|---|
| [d1744fd] | 199 | } else {
|
|---|
| [55e35a22] | 200 | str_append(usr->line, input_length, tokens[i].text);
|
|---|
| 201 | }
|
|---|
| 202 | }
|
|---|
| [4bf08aa5] | 203 |
|
|---|
| 204 | //reprocess input after string replace
|
|---|
| 205 | rc = process_input(usr);
|
|---|
| 206 | usr->line = oldLine;
|
|---|
| 207 | goto finit;
|
|---|
| 208 | }
|
|---|
| 209 |
|
|---|
| 210 |
|
|---|
| [6ea9a1d] | 211 | iostate_t new_iostate = {
|
|---|
| 212 | .stdin = stdin,
|
|---|
| 213 | .stdout = stdout,
|
|---|
| 214 | .stderr = stderr
|
|---|
| 215 | };
|
|---|
| [a35b458] | 216 |
|
|---|
| [6ea9a1d] | 217 | FILE *from = NULL;
|
|---|
| 218 | FILE *to = NULL;
|
|---|
| [a35b458] | 219 |
|
|---|
| [ae45201] | 220 | if (redir_from) {
|
|---|
| [6ea9a1d] | 221 | from = fopen(redir_from, "r");
|
|---|
| [ae45201] | 222 | if (from == NULL) {
|
|---|
| 223 | printf("Cannot open file %s\n", redir_from);
|
|---|
| 224 | rc = errno;
|
|---|
| [6ea9a1d] | 225 | goto finit_with_files;
|
|---|
| [ae45201] | 226 | }
|
|---|
| [6ea9a1d] | 227 | new_iostate.stdin = from;
|
|---|
| [ae45201] | 228 | }
|
|---|
| [a35b458] | 229 |
|
|---|
| [ae45201] | 230 | if (redir_to) {
|
|---|
| [6ea9a1d] | 231 | to = fopen(redir_to, "w");
|
|---|
| [ae45201] | 232 | if (to == NULL) {
|
|---|
| 233 | printf("Cannot open file %s\n", redir_to);
|
|---|
| 234 | rc = errno;
|
|---|
| [6ea9a1d] | 235 | goto finit_with_files;
|
|---|
| [ae45201] | 236 | }
|
|---|
| [6ea9a1d] | 237 | new_iostate.stdout = to;
|
|---|
| [ae45201] | 238 | }
|
|---|
| [10f4b47c] | 239 |
|
|---|
| [1569a9b] | 240 | if (run_command(cmd, usr, &new_iostate) == 0) {
|
|---|
| 241 | rc = EOK;
|
|---|
| 242 | } else {
|
|---|
| 243 | rc = EINVAL;
|
|---|
| 244 | }
|
|---|
| [a35b458] | 245 |
|
|---|
| [6ea9a1d] | 246 | finit_with_files:
|
|---|
| 247 | if (from != NULL) {
|
|---|
| 248 | fclose(from);
|
|---|
| 249 | }
|
|---|
| 250 | if (to != NULL) {
|
|---|
| 251 | fclose(to);
|
|---|
| 252 | }
|
|---|
| [a35b458] | 253 |
|
|---|
| [6939edb] | 254 | finit:
|
|---|
| [216d6fc] | 255 | if (NULL != usr->line) {
|
|---|
| 256 | free(usr->line);
|
|---|
| 257 | usr->line = (char *) NULL;
|
|---|
| 258 | }
|
|---|
| [6939edb] | 259 | tok_fini(&tok);
|
|---|
| [01e397ac] | 260 | free(tokens_buf);
|
|---|
| [216d6fc] | 261 |
|
|---|
| 262 | return rc;
|
|---|
| 263 | }
|
|---|
| 264 |
|
|---|
| [193d280c] | 265 | void print_pipe_usage(void)
|
|---|
| [ae45201] | 266 | {
|
|---|
| 267 | printf("Invalid syntax!\n");
|
|---|
| 268 | printf("Usage of redirection (pipes in the future):\n");
|
|---|
| 269 | printf("from filename | command ...\n");
|
|---|
| 270 | printf("from filename | command ... | to filename\n");
|
|---|
| 271 | printf("command ... | to filename\n");
|
|---|
| [a35b458] | 272 |
|
|---|
| [ae45201] | 273 | }
|
|---|
| 274 |
|
|---|
| [6ea9a1d] | 275 | int run_command(char **cmd, cliuser_t *usr, iostate_t *new_iostate)
|
|---|
| [659e9473] | 276 | {
|
|---|
| 277 | int id = 0;
|
|---|
| [a35b458] | 278 |
|
|---|
| [659e9473] | 279 | /* We have rubbish */
|
|---|
| 280 | if (NULL == cmd[0]) {
|
|---|
| 281 | return CL_ENOENT;
|
|---|
| 282 | }
|
|---|
| [a35b458] | 283 |
|
|---|
| [659e9473] | 284 | /* Is it a builtin command ? */
|
|---|
| 285 | if ((id = (is_builtin(cmd[0]))) > -1) {
|
|---|
| [6ea9a1d] | 286 | return run_builtin(id, cmd, usr, new_iostate);
|
|---|
| [659e9473] | 287 | }
|
|---|
| [a35b458] | 288 |
|
|---|
| [659e9473] | 289 | /* Is it a module ? */
|
|---|
| 290 | if ((id = (is_module(cmd[0]))) > -1) {
|
|---|
| [6ea9a1d] | 291 | return run_module(id, cmd, new_iostate);
|
|---|
| [659e9473] | 292 | }
|
|---|
| 293 |
|
|---|
| 294 | /* See what try_exec thinks of it */
|
|---|
| [6ea9a1d] | 295 | return try_exec(cmd[0], cmd, new_iostate);
|
|---|
| [659e9473] | 296 | }
|
|---|
| 297 |
|
|---|
| [216d6fc] | 298 | void get_input(cliuser_t *usr)
|
|---|
| 299 | {
|
|---|
| [19528516] | 300 | char *str;
|
|---|
| [b7fd2a0] | 301 | errno_t rc;
|
|---|
| [a35b458] | 302 |
|
|---|
| [9be9c4d] | 303 | tinput_set_prompt(tinput, usr->prompt);
|
|---|
| [9805cde] | 304 |
|
|---|
| [5db9084] | 305 | rc = tinput_read(tinput, &str);
|
|---|
| 306 | if (rc == ENOENT) {
|
|---|
| 307 | /* User requested exit */
|
|---|
| 308 | cli_quit = 1;
|
|---|
| 309 | putchar('\n');
|
|---|
| 310 | return;
|
|---|
| 311 | }
|
|---|
| 312 |
|
|---|
| 313 | if (rc != EOK) {
|
|---|
| 314 | /* Error in communication with console */
|
|---|
| [6d5e378] | 315 | cli_quit = 1;
|
|---|
| [5db9084] | 316 | return;
|
|---|
| 317 | }
|
|---|
| [19528516] | 318 |
|
|---|
| 319 | /* Check for empty input. */
|
|---|
| 320 | if (str_cmp(str, "") == 0) {
|
|---|
| 321 | free(str);
|
|---|
| [216d6fc] | 322 | return;
|
|---|
| [19528516] | 323 | }
|
|---|
| [216d6fc] | 324 |
|
|---|
| [19528516] | 325 | usr->line = str;
|
|---|
| [216d6fc] | 326 | return;
|
|---|
| 327 | }
|
|---|
| [da2bd08] | 328 |
|
|---|
| [36a75a2] | 329 | int input_init(void)
|
|---|
| [da2bd08] | 330 | {
|
|---|
| [36a75a2] | 331 | tinput = tinput_new();
|
|---|
| 332 | if (tinput == NULL) {
|
|---|
| 333 | printf("Failed to initialize input.\n");
|
|---|
| 334 | return 1;
|
|---|
| 335 | }
|
|---|
| 336 |
|
|---|
| [9be9c4d] | 337 | tinput_set_compl_ops(tinput, &compl_ops);
|
|---|
| 338 |
|
|---|
| [36a75a2] | 339 | return 0;
|
|---|
| [da2bd08] | 340 | }
|
|---|