Subversion Repositories HelenOS-historic

Rev

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

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