[216d6fc] | 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>
|
---|
[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>
|
---|
[6071a8f] | 43 | #include <bool.h>
|
---|
[36a75a2] | 44 | #include <tinput.h>
|
---|
[216d6fc] | 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 |
|
---|
[ed372da] | 53 | /** Text input field. */
|
---|
[36a75a2] | 54 | static tinput_t *tinput;
|
---|
[88944695] | 55 |
|
---|
[216d6fc] | 56 | /* Tokenizes input from console, sees if the first word is a built-in, if so
|
---|
| 57 | * invokes the built-in entry point (a[0]) passing all arguments in a[] to
|
---|
| 58 | * the handler */
|
---|
| 59 | int tok_input(cliuser_t *usr)
|
---|
| 60 | {
|
---|
| 61 | char *cmd[WORD_MAX];
|
---|
| 62 | int n = 0, i = 0;
|
---|
| 63 | int rc = 0;
|
---|
| 64 | char *tmp;
|
---|
| 65 |
|
---|
| 66 | if (NULL == usr->line)
|
---|
| 67 | return CL_EFAIL;
|
---|
| 68 |
|
---|
[095003a8] | 69 | tmp = str_dup(usr->line);
|
---|
[216d6fc] | 70 |
|
---|
[4cc0c9ee] | 71 | cmd[n] = strtok(tmp, " ");
|
---|
[216d6fc] | 72 | while (cmd[n] && n < WORD_MAX) {
|
---|
[4cc0c9ee] | 73 | cmd[++n] = strtok(NULL, " ");
|
---|
[216d6fc] | 74 | }
|
---|
| 75 |
|
---|
| 76 | /* We have rubbish */
|
---|
| 77 | if (NULL == cmd[0]) {
|
---|
| 78 | rc = CL_ENOENT;
|
---|
| 79 | goto finit;
|
---|
| 80 | }
|
---|
| 81 |
|
---|
[d752cf4] | 82 | /* Its a builtin command ? */
|
---|
[216d6fc] | 83 | if ((i = (is_builtin(cmd[0]))) > -1) {
|
---|
| 84 | rc = run_builtin(i, cmd, usr);
|
---|
| 85 | goto finit;
|
---|
[d752cf4] | 86 | /* Its a module ? */
|
---|
[216d6fc] | 87 | } else if ((i = (is_module(cmd[0]))) > -1) {
|
---|
| 88 | rc = run_module(i, cmd);
|
---|
| 89 | goto finit;
|
---|
| 90 | }
|
---|
| 91 |
|
---|
[d752cf4] | 92 | /* See what try_exec thinks of it */
|
---|
| 93 | rc = try_exec(cmd[0], cmd);
|
---|
| 94 |
|
---|
[216d6fc] | 95 | finit:
|
---|
| 96 | if (NULL != usr->line) {
|
---|
| 97 | free(usr->line);
|
---|
| 98 | usr->line = (char *) NULL;
|
---|
| 99 | }
|
---|
| 100 | if (NULL != tmp)
|
---|
| 101 | free(tmp);
|
---|
| 102 |
|
---|
| 103 | return rc;
|
---|
| 104 | }
|
---|
| 105 |
|
---|
| 106 | void get_input(cliuser_t *usr)
|
---|
| 107 | {
|
---|
[19528516] | 108 | char *str;
|
---|
[216d6fc] | 109 |
|
---|
[ef8bcc6] | 110 | fflush(stdout);
|
---|
[73878c1] | 111 | console_set_style(fphone(stdout), STYLE_EMPHASIS);
|
---|
[216d6fc] | 112 | printf("%s", usr->prompt);
|
---|
[ef8bcc6] | 113 | fflush(stdout);
|
---|
[73878c1] | 114 | console_set_style(fphone(stdout), STYLE_NORMAL);
|
---|
[9805cde] | 115 |
|
---|
[36a75a2] | 116 | str = tinput_read(tinput);
|
---|
[19528516] | 117 |
|
---|
| 118 | /* Check for empty input. */
|
---|
| 119 | if (str_cmp(str, "") == 0) {
|
---|
| 120 | free(str);
|
---|
[216d6fc] | 121 | return;
|
---|
[19528516] | 122 | }
|
---|
[216d6fc] | 123 |
|
---|
[19528516] | 124 | usr->line = str;
|
---|
[216d6fc] | 125 | return;
|
---|
| 126 | }
|
---|
[da2bd08] | 127 |
|
---|
[36a75a2] | 128 | int input_init(void)
|
---|
[da2bd08] | 129 | {
|
---|
[36a75a2] | 130 | tinput = tinput_new();
|
---|
| 131 | if (tinput == NULL) {
|
---|
| 132 | printf("Failed to initialize input.\n");
|
---|
| 133 | return 1;
|
---|
| 134 | }
|
---|
| 135 |
|
---|
| 136 | return 0;
|
---|
[da2bd08] | 137 | }
|
---|