Subversion Repositories HelenOS

Rev

Rev 4217 | Rev 4224 | Go to most recent revision | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 4217 Rev 4223
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 count_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, count_t count)
163
{
163
{
164
    count_t i;
164
    count_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
    count_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
    count_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_ncpy(output, hint, MAX_CMDLINE);
217
            str_ncpy(output, hint, MAX_CMDLINE);
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_ncpy(input, output, size);
234
        str_ncpy(input, output, size);
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
    count_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 = _getc(indev);
248
        wchar_t ch = _getc(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
                putchar('\b');
263
                putchar('\b');
263
                printf("%ls", current + position);
264
                printf("%ls ", current + position);
264
                position--;
-
 
265
                print_cc('\b', wstr_length(current) - position);
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
            count_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
            size_t off = 0;
305
            size_t off = 0;
306
            count_t i = 0;
306
            count_t i = 0;
307
            while ((ch = str_decode(tmp, &off, STR_NO_LIMIT)) != 0) {
307
            while ((ch = str_decode(tmp, &off, STR_NO_LIMIT)) != 0) {
308
                if (!wstr_linsert(current, ch, position + i, MAX_CMDLINE))
308
                if (!wstr_linsert(current, ch, position + i, MAX_CMDLINE))
309
                    break;
309
                    break;
310
                i++;
310
                i++;
311
            }
311
            }
312
           
312
           
313
            if ((str_length(tmp) > 0) || (found == 1)) {
313
            if ((str_length(tmp) > 0) || (found == 1)) {
314
                /* We have a hint */
314
                /* We have a hint */
315
                printf("%ls", current + position);
315
                printf("%ls", current + position);
316
                print_cc('\b', wstr_length(current) - position);
316
                print_cc('\b', wstr_length(current) - position);
317
                position += str_length(tmp);
317
                position += str_length(tmp);
318
               
318
               
319
                if ((found == 1) && (position == wstr_length(current))) {
319
                if ((found == 1) && (position == wstr_length(current))) {
320
                    if (wstr_linsert(current, ' ', position, MAX_CMDLINE)) {
320
                    if (wstr_linsert(current, ' ', position, MAX_CMDLINE)) {
321
                        printf("%ls", current + position);
321
                        printf("%ls", current + position);
322
                        position++;
322
                        position++;
323
                    }
323
                    }
324
                }
324
                }
325
            } else {
325
            } else {
326
                /* No unique hint, list was printed */
326
                /* No unique hint, list was printed */
327
                printf("%s> ", prompt);
327
                printf("%s> ", prompt);
328
                printf("%ls", current);
328
                printf("%ls", current);
329
                position += str_length(tmp);
329
                position += str_length(tmp);
330
            }
330
            }
331
           
331
           
332
            print_cc('\b', wstr_length(current) - position);
332
            print_cc('\b', wstr_length(current) - position);
333
            continue;
333
            continue;
334
        }
334
        }
335
       
335
       
336
        if (ch == 0x1b) {
336
        if (ch == U_LEFT_ARROW) {
337
            /* Special command */
-
 
338
            wchar_t mod = _getc(indev);
-
 
339
            wchar_t ch = _getc(indev);
-
 
340
           
-
 
341
            if ((mod != 0x5b) && (mod != 0x4f))
-
 
342
                continue;
-
 
343
           
-
 
344
            if ((ch == 0x33) && (_getc(indev) == 0x7e)) {
-
 
345
                /* Delete */
-
 
346
                if (position == wstr_length(current))
-
 
347
                    continue;
-
 
348
               
-
 
349
                if (wstr_remove(current, position)) {
-
 
350
                    putchar('\b');
-
 
351
                    printf("%ls", current + position);
-
 
352
                    position--;
-
 
353
                    print_cc('\b', wstr_length(current) - position);
-
 
354
                }
-
 
355
            } else if (ch == 0x48) {
-
 
356
                /* Home */
-
 
357
                print_cc('\b', position);
-
 
358
                position = 0;
-
 
359
            } else if (ch == 0x46) {
-
 
360
                /* End */
-
 
361
                printf("%ls", current + position);
-
 
362
                position = wstr_length(current);
-
 
363
            } else if (ch == 0x44) {
-
 
364
                /* Left */
337
            /* Left */
365
                if (position > 0) {
338
            if (position > 0) {
366
                    putchar('\b');
339
                putchar('\b');
367
                    position--;
340
                position--;
368
                }
341
            }
-
 
342
            continue;
-
 
343
        }
-
 
344
       
369
            } else if (ch == 0x43) {
345
        if (ch == U_RIGHT_ARROW) {
370
                /* Right */
346
            /* Right */
371
                if (position < wstr_length(current)) {
347
            if (position < wstr_length(current)) {
372
                    putchar(current[position]);
348
                putchar(current[position]);
373
                    position++;
349
                position++;
374
                }
350
            }
-
 
351
            continue;
-
 
352
        }
-
 
353
       
375
            } else if ((ch == 0x41) || (ch == 0x42)) {
354
        if ((ch == U_UP_ARROW) || (ch == U_DOWN_ARROW)) {
376
                /* Up, down */
355
            /* Up, down */
377
                print_cc('\b', position);
356
            print_cc('\b', position);
378
                print_cc(' ', wstr_length(current));
357
            print_cc(' ', wstr_length(current));
379
                print_cc('\b', wstr_length(current));
358
            print_cc('\b', wstr_length(current));
380
               
359
           
381
                if (ch == 0x41) {
360
            if (ch == U_UP_ARROW) {
382
                    /* Up */
361
                /* Up */
383
                    if (history_pos == 0)
362
                if (history_pos == 0)
384
                        history_pos = KCONSOLE_HISTORY - 1;
363
                    history_pos = KCONSOLE_HISTORY - 1;
385
                    else
364
                else
386
                        history_pos--;
365
                    history_pos--;
387
                } else {
366
            } else {
388
                    /* Down */
367
                /* Down */
389
                    history_pos++;
368
                history_pos++;
390
                    history_pos = history_pos % KCONSOLE_HISTORY;
369
                history_pos = history_pos % KCONSOLE_HISTORY;
391
                }
370
            }
392
                current = history[history_pos];
371
            current = history[history_pos];
393
                printf("%ls", current);
372
            printf("%ls", current);
394
                position = wstr_length(current);
373
            position = wstr_length(current);
-
 
374
            continue;
-
 
375
        }
-
 
376
       
-
 
377
        if (ch == U_HOME_ARROW) {
-
 
378
            /* Home */
-
 
379
            print_cc('\b', position);
-
 
380
            position = 0;
-
 
381
            continue;
-
 
382
        }
-
 
383
       
-
 
384
        if (ch == U_END_ARROW) {
-
 
385
            /* End */
-
 
386
            printf("%ls", current + position);
-
 
387
            position = wstr_length(current);
-
 
388
            continue;
-
 
389
        }
-
 
390
       
-
 
391
        if (ch == U_DELETE) {
-
 
392
            /* Delete */
-
 
393
            if (position == wstr_length(current))
-
 
394
                continue;
-
 
395
           
-
 
396
            if (wstr_remove(current, position)) {
-
 
397
                printf("%ls ", current + position);
-
 
398
                print_cc('\b', wstr_length(current) - position + 1);
395
            }
399
            }
396
            continue;
400
            continue;
397
        }
401
        }
398
       
402
       
399
        if (wstr_linsert(current, ch, position, MAX_CMDLINE)) {
403
        if (wstr_linsert(current, ch, position, MAX_CMDLINE)) {
400
            printf("%ls", current + position);
404
            printf("%ls", current + position);
401
            position++;
405
            position++;
402
            print_cc('\b', wstr_length(current) - position);
406
            print_cc('\b', wstr_length(current) - position);
403
        }
407
        }
404
    }
408
    }
405
   
409
   
406
    if (wstr_length(current) > 0) {
410
    if (wstr_length(current) > 0) {
407
        history_pos++;
411
        history_pos++;
408
        history_pos = history_pos % KCONSOLE_HISTORY;
412
        history_pos = history_pos % KCONSOLE_HISTORY;
409
    }
413
    }
410
   
414
   
411
    return current;
415
    return current;
412
}
416
}
413
 
417
 
414
bool kconsole_check_poll(void)
418
bool kconsole_check_poll(void)
415
{
419
{
416
    return check_poll(stdin);
420
    return check_poll(stdin);
417
}
421
}
418
 
422
 
419
static bool parse_int_arg(const char *text, size_t len, unative_t *result)
423
static bool parse_int_arg(const char *text, size_t len, unative_t *result)
420
{
424
{
421
    bool isaddr = false;
425
    bool isaddr = false;
422
    bool isptr = false;
426
    bool isptr = false;
423
   
427
   
424
    /* If we get a name, try to find it in symbol table */
428
    /* If we get a name, try to find it in symbol table */
425
    if (text[0] == '&') {
429
    if (text[0] == '&') {
426
        isaddr = true;
430
        isaddr = true;
427
        text++;
431
        text++;
428
        len--;
432
        len--;
429
    } else if (text[0] == '*') {
433
    } else if (text[0] == '*') {
430
        isptr = true;
434
        isptr = true;
431
        text++;
435
        text++;
432
        len--;
436
        len--;
433
    }
437
    }
434
   
438
   
435
    if ((text[0] < '0') || (text[0] > '9')) {
439
    if ((text[0] < '0') || (text[0] > '9')) {
436
        char symname[MAX_SYMBOL_NAME];
440
        char symname[MAX_SYMBOL_NAME];
437
        str_ncpy(symname, text, min(len + 1, MAX_SYMBOL_NAME));
441
        str_ncpy(symname, text, min(len + 1, MAX_SYMBOL_NAME));
438
       
442
       
439
        uintptr_t symaddr;
443
        uintptr_t symaddr;
440
        int rc = symtab_addr_lookup(symname, &symaddr);
444
        int rc = symtab_addr_lookup(symname, &symaddr);
441
        switch (rc) {
445
        switch (rc) {
442
        case ENOENT:
446
        case ENOENT:
443
            printf("Symbol %s not found.\n", symname);
447
            printf("Symbol %s not found.\n", symname);
444
            return false;
448
            return false;
445
        case EOVERFLOW:
449
        case EOVERFLOW:
446
            printf("Duplicate symbol %s.\n", symname);
450
            printf("Duplicate symbol %s.\n", symname);
447
            symtab_print_search(symname);
451
            symtab_print_search(symname);
448
            return false;
452
            return false;
449
        case ENOTSUP:
453
        case ENOTSUP:
450
            printf("No symbol information available.\n");
454
            printf("No symbol information available.\n");
451
            return false;
455
            return false;
452
        }
456
        }
453
       
457
       
454
        if (isaddr)
458
        if (isaddr)
455
            *result = (unative_t) symaddr;
459
            *result = (unative_t) symaddr;
456
        else if (isptr)
460
        else if (isptr)
457
            *result = **((unative_t **) symaddr);
461
            *result = **((unative_t **) symaddr);
458
        else
462
        else
459
            *result = *((unative_t *) symaddr);
463
            *result = *((unative_t *) symaddr);
460
    } else {
464
    } else {
461
        /* It's a number - convert it */
465
        /* It's a number - convert it */
462
        *result = atoi(text);
466
        *result = atoi(text);
463
        if (isptr)
467
        if (isptr)
464
            *result = *((unative_t *) *result);
468
            *result = *((unative_t *) *result);
465
    }
469
    }
466
   
470
   
467
    return true;
471
    return true;
468
}
472
}
469
 
473
 
470
/** Parse argument.
474
/** Parse argument.
471
 *
475
 *
472
 * Find start and end positions of command line argument.
476
 * Find start and end positions of command line argument.
473
 *
477
 *
474
 * @param cmdline Command line as read from the input device.
478
 * @param cmdline Command line as read from the input device.
475
 * @param size    Size (in bytes) of the string.
479
 * @param size    Size (in bytes) of the string.
476
 * @param start   On entry, 'start' contains pointer to the offset
480
 * @param start   On entry, 'start' contains pointer to the offset
477
 *                of the first unprocessed character of cmdline.
481
 *                of the first unprocessed character of cmdline.
478
 *                On successful exit, it marks beginning of the next argument.
482
 *                On successful exit, it marks beginning of the next argument.
479
 * @param end     Undefined on entry. On exit, 'end' is the offset of the first
483
 * @param end     Undefined on entry. On exit, 'end' is the offset of the first
480
 *                character behind the next argument.
484
 *                character behind the next argument.
481
 *
485
 *
482
 * @return False on failure, true on success.
486
 * @return False on failure, true on success.
483
 *
487
 *
484
 */
488
 */
485
static bool parse_argument(const char *cmdline, size_t size, size_t *start, size_t *end)
489
static bool parse_argument(const char *cmdline, size_t size, size_t *start, size_t *end)
486
{
490
{
487
    ASSERT(start != NULL);
491
    ASSERT(start != NULL);
488
    ASSERT(end != NULL);
492
    ASSERT(end != NULL);
489
   
493
   
490
    bool found_start = false;
494
    bool found_start = false;
491
    size_t offset = *start;
495
    size_t offset = *start;
492
    size_t prev = *start;
496
    size_t prev = *start;
493
    wchar_t ch;
497
    wchar_t ch;
494
   
498
   
495
    while ((ch = str_decode(cmdline, &offset, size)) != 0) {
499
    while ((ch = str_decode(cmdline, &offset, size)) != 0) {
496
        if (!found_start) {
500
        if (!found_start) {
497
            if (!isspace(ch)) {
501
            if (!isspace(ch)) {
498
                *start = prev;
502
                *start = prev;
499
                found_start = true;
503
                found_start = true;
500
            }
504
            }
501
        } else {
505
        } else {
502
            if (isspace(ch))
506
            if (isspace(ch))
503
                break;
507
                break;
504
        }
508
        }
505
       
509
       
506
        prev = offset;
510
        prev = offset;
507
    }
511
    }
508
    *end = offset;
512
    *end = offset;
509
   
513
   
510
    return found_start;
514
    return found_start;
511
}
515
}
512
 
516
 
513
/** Parse command line.
517
/** Parse command line.
514
 *
518
 *
515
 * @param cmdline Command line as read from input device.
519
 * @param cmdline Command line as read from input device.
516
 * @param size    Size (in bytes) of the string.
520
 * @param size    Size (in bytes) of the string.
517
 *
521
 *
518
 * @return Structure describing the command.
522
 * @return Structure describing the command.
519
 *
523
 *
520
 */
524
 */
521
static cmd_info_t *parse_cmdline(const char *cmdline, size_t size)
525
static cmd_info_t *parse_cmdline(const char *cmdline, size_t size)
522
{
526
{
523
    size_t start = 0;
527
    size_t start = 0;
524
    size_t end = 0;
528
    size_t end = 0;
525
    if (!parse_argument(cmdline, size, &start, &end)) {
529
    if (!parse_argument(cmdline, size, &start, &end)) {
526
        /* Command line did not contain alphanumeric word. */
530
        /* Command line did not contain alphanumeric word. */
527
        return NULL;
531
        return NULL;
528
    }
532
    }
529
    spinlock_lock(&cmd_lock);
533
    spinlock_lock(&cmd_lock);
530
   
534
   
531
    cmd_info_t *cmd = NULL;
535
    cmd_info_t *cmd = NULL;
532
    link_t *cur;
536
    link_t *cur;
533
   
537
   
534
    for (cur = cmd_head.next; cur != &cmd_head; cur = cur->next) {
538
    for (cur = cmd_head.next; cur != &cmd_head; cur = cur->next) {
535
        cmd_info_t *hlp = list_get_instance(cur, cmd_info_t, link);
539
        cmd_info_t *hlp = list_get_instance(cur, cmd_info_t, link);
536
        spinlock_lock(&hlp->lock);
540
        spinlock_lock(&hlp->lock);
537
       
541
       
538
        if (str_lcmp(hlp->name, cmdline + start,
542
        if (str_lcmp(hlp->name, cmdline + start,
539
            max(str_length(hlp->name),
543
            max(str_length(hlp->name),
540
            str_nlength(cmdline + start, (count_t) (end - start) - 1))) == 0) {
544
            str_nlength(cmdline + start, (count_t) (end - start) - 1))) == 0) {
541
            cmd = hlp;
545
            cmd = hlp;
542
            break;
546
            break;
543
        }
547
        }
544
       
548
       
545
        spinlock_unlock(&hlp->lock);
549
        spinlock_unlock(&hlp->lock);
546
    }
550
    }
547
   
551
   
548
    spinlock_unlock(&cmd_lock);
552
    spinlock_unlock(&cmd_lock);
549
   
553
   
550
    if (!cmd) {
554
    if (!cmd) {
551
        /* Unknown command. */
555
        /* Unknown command. */
552
        printf("Unknown command.\n");
556
        printf("Unknown command.\n");
553
        return NULL;
557
        return NULL;
554
    }
558
    }
555
   
559
   
556
    /* cmd == hlp is locked */
560
    /* cmd == hlp is locked */
557
   
561
   
558
    /*
562
    /*
559
     * The command line must be further analyzed and
563
     * The command line must be further analyzed and
560
     * the parameters therefrom must be matched and
564
     * the parameters therefrom must be matched and
561
     * converted to those specified in the cmd info
565
     * converted to those specified in the cmd info
562
     * structure.
566
     * structure.
563
     */
567
     */
564
   
568
   
565
    bool error = false;
569
    bool error = false;
566
    count_t i;
570
    count_t i;
567
    for (i = 0; i < cmd->argc; i++) {
571
    for (i = 0; i < cmd->argc; i++) {
568
        start = end;
572
        start = end;
569
        if (!parse_argument(cmdline, size, &start, &end)) {
573
        if (!parse_argument(cmdline, size, &start, &end)) {
570
            printf("Too few arguments.\n");
574
            printf("Too few arguments.\n");
571
            spinlock_unlock(&cmd->lock);
575
            spinlock_unlock(&cmd->lock);
572
            return NULL;
576
            return NULL;
573
        }
577
        }
574
       
578
       
575
        char *buf;
579
        char *buf;
576
        switch (cmd->argv[i].type) {
580
        switch (cmd->argv[i].type) {
577
        case ARG_TYPE_STRING:
581
        case ARG_TYPE_STRING:
578
            buf = (char *) cmd->argv[i].buffer;
582
            buf = (char *) cmd->argv[i].buffer;
579
            str_ncpy(buf, cmdline + start,
583
            str_ncpy(buf, cmdline + start,
580
                min((end - start) + 1, cmd->argv[i].len));
584
                min((end - start) + 1, cmd->argv[i].len));
581
            break;
585
            break;
582
        case ARG_TYPE_INT:
586
        case ARG_TYPE_INT:
583
            if (!parse_int_arg(cmdline + start, end - start,
587
            if (!parse_int_arg(cmdline + start, end - start,
584
                &cmd->argv[i].intval))
588
                &cmd->argv[i].intval))
585
                error = true;
589
                error = true;
586
            break;
590
            break;
587
        case ARG_TYPE_VAR:
591
        case ARG_TYPE_VAR:
588
            if ((start < end - 1) && (cmdline[start] == '"')) {
592
            if ((start < end - 1) && (cmdline[start] == '"')) {
589
                if (cmdline[end - 1] == '"') {
593
                if (cmdline[end - 1] == '"') {
590
                    buf = (char *) cmd->argv[i].buffer;
594
                    buf = (char *) cmd->argv[i].buffer;
591
                    str_ncpy(buf, cmdline + start + 1,
595
                    str_ncpy(buf, cmdline + start + 1,
592
                        min((end - start) - 1, cmd->argv[i].len));
596
                        min((end - start) - 1, cmd->argv[i].len));
593
                    cmd->argv[i].intval = (unative_t) buf;
597
                    cmd->argv[i].intval = (unative_t) buf;
594
                    cmd->argv[i].vartype = ARG_TYPE_STRING;
598
                    cmd->argv[i].vartype = ARG_TYPE_STRING;
595
                } else {
599
                } else {
596
                    printf("Wrong synxtax.\n");
600
                    printf("Wrong synxtax.\n");
597
                    error = true;
601
                    error = true;
598
                }
602
                }
599
            } else if (parse_int_arg(cmdline + start,
603
            } else if (parse_int_arg(cmdline + start,
600
                end - start, &cmd->argv[i].intval)) {
604
                end - start, &cmd->argv[i].intval)) {
601
                cmd->argv[i].vartype = ARG_TYPE_INT;
605
                cmd->argv[i].vartype = ARG_TYPE_INT;
602
            } else {
606
            } else {
603
                printf("Unrecognized variable argument.\n");
607
                printf("Unrecognized variable argument.\n");
604
                error = true;
608
                error = true;
605
            }
609
            }
606
            break;
610
            break;
607
        case ARG_TYPE_INVALID:
611
        case ARG_TYPE_INVALID:
608
        default:
612
        default:
609
            printf("Invalid argument type\n");
613
            printf("Invalid argument type\n");
610
            error = true;
614
            error = true;
611
            break;
615
            break;
612
        }
616
        }
613
    }
617
    }
614
   
618
   
615
    if (error) {
619
    if (error) {
616
        spinlock_unlock(&cmd->lock);
620
        spinlock_unlock(&cmd->lock);
617
        return NULL;
621
        return NULL;
618
    }
622
    }
619
   
623
   
620
    start = end;
624
    start = end;
621
    if (parse_argument(cmdline, size, &start, &end)) {
625
    if (parse_argument(cmdline, size, &start, &end)) {
622
        printf("Too many arguments.\n");
626
        printf("Too many arguments.\n");
623
        spinlock_unlock(&cmd->lock);
627
        spinlock_unlock(&cmd->lock);
624
        return NULL;
628
        return NULL;
625
    }
629
    }
626
   
630
   
627
    spinlock_unlock(&cmd->lock);
631
    spinlock_unlock(&cmd->lock);
628
    return cmd;
632
    return cmd;
629
}
633
}
630
 
634
 
631
/** Kernel console prompt.
635
/** Kernel console prompt.
632
 *
636
 *
633
 * @param prompt Kernel console prompt (e.g kconsole/panic).
637
 * @param prompt Kernel console prompt (e.g kconsole/panic).
634
 * @param msg    Message to display in the beginning.
638
 * @param msg    Message to display in the beginning.
635
 * @param kcon   Wait for keypress to show the prompt
639
 * @param kcon   Wait for keypress to show the prompt
636
 *               and never exit.
640
 *               and never exit.
637
 *
641
 *
638
 */
642
 */
639
void kconsole(char *prompt, char *msg, bool kcon)
643
void kconsole(char *prompt, char *msg, bool kcon)
640
{
644
{
641
    if (!stdin) {
645
    if (!stdin) {
642
        LOG("No stdin for kernel console");
646
        LOG("No stdin for kernel console");
643
        return;
647
        return;
644
    }
648
    }
645
   
649
   
646
    if (msg)
650
    if (msg)
647
        printf("%s", msg);
651
        printf("%s", msg);
648
   
652
   
649
    if (kcon)
653
    if (kcon)
650
        _getc(stdin);
654
        _getc(stdin);
651
    else
655
    else
652
        printf("Type \"exit\" to leave the console.\n");
656
        printf("Type \"exit\" to leave the console.\n");
653
   
657
   
654
    while (true) {
658
    while (true) {
655
        wchar_t *tmp = clever_readline((char *) prompt, stdin);
659
        wchar_t *tmp = clever_readline((char *) prompt, stdin);
656
        count_t len = wstr_length(tmp);
660
        count_t len = wstr_length(tmp);
657
        if (!len)
661
        if (!len)
658
            continue;
662
            continue;
659
       
663
       
660
        char cmdline[STR_BOUNDS(MAX_CMDLINE)];
664
        char cmdline[STR_BOUNDS(MAX_CMDLINE)];
661
        wstr_nstr(cmdline, tmp, STR_BOUNDS(MAX_CMDLINE));
665
        wstr_nstr(cmdline, tmp, STR_BOUNDS(MAX_CMDLINE));
662
       
666
       
663
        if ((!kcon) && (len == 4) && (str_lcmp(cmdline, "exit", 4) == 0))
667
        if ((!kcon) && (len == 4) && (str_lcmp(cmdline, "exit", 4) == 0))
664
            break;
668
            break;
665
       
669
       
666
        cmd_info_t *cmd_info = parse_cmdline(cmdline, STR_BOUNDS(MAX_CMDLINE));
670
        cmd_info_t *cmd_info = parse_cmdline(cmdline, STR_BOUNDS(MAX_CMDLINE));
667
        if (!cmd_info)
671
        if (!cmd_info)
668
            continue;
672
            continue;
669
       
673
       
670
        (void) cmd_info->func(cmd_info->argv);
674
        (void) cmd_info->func(cmd_info->argv);
671
    }
675
    }
672
}
676
}
673
 
677
 
674
/** Kernel console managing thread.
678
/** Kernel console managing thread.
675
 *
679
 *
676
 */
680
 */
677
void kconsole_thread(void *data)
681
void kconsole_thread(void *data)
678
{
682
{
679
    kconsole("kconsole", "Kernel console ready (press any key to activate)\n", true);
683
    kconsole("kconsole", "Kernel console ready (press any key to activate)\n", true);
680
}
684
}
681
 
685
 
682
/** @}
686
/** @}
683
 */
687
 */
684
 
688