Subversion Repositories HelenOS-historic

Rev

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

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