Subversion Repositories HelenOS-historic

Rev

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

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