Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • uspace/app/bdsh/input.c

    ref8bcc6 r5db9084  
    11/* Copyright (c) 2008, Tim Post <tinkertim@gmail.com>
    22 * All rights reserved.
    3  * Copyright (c) 2008, Jiri Svoboda - All Rights Reserved
    43 *
    54 * Redistribution and use in source and binary forms, with or without
     
    3231#include <stdio.h>
    3332#include <stdlib.h>
    34 #include <string.h>
     33#include <str.h>
    3534#include <io/console.h>
    3635#include <io/keycode.h>
    3736#include <io/style.h>
     37#include <io/color.h>
    3838#include <vfs/vfs.h>
     39#include <clipboard.h>
     40#include <macros.h>
    3941#include <errno.h>
     42#include <assert.h>
    4043#include <bool.h>
     44#include <tinput.h>
    4145
    4246#include "config.h"
     
    4751#include "exec.h"
    4852
    49 static void read_line(char *, int);
     53extern volatile unsigned int cli_quit;
     54
     55/** Text input field. */
     56static tinput_t *tinput;
    5057
    5158/* Tokenizes input from console, sees if the first word is a built-in, if so
     
    99106}
    100107
    101 static void read_line(char *buffer, int n)
    102 {
    103         console_event_t ev;
    104         size_t offs, otmp;
    105         wchar_t dec;
    106 
    107         offs = 0;
    108         while (true) {
    109                 fflush(stdout);
    110                 if (!console_get_event(fphone(stdin), &ev))
    111                         return;
    112                
    113                 if (ev.type != KEY_PRESS)
    114                         continue;
    115                
    116                 if (ev.key == KC_ENTER || ev.key == KC_NENTER)
    117                         break;
    118                 if (ev.key == KC_BACKSPACE) {
    119                         if (offs > 0) {
    120                                 /*
    121                                  * Back up until we reach valid start of
    122                                  * character.
    123                                  */
    124                                 while (offs > 0) {
    125                                         --offs; otmp = offs;
    126                                         dec = str_decode(buffer, &otmp, n);
    127                                         if (dec != U_SPECIAL)
    128                                                 break;
    129                                 }
    130                                 putchar('\b');
    131                         }
    132                         continue;
    133                 }
    134                 if (ev.c >= ' ') {
    135                         if (chr_encode(ev.c, buffer, &offs, n - 1) == EOK)
    136                                 putchar(ev.c);
    137                 }
    138         }
    139         putchar('\n');
    140         buffer[offs] = '\0';
    141 }
    142 
    143 /* TODO:
    144  * Implement something like editline() / readline(), if even
    145  * just for command history and making arrows work. */
    146108void get_input(cliuser_t *usr)
    147109{
    148         char line[INPUT_MAX];
     110        char *str;
     111        int rc;
    149112
    150113        fflush(stdout);
     
    154117        console_set_style(fphone(stdout), STYLE_NORMAL);
    155118
    156         read_line(line, INPUT_MAX);
    157         /* Make sure we don't have rubbish or a C/R happy user */
    158         if (str_cmp(line, "") == 0 || str_cmp(line, "\n") == 0)
     119        rc = tinput_read(tinput, &str);
     120        if (rc == ENOENT) {
     121                /* User requested exit */
     122                cli_quit = 1;
     123                putchar('\n');
    159124                return;
    160         usr->line = str_dup(line);
     125        }
    161126
     127        if (rc != EOK) {
     128                /* Error in communication with console */
     129                return;
     130        }
     131
     132        /* Check for empty input. */
     133        if (str_cmp(str, "") == 0) {
     134                free(str);
     135                return;
     136        }
     137
     138        usr->line = str;
    162139        return;
    163140}
    164141
     142int input_init(void)
     143{
     144        tinput = tinput_new();
     145        if (tinput == NULL) {
     146                printf("Failed to initialize input.\n");
     147                return 1;
     148        }
     149
     150        return 0;
     151}
Note: See TracChangeset for help on using the changeset viewer.