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