Rev 4348 | Go to most recent revision | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 4348 | Rev 4691 | ||
---|---|---|---|
1 | /* |
1 | /* |
2 | * Copyright (c) 2005 Jakub Jermar |
2 | * Copyright (c) 2005 Jakub Jermar |
3 | * All rights reserved. |
3 | * 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 |
6 | * modification, are permitted provided that the following conditions |
7 | * are met: |
7 | * are met: |
8 | * |
8 | * |
9 | * - Redistributions of source code must retain the above copyright |
9 | * - Redistributions of source code must retain the above copyright |
10 | * notice, this list of conditions and the following disclaimer. |
10 | * notice, this list of conditions and the following disclaimer. |
11 | * - Redistributions in binary form must reproduce the above copyright |
11 | * - Redistributions in binary form must reproduce the above copyright |
12 | * notice, this list of conditions and the following disclaimer in the |
12 | * notice, this list of conditions and the following disclaimer in the |
13 | * documentation and/or other materials provided with the distribution. |
13 | * documentation and/or other materials provided with the distribution. |
14 | * - The name of the author may not be used to endorse or promote products |
14 | * - The name of the author may not be used to endorse or promote products |
15 | * derived from this software without specific prior written permission. |
15 | * derived from this software without specific prior written permission. |
16 | * |
16 | * |
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
27 | */ |
27 | */ |
28 | 28 | ||
29 | /** @addtogroup genericconsole |
29 | /** @addtogroup genericconsole |
30 | * @{ |
30 | * @{ |
31 | */ |
31 | */ |
32 | 32 | ||
33 | /** |
33 | /** |
34 | * @file kconsole.c |
34 | * @file kconsole.c |
35 | * @brief Kernel console. |
35 | * @brief Kernel console. |
36 | * |
36 | * |
37 | * This file contains kernel thread managing the kernel console. |
37 | * This file contains kernel thread managing the kernel console. |
38 | * |
38 | * |
39 | */ |
39 | */ |
40 | 40 | ||
41 | #include <console/kconsole.h> |
41 | #include <console/kconsole.h> |
42 | #include <console/console.h> |
42 | #include <console/console.h> |
43 | #include <console/chardev.h> |
43 | #include <console/chardev.h> |
44 | #include <console/cmd.h> |
44 | #include <console/cmd.h> |
45 | #include <print.h> |
45 | #include <print.h> |
46 | #include <panic.h> |
46 | #include <panic.h> |
47 | #include <arch/types.h> |
47 | #include <arch/types.h> |
48 | #include <adt/list.h> |
48 | #include <adt/list.h> |
49 | #include <arch.h> |
49 | #include <arch.h> |
50 | #include <macros.h> |
50 | #include <macros.h> |
51 | #include <debug.h> |
51 | #include <debug.h> |
52 | #include <func.h> |
52 | #include <func.h> |
53 | #include <string.h> |
53 | #include <string.h> |
54 | #include <macros.h> |
54 | #include <macros.h> |
55 | #include <sysinfo/sysinfo.h> |
55 | #include <sysinfo/sysinfo.h> |
56 | #include <ddi/device.h> |
56 | #include <ddi/device.h> |
57 | #include <symtab.h> |
57 | #include <symtab.h> |
58 | #include <errno.h> |
58 | #include <errno.h> |
59 | #include <putchar.h> |
59 | #include <putchar.h> |
60 | #include <string.h> |
60 | #include <string.h> |
61 | 61 | ||
62 | /** Simple kernel console. |
62 | /** Simple kernel console. |
63 | * |
63 | * |
64 | * The console is realized by kernel thread kconsole. |
64 | * The console is realized by kernel thread kconsole. |
65 | * It doesn't understand any useful command on its own, |
65 | * It doesn't understand any useful command on its own, |
66 | * but makes it possible for other kernel subsystems to |
66 | * but makes it possible for other kernel subsystems to |
67 | * register their own commands. |
67 | * register their own commands. |
68 | */ |
68 | */ |
69 | 69 | ||
70 | /** Locking. |
70 | /** Locking. |
71 | * |
71 | * |
72 | * There is a list of cmd_info_t structures. This list |
72 | * There is a list of cmd_info_t structures. This list |
73 | * is protected by cmd_lock spinlock. Note that specially |
73 | * is protected by cmd_lock spinlock. Note that specially |
74 | * the link elements of cmd_info_t are protected by |
74 | * the link elements of cmd_info_t are protected by |
75 | * this lock. |
75 | * this lock. |
76 | * |
76 | * |
77 | * Each cmd_info_t also has its own lock, which protects |
77 | * Each cmd_info_t also has its own lock, which protects |
78 | * all elements thereof except the link element. |
78 | * all elements thereof except the link element. |
79 | * |
79 | * |
80 | * cmd_lock must be acquired before any cmd_info lock. |
80 | * cmd_lock must be acquired before any cmd_info lock. |
81 | * When locking two cmd info structures, structure with |
81 | * When locking two cmd info structures, structure with |
82 | * lower address must be locked first. |
82 | * lower address must be locked first. |
83 | */ |
83 | */ |
84 | 84 | ||
85 | SPINLOCK_INITIALIZE(cmd_lock); /**< Lock protecting command list. */ |
85 | SPINLOCK_INITIALIZE(cmd_lock); /**< Lock protecting command list. */ |
86 | LIST_INITIALIZE(cmd_head); /**< Command list. */ |
86 | LIST_INITIALIZE(cmd_head); /**< Command list. */ |
87 | 87 | ||
88 | static wchar_t history[KCONSOLE_HISTORY][MAX_CMDLINE] = {}; |
88 | static wchar_t history[KCONSOLE_HISTORY][MAX_CMDLINE] = {}; |
89 | static count_t history_pos = 0; |
89 | static size_t history_pos = 0; |
90 | 90 | ||
91 | /** Initialize kconsole data structures |
91 | /** Initialize kconsole data structures |
92 | * |
92 | * |
93 | * This is the most basic initialization, almost no |
93 | * This is the most basic initialization, almost no |
94 | * other kernel subsystem is ready yet. |
94 | * other kernel subsystem is ready yet. |
95 | * |
95 | * |
96 | */ |
96 | */ |
97 | void kconsole_init(void) |
97 | void kconsole_init(void) |
98 | { |
98 | { |
99 | unsigned int i; |
99 | unsigned int i; |
100 | 100 | ||
101 | cmd_init(); |
101 | cmd_init(); |
102 | for (i = 0; i < KCONSOLE_HISTORY; i++) |
102 | for (i = 0; i < KCONSOLE_HISTORY; i++) |
103 | history[i][0] = 0; |
103 | history[i][0] = 0; |
104 | } |
104 | } |
105 | 105 | ||
106 | /** Register kconsole command. |
106 | /** Register kconsole command. |
107 | * |
107 | * |
108 | * @param cmd Structure describing the command. |
108 | * @param cmd Structure describing the command. |
109 | * |
109 | * |
110 | * @return False on failure, true on success. |
110 | * @return False on failure, true on success. |
111 | * |
111 | * |
112 | */ |
112 | */ |
113 | bool cmd_register(cmd_info_t *cmd) |
113 | bool cmd_register(cmd_info_t *cmd) |
114 | { |
114 | { |
115 | link_t *cur; |
115 | link_t *cur; |
116 | 116 | ||
117 | spinlock_lock(&cmd_lock); |
117 | spinlock_lock(&cmd_lock); |
118 | 118 | ||
119 | /* |
119 | /* |
120 | * Make sure the command is not already listed. |
120 | * Make sure the command is not already listed. |
121 | */ |
121 | */ |
122 | for (cur = cmd_head.next; cur != &cmd_head; cur = cur->next) { |
122 | for (cur = cmd_head.next; cur != &cmd_head; cur = cur->next) { |
123 | cmd_info_t *hlp = list_get_instance(cur, cmd_info_t, link); |
123 | cmd_info_t *hlp = list_get_instance(cur, cmd_info_t, link); |
124 | 124 | ||
125 | if (hlp == cmd) { |
125 | if (hlp == cmd) { |
126 | /* The command is already there. */ |
126 | /* The command is already there. */ |
127 | spinlock_unlock(&cmd_lock); |
127 | spinlock_unlock(&cmd_lock); |
128 | return false; |
128 | return false; |
129 | } |
129 | } |
130 | 130 | ||
131 | /* Avoid deadlock. */ |
131 | /* Avoid deadlock. */ |
132 | if (hlp < cmd) { |
132 | if (hlp < cmd) { |
133 | spinlock_lock(&hlp->lock); |
133 | spinlock_lock(&hlp->lock); |
134 | spinlock_lock(&cmd->lock); |
134 | spinlock_lock(&cmd->lock); |
135 | } else { |
135 | } else { |
136 | spinlock_lock(&cmd->lock); |
136 | spinlock_lock(&cmd->lock); |
137 | spinlock_lock(&hlp->lock); |
137 | spinlock_lock(&hlp->lock); |
138 | } |
138 | } |
139 | 139 | ||
140 | if (str_cmp(hlp->name, cmd->name) == 0) { |
140 | if (str_cmp(hlp->name, cmd->name) == 0) { |
141 | /* The command is already there. */ |
141 | /* The command is already there. */ |
142 | spinlock_unlock(&hlp->lock); |
142 | spinlock_unlock(&hlp->lock); |
143 | spinlock_unlock(&cmd->lock); |
143 | spinlock_unlock(&cmd->lock); |
144 | spinlock_unlock(&cmd_lock); |
144 | spinlock_unlock(&cmd_lock); |
145 | return false; |
145 | return false; |
146 | } |
146 | } |
147 | 147 | ||
148 | spinlock_unlock(&hlp->lock); |
148 | spinlock_unlock(&hlp->lock); |
149 | spinlock_unlock(&cmd->lock); |
149 | spinlock_unlock(&cmd->lock); |
150 | } |
150 | } |
151 | 151 | ||
152 | /* |
152 | /* |
153 | * Now the command can be added. |
153 | * Now the command can be added. |
154 | */ |
154 | */ |
155 | list_append(&cmd->link, &cmd_head); |
155 | list_append(&cmd->link, &cmd_head); |
156 | 156 | ||
157 | spinlock_unlock(&cmd_lock); |
157 | spinlock_unlock(&cmd_lock); |
158 | return true; |
158 | return true; |
159 | } |
159 | } |
160 | 160 | ||
161 | /** Print count times a character */ |
161 | /** Print count times a character */ |
162 | static void print_cc(wchar_t ch, count_t count) |
162 | static void print_cc(wchar_t ch, size_t count) |
163 | { |
163 | { |
164 | count_t i; |
164 | size_t i; |
165 | for (i = 0; i < count; i++) |
165 | for (i = 0; i < count; i++) |
166 | putchar(ch); |
166 | putchar(ch); |
167 | } |
167 | } |
168 | 168 | ||
169 | /** Try to find a command beginning with prefix */ |
169 | /** Try to find a command beginning with prefix */ |
170 | static const char *cmdtab_search_one(const char *name, link_t **startpos) |
170 | static const char *cmdtab_search_one(const char *name, link_t **startpos) |
171 | { |
171 | { |
172 | count_t namelen = str_length(name); |
172 | size_t namelen = str_length(name); |
173 | 173 | ||
174 | spinlock_lock(&cmd_lock); |
174 | spinlock_lock(&cmd_lock); |
175 | 175 | ||
176 | if (*startpos == NULL) |
176 | if (*startpos == NULL) |
177 | *startpos = cmd_head.next; |
177 | *startpos = cmd_head.next; |
178 | 178 | ||
179 | for (; *startpos != &cmd_head; *startpos = (*startpos)->next) { |
179 | for (; *startpos != &cmd_head; *startpos = (*startpos)->next) { |
180 | cmd_info_t *hlp = list_get_instance(*startpos, cmd_info_t, link); |
180 | cmd_info_t *hlp = list_get_instance(*startpos, cmd_info_t, link); |
181 | 181 | ||
182 | const char *curname = hlp->name; |
182 | const char *curname = hlp->name; |
183 | if (str_length(curname) < namelen) |
183 | if (str_length(curname) < namelen) |
184 | continue; |
184 | continue; |
185 | 185 | ||
186 | if (str_lcmp(curname, name, namelen) == 0) { |
186 | if (str_lcmp(curname, name, namelen) == 0) { |
187 | spinlock_unlock(&cmd_lock); |
187 | spinlock_unlock(&cmd_lock); |
188 | return (curname + str_lsize(curname, namelen)); |
188 | return (curname + str_lsize(curname, namelen)); |
189 | } |
189 | } |
190 | } |
190 | } |
191 | 191 | ||
192 | spinlock_unlock(&cmd_lock); |
192 | spinlock_unlock(&cmd_lock); |
193 | return NULL; |
193 | return NULL; |
194 | } |
194 | } |
195 | 195 | ||
196 | /** Command completion of the commands |
196 | /** Command completion of the commands |
197 | * |
197 | * |
198 | * @param name String to match, changed to hint on exit |
198 | * @param name String to match, changed to hint on exit |
199 | * @param size Input buffer size |
199 | * @param size Input buffer size |
200 | * |
200 | * |
201 | * @return Number of found matches |
201 | * @return Number of found matches |
202 | * |
202 | * |
203 | */ |
203 | */ |
204 | static int cmdtab_compl(char *input, size_t size) |
204 | static int cmdtab_compl(char *input, size_t size) |
205 | { |
205 | { |
206 | const char *name = input; |
206 | const char *name = input; |
207 | 207 | ||
208 | count_t found = 0; |
208 | size_t found = 0; |
209 | link_t *pos = NULL; |
209 | link_t *pos = NULL; |
210 | const char *hint; |
210 | const char *hint; |
211 | char output[MAX_CMDLINE]; |
211 | char output[MAX_CMDLINE]; |
212 | 212 | ||
213 | output[0] = 0; |
213 | output[0] = 0; |
214 | 214 | ||
215 | while ((hint = cmdtab_search_one(name, &pos))) { |
215 | while ((hint = cmdtab_search_one(name, &pos))) { |
216 | if ((found == 0) || (str_length(output) > str_length(hint))) |
216 | if ((found == 0) || (str_length(output) > str_length(hint))) |
217 | str_cpy(output, MAX_CMDLINE, hint); |
217 | str_cpy(output, MAX_CMDLINE, hint); |
218 | 218 | ||
219 | pos = pos->next; |
219 | pos = pos->next; |
220 | found++; |
220 | found++; |
221 | } |
221 | } |
222 | 222 | ||
223 | if ((found > 1) && (str_length(output) != 0)) { |
223 | if ((found > 1) && (str_length(output) != 0)) { |
224 | printf("\n"); |
224 | printf("\n"); |
225 | pos = NULL; |
225 | pos = NULL; |
226 | while ((hint = cmdtab_search_one(name, &pos))) { |
226 | while ((hint = cmdtab_search_one(name, &pos))) { |
227 | cmd_info_t *hlp = list_get_instance(pos, cmd_info_t, link); |
227 | cmd_info_t *hlp = list_get_instance(pos, cmd_info_t, link); |
228 | printf("%s (%s)\n", hlp->name, hlp->description); |
228 | printf("%s (%s)\n", hlp->name, hlp->description); |
229 | pos = pos->next; |
229 | pos = pos->next; |
230 | } |
230 | } |
231 | } |
231 | } |
232 | 232 | ||
233 | if (found > 0) |
233 | if (found > 0) |
234 | str_cpy(input, size, output); |
234 | str_cpy(input, size, output); |
235 | 235 | ||
236 | return found; |
236 | return found; |
237 | } |
237 | } |
238 | 238 | ||
239 | static wchar_t *clever_readline(const char *prompt, indev_t *indev) |
239 | static wchar_t *clever_readline(const char *prompt, indev_t *indev) |
240 | { |
240 | { |
241 | printf("%s> ", prompt); |
241 | printf("%s> ", prompt); |
242 | 242 | ||
243 | count_t position = 0; |
243 | size_t position = 0; |
244 | wchar_t *current = history[history_pos]; |
244 | wchar_t *current = history[history_pos]; |
245 | current[0] = 0; |
245 | current[0] = 0; |
246 | 246 | ||
247 | while (true) { |
247 | while (true) { |
248 | wchar_t ch = indev_pop_character(indev); |
248 | wchar_t ch = indev_pop_character(indev); |
249 | 249 | ||
250 | if (ch == '\n') { |
250 | if (ch == '\n') { |
251 | /* Enter */ |
251 | /* Enter */ |
252 | putchar(ch); |
252 | putchar(ch); |
253 | break; |
253 | break; |
254 | } |
254 | } |
255 | 255 | ||
256 | if (ch == '\b') { |
256 | if (ch == '\b') { |
257 | /* Backspace */ |
257 | /* Backspace */ |
258 | if (position == 0) |
258 | if (position == 0) |
259 | continue; |
259 | continue; |
260 | 260 | ||
261 | if (wstr_remove(current, position - 1)) { |
261 | if (wstr_remove(current, position - 1)) { |
262 | position--; |
262 | position--; |
263 | putchar('\b'); |
263 | putchar('\b'); |
264 | printf("%ls ", current + position); |
264 | printf("%ls ", current + position); |
265 | print_cc('\b', wstr_length(current) - position + 1); |
265 | print_cc('\b', wstr_length(current) - position + 1); |
266 | continue; |
266 | continue; |
267 | } |
267 | } |
268 | } |
268 | } |
269 | 269 | ||
270 | if (ch == '\t') { |
270 | if (ch == '\t') { |
271 | /* Tab completion */ |
271 | /* Tab completion */ |
272 | 272 | ||
273 | /* Move to the end of the word */ |
273 | /* Move to the end of the word */ |
274 | for (; (current[position] != 0) && (!isspace(current[position])); |
274 | for (; (current[position] != 0) && (!isspace(current[position])); |
275 | position++) |
275 | position++) |
276 | putchar(current[position]); |
276 | putchar(current[position]); |
277 | 277 | ||
278 | if (position == 0) |
278 | if (position == 0) |
279 | continue; |
279 | continue; |
280 | 280 | ||
281 | /* Find the beginning of the word |
281 | /* Find the beginning of the word |
282 | and copy it to tmp */ |
282 | and copy it to tmp */ |
283 | count_t beg; |
283 | size_t beg; |
284 | for (beg = position - 1; (beg > 0) && (!isspace(current[beg])); |
284 | for (beg = position - 1; (beg > 0) && (!isspace(current[beg])); |
285 | beg--); |
285 | beg--); |
286 | 286 | ||
287 | if (isspace(current[beg])) |
287 | if (isspace(current[beg])) |
288 | beg++; |
288 | beg++; |
289 | 289 | ||
290 | char tmp[STR_BOUNDS(MAX_CMDLINE)]; |
290 | char tmp[STR_BOUNDS(MAX_CMDLINE)]; |
291 | wstr_nstr(tmp, current + beg, position - beg + 1); |
291 | wstr_nstr(tmp, current + beg, position - beg + 1); |
292 | 292 | ||
293 | int found; |
293 | int found; |
294 | if (beg == 0) { |
294 | if (beg == 0) { |
295 | /* Command completion */ |
295 | /* Command completion */ |
296 | found = cmdtab_compl(tmp, STR_BOUNDS(MAX_CMDLINE)); |
296 | found = cmdtab_compl(tmp, STR_BOUNDS(MAX_CMDLINE)); |
297 | } else { |
297 | } else { |
298 | /* Symbol completion */ |
298 | /* Symbol completion */ |
299 | found = symtab_compl(tmp, STR_BOUNDS(MAX_CMDLINE)); |
299 | found = symtab_compl(tmp, STR_BOUNDS(MAX_CMDLINE)); |
300 | } |
300 | } |
301 | 301 | ||
302 | if (found == 0) |
302 | if (found == 0) |
303 | continue; |
303 | continue; |
304 | 304 | ||
305 | if (found > 1) { |
305 | if (found > 1) { |
306 | /* No unique hint, list was printed */ |
306 | /* No unique hint, list was printed */ |
307 | printf("%s> ", prompt); |
307 | printf("%s> ", prompt); |
308 | printf("%ls", current); |
308 | printf("%ls", current); |
309 | print_cc('\b', wstr_length(current) - position); |
309 | print_cc('\b', wstr_length(current) - position); |
310 | continue; |
310 | continue; |
311 | } |
311 | } |
312 | 312 | ||
313 | /* We have a hint */ |
313 | /* We have a hint */ |
314 | 314 | ||
315 | size_t off = 0; |
315 | size_t off = 0; |
316 | count_t i = 0; |
316 | size_t i = 0; |
317 | while ((ch = str_decode(tmp, &off, STR_NO_LIMIT)) != 0) { |
317 | while ((ch = str_decode(tmp, &off, STR_NO_LIMIT)) != 0) { |
318 | if (!wstr_linsert(current, ch, position + i, MAX_CMDLINE)) |
318 | if (!wstr_linsert(current, ch, position + i, MAX_CMDLINE)) |
319 | break; |
319 | break; |
320 | i++; |
320 | i++; |
321 | } |
321 | } |
322 | 322 | ||
323 | printf("%ls", current + position); |
323 | printf("%ls", current + position); |
324 | position += str_length(tmp); |
324 | position += str_length(tmp); |
325 | print_cc('\b', wstr_length(current) - position); |
325 | print_cc('\b', wstr_length(current) - position); |
326 | 326 | ||
327 | if (position == wstr_length(current)) { |
327 | if (position == wstr_length(current)) { |
328 | /* Insert a space after the last completed argument */ |
328 | /* Insert a space after the last completed argument */ |
329 | if (wstr_linsert(current, ' ', position, MAX_CMDLINE)) { |
329 | if (wstr_linsert(current, ' ', position, MAX_CMDLINE)) { |
330 | printf("%ls", current + position); |
330 | printf("%ls", current + position); |
331 | position++; |
331 | position++; |
332 | } |
332 | } |
333 | } |
333 | } |
334 | continue; |
334 | continue; |
335 | } |
335 | } |
336 | 336 | ||
337 | if (ch == U_LEFT_ARROW) { |
337 | if (ch == U_LEFT_ARROW) { |
338 | /* Left */ |
338 | /* Left */ |
339 | if (position > 0) { |
339 | if (position > 0) { |
340 | putchar('\b'); |
340 | putchar('\b'); |
341 | position--; |
341 | position--; |
342 | } |
342 | } |
343 | continue; |
343 | continue; |
344 | } |
344 | } |
345 | 345 | ||
346 | if (ch == U_RIGHT_ARROW) { |
346 | if (ch == U_RIGHT_ARROW) { |
347 | /* Right */ |
347 | /* Right */ |
348 | if (position < wstr_length(current)) { |
348 | if (position < wstr_length(current)) { |
349 | putchar(current[position]); |
349 | putchar(current[position]); |
350 | position++; |
350 | position++; |
351 | } |
351 | } |
352 | continue; |
352 | continue; |
353 | } |
353 | } |
354 | 354 | ||
355 | if ((ch == U_UP_ARROW) || (ch == U_DOWN_ARROW)) { |
355 | if ((ch == U_UP_ARROW) || (ch == U_DOWN_ARROW)) { |
356 | /* Up, down */ |
356 | /* Up, down */ |
357 | print_cc('\b', position); |
357 | print_cc('\b', position); |
358 | print_cc(' ', wstr_length(current)); |
358 | print_cc(' ', wstr_length(current)); |
359 | print_cc('\b', wstr_length(current)); |
359 | print_cc('\b', wstr_length(current)); |
360 | 360 | ||
361 | if (ch == U_UP_ARROW) { |
361 | if (ch == U_UP_ARROW) { |
362 | /* Up */ |
362 | /* Up */ |
363 | if (history_pos == 0) |
363 | if (history_pos == 0) |
364 | history_pos = KCONSOLE_HISTORY - 1; |
364 | history_pos = KCONSOLE_HISTORY - 1; |
365 | else |
365 | else |
366 | history_pos--; |
366 | history_pos--; |
367 | } else { |
367 | } else { |
368 | /* Down */ |
368 | /* Down */ |
369 | history_pos++; |
369 | history_pos++; |
370 | history_pos = history_pos % KCONSOLE_HISTORY; |
370 | history_pos = history_pos % KCONSOLE_HISTORY; |
371 | } |
371 | } |
372 | current = history[history_pos]; |
372 | current = history[history_pos]; |
373 | printf("%ls", current); |
373 | printf("%ls", current); |
374 | position = wstr_length(current); |
374 | position = wstr_length(current); |
375 | continue; |
375 | continue; |
376 | } |
376 | } |
377 | 377 | ||
378 | if (ch == U_HOME_ARROW) { |
378 | if (ch == U_HOME_ARROW) { |
379 | /* Home */ |
379 | /* Home */ |
380 | print_cc('\b', position); |
380 | print_cc('\b', position); |
381 | position = 0; |
381 | position = 0; |
382 | continue; |
382 | continue; |
383 | } |
383 | } |
384 | 384 | ||
385 | if (ch == U_END_ARROW) { |
385 | if (ch == U_END_ARROW) { |
386 | /* End */ |
386 | /* End */ |
387 | printf("%ls", current + position); |
387 | printf("%ls", current + position); |
388 | position = wstr_length(current); |
388 | position = wstr_length(current); |
389 | continue; |
389 | continue; |
390 | } |
390 | } |
391 | 391 | ||
392 | if (ch == U_DELETE) { |
392 | if (ch == U_DELETE) { |
393 | /* Delete */ |
393 | /* Delete */ |
394 | if (position == wstr_length(current)) |
394 | if (position == wstr_length(current)) |
395 | continue; |
395 | continue; |
396 | 396 | ||
397 | if (wstr_remove(current, position)) { |
397 | if (wstr_remove(current, position)) { |
398 | printf("%ls ", current + position); |
398 | printf("%ls ", current + position); |
399 | print_cc('\b', wstr_length(current) - position + 1); |
399 | print_cc('\b', wstr_length(current) - position + 1); |
400 | } |
400 | } |
401 | continue; |
401 | continue; |
402 | } |
402 | } |
403 | 403 | ||
404 | if (wstr_linsert(current, ch, position, MAX_CMDLINE)) { |
404 | if (wstr_linsert(current, ch, position, MAX_CMDLINE)) { |
405 | printf("%ls", current + position); |
405 | printf("%ls", current + position); |
406 | position++; |
406 | position++; |
407 | print_cc('\b', wstr_length(current) - position); |
407 | print_cc('\b', wstr_length(current) - position); |
408 | } |
408 | } |
409 | } |
409 | } |
410 | 410 | ||
411 | if (wstr_length(current) > 0) { |
411 | if (wstr_length(current) > 0) { |
412 | history_pos++; |
412 | history_pos++; |
413 | history_pos = history_pos % KCONSOLE_HISTORY; |
413 | history_pos = history_pos % KCONSOLE_HISTORY; |
414 | } |
414 | } |
415 | 415 | ||
416 | return current; |
416 | return current; |
417 | } |
417 | } |
418 | 418 | ||
419 | bool kconsole_check_poll(void) |
419 | bool kconsole_check_poll(void) |
420 | { |
420 | { |
421 | return check_poll(stdin); |
421 | return check_poll(stdin); |
422 | } |
422 | } |
423 | 423 | ||
424 | static bool parse_int_arg(const char *text, size_t len, unative_t *result) |
424 | static bool parse_int_arg(const char *text, size_t len, unative_t *result) |
425 | { |
425 | { |
426 | bool isaddr = false; |
426 | bool isaddr = false; |
427 | bool isptr = false; |
427 | bool isptr = false; |
428 | 428 | ||
429 | /* If we get a name, try to find it in symbol table */ |
429 | /* If we get a name, try to find it in symbol table */ |
430 | if (text[0] == '&') { |
430 | if (text[0] == '&') { |
431 | isaddr = true; |
431 | isaddr = true; |
432 | text++; |
432 | text++; |
433 | len--; |
433 | len--; |
434 | } else if (text[0] == '*') { |
434 | } else if (text[0] == '*') { |
435 | isptr = true; |
435 | isptr = true; |
436 | text++; |
436 | text++; |
437 | len--; |
437 | len--; |
438 | } |
438 | } |
439 | 439 | ||
440 | if ((text[0] < '0') || (text[0] > '9')) { |
440 | if ((text[0] < '0') || (text[0] > '9')) { |
441 | char symname[MAX_SYMBOL_NAME]; |
441 | char symname[MAX_SYMBOL_NAME]; |
442 | str_ncpy(symname, MAX_SYMBOL_NAME, text, len + 1); |
442 | str_ncpy(symname, MAX_SYMBOL_NAME, text, len + 1); |
443 | 443 | ||
444 | uintptr_t symaddr; |
444 | uintptr_t symaddr; |
445 | int rc = symtab_addr_lookup(symname, &symaddr); |
445 | int rc = symtab_addr_lookup(symname, &symaddr); |
446 | switch (rc) { |
446 | switch (rc) { |
447 | case ENOENT: |
447 | case ENOENT: |
448 | printf("Symbol %s not found.\n", symname); |
448 | printf("Symbol %s not found.\n", symname); |
449 | return false; |
449 | return false; |
450 | case EOVERFLOW: |
450 | case EOVERFLOW: |
451 | printf("Duplicate symbol %s.\n", symname); |
451 | printf("Duplicate symbol %s.\n", symname); |
452 | symtab_print_search(symname); |
452 | symtab_print_search(symname); |
453 | return false; |
453 | return false; |
454 | case ENOTSUP: |
454 | case ENOTSUP: |
455 | printf("No symbol information available.\n"); |
455 | printf("No symbol information available.\n"); |
456 | return false; |
456 | return false; |
457 | } |
457 | } |
458 | 458 | ||
459 | if (isaddr) |
459 | if (isaddr) |
460 | *result = (unative_t) symaddr; |
460 | *result = (unative_t) symaddr; |
461 | else if (isptr) |
461 | else if (isptr) |
462 | *result = **((unative_t **) symaddr); |
462 | *result = **((unative_t **) symaddr); |
463 | else |
463 | else |
464 | *result = *((unative_t *) symaddr); |
464 | *result = *((unative_t *) symaddr); |
465 | } else { |
465 | } else { |
466 | /* It's a number - convert it */ |
466 | /* It's a number - convert it */ |
467 | *result = atoi(text); |
467 | *result = atoi(text); |
468 | if (isptr) |
468 | if (isptr) |
469 | *result = *((unative_t *) *result); |
469 | *result = *((unative_t *) *result); |
470 | } |
470 | } |
471 | 471 | ||
472 | return true; |
472 | return true; |
473 | } |
473 | } |
474 | 474 | ||
475 | /** Parse argument. |
475 | /** Parse argument. |
476 | * |
476 | * |
477 | * Find start and end positions of command line argument. |
477 | * Find start and end positions of command line argument. |
478 | * |
478 | * |
479 | * @param cmdline Command line as read from the input device. |
479 | * @param cmdline Command line as read from the input device. |
480 | * @param size Size (in bytes) of the string. |
480 | * @param size Size (in bytes) of the string. |
481 | * @param start On entry, 'start' contains pointer to the offset |
481 | * @param start On entry, 'start' contains pointer to the offset |
482 | * of the first unprocessed character of cmdline. |
482 | * of the first unprocessed character of cmdline. |
483 | * On successful exit, it marks beginning of the next argument. |
483 | * On successful exit, it marks beginning of the next argument. |
484 | * @param end Undefined on entry. On exit, 'end' is the offset of the first |
484 | * @param end Undefined on entry. On exit, 'end' is the offset of the first |
485 | * character behind the next argument. |
485 | * character behind the next argument. |
486 | * |
486 | * |
487 | * @return False on failure, true on success. |
487 | * @return False on failure, true on success. |
488 | * |
488 | * |
489 | */ |
489 | */ |
490 | static bool parse_argument(const char *cmdline, size_t size, size_t *start, size_t *end) |
490 | static bool parse_argument(const char *cmdline, size_t size, size_t *start, size_t *end) |
491 | { |
491 | { |
492 | ASSERT(start != NULL); |
492 | ASSERT(start != NULL); |
493 | ASSERT(end != NULL); |
493 | ASSERT(end != NULL); |
494 | 494 | ||
495 | bool found_start = false; |
495 | bool found_start = false; |
496 | size_t offset = *start; |
496 | size_t offset = *start; |
497 | size_t prev = *start; |
497 | size_t prev = *start; |
498 | wchar_t ch; |
498 | wchar_t ch; |
499 | 499 | ||
500 | while ((ch = str_decode(cmdline, &offset, size)) != 0) { |
500 | while ((ch = str_decode(cmdline, &offset, size)) != 0) { |
501 | if (!found_start) { |
501 | if (!found_start) { |
502 | if (!isspace(ch)) { |
502 | if (!isspace(ch)) { |
503 | *start = prev; |
503 | *start = prev; |
504 | found_start = true; |
504 | found_start = true; |
505 | } |
505 | } |
506 | } else { |
506 | } else { |
507 | if (isspace(ch)) |
507 | if (isspace(ch)) |
508 | break; |
508 | break; |
509 | } |
509 | } |
510 | 510 | ||
511 | prev = offset; |
511 | prev = offset; |
512 | } |
512 | } |
513 | *end = prev; |
513 | *end = prev; |
514 | 514 | ||
515 | return found_start; |
515 | return found_start; |
516 | } |
516 | } |
517 | 517 | ||
518 | /** Parse command line. |
518 | /** Parse command line. |
519 | * |
519 | * |
520 | * @param cmdline Command line as read from input device. |
520 | * @param cmdline Command line as read from input device. |
521 | * @param size Size (in bytes) of the string. |
521 | * @param size Size (in bytes) of the string. |
522 | * |
522 | * |
523 | * @return Structure describing the command. |
523 | * @return Structure describing the command. |
524 | * |
524 | * |
525 | */ |
525 | */ |
526 | static cmd_info_t *parse_cmdline(const char *cmdline, size_t size) |
526 | static cmd_info_t *parse_cmdline(const char *cmdline, size_t size) |
527 | { |
527 | { |
528 | size_t start = 0; |
528 | size_t start = 0; |
529 | size_t end = 0; |
529 | size_t end = 0; |
530 | if (!parse_argument(cmdline, size, &start, &end)) { |
530 | if (!parse_argument(cmdline, size, &start, &end)) { |
531 | /* Command line did not contain alphanumeric word. */ |
531 | /* Command line did not contain alphanumeric word. */ |
532 | return NULL; |
532 | return NULL; |
533 | } |
533 | } |
534 | spinlock_lock(&cmd_lock); |
534 | spinlock_lock(&cmd_lock); |
535 | 535 | ||
536 | cmd_info_t *cmd = NULL; |
536 | cmd_info_t *cmd = NULL; |
537 | link_t *cur; |
537 | link_t *cur; |
538 | 538 | ||
539 | for (cur = cmd_head.next; cur != &cmd_head; cur = cur->next) { |
539 | for (cur = cmd_head.next; cur != &cmd_head; cur = cur->next) { |
540 | cmd_info_t *hlp = list_get_instance(cur, cmd_info_t, link); |
540 | cmd_info_t *hlp = list_get_instance(cur, cmd_info_t, link); |
541 | spinlock_lock(&hlp->lock); |
541 | spinlock_lock(&hlp->lock); |
542 | 542 | ||
543 | if (str_lcmp(hlp->name, cmdline + start, |
543 | if (str_lcmp(hlp->name, cmdline + start, |
544 | max(str_length(hlp->name), |
544 | max(str_length(hlp->name), |
545 | str_nlength(cmdline + start, (count_t) (end - start) - 1))) == 0) { |
545 | str_nlength(cmdline + start, (size_t) (end - start) - 1))) == 0) { |
546 | cmd = hlp; |
546 | cmd = hlp; |
547 | break; |
547 | break; |
548 | } |
548 | } |
549 | 549 | ||
550 | spinlock_unlock(&hlp->lock); |
550 | spinlock_unlock(&hlp->lock); |
551 | } |
551 | } |
552 | 552 | ||
553 | spinlock_unlock(&cmd_lock); |
553 | spinlock_unlock(&cmd_lock); |
554 | 554 | ||
555 | if (!cmd) { |
555 | if (!cmd) { |
556 | /* Unknown command. */ |
556 | /* Unknown command. */ |
557 | printf("Unknown command.\n"); |
557 | printf("Unknown command.\n"); |
558 | return NULL; |
558 | return NULL; |
559 | } |
559 | } |
560 | 560 | ||
561 | /* cmd == hlp is locked */ |
561 | /* cmd == hlp is locked */ |
562 | 562 | ||
563 | /* |
563 | /* |
564 | * The command line must be further analyzed and |
564 | * The command line must be further analyzed and |
565 | * the parameters therefrom must be matched and |
565 | * the parameters therefrom must be matched and |
566 | * converted to those specified in the cmd info |
566 | * converted to those specified in the cmd info |
567 | * structure. |
567 | * structure. |
568 | */ |
568 | */ |
569 | 569 | ||
570 | bool error = false; |
570 | bool error = false; |
571 | count_t i; |
571 | size_t i; |
572 | for (i = 0; i < cmd->argc; i++) { |
572 | for (i = 0; i < cmd->argc; i++) { |
573 | start = end; |
573 | start = end; |
574 | if (!parse_argument(cmdline, size, &start, &end)) { |
574 | if (!parse_argument(cmdline, size, &start, &end)) { |
575 | printf("Too few arguments.\n"); |
575 | printf("Too few arguments.\n"); |
576 | spinlock_unlock(&cmd->lock); |
576 | spinlock_unlock(&cmd->lock); |
577 | return NULL; |
577 | return NULL; |
578 | } |
578 | } |
579 | 579 | ||
580 | char *buf; |
580 | char *buf; |
581 | switch (cmd->argv[i].type) { |
581 | switch (cmd->argv[i].type) { |
582 | case ARG_TYPE_STRING: |
582 | case ARG_TYPE_STRING: |
583 | buf = (char *) cmd->argv[i].buffer; |
583 | buf = (char *) cmd->argv[i].buffer; |
584 | str_ncpy(buf, cmd->argv[i].len, cmdline + start, |
584 | str_ncpy(buf, cmd->argv[i].len, cmdline + start, |
585 | end - start); |
585 | end - start); |
586 | break; |
586 | break; |
587 | case ARG_TYPE_INT: |
587 | case ARG_TYPE_INT: |
588 | if (!parse_int_arg(cmdline + start, end - start, |
588 | if (!parse_int_arg(cmdline + start, end - start, |
589 | &cmd->argv[i].intval)) |
589 | &cmd->argv[i].intval)) |
590 | error = true; |
590 | error = true; |
591 | break; |
591 | break; |
592 | case ARG_TYPE_VAR: |
592 | case ARG_TYPE_VAR: |
593 | if ((start < end - 1) && (cmdline[start] == '"')) { |
593 | if ((start < end - 1) && (cmdline[start] == '"')) { |
594 | if (cmdline[end - 1] == '"') { |
594 | if (cmdline[end - 1] == '"') { |
595 | buf = (char *) cmd->argv[i].buffer; |
595 | buf = (char *) cmd->argv[i].buffer; |
596 | str_ncpy(buf, cmd->argv[i].len, |
596 | str_ncpy(buf, cmd->argv[i].len, |
597 | cmdline + start + 1, |
597 | cmdline + start + 1, |
598 | (end - start) - 1); |
598 | (end - start) - 1); |
599 | cmd->argv[i].intval = (unative_t) buf; |
599 | cmd->argv[i].intval = (unative_t) buf; |
600 | cmd->argv[i].vartype = ARG_TYPE_STRING; |
600 | cmd->argv[i].vartype = ARG_TYPE_STRING; |
601 | } else { |
601 | } else { |
602 | printf("Wrong synxtax.\n"); |
602 | printf("Wrong synxtax.\n"); |
603 | error = true; |
603 | error = true; |
604 | } |
604 | } |
605 | } else if (parse_int_arg(cmdline + start, |
605 | } else if (parse_int_arg(cmdline + start, |
606 | end - start, &cmd->argv[i].intval)) { |
606 | end - start, &cmd->argv[i].intval)) { |
607 | cmd->argv[i].vartype = ARG_TYPE_INT; |
607 | cmd->argv[i].vartype = ARG_TYPE_INT; |
608 | } else { |
608 | } else { |
609 | printf("Unrecognized variable argument.\n"); |
609 | printf("Unrecognized variable argument.\n"); |
610 | error = true; |
610 | error = true; |
611 | } |
611 | } |
612 | break; |
612 | break; |
613 | case ARG_TYPE_INVALID: |
613 | case ARG_TYPE_INVALID: |
614 | default: |
614 | default: |
615 | printf("Invalid argument type\n"); |
615 | printf("Invalid argument type\n"); |
616 | error = true; |
616 | error = true; |
617 | break; |
617 | break; |
618 | } |
618 | } |
619 | } |
619 | } |
620 | 620 | ||
621 | if (error) { |
621 | if (error) { |
622 | spinlock_unlock(&cmd->lock); |
622 | spinlock_unlock(&cmd->lock); |
623 | return NULL; |
623 | return NULL; |
624 | } |
624 | } |
625 | 625 | ||
626 | start = end; |
626 | start = end; |
627 | if (parse_argument(cmdline, size, &start, &end)) { |
627 | if (parse_argument(cmdline, size, &start, &end)) { |
628 | printf("Too many arguments.\n"); |
628 | printf("Too many arguments.\n"); |
629 | spinlock_unlock(&cmd->lock); |
629 | spinlock_unlock(&cmd->lock); |
630 | return NULL; |
630 | return NULL; |
631 | } |
631 | } |
632 | 632 | ||
633 | spinlock_unlock(&cmd->lock); |
633 | spinlock_unlock(&cmd->lock); |
634 | return cmd; |
634 | return cmd; |
635 | } |
635 | } |
636 | 636 | ||
637 | /** Kernel console prompt. |
637 | /** Kernel console prompt. |
638 | * |
638 | * |
639 | * @param prompt Kernel console prompt (e.g kconsole/panic). |
639 | * @param prompt Kernel console prompt (e.g kconsole/panic). |
640 | * @param msg Message to display in the beginning. |
640 | * @param msg Message to display in the beginning. |
641 | * @param kcon Wait for keypress to show the prompt |
641 | * @param kcon Wait for keypress to show the prompt |
642 | * and never exit. |
642 | * and never exit. |
643 | * |
643 | * |
644 | */ |
644 | */ |
645 | void kconsole(char *prompt, char *msg, bool kcon) |
645 | void kconsole(char *prompt, char *msg, bool kcon) |
646 | { |
646 | { |
647 | if (!stdin) { |
647 | if (!stdin) { |
648 | LOG("No stdin for kernel console"); |
648 | LOG("No stdin for kernel console"); |
649 | return; |
649 | return; |
650 | } |
650 | } |
651 | 651 | ||
652 | if (msg) |
652 | if (msg) |
653 | printf("%s", msg); |
653 | printf("%s", msg); |
654 | 654 | ||
655 | if (kcon) |
655 | if (kcon) |
656 | indev_pop_character(stdin); |
656 | indev_pop_character(stdin); |
657 | else |
657 | else |
658 | printf("Type \"exit\" to leave the console.\n"); |
658 | printf("Type \"exit\" to leave the console.\n"); |
659 | 659 | ||
660 | while (true) { |
660 | while (true) { |
661 | wchar_t *tmp = clever_readline((char *) prompt, stdin); |
661 | wchar_t *tmp = clever_readline((char *) prompt, stdin); |
662 | count_t len = wstr_length(tmp); |
662 | size_t len = wstr_length(tmp); |
663 | if (!len) |
663 | if (!len) |
664 | continue; |
664 | continue; |
665 | 665 | ||
666 | char cmdline[STR_BOUNDS(MAX_CMDLINE)]; |
666 | char cmdline[STR_BOUNDS(MAX_CMDLINE)]; |
667 | wstr_nstr(cmdline, tmp, STR_BOUNDS(MAX_CMDLINE)); |
667 | wstr_nstr(cmdline, tmp, STR_BOUNDS(MAX_CMDLINE)); |
668 | 668 | ||
669 | if ((!kcon) && (len == 4) && (str_lcmp(cmdline, "exit", 4) == 0)) |
669 | if ((!kcon) && (len == 4) && (str_lcmp(cmdline, "exit", 4) == 0)) |
670 | break; |
670 | break; |
671 | 671 | ||
672 | cmd_info_t *cmd_info = parse_cmdline(cmdline, STR_BOUNDS(MAX_CMDLINE)); |
672 | cmd_info_t *cmd_info = parse_cmdline(cmdline, STR_BOUNDS(MAX_CMDLINE)); |
673 | if (!cmd_info) |
673 | if (!cmd_info) |
674 | continue; |
674 | continue; |
675 | 675 | ||
676 | (void) cmd_info->func(cmd_info->argv); |
676 | (void) cmd_info->func(cmd_info->argv); |
677 | } |
677 | } |
678 | } |
678 | } |
679 | 679 | ||
680 | /** Kernel console managing thread. |
680 | /** Kernel console managing thread. |
681 | * |
681 | * |
682 | */ |
682 | */ |
683 | void kconsole_thread(void *data) |
683 | void kconsole_thread(void *data) |
684 | { |
684 | { |
685 | kconsole("kconsole", "Kernel console ready (press any key to activate)\n", true); |
685 | kconsole("kconsole", "Kernel console ready (press any key to activate)\n", true); |
686 | } |
686 | } |
687 | 687 | ||
688 | /** @} |
688 | /** @} |
689 | */ |
689 | */ |
690 | 690 |