Subversion Repositories HelenOS

Rev

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

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