Ignore:
File:
1 edited

Legend:

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

    r5db9084 ref8bcc6  
    11/* Copyright (c) 2008, Tim Post <tinkertim@gmail.com>
    22 * All rights reserved.
     3 * Copyright (c) 2008, Jiri Svoboda - All Rights Reserved
    34 *
    45 * Redistribution and use in source and binary forms, with or without
     
    3132#include <stdio.h>
    3233#include <stdlib.h>
    33 #include <str.h>
     34#include <string.h>
    3435#include <io/console.h>
    3536#include <io/keycode.h>
    3637#include <io/style.h>
    37 #include <io/color.h>
    3838#include <vfs/vfs.h>
    39 #include <clipboard.h>
    40 #include <macros.h>
    4139#include <errno.h>
    42 #include <assert.h>
    4340#include <bool.h>
    44 #include <tinput.h>
    4541
    4642#include "config.h"
     
    5147#include "exec.h"
    5248
    53 extern volatile unsigned int cli_quit;
    54 
    55 /** Text input field. */
    56 static tinput_t *tinput;
     49static void read_line(char *, int);
    5750
    5851/* Tokenizes input from console, sees if the first word is a built-in, if so
     
    10699}
    107100
     101static 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. */
    108146void get_input(cliuser_t *usr)
    109147{
    110         char *str;
    111         int rc;
     148        char line[INPUT_MAX];
    112149
    113150        fflush(stdout);
     
    117154        console_set_style(fphone(stdout), STYLE_NORMAL);
    118155
    119         rc = tinput_read(tinput, &str);
    120         if (rc == ENOENT) {
    121                 /* User requested exit */
    122                 cli_quit = 1;
    123                 putchar('\n');
     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)
    124159                return;
    125         }
     160        usr->line = str_dup(line);
    126161
    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;
    139162        return;
    140163}
    141164
    142 int 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.