Subversion Repositories HelenOS

Rev

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

Rev 4055 Rev 4296
Line 32... Line 32...
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
#include <console.h>
-
 
37
#include <kbd/kbd.h>
-
 
38
#include <kbd/keycode.h>
-
 
39
#include <errno.h>
-
 
40
#include <bool.h>
37
 
41
 
38
#include "config.h"
42
#include "config.h"
39
#include "util.h"
43
#include "util.h"
40
#include "scli.h"
44
#include "scli.h"
41
#include "input.h"
45
#include "input.h"
Line 55... Line 59...
55
    char *tmp;
59
    char *tmp;
56
 
60
 
57
    if (NULL == usr->line)
61
    if (NULL == usr->line)
58
        return CL_EFAIL;
62
        return CL_EFAIL;
59
 
63
 
60
    tmp = strdup(usr->line);
64
    tmp = str_dup(usr->line);
61
 
65
 
62
    cmd[n] = strtok(tmp, " ");
66
    cmd[n] = strtok(tmp, " ");
63
    while (cmd[n] && n < WORD_MAX) {
67
    while (cmd[n] && n < WORD_MAX) {
64
        cmd[++n] = strtok(NULL, " ");
68
        cmd[++n] = strtok(NULL, " ");
65
    }
69
    }
Line 94... Line 98...
94
    return rc;
98
    return rc;
95
}
99
}
96
 
100
 
97
static void read_line(char *buffer, int n)
101
static void read_line(char *buffer, int n)
98
{
102
{
99
    char c;
103
    kbd_event_t ev;
-
 
104
    size_t offs, otmp;
100
    int chars;
105
    wchar_t dec;
101
 
106
 
102
    chars = 0;
107
    offs = 0;
103
    while (chars < n - 1) {
108
    while (true) {
104
        c = getchar();
109
        fflush(stdout);
105
        if (c < 0)
110
        if (kbd_get_event(&ev) < 0)
106
            return;
111
            return;
107
        if (c == '\n')
112
        if (ev.type == KE_RELEASE)
-
 
113
            continue;
-
 
114
 
-
 
115
        if (ev.key == KC_ENTER || ev.key == KC_NENTER)
108
            break;
116
            break;
-
 
117
        if (ev.key == KC_BACKSPACE) {
109
        if (c == '\b') {
118
            if (offs > 0) {
-
 
119
                /*
-
 
120
                 * Back up until we reach valid start of
-
 
121
                 * character.
-
 
122
                 */
110
            if (chars > 0) {
123
                while (offs > 0) {
-
 
124
                    --offs; otmp = offs;
-
 
125
                    dec = str_decode(buffer, &otmp, n);
-
 
126
                    if (dec != U_SPECIAL)
-
 
127
                        break;
-
 
128
                }
111
                putchar('\b');
129
                putchar('\b');
112
                --chars;
-
 
113
            }
130
            }
114
            continue;
131
            continue;
115
        }
132
        }
116
        if (c >= ' ') {
133
        if (ev.c >= ' ') {
117
            putchar(c);
134
            //putchar(ev.c);
-
 
135
            if (chr_encode(ev.c, buffer, &offs, n - 1) == EOK)
118
            buffer[chars++] = c;
136
                console_putchar(ev.c);
119
        }
137
        }
120
    }
138
    }
121
    putchar('\n');
139
    putchar('\n');
122
    buffer[chars] = '\0';
140
    buffer[offs] = '\0';
123
}
141
}
124
 
142
 
125
/* TODO:
143
/* TODO:
126
 * Implement something like editline() / readline(), if even
144
 * Implement something like editline() / readline(), if even
127
 * just for command history and making arrows work. */
145
 * just for command history and making arrows work. */
128
void get_input(cliuser_t *usr)
146
void get_input(cliuser_t *usr)
129
{
147
{
130
    char line[INPUT_MAX];
148
    char line[INPUT_MAX];
131
    size_t len = 0;
-
 
132
 
149
 
133
    console_set_style(STYLE_EMPHASIS);
150
    console_set_style(STYLE_EMPHASIS);
134
    printf("%s", usr->prompt);
151
    printf("%s", usr->prompt);
135
    console_set_style(STYLE_NORMAL);
152
    console_set_style(STYLE_NORMAL);
136
 
153
 
137
    read_line(line, INPUT_MAX);
154
    read_line(line, INPUT_MAX);
138
    len = strlen(line);
-
 
139
    /* Make sure we don't have rubbish or a C/R happy user */
155
    /* Make sure we don't have rubbish or a C/R happy user */
140
    if (len == 0 || line[0] == '\n')
156
    if (str_cmp(line, "") == 0 || str_cmp(line, "\n") == 0)
141
        return;
157
        return;
142
    usr->line = strdup(line);
158
    usr->line = str_dup(line);
143
 
159
 
144
    return;
160
    return;
145
}
161
}
146
 
162