Subversion Repositories HelenOS

Rev

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

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