Subversion Repositories HelenOS

Rev

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

Rev 3275 Rev 3304
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
 
-
 
36
#ifdef HELENOS
-
 
37
#include <io/stream.h>
35
#include <io/stream.h>
38
#endif
-
 
39
 
36
 
40
#include "config.h"
37
#include "config.h"
41
#include "util.h"
38
#include "util.h"
42
#include "scli.h"
39
#include "scli.h"
43
#include "input.h"
40
#include "input.h"
44
#include "errors.h"
41
#include "errors.h"
45
#include "exec.h"
42
#include "exec.h"
46
 
43
 
47
extern volatile unsigned int cli_interactive;
44
extern volatile unsigned int cli_interactive;
48
 
45
 
49
/* More than a macro than anything */
46
/* More than a macro than anything */
50
void cli_restricted(char *cmd)
47
void cli_restricted(char *cmd)
51
{
48
{
52
    cli_verbose("%s is not available in %s mode\n", cmd,
49
    cli_verbose("%s is not available in %s mode\n", cmd,
53
        cli_interactive ? "interactive" : "non-interactive");
50
        cli_interactive ? "interactive" : "non-interactive");
54
 
51
 
55
    return;
52
    return;
56
}
53
}
57
 
54
 
58
/* Tokenizes input from console, sees if the first word is a built-in, if so
55
/* Tokenizes input from console, sees if the first word is a built-in, if so
59
 * invokes the built-in entry point (a[0]) passing all arguments in a[] to
56
 * invokes the built-in entry point (a[0]) passing all arguments in a[] to
60
 * the handler */
57
 * the handler */
61
int tok_input(cliuser_t *usr)
58
int tok_input(cliuser_t *usr)
62
{
59
{
63
    char *cmd[WORD_MAX];
60
    char *cmd[WORD_MAX];
64
    int n = 0, i = 0;
61
    int n = 0, i = 0;
65
    int rc = 0;
62
    int rc = 0;
66
    char *tmp;
63
    char *tmp;
67
 
64
 
68
    if (NULL == usr->line)
65
    if (NULL == usr->line)
69
        return CL_EFAIL;
66
        return CL_EFAIL;
70
 
67
 
71
    tmp = cli_strdup(usr->line);
68
    tmp = cli_strdup(usr->line);
72
 
69
 
73
    /* Break up what the user typed, space delimited */
70
    /* Break up what the user typed, space delimited */
74
    cmd[n] = cli_strtok(tmp, " ");
71
    cmd[n] = cli_strtok(tmp, " ");
75
    while (cmd[n] && n < WORD_MAX) {
72
    while (cmd[n] && n < WORD_MAX) {
76
        cmd[++n] = cli_strtok(NULL, " ");
73
        cmd[++n] = cli_strtok(NULL, " ");
77
    }
74
    }
78
 
75
 
79
    /* We have rubbish */
76
    /* We have rubbish */
80
    if (NULL == cmd[0]) {
77
    if (NULL == cmd[0]) {
81
        rc = CL_ENOENT;
78
        rc = CL_ENOENT;
82
        goto finit;
79
        goto finit;
83
    }
80
    }
84
 
81
 
85
    /* Check what kind of command argv[0] might be */
82
    /* Check what kind of command argv[0] might be */
86
    if ((i = (is_builtin(cmd[0]))) > -1) {
83
    if ((i = (is_builtin(cmd[0]))) > -1) {
87
        /* Its a builtin */
84
        /* Its a builtin */
88
        if (builtin_is_restricted(i)) {
85
        if (builtin_is_restricted(i)) {
89
            /* Try an external command matching argv[0] */
86
            /* Try an external command matching argv[0] */
90
                rc = try_exec(cmd[0], cmd);
87
                rc = try_exec(cmd[0], cmd);
91
                if (rc)
88
                if (rc)
92
                    cli_restricted(cmd[0]);
89
                    cli_restricted(cmd[0]);
93
                goto finit;
90
                goto finit;
94
        }
91
        }
95
        rc = run_builtin(i, cmd, usr);
92
        rc = run_builtin(i, cmd, usr);
96
        goto finit;
93
        goto finit;
97
    } else if ((i = (is_module(cmd[0]))) > -1) {
94
    } else if ((i = (is_module(cmd[0]))) > -1) {
98
        /* Its a module, it can't modify cliuser_t */
95
        /* Its a module, it can't modify cliuser_t */
99
        if (module_is_restricted(i)) {
96
        if (module_is_restricted(i)) {
100
            rc = try_exec(cmd[0], cmd);
97
            rc = try_exec(cmd[0], cmd);
101
            if (rc)
98
            if (rc)
102
                cli_restricted(cmd[0]);
99
                cli_restricted(cmd[0]);
103
            goto finit;
100
            goto finit;
104
        }
101
        }
105
        rc = run_module(i, cmd);
102
        rc = run_module(i, cmd);
106
        goto finit;
103
        goto finit;
107
    } else {
104
    } else {
108
        /* See what try_exec() thinks of it */
105
        /* See what try_exec() thinks of it */
109
        rc = try_exec(cmd[0], cmd);
106
        rc = try_exec(cmd[0], cmd);
110
        goto finit;
107
        goto finit;
111
    }
108
    }
112
 
109
 
113
finit:
110
finit:
114
    if (NULL != usr->line) {
111
    if (NULL != usr->line) {
115
        free(usr->line);
112
        free(usr->line);
116
        usr->line = (char *) NULL;
113
        usr->line = (char *) NULL;
117
    }
114
    }
118
    if (NULL != tmp)
115
    if (NULL != tmp)
119
        free(tmp);
116
        free(tmp);
120
 
117
 
121
    return rc;
118
    return rc;
122
}
119
}
123
 
120
 
124
#ifdef HELENOS
-
 
125
/* Borrowed from Jiri Svoboda's 'cli' uspace app */
121
/* Borrowed from Jiri Svoboda's 'cli' uspace app */
126
void read_line(char *buffer, int n)
122
void read_line(char *buffer, int n)
127
{
123
{
128
    char c;
124
    char c;
129
    int chars;
125
    int chars;
130
 
126
 
131
    chars = 0;
127
    chars = 0;
132
    while (chars < n - 1) {
128
    while (chars < n - 1) {
133
        c = getchar();
129
        c = getchar();
134
        if (c < 0)
130
        if (c < 0)
135
            return;
131
            return;
136
        if (c == '\n')
132
        if (c == '\n')
137
            break;
133
            break;
138
        if (c == '\b') {
134
        if (c == '\b') {
139
            if (chars > 0) {
135
            if (chars > 0) {
140
                putchar('\b');
136
                putchar('\b');
141
                --chars;
137
                --chars;
142
            }
138
            }
143
            continue;
139
            continue;
144
        }
140
        }
145
        putchar(c);
141
        putchar(c);
146
        buffer[chars++] = c;
142
        buffer[chars++] = c;
147
    }
143
    }
148
    putchar('\n');
144
    putchar('\n');
149
    buffer[chars] = '\0';
145
    buffer[chars] = '\0';
150
}
146
}
151
#endif
-
 
152
 
147
 
153
/* This is soupy, fix me */
-
 
154
void get_input(cliuser_t *usr)
148
void get_input(cliuser_t *usr)
155
{
149
{
156
    char line[INPUT_MAX];
150
    char line[INPUT_MAX];
157
    size_t len = 0;
151
    size_t len = 0;
158
 
152
 
159
    printf("%s", usr->prompt);
153
    printf("%s", usr->prompt);
160
#ifdef HELENOS
-
 
161
    read_line(line, INPUT_MAX);
154
    read_line(line, INPUT_MAX);
162
#else
-
 
163
    fgets(line, INPUT_MAX, stdin);
-
 
164
#endif
-
 
165
    len = strlen(line);
155
    len = strlen(line);
166
    /* Make sure we don't have rubbish or a C/R happy user */
156
    /* Make sure we don't have rubbish or a C/R happy user */
167
    if (len == 0 || line[0] == '\n')
157
    if (len == 0 || line[0] == '\n')
168
        return;
158
        return;
169
    if (len == 1 && line[len-1] == '\n')
159
    if (len == 1 && line[len-1] == '\n')
170
        return;
160
        return;
171
#ifndef HELENOS
-
 
172
    /* Null terminate line */
-
 
173
    if (len > 0 && line[len-1] == '\n')
-
 
174
        line[len-1] = '\0';
-
 
175
#endif
-
 
176
    usr->line = cli_strdup(line);
161
    usr->line = cli_strdup(line);
-
 
162
 
177
    return;
163
    return;
178
}
164
}
179
 
165
 
180
 
166