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