Subversion Repositories HelenOS-historic

Rev

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

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