Subversion Repositories HelenOS-historic

Rev

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

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