Subversion Repositories HelenOS

Rev

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

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