Subversion Repositories HelenOS

Rev

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

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