Subversion Repositories HelenOS-historic

Rev

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

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