Subversion Repositories HelenOS

Rev

Rev 3980 | Rev 4255 | 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>
4239 svoboda 37
#include <kbd/kbd.h>
38
#include <kbd/keycode.h>
3265 post 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
 
3372 post 47
static void read_line(char *, int);
48
 
3265 post 49
/* Tokenizes input from console, sees if the first word is a built-in, if so
50
 * invokes the built-in entry point (a[0]) passing all arguments in a[] to
51
 * the handler */
52
int tok_input(cliuser_t *usr)
53
{
54
    char *cmd[WORD_MAX];
55
    int n = 0, i = 0;
56
    int rc = 0;
3269 post 57
    char *tmp;
3265 post 58
 
3269 post 59
    if (NULL == usr->line)
60
        return CL_EFAIL;
3265 post 61
 
3813 post 62
    tmp = strdup(usr->line);
3275 post 63
 
3813 post 64
    cmd[n] = strtok(tmp, " ");
3265 post 65
    while (cmd[n] && n < WORD_MAX) {
3813 post 66
        cmd[++n] = strtok(NULL, " ");
3265 post 67
    }
68
 
69
    /* We have rubbish */
70
    if (NULL == cmd[0]) {
71
        rc = CL_ENOENT;
72
        goto finit;
73
    }
74
 
3809 post 75
    /* Its a builtin command ? */
3265 post 76
    if ((i = (is_builtin(cmd[0]))) > -1) {
77
        rc = run_builtin(i, cmd, usr);
78
        goto finit;
3809 post 79
    /* Its a module ? */
3265 post 80
    } else if ((i = (is_module(cmd[0]))) > -1) {
81
        rc = run_module(i, cmd);
82
        goto finit;
83
    }
84
 
3809 post 85
    /* See what try_exec thinks of it */
86
    rc = try_exec(cmd[0], cmd);
87
 
3265 post 88
finit:
89
    if (NULL != usr->line) {
90
        free(usr->line);
91
        usr->line = (char *) NULL;
92
    }
3268 post 93
    if (NULL != tmp)
94
        free(tmp);
3275 post 95
 
3265 post 96
    return rc;
97
}
98
 
3372 post 99
static void read_line(char *buffer, int n)
3265 post 100
{
4239 svoboda 101
    kbd_event_t ev;
3265 post 102
    int chars;
103
 
104
    chars = 0;
105
    while (chars < n - 1) {
4239 svoboda 106
        fflush(stdout);
107
        if (kbd_get_event(&ev) < 0)
3265 post 108
            return;
4239 svoboda 109
        if (ev.type == KE_RELEASE)
110
            continue;
111
 
112
        if (ev.key == KC_ENTER || ev.key == KC_NENTER)
3265 post 113
            break;
4239 svoboda 114
        if (ev.key == KC_BACKSPACE) {
3265 post 115
            if (chars > 0) {
116
                putchar('\b');
117
                --chars;
118
            }
119
            continue;
120
        }
4239 svoboda 121
        if (ev.c >= ' ') {
122
            //putchar(ev.c);
123
            console_putchar(ev.c);
124
            buffer[chars++] = ev.c;
3980 svoboda 125
        }
3265 post 126
    }
127
    putchar('\n');
128
    buffer[chars] = '\0';
129
}
130
 
3372 post 131
/* TODO:
132
 * Implement something like editline() / readline(), if even
133
 * just for command history and making arrows work. */
3265 post 134
void get_input(cliuser_t *usr)
135
{
136
    char line[INPUT_MAX];
137
    size_t len = 0;
138
 
3767 svoboda 139
    console_set_style(STYLE_EMPHASIS);
3265 post 140
    printf("%s", usr->prompt);
3767 svoboda 141
    console_set_style(STYLE_NORMAL);
142
 
3265 post 143
    read_line(line, INPUT_MAX);
144
    len = strlen(line);
145
    /* Make sure we don't have rubbish or a C/R happy user */
3275 post 146
    if (len == 0 || line[0] == '\n')
3265 post 147
        return;
3813 post 148
    usr->line = strdup(line);
3304 post 149
 
3265 post 150
    return;
151
}
152