Subversion Repositories HelenOS

Rev

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

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