Subversion Repositories HelenOS

Rev

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

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