Subversion Repositories HelenOS

Rev

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

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