Subversion Repositories HelenOS

Rev

Rev 4347 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 4347 Rev 4348
Line 29... Line 29...
29
/** @addtogroup genericconsole
29
/** @addtogroup genericconsole
30
 * @{
30
 * @{
31
 */
31
 */
32
 
32
 
33
/**
33
/**
34
 * @file    kconsole.c
34
 * @file  kconsole.c
35
 * @brief   Kernel console.
35
 * @brief Kernel console.
36
 *
36
 *
37
 * This file contains kernel thread managing the kernel console.
37
 * This file contains kernel thread managing the kernel console.
-
 
38
 *
38
 */
39
 */
39
 
40
 
40
#include <console/kconsole.h>
41
#include <console/kconsole.h>
41
#include <console/console.h>
42
#include <console/console.h>
42
#include <console/chardev.h>
43
#include <console/chardev.h>
Line 54... Line 55...
54
#include <sysinfo/sysinfo.h>
55
#include <sysinfo/sysinfo.h>
55
#include <ddi/device.h>
56
#include <ddi/device.h>
56
#include <symtab.h>
57
#include <symtab.h>
57
#include <errno.h>
58
#include <errno.h>
58
#include <putchar.h>
59
#include <putchar.h>
-
 
60
#include <string.h>
59
 
61
 
60
/** Simple kernel console.
62
/** Simple kernel console.
61
 *
63
 *
62
 * The console is realized by kernel thread kconsole.
64
 * The console is realized by kernel thread kconsole.
63
 * It doesn't understand any useful command on its own,
65
 * It doesn't understand any useful command on its own,
64
 * but makes it possible for other kernel subsystems to
66
 * but makes it possible for other kernel subsystems to
65
 * register their own commands.
67
 * register their own commands.
66
 */
68
 */
67
 
69
 
68
/** Locking.
70
/** Locking.
69
 *
71
 *
70
 * There is a list of cmd_info_t structures. This list
72
 * There is a list of cmd_info_t structures. This list
71
 * is protected by cmd_lock spinlock. Note that specially
73
 * is protected by cmd_lock spinlock. Note that specially
72
 * the link elements of cmd_info_t are protected by
74
 * the link elements of cmd_info_t are protected by
Line 77... Line 79...
77
 *
79
 *
78
 * cmd_lock must be acquired before any cmd_info lock.
80
 * cmd_lock must be acquired before any cmd_info lock.
79
 * When locking two cmd info structures, structure with
81
 * When locking two cmd info structures, structure with
80
 * lower address must be locked first.
82
 * lower address must be locked first.
81
 */
83
 */
82
 
84
 
83
SPINLOCK_INITIALIZE(cmd_lock);  /**< Lock protecting command list. */
85
SPINLOCK_INITIALIZE(cmd_lock);  /**< Lock protecting command list. */
84
LIST_INITIALIZE(cmd_head);  /**< Command list. */
86
LIST_INITIALIZE(cmd_head);      /**< Command list. */
85
 
87
 
86
static cmd_info_t *parse_cmdline(char *cmdline, size_t len);
88
static wchar_t history[KCONSOLE_HISTORY][MAX_CMDLINE] = {};
87
static bool parse_argument(char *cmdline, size_t len, index_t *start,
-
 
88
    index_t *end);
-
 
89
static char history[KCONSOLE_HISTORY][MAX_CMDLINE] = {};
89
static count_t history_pos = 0;
90
 
90
 
91
/** Initialize kconsole data structures
91
/** Initialize kconsole data structures
92
 *
92
 *
93
 * This is the most basic initialization, almost no
93
 * This is the most basic initialization, almost no
94
 * other kernel subsystem is ready yet.
94
 * other kernel subsystem is ready yet.
95
 *
95
 *
96
 */
96
 */
97
void kconsole_init(void)
97
void kconsole_init(void)
98
{
98
{
99
    unsigned int i;
99
    unsigned int i;
100
 
100
   
101
    cmd_init();
101
    cmd_init();
102
    for (i = 0; i < KCONSOLE_HISTORY; i++)
102
    for (i = 0; i < KCONSOLE_HISTORY; i++)
103
        history[i][0] = '\0';
103
        history[i][0] = 0;
104
}
104
}
105
 
105
 
106
/** Register kconsole command.
106
/** Register kconsole command.
107
 *
107
 *
108
 * @param cmd Structure describing the command.
108
 * @param cmd Structure describing the command.
109
 *
109
 *
110
 * @return 0 on failure, 1 on success.
110
 * @return False on failure, true on success.
-
 
111
 *
111
 */
112
 */
112
int cmd_register(cmd_info_t *cmd)
113
bool cmd_register(cmd_info_t *cmd)
113
{
114
{
114
    link_t *cur;
115
    link_t *cur;
115
   
116
   
116
    spinlock_lock(&cmd_lock);
117
    spinlock_lock(&cmd_lock);
117
   
118
   
118
    /*
119
    /*
119
     * Make sure the command is not already listed.
120
     * Make sure the command is not already listed.
120
     */
121
     */
121
    for (cur = cmd_head.next; cur != &cmd_head; cur = cur->next) {
122
    for (cur = cmd_head.next; cur != &cmd_head; cur = cur->next) {
122
        cmd_info_t *hlp;
123
        cmd_info_t *hlp = list_get_instance(cur, cmd_info_t, link);
123
       
124
       
124
        hlp = list_get_instance(cur, cmd_info_t, link);
-
 
125
 
-
 
126
        if (hlp == cmd) {
125
        if (hlp == cmd) {
127
            /* The command is already there. */
126
            /* The command is already there. */
128
            spinlock_unlock(&cmd_lock);
127
            spinlock_unlock(&cmd_lock);
129
            return 0;
128
            return false;
130
        }
129
        }
131
 
130
       
132
        /* Avoid deadlock. */
131
        /* Avoid deadlock. */
133
        if (hlp < cmd) {
132
        if (hlp < cmd) {
134
            spinlock_lock(&hlp->lock);
133
            spinlock_lock(&hlp->lock);
135
            spinlock_lock(&cmd->lock);
134
            spinlock_lock(&cmd->lock);
136
        } else {
135
        } else {
137
            spinlock_lock(&cmd->lock);
136
            spinlock_lock(&cmd->lock);
138
            spinlock_lock(&hlp->lock);
137
            spinlock_lock(&hlp->lock);
139
        }
138
        }
140
        if ((strncmp(hlp->name, cmd->name, max(strlen(cmd->name),
-
 
-
 
139
       
141
            strlen(hlp->name))) == 0)) {
140
        if (str_cmp(hlp->name, cmd->name) == 0) {
142
            /* The command is already there. */
141
            /* The command is already there. */
143
            spinlock_unlock(&hlp->lock);
142
            spinlock_unlock(&hlp->lock);
144
            spinlock_unlock(&cmd->lock);
143
            spinlock_unlock(&cmd->lock);
145
            spinlock_unlock(&cmd_lock);
144
            spinlock_unlock(&cmd_lock);
146
            return 0;
145
            return false;
147
        }
146
        }
148
       
147
       
149
        spinlock_unlock(&hlp->lock);
148
        spinlock_unlock(&hlp->lock);
150
        spinlock_unlock(&cmd->lock);
149
        spinlock_unlock(&cmd->lock);
151
    }
150
    }
Line 154... Line 153...
154
     * Now the command can be added.
153
     * Now the command can be added.
155
     */
154
     */
156
    list_append(&cmd->link, &cmd_head);
155
    list_append(&cmd->link, &cmd_head);
157
   
156
   
158
    spinlock_unlock(&cmd_lock);
157
    spinlock_unlock(&cmd_lock);
159
    return 1;
158
    return true;
160
}
159
}
161
 
160
 
162
/** Print count times a character */
161
/** Print count times a character */
163
static void rdln_print_c(wchar_t ch, count_t count)
162
static void print_cc(wchar_t ch, count_t count)
164
{
163
{
165
    count_t i;
164
    count_t i;
166
    for (i = 0; i < count; i++)
165
    for (i = 0; i < count; i++)
167
        putchar(ch);
166
        putchar(ch);
168
}
167
}
169
 
168
 
170
/** Insert character to string */
-
 
171
static void insert_char(char *str, char ch, int pos)
-
 
172
{
-
 
173
    int i;
-
 
174
   
-
 
175
    for (i = strlen(str); i > pos; i--)
-
 
176
        str[i] = str[i - 1];
-
 
177
    str[pos] = ch;
-
 
178
}
-
 
179
 
-
 
180
/** Try to find a command beginning with prefix */
169
/** Try to find a command beginning with prefix */
181
static const char *cmdtab_search_one(const char *name,link_t **startpos)
170
static const char *cmdtab_search_one(const char *name, link_t **startpos)
182
{
171
{
183
    size_t namelen = strlen(name);
172
    count_t namelen = str_length(name);
184
    const char *curname;
-
 
185
 
173
   
186
    spinlock_lock(&cmd_lock);
174
    spinlock_lock(&cmd_lock);
187
 
175
   
188
    if (!*startpos)
176
    if (*startpos == NULL)
189
        *startpos = cmd_head.next;
177
        *startpos = cmd_head.next;
190
 
178
   
191
    for (; *startpos != &cmd_head; *startpos = (*startpos)->next) {
179
    for (; *startpos != &cmd_head; *startpos = (*startpos)->next) {
192
        cmd_info_t *hlp;
-
 
193
        hlp = list_get_instance(*startpos, cmd_info_t, link);
180
        cmd_info_t *hlp = list_get_instance(*startpos, cmd_info_t, link);
194
 
181
       
195
        curname = hlp->name;
182
        const char *curname = hlp->name;
196
        if (strlen(curname) < namelen)
183
        if (str_length(curname) < namelen)
197
            continue;
184
            continue;
-
 
185
       
198
        if (strncmp(curname, name, namelen) == 0) {
186
        if (str_lcmp(curname, name, namelen) == 0) {
199
            spinlock_unlock(&cmd_lock);
187
            spinlock_unlock(&cmd_lock);
200
            return curname+namelen;
188
            return (curname + str_lsize(curname, namelen));
201
        }
189
        }
202
    }
190
    }
-
 
191
   
203
    spinlock_unlock(&cmd_lock);
192
    spinlock_unlock(&cmd_lock);
204
    return NULL;
193
    return NULL;
205
}
194
}
206
 
195
 
-
 
196
/** Command completion of the commands
-
 
197
 *
-
 
198
 * @param name String to match, changed to hint on exit
-
 
199
 * @param size Input buffer size
207
 
200
 *
208
/** Command completion of the commands
201
 * @return Number of found matches
209
 *
202
 *
210
 * @param name - string to match, changed to hint on exit
-
 
211
 * @return number of found matches
-
 
212
 */
203
 */
213
static int cmdtab_compl(char *name)
204
static int cmdtab_compl(char *input, size_t size)
214
{
205
{
215
    static char output[/*MAX_SYMBOL_NAME*/128 + 1];
206
    const char *name = input;
-
 
207
   
-
 
208
    count_t found = 0;
216
    link_t *startpos = NULL;
209
    link_t *pos = NULL;
217
    const char *foundtxt;
210
    const char *hint;
218
    int found = 0;
211
    char output[MAX_CMDLINE];
219
    int i;
-
 
220
 
212
   
221
    output[0] = '\0';
213
    output[0] = 0;
-
 
214
   
222
    while ((foundtxt = cmdtab_search_one(name, &startpos))) {
215
    while ((hint = cmdtab_search_one(name, &pos))) {
223
        startpos = startpos->next;
-
 
224
        if (!found)
-
 
225
            strncpy(output, foundtxt, strlen(foundtxt) + 1);
-
 
226
        else {
-
 
227
            for (i = 0; output[i] && foundtxt[i] &&
216
        if ((found == 0) || (str_length(output) > str_length(hint)))
228
                output[i] == foundtxt[i]; i++)
217
            str_cpy(output, MAX_CMDLINE, hint);
229
                ;
218
       
230
            output[i] = '\0';
219
        pos = pos->next;
231
        }
-
 
232
        found++;
220
        found++;
233
    }
221
    }
234
    if (!found)
-
 
235
        return 0;
-
 
236
 
222
   
237
    if (found > 1 && !strlen(output)) {
223
    if ((found > 1) && (str_length(output) != 0)) {
238
        printf("\n");
224
        printf("\n");
239
        startpos = NULL;
225
        pos = NULL;
240
        while ((foundtxt = cmdtab_search_one(name, &startpos))) {
226
        while ((hint = cmdtab_search_one(name, &pos))) {
241
            cmd_info_t *hlp;
-
 
242
            hlp = list_get_instance(startpos, cmd_info_t, link);
227
            cmd_info_t *hlp = list_get_instance(pos, cmd_info_t, link);
243
            printf("%s - %s\n", hlp->name, hlp->description);
228
            printf("%s (%s)\n", hlp->name, hlp->description);
244
            startpos = startpos->next;
229
            pos = pos->next;
245
        }
230
        }
246
    }
231
    }
-
 
232
   
-
 
233
    if (found > 0)
247
    strncpy(name, output, 128/*MAX_SYMBOL_NAME*/);
234
        str_cpy(input, size, output);
-
 
235
   
248
    return found;
236
    return found;
249
}
237
}
250
 
238
 
251
static char *clever_readline(const char *prompt, indev_t *input)
239
static wchar_t *clever_readline(const char *prompt, indev_t *indev)
252
{
240
{
253
    static int histposition = 0;
-
 
254
 
-
 
255
    static char tmp[MAX_CMDLINE + 1];
-
 
256
    int curlen = 0, position = 0;
-
 
257
    char *current = history[histposition];
-
 
258
    int i;
-
 
259
    char mod; /* Command Modifier */
-
 
260
    char c;
-
 
261
 
-
 
262
    printf("%s> ", prompt);
241
    printf("%s> ", prompt);
-
 
242
   
-
 
243
    count_t position = 0;
-
 
244
    wchar_t *current = history[history_pos];
-
 
245
    current[0] = 0;
-
 
246
   
263
    while (1) {
247
    while (true) {
264
        c = _getc(input);
248
        wchar_t ch = indev_pop_character(indev);
-
 
249
       
265
        if (c == '\n') {
250
        if (ch == '\n') {
-
 
251
            /* Enter */
266
            putchar(c);
252
            putchar(ch);
267
            break;
253
            break;
268
        }
254
        }
-
 
255
       
269
        if (c == '\b') { /* Backspace */
256
        if (ch == '\b') {
-
 
257
            /* Backspace */
270
            if (position == 0)
258
            if (position == 0)
271
                continue;
259
                continue;
272
            for (i = position; i < curlen; i++)
260
           
273
                current[i - 1] = current[i];
261
            if (wstr_remove(current, position - 1)) {
274
            curlen--;
-
 
275
            position--;
262
                position--;
276
            putchar('\b');
263
                putchar('\b');
277
            for (i = position; i < curlen; i++)
-
 
278
                putchar(current[i]);
264
                printf("%ls ", current + position);
279
            putchar(' ');
-
 
280
            rdln_print_c('\b', curlen - position + 1);
265
                print_cc('\b', wstr_length(current) - position + 1);
281
            continue;
266
                continue;
-
 
267
            }
282
        }
268
        }
-
 
269
       
283
        if (c == '\t') { /* Tabulator */
270
        if (ch == '\t') {
284
            int found;
271
            /* Tab completion */
285
 
272
           
286
            /* Move to the end of the word */
273
            /* Move to the end of the word */
287
            for (; position < curlen && current[position] != ' ';
274
            for (; (current[position] != 0) && (!isspace(current[position]));
288
                position++)
275
                position++)
289
                putchar(current[position]);
276
                putchar(current[position]);
-
 
277
           
290
            /* Copy to tmp last word */
278
            if (position == 0)
291
            for (i = position - 1; i >= 0 && current[i] != ' '; i--)
279
                continue;
292
                ;
280
           
293
            /* If word begins with * or &, skip it */
281
            /* Find the beginning of the word
294
            if (tmp[0] == '*' || tmp[0] == '&')
282
               and copy it to tmp */
-
 
283
            count_t beg;
-
 
284
            for (beg = position - 1; (beg > 0) && (!isspace(current[beg]));
295
                for (i = 1; tmp[i]; i++)
285
                beg--);
-
 
286
           
296
                    tmp[i - 1] = tmp[i];
287
            if (isspace(current[beg]))
-
 
288
                beg++;
-
 
289
           
297
            i++; /* I is at the start of the word */
290
            char tmp[STR_BOUNDS(MAX_CMDLINE)];
298
            strncpy(tmp, current + i, position - i + 1);
291
            wstr_nstr(tmp, current + beg, position - beg + 1);
299
 
292
           
-
 
293
            int found;
-
 
294
            if (beg == 0) {
300
            if (i == 0) { /* Command completion */
295
                /* Command completion */
301
                found = cmdtab_compl(tmp);
296
                found = cmdtab_compl(tmp, STR_BOUNDS(MAX_CMDLINE));
-
 
297
            } else {
302
            } else { /* Symtab completion */
298
                /* Symbol completion */
303
                found = symtab_compl(tmp);
299
                found = symtab_compl(tmp, STR_BOUNDS(MAX_CMDLINE));
304
            }
300
            }
305
 
301
           
306
            if (found == 0)
302
            if (found == 0)
307
                continue;
303
                continue;
-
 
304
           
-
 
305
            if (found > 1) {
308
            for (i = 0; tmp[i] && curlen < MAX_CMDLINE;
306
                /* No unique hint, list was printed */
-
 
307
                printf("%s> ", prompt);
309
                i++, curlen++)
308
                printf("%ls", current);
310
                insert_char(current, tmp[i], i + position);
309
                print_cc('\b', wstr_length(current) - position);
-
 
310
                continue;
-
 
311
            }
-
 
312
           
-
 
313
            /* We have a hint */
311
 
314
           
-
 
315
            size_t off = 0;
-
 
316
            count_t i = 0;
312
            if (strlen(tmp) || found == 1) { /* If we have a hint */
317
            while ((ch = str_decode(tmp, &off, STR_NO_LIMIT)) != 0) {
313
                for (i = position; i < curlen; i++)
318
                if (!wstr_linsert(current, ch, position + i, MAX_CMDLINE))
-
 
319
                    break;
-
 
320
                i++;
-
 
321
            }
-
 
322
           
314
                    putchar(current[i]);
323
            printf("%ls", current + position);
315
                position += strlen(tmp);
324
            position += str_length(tmp);
316
                /* Add space to end */
325
            print_cc('\b', wstr_length(current) - position);
-
 
326
           
317
                if (found == 1 && position == curlen &&
327
            if (position == wstr_length(current)) {
318
                    curlen < MAX_CMDLINE) {
328
                /* Insert a space after the last completed argument */
319
                    current[position] = ' ';
329
                if (wstr_linsert(current, ' ', position, MAX_CMDLINE)) {
320
                    curlen++;
330
                    printf("%ls", current + position);
321
                    position++;
331
                    position++;
322
                    putchar(' ');
-
 
323
                }
332
                }
324
            } else { /* No hint, table was printed */
-
 
325
                printf("%s> ", prompt);
-
 
326
                for (i = 0; i < curlen; i++)
-
 
327
                    putchar(current[i]);
-
 
328
                position += strlen(tmp);
-
 
329
            }
333
            }
330
            rdln_print_c('\b', curlen - position);
-
 
331
            continue;
334
            continue;
332
        }
335
        }
333
        if (c == 0x1b) { /* Special command */
-
 
334
            mod = _getc(input);
-
 
335
            c = _getc(input);
-
 
336
 
336
       
337
            if (mod != 0x5b && mod != 0x4f)
337
        if (ch == U_LEFT_ARROW) {
338
                continue;
-
 
339
 
-
 
340
            if (c == 0x33 && _getc(input) == 0x7e) {
-
 
341
                /* Delete */
338
            /* Left */
342
                if (position == curlen)
-
 
343
                    continue;
-
 
344
                for (i = position + 1; i < curlen; i++) {
-
 
345
                    putchar(current[i]);
-
 
346
                    current[i - 1] = current[i];
-
 
347
                }
-
 
348
                putchar(' ');
-
 
349
                rdln_print_c('\b', curlen - position);
-
 
350
                curlen--;
-
 
351
            } else if (c == 0x48) { /* Home */
-
 
352
                rdln_print_c('\b', position);
-
 
353
                position = 0;
-
 
354
            } else if (c == 0x46) {  /* End */
-
 
355
                for (i = position; i < curlen; i++)
-
 
356
                    putchar(current[i]);
-
 
357
                position = curlen;
-
 
358
            } else if (c == 0x44) { /* Left */
-
 
359
                if (position > 0) {
339
            if (position > 0) {
360
                    putchar('\b');
340
                putchar('\b');
361
                    position--;
341
                position--;
362
                }
-
 
363
                continue;
-
 
364
            } else if (c == 0x43) { /* Right */
-
 
365
                if (position < curlen) {
-
 
366
                    putchar(current[position]);
-
 
367
                    position++;
-
 
368
                }
-
 
369
                continue;
-
 
370
            } else if (c == 0x41 || c == 0x42) {
-
 
371
                                /* Up, down */
-
 
372
                rdln_print_c('\b', position);
-
 
373
                rdln_print_c(' ', curlen);
-
 
374
                rdln_print_c('\b', curlen);
-
 
375
                if (c == 0x41) /* Up */
-
 
376
                    histposition--;
-
 
377
                else
-
 
378
                    histposition++;
-
 
379
                if (histposition < 0) {
-
 
380
                    histposition = KCONSOLE_HISTORY - 1;
-
 
381
                } else {
-
 
382
                    histposition =
-
 
383
                        histposition % KCONSOLE_HISTORY;
-
 
384
                }
-
 
385
                current = history[histposition];
-
 
386
                printf("%s", current);
-
 
387
                curlen = strlen(current);
-
 
388
                position = curlen;
-
 
389
                continue;
-
 
390
            }
342
            }
391
            continue;
343
            continue;
392
        }
344
        }
-
 
345
       
393
        if (curlen >= MAX_CMDLINE)
346
        if (ch == U_RIGHT_ARROW) {
-
 
347
            /* Right */
-
 
348
            if (position < wstr_length(current)) {
-
 
349
                putchar(current[position]);
-
 
350
                position++;
-
 
351
            }
394
            continue;
352
            continue;
395
 
353
        }
396
        insert_char(current, c, position);
-
 
397
 
354
       
398
        curlen++;
-
 
399
        for (i = position; i < curlen; i++)
355
        if ((ch == U_UP_ARROW) || (ch == U_DOWN_ARROW)) {
400
            putchar(current[i]);
-
 
401
        position++;
356
            /* Up, down */
402
        rdln_print_c('\b',curlen - position);
357
            print_cc('\b', position);
403
    }
-
 
404
    if (curlen) {
358
            print_cc(' ', wstr_length(current));
405
        histposition++;
-
 
406
        histposition = histposition % KCONSOLE_HISTORY;
359
            print_cc('\b', wstr_length(current));
407
    }
360
           
408
    current[curlen] = '\0';
361
            if (ch == U_UP_ARROW) {
409
    return current;
362
                /* Up */
410
}
-
 
411
 
-
 
412
bool kconsole_check_poll(void)
-
 
413
{
-
 
414
    return check_poll(stdin);
363
                if (history_pos == 0)
415
}
-
 
416
 
-
 
417
/** Kernel console prompt.
-
 
418
 *
-
 
419
 * @param prompt Kernel console prompt (e.g kconsole/panic).
-
 
420
 * @param msg    Message to display in the beginning.
-
 
421
 * @param kcon   Wait for keypress to show the prompt
364
                    history_pos = KCONSOLE_HISTORY - 1;
422
 *               and never exit.
-
 
423
 *
-
 
424
 */
365
                else
425
void kconsole(char *prompt, char *msg, bool kcon)
-
 
426
{
-
 
427
    cmd_info_t *cmd_info;
366
                    history_pos--;
428
    count_t len;
367
            } else {
429
    char *cmdline;
368
                /* Down */
430
   
-
 
431
    if (!stdin) {
369
                history_pos++;
432
        LOG("No stdin for kernel console");
370
                history_pos = history_pos % KCONSOLE_HISTORY;
433
        return;
-
 
434
    }
371
            }
435
   
-
 
436
    if (msg)
372
            current = history[history_pos];
437
        printf("%s", msg);
373
            printf("%ls", current);
438
   
-
 
439
    if (kcon)
374
            position = wstr_length(current);
440
        _getc(stdin);
375
            continue;
441
    else
376
        }
442
        printf("Type \"exit\" to leave the console.\n");
-
 
443
   
377
       
444
    while (true) {
378
        if (ch == U_HOME_ARROW) {
445
        cmdline = clever_readline((char *) prompt, stdin);
379
            /* Home */
446
        len = strlen(cmdline);
380
            print_cc('\b', position);
447
        if (!len)
381
            position = 0;
448
            continue;
382
            continue;
-
 
383
        }
449
       
384
       
-
 
385
        if (ch == U_END_ARROW) {
-
 
386
            /* End */
-
 
387
            printf("%ls", current + position);
450
        if ((!kcon) && (len == 4) && (strncmp(cmdline, "exit", 4) == 0))
388
            position = wstr_length(current);
451
            break;
389
            continue;
-
 
390
        }
452
       
391
       
-
 
392
        if (ch == U_DELETE) {
-
 
393
            /* Delete */
453
        cmd_info = parse_cmdline(cmdline, len);
394
            if (position == wstr_length(current))
454
        if (!cmd_info)
395
                continue;
-
 
396
           
-
 
397
            if (wstr_remove(current, position)) {
-
 
398
                printf("%ls ", current + position);
-
 
399
                print_cc('\b', wstr_length(current) - position + 1);
-
 
400
            }
455
            continue;
401
            continue;
-
 
402
        }
456
       
403
       
-
 
404
        if (wstr_linsert(current, ch, position, MAX_CMDLINE)) {
457
        (void) cmd_info->func(cmd_info->argv);
405
            printf("%ls", current + position);
-
 
406
            position++;
-
 
407
            print_cc('\b', wstr_length(current) - position);
-
 
408
        }
458
    }
409
    }
-
 
410
   
-
 
411
    if (wstr_length(current) > 0) {
-
 
412
        history_pos++;
-
 
413
        history_pos = history_pos % KCONSOLE_HISTORY;
-
 
414
    }
-
 
415
   
-
 
416
    return current;
459
}
417
}
460
 
418
 
461
/** Kernel console managing thread.
-
 
462
 *
-
 
463
 */
-
 
464
void kconsole_thread(void *data)
419
bool kconsole_check_poll(void)
465
{
420
{
466
    kconsole("kconsole", "Kernel console ready (press any key to activate)\n", true);
421
    return check_poll(stdin);
467
}
422
}
468
 
423
 
469
static int parse_int_arg(char *text, size_t len, unative_t *result)
424
static bool parse_int_arg(const char *text, size_t len, unative_t *result)
470
{
425
{
471
    uintptr_t symaddr;
-
 
472
    bool isaddr = false;
426
    bool isaddr = false;
473
    bool isptr = false;
427
    bool isptr = false;
474
    int rc;
-
 
475
 
-
 
476
    static char symname[MAX_SYMBOL_NAME];
-
 
477
   
428
   
478
    /* If we get a name, try to find it in symbol table */
429
    /* If we get a name, try to find it in symbol table */
479
    if (text[0] == '&') {
430
    if (text[0] == '&') {
480
        isaddr = true;
431
        isaddr = true;
481
        text++;
432
        text++;
Line 483... Line 434...
483
    } else if (text[0] == '*') {
434
    } else if (text[0] == '*') {
484
        isptr = true;
435
        isptr = true;
485
        text++;
436
        text++;
486
        len--;
437
        len--;
487
    }
438
    }
-
 
439
   
488
    if (text[0] < '0' || text[0] > '9') {
440
    if ((text[0] < '0') || (text[0] > '9')) {
-
 
441
        char symname[MAX_SYMBOL_NAME];
489
        strncpy(symname, text, min(len + 1, MAX_SYMBOL_NAME));
442
        str_ncpy(symname, MAX_SYMBOL_NAME, text, len + 1);
-
 
443
       
-
 
444
        uintptr_t symaddr;
490
        rc = symtab_addr_lookup(symname, &symaddr);
445
        int rc = symtab_addr_lookup(symname, &symaddr);
491
        switch (rc) {
446
        switch (rc) {
492
        case ENOENT:
447
        case ENOENT:
493
            printf("Symbol %s not found.\n", symname);
448
            printf("Symbol %s not found.\n", symname);
494
            return -1;
449
            return false;
495
        case EOVERFLOW:
450
        case EOVERFLOW:
496
            printf("Duplicate symbol %s.\n", symname);
451
            printf("Duplicate symbol %s.\n", symname);
497
            symtab_print_search(symname);
452
            symtab_print_search(symname);
498
            return -1;
453
            return false;
499
        default:
454
        case ENOTSUP:
500
            printf("No symbol information available.\n");
455
            printf("No symbol information available.\n");
501
            return -1;
456
            return false;
502
        }
457
        }
503
 
458
       
504
        if (isaddr)
459
        if (isaddr)
505
            *result = (unative_t)symaddr;
460
            *result = (unative_t) symaddr;
506
        else if (isptr)
461
        else if (isptr)
507
            *result = **((unative_t **)symaddr);
462
            *result = **((unative_t **) symaddr);
508
        else
463
        else
509
            *result = *((unative_t *)symaddr);
464
            *result = *((unative_t *) symaddr);
-
 
465
    } else {
510
    } else { /* It's a number - convert it */
466
        /* It's a number - convert it */
511
        *result = atoi(text);
467
        *result = atoi(text);
512
        if (isptr)
468
        if (isptr)
513
            *result = *((unative_t *)*result);
469
            *result = *((unative_t *) *result);
514
    }
470
    }
-
 
471
   
-
 
472
    return true;
-
 
473
}
515
 
474
 
-
 
475
/** Parse argument.
-
 
476
 *
-
 
477
 * Find start and end positions of command line argument.
-
 
478
 *
-
 
479
 * @param cmdline Command line as read from the input device.
-
 
480
 * @param size    Size (in bytes) of the string.
-
 
481
 * @param start   On entry, 'start' contains pointer to the offset
-
 
482
 *                of the first unprocessed character of cmdline.
-
 
483
 *                On successful exit, it marks beginning of the next argument.
-
 
484
 * @param end     Undefined on entry. On exit, 'end' is the offset of the first
-
 
485
 *                character behind the next argument.
-
 
486
 *
-
 
487
 * @return False on failure, true on success.
-
 
488
 *
-
 
489
 */
-
 
490
static bool parse_argument(const char *cmdline, size_t size, size_t *start, size_t *end)
-
 
491
{
-
 
492
    ASSERT(start != NULL);
-
 
493
    ASSERT(end != NULL);
-
 
494
   
-
 
495
    bool found_start = false;
-
 
496
    size_t offset = *start;
-
 
497
    size_t prev = *start;
-
 
498
    wchar_t ch;
-
 
499
   
-
 
500
    while ((ch = str_decode(cmdline, &offset, size)) != 0) {
-
 
501
        if (!found_start) {
-
 
502
            if (!isspace(ch)) {
-
 
503
                *start = prev;
-
 
504
                found_start = true;
-
 
505
            }
-
 
506
        } else {
-
 
507
            if (isspace(ch))
516
    return 0;
508
                break;
-
 
509
        }
-
 
510
       
-
 
511
        prev = offset;
-
 
512
    }
-
 
513
    *end = prev;
-
 
514
   
-
 
515
    return found_start;
517
}
516
}
518
 
517
 
519
/** Parse command line.
518
/** Parse command line.
520
 *
519
 *
521
 * @param cmdline Command line as read from input device.
520
 * @param cmdline Command line as read from input device.
522
 * @param len Command line length.
521
 * @param size    Size (in bytes) of the string.
523
 *
522
 *
524
 * @return Structure describing the command.
523
 * @return Structure describing the command.
-
 
524
 *
525
 */
525
 */
526
cmd_info_t *parse_cmdline(char *cmdline, size_t len)
526
static cmd_info_t *parse_cmdline(const char *cmdline, size_t size)
527
{
527
{
528
    index_t start = 0, end = 0;
528
    size_t start = 0;
529
    cmd_info_t *cmd = NULL;
-
 
530
    link_t *cur;
-
 
531
    count_t i;
-
 
532
    int error = 0;
529
    size_t end = 0;
533
   
-
 
534
    if (!parse_argument(cmdline, len, &start, &end)) {
530
    if (!parse_argument(cmdline, size, &start, &end)) {
535
        /* Command line did not contain alphanumeric word. */
531
        /* Command line did not contain alphanumeric word. */
536
        return NULL;
532
        return NULL;
537
    }
533
    }
538
 
-
 
539
    spinlock_lock(&cmd_lock);
534
    spinlock_lock(&cmd_lock);
540
   
535
   
-
 
536
    cmd_info_t *cmd = NULL;
-
 
537
    link_t *cur;
-
 
538
   
541
    for (cur = cmd_head.next; cur != &cmd_head; cur = cur->next) {
539
    for (cur = cmd_head.next; cur != &cmd_head; cur = cur->next) {
542
        cmd_info_t *hlp;
-
 
543
       
-
 
544
        hlp = list_get_instance(cur, cmd_info_t, link);
540
        cmd_info_t *hlp = list_get_instance(cur, cmd_info_t, link);
545
        spinlock_lock(&hlp->lock);
541
        spinlock_lock(&hlp->lock);
546
       
542
       
547
        if (strncmp(hlp->name, &cmdline[start], max(strlen(hlp->name),
543
        if (str_lcmp(hlp->name, cmdline + start,
548
            end - start + 1)) == 0) {
544
            max(str_length(hlp->name),
-
 
545
            str_nlength(cmdline + start, (count_t) (end - start) - 1))) == 0) {
549
            cmd = hlp;
546
            cmd = hlp;
550
            break;
547
            break;
551
        }
548
        }
552
       
549
       
553
        spinlock_unlock(&hlp->lock);
550
        spinlock_unlock(&hlp->lock);
554
    }
551
    }
555
   
552
   
556
    spinlock_unlock(&cmd_lock);
553
    spinlock_unlock(&cmd_lock);
557
   
554
   
558
    if (!cmd) {
555
    if (!cmd) {
559
        /* Unknown command. */
556
        /* Unknown command. */
560
        printf("Unknown command.\n");
557
        printf("Unknown command.\n");
561
        return NULL;
558
        return NULL;
562
    }
559
    }
563
 
560
   
564
    /* cmd == hlp is locked */
561
    /* cmd == hlp is locked */
565
   
562
   
566
    /*
563
    /*
567
     * The command line must be further analyzed and
564
     * The command line must be further analyzed and
568
     * the parameters therefrom must be matched and
565
     * the parameters therefrom must be matched and
569
     * converted to those specified in the cmd info
566
     * converted to those specified in the cmd info
570
     * structure.
567
     * structure.
571
     */
568
     */
572
 
569
   
-
 
570
    bool error = false;
-
 
571
    count_t i;
573
    for (i = 0; i < cmd->argc; i++) {
572
    for (i = 0; i < cmd->argc; i++) {
574
        char *buf;
-
 
575
        start = end + 1;
573
        start = end;
576
        if (!parse_argument(cmdline, len, &start, &end)) {
574
        if (!parse_argument(cmdline, size, &start, &end)) {
577
            printf("Too few arguments.\n");
575
            printf("Too few arguments.\n");
578
            spinlock_unlock(&cmd->lock);
576
            spinlock_unlock(&cmd->lock);
579
            return NULL;
577
            return NULL;
580
        }
578
        }
581
       
579
       
582
        error = 0;
580
        char *buf;
583
        switch (cmd->argv[i].type) {
581
        switch (cmd->argv[i].type) {
584
        case ARG_TYPE_STRING:
582
        case ARG_TYPE_STRING:
585
            buf = (char *) cmd->argv[i].buffer;
583
            buf = (char *) cmd->argv[i].buffer;
586
            strncpy(buf, (const char *) &cmdline[start],
584
            str_ncpy(buf, cmd->argv[i].len, cmdline + start,
587
                min((end - start) + 2, cmd->argv[i].len));
-
 
588
            buf[min((end - start) + 1, cmd->argv[i].len - 1)] =
-
 
589
                '\0';
585
                end - start);
590
            break;
586
            break;
591
        case ARG_TYPE_INT:
587
        case ARG_TYPE_INT:
592
            if (parse_int_arg(cmdline + start, end - start + 1,
588
            if (!parse_int_arg(cmdline + start, end - start,
593
                &cmd->argv[i].intval))
589
                &cmd->argv[i].intval))
594
                error = 1;
590
                error = true;
595
            break;
591
            break;
596
        case ARG_TYPE_VAR:
592
        case ARG_TYPE_VAR:
597
            if (start != end && cmdline[start] == '"' &&
593
            if ((start < end - 1) && (cmdline[start] == '"')) {
598
                cmdline[end] == '"') {
594
                if (cmdline[end - 1] == '"') {
599
                buf = (char *) cmd->argv[i].buffer;
595
                    buf = (char *) cmd->argv[i].buffer;
600
                strncpy(buf, (const char *) &cmdline[start + 1],
596
                    str_ncpy(buf, cmd->argv[i].len,
601
                    min((end-start), cmd->argv[i].len));
597
                        cmdline + start + 1,
602
                buf[min((end - start), cmd->argv[i].len - 1)] =
598
                        (end - start) - 1);
603
                    '\0';
-
 
604
                cmd->argv[i].intval = (unative_t) buf;
599
                    cmd->argv[i].intval = (unative_t) buf;
605
                cmd->argv[i].vartype = ARG_TYPE_STRING;
600
                    cmd->argv[i].vartype = ARG_TYPE_STRING;
-
 
601
                } else {
-
 
602
                    printf("Wrong synxtax.\n");
-
 
603
                    error = true;
-
 
604
                }
606
            } else if (!parse_int_arg(cmdline + start,
605
            } else if (parse_int_arg(cmdline + start,
607
                end - start + 1, &cmd->argv[i].intval)) {
606
                end - start, &cmd->argv[i].intval)) {
608
                cmd->argv[i].vartype = ARG_TYPE_INT;
607
                cmd->argv[i].vartype = ARG_TYPE_INT;
609
            } else {
608
            } else {
610
                printf("Unrecognized variable argument.\n");
609
                printf("Unrecognized variable argument.\n");
611
                error = 1;
610
                error = true;
612
            }
611
            }
613
            break;
612
            break;
614
        case ARG_TYPE_INVALID:
613
        case ARG_TYPE_INVALID:
615
        default:
614
        default:
616
            printf("invalid argument type\n");
615
            printf("Invalid argument type\n");
617
            error = 1;
616
            error = true;
618
            break;
617
            break;
619
        }
618
        }
620
    }
619
    }
621
   
620
   
622
    if (error) {
621
    if (error) {
623
        spinlock_unlock(&cmd->lock);
622
        spinlock_unlock(&cmd->lock);
624
        return NULL;
623
        return NULL;
625
    }
624
    }
626
   
625
   
627
    start = end + 1;
626
    start = end;
628
    if (parse_argument(cmdline, len, &start, &end)) {
627
    if (parse_argument(cmdline, size, &start, &end)) {
629
        printf("Too many arguments.\n");
628
        printf("Too many arguments.\n");
630
        spinlock_unlock(&cmd->lock);
629
        spinlock_unlock(&cmd->lock);
631
        return NULL;
630
        return NULL;
632
    }
631
    }
633
   
632
   
634
    spinlock_unlock(&cmd->lock);
633
    spinlock_unlock(&cmd->lock);
635
    return cmd;
634
    return cmd;
636
}
635
}
637
 
636
 
638
/** Parse argument.
637
/** Kernel console prompt.
639
 *
-
 
640
 * Find start and end positions of command line argument.
-
 
641
 *
638
 *
642
 * @param cmdline Command line as read from the input device.
639
 * @param prompt Kernel console prompt (e.g kconsole/panic).
643
 * @param len Number of characters in cmdline.
640
 * @param msg    Message to display in the beginning.
644
 * @param start On entry, 'start' contains pointer to the index
641
 * @param kcon   Wait for keypress to show the prompt
645
 *        of first unprocessed character of cmdline.
-
 
646
 *        On successful exit, it marks beginning of the next argument.
-
 
647
 * @param end Undefined on entry. On exit, 'end' points to the last character
-
 
648
 *        of the next argument.
642
 *               and never exit.
649
 *
643
 *
650
 * @return false on failure, true on success.
-
 
651
 */
644
 */
652
bool parse_argument(char *cmdline, size_t len, index_t *start, index_t *end)
645
void kconsole(char *prompt, char *msg, bool kcon)
653
{
646
{
654
    index_t i;
647
    if (!stdin) {
655
    bool found_start = false;
648
        LOG("No stdin for kernel console");
-
 
649
        return;
-
 
650
    }
656
   
651
   
657
    ASSERT(start != NULL);
652
    if (msg)
658
    ASSERT(end != NULL);
653
        printf("%s", msg);
659
   
654
   
-
 
655
    if (kcon)
660
    for (i = *start; i < len; i++) {
656
        indev_pop_character(stdin);
-
 
657
    else
-
 
658
        printf("Type \"exit\" to leave the console.\n");
-
 
659
   
661
        if (!found_start) {
660
    while (true) {
-
 
661
        wchar_t *tmp = clever_readline((char *) prompt, stdin);
-
 
662
        count_t len = wstr_length(tmp);
662
            if (isspace(cmdline[i]))
663
        if (!len)
663
                (*start)++;
664
            continue;
664
            else
665
       
665
                found_start = true;
666
        char cmdline[STR_BOUNDS(MAX_CMDLINE)];
-
 
667
        wstr_nstr(cmdline, tmp, STR_BOUNDS(MAX_CMDLINE));
-
 
668
       
-
 
669
        if ((!kcon) && (len == 4) && (str_lcmp(cmdline, "exit", 4) == 0))
666
        } else {
670
            break;
-
 
671
       
-
 
672
        cmd_info_t *cmd_info = parse_cmdline(cmdline, STR_BOUNDS(MAX_CMDLINE));
667
            if (isspace(cmdline[i]))
673
        if (!cmd_info)
668
                break;
674
            continue;
669
        }
675
       
-
 
676
        (void) cmd_info->func(cmd_info->argv);
670
    }
677
    }
671
    *end = i - 1;
678
}
672
 
679
 
673
    return found_start;
680
/** Kernel console managing thread.
-
 
681
 *
-
 
682
 */
-
 
683
void kconsole_thread(void *data)
-
 
684
{
-
 
685
    kconsole("kconsole", "Kernel console ready (press any key to activate)\n", true);
674
}
686
}
675
 
687
 
676
/** @}
688
/** @}
677
 */
689
 */