Subversion Repositories HelenOS-historic

Rev

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

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