[216d6fc] | 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>
|
---|
[73878c1] | 35 | #include <io/console.h>
|
---|
| 36 | #include <io/keycode.h>
|
---|
| 37 | #include <io/style.h>
|
---|
[7e0cb78] | 38 | #include <io/color.h>
|
---|
[3bf907a] | 39 | #include <vfs/vfs.h>
|
---|
[6071a8f] | 40 | #include <errno.h>
|
---|
[da2bd08] | 41 | #include <assert.h>
|
---|
[6071a8f] | 42 | #include <bool.h>
|
---|
[216d6fc] | 43 |
|
---|
| 44 | #include "config.h"
|
---|
| 45 | #include "util.h"
|
---|
| 46 | #include "scli.h"
|
---|
| 47 | #include "input.h"
|
---|
| 48 | #include "errors.h"
|
---|
| 49 | #include "exec.h"
|
---|
| 50 |
|
---|
[da2bd08] | 51 | #define HISTORY_LEN 10
|
---|
| 52 |
|
---|
[ed372da] | 53 | /** Text input field. */
|
---|
[19528516] | 54 | typedef struct {
|
---|
[ed372da] | 55 | /** Buffer holding text currently being edited */
|
---|
[19528516] | 56 | wchar_t buffer[INPUT_MAX];
|
---|
[ed372da] | 57 | /** Screen coordinates of the top-left corner of the text field */
|
---|
[19528516] | 58 | int col0, row0;
|
---|
[ed372da] | 59 | /** Screen dimensions */
|
---|
[19528516] | 60 | int con_cols, con_rows;
|
---|
[ed372da] | 61 | /** Number of characters in @c buffer */
|
---|
[19528516] | 62 | int nc;
|
---|
[ed372da] | 63 | /** Caret position within buffer */
|
---|
[19528516] | 64 | int pos;
|
---|
[ed372da] | 65 | /** Selection mark position within buffer */
|
---|
[7e0cb78] | 66 | int sel_start;
|
---|
[da2bd08] | 67 |
|
---|
[ed372da] | 68 | /** History (dynamically allocated strings) */
|
---|
[da2bd08] | 69 | char *history[1 + HISTORY_LEN];
|
---|
[ed372da] | 70 | /** Number of entries in @c history, not counting [0] */
|
---|
[da2bd08] | 71 | int hnum;
|
---|
[ed372da] | 72 | /** Current position in history */
|
---|
[da2bd08] | 73 | int hpos;
|
---|
[ed372da] | 74 | /** Exit flag */
|
---|
[7e0cb78] | 75 | bool done;
|
---|
[19528516] | 76 | } tinput_t;
|
---|
| 77 |
|
---|
[ed372da] | 78 | /** Seek direction */
|
---|
[19528516] | 79 | typedef enum {
|
---|
| 80 | seek_backward = -1,
|
---|
| 81 | seek_forward = 1
|
---|
| 82 | } seek_dir_t;
|
---|
| 83 |
|
---|
| 84 | static tinput_t tinput;
|
---|
| 85 |
|
---|
| 86 | static char *tinput_read(tinput_t *ti);
|
---|
[7e0cb78] | 87 | static void tinput_sel_get_bounds(tinput_t *ti, int *sa, int *sb);
|
---|
| 88 | static bool tinput_sel_active(tinput_t *ti);
|
---|
| 89 | static void tinput_sel_delete(tinput_t *ti);
|
---|
| 90 | static void tinput_key_ctrl(tinput_t *ti, console_event_t *ev);
|
---|
| 91 | static void tinput_key_shift(tinput_t *ti, console_event_t *ev);
|
---|
| 92 | static void tinput_key_ctrl_shift(tinput_t *ti, console_event_t *ev);
|
---|
| 93 | static void tinput_key_unmod(tinput_t *ti, console_event_t *ev);
|
---|
| 94 | static void tinput_pre_seek(tinput_t *ti, bool shift_held);
|
---|
| 95 | static void tinput_post_seek(tinput_t *ti, bool shift_held);
|
---|
[88944695] | 96 |
|
---|
[216d6fc] | 97 | /* Tokenizes input from console, sees if the first word is a built-in, if so
|
---|
| 98 | * invokes the built-in entry point (a[0]) passing all arguments in a[] to
|
---|
| 99 | * the handler */
|
---|
| 100 | int tok_input(cliuser_t *usr)
|
---|
| 101 | {
|
---|
| 102 | char *cmd[WORD_MAX];
|
---|
| 103 | int n = 0, i = 0;
|
---|
| 104 | int rc = 0;
|
---|
| 105 | char *tmp;
|
---|
| 106 |
|
---|
| 107 | if (NULL == usr->line)
|
---|
| 108 | return CL_EFAIL;
|
---|
| 109 |
|
---|
[095003a8] | 110 | tmp = str_dup(usr->line);
|
---|
[216d6fc] | 111 |
|
---|
[4cc0c9ee] | 112 | cmd[n] = strtok(tmp, " ");
|
---|
[216d6fc] | 113 | while (cmd[n] && n < WORD_MAX) {
|
---|
[4cc0c9ee] | 114 | cmd[++n] = strtok(NULL, " ");
|
---|
[216d6fc] | 115 | }
|
---|
| 116 |
|
---|
| 117 | /* We have rubbish */
|
---|
| 118 | if (NULL == cmd[0]) {
|
---|
| 119 | rc = CL_ENOENT;
|
---|
| 120 | goto finit;
|
---|
| 121 | }
|
---|
| 122 |
|
---|
[d752cf4] | 123 | /* Its a builtin command ? */
|
---|
[216d6fc] | 124 | if ((i = (is_builtin(cmd[0]))) > -1) {
|
---|
| 125 | rc = run_builtin(i, cmd, usr);
|
---|
| 126 | goto finit;
|
---|
[d752cf4] | 127 | /* Its a module ? */
|
---|
[216d6fc] | 128 | } else if ((i = (is_module(cmd[0]))) > -1) {
|
---|
| 129 | rc = run_module(i, cmd);
|
---|
| 130 | goto finit;
|
---|
| 131 | }
|
---|
| 132 |
|
---|
[d752cf4] | 133 | /* See what try_exec thinks of it */
|
---|
| 134 | rc = try_exec(cmd[0], cmd);
|
---|
| 135 |
|
---|
[216d6fc] | 136 | finit:
|
---|
| 137 | if (NULL != usr->line) {
|
---|
| 138 | free(usr->line);
|
---|
| 139 | usr->line = (char *) NULL;
|
---|
| 140 | }
|
---|
| 141 | if (NULL != tmp)
|
---|
| 142 | free(tmp);
|
---|
| 143 |
|
---|
| 144 | return rc;
|
---|
| 145 | }
|
---|
| 146 |
|
---|
[19528516] | 147 | static void tinput_display_tail(tinput_t *ti, int start, int pad)
|
---|
| 148 | {
|
---|
[7e0cb78] | 149 | static wchar_t dbuf[INPUT_MAX + 1];
|
---|
| 150 | int sa, sb;
|
---|
| 151 | int i, p;
|
---|
| 152 |
|
---|
| 153 | tinput_sel_get_bounds(ti, &sa, &sb);
|
---|
[19528516] | 154 |
|
---|
[e228280] | 155 | console_goto(fphone(stdout), (ti->col0 + start) % ti->con_cols,
|
---|
| 156 | ti->row0 + (ti->col0 + start) / ti->con_cols);
|
---|
[7e0cb78] | 157 | console_set_color(fphone(stdout), COLOR_BLACK, COLOR_WHITE, 0);
|
---|
| 158 |
|
---|
| 159 | p = start;
|
---|
| 160 | if (p < sa) {
|
---|
| 161 | memcpy(dbuf, ti->buffer + p, (sa - p) * sizeof(wchar_t));
|
---|
| 162 | dbuf[sa - p] = '\0';
|
---|
| 163 | printf("%ls", dbuf);
|
---|
| 164 | p = sa;
|
---|
| 165 | }
|
---|
| 166 |
|
---|
| 167 | if (p < sb) {
|
---|
| 168 | fflush(stdout);
|
---|
| 169 | console_set_color(fphone(stdout), COLOR_BLACK, COLOR_RED, 0);
|
---|
| 170 | memcpy(dbuf, ti->buffer + p,
|
---|
| 171 | (sb - p) * sizeof(wchar_t));
|
---|
| 172 | dbuf[sb - p] = '\0';
|
---|
| 173 | printf("%ls", dbuf);
|
---|
| 174 | p = sb;
|
---|
| 175 | }
|
---|
| 176 |
|
---|
| 177 | fflush(stdout);
|
---|
| 178 | console_set_color(fphone(stdout), COLOR_BLACK, COLOR_WHITE, 0);
|
---|
| 179 |
|
---|
| 180 | if (p < ti->nc) {
|
---|
| 181 | memcpy(dbuf, ti->buffer + p,
|
---|
| 182 | (ti->nc - p) * sizeof(wchar_t));
|
---|
| 183 | dbuf[ti->nc - p] = '\0';
|
---|
| 184 | printf("%ls", dbuf);
|
---|
| 185 | }
|
---|
| 186 |
|
---|
[19528516] | 187 | for (i = 0; i < pad; ++i)
|
---|
| 188 | putchar(' ');
|
---|
| 189 | fflush(stdout);
|
---|
| 190 | }
|
---|
| 191 |
|
---|
[da2bd08] | 192 | static char *tinput_get_str(tinput_t *ti)
|
---|
| 193 | {
|
---|
[b67c7d64] | 194 | return wstr_to_astr(ti->buffer);
|
---|
[da2bd08] | 195 | }
|
---|
| 196 |
|
---|
[19528516] | 197 | static void tinput_position_caret(tinput_t *ti)
|
---|
| 198 | {
|
---|
[e228280] | 199 | console_goto(fphone(stdout), (ti->col0 + ti->pos) % ti->con_cols,
|
---|
| 200 | ti->row0 + (ti->col0 + ti->pos) / ti->con_cols);
|
---|
| 201 | }
|
---|
| 202 |
|
---|
| 203 | /** Update row0 in case the screen could have scrolled. */
|
---|
| 204 | static void tinput_update_origin(tinput_t *ti)
|
---|
| 205 | {
|
---|
| 206 | int width, rows;
|
---|
| 207 |
|
---|
| 208 | width = ti->col0 + ti->nc;
|
---|
| 209 | rows = (width / ti->con_cols) + 1;
|
---|
[ed372da] | 210 |
|
---|
[e228280] | 211 | /* Update row0 if the screen scrolled. */
|
---|
| 212 | if (ti->row0 + rows > ti->con_rows)
|
---|
| 213 | ti->row0 = ti->con_rows - rows;
|
---|
[19528516] | 214 | }
|
---|
| 215 |
|
---|
| 216 | static void tinput_insert_char(tinput_t *ti, wchar_t c)
|
---|
| 217 | {
|
---|
| 218 | int i;
|
---|
[e228280] | 219 | int new_width, new_height;
|
---|
[19528516] | 220 |
|
---|
| 221 | if (ti->nc == INPUT_MAX)
|
---|
| 222 | return;
|
---|
| 223 |
|
---|
[e228280] | 224 | new_width = ti->col0 + ti->nc + 1;
|
---|
| 225 | if (new_width % ti->con_cols == 0) {
|
---|
| 226 | /* Advancing to new line. */
|
---|
| 227 | new_height = (new_width / ti->con_cols) + 1;
|
---|
| 228 | if (new_height >= ti->con_rows)
|
---|
| 229 | return; /* Disallow text longer than 1 page for now. */
|
---|
| 230 | }
|
---|
[19528516] | 231 |
|
---|
| 232 | for (i = ti->nc; i > ti->pos; --i)
|
---|
| 233 | ti->buffer[i] = ti->buffer[i - 1];
|
---|
| 234 |
|
---|
| 235 | ti->buffer[ti->pos] = c;
|
---|
| 236 | ti->pos += 1;
|
---|
| 237 | ti->nc += 1;
|
---|
| 238 | ti->buffer[ti->nc] = '\0';
|
---|
[7e0cb78] | 239 | ti->sel_start = ti->pos;
|
---|
[19528516] | 240 |
|
---|
| 241 | tinput_display_tail(ti, ti->pos - 1, 0);
|
---|
[e228280] | 242 | tinput_update_origin(ti);
|
---|
[19528516] | 243 | tinput_position_caret(ti);
|
---|
| 244 | }
|
---|
| 245 |
|
---|
| 246 | static void tinput_backspace(tinput_t *ti)
|
---|
| 247 | {
|
---|
| 248 | int i;
|
---|
| 249 |
|
---|
[7e0cb78] | 250 | if (tinput_sel_active(ti)) {
|
---|
| 251 | tinput_sel_delete(ti);
|
---|
| 252 | return;
|
---|
| 253 | }
|
---|
[ed372da] | 254 |
|
---|
[19528516] | 255 | if (ti->pos == 0)
|
---|
| 256 | return;
|
---|
| 257 |
|
---|
| 258 | for (i = ti->pos; i < ti->nc; ++i)
|
---|
| 259 | ti->buffer[i - 1] = ti->buffer[i];
|
---|
| 260 | ti->pos -= 1;
|
---|
| 261 | ti->nc -= 1;
|
---|
| 262 | ti->buffer[ti->nc] = '\0';
|
---|
[7e0cb78] | 263 | ti->sel_start = ti->pos;
|
---|
[19528516] | 264 |
|
---|
| 265 | tinput_display_tail(ti, ti->pos, 1);
|
---|
| 266 | tinput_position_caret(ti);
|
---|
| 267 | }
|
---|
| 268 |
|
---|
| 269 | static void tinput_delete(tinput_t *ti)
|
---|
| 270 | {
|
---|
[7e0cb78] | 271 | if (tinput_sel_active(ti)) {
|
---|
| 272 | tinput_sel_delete(ti);
|
---|
| 273 | return;
|
---|
| 274 | }
|
---|
| 275 |
|
---|
[19528516] | 276 | if (ti->pos == ti->nc)
|
---|
| 277 | return;
|
---|
| 278 |
|
---|
| 279 | ti->pos += 1;
|
---|
[7e0cb78] | 280 | ti->sel_start = ti->pos;
|
---|
| 281 |
|
---|
[19528516] | 282 | tinput_backspace(ti);
|
---|
| 283 | }
|
---|
| 284 |
|
---|
[7e0cb78] | 285 | static void tinput_seek_cell(tinput_t *ti, seek_dir_t dir, bool shift_held)
|
---|
[19528516] | 286 | {
|
---|
[7e0cb78] | 287 | tinput_pre_seek(ti, shift_held);
|
---|
| 288 |
|
---|
[025759c] | 289 | if (dir == seek_forward) {
|
---|
| 290 | if (ti->pos < ti->nc)
|
---|
| 291 | ti->pos += 1;
|
---|
| 292 | } else {
|
---|
| 293 | if (ti->pos > 0)
|
---|
| 294 | ti->pos -= 1;
|
---|
[19528516] | 295 | }
|
---|
[025759c] | 296 |
|
---|
[7e0cb78] | 297 | tinput_post_seek(ti, shift_held);
|
---|
[025759c] | 298 | }
|
---|
| 299 |
|
---|
[7e0cb78] | 300 | static void tinput_seek_word(tinput_t *ti, seek_dir_t dir, bool shift_held)
|
---|
[025759c] | 301 | {
|
---|
[7e0cb78] | 302 | tinput_pre_seek(ti, shift_held);
|
---|
| 303 |
|
---|
[025759c] | 304 | if (dir == seek_forward) {
|
---|
| 305 | if (ti->pos == ti->nc)
|
---|
| 306 | return;
|
---|
| 307 |
|
---|
| 308 | while (1) {
|
---|
| 309 | ti->pos += 1;
|
---|
| 310 |
|
---|
| 311 | if (ti->pos == ti->nc)
|
---|
| 312 | break;
|
---|
| 313 |
|
---|
| 314 | if (ti->buffer[ti->pos - 1] == ' ' &&
|
---|
| 315 | ti->buffer[ti->pos] != ' ')
|
---|
| 316 | break;
|
---|
| 317 | }
|
---|
| 318 | } else {
|
---|
| 319 | if (ti->pos == 0)
|
---|
| 320 | return;
|
---|
| 321 |
|
---|
| 322 | while (1) {
|
---|
| 323 | ti->pos -= 1;
|
---|
| 324 |
|
---|
| 325 | if (ti->pos == 0)
|
---|
| 326 | break;
|
---|
| 327 |
|
---|
| 328 | if (ti->buffer[ti->pos - 1] == ' ' &&
|
---|
| 329 | ti->buffer[ti->pos] != ' ')
|
---|
| 330 | break;
|
---|
| 331 | }
|
---|
| 332 |
|
---|
| 333 | }
|
---|
| 334 |
|
---|
[7e0cb78] | 335 | tinput_post_seek(ti, shift_held);
|
---|
[025759c] | 336 | }
|
---|
| 337 |
|
---|
[7e0cb78] | 338 | static void tinput_seek_vertical(tinput_t *ti, seek_dir_t dir, bool shift_held)
|
---|
[e228280] | 339 | {
|
---|
[7e0cb78] | 340 | tinput_pre_seek(ti, shift_held);
|
---|
| 341 |
|
---|
[3041fef1] | 342 | if (dir == seek_forward) {
|
---|
| 343 | if (ti->pos + ti->con_cols <= ti->nc)
|
---|
| 344 | ti->pos = ti->pos + ti->con_cols;
|
---|
| 345 | } else {
|
---|
| 346 | if (ti->pos - ti->con_cols >= 0)
|
---|
| 347 | ti->pos = ti->pos - ti->con_cols;
|
---|
| 348 | }
|
---|
[e228280] | 349 |
|
---|
[7e0cb78] | 350 | tinput_post_seek(ti, shift_held);
|
---|
[e228280] | 351 | }
|
---|
| 352 |
|
---|
[7e0cb78] | 353 | static void tinput_seek_max(tinput_t *ti, seek_dir_t dir, bool shift_held)
|
---|
[025759c] | 354 | {
|
---|
[7e0cb78] | 355 | tinput_pre_seek(ti, shift_held);
|
---|
| 356 |
|
---|
[025759c] | 357 | if (dir == seek_backward)
|
---|
| 358 | ti->pos = 0;
|
---|
| 359 | else
|
---|
| 360 | ti->pos = ti->nc;
|
---|
[19528516] | 361 |
|
---|
[7e0cb78] | 362 | tinput_post_seek(ti, shift_held);
|
---|
| 363 | }
|
---|
| 364 |
|
---|
| 365 | static void tinput_pre_seek(tinput_t *ti, bool shift_held)
|
---|
| 366 | {
|
---|
| 367 | if (tinput_sel_active(ti) && !shift_held) {
|
---|
| 368 | /* Unselect and redraw. */
|
---|
| 369 | ti->sel_start = ti->pos;
|
---|
| 370 | tinput_display_tail(ti, 0, 0);
|
---|
| 371 | tinput_position_caret(ti);
|
---|
| 372 | }
|
---|
| 373 | }
|
---|
| 374 |
|
---|
| 375 | static void tinput_post_seek(tinput_t *ti, bool shift_held)
|
---|
| 376 | {
|
---|
| 377 | if (shift_held) {
|
---|
| 378 | /* Selecting text. Need redraw. */
|
---|
| 379 | tinput_display_tail(ti, 0, 0);
|
---|
| 380 | } else {
|
---|
| 381 | /* Shift not held. Keep selection empty. */
|
---|
| 382 | ti->sel_start = ti->pos;
|
---|
| 383 | }
|
---|
[19528516] | 384 | tinput_position_caret(ti);
|
---|
| 385 | }
|
---|
| 386 |
|
---|
[da2bd08] | 387 | static void tinput_history_insert(tinput_t *ti, char *str)
|
---|
| 388 | {
|
---|
| 389 | int i;
|
---|
| 390 |
|
---|
| 391 | if (ti->hnum < HISTORY_LEN) {
|
---|
| 392 | ti->hnum += 1;
|
---|
| 393 | } else {
|
---|
| 394 | if (ti->history[HISTORY_LEN] != NULL)
|
---|
| 395 | free(ti->history[HISTORY_LEN]);
|
---|
| 396 | }
|
---|
| 397 |
|
---|
| 398 | for (i = ti->hnum; i > 1; --i)
|
---|
| 399 | ti->history[i] = ti->history[i - 1];
|
---|
| 400 |
|
---|
| 401 | ti->history[1] = str_dup(str);
|
---|
| 402 |
|
---|
| 403 | if (ti->history[0] != NULL) {
|
---|
| 404 | free(ti->history[0]);
|
---|
| 405 | ti->history[0] = NULL;
|
---|
| 406 | }
|
---|
| 407 | }
|
---|
| 408 |
|
---|
| 409 | static void tinput_set_str(tinput_t *ti, char *str)
|
---|
| 410 | {
|
---|
| 411 | str_to_wstr(ti->buffer, INPUT_MAX, str);
|
---|
| 412 | ti->nc = wstr_length(ti->buffer);
|
---|
| 413 | ti->pos = ti->nc;
|
---|
[7e0cb78] | 414 | ti->sel_start = ti->pos;
|
---|
| 415 | }
|
---|
| 416 |
|
---|
| 417 | static void tinput_sel_get_bounds(tinput_t *ti, int *sa, int *sb)
|
---|
| 418 | {
|
---|
| 419 | if (ti->sel_start < ti->pos) {
|
---|
| 420 | *sa = ti->sel_start;
|
---|
| 421 | *sb = ti->pos;
|
---|
| 422 | } else {
|
---|
| 423 | *sa = ti->pos;
|
---|
| 424 | *sb = ti->sel_start;
|
---|
| 425 | }
|
---|
| 426 | }
|
---|
| 427 |
|
---|
| 428 | static bool tinput_sel_active(tinput_t *ti)
|
---|
| 429 | {
|
---|
| 430 | return ti->sel_start != ti->pos;
|
---|
| 431 | }
|
---|
| 432 |
|
---|
| 433 | static void tinput_sel_delete(tinput_t *ti)
|
---|
| 434 | {
|
---|
| 435 | int sa, sb;
|
---|
| 436 |
|
---|
| 437 | tinput_sel_get_bounds(ti, &sa, &sb);
|
---|
| 438 | if (sa == sb)
|
---|
| 439 | return;
|
---|
| 440 |
|
---|
| 441 | memmove(ti->buffer + sa, ti->buffer + sb,
|
---|
| 442 | (ti->nc - sb) * sizeof(wchar_t));
|
---|
| 443 | ti->pos = ti->sel_start = sa;
|
---|
| 444 | ti->nc -= (sb - sa);
|
---|
| 445 | ti->buffer[ti->nc] = '\0';
|
---|
| 446 |
|
---|
| 447 | tinput_display_tail(ti, sa, sb - sa);
|
---|
| 448 | tinput_position_caret(ti);
|
---|
[da2bd08] | 449 | }
|
---|
| 450 |
|
---|
| 451 | static void tinput_history_seek(tinput_t *ti, int offs)
|
---|
| 452 | {
|
---|
| 453 | int pad;
|
---|
| 454 |
|
---|
| 455 | if (ti->hpos + offs < 0 || ti->hpos + offs > ti->hnum)
|
---|
| 456 | return;
|
---|
| 457 |
|
---|
| 458 | if (ti->history[ti->hpos] != NULL) {
|
---|
| 459 | free(ti->history[ti->hpos]);
|
---|
| 460 | ti->history[ti->hpos] = NULL;
|
---|
| 461 | }
|
---|
| 462 |
|
---|
| 463 | ti->history[ti->hpos] = tinput_get_str(ti);
|
---|
| 464 | ti->hpos += offs;
|
---|
| 465 |
|
---|
| 466 | pad = ti->nc - str_length(ti->history[ti->hpos]);
|
---|
| 467 | if (pad < 0) pad = 0;
|
---|
| 468 |
|
---|
| 469 | tinput_set_str(ti, ti->history[ti->hpos]);
|
---|
| 470 | tinput_display_tail(ti, 0, pad);
|
---|
[e228280] | 471 | tinput_update_origin(ti);
|
---|
[da2bd08] | 472 | tinput_position_caret(ti);
|
---|
| 473 | }
|
---|
| 474 |
|
---|
[ed372da] | 475 | /** Initialize text input field.
|
---|
| 476 | *
|
---|
| 477 | * Must be called before using the field. It clears the history.
|
---|
| 478 | */
|
---|
[da2bd08] | 479 | static void tinput_init(tinput_t *ti)
|
---|
| 480 | {
|
---|
| 481 | ti->hnum = 0;
|
---|
| 482 | ti->hpos = 0;
|
---|
| 483 | ti->history[0] = NULL;
|
---|
| 484 | }
|
---|
| 485 |
|
---|
[ed372da] | 486 | /** Read in one line of input. */
|
---|
[19528516] | 487 | static char *tinput_read(tinput_t *ti)
|
---|
[216d6fc] | 488 | {
|
---|
[73878c1] | 489 | console_event_t ev;
|
---|
[19528516] | 490 | char *str;
|
---|
| 491 |
|
---|
| 492 | fflush(stdout);
|
---|
| 493 |
|
---|
| 494 | if (console_get_size(fphone(stdin), &ti->con_cols, &ti->con_rows) != EOK)
|
---|
| 495 | return NULL;
|
---|
| 496 | if (console_get_pos(fphone(stdin), &ti->col0, &ti->row0) != EOK)
|
---|
| 497 | return NULL;
|
---|
| 498 |
|
---|
[7e0cb78] | 499 | ti->pos = ti->sel_start = 0;
|
---|
[19528516] | 500 | ti->nc = 0;
|
---|
[fd34f4e] | 501 | ti->buffer[0] = '\0';
|
---|
[7e0cb78] | 502 | ti->done = false;
|
---|
[216d6fc] | 503 |
|
---|
[7e0cb78] | 504 | while (!ti->done) {
|
---|
[56fa418] | 505 | fflush(stdout);
|
---|
[73878c1] | 506 | if (!console_get_event(fphone(stdin), &ev))
|
---|
[19528516] | 507 | return NULL;
|
---|
[7e0cb78] | 508 |
|
---|
[73878c1] | 509 | if (ev.type != KEY_PRESS)
|
---|
[56fa418] | 510 | continue;
|
---|
[19528516] | 511 |
|
---|
[025759c] | 512 | if ((ev.mods & KM_CTRL) != 0 &&
|
---|
| 513 | (ev.mods & (KM_ALT | KM_SHIFT)) == 0) {
|
---|
[7e0cb78] | 514 | tinput_key_ctrl(ti, &ev);
|
---|
| 515 | }
|
---|
| 516 |
|
---|
| 517 | if ((ev.mods & KM_SHIFT) != 0 &&
|
---|
| 518 | (ev.mods & (KM_CTRL | KM_ALT)) == 0) {
|
---|
| 519 | tinput_key_shift(ti, &ev);
|
---|
| 520 | }
|
---|
| 521 |
|
---|
| 522 | if ((ev.mods & KM_CTRL) != 0 &&
|
---|
| 523 | (ev.mods & KM_SHIFT) != 0 &&
|
---|
| 524 | (ev.mods & KM_ALT) == 0) {
|
---|
| 525 | tinput_key_ctrl_shift(ti, &ev);
|
---|
[025759c] | 526 | }
|
---|
[19528516] | 527 |
|
---|
[025759c] | 528 | if ((ev.mods & (KM_CTRL | KM_ALT | KM_SHIFT)) == 0) {
|
---|
[7e0cb78] | 529 | tinput_key_unmod(ti, &ev);
|
---|
[216d6fc] | 530 | }
|
---|
[025759c] | 531 |
|
---|
[56fa418] | 532 | if (ev.c >= ' ') {
|
---|
[7e0cb78] | 533 | tinput_sel_delete(ti);
|
---|
[19528516] | 534 | tinput_insert_char(ti, ev.c);
|
---|
[38c64e8] | 535 | }
|
---|
[216d6fc] | 536 | }
|
---|
[19528516] | 537 |
|
---|
[e228280] | 538 | ti->pos = ti->nc;
|
---|
| 539 | tinput_position_caret(ti);
|
---|
[216d6fc] | 540 | putchar('\n');
|
---|
[19528516] | 541 |
|
---|
[da2bd08] | 542 | str = tinput_get_str(ti);
|
---|
| 543 | if (str_cmp(str, "") != 0)
|
---|
| 544 | tinput_history_insert(ti, str);
|
---|
[19528516] | 545 |
|
---|
[da2bd08] | 546 | ti->hpos = 0;
|
---|
[19528516] | 547 |
|
---|
| 548 | return str;
|
---|
[216d6fc] | 549 | }
|
---|
| 550 |
|
---|
[7e0cb78] | 551 | static void tinput_key_ctrl(tinput_t *ti, console_event_t *ev)
|
---|
| 552 | {
|
---|
| 553 | switch (ev->key) {
|
---|
| 554 | case KC_LEFT:
|
---|
| 555 | tinput_seek_word(ti, seek_backward, false);
|
---|
| 556 | break;
|
---|
| 557 | case KC_RIGHT:
|
---|
| 558 | tinput_seek_word(ti, seek_forward, false);
|
---|
| 559 | break;
|
---|
| 560 | case KC_UP:
|
---|
| 561 | tinput_seek_vertical(ti, seek_backward, false);
|
---|
| 562 | break;
|
---|
| 563 | case KC_DOWN:
|
---|
| 564 | tinput_seek_vertical(ti, seek_forward, false);
|
---|
| 565 | break;
|
---|
| 566 | default:
|
---|
| 567 | break;
|
---|
| 568 | }
|
---|
| 569 | }
|
---|
| 570 |
|
---|
| 571 | static void tinput_key_ctrl_shift(tinput_t *ti, console_event_t *ev)
|
---|
| 572 | {
|
---|
| 573 | switch (ev->key) {
|
---|
| 574 | case KC_LEFT:
|
---|
| 575 | tinput_seek_word(ti, seek_backward, true);
|
---|
| 576 | break;
|
---|
| 577 | case KC_RIGHT:
|
---|
| 578 | tinput_seek_word(ti, seek_forward, true);
|
---|
| 579 | break;
|
---|
| 580 | case KC_UP:
|
---|
| 581 | tinput_seek_vertical(ti, seek_backward, true);
|
---|
| 582 | break;
|
---|
| 583 | case KC_DOWN:
|
---|
| 584 | tinput_seek_vertical(ti, seek_forward, true);
|
---|
| 585 | break;
|
---|
| 586 | default:
|
---|
| 587 | break;
|
---|
| 588 | }
|
---|
| 589 | }
|
---|
| 590 |
|
---|
| 591 | static void tinput_key_shift(tinput_t *ti, console_event_t *ev)
|
---|
| 592 | {
|
---|
| 593 | switch (ev->key) {
|
---|
| 594 | case KC_LEFT:
|
---|
| 595 | tinput_seek_cell(ti, seek_backward, true);
|
---|
| 596 | break;
|
---|
| 597 | case KC_RIGHT:
|
---|
| 598 | tinput_seek_cell(ti, seek_forward, true);
|
---|
| 599 | break;
|
---|
| 600 | case KC_UP:
|
---|
| 601 | tinput_seek_vertical(ti, seek_backward, true);
|
---|
| 602 | break;
|
---|
| 603 | case KC_DOWN:
|
---|
| 604 | tinput_seek_vertical(ti, seek_forward, true);
|
---|
| 605 | break;
|
---|
| 606 | case KC_HOME:
|
---|
| 607 | tinput_seek_max(ti, seek_backward, true);
|
---|
| 608 | break;
|
---|
| 609 | case KC_END:
|
---|
| 610 | tinput_seek_max(ti, seek_forward, true);
|
---|
| 611 | break;
|
---|
| 612 | default:
|
---|
| 613 | break;
|
---|
| 614 | }
|
---|
| 615 | }
|
---|
| 616 |
|
---|
| 617 | static void tinput_key_unmod(tinput_t *ti, console_event_t *ev)
|
---|
| 618 | {
|
---|
| 619 | switch (ev->key) {
|
---|
| 620 | case KC_ENTER:
|
---|
| 621 | case KC_NENTER:
|
---|
| 622 | ti->done = true;
|
---|
| 623 | break;
|
---|
| 624 | case KC_BACKSPACE:
|
---|
| 625 | tinput_backspace(ti);
|
---|
| 626 | break;
|
---|
| 627 | case KC_DELETE:
|
---|
| 628 | tinput_delete(ti);
|
---|
| 629 | break;
|
---|
| 630 | case KC_LEFT:
|
---|
| 631 | tinput_seek_cell(ti, seek_backward, false);
|
---|
| 632 | break;
|
---|
| 633 | case KC_RIGHT:
|
---|
| 634 | tinput_seek_cell(ti, seek_forward, false);
|
---|
| 635 | break;
|
---|
| 636 | case KC_HOME:
|
---|
| 637 | tinput_seek_max(ti, seek_backward, false);
|
---|
| 638 | break;
|
---|
| 639 | case KC_END:
|
---|
| 640 | tinput_seek_max(ti, seek_forward, false);
|
---|
| 641 | break;
|
---|
| 642 | case KC_UP:
|
---|
| 643 | tinput_history_seek(ti, +1);
|
---|
| 644 | break;
|
---|
| 645 | case KC_DOWN:
|
---|
| 646 | tinput_history_seek(ti, -1);
|
---|
| 647 | break;
|
---|
| 648 | default:
|
---|
| 649 | break;
|
---|
| 650 | }
|
---|
| 651 | }
|
---|
| 652 |
|
---|
[216d6fc] | 653 | void get_input(cliuser_t *usr)
|
---|
| 654 | {
|
---|
[19528516] | 655 | char *str;
|
---|
[216d6fc] | 656 |
|
---|
[ef8bcc6] | 657 | fflush(stdout);
|
---|
[73878c1] | 658 | console_set_style(fphone(stdout), STYLE_EMPHASIS);
|
---|
[216d6fc] | 659 | printf("%s", usr->prompt);
|
---|
[ef8bcc6] | 660 | fflush(stdout);
|
---|
[73878c1] | 661 | console_set_style(fphone(stdout), STYLE_NORMAL);
|
---|
[9805cde] | 662 |
|
---|
[19528516] | 663 | str = tinput_read(&tinput);
|
---|
| 664 |
|
---|
| 665 | /* Check for empty input. */
|
---|
| 666 | if (str_cmp(str, "") == 0) {
|
---|
| 667 | free(str);
|
---|
[216d6fc] | 668 | return;
|
---|
[19528516] | 669 | }
|
---|
[216d6fc] | 670 |
|
---|
[19528516] | 671 | usr->line = str;
|
---|
[216d6fc] | 672 | return;
|
---|
| 673 | }
|
---|
[da2bd08] | 674 |
|
---|
| 675 | void input_init(void)
|
---|
| 676 | {
|
---|
| 677 | tinput_init(&tinput);
|
---|
| 678 | }
|
---|