Rev 4327 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
3265 | post | 1 | /* Copyright (c) 2008, Tim Post <tinkertim@gmail.com> |
2 | * All rights reserved. |
||
3 | * Copyright (c) 2008, Jiri Svoboda - All Rights Reserved |
||
4 | * |
||
5 | * Redistribution and use in source and binary forms, with or without |
||
6 | * modification, are permitted provided that the following conditions are met: |
||
7 | * |
||
8 | * Redistributions of source code must retain the above copyright notice, this |
||
9 | * list of conditions and the following disclaimer. |
||
10 | * |
||
11 | * Redistributions in binary form must reproduce the above copyright notice, |
||
12 | * this list of conditions and the following disclaimer in the documentation |
||
13 | * and/or other materials provided with the distribution. |
||
14 | * |
||
15 | * Neither the name of the original program's authors nor the names of its |
||
16 | * contributors may be used to endorse or promote products derived from this |
||
17 | * software without specific prior written permission. |
||
18 | * |
||
19 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
||
20 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
||
21 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
||
22 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
||
23 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
||
24 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
||
25 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
||
26 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
||
27 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
||
28 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
||
29 | * POSSIBILITY OF SUCH DAMAGE. |
||
30 | */ |
||
31 | |||
32 | #include <stdio.h> |
||
33 | #include <stdlib.h> |
||
34 | #include <string.h> |
||
4581 | mejdrech | 35 | #include <io/console.h> |
36 | #include <io/keycode.h> |
||
37 | #include <io/style.h> |
||
38 | #include <vfs/vfs.h> |
||
4263 | mejdrech | 39 | #include <errno.h> |
40 | #include <bool.h> |
||
3265 | post | 41 | |
42 | #include "config.h" |
||
43 | #include "util.h" |
||
44 | #include "scli.h" |
||
45 | #include "input.h" |
||
46 | #include "errors.h" |
||
47 | #include "exec.h" |
||
48 | |||
3372 | post | 49 | static void read_line(char *, int); |
50 | |||
3265 | post | 51 | /* Tokenizes input from console, sees if the first word is a built-in, if so |
52 | * invokes the built-in entry point (a[0]) passing all arguments in a[] to |
||
53 | * the handler */ |
||
54 | int tok_input(cliuser_t *usr) |
||
55 | { |
||
56 | char *cmd[WORD_MAX]; |
||
57 | int n = 0, i = 0; |
||
58 | int rc = 0; |
||
3269 | post | 59 | char *tmp; |
3265 | post | 60 | |
3269 | post | 61 | if (NULL == usr->line) |
62 | return CL_EFAIL; |
||
3265 | post | 63 | |
4327 | mejdrech | 64 | tmp = str_dup(usr->line); |
3275 | post | 65 | |
4153 | mejdrech | 66 | cmd[n] = strtok(tmp, " "); |
3265 | post | 67 | while (cmd[n] && n < WORD_MAX) { |
4153 | mejdrech | 68 | cmd[++n] = strtok(NULL, " "); |
3265 | post | 69 | } |
70 | |||
71 | /* We have rubbish */ |
||
72 | if (NULL == cmd[0]) { |
||
73 | rc = CL_ENOENT; |
||
74 | goto finit; |
||
75 | } |
||
76 | |||
4153 | mejdrech | 77 | /* Its a builtin command ? */ |
3265 | post | 78 | if ((i = (is_builtin(cmd[0]))) > -1) { |
79 | rc = run_builtin(i, cmd, usr); |
||
80 | goto finit; |
||
4153 | mejdrech | 81 | /* Its a module ? */ |
3265 | post | 82 | } else if ((i = (is_module(cmd[0]))) > -1) { |
83 | rc = run_module(i, cmd); |
||
84 | goto finit; |
||
85 | } |
||
86 | |||
4153 | mejdrech | 87 | /* See what try_exec thinks of it */ |
88 | rc = try_exec(cmd[0], cmd); |
||
89 | |||
3265 | post | 90 | finit: |
91 | if (NULL != usr->line) { |
||
92 | free(usr->line); |
||
93 | usr->line = (char *) NULL; |
||
94 | } |
||
3268 | post | 95 | if (NULL != tmp) |
96 | free(tmp); |
||
3275 | post | 97 | |
3265 | post | 98 | return rc; |
99 | } |
||
100 | |||
3372 | post | 101 | static void read_line(char *buffer, int n) |
3265 | post | 102 | { |
4581 | mejdrech | 103 | console_event_t ev; |
4263 | mejdrech | 104 | size_t offs, otmp; |
105 | wchar_t dec; |
||
3265 | post | 106 | |
4263 | mejdrech | 107 | offs = 0; |
108 | while (true) { |
||
109 | fflush(stdout); |
||
4581 | mejdrech | 110 | if (!console_get_event(fphone(stdin), &ev)) |
3265 | post | 111 | return; |
4581 | mejdrech | 112 | |
113 | if (ev.type != KEY_PRESS) |
||
4263 | mejdrech | 114 | continue; |
4581 | mejdrech | 115 | |
4263 | mejdrech | 116 | if (ev.key == KC_ENTER || ev.key == KC_NENTER) |
3265 | post | 117 | break; |
4263 | mejdrech | 118 | if (ev.key == KC_BACKSPACE) { |
119 | if (offs > 0) { |
||
120 | /* |
||
121 | * Back up until we reach valid start of |
||
122 | * character. |
||
123 | */ |
||
124 | while (offs > 0) { |
||
125 | --offs; otmp = offs; |
||
126 | dec = str_decode(buffer, &otmp, n); |
||
127 | if (dec != U_SPECIAL) |
||
128 | break; |
||
129 | } |
||
3265 | post | 130 | putchar('\b'); |
131 | } |
||
132 | continue; |
||
133 | } |
||
4263 | mejdrech | 134 | if (ev.c >= ' ') { |
135 | if (chr_encode(ev.c, buffer, &offs, n - 1) == EOK) |
||
4581 | mejdrech | 136 | putchar(ev.c); |
4153 | mejdrech | 137 | } |
3265 | post | 138 | } |
139 | putchar('\n'); |
||
4263 | mejdrech | 140 | buffer[offs] = '\0'; |
3265 | post | 141 | } |
142 | |||
3372 | post | 143 | /* TODO: |
144 | * Implement something like editline() / readline(), if even |
||
145 | * just for command history and making arrows work. */ |
||
3265 | post | 146 | void get_input(cliuser_t *usr) |
147 | { |
||
148 | char line[INPUT_MAX]; |
||
149 | |||
4581 | mejdrech | 150 | fflush(stdout); |
151 | console_set_style(fphone(stdout), STYLE_EMPHASIS); |
||
3265 | post | 152 | printf("%s", usr->prompt); |
4581 | mejdrech | 153 | fflush(stdout); |
154 | console_set_style(fphone(stdout), STYLE_NORMAL); |
||
4153 | mejdrech | 155 | |
3265 | post | 156 | read_line(line, INPUT_MAX); |
157 | /* Make sure we don't have rubbish or a C/R happy user */ |
||
4327 | mejdrech | 158 | if (str_cmp(line, "") == 0 || str_cmp(line, "\n") == 0) |
3265 | post | 159 | return; |
4327 | mejdrech | 160 | usr->line = str_dup(line); |
3304 | post | 161 | |
3265 | post | 162 | return; |
163 | } |
||
164 |