source: mainline/uspace/app/bdsh/input.c@ 095003a8

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 095003a8 was 095003a8, checked in by Jiri Svoboda <jirik.svoboda@…>, 16 years ago

strdup() → str_dup()

  • Property mode set to 100644
File size: 4.1 KB
RevLine 
[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>
35#include <io/stream.h>
[9805cde]36#include <console.h>
[56fa418]37#include <kbd/kbd.h>
38#include <kbd/keycode.h>
[6071a8f]39#include <errno.h>
40#include <bool.h>
[216d6fc]41
42#include "config.h"
43#include "util.h"
44#include "scli.h"
45#include "input.h"
46#include "errors.h"
47#include "exec.h"
48
[88944695]49static void read_line(char *, int);
50
[216d6fc]51/* Tokenizes input from console, sees if the first word is a built-in, if so
52 * invokes the built-in entry point (a[0]) passing all arguments in a[] to
53 * the handler */
54int tok_input(cliuser_t *usr)
55{
56 char *cmd[WORD_MAX];
57 int n = 0, i = 0;
58 int rc = 0;
59 char *tmp;
60
61 if (NULL == usr->line)
62 return CL_EFAIL;
63
[095003a8]64 tmp = str_dup(usr->line);
[216d6fc]65
[4cc0c9ee]66 cmd[n] = strtok(tmp, " ");
[216d6fc]67 while (cmd[n] && n < WORD_MAX) {
[4cc0c9ee]68 cmd[++n] = strtok(NULL, " ");
[216d6fc]69 }
70
71 /* We have rubbish */
72 if (NULL == cmd[0]) {
73 rc = CL_ENOENT;
74 goto finit;
75 }
76
[d752cf4]77 /* Its a builtin command ? */
[216d6fc]78 if ((i = (is_builtin(cmd[0]))) > -1) {
79 rc = run_builtin(i, cmd, usr);
80 goto finit;
[d752cf4]81 /* Its a module ? */
[216d6fc]82 } else if ((i = (is_module(cmd[0]))) > -1) {
83 rc = run_module(i, cmd);
84 goto finit;
85 }
86
[d752cf4]87 /* See what try_exec thinks of it */
88 rc = try_exec(cmd[0], cmd);
89
[216d6fc]90finit:
91 if (NULL != usr->line) {
92 free(usr->line);
93 usr->line = (char *) NULL;
94 }
95 if (NULL != tmp)
96 free(tmp);
97
98 return rc;
99}
100
[88944695]101static void read_line(char *buffer, int n)
[216d6fc]102{
[56fa418]103 kbd_event_t ev;
[6071a8f]104 size_t offs, otmp;
105 wchar_t dec;
[216d6fc]106
[6071a8f]107 offs = 0;
108 while (true) {
[56fa418]109 fflush(stdout);
110 if (kbd_get_event(&ev) < 0)
[216d6fc]111 return;
[56fa418]112 if (ev.type == KE_RELEASE)
113 continue;
114
115 if (ev.key == KC_ENTER || ev.key == KC_NENTER)
[216d6fc]116 break;
[56fa418]117 if (ev.key == KC_BACKSPACE) {
[6071a8f]118 if (offs > 0) {
119 /*
120 * Back up until we reach valid start of
121 * character.
122 */
123 while (offs > 0) {
124 --offs; otmp = offs;
125 dec = str_decode(buffer, &otmp, n);
126 if (dec != U_SPECIAL)
127 break;
128 }
[216d6fc]129 putchar('\b');
130 }
131 continue;
132 }
[56fa418]133 if (ev.c >= ' ') {
134 //putchar(ev.c);
[6071a8f]135 if (chr_encode(ev.c, buffer, &offs, n - 1) == EOK)
136 console_putchar(ev.c);
[38c64e8]137 }
[216d6fc]138 }
139 putchar('\n');
[6071a8f]140 buffer[offs] = '\0';
[216d6fc]141}
142
[88944695]143/* TODO:
144 * Implement something like editline() / readline(), if even
145 * just for command history and making arrows work. */
[216d6fc]146void get_input(cliuser_t *usr)
147{
148 char line[INPUT_MAX];
149
[9805cde]150 console_set_style(STYLE_EMPHASIS);
[216d6fc]151 printf("%s", usr->prompt);
[9805cde]152 console_set_style(STYLE_NORMAL);
153
[216d6fc]154 read_line(line, INPUT_MAX);
155 /* Make sure we don't have rubbish or a C/R happy user */
[92fd52d7]156 if (str_cmp(line, "") == 0 || str_cmp(line, "\n") == 0)
[216d6fc]157 return;
[095003a8]158 usr->line = str_dup(line);
[216d6fc]159
160 return;
161}
162
Note: See TracBrowser for help on using the repository browser.