Subversion Repositories HelenOS

Rev

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

Rev 3386 Rev 4153
Line 31... Line 31...
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
 
37
 
37
#include "config.h"
38
#include "config.h"
38
#include "util.h"
39
#include "util.h"
39
#include "scli.h"
40
#include "scli.h"
40
#include "input.h"
41
#include "input.h"
41
#include "errors.h"
42
#include "errors.h"
42
#include "exec.h"
43
#include "exec.h"
43
 
44
 
44
extern volatile unsigned int cli_interactive;
-
 
45
 
-
 
46
/* Not exposed in input.h */
-
 
47
static void cli_restricted(char *);
-
 
48
static void read_line(char *, int);
45
static void read_line(char *, int);
49
 
46
 
50
/* More than a macro than anything */
-
 
51
static void cli_restricted(char *cmd)
-
 
52
{
-
 
53
    printf("%s is not available in %s mode\n", cmd,
-
 
54
        cli_interactive ? "interactive" : "non-interactive");
-
 
55
 
-
 
56
    return;
-
 
57
}
-
 
58
 
-
 
59
/* Tokenizes input from console, sees if the first word is a built-in, if so
47
/* Tokenizes input from console, sees if the first word is a built-in, if so
60
 * invokes the built-in entry point (a[0]) passing all arguments in a[] to
48
 * invokes the built-in entry point (a[0]) passing all arguments in a[] to
61
 * the handler */
49
 * the handler */
62
int tok_input(cliuser_t *usr)
50
int tok_input(cliuser_t *usr)
63
{
51
{
Line 67... Line 55...
67
    char *tmp;
55
    char *tmp;
68
 
56
 
69
    if (NULL == usr->line)
57
    if (NULL == usr->line)
70
        return CL_EFAIL;
58
        return CL_EFAIL;
71
 
59
 
72
    tmp = cli_strdup(usr->line);
60
    tmp = strdup(usr->line);
73
 
-
 
74
    /* Break up what the user typed, space delimited */
-
 
75
 
61
 
76
    /* TODO: Protect things in quotes / ticks, expand wildcards */
-
 
77
    cmd[n] = cli_strtok(tmp, " ");
62
    cmd[n] = strtok(tmp, " ");
78
    while (cmd[n] && n < WORD_MAX) {
63
    while (cmd[n] && n < WORD_MAX) {
79
        cmd[++n] = cli_strtok(NULL, " ");
64
        cmd[++n] = strtok(NULL, " ");
80
    }
65
    }
81
 
66
 
82
    /* We have rubbish */
67
    /* We have rubbish */
83
    if (NULL == cmd[0]) {
68
    if (NULL == cmd[0]) {
84
        rc = CL_ENOENT;
69
        rc = CL_ENOENT;
85
        goto finit;
70
        goto finit;
86
    }
71
    }
87
 
72
 
88
    /* Its a builtin command */
73
    /* Its a builtin command ? */
89
    if ((i = (is_builtin(cmd[0]))) > -1) {
74
    if ((i = (is_builtin(cmd[0]))) > -1) {
90
        /* Its not available in this mode, see what try_exec() thinks */
-
 
91
        if (builtin_is_restricted(i)) {
-
 
92
                rc = try_exec(cmd[0], cmd);
-
 
93
                if (rc)
-
 
94
                    /* No external matching it could be found, tell the
-
 
95
                     * user that the command does exist, but is not
-
 
96
                     * available in this mode. */
-
 
97
                    cli_restricted(cmd[0]);
-
 
98
                goto finit;
-
 
99
        }
-
 
100
        /* Its a builtin, its available, run it */
-
 
101
        rc = run_builtin(i, cmd, usr);
75
        rc = run_builtin(i, cmd, usr);
102
        goto finit;
76
        goto finit;
103
    /* We repeat the same dance for modules */
77
    /* Its a module ? */
104
    } else if ((i = (is_module(cmd[0]))) > -1) {
78
    } else if ((i = (is_module(cmd[0]))) > -1) {
105
        if (module_is_restricted(i)) {
-
 
106
            rc = try_exec(cmd[0], cmd);
-
 
107
            if (rc)
-
 
108
                cli_restricted(cmd[0]);
-
 
109
            goto finit;
-
 
110
        }
-
 
111
        rc = run_module(i, cmd);
79
        rc = run_module(i, cmd);
112
        goto finit;
80
        goto finit;
113
    } else {
-
 
114
        /* Its not a module or builtin, restricted or otherwise.
-
 
115
         * See what try_exec() thinks of it and just pass its return
-
 
116
         * value back to the caller */
-
 
117
        rc = try_exec(cmd[0], cmd);
-
 
118
        goto finit;
-
 
119
    }
81
    }
120
 
82
 
-
 
83
    /* See what try_exec thinks of it */
-
 
84
    rc = try_exec(cmd[0], cmd);
-
 
85
 
121
finit:
86
finit:
122
    if (NULL != usr->line) {
87
    if (NULL != usr->line) {
123
        free(usr->line);
88
        free(usr->line);
124
        usr->line = (char *) NULL;
89
        usr->line = (char *) NULL;
125
    }
90
    }
Line 127... Line 92...
127
        free(tmp);
92
        free(tmp);
128
 
93
 
129
    return rc;
94
    return rc;
130
}
95
}
131
 
96
 
132
/* Borrowed from Jiri Svoboda's 'cli' uspace app */
-
 
133
static void read_line(char *buffer, int n)
97
static void read_line(char *buffer, int n)
134
{
98
{
135
    char c;
99
    char c;
136
    int chars;
100
    int chars;
137
 
101
 
Line 147... Line 111...
147
                putchar('\b');
111
                putchar('\b');
148
                --chars;
112
                --chars;
149
            }
113
            }
150
            continue;
114
            continue;
151
        }
115
        }
-
 
116
        if (c >= ' ') {
152
        putchar(c);
117
            putchar(c);
153
        buffer[chars++] = c;
118
            buffer[chars++] = c;
-
 
119
        }
154
    }
120
    }
155
    putchar('\n');
121
    putchar('\n');
156
    buffer[chars] = '\0';
122
    buffer[chars] = '\0';
157
}
123
}
158
 
124
 
Line 162... Line 128...
162
void get_input(cliuser_t *usr)
128
void get_input(cliuser_t *usr)
163
{
129
{
164
    char line[INPUT_MAX];
130
    char line[INPUT_MAX];
165
    size_t len = 0;
131
    size_t len = 0;
166
 
132
 
-
 
133
    console_set_style(STYLE_EMPHASIS);
167
    printf("%s", usr->prompt);
134
    printf("%s", usr->prompt);
-
 
135
    console_set_style(STYLE_NORMAL);
-
 
136
 
168
    read_line(line, INPUT_MAX);
137
    read_line(line, INPUT_MAX);
169
    len = strlen(line);
138
    len = strlen(line);
170
    /* Make sure we don't have rubbish or a C/R happy user */
139
    /* Make sure we don't have rubbish or a C/R happy user */
171
    if (len == 0 || line[0] == '\n')
140
    if (len == 0 || line[0] == '\n')
172
        return;
141
        return;
173
    usr->line = cli_strdup(line);
142
    usr->line = strdup(line);
174
 
143
 
175
    return;
144
    return;
176
}
145
}
177
 
146