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