| 1 | /* Copyright (c) 2008, Tim Post <tinkertim@gmail.com>
|
|---|
| 2 | * All rights reserved.
|
|---|
| 3 | *
|
|---|
| 4 | * Redistribution and use in source and binary forms, with or without
|
|---|
| 5 | * modification, are permitted provided that the following conditions are met:
|
|---|
| 6 | *
|
|---|
| 7 | * Redistributions of source code must retain the above copyright notice, this
|
|---|
| 8 | * list of conditions and the following disclaimer.
|
|---|
| 9 | *
|
|---|
| 10 | * Redistributions in binary form must reproduce the above copyright notice,
|
|---|
| 11 | * this list of conditions and the following disclaimer in the documentation
|
|---|
| 12 | * and/or other materials provided with the distribution.
|
|---|
| 13 | *
|
|---|
| 14 | * Neither the name of the original program's authors nor the names of its
|
|---|
| 15 | * contributors may be used to endorse or promote products derived from this
|
|---|
| 16 | * software without specific prior written permission.
|
|---|
| 17 | *
|
|---|
| 18 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|---|
| 19 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|---|
| 20 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|---|
| 21 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
|---|
| 22 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|---|
| 23 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|---|
| 24 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|---|
| 25 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|---|
| 26 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|---|
| 27 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|---|
| 28 | * 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 "util.h"
|
|---|
| 48 | #include "scli.h"
|
|---|
| 49 | #include "input.h"
|
|---|
| 50 | #include "errors.h"
|
|---|
| 51 | #include "exec.h"
|
|---|
| 52 | #include "tok.h"
|
|---|
| 53 |
|
|---|
| 54 | extern volatile unsigned int cli_quit;
|
|---|
| 55 |
|
|---|
| 56 | /** Text input field. */
|
|---|
| 57 | static tinput_t *tinput;
|
|---|
| 58 |
|
|---|
| 59 | /* Private helpers */
|
|---|
| 60 | static int run_command(char **, cliuser_t *);
|
|---|
| 61 |
|
|---|
| 62 | /* Tokenizes input from console, sees if the first word is a built-in, if so
|
|---|
| 63 | * invokes the built-in entry point (a[0]) passing all arguments in a[] to
|
|---|
| 64 | * the handler */
|
|---|
| 65 | int process_input(cliuser_t *usr)
|
|---|
| 66 | {
|
|---|
| 67 | char *cmd[WORD_MAX];
|
|---|
| 68 | int rc = 0;
|
|---|
| 69 | tokenizer_t tok;
|
|---|
| 70 |
|
|---|
| 71 | if (NULL == usr->line)
|
|---|
| 72 | return CL_EFAIL;
|
|---|
| 73 |
|
|---|
| 74 | rc = tok_init(&tok, usr->line, cmd, WORD_MAX);
|
|---|
| 75 | if (rc != EOK) {
|
|---|
| 76 | goto finit;
|
|---|
| 77 | }
|
|---|
| 78 |
|
|---|
| 79 | rc = tok_tokenize(&tok);
|
|---|
| 80 | if (rc != EOK) {
|
|---|
| 81 | goto finit;
|
|---|
| 82 | }
|
|---|
| 83 |
|
|---|
| 84 | rc = run_command(cmd, usr);
|
|---|
| 85 |
|
|---|
| 86 | finit:
|
|---|
| 87 | if (NULL != usr->line) {
|
|---|
| 88 | free(usr->line);
|
|---|
| 89 | usr->line = (char *) NULL;
|
|---|
| 90 | }
|
|---|
| 91 | tok_fini(&tok);
|
|---|
| 92 |
|
|---|
| 93 | return rc;
|
|---|
| 94 | }
|
|---|
| 95 |
|
|---|
| 96 | int run_command(char **cmd, cliuser_t *usr)
|
|---|
| 97 | {
|
|---|
| 98 | int id = 0;
|
|---|
| 99 |
|
|---|
| 100 | /* We have rubbish */
|
|---|
| 101 | if (NULL == cmd[0]) {
|
|---|
| 102 | return CL_ENOENT;
|
|---|
| 103 | }
|
|---|
| 104 |
|
|---|
| 105 | /* Is it a builtin command ? */
|
|---|
| 106 | if ((id = (is_builtin(cmd[0]))) > -1) {
|
|---|
| 107 | return run_builtin(id, cmd, usr);
|
|---|
| 108 | }
|
|---|
| 109 |
|
|---|
| 110 | /* Is it a module ? */
|
|---|
| 111 | if ((id = (is_module(cmd[0]))) > -1) {
|
|---|
| 112 | return run_module(id, cmd);
|
|---|
| 113 | }
|
|---|
| 114 |
|
|---|
| 115 | /* See what try_exec thinks of it */
|
|---|
| 116 | return try_exec(cmd[0], cmd);
|
|---|
| 117 | }
|
|---|
| 118 |
|
|---|
| 119 | void get_input(cliuser_t *usr)
|
|---|
| 120 | {
|
|---|
| 121 | char *str;
|
|---|
| 122 | int rc;
|
|---|
| 123 |
|
|---|
| 124 | console_flush(tinput->console);
|
|---|
| 125 | console_set_style(tinput->console, STYLE_EMPHASIS);
|
|---|
| 126 | printf("%s", usr->prompt);
|
|---|
| 127 | console_flush(tinput->console);
|
|---|
| 128 | console_set_style(tinput->console, STYLE_NORMAL);
|
|---|
| 129 |
|
|---|
| 130 | rc = tinput_read(tinput, &str);
|
|---|
| 131 | if (rc == ENOENT) {
|
|---|
| 132 | /* User requested exit */
|
|---|
| 133 | cli_quit = 1;
|
|---|
| 134 | putchar('\n');
|
|---|
| 135 | return;
|
|---|
| 136 | }
|
|---|
| 137 |
|
|---|
| 138 | if (rc != EOK) {
|
|---|
| 139 | /* Error in communication with console */
|
|---|
| 140 | return;
|
|---|
| 141 | }
|
|---|
| 142 |
|
|---|
| 143 | /* Check for empty input. */
|
|---|
| 144 | if (str_cmp(str, "") == 0) {
|
|---|
| 145 | free(str);
|
|---|
| 146 | return;
|
|---|
| 147 | }
|
|---|
| 148 |
|
|---|
| 149 | usr->line = str;
|
|---|
| 150 | return;
|
|---|
| 151 | }
|
|---|
| 152 |
|
|---|
| 153 | int input_init(void)
|
|---|
| 154 | {
|
|---|
| 155 | tinput = tinput_new();
|
|---|
| 156 | if (tinput == NULL) {
|
|---|
| 157 | printf("Failed to initialize input.\n");
|
|---|
| 158 | return 1;
|
|---|
| 159 | }
|
|---|
| 160 |
|
|---|
| 161 | return 0;
|
|---|
| 162 | }
|
|---|