Subversion Repositories HelenOS

Rev

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

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