Subversion Repositories HelenOS

Rev

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