Subversion Repositories HelenOS

Rev

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

Rev 1888 Rev 1972
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 <typedefs.h>
46
#include <typedefs.h>
47
#include <arch/types.h>
47
#include <arch/types.h>
48
#include <adt/list.h>
48
#include <adt/list.h>
49
#include <arch.h>
49
#include <arch.h>
50
#include <macros.h>
50
#include <macros.h>
51
#include <debug.h>
51
#include <debug.h>
52
#include <func.h>
52
#include <func.h>
53
#include <symtab.h>
53
#include <symtab.h>
54
#include <macros.h>
54
#include <macros.h>
55
 
55
 
56
/** Simple kernel console.
56
/** Simple kernel console.
57
 *
57
 *
58
 * The console is realized by kernel thread kconsole.
58
 * The console is realized by kernel thread kconsole.
59
 * It doesn't understand any useful command on its own,
59
 * It doesn't understand any useful command on its own,
60
 * but makes it possible for other kernel subsystems to
60
 * but makes it possible for other kernel subsystems to
61
 * register their own commands.
61
 * register their own commands.
62
 */
62
 */
63
 
63
 
64
/** Locking.
64
/** Locking.
65
 *
65
 *
66
 * There is a list of cmd_info_t structures. This list
66
 * There is a list of cmd_info_t structures. This list
67
 * is protected by cmd_lock spinlock. Note that specially
67
 * is protected by cmd_lock spinlock. Note that specially
68
 * the link elements of cmd_info_t are protected by
68
 * the link elements of cmd_info_t are protected by
69
 * this lock.
69
 * this lock.
70
 *
70
 *
71
 * Each cmd_info_t also has its own lock, which protects
71
 * Each cmd_info_t also has its own lock, which protects
72
 * all elements thereof except the link element.
72
 * all elements thereof except the link element.
73
 *
73
 *
74
 * cmd_lock must be acquired before any cmd_info lock.
74
 * cmd_lock must be acquired before any cmd_info lock.
75
 * When locking two cmd info structures, structure with
75
 * When locking two cmd info structures, structure with
76
 * lower address must be locked first.
76
 * lower address must be locked first.
77
 */
77
 */
78
 
78
 
79
SPINLOCK_INITIALIZE(cmd_lock);  /**< Lock protecting command list. */
79
SPINLOCK_INITIALIZE(cmd_lock);  /**< Lock protecting command list. */
80
LIST_INITIALIZE(cmd_head);  /**< Command list. */
80
LIST_INITIALIZE(cmd_head);  /**< Command list. */
81
 
81
 
82
static cmd_info_t *parse_cmdline(char *cmdline, size_t len);
82
static cmd_info_t *parse_cmdline(char *cmdline, size_t len);
83
static bool parse_argument(char *cmdline, size_t len, index_t *start, index_t *end);
83
static bool parse_argument(char *cmdline, size_t len, index_t *start, 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,
131
        if ((strncmp(hlp->name,
132
                 cmd->name, max(strlen(cmd->name),
132
                 cmd->name, max(strlen(cmd->name),
133
                        strlen(hlp->name))) == 0)) {
133
                        strlen(hlp->name))) == 0)) {
134
            /* The command is already there. */
134
            /* The command is already there. */
135
            spinlock_unlock(&hlp->lock);
135
            spinlock_unlock(&hlp->lock);
136
            spinlock_unlock(&cmd->lock);
136
            spinlock_unlock(&cmd->lock);
137
            spinlock_unlock(&cmd_lock);
137
            spinlock_unlock(&cmd_lock);
138
            return 0;
138
            return 0;
139
        }
139
        }
140
       
140
       
141
        spinlock_unlock(&hlp->lock);
141
        spinlock_unlock(&hlp->lock);
142
        spinlock_unlock(&cmd->lock);
142
        spinlock_unlock(&cmd->lock);
143
    }
143
    }
144
   
144
   
145
    /*
145
    /*
146
     * Now the command can be added.
146
     * Now the command can be added.
147
     */
147
     */
148
    list_append(&cmd->link, &cmd_head);
148
    list_append(&cmd->link, &cmd_head);
149
   
149
   
150
    spinlock_unlock(&cmd_lock);
150
    spinlock_unlock(&cmd_lock);
151
    return 1;
151
    return 1;
152
}
152
}
153
 
153
 
154
/** Print count times a character */
154
/** Print count times a character */
155
static void rdln_print_c(char ch, int count)
155
static void rdln_print_c(char ch, int count)
156
{
156
{
157
    int i;
157
    int i;
158
    for (i=0;i<count;i++)
158
    for (i=0;i<count;i++)
159
        putchar(ch);
159
        putchar(ch);
160
}
160
}
161
 
161
 
162
/** Insert character to string */
162
/** Insert character to string */
163
static void insert_char(char *str, char ch, int pos)
163
static void insert_char(char *str, char ch, int pos)
164
{
164
{
165
    int i;
165
    int i;
166
   
166
   
167
    for (i=strlen(str);i > pos; i--)
167
    for (i=strlen(str);i > pos; i--)
168
        str[i] = str[i-1];
168
        str[i] = str[i-1];
169
    str[pos] = ch;
169
    str[pos] = ch;
170
}
170
}
171
 
171
 
172
/** Try to find a command beginning with prefix */
172
/** Try to find a command beginning with prefix */
173
static const char * cmdtab_search_one(const char *name,link_t **startpos)
173
static const char * cmdtab_search_one(const char *name,link_t **startpos)
174
{
174
{
175
    int namelen = strlen(name);
175
    int namelen = strlen(name);
176
    const char *curname;
176
    const char *curname;
177
 
177
 
178
    spinlock_lock(&cmd_lock);
178
    spinlock_lock(&cmd_lock);
179
 
179
 
180
    if (!*startpos)
180
    if (!*startpos)
181
        *startpos = cmd_head.next;
181
        *startpos = cmd_head.next;
182
 
182
 
183
    for (;*startpos != &cmd_head;*startpos = (*startpos)->next) {
183
    for (;*startpos != &cmd_head;*startpos = (*startpos)->next) {
184
        cmd_info_t *hlp;
184
        cmd_info_t *hlp;
185
        hlp = list_get_instance(*startpos, cmd_info_t, link);
185
        hlp = list_get_instance(*startpos, cmd_info_t, link);
186
 
186
 
187
        curname = hlp->name;
187
        curname = hlp->name;
188
        if (strlen(curname) < namelen)
188
        if (strlen(curname) < namelen)
189
            continue;
189
            continue;
190
        if (strncmp(curname, name, namelen) == 0) {
190
        if (strncmp(curname, name, namelen) == 0) {
191
            spinlock_unlock(&cmd_lock);
191
            spinlock_unlock(&cmd_lock);
192
            return curname+namelen;
192
            return curname+namelen;
193
        }
193
        }
194
    }
194
    }
195
    spinlock_unlock(&cmd_lock);
195
    spinlock_unlock(&cmd_lock);
196
    return NULL;
196
    return NULL;
197
}
197
}
198
 
198
 
199
 
199
 
200
/** Command completion of the commands
200
/** Command completion of the commands
201
 *
201
 *
202
 * @param name - string to match, changed to hint on exit
202
 * @param name - string to match, changed to hint on exit
203
 * @return number of found matches
203
 * @return number of found matches
204
 */
204
 */
205
static int cmdtab_compl(char *name)
205
static int cmdtab_compl(char *name)
206
{
206
{
207
    char output[MAX_SYMBOL_NAME+1];
207
    static char output[MAX_SYMBOL_NAME+1];
208
    link_t *startpos = NULL;
208
    link_t *startpos = NULL;
209
    const char *foundtxt;
209
    const char *foundtxt;
210
    int found = 0;
210
    int found = 0;
211
    int i;
211
    int i;
212
 
212
 
213
    output[0] = '\0';
213
    output[0] = '\0';
214
    while ((foundtxt = cmdtab_search_one(name, &startpos))) {
214
    while ((foundtxt = cmdtab_search_one(name, &startpos))) {
215
        startpos = startpos->next;
215
        startpos = startpos->next;
216
        if (!found)
216
        if (!found)
217
            strncpy(output, foundtxt, strlen(foundtxt)+1);
217
            strncpy(output, foundtxt, strlen(foundtxt)+1);
218
        else {
218
        else {
219
            for (i=0; output[i] && foundtxt[i] && output[i]==foundtxt[i]; i++)
219
            for (i=0; output[i] && foundtxt[i] && 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
    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]!=' ';position++)
278
            for (;position<curlen && current[position]!=' ';position++)
279
                putchar(current[position]);
279
                putchar(current[position]);
280
            /* Copy to tmp last word */
280
            /* Copy to tmp last word */
281
            for (i=position-1;i >= 0 && current[i]!=' ' ;i--)
281
            for (i=position-1;i >= 0 && current[i]!=' ' ;i--)
282
                ;
282
                ;
283
            /* If word begins with * or &, skip it */
283
            /* If word begins with * or &, skip it */
284
            if (tmp[0] == '*' || tmp[0] == '&')
284
            if (tmp[0] == '*' || tmp[0] == '&')
285
                for (i=1;tmp[i];i++)
285
                for (i=1;tmp[i];i++)
286
                    tmp[i-1] = tmp[i];
286
                    tmp[i-1] = tmp[i];
287
            i++; /* I is at the start of the word */
287
            i++; /* I is at the start of the word */
288
            strncpy(tmp, current+i, position-i+1);
288
            strncpy(tmp, current+i, position-i+1);
289
 
289
 
290
            if (i==0) { /* Command completion */
290
            if (i==0) { /* Command completion */
291
                found = cmdtab_compl(tmp);
291
                found = cmdtab_compl(tmp);
292
            } else { /* Symtab completion */
292
            } else { /* Symtab completion */
293
                found = symtab_compl(tmp);
293
                found = symtab_compl(tmp);
294
            }
294
            }
295
 
295
 
296
            if (found == 0)
296
            if (found == 0)
297
                continue;
297
                continue;
298
            for (i=0;tmp[i] && curlen < MAX_CMDLINE;i++,curlen++)
298
            for (i=0;tmp[i] && curlen < MAX_CMDLINE;i++,curlen++)
299
                insert_char(current, tmp[i], i+position);
299
                insert_char(current, tmp[i], i+position);
300
 
300
 
301
            if (strlen(tmp) || found==1) { /* If we have a hint */
301
            if (strlen(tmp) || found==1) { /* If we have a hint */
302
                for (i=position;i<curlen;i++)
302
                for (i=position;i<curlen;i++)
303
                    putchar(current[i]);
303
                    putchar(current[i]);
304
                position += strlen(tmp);
304
                position += strlen(tmp);
305
                /* Add space to end */
305
                /* Add space to end */
306
                if (found == 1 && position == curlen && \
306
                if (found == 1 && position == curlen && \
307
                    curlen < MAX_CMDLINE) {
307
                    curlen < MAX_CMDLINE) {
308
                    current[position] = ' ';
308
                    current[position] = ' ';
309
                    curlen++;
309
                    curlen++;
310
                    position++;
310
                    position++;
311
                    putchar(' ');
311
                    putchar(' ');
312
                }
312
                }
313
            } else { /* No hint, table was printed */
313
            } else { /* No hint, table was printed */
314
                printf("%s> ", prompt);
314
                printf("%s> ", prompt);
315
                for (i=0; i<curlen;i++)
315
                for (i=0; i<curlen;i++)
316
                    putchar(current[i]);
316
                    putchar(current[i]);
317
                position += strlen(tmp);
317
                position += strlen(tmp);
318
            }
318
            }
319
            rdln_print_c('\b', curlen-position);
319
            rdln_print_c('\b', curlen-position);
320
            continue;
320
            continue;
321
        }
321
        }
322
        if (c == 0x1b) { /* Special command */
322
        if (c == 0x1b) { /* Special command */
323
            mod = _getc(input);
323
            mod = _getc(input);
324
            c = _getc(input);
324
            c = _getc(input);
325
 
325
 
326
            if (mod != 0x5b && mod != 0x4f)
326
            if (mod != 0x5b && mod != 0x4f)
327
                continue;
327
                continue;
328
 
328
 
329
            if (c == 0x33 && _getc(input) == 0x7e) {
329
            if (c == 0x33 && _getc(input) == 0x7e) {
330
                /* Delete */
330
                /* Delete */
331
                if (position == curlen)
331
                if (position == curlen)
332
                    continue;
332
                    continue;
333
                for (i=position+1; i<curlen;i++) {
333
                for (i=position+1; i<curlen;i++) {
334
                    putchar(current[i]);
334
                    putchar(current[i]);
335
                    current[i-1] = current[i];
335
                    current[i-1] = current[i];
336
                }
336
                }
337
                putchar(' ');
337
                putchar(' ');
338
                rdln_print_c('\b',curlen-position);
338
                rdln_print_c('\b',curlen-position);
339
                curlen--;
339
                curlen--;
340
            }
340
            }
341
            else if (c == 0x48) { /* Home */
341
            else if (c == 0x48) { /* Home */
342
                rdln_print_c('\b',position);
342
                rdln_print_c('\b',position);
343
                position = 0;
343
                position = 0;
344
            }
344
            }
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
            }
349
            }
350
            else if (c == 0x44) { /* Left */
350
            else if (c == 0x44) { /* Left */
351
                if (position > 0) {
351
                if (position > 0) {
352
                    putchar('\b');
352
                    putchar('\b');
353
                    position--;
353
                    position--;
354
                }
354
                }
355
                continue;
355
                continue;
356
            }
356
            }
357
            else if (c == 0x43) { /* Right */
357
            else if (c == 0x43) { /* Right */
358
                if (position < curlen) {
358
                if (position < curlen) {
359
                    putchar(current[position]);
359
                    putchar(current[position]);
360
                    position++;
360
                    position++;
361
                }
361
                }
362
                continue;
362
                continue;
363
            }
363
            }
364
            else if (c == 0x41 || c == 0x42) {
364
            else if (c == 0x41 || c == 0x42) {
365
                                /* Up,down */
365
                                /* Up,down */
366
                rdln_print_c('\b',position);
366
                rdln_print_c('\b',position);
367
                rdln_print_c(' ',curlen);
367
                rdln_print_c(' ',curlen);
368
                rdln_print_c('\b',curlen);
368
                rdln_print_c('\b',curlen);
369
                if (c == 0x41) /* Up */
369
                if (c == 0x41) /* Up */
370
                    histposition--;
370
                    histposition--;
371
                else
371
                else
372
                    histposition++;
372
                    histposition++;
373
                if (histposition < 0)
373
                if (histposition < 0)
374
                    histposition = KCONSOLE_HISTORY -1 ;
374
                    histposition = KCONSOLE_HISTORY -1 ;
375
                else
375
                else
376
                    histposition =  histposition % KCONSOLE_HISTORY;
376
                    histposition =  histposition % KCONSOLE_HISTORY;
377
                current = history[histposition];
377
                current = history[histposition];
378
                printf("%s", current);
378
                printf("%s", current);
379
                curlen = strlen(current);
379
                curlen = strlen(current);
380
                position = curlen;
380
                position = curlen;
381
                continue;
381
                continue;
382
            }
382
            }
383
            continue;
383
            continue;
384
        }
384
        }
385
        if (curlen >= MAX_CMDLINE)
385
        if (curlen >= MAX_CMDLINE)
386
            continue;
386
            continue;
387
 
387
 
388
        insert_char(current, c, position);
388
        insert_char(current, c, position);
389
 
389
 
390
        curlen++;
390
        curlen++;
391
        for (i=position;i<curlen;i++)
391
        for (i=position;i<curlen;i++)
392
            putchar(current[i]);
392
            putchar(current[i]);
393
        position++;
393
        position++;
394
        rdln_print_c('\b',curlen-position);
394
        rdln_print_c('\b',curlen-position);
395
    }
395
    }
396
    if (curlen) {
396
    if (curlen) {
397
        histposition++;
397
        histposition++;
398
        histposition = histposition % KCONSOLE_HISTORY;
398
        histposition = histposition % KCONSOLE_HISTORY;
399
    }
399
    }
400
    current[curlen] = '\0';
400
    current[curlen] = '\0';
401
    return current;
401
    return current;
402
}
402
}
403
 
403
 
404
/** Kernel console managing thread.
404
/** Kernel console managing thread.
405
 *
405
 *
406
 * @param prompt Kernel console prompt (e.g kconsole/panic).
406
 * @param prompt Kernel console prompt (e.g kconsole/panic).
407
 */
407
 */
408
void kconsole(void *prompt)
408
void kconsole(void *prompt)
409
{
409
{
410
    cmd_info_t *cmd_info;
410
    cmd_info_t *cmd_info;
411
    count_t len;
411
    count_t len;
412
    char *cmdline;
412
    char *cmdline;
413
 
413
 
414
    if (!stdin) {
414
    if (!stdin) {
415
        printf("%s: no stdin\n", __FUNCTION__);
415
        printf("%s: no stdin\n", __FUNCTION__);
416
        return;
416
        return;
417
    }
417
    }
418
   
418
   
419
    while (true) {
419
    while (true) {
420
        cmdline = clever_readline(prompt, stdin);
420
        cmdline = clever_readline(prompt, stdin);
421
        len = strlen(cmdline);
421
        len = strlen(cmdline);
422
        if (!len)
422
        if (!len)
423
            continue;
423
            continue;
424
        cmd_info = parse_cmdline(cmdline, len);
424
        cmd_info = parse_cmdline(cmdline, len);
425
        if (!cmd_info)
425
        if (!cmd_info)
426
            continue;
426
            continue;
427
        if (strncmp(cmd_info->name,"exit", \
427
        if (strncmp(cmd_info->name,"exit", \
428
                min(strlen(cmd_info->name),5)) == 0)
428
                min(strlen(cmd_info->name),5)) == 0)
429
            break;
429
            break;
430
        (void) cmd_info->func(cmd_info->argv);
430
        (void) cmd_info->func(cmd_info->argv);
431
    }
431
    }
432
}
432
}
433
 
433
 
434
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)
435
{
435
{
436
    char symname[MAX_SYMBOL_NAME];
436
    static char symname[MAX_SYMBOL_NAME];
437
    uintptr_t symaddr;
437
    uintptr_t symaddr;
438
    bool isaddr = false;
438
    bool isaddr = false;
439
    bool isptr = false;
439
    bool isptr = false;
440
   
440
   
441
    /* 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 */
442
    if (text[0] == '&') {
442
    if (text[0] == '&') {
443
        isaddr = true;
443
        isaddr = true;
444
        text++;len--;
444
        text++;len--;
445
    } else if (text[0] == '*') {
445
    } else if (text[0] == '*') {
446
        isptr = true;
446
        isptr = true;
447
        text++;len--;
447
        text++;len--;
448
    }
448
    }
449
    if (text[0] < '0' || text[0] > '9') {
449
    if (text[0] < '0' || text[0] > '9') {
450
        strncpy(symname, text, min(len+1, MAX_SYMBOL_NAME));
450
        strncpy(symname, text, min(len+1, MAX_SYMBOL_NAME));
451
        symaddr = get_symbol_addr(symname);
451
        symaddr = get_symbol_addr(symname);
452
        if (!symaddr) {
452
        if (!symaddr) {
453
            printf("Symbol %s not found.\n",symname);
453
            printf("Symbol %s not found.\n",symname);
454
            return -1;
454
            return -1;
455
        }
455
        }
456
        if (symaddr == (uintptr_t) -1) {
456
        if (symaddr == (uintptr_t) -1) {
457
            printf("Duplicate symbol %s.\n",symname);
457
            printf("Duplicate symbol %s.\n",symname);
458
            symtab_print_search(symname);
458
            symtab_print_search(symname);
459
            return -1;
459
            return -1;
460
        }
460
        }
461
        if (isaddr)
461
        if (isaddr)
462
            *result = (unative_t)symaddr;
462
            *result = (unative_t)symaddr;
463
        else if (isptr)
463
        else if (isptr)
464
            *result = **((unative_t **)symaddr);
464
            *result = **((unative_t **)symaddr);
465
        else
465
        else
466
            *result = *((unative_t *)symaddr);
466
            *result = *((unative_t *)symaddr);
467
    } else { /* It's a number - convert it */
467
    } else { /* It's a number - convert it */
468
        *result = atoi(text);
468
        *result = atoi(text);
469
        if (isptr)
469
        if (isptr)
470
            *result = *((unative_t *)*result);
470
            *result = *((unative_t *)*result);
471
    }
471
    }
472
 
472
 
473
    return 0;
473
    return 0;
474
}
474
}
475
 
475
 
476
/** Parse command line.
476
/** Parse command line.
477
 *
477
 *
478
 * @param cmdline Command line as read from input device.
478
 * @param cmdline Command line as read from input device.
479
 * @param len Command line length.
479
 * @param len Command line length.
480
 *
480
 *
481
 * @return Structure describing the command.
481
 * @return Structure describing the command.
482
 */
482
 */
483
cmd_info_t *parse_cmdline(char *cmdline, size_t len)
483
cmd_info_t *parse_cmdline(char *cmdline, size_t len)
484
{
484
{
485
    index_t start = 0, end = 0;
485
    index_t start = 0, end = 0;
486
    cmd_info_t *cmd = NULL;
486
    cmd_info_t *cmd = NULL;
487
    link_t *cur;
487
    link_t *cur;
488
    int i;
488
    int i;
489
    int error = 0;
489
    int error = 0;
490
   
490
   
491
    if (!parse_argument(cmdline, len, &start, &end)) {
491
    if (!parse_argument(cmdline, len, &start, &end)) {
492
        /* Command line did not contain alphanumeric word. */
492
        /* Command line did not contain alphanumeric word. */
493
        return NULL;
493
        return NULL;
494
    }
494
    }
495
 
495
 
496
    spinlock_lock(&cmd_lock);
496
    spinlock_lock(&cmd_lock);
497
   
497
   
498
    for (cur = cmd_head.next; cur != &cmd_head; cur = cur->next) {
498
    for (cur = cmd_head.next; cur != &cmd_head; cur = cur->next) {
499
        cmd_info_t *hlp;
499
        cmd_info_t *hlp;
500
       
500
       
501
        hlp = list_get_instance(cur, cmd_info_t, link);
501
        hlp = list_get_instance(cur, cmd_info_t, link);
502
        spinlock_lock(&hlp->lock);
502
        spinlock_lock(&hlp->lock);
503
       
503
       
504
        if (strncmp(hlp->name, &cmdline[start], max(strlen(hlp->name),
504
        if (strncmp(hlp->name, &cmdline[start], max(strlen(hlp->name),
505
                                end-start+1)) == 0) {
505
                                end-start+1)) == 0) {
506
            cmd = hlp;
506
            cmd = hlp;
507
            break;
507
            break;
508
        }
508
        }
509
       
509
       
510
        spinlock_unlock(&hlp->lock);
510
        spinlock_unlock(&hlp->lock);
511
    }
511
    }
512
   
512
   
513
    spinlock_unlock(&cmd_lock);
513
    spinlock_unlock(&cmd_lock);
514
   
514
   
515
    if (!cmd) {
515
    if (!cmd) {
516
        /* Unknown command. */
516
        /* Unknown command. */
517
        printf("Unknown command.\n");
517
        printf("Unknown command.\n");
518
        return NULL;
518
        return NULL;
519
    }
519
    }
520
 
520
 
521
    /* cmd == hlp is locked */
521
    /* cmd == hlp is locked */
522
   
522
   
523
    /*
523
    /*
524
     * The command line must be further analyzed and
524
     * The command line must be further analyzed and
525
     * the parameters therefrom must be matched and
525
     * the parameters therefrom must be matched and
526
     * converted to those specified in the cmd info
526
     * converted to those specified in the cmd info
527
     * structure.
527
     * structure.
528
     */
528
     */
529
 
529
 
530
    for (i = 0; i < cmd->argc; i++) {
530
    for (i = 0; i < cmd->argc; i++) {
531
        char *buf;
531
        char *buf;
532
        start = end + 1;
532
        start = end + 1;
533
        if (!parse_argument(cmdline, len, &start, &end)) {
533
        if (!parse_argument(cmdline, len, &start, &end)) {
534
            printf("Too few arguments.\n");
534
            printf("Too few arguments.\n");
535
            spinlock_unlock(&cmd->lock);
535
            spinlock_unlock(&cmd->lock);
536
            return NULL;
536
            return NULL;
537
        }
537
        }
538
       
538
       
539
        error = 0;
539
        error = 0;
540
        switch (cmd->argv[i].type) {
540
        switch (cmd->argv[i].type) {
541
        case ARG_TYPE_STRING:
541
        case ARG_TYPE_STRING:
542
                buf = cmd->argv[i].buffer;
542
                buf = cmd->argv[i].buffer;
543
                strncpy(buf, (const char *) &cmdline[start], min((end - start) + 2, cmd->argv[i].len));
543
                strncpy(buf, (const char *) &cmdline[start], min((end - start) + 2, cmd->argv[i].len));
544
            buf[min((end - start) + 1, cmd->argv[i].len - 1)] = '\0';
544
            buf[min((end - start) + 1, cmd->argv[i].len - 1)] = '\0';
545
            break;
545
            break;
546
        case ARG_TYPE_INT:
546
        case ARG_TYPE_INT:
547
            if (parse_int_arg(cmdline+start, end-start+1,
547
            if (parse_int_arg(cmdline+start, end-start+1,
548
                      &cmd->argv[i].intval))
548
                      &cmd->argv[i].intval))
549
                error = 1;
549
                error = 1;
550
            break;
550
            break;
551
        case ARG_TYPE_VAR:
551
        case ARG_TYPE_VAR:
552
            if (start != end && cmdline[start] == '"' && cmdline[end] == '"') {
552
            if (start != end && cmdline[start] == '"' && cmdline[end] == '"') {
553
                buf = cmd->argv[i].buffer;
553
                buf = cmd->argv[i].buffer;
554
                strncpy(buf, (const char *) &cmdline[start+1],
554
                strncpy(buf, (const char *) &cmdline[start+1],
555
                    min((end-start), cmd->argv[i].len));
555
                    min((end-start), cmd->argv[i].len));
556
                buf[min((end - start), cmd->argv[i].len - 1)] = '\0';
556
                buf[min((end - start), cmd->argv[i].len - 1)] = '\0';
557
                cmd->argv[i].intval = (unative_t) buf;
557
                cmd->argv[i].intval = (unative_t) buf;
558
                cmd->argv[i].vartype = ARG_TYPE_STRING;
558
                cmd->argv[i].vartype = ARG_TYPE_STRING;
559
            } else if (!parse_int_arg(cmdline+start, end-start+1,
559
            } else if (!parse_int_arg(cmdline+start, end-start+1,
560
                         &cmd->argv[i].intval))
560
                         &cmd->argv[i].intval))
561
                cmd->argv[i].vartype = ARG_TYPE_INT;
561
                cmd->argv[i].vartype = ARG_TYPE_INT;
562
            else {
562
            else {
563
                printf("Unrecognized variable argument.\n");
563
                printf("Unrecognized variable argument.\n");
564
                error = 1;
564
                error = 1;
565
            }
565
            }
566
            break;
566
            break;
567
        case ARG_TYPE_INVALID:
567
        case ARG_TYPE_INVALID:
568
        default:
568
        default:
569
            printf("invalid argument type\n");
569
            printf("invalid argument type\n");
570
            error = 1;
570
            error = 1;
571
            break;
571
            break;
572
        }
572
        }
573
    }
573
    }
574
   
574
   
575
    if (error) {
575
    if (error) {
576
        spinlock_unlock(&cmd->lock);
576
        spinlock_unlock(&cmd->lock);
577
        return NULL;
577
        return NULL;
578
    }
578
    }
579
   
579
   
580
    start = end + 1;
580
    start = end + 1;
581
    if (parse_argument(cmdline, len, &start, &end)) {
581
    if (parse_argument(cmdline, len, &start, &end)) {
582
        printf("Too many arguments.\n");
582
        printf("Too many arguments.\n");
583
        spinlock_unlock(&cmd->lock);
583
        spinlock_unlock(&cmd->lock);
584
        return NULL;
584
        return NULL;
585
    }
585
    }
586
   
586
   
587
    spinlock_unlock(&cmd->lock);
587
    spinlock_unlock(&cmd->lock);
588
    return cmd;
588
    return cmd;
589
}
589
}
590
 
590
 
591
/** Parse argument.
591
/** Parse argument.
592
 *
592
 *
593
 * Find start and end positions of command line argument.
593
 * Find start and end positions of command line argument.
594
 *
594
 *
595
 * @param cmdline Command line as read from the input device.
595
 * @param cmdline Command line as read from the input device.
596
 * @param len Number of characters in cmdline.
596
 * @param len Number of characters in cmdline.
597
 * @param start On entry, 'start' contains pointer to the index
597
 * @param start On entry, 'start' contains pointer to the index
598
 *        of first unprocessed character of cmdline.
598
 *        of first unprocessed character of cmdline.
599
 *        On successful exit, it marks beginning of the next argument.
599
 *        On successful exit, it marks beginning of the next argument.
600
 * @param end Undefined on entry. On exit, 'end' points to the last character
600
 * @param end Undefined on entry. On exit, 'end' points to the last character
601
 *        of the next argument.
601
 *        of the next argument.
602
 *
602
 *
603
 * @return false on failure, true on success.
603
 * @return false on failure, true on success.
604
 */
604
 */
605
bool parse_argument(char *cmdline, size_t len, index_t *start, index_t *end)
605
bool parse_argument(char *cmdline, size_t len, index_t *start, index_t *end)
606
{
606
{
607
    int i;
607
    int i;
608
    bool found_start = false;
608
    bool found_start = false;
609
   
609
   
610
    ASSERT(start != NULL);
610
    ASSERT(start != NULL);
611
    ASSERT(end != NULL);
611
    ASSERT(end != NULL);
612
   
612
   
613
    for (i = *start; i < len; i++) {
613
    for (i = *start; i < len; i++) {
614
        if (!found_start) {
614
        if (!found_start) {
615
            if (is_white(cmdline[i]))
615
            if (is_white(cmdline[i]))
616
                (*start)++;
616
                (*start)++;
617
            else
617
            else
618
                found_start = true;
618
                found_start = true;
619
        } else {
619
        } else {
620
            if (is_white(cmdline[i]))
620
            if (is_white(cmdline[i]))
621
                break;
621
                break;
622
        }
622
        }
623
    }
623
    }
624
    *end = i - 1;
624
    *end = i - 1;
625
 
625
 
626
    return found_start;
626
    return found_start;
627
}
627
}
628
 
628
 
629
/** @}
629
/** @}
630
 */
630
 */
631
 
631