Subversion Repositories HelenOS

Rev

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

Rev 3980 Rev 4239
1
/* Copyright (c) 2008, Tim Post <tinkertim@gmail.com>
1
/* Copyright (c) 2008, Tim Post <tinkertim@gmail.com>
2
 * All rights reserved.
2
 * All rights reserved.
3
 * Copyright (c) 2008, Jiri Svoboda - All Rights Reserved
3
 * Copyright (c) 2008, Jiri Svoboda - All Rights Reserved
4
 *
4
 *
5
 * Redistribution and use in source and binary forms, with or without
5
 * Redistribution and use in source and binary forms, with or without
6
 * modification, are permitted provided that the following conditions are met:
6
 * modification, are permitted provided that the following conditions are met:
7
 *
7
 *
8
 * Redistributions of source code must retain the above copyright notice, this
8
 * Redistributions of source code must retain the above copyright notice, this
9
 * list of conditions and the following disclaimer.
9
 * list of conditions and the following disclaimer.
10
 *
10
 *
11
 * Redistributions in binary form must reproduce the above copyright notice,
11
 * Redistributions in binary form must reproduce the above copyright notice,
12
 * this list of conditions and the following disclaimer in the documentation
12
 * this list of conditions and the following disclaimer in the documentation
13
 * and/or other materials provided with the distribution.
13
 * and/or other materials provided with the distribution.
14
 *
14
 *
15
 * Neither the name of the original program's authors nor the names of its
15
 * Neither the name of the original program's authors nor the names of its
16
 * contributors may be used to endorse or promote products derived from this
16
 * contributors may be used to endorse or promote products derived from this
17
 * software without specific prior written permission.
17
 * software without specific prior written permission.
18
 *
18
 *
19
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
22
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
23
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29
 * POSSIBILITY OF SUCH DAMAGE.
29
 * POSSIBILITY OF SUCH DAMAGE.
30
 */
30
 */
31
 
31
 
32
#include <stdio.h>
32
#include <stdio.h>
33
#include <stdlib.h>
33
#include <stdlib.h>
34
#include <string.h>
34
#include <string.h>
35
#include <io/stream.h>
35
#include <io/stream.h>
36
#include <console.h>
36
#include <console.h>
-
 
37
#include <kbd/kbd.h>
-
 
38
#include <kbd/keycode.h>
37
 
39
 
38
#include "config.h"
40
#include "config.h"
39
#include "util.h"
41
#include "util.h"
40
#include "scli.h"
42
#include "scli.h"
41
#include "input.h"
43
#include "input.h"
42
#include "errors.h"
44
#include "errors.h"
43
#include "exec.h"
45
#include "exec.h"
44
 
46
 
45
static void read_line(char *, int);
47
static void read_line(char *, int);
46
 
48
 
47
/* Tokenizes input from console, sees if the first word is a built-in, if so
49
/* Tokenizes input from console, sees if the first word is a built-in, if so
48
 * invokes the built-in entry point (a[0]) passing all arguments in a[] to
50
 * invokes the built-in entry point (a[0]) passing all arguments in a[] to
49
 * the handler */
51
 * the handler */
50
int tok_input(cliuser_t *usr)
52
int tok_input(cliuser_t *usr)
51
{
53
{
52
    char *cmd[WORD_MAX];
54
    char *cmd[WORD_MAX];
53
    int n = 0, i = 0;
55
    int n = 0, i = 0;
54
    int rc = 0;
56
    int rc = 0;
55
    char *tmp;
57
    char *tmp;
56
 
58
 
57
    if (NULL == usr->line)
59
    if (NULL == usr->line)
58
        return CL_EFAIL;
60
        return CL_EFAIL;
59
 
61
 
60
    tmp = strdup(usr->line);
62
    tmp = strdup(usr->line);
61
 
63
 
62
    cmd[n] = strtok(tmp, " ");
64
    cmd[n] = strtok(tmp, " ");
63
    while (cmd[n] && n < WORD_MAX) {
65
    while (cmd[n] && n < WORD_MAX) {
64
        cmd[++n] = strtok(NULL, " ");
66
        cmd[++n] = strtok(NULL, " ");
65
    }
67
    }
66
 
68
 
67
    /* We have rubbish */
69
    /* We have rubbish */
68
    if (NULL == cmd[0]) {
70
    if (NULL == cmd[0]) {
69
        rc = CL_ENOENT;
71
        rc = CL_ENOENT;
70
        goto finit;
72
        goto finit;
71
    }
73
    }
72
 
74
 
73
    /* Its a builtin command ? */
75
    /* Its a builtin command ? */
74
    if ((i = (is_builtin(cmd[0]))) > -1) {
76
    if ((i = (is_builtin(cmd[0]))) > -1) {
75
        rc = run_builtin(i, cmd, usr);
77
        rc = run_builtin(i, cmd, usr);
76
        goto finit;
78
        goto finit;
77
    /* Its a module ? */
79
    /* Its a module ? */
78
    } else if ((i = (is_module(cmd[0]))) > -1) {
80
    } else if ((i = (is_module(cmd[0]))) > -1) {
79
        rc = run_module(i, cmd);
81
        rc = run_module(i, cmd);
80
        goto finit;
82
        goto finit;
81
    }
83
    }
82
 
84
 
83
    /* See what try_exec thinks of it */
85
    /* See what try_exec thinks of it */
84
    rc = try_exec(cmd[0], cmd);
86
    rc = try_exec(cmd[0], cmd);
85
 
87
 
86
finit:
88
finit:
87
    if (NULL != usr->line) {
89
    if (NULL != usr->line) {
88
        free(usr->line);
90
        free(usr->line);
89
        usr->line = (char *) NULL;
91
        usr->line = (char *) NULL;
90
    }
92
    }
91
    if (NULL != tmp)
93
    if (NULL != tmp)
92
        free(tmp);
94
        free(tmp);
93
 
95
 
94
    return rc;
96
    return rc;
95
}
97
}
96
 
98
 
97
static void read_line(char *buffer, int n)
99
static void read_line(char *buffer, int n)
98
{
100
{
99
    char c;
101
    kbd_event_t ev;
100
    int chars;
102
    int chars;
101
 
103
 
102
    chars = 0;
104
    chars = 0;
103
    while (chars < n - 1) {
105
    while (chars < n - 1) {
104
        c = getchar();
106
        fflush(stdout);
105
        if (c < 0)
107
        if (kbd_get_event(&ev) < 0)
106
            return;
108
            return;
107
        if (c == '\n')
109
        if (ev.type == KE_RELEASE)
-
 
110
            continue;
-
 
111
 
-
 
112
        if (ev.key == KC_ENTER || ev.key == KC_NENTER)
108
            break;
113
            break;
109
        if (c == '\b') {
114
        if (ev.key == KC_BACKSPACE) {
110
            if (chars > 0) {
115
            if (chars > 0) {
111
                putchar('\b');
116
                putchar('\b');
112
                --chars;
117
                --chars;
113
            }
118
            }
114
            continue;
119
            continue;
115
        }
120
        }
116
        if (c >= ' ') {
121
        if (ev.c >= ' ') {
117
            putchar(c);
122
            //putchar(ev.c);
-
 
123
            console_putchar(ev.c);
118
            buffer[chars++] = c;
124
            buffer[chars++] = ev.c;
119
        }
125
        }
120
    }
126
    }
121
    putchar('\n');
127
    putchar('\n');
122
    buffer[chars] = '\0';
128
    buffer[chars] = '\0';
123
}
129
}
124
 
130
 
125
/* TODO:
131
/* TODO:
126
 * Implement something like editline() / readline(), if even
132
 * Implement something like editline() / readline(), if even
127
 * just for command history and making arrows work. */
133
 * just for command history and making arrows work. */
128
void get_input(cliuser_t *usr)
134
void get_input(cliuser_t *usr)
129
{
135
{
130
    char line[INPUT_MAX];
136
    char line[INPUT_MAX];
131
    size_t len = 0;
137
    size_t len = 0;
132
 
138
 
133
    console_set_style(STYLE_EMPHASIS);
139
    console_set_style(STYLE_EMPHASIS);
134
    printf("%s", usr->prompt);
140
    printf("%s", usr->prompt);
135
    console_set_style(STYLE_NORMAL);
141
    console_set_style(STYLE_NORMAL);
136
 
142
 
137
    read_line(line, INPUT_MAX);
143
    read_line(line, INPUT_MAX);
138
    len = strlen(line);
144
    len = strlen(line);
139
    /* Make sure we don't have rubbish or a C/R happy user */
145
    /* Make sure we don't have rubbish or a C/R happy user */
140
    if (len == 0 || line[0] == '\n')
146
    if (len == 0 || line[0] == '\n')
141
        return;
147
        return;
142
    usr->line = strdup(line);
148
    usr->line = strdup(line);
143
 
149
 
144
    return;
150
    return;
145
}
151
}
146
 
152
 
147
 
153