| 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 |
|
|---|
| 53 | extern volatile unsigned int cli_quit;
|
|---|
| 54 |
|
|---|
| 55 | /** Text input field. */
|
|---|
| 56 | static tinput_t *tinput;
|
|---|
| 57 |
|
|---|
| 58 | static int run_command(char **, cliuser_t *);
|
|---|
| 59 |
|
|---|
| 60 | /* Tokenizes input from console, sees if the first word is a built-in, if so
|
|---|
| 61 | * invokes the built-in entry point (a[0]) passing all arguments in a[] to
|
|---|
| 62 | * the handler */
|
|---|
| 63 | int tok_input(cliuser_t *usr)
|
|---|
| 64 | {
|
|---|
| 65 | char *cmd[WORD_MAX];
|
|---|
| 66 | int n = 0;
|
|---|
| 67 | int rc = 0;
|
|---|
| 68 | char *tmp;
|
|---|
| 69 |
|
|---|
| 70 | if (NULL == usr->line)
|
|---|
| 71 | return CL_EFAIL;
|
|---|
| 72 |
|
|---|
| 73 | tmp = str_dup(usr->line);
|
|---|
| 74 |
|
|---|
| 75 | cmd[n] = strtok(tmp, " ");
|
|---|
| 76 | while (cmd[n] && n < WORD_MAX) {
|
|---|
| 77 | cmd[++n] = strtok(NULL, " ");
|
|---|
| 78 | }
|
|---|
| 79 |
|
|---|
| 80 | rc = run_command(cmd, usr);
|
|---|
| 81 |
|
|---|
| 82 | if (NULL != usr->line) {
|
|---|
| 83 | free(usr->line);
|
|---|
| 84 | usr->line = (char *) NULL;
|
|---|
| 85 | }
|
|---|
| 86 | if (NULL != tmp)
|
|---|
| 87 | free(tmp);
|
|---|
| 88 |
|
|---|
| 89 | return rc;
|
|---|
| 90 | }
|
|---|
| 91 |
|
|---|
| 92 | int run_command(char **cmd, cliuser_t *usr)
|
|---|
| 93 | {
|
|---|
| 94 | int id = 0;
|
|---|
| 95 |
|
|---|
| 96 | /* We have rubbish */
|
|---|
| 97 | if (NULL == cmd[0]) {
|
|---|
| 98 | return CL_ENOENT;
|
|---|
| 99 | }
|
|---|
| 100 |
|
|---|
| 101 | /* Is it a builtin command ? */
|
|---|
| 102 | if ((id = (is_builtin(cmd[0]))) > -1) {
|
|---|
| 103 | return run_builtin(id, cmd, usr);
|
|---|
| 104 | }
|
|---|
| 105 |
|
|---|
| 106 | /* Is it a module ? */
|
|---|
| 107 | if ((id = (is_module(cmd[0]))) > -1) {
|
|---|
| 108 | return run_module(id, cmd);
|
|---|
| 109 | }
|
|---|
| 110 |
|
|---|
| 111 | /* See what try_exec thinks of it */
|
|---|
| 112 | return try_exec(cmd[0], cmd);
|
|---|
| 113 | }
|
|---|
| 114 |
|
|---|
| 115 | void get_input(cliuser_t *usr)
|
|---|
| 116 | {
|
|---|
| 117 | char *str;
|
|---|
| 118 | int rc;
|
|---|
| 119 |
|
|---|
| 120 | console_flush(tinput->console);
|
|---|
| 121 | console_set_style(tinput->console, STYLE_EMPHASIS);
|
|---|
| 122 | printf("%s", usr->prompt);
|
|---|
| 123 | console_flush(tinput->console);
|
|---|
| 124 | console_set_style(tinput->console, STYLE_NORMAL);
|
|---|
| 125 |
|
|---|
| 126 | rc = tinput_read(tinput, &str);
|
|---|
| 127 | if (rc == ENOENT) {
|
|---|
| 128 | /* User requested exit */
|
|---|
| 129 | cli_quit = 1;
|
|---|
| 130 | putchar('\n');
|
|---|
| 131 | return;
|
|---|
| 132 | }
|
|---|
| 133 |
|
|---|
| 134 | if (rc != EOK) {
|
|---|
| 135 | /* Error in communication with console */
|
|---|
| 136 | return;
|
|---|
| 137 | }
|
|---|
| 138 |
|
|---|
| 139 | /* Check for empty input. */
|
|---|
| 140 | if (str_cmp(str, "") == 0) {
|
|---|
| 141 | free(str);
|
|---|
| 142 | return;
|
|---|
| 143 | }
|
|---|
| 144 |
|
|---|
| 145 | usr->line = str;
|
|---|
| 146 | return;
|
|---|
| 147 | }
|
|---|
| 148 |
|
|---|
| 149 | int input_init(void)
|
|---|
| 150 | {
|
|---|
| 151 | tinput = tinput_new();
|
|---|
| 152 | if (tinput == NULL) {
|
|---|
| 153 | printf("Failed to initialize input.\n");
|
|---|
| 154 | return 1;
|
|---|
| 155 | }
|
|---|
| 156 |
|
|---|
| 157 | return 0;
|
|---|
| 158 | }
|
|---|