| 1 | /*
|
|---|
| 2 | * Copyright (c) 2008 Tim Post
|
|---|
| 3 | * Copyright (c) 2011 Jiri Svoboda
|
|---|
| 4 | * Copyright (c) 2011 Martin Sucha
|
|---|
| 5 | * All rights reserved.
|
|---|
| 6 | *
|
|---|
| 7 | * Redistribution and use in source and binary forms, with or without
|
|---|
| 8 | * modification, are permitted provided that the following conditions
|
|---|
| 9 | * are met:
|
|---|
| 10 | *
|
|---|
| 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.
|
|---|
| 18 | *
|
|---|
| 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.
|
|---|
| 29 | */
|
|---|
| 30 |
|
|---|
| 31 | #include <stdio.h>
|
|---|
| 32 | #include <stdlib.h>
|
|---|
| 33 | #include <str.h>
|
|---|
| 34 | #include <io/console.h>
|
|---|
| 35 | #include <io/keycode.h>
|
|---|
| 36 | #include <io/style.h>
|
|---|
| 37 | #include <io/color.h>
|
|---|
| 38 | #include <vfs/vfs.h>
|
|---|
| 39 | #include <clipboard.h>
|
|---|
| 40 | #include <macros.h>
|
|---|
| 41 | #include <errno.h>
|
|---|
| 42 | #include <assert.h>
|
|---|
| 43 | #include <stdbool.h>
|
|---|
| 44 | #include <tinput.h>
|
|---|
| 45 |
|
|---|
| 46 | #include "config.h"
|
|---|
| 47 | #include "compl.h"
|
|---|
| 48 | #include "util.h"
|
|---|
| 49 | #include "scli.h"
|
|---|
| 50 | #include "input.h"
|
|---|
| 51 | #include "errors.h"
|
|---|
| 52 | #include "exec.h"
|
|---|
| 53 | #include "tok.h"
|
|---|
| 54 |
|
|---|
| 55 | extern volatile unsigned int cli_quit;
|
|---|
| 56 |
|
|---|
| 57 | /** Text input field. */
|
|---|
| 58 | static tinput_t *tinput;
|
|---|
| 59 |
|
|---|
| 60 | /* Private helpers */
|
|---|
| 61 | static int run_command(char **, cliuser_t *, iostate_t *);
|
|---|
| 62 | static void print_pipe_usage(void);
|
|---|
| 63 |
|
|---|
| 64 | /*
|
|---|
| 65 | * Tokenizes input from console, sees if the first word is a built-in, if so
|
|---|
| 66 | * invokes the built-in entry point (a[0]) passing all arguments in a[] to
|
|---|
| 67 | * the handler
|
|---|
| 68 | */
|
|---|
| 69 | errno_t process_input(cliuser_t *usr)
|
|---|
| 70 | {
|
|---|
| 71 | token_t *tokens_buf = calloc(WORD_MAX, sizeof(token_t));
|
|---|
| 72 | if (tokens_buf == NULL)
|
|---|
| 73 | return ENOMEM;
|
|---|
| 74 | token_t *tokens = tokens_buf;
|
|---|
| 75 |
|
|---|
| 76 | char *cmd[WORD_MAX];
|
|---|
| 77 | errno_t rc = EOK;
|
|---|
| 78 | tokenizer_t tok;
|
|---|
| 79 | unsigned int i, pipe_count, processed_pipes;
|
|---|
| 80 | unsigned int pipe_pos[2];
|
|---|
| 81 | char *redir_from = NULL;
|
|---|
| 82 | char *redir_to = NULL;
|
|---|
| 83 |
|
|---|
| 84 | if (usr->line == NULL) {
|
|---|
| 85 | free(tokens_buf);
|
|---|
| 86 | return EINVAL;
|
|---|
| 87 | }
|
|---|
| 88 |
|
|---|
| 89 | rc = tok_init(&tok, usr->line, tokens, WORD_MAX);
|
|---|
| 90 | if (rc != EOK) {
|
|---|
| 91 | goto finit;
|
|---|
| 92 | }
|
|---|
| 93 |
|
|---|
| 94 | size_t tokens_length;
|
|---|
| 95 | rc = tok_tokenize(&tok, &tokens_length);
|
|---|
| 96 | if (rc != EOK) {
|
|---|
| 97 | goto finit;
|
|---|
| 98 | }
|
|---|
| 99 |
|
|---|
| 100 | if (tokens_length > 0 && tokens[0].type == TOKTYPE_SPACE) {
|
|---|
| 101 | tokens++;
|
|---|
| 102 | tokens_length--;
|
|---|
| 103 | }
|
|---|
| 104 |
|
|---|
| 105 | if (tokens_length > 0 && tokens[tokens_length - 1].type == TOKTYPE_SPACE) {
|
|---|
| 106 | tokens_length--;
|
|---|
| 107 | }
|
|---|
| 108 |
|
|---|
| 109 | /*
|
|---|
| 110 | * Until full support for pipes is implemented, allow for a simple case:
|
|---|
| 111 | * [from <file> |] command [| to <file>]
|
|---|
| 112 | *
|
|---|
| 113 | * First find the pipes and check that there are no more
|
|---|
| 114 | */
|
|---|
| 115 | for (i = 0, pipe_count = 0; i < tokens_length; i++) {
|
|---|
| 116 | if (tokens[i].type == TOKTYPE_PIPE) {
|
|---|
| 117 | if (pipe_count >= 2) {
|
|---|
| 118 | print_pipe_usage();
|
|---|
| 119 | rc = ENOTSUP;
|
|---|
| 120 | goto finit;
|
|---|
| 121 | }
|
|---|
| 122 | pipe_pos[pipe_count] = i;
|
|---|
| 123 | pipe_count++;
|
|---|
| 124 | }
|
|---|
| 125 | }
|
|---|
| 126 |
|
|---|
| 127 | unsigned int cmd_token_start = 0;
|
|---|
| 128 | unsigned int cmd_token_end = tokens_length;
|
|---|
| 129 |
|
|---|
| 130 | processed_pipes = 0;
|
|---|
| 131 |
|
|---|
| 132 | /* Check if the first part (from <file> |) is present */
|
|---|
| 133 | if (pipe_count > 0 && (pipe_pos[0] == 3 || pipe_pos[0] == 4) && str_cmp(tokens[0].text, "from") == 0) {
|
|---|
| 134 | /* Ignore the first three tokens (from, file, pipe) and set from */
|
|---|
| 135 | redir_from = tokens[2].text;
|
|---|
| 136 | cmd_token_start = pipe_pos[0] + 1;
|
|---|
| 137 | processed_pipes++;
|
|---|
| 138 | }
|
|---|
| 139 |
|
|---|
| 140 | /* Check if the second part (| to <file>) is present */
|
|---|
| 141 | if ((pipe_count - processed_pipes) > 0 &&
|
|---|
| 142 | (pipe_pos[processed_pipes] == tokens_length - 4 ||
|
|---|
| 143 | (pipe_pos[processed_pipes] == tokens_length - 5 &&
|
|---|
| 144 | tokens[tokens_length - 4].type == TOKTYPE_SPACE)) &&
|
|---|
| 145 | str_cmp(tokens[tokens_length - 3].text, "to") == 0) {
|
|---|
| 146 | /* Ignore the last three tokens (pipe, to, file) and set to */
|
|---|
| 147 | redir_to = tokens[tokens_length - 1].text;
|
|---|
| 148 | cmd_token_end = pipe_pos[processed_pipes];
|
|---|
| 149 | processed_pipes++;
|
|---|
| 150 | }
|
|---|
| 151 |
|
|---|
| 152 | if (processed_pipes != pipe_count) {
|
|---|
| 153 | print_pipe_usage();
|
|---|
| 154 | rc = ENOTSUP;
|
|---|
| 155 | goto finit;
|
|---|
| 156 | }
|
|---|
| 157 |
|
|---|
| 158 | /* Convert tokens of the command to string array */
|
|---|
| 159 | unsigned int cmd_pos = 0;
|
|---|
| 160 | for (i = cmd_token_start; i < cmd_token_end; i++) {
|
|---|
| 161 | if (tokens[i].type != TOKTYPE_SPACE) {
|
|---|
| 162 | cmd[cmd_pos++] = tokens[i].text;
|
|---|
| 163 | }
|
|---|
| 164 | }
|
|---|
| 165 | cmd[cmd_pos++] = NULL;
|
|---|
| 166 |
|
|---|
| 167 | if (cmd[0] == NULL) {
|
|---|
| 168 | print_pipe_usage();
|
|---|
| 169 | rc = ENOTSUP;
|
|---|
| 170 | goto finit;
|
|---|
| 171 | }
|
|---|
| 172 |
|
|---|
| 173 | iostate_t new_iostate = {
|
|---|
| 174 | .stdin = stdin,
|
|---|
| 175 | .stdout = stdout,
|
|---|
| 176 | .stderr = stderr
|
|---|
| 177 | };
|
|---|
| 178 |
|
|---|
| 179 | FILE *from = NULL;
|
|---|
| 180 | FILE *to = NULL;
|
|---|
| 181 |
|
|---|
| 182 | if (redir_from) {
|
|---|
| 183 | from = fopen(redir_from, "r");
|
|---|
| 184 | if (from == NULL) {
|
|---|
| 185 | printf("Cannot open file %s\n", redir_from);
|
|---|
| 186 | rc = errno;
|
|---|
| 187 | goto finit_with_files;
|
|---|
| 188 | }
|
|---|
| 189 | new_iostate.stdin = from;
|
|---|
| 190 | }
|
|---|
| 191 |
|
|---|
| 192 | if (redir_to) {
|
|---|
| 193 | to = fopen(redir_to, "w");
|
|---|
| 194 | if (to == NULL) {
|
|---|
| 195 | printf("Cannot open file %s\n", redir_to);
|
|---|
| 196 | rc = errno;
|
|---|
| 197 | goto finit_with_files;
|
|---|
| 198 | }
|
|---|
| 199 | new_iostate.stdout = to;
|
|---|
| 200 | }
|
|---|
| 201 |
|
|---|
| 202 | if (run_command(cmd, usr, &new_iostate) == 0) {
|
|---|
| 203 | rc = EOK;
|
|---|
| 204 | } else {
|
|---|
| 205 | rc = EINVAL;
|
|---|
| 206 | }
|
|---|
| 207 |
|
|---|
| 208 | finit_with_files:
|
|---|
| 209 | if (from != NULL) {
|
|---|
| 210 | fclose(from);
|
|---|
| 211 | }
|
|---|
| 212 | if (to != NULL) {
|
|---|
| 213 | fclose(to);
|
|---|
| 214 | }
|
|---|
| 215 |
|
|---|
| 216 | finit:
|
|---|
| 217 | if (NULL != usr->line) {
|
|---|
| 218 | free(usr->line);
|
|---|
| 219 | usr->line = (char *) NULL;
|
|---|
| 220 | }
|
|---|
| 221 | tok_fini(&tok);
|
|---|
| 222 | free(tokens_buf);
|
|---|
| 223 |
|
|---|
| 224 | return rc;
|
|---|
| 225 | }
|
|---|
| 226 |
|
|---|
| 227 | void print_pipe_usage(void)
|
|---|
| 228 | {
|
|---|
| 229 | printf("Invalid syntax!\n");
|
|---|
| 230 | printf("Usage of redirection (pipes in the future):\n");
|
|---|
| 231 | printf("from filename | command ...\n");
|
|---|
| 232 | printf("from filename | command ... | to filename\n");
|
|---|
| 233 | printf("command ... | to filename\n");
|
|---|
| 234 |
|
|---|
| 235 | }
|
|---|
| 236 |
|
|---|
| 237 | int run_command(char **cmd, cliuser_t *usr, iostate_t *new_iostate)
|
|---|
| 238 | {
|
|---|
| 239 | int id = 0;
|
|---|
| 240 |
|
|---|
| 241 | /* We have rubbish */
|
|---|
| 242 | if (NULL == cmd[0]) {
|
|---|
| 243 | return CL_ENOENT;
|
|---|
| 244 | }
|
|---|
| 245 |
|
|---|
| 246 | /* Is it a builtin command ? */
|
|---|
| 247 | if ((id = (is_builtin(cmd[0]))) > -1) {
|
|---|
| 248 | return run_builtin(id, cmd, usr, new_iostate);
|
|---|
| 249 | }
|
|---|
| 250 |
|
|---|
| 251 | /* Is it a module ? */
|
|---|
| 252 | if ((id = (is_module(cmd[0]))) > -1) {
|
|---|
| 253 | return run_module(id, cmd, new_iostate);
|
|---|
| 254 | }
|
|---|
| 255 |
|
|---|
| 256 | /* See what try_exec thinks of it */
|
|---|
| 257 | return try_exec(cmd[0], cmd, new_iostate);
|
|---|
| 258 | }
|
|---|
| 259 |
|
|---|
| 260 | void get_input(cliuser_t *usr)
|
|---|
| 261 | {
|
|---|
| 262 | char *str;
|
|---|
| 263 | errno_t rc;
|
|---|
| 264 |
|
|---|
| 265 | tinput_set_prompt(tinput, usr->prompt);
|
|---|
| 266 |
|
|---|
| 267 | rc = tinput_read(tinput, &str);
|
|---|
| 268 | if (rc == ENOENT) {
|
|---|
| 269 | /* User requested exit */
|
|---|
| 270 | cli_quit = 1;
|
|---|
| 271 | putchar('\n');
|
|---|
| 272 | return;
|
|---|
| 273 | }
|
|---|
| 274 |
|
|---|
| 275 | if (rc != EOK) {
|
|---|
| 276 | /* Error in communication with console */
|
|---|
| 277 | cli_quit = 1;
|
|---|
| 278 | return;
|
|---|
| 279 | }
|
|---|
| 280 |
|
|---|
| 281 | /* Check for empty input. */
|
|---|
| 282 | if (str_cmp(str, "") == 0) {
|
|---|
| 283 | free(str);
|
|---|
| 284 | return;
|
|---|
| 285 | }
|
|---|
| 286 |
|
|---|
| 287 | usr->line = str;
|
|---|
| 288 | return;
|
|---|
| 289 | }
|
|---|
| 290 |
|
|---|
| 291 | int input_init(void)
|
|---|
| 292 | {
|
|---|
| 293 | tinput = tinput_new();
|
|---|
| 294 | if (tinput == NULL) {
|
|---|
| 295 | printf("Failed to initialize input.\n");
|
|---|
| 296 | return 1;
|
|---|
| 297 | }
|
|---|
| 298 |
|
|---|
| 299 | tinput_set_compl_ops(tinput, &compl_ops);
|
|---|
| 300 |
|
|---|
| 301 | return 0;
|
|---|
| 302 | }
|
|---|