Subversion Repositories HelenOS

Rev

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

Rev 3193 Rev 3194
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
//char *clever_readline(const char *prompt, chardev_t *input);
-
 
244
static char *clever_readline(const char *prompt, chardev_t *input)
243
static char *clever_readline(const char *prompt, chardev_t *input)
245
{
244
{
246
    static int histposition = 0;
245
    static int histposition = 0;
247
 
246
 
248
    static char tmp[MAX_CMDLINE + 1];
247
    static char tmp[MAX_CMDLINE + 1];
249
    int curlen = 0, position = 0;
248
    int curlen = 0, position = 0;
250
    char *current = history[histposition];
249
    char *current = history[histposition];
251
    int i;
250
    int i;
252
    char mod; /* Command Modifier */
251
    char mod; /* Command Modifier */
253
    char c;
252
    char c;
254
 
253
 
255
    printf("%s> ", prompt);
254
    printf("%s> ", prompt);
256
    while (1) {
255
    while (1) {
257
        c = _getc(input);
256
        c = _getc(input);
258
        if (c == '\n') {
257
        if (c == '\n') {
259
            putchar(c);
258
            putchar(c);
260
            break;
259
            break;
261
        }
260
        }
262
        if (c == '\b') { /* Backspace */
261
        if (c == '\b') { /* Backspace */
263
            if (position == 0)
262
            if (position == 0)
264
                continue;
263
                continue;
265
            for (i = position; i < curlen; i++)
264
            for (i = position; i < curlen; i++)
266
                current[i - 1] = current[i];
265
                current[i - 1] = current[i];
267
            curlen--;
266
            curlen--;
268
            position--;
267
            position--;
269
            putchar('\b');
268
            putchar('\b');
270
            for (i = position; i < curlen; i++)
269
            for (i = position; i < curlen; i++)
271
                putchar(current[i]);
270
                putchar(current[i]);
272
            putchar(' ');
271
            putchar(' ');
273
            rdln_print_c('\b', curlen - position + 1);
272
            rdln_print_c('\b', curlen - position + 1);
274
            continue;
273
            continue;
275
        }
274
        }
276
        if (c == '\t') { /* Tabulator */
275
        if (c == '\t') { /* Tabulator */
277
            int found;
276
            int found;
278
 
277
 
279
            /* Move to the end of the word */
278
            /* Move to the end of the word */
280
            for (; position < curlen && current[position] != ' ';
279
            for (; position < curlen && current[position] != ' ';
281
                position++)
280
                position++)
282
                putchar(current[position]);
281
                putchar(current[position]);
283
            /* Copy to tmp last word */
282
            /* Copy to tmp last word */
284
            for (i = position - 1; i >= 0 && current[i] != ' '; i--)
283
            for (i = position - 1; i >= 0 && current[i] != ' '; i--)
285
                ;
284
                ;
286
            /* If word begins with * or &, skip it */
285
            /* If word begins with * or &, skip it */
287
            if (tmp[0] == '*' || tmp[0] == '&')
286
            if (tmp[0] == '*' || tmp[0] == '&')
288
                for (i = 1; tmp[i]; i++)
287
                for (i = 1; tmp[i]; i++)
289
                    tmp[i - 1] = tmp[i];
288
                    tmp[i - 1] = tmp[i];
290
            i++; /* I is at the start of the word */
289
            i++; /* I is at the start of the word */
291
            strncpy(tmp, current + i, position - i + 1);
290
            strncpy(tmp, current + i, position - i + 1);
292
 
291
 
293
            if (i == 0) { /* Command completion */
292
            if (i == 0) { /* Command completion */
294
                found = cmdtab_compl(tmp);
293
                found = cmdtab_compl(tmp);
295
            } else { /* Symtab completion */
294
            } else { /* Symtab completion */
296
                found = symtab_compl(tmp);
295
                found = symtab_compl(tmp);
297
            }
296
            }
298
 
297
 
299
            if (found == 0)
298
            if (found == 0)
300
                continue;
299
                continue;
301
            for (i = 0; tmp[i] && curlen < MAX_CMDLINE;
300
            for (i = 0; tmp[i] && curlen < MAX_CMDLINE;
302
                i++, curlen++)
301
                i++, curlen++)
303
                insert_char(current, tmp[i], i + position);
302
                insert_char(current, tmp[i], i + position);
304
 
303
 
305
            if (strlen(tmp) || found == 1) { /* If we have a hint */
304
            if (strlen(tmp) || found == 1) { /* If we have a hint */
306
                for (i = position; i < curlen; i++)
305
                for (i = position; i < curlen; i++)
307
                    putchar(current[i]);
306
                    putchar(current[i]);
308
                position += strlen(tmp);
307
                position += strlen(tmp);
309
                /* Add space to end */
308
                /* Add space to end */
310
                if (found == 1 && position == curlen &&
309
                if (found == 1 && position == curlen &&
311
                    curlen < MAX_CMDLINE) {
310
                    curlen < MAX_CMDLINE) {
312
                    current[position] = ' ';
311
                    current[position] = ' ';
313
                    curlen++;
312
                    curlen++;
314
                    position++;
313
                    position++;
315
                    putchar(' ');
314
                    putchar(' ');
316
                }
315
                }
317
            } else { /* No hint, table was printed */
316
            } else { /* No hint, table was printed */
318
                printf("%s> ", prompt);
317
                printf("%s> ", prompt);
319
                for (i = 0; i < curlen; i++)
318
                for (i = 0; i < curlen; i++)
320
                    putchar(current[i]);
319
                    putchar(current[i]);
321
                position += strlen(tmp);
320
                position += strlen(tmp);
322
            }
321
            }
323
            rdln_print_c('\b', curlen - position);
322
            rdln_print_c('\b', curlen - position);
324
            continue;
323
            continue;
325
        }
324
        }
326
        if (c == 0x1b) { /* Special command */
325
        if (c == 0x1b) { /* Special command */
327
            mod = _getc(input);
326
            mod = _getc(input);
328
            c = _getc(input);
327
            c = _getc(input);
329
 
328
 
330
            if (mod != 0x5b && mod != 0x4f)
329
            if (mod != 0x5b && mod != 0x4f)
331
                continue;
330
                continue;
332
 
331
 
333
            if (c == 0x33 && _getc(input) == 0x7e) {
332
            if (c == 0x33 && _getc(input) == 0x7e) {
334
                /* Delete */
333
                /* Delete */
335
                if (position == curlen)
334
                if (position == curlen)
336
                    continue;
335
                    continue;
337
                for (i = position + 1; i < curlen; i++) {
336
                for (i = position + 1; i < curlen; i++) {
338
                    putchar(current[i]);
337
                    putchar(current[i]);
339
                    current[i - 1] = current[i];
338
                    current[i - 1] = current[i];
340
                }
339
                }
341
                putchar(' ');
340
                putchar(' ');
342
                rdln_print_c('\b', curlen - position);
341
                rdln_print_c('\b', curlen - position);
343
                curlen--;
342
                curlen--;
344
            } else if (c == 0x48) { /* Home */
343
            } else if (c == 0x48) { /* Home */
345
                rdln_print_c('\b', position);
344
                rdln_print_c('\b', position);
346
                position = 0;
345
                position = 0;
347
            } else if (c == 0x46) {  /* End */
346
            } else if (c == 0x46) {  /* End */
348
                for (i = position; i < curlen; i++)
347
                for (i = position; i < curlen; i++)
349
                    putchar(current[i]);
348
                    putchar(current[i]);
350
                position = curlen;
349
                position = curlen;
351
            } else if (c == 0x44) { /* Left */
350
            } else if (c == 0x44) { /* Left */
352
                if (position > 0) {
351
                if (position > 0) {
353
                    putchar('\b');
352
                    putchar('\b');
354
                    position--;
353
                    position--;
355
                }
354
                }
356
                continue;
355
                continue;
357
            } else if (c == 0x43) { /* Right */
356
            } else if (c == 0x43) { /* Right */
358
                if (position < curlen) {
357
                if (position < curlen) {
359
                    putchar(current[position]);
358
                    putchar(current[position]);
360
                    position++;
359
                    position++;
361
                }
360
                }
362
                continue;
361
                continue;
363
            } else if (c == 0x41 || c == 0x42) {
362
            } else if (c == 0x41 || c == 0x42) {
364
                                /* Up, down */
363
                                /* Up, down */
365
                rdln_print_c('\b', position);
364
                rdln_print_c('\b', position);
366
                rdln_print_c(' ', curlen);
365
                rdln_print_c(' ', curlen);
367
                rdln_print_c('\b', curlen);
366
                rdln_print_c('\b', curlen);
368
                if (c == 0x41) /* Up */
367
                if (c == 0x41) /* Up */
369
                    histposition--;
368
                    histposition--;
370
                else
369
                else
371
                    histposition++;
370
                    histposition++;
372
                if (histposition < 0) {
371
                if (histposition < 0) {
373
                    histposition = KCONSOLE_HISTORY - 1;
372
                    histposition = KCONSOLE_HISTORY - 1;
374
                } else {
373
                } else {
375
                    histposition =
374
                    histposition =
376
                        histposition % KCONSOLE_HISTORY;
375
                        histposition % KCONSOLE_HISTORY;
377
                }
376
                }
378
                current = history[histposition];
377
                current = history[histposition];
379
                printf("%s", current);
378
                printf("%s", current);
380
                curlen = strlen(current);
379
                curlen = strlen(current);
381
                position = curlen;
380
                position = curlen;
382
                continue;
381
                continue;
383
            }
382
            }
384
            continue;
383
            continue;
385
        }
384
        }
386
        if (curlen >= MAX_CMDLINE)
385
        if (curlen >= MAX_CMDLINE)
387
            continue;
386
            continue;
388
 
387
 
389
        insert_char(current, c, position);
388
        insert_char(current, c, position);
390
 
389
 
391
        curlen++;
390
        curlen++;
392
        for (i = position; i < curlen; i++)
391
        for (i = position; i < curlen; i++)
393
            putchar(current[i]);
392
            putchar(current[i]);
394
        position++;
393
        position++;
395
        rdln_print_c('\b',curlen - position);
394
        rdln_print_c('\b',curlen - position);
396
    }
395
    }
397
    if (curlen) {
396
    if (curlen) {
398
        histposition++;
397
        histposition++;
399
        histposition = histposition % KCONSOLE_HISTORY;
398
        histposition = histposition % KCONSOLE_HISTORY;
400
    }
399
    }
401
    current[curlen] = '\0';
400
    current[curlen] = '\0';
402
    return current;
401
    return current;
403
}
402
}
404
 
403
 
405
/** Kernel console managing thread.
404
/** Kernel console managing thread.
406
 *
405
 *
407
 * @param prompt Kernel console prompt (e.g kconsole/panic).
406
 * @param prompt Kernel console prompt (e.g kconsole/panic).
408
 */
407
 */
409
void kconsole(void *prompt)
408
void kconsole(void *prompt)
410
{
409
{
411
    cmd_info_t *cmd_info;
410
    cmd_info_t *cmd_info;
412
    count_t len;
411
    count_t len;
413
    char *cmdline;
412
    char *cmdline;
414
 
413
 
415
    if (!stdin) {
414
    if (!stdin) {
416
        printf("%s: no stdin\n", __func__);
415
        printf("%s: no stdin\n", __func__);
417
        return;
416
        return;
418
    }
417
    }
419
   
418
   
420
    while (true) {
419
    while (true) {
421
        cmdline = clever_readline((char *) prompt, stdin);
420
        cmdline = clever_readline((char *) prompt, stdin);
422
        len = strlen(cmdline);
421
        len = strlen(cmdline);
423
        if (!len)
422
        if (!len)
424
            continue;
423
            continue;
425
        cmd_info = parse_cmdline(cmdline, len);
424
        cmd_info = parse_cmdline(cmdline, len);
426
        if (!cmd_info)
425
        if (!cmd_info)
427
            continue;
426
            continue;
428
        if (strncmp(cmd_info->name, "exit",
427
        if (strncmp(cmd_info->name, "exit",
429
            min(strlen(cmd_info->name), 5)) == 0)
428
            min(strlen(cmd_info->name), 5)) == 0)
430
            break;
429
            break;
431
        (void) cmd_info->func(cmd_info->argv);
430
        (void) cmd_info->func(cmd_info->argv);
432
    }
431
    }
433
}
432
}
434
 
433
 
435
static int parse_int_arg(char *text, size_t len, unative_t *result)
434
static int parse_int_arg(char *text, size_t len, unative_t *result)
436
{
435
{
437
    static char symname[MAX_SYMBOL_NAME];
436
    static char symname[MAX_SYMBOL_NAME];
438
    uintptr_t symaddr;
437
    uintptr_t symaddr;
439
    bool isaddr = false;
438
    bool isaddr = false;
440
    bool isptr = false;
439
    bool isptr = false;
441
   
440
   
442
    /* If we get a name, try to find it in symbol table */
441
    /* If we get a name, try to find it in symbol table */
443
    if (text[0] == '&') {
442
    if (text[0] == '&') {
444
        isaddr = true;
443
        isaddr = true;
445
        text++;
444
        text++;
446
        len--;
445
        len--;
447
    } else if (text[0] == '*') {
446
    } else if (text[0] == '*') {
448
        isptr = true;
447
        isptr = true;
449
        text++;
448
        text++;
450
        len--;
449
        len--;
451
    }
450
    }
452
    if (text[0] < '0' || text[0] > '9') {
451
    if (text[0] < '0' || text[0] > '9') {
453
        strncpy(symname, text, min(len + 1, MAX_SYMBOL_NAME));
452
        strncpy(symname, text, min(len + 1, MAX_SYMBOL_NAME));
454
        symaddr = get_symbol_addr(symname);
453
        symaddr = get_symbol_addr(symname);
455
        if (!symaddr) {
454
        if (!symaddr) {
456
            printf("Symbol %s not found.\n", symname);
455
            printf("Symbol %s not found.\n", symname);
457
            return -1;
456
            return -1;
458
        }
457
        }
459
        if (symaddr == (uintptr_t) -1) {
458
        if (symaddr == (uintptr_t) -1) {
460
            printf("Duplicate symbol %s.\n", symname);
459
            printf("Duplicate symbol %s.\n", symname);
461
            symtab_print_search(symname);
460
            symtab_print_search(symname);
462
            return -1;
461
            return -1;
463
        }
462
        }
464
        if (isaddr)
463
        if (isaddr)
465
            *result = (unative_t)symaddr;
464
            *result = (unative_t)symaddr;
466
        else if (isptr)
465
        else if (isptr)
467
            *result = **((unative_t **)symaddr);
466
            *result = **((unative_t **)symaddr);
468
        else
467
        else
469
            *result = *((unative_t *)symaddr);
468
            *result = *((unative_t *)symaddr);
470
    } else { /* It's a number - convert it */
469
    } else { /* It's a number - convert it */
471
        *result = atoi(text);
470
        *result = atoi(text);
472
        if (isptr)
471
        if (isptr)
473
            *result = *((unative_t *)*result);
472
            *result = *((unative_t *)*result);
474
    }
473
    }
475
 
474
 
476
    return 0;
475
    return 0;
477
}
476
}
478
 
477
 
479
/** Parse command line.
478
/** Parse command line.
480
 *
479
 *
481
 * @param cmdline Command line as read from input device.
480
 * @param cmdline Command line as read from input device.
482
 * @param len Command line length.
481
 * @param len Command line length.
483
 *
482
 *
484
 * @return Structure describing the command.
483
 * @return Structure describing the command.
485
 */
484
 */
486
cmd_info_t *parse_cmdline(char *cmdline, size_t len)
485
cmd_info_t *parse_cmdline(char *cmdline, size_t len)
487
{
486
{
488
    index_t start = 0, end = 0;
487
    index_t start = 0, end = 0;
489
    cmd_info_t *cmd = NULL;
488
    cmd_info_t *cmd = NULL;
490
    link_t *cur;
489
    link_t *cur;
491
    count_t i;
490
    count_t i;
492
    int error = 0;
491
    int error = 0;
493
   
492
   
494
    if (!parse_argument(cmdline, len, &start, &end)) {
493
    if (!parse_argument(cmdline, len, &start, &end)) {
495
        /* Command line did not contain alphanumeric word. */
494
        /* Command line did not contain alphanumeric word. */
496
        return NULL;
495
        return NULL;
497
    }
496
    }
498
 
497
 
499
    spinlock_lock(&cmd_lock);
498
    spinlock_lock(&cmd_lock);
500
   
499
   
501
    for (cur = cmd_head.next; cur != &cmd_head; cur = cur->next) {
500
    for (cur = cmd_head.next; cur != &cmd_head; cur = cur->next) {
502
        cmd_info_t *hlp;
501
        cmd_info_t *hlp;
503
       
502
       
504
        hlp = list_get_instance(cur, cmd_info_t, link);
503
        hlp = list_get_instance(cur, cmd_info_t, link);
505
        spinlock_lock(&hlp->lock);
504
        spinlock_lock(&hlp->lock);
506
       
505
       
507
        if (strncmp(hlp->name, &cmdline[start], max(strlen(hlp->name),
506
        if (strncmp(hlp->name, &cmdline[start], max(strlen(hlp->name),
508
            end - start + 1)) == 0) {
507
            end - start + 1)) == 0) {
509
            cmd = hlp;
508
            cmd = hlp;
510
            break;
509
            break;
511
        }
510
        }
512
       
511
       
513
        spinlock_unlock(&hlp->lock);
512
        spinlock_unlock(&hlp->lock);
514
    }
513
    }
515
   
514
   
516
    spinlock_unlock(&cmd_lock);
515
    spinlock_unlock(&cmd_lock);
517
   
516
   
518
    if (!cmd) {
517
    if (!cmd) {
519
        /* Unknown command. */
518
        /* Unknown command. */
520
        printf("Unknown command.\n");
519
        printf("Unknown command.\n");
521
        return NULL;
520
        return NULL;
522
    }
521
    }
523
 
522
 
524
    /* cmd == hlp is locked */
523
    /* cmd == hlp is locked */
525
   
524
   
526
    /*
525
    /*
527
     * The command line must be further analyzed and
526
     * The command line must be further analyzed and
528
     * the parameters therefrom must be matched and
527
     * the parameters therefrom must be matched and
529
     * converted to those specified in the cmd info
528
     * converted to those specified in the cmd info
530
     * structure.
529
     * structure.
531
     */
530
     */
532
 
531
 
533
    for (i = 0; i < cmd->argc; i++) {
532
    for (i = 0; i < cmd->argc; i++) {
534
        char *buf;
533
        char *buf;
535
        start = end + 1;
534
        start = end + 1;
536
        if (!parse_argument(cmdline, len, &start, &end)) {
535
        if (!parse_argument(cmdline, len, &start, &end)) {
537
            printf("Too few arguments.\n");
536
            printf("Too few arguments.\n");
538
            spinlock_unlock(&cmd->lock);
537
            spinlock_unlock(&cmd->lock);
539
            return NULL;
538
            return NULL;
540
        }
539
        }
541
       
540
       
542
        error = 0;
541
        error = 0;
543
        switch (cmd->argv[i].type) {
542
        switch (cmd->argv[i].type) {
544
        case ARG_TYPE_STRING:
543
        case ARG_TYPE_STRING:
545
            buf = (char *) cmd->argv[i].buffer;
544
            buf = (char *) cmd->argv[i].buffer;
546
            strncpy(buf, (const char *) &cmdline[start],
545
            strncpy(buf, (const char *) &cmdline[start],
547
                min((end - start) + 2, cmd->argv[i].len));
546
                min((end - start) + 2, cmd->argv[i].len));
548
            buf[min((end - start) + 1, cmd->argv[i].len - 1)] =
547
            buf[min((end - start) + 1, cmd->argv[i].len - 1)] =
549
                '\0';
548
                '\0';
550
            break;
549
            break;
551
        case ARG_TYPE_INT:
550
        case ARG_TYPE_INT:
552
            if (parse_int_arg(cmdline + start, end - start + 1,
551
            if (parse_int_arg(cmdline + start, end - start + 1,
553
                &cmd->argv[i].intval))
552
                &cmd->argv[i].intval))
554
                error = 1;
553
                error = 1;
555
            break;
554
            break;
556
        case ARG_TYPE_VAR:
555
        case ARG_TYPE_VAR:
557
            if (start != end && cmdline[start] == '"' &&
556
            if (start != end && cmdline[start] == '"' &&
558
                cmdline[end] == '"') {
557
                cmdline[end] == '"') {
559
                buf = (char *) cmd->argv[i].buffer;
558
                buf = (char *) cmd->argv[i].buffer;
560
                strncpy(buf, (const char *) &cmdline[start + 1],
559
                strncpy(buf, (const char *) &cmdline[start + 1],
561
                    min((end-start), cmd->argv[i].len));
560
                    min((end-start), cmd->argv[i].len));
562
                buf[min((end - start), cmd->argv[i].len - 1)] =
561
                buf[min((end - start), cmd->argv[i].len - 1)] =
563
                    '\0';
562
                    '\0';
564
                cmd->argv[i].intval = (unative_t) buf;
563
                cmd->argv[i].intval = (unative_t) buf;
565
                cmd->argv[i].vartype = ARG_TYPE_STRING;
564
                cmd->argv[i].vartype = ARG_TYPE_STRING;
566
            } else if (!parse_int_arg(cmdline + start,
565
            } else if (!parse_int_arg(cmdline + start,
567
                end - start + 1, &cmd->argv[i].intval)) {
566
                end - start + 1, &cmd->argv[i].intval)) {
568
                cmd->argv[i].vartype = ARG_TYPE_INT;
567
                cmd->argv[i].vartype = ARG_TYPE_INT;
569
            } else {
568
            } else {
570
                printf("Unrecognized variable argument.\n");
569
                printf("Unrecognized variable argument.\n");
571
                error = 1;
570
                error = 1;
572
            }
571
            }
573
            break;
572
            break;
574
        case ARG_TYPE_INVALID:
573
        case ARG_TYPE_INVALID:
575
        default:
574
        default:
576
            printf("invalid argument type\n");
575
            printf("invalid argument type\n");
577
            error = 1;
576
            error = 1;
578
            break;
577
            break;
579
        }
578
        }
580
    }
579
    }
581
   
580
   
582
    if (error) {
581
    if (error) {
583
        spinlock_unlock(&cmd->lock);
582
        spinlock_unlock(&cmd->lock);
584
        return NULL;
583
        return NULL;
585
    }
584
    }
586
   
585
   
587
    start = end + 1;
586
    start = end + 1;
588
    if (parse_argument(cmdline, len, &start, &end)) {
587
    if (parse_argument(cmdline, len, &start, &end)) {
589
        printf("Too many arguments.\n");
588
        printf("Too many arguments.\n");
590
        spinlock_unlock(&cmd->lock);
589
        spinlock_unlock(&cmd->lock);
591
        return NULL;
590
        return NULL;
592
    }
591
    }
593
   
592
   
594
    spinlock_unlock(&cmd->lock);
593
    spinlock_unlock(&cmd->lock);
595
    return cmd;
594
    return cmd;
596
}
595
}
597
 
596
 
598
/** Parse argument.
597
/** Parse argument.
599
 *
598
 *
600
 * Find start and end positions of command line argument.
599
 * Find start and end positions of command line argument.
601
 *
600
 *
602
 * @param cmdline Command line as read from the input device.
601
 * @param cmdline Command line as read from the input device.
603
 * @param len Number of characters in cmdline.
602
 * @param len Number of characters in cmdline.
604
 * @param start On entry, 'start' contains pointer to the index
603
 * @param start On entry, 'start' contains pointer to the index
605
 *        of first unprocessed character of cmdline.
604
 *        of first unprocessed character of cmdline.
606
 *        On successful exit, it marks beginning of the next argument.
605
 *        On successful exit, it marks beginning of the next argument.
607
 * @param end Undefined on entry. On exit, 'end' points to the last character
606
 * @param end Undefined on entry. On exit, 'end' points to the last character
608
 *        of the next argument.
607
 *        of the next argument.
609
 *
608
 *
610
 * @return false on failure, true on success.
609
 * @return false on failure, true on success.
611
 */
610
 */
612
bool parse_argument(char *cmdline, size_t len, index_t *start, index_t *end)
611
bool parse_argument(char *cmdline, size_t len, index_t *start, index_t *end)
613
{
612
{
614
    index_t i;
613
    index_t i;
615
    bool found_start = false;
614
    bool found_start = false;
616
   
615
   
617
    ASSERT(start != NULL);
616
    ASSERT(start != NULL);
618
    ASSERT(end != NULL);
617
    ASSERT(end != NULL);
619
   
618
   
620
    for (i = *start; i < len; i++) {
619
    for (i = *start; i < len; i++) {
621
        if (!found_start) {
620
        if (!found_start) {
622
            if (isspace(cmdline[i]))
621
            if (isspace(cmdline[i]))
623
                (*start)++;
622
                (*start)++;
624
            else
623
            else
625
                found_start = true;
624
                found_start = true;
626
        } else {
625
        } else {
627
            if (isspace(cmdline[i]))
626
            if (isspace(cmdline[i]))
628
                break;
627
                break;
629
        }
628
        }
630
    }
629
    }
631
    *end = i - 1;
630
    *end = i - 1;
632
 
631
 
633
    return found_start;
632
    return found_start;
634
}
633
}
635
 
634
 
636
/** @}
635
/** @}
637
 */
636
 */
638
 
637