Subversion Repositories HelenOS

Rev

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

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