Changeset da2bd08 in mainline


Ignore:
Timestamp:
2009-11-29T14:53:18Z (14 years ago)
Author:
Jiri Svoboda <jiri@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
272f88f0, 8b5001b
Parents:
025759c
Message:

Input history.

Location:
uspace
Files:
5 edited

Legend:

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

    r025759c rda2bd08  
    3838#include <vfs/vfs.h>
    3939#include <errno.h>
     40#include <assert.h>
    4041#include <bool.h>
    4142
     
    4748#include "exec.h"
    4849
     50#define HISTORY_LEN 10
     51
    4952typedef struct {
    5053        wchar_t buffer[INPUT_MAX];
     
    5356        int nc;
    5457        int pos;
     58
     59        char *history[1 + HISTORY_LEN];
     60        int hnum;
     61        int hpos;
    5562} tinput_t;
    5663
     
    125132}
    126133
     134static char *tinput_get_str(tinput_t *ti)
     135{
     136        char *str;
     137
     138        str = malloc(STR_BOUNDS(ti->nc) + 1);
     139        if (str == NULL)
     140                return NULL;
     141
     142        wstr_nstr(str, ti->buffer, STR_BOUNDS(ti->nc) + 1);
     143
     144        return str;
     145}
     146
    127147static void tinput_position_caret(tinput_t *ti)
    128148{
     
    235255
    236256        tinput_position_caret(ti);
     257}
     258
     259static void tinput_history_insert(tinput_t *ti, char *str)
     260{
     261        int i;
     262
     263        if (ti->hnum < HISTORY_LEN) {
     264                ti->hnum += 1;
     265        } else {
     266                if (ti->history[HISTORY_LEN] != NULL)
     267                        free(ti->history[HISTORY_LEN]);
     268        }
     269
     270        for (i = ti->hnum; i > 1; --i)
     271                ti->history[i] = ti->history[i - 1];
     272
     273        ti->history[1] = str_dup(str);
     274
     275        if (ti->history[0] != NULL) {
     276                free(ti->history[0]);
     277                ti->history[0] = NULL;
     278        }
     279}
     280
     281static void tinput_set_str(tinput_t *ti, char *str)
     282{
     283        str_to_wstr(ti->buffer, INPUT_MAX, str);
     284        ti->nc = wstr_length(ti->buffer);
     285        ti->pos = ti->nc;
     286}
     287
     288static void tinput_history_seek(tinput_t *ti, int offs)
     289{
     290        int pad;
     291
     292        if (ti->hpos + offs < 0 || ti->hpos + offs > ti->hnum)
     293                return;
     294
     295        if (ti->history[ti->hpos] != NULL) {
     296                free(ti->history[ti->hpos]);
     297                ti->history[ti->hpos] = NULL;
     298        }
     299
     300        ti->history[ti->hpos] = tinput_get_str(ti);
     301        ti->hpos += offs;
     302
     303        pad = ti->nc - str_length(ti->history[ti->hpos]);
     304        if (pad < 0) pad = 0;
     305
     306        tinput_set_str(ti, ti->history[ti->hpos]);
     307        tinput_display_tail(ti, 0, pad);
     308        tinput_position_caret(ti);
     309}
     310
     311static void tinput_init(tinput_t *ti)
     312{
     313        ti->hnum = 0;
     314        ti->hpos = 0;
     315        ti->history[0] = NULL;
    237316}
    238317
     
    295374                                tinput_seek_max(ti, seek_forward);
    296375                                break;
     376                        case KC_UP:
     377                                tinput_history_seek(ti, +1);
     378                                break;
     379                        case KC_DOWN:
     380                                tinput_history_seek(ti, -1);
     381                                break;
    297382                        }
    298383                }
     
    306391        putchar('\n');
    307392
    308         ti->buffer[ti->nc] = '\0';
    309         str = malloc(STR_BOUNDS(ti->nc) + 1);
    310         if (str == NULL)
    311                 return NULL;
    312 
    313         wstr_nstr(str, ti->buffer, STR_BOUNDS(ti->nc) + 1);
     393        str = tinput_get_str(ti);
     394        if (str_cmp(str, "") != 0)
     395                tinput_history_insert(ti, str);
     396
     397        ti->hpos = 0;
    314398
    315399        return str;
     
    337421        return;
    338422}
     423
     424void input_init(void)
     425{
     426        tinput_init(&tinput);
     427}
  • uspace/app/bdsh/input.h

    r025759c rda2bd08  
    88extern void get_input(cliuser_t *);
    99extern int tok_input(cliuser_t *);
     10extern void input_init(void);
    1011
    1112#endif
  • uspace/app/bdsh/scli.c

    r025759c rda2bd08  
    6464        usr->prompt = (char *) NULL;
    6565        usr->lasterr = 0;
     66
     67        input_init();
     68
    6669        return (int) cli_set_prompt(usr);
    6770}
  • uspace/lib/libc/generic/string.c

    r025759c rda2bd08  
    583583}
    584584
     585/** Convert string to wide string.
     586 *
     587 * Convert string @a src to wide string. The output is written to the
     588 * buffer specified by @a dest and @a size, which must have non-zero
     589 * size. The output will always be null-terminated.
     590 *
     591 * @param dest  Destination buffer.
     592 * @param dlen  Length of destination buffer (number of wchars).
     593 * @param src   Source string.
     594 */
     595void str_to_wstr(wchar_t *dest, size_t dlen, const char *src)
     596{
     597        size_t offset;
     598        size_t di;
     599        wchar_t c;
     600
     601        assert(dlen > 0);
     602
     603        offset = 0;
     604        di = 0;
     605
     606        do {
     607                if (di >= dlen - 1)
     608                        break;
     609
     610                c = str_decode(src, &offset, STR_NO_LIMIT);
     611                dest[di++] = c;
     612        } while (c != '\0');
     613
     614        dest[dlen - 1] = '\0';
     615}
     616
    585617/** Find first occurence of character in string.
    586618 *
  • uspace/lib/libc/include/string.h

    r025759c rda2bd08  
    7474
    7575extern void wstr_nstr(char *dst, const wchar_t *src, size_t size);
     76extern void str_to_wstr(wchar_t *dest, size_t dlen, const char *src);
    7677
    7778extern char *str_chr(const char *str, wchar_t ch);
Note: See TracChangeset for help on using the changeset viewer.