[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>
|
---|
[55e35a22] | 45 | #include <adt/odict.h>
|
---|
[216d6fc] | 46 |
|
---|
| 47 | #include "config.h"
|
---|
[9be9c4d] | 48 | #include "compl.h"
|
---|
[216d6fc] | 49 | #include "util.h"
|
---|
| 50 | #include "scli.h"
|
---|
| 51 | #include "input.h"
|
---|
| 52 | #include "errors.h"
|
---|
| 53 | #include "exec.h"
|
---|
[6939edb] | 54 | #include "tok.h"
|
---|
[216d6fc] | 55 |
|
---|
[5db9084] | 56 | extern volatile unsigned int cli_quit;
|
---|
| 57 |
|
---|
[ed372da] | 58 | /** Text input field. */
|
---|
[36a75a2] | 59 | static tinput_t *tinput;
|
---|
[88944695] | 60 |
|
---|
[6939edb] | 61 | /* Private helpers */
|
---|
[6ea9a1d] | 62 | static int run_command(char **, cliuser_t *, iostate_t *);
|
---|
[ae45201] | 63 | static void print_pipe_usage(void);
|
---|
[659e9473] | 64 |
|
---|
[7c3fb9b] | 65 | /*
|
---|
| 66 | * Tokenizes input from console, sees if the first word is a built-in, if so
|
---|
[216d6fc] | 67 | * invokes the built-in entry point (a[0]) passing all arguments in a[] to
|
---|
[7c3fb9b] | 68 | * the handler
|
---|
| 69 | */
|
---|
[b7fd2a0] | 70 | errno_t process_input(cliuser_t *usr)
|
---|
[216d6fc] | 71 | {
|
---|
[01e397ac] | 72 | token_t *tokens_buf = calloc(WORD_MAX, sizeof(token_t));
|
---|
| 73 | if (tokens_buf == NULL)
|
---|
[b9ae539] | 74 | return ENOMEM;
|
---|
[01e397ac] | 75 | token_t *tokens = tokens_buf;
|
---|
[a35b458] | 76 |
|
---|
[216d6fc] | 77 | char *cmd[WORD_MAX];
|
---|
[b7fd2a0] | 78 | errno_t rc = EOK;
|
---|
[6939edb] | 79 | tokenizer_t tok;
|
---|
[0662451] | 80 | unsigned int i, pipe_count, processed_pipes;
|
---|
| 81 | unsigned int pipe_pos[2];
|
---|
[ae45201] | 82 | char *redir_from = NULL;
|
---|
| 83 | char *redir_to = NULL;
|
---|
[216d6fc] | 84 |
|
---|
[b48d046] | 85 | if (usr->line == NULL) {
|
---|
[01e397ac] | 86 | free(tokens_buf);
|
---|
[1569a9b] | 87 | return EINVAL;
|
---|
[b9ae539] | 88 | }
|
---|
[216d6fc] | 89 |
|
---|
[0662451] | 90 | rc = tok_init(&tok, usr->line, tokens, WORD_MAX);
|
---|
[6939edb] | 91 | if (rc != EOK) {
|
---|
| 92 | goto finit;
|
---|
[216d6fc] | 93 | }
|
---|
[a35b458] | 94 |
|
---|
[0662451] | 95 | size_t tokens_length;
|
---|
| 96 | rc = tok_tokenize(&tok, &tokens_length);
|
---|
[6939edb] | 97 | if (rc != EOK) {
|
---|
| 98 | goto finit;
|
---|
| 99 | }
|
---|
[a35b458] | 100 |
|
---|
[0662451] | 101 | if (tokens_length > 0 && tokens[0].type == TOKTYPE_SPACE) {
|
---|
| 102 | tokens++;
|
---|
| 103 | tokens_length--;
|
---|
| 104 | }
|
---|
[a35b458] | 105 |
|
---|
[1433ecda] | 106 | if (tokens_length > 0 && tokens[tokens_length - 1].type == TOKTYPE_SPACE) {
|
---|
[0662451] | 107 | tokens_length--;
|
---|
| 108 | }
|
---|
[a35b458] | 109 |
|
---|
[7c3fb9b] | 110 | /*
|
---|
| 111 | * Until full support for pipes is implemented, allow for a simple case:
|
---|
[ae45201] | 112 | * [from <file> |] command [| to <file>]
|
---|
[1b20da0] | 113 | *
|
---|
[ae45201] | 114 | * First find the pipes and check that there are no more
|
---|
| 115 | */
|
---|
[0662451] | 116 | for (i = 0, pipe_count = 0; i < tokens_length; i++) {
|
---|
| 117 | if (tokens[i].type == TOKTYPE_PIPE) {
|
---|
[ae45201] | 118 | if (pipe_count >= 2) {
|
---|
| 119 | print_pipe_usage();
|
---|
| 120 | rc = ENOTSUP;
|
---|
| 121 | goto finit;
|
---|
| 122 | }
|
---|
| 123 | pipe_pos[pipe_count] = i;
|
---|
| 124 | pipe_count++;
|
---|
| 125 | }
|
---|
| 126 | }
|
---|
[a35b458] | 127 |
|
---|
[0662451] | 128 | unsigned int cmd_token_start = 0;
|
---|
| 129 | unsigned int cmd_token_end = tokens_length;
|
---|
[a35b458] | 130 |
|
---|
[ae45201] | 131 | processed_pipes = 0;
|
---|
[a35b458] | 132 |
|
---|
[ae45201] | 133 | /* Check if the first part (from <file> |) is present */
|
---|
[0662451] | 134 | if (pipe_count > 0 && (pipe_pos[0] == 3 || pipe_pos[0] == 4) && str_cmp(tokens[0].text, "from") == 0) {
|
---|
[ae45201] | 135 | /* Ignore the first three tokens (from, file, pipe) and set from */
|
---|
[0662451] | 136 | redir_from = tokens[2].text;
|
---|
[1433ecda] | 137 | cmd_token_start = pipe_pos[0] + 1;
|
---|
[ae45201] | 138 | processed_pipes++;
|
---|
| 139 | }
|
---|
[a35b458] | 140 |
|
---|
[ae45201] | 141 | /* Check if the second part (| to <file>) is present */
|
---|
| 142 | if ((pipe_count - processed_pipes) > 0 &&
|
---|
[0662451] | 143 | (pipe_pos[processed_pipes] == tokens_length - 4 ||
|
---|
| 144 | (pipe_pos[processed_pipes] == tokens_length - 5 &&
|
---|
[1433ecda] | 145 | tokens[tokens_length - 4].type == TOKTYPE_SPACE)) &&
|
---|
| 146 | str_cmp(tokens[tokens_length - 3].text, "to") == 0) {
|
---|
[ae45201] | 147 | /* Ignore the last three tokens (pipe, to, file) and set to */
|
---|
[1433ecda] | 148 | redir_to = tokens[tokens_length - 1].text;
|
---|
[0662451] | 149 | cmd_token_end = pipe_pos[processed_pipes];
|
---|
[ae45201] | 150 | processed_pipes++;
|
---|
| 151 | }
|
---|
[a35b458] | 152 |
|
---|
[ae45201] | 153 | if (processed_pipes != pipe_count) {
|
---|
| 154 | print_pipe_usage();
|
---|
| 155 | rc = ENOTSUP;
|
---|
| 156 | goto finit;
|
---|
| 157 | }
|
---|
[a35b458] | 158 |
|
---|
[0662451] | 159 | /* Convert tokens of the command to string array */
|
---|
| 160 | unsigned int cmd_pos = 0;
|
---|
| 161 | for (i = cmd_token_start; i < cmd_token_end; i++) {
|
---|
| 162 | if (tokens[i].type != TOKTYPE_SPACE) {
|
---|
| 163 | cmd[cmd_pos++] = tokens[i].text;
|
---|
| 164 | }
|
---|
| 165 | }
|
---|
| 166 | cmd[cmd_pos++] = NULL;
|
---|
[a35b458] | 167 |
|
---|
[0662451] | 168 | if (cmd[0] == NULL) {
|
---|
[ae45201] | 169 | print_pipe_usage();
|
---|
| 170 | rc = ENOTSUP;
|
---|
| 171 | goto finit;
|
---|
| 172 | }
|
---|
[a35b458] | 173 |
|
---|
[4bf08aa5] | 174 |
|
---|
| 175 |
|
---|
[55e35a22] | 176 | /* test if the passed cmd is an alias */
|
---|
| 177 | odlink_t* alias_link = odict_find_eq(&alias_dict, (void*)cmd[0], NULL);
|
---|
| 178 | if (alias_link != NULL) {
|
---|
| 179 | alias_t* data = odict_get_instance(alias_link, alias_t, odict);
|
---|
[4bf08aa5] | 180 | char* oldLine = usr->line;
|
---|
[55e35a22] | 181 |
|
---|
| 182 |
|
---|
| 183 | const size_t input_length = str_size(usr->line) - str_size(cmd[0]) + str_size(data->value) + 1;
|
---|
| 184 | usr->line = (char*)malloc(INPUT_MAX * sizeof(char));
|
---|
| 185 | usr->line[0] = '\0';
|
---|
| 186 |
|
---|
| 187 | unsigned int cmd_replace_index = cmd_token_start;
|
---|
| 188 | for (i = 0; i < tokens_length; i++) {
|
---|
| 189 | if(i == cmd_replace_index) {
|
---|
| 190 | //if there is a pipe symbol than cmd_token_start will point at the SPACE after the pipe symbol
|
---|
| 191 | if(tokens[i].type == TOKTYPE_SPACE) {
|
---|
| 192 | cmd_replace_index++;
|
---|
| 193 | str_append(usr->line, input_length, tokens[i].text);
|
---|
| 194 | continue;
|
---|
| 195 | }
|
---|
| 196 |
|
---|
| 197 | str_append(usr->line, input_length, data->value);
|
---|
| 198 | }else {
|
---|
| 199 | str_append(usr->line, input_length, tokens[i].text);
|
---|
| 200 | }
|
---|
| 201 | }
|
---|
[4bf08aa5] | 202 |
|
---|
| 203 | //reprocess input after string replace
|
---|
| 204 | rc = process_input(usr);
|
---|
| 205 | usr->line = oldLine;
|
---|
| 206 | goto finit;
|
---|
| 207 | }
|
---|
| 208 |
|
---|
| 209 |
|
---|
[6ea9a1d] | 210 | iostate_t new_iostate = {
|
---|
| 211 | .stdin = stdin,
|
---|
| 212 | .stdout = stdout,
|
---|
| 213 | .stderr = stderr
|
---|
| 214 | };
|
---|
[a35b458] | 215 |
|
---|
[6ea9a1d] | 216 | FILE *from = NULL;
|
---|
| 217 | FILE *to = NULL;
|
---|
[a35b458] | 218 |
|
---|
[ae45201] | 219 | if (redir_from) {
|
---|
[6ea9a1d] | 220 | from = fopen(redir_from, "r");
|
---|
[ae45201] | 221 | if (from == NULL) {
|
---|
| 222 | printf("Cannot open file %s\n", redir_from);
|
---|
| 223 | rc = errno;
|
---|
[6ea9a1d] | 224 | goto finit_with_files;
|
---|
[ae45201] | 225 | }
|
---|
[6ea9a1d] | 226 | new_iostate.stdin = from;
|
---|
[ae45201] | 227 | }
|
---|
[a35b458] | 228 |
|
---|
[ae45201] | 229 | if (redir_to) {
|
---|
[6ea9a1d] | 230 | to = fopen(redir_to, "w");
|
---|
[ae45201] | 231 | if (to == NULL) {
|
---|
| 232 | printf("Cannot open file %s\n", redir_to);
|
---|
| 233 | rc = errno;
|
---|
[6ea9a1d] | 234 | goto finit_with_files;
|
---|
[ae45201] | 235 | }
|
---|
[6ea9a1d] | 236 | new_iostate.stdout = to;
|
---|
[ae45201] | 237 | }
|
---|
[10f4b47c] | 238 |
|
---|
[1569a9b] | 239 | if (run_command(cmd, usr, &new_iostate) == 0) {
|
---|
| 240 | rc = EOK;
|
---|
| 241 | } else {
|
---|
| 242 | rc = EINVAL;
|
---|
| 243 | }
|
---|
[a35b458] | 244 |
|
---|
[6ea9a1d] | 245 | finit_with_files:
|
---|
| 246 | if (from != NULL) {
|
---|
| 247 | fclose(from);
|
---|
| 248 | }
|
---|
| 249 | if (to != NULL) {
|
---|
| 250 | fclose(to);
|
---|
| 251 | }
|
---|
[a35b458] | 252 |
|
---|
[6939edb] | 253 | finit:
|
---|
[216d6fc] | 254 | if (NULL != usr->line) {
|
---|
| 255 | free(usr->line);
|
---|
| 256 | usr->line = (char *) NULL;
|
---|
| 257 | }
|
---|
[4bf08aa5] | 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 | }
|
---|