Subversion Repositories HelenOS

Rev

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

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