Subversion Repositories HelenOS

Rev

Rev 3372 | Rev 3812 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
3265 post 1
/* Copyright (c) 2008, Tim Post <tinkertim@gmail.com>
2
 * All rights reserved.
3
 * Copyright (c) 2008, Jiri Svoboda - All Rights Reserved
4
 *
5
 * Redistribution and use in source and binary forms, with or without
6
 * modification, are permitted provided that the following conditions are met:
7
 *
8
 * Redistributions of source code must retain the above copyright notice, this
9
 * list of conditions and the following disclaimer.
10
 *
11
 * Redistributions in binary form must reproduce the above copyright notice,
12
 * this list of conditions and the following disclaimer in the documentation
13
 * and/or other materials provided with the distribution.
14
 *
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
17
 * software without specific prior written permission.
18
 *
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
21
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
23
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
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
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
29
 * POSSIBILITY OF SUCH DAMAGE.
30
 */
31
 
32
#include <stdio.h>
33
#include <stdlib.h>
34
#include <string.h>
35
#include <io/stream.h>
3767 svoboda 36
#include <console.h>
3265 post 37
 
38
#include "config.h"
39
#include "util.h"
40
#include "scli.h"
41
#include "input.h"
42
#include "errors.h"
43
#include "exec.h"
44
 
45
extern volatile unsigned int cli_interactive;
46
 
3372 post 47
/* Not exposed in input.h */
48
static void cli_restricted(char *);
49
static void read_line(char *, int);
50
 
3265 post 51
/* More than a macro than anything */
3372 post 52
static void cli_restricted(char *cmd)
3265 post 53
{
3366 post 54
    printf("%s is not available in %s mode\n", cmd,
3265 post 55
        cli_interactive ? "interactive" : "non-interactive");
56
 
57
    return;
58
}
59
 
60
/* Tokenizes input from console, sees if the first word is a built-in, if so
61
 * invokes the built-in entry point (a[0]) passing all arguments in a[] to
62
 * the handler */
63
int tok_input(cliuser_t *usr)
64
{
65
    char *cmd[WORD_MAX];
66
    int n = 0, i = 0;
67
    int rc = 0;
3269 post 68
    char *tmp;
3265 post 69
 
3269 post 70
    if (NULL == usr->line)
71
        return CL_EFAIL;
3265 post 72
 
3268 post 73
    tmp = cli_strdup(usr->line);
3275 post 74
 
3265 post 75
    /* Break up what the user typed, space delimited */
3339 post 76
 
77
    /* TODO: Protect things in quotes / ticks, expand wildcards */
3265 post 78
    cmd[n] = cli_strtok(tmp, " ");
79
    while (cmd[n] && n < WORD_MAX) {
80
        cmd[++n] = cli_strtok(NULL, " ");
81
    }
82
 
83
    /* We have rubbish */
84
    if (NULL == cmd[0]) {
85
        rc = CL_ENOENT;
86
        goto finit;
87
    }
88
 
3372 post 89
    /* Its a builtin command */
3265 post 90
    if ((i = (is_builtin(cmd[0]))) > -1) {
3372 post 91
        /* Its not available in this mode, see what try_exec() thinks */
3265 post 92
        if (builtin_is_restricted(i)) {
93
                rc = try_exec(cmd[0], cmd);
94
                if (rc)
3372 post 95
                    /* No external matching it could be found, tell the
96
                     * user that the command does exist, but is not
97
                     * available in this mode. */
3265 post 98
                    cli_restricted(cmd[0]);
99
                goto finit;
100
        }
3372 post 101
        /* Its a builtin, its available, run it */
3265 post 102
        rc = run_builtin(i, cmd, usr);
103
        goto finit;
3372 post 104
    /* We repeat the same dance for modules */
3265 post 105
    } else if ((i = (is_module(cmd[0]))) > -1) {
106
        if (module_is_restricted(i)) {
107
            rc = try_exec(cmd[0], cmd);
108
            if (rc)
109
                cli_restricted(cmd[0]);
110
            goto finit;
111
        }
112
        rc = run_module(i, cmd);
113
        goto finit;
114
    } else {
3372 post 115
        /* Its not a module or builtin, restricted or otherwise.
116
         * See what try_exec() thinks of it and just pass its return
117
         * value back to the caller */
3265 post 118
        rc = try_exec(cmd[0], cmd);
119
        goto finit;
120
    }
121
 
122
finit:
123
    if (NULL != usr->line) {
124
        free(usr->line);
125
        usr->line = (char *) NULL;
126
    }
3268 post 127
    if (NULL != tmp)
128
        free(tmp);
3275 post 129
 
3265 post 130
    return rc;
131
}
132
 
133
/* Borrowed from Jiri Svoboda's 'cli' uspace app */
3372 post 134
static void read_line(char *buffer, int n)
3265 post 135
{
136
    char c;
137
    int chars;
138
 
139
    chars = 0;
140
    while (chars < n - 1) {
141
        c = getchar();
142
        if (c < 0)
143
            return;
144
        if (c == '\n')
145
            break;
146
        if (c == '\b') {
147
            if (chars > 0) {
148
                putchar('\b');
149
                --chars;
150
            }
151
            continue;
152
        }
153
        putchar(c);
154
        buffer[chars++] = c;
155
    }
156
    putchar('\n');
157
    buffer[chars] = '\0';
158
}
159
 
3372 post 160
/* TODO:
161
 * Implement something like editline() / readline(), if even
162
 * just for command history and making arrows work. */
3265 post 163
void get_input(cliuser_t *usr)
164
{
165
    char line[INPUT_MAX];
166
    size_t len = 0;
167
 
3767 svoboda 168
    console_set_style(STYLE_EMPHASIS);
3265 post 169
    printf("%s", usr->prompt);
3767 svoboda 170
    console_set_style(STYLE_NORMAL);
171
 
3265 post 172
    read_line(line, INPUT_MAX);
173
    len = strlen(line);
174
    /* Make sure we don't have rubbish or a C/R happy user */
3275 post 175
    if (len == 0 || line[0] == '\n')
3265 post 176
        return;
177
    usr->line = cli_strdup(line);
3304 post 178
 
3265 post 179
    return;
180
}
181