Subversion Repositories HelenOS

Rev

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

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