Subversion Repositories HelenOS

Rev

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

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