Subversion Repositories HelenOS

Rev

Rev 4346 | Rev 4348 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 4346 Rev 4347
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
#include <console/kconsole.h>
40
#include <console/kconsole.h>
41
#include <console/console.h>
41
#include <console/console.h>
42
#include <console/chardev.h>
42
#include <console/chardev.h>
43
#include <console/cmd.h>
43
#include <console/cmd.h>
44
#include <print.h>
44
#include <print.h>
45
#include <panic.h>
45
#include <panic.h>
46
#include <arch/types.h>
46
#include <arch/types.h>
47
#include <adt/list.h>
47
#include <adt/list.h>
48
#include <arch.h>
48
#include <arch.h>
49
#include <macros.h>
49
#include <macros.h>
50
#include <debug.h>
50
#include <debug.h>
51
#include <func.h>
51
#include <func.h>
52
#include <string.h>
52
#include <string.h>
53
#include <macros.h>
53
#include <macros.h>
54
#include <sysinfo/sysinfo.h>
54
#include <sysinfo/sysinfo.h>
55
#include <ddi/device.h>
55
#include <ddi/device.h>
56
#include <symtab.h>
56
#include <symtab.h>
57
#include <errno.h>
57
#include <errno.h>
-
 
58
#include <putchar.h>
58
 
59
 
59
/** Simple kernel console.
60
/** Simple kernel console.
60
 *
61
 *
61
 * The console is realized by kernel thread kconsole.
62
 * The console is realized by kernel thread kconsole.
62
 * It doesn't understand any useful command on its own,
63
 * It doesn't understand any useful command on its own,
63
 * but makes it possible for other kernel subsystems to
64
 * but makes it possible for other kernel subsystems to
64
 * register their own commands.
65
 * register their own commands.
65
 */
66
 */
66
 
67
 
67
/** Locking.
68
/** Locking.
68
 *
69
 *
69
 * There is a list of cmd_info_t structures. This list
70
 * There is a list of cmd_info_t structures. This list
70
 * is protected by cmd_lock spinlock. Note that specially
71
 * is protected by cmd_lock spinlock. Note that specially
71
 * the link elements of cmd_info_t are protected by
72
 * the link elements of cmd_info_t are protected by
72
 * this lock.
73
 * this lock.
73
 *
74
 *
74
 * Each cmd_info_t also has its own lock, which protects
75
 * Each cmd_info_t also has its own lock, which protects
75
 * all elements thereof except the link element.
76
 * all elements thereof except the link element.
76
 *
77
 *
77
 * cmd_lock must be acquired before any cmd_info lock.
78
 * cmd_lock must be acquired before any cmd_info lock.
78
 * When locking two cmd info structures, structure with
79
 * When locking two cmd info structures, structure with
79
 * lower address must be locked first.
80
 * lower address must be locked first.
80
 */
81
 */
81
 
82
 
82
SPINLOCK_INITIALIZE(cmd_lock);  /**< Lock protecting command list. */
83
SPINLOCK_INITIALIZE(cmd_lock);  /**< Lock protecting command list. */
83
LIST_INITIALIZE(cmd_head);  /**< Command list. */
84
LIST_INITIALIZE(cmd_head);  /**< Command list. */
84
 
85
 
85
static cmd_info_t *parse_cmdline(char *cmdline, size_t len);
86
static cmd_info_t *parse_cmdline(char *cmdline, size_t len);
86
static bool parse_argument(char *cmdline, size_t len, index_t *start,
87
static bool parse_argument(char *cmdline, size_t len, index_t *start,
87
    index_t *end);
88
    index_t *end);
88
static char history[KCONSOLE_HISTORY][MAX_CMDLINE] = {};
89
static char history[KCONSOLE_HISTORY][MAX_CMDLINE] = {};
89
 
90
 
90
/*
-
 
91
 * For now, we use 0 as INR.
-
 
92
 * However, it is therefore desirable to have architecture specific
-
 
93
 * definition of KCONSOLE_VIRT_INR in the future.
-
 
94
 */
-
 
95
#define KCONSOLE_VIRT_INR  0
-
 
96
 
-
 
97
bool kconsole_notify = false;
-
 
98
irq_t kconsole_irq;
-
 
99
 
-
 
100
 
-
 
101
/** Allways refuse IRQ ownership.
-
 
102
 *
-
 
103
 * This is not a real IRQ, so we always decline.
-
 
104
 *
-
 
105
 * @return Always returns IRQ_DECLINE.
-
 
106
 *
-
 
107
 */
-
 
108
static irq_ownership_t kconsole_claim(irq_t *irq)
-
 
109
{
-
 
110
    return IRQ_DECLINE;
-
 
111
}
-
 
112
 
-
 
113
 
-
 
114
/** Initialize kconsole data structures
91
/** Initialize kconsole data structures
115
 *
92
 *
116
 * This is the most basic initialization, almost no
93
 * This is the most basic initialization, almost no
117
 * other kernel subsystem is ready yet.
94
 * other kernel subsystem is ready yet.
118
 *
95
 *
119
 */
96
 */
120
void kconsole_init(void)
97
void kconsole_init(void)
121
{
98
{
122
    unsigned int i;
99
    unsigned int i;
123
 
100
 
124
    cmd_init();
101
    cmd_init();
125
    for (i = 0; i < KCONSOLE_HISTORY; i++)
102
    for (i = 0; i < KCONSOLE_HISTORY; i++)
126
        history[i][0] = '\0';
103
        history[i][0] = '\0';
127
}
104
}
128
 
105
 
129
 
-
 
130
/** Initialize kconsole notification mechanism
-
 
131
 *
-
 
132
 * Initialize the virtual IRQ notification mechanism.
-
 
133
 *
-
 
134
 */
-
 
135
void kconsole_notify_init(void)
-
 
136
{
-
 
137
    devno_t devno = device_assign_devno();
-
 
138
   
-
 
139
    sysinfo_set_item_val("kconsole.present", NULL, true);
-
 
140
    sysinfo_set_item_val("kconsole.devno", NULL, devno);
-
 
141
    sysinfo_set_item_val("kconsole.inr", NULL, KCONSOLE_VIRT_INR);
-
 
142
   
-
 
143
    irq_initialize(&kconsole_irq);
-
 
144
    kconsole_irq.devno = devno;
-
 
145
    kconsole_irq.inr = KCONSOLE_VIRT_INR;
-
 
146
    kconsole_irq.claim = kconsole_claim;
-
 
147
    irq_register(&kconsole_irq);
-
 
148
   
-
 
149
    kconsole_notify = true;
-
 
150
}
-
 
151
 
-
 
152
 
-
 
153
/** Register kconsole command.
106
/** Register kconsole command.
154
 *
107
 *
155
 * @param cmd Structure describing the command.
108
 * @param cmd Structure describing the command.
156
 *
109
 *
157
 * @return 0 on failure, 1 on success.
110
 * @return 0 on failure, 1 on success.
158
 */
111
 */
159
int cmd_register(cmd_info_t *cmd)
112
int cmd_register(cmd_info_t *cmd)
160
{
113
{
161
    link_t *cur;
114
    link_t *cur;
162
   
115
   
163
    spinlock_lock(&cmd_lock);
116
    spinlock_lock(&cmd_lock);
164
   
117
   
165
    /*
118
    /*
166
     * Make sure the command is not already listed.
119
     * Make sure the command is not already listed.
167
     */
120
     */
168
    for (cur = cmd_head.next; cur != &cmd_head; cur = cur->next) {
121
    for (cur = cmd_head.next; cur != &cmd_head; cur = cur->next) {
169
        cmd_info_t *hlp;
122
        cmd_info_t *hlp;
170
       
123
       
171
        hlp = list_get_instance(cur, cmd_info_t, link);
124
        hlp = list_get_instance(cur, cmd_info_t, link);
172
 
125
 
173
        if (hlp == cmd) {
126
        if (hlp == cmd) {
174
            /* The command is already there. */
127
            /* The command is already there. */
175
            spinlock_unlock(&cmd_lock);
128
            spinlock_unlock(&cmd_lock);
176
            return 0;
129
            return 0;
177
        }
130
        }
178
 
131
 
179
        /* Avoid deadlock. */
132
        /* Avoid deadlock. */
180
        if (hlp < cmd) {
133
        if (hlp < cmd) {
181
            spinlock_lock(&hlp->lock);
134
            spinlock_lock(&hlp->lock);
182
            spinlock_lock(&cmd->lock);
135
            spinlock_lock(&cmd->lock);
183
        } else {
136
        } else {
184
            spinlock_lock(&cmd->lock);
137
            spinlock_lock(&cmd->lock);
185
            spinlock_lock(&hlp->lock);
138
            spinlock_lock(&hlp->lock);
186
        }
139
        }
187
        if ((strncmp(hlp->name, cmd->name, max(strlen(cmd->name),
140
        if ((strncmp(hlp->name, cmd->name, max(strlen(cmd->name),
188
            strlen(hlp->name))) == 0)) {
141
            strlen(hlp->name))) == 0)) {
189
            /* The command is already there. */
142
            /* The command is already there. */
190
            spinlock_unlock(&hlp->lock);
143
            spinlock_unlock(&hlp->lock);
191
            spinlock_unlock(&cmd->lock);
144
            spinlock_unlock(&cmd->lock);
192
            spinlock_unlock(&cmd_lock);
145
            spinlock_unlock(&cmd_lock);
193
            return 0;
146
            return 0;
194
        }
147
        }
195
       
148
       
196
        spinlock_unlock(&hlp->lock);
149
        spinlock_unlock(&hlp->lock);
197
        spinlock_unlock(&cmd->lock);
150
        spinlock_unlock(&cmd->lock);
198
    }
151
    }
199
   
152
   
200
    /*
153
    /*
201
     * Now the command can be added.
154
     * Now the command can be added.
202
     */
155
     */
203
    list_append(&cmd->link, &cmd_head);
156
    list_append(&cmd->link, &cmd_head);
204
   
157
   
205
    spinlock_unlock(&cmd_lock);
158
    spinlock_unlock(&cmd_lock);
206
    return 1;
159
    return 1;
207
}
160
}
208
 
161
 
209
/** Print count times a character */
162
/** Print count times a character */
210
static void rdln_print_c(char ch, int count)
163
static void rdln_print_c(wchar_t ch, count_t count)
211
{
164
{
212
    int i;
165
    count_t i;
213
    for (i = 0; i < count; i++)
166
    for (i = 0; i < count; i++)
214
        putchar(ch);
167
        putchar(ch);
215
}
168
}
216
 
169
 
217
/** Insert character to string */
170
/** Insert character to string */
218
static void insert_char(char *str, char ch, int pos)
171
static void insert_char(char *str, char ch, int pos)
219
{
172
{
220
    int i;
173
    int i;
221
   
174
   
222
    for (i = strlen(str); i > pos; i--)
175
    for (i = strlen(str); i > pos; i--)
223
        str[i] = str[i - 1];
176
        str[i] = str[i - 1];
224
    str[pos] = ch;
177
    str[pos] = ch;
225
}
178
}
226
 
179
 
227
/** Try to find a command beginning with prefix */
180
/** Try to find a command beginning with prefix */
228
static const char *cmdtab_search_one(const char *name,link_t **startpos)
181
static const char *cmdtab_search_one(const char *name,link_t **startpos)
229
{
182
{
230
    size_t namelen = strlen(name);
183
    size_t namelen = strlen(name);
231
    const char *curname;
184
    const char *curname;
232
 
185
 
233
    spinlock_lock(&cmd_lock);
186
    spinlock_lock(&cmd_lock);
234
 
187
 
235
    if (!*startpos)
188
    if (!*startpos)
236
        *startpos = cmd_head.next;
189
        *startpos = cmd_head.next;
237
 
190
 
238
    for (; *startpos != &cmd_head; *startpos = (*startpos)->next) {
191
    for (; *startpos != &cmd_head; *startpos = (*startpos)->next) {
239
        cmd_info_t *hlp;
192
        cmd_info_t *hlp;
240
        hlp = list_get_instance(*startpos, cmd_info_t, link);
193
        hlp = list_get_instance(*startpos, cmd_info_t, link);
241
 
194
 
242
        curname = hlp->name;
195
        curname = hlp->name;
243
        if (strlen(curname) < namelen)
196
        if (strlen(curname) < namelen)
244
            continue;
197
            continue;
245
        if (strncmp(curname, name, namelen) == 0) {
198
        if (strncmp(curname, name, namelen) == 0) {
246
            spinlock_unlock(&cmd_lock);
199
            spinlock_unlock(&cmd_lock);
247
            return curname+namelen;
200
            return curname+namelen;
248
        }
201
        }
249
    }
202
    }
250
    spinlock_unlock(&cmd_lock);
203
    spinlock_unlock(&cmd_lock);
251
    return NULL;
204
    return NULL;
252
}
205
}
253
 
206
 
254
 
207
 
255
/** Command completion of the commands
208
/** Command completion of the commands
256
 *
209
 *
257
 * @param name - string to match, changed to hint on exit
210
 * @param name - string to match, changed to hint on exit
258
 * @return number of found matches
211
 * @return number of found matches
259
 */
212
 */
260
static int cmdtab_compl(char *name)
213
static int cmdtab_compl(char *name)
261
{
214
{
262
    static char output[/*MAX_SYMBOL_NAME*/128 + 1];
215
    static char output[/*MAX_SYMBOL_NAME*/128 + 1];
263
    link_t *startpos = NULL;
216
    link_t *startpos = NULL;
264
    const char *foundtxt;
217
    const char *foundtxt;
265
    int found = 0;
218
    int found = 0;
266
    int i;
219
    int i;
267
 
220
 
268
    output[0] = '\0';
221
    output[0] = '\0';
269
    while ((foundtxt = cmdtab_search_one(name, &startpos))) {
222
    while ((foundtxt = cmdtab_search_one(name, &startpos))) {
270
        startpos = startpos->next;
223
        startpos = startpos->next;
271
        if (!found)
224
        if (!found)
272
            strncpy(output, foundtxt, strlen(foundtxt) + 1);
225
            strncpy(output, foundtxt, strlen(foundtxt) + 1);
273
        else {
226
        else {
274
            for (i = 0; output[i] && foundtxt[i] &&
227
            for (i = 0; output[i] && foundtxt[i] &&
275
                output[i] == foundtxt[i]; i++)
228
                output[i] == foundtxt[i]; i++)
276
                ;
229
                ;
277
            output[i] = '\0';
230
            output[i] = '\0';
278
        }
231
        }
279
        found++;
232
        found++;
280
    }
233
    }
281
    if (!found)
234
    if (!found)
282
        return 0;
235
        return 0;
283
 
236
 
284
    if (found > 1 && !strlen(output)) {
237
    if (found > 1 && !strlen(output)) {
285
        printf("\n");
238
        printf("\n");
286
        startpos = NULL;
239
        startpos = NULL;
287
        while ((foundtxt = cmdtab_search_one(name, &startpos))) {
240
        while ((foundtxt = cmdtab_search_one(name, &startpos))) {
288
            cmd_info_t *hlp;
241
            cmd_info_t *hlp;
289
            hlp = list_get_instance(startpos, cmd_info_t, link);
242
            hlp = list_get_instance(startpos, cmd_info_t, link);
290
            printf("%s - %s\n", hlp->name, hlp->description);
243
            printf("%s - %s\n", hlp->name, hlp->description);
291
            startpos = startpos->next;
244
            startpos = startpos->next;
292
        }
245
        }
293
    }
246
    }
294
    strncpy(name, output, 128/*MAX_SYMBOL_NAME*/);
247
    strncpy(name, output, 128/*MAX_SYMBOL_NAME*/);
295
    return found;
248
    return found;
296
}
249
}
297
 
250
 
298
static char *clever_readline(const char *prompt, indev_t *input)
251
static char *clever_readline(const char *prompt, indev_t *input)
299
{
252
{
300
    static int histposition = 0;
253
    static int histposition = 0;
301
 
254
 
302
    static char tmp[MAX_CMDLINE + 1];
255
    static char tmp[MAX_CMDLINE + 1];
303
    int curlen = 0, position = 0;
256
    int curlen = 0, position = 0;
304
    char *current = history[histposition];
257
    char *current = history[histposition];
305
    int i;
258
    int i;
306
    char mod; /* Command Modifier */
259
    char mod; /* Command Modifier */
307
    char c;
260
    char c;
308
 
261
 
309
    printf("%s> ", prompt);
262
    printf("%s> ", prompt);
310
    while (1) {
263
    while (1) {
311
        c = _getc(input);
264
        c = _getc(input);
312
        if (c == '\n') {
265
        if (c == '\n') {
313
            putchar(c);
266
            putchar(c);
314
            break;
267
            break;
315
        }
268
        }
316
        if (c == '\b') { /* Backspace */
269
        if (c == '\b') { /* Backspace */
317
            if (position == 0)
270
            if (position == 0)
318
                continue;
271
                continue;
319
            for (i = position; i < curlen; i++)
272
            for (i = position; i < curlen; i++)
320
                current[i - 1] = current[i];
273
                current[i - 1] = current[i];
321
            curlen--;
274
            curlen--;
322
            position--;
275
            position--;
323
            putchar('\b');
276
            putchar('\b');
324
            for (i = position; i < curlen; i++)
277
            for (i = position; i < curlen; i++)
325
                putchar(current[i]);
278
                putchar(current[i]);
326
            putchar(' ');
279
            putchar(' ');
327
            rdln_print_c('\b', curlen - position + 1);
280
            rdln_print_c('\b', curlen - position + 1);
328
            continue;
281
            continue;
329
        }
282
        }
330
        if (c == '\t') { /* Tabulator */
283
        if (c == '\t') { /* Tabulator */
331
            int found;
284
            int found;
332
 
285
 
333
            /* Move to the end of the word */
286
            /* Move to the end of the word */
334
            for (; position < curlen && current[position] != ' ';
287
            for (; position < curlen && current[position] != ' ';
335
                position++)
288
                position++)
336
                putchar(current[position]);
289
                putchar(current[position]);
337
            /* Copy to tmp last word */
290
            /* Copy to tmp last word */
338
            for (i = position - 1; i >= 0 && current[i] != ' '; i--)
291
            for (i = position - 1; i >= 0 && current[i] != ' '; i--)
339
                ;
292
                ;
340
            /* If word begins with * or &, skip it */
293
            /* If word begins with * or &, skip it */
341
            if (tmp[0] == '*' || tmp[0] == '&')
294
            if (tmp[0] == '*' || tmp[0] == '&')
342
                for (i = 1; tmp[i]; i++)
295
                for (i = 1; tmp[i]; i++)
343
                    tmp[i - 1] = tmp[i];
296
                    tmp[i - 1] = tmp[i];
344
            i++; /* I is at the start of the word */
297
            i++; /* I is at the start of the word */
345
            strncpy(tmp, current + i, position - i + 1);
298
            strncpy(tmp, current + i, position - i + 1);
346
 
299
 
347
            if (i == 0) { /* Command completion */
300
            if (i == 0) { /* Command completion */
348
                found = cmdtab_compl(tmp);
301
                found = cmdtab_compl(tmp);
349
            } else { /* Symtab completion */
302
            } else { /* Symtab completion */
350
                found = symtab_compl(tmp);
303
                found = symtab_compl(tmp);
351
            }
304
            }
352
 
305
 
353
            if (found == 0)
306
            if (found == 0)
354
                continue;
307
                continue;
355
            for (i = 0; tmp[i] && curlen < MAX_CMDLINE;
308
            for (i = 0; tmp[i] && curlen < MAX_CMDLINE;
356
                i++, curlen++)
309
                i++, curlen++)
357
                insert_char(current, tmp[i], i + position);
310
                insert_char(current, tmp[i], i + position);
358
 
311
 
359
            if (strlen(tmp) || found == 1) { /* If we have a hint */
312
            if (strlen(tmp) || found == 1) { /* If we have a hint */
360
                for (i = position; i < curlen; i++)
313
                for (i = position; i < curlen; i++)
361
                    putchar(current[i]);
314
                    putchar(current[i]);
362
                position += strlen(tmp);
315
                position += strlen(tmp);
363
                /* Add space to end */
316
                /* Add space to end */
364
                if (found == 1 && position == curlen &&
317
                if (found == 1 && position == curlen &&
365
                    curlen < MAX_CMDLINE) {
318
                    curlen < MAX_CMDLINE) {
366
                    current[position] = ' ';
319
                    current[position] = ' ';
367
                    curlen++;
320
                    curlen++;
368
                    position++;
321
                    position++;
369
                    putchar(' ');
322
                    putchar(' ');
370
                }
323
                }
371
            } else { /* No hint, table was printed */
324
            } else { /* No hint, table was printed */
372
                printf("%s> ", prompt);
325
                printf("%s> ", prompt);
373
                for (i = 0; i < curlen; i++)
326
                for (i = 0; i < curlen; i++)
374
                    putchar(current[i]);
327
                    putchar(current[i]);
375
                position += strlen(tmp);
328
                position += strlen(tmp);
376
            }
329
            }
377
            rdln_print_c('\b', curlen - position);
330
            rdln_print_c('\b', curlen - position);
378
            continue;
331
            continue;
379
        }
332
        }
380
        if (c == 0x1b) { /* Special command */
333
        if (c == 0x1b) { /* Special command */
381
            mod = _getc(input);
334
            mod = _getc(input);
382
            c = _getc(input);
335
            c = _getc(input);
383
 
336
 
384
            if (mod != 0x5b && mod != 0x4f)
337
            if (mod != 0x5b && mod != 0x4f)
385
                continue;
338
                continue;
386
 
339
 
387
            if (c == 0x33 && _getc(input) == 0x7e) {
340
            if (c == 0x33 && _getc(input) == 0x7e) {
388
                /* Delete */
341
                /* Delete */
389
                if (position == curlen)
342
                if (position == curlen)
390
                    continue;
343
                    continue;
391
                for (i = position + 1; i < curlen; i++) {
344
                for (i = position + 1; i < curlen; i++) {
392
                    putchar(current[i]);
345
                    putchar(current[i]);
393
                    current[i - 1] = current[i];
346
                    current[i - 1] = current[i];
394
                }
347
                }
395
                putchar(' ');
348
                putchar(' ');
396
                rdln_print_c('\b', curlen - position);
349
                rdln_print_c('\b', curlen - position);
397
                curlen--;
350
                curlen--;
398
            } else if (c == 0x48) { /* Home */
351
            } else if (c == 0x48) { /* Home */
399
                rdln_print_c('\b', position);
352
                rdln_print_c('\b', position);
400
                position = 0;
353
                position = 0;
401
            } else if (c == 0x46) {  /* End */
354
            } else if (c == 0x46) {  /* End */
402
                for (i = position; i < curlen; i++)
355
                for (i = position; i < curlen; i++)
403
                    putchar(current[i]);
356
                    putchar(current[i]);
404
                position = curlen;
357
                position = curlen;
405
            } else if (c == 0x44) { /* Left */
358
            } else if (c == 0x44) { /* Left */
406
                if (position > 0) {
359
                if (position > 0) {
407
                    putchar('\b');
360
                    putchar('\b');
408
                    position--;
361
                    position--;
409
                }
362
                }
410
                continue;
363
                continue;
411
            } else if (c == 0x43) { /* Right */
364
            } else if (c == 0x43) { /* Right */
412
                if (position < curlen) {
365
                if (position < curlen) {
413
                    putchar(current[position]);
366
                    putchar(current[position]);
414
                    position++;
367
                    position++;
415
                }
368
                }
416
                continue;
369
                continue;
417
            } else if (c == 0x41 || c == 0x42) {
370
            } else if (c == 0x41 || c == 0x42) {
418
                                /* Up, down */
371
                                /* Up, down */
419
                rdln_print_c('\b', position);
372
                rdln_print_c('\b', position);
420
                rdln_print_c(' ', curlen);
373
                rdln_print_c(' ', curlen);
421
                rdln_print_c('\b', curlen);
374
                rdln_print_c('\b', curlen);
422
                if (c == 0x41) /* Up */
375
                if (c == 0x41) /* Up */
423
                    histposition--;
376
                    histposition--;
424
                else
377
                else
425
                    histposition++;
378
                    histposition++;
426
                if (histposition < 0) {
379
                if (histposition < 0) {
427
                    histposition = KCONSOLE_HISTORY - 1;
380
                    histposition = KCONSOLE_HISTORY - 1;
428
                } else {
381
                } else {
429
                    histposition =
382
                    histposition =
430
                        histposition % KCONSOLE_HISTORY;
383
                        histposition % KCONSOLE_HISTORY;
431
                }
384
                }
432
                current = history[histposition];
385
                current = history[histposition];
433
                printf("%s", current);
386
                printf("%s", current);
434
                curlen = strlen(current);
387
                curlen = strlen(current);
435
                position = curlen;
388
                position = curlen;
436
                continue;
389
                continue;
437
            }
390
            }
438
            continue;
391
            continue;
439
        }
392
        }
440
        if (curlen >= MAX_CMDLINE)
393
        if (curlen >= MAX_CMDLINE)
441
            continue;
394
            continue;
442
 
395
 
443
        insert_char(current, c, position);
396
        insert_char(current, c, position);
444
 
397
 
445
        curlen++;
398
        curlen++;
446
        for (i = position; i < curlen; i++)
399
        for (i = position; i < curlen; i++)
447
            putchar(current[i]);
400
            putchar(current[i]);
448
        position++;
401
        position++;
449
        rdln_print_c('\b',curlen - position);
402
        rdln_print_c('\b',curlen - position);
450
    }
403
    }
451
    if (curlen) {
404
    if (curlen) {
452
        histposition++;
405
        histposition++;
453
        histposition = histposition % KCONSOLE_HISTORY;
406
        histposition = histposition % KCONSOLE_HISTORY;
454
    }
407
    }
455
    current[curlen] = '\0';
408
    current[curlen] = '\0';
456
    return current;
409
    return current;
457
}
410
}
458
 
411
 
459
bool kconsole_check_poll(void)
412
bool kconsole_check_poll(void)
460
{
413
{
461
    return check_poll(stdin);
414
    return check_poll(stdin);
462
}
415
}
463
 
416
 
464
/** Kernel console prompt.
417
/** Kernel console prompt.
465
 *
418
 *
466
 * @param prompt Kernel console prompt (e.g kconsole/panic).
419
 * @param prompt Kernel console prompt (e.g kconsole/panic).
467
 * @param msg    Message to display in the beginning.
420
 * @param msg    Message to display in the beginning.
468
 * @param kcon   Wait for keypress to show the prompt
421
 * @param kcon   Wait for keypress to show the prompt
469
 *               and never exit.
422
 *               and never exit.
470
 *
423
 *
471
 */
424
 */
472
void kconsole(char *prompt, char *msg, bool kcon)
425
void kconsole(char *prompt, char *msg, bool kcon)
473
{
426
{
474
    cmd_info_t *cmd_info;
427
    cmd_info_t *cmd_info;
475
    count_t len;
428
    count_t len;
476
    char *cmdline;
429
    char *cmdline;
477
   
430
   
478
    if (!stdin) {
431
    if (!stdin) {
479
        LOG("No stdin for kernel console");
432
        LOG("No stdin for kernel console");
480
        return;
433
        return;
481
    }
434
    }
482
   
435
   
483
    if (msg)
436
    if (msg)
484
        printf("%s", msg);
437
        printf("%s", msg);
485
   
438
   
486
    if (kcon)
439
    if (kcon)
487
        _getc(stdin);
440
        _getc(stdin);
488
    else
441
    else
489
        printf("Type \"exit\" to leave the console.\n");
442
        printf("Type \"exit\" to leave the console.\n");
490
   
443
   
491
    while (true) {
444
    while (true) {
492
        cmdline = clever_readline((char *) prompt, stdin);
445
        cmdline = clever_readline((char *) prompt, stdin);
493
        len = strlen(cmdline);
446
        len = strlen(cmdline);
494
        if (!len)
447
        if (!len)
495
            continue;
448
            continue;
496
       
449
       
497
        if ((!kcon) && (len == 4) && (strncmp(cmdline, "exit", 4) == 0))
450
        if ((!kcon) && (len == 4) && (strncmp(cmdline, "exit", 4) == 0))
498
            break;
451
            break;
499
       
452
       
500
        cmd_info = parse_cmdline(cmdline, len);
453
        cmd_info = parse_cmdline(cmdline, len);
501
        if (!cmd_info)
454
        if (!cmd_info)
502
            continue;
455
            continue;
503
       
456
       
504
        (void) cmd_info->func(cmd_info->argv);
457
        (void) cmd_info->func(cmd_info->argv);
505
    }
458
    }
506
}
459
}
507
 
460
 
508
/** Kernel console managing thread.
461
/** Kernel console managing thread.
509
 *
462
 *
510
 */
463
 */
511
void kconsole_thread(void *data)
464
void kconsole_thread(void *data)
512
{
465
{
513
    kconsole("kconsole", "Kernel console ready (press any key to activate)\n", true);
466
    kconsole("kconsole", "Kernel console ready (press any key to activate)\n", true);
514
}
467
}
515
 
468
 
516
static int parse_int_arg(char *text, size_t len, unative_t *result)
469
static int parse_int_arg(char *text, size_t len, unative_t *result)
517
{
470
{
518
    uintptr_t symaddr;
471
    uintptr_t symaddr;
519
    bool isaddr = false;
472
    bool isaddr = false;
520
    bool isptr = false;
473
    bool isptr = false;
521
    int rc;
474
    int rc;
522
 
475
 
523
    static char symname[MAX_SYMBOL_NAME];
476
    static char symname[MAX_SYMBOL_NAME];
524
   
477
   
525
    /* If we get a name, try to find it in symbol table */
478
    /* If we get a name, try to find it in symbol table */
526
    if (text[0] == '&') {
479
    if (text[0] == '&') {
527
        isaddr = true;
480
        isaddr = true;
528
        text++;
481
        text++;
529
        len--;
482
        len--;
530
    } else if (text[0] == '*') {
483
    } else if (text[0] == '*') {
531
        isptr = true;
484
        isptr = true;
532
        text++;
485
        text++;
533
        len--;
486
        len--;
534
    }
487
    }
535
    if (text[0] < '0' || text[0] > '9') {
488
    if (text[0] < '0' || text[0] > '9') {
536
        strncpy(symname, text, min(len + 1, MAX_SYMBOL_NAME));
489
        strncpy(symname, text, min(len + 1, MAX_SYMBOL_NAME));
537
        rc = symtab_addr_lookup(symname, &symaddr);
490
        rc = symtab_addr_lookup(symname, &symaddr);
538
        switch (rc) {
491
        switch (rc) {
539
        case ENOENT:
492
        case ENOENT:
540
            printf("Symbol %s not found.\n", symname);
493
            printf("Symbol %s not found.\n", symname);
541
            return -1;
494
            return -1;
542
        case EOVERFLOW:
495
        case EOVERFLOW:
543
            printf("Duplicate symbol %s.\n", symname);
496
            printf("Duplicate symbol %s.\n", symname);
544
            symtab_print_search(symname);
497
            symtab_print_search(symname);
545
            return -1;
498
            return -1;
546
        default:
499
        default:
547
            printf("No symbol information available.\n");
500
            printf("No symbol information available.\n");
548
            return -1;
501
            return -1;
549
        }
502
        }
550
 
503
 
551
        if (isaddr)
504
        if (isaddr)
552
            *result = (unative_t)symaddr;
505
            *result = (unative_t)symaddr;
553
        else if (isptr)
506
        else if (isptr)
554
            *result = **((unative_t **)symaddr);
507
            *result = **((unative_t **)symaddr);
555
        else
508
        else
556
            *result = *((unative_t *)symaddr);
509
            *result = *((unative_t *)symaddr);
557
    } else { /* It's a number - convert it */
510
    } else { /* It's a number - convert it */
558
        *result = atoi(text);
511
        *result = atoi(text);
559
        if (isptr)
512
        if (isptr)
560
            *result = *((unative_t *)*result);
513
            *result = *((unative_t *)*result);
561
    }
514
    }
562
 
515
 
563
    return 0;
516
    return 0;
564
}
517
}
565
 
518
 
566
/** Parse command line.
519
/** Parse command line.
567
 *
520
 *
568
 * @param cmdline Command line as read from input device.
521
 * @param cmdline Command line as read from input device.
569
 * @param len Command line length.
522
 * @param len Command line length.
570
 *
523
 *
571
 * @return Structure describing the command.
524
 * @return Structure describing the command.
572
 */
525
 */
573
cmd_info_t *parse_cmdline(char *cmdline, size_t len)
526
cmd_info_t *parse_cmdline(char *cmdline, size_t len)
574
{
527
{
575
    index_t start = 0, end = 0;
528
    index_t start = 0, end = 0;
576
    cmd_info_t *cmd = NULL;
529
    cmd_info_t *cmd = NULL;
577
    link_t *cur;
530
    link_t *cur;
578
    count_t i;
531
    count_t i;
579
    int error = 0;
532
    int error = 0;
580
   
533
   
581
    if (!parse_argument(cmdline, len, &start, &end)) {
534
    if (!parse_argument(cmdline, len, &start, &end)) {
582
        /* Command line did not contain alphanumeric word. */
535
        /* Command line did not contain alphanumeric word. */
583
        return NULL;
536
        return NULL;
584
    }
537
    }
585
 
538
 
586
    spinlock_lock(&cmd_lock);
539
    spinlock_lock(&cmd_lock);
587
   
540
   
588
    for (cur = cmd_head.next; cur != &cmd_head; cur = cur->next) {
541
    for (cur = cmd_head.next; cur != &cmd_head; cur = cur->next) {
589
        cmd_info_t *hlp;
542
        cmd_info_t *hlp;
590
       
543
       
591
        hlp = list_get_instance(cur, cmd_info_t, link);
544
        hlp = list_get_instance(cur, cmd_info_t, link);
592
        spinlock_lock(&hlp->lock);
545
        spinlock_lock(&hlp->lock);
593
       
546
       
594
        if (strncmp(hlp->name, &cmdline[start], max(strlen(hlp->name),
547
        if (strncmp(hlp->name, &cmdline[start], max(strlen(hlp->name),
595
            end - start + 1)) == 0) {
548
            end - start + 1)) == 0) {
596
            cmd = hlp;
549
            cmd = hlp;
597
            break;
550
            break;
598
        }
551
        }
599
       
552
       
600
        spinlock_unlock(&hlp->lock);
553
        spinlock_unlock(&hlp->lock);
601
    }
554
    }
602
   
555
   
603
    spinlock_unlock(&cmd_lock);
556
    spinlock_unlock(&cmd_lock);
604
   
557
   
605
    if (!cmd) {
558
    if (!cmd) {
606
        /* Unknown command. */
559
        /* Unknown command. */
607
        printf("Unknown command.\n");
560
        printf("Unknown command.\n");
608
        return NULL;
561
        return NULL;
609
    }
562
    }
610
 
563
 
611
    /* cmd == hlp is locked */
564
    /* cmd == hlp is locked */
612
   
565
   
613
    /*
566
    /*
614
     * The command line must be further analyzed and
567
     * The command line must be further analyzed and
615
     * the parameters therefrom must be matched and
568
     * the parameters therefrom must be matched and
616
     * converted to those specified in the cmd info
569
     * converted to those specified in the cmd info
617
     * structure.
570
     * structure.
618
     */
571
     */
619
 
572
 
620
    for (i = 0; i < cmd->argc; i++) {
573
    for (i = 0; i < cmd->argc; i++) {
621
        char *buf;
574
        char *buf;
622
        start = end + 1;
575
        start = end + 1;
623
        if (!parse_argument(cmdline, len, &start, &end)) {
576
        if (!parse_argument(cmdline, len, &start, &end)) {
624
            printf("Too few arguments.\n");
577
            printf("Too few arguments.\n");
625
            spinlock_unlock(&cmd->lock);
578
            spinlock_unlock(&cmd->lock);
626
            return NULL;
579
            return NULL;
627
        }
580
        }
628
       
581
       
629
        error = 0;
582
        error = 0;
630
        switch (cmd->argv[i].type) {
583
        switch (cmd->argv[i].type) {
631
        case ARG_TYPE_STRING:
584
        case ARG_TYPE_STRING:
632
            buf = (char *) cmd->argv[i].buffer;
585
            buf = (char *) cmd->argv[i].buffer;
633
            strncpy(buf, (const char *) &cmdline[start],
586
            strncpy(buf, (const char *) &cmdline[start],
634
                min((end - start) + 2, cmd->argv[i].len));
587
                min((end - start) + 2, cmd->argv[i].len));
635
            buf[min((end - start) + 1, cmd->argv[i].len - 1)] =
588
            buf[min((end - start) + 1, cmd->argv[i].len - 1)] =
636
                '\0';
589
                '\0';
637
            break;
590
            break;
638
        case ARG_TYPE_INT:
591
        case ARG_TYPE_INT:
639
            if (parse_int_arg(cmdline + start, end - start + 1,
592
            if (parse_int_arg(cmdline + start, end - start + 1,
640
                &cmd->argv[i].intval))
593
                &cmd->argv[i].intval))
641
                error = 1;
594
                error = 1;
642
            break;
595
            break;
643
        case ARG_TYPE_VAR:
596
        case ARG_TYPE_VAR:
644
            if (start != end && cmdline[start] == '"' &&
597
            if (start != end && cmdline[start] == '"' &&
645
                cmdline[end] == '"') {
598
                cmdline[end] == '"') {
646
                buf = (char *) cmd->argv[i].buffer;
599
                buf = (char *) cmd->argv[i].buffer;
647
                strncpy(buf, (const char *) &cmdline[start + 1],
600
                strncpy(buf, (const char *) &cmdline[start + 1],
648
                    min((end-start), cmd->argv[i].len));
601
                    min((end-start), cmd->argv[i].len));
649
                buf[min((end - start), cmd->argv[i].len - 1)] =
602
                buf[min((end - start), cmd->argv[i].len - 1)] =
650
                    '\0';
603
                    '\0';
651
                cmd->argv[i].intval = (unative_t) buf;
604
                cmd->argv[i].intval = (unative_t) buf;
652
                cmd->argv[i].vartype = ARG_TYPE_STRING;
605
                cmd->argv[i].vartype = ARG_TYPE_STRING;
653
            } else if (!parse_int_arg(cmdline + start,
606
            } else if (!parse_int_arg(cmdline + start,
654
                end - start + 1, &cmd->argv[i].intval)) {
607
                end - start + 1, &cmd->argv[i].intval)) {
655
                cmd->argv[i].vartype = ARG_TYPE_INT;
608
                cmd->argv[i].vartype = ARG_TYPE_INT;
656
            } else {
609
            } else {
657
                printf("Unrecognized variable argument.\n");
610
                printf("Unrecognized variable argument.\n");
658
                error = 1;
611
                error = 1;
659
            }
612
            }
660
            break;
613
            break;
661
        case ARG_TYPE_INVALID:
614
        case ARG_TYPE_INVALID:
662
        default:
615
        default:
663
            printf("invalid argument type\n");
616
            printf("invalid argument type\n");
664
            error = 1;
617
            error = 1;
665
            break;
618
            break;
666
        }
619
        }
667
    }
620
    }
668
   
621
   
669
    if (error) {
622
    if (error) {
670
        spinlock_unlock(&cmd->lock);
623
        spinlock_unlock(&cmd->lock);
671
        return NULL;
624
        return NULL;
672
    }
625
    }
673
   
626
   
674
    start = end + 1;
627
    start = end + 1;
675
    if (parse_argument(cmdline, len, &start, &end)) {
628
    if (parse_argument(cmdline, len, &start, &end)) {
676
        printf("Too many arguments.\n");
629
        printf("Too many arguments.\n");
677
        spinlock_unlock(&cmd->lock);
630
        spinlock_unlock(&cmd->lock);
678
        return NULL;
631
        return NULL;
679
    }
632
    }
680
   
633
   
681
    spinlock_unlock(&cmd->lock);
634
    spinlock_unlock(&cmd->lock);
682
    return cmd;
635
    return cmd;
683
}
636
}
684
 
637
 
685
/** Parse argument.
638
/** Parse argument.
686
 *
639
 *
687
 * Find start and end positions of command line argument.
640
 * Find start and end positions of command line argument.
688
 *
641
 *
689
 * @param cmdline Command line as read from the input device.
642
 * @param cmdline Command line as read from the input device.
690
 * @param len Number of characters in cmdline.
643
 * @param len Number of characters in cmdline.
691
 * @param start On entry, 'start' contains pointer to the index
644
 * @param start On entry, 'start' contains pointer to the index
692
 *        of first unprocessed character of cmdline.
645
 *        of first unprocessed character of cmdline.
693
 *        On successful exit, it marks beginning of the next argument.
646
 *        On successful exit, it marks beginning of the next argument.
694
 * @param end Undefined on entry. On exit, 'end' points to the last character
647
 * @param end Undefined on entry. On exit, 'end' points to the last character
695
 *        of the next argument.
648
 *        of the next argument.
696
 *
649
 *
697
 * @return false on failure, true on success.
650
 * @return false on failure, true on success.
698
 */
651
 */
699
bool parse_argument(char *cmdline, size_t len, index_t *start, index_t *end)
652
bool parse_argument(char *cmdline, size_t len, index_t *start, index_t *end)
700
{
653
{
701
    index_t i;
654
    index_t i;
702
    bool found_start = false;
655
    bool found_start = false;
703
   
656
   
704
    ASSERT(start != NULL);
657
    ASSERT(start != NULL);
705
    ASSERT(end != NULL);
658
    ASSERT(end != NULL);
706
   
659
   
707
    for (i = *start; i < len; i++) {
660
    for (i = *start; i < len; i++) {
708
        if (!found_start) {
661
        if (!found_start) {
709
            if (isspace(cmdline[i]))
662
            if (isspace(cmdline[i]))
710
                (*start)++;
663
                (*start)++;
711
            else
664
            else
712
                found_start = true;
665
                found_start = true;
713
        } else {
666
        } else {
714
            if (isspace(cmdline[i]))
667
            if (isspace(cmdline[i]))
715
                break;
668
                break;
716
        }
669
        }
717
    }
670
    }
718
    *end = i - 1;
671
    *end = i - 1;
719
 
672
 
720
    return found_start;
673
    return found_start;
721
}
674
}
722
 
675
 
723
/** @}
676
/** @}
724
 */
677
 */
725
 
678