Subversion Repositories HelenOS

Rev

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